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