iplink.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * iplink.c "ip link".
  4. *
  5. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. //#include <sys/ioctl.h>
  10. //#include <sys/socket.h>
  11. #include <net/if.h>
  12. #include <net/if_packet.h>
  13. #include <netpacket/packet.h>
  14. #include <net/ethernet.h>
  15. #include "ip_common.h" /* #include "libbb.h" is inside */
  16. #include "rt_names.h"
  17. #include "utils.h"
  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(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  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(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  53. strncpy(ifr.ifr_newname, newdev, sizeof(ifr.ifr_newname));
  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(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  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(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  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(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  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. if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
  100. bb_perror_msg_and_die("getsockname");
  101. }
  102. close(s);
  103. *htype = me.sll_hatype;
  104. return me.sll_halen;
  105. }
  106. /* Exits on error */
  107. static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
  108. {
  109. int alen;
  110. memset(ifr, 0, sizeof(*ifr));
  111. strncpy(ifr->ifr_name, dev, sizeof(ifr->ifr_name));
  112. ifr->ifr_hwaddr.sa_family = hatype;
  113. alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/;
  114. alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla);
  115. if (alen < 0)
  116. exit(EXIT_FAILURE);
  117. if (alen != halen) {
  118. bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
  119. }
  120. }
  121. /* Exits on error */
  122. static void set_address(struct ifreq *ifr, int brd)
  123. {
  124. int s;
  125. s = get_ctl_fd();
  126. if (brd)
  127. xioctl(s, SIOCSIFHWBROADCAST, ifr);
  128. else
  129. xioctl(s, SIOCSIFHWADDR, ifr);
  130. close(s);
  131. }
  132. static void die_must_be_on_off(const char *msg) ATTRIBUTE_NORETURN;
  133. static void die_must_be_on_off(const char *msg)
  134. {
  135. bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg);
  136. }
  137. /* Return value becomes exitcode. It's okay to not return at all */
  138. static int do_set(char **argv)
  139. {
  140. char *dev = NULL;
  141. uint32_t mask = 0;
  142. uint32_t flags = 0;
  143. int qlen = -1;
  144. int mtu = -1;
  145. char *newaddr = NULL;
  146. char *newbrd = NULL;
  147. struct ifreq ifr0, ifr1;
  148. char *newname = NULL;
  149. int htype, halen;
  150. static const char keywords[] ALIGN1 =
  151. "up\0""down\0""name\0""mtu\0""multicast\0""arp\0""addr\0""dev\0";
  152. enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_multicast, ARG_arp,
  153. ARG_addr, ARG_dev };
  154. static const char str_on_off[] ALIGN1 = "on\0""off\0";
  155. enum { PARM_on = 0, PARM_off };
  156. smalluint key;
  157. while (*argv) {
  158. key = index_in_strings(keywords, *argv);
  159. if (key == ARG_up) {
  160. mask |= IFF_UP;
  161. flags |= IFF_UP;
  162. }
  163. if (key == ARG_down) {
  164. mask |= IFF_UP;
  165. flags &= ~IFF_UP;
  166. }
  167. if (key == ARG_name) {
  168. NEXT_ARG();
  169. newname = *argv;
  170. }
  171. if (key == ARG_mtu) {
  172. NEXT_ARG();
  173. if (mtu != -1)
  174. duparg("mtu", *argv);
  175. if (get_integer(&mtu, *argv, 0))
  176. invarg(*argv, "mtu");
  177. }
  178. if (key == ARG_multicast) {
  179. int param;
  180. NEXT_ARG();
  181. mask |= IFF_MULTICAST;
  182. param = index_in_strings(str_on_off, *argv);
  183. if (param < 0)
  184. die_must_be_on_off("multicast");
  185. if (param == PARM_on)
  186. flags |= IFF_MULTICAST;
  187. else
  188. flags &= ~IFF_MULTICAST;
  189. }
  190. if (key == ARG_arp) {
  191. int param;
  192. NEXT_ARG();
  193. mask |= IFF_NOARP;
  194. param = index_in_strings(str_on_off, *argv);
  195. if (param < 0)
  196. die_must_be_on_off("arp");
  197. if (param == PARM_on)
  198. flags &= ~IFF_NOARP;
  199. else
  200. flags |= IFF_NOARP;
  201. }
  202. if (key == ARG_addr) {
  203. NEXT_ARG();
  204. newaddr = *argv;
  205. }
  206. if (key >= ARG_dev) {
  207. if (key == ARG_dev) {
  208. NEXT_ARG();
  209. }
  210. if (dev)
  211. duparg2("dev", *argv);
  212. dev = *argv;
  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. }
  224. if (newbrd) {
  225. parse_address(dev, htype, halen, newbrd, &ifr1);
  226. }
  227. }
  228. if (newname && strcmp(dev, newname)) {
  229. do_changename(dev, newname);
  230. dev = newname;
  231. }
  232. if (qlen != -1) {
  233. set_qlen(dev, qlen);
  234. }
  235. if (mtu != -1) {
  236. set_mtu(dev, mtu);
  237. }
  238. if (newaddr || newbrd) {
  239. if (newbrd) {
  240. set_address(&ifr1, 1);
  241. }
  242. if (newaddr) {
  243. set_address(&ifr0, 0);
  244. }
  245. }
  246. if (mask)
  247. do_chflags(dev, flags, mask);
  248. return 0;
  249. }
  250. static int ipaddr_list_link(char **argv)
  251. {
  252. preferred_family = AF_PACKET;
  253. return ipaddr_list_or_flush(argv, 0);
  254. }
  255. /* Return value becomes exitcode. It's okay to not return at all */
  256. int do_iplink(char **argv)
  257. {
  258. static const char keywords[] ALIGN1 =
  259. "set\0""show\0""lst\0""list\0";
  260. int key;
  261. if (!*argv)
  262. return ipaddr_list_link(argv);
  263. key = index_in_substrings(keywords, *argv);
  264. if (key < 0)
  265. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
  266. argv++;
  267. if (key == 0) /* set */
  268. return do_set(argv);
  269. /* show, lst, list */
  270. return ipaddr_list_link(argv);
  271. }