Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/networking/libiproute/iptunnel.c

    r1772 r2725  
    11/* vi: set sw=4 ts=4: */
    22/*
    3  * iptunnel.c          "ip tunnel"
     3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    44 *
    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>
    96 *
    107 * Changes:
    118 *
    12  * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
    13  * Rani Assaf <rani@magic.metawire.com> 980930: do not allow key for ipip/sit
    14  * Phil Karn <karn@ka9q.ampr.org>   990408: "pmtudisc" flag
     9 * 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
    1512 */
    1613
     
    1916#include <net/if_arp.h>
    2017#include <asm/types.h>
     18
    2119#ifndef __constant_htons
    2220#define __constant_htons htons
    2321#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)
     43struct 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
    2564
    2665#include "ip_common.h"  /* #include "libbb.h" is inside */
     
    3574    int fd;
    3675
    37     strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
     76    strncpy_IFNAMSIZ(ifr.ifr_name, dev);
    3877    fd = xsocket(AF_INET, SOCK_DGRAM, 0);
    3978    xioctl(fd, SIOCGIFINDEX, &ifr);
     
    4887    int err;
    4988
    50     strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
     89    strncpy_IFNAMSIZ(ifr.ifr_name, dev);
    5190    fd = xsocket(AF_INET, SOCK_DGRAM, 0);
    5291    err = ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr);
     
    74113    int err;
    75114
    76     strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
     115    strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
    77116    ifr.ifr_ifru.ifru_data = (void*)p;
    78117    fd = xsocket(AF_INET, SOCK_DGRAM, 0);
     
    89128
    90129    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);
    92131    } else {
    93         strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
     132        strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
    94133    }
    95134    ifr.ifr_ifru.ifru_data = (void*)p;
     
    115154
    116155    if (p->name[0]) {
    117         strncpy(ifr.ifr_name, p->name, sizeof(ifr.ifr_name));
     156        strncpy_IFNAMSIZ(ifr.ifr_name, p->name);
    118157    } else {
    119         strncpy(ifr.ifr_name, basedev, sizeof(ifr.ifr_name));
     158        strncpy_IFNAMSIZ(ifr.ifr_name, basedev);
    120159    }
    121160    ifr.ifr_ifru.ifru_data = (void*)p;
     
    127166
    128167/* Dies on error */
    129 static void parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
     168static void parse_args(char **argv, int cmd, struct ip_tunnel_parm *p)
    130169{
    131170    static const char keywords[] ALIGN1 =
     
    149188
    150189    memset(p, 0, sizeof(*p));
    151     memset(&medium, 0, sizeof(medium));
     190    medium[0] = '\0';
    152191
    153192    p->iph.version = 4;
     
    158197    p->iph.frag_off = htons(IP_DF);
    159198
    160     while (argc > 0) {
     199    while (*argv) {
    161200        key = index_in_strings(keywords, *argv);
    162201        if (key == ARG_mode) {
     
    164203            key = index_in_strings(keywords, *argv);
    165204            if (key == ARG_ipip ||
    166                 key == ARG_ip_ip) {
     205                key == ARG_ip_ip
     206            ) {
    167207                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");
    169209                }
    170210                p->iph.protocol = IPPROTO_IPIP;
    171211            } else if (key == ARG_gre ||
    172                    key == ARG_gre_ip) {
     212                   key == ARG_gre_ip
     213            ) {
    173214                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");
    175216                }
    176217                p->iph.protocol = IPPROTO_GRE;
    177218            } else if (key == ARG_sit ||
    178                    key == ARG_ip6_ip) {
     219                   key == ARG_ip6_ip
     220            ) {
    179221                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");
    181223                }
    182224                p->iph.protocol = IPPROTO_IPV6;
    183225            } else {
    184                 bb_error_msg_and_die("cannot guess tunnel mode");
     226                bb_error_msg_and_die("%s tunnel mode", "can't guess");
    185227            }
    186228        } else if (key == ARG_key) {
     
    192234                p->i_key = p->o_key = get_addr32(*argv);
    193235            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");
    197237                p->i_key = p->o_key = htonl(uval);
    198238            }
     
    204244                p->o_key = get_addr32(*argv);
    205245            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");
    209247                p->i_key = htonl(uval);
    210248            }
     
    216254                p->o_key = get_addr32(*argv);
    217255            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");
    221257                p->o_key = htonl(uval);
    222258            }
     
    251287        } else if (key == ARG_dev) {
    252288            NEXT_ARG();
    253             strncpy(medium, *argv, IFNAMSIZ-1);
     289            strncpy_IFNAMSIZ(medium, *argv);
    254290        } else if (key == ARG_ttl) {
    255291            unsigned uval;
     
    257293            key = index_in_strings(keywords, *argv);
    258294            if (key != ARG_inherit) {
    259                 if (get_unsigned(&uval, *argv, 0))
    260                     invarg(*argv, "TTL");
     295                uval = get_unsigned(*argv, "TTL");
    261296                if (uval > 255)
    262297                    invarg(*argv, "TTL must be <=255");
     
    264299            }
    265300        } else if (key == ARG_tos ||
    266                key == ARG_dsfield) {
     301               key == ARG_dsfield
     302        ) {
    267303            uint32_t uval;
    268304            NEXT_ARG();
     
    280316            if (p->name[0])
    281317                duparg2("name", *argv);
    282             strncpy(p->name, *argv, IFNAMSIZ);
     318            strncpy_IFNAMSIZ(p->name, *argv);
    283319            if (cmd == SIOCCHGTUNNEL && count == 0) {
    284320                struct ip_tunnel_parm old_p;
    285321                memset(&old_p, 0, sizeof(old_p));
    286322                if (do_get_ioctl(*argv, &old_p))
    287                     exit(1);
     323                    exit(EXIT_FAILURE);
    288324                *p = old_p;
    289325            }
    290326        }
    291327        count++;
    292         argc--;
    293328        argv++;
    294329    }
     
    326361}
    327362
    328 
    329363/* Return value becomes exitcode. It's okay to not return at all */
    330 static int do_add(int cmd, int argc, char **argv)
     364static int do_add(int cmd, char **argv)
    331365{
    332366    struct ip_tunnel_parm p;
    333367
    334     parse_args(argc, argv, cmd, &p);
     368    parse_args(argv, cmd, &p);
    335369
    336370    if (p.iph.ttl && p.iph.frag_off == 0) {
     
    346380        return do_add_ioctl(cmd, "sit0", &p);
    347381    default:
    348         bb_error_msg_and_die("cannot determine tunnel mode (ipip, gre or sit)");
     382        bb_error_msg_and_die("can't determine tunnel mode (ipip, gre or sit)");
    349383    }
    350384}
    351385
    352386/* Return value becomes exitcode. It's okay to not return at all */
    353 static int do_del(int argc, char **argv)
     387static int do_del(char **argv)
    354388{
    355389    struct ip_tunnel_parm p;
    356390
    357     parse_args(argc, argv, SIOCDELTUNNEL, &p);
     391    parse_args(argv, SIOCDELTUNNEL, &p);
    358392
    359393    switch (p.iph.protocol) {
     
    405439        if (p->iph.tos & ~1)
    406440            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));
    408442    }
    409443    if (!(p->iph.frag_off & htons(IP_DF)))
     
    444478        return;
    445479    }
    446 
     480    /* skip headers */
    447481    fgets(buf, sizeof(buf), fp);
    448482    fgets(buf, sizeof(buf), fp);
     
    454488        ptr = strchr(buf, ':');
    455489        if (ptr == NULL ||
    456             (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
     490            (*ptr++ = 0, sscanf(buf, "%s", name) != 1)
     491        ) {
    457492            bb_error_msg("wrong format of /proc/net/dev");
    458493            return;
     
    468503        type = do_ioctl_get_iftype(name);
    469504        if (type == -1) {
    470             bb_error_msg("cannot get type of [%s]", name);
     505            bb_error_msg("can't get type of [%s]", name);
    471506            continue;
    472507        }
     
    480515            (p->iph.daddr && p1.iph.daddr != p->iph.daddr) ||
    481516            (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        ) {
    483519            continue;
     520        }
    484521        print_tunnel(&p1);
    485         puts("");
     522        bb_putchar('\n');
    486523    }
    487524}
    488525
    489526/* Return value becomes exitcode. It's okay to not return at all */
    490 static int do_show(int argc, char **argv)
     527static int do_show(char **argv)
    491528{
    492529    int err;
    493530    struct ip_tunnel_parm p;
    494531
    495     parse_args(argc, argv, SIOCGETTUNNEL, &p);
     532    parse_args(argv, SIOCGETTUNNEL, &p);
    496533
    497534    switch (p.iph.protocol) {
     
    513550
    514551    print_tunnel(&p);
    515     puts("");
     552    bb_putchar('\n');
    516553    return 0;
    517554}
    518555
    519556/* Return value becomes exitcode. It's okay to not return at all */
    520 int do_iptunnel(int argc, char **argv)
     557int FAST_FUNC do_iptunnel(char **argv)
    521558{
    522559    static const char keywords[] ALIGN1 =
    523560        "add\0""change\0""delete\0""show\0""list\0""lst\0";
    524561    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)
    530566            bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
    531         --argc;
    532         ++argv;
     567        argv++;
    533568        if (key == ARG_add)
    534             return do_add(SIOCADDTUNNEL, argc, argv);
     569            return do_add(SIOCADDTUNNEL, argv);
    535570        if (key == ARG_change)
    536             return do_add(SIOCCHGTUNNEL, argc, argv);
     571            return do_add(SIOCCHGTUNNEL, argv);
    537572        if (key == ARG_del)
    538             return do_del(argc, argv);
    539     }
    540     return do_show(argc, argv);
    541 }
     573            return do_del(argv);
     574    }
     575    return do_show(argv);
     576}
Note: See TracChangeset for help on using the changeset viewer.