iplink.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), 14, lla);
  114. if (alen < 0)
  115. exit(1);
  116. if (alen != halen) {
  117. bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen);
  118. }
  119. }
  120. /* Exits on error */
  121. static void set_address(struct ifreq *ifr, int brd)
  122. {
  123. int s;
  124. s = get_ctl_fd();
  125. if (brd)
  126. xioctl(s, SIOCSIFHWBROADCAST, ifr);
  127. else
  128. xioctl(s, SIOCSIFHWADDR, ifr);
  129. close(s);
  130. }
  131. static void die_must_be_on_off(const char *msg) ATTRIBUTE_NORETURN;
  132. static void die_must_be_on_off(const char *msg)
  133. {
  134. bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg);
  135. }
  136. /* Return value becomes exitcode. It's okay to not return at all */
  137. static int do_set(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. enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_multicast, ARG_arp,
  152. ARG_addr, ARG_dev };
  153. static const char str_on_off[] ALIGN1 = "on\0""off\0";
  154. enum { PARM_on = 0, PARM_off };
  155. smalluint key;
  156. while (*argv) {
  157. key = index_in_strings(keywords, *argv);
  158. if (key == ARG_up) {
  159. mask |= IFF_UP;
  160. flags |= IFF_UP;
  161. }
  162. if (key == ARG_down) {
  163. mask |= IFF_UP;
  164. flags &= ~IFF_UP;
  165. }
  166. if (key == ARG_name) {
  167. NEXT_ARG();
  168. newname = *argv;
  169. }
  170. if (key == ARG_mtu) {
  171. NEXT_ARG();
  172. if (mtu != -1)
  173. duparg("mtu", *argv);
  174. if (get_integer(&mtu, *argv, 0))
  175. invarg(*argv, "mtu");
  176. }
  177. if (key == ARG_multicast) {
  178. int param;
  179. NEXT_ARG();
  180. mask |= IFF_MULTICAST;
  181. param = index_in_strings(str_on_off, *argv);
  182. if (param < 0)
  183. die_must_be_on_off("multicast");
  184. if (param == PARM_on)
  185. flags |= IFF_MULTICAST;
  186. else
  187. flags &= ~IFF_MULTICAST;
  188. }
  189. if (key == ARG_arp) {
  190. int param;
  191. NEXT_ARG();
  192. mask |= IFF_NOARP;
  193. param = index_in_strings(str_on_off, *argv);
  194. if (param < 0)
  195. die_must_be_on_off("arp");
  196. if (param == PARM_on)
  197. flags &= ~IFF_NOARP;
  198. else
  199. flags |= IFF_NOARP;
  200. }
  201. if (key == ARG_addr) {
  202. NEXT_ARG();
  203. newaddr = *argv;
  204. }
  205. if (key >= ARG_dev) {
  206. if (key == ARG_dev) {
  207. NEXT_ARG();
  208. }
  209. if (dev)
  210. duparg2("dev", *argv);
  211. dev = *argv;
  212. }
  213. argv++;
  214. }
  215. if (!dev) {
  216. bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\"");
  217. }
  218. if (newaddr || newbrd) {
  219. halen = get_address(dev, &htype);
  220. if (newaddr) {
  221. parse_address(dev, htype, halen, newaddr, &ifr0);
  222. }
  223. if (newbrd) {
  224. parse_address(dev, htype, halen, newbrd, &ifr1);
  225. }
  226. }
  227. if (newname && strcmp(dev, newname)) {
  228. do_changename(dev, newname);
  229. dev = newname;
  230. }
  231. if (qlen != -1) {
  232. set_qlen(dev, qlen);
  233. }
  234. if (mtu != -1) {
  235. set_mtu(dev, mtu);
  236. }
  237. if (newaddr || newbrd) {
  238. if (newbrd) {
  239. set_address(&ifr1, 1);
  240. }
  241. if (newaddr) {
  242. set_address(&ifr0, 0);
  243. }
  244. }
  245. if (mask)
  246. do_chflags(dev, flags, mask);
  247. return 0;
  248. }
  249. static int ipaddr_list_link(char **argv)
  250. {
  251. preferred_family = AF_PACKET;
  252. return ipaddr_list_or_flush(argv, 0);
  253. }
  254. /* Return value becomes exitcode. It's okay to not return at all */
  255. int do_iplink(char **argv)
  256. {
  257. static const char keywords[] ALIGN1 =
  258. "set\0""show\0""lst\0""list\0";
  259. int key;
  260. if (!*argv)
  261. return ipaddr_list_link(argv);
  262. key = index_in_substrings(keywords, *argv);
  263. if (key < 0)
  264. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
  265. argv++;
  266. if (key == 0) /* set */
  267. return do_set(argv);
  268. /* show, lst, list */
  269. return ipaddr_list_link(argv);
  270. }