1 /* vi: set sw=4 ts=4: */
3 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
4 * Patrick McHardy <kaber@trash.net>
6 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 #include <net/if_packet.h>
10 #include <netpacket/packet.h>
11 #include <netinet/if_ether.h>
13 #include <linux/if_vlan.h>
15 #define ETH_P_8021AD 0x88A8
16 #undef VLAN_FLAG_REORDER_HDR
17 #define VLAN_FLAG_REORDER_HDR 0x1
19 #define VLAN_FLAG_GVRP 0x2
20 #undef VLAN_FLAG_LOOSE_BINDING
21 #define VLAN_FLAG_LOOSE_BINDING 0x4
23 #define VLAN_FLAG_MVRP 0x8
24 #undef IFLA_VLAN_PROTOCOL
25 #define IFLA_VLAN_PROTOCOL 5
27 #include "ip_common.h" /* #include "libbb.h" is inside */
32 # define IFLA_LINKINFO 18
33 # define IFLA_INFO_KIND 1
36 /* taken from linux/sockios.h */
37 #define SIOCSIFNAME 0x8923 /* set interface name */
40 static int get_ctl_fd(void)
44 fd = socket(PF_INET, SOCK_DGRAM, 0);
47 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
50 return xsocket(PF_INET6, SOCK_DGRAM, 0);
54 static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
59 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
61 xioctl(fd, SIOCGIFFLAGS, &ifr);
62 if ((ifr.ifr_flags ^ flags) & mask) {
63 ifr.ifr_flags &= ~mask;
64 ifr.ifr_flags |= mask & flags;
65 xioctl(fd, SIOCSIFFLAGS, &ifr);
71 static void do_changename(char *dev, char *newdev)
76 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
77 strncpy_IFNAMSIZ(ifr.ifr_newname, newdev);
79 xioctl(fd, SIOCSIFNAME, &ifr);
84 static void set_qlen(char *dev, int qlen)
90 memset(&ifr, 0, sizeof(ifr));
91 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
93 xioctl(s, SIOCSIFTXQLEN, &ifr);
98 static void set_mtu(char *dev, int mtu)
104 memset(&ifr, 0, sizeof(ifr));
105 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
107 xioctl(s, SIOCSIFMTU, &ifr);
112 static int get_address(char *dev, int *htype)
115 struct sockaddr_ll me;
119 s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
121 memset(&ifr, 0, sizeof(ifr));
122 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
123 xioctl(s, SIOCGIFINDEX, &ifr);
125 memset(&me, 0, sizeof(me));
126 me.sll_family = AF_PACKET;
127 me.sll_ifindex = ifr.ifr_ifindex;
128 me.sll_protocol = htons(ETH_P_LOOP);
129 xbind(s, (struct sockaddr*)&me, sizeof(me));
131 getsockname(s, (struct sockaddr*)&me, &alen);
133 //if (getsockname(s, (struct sockaddr*)&me, &alen) == -1)
134 // bb_perror_msg_and_die("getsockname");
136 *htype = me.sll_hatype;
141 static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
145 memset(ifr, 0, sizeof(*ifr));
146 strncpy_IFNAMSIZ(ifr->ifr_name, dev);
147 ifr->ifr_hwaddr.sa_family = hatype;
149 alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;
150 alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla);
154 bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
159 static void set_address(struct ifreq *ifr, int brd)
165 xioctl(s, SIOCSIFHWBROADCAST, ifr);
167 xioctl(s, SIOCSIFHWADDR, ifr);
172 static void die_must_be_on_off(const char *msg) NORETURN;
173 static void die_must_be_on_off(const char *msg)
175 bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg);
178 /* Return value becomes exitcode. It's okay to not return at all */
179 static int do_set(char **argv)
186 char *newaddr = NULL;
188 struct ifreq ifr0, ifr1;
189 char *newname = NULL;
191 static const char keywords[] ALIGN1 =
192 "up\0""down\0""name\0""mtu\0""qlen\0""multicast\0"
193 "arp\0""address\0""dev\0";
194 enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_qlen, ARG_multicast,
195 ARG_arp, ARG_addr, ARG_dev };
196 static const char str_on_off[] ALIGN1 = "on\0""off\0";
197 enum { PARM_on = 0, PARM_off };
201 /* substring search ensures that e.g. "addr" and "address"
202 * are both accepted */
203 key = index_in_substrings(keywords, *argv);
207 } else if (key == ARG_down) {
210 } else if (key == ARG_name) {
213 } else if (key == ARG_mtu) {
216 duparg("mtu", *argv);
217 mtu = get_unsigned(*argv, "mtu");
218 } else if (key == ARG_qlen) {
221 duparg("qlen", *argv);
222 qlen = get_unsigned(*argv, "qlen");
223 } else if (key == ARG_addr) {
226 } else if (key >= ARG_dev) {
227 if (key == ARG_dev) {
231 duparg2("dev", *argv);
236 param = index_in_strings(str_on_off, *argv);
237 if (key == ARG_multicast) {
239 die_must_be_on_off("multicast");
240 mask |= IFF_MULTICAST;
241 if (param == PARM_on)
242 flags |= IFF_MULTICAST;
244 flags &= ~IFF_MULTICAST;
245 } else if (key == ARG_arp) {
247 die_must_be_on_off("arp");
249 if (param == PARM_on)
259 bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
262 if (newaddr || newbrd) {
263 halen = get_address(dev, &htype);
265 parse_address(dev, htype, halen, newaddr, &ifr0);
266 set_address(&ifr0, 0);
269 parse_address(dev, htype, halen, newbrd, &ifr1);
270 set_address(&ifr1, 1);
274 if (newname && strcmp(dev, newname)) {
275 do_changename(dev, newname);
285 do_chflags(dev, flags, mask);
289 static int ipaddr_list_link(char **argv)
291 preferred_family = AF_PACKET;
292 return ipaddr_list_or_flush(argv, 0);
295 static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size)
297 static const char keywords[] ALIGN1 =
305 static const char protocols[] ALIGN1 =
309 static const char str_on_off[] ALIGN1 =
331 struct ifla_vlan_flags flags = {};
334 arg = index_in_substrings(keywords, *argv);
336 invarg(*argv, "type vlan");
340 id = get_u16(*argv, "id");
341 addattr_l(n, size, IFLA_VLAN_ID, &id, sizeof(id));
342 } else if (arg == ARG_protocol) {
343 arg = index_in_substrings(protocols, *argv);
344 if (arg == PROTO_8021Q)
346 else if (arg == PROTO_8021AD)
347 proto = ETH_P_8021AD;
349 bb_error_msg_and_die("unknown VLAN encapsulation protocol '%s'",
351 addattr_l(n, size, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto));
353 int param = index_in_strings(str_on_off, *argv);
355 die_must_be_on_off(nth_string(keywords, arg));
357 if (arg == ARG_reorder_hdr) {
358 flags.mask |= VLAN_FLAG_REORDER_HDR;
359 flags.flags &= ~VLAN_FLAG_REORDER_HDR;
360 if (param == PARM_on)
361 flags.flags |= VLAN_FLAG_REORDER_HDR;
362 } else if (arg == ARG_gvrp) {
363 flags.mask |= VLAN_FLAG_GVRP;
364 flags.flags &= ~VLAN_FLAG_GVRP;
365 if (param == PARM_on)
366 flags.flags |= VLAN_FLAG_GVRP;
367 } else if (arg == ARG_mvrp) {
368 flags.mask |= VLAN_FLAG_MVRP;
369 flags.flags &= ~VLAN_FLAG_MVRP;
370 if (param == PARM_on)
371 flags.flags |= VLAN_FLAG_MVRP;
372 } else { /*if (arg == ARG_loose_binding) */
373 flags.mask |= VLAN_FLAG_LOOSE_BINDING;
374 flags.flags &= ~VLAN_FLAG_LOOSE_BINDING;
375 if (param == PARM_on)
376 flags.flags |= VLAN_FLAG_LOOSE_BINDING;
383 addattr_l(n, size, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
387 #define NLMSG_TAIL(nmsg) \
388 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
390 /* Return value becomes exitcode. It's okay to not return at all */
391 static int do_add_or_delete(char **argv, const unsigned rtm)
393 static const char keywords[] ALIGN1 =
394 "link\0""name\0""type\0""dev\0";
401 struct rtnl_handle rth;
408 char *name_str = NULL, *link_str = NULL, *type_str = NULL, *dev_str = NULL;
410 memset(&req, 0, sizeof(req));
412 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
413 req.n.nlmsg_flags = NLM_F_REQUEST;
414 req.n.nlmsg_type = rtm;
415 req.i.ifi_family = preferred_family;
416 if (rtm == RTM_NEWLINK)
417 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
420 arg = index_in_substrings(keywords, *argv);
421 if (arg == ARG_type) {
426 if (arg == ARG_link) {
429 } else if (arg == ARG_name) {
433 if (arg == ARG_dev) {
435 duparg(*argv, "dev");
445 struct rtattr *linkinfo = NLMSG_TAIL(&req.n);
447 addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0);
448 addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str,
452 struct rtattr *data = NLMSG_TAIL(&req.n);
453 addattr_l(&req.n, sizeof(req), IFLA_INFO_DATA, NULL, 0);
455 if (strcmp(type_str, "vlan") == 0)
456 vlan_parse_opt(argv, &req.n, sizeof(req));
458 data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;
461 linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;
463 if (rtm != RTM_NEWLINK) {
465 return 1; /* Need a device to delete */
466 req.i.ifi_index = xll_name_to_index(dev_str);
471 int idx = xll_name_to_index(link_str);
472 addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4);
476 const size_t name_len = strlen(name_str) + 1;
477 if (name_len < 2 || name_len > IFNAMSIZ)
478 invarg(name_str, "name");
479 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name_str, name_len);
481 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
486 /* Return value becomes exitcode. It's okay to not return at all */
487 int FAST_FUNC do_iplink(char **argv)
489 static const char keywords[] ALIGN1 =
490 "add\0""delete\0""set\0""show\0""lst\0""list\0";
492 int key = index_in_substrings(keywords, *argv);
493 if (key < 0) /* invalid argument */
494 invarg(*argv, applet_name);
496 if (key <= 1) /* add/delete */
497 return do_add_or_delete(argv, key ? RTM_DELLINK : RTM_NEWLINK);
498 if (key == 2) /* set */
501 /* show, lst, list */
502 return ipaddr_list_link(argv);