4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
19 #include <sys/socket.h>
21 #include <linux/if_packet.h>
22 #include <linux/if_ether.h>
23 #include <linux/sockios.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
27 #include <sys/ioctl.h>
28 #include <linux/sockios.h>
32 #include "ip_common.h"
35 static void usage(void) __attribute__((noreturn));
37 void iplink_usage(void)
39 fprintf(stderr, "Usage: ip link set DEVICE { up | down |\n");
40 fprintf(stderr, " arp { on | off } |\n");
41 fprintf(stderr, " dynamic { on | off } |\n");
42 fprintf(stderr, " multicast { on | off } |\n");
43 fprintf(stderr, " allmulticast { on | off } |\n");
44 fprintf(stderr, " promisc { on | off } |\n");
45 fprintf(stderr, " trailers { on | off } |\n");
46 fprintf(stderr, " txqueuelen PACKETS |\n");
47 fprintf(stderr, " name NEWNAME |\n");
48 fprintf(stderr, " address LLADDR | broadcast LLADDR |\n");
49 fprintf(stderr, " mtu MTU }\n");
50 fprintf(stderr, " ip link show [ DEVICE ]\n");
54 static void usage(void)
59 static int on_off(char *msg)
61 fprintf(stderr, "Error: argument of \"%s\" must be \"on\" or \"off\"\n", msg);
65 static int get_ctl_fd(void)
70 fd = socket(PF_INET, SOCK_DGRAM, 0);
74 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
77 fd = socket(PF_INET6, SOCK_DGRAM, 0);
81 perror("Cannot create control socket");
85 static int do_chflags(const char *dev, __u32 flags, __u32 mask)
91 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
95 err = ioctl(fd, SIOCGIFFLAGS, &ifr);
97 perror("SIOCGIFFLAGS");
101 if ((ifr.ifr_flags^flags)&mask) {
102 ifr.ifr_flags &= ~mask;
103 ifr.ifr_flags |= mask&flags;
104 err = ioctl(fd, SIOCSIFFLAGS, &ifr);
106 perror("SIOCSIFFLAGS");
112 static int do_changename(const char *dev, const char *newdev)
118 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
119 strncpy(ifr.ifr_newname, newdev, IFNAMSIZ);
123 err = ioctl(fd, SIOCSIFNAME, &ifr);
125 perror("SIOCSIFNAME");
133 static int set_qlen(const char *dev, int qlen)
142 memset(&ifr, 0, sizeof(ifr));
143 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
145 if (ioctl(s, SIOCSIFTXQLEN, &ifr) < 0) {
146 perror("SIOCSIFXQLEN");
155 static int set_mtu(const char *dev, int mtu)
164 memset(&ifr, 0, sizeof(ifr));
165 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
167 if (ioctl(s, SIOCSIFMTU, &ifr) < 0) {
168 perror("SIOCSIFMTU");
177 static int get_address(const char *dev, int *htype)
180 struct sockaddr_ll me;
184 s = socket(PF_PACKET, SOCK_DGRAM, 0);
186 perror("socket(PF_PACKET)");
190 memset(&ifr, 0, sizeof(ifr));
191 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
192 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
193 perror("SIOCGIFINDEX");
198 memset(&me, 0, sizeof(me));
199 me.sll_family = AF_PACKET;
200 me.sll_ifindex = ifr.ifr_ifindex;
201 me.sll_protocol = htons(ETH_P_LOOP);
202 if (bind(s, (struct sockaddr*)&me, sizeof(me)) == -1) {
209 if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
210 perror("getsockname");
215 *htype = me.sll_hatype;
219 static int parse_address(const char *dev, int hatype, int halen,
220 char *lla, struct ifreq *ifr)
224 memset(ifr, 0, sizeof(*ifr));
225 strncpy(ifr->ifr_name, dev, IFNAMSIZ);
226 ifr->ifr_hwaddr.sa_family = hatype;
227 alen = ll_addr_a2n(ifr->ifr_hwaddr.sa_data, 14, lla);
231 fprintf(stderr, "Wrong address (%s) length: expected %d bytes\n", lla, halen);
237 static int set_address(struct ifreq *ifr, int brd)
244 if (ioctl(s, brd?SIOCSIFHWBROADCAST:SIOCSIFHWADDR, ifr) < 0) {
245 perror(brd?"SIOCSIFHWBROADCAST":"SIOCSIFHWADDR");
254 static int do_set(int argc, char **argv)
261 char *newaddr = NULL;
263 struct ifreq ifr0, ifr1;
264 char *newname = NULL;
268 if (strcmp(*argv, "up") == 0) {
271 } else if (strcmp(*argv, "down") == 0) {
274 } else if (strcmp(*argv, "name") == 0) {
277 } else if (matches(*argv, "address") == 0) {
280 } else if (matches(*argv, "broadcast") == 0 ||
281 strcmp(*argv, "brd") == 0) {
284 } else if (matches(*argv, "txqueuelen") == 0 ||
285 strcmp(*argv, "qlen") == 0 ||
286 matches(*argv, "txqlen") == 0) {
289 duparg("txqueuelen", *argv);
290 if (get_integer(&qlen, *argv, 0))
291 invarg("Invalid \"txqueuelen\" value\n", *argv);
292 } else if (strcmp(*argv, "mtu") == 0) {
295 duparg("mtu", *argv);
296 if (get_integer(&mtu, *argv, 0))
297 invarg("Invalid \"mtu\" value\n", *argv);
298 } else if (strcmp(*argv, "multicast") == 0) {
300 mask |= IFF_MULTICAST;
301 if (strcmp(*argv, "on") == 0) {
302 flags |= IFF_MULTICAST;
303 } else if (strcmp(*argv, "off") == 0) {
304 flags &= ~IFF_MULTICAST;
306 return on_off("multicast");
307 } else if (strcmp(*argv, "allmulticast") == 0) {
309 mask |= IFF_ALLMULTI;
310 if (strcmp(*argv, "on") == 0) {
311 flags |= IFF_ALLMULTI;
312 } else if (strcmp(*argv, "off") == 0) {
313 flags &= ~IFF_ALLMULTI;
315 return on_off("allmulticast");
316 } else if (strcmp(*argv, "promisc") == 0) {
319 if (strcmp(*argv, "on") == 0) {
320 flags |= IFF_PROMISC;
321 } else if (strcmp(*argv, "off") == 0) {
322 flags &= ~IFF_PROMISC;
324 return on_off("promisc");
325 } else if (strcmp(*argv, "trailers") == 0) {
327 mask |= IFF_NOTRAILERS;
328 if (strcmp(*argv, "off") == 0) {
329 flags |= IFF_NOTRAILERS;
330 } else if (strcmp(*argv, "on") == 0) {
331 flags &= ~IFF_NOTRAILERS;
333 return on_off("trailers");
334 } else if (strcmp(*argv, "arp") == 0) {
337 if (strcmp(*argv, "on") == 0) {
339 } else if (strcmp(*argv, "off") == 0) {
342 return on_off("noarp");
344 } else if (matches(*argv, "dynamic") == 0) {
347 if (strcmp(*argv, "on") == 0) {
348 flags |= IFF_DYNAMIC;
349 } else if (strcmp(*argv, "off") == 0) {
350 flags &= ~IFF_DYNAMIC;
352 return on_off("dynamic");
355 if (strcmp(*argv, "dev") == 0) {
358 if (matches(*argv, "help") == 0)
361 duparg2("dev", *argv);
368 fprintf(stderr, "Not enough of information: \"dev\" argument is required.\n");
372 if (newaddr || newbrd) {
373 halen = get_address(dev, &htype);
377 if (parse_address(dev, htype, halen, newaddr, &ifr0) < 0)
381 if (parse_address(dev, htype, halen, newbrd, &ifr1) < 0)
386 if (newname && strcmp(dev, newname)) {
387 if (do_changename(dev, newname) < 0)
392 if (set_qlen(dev, qlen) < 0)
396 if (set_mtu(dev, mtu) < 0)
399 if (newaddr || newbrd) {
401 if (set_address(&ifr1, 1) < 0)
405 if (set_address(&ifr0, 0) < 0)
410 return do_chflags(dev, flags, mask);
414 int do_iplink(int argc, char **argv)
417 if (matches(*argv, "set") == 0)
418 return do_set(argc-1, argv+1);
419 if (matches(*argv, "show") == 0 ||
420 matches(*argv, "lst") == 0 ||
421 matches(*argv, "list") == 0)
422 return ipaddr_list_link(argc-1, argv+1);
423 if (matches(*argv, "help") == 0)
426 return ipaddr_list_link(0, NULL);
428 fprintf(stderr, "Command \"%s\" is unknown, try \"ip link help\".\n", *argv);