"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 | * iplink.c "ip link". |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 4 | * |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * 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] | 8 | */ |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 9 | #include <net/if.h> |
| 10 | #include <net/if_packet.h> |
| 11 | #include <netpacket/packet.h> |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 12 | #include <net/ethernet.h> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 13 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame] | 14 | #include "ip_common.h" /* #include "libbb.h" is inside */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 15 | #include "rt_names.h" |
| 16 | #include "utils.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 17 | |
Denys Vlasenko | b276e41 | 2010-08-22 10:02:55 +0200 | [diff] [blame] | 18 | #ifndef IFLA_LINKINFO |
| 19 | # define IFLA_LINKINFO 18 |
Denys Vlasenko | c71ec70 | 2010-08-30 19:20:09 +0200 | [diff] [blame] | 20 | # define IFLA_INFO_KIND 1 |
Denys Vlasenko | b276e41 | 2010-08-22 10:02:55 +0200 | [diff] [blame] | 21 | #endif |
Denys Vlasenko | c71ec70 | 2010-08-30 19:20:09 +0200 | [diff] [blame] | 22 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 23 | /* taken from linux/sockios.h */ |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame^] | 24 | #define SIOCSIFNAME 0x8923 /* set interface name */ |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 26 | /* Exits on error */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 27 | static int get_ctl_fd(void) |
| 28 | { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 29 | int fd; |
| 30 | |
| 31 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 32 | if (fd >= 0) |
| 33 | return fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 34 | fd = socket(PF_PACKET, SOCK_DGRAM, 0); |
| 35 | if (fd >= 0) |
| 36 | return fd; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 37 | return xsocket(PF_INET6, SOCK_DGRAM, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 40 | /* Exits on error */ |
| 41 | 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] | 42 | { |
| 43 | struct ifreq ifr; |
| 44 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 46 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 47 | fd = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 48 | xioctl(fd, SIOCGIFFLAGS, &ifr); |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 49 | if ((ifr.ifr_flags ^ flags) & mask) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 50 | ifr.ifr_flags &= ~mask; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 51 | ifr.ifr_flags |= mask & flags; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 52 | xioctl(fd, SIOCSIFFLAGS, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 53 | } |
| 54 | close(fd); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 57 | /* Exits on error */ |
| 58 | static void do_changename(char *dev, char *newdev) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 59 | { |
| 60 | struct ifreq ifr; |
| 61 | int fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 63 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
| 64 | strncpy_IFNAMSIZ(ifr.ifr_newname, newdev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 65 | fd = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 66 | xioctl(fd, SIOCSIFNAME, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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 set_qlen(char *dev, int qlen) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 72 | { |
| 73 | struct ifreq ifr; |
| 74 | int s; |
| 75 | |
| 76 | s = get_ctl_fd(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 77 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 78 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 79 | ifr.ifr_qlen = qlen; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 80 | xioctl(s, SIOCSIFTXQLEN, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 81 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 84 | /* Exits on error */ |
| 85 | static void set_mtu(char *dev, int mtu) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 86 | { |
| 87 | struct ifreq ifr; |
| 88 | int s; |
| 89 | |
| 90 | s = get_ctl_fd(); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 91 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 92 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 93 | ifr.ifr_mtu = mtu; |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 94 | xioctl(s, SIOCSIFMTU, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 95 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 98 | /* Exits on error */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 99 | static int get_address(char *dev, int *htype) |
| 100 | { |
| 101 | struct ifreq ifr; |
| 102 | struct sockaddr_ll me; |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 103 | socklen_t alen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 104 | int s; |
| 105 | |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 106 | s = xsocket(PF_PACKET, SOCK_DGRAM, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 107 | |
| 108 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 109 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 110 | xioctl(s, SIOCGIFINDEX, &ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 111 | |
| 112 | memset(&me, 0, sizeof(me)); |
| 113 | me.sll_family = AF_PACKET; |
| 114 | me.sll_ifindex = ifr.ifr_ifindex; |
| 115 | me.sll_protocol = htons(ETH_P_LOOP); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 116 | xbind(s, (struct sockaddr*)&me, sizeof(me)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 117 | alen = sizeof(me); |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 118 | getsockname(s, (struct sockaddr*)&me, &alen); |
| 119 | //never happens: |
| 120 | //if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) |
| 121 | // bb_perror_msg_and_die("getsockname"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 122 | close(s); |
| 123 | *htype = me.sll_hatype; |
| 124 | return me.sll_halen; |
| 125 | } |
| 126 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 127 | /* Exits on error */ |
| 128 | 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] | 129 | { |
| 130 | int alen; |
| 131 | |
| 132 | memset(ifr, 0, sizeof(*ifr)); |
Denis Vlasenko | 360d966 | 2008-12-02 18:18:50 +0000 | [diff] [blame] | 133 | strncpy_IFNAMSIZ(ifr->ifr_name, dev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 134 | ifr->ifr_hwaddr.sa_family = hatype; |
Bernhard Reutner-Fischer | 51a06c0 | 2008-05-16 17:19:03 +0000 | [diff] [blame] | 135 | |
| 136 | alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/; |
| 137 | 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] | 138 | if (alen < 0) |
Bernhard Reutner-Fischer | 51a06c0 | 2008-05-16 17:19:03 +0000 | [diff] [blame] | 139 | exit(EXIT_FAILURE); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 140 | if (alen != halen) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 141 | 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] | 142 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 145 | /* Exits on error */ |
| 146 | static void set_address(struct ifreq *ifr, int brd) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 147 | { |
| 148 | int s; |
| 149 | |
| 150 | s = get_ctl_fd(); |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 151 | if (brd) |
| 152 | xioctl(s, SIOCSIFHWBROADCAST, ifr); |
| 153 | else |
| 154 | xioctl(s, SIOCSIFHWADDR, ifr); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 155 | close(s); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 159 | static void die_must_be_on_off(const char *msg) NORETURN; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 160 | static void die_must_be_on_off(const char *msg) |
| 161 | { |
| 162 | bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg); |
| 163 | } |
| 164 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 165 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 166 | static int do_set(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 167 | { |
| 168 | char *dev = NULL; |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 169 | uint32_t mask = 0; |
| 170 | uint32_t flags = 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 171 | int qlen = -1; |
| 172 | int mtu = -1; |
| 173 | char *newaddr = NULL; |
| 174 | char *newbrd = NULL; |
| 175 | struct ifreq ifr0, ifr1; |
| 176 | char *newname = NULL; |
| 177 | int htype, halen; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 178 | static const char keywords[] ALIGN1 = |
Bernhard Reutner-Fischer | ab0e412 | 2010-05-25 16:57:08 +0200 | [diff] [blame] | 179 | "up\0""down\0""name\0""mtu\0""qlen\0""multicast\0" |
Denis Vlasenko | 44d5dce | 2008-11-01 00:10:51 +0000 | [diff] [blame] | 180 | "arp\0""address\0""dev\0"; |
Bernhard Reutner-Fischer | ab0e412 | 2010-05-25 16:57:08 +0200 | [diff] [blame] | 181 | 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] | 182 | ARG_arp, ARG_addr, ARG_dev }; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 183 | static const char str_on_off[] ALIGN1 = "on\0""off\0"; |
| 184 | enum { PARM_on = 0, PARM_off }; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 185 | smalluint key; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 186 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 187 | while (*argv) { |
Denis Vlasenko | 44d5dce | 2008-11-01 00:10:51 +0000 | [diff] [blame] | 188 | /* substring search ensures that e.g. "addr" and "address" |
| 189 | * are both accepted */ |
| 190 | key = index_in_substrings(keywords, *argv); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 191 | if (key == ARG_up) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 192 | mask |= IFF_UP; |
| 193 | flags |= IFF_UP; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 194 | } else if (key == ARG_down) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 195 | mask |= IFF_UP; |
| 196 | flags &= ~IFF_UP; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 197 | } else if (key == ARG_name) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 198 | NEXT_ARG(); |
| 199 | newname = *argv; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 200 | } else if (key == ARG_mtu) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 201 | NEXT_ARG(); |
| 202 | if (mtu != -1) |
| 203 | duparg("mtu", *argv); |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 204 | mtu = get_unsigned(*argv, "mtu"); |
Bernhard Reutner-Fischer | ab0e412 | 2010-05-25 16:57:08 +0200 | [diff] [blame] | 205 | } else if (key == ARG_qlen) { |
| 206 | NEXT_ARG(); |
| 207 | if (qlen != -1) |
| 208 | duparg("qlen", *argv); |
| 209 | qlen = get_unsigned(*argv, "qlen"); |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 210 | } else if (key == ARG_addr) { |
Rob Landley | 483027f | 2005-12-15 05:29:48 +0000 | [diff] [blame] | 211 | NEXT_ARG(); |
| 212 | newaddr = *argv; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 213 | } else if (key >= ARG_dev) { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 214 | if (key == ARG_dev) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 215 | NEXT_ARG(); |
| 216 | } |
| 217 | if (dev) |
| 218 | duparg2("dev", *argv); |
| 219 | dev = *argv; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 220 | } else { |
| 221 | int param; |
| 222 | NEXT_ARG(); |
| 223 | param = index_in_strings(str_on_off, *argv); |
| 224 | if (key == ARG_multicast) { |
| 225 | if (param < 0) |
| 226 | die_must_be_on_off("multicast"); |
| 227 | mask |= IFF_MULTICAST; |
| 228 | if (param == PARM_on) |
| 229 | flags |= IFF_MULTICAST; |
| 230 | else |
| 231 | flags &= ~IFF_MULTICAST; |
| 232 | } else if (key == ARG_arp) { |
| 233 | if (param < 0) |
| 234 | die_must_be_on_off("arp"); |
| 235 | mask |= IFF_NOARP; |
| 236 | if (param == PARM_on) |
| 237 | flags &= ~IFF_NOARP; |
| 238 | else |
| 239 | flags |= IFF_NOARP; |
| 240 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 241 | } |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 242 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | if (!dev) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 246 | bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | if (newaddr || newbrd) { |
| 250 | halen = get_address(dev, &htype); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 251 | if (newaddr) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 252 | parse_address(dev, htype, halen, newaddr, &ifr0); |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 253 | set_address(&ifr0, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 254 | } |
| 255 | if (newbrd) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 256 | parse_address(dev, htype, halen, newbrd, &ifr1); |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 257 | set_address(&ifr1, 1); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | if (newname && strcmp(dev, newname)) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 262 | do_changename(dev, newname); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 263 | dev = newname; |
| 264 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 265 | if (qlen != -1) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 266 | set_qlen(dev, qlen); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 267 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 268 | if (mtu != -1) { |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 269 | set_mtu(dev, mtu); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 270 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 271 | if (mask) |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 272 | do_chflags(dev, flags, mask); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 276 | static int ipaddr_list_link(char **argv) |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 277 | { |
| 278 | preferred_family = AF_PACKET; |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 279 | return ipaddr_list_or_flush(argv, 0); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 282 | #ifndef NLMSG_TAIL |
| 283 | #define NLMSG_TAIL(nmsg) \ |
| 284 | ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) |
| 285 | #endif |
| 286 | /* Return value becomes exitcode. It's okay to not return at all */ |
| 287 | static int do_change(char **argv, const unsigned rtm) |
| 288 | { |
| 289 | static const char keywords[] ALIGN1 = |
| 290 | "link\0""name\0""type\0""dev\0"; |
| 291 | enum { |
| 292 | ARG_link, |
| 293 | ARG_name, |
| 294 | ARG_type, |
| 295 | ARG_dev, |
| 296 | }; |
| 297 | struct rtnl_handle rth; |
| 298 | struct { |
| 299 | struct nlmsghdr n; |
| 300 | struct ifinfomsg i; |
| 301 | char buf[1024]; |
| 302 | } req; |
Bernhard Reutner-Fischer | d148e48 | 2010-05-25 16:16:28 +0200 | [diff] [blame] | 303 | smalluint arg; |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 304 | char *name_str = NULL, *link_str = NULL, *type_str = NULL, *dev_str = NULL; |
| 305 | |
| 306 | memset(&req, 0, sizeof(req)); |
| 307 | |
| 308 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); |
| 309 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 310 | req.n.nlmsg_type = rtm; |
| 311 | req.i.ifi_family = preferred_family; |
| 312 | if (rtm == RTM_NEWLINK) |
| 313 | req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL; |
| 314 | |
| 315 | while (*argv) { |
| 316 | arg = index_in_substrings(keywords, *argv); |
| 317 | if (arg == ARG_link) { |
| 318 | NEXT_ARG(); |
| 319 | link_str = *argv; |
| 320 | } else if (arg == ARG_name) { |
| 321 | NEXT_ARG(); |
| 322 | name_str = *argv; |
| 323 | } else if (arg == ARG_type) { |
| 324 | NEXT_ARG(); |
| 325 | type_str = *argv; |
| 326 | } else { |
| 327 | if (arg == ARG_dev) { |
| 328 | if (dev_str) |
| 329 | duparg(*argv, "dev"); |
| 330 | NEXT_ARG(); |
| 331 | } |
| 332 | dev_str = *argv; |
| 333 | } |
| 334 | argv++; |
| 335 | } |
| 336 | xrtnl_open(&rth); |
| 337 | ll_init_map(&rth); |
| 338 | if (type_str) { |
| 339 | struct rtattr *linkinfo = NLMSG_TAIL(&req.n); |
| 340 | |
| 341 | addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0); |
| 342 | addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str, |
| 343 | strlen(type_str)); |
| 344 | linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo; |
| 345 | } |
| 346 | if (rtm != RTM_NEWLINK) { |
| 347 | if (!dev_str) |
| 348 | return 1; /* Need a device to delete */ |
| 349 | req.i.ifi_index = xll_name_to_index(dev_str); |
| 350 | } else { |
| 351 | if (!name_str) |
| 352 | name_str = dev_str; |
| 353 | if (link_str) { |
| 354 | int idx = xll_name_to_index(link_str); |
| 355 | addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4); |
| 356 | } |
| 357 | } |
| 358 | if (name_str) { |
| 359 | const size_t name_len = strlen(name_str) + 1; |
| 360 | if (name_len < 2 || name_len > IFNAMSIZ) |
| 361 | invarg(name_str, "name"); |
| 362 | addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name_str, name_len); |
| 363 | } |
| 364 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) |
| 365 | return 2; |
| 366 | return 0; |
| 367 | } |
| 368 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 369 | /* Return value becomes exitcode. It's okay to not return at all */ |
Denys Vlasenko | 2e9b551 | 2010-07-24 23:27:38 +0200 | [diff] [blame] | 370 | int FAST_FUNC do_iplink(char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 371 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 372 | static const char keywords[] ALIGN1 = |
Bernhard Reutner-Fischer | 6faebfa | 2010-05-25 10:34:27 +0200 | [diff] [blame] | 373 | "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] | 374 | if (*argv) { |
| 375 | smalluint key = index_in_substrings(keywords, *argv); |
| 376 | if (key > 5) /* invalid argument */ |
| 377 | bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); |
| 378 | argv++; |
| 379 | if (key <= 1) /* add/delete */ |
| 380 | return do_change(argv, key ? RTM_DELLINK : RTM_NEWLINK); |
| 381 | else if (key == 2) /* set */ |
| 382 | return do_set(argv); |
| 383 | } |
Denis Vlasenko | ed6a49c | 2007-11-18 22:56:25 +0000 | [diff] [blame] | 384 | /* show, lst, list */ |
| 385 | return ipaddr_list_link(argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 386 | } |