iplink.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 "libbb.h"
  10. #include <sys/ioctl.h>
  11. #include <sys/socket.h>
  12. #include <net/if.h>
  13. #include <net/if_packet.h>
  14. #include <netpacket/packet.h>
  15. #include <net/ethernet.h>
  16. #include "rt_names.h"
  17. #include "utils.h"
  18. #include "ip_common.h"
  19. /* take from linux/sockios.h */
  20. #define SIOCSIFNAME 0x8923 /* set interface name */
  21. static int on_off(char *msg)
  22. {
  23. bb_error_msg("error: argument of \"%s\" must be \"on\" or \"off\"", msg);
  24. return -1;
  25. }
  26. static int get_ctl_fd(void)
  27. {
  28. int s_errno;
  29. int fd;
  30. fd = socket(PF_INET, SOCK_DGRAM, 0);
  31. if (fd >= 0)
  32. return fd;
  33. s_errno = errno;
  34. fd = socket(PF_PACKET, SOCK_DGRAM, 0);
  35. if (fd >= 0)
  36. return fd;
  37. fd = socket(PF_INET6, SOCK_DGRAM, 0);
  38. if (fd >= 0)
  39. return fd;
  40. errno = s_errno;
  41. bb_perror_msg("cannot create control socket");
  42. return -1;
  43. }
  44. static int do_chflags(char *dev, uint32_t flags, uint32_t mask)
  45. {
  46. struct ifreq ifr;
  47. int fd;
  48. int err;
  49. strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  50. fd = get_ctl_fd();
  51. if (fd < 0)
  52. return -1;
  53. err = ioctl(fd, SIOCGIFFLAGS, &ifr);
  54. if (err) {
  55. bb_perror_msg("SIOCGIFFLAGS");
  56. close(fd);
  57. return -1;
  58. }
  59. if ((ifr.ifr_flags^flags)&mask) {
  60. ifr.ifr_flags &= ~mask;
  61. ifr.ifr_flags |= mask&flags;
  62. err = ioctl(fd, SIOCSIFFLAGS, &ifr);
  63. if (err)
  64. bb_perror_msg("SIOCSIFFLAGS");
  65. }
  66. close(fd);
  67. return err;
  68. }
  69. static int do_changename(char *dev, char *newdev)
  70. {
  71. struct ifreq ifr;
  72. int fd;
  73. int err;
  74. strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  75. strncpy(ifr.ifr_newname, newdev, sizeof(ifr.ifr_newname));
  76. fd = get_ctl_fd();
  77. if (fd < 0)
  78. return -1;
  79. err = ioctl(fd, SIOCSIFNAME, &ifr);
  80. if (err) {
  81. bb_perror_msg("SIOCSIFNAME");
  82. close(fd);
  83. return -1;
  84. }
  85. close(fd);
  86. return err;
  87. }
  88. static int set_qlen(char *dev, int qlen)
  89. {
  90. struct ifreq ifr;
  91. int s;
  92. s = get_ctl_fd();
  93. if (s < 0)
  94. return -1;
  95. memset(&ifr, 0, sizeof(ifr));
  96. strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  97. ifr.ifr_qlen = qlen;
  98. if (ioctl(s, SIOCSIFTXQLEN, &ifr) < 0) {
  99. bb_perror_msg("SIOCSIFXQLEN");
  100. close(s);
  101. return -1;
  102. }
  103. close(s);
  104. return 0;
  105. }
  106. static int set_mtu(char *dev, int mtu)
  107. {
  108. struct ifreq ifr;
  109. int s;
  110. s = get_ctl_fd();
  111. if (s < 0)
  112. return -1;
  113. memset(&ifr, 0, sizeof(ifr));
  114. strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  115. ifr.ifr_mtu = mtu;
  116. if (ioctl(s, SIOCSIFMTU, &ifr) < 0) {
  117. bb_perror_msg("SIOCSIFMTU");
  118. close(s);
  119. return -1;
  120. }
  121. close(s);
  122. return 0;
  123. }
  124. static int get_address(char *dev, int *htype)
  125. {
  126. struct ifreq ifr;
  127. struct sockaddr_ll me;
  128. socklen_t alen;
  129. int s;
  130. s = socket(PF_PACKET, SOCK_DGRAM, 0);
  131. if (s < 0) {
  132. bb_perror_msg("socket(PF_PACKET)");
  133. return -1;
  134. }
  135. memset(&ifr, 0, sizeof(ifr));
  136. strncpy(ifr.ifr_name, dev, sizeof(ifr.ifr_name));
  137. if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
  138. bb_perror_msg("SIOCGIFINDEX");
  139. close(s);
  140. return -1;
  141. }
  142. memset(&me, 0, sizeof(me));
  143. me.sll_family = AF_PACKET;
  144. me.sll_ifindex = ifr.ifr_ifindex;
  145. me.sll_protocol = htons(ETH_P_LOOP);
  146. if (bind(s, (struct sockaddr*)&me, sizeof(me)) == -1) {
  147. bb_perror_msg("bind");
  148. close(s);
  149. return -1;
  150. }
  151. alen = sizeof(me);
  152. if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
  153. bb_perror_msg("getsockname");
  154. close(s);
  155. return -1;
  156. }
  157. close(s);
  158. *htype = me.sll_hatype;
  159. return me.sll_halen;
  160. }
  161. static int parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr)
  162. {
  163. int alen;
  164. memset(ifr, 0, sizeof(*ifr));
  165. strncpy(ifr->ifr_name, dev, sizeof(ifr->ifr_name));
  166. ifr->ifr_hwaddr.sa_family = hatype;
  167. alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), 14, lla);
  168. if (alen < 0)
  169. return -1;
  170. if (alen != halen) {
  171. bb_error_msg("wrong address (%s) length: expected %d bytes", lla, halen);
  172. return -1;
  173. }
  174. return 0;
  175. }
  176. static int set_address(struct ifreq *ifr, int brd)
  177. {
  178. int s;
  179. s = get_ctl_fd();
  180. if (s < 0)
  181. return -1;
  182. if (ioctl(s, brd?SIOCSIFHWBROADCAST:SIOCSIFHWADDR, ifr) < 0) {
  183. bb_perror_msg(brd ? "SIOCSIFHWBROADCAST" : "SIOCSIFHWADDR");
  184. close(s);
  185. return -1;
  186. }
  187. close(s);
  188. return 0;
  189. }
  190. static int do_set(int argc, char **argv)
  191. {
  192. char *dev = NULL;
  193. uint32_t mask = 0;
  194. uint32_t flags = 0;
  195. int qlen = -1;
  196. int mtu = -1;
  197. char *newaddr = NULL;
  198. char *newbrd = NULL;
  199. struct ifreq ifr0, ifr1;
  200. char *newname = NULL;
  201. int htype, halen;
  202. while (argc > 0) {
  203. if (strcmp(*argv, "up") == 0) {
  204. mask |= IFF_UP;
  205. flags |= IFF_UP;
  206. } else if (strcmp(*argv, "down") == 0) {
  207. mask |= IFF_UP;
  208. flags &= ~IFF_UP;
  209. } else if (strcmp(*argv, "name") == 0) {
  210. NEXT_ARG();
  211. newname = *argv;
  212. } else if (strcmp(*argv, "mtu") == 0) {
  213. NEXT_ARG();
  214. if (mtu != -1)
  215. duparg("mtu", *argv);
  216. if (get_integer(&mtu, *argv, 0))
  217. invarg(*argv, "mtu");
  218. } else if (strcmp(*argv, "multicast") == 0) {
  219. NEXT_ARG();
  220. mask |= IFF_MULTICAST;
  221. if (strcmp(*argv, "on") == 0) {
  222. flags |= IFF_MULTICAST;
  223. } else if (strcmp(*argv, "off") == 0) {
  224. flags &= ~IFF_MULTICAST;
  225. } else
  226. return on_off("multicast");
  227. } else if (strcmp(*argv, "arp") == 0) {
  228. NEXT_ARG();
  229. mask |= IFF_NOARP;
  230. if (strcmp(*argv, "on") == 0) {
  231. flags &= ~IFF_NOARP;
  232. } else if (strcmp(*argv, "off") == 0) {
  233. flags |= IFF_NOARP;
  234. } else
  235. return on_off("noarp");
  236. } else if (strcmp(*argv, "addr") == 0) {
  237. NEXT_ARG();
  238. newaddr = *argv;
  239. } else {
  240. if (strcmp(*argv, "dev") == 0) {
  241. NEXT_ARG();
  242. }
  243. if (dev)
  244. duparg2("dev", *argv);
  245. dev = *argv;
  246. }
  247. argc--; argv++;
  248. }
  249. if (!dev) {
  250. bb_error_msg(bb_msg_requires_arg, "\"dev\"");
  251. exit(-1);
  252. }
  253. if (newaddr || newbrd) {
  254. halen = get_address(dev, &htype);
  255. if (halen < 0)
  256. return -1;
  257. if (newaddr) {
  258. if (parse_address(dev, htype, halen, newaddr, &ifr0) < 0)
  259. return -1;
  260. }
  261. if (newbrd) {
  262. if (parse_address(dev, htype, halen, newbrd, &ifr1) < 0)
  263. return -1;
  264. }
  265. }
  266. if (newname && strcmp(dev, newname)) {
  267. if (do_changename(dev, newname) < 0)
  268. return -1;
  269. dev = newname;
  270. }
  271. if (qlen != -1) {
  272. if (set_qlen(dev, qlen) < 0)
  273. return -1;
  274. }
  275. if (mtu != -1) {
  276. if (set_mtu(dev, mtu) < 0)
  277. return -1;
  278. }
  279. if (newaddr || newbrd) {
  280. if (newbrd) {
  281. if (set_address(&ifr1, 1) < 0)
  282. return -1;
  283. }
  284. if (newaddr) {
  285. if (set_address(&ifr0, 0) < 0)
  286. return -1;
  287. }
  288. }
  289. if (mask)
  290. return do_chflags(dev, flags, mask);
  291. return 0;
  292. }
  293. static int ipaddr_list_link(int argc, char **argv)
  294. {
  295. preferred_family = AF_PACKET;
  296. return ipaddr_list_or_flush(argc, argv, 0);
  297. }
  298. int do_iplink(int argc, char **argv)
  299. {
  300. if (argc > 0) {
  301. if (matches(*argv, "set") == 0)
  302. return do_set(argc-1, argv+1);
  303. if (matches(*argv, "show") == 0 ||
  304. matches(*argv, "lst") == 0 ||
  305. matches(*argv, "list") == 0)
  306. return ipaddr_list_link(argc-1, argv+1);
  307. } else
  308. return ipaddr_list_link(0, NULL);
  309. bb_error_msg("command \"%s\" is unknown", *argv);
  310. exit(-1);
  311. }