Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | /* * IP_MASQ_USER user space control module * * * $Id: ip_masq_user.c,v 1.1.2.3 1999/11/16 06:33:51 davem Exp $ */ #include <linux/config.h> #include <linux/module.h> #include <linux/types.h> #include <linux/kernel.h> #include <linux/errno.h> #include <linux/skbuff.h> #include <asm/system.h> #include <linux/stat.h> #include <linux/proc_fs.h> #include <linux/in.h> #include <linux/ip.h> #include <linux/inet.h> #include <linux/init.h> #include <net/protocol.h> #include <net/icmp.h> #include <net/tcp.h> #include <net/udp.h> #include <net/checksum.h> #include <net/ip_masq.h> #include <net/ip_masq_mod.h> #include <linux/sysctl.h> #include <linux/ip_fw.h> #include <linux/ip_masq.h> /* * Debug level */ static int debug=0; MODULE_PARM(ports, "1-" __MODULE_STRING(MAX_MASQ_APP_PORTS) "i"); MODULE_PARM(debug, "i"); /* static int check_5uple (struct ip_masq_user *ums) { return 0; } */ static void masq_user_k2u(const struct ip_masq *ms, struct ip_masq_user *ums) { ums->protocol = ms->protocol; ums->daddr = ms->daddr; ums->dport = ms->dport; ums->maddr = ms->maddr; ums->mport = ms->mport; ums->saddr = ms->saddr; ums->sport = ms->sport; ums->timeout = ms->timeout; } static int ip_masq_user_maddr(struct ip_masq_user *ums) { struct device *dev; struct rtable *rt; int ret = -EINVAL; u32 rt_daddr, rt_saddr; u32 tos; /* * Did specify masq address. */ if (ums->maddr) return 0; /* * Select address to use for routing query */ rt_daddr = ums->rt_daddr? ums->rt_daddr : ums->daddr; rt_saddr = ums->rt_saddr? ums->rt_saddr : ums->saddr; /* * No address for routing, cannot continue */ if (rt_daddr == 0) { IP_MASQ_DEBUG(1-debug, "cannot setup maddr with daddr=%lX, rt_addr=%lX\n", ntohl(ums->daddr), ntohl(ums->rt_daddr)); return -EINVAL; } /* * Find out rt device */ rt_saddr = 0; tos = RT_TOS(ums->ip_tos) | RTO_CONN; if ((ret=ip_route_output(&rt, rt_daddr, rt_saddr, tos, 0 /* dev */))) { IP_MASQ_DEBUG(0-debug, "could not setup maddr for routing daddr=%lX, saddr=%lX\n", ntohl(rt_daddr), ntohl(rt_saddr)); return ret; } dev = rt->u.dst.dev; ums->maddr = rt->rt_src; /* Per Alexey */ IP_MASQ_DEBUG(1-debug, "did setup maddr=%lX\n", ntohl(ums->maddr)); ip_rt_put(rt); return 0; } /* * Create new entry (from uspace) */ static int ip_masq_user_new(struct ip_masq_user *ums) { struct ip_masq *ms = NULL; unsigned mflags = 0; int ret; if (masq_proto_num (ums->protocol) == -1) { return EPROTONOSUPPORT; } if (ums->dport == 0) { ums->flags |= IP_MASQ_USER_F_LISTEN; } if (ums->flags | IP_MASQ_USER_F_LISTEN) { if ((ums->saddr == 0) || (ums->sport == 0)) { return EINVAL; } mflags |= (IP_MASQ_F_NO_DPORT|IP_MASQ_F_NO_DADDR); } if ((ret = ip_masq_user_maddr(ums)) < 0) { return -ret; } mflags |= IP_MASQ_F_USER; ms = ip_masq_new(ums->protocol, ums->maddr, ums->mport, ums->saddr, ums->sport, ums->daddr, ums->dport, mflags); if (ms == NULL) { /* * FIXME: ip_masq_new() should return errno */ return EBUSY; } /* * Setup timeouts for this new entry */ if (ums->timeout) { ms->timeout = ums->timeout; } else if (ums->flags | IP_MASQ_USER_F_LISTEN) { ip_masq_listen(ms); } masq_user_k2u(ms, ums); ip_masq_put(ms); return 0; } /* * Delete existing entry */ static int ip_masq_user_del(struct ip_masq_user *ums) { struct ip_masq *ms=NULL; if (masq_proto_num (ums->protocol) == -1) { return EPROTONOSUPPORT; } start_bh_atomic(); if (ums->mport && ums->maddr) { ms = ip_masq_in_get(ums->protocol, ums->daddr, ums->dport, ums->maddr, ums->mport); end_bh_atomic(); } else if (ums->sport && ums->saddr) { ms = ip_masq_out_get(ums->protocol, ums->saddr, ums->sport, ums->daddr, ums->dport); end_bh_atomic(); } else return EINVAL; if (ms == NULL) { return ESRCH; } /* * got (locked) entry, setup almost tiny timeout :) and * give away * * FIXME: should use something better than S_CLOSE */ ms->timeout = IP_MASQ_S_CLOSE; masq_user_k2u(ms, ums); ip_masq_put(ms); return 0; } static struct ip_masq * ip_masq_user_locked_get (struct ip_masq_user *ums, int *err) { struct ip_masq *ms=NULL; if (masq_proto_num (ums->protocol) == -1) { *err = EPROTONOSUPPORT; } start_bh_atomic(); if (ums->mport && ums->maddr) { ms = ip_masq_in_get(ums->protocol, ums->daddr, ums->dport, ums->maddr, ums->mport); end_bh_atomic(); } else if (ums->sport && ums->saddr) { ms = ip_masq_out_get(ums->protocol, ums->saddr, ums->sport, ums->daddr, ums->dport); end_bh_atomic(); } else *err = EINVAL; if (ms == NULL) *err = ESRCH; return ms; } /* * Get existing entry (complete full tunnel info) */ static int ip_masq_user_get(struct ip_masq_user *ums) { struct ip_masq *ms=NULL; int err; ms = ip_masq_user_locked_get(ums, &err); if (ms == NULL) return err; masq_user_k2u(ms, ums); ip_masq_put(ms); return 0; } /* * Set (some, valid) entry parameters */ static int ip_masq_user_set(struct ip_masq_user *ums) { struct ip_masq *ms = NULL; int err; ms = ip_masq_user_locked_get(ums, &err); if (ms == NULL) return err; /* * FIXME: must allow selecting what you want to set */ ms->timeout = ums->timeout; masq_user_k2u(ms, ums); ip_masq_put(ms); return 0; } /* * Entry point * ret value: * <0 err * ==0 ok * >0 ok, copy to user */ static int ip_masq_user_ctl(int optname, struct ip_masq_ctl *mctl, int optlen) { struct ip_masq_user *ums = &mctl->u.user; int ret = EINVAL; int arglen = optlen - IP_MASQ_CTL_BSIZE; int cmd; IP_MASQ_DEBUG(1-debug, "ip_masq_user_ctl(len=%d/%d|%d/%d)\n", arglen, sizeof (*ums), optlen, sizeof (*mctl)); /* * Yes, I'm a bad guy ... */ if (arglen != sizeof(*ums) && optlen != sizeof(*mctl)) return EINVAL; MOD_INC_USE_COUNT; /* * Don't trust the lusers - plenty of error checking! */ cmd = mctl->m_cmd; IP_MASQ_DEBUG(1-debug, "ip_masq_user_ctl(cmd=%d)\n", cmd); switch (mctl->m_cmd) { case IP_MASQ_CMD_ADD: case IP_MASQ_CMD_INSERT: ret = ip_masq_user_new(ums); break; case IP_MASQ_CMD_DEL: ret = ip_masq_user_del(ums); break; case IP_MASQ_CMD_SET: ret = ip_masq_user_set(ums); break; case IP_MASQ_CMD_GET: ret = ip_masq_user_get(ums); break; } /* * For all of the above, return masq tunnel info */ ret = -ret; if (ret == 0) { ret = sizeof (*ums) + IP_MASQ_CTL_BSIZE; IP_MASQ_DEBUG(1-debug, "will return %d bytes to user\n", ret); } MOD_DEC_USE_COUNT; return ret; } #ifdef CONFIG_PROC_FS static int ip_masq_user_info(char *buffer, char **start, off_t offset, int length, int proto) { off_t pos=0, begin; struct ip_masq *ms; char temp[129]; int idx = 0; int col; int len=0; int magic_control; struct list_head *l,*e; MOD_INC_USE_COUNT; IP_MASQ_DEBUG(1-debug, "Entered user_info with proto=%d\n", proto); if (offset < 128) { sprintf(temp, "Prot SrcIP SPrt DstIP DPrt MAddr MPrt State Flgs Ref Ctl Expires HRow HCol (free=%d,%d,%d)", atomic_read(ip_masq_free_ports), atomic_read(ip_masq_free_ports+1), atomic_read(ip_masq_free_ports+2)); len = sprintf(buffer, "%-127s\n", temp); } pos = 128; for(idx = 0; idx < IP_MASQ_TAB_SIZE; idx++) { /* * Lock is actually only need in next loop * we are called from uspace: must stop bh. */ col=0; read_lock_bh(&__ip_masq_lock); l = &ip_masq_m_table[idx]; for (e=l->next; e!=l; e=e->next) { col++; ms = list_entry(e, struct ip_masq, m_list); if (ms->protocol != proto) { continue; } pos += 128; if (pos <= offset) { len = 0; continue; } /* * We have locked the tables, no need to del/add timers * nor cli() 8) */ magic_control = atomic_read(&ms->n_control); if (!magic_control && ms->control) magic_control = -1; sprintf(temp,"%-4s %08lX:%04X %08lX:%04X %08lX:%04X %-12s %3X %4d %3d %7lu %4d %4d", masq_proto_name(ms->protocol), ntohl(ms->saddr), ntohs(ms->sport), ntohl(ms->daddr), ntohs(ms->dport), ntohl(ms->maddr), ntohs(ms->mport), ip_masq_state_name(ms->state), ms->flags, atomic_read(&ms->refcnt), magic_control, (ms->timer.expires-jiffies)/HZ, idx, col); len += sprintf(buffer+len, "%-127s\n", temp); if(len >= length) { read_unlock_bh(&__ip_masq_lock); goto done; } } read_unlock_bh(&__ip_masq_lock); } done: if (len) { begin = len - (pos - offset); *start = buffer + begin; len -= begin; } if(len>length) len = length; MOD_DEC_USE_COUNT; return len; } #else #define ip_masq_user_info NULL #endif static struct ip_masq_hook ip_masq_user = { ip_masq_user_ctl, ip_masq_user_info }; int ip_masq_user_init(void) { if (ip_masq_user_hook != NULL) return -EEXIST; ip_masq_user_hook = &ip_masq_user; return 0; } int ip_masq_user_done(void) { if (ip_masq_user_hook == NULL) return ENOENT; ip_masq_user_hook = NULL; return 0; } #ifdef MODULE EXPORT_NO_SYMBOLS; int init_module(void) { if (ip_masq_user_init() != 0) return -EIO; return 0; } void cleanup_module(void) { if (ip_masq_user_done() != 0) printk(KERN_INFO "ip_masq_user_done(): can't remove module"); } #endif /* MODULE */ |