"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 | */ |
| 9 | |
Rob Landley | ecae66a | 2006-06-02 20:53:38 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
| 11 | |
Glenn L McGrath | 275be87 | 2002-12-16 07:37:21 +0000 | [diff] [blame] | 12 | #include <sys/ioctl.h> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 13 | #include <sys/socket.h> |
Glenn L McGrath | 275be87 | 2002-12-16 07:37:21 +0000 | [diff] [blame] | 14 | |
| 15 | #include <errno.h> |
Glenn L McGrath | 275be87 | 2002-12-16 07:37:21 +0000 | [diff] [blame] | 16 | #include <string.h> |
| 17 | #include <unistd.h> |
| 18 | |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 19 | #include <net/if.h> |
| 20 | #include <net/if_packet.h> |
| 21 | #include <netpacket/packet.h> |
| 22 | |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 23 | #include <net/ethernet.h> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 24 | |
| 25 | #include "rt_names.h" |
| 26 | #include "utils.h" |
| 27 | #include "ip_common.h" |
| 28 | |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 29 | /* take from linux/sockios.h */ |
| 30 | #define SIOCSIFNAME 0x8923 /* set interface name */ |
| 31 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 32 | static int on_off(char *msg) |
| 33 | { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 34 | bb_error_msg("error: argument of \"%s\" must be \"on\" or \"off\"", msg); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 35 | return -1; |
| 36 | } |
| 37 | |
| 38 | static int get_ctl_fd(void) |
| 39 | { |
| 40 | int s_errno; |
| 41 | int fd; |
| 42 | |
| 43 | fd = socket(PF_INET, SOCK_DGRAM, 0); |
| 44 | if (fd >= 0) |
| 45 | return fd; |
| 46 | s_errno = errno; |
| 47 | fd = socket(PF_PACKET, SOCK_DGRAM, 0); |
| 48 | if (fd >= 0) |
| 49 | return fd; |
| 50 | fd = socket(PF_INET6, SOCK_DGRAM, 0); |
| 51 | if (fd >= 0) |
| 52 | return fd; |
| 53 | errno = s_errno; |
| 54 | perror("Cannot create control socket"); |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | static int do_chflags(char *dev, __u32 flags, __u32 mask) |
| 59 | { |
| 60 | struct ifreq ifr; |
| 61 | int fd; |
| 62 | int err; |
| 63 | |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame^] | 64 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 65 | fd = get_ctl_fd(); |
| 66 | if (fd < 0) |
| 67 | return -1; |
| 68 | err = ioctl(fd, SIOCGIFFLAGS, &ifr); |
| 69 | if (err) { |
| 70 | perror("SIOCGIFFLAGS"); |
| 71 | close(fd); |
| 72 | return -1; |
| 73 | } |
| 74 | if ((ifr.ifr_flags^flags)&mask) { |
| 75 | ifr.ifr_flags &= ~mask; |
| 76 | ifr.ifr_flags |= mask&flags; |
| 77 | err = ioctl(fd, SIOCSIFFLAGS, &ifr); |
| 78 | if (err) |
| 79 | perror("SIOCSIFFLAGS"); |
| 80 | } |
| 81 | close(fd); |
| 82 | return err; |
| 83 | } |
| 84 | |
| 85 | static int do_changename(char *dev, char *newdev) |
| 86 | { |
| 87 | struct ifreq ifr; |
| 88 | int fd; |
| 89 | int err; |
| 90 | |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame^] | 91 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
| 92 | strncpy(ifr.ifr_newname, newdev, sizeof(ifr.ifr_newname)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 93 | fd = get_ctl_fd(); |
| 94 | if (fd < 0) |
| 95 | return -1; |
| 96 | err = ioctl(fd, SIOCSIFNAME, &ifr); |
| 97 | if (err) { |
| 98 | perror("SIOCSIFNAME"); |
| 99 | close(fd); |
| 100 | return -1; |
| 101 | } |
| 102 | close(fd); |
| 103 | return err; |
| 104 | } |
| 105 | |
| 106 | static int set_qlen(char *dev, int qlen) |
| 107 | { |
| 108 | struct ifreq ifr; |
| 109 | int s; |
| 110 | |
| 111 | s = get_ctl_fd(); |
| 112 | if (s < 0) |
| 113 | return -1; |
| 114 | |
| 115 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame^] | 116 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 117 | ifr.ifr_qlen = qlen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 118 | if (ioctl(s, SIOCSIFTXQLEN, &ifr) < 0) { |
| 119 | perror("SIOCSIFXQLEN"); |
| 120 | close(s); |
| 121 | return -1; |
| 122 | } |
| 123 | close(s); |
| 124 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 125 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static int set_mtu(char *dev, int mtu) |
| 129 | { |
| 130 | struct ifreq ifr; |
| 131 | int s; |
| 132 | |
| 133 | s = get_ctl_fd(); |
| 134 | if (s < 0) |
| 135 | return -1; |
| 136 | |
| 137 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame^] | 138 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 139 | ifr.ifr_mtu = mtu; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 140 | if (ioctl(s, SIOCSIFMTU, &ifr) < 0) { |
| 141 | perror("SIOCSIFMTU"); |
| 142 | close(s); |
| 143 | return -1; |
| 144 | } |
| 145 | close(s); |
| 146 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 147 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static int get_address(char *dev, int *htype) |
| 151 | { |
| 152 | struct ifreq ifr; |
| 153 | struct sockaddr_ll me; |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 154 | socklen_t alen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 155 | int s; |
| 156 | |
| 157 | s = socket(PF_PACKET, SOCK_DGRAM, 0); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 158 | if (s < 0) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 159 | perror("socket(PF_PACKET)"); |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | memset(&ifr, 0, sizeof(ifr)); |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame^] | 164 | strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 165 | if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { |
| 166 | perror("SIOCGIFINDEX"); |
| 167 | close(s); |
| 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | memset(&me, 0, sizeof(me)); |
| 172 | me.sll_family = AF_PACKET; |
| 173 | me.sll_ifindex = ifr.ifr_ifindex; |
| 174 | me.sll_protocol = htons(ETH_P_LOOP); |
| 175 | if (bind(s, (struct sockaddr*)&me, sizeof(me)) == -1) { |
| 176 | perror("bind"); |
| 177 | close(s); |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | alen = sizeof(me); |
| 182 | if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) { |
| 183 | perror("getsockname"); |
| 184 | close(s); |
| 185 | return -1; |
| 186 | } |
| 187 | close(s); |
| 188 | *htype = me.sll_hatype; |
| 189 | return me.sll_halen; |
| 190 | } |
| 191 | |
| 192 | static int parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr) |
| 193 | { |
| 194 | int alen; |
| 195 | |
| 196 | memset(ifr, 0, sizeof(*ifr)); |
Denis Vlasenko | 229b3d2 | 2006-11-27 23:44:57 +0000 | [diff] [blame^] | 197 | strncpy(ifr->ifr_name, dev, sizeof(ifr->ifr_name)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 198 | ifr->ifr_hwaddr.sa_family = hatype; |
Eric Andersen | d78aea8 | 2006-01-30 18:00:02 +0000 | [diff] [blame] | 199 | alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), 14, lla); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 200 | if (alen < 0) |
| 201 | return -1; |
| 202 | if (alen != halen) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 203 | bb_error_msg("wrong address (%s) length: expected %d bytes", lla, halen); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 204 | return -1; |
| 205 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 206 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static int set_address(struct ifreq *ifr, int brd) |
| 210 | { |
| 211 | int s; |
| 212 | |
| 213 | s = get_ctl_fd(); |
| 214 | if (s < 0) |
| 215 | return -1; |
| 216 | if (ioctl(s, brd?SIOCSIFHWBROADCAST:SIOCSIFHWADDR, ifr) < 0) { |
| 217 | perror(brd?"SIOCSIFHWBROADCAST":"SIOCSIFHWADDR"); |
| 218 | close(s); |
| 219 | return -1; |
| 220 | } |
| 221 | close(s); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 222 | return 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | |
| 226 | static int do_set(int argc, char **argv) |
| 227 | { |
| 228 | char *dev = NULL; |
| 229 | __u32 mask = 0; |
| 230 | __u32 flags = 0; |
| 231 | int qlen = -1; |
| 232 | int mtu = -1; |
| 233 | char *newaddr = NULL; |
| 234 | char *newbrd = NULL; |
| 235 | struct ifreq ifr0, ifr1; |
| 236 | char *newname = NULL; |
| 237 | int htype, halen; |
| 238 | |
| 239 | while (argc > 0) { |
| 240 | if (strcmp(*argv, "up") == 0) { |
| 241 | mask |= IFF_UP; |
| 242 | flags |= IFF_UP; |
| 243 | } else if (strcmp(*argv, "down") == 0) { |
| 244 | mask |= IFF_UP; |
| 245 | flags &= ~IFF_UP; |
| 246 | } else if (strcmp(*argv, "name") == 0) { |
| 247 | NEXT_ARG(); |
| 248 | newname = *argv; |
| 249 | } else if (strcmp(*argv, "mtu") == 0) { |
| 250 | NEXT_ARG(); |
| 251 | if (mtu != -1) |
| 252 | duparg("mtu", *argv); |
| 253 | if (get_integer(&mtu, *argv, 0)) |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 254 | invarg(*argv, "mtu"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 255 | } else if (strcmp(*argv, "multicast") == 0) { |
| 256 | NEXT_ARG(); |
| 257 | mask |= IFF_MULTICAST; |
| 258 | if (strcmp(*argv, "on") == 0) { |
| 259 | flags |= IFF_MULTICAST; |
| 260 | } else if (strcmp(*argv, "off") == 0) { |
| 261 | flags &= ~IFF_MULTICAST; |
| 262 | } else |
| 263 | return on_off("multicast"); |
| 264 | } else if (strcmp(*argv, "arp") == 0) { |
| 265 | NEXT_ARG(); |
| 266 | mask |= IFF_NOARP; |
| 267 | if (strcmp(*argv, "on") == 0) { |
| 268 | flags &= ~IFF_NOARP; |
| 269 | } else if (strcmp(*argv, "off") == 0) { |
| 270 | flags |= IFF_NOARP; |
| 271 | } else |
| 272 | return on_off("noarp"); |
Rob Landley | 483027f | 2005-12-15 05:29:48 +0000 | [diff] [blame] | 273 | } else if (strcmp(*argv, "addr") == 0) { |
| 274 | NEXT_ARG(); |
| 275 | newaddr = *argv; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 276 | } else { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 277 | if (strcmp(*argv, "dev") == 0) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 278 | NEXT_ARG(); |
| 279 | } |
| 280 | if (dev) |
| 281 | duparg2("dev", *argv); |
| 282 | dev = *argv; |
| 283 | } |
| 284 | argc--; argv++; |
| 285 | } |
| 286 | |
| 287 | if (!dev) { |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 288 | bb_error_msg(bb_msg_requires_arg, "\"dev\""); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 289 | exit(-1); |
| 290 | } |
| 291 | |
| 292 | if (newaddr || newbrd) { |
| 293 | halen = get_address(dev, &htype); |
| 294 | if (halen < 0) |
| 295 | return -1; |
| 296 | if (newaddr) { |
| 297 | if (parse_address(dev, htype, halen, newaddr, &ifr0) < 0) |
| 298 | return -1; |
| 299 | } |
| 300 | if (newbrd) { |
| 301 | if (parse_address(dev, htype, halen, newbrd, &ifr1) < 0) |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 302 | return -1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | if (newname && strcmp(dev, newname)) { |
| 307 | if (do_changename(dev, newname) < 0) |
| 308 | return -1; |
| 309 | dev = newname; |
| 310 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 311 | if (qlen != -1) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 312 | if (set_qlen(dev, qlen) < 0) |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 313 | return -1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 314 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 315 | if (mtu != -1) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 316 | if (set_mtu(dev, mtu) < 0) |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 317 | return -1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 318 | } |
| 319 | if (newaddr || newbrd) { |
| 320 | if (newbrd) { |
| 321 | if (set_address(&ifr1, 1) < 0) |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 322 | return -1; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 323 | } |
| 324 | if (newaddr) { |
| 325 | if (set_address(&ifr0, 0) < 0) |
| 326 | return -1; |
| 327 | } |
| 328 | } |
| 329 | if (mask) |
| 330 | return do_chflags(dev, flags, mask); |
| 331 | return 0; |
| 332 | } |
| 333 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 334 | static int ipaddr_list_link(int argc, char **argv) |
| 335 | { |
| 336 | preferred_family = AF_PACKET; |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 337 | return ipaddr_list_or_flush(argc, argv, 0); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 340 | int do_iplink(int argc, char **argv) |
| 341 | { |
| 342 | if (argc > 0) { |
| 343 | if (matches(*argv, "set") == 0) |
| 344 | return do_set(argc-1, argv+1); |
| 345 | if (matches(*argv, "show") == 0 || |
| 346 | matches(*argv, "lst") == 0 || |
| 347 | matches(*argv, "list") == 0) |
| 348 | return ipaddr_list_link(argc-1, argv+1); |
| 349 | } else |
| 350 | return ipaddr_list_link(0, NULL); |
| 351 | |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 352 | bb_error_msg("command \"%s\" is unknown", *argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 353 | exit(-1); |
| 354 | } |