"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 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 49 | /* Exits on error */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 50 | static int get_ctl_fd(void) |
| 51 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 52 | int fd; |
| 53 | |
| 54 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 55 | if (fd >= 0) |
| 56 | return fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 57 | fd = socket(PF_PACKET, SOCK_DGRAM, 0); |
| 58 | if (fd >= 0) |
| 59 | return fd; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 60 | return xsocket(PF_INET6, SOCK_DGRAM, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 63 | /* Exits on error */ |
| 64 | 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] | 65 | { |
| 66 | struct ifreq ifr; |
| 67 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 69 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 70 | fd = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 71 | xioctl(fd, SIOCGIFFLAGS, &ifr); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 72 | if ((ifr.ifr_flags ^ flags) & mask) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 73 | ifr.ifr_flags &= ~mask; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 74 | ifr.ifr_flags |= mask & flags; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 75 | xioctl(fd, SIOCSIFFLAGS, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 76 | } |
| 77 | close(fd); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 80 | /* Exits on error */ |
| 81 | static void do_changename(char *dev, char *newdev) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 82 | { |
| 83 | struct ifreq ifr; |
| 84 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 85 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 86 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
| 87 | strncpy_IFNAMSIZ(ifr.ifr_newname, newdev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 88 | fd = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 89 | xioctl(fd, SIOCSIFNAME, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 90 | close(fd); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 93 | /* Exits on error */ |
| 94 | static void set_qlen(char *dev, int qlen) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 95 | { |
| 96 | struct ifreq ifr; |
| 97 | int s; |
| 98 | |
| 99 | s = get_ctl_fd(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 100 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 101 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 102 | ifr.ifr_qlen = qlen; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 103 | xioctl(s, SIOCSIFTXQLEN, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 104 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 107 | /* Exits on error */ |
| 108 | static void set_mtu(char *dev, int mtu) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 109 | { |
| 110 | struct ifreq ifr; |
| 111 | int s; |
| 112 | |
| 113 | s = get_ctl_fd(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 114 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 115 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 116 | ifr.ifr_mtu = mtu; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 117 | xioctl(s, SIOCSIFMTU, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 118 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 121 | /* Exits on error */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 122 | static int get_address(char *dev, int *htype) |
| 123 | { |
| 124 | struct ifreq ifr; |
| 125 | struct sockaddr_ll me; |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 126 | socklen_t alen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 127 | int s; |
| 128 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 129 | s = xsocket(PF_PACKET, SOCK_DGRAM, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 130 | |
| 131 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 132 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 133 | xioctl(s, SIOCGIFINDEX, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 134 | |
| 135 | memset(&me, 0, sizeof(me)); |
| 136 | me.sll_family = AF_PACKET; |
| 137 | me.sll_ifindex = ifr.ifr_ifindex; |
| 138 | me.sll_protocol = htons(ETH_P_LOOP); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 139 | xbind(s, (struct sockaddr*)&me, sizeof(me)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 140 | alen = sizeof(me); |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 141 | getsockname(s, (struct sockaddr*)&me, &alen); |
| 142 | //never happens: |
| 143 | //if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) |
| 144 | // bb_perror_msg_and_die("getsockname"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 145 | close(s); |
| 146 | *htype = me.sll_hatype; |
| 147 | return me.sll_halen; |
| 148 | } |
| 149 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 150 | /* Exits on error */ |
| 151 | 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] | 152 | { |
| 153 | int alen; |
| 154 | |
| 155 | memset(ifr, 0, sizeof(*ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 156 | strncpy_IFNAMSIZ(ifr->ifr_name, dev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 157 | ifr->ifr_hwaddr.sa_family = hatype; |
Bernhard Reutner-Fischer | 51a06c0 | 2008-05-16 17:19:03 +0000 | [diff] [blame] | 158 | |
| 159 | alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/; |
| 160 | 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] | 161 | if (alen < 0) |
Bernhard Reutner-Fischer | 51a06c0 | 2008-05-16 17:19:03 +0000 | [diff] [blame] | 162 | exit(EXIT_FAILURE); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 163 | if (alen != halen) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 164 | 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] | 165 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 168 | /* Exits on error */ |
| 169 | static void set_address(struct ifreq *ifr, int brd) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 170 | { |
| 171 | int s; |
| 172 | |
| 173 | s = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 174 | if (brd) |
| 175 | xioctl(s, SIOCSIFHWBROADCAST, ifr); |
| 176 | else |
| 177 | xioctl(s, SIOCSIFHWADDR, ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 178 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 182 | static void die_must_be_on_off(const char *msg) NORETURN; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 183 | static void die_must_be_on_off(const char *msg) |
| 184 | { |
| 185 | bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg); |
| 186 | } |
| 187 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 188 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 189 | static int do_set(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 190 | { |
| 191 | char *dev = NULL; |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 192 | uint32_t mask = 0; |
| 193 | uint32_t flags = 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 194 | int qlen = -1; |
| 195 | int mtu = -1; |
| 196 | char *newaddr = NULL; |
| 197 | char *newbrd = NULL; |
| 198 | struct ifreq ifr0, ifr1; |
| 199 | char *newname = NULL; |
| 200 | int htype, halen; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 201 | static const char keywords[] ALIGN1 = |
Bernhard Reutner-Fischer | ab0e412 | 2010-05-25 16:57:08 +0200 | [diff] [blame] | 202 | "up\0""down\0""name\0""mtu\0""qlen\0""multicast\0" |
Denis Vlasenko | 44d5dce | 2008-11-01 00:10:51 +0000 | [diff] [blame] | 203 | "arp\0""address\0""dev\0"; |
Bernhard Reutner-Fischer | ab0e412 | 2010-05-25 16:57:08 +0200 | [diff] [blame] | 204 | 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] | 205 | ARG_arp, ARG_addr, ARG_dev }; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 206 | static const char str_on_off[] ALIGN1 = "on\0""off\0"; |
| 207 | enum { PARM_on = 0, PARM_off }; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 208 | smalluint key; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 209 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 210 | while (*argv) { |
Denis Vlasenko | 44d5dce | 2008-11-01 00:10:51 +0000 | [diff] [blame] | 211 | /* substring search ensures that e.g. "addr" and "address" |
| 212 | * are both accepted */ |
| 213 | key = index_in_substrings(keywords, *argv); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 214 | if (key == ARG_up) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 215 | mask |= IFF_UP; |
| 216 | flags |= IFF_UP; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 217 | } else if (key == ARG_down) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 218 | mask |= IFF_UP; |
| 219 | flags &= ~IFF_UP; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 220 | } else if (key == ARG_name) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 221 | NEXT_ARG(); |
| 222 | newname = *argv; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 223 | } else if (key == ARG_mtu) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 224 | NEXT_ARG(); |
| 225 | if (mtu != -1) |
| 226 | duparg("mtu", *argv); |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 227 | mtu = get_unsigned(*argv, "mtu"); |
Bernhard Reutner-Fischer | ab0e412 | 2010-05-25 16:57:08 +0200 | [diff] [blame] | 228 | } else if (key == ARG_qlen) { |
| 229 | NEXT_ARG(); |
| 230 | if (qlen != -1) |
| 231 | duparg("qlen", *argv); |
| 232 | qlen = get_unsigned(*argv, "qlen"); |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 233 | } else if (key == ARG_addr) { |
Rob Landley | 483027f | 2005-12-15 05:29:48 +0000 | [diff] [blame] | 234 | NEXT_ARG(); |
| 235 | newaddr = *argv; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 236 | } else if (key >= ARG_dev) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 237 | if (key == ARG_dev) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 238 | NEXT_ARG(); |
| 239 | } |
| 240 | if (dev) |
| 241 | duparg2("dev", *argv); |
| 242 | dev = *argv; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 243 | } else { |
| 244 | int param; |
| 245 | NEXT_ARG(); |
| 246 | param = index_in_strings(str_on_off, *argv); |
| 247 | if (key == ARG_multicast) { |
| 248 | if (param < 0) |
| 249 | die_must_be_on_off("multicast"); |
| 250 | mask |= IFF_MULTICAST; |
| 251 | if (param == PARM_on) |
| 252 | flags |= IFF_MULTICAST; |
| 253 | else |
| 254 | flags &= ~IFF_MULTICAST; |
| 255 | } else if (key == ARG_arp) { |
| 256 | if (param < 0) |
| 257 | die_must_be_on_off("arp"); |
| 258 | mask |= IFF_NOARP; |
| 259 | if (param == PARM_on) |
| 260 | flags &= ~IFF_NOARP; |
| 261 | else |
| 262 | flags |= IFF_NOARP; |
| 263 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 264 | } |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 265 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | if (!dev) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 269 | bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if (newaddr || newbrd) { |
| 273 | halen = get_address(dev, &htype); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 274 | if (newaddr) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 275 | parse_address(dev, htype, halen, newaddr, &ifr0); |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 276 | set_address(&ifr0, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 277 | } |
| 278 | if (newbrd) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 279 | parse_address(dev, htype, halen, newbrd, &ifr1); |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 280 | set_address(&ifr1, 1); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | if (newname && strcmp(dev, newname)) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 285 | do_changename(dev, newname); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 286 | dev = newname; |
| 287 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 288 | if (qlen != -1) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 289 | set_qlen(dev, qlen); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 290 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 291 | if (mtu != -1) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 292 | set_mtu(dev, mtu); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 293 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 294 | if (mask) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 295 | do_chflags(dev, flags, mask); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 299 | static int ipaddr_list_link(char **argv) |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 300 | { |
| 301 | preferred_family = AF_PACKET; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 302 | return ipaddr_list_or_flush(argv, 0); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 305 | static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size) |
| 306 | { |
| 307 | static const char keywords[] ALIGN1 = |
| 308 | "id\0" |
| 309 | "protocol\0" |
| 310 | "reorder_hdr\0" |
| 311 | "gvrp\0" |
| 312 | "mvrp\0" |
| 313 | "loose_binding\0" |
| 314 | ; |
| 315 | static const char protocols[] ALIGN1 = |
| 316 | "802.1q\0" |
| 317 | "802.1ad\0" |
| 318 | ; |
| 319 | static const char str_on_off[] ALIGN1 = |
| 320 | "on\0" |
| 321 | "off\0" |
| 322 | ; |
| 323 | enum { |
| 324 | ARG_id = 0, |
| 325 | ARG_reorder_hdr, |
| 326 | ARG_gvrp, |
| 327 | ARG_mvrp, |
| 328 | ARG_loose_binding, |
| 329 | ARG_protocol, |
| 330 | }; |
| 331 | enum { |
| 332 | PROTO_8021Q = 0, |
| 333 | PROTO_8021AD, |
| 334 | }; |
| 335 | enum { |
| 336 | PARM_on = 0, |
| 337 | PARM_off |
| 338 | }; |
| 339 | int arg; |
| 340 | uint16_t id, proto; |
| 341 | struct ifla_vlan_flags flags = {}; |
| 342 | |
| 343 | while (*argv) { |
| 344 | arg = index_in_substrings(keywords, *argv); |
| 345 | if (arg < 0) |
| 346 | invarg(*argv, "type vlan"); |
| 347 | |
| 348 | NEXT_ARG(); |
| 349 | if (arg == ARG_id) { |
| 350 | id = get_u16(*argv, "id"); |
| 351 | addattr_l(n, size, IFLA_VLAN_ID, &id, sizeof(id)); |
| 352 | } else if (arg == ARG_protocol) { |
| 353 | arg = index_in_substrings(protocols, *argv); |
| 354 | if (arg == PROTO_8021Q) |
| 355 | proto = ETH_P_8021Q; |
| 356 | else if (arg == PROTO_8021AD) |
| 357 | proto = ETH_P_8021AD; |
| 358 | else |
| 359 | bb_error_msg_and_die("unknown VLAN encapsulation protocol '%s'", |
| 360 | *argv); |
| 361 | addattr_l(n, size, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto)); |
| 362 | } else { |
| 363 | int param = index_in_strings(str_on_off, *argv); |
| 364 | if (param < 0) |
| 365 | die_must_be_on_off(nth_string(keywords, arg)); |
| 366 | |
| 367 | if (arg == ARG_reorder_hdr) { |
| 368 | flags.mask |= VLAN_FLAG_REORDER_HDR; |
| 369 | flags.flags &= ~VLAN_FLAG_REORDER_HDR; |
| 370 | if (param == PARM_on) |
| 371 | flags.flags |= VLAN_FLAG_REORDER_HDR; |
| 372 | } else if (arg == ARG_gvrp) { |
| 373 | flags.mask |= VLAN_FLAG_GVRP; |
| 374 | flags.flags &= ~VLAN_FLAG_GVRP; |
| 375 | if (param == PARM_on) |
| 376 | flags.flags |= VLAN_FLAG_GVRP; |
| 377 | } else if (arg == ARG_mvrp) { |
| 378 | flags.mask |= VLAN_FLAG_MVRP; |
| 379 | flags.flags &= ~VLAN_FLAG_MVRP; |
| 380 | if (param == PARM_on) |
| 381 | flags.flags |= VLAN_FLAG_MVRP; |
| 382 | } else { /*if (arg == ARG_loose_binding) */ |
| 383 | flags.mask |= VLAN_FLAG_LOOSE_BINDING; |
| 384 | flags.flags &= ~VLAN_FLAG_LOOSE_BINDING; |
| 385 | if (param == PARM_on) |
| 386 | flags.flags |= VLAN_FLAG_LOOSE_BINDING; |
| 387 | } |
| 388 | } |
| 389 | argv++; |
| 390 | } |
| 391 | |
| 392 | if (flags.mask) |
| 393 | addattr_l(n, size, IFLA_VLAN_FLAGS, &flags, sizeof(flags)); |
| 394 | } |
| 395 | |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 396 | #ifndef NLMSG_TAIL |
| 397 | #define NLMSG_TAIL(nmsg) \ |
| 398 | ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) |
| 399 | #endif |
| 400 | /* Return value becomes exitcode. It's okay to not return at all */ |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 401 | static int do_add_or_delete(char **argv, const unsigned rtm) |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 402 | { |
| 403 | static const char keywords[] ALIGN1 = |
| 404 | "link\0""name\0""type\0""dev\0"; |
| 405 | enum { |
| 406 | ARG_link, |
| 407 | ARG_name, |
| 408 | ARG_type, |
| 409 | ARG_dev, |
| 410 | }; |
| 411 | struct rtnl_handle rth; |
| 412 | struct { |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 413 | struct nlmsghdr n; |
| 414 | struct ifinfomsg i; |
| 415 | char buf[1024]; |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 416 | } req; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 417 | smalluint arg; |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 418 | char *name_str = NULL, *link_str = NULL, *type_str = NULL, *dev_str = NULL; |
| 419 | |
| 420 | memset(&req, 0, sizeof(req)); |
| 421 | |
| 422 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
| 423 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 424 | req.n.nlmsg_type = rtm; |
| 425 | req.i.ifi_family = preferred_family; |
| 426 | if (rtm == RTM_NEWLINK) |
| 427 | req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL; |
| 428 | |
| 429 | while (*argv) { |
| 430 | arg = index_in_substrings(keywords, *argv); |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 431 | if (arg == ARG_type) { |
| 432 | NEXT_ARG(); |
| 433 | type_str = *argv++; |
| 434 | break; |
| 435 | } |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 436 | if (arg == ARG_link) { |
| 437 | NEXT_ARG(); |
| 438 | link_str = *argv; |
| 439 | } else if (arg == ARG_name) { |
| 440 | NEXT_ARG(); |
| 441 | name_str = *argv; |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 442 | } else { |
| 443 | if (arg == ARG_dev) { |
| 444 | if (dev_str) |
| 445 | duparg(*argv, "dev"); |
| 446 | NEXT_ARG(); |
| 447 | } |
| 448 | dev_str = *argv; |
| 449 | } |
| 450 | argv++; |
| 451 | } |
| 452 | xrtnl_open(&rth); |
| 453 | ll_init_map(&rth); |
| 454 | if (type_str) { |
| 455 | struct rtattr *linkinfo = NLMSG_TAIL(&req.n); |
| 456 | |
| 457 | addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0); |
| 458 | addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str, |
| 459 | strlen(type_str)); |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 460 | |
| 461 | if (*argv) { |
| 462 | struct rtattr *data = NLMSG_TAIL(&req.n); |
| 463 | addattr_l(&req.n, sizeof(req), IFLA_INFO_DATA, NULL, 0); |
| 464 | |
| 465 | if (strcmp(type_str, "vlan") == 0) |
| 466 | vlan_parse_opt(argv, &req.n, sizeof(req)); |
| 467 | |
| 468 | data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data; |
| 469 | } |
| 470 | |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 471 | linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo; |
| 472 | } |
| 473 | if (rtm != RTM_NEWLINK) { |
| 474 | if (!dev_str) |
| 475 | return 1; /* Need a device to delete */ |
| 476 | req.i.ifi_index = xll_name_to_index(dev_str); |
| 477 | } else { |
| 478 | if (!name_str) |
| 479 | name_str = dev_str; |
| 480 | if (link_str) { |
| 481 | int idx = xll_name_to_index(link_str); |
| 482 | addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4); |
| 483 | } |
| 484 | } |
| 485 | if (name_str) { |
| 486 | const size_t name_len = strlen(name_str) + 1; |
| 487 | if (name_len < 2 || name_len > IFNAMSIZ) |
| 488 | invarg(name_str, "name"); |
| 489 | addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name_str, name_len); |
| 490 | } |
| 491 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) |
| 492 | return 2; |
| 493 | return 0; |
| 494 | } |
| 495 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 496 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denys Vlasenko | 2e9b551 | 2010-07-24 23:27:38 +0200 | [diff] [blame] | 497 | int FAST_FUNC do_iplink(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 498 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 499 | static const char keywords[] ALIGN1 = |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 500 | "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] | 501 | if (*argv) { |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 502 | int key = index_in_substrings(keywords, *argv); |
| 503 | if (key < 0) /* invalid argument */ |
| 504 | invarg(*argv, applet_name); |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 505 | argv++; |
| 506 | if (key <= 1) /* add/delete */ |
Patrick McHardy | a7ecbed | 2013-07-15 05:16:13 +0200 | [diff] [blame] | 507 | return do_add_or_delete(argv, key ? RTM_DELLINK : RTM_NEWLINK); |
| 508 | if (key == 2) /* set */ |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 509 | return do_set(argv); |
| 510 | } |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 511 | /* show, lst, list */ |
| 512 | return ipaddr_list_link(argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 513 | } |