]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/iplink.c
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
));
42 void iplink_usage(void)
44 fprintf(stderr
, "Usage: ip link set DEVICE { up | down |\n");
45 fprintf(stderr
, " arp { on | off } |\n");
46 fprintf(stderr
, " dynamic { on | off } |\n");
47 fprintf(stderr
, " multicast { on | off } |\n");
48 fprintf(stderr
, " allmulticast { on | off } |\n");
49 fprintf(stderr
, " promisc { on | off } |\n");
50 fprintf(stderr
, " trailers { on | off } |\n");
51 fprintf(stderr
, " txqueuelen PACKETS |\n");
52 fprintf(stderr
, " name NEWNAME |\n");
53 fprintf(stderr
, " address LLADDR | broadcast LLADDR |\n");
54 fprintf(stderr
, " mtu MTU }\n");
55 fprintf(stderr
, " netns PID }\n");
56 fprintf(stderr
, " ip link show [ DEVICE ]\n");
60 static void usage(void)
65 static int on_off(char *msg
)
67 fprintf(stderr
, "Error: argument of \"%s\" must be \"on\" or \"off\"\n", msg
);
71 static void *BODY
; /* cached dlopen(NULL) handle */
72 static struct link_util
*linkutil_list
;
74 struct link_util
*get_link_kind(const char *id
)
80 for (l
= linkutil_list
; l
; l
= l
->next
)
81 if (strcmp(l
->id
, id
) == 0)
84 snprintf(buf
, sizeof(buf
), LIBDIR
"/ip/link_%s.so", id
);
85 dlh
= dlopen(buf
, RTLD_LAZY
);
87 /* look in current binary, only open once */
90 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
96 snprintf(buf
, sizeof(buf
), "%s_link_util", id
);
101 l
->next
= linkutil_list
;
106 #if IPLINK_IOCTL_COMPAT
107 static int have_rtnl_newlink
= -1;
109 static int accept_msg(const struct sockaddr_nl
*who
,
110 struct nlmsghdr
*n
, void *arg
)
112 struct nlmsgerr
*err
= (struct nlmsgerr
*)NLMSG_DATA(n
);
114 if (n
->nlmsg_type
== NLMSG_ERROR
&&
115 (err
->error
== -EOPNOTSUPP
|| err
->error
== -EINVAL
))
116 have_rtnl_newlink
= 0;
118 have_rtnl_newlink
= 1;
122 static int iplink_have_newlink(void)
130 if (have_rtnl_newlink
< 0) {
131 memset(&req
, 0, sizeof(req
));
133 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
134 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_ACK
;
135 req
.n
.nlmsg_type
= RTM_NEWLINK
;
136 req
.i
.ifi_family
= AF_UNSPEC
;
138 rtnl_send(&rth
, (char *)&req
.n
, req
.n
.nlmsg_len
);
139 rtnl_listen(&rth
, accept_msg
, NULL
);
141 return have_rtnl_newlink
;
143 #else /* IPLINK_IOCTL_COMPAT */
144 static int iplink_have_newlink(void)
148 #endif /* ! IPLINK_IOCTL_COMPAT */
156 int iplink_parse(int argc
, char **argv
, struct iplink_req
*req
,
157 char **name
, char **type
, char **link
, char **dev
)
168 if (strcmp(*argv
, "up") == 0) {
169 req
->i
.ifi_change
|= IFF_UP
;
170 req
->i
.ifi_flags
|= IFF_UP
;
171 } else if (strcmp(*argv
, "down") == 0) {
172 req
->i
.ifi_change
|= IFF_UP
;
173 req
->i
.ifi_flags
&= ~IFF_UP
;
174 } else if (strcmp(*argv
, "name") == 0) {
177 } else if (matches(*argv
, "link") == 0) {
180 } else if (matches(*argv
, "address") == 0) {
182 len
= ll_addr_a2n(abuf
, sizeof(abuf
), *argv
);
183 addattr_l(&req
->n
, sizeof(*req
), IFLA_ADDRESS
, abuf
, len
);
184 } else if (matches(*argv
, "broadcast") == 0 ||
185 strcmp(*argv
, "brd") == 0) {
187 len
= ll_addr_a2n(abuf
, sizeof(abuf
), *argv
);
188 addattr_l(&req
->n
, sizeof(*req
), IFLA_BROADCAST
, abuf
, len
);
189 } else if (matches(*argv
, "txqueuelen") == 0 ||
190 strcmp(*argv
, "qlen") == 0 ||
191 matches(*argv
, "txqlen") == 0) {
194 duparg("txqueuelen", *argv
);
195 if (get_integer(&qlen
, *argv
, 0))
196 invarg("Invalid \"txqueuelen\" value\n", *argv
);
197 addattr_l(&req
->n
, sizeof(*req
), IFLA_TXQLEN
, &qlen
, 4);
198 } else if (strcmp(*argv
, "mtu") == 0) {
201 duparg("mtu", *argv
);
202 if (get_integer(&mtu
, *argv
, 0))
203 invarg("Invalid \"mtu\" value\n", *argv
);
204 addattr_l(&req
->n
, sizeof(*req
), IFLA_MTU
, &mtu
, 4);
205 } else if (strcmp(*argv
, "netns") == 0) {
208 duparg("netns", *argv
);
209 if (get_integer(&netns
, *argv
, 0))
210 invarg("Invalid \"netns\" value\n", *argv
);
211 addattr_l(&req
->n
, sizeof(*req
), IFLA_NET_NS_PID
, &netns
, 4);
212 } else if (strcmp(*argv
, "multicast") == 0) {
214 req
->i
.ifi_change
|= IFF_MULTICAST
;
215 if (strcmp(*argv
, "on") == 0) {
216 req
->i
.ifi_flags
|= IFF_MULTICAST
;
217 } else if (strcmp(*argv
, "off") == 0) {
218 req
->i
.ifi_flags
&= ~IFF_MULTICAST
;
220 return on_off("multicast");
221 } else if (strcmp(*argv
, "allmulticast") == 0) {
223 req
->i
.ifi_change
|= IFF_ALLMULTI
;
224 if (strcmp(*argv
, "on") == 0) {
225 req
->i
.ifi_flags
|= IFF_ALLMULTI
;
226 } else if (strcmp(*argv
, "off") == 0) {
227 req
->i
.ifi_flags
&= ~IFF_ALLMULTI
;
229 return on_off("allmulticast");
230 } else if (strcmp(*argv
, "promisc") == 0) {
232 req
->i
.ifi_change
|= IFF_PROMISC
;
233 if (strcmp(*argv
, "on") == 0) {
234 req
->i
.ifi_flags
|= IFF_PROMISC
;
235 } else if (strcmp(*argv
, "off") == 0) {
236 req
->i
.ifi_flags
&= ~IFF_PROMISC
;
238 return on_off("promisc");
239 } else if (strcmp(*argv
, "trailers") == 0) {
241 req
->i
.ifi_change
|= IFF_NOTRAILERS
;
242 if (strcmp(*argv
, "off") == 0) {
243 req
->i
.ifi_flags
|= IFF_NOTRAILERS
;
244 } else if (strcmp(*argv
, "on") == 0) {
245 req
->i
.ifi_flags
&= ~IFF_NOTRAILERS
;
247 return on_off("trailers");
248 } else if (strcmp(*argv
, "arp") == 0) {
250 req
->i
.ifi_change
|= IFF_NOARP
;
251 if (strcmp(*argv
, "on") == 0) {
252 req
->i
.ifi_flags
&= ~IFF_NOARP
;
253 } else if (strcmp(*argv
, "off") == 0) {
254 req
->i
.ifi_flags
|= IFF_NOARP
;
256 return on_off("noarp");
258 } else if (matches(*argv
, "dynamic") == 0) {
260 req
->i
.ifi_change
|= IFF_DYNAMIC
;
261 if (strcmp(*argv
, "on") == 0) {
262 req
->i
.ifi_flags
|= IFF_DYNAMIC
;
263 } else if (strcmp(*argv
, "off") == 0) {
264 req
->i
.ifi_flags
&= ~IFF_DYNAMIC
;
266 return on_off("dynamic");
268 } else if (matches(*argv
, "type") == 0) {
274 if (strcmp(*argv
, "dev") == 0) {
277 if (matches(*argv
, "help") == 0)
280 duparg2("dev", *argv
);
289 static int iplink_modify(int cmd
, unsigned int flags
, int argc
, char **argv
)
296 struct link_util
*lu
= NULL
;
297 struct iplink_req req
;
300 memset(&req
, 0, sizeof(req
));
302 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
303 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
304 req
.n
.nlmsg_type
= cmd
;
305 req
.i
.ifi_family
= preferred_family
;
307 ret
= iplink_parse(argc
, argv
, &req
, &name
, &type
, &link
, &dev
);
316 struct rtattr
*linkinfo
= NLMSG_TAIL(&req
.n
);
317 addattr_l(&req
.n
, sizeof(req
), IFLA_LINKINFO
, NULL
, 0);
318 addattr_l(&req
.n
, sizeof(req
), IFLA_INFO_KIND
, type
,
321 lu
= get_link_kind(type
);
323 struct rtattr
* data
= NLMSG_TAIL(&req
.n
);
324 addattr_l(&req
.n
, sizeof(req
), IFLA_INFO_DATA
, NULL
, 0);
327 lu
->parse_opt(lu
, argc
, argv
, &req
.n
))
330 data
->rta_len
= (void *)NLMSG_TAIL(&req
.n
) - (void *)data
;
332 if (matches(*argv
, "help") == 0)
334 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". "
335 "Try \"ip link help\".\n", *argv
);
338 linkinfo
->rta_len
= (void *)NLMSG_TAIL(&req
.n
) - (void *)linkinfo
;
341 if (!(flags
& NLM_F_CREATE
)) {
343 fprintf(stderr
, "Not enough information: \"dev\" "
344 "argument is required.\n");
348 req
.i
.ifi_index
= ll_name_to_index(dev
);
349 if (req
.i
.ifi_index
== 0) {
350 fprintf(stderr
, "Cannot find device \"%s\"\n", dev
);
354 /* Allow "ip link add dev" and "ip link add name" */
361 ifindex
= ll_name_to_index(link
);
363 fprintf(stderr
, "Cannot find device \"%s\"\n",
367 addattr_l(&req
.n
, sizeof(req
), IFLA_LINK
, &ifindex
, 4);
372 len
= strlen(name
) + 1;
374 invarg("\"\" is not a valid device identifier\n", "name");
376 invarg("\"name\" too long\n", name
);
377 addattr_l(&req
.n
, sizeof(req
), IFLA_IFNAME
, name
, len
);
380 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
386 #if IPLINK_IOCTL_COMPAT
387 static int get_ctl_fd(void)
392 fd
= socket(PF_INET
, SOCK_DGRAM
, 0);
396 fd
= socket(PF_PACKET
, SOCK_DGRAM
, 0);
399 fd
= socket(PF_INET6
, SOCK_DGRAM
, 0);
403 perror("Cannot create control socket");
407 static int do_chflags(const char *dev
, __u32 flags
, __u32 mask
)
413 strncpy(ifr
.ifr_name
, dev
, IFNAMSIZ
);
417 err
= ioctl(fd
, SIOCGIFFLAGS
, &ifr
);
419 perror("SIOCGIFFLAGS");
423 if ((ifr
.ifr_flags
^flags
)&mask
) {
424 ifr
.ifr_flags
&= ~mask
;
425 ifr
.ifr_flags
|= mask
&flags
;
426 err
= ioctl(fd
, SIOCSIFFLAGS
, &ifr
);
428 perror("SIOCSIFFLAGS");
434 static int do_changename(const char *dev
, const char *newdev
)
440 strncpy(ifr
.ifr_name
, dev
, IFNAMSIZ
);
441 strncpy(ifr
.ifr_newname
, newdev
, IFNAMSIZ
);
445 err
= ioctl(fd
, SIOCSIFNAME
, &ifr
);
447 perror("SIOCSIFNAME");
455 static int set_qlen(const char *dev
, int qlen
)
464 memset(&ifr
, 0, sizeof(ifr
));
465 strncpy(ifr
.ifr_name
, dev
, IFNAMSIZ
);
467 if (ioctl(s
, SIOCSIFTXQLEN
, &ifr
) < 0) {
468 perror("SIOCSIFXQLEN");
477 static int set_mtu(const char *dev
, int mtu
)
486 memset(&ifr
, 0, sizeof(ifr
));
487 strncpy(ifr
.ifr_name
, dev
, IFNAMSIZ
);
489 if (ioctl(s
, SIOCSIFMTU
, &ifr
) < 0) {
490 perror("SIOCSIFMTU");
499 static int get_address(const char *dev
, int *htype
)
502 struct sockaddr_ll me
;
506 s
= socket(PF_PACKET
, SOCK_DGRAM
, 0);
508 perror("socket(PF_PACKET)");
512 memset(&ifr
, 0, sizeof(ifr
));
513 strncpy(ifr
.ifr_name
, dev
, IFNAMSIZ
);
514 if (ioctl(s
, SIOCGIFINDEX
, &ifr
) < 0) {
515 perror("SIOCGIFINDEX");
520 memset(&me
, 0, sizeof(me
));
521 me
.sll_family
= AF_PACKET
;
522 me
.sll_ifindex
= ifr
.ifr_ifindex
;
523 me
.sll_protocol
= htons(ETH_P_LOOP
);
524 if (bind(s
, (struct sockaddr
*)&me
, sizeof(me
)) == -1) {
531 if (getsockname(s
, (struct sockaddr
*)&me
, &alen
) == -1) {
532 perror("getsockname");
537 *htype
= me
.sll_hatype
;
541 static int parse_address(const char *dev
, int hatype
, int halen
,
542 char *lla
, struct ifreq
*ifr
)
546 memset(ifr
, 0, sizeof(*ifr
));
547 strncpy(ifr
->ifr_name
, dev
, IFNAMSIZ
);
548 ifr
->ifr_hwaddr
.sa_family
= hatype
;
549 alen
= ll_addr_a2n(ifr
->ifr_hwaddr
.sa_data
, 14, lla
);
553 fprintf(stderr
, "Wrong address (%s) length: expected %d bytes\n", lla
, halen
);
559 static int set_address(struct ifreq
*ifr
, int brd
)
566 if (ioctl(s
, brd
?SIOCSIFHWBROADCAST
:SIOCSIFHWADDR
, ifr
) < 0) {
567 perror(brd
?"SIOCSIFHWBROADCAST":"SIOCSIFHWADDR");
576 static int do_set(int argc
, char **argv
)
583 char *newaddr
= NULL
;
585 struct ifreq ifr0
, ifr1
;
586 char *newname
= NULL
;
590 if (strcmp(*argv
, "up") == 0) {
593 } else if (strcmp(*argv
, "down") == 0) {
596 } else if (strcmp(*argv
, "name") == 0) {
599 } else if (matches(*argv
, "address") == 0) {
602 } else if (matches(*argv
, "broadcast") == 0 ||
603 strcmp(*argv
, "brd") == 0) {
606 } else if (matches(*argv
, "txqueuelen") == 0 ||
607 strcmp(*argv
, "qlen") == 0 ||
608 matches(*argv
, "txqlen") == 0) {
611 duparg("txqueuelen", *argv
);
612 if (get_integer(&qlen
, *argv
, 0))
613 invarg("Invalid \"txqueuelen\" value\n", *argv
);
614 } else if (strcmp(*argv
, "mtu") == 0) {
617 duparg("mtu", *argv
);
618 if (get_integer(&mtu
, *argv
, 0))
619 invarg("Invalid \"mtu\" value\n", *argv
);
620 } else if (strcmp(*argv
, "multicast") == 0) {
622 mask
|= IFF_MULTICAST
;
623 if (strcmp(*argv
, "on") == 0) {
624 flags
|= IFF_MULTICAST
;
625 } else if (strcmp(*argv
, "off") == 0) {
626 flags
&= ~IFF_MULTICAST
;
628 return on_off("multicast");
629 } else if (strcmp(*argv
, "allmulticast") == 0) {
631 mask
|= IFF_ALLMULTI
;
632 if (strcmp(*argv
, "on") == 0) {
633 flags
|= IFF_ALLMULTI
;
634 } else if (strcmp(*argv
, "off") == 0) {
635 flags
&= ~IFF_ALLMULTI
;
637 return on_off("allmulticast");
638 } else if (strcmp(*argv
, "promisc") == 0) {
641 if (strcmp(*argv
, "on") == 0) {
642 flags
|= IFF_PROMISC
;
643 } else if (strcmp(*argv
, "off") == 0) {
644 flags
&= ~IFF_PROMISC
;
646 return on_off("promisc");
647 } else if (strcmp(*argv
, "trailers") == 0) {
649 mask
|= IFF_NOTRAILERS
;
650 if (strcmp(*argv
, "off") == 0) {
651 flags
|= IFF_NOTRAILERS
;
652 } else if (strcmp(*argv
, "on") == 0) {
653 flags
&= ~IFF_NOTRAILERS
;
655 return on_off("trailers");
656 } else if (strcmp(*argv
, "arp") == 0) {
659 if (strcmp(*argv
, "on") == 0) {
661 } else if (strcmp(*argv
, "off") == 0) {
664 return on_off("noarp");
666 } else if (matches(*argv
, "dynamic") == 0) {
669 if (strcmp(*argv
, "on") == 0) {
670 flags
|= IFF_DYNAMIC
;
671 } else if (strcmp(*argv
, "off") == 0) {
672 flags
&= ~IFF_DYNAMIC
;
674 return on_off("dynamic");
677 if (strcmp(*argv
, "dev") == 0) {
680 if (matches(*argv
, "help") == 0)
683 duparg2("dev", *argv
);
690 fprintf(stderr
, "Not enough of information: \"dev\" argument is required.\n");
694 if (newaddr
|| newbrd
) {
695 halen
= get_address(dev
, &htype
);
699 if (parse_address(dev
, htype
, halen
, newaddr
, &ifr0
) < 0)
703 if (parse_address(dev
, htype
, halen
, newbrd
, &ifr1
) < 0)
708 if (newname
&& strcmp(dev
, newname
)) {
709 if (strlen(newname
) == 0)
710 invarg("\"\" is not a valid device identifier\n", "name");
711 if (do_changename(dev
, newname
) < 0)
716 if (set_qlen(dev
, qlen
) < 0)
720 if (set_mtu(dev
, mtu
) < 0)
723 if (newaddr
|| newbrd
) {
725 if (set_address(&ifr1
, 1) < 0)
729 if (set_address(&ifr0
, 0) < 0)
734 return do_chflags(dev
, flags
, mask
);
737 #endif /* IPLINK_IOCTL_COMPAT */
739 int do_iplink(int argc
, char **argv
)
742 if (iplink_have_newlink()) {
743 if (matches(*argv
, "add") == 0)
744 return iplink_modify(RTM_NEWLINK
,
745 NLM_F_CREATE
|NLM_F_EXCL
,
747 if (matches(*argv
, "set") == 0 ||
748 matches(*argv
, "change") == 0)
749 return iplink_modify(RTM_NEWLINK
, 0,
751 if (matches(*argv
, "replace") == 0)
752 return iplink_modify(RTM_NEWLINK
,
753 NLM_F_CREATE
|NLM_F_REPLACE
,
755 if (matches(*argv
, "delete") == 0)
756 return iplink_modify(RTM_DELLINK
, 0,
759 #if IPLINK_IOCTL_COMPAT
760 if (matches(*argv
, "set") == 0)
761 return do_set(argc
-1, argv
+1);
764 if (matches(*argv
, "show") == 0 ||
765 matches(*argv
, "lst") == 0 ||
766 matches(*argv
, "list") == 0)
767 return ipaddr_list_link(argc
-1, argv
+1);
768 if (matches(*argv
, "help") == 0)
771 return ipaddr_list_link(0, NULL
);
773 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip link help\".\n", *argv
);