"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 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 4 | * Patrick McHardy <kaber@trash.net> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 7 | */ |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 8 | #include <net/if.h> |
Denys Vlasenko | ec2bef1 | 2013-08-16 12:20:25 +0200 | [diff] [blame] | 9 | /*#include <net/if_packet.h> - not needed? */ |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 10 | #include <netpacket/packet.h> |
Denys Vlasenko | b307eab | 2011-06-12 17:15:16 +0200 | [diff] [blame] | 11 | #include <netinet/if_ether.h> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 12 | |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 13 | #include <linux/if_vlan.h> |
Denys Vlasenko | 2ead531 | 2013-07-28 23:24:55 +0200 | [diff] [blame] | 14 | #include "ip_common.h" /* #include "libbb.h" is inside */ |
| 15 | #include "rt_names.h" |
| 16 | #include "utils.h" |
| 17 | |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 18 | #undef ETH_P_8021AD |
| 19 | #define ETH_P_8021AD 0x88A8 |
| 20 | #undef VLAN_FLAG_REORDER_HDR |
| 21 | #define VLAN_FLAG_REORDER_HDR 0x1 |
| 22 | #undef VLAN_FLAG_GVRP |
| 23 | #define VLAN_FLAG_GVRP 0x2 |
| 24 | #undef VLAN_FLAG_LOOSE_BINDING |
| 25 | #define VLAN_FLAG_LOOSE_BINDING 0x4 |
| 26 | #undef VLAN_FLAG_MVRP |
| 27 | #define VLAN_FLAG_MVRP 0x8 |
| 28 | #undef IFLA_VLAN_PROTOCOL |
| 29 | #define IFLA_VLAN_PROTOCOL 5 |
| 30 | |
Denys Vlasenko | b276e41 | 2010-08-22 10:02:55 +0200 | [diff] [blame] | 31 | #ifndef IFLA_LINKINFO |
| 32 | # define IFLA_LINKINFO 18 |
Denys Vlasenko | c71ec70 | 2010-08-30 19:20:09 +0200 | [diff] [blame] | 33 | # define IFLA_INFO_KIND 1 |
Eugene Rudoy | 731f596 | 2014-01-22 11:49:46 +0100 | [diff] [blame] | 34 | # define IFLA_INFO_DATA 2 |
| 35 | #endif |
| 36 | |
| 37 | #ifndef IFLA_VLAN_MAX |
| 38 | # define IFLA_VLAN_ID 1 |
| 39 | # define IFLA_VLAN_FLAGS 2 |
| 40 | struct ifla_vlan_flags { |
| 41 | uint32_t flags; |
| 42 | uint32_t mask; |
| 43 | }; |
Denys Vlasenko | b276e41 | 2010-08-22 10:02:55 +0200 | [diff] [blame] | 44 | #endif |
Denys Vlasenko | c71ec70 | 2010-08-30 19:20:09 +0200 | [diff] [blame] | 45 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 46 | /* taken from linux/sockios.h */ |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 47 | #define SIOCSIFNAME 0x8923 /* set interface name */ |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 48 | |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 49 | #if 0 |
| 50 | # define dbg(...) bb_error_msg(__VA_ARGS__) |
| 51 | #else |
| 52 | # define dbg(...) ((void)0) |
| 53 | #endif |
| 54 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 55 | /* Exits on error */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 56 | static int get_ctl_fd(void) |
| 57 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 58 | int fd; |
| 59 | |
| 60 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 61 | if (fd >= 0) |
| 62 | return fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 63 | fd = socket(PF_PACKET, SOCK_DGRAM, 0); |
| 64 | if (fd >= 0) |
| 65 | return fd; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 66 | return xsocket(PF_INET6, SOCK_DGRAM, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 69 | /* Exits on error */ |
| 70 | 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] | 71 | { |
| 72 | struct ifreq ifr; |
| 73 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 75 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 76 | fd = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 77 | xioctl(fd, SIOCGIFFLAGS, &ifr); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 78 | if ((ifr.ifr_flags ^ flags) & mask) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 79 | ifr.ifr_flags &= ~mask; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 80 | ifr.ifr_flags |= mask & flags; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 81 | xioctl(fd, SIOCSIFFLAGS, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 82 | } |
| 83 | close(fd); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 86 | /* Exits on error */ |
| 87 | static void do_changename(char *dev, char *newdev) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 88 | { |
| 89 | struct ifreq ifr; |
| 90 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 91 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 92 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
| 93 | strncpy_IFNAMSIZ(ifr.ifr_newname, newdev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 94 | fd = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 95 | xioctl(fd, SIOCSIFNAME, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 96 | close(fd); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 99 | /* Exits on error */ |
| 100 | static void set_qlen(char *dev, int qlen) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 101 | { |
| 102 | struct ifreq ifr; |
| 103 | int s; |
| 104 | |
| 105 | s = get_ctl_fd(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 108 | ifr.ifr_qlen = qlen; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 109 | xioctl(s, SIOCSIFTXQLEN, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 110 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 113 | /* Exits on error */ |
| 114 | static void set_mtu(char *dev, int mtu) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 115 | { |
| 116 | struct ifreq ifr; |
| 117 | int s; |
| 118 | |
| 119 | s = get_ctl_fd(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 120 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 121 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 122 | ifr.ifr_mtu = mtu; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 123 | xioctl(s, SIOCSIFMTU, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 124 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 127 | /* Exits on error */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 128 | static int get_address(char *dev, int *htype) |
| 129 | { |
| 130 | struct ifreq ifr; |
| 131 | struct sockaddr_ll me; |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 132 | socklen_t alen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 133 | int s; |
| 134 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 135 | s = xsocket(PF_PACKET, SOCK_DGRAM, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 136 | |
| 137 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 138 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 139 | xioctl(s, SIOCGIFINDEX, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 140 | |
| 141 | memset(&me, 0, sizeof(me)); |
| 142 | me.sll_family = AF_PACKET; |
| 143 | me.sll_ifindex = ifr.ifr_ifindex; |
| 144 | me.sll_protocol = htons(ETH_P_LOOP); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 145 | xbind(s, (struct sockaddr*)&me, sizeof(me)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 146 | alen = sizeof(me); |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 147 | getsockname(s, (struct sockaddr*)&me, &alen); |
| 148 | //never happens: |
| 149 | //if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) |
| 150 | // bb_perror_msg_and_die("getsockname"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 151 | close(s); |
| 152 | *htype = me.sll_hatype; |
| 153 | return me.sll_halen; |
| 154 | } |
| 155 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 156 | /* Exits on error */ |
| 157 | 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] | 158 | { |
| 159 | int alen; |
| 160 | |
| 161 | memset(ifr, 0, sizeof(*ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 162 | strncpy_IFNAMSIZ(ifr->ifr_name, dev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 163 | ifr->ifr_hwaddr.sa_family = hatype; |
Bernhard Reutner-Fischer | 51a06c0 | 2008-05-16 17:19:03 +0000 | [diff] [blame] | 164 | |
| 165 | alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/; |
| 166 | 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] | 167 | if (alen < 0) |
Bernhard Reutner-Fischer | 51a06c0 | 2008-05-16 17:19:03 +0000 | [diff] [blame] | 168 | exit(EXIT_FAILURE); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 169 | if (alen != halen) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 170 | 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] | 171 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 174 | /* Exits on error */ |
| 175 | static void set_address(struct ifreq *ifr, int brd) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 176 | { |
| 177 | int s; |
| 178 | |
| 179 | s = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 180 | if (brd) |
| 181 | xioctl(s, SIOCSIFHWBROADCAST, ifr); |
| 182 | else |
| 183 | xioctl(s, SIOCSIFHWADDR, ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 184 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 188 | static void die_must_be_on_off(const char *msg) NORETURN; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 189 | static void die_must_be_on_off(const char *msg) |
| 190 | { |
| 191 | bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg); |
| 192 | } |
| 193 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 194 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 195 | static int do_set(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 196 | { |
| 197 | char *dev = NULL; |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 198 | uint32_t mask = 0; |
| 199 | uint32_t flags = 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 200 | int qlen = -1; |
| 201 | int mtu = -1; |
| 202 | char *newaddr = NULL; |
| 203 | char *newbrd = NULL; |
| 204 | struct ifreq ifr0, ifr1; |
| 205 | char *newname = NULL; |
| 206 | int htype, halen; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 207 | static const char keywords[] ALIGN1 = |
Bernhard Reutner-Fischer | ab0e412 | 2010-05-25 16:57:08 +0200 | [diff] [blame] | 208 | "up\0""down\0""name\0""mtu\0""qlen\0""multicast\0" |
Denis Vlasenko | 44d5dce | 2008-11-01 00:10:51 +0000 | [diff] [blame] | 209 | "arp\0""address\0""dev\0"; |
Bernhard Reutner-Fischer | ab0e412 | 2010-05-25 16:57:08 +0200 | [diff] [blame] | 210 | enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_qlen, ARG_multicast, |
Denis Vlasenko | 44d5dce | 2008-11-01 00:10:51 +0000 | [diff] [blame] | 211 | ARG_arp, ARG_addr, ARG_dev }; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 212 | static const char str_on_off[] ALIGN1 = "on\0""off\0"; |
| 213 | enum { PARM_on = 0, PARM_off }; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 214 | smalluint key; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 215 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 216 | while (*argv) { |
Denis Vlasenko | 44d5dce | 2008-11-01 00:10:51 +0000 | [diff] [blame] | 217 | /* substring search ensures that e.g. "addr" and "address" |
| 218 | * are both accepted */ |
| 219 | key = index_in_substrings(keywords, *argv); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 220 | if (key == ARG_up) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 221 | mask |= IFF_UP; |
| 222 | flags |= IFF_UP; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 223 | } else if (key == ARG_down) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 224 | mask |= IFF_UP; |
| 225 | flags &= ~IFF_UP; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 226 | } else if (key == ARG_name) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 227 | NEXT_ARG(); |
| 228 | newname = *argv; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 229 | } else if (key == ARG_mtu) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 230 | NEXT_ARG(); |
| 231 | if (mtu != -1) |
| 232 | duparg("mtu", *argv); |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 233 | mtu = get_unsigned(*argv, "mtu"); |
Bernhard Reutner-Fischer | ab0e412 | 2010-05-25 16:57:08 +0200 | [diff] [blame] | 234 | } else if (key == ARG_qlen) { |
| 235 | NEXT_ARG(); |
| 236 | if (qlen != -1) |
| 237 | duparg("qlen", *argv); |
| 238 | qlen = get_unsigned(*argv, "qlen"); |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 239 | } else if (key == ARG_addr) { |
Rob Landley | 483027f | 2005-12-15 05:29:48 +0000 | [diff] [blame] | 240 | NEXT_ARG(); |
| 241 | newaddr = *argv; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 242 | } else if (key >= ARG_dev) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 243 | if (key == ARG_dev) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 244 | NEXT_ARG(); |
| 245 | } |
| 246 | if (dev) |
| 247 | duparg2("dev", *argv); |
| 248 | dev = *argv; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 249 | } else { |
| 250 | int param; |
| 251 | NEXT_ARG(); |
| 252 | param = index_in_strings(str_on_off, *argv); |
| 253 | if (key == ARG_multicast) { |
| 254 | if (param < 0) |
| 255 | die_must_be_on_off("multicast"); |
| 256 | mask |= IFF_MULTICAST; |
| 257 | if (param == PARM_on) |
| 258 | flags |= IFF_MULTICAST; |
| 259 | else |
| 260 | flags &= ~IFF_MULTICAST; |
| 261 | } else if (key == ARG_arp) { |
| 262 | if (param < 0) |
| 263 | die_must_be_on_off("arp"); |
| 264 | mask |= IFF_NOARP; |
| 265 | if (param == PARM_on) |
| 266 | flags &= ~IFF_NOARP; |
| 267 | else |
| 268 | flags |= IFF_NOARP; |
| 269 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 270 | } |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 271 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | if (!dev) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 275 | bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | if (newaddr || newbrd) { |
| 279 | halen = get_address(dev, &htype); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 280 | if (newaddr) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 281 | parse_address(dev, htype, halen, newaddr, &ifr0); |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 282 | set_address(&ifr0, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 283 | } |
| 284 | if (newbrd) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 285 | parse_address(dev, htype, halen, newbrd, &ifr1); |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 286 | set_address(&ifr1, 1); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | if (newname && strcmp(dev, newname)) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 291 | do_changename(dev, newname); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 292 | dev = newname; |
| 293 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 294 | if (qlen != -1) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 295 | set_qlen(dev, qlen); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 296 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 297 | if (mtu != -1) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 298 | set_mtu(dev, mtu); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 299 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 300 | if (mask) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 301 | do_chflags(dev, flags, mask); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 305 | static int ipaddr_list_link(char **argv) |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 306 | { |
| 307 | preferred_family = AF_PACKET; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 308 | return ipaddr_list_or_flush(argv, 0); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 311 | static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size) |
| 312 | { |
| 313 | static const char keywords[] ALIGN1 = |
| 314 | "id\0" |
| 315 | "protocol\0" |
| 316 | "reorder_hdr\0" |
| 317 | "gvrp\0" |
| 318 | "mvrp\0" |
| 319 | "loose_binding\0" |
| 320 | ; |
| 321 | static const char protocols[] ALIGN1 = |
| 322 | "802.1q\0" |
| 323 | "802.1ad\0" |
| 324 | ; |
| 325 | static const char str_on_off[] ALIGN1 = |
| 326 | "on\0" |
| 327 | "off\0" |
| 328 | ; |
| 329 | enum { |
| 330 | ARG_id = 0, |
| 331 | ARG_reorder_hdr, |
| 332 | ARG_gvrp, |
| 333 | ARG_mvrp, |
| 334 | ARG_loose_binding, |
| 335 | ARG_protocol, |
| 336 | }; |
| 337 | enum { |
| 338 | PROTO_8021Q = 0, |
| 339 | PROTO_8021AD, |
| 340 | }; |
| 341 | enum { |
| 342 | PARM_on = 0, |
| 343 | PARM_off |
| 344 | }; |
| 345 | int arg; |
| 346 | uint16_t id, proto; |
| 347 | struct ifla_vlan_flags flags = {}; |
| 348 | |
| 349 | while (*argv) { |
| 350 | arg = index_in_substrings(keywords, *argv); |
| 351 | if (arg < 0) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 352 | invarg_1_to_2(*argv, "type vlan"); |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 353 | |
| 354 | NEXT_ARG(); |
| 355 | if (arg == ARG_id) { |
| 356 | id = get_u16(*argv, "id"); |
| 357 | addattr_l(n, size, IFLA_VLAN_ID, &id, sizeof(id)); |
| 358 | } else if (arg == ARG_protocol) { |
| 359 | arg = index_in_substrings(protocols, *argv); |
| 360 | if (arg == PROTO_8021Q) |
| 361 | proto = ETH_P_8021Q; |
| 362 | else if (arg == PROTO_8021AD) |
| 363 | proto = ETH_P_8021AD; |
| 364 | else |
| 365 | bb_error_msg_and_die("unknown VLAN encapsulation protocol '%s'", |
| 366 | *argv); |
| 367 | addattr_l(n, size, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto)); |
| 368 | } else { |
| 369 | int param = index_in_strings(str_on_off, *argv); |
| 370 | if (param < 0) |
| 371 | die_must_be_on_off(nth_string(keywords, arg)); |
| 372 | |
| 373 | if (arg == ARG_reorder_hdr) { |
| 374 | flags.mask |= VLAN_FLAG_REORDER_HDR; |
| 375 | flags.flags &= ~VLAN_FLAG_REORDER_HDR; |
| 376 | if (param == PARM_on) |
| 377 | flags.flags |= VLAN_FLAG_REORDER_HDR; |
| 378 | } else if (arg == ARG_gvrp) { |
| 379 | flags.mask |= VLAN_FLAG_GVRP; |
| 380 | flags.flags &= ~VLAN_FLAG_GVRP; |
| 381 | if (param == PARM_on) |
| 382 | flags.flags |= VLAN_FLAG_GVRP; |
| 383 | } else if (arg == ARG_mvrp) { |
| 384 | flags.mask |= VLAN_FLAG_MVRP; |
| 385 | flags.flags &= ~VLAN_FLAG_MVRP; |
| 386 | if (param == PARM_on) |
| 387 | flags.flags |= VLAN_FLAG_MVRP; |
| 388 | } else { /*if (arg == ARG_loose_binding) */ |
| 389 | flags.mask |= VLAN_FLAG_LOOSE_BINDING; |
| 390 | flags.flags &= ~VLAN_FLAG_LOOSE_BINDING; |
| 391 | if (param == PARM_on) |
| 392 | flags.flags |= VLAN_FLAG_LOOSE_BINDING; |
| 393 | } |
| 394 | } |
| 395 | argv++; |
| 396 | } |
| 397 | |
| 398 | if (flags.mask) |
| 399 | addattr_l(n, size, IFLA_VLAN_FLAGS, &flags, sizeof(flags)); |
| 400 | } |
| 401 | |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 402 | #ifndef NLMSG_TAIL |
| 403 | #define NLMSG_TAIL(nmsg) \ |
| 404 | ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) |
| 405 | #endif |
| 406 | /* Return value becomes exitcode. It's okay to not return at all */ |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 407 | static int do_add_or_delete(char **argv, const unsigned rtm) |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 408 | { |
| 409 | static const char keywords[] ALIGN1 = |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 410 | "link\0""name\0""type\0""dev\0""address\0"; |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 411 | enum { |
| 412 | ARG_link, |
| 413 | ARG_name, |
| 414 | ARG_type, |
| 415 | ARG_dev, |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 416 | ARG_address, |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 417 | }; |
| 418 | struct rtnl_handle rth; |
| 419 | struct { |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 420 | struct nlmsghdr n; |
| 421 | struct ifinfomsg i; |
| 422 | char buf[1024]; |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 423 | } req; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 424 | smalluint arg; |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 425 | char *name_str = NULL; |
| 426 | char *link_str = NULL; |
| 427 | char *type_str = NULL; |
| 428 | char *dev_str = NULL; |
| 429 | char *address_str = NULL; |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 430 | |
| 431 | memset(&req, 0, sizeof(req)); |
| 432 | |
| 433 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
| 434 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 435 | req.n.nlmsg_type = rtm; |
| 436 | req.i.ifi_family = preferred_family; |
| 437 | if (rtm == RTM_NEWLINK) |
| 438 | req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL; |
| 439 | |
| 440 | while (*argv) { |
| 441 | arg = index_in_substrings(keywords, *argv); |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 442 | if (arg == ARG_type) { |
| 443 | NEXT_ARG(); |
| 444 | type_str = *argv++; |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 445 | dbg("type_str:'%s'", type_str); |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 446 | break; |
| 447 | } |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 448 | if (arg == ARG_link) { |
| 449 | NEXT_ARG(); |
| 450 | link_str = *argv; |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 451 | dbg("link_str:'%s'", link_str); |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 452 | } else if (arg == ARG_name) { |
| 453 | NEXT_ARG(); |
| 454 | name_str = *argv; |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 455 | dbg("name_str:'%s'", name_str); |
| 456 | } else if (arg == ARG_address) { |
| 457 | NEXT_ARG(); |
| 458 | address_str = *argv; |
| 459 | dbg("address_str:'%s'", name_str); |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 460 | } else { |
| 461 | if (arg == ARG_dev) { |
| 462 | if (dev_str) |
| 463 | duparg(*argv, "dev"); |
| 464 | NEXT_ARG(); |
| 465 | } |
| 466 | dev_str = *argv; |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 467 | dbg("dev_str:'%s'", dev_str); |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 468 | } |
| 469 | argv++; |
| 470 | } |
| 471 | xrtnl_open(&rth); |
| 472 | ll_init_map(&rth); |
| 473 | if (type_str) { |
| 474 | struct rtattr *linkinfo = NLMSG_TAIL(&req.n); |
| 475 | |
| 476 | addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0); |
| 477 | addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str, |
| 478 | strlen(type_str)); |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 479 | |
| 480 | if (*argv) { |
| 481 | struct rtattr *data = NLMSG_TAIL(&req.n); |
| 482 | addattr_l(&req.n, sizeof(req), IFLA_INFO_DATA, NULL, 0); |
| 483 | |
| 484 | if (strcmp(type_str, "vlan") == 0) |
| 485 | vlan_parse_opt(argv, &req.n, sizeof(req)); |
| 486 | |
| 487 | data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data; |
| 488 | } |
| 489 | |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 490 | linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo; |
| 491 | } |
| 492 | if (rtm != RTM_NEWLINK) { |
| 493 | if (!dev_str) |
| 494 | return 1; /* Need a device to delete */ |
| 495 | req.i.ifi_index = xll_name_to_index(dev_str); |
| 496 | } else { |
| 497 | if (!name_str) |
| 498 | name_str = dev_str; |
| 499 | if (link_str) { |
| 500 | int idx = xll_name_to_index(link_str); |
| 501 | addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4); |
| 502 | } |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 503 | if (address_str) { |
| 504 | unsigned char abuf[32]; |
| 505 | int len = ll_addr_a2n(abuf, sizeof(abuf), address_str); |
| 506 | dbg("address len:%d", len); |
| 507 | if (len < 0) |
| 508 | return -1; |
| 509 | addattr_l(&req.n, sizeof(req), IFLA_ADDRESS, abuf, len); |
| 510 | } |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 511 | } |
| 512 | if (name_str) { |
| 513 | const size_t name_len = strlen(name_str) + 1; |
| 514 | if (name_len < 2 || name_len > IFNAMSIZ) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 515 | invarg_1_to_2(name_str, "name"); |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 516 | addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name_str, name_len); |
| 517 | } |
| 518 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) |
| 519 | return 2; |
| 520 | return 0; |
| 521 | } |
| 522 | |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 523 | /* Other keywords recognized by iproute2-3.12.0: */ |
| 524 | #if 0 |
| 525 | } else if (matches(*argv, "broadcast") == 0 || |
| 526 | strcmp(*argv, "brd") == 0) { |
| 527 | NEXT_ARG(); |
| 528 | len = ll_addr_a2n(abuf, sizeof(abuf), *argv); |
| 529 | if (len < 0) |
| 530 | return -1; |
| 531 | addattr_l(&req->n, sizeof(*req), IFLA_BROADCAST, abuf, len); |
| 532 | } else if (matches(*argv, "txqueuelen") == 0 || |
| 533 | strcmp(*argv, "qlen") == 0 || |
| 534 | matches(*argv, "txqlen") == 0) { |
| 535 | NEXT_ARG(); |
| 536 | if (qlen != -1) |
| 537 | duparg("txqueuelen", *argv); |
| 538 | if (get_integer(&qlen, *argv, 0)) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 539 | invarg_1_to_2(*argv, "txqueuelen"); |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 540 | addattr_l(&req->n, sizeof(*req), IFLA_TXQLEN, &qlen, 4); |
| 541 | } else if (strcmp(*argv, "mtu") == 0) { |
| 542 | NEXT_ARG(); |
| 543 | if (mtu != -1) |
| 544 | duparg("mtu", *argv); |
| 545 | if (get_integer(&mtu, *argv, 0)) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 546 | invarg_1_to_2(*argv, "mtu"); |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 547 | addattr_l(&req->n, sizeof(*req), IFLA_MTU, &mtu, 4); |
| 548 | } else if (strcmp(*argv, "netns") == 0) { |
| 549 | NEXT_ARG(); |
| 550 | if (netns != -1) |
| 551 | duparg("netns", *argv); |
| 552 | if ((netns = get_netns_fd(*argv)) >= 0) |
| 553 | addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_FD, &netns, 4); |
| 554 | else if (get_integer(&netns, *argv, 0) == 0) |
| 555 | addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_PID, &netns, 4); |
| 556 | else |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 557 | invarg_1_to_2(*argv, "netns"); |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 558 | } else if (strcmp(*argv, "multicast") == 0) { |
| 559 | NEXT_ARG(); |
| 560 | req->i.ifi_change |= IFF_MULTICAST; |
| 561 | if (strcmp(*argv, "on") == 0) { |
| 562 | req->i.ifi_flags |= IFF_MULTICAST; |
| 563 | } else if (strcmp(*argv, "off") == 0) { |
| 564 | req->i.ifi_flags &= ~IFF_MULTICAST; |
| 565 | } else |
| 566 | return on_off("multicast", *argv); |
| 567 | } else if (strcmp(*argv, "allmulticast") == 0) { |
| 568 | NEXT_ARG(); |
| 569 | req->i.ifi_change |= IFF_ALLMULTI; |
| 570 | if (strcmp(*argv, "on") == 0) { |
| 571 | req->i.ifi_flags |= IFF_ALLMULTI; |
| 572 | } else if (strcmp(*argv, "off") == 0) { |
| 573 | req->i.ifi_flags &= ~IFF_ALLMULTI; |
| 574 | } else |
| 575 | return on_off("allmulticast", *argv); |
| 576 | } else if (strcmp(*argv, "promisc") == 0) { |
| 577 | NEXT_ARG(); |
| 578 | req->i.ifi_change |= IFF_PROMISC; |
| 579 | if (strcmp(*argv, "on") == 0) { |
| 580 | req->i.ifi_flags |= IFF_PROMISC; |
| 581 | } else if (strcmp(*argv, "off") == 0) { |
| 582 | req->i.ifi_flags &= ~IFF_PROMISC; |
| 583 | } else |
| 584 | return on_off("promisc", *argv); |
| 585 | } else if (strcmp(*argv, "trailers") == 0) { |
| 586 | NEXT_ARG(); |
| 587 | req->i.ifi_change |= IFF_NOTRAILERS; |
| 588 | if (strcmp(*argv, "off") == 0) { |
| 589 | req->i.ifi_flags |= IFF_NOTRAILERS; |
| 590 | } else if (strcmp(*argv, "on") == 0) { |
| 591 | req->i.ifi_flags &= ~IFF_NOTRAILERS; |
| 592 | } else |
| 593 | return on_off("trailers", *argv); |
| 594 | } else if (strcmp(*argv, "arp") == 0) { |
| 595 | NEXT_ARG(); |
| 596 | req->i.ifi_change |= IFF_NOARP; |
| 597 | if (strcmp(*argv, "on") == 0) { |
| 598 | req->i.ifi_flags &= ~IFF_NOARP; |
| 599 | } else if (strcmp(*argv, "off") == 0) { |
| 600 | req->i.ifi_flags |= IFF_NOARP; |
| 601 | } else |
| 602 | return on_off("noarp", *argv); |
| 603 | } else if (strcmp(*argv, "vf") == 0) { |
| 604 | struct rtattr *vflist; |
| 605 | NEXT_ARG(); |
| 606 | if (get_integer(&vf, *argv, 0)) { |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 607 | invarg_1_to_2(*argv, "vf"); |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 608 | } |
| 609 | vflist = addattr_nest(&req->n, sizeof(*req), |
| 610 | IFLA_VFINFO_LIST); |
| 611 | len = iplink_parse_vf(vf, &argc, &argv, req); |
| 612 | if (len < 0) |
| 613 | return -1; |
| 614 | addattr_nest_end(&req->n, vflist); |
| 615 | } else if (matches(*argv, "master") == 0) { |
| 616 | int ifindex; |
| 617 | NEXT_ARG(); |
| 618 | ifindex = ll_name_to_index(*argv); |
| 619 | if (!ifindex) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 620 | invarg_1_to_2(*argv, "master"); |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 621 | addattr_l(&req->n, sizeof(*req), IFLA_MASTER, |
| 622 | &ifindex, 4); |
| 623 | } else if (matches(*argv, "nomaster") == 0) { |
| 624 | int ifindex = 0; |
| 625 | addattr_l(&req->n, sizeof(*req), IFLA_MASTER, |
| 626 | &ifindex, 4); |
| 627 | } else if (matches(*argv, "dynamic") == 0) { |
| 628 | NEXT_ARG(); |
| 629 | req->i.ifi_change |= IFF_DYNAMIC; |
| 630 | if (strcmp(*argv, "on") == 0) { |
| 631 | req->i.ifi_flags |= IFF_DYNAMIC; |
| 632 | } else if (strcmp(*argv, "off") == 0) { |
| 633 | req->i.ifi_flags &= ~IFF_DYNAMIC; |
| 634 | } else |
| 635 | return on_off("dynamic", *argv); |
| 636 | } else if (matches(*argv, "alias") == 0) { |
| 637 | NEXT_ARG(); |
| 638 | addattr_l(&req->n, sizeof(*req), IFLA_IFALIAS, |
| 639 | *argv, strlen(*argv)); |
| 640 | argc--; argv++; |
| 641 | break; |
| 642 | } else if (strcmp(*argv, "group") == 0) { |
| 643 | NEXT_ARG(); |
| 644 | if (*group != -1) |
| 645 | duparg("group", *argv); |
| 646 | if (rtnl_group_a2n(group, *argv)) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 647 | invarg_1_to_2(*argv, "group"); |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 648 | } else if (strcmp(*argv, "mode") == 0) { |
| 649 | int mode; |
| 650 | NEXT_ARG(); |
| 651 | mode = get_link_mode(*argv); |
| 652 | if (mode < 0) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 653 | invarg_1_to_2(*argv, "mode"); |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 654 | addattr8(&req->n, sizeof(*req), IFLA_LINKMODE, mode); |
| 655 | } else if (strcmp(*argv, "state") == 0) { |
| 656 | int state; |
| 657 | NEXT_ARG(); |
| 658 | state = get_operstate(*argv); |
| 659 | if (state < 0) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 660 | invarg_1_to_2(*argv, "state"); |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 661 | addattr8(&req->n, sizeof(*req), IFLA_OPERSTATE, state); |
| 662 | } else if (matches(*argv, "numtxqueues") == 0) { |
| 663 | NEXT_ARG(); |
| 664 | if (numtxqueues != -1) |
| 665 | duparg("numtxqueues", *argv); |
| 666 | if (get_integer(&numtxqueues, *argv, 0)) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 667 | invarg_1_to_2(*argv, "numtxqueues"); |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 668 | addattr_l(&req->n, sizeof(*req), IFLA_NUM_TX_QUEUES, |
| 669 | &numtxqueues, 4); |
| 670 | } else if (matches(*argv, "numrxqueues") == 0) { |
| 671 | NEXT_ARG(); |
| 672 | if (numrxqueues != -1) |
| 673 | duparg("numrxqueues", *argv); |
| 674 | if (get_integer(&numrxqueues, *argv, 0)) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 675 | invarg_1_to_2(*argv, "numrxqueues"); |
Denys Vlasenko | ecd8c3a | 2014-03-01 17:38:41 +0100 | [diff] [blame] | 676 | addattr_l(&req->n, sizeof(*req), IFLA_NUM_RX_QUEUES, |
| 677 | &numrxqueues, 4); |
| 678 | } |
| 679 | #endif |
| 680 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 681 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denys Vlasenko | 2e9b551 | 2010-07-24 23:27:38 +0200 | [diff] [blame] | 682 | int FAST_FUNC do_iplink(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 683 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 684 | static const char keywords[] ALIGN1 = |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 685 | "add\0""delete\0""set\0""show\0""lst\0""list\0"; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 686 | if (*argv) { |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 687 | int key = index_in_substrings(keywords, *argv); |
| 688 | if (key < 0) /* invalid argument */ |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 689 | invarg_1_to_2(*argv, applet_name); |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 690 | argv++; |
| 691 | if (key <= 1) /* add/delete */ |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 692 | return do_add_or_delete(argv, key ? RTM_DELLINK : RTM_NEWLINK); |
| 693 | if (key == 2) /* set */ |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 694 | return do_set(argv); |
| 695 | } |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 696 | /* show, lst, list */ |
| 697 | return ipaddr_list_link(argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 698 | } |