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