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