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