3
0

iplink.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. static void on_off(const char *msg) ATTRIBUTE_NORETURN;
  21. static void on_off(const char *msg)
  22. {
  23. bb_error_msg_and_die("error: argument of \"%s\" must be \"on\" or \"off\"", msg);
  24. }
  25. /* Exits on error */
  26. static int get_ctl_fd(void)
  27. {
  28. int fd;
  29. fd = socket(PF_INET, SOCK_DGRAM, 0);
  30. if (fd >= 0)
  31. return fd;
  32. fd = socket(PF_PACKET, SOCK_DGRAM, 0);
  33. if (fd >= 0)
  34. return fd;
  35. return xsocket(PF_INET6, SOCK_DGRAM, 0);
  36. }
  37. /* Exits on error */
  38. static void do_chflags(char *dev, uint32_t flags, uint32_t mask)
  39. {
  40. struct ifreq ifr;
  41. int fd;
  42. strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  43. fd = get_ctl_fd();
  44. xioctl(fd, SIOCGIFFLAGS, &ifr);
  45. if ((ifr.ifr_flags ^ flags) & mask) {
  46. ifr.ifr_flags &= ~mask;
  47. ifr.ifr_flags |= mask & flags;
  48. xioctl(fd, SIOCSIFFLAGS, &ifr);
  49. }
  50. close(fd);
  51. }
  52. /* Exits on error */
  53. static void do_changename(char *dev, char *newdev)
  54. {
  55. struct ifreq ifr;
  56. int fd;
  57. strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  58. strncpy(ifr.ifr_newname, newdev, sizeof(ifr.ifr_newname));
  59. fd = get_ctl_fd();
  60. xioctl(fd, SIOCSIFNAME, &ifr);
  61. close(fd);
  62. }
  63. /* Exits on error */
  64. static void set_qlen(char *dev, int qlen)
  65. {
  66. struct ifreq ifr;
  67. int s;
  68. s = get_ctl_fd();
  69. memset(&ifr, 0, sizeof(ifr));
  70. strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  71. ifr.ifr_qlen = qlen;
  72. xioctl(s, SIOCSIFTXQLEN, &ifr);
  73. close(s);
  74. }
  75. /* Exits on error */
  76. static void set_mtu(char *dev, int mtu)
  77. {
  78. struct ifreq ifr;
  79. int s;
  80. s = get_ctl_fd();
  81. memset(&ifr, 0, sizeof(ifr));
  82. strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  83. ifr.ifr_mtu = mtu;
  84. xioctl(s, SIOCSIFMTU, &ifr);
  85. close(s);
  86. }
  87. /* Exits on error */
  88. static int get_address(char *dev, int *htype)
  89. {
  90. struct ifreq ifr;
  91. struct sockaddr_ll me;
  92. socklen_t alen;
  93. int s;
  94. s = xsocket(PF_PACKET, SOCK_DGRAM, 0);
  95. memset(&ifr, 0, sizeof(ifr));
  96. strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  97. xioctl(s, SIOCGIFINDEX, &ifr);
  98. memset(&me, 0, sizeof(me));
  99. me.sll_family = AF_PACKET;
  100. me.sll_ifindex = ifr.ifr_ifindex;
  101. me.sll_protocol = htons(ETH_P_LOOP);
  102. xbind(s, (struct sockaddr*)&me, sizeof(me));
  103. alen = sizeof(me);
  104. if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
  105. bb_perror_msg_and_die("getsockname");
  106. }
  107. close(s);
  108. *htype = me.sll_hatype;
  109. return me.sll_halen;
  110. }
  111. /* Exits on error */
  112. static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
  113. {
  114. int alen;
  115. memset(ifr, 0, sizeof(*ifr));
  116. strncpy(ifr->ifr_name, dev, sizeof(ifr->ifr_name));
  117. ifr->ifr_hwaddr.sa_family = hatype;
  118. alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), 14, lla);
  119. if (alen < 0)
  120. exit(1);
  121. if (alen != halen) {
  122. bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
  123. }
  124. }
  125. /* Exits on error */
  126. static void set_address(struct ifreq *ifr, int brd)
  127. {
  128. int s;
  129. s = get_ctl_fd();
  130. if (brd)
  131. xioctl(s, SIOCSIFHWBROADCAST, ifr);
  132. else
  133. xioctl(s, SIOCSIFHWADDR, ifr);
  134. close(s);
  135. }
  136. /* Return value becomes exitcode. It's okay to not return at all */
  137. static int do_set(int argc, char **argv)
  138. {
  139. char *dev = NULL;
  140. uint32_t mask = 0;
  141. uint32_t flags = 0;
  142. int qlen = -1;
  143. int mtu = -1;
  144. char *newaddr = NULL;
  145. char *newbrd = NULL;
  146. struct ifreq ifr0, ifr1;
  147. char *newname = NULL;
  148. int htype, halen;
  149. static const char keywords[] ALIGN1 =
  150. "up\0""down\0""name\0""mtu\0""multicast\0""arp\0""addr\0""dev\0"
  151. "on\0""off\0";
  152. enum { ARG_up = 1, ARG_down, ARG_name, ARG_mtu, ARG_multicast, ARG_arp,
  153. ARG_addr, ARG_dev, PARM_on, PARM_off };
  154. smalluint key;
  155. while (argc > 0) {
  156. key = index_in_strings(keywords, *argv) + 1;
  157. if (key == ARG_up) {
  158. mask |= IFF_UP;
  159. flags |= IFF_UP;
  160. } else if (key == ARG_down) {
  161. mask |= IFF_UP;
  162. flags &= ~IFF_UP;
  163. } else if (key == ARG_name) {
  164. NEXT_ARG();
  165. newname = *argv;
  166. } else if (key == ARG_mtu) {
  167. NEXT_ARG();
  168. if (mtu != -1)
  169. duparg("mtu", *argv);
  170. if (get_integer(&mtu, *argv, 0))
  171. invarg(*argv, "mtu");
  172. } else if (key == ARG_multicast) {
  173. NEXT_ARG();
  174. mask |= IFF_MULTICAST;
  175. key = index_in_strings(keywords, *argv) + 1;
  176. if (key == PARM_on) {
  177. flags |= IFF_MULTICAST;
  178. } else if (key == PARM_off) {
  179. flags &= ~IFF_MULTICAST;
  180. } else
  181. on_off("multicast");
  182. } else if (key == ARG_arp) {
  183. NEXT_ARG();
  184. mask |= IFF_NOARP;
  185. key = index_in_strings(keywords, *argv) + 1;
  186. if (key == PARM_on) {
  187. flags &= ~IFF_NOARP;
  188. } else if (key == PARM_off) {
  189. flags |= IFF_NOARP;
  190. } else
  191. on_off("arp");
  192. } else if (key == ARG_addr) {
  193. NEXT_ARG();
  194. newaddr = *argv;
  195. } else {
  196. if (key == ARG_dev) {
  197. NEXT_ARG();
  198. }
  199. if (dev)
  200. duparg2("dev", *argv);
  201. dev = *argv;
  202. }
  203. argc--; argv++;
  204. }
  205. if (!dev) {
  206. bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
  207. }
  208. if (newaddr || newbrd) {
  209. halen = get_address(dev, &htype);
  210. if (newaddr) {
  211. parse_address(dev, htype, halen, newaddr, &ifr0);
  212. }
  213. if (newbrd) {
  214. parse_address(dev, htype, halen, newbrd, &ifr1);
  215. }
  216. }
  217. if (newname && strcmp(dev, newname)) {
  218. do_changename(dev, newname);
  219. dev = newname;
  220. }
  221. if (qlen != -1) {
  222. set_qlen(dev, qlen);
  223. }
  224. if (mtu != -1) {
  225. set_mtu(dev, mtu);
  226. }
  227. if (newaddr || newbrd) {
  228. if (newbrd) {
  229. set_address(&ifr1, 1);
  230. }
  231. if (newaddr) {
  232. set_address(&ifr0, 0);
  233. }
  234. }
  235. if (mask)
  236. do_chflags(dev, flags, mask);
  237. return 0;
  238. }
  239. static int ipaddr_list_link(int argc, char **argv)
  240. {
  241. preferred_family = AF_PACKET;
  242. return ipaddr_list_or_flush(argc, argv, 0);
  243. }
  244. /* Return value becomes exitcode. It's okay to not return at all */
  245. int do_iplink(int argc, char **argv)
  246. {
  247. static const char keywords[] ALIGN1 =
  248. "set\0""show\0""lst\0""list\0";
  249. smalluint key;
  250. if (argc <= 0)
  251. return ipaddr_list_link(0, NULL);
  252. key = index_in_substrings(keywords, *argv) + 1;
  253. if (key == 0)
  254. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
  255. argc--; argv++;
  256. if (key == 1) /* set */
  257. return do_set(argc, argv);
  258. else /* show, lst, list */
  259. return ipaddr_list_link(argc, argv);
  260. }