"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 2 | /* |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 3 | * iplink.c "ip link". |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 4 | * |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 6 | * |
Rob Landley | ecae66a | 2006-06-02 20:53:38 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 10 | //#include <sys/ioctl.h> |
| 11 | //#include <sys/socket.h> |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 12 | #include <net/if.h> |
| 13 | #include <net/if_packet.h> |
| 14 | #include <netpacket/packet.h> |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 15 | #include <net/ethernet.h> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 17 | #include "ip_common.h" /* #include "libbb.h" is inside */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 18 | #include "rt_names.h" |
| 19 | #include "utils.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 21 | /* taken from linux/sockios.h */ |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 22 | #define SIOCSIFNAME 0x8923 /* set interface name */ |
| 23 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 24 | static void on_off(const char *msg) ATTRIBUTE_NORETURN; |
| 25 | static void on_off(const char *msg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 26 | { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 27 | bb_error_msg_and_die("error: argument of \"%s\" must be \"on\" or \"off\"", msg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 30 | /* Exits on error */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 31 | static int get_ctl_fd(void) |
| 32 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 33 | int fd; |
| 34 | |
| 35 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 36 | if (fd >= 0) |
| 37 | return fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 38 | fd = socket(PF_PACKET, SOCK_DGRAM, 0); |
| 39 | if (fd >= 0) |
| 40 | return fd; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 41 | return xsocket(PF_INET6, SOCK_DGRAM, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 44 | /* Exits on error */ |
| 45 | static void do_chflags(char *dev, uint32_t flags, uint32_t mask) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 46 | { |
| 47 | struct ifreq ifr; |
| 48 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 49 | |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 50 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 51 | fd = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 52 | xioctl(fd, SIOCGIFFLAGS, &ifr); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 53 | if ((ifr.ifr_flags ^ flags) & mask) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 54 | ifr.ifr_flags &= ~mask; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 55 | ifr.ifr_flags |= mask & flags; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 56 | xioctl(fd, SIOCSIFFLAGS, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 57 | } |
| 58 | close(fd); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 61 | /* Exits on error */ |
| 62 | static void do_changename(char *dev, char *newdev) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 63 | { |
| 64 | struct ifreq ifr; |
| 65 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 67 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
| 68 | strncpy(ifr.ifr_newname, newdev, sizeof(ifr.ifr_newname)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 69 | fd = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 70 | xioctl(fd, SIOCSIFNAME, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 71 | close(fd); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 74 | /* Exits on error */ |
| 75 | static void set_qlen(char *dev, int qlen) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 76 | { |
| 77 | struct ifreq ifr; |
| 78 | int s; |
| 79 | |
| 80 | s = get_ctl_fd(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 81 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 82 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 83 | ifr.ifr_qlen = qlen; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 84 | xioctl(s, SIOCSIFTXQLEN, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 85 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 88 | /* Exits on error */ |
| 89 | static void set_mtu(char *dev, int mtu) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 90 | { |
| 91 | struct ifreq ifr; |
| 92 | int s; |
| 93 | |
| 94 | s = get_ctl_fd(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 95 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 96 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 97 | ifr.ifr_mtu = mtu; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 98 | xioctl(s, SIOCSIFMTU, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 99 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 102 | /* Exits on error */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 103 | static int get_address(char *dev, int *htype) |
| 104 | { |
| 105 | struct ifreq ifr; |
| 106 | struct sockaddr_ll me; |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 107 | socklen_t alen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 108 | int s; |
| 109 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 110 | s = xsocket(PF_PACKET, SOCK_DGRAM, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 111 | |
| 112 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 113 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 114 | xioctl(s, SIOCGIFINDEX, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 115 | |
| 116 | memset(&me, 0, sizeof(me)); |
| 117 | me.sll_family = AF_PACKET; |
| 118 | me.sll_ifindex = ifr.ifr_ifindex; |
| 119 | me.sll_protocol = htons(ETH_P_LOOP); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 120 | xbind(s, (struct sockaddr*)&me, sizeof(me)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 121 | |
| 122 | alen = sizeof(me); |
| 123 | if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 124 | bb_perror_msg_and_die("getsockname"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 125 | } |
| 126 | close(s); |
| 127 | *htype = me.sll_hatype; |
| 128 | return me.sll_halen; |
| 129 | } |
| 130 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 131 | /* Exits on error */ |
| 132 | static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 133 | { |
| 134 | int alen; |
| 135 | |
| 136 | memset(ifr, 0, sizeof(*ifr)); |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame] | 137 | strncpy(ifr->ifr_name, dev, sizeof(ifr->ifr_name)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 138 | ifr->ifr_hwaddr.sa_family = hatype; |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 139 | alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), 14, lla); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 140 | if (alen < 0) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 141 | exit(1); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 142 | if (alen != halen) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 143 | bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 144 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 147 | /* Exits on error */ |
| 148 | static void set_address(struct ifreq *ifr, int brd) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 149 | { |
| 150 | int s; |
| 151 | |
| 152 | s = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 153 | if (brd) |
| 154 | xioctl(s, SIOCSIFHWBROADCAST, ifr); |
| 155 | else |
| 156 | xioctl(s, SIOCSIFHWADDR, ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 157 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 161 | /* Return value becomes exitcode. It's okay to not return at all */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 162 | static int do_set(int argc, char **argv) |
| 163 | { |
| 164 | char *dev = NULL; |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 165 | uint32_t mask = 0; |
| 166 | uint32_t flags = 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 167 | int qlen = -1; |
| 168 | int mtu = -1; |
| 169 | char *newaddr = NULL; |
| 170 | char *newbrd = NULL; |
| 171 | struct ifreq ifr0, ifr1; |
| 172 | char *newname = NULL; |
| 173 | int htype, halen; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame^] | 174 | static const char keywords[] = |
| 175 | "up\0""down\0""name\0""mtu\0""multicast\0""arp\0""addr\0""dev\0" |
| 176 | "on\0""off\0"; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 177 | enum { ARG_up = 1, ARG_down, ARG_name, ARG_mtu, ARG_multicast, ARG_arp, |
| 178 | ARG_addr, ARG_dev, PARM_on, PARM_off }; |
| 179 | smalluint key; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 180 | |
| 181 | while (argc > 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame^] | 182 | key = index_in_strings(keywords, *argv) + 1; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 183 | if (key == ARG_up) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 184 | mask |= IFF_UP; |
| 185 | flags |= IFF_UP; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 186 | } else if (key == ARG_down) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 187 | mask |= IFF_UP; |
| 188 | flags &= ~IFF_UP; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 189 | } else if (key == ARG_name) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 190 | NEXT_ARG(); |
| 191 | newname = *argv; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 192 | } else if (key == ARG_mtu) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 193 | NEXT_ARG(); |
| 194 | if (mtu != -1) |
| 195 | duparg("mtu", *argv); |
| 196 | if (get_integer(&mtu, *argv, 0)) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 197 | invarg(*argv, "mtu"); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 198 | } else if (key == ARG_multicast) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 199 | NEXT_ARG(); |
| 200 | mask |= IFF_MULTICAST; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame^] | 201 | key = index_in_strings(keywords, *argv) + 1; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 202 | if (key == PARM_on) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 203 | flags |= IFF_MULTICAST; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 204 | } else if (key == PARM_off) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 205 | flags &= ~IFF_MULTICAST; |
| 206 | } else |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 207 | on_off("multicast"); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 208 | } else if (key == ARG_arp) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 209 | NEXT_ARG(); |
| 210 | mask |= IFF_NOARP; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame^] | 211 | key = index_in_strings(keywords, *argv) + 1; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 212 | if (key == PARM_on) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 213 | flags &= ~IFF_NOARP; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 214 | } else if (key == PARM_off) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 215 | flags |= IFF_NOARP; |
| 216 | } else |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 217 | on_off("arp"); |
| 218 | } else if (key == ARG_addr) { |
Rob Landley | 483027f | 2005-12-15 05:29:48 +0000 | [diff] [blame] | 219 | NEXT_ARG(); |
| 220 | newaddr = *argv; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 221 | } else { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 222 | if (key == ARG_dev) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 223 | NEXT_ARG(); |
| 224 | } |
| 225 | if (dev) |
| 226 | duparg2("dev", *argv); |
| 227 | dev = *argv; |
| 228 | } |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 229 | argc--; argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | if (!dev) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 233 | bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | if (newaddr || newbrd) { |
| 237 | halen = get_address(dev, &htype); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 238 | if (newaddr) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 239 | parse_address(dev, htype, halen, newaddr, &ifr0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 240 | } |
| 241 | if (newbrd) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 242 | parse_address(dev, htype, halen, newbrd, &ifr1); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | if (newname && strcmp(dev, newname)) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 247 | do_changename(dev, newname); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 248 | dev = newname; |
| 249 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 250 | if (qlen != -1) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 251 | set_qlen(dev, qlen); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 252 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 253 | if (mtu != -1) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 254 | set_mtu(dev, mtu); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 255 | } |
| 256 | if (newaddr || newbrd) { |
| 257 | if (newbrd) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 258 | set_address(&ifr1, 1); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 259 | } |
| 260 | if (newaddr) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 261 | set_address(&ifr0, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | if (mask) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 265 | do_chflags(dev, flags, mask); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 269 | static int ipaddr_list_link(int argc, char **argv) |
| 270 | { |
| 271 | preferred_family = AF_PACKET; |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 272 | return ipaddr_list_or_flush(argc, argv, 0); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 275 | /* Return value becomes exitcode. It's okay to not return at all */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 276 | int do_iplink(int argc, char **argv) |
| 277 | { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame^] | 278 | static const char keywords[] = |
| 279 | "set\0""show\0""lst\0""list\0"; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 280 | smalluint key; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 281 | if (argc <= 0) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 282 | return ipaddr_list_link(0, NULL); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame^] | 283 | key = index_in_substrings(keywords, *argv) + 1; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 284 | if (key == 0) |
| 285 | bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); |
| 286 | argc--; argv++; |
| 287 | if (key == 1) /* set */ |
| 288 | return do_set(argc, argv); |
| 289 | else /* show, lst, list */ |
| 290 | return ipaddr_list_link(argc, argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 291 | } |