"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 | /* Exits on error */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 25 | static int get_ctl_fd(void) |
| 26 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 27 | int fd; |
| 28 | |
| 29 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 30 | if (fd >= 0) |
| 31 | return fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 32 | fd = socket(PF_PACKET, SOCK_DGRAM, 0); |
| 33 | if (fd >= 0) |
| 34 | return fd; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 35 | return xsocket(PF_INET6, SOCK_DGRAM, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 38 | /* Exits on error */ |
| 39 | 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] | 40 | { |
| 41 | struct ifreq ifr; |
| 42 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 44 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 45 | fd = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 46 | xioctl(fd, SIOCGIFFLAGS, &ifr); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 47 | if ((ifr.ifr_flags ^ flags) & mask) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 48 | ifr.ifr_flags &= ~mask; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 49 | ifr.ifr_flags |= mask & flags; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 50 | xioctl(fd, SIOCSIFFLAGS, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 51 | } |
| 52 | close(fd); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 55 | /* Exits on error */ |
| 56 | static void do_changename(char *dev, char *newdev) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 57 | { |
| 58 | struct ifreq ifr; |
| 59 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 61 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
| 62 | strncpy_IFNAMSIZ(ifr.ifr_newname, newdev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 63 | fd = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 64 | xioctl(fd, SIOCSIFNAME, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 65 | close(fd); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 68 | /* Exits on error */ |
| 69 | static void set_qlen(char *dev, int qlen) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 70 | { |
| 71 | struct ifreq ifr; |
| 72 | int s; |
| 73 | |
| 74 | s = get_ctl_fd(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 75 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 76 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 77 | ifr.ifr_qlen = qlen; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 78 | xioctl(s, SIOCSIFTXQLEN, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 79 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 82 | /* Exits on error */ |
| 83 | static void set_mtu(char *dev, int mtu) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 84 | { |
| 85 | struct ifreq ifr; |
| 86 | int s; |
| 87 | |
| 88 | s = get_ctl_fd(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 89 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 90 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 91 | ifr.ifr_mtu = mtu; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 92 | xioctl(s, SIOCSIFMTU, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 93 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 96 | /* Exits on error */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 97 | static int get_address(char *dev, int *htype) |
| 98 | { |
| 99 | struct ifreq ifr; |
| 100 | struct sockaddr_ll me; |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 101 | socklen_t alen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 102 | int s; |
| 103 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 104 | s = xsocket(PF_PACKET, SOCK_DGRAM, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 105 | |
| 106 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 107 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 108 | xioctl(s, SIOCGIFINDEX, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 109 | |
| 110 | memset(&me, 0, sizeof(me)); |
| 111 | me.sll_family = AF_PACKET; |
| 112 | me.sll_ifindex = ifr.ifr_ifindex; |
| 113 | me.sll_protocol = htons(ETH_P_LOOP); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 114 | xbind(s, (struct sockaddr*)&me, sizeof(me)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 115 | |
| 116 | alen = sizeof(me); |
| 117 | if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 118 | bb_perror_msg_and_die("getsockname"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 119 | } |
| 120 | close(s); |
| 121 | *htype = me.sll_hatype; |
| 122 | return me.sll_halen; |
| 123 | } |
| 124 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 125 | /* Exits on error */ |
| 126 | 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] | 127 | { |
| 128 | int alen; |
| 129 | |
| 130 | memset(ifr, 0, sizeof(*ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 131 | strncpy_IFNAMSIZ(ifr->ifr_name, dev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 132 | ifr->ifr_hwaddr.sa_family = hatype; |
Bernhard Reutner-Fischer | 51a06c0 | 2008-05-16 17:19:03 +0000 | [diff] [blame] | 133 | |
| 134 | alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/; |
| 135 | alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 136 | if (alen < 0) |
Bernhard Reutner-Fischer | 51a06c0 | 2008-05-16 17:19:03 +0000 | [diff] [blame] | 137 | exit(EXIT_FAILURE); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 138 | if (alen != halen) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 139 | 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] | 140 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 143 | /* Exits on error */ |
| 144 | static void set_address(struct ifreq *ifr, int brd) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 145 | { |
| 146 | int s; |
| 147 | |
| 148 | s = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 149 | if (brd) |
| 150 | xioctl(s, SIOCSIFHWBROADCAST, ifr); |
| 151 | else |
| 152 | xioctl(s, SIOCSIFHWADDR, ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 153 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 157 | static void die_must_be_on_off(const char *msg) NORETURN; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 158 | static void die_must_be_on_off(const char *msg) |
| 159 | { |
| 160 | bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg); |
| 161 | } |
| 162 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 163 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 164 | static int do_set(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 165 | { |
| 166 | char *dev = NULL; |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 167 | uint32_t mask = 0; |
| 168 | uint32_t flags = 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 169 | int qlen = -1; |
| 170 | int mtu = -1; |
| 171 | char *newaddr = NULL; |
| 172 | char *newbrd = NULL; |
| 173 | struct ifreq ifr0, ifr1; |
| 174 | char *newname = NULL; |
| 175 | int htype, halen; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 176 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 44d5dce | 2008-11-01 00:10:51 +0000 | [diff] [blame] | 177 | "up\0""down\0""name\0""mtu\0""multicast\0" |
| 178 | "arp\0""address\0""dev\0"; |
| 179 | enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_multicast, |
| 180 | ARG_arp, ARG_addr, ARG_dev }; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 181 | static const char str_on_off[] ALIGN1 = "on\0""off\0"; |
| 182 | enum { PARM_on = 0, PARM_off }; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 183 | smalluint key; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 184 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 185 | while (*argv) { |
Denis Vlasenko | 44d5dce | 2008-11-01 00:10:51 +0000 | [diff] [blame] | 186 | /* substring search ensures that e.g. "addr" and "address" |
| 187 | * are both accepted */ |
| 188 | key = index_in_substrings(keywords, *argv); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 189 | if (key == ARG_up) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 190 | mask |= IFF_UP; |
| 191 | flags |= IFF_UP; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 192 | } |
| 193 | if (key == ARG_down) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 194 | mask |= IFF_UP; |
| 195 | flags &= ~IFF_UP; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 196 | } |
| 197 | if (key == ARG_name) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 198 | NEXT_ARG(); |
| 199 | newname = *argv; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 200 | } |
| 201 | if (key == ARG_mtu) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 202 | NEXT_ARG(); |
| 203 | if (mtu != -1) |
| 204 | duparg("mtu", *argv); |
| 205 | if (get_integer(&mtu, *argv, 0)) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 206 | invarg(*argv, "mtu"); |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 207 | } |
| 208 | if (key == ARG_multicast) { |
| 209 | int param; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 210 | NEXT_ARG(); |
| 211 | mask |= IFF_MULTICAST; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 212 | param = index_in_strings(str_on_off, *argv); |
| 213 | if (param < 0) |
| 214 | die_must_be_on_off("multicast"); |
| 215 | if (param == PARM_on) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 216 | flags |= IFF_MULTICAST; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 217 | else |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 218 | flags &= ~IFF_MULTICAST; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 219 | } |
| 220 | if (key == ARG_arp) { |
| 221 | int param; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 222 | NEXT_ARG(); |
| 223 | mask |= IFF_NOARP; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 224 | param = index_in_strings(str_on_off, *argv); |
| 225 | if (param < 0) |
| 226 | die_must_be_on_off("arp"); |
| 227 | if (param == PARM_on) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 228 | flags &= ~IFF_NOARP; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 229 | else |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 230 | flags |= IFF_NOARP; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 231 | } |
| 232 | if (key == ARG_addr) { |
Rob Landley | 483027f | 2005-12-15 05:29:48 +0000 | [diff] [blame] | 233 | NEXT_ARG(); |
| 234 | newaddr = *argv; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 235 | } |
| 236 | if (key >= ARG_dev) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 237 | if (key == ARG_dev) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 238 | NEXT_ARG(); |
| 239 | } |
| 240 | if (dev) |
| 241 | duparg2("dev", *argv); |
| 242 | dev = *argv; |
| 243 | } |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 244 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | if (!dev) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 248 | bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | if (newaddr || newbrd) { |
| 252 | halen = get_address(dev, &htype); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 253 | if (newaddr) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 254 | parse_address(dev, htype, halen, newaddr, &ifr0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 255 | } |
| 256 | if (newbrd) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 257 | parse_address(dev, htype, halen, newbrd, &ifr1); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | if (newname && strcmp(dev, newname)) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 262 | do_changename(dev, newname); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 263 | dev = newname; |
| 264 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 265 | if (qlen != -1) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 266 | set_qlen(dev, qlen); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 267 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 268 | if (mtu != -1) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 269 | set_mtu(dev, mtu); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 270 | } |
| 271 | if (newaddr || newbrd) { |
| 272 | if (newbrd) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 273 | set_address(&ifr1, 1); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 274 | } |
| 275 | if (newaddr) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 276 | set_address(&ifr0, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | if (mask) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 280 | do_chflags(dev, flags, mask); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 281 | return 0; |
| 282 | } |
| 283 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 284 | static int ipaddr_list_link(char **argv) |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 285 | { |
| 286 | preferred_family = AF_PACKET; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 287 | return ipaddr_list_or_flush(argv, 0); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 290 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 291 | int do_iplink(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 292 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 293 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 294 | "set\0""show\0""lst\0""list\0"; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 295 | int key; |
| 296 | if (!*argv) |
| 297 | return ipaddr_list_link(argv); |
| 298 | key = index_in_substrings(keywords, *argv); |
| 299 | if (key < 0) |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 300 | bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 301 | argv++; |
| 302 | if (key == 0) /* set */ |
| 303 | return do_set(argv); |
| 304 | /* show, lst, list */ |
| 305 | return ipaddr_list_link(argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 306 | } |