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> - not needed? */
10 #include <netpacket/packet.h>
11 #include <netinet/if_ether.h>
13 #include <linux/if_vlan.h>
14 #include "ip_common.h" /* #include "libbb.h" is inside */
19 #define ETH_P_8021AD 0x88A8
20 #undef VLAN_FLAG_REORDER_HDR
21 #define VLAN_FLAG_REORDER_HDR 0x1
23 #define VLAN_FLAG_GVRP 0x2
24 #undef VLAN_FLAG_LOOSE_BINDING
25 #define VLAN_FLAG_LOOSE_BINDING 0x4
27 #define VLAN_FLAG_MVRP 0x8
28 #undef IFLA_VLAN_PROTOCOL
29 #define IFLA_VLAN_PROTOCOL 5
32 # define IFLA_LINKINFO 18
33 # define IFLA_INFO_KIND 1
34 # define IFLA_INFO_DATA 2
38 # define IFLA_VLAN_ID 1
39 # define IFLA_VLAN_FLAGS 2
40 struct ifla_vlan_flags {
46 /* taken from linux/sockios.h */
47 #define SIOCSIFNAME 0x8923 /* set interface name */
50 static int get_ctl_fd(void)
54 fd = socket(PF_INET, SOCK_DGRAM, 0);
57 fd = socket(PF_PACKET, SOCK_DGRAM, 0);
60 return xsocket(PF_INET6, SOCK_DGRAM, 0);
64 static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
69 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
71 xioctl(fd, SIOCGIFFLAGS, &ifr);
72 if ((ifr.ifr_flags ^ flags) & mask) {
73 ifr.ifr_flags &= ~mask;
74 ifr.ifr_flags |= mask & flags;
75 xioctl(fd, SIOCSIFFLAGS, &ifr);
81 static void do_changename(char *dev, char *newdev)
86 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
87 strncpy_IFNAMSIZ(ifr.ifr_newname, newdev);
89 xioctl(fd, SIOCSIFNAME, &ifr);
94 static void set_qlen(char *dev, int qlen)
100 memset(&ifr, 0, sizeof(ifr));
101 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
103 xioctl(s, SIOCSIFTXQLEN, &ifr);
108 static void set_mtu(char *dev, int mtu)
114 memset(&ifr, 0, sizeof(ifr));
115 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
117 xioctl(s, SIOCSIFMTU, &ifr);
122 static int get_address(char *dev, int *htype)
125 struct sockaddr_ll me;
129 s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
131 memset(&ifr, 0, sizeof(ifr));
132 strncpy_IFNAMSIZ(ifr.ifr_name, dev);
133 xioctl(s, SIOCGIFINDEX, &ifr);
135 memset(&me, 0, sizeof(me));
136 me.sll_family = AF_PACKET;
137 me.sll_ifindex = ifr.ifr_ifindex;
138 me.sll_protocol = htons(ETH_P_LOOP);
139 xbind(s, (struct sockaddr*)&me, sizeof(me));
141 getsockname(s, (struct sockaddr*)&me, &alen);
143 //if (getsockname(s, (struct sockaddr*)&me, &alen) == -1)
144 // bb_perror_msg_and_die("getsockname");
146 *htype = me.sll_hatype;
151 static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
155 memset(ifr, 0, sizeof(*ifr));
156 strncpy_IFNAMSIZ(ifr->ifr_name, dev);
157 ifr->ifr_hwaddr.sa_family = hatype;
159 alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;
160 alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla);
164 bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
169 static void set_address(struct ifreq *ifr, int brd)
175 xioctl(s, SIOCSIFHWBROADCAST, ifr);
177 xioctl(s, SIOCSIFHWADDR, ifr);
182 static void die_must_be_on_off(const char *msg) NORETURN;
183 static void die_must_be_on_off(const char *msg)
185 bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg);
188 /* Return value becomes exitcode. It's okay to not return at all */
189 static int do_set(char **argv)
196 char *newaddr = NULL;
198 struct ifreq ifr0, ifr1;
199 char *newname = NULL;
201 static const char keywords[] ALIGN1 =
202 "up\0""down\0""name\0""mtu\0""qlen\0""multicast\0"
203 "arp\0""address\0""dev\0";
204 enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_qlen, ARG_multicast,
205 ARG_arp, ARG_addr, ARG_dev };
206 static const char str_on_off[] ALIGN1 = "on\0""off\0";
207 enum { PARM_on = 0, PARM_off };
211 /* substring search ensures that e.g. "addr" and "address"
212 * are both accepted */
213 key = index_in_substrings(keywords, *argv);
217 } else if (key == ARG_down) {
220 } else if (key == ARG_name) {
223 } else if (key == ARG_mtu) {
226 duparg("mtu", *argv);
227 mtu = get_unsigned(*argv, "mtu");
228 } else if (key == ARG_qlen) {
231 duparg("qlen", *argv);
232 qlen = get_unsigned(*argv, "qlen");
233 } else if (key == ARG_addr) {
236 } else if (key >= ARG_dev) {
237 if (key == ARG_dev) {
241 duparg2("dev", *argv);
246 param = index_in_strings(str_on_off, *argv);
247 if (key == ARG_multicast) {
249 die_must_be_on_off("multicast");
250 mask |= IFF_MULTICAST;
251 if (param == PARM_on)
252 flags |= IFF_MULTICAST;
254 flags &= ~IFF_MULTICAST;
255 } else if (key == ARG_arp) {
257 die_must_be_on_off("arp");
259 if (param == PARM_on)
269 bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
272 if (newaddr || newbrd) {
273 halen = get_address(dev, &htype);
275 parse_address(dev, htype, halen, newaddr, &ifr0);
276 set_address(&ifr0, 0);
279 parse_address(dev, htype, halen, newbrd, &ifr1);
280 set_address(&ifr1, 1);
284 if (newname && strcmp(dev, newname)) {
285 do_changename(dev, newname);
295 do_chflags(dev, flags, mask);
299 static int ipaddr_list_link(char **argv)
301 preferred_family = AF_PACKET;
302 return ipaddr_list_or_flush(argv, 0);
305 static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size)
307 static const char keywords[] ALIGN1 =
315 static const char protocols[] ALIGN1 =
319 static const char str_on_off[] ALIGN1 =
341 struct ifla_vlan_flags flags = {};
344 arg = index_in_substrings(keywords, *argv);
346 invarg(*argv, "type vlan");
350 id = get_u16(*argv, "id");
351 addattr_l(n, size, IFLA_VLAN_ID, &id, sizeof(id));
352 } else if (arg == ARG_protocol) {
353 arg = index_in_substrings(protocols, *argv);
354 if (arg == PROTO_8021Q)
356 else if (arg == PROTO_8021AD)
357 proto = ETH_P_8021AD;
359 bb_error_msg_and_die("unknown VLAN encapsulation protocol '%s'",
361 addattr_l(n, size, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto));
363 int param = index_in_strings(str_on_off, *argv);
365 die_must_be_on_off(nth_string(keywords, arg));
367 if (arg == ARG_reorder_hdr) {
368 flags.mask |= VLAN_FLAG_REORDER_HDR;
369 flags.flags &= ~VLAN_FLAG_REORDER_HDR;
370 if (param == PARM_on)
371 flags.flags |= VLAN_FLAG_REORDER_HDR;
372 } else if (arg == ARG_gvrp) {
373 flags.mask |= VLAN_FLAG_GVRP;
374 flags.flags &= ~VLAN_FLAG_GVRP;
375 if (param == PARM_on)
376 flags.flags |= VLAN_FLAG_GVRP;
377 } else if (arg == ARG_mvrp) {
378 flags.mask |= VLAN_FLAG_MVRP;
379 flags.flags &= ~VLAN_FLAG_MVRP;
380 if (param == PARM_on)
381 flags.flags |= VLAN_FLAG_MVRP;
382 } else { /*if (arg == ARG_loose_binding) */
383 flags.mask |= VLAN_FLAG_LOOSE_BINDING;
384 flags.flags &= ~VLAN_FLAG_LOOSE_BINDING;
385 if (param == PARM_on)
386 flags.flags |= VLAN_FLAG_LOOSE_BINDING;
393 addattr_l(n, size, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
397 #define NLMSG_TAIL(nmsg) \
398 ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
400 /* Return value becomes exitcode. It's okay to not return at all */
401 static int do_add_or_delete(char **argv, const unsigned rtm)
403 static const char keywords[] ALIGN1 =
404 "link\0""name\0""type\0""dev\0";
411 struct rtnl_handle rth;
418 char *name_str = NULL, *link_str = NULL, *type_str = NULL, *dev_str = NULL;
420 memset(&req, 0, sizeof(req));
422 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
423 req.n.nlmsg_flags = NLM_F_REQUEST;
424 req.n.nlmsg_type = rtm;
425 req.i.ifi_family = preferred_family;
426 if (rtm == RTM_NEWLINK)
427 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
430 arg = index_in_substrings(keywords, *argv);
431 if (arg == ARG_type) {
436 if (arg == ARG_link) {
439 } else if (arg == ARG_name) {
443 if (arg == ARG_dev) {
445 duparg(*argv, "dev");
455 struct rtattr *linkinfo = NLMSG_TAIL(&req.n);
457 addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0);
458 addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str,
462 struct rtattr *data = NLMSG_TAIL(&req.n);
463 addattr_l(&req.n, sizeof(req), IFLA_INFO_DATA, NULL, 0);
465 if (strcmp(type_str, "vlan") == 0)
466 vlan_parse_opt(argv, &req.n, sizeof(req));
468 data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;
471 linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;
473 if (rtm != RTM_NEWLINK) {
475 return 1; /* Need a device to delete */
476 req.i.ifi_index = xll_name_to_index(dev_str);
481 int idx = xll_name_to_index(link_str);
482 addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4);
486 const size_t name_len = strlen(name_str) + 1;
487 if (name_len < 2 || name_len > IFNAMSIZ)
488 invarg(name_str, "name");
489 addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name_str, name_len);
491 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
496 /* Return value becomes exitcode. It's okay to not return at all */
497 int FAST_FUNC do_iplink(char **argv)
499 static const char keywords[] ALIGN1 =
500 "add\0""delete\0""set\0""show\0""lst\0""list\0";
502 int key = index_in_substrings(keywords, *argv);
503 if (key < 0) /* invalid argument */
504 invarg(*argv, applet_name);
506 if (key <= 1) /* add/delete */
507 return do_add_or_delete(argv, key ? RTM_DELLINK : RTM_NEWLINK);
508 if (key == 2) /* set */
511 /* show, lst, list */
512 return ipaddr_list_link(argv);