iplink.c 16 KB

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