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>
20 #include <sys/socket.h>
22 #include <linux/if_packet.h>
23 #include <linux/if_ether.h>
24 #include <linux/sockios.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
28 #include <sys/ioctl.h>
29 #include <linux/sockios.h>
33 #include "ip_common.h"
35 #define IPLINK_IOCTL_COMPAT 1
37 #define LIBDIR "/usr/lib/"
40 static void usage(void) __attribute__((noreturn
));
41 static int iplink_have_newlink(void);
43 void iplink_usage(void)
45 if (iplink_have_newlink()) {
46 fprintf(stderr
, "Usage: ip link add link DEV [ name ] NAME\n");
47 fprintf(stderr
, " [ txqueuelen PACKETS ]\n");
48 fprintf(stderr
, " [ address LLADDR ]\n");
49 fprintf(stderr
, " [ broadcast LLADDR ]\n");
50 fprintf(stderr
, " [ mtu MTU ]\n");
51 fprintf(stderr
, " type TYPE [ ARGS ]\n");
52 fprintf(stderr
, " ip link delete DEV type TYPE [ ARGS ]\n");
53 fprintf(stderr
, "\n");
54 fprintf(stderr
, " ip link set DEVICE [ { up | down } ]\n");
56 fprintf(stderr
, "Usage: ip link set DEVICE [ { up | down } ]\n");
58 fprintf(stderr
, " [ arp { on | off } ]\n");
59 fprintf(stderr
, " [ dynamic { on | off } ]\n");
60 fprintf(stderr
, " [ multicast { on | off } ]\n");
61 fprintf(stderr
, " [ allmulticast { on | off } ]\n");
62 fprintf(stderr
, " [ promisc { on | off } ]\n");
63 fprintf(stderr
, " [ trailers { on | off } ]\n");
64 fprintf(stderr
, " [ txqueuelen PACKETS ]\n");
65 fprintf(stderr
, " [ name NEWNAME ]\n");
66 fprintf(stderr
, " [ address LLADDR ]\n");
67 fprintf(stderr
, " [ broadcast LLADDR ]\n");
68 fprintf(stderr
, " [ mtu MTU ]\n");
69 fprintf(stderr
, " [ netns PID ]\n");
70 fprintf(stderr
, " [ alias NAME ]\n");
71 fprintf(stderr
, " ip link show [ DEVICE ]\n");
73 if (iplink_have_newlink()) {
74 fprintf(stderr
, "\n");
75 fprintf(stderr
, "TYPE := { vlan | veth | vcan | dummy | ifb | macvlan }\n");
80 static void usage(void)
85 static int on_off(char *msg
)
87 fprintf(stderr
, "Error: argument of \"%s\" must be \"on\" or \"off\"\n", msg
);
91 static void *BODY
; /* cached dlopen(NULL) handle */
92 static struct link_util
*linkutil_list
;
94 struct link_util
*get_link_kind(const char *id
)
100 for (l
= linkutil_list
; l
; l
= l
->next
)
101 if (strcmp(l
->id
, id
) == 0)
104 snprintf(buf
, sizeof(buf
), LIBDIR
"/ip/link_%s.so", id
);
105 dlh
= dlopen(buf
, RTLD_LAZY
);
107 /* look in current binary, only open once */
110 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
116 snprintf(buf
, sizeof(buf
), "%s_link_util", id
);
121 l
->next
= linkutil_list
;
126 #if IPLINK_IOCTL_COMPAT
127 static int have_rtnl_newlink
= -1;
129 static int accept_msg(const struct sockaddr_nl
*who
,
130 struct nlmsghdr
*n
, void *arg
)
132 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(n
);
134 if (n
->nlmsg_type
== NLMSG_ERROR
&&
135 (err
->error
== -EOPNOTSUPP
|| err
->error
== -EINVAL
))
136 have_rtnl_newlink
= 0;
138 have_rtnl_newlink
= 1;
142 static int iplink_have_newlink(void)
150 if (have_rtnl_newlink
< 0) {
151 memset(&req
, 0, sizeof(req
));
153 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
154 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_ACK
;
155 req
.n
.nlmsg_type
= RTM_NEWLINK
;
156 req
.i
.ifi_family
= AF_UNSPEC
;
158 rtnl_send(&rth
, (char *)&req
.n
, req
.n
.nlmsg_len
);
159 rtnl_listen(&rth
, accept_msg
, NULL
);
161 return have_rtnl_newlink
;
163 #else /* IPLINK_IOCTL_COMPAT */
164 static int iplink_have_newlink(void)
168 #endif /* ! IPLINK_IOCTL_COMPAT */
176 int iplink_parse(int argc
, char **argv
, struct iplink_req
*req
,
177 char **name
, char **type
, char **link
, char **dev
)
188 if (strcmp(*argv
, "up") == 0) {
189 req
->i
.ifi_change
|= IFF_UP
;
190 req
->i
.ifi_flags
|= IFF_UP
;
191 } else if (strcmp(*argv
, "down") == 0) {
192 req
->i
.ifi_change
|= IFF_UP
;
193 req
->i
.ifi_flags
&= ~IFF_UP
;
194 } else if (strcmp(*argv
, "name") == 0) {
197 } else if (matches(*argv
, "link") == 0) {
200 } else if (matches(*argv
, "address") == 0) {
202 len
= ll_addr_a2n(abuf
, sizeof(abuf
), *argv
);
203 addattr_l(&req
->n
, sizeof(*req
), IFLA_ADDRESS
, abuf
, len
);
204 } else if (matches(*argv
, "broadcast") == 0 ||
205 strcmp(*argv
, "brd") == 0) {
207 len
= ll_addr_a2n(abuf
, sizeof(abuf
), *argv
);
208 addattr_l(&req
->n
, sizeof(*req
), IFLA_BROADCAST
, abuf
, len
);
209 } else if (matches(*argv
, "txqueuelen") == 0 ||
210 strcmp(*argv
, "qlen") == 0 ||
211 matches(*argv
, "txqlen") == 0) {
214 duparg("txqueuelen", *argv
);
215 if (get_integer(&qlen
, *argv
, 0))
216 invarg("Invalid \"txqueuelen\" value\n", *argv
);
217 addattr_l(&req
->n
, sizeof(*req
), IFLA_TXQLEN
, &qlen
, 4);
218 } else if (strcmp(*argv
, "mtu") == 0) {
221 duparg("mtu", *argv
);
222 if (get_integer(&mtu
, *argv
, 0))
223 invarg("Invalid \"mtu\" value\n", *argv
);
224 addattr_l(&req
->n
, sizeof(*req
), IFLA_MTU
, &mtu
, 4);
225 } else if (strcmp(*argv
, "netns") == 0) {
228 duparg("netns", *argv
);
229 if (get_integer(&netns
, *argv
, 0))
230 invarg("Invalid \"netns\" value\n", *argv
);
231 addattr_l(&req
->n
, sizeof(*req
), IFLA_NET_NS_PID
, &netns
, 4);
232 } else if (strcmp(*argv
, "multicast") == 0) {
234 req
->i
.ifi_change
|= IFF_MULTICAST
;
235 if (strcmp(*argv
, "on") == 0) {
236 req
->i
.ifi_flags
|= IFF_MULTICAST
;
237 } else if (strcmp(*argv
, "off") == 0) {
238 req
->i
.ifi_flags
&= ~IFF_MULTICAST
;
240 return on_off("multicast");
241 } else if (strcmp(*argv
, "allmulticast") == 0) {
243 req
->i
.ifi_change
|= IFF_ALLMULTI
;
244 if (strcmp(*argv
, "on") == 0) {
245 req
->i
.ifi_flags
|= IFF_ALLMULTI
;
246 } else if (strcmp(*argv
, "off") == 0) {
247 req
->i
.ifi_flags
&= ~IFF_ALLMULTI
;
249 return on_off("allmulticast");
250 } else if (strcmp(*argv
, "promisc") == 0) {
252 req
->i
.ifi_change
|= IFF_PROMISC
;
253 if (strcmp(*argv
, "on") == 0) {
254 req
->i
.ifi_flags
|= IFF_PROMISC
;
255 } else if (strcmp(*argv
, "off") == 0) {
256 req
->i
.ifi_flags
&= ~IFF_PROMISC
;
258 return on_off("promisc");
259 } else if (strcmp(*argv
, "trailers") == 0) {
261 req
->i
.ifi_change
|= IFF_NOTRAILERS
;
262 if (strcmp(*argv
, "off") == 0) {
263 req
->i
.ifi_flags
|= IFF_NOTRAILERS
;
264 } else if (strcmp(*argv
, "on") == 0) {
265 req
->i
.ifi_flags
&= ~IFF_NOTRAILERS
;
267 return on_off("trailers");
268 } else if (strcmp(*argv
, "arp") == 0) {
270 req
->i
.ifi_change
|= IFF_NOARP
;
271 if (strcmp(*argv
, "on") == 0) {
272 req
->i
.ifi_flags
&= ~IFF_NOARP
;
273 } else if (strcmp(*argv
, "off") == 0) {
274 req
->i
.ifi_flags
|= IFF_NOARP
;
276 return on_off("noarp");
278 } else if (matches(*argv
, "dynamic") == 0) {
280 req
->i
.ifi_change
|= IFF_DYNAMIC
;
281 if (strcmp(*argv
, "on") == 0) {
282 req
->i
.ifi_flags
|= IFF_DYNAMIC
;
283 } else if (strcmp(*argv
, "off") == 0) {
284 req
->i
.ifi_flags
&= ~IFF_DYNAMIC
;
286 return on_off("dynamic");
288 } else if (matches(*argv
, "type") == 0) {
293 } else if (matches(*argv
, "alias") == 0) {
295 addattr_l(&req
->n
, sizeof(*req
), IFLA_IFALIAS
,
296 *argv
, strlen(*argv
));
300 if (strcmp(*argv
, "dev") == 0) {
303 if (matches(*argv
, "help") == 0)
306 duparg2("dev", *argv
);
315 static int iplink_modify(int cmd
, unsigned int flags
, int argc
, char **argv
)
322 struct link_util
*lu
= NULL
;
323 struct iplink_req req
;
326 memset(&req
, 0, sizeof(req
));
328 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
329 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
330 req
.n
.nlmsg_type
= cmd
;
331 req
.i
.ifi_family
= preferred_family
;
333 ret
= iplink_parse(argc
, argv
, &req
, &name
, &type
, &link
, &dev
);
342 struct rtattr
*linkinfo
= NLMSG_TAIL(&req
.n
);
343 addattr_l(&req
.n
, sizeof(req
), IFLA_LINKINFO
, NULL
, 0);
344 addattr_l(&req
.n
, sizeof(req
), IFLA_INFO_KIND
, type
,
347 lu
= get_link_kind(type
);
349 struct rtattr
* data
= NLMSG_TAIL(&req
.n
);
350 addattr_l(&req
.n
, sizeof(req
), IFLA_INFO_DATA
, NULL
, 0);
353 lu
->parse_opt(lu
, argc
, argv
, &req
.n
))
356 data
->rta_len
= (void *)NLMSG_TAIL(&req
.n
) - (void *)data
;
358 if (matches(*argv
, "help") == 0)
360 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". "
361 "Try \"ip link help\".\n", *argv
);
364 linkinfo
->rta_len
= (void *)NLMSG_TAIL(&req
.n
) - (void *)linkinfo
;
365 } else if (flags
& NLM_F_CREATE
) {
366 fprintf(stderr
, "Not enough information: \"type\" argument "
371 if (!(flags
& NLM_F_CREATE
)) {
373 fprintf(stderr
, "Not enough information: \"dev\" "
374 "argument is required.\n");
378 req
.i
.ifi_index
= ll_name_to_index(dev
);
379 if (req
.i
.ifi_index
== 0) {
380 fprintf(stderr
, "Cannot find device \"%s\"\n", dev
);
384 /* Allow "ip link add dev" and "ip link add name" */
391 ifindex
= ll_name_to_index(link
);
393 fprintf(stderr
, "Cannot find device \"%s\"\n",
397 addattr_l(&req
.n
, sizeof(req
), IFLA_LINK
, &ifindex
, 4);
402 len
= strlen(name
) + 1;
404 invarg("\"\" is not a valid device identifier\n", "name");
406 invarg("\"name\" too long\n", name
);
407 addattr_l(&req
.n
, sizeof(req
), IFLA_IFNAME
, name
, len
);
410 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
416 #if IPLINK_IOCTL_COMPAT
417 static int get_ctl_fd(void)
422 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
426 fd
= socket(PF_PACKET
, SOCK_DGRAM
, 0);
429 fd
= socket(PF_INET6
, SOCK_DGRAM
, 0);
433 perror("Cannot create control socket");
437 static int do_chflags(const char *dev
, __u32 flags
, __u32 mask
)
443 strncpy(ifr
.ifr_name
, dev
, IFNAMSIZ
);
447 err
= ioctl(fd
, SIOCGIFFLAGS
, &ifr
);
449 perror("SIOCGIFFLAGS");
453 if ((ifr
.ifr_flags
^flags
)&mask
) {
454 ifr
.ifr_flags
&= ~mask
;
455 ifr
.ifr_flags
|= mask
&flags
;
456 err
= ioctl(fd
, SIOCSIFFLAGS
, &ifr
);
458 perror("SIOCSIFFLAGS");
464 static int do_changename(const char *dev
, const char *newdev
)
470 strncpy(ifr
.ifr_name
, dev
, IFNAMSIZ
);
471 strncpy(ifr
.ifr_newname
, newdev
, IFNAMSIZ
);
475 err
= ioctl(fd
, SIOCSIFNAME
, &ifr
);
477 perror("SIOCSIFNAME");
485 static int set_qlen(const char *dev
, int qlen
)
494 memset(&ifr
, 0, sizeof(ifr
));
495 strncpy(ifr
.ifr_name
, dev
, IFNAMSIZ
);
497 if (ioctl(s
, SIOCSIFTXQLEN
, &ifr
) < 0) {
498 perror("SIOCSIFXQLEN");
507 static int set_mtu(const char *dev
, int mtu
)
516 memset(&ifr
, 0, sizeof(ifr
));
517 strncpy(ifr
.ifr_name
, dev
, IFNAMSIZ
);
519 if (ioctl(s
, SIOCSIFMTU
, &ifr
) < 0) {
520 perror("SIOCSIFMTU");
529 static int get_address(const char *dev
, int *htype
)
532 struct sockaddr_ll me
;
536 s
= socket(PF_PACKET
, SOCK_DGRAM
, 0);
538 perror("socket(PF_PACKET)");
542 memset(&ifr
, 0, sizeof(ifr
));
543 strncpy(ifr
.ifr_name
, dev
, IFNAMSIZ
);
544 if (ioctl(s
, SIOCGIFINDEX
, &ifr
) < 0) {
545 perror("SIOCGIFINDEX");
550 memset(&me
, 0, sizeof(me
));
551 me
.sll_family
= AF_PACKET
;
552 me
.sll_ifindex
= ifr
.ifr_ifindex
;
553 me
.sll_protocol
= htons(ETH_P_LOOP
);
554 if (bind(s
, (struct sockaddr
*)&me
, sizeof(me
)) == -1) {
561 if (getsockname(s
, (struct sockaddr
*)&me
, &alen
) == -1) {
562 perror("getsockname");
567 *htype
= me
.sll_hatype
;
571 static int parse_address(const char *dev
, int hatype
, int halen
,
572 char *lla
, struct ifreq
*ifr
)
576 memset(ifr
, 0, sizeof(*ifr
));
577 strncpy(ifr
->ifr_name
, dev
, IFNAMSIZ
);
578 ifr
->ifr_hwaddr
.sa_family
= hatype
;
579 alen
= ll_addr_a2n(ifr
->ifr_hwaddr
.sa_data
, 14, lla
);
583 fprintf(stderr
, "Wrong address (%s) length: expected %d bytes\n", lla
, halen
);
589 static int set_address(struct ifreq
*ifr
, int brd
)
596 if (ioctl(s
, brd
?SIOCSIFHWBROADCAST
:SIOCSIFHWADDR
, ifr
) < 0) {
597 perror(brd
?"SIOCSIFHWBROADCAST":"SIOCSIFHWADDR");
606 static int do_set(int argc
, char **argv
)
613 char *newaddr
= NULL
;
615 struct ifreq ifr0
, ifr1
;
616 char *newname
= NULL
;
620 if (strcmp(*argv
, "up") == 0) {
623 } else if (strcmp(*argv
, "down") == 0) {
626 } else if (strcmp(*argv
, "name") == 0) {
629 } else if (matches(*argv
, "address") == 0) {
632 } else if (matches(*argv
, "broadcast") == 0 ||
633 strcmp(*argv
, "brd") == 0) {
636 } else if (matches(*argv
, "txqueuelen") == 0 ||
637 strcmp(*argv
, "qlen") == 0 ||
638 matches(*argv
, "txqlen") == 0) {
641 duparg("txqueuelen", *argv
);
642 if (get_integer(&qlen
, *argv
, 0))
643 invarg("Invalid \"txqueuelen\" value\n", *argv
);
644 } else if (strcmp(*argv
, "mtu") == 0) {
647 duparg("mtu", *argv
);
648 if (get_integer(&mtu
, *argv
, 0))
649 invarg("Invalid \"mtu\" value\n", *argv
);
650 } else if (strcmp(*argv
, "multicast") == 0) {
652 mask
|= IFF_MULTICAST
;
653 if (strcmp(*argv
, "on") == 0) {
654 flags
|= IFF_MULTICAST
;
655 } else if (strcmp(*argv
, "off") == 0) {
656 flags
&= ~IFF_MULTICAST
;
658 return on_off("multicast");
659 } else if (strcmp(*argv
, "allmulticast") == 0) {
661 mask
|= IFF_ALLMULTI
;
662 if (strcmp(*argv
, "on") == 0) {
663 flags
|= IFF_ALLMULTI
;
664 } else if (strcmp(*argv
, "off") == 0) {
665 flags
&= ~IFF_ALLMULTI
;
667 return on_off("allmulticast");
668 } else if (strcmp(*argv
, "promisc") == 0) {
671 if (strcmp(*argv
, "on") == 0) {
672 flags
|= IFF_PROMISC
;
673 } else if (strcmp(*argv
, "off") == 0) {
674 flags
&= ~IFF_PROMISC
;
676 return on_off("promisc");
677 } else if (strcmp(*argv
, "trailers") == 0) {
679 mask
|= IFF_NOTRAILERS
;
680 if (strcmp(*argv
, "off") == 0) {
681 flags
|= IFF_NOTRAILERS
;
682 } else if (strcmp(*argv
, "on") == 0) {
683 flags
&= ~IFF_NOTRAILERS
;
685 return on_off("trailers");
686 } else if (strcmp(*argv
, "arp") == 0) {
689 if (strcmp(*argv
, "on") == 0) {
691 } else if (strcmp(*argv
, "off") == 0) {
694 return on_off("noarp");
696 } else if (matches(*argv
, "dynamic") == 0) {
699 if (strcmp(*argv
, "on") == 0) {
700 flags
|= IFF_DYNAMIC
;
701 } else if (strcmp(*argv
, "off") == 0) {
702 flags
&= ~IFF_DYNAMIC
;
704 return on_off("dynamic");
707 if (strcmp(*argv
, "dev") == 0) {
710 if (matches(*argv
, "help") == 0)
713 duparg2("dev", *argv
);
720 fprintf(stderr
, "Not enough of information: \"dev\" argument is required.\n");
724 if (newaddr
|| newbrd
) {
725 halen
= get_address(dev
, &htype
);
729 if (parse_address(dev
, htype
, halen
, newaddr
, &ifr0
) < 0)
733 if (parse_address(dev
, htype
, halen
, newbrd
, &ifr1
) < 0)
738 if (newname
&& strcmp(dev
, newname
)) {
739 if (strlen(newname
) == 0)
740 invarg("\"\" is not a valid device identifier\n", "name");
741 if (do_changename(dev
, newname
) < 0)
746 if (set_qlen(dev
, qlen
) < 0)
750 if (set_mtu(dev
, mtu
) < 0)
753 if (newaddr
|| newbrd
) {
755 if (set_address(&ifr1
, 1) < 0)
759 if (set_address(&ifr0
, 0) < 0)
764 return do_chflags(dev
, flags
, mask
);
767 #endif /* IPLINK_IOCTL_COMPAT */
769 int do_iplink(int argc
, char **argv
)
772 if (iplink_have_newlink()) {
773 if (matches(*argv
, "add") == 0)
774 return iplink_modify(RTM_NEWLINK
,
775 NLM_F_CREATE
|NLM_F_EXCL
,
777 if (matches(*argv
, "set") == 0 ||
778 matches(*argv
, "change") == 0)
779 return iplink_modify(RTM_NEWLINK
, 0,
781 if (matches(*argv
, "replace") == 0)
782 return iplink_modify(RTM_NEWLINK
,
783 NLM_F_CREATE
|NLM_F_REPLACE
,
785 if (matches(*argv
, "delete") == 0)
786 return iplink_modify(RTM_DELLINK
, 0,
789 #if IPLINK_IOCTL_COMPAT
790 if (matches(*argv
, "set") == 0)
791 return do_set(argc
-1, argv
+1);
794 if (matches(*argv
, "show") == 0 ||
795 matches(*argv
, "lst") == 0 ||
796 matches(*argv
, "list") == 0)
797 return ipaddr_list_link(argc
-1, argv
+1);
798 if (matches(*argv
, "help") == 0)
801 return ipaddr_list_link(0, NULL
);
803 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip link help\".\n", *argv
);