3
0

iplink.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  4. *
  5. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  6. */
  7. #include <net/if.h>
  8. #include <net/if_packet.h>
  9. #include <netpacket/packet.h>
  10. #include <netinet/if_ether.h>
  11. #include "ip_common.h" /* #include "libbb.h" is inside */
  12. #include "rt_names.h"
  13. #include "utils.h"
  14. #ifndef IFLA_LINKINFO
  15. # define IFLA_LINKINFO 18
  16. # define IFLA_INFO_KIND 1
  17. #endif
  18. /* taken from linux/sockios.h */
  19. #define SIOCSIFNAME 0x8923 /* set interface name */
  20. /* Exits on error */
  21. static int get_ctl_fd(void)
  22. {
  23. int fd;
  24. fd = socket(PF_INET, SOCK_DGRAM, 0);
  25. if (fd >= 0)
  26. return fd;
  27. fd = socket(PF_PACKET, SOCK_DGRAM, 0);
  28. if (fd >= 0)
  29. return fd;
  30. return xsocket(PF_INET6, SOCK_DGRAM, 0);
  31. }
  32. /* Exits on error */
  33. static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
  34. {
  35. struct ifreq ifr;
  36. int fd;
  37. strncpy_IFNAMSIZ(ifr.ifr_name, dev);
  38. fd = get_ctl_fd();
  39. xioctl(fd, SIOCGIFFLAGS, &ifr);
  40. if ((ifr.ifr_flags ^ flags) & mask) {
  41. ifr.ifr_flags &= ~mask;
  42. ifr.ifr_flags |= mask & flags;
  43. xioctl(fd, SIOCSIFFLAGS, &ifr);
  44. }
  45. close(fd);
  46. }
  47. /* Exits on error */
  48. static void do_changename(char *dev, char *newdev)
  49. {
  50. struct ifreq ifr;
  51. int fd;
  52. strncpy_IFNAMSIZ(ifr.ifr_name, dev);
  53. strncpy_IFNAMSIZ(ifr.ifr_newname, newdev);
  54. fd = get_ctl_fd();
  55. xioctl(fd, SIOCSIFNAME, &ifr);
  56. close(fd);
  57. }
  58. /* Exits on error */
  59. static void set_qlen(char *dev, int qlen)
  60. {
  61. struct ifreq ifr;
  62. int s;
  63. s = get_ctl_fd();
  64. memset(&ifr, 0, sizeof(ifr));
  65. strncpy_IFNAMSIZ(ifr.ifr_name, dev);
  66. ifr.ifr_qlen = qlen;
  67. xioctl(s, SIOCSIFTXQLEN, &ifr);
  68. close(s);
  69. }
  70. /* Exits on error */
  71. static void set_mtu(char *dev, int mtu)
  72. {
  73. struct ifreq ifr;
  74. int s;
  75. s = get_ctl_fd();
  76. memset(&ifr, 0, sizeof(ifr));
  77. strncpy_IFNAMSIZ(ifr.ifr_name, dev);
  78. ifr.ifr_mtu = mtu;
  79. xioctl(s, SIOCSIFMTU, &ifr);
  80. close(s);
  81. }
  82. /* Exits on error */
  83. static int get_address(char *dev, int *htype)
  84. {
  85. struct ifreq ifr;
  86. struct sockaddr_ll me;
  87. socklen_t alen;
  88. int s;
  89. s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
  90. memset(&ifr, 0, sizeof(ifr));
  91. strncpy_IFNAMSIZ(ifr.ifr_name, dev);
  92. xioctl(s, SIOCGIFINDEX, &ifr);
  93. memset(&me, 0, sizeof(me));
  94. me.sll_family = AF_PACKET;
  95. me.sll_ifindex = ifr.ifr_ifindex;
  96. me.sll_protocol = htons(ETH_P_LOOP);
  97. xbind(s, (struct sockaddr*)&me, sizeof(me));
  98. alen = sizeof(me);
  99. getsockname(s, (struct sockaddr*)&me, &alen);
  100. //never happens:
  101. //if (getsockname(s, (struct sockaddr*)&me, &alen) == -1)
  102. // bb_perror_msg_and_die("getsockname");
  103. close(s);
  104. *htype = me.sll_hatype;
  105. return me.sll_halen;
  106. }
  107. /* Exits on error */
  108. static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
  109. {
  110. int alen;
  111. memset(ifr, 0, sizeof(*ifr));
  112. strncpy_IFNAMSIZ(ifr->ifr_name, dev);
  113. ifr->ifr_hwaddr.sa_family = hatype;
  114. alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;
  115. alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla);
  116. if (alen < 0)
  117. exit(EXIT_FAILURE);
  118. if (alen != halen) {
  119. bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
  120. }
  121. }
  122. /* Exits on error */
  123. static void set_address(struct ifreq *ifr, int brd)
  124. {
  125. int s;
  126. s = get_ctl_fd();
  127. if (brd)
  128. xioctl(s, SIOCSIFHWBROADCAST, ifr);
  129. else
  130. xioctl(s, SIOCSIFHWADDR, ifr);
  131. close(s);
  132. }
  133. static void die_must_be_on_off(const char *msg) NORETURN;
  134. static void die_must_be_on_off(const char *msg)
  135. {
  136. bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg);
  137. }
  138. /* Return value becomes exitcode. It's okay to not return at all */
  139. static int do_set(char **argv)
  140. {
  141. char *dev = NULL;
  142. uint32_t mask = 0;
  143. uint32_t flags = 0;
  144. int qlen = -1;
  145. int mtu = -1;
  146. char *newaddr = NULL;
  147. char *newbrd = NULL;
  148. struct ifreq ifr0, ifr1;
  149. char *newname = NULL;
  150. int htype, halen;
  151. static const char keywords[] ALIGN1 =
  152. "up\0""down\0""name\0""mtu\0""qlen\0""multicast\0"
  153. "arp\0""address\0""dev\0";
  154. enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_qlen, ARG_multicast,
  155. ARG_arp, ARG_addr, ARG_dev };
  156. static const char str_on_off[] ALIGN1 = "on\0""off\0";
  157. enum { PARM_on = 0, PARM_off };
  158. smalluint key;
  159. while (*argv) {
  160. /* substring search ensures that e.g. "addr" and "address"
  161. * are both accepted */
  162. key = index_in_substrings(keywords, *argv);
  163. if (key == ARG_up) {
  164. mask |= IFF_UP;
  165. flags |= IFF_UP;
  166. } else if (key == ARG_down) {
  167. mask |= IFF_UP;
  168. flags &= ~IFF_UP;
  169. } else if (key == ARG_name) {
  170. NEXT_ARG();
  171. newname = *argv;
  172. } else if (key == ARG_mtu) {
  173. NEXT_ARG();
  174. if (mtu != -1)
  175. duparg("mtu", *argv);
  176. mtu = get_unsigned(*argv, "mtu");
  177. } else if (key == ARG_qlen) {
  178. NEXT_ARG();
  179. if (qlen != -1)
  180. duparg("qlen", *argv);
  181. qlen = get_unsigned(*argv, "qlen");
  182. } else if (key == ARG_addr) {
  183. NEXT_ARG();
  184. newaddr = *argv;
  185. } else if (key >= ARG_dev) {
  186. if (key == ARG_dev) {
  187. NEXT_ARG();
  188. }
  189. if (dev)
  190. duparg2("dev", *argv);
  191. dev = *argv;
  192. } else {
  193. int param;
  194. NEXT_ARG();
  195. param = index_in_strings(str_on_off, *argv);
  196. if (key == ARG_multicast) {
  197. if (param < 0)
  198. die_must_be_on_off("multicast");
  199. mask |= IFF_MULTICAST;
  200. if (param == PARM_on)
  201. flags |= IFF_MULTICAST;
  202. else
  203. flags &= ~IFF_MULTICAST;
  204. } else if (key == ARG_arp) {
  205. if (param < 0)
  206. die_must_be_on_off("arp");
  207. mask |= IFF_NOARP;
  208. if (param == PARM_on)
  209. flags &= ~IFF_NOARP;
  210. else
  211. flags |= IFF_NOARP;
  212. }
  213. }
  214. argv++;
  215. }
  216. if (!dev) {
  217. bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
  218. }
  219. if (newaddr || newbrd) {
  220. halen = get_address(dev, &htype);
  221. if (newaddr) {
  222. parse_address(dev, htype, halen, newaddr, &ifr0);
  223. set_address(&ifr0, 0);
  224. }
  225. if (newbrd) {
  226. parse_address(dev, htype, halen, newbrd, &ifr1);
  227. set_address(&ifr1, 1);
  228. }
  229. }
  230. if (newname && strcmp(dev, newname)) {
  231. do_changename(dev, newname);
  232. dev = newname;
  233. }
  234. if (qlen != -1) {
  235. set_qlen(dev, qlen);
  236. }
  237. if (mtu != -1) {
  238. set_mtu(dev, mtu);
  239. }
  240. if (mask)
  241. do_chflags(dev, flags, mask);
  242. return 0;
  243. }
  244. static int ipaddr_list_link(char **argv)
  245. {
  246. preferred_family = AF_PACKET;
  247. return ipaddr_list_or_flush(argv, 0);
  248. }
  249. #ifndef NLMSG_TAIL
  250. #define NLMSG_TAIL(nmsg) \
  251. ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
  252. #endif
  253. /* Return value becomes exitcode. It's okay to not return at all */
  254. static int do_change(char **argv, const unsigned rtm)
  255. {
  256. static const char keywords[] ALIGN1 =
  257. "link\0""name\0""type\0""dev\0";
  258. enum {
  259. ARG_link,
  260. ARG_name,
  261. ARG_type,
  262. ARG_dev,
  263. };
  264. struct rtnl_handle rth;
  265. struct {
  266. struct nlmsghdr n;
  267. struct ifinfomsg i;
  268. char buf[1024];
  269. } req;
  270. smalluint arg;
  271. char *name_str = NULL, *link_str = NULL, *type_str = NULL, *dev_str = NULL;
  272. memset(&req, 0, sizeof(req));
  273. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
  274. req.n.nlmsg_flags = NLM_F_REQUEST;
  275. req.n.nlmsg_type = rtm;
  276. req.i.ifi_family = preferred_family;
  277. if (rtm == RTM_NEWLINK)
  278. req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
  279. while (*argv) {
  280. arg = index_in_substrings(keywords, *argv);
  281. if (arg == ARG_link) {
  282. NEXT_ARG();
  283. link_str = *argv;
  284. } else if (arg == ARG_name) {
  285. NEXT_ARG();
  286. name_str = *argv;
  287. } else if (arg == ARG_type) {
  288. NEXT_ARG();
  289. type_str = *argv;
  290. } else {
  291. if (arg == ARG_dev) {
  292. if (dev_str)
  293. duparg(*argv, "dev");
  294. NEXT_ARG();
  295. }
  296. dev_str = *argv;
  297. }
  298. argv++;
  299. }
  300. xrtnl_open(&rth);
  301. ll_init_map(&rth);
  302. if (type_str) {
  303. struct rtattr *linkinfo = NLMSG_TAIL(&req.n);
  304. addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0);
  305. addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str,
  306. strlen(type_str));
  307. linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo;
  308. }
  309. if (rtm != RTM_NEWLINK) {
  310. if (!dev_str)
  311. return 1; /* Need a device to delete */
  312. req.i.ifi_index = xll_name_to_index(dev_str);
  313. } else {
  314. if (!name_str)
  315. name_str = dev_str;
  316. if (link_str) {
  317. int idx = xll_name_to_index(link_str);
  318. addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4);
  319. }
  320. }
  321. if (name_str) {
  322. const size_t name_len = strlen(name_str) + 1;
  323. if (name_len < 2 || name_len > IFNAMSIZ)
  324. invarg(name_str, "name");
  325. addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name_str, name_len);
  326. }
  327. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  328. return 2;
  329. return 0;
  330. }
  331. /* Return value becomes exitcode. It's okay to not return at all */
  332. int FAST_FUNC do_iplink(char **argv)
  333. {
  334. static const char keywords[] ALIGN1 =
  335. "add\0""delete\0""set\0""show\0""lst\0""list\0";
  336. if (*argv) {
  337. smalluint key = index_in_substrings(keywords, *argv);
  338. if (key > 5) /* invalid argument */
  339. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
  340. argv++;
  341. if (key <= 1) /* add/delete */
  342. return do_change(argv, key ? RTM_DELLINK : RTM_NEWLINK);
  343. else if (key == 2) /* set */
  344. return do_set(argv);
  345. }
  346. /* show, lst, list */
  347. return ipaddr_list_link(argv);
  348. }