iplink.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  4. * Patrick McHardy <kaber@trash.net>
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. #include <net/if.h>
  9. /*#include <net/if_packet.h> - not needed? */
  10. #include <netpacket/packet.h>
  11. #include <netinet/if_ether.h>
  12. #include <linux/if_vlan.h>
  13. #include "ip_common.h" /* #include "libbb.h" is inside */
  14. #include "rt_names.h"
  15. #include "utils.h"
  16. #undef ETH_P_8021AD
  17. #define ETH_P_8021AD 0x88A8
  18. #undef VLAN_FLAG_REORDER_HDR
  19. #define VLAN_FLAG_REORDER_HDR 0x1
  20. #undef VLAN_FLAG_GVRP
  21. #define VLAN_FLAG_GVRP 0x2
  22. #undef VLAN_FLAG_LOOSE_BINDING
  23. #define VLAN_FLAG_LOOSE_BINDING 0x4
  24. #undef VLAN_FLAG_MVRP
  25. #define VLAN_FLAG_MVRP 0x8
  26. #undef IFLA_VLAN_PROTOCOL
  27. #define IFLA_VLAN_PROTOCOL 5
  28. #ifndef IFLA_LINKINFO
  29. # define IFLA_LINKINFO 18
  30. # define IFLA_INFO_KIND 1
  31. # define IFLA_INFO_DATA 2
  32. #endif
  33. #ifndef IFLA_VLAN_MAX
  34. # define IFLA_VLAN_ID 1
  35. # define IFLA_VLAN_FLAGS 2
  36. struct ifla_vlan_flags {
  37. uint32_t flags;
  38. uint32_t mask;
  39. };
  40. #endif
  41. /* taken from linux/sockios.h */
  42. #define SIOCSIFNAME 0x8923 /* set interface name */
  43. #if 0
  44. # define dbg(...) bb_error_msg(__VA_ARGS__)
  45. #else
  46. # define dbg(...) ((void)0)
  47. #endif
  48. /* Exits on error */
  49. static int get_ctl_fd(void)
  50. {
  51. int fd;
  52. fd = socket(PF_INET, SOCK_DGRAM, 0);
  53. if (fd >= 0)
  54. return fd;
  55. fd = socket(PF_PACKET, SOCK_DGRAM, 0);
  56. if (fd >= 0)
  57. return fd;
  58. return xsocket(PF_INET6, SOCK_DGRAM, 0);
  59. }
  60. /* Exits on error */
  61. static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
  62. {
  63. struct ifreq ifr;
  64. int fd;
  65. strncpy_IFNAMSIZ(ifr.ifr_name, dev);
  66. fd = get_ctl_fd();
  67. xioctl(fd, SIOCGIFFLAGS, &ifr);
  68. if ((ifr.ifr_flags ^ flags) & mask) {
  69. ifr.ifr_flags &= ~mask;
  70. ifr.ifr_flags |= mask & flags;
  71. xioctl(fd, SIOCSIFFLAGS, &ifr);
  72. }
  73. close(fd);
  74. }
  75. /* Exits on error */
  76. static void do_changename(char *dev, char *newdev)
  77. {
  78. struct ifreq ifr;
  79. int fd;
  80. strncpy_IFNAMSIZ(ifr.ifr_name, dev);
  81. strncpy_IFNAMSIZ(ifr.ifr_newname, newdev);
  82. fd = get_ctl_fd();
  83. xioctl(fd, SIOCSIFNAME, &ifr);
  84. close(fd);
  85. }
  86. /* Exits on error */
  87. static void set_qlen(char *dev, int qlen)
  88. {
  89. struct ifreq ifr;
  90. int s;
  91. s = get_ctl_fd();
  92. memset(&ifr, 0, sizeof(ifr));
  93. strncpy_IFNAMSIZ(ifr.ifr_name, dev);
  94. ifr.ifr_qlen = qlen;
  95. xioctl(s, SIOCSIFTXQLEN, &ifr);
  96. close(s);
  97. }
  98. /* Exits on error */
  99. static void set_mtu(char *dev, int mtu)
  100. {
  101. struct ifreq ifr;
  102. int s;
  103. s = get_ctl_fd();
  104. memset(&ifr, 0, sizeof(ifr));
  105. strncpy_IFNAMSIZ(ifr.ifr_name, dev);
  106. ifr.ifr_mtu = mtu;
  107. xioctl(s, SIOCSIFMTU, &ifr);
  108. close(s);
  109. }
  110. /* Exits on error */
  111. static int get_address(char *dev, int *htype)
  112. {
  113. struct ifreq ifr;
  114. struct sockaddr_ll me;
  115. socklen_t alen;
  116. int s;
  117. s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
  118. memset(&ifr, 0, sizeof(ifr));
  119. strncpy_IFNAMSIZ(ifr.ifr_name, dev);
  120. xioctl(s, SIOCGIFINDEX, &ifr);
  121. memset(&me, 0, sizeof(me));
  122. me.sll_family = AF_PACKET;
  123. me.sll_ifindex = ifr.ifr_ifindex;
  124. me.sll_protocol = htons(ETH_P_LOOP);
  125. xbind(s, (struct sockaddr*)&me, sizeof(me));
  126. alen = sizeof(me);
  127. getsockname(s, (struct sockaddr*)&me, &alen);
  128. //never happens:
  129. //if (getsockname(s, (struct sockaddr*)&me, &alen) == -1)
  130. // bb_perror_msg_and_die("getsockname");
  131. close(s);
  132. *htype = me.sll_hatype;
  133. return me.sll_halen;
  134. }
  135. /* Exits on error */
  136. static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
  137. {
  138. int alen;
  139. memset(ifr, 0, sizeof(*ifr));
  140. strncpy_IFNAMSIZ(ifr->ifr_name, dev);
  141. ifr->ifr_hwaddr.sa_family = hatype;
  142. alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;
  143. alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla);
  144. if (alen < 0)
  145. exit(EXIT_FAILURE);
  146. if (alen != halen) {
  147. bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
  148. }
  149. }
  150. /* Exits on error */
  151. static void set_address(struct ifreq *ifr, int brd)
  152. {
  153. int s;
  154. s = get_ctl_fd();
  155. if (brd)
  156. xioctl(s, SIOCSIFHWBROADCAST, ifr);
  157. else
  158. xioctl(s, SIOCSIFHWADDR, ifr);
  159. close(s);
  160. }
  161. static void die_must_be_on_off(const char *msg) NORETURN;
  162. static void die_must_be_on_off(const char *msg)
  163. {
  164. bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg);
  165. }
  166. /* Return value becomes exitcode. It's okay to not return at all */
  167. static int do_set(char **argv)
  168. {
  169. char *dev = NULL;
  170. uint32_t mask = 0;
  171. uint32_t flags = 0;
  172. int qlen = -1;
  173. int mtu = -1;
  174. char *newaddr = NULL;
  175. char *newbrd = NULL;
  176. struct ifreq ifr0, ifr1;
  177. char *newname = NULL;
  178. int htype, halen;
  179. static const char keywords[] ALIGN1 =
  180. "up\0""down\0""name\0""mtu\0""qlen\0""multicast\0"
  181. "arp\0""address\0""dev\0";
  182. enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_qlen, ARG_multicast,
  183. ARG_arp, ARG_addr, ARG_dev };
  184. static const char str_on_off[] ALIGN1 = "on\0""off\0";
  185. enum { PARM_on = 0, PARM_off };
  186. smalluint key;
  187. while (*argv) {
  188. /* substring search ensures that e.g. "addr" and "address"
  189. * are both accepted */
  190. key = index_in_substrings(keywords, *argv);
  191. if (key == ARG_up) {
  192. mask |= IFF_UP;
  193. flags |= IFF_UP;
  194. } else if (key == ARG_down) {
  195. mask |= IFF_UP;
  196. flags &= ~IFF_UP;
  197. } else if (key == ARG_name) {
  198. NEXT_ARG();
  199. newname = *argv;
  200. } else if (key == ARG_mtu) {
  201. NEXT_ARG();
  202. if (mtu != -1)
  203. duparg("mtu", *argv);
  204. mtu = get_unsigned(*argv, "mtu");
  205. } else if (key == ARG_qlen) {
  206. NEXT_ARG();
  207. if (qlen != -1)
  208. duparg("qlen", *argv);
  209. qlen = get_unsigned(*argv, "qlen");
  210. } else if (key == ARG_addr) {
  211. NEXT_ARG();
  212. newaddr = *argv;
  213. } else if (key >= ARG_dev) {
  214. if (key == ARG_dev) {
  215. NEXT_ARG();
  216. }
  217. if (dev)
  218. duparg2("dev", *argv);
  219. dev = *argv;
  220. } else {
  221. int param;
  222. NEXT_ARG();
  223. param = index_in_strings(str_on_off, *argv);
  224. if (key == ARG_multicast) {
  225. if (param < 0)
  226. die_must_be_on_off("multicast");
  227. mask |= IFF_MULTICAST;
  228. if (param == PARM_on)
  229. flags |= IFF_MULTICAST;
  230. else
  231. flags &= ~IFF_MULTICAST;
  232. } else if (key == ARG_arp) {
  233. if (param < 0)
  234. die_must_be_on_off("arp");
  235. mask |= IFF_NOARP;
  236. if (param == PARM_on)
  237. flags &= ~IFF_NOARP;
  238. else
  239. flags |= IFF_NOARP;
  240. }
  241. }
  242. argv++;
  243. }
  244. if (!dev) {
  245. bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
  246. }
  247. if (newaddr || newbrd) {
  248. halen = get_address(dev, &htype);
  249. if (newaddr) {
  250. parse_address(dev, htype, halen, newaddr, &ifr0);
  251. set_address(&ifr0, 0);
  252. }
  253. if (newbrd) {
  254. parse_address(dev, htype, halen, newbrd, &ifr1);
  255. set_address(&ifr1, 1);
  256. }
  257. }
  258. if (newname && strcmp(dev, newname)) {
  259. do_changename(dev, newname);
  260. dev = newname;
  261. }
  262. if (qlen != -1) {
  263. set_qlen(dev, qlen);
  264. }
  265. if (mtu != -1) {
  266. set_mtu(dev, mtu);
  267. }
  268. if (mask)
  269. do_chflags(dev, flags, mask);
  270. return 0;
  271. }
  272. static int ipaddr_list_link(char **argv)
  273. {
  274. preferred_family = AF_PACKET;
  275. return ipaddr_list_or_flush(argv, 0);
  276. }
  277. static void vlan_parse_opt(char **argv, struct nlmsghdr *n, unsigned int size)
  278. {
  279. static const char keywords[] ALIGN1 =
  280. "id\0"
  281. "protocol\0"
  282. "reorder_hdr\0"
  283. "gvrp\0"
  284. "mvrp\0"
  285. "loose_binding\0"
  286. ;
  287. static const char protocols[] ALIGN1 =
  288. "802.1q\0"
  289. "802.1ad\0"
  290. ;
  291. static const char str_on_off[] ALIGN1 =
  292. "on\0"
  293. "off\0"
  294. ;
  295. enum {
  296. ARG_id = 0,
  297. ARG_reorder_hdr,
  298. ARG_gvrp,
  299. ARG_mvrp,
  300. ARG_loose_binding,
  301. ARG_protocol,
  302. };
  303. enum {
  304. PROTO_8021Q = 0,
  305. PROTO_8021AD,
  306. };
  307. enum {
  308. PARM_on = 0,
  309. PARM_off
  310. };
  311. int arg;
  312. uint16_t id, proto;
  313. struct ifla_vlan_flags flags = {};
  314. while (*argv) {
  315. arg = index_in_substrings(keywords, *argv);
  316. if (arg < 0)
  317. invarg(*argv, "type vlan");
  318. NEXT_ARG();
  319. if (arg == ARG_id) {
  320. id = get_u16(*argv, "id");
  321. addattr_l(n, size, IFLA_VLAN_ID, &id, sizeof(id));
  322. } else if (arg == ARG_protocol) {
  323. arg = index_in_substrings(protocols, *argv);
  324. if (arg == PROTO_8021Q)
  325. proto = ETH_P_8021Q;
  326. else if (arg == PROTO_8021AD)
  327. proto = ETH_P_8021AD;
  328. else
  329. bb_error_msg_and_die("unknown VLAN encapsulation protocol '%s'",
  330. *argv);
  331. addattr_l(n, size, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto));
  332. } else {
  333. int param = index_in_strings(str_on_off, *argv);
  334. if (param < 0)
  335. die_must_be_on_off(nth_string(keywords, arg));
  336. if (arg == ARG_reorder_hdr) {
  337. flags.mask |= VLAN_FLAG_REORDER_HDR;
  338. flags.flags &= ~VLAN_FLAG_REORDER_HDR;
  339. if (param == PARM_on)
  340. flags.flags |= VLAN_FLAG_REORDER_HDR;
  341. } else if (arg == ARG_gvrp) {
  342. flags.mask |= VLAN_FLAG_GVRP;
  343. flags.flags &= ~VLAN_FLAG_GVRP;
  344. if (param == PARM_on)
  345. flags.flags |= VLAN_FLAG_GVRP;
  346. } else if (arg == ARG_mvrp) {
  347. flags.mask |= VLAN_FLAG_MVRP;
  348. flags.flags &= ~VLAN_FLAG_MVRP;
  349. if (param == PARM_on)
  350. flags.flags |= VLAN_FLAG_MVRP;
  351. } else { /*if (arg == ARG_loose_binding) */
  352. flags.mask |= VLAN_FLAG_LOOSE_BINDING;
  353. flags.flags &= ~VLAN_FLAG_LOOSE_BINDING;
  354. if (param == PARM_on)
  355. flags.flags |= VLAN_FLAG_LOOSE_BINDING;
  356. }
  357. }
  358. argv++;
  359. }
  360. if (flags.mask)
  361. addattr_l(n, size, IFLA_VLAN_FLAGS, &flags, sizeof(flags));
  362. }
  363. #ifndef NLMSG_TAIL
  364. #define NLMSG_TAIL(nmsg) \
  365. ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
  366. #endif
  367. /* Return value becomes exitcode. It's okay to not return at all */
  368. static int do_add_or_delete(char **argv, const unsigned rtm)
  369. {
  370. static const char keywords[] ALIGN1 =
  371. "link\0""name\0""type\0""dev\0""address\0";
  372. enum {
  373. ARG_link,
  374. ARG_name,
  375. ARG_type,
  376. ARG_dev,
  377. ARG_address,
  378. };
  379. struct rtnl_handle rth;
  380. struct {
  381. struct nlmsghdr n;
  382. struct ifinfomsg i;
  383. char buf[1024];
  384. } req;
  385. smalluint arg;
  386. char *name_str = NULL;
  387. char *link_str = NULL;
  388. char *type_str = NULL;
  389. char *dev_str = NULL;
  390. char *address_str = NULL;
  391. memset(&req, 0, sizeof(req));
  392. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
  393. req.n.nlmsg_flags = NLM_F_REQUEST;
  394. req.n.nlmsg_type = rtm;
  395. req.i.ifi_family = preferred_family;
  396. if (rtm == RTM_NEWLINK)
  397. req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
  398. while (*argv) {
  399. arg = index_in_substrings(keywords, *argv);
  400. if (arg == ARG_type) {
  401. NEXT_ARG();
  402. type_str = *argv++;
  403. dbg("type_str:'%s'", type_str);
  404. break;
  405. }
  406. if (arg == ARG_link) {
  407. NEXT_ARG();
  408. link_str = *argv;
  409. dbg("link_str:'%s'", link_str);
  410. } else if (arg == ARG_name) {
  411. NEXT_ARG();
  412. name_str = *argv;
  413. dbg("name_str:'%s'", name_str);
  414. } else if (arg == ARG_address) {
  415. NEXT_ARG();
  416. address_str = *argv;
  417. dbg("address_str:'%s'", name_str);
  418. } else {
  419. if (arg == ARG_dev) {
  420. if (dev_str)
  421. duparg(*argv, "dev");
  422. NEXT_ARG();
  423. }
  424. dev_str = *argv;
  425. dbg("dev_str:'%s'", dev_str);
  426. }
  427. argv++;
  428. }
  429. xrtnl_open(&rth);
  430. ll_init_map(&rth);
  431. if (type_str) {
  432. struct rtattr *linkinfo = NLMSG_TAIL(&req.n);
  433. addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0);
  434. addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str,
  435. strlen(type_str));
  436. if (*argv) {
  437. struct rtattr *data = NLMSG_TAIL(&req.n);
  438. addattr_l(&req.n, sizeof(req), IFLA_INFO_DATA, NULL, 0);
  439. if (strcmp(type_str, "vlan") == 0)
  440. vlan_parse_opt(argv, &req.n, sizeof(req));
  441. data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;
  442. }
  443. linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;
  444. }
  445. if (rtm != RTM_NEWLINK) {
  446. if (!dev_str)
  447. return 1; /* Need a device to delete */
  448. req.i.ifi_index = xll_name_to_index(dev_str);
  449. } else {
  450. if (!name_str)
  451. name_str = dev_str;
  452. if (link_str) {
  453. int idx = xll_name_to_index(link_str);
  454. addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4);
  455. }
  456. if (address_str) {
  457. unsigned char abuf[32];
  458. int len = ll_addr_a2n(abuf, sizeof(abuf), address_str);
  459. dbg("address len:%d", len);
  460. if (len < 0)
  461. return -1;
  462. addattr_l(&req.n, sizeof(req), IFLA_ADDRESS, abuf, len);
  463. }
  464. }
  465. if (name_str) {
  466. const size_t name_len = strlen(name_str) + 1;
  467. if (name_len < 2 || name_len > IFNAMSIZ)
  468. invarg(name_str, "name");
  469. addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name_str, name_len);
  470. }
  471. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  472. return 2;
  473. return 0;
  474. }
  475. /* Other keywords recognized by iproute2-3.12.0: */
  476. #if 0
  477. } else if (matches(*argv, "broadcast") == 0 ||
  478. strcmp(*argv, "brd") == 0) {
  479. NEXT_ARG();
  480. len = ll_addr_a2n(abuf, sizeof(abuf), *argv);
  481. if (len < 0)
  482. return -1;
  483. addattr_l(&req->n, sizeof(*req), IFLA_BROADCAST, abuf, len);
  484. } else if (matches(*argv, "txqueuelen") == 0 ||
  485. strcmp(*argv, "qlen") == 0 ||
  486. matches(*argv, "txqlen") == 0) {
  487. NEXT_ARG();
  488. if (qlen != -1)
  489. duparg("txqueuelen", *argv);
  490. if (get_integer(&qlen, *argv, 0))
  491. invarg("Invalid \"txqueuelen\" value\n", *argv);
  492. addattr_l(&req->n, sizeof(*req), IFLA_TXQLEN, &qlen, 4);
  493. } else if (strcmp(*argv, "mtu") == 0) {
  494. NEXT_ARG();
  495. if (mtu != -1)
  496. duparg("mtu", *argv);
  497. if (get_integer(&mtu, *argv, 0))
  498. invarg("Invalid \"mtu\" value\n", *argv);
  499. addattr_l(&req->n, sizeof(*req), IFLA_MTU, &mtu, 4);
  500. } else if (strcmp(*argv, "netns") == 0) {
  501. NEXT_ARG();
  502. if (netns != -1)
  503. duparg("netns", *argv);
  504. if ((netns = get_netns_fd(*argv)) >= 0)
  505. addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_FD, &netns, 4);
  506. else if (get_integer(&netns, *argv, 0) == 0)
  507. addattr_l(&req->n, sizeof(*req), IFLA_NET_NS_PID, &netns, 4);
  508. else
  509. invarg("Invalid \"netns\" value\n", *argv);
  510. } else if (strcmp(*argv, "multicast") == 0) {
  511. NEXT_ARG();
  512. req->i.ifi_change |= IFF_MULTICAST;
  513. if (strcmp(*argv, "on") == 0) {
  514. req->i.ifi_flags |= IFF_MULTICAST;
  515. } else if (strcmp(*argv, "off") == 0) {
  516. req->i.ifi_flags &= ~IFF_MULTICAST;
  517. } else
  518. return on_off("multicast", *argv);
  519. } else if (strcmp(*argv, "allmulticast") == 0) {
  520. NEXT_ARG();
  521. req->i.ifi_change |= IFF_ALLMULTI;
  522. if (strcmp(*argv, "on") == 0) {
  523. req->i.ifi_flags |= IFF_ALLMULTI;
  524. } else if (strcmp(*argv, "off") == 0) {
  525. req->i.ifi_flags &= ~IFF_ALLMULTI;
  526. } else
  527. return on_off("allmulticast", *argv);
  528. } else if (strcmp(*argv, "promisc") == 0) {
  529. NEXT_ARG();
  530. req->i.ifi_change |= IFF_PROMISC;
  531. if (strcmp(*argv, "on") == 0) {
  532. req->i.ifi_flags |= IFF_PROMISC;
  533. } else if (strcmp(*argv, "off") == 0) {
  534. req->i.ifi_flags &= ~IFF_PROMISC;
  535. } else
  536. return on_off("promisc", *argv);
  537. } else if (strcmp(*argv, "trailers") == 0) {
  538. NEXT_ARG();
  539. req->i.ifi_change |= IFF_NOTRAILERS;
  540. if (strcmp(*argv, "off") == 0) {
  541. req->i.ifi_flags |= IFF_NOTRAILERS;
  542. } else if (strcmp(*argv, "on") == 0) {
  543. req->i.ifi_flags &= ~IFF_NOTRAILERS;
  544. } else
  545. return on_off("trailers", *argv);
  546. } else if (strcmp(*argv, "arp") == 0) {
  547. NEXT_ARG();
  548. req->i.ifi_change |= IFF_NOARP;
  549. if (strcmp(*argv, "on") == 0) {
  550. req->i.ifi_flags &= ~IFF_NOARP;
  551. } else if (strcmp(*argv, "off") == 0) {
  552. req->i.ifi_flags |= IFF_NOARP;
  553. } else
  554. return on_off("noarp", *argv);
  555. } else if (strcmp(*argv, "vf") == 0) {
  556. struct rtattr *vflist;
  557. NEXT_ARG();
  558. if (get_integer(&vf, *argv, 0)) {
  559. invarg("Invalid \"vf\" value\n", *argv);
  560. }
  561. vflist = addattr_nest(&req->n, sizeof(*req),
  562. IFLA_VFINFO_LIST);
  563. len = iplink_parse_vf(vf, &argc, &argv, req);
  564. if (len < 0)
  565. return -1;
  566. addattr_nest_end(&req->n, vflist);
  567. } else if (matches(*argv, "master") == 0) {
  568. int ifindex;
  569. NEXT_ARG();
  570. ifindex = ll_name_to_index(*argv);
  571. if (!ifindex)
  572. invarg("Device does not exist\n", *argv);
  573. addattr_l(&req->n, sizeof(*req), IFLA_MASTER,
  574. &ifindex, 4);
  575. } else if (matches(*argv, "nomaster") == 0) {
  576. int ifindex = 0;
  577. addattr_l(&req->n, sizeof(*req), IFLA_MASTER,
  578. &ifindex, 4);
  579. } else if (matches(*argv, "dynamic") == 0) {
  580. NEXT_ARG();
  581. req->i.ifi_change |= IFF_DYNAMIC;
  582. if (strcmp(*argv, "on") == 0) {
  583. req->i.ifi_flags |= IFF_DYNAMIC;
  584. } else if (strcmp(*argv, "off") == 0) {
  585. req->i.ifi_flags &= ~IFF_DYNAMIC;
  586. } else
  587. return on_off("dynamic", *argv);
  588. } else if (matches(*argv, "alias") == 0) {
  589. NEXT_ARG();
  590. addattr_l(&req->n, sizeof(*req), IFLA_IFALIAS,
  591. *argv, strlen(*argv));
  592. argc--; argv++;
  593. break;
  594. } else if (strcmp(*argv, "group") == 0) {
  595. NEXT_ARG();
  596. if (*group != -1)
  597. duparg("group", *argv);
  598. if (rtnl_group_a2n(group, *argv))
  599. invarg("Invalid \"group\" value\n", *argv);
  600. } else if (strcmp(*argv, "mode") == 0) {
  601. int mode;
  602. NEXT_ARG();
  603. mode = get_link_mode(*argv);
  604. if (mode < 0)
  605. invarg("Invalid link mode\n", *argv);
  606. addattr8(&req->n, sizeof(*req), IFLA_LINKMODE, mode);
  607. } else if (strcmp(*argv, "state") == 0) {
  608. int state;
  609. NEXT_ARG();
  610. state = get_operstate(*argv);
  611. if (state < 0)
  612. invarg("Invalid operstate\n", *argv);
  613. addattr8(&req->n, sizeof(*req), IFLA_OPERSTATE, state);
  614. } else if (matches(*argv, "numtxqueues") == 0) {
  615. NEXT_ARG();
  616. if (numtxqueues != -1)
  617. duparg("numtxqueues", *argv);
  618. if (get_integer(&numtxqueues, *argv, 0))
  619. invarg("Invalid \"numtxqueues\" value\n", *argv);
  620. addattr_l(&req->n, sizeof(*req), IFLA_NUM_TX_QUEUES,
  621. &numtxqueues, 4);
  622. } else if (matches(*argv, "numrxqueues") == 0) {
  623. NEXT_ARG();
  624. if (numrxqueues != -1)
  625. duparg("numrxqueues", *argv);
  626. if (get_integer(&numrxqueues, *argv, 0))
  627. invarg("Invalid \"numrxqueues\" value\n", *argv);
  628. addattr_l(&req->n, sizeof(*req), IFLA_NUM_RX_QUEUES,
  629. &numrxqueues, 4);
  630. }
  631. #endif
  632. /* Return value becomes exitcode. It's okay to not return at all */
  633. int FAST_FUNC do_iplink(char **argv)
  634. {
  635. static const char keywords[] ALIGN1 =
  636. "add\0""delete\0""set\0""show\0""lst\0""list\0";
  637. if (*argv) {
  638. int key = index_in_substrings(keywords, *argv);
  639. if (key < 0) /* invalid argument */
  640. invarg(*argv, applet_name);
  641. argv++;
  642. if (key <= 1) /* add/delete */
  643. return do_add_or_delete(argv, key ? RTM_DELLINK : RTM_NEWLINK);
  644. if (key == 2) /* set */
  645. return do_set(argv);
  646. }
  647. /* show, lst, list */
  648. return ipaddr_list_link(argv);
  649. }