Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/networking/libiproute/iptunnel.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/networking/libiproute/iptunnel.c
r1772 r2725 1 1 /* vi: set sw=4 ts=4: */ 2 2 /* 3 * iptunnel.c "ip tunnel"3 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 4 4 * 5 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. 6 * 7 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 8 * 5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 9 6 * 10 7 * Changes: 11 8 * 12 * Rani Assaf <rani@magic.metawire.com> 980929: 13 * Rani Assaf <rani@magic.metawire.com> 980930: 14 * Phil Karn <karn@ka9q.ampr.org> 990408:"pmtudisc" flag9 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses 10 * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit 11 * Phil Karn <karn@ka9q.ampr.org> 990408: "pmtudisc" flag 15 12 */ 16 13 … … 19 16 #include <net/if_arp.h> 20 17 #include <asm/types.h> 18 21 19 #ifndef __constant_htons 22 20 #define __constant_htons htons 23 21 #endif 24 #include <linux/if_tunnel.h> 22 23 // FYI: #define SIOCDEVPRIVATE 0x89F0 24 25 /* From linux/if_tunnel.h. #including it proved troublesome 26 * (redefiniton errors due to name collisions in linux/ and net[inet]/) */ 27 #define SIOCGETTUNNEL (SIOCDEVPRIVATE + 0) 28 #define SIOCADDTUNNEL (SIOCDEVPRIVATE + 1) 29 #define SIOCDELTUNNEL (SIOCDEVPRIVATE + 2) 30 #define SIOCCHGTUNNEL (SIOCDEVPRIVATE + 3) 31 //#define SIOCGETPRL (SIOCDEVPRIVATE + 4) 32 //#define SIOCADDPRL (SIOCDEVPRIVATE + 5) 33 //#define SIOCDELPRL (SIOCDEVPRIVATE + 6) 34 //#define SIOCCHGPRL (SIOCDEVPRIVATE + 7) 35 #define GRE_CSUM __constant_htons(0x8000) 36 //#define GRE_ROUTING __constant_htons(0x4000) 37 #define GRE_KEY __constant_htons(0x2000) 38 #define GRE_SEQ __constant_htons(0x1000) 39 //#define GRE_STRICT __constant_htons(0x0800) 40 //#define GRE_REC __constant_htons(0x0700) 41 //#define GRE_FLAGS __constant_htons(0x00F8) 42 //#define GRE_VERSION __constant_htons(0x0007) 43 struct ip_tunnel_parm { 44 char name[IFNAMSIZ]; 45 int link; 46 uint16_t i_flags; 47 uint16_t o_flags; 48 uint32_t i_key; 49 uint32_t o_key; 50 struct iphdr iph; 51 }; 52 /* SIT-mode i_flags */ 53 //#define SIT_ISATAP 0x0001 54 //struct ip_tunnel_prl { 55 // uint32_t addr; 56 // uint16_t flags; 57 // uint16_t __reserved; 58 // uint32_t datalen; 59 // uint32_t __reserved2; 60 // /* data follows */ 61 //}; 62 ///* PRL flags */ 63 //#define PRL_DEFAULT 0x0001 25 64 26 65 #include "ip_common.h" /* #include "libbb.h" is inside */ … … 35 74 int fd; 36 75 37 strncpy (ifr.ifr_name, dev, sizeof(ifr.ifr_name));76 strncpy_IFNAMSIZ(ifr.ifr_name, dev); 38 77 fd = xsocket(AF_INET, SOCK_DGRAM, 0); 39 78 xioctl(fd, SIOCGIFINDEX, &ifr); … … 48 87 int err; 49 88 50 strncpy (ifr.ifr_name, dev, sizeof(ifr.ifr_name));89 strncpy_IFNAMSIZ(ifr.ifr_name, dev); 51 90 fd = xsocket(AF_INET, SOCK_DGRAM, 0); 52 91 err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr); … … 74 113 int err; 75 114 76 strncpy (ifr.ifr_name, basedev, sizeof(ifr.ifr_name));115 strncpy_IFNAMSIZ(ifr.ifr_name, basedev); 77 116 ifr.ifr_ifru.ifru_data = (void*)p; 78 117 fd = xsocket(AF_INET, SOCK_DGRAM, 0); … … 89 128 90 129 if (cmd == SIOCCHGTUNNEL && p->name[0]) { 91 strncpy (ifr.ifr_name, p->name, sizeof(ifr.ifr_name));130 strncpy_IFNAMSIZ(ifr.ifr_name, p->name); 92 131 } else { 93 strncpy (ifr.ifr_name, basedev, sizeof(ifr.ifr_name));132 strncpy_IFNAMSIZ(ifr.ifr_name, basedev); 94 133 } 95 134 ifr.ifr_ifru.ifru_data = (void*)p; … … 115 154 116 155 if (p->name[0]) { 117 strncpy (ifr.ifr_name, p->name, sizeof(ifr.ifr_name));156 strncpy_IFNAMSIZ(ifr.ifr_name, p->name); 118 157 } else { 119 strncpy (ifr.ifr_name, basedev, sizeof(ifr.ifr_name));158 strncpy_IFNAMSIZ(ifr.ifr_name, basedev); 120 159 } 121 160 ifr.ifr_ifru.ifru_data = (void*)p; … … 127 166 128 167 /* Dies on error */ 129 static void parse_args( int argc,char **argv, int cmd, struct ip_tunnel_parm *p)168 static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p) 130 169 { 131 170 static const char keywords[] ALIGN1 = … … 149 188 150 189 memset(p, 0, sizeof(*p)); 151 me mset(&medium, 0, sizeof(medium));190 medium[0] = '\0'; 152 191 153 192 p->iph.version = 4; … … 158 197 p->iph.frag_off = htons(IP_DF); 159 198 160 while ( argc > 0) {199 while (*argv) { 161 200 key = index_in_strings(keywords, *argv); 162 201 if (key == ARG_mode) { … … 164 203 key = index_in_strings(keywords, *argv); 165 204 if (key == ARG_ipip || 166 key == ARG_ip_ip) { 205 key == ARG_ip_ip 206 ) { 167 207 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) { 168 bb_error_msg_and_die(" you managed to ask for more than one tunnel mode");208 bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one"); 169 209 } 170 210 p->iph.protocol = IPPROTO_IPIP; 171 211 } else if (key == ARG_gre || 172 key == ARG_gre_ip) { 212 key == ARG_gre_ip 213 ) { 173 214 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) { 174 bb_error_msg_and_die(" you managed to ask for more than one tunnel mode");215 bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one"); 175 216 } 176 217 p->iph.protocol = IPPROTO_GRE; 177 218 } else if (key == ARG_sit || 178 key == ARG_ip6_ip) { 219 key == ARG_ip6_ip 220 ) { 179 221 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) { 180 bb_error_msg_and_die(" you managed to ask for more than one tunnel mode");222 bb_error_msg_and_die("%s tunnel mode", "you managed to ask for more than one"); 181 223 } 182 224 p->iph.protocol = IPPROTO_IPV6; 183 225 } else { 184 bb_error_msg_and_die(" cannot guess tunnel mode");226 bb_error_msg_and_die("%s tunnel mode", "can't guess"); 185 227 } 186 228 } else if (key == ARG_key) { … … 192 234 p->i_key = p->o_key = get_addr32(*argv); 193 235 else { 194 if (get_unsigned(&uval, *argv, 0)<0) { 195 bb_error_msg_and_die("invalid value of \"key\""); 196 } 236 uval = get_unsigned(*argv, "key"); 197 237 p->i_key = p->o_key = htonl(uval); 198 238 } … … 204 244 p->o_key = get_addr32(*argv); 205 245 else { 206 if (get_unsigned(&uval, *argv, 0)<0) { 207 bb_error_msg_and_die("invalid value of \"ikey\""); 208 } 246 uval = get_unsigned(*argv, "ikey"); 209 247 p->i_key = htonl(uval); 210 248 } … … 216 254 p->o_key = get_addr32(*argv); 217 255 else { 218 if (get_unsigned(&uval, *argv, 0)<0) { 219 bb_error_msg_and_die("invalid value of \"okey\""); 220 } 256 uval = get_unsigned(*argv, "okey"); 221 257 p->o_key = htonl(uval); 222 258 } … … 251 287 } else if (key == ARG_dev) { 252 288 NEXT_ARG(); 253 strncpy (medium, *argv, IFNAMSIZ-1);289 strncpy_IFNAMSIZ(medium, *argv); 254 290 } else if (key == ARG_ttl) { 255 291 unsigned uval; … … 257 293 key = index_in_strings(keywords, *argv); 258 294 if (key != ARG_inherit) { 259 if (get_unsigned(&uval, *argv, 0)) 260 invarg(*argv, "TTL"); 295 uval = get_unsigned(*argv, "TTL"); 261 296 if (uval > 255) 262 297 invarg(*argv, "TTL must be <=255"); … … 264 299 } 265 300 } else if (key == ARG_tos || 266 key == ARG_dsfield) { 301 key == ARG_dsfield 302 ) { 267 303 uint32_t uval; 268 304 NEXT_ARG(); … … 280 316 if (p->name[0]) 281 317 duparg2("name", *argv); 282 strncpy (p->name, *argv, IFNAMSIZ);318 strncpy_IFNAMSIZ(p->name, *argv); 283 319 if (cmd == SIOCCHGTUNNEL && count == 0) { 284 320 struct ip_tunnel_parm old_p; 285 321 memset(&old_p, 0, sizeof(old_p)); 286 322 if (do_get_ioctl(*argv, &old_p)) 287 exit( 1);323 exit(EXIT_FAILURE); 288 324 *p = old_p; 289 325 } 290 326 } 291 327 count++; 292 argc--;293 328 argv++; 294 329 } … … 326 361 } 327 362 328 329 363 /* Return value becomes exitcode. It's okay to not return at all */ 330 static int do_add(int cmd, int argc,char **argv)364 static int do_add(int cmd, char **argv) 331 365 { 332 366 struct ip_tunnel_parm p; 333 367 334 parse_args(arg c, argv, cmd, &p);368 parse_args(argv, cmd, &p); 335 369 336 370 if (p.iph.ttl && p.iph.frag_off == 0) { … … 346 380 return do_add_ioctl(cmd, "sit0", &p); 347 381 default: 348 bb_error_msg_and_die("can not determine tunnel mode (ipip, gre or sit)");382 bb_error_msg_and_die("can't determine tunnel mode (ipip, gre or sit)"); 349 383 } 350 384 } 351 385 352 386 /* Return value becomes exitcode. It's okay to not return at all */ 353 static int do_del( int argc,char **argv)387 static int do_del(char **argv) 354 388 { 355 389 struct ip_tunnel_parm p; 356 390 357 parse_args(arg c, argv, SIOCDELTUNNEL, &p);391 parse_args(argv, SIOCDELTUNNEL, &p); 358 392 359 393 switch (p.iph.protocol) { … … 405 439 if (p->iph.tos & ~1) 406 440 printf("%c%s ", p->iph.tos & 1 ? '/' : ' ', 407 rtnl_dsfield_n2a(p->iph.tos & ~1, b1 , sizeof(b1)));441 rtnl_dsfield_n2a(p->iph.tos & ~1, b1)); 408 442 } 409 443 if (!(p->iph.frag_off & htons(IP_DF))) … … 444 478 return; 445 479 } 446 480 /* skip headers */ 447 481 fgets(buf, sizeof(buf), fp); 448 482 fgets(buf, sizeof(buf), fp); … … 454 488 ptr = strchr(buf, ':'); 455 489 if (ptr == NULL || 456 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { 490 (*ptr++ = 0, sscanf(buf, "%s", name) != 1) 491 ) { 457 492 bb_error_msg("wrong format of /proc/net/dev"); 458 493 return; … … 468 503 type = do_ioctl_get_iftype(name); 469 504 if (type == -1) { 470 bb_error_msg("can not get type of [%s]", name);505 bb_error_msg("can't get type of [%s]", name); 471 506 continue; 472 507 } … … 480 515 (p->iph.daddr && p1.iph.daddr != p->iph.daddr) || 481 516 (p->iph.saddr && p1.iph.saddr != p->iph.saddr) || 482 (p->i_key && p1.i_key != p->i_key)) 517 (p->i_key && p1.i_key != p->i_key) 518 ) { 483 519 continue; 520 } 484 521 print_tunnel(&p1); 485 puts("");522 bb_putchar('\n'); 486 523 } 487 524 } 488 525 489 526 /* Return value becomes exitcode. It's okay to not return at all */ 490 static int do_show( int argc,char **argv)527 static int do_show(char **argv) 491 528 { 492 529 int err; 493 530 struct ip_tunnel_parm p; 494 531 495 parse_args(arg c, argv, SIOCGETTUNNEL, &p);532 parse_args(argv, SIOCGETTUNNEL, &p); 496 533 497 534 switch (p.iph.protocol) { … … 513 550 514 551 print_tunnel(&p); 515 puts("");552 bb_putchar('\n'); 516 553 return 0; 517 554 } 518 555 519 556 /* Return value becomes exitcode. It's okay to not return at all */ 520 int do_iptunnel(int argc,char **argv)557 int FAST_FUNC do_iptunnel(char **argv) 521 558 { 522 559 static const char keywords[] ALIGN1 = 523 560 "add\0""change\0""delete\0""show\0""list\0""lst\0"; 524 561 enum { ARG_add = 0, ARG_change, ARG_del, ARG_show, ARG_list, ARG_lst }; 525 int key; 526 527 if (argc) { 528 key = index_in_substrings(keywords, *argv); 529 if (key < 0) 562 563 if (*argv) { 564 smalluint key = index_in_substrings(keywords, *argv); 565 if (key > 5) 530 566 bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); 531 --argc; 532 ++argv; 567 argv++; 533 568 if (key == ARG_add) 534 return do_add(SIOCADDTUNNEL, arg c, argv);569 return do_add(SIOCADDTUNNEL, argv); 535 570 if (key == ARG_change) 536 return do_add(SIOCCHGTUNNEL, arg c, argv);571 return do_add(SIOCCHGTUNNEL, argv); 537 572 if (key == ARG_del) 538 return do_del(arg c, argv);539 } 540 return do_show(arg c, argv);541 } 573 return do_del(argv); 574 } 575 return do_show(argv); 576 }
Note:
See TracChangeset
for help on using the changeset viewer.