ll_map.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License
  5. * as published by the Free Software Foundation; either version
  6. * 2 of the License, or (at your option) any later version.
  7. *
  8. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. */
  10. #include <net/if.h> /* struct ifreq and co. */
  11. #include "libbb.h"
  12. #include "libnetlink.h"
  13. #include "ll_map.h"
  14. struct idxmap {
  15. struct idxmap *next;
  16. int index;
  17. int type;
  18. int alen;
  19. unsigned flags;
  20. unsigned char addr[8];
  21. char name[16];
  22. };
  23. static struct idxmap **idxmap; /* treat as *idxmap[16] */
  24. static struct idxmap *find_by_index(int idx)
  25. {
  26. struct idxmap *im;
  27. if (idxmap)
  28. for (im = idxmap[idx & 0xF]; im; im = im->next)
  29. if (im->index == idx)
  30. return im;
  31. return NULL;
  32. }
  33. int FAST_FUNC ll_remember_index(const struct sockaddr_nl *who UNUSED_PARAM,
  34. struct nlmsghdr *n,
  35. void *arg UNUSED_PARAM)
  36. {
  37. int h;
  38. struct ifinfomsg *ifi = NLMSG_DATA(n);
  39. struct idxmap *im, **imp;
  40. struct rtattr *tb[IFLA_MAX+1];
  41. if (n->nlmsg_type != RTM_NEWLINK)
  42. return 0;
  43. if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
  44. return -1;
  45. memset(tb, 0, sizeof(tb));
  46. parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
  47. if (tb[IFLA_IFNAME] == NULL)
  48. return 0;
  49. if (!idxmap)
  50. idxmap = xzalloc(sizeof(idxmap[0]) * 16);
  51. h = ifi->ifi_index & 0xF;
  52. for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next)
  53. if (im->index == ifi->ifi_index)
  54. goto found;
  55. im = xmalloc(sizeof(*im));
  56. im->next = *imp;
  57. im->index = ifi->ifi_index;
  58. *imp = im;
  59. found:
  60. im->type = ifi->ifi_type;
  61. im->flags = ifi->ifi_flags;
  62. if (tb[IFLA_ADDRESS]) {
  63. int alen;
  64. im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
  65. if (alen > (int)sizeof(im->addr))
  66. alen = sizeof(im->addr);
  67. memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
  68. } else {
  69. im->alen = 0;
  70. memset(im->addr, 0, sizeof(im->addr));
  71. }
  72. strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
  73. return 0;
  74. }
  75. const char FAST_FUNC *ll_idx_n2a(int idx, char *buf)
  76. {
  77. struct idxmap *im;
  78. if (idx == 0)
  79. return "*";
  80. im = find_by_index(idx);
  81. if (im)
  82. return im->name;
  83. snprintf(buf, 16, "if%d", idx);
  84. return buf;
  85. }
  86. const char FAST_FUNC *ll_index_to_name(int idx)
  87. {
  88. static char nbuf[16];
  89. return ll_idx_n2a(idx, nbuf);
  90. }
  91. #ifdef UNUSED
  92. int ll_index_to_type(int idx)
  93. {
  94. struct idxmap *im;
  95. if (idx == 0)
  96. return -1;
  97. im = find_by_index(idx);
  98. if (im)
  99. return im->type;
  100. return -1;
  101. }
  102. #endif
  103. unsigned FAST_FUNC ll_index_to_flags(int idx)
  104. {
  105. struct idxmap *im;
  106. if (idx == 0)
  107. return 0;
  108. im = find_by_index(idx);
  109. if (im)
  110. return im->flags;
  111. return 0;
  112. }
  113. int FAST_FUNC xll_name_to_index(const char *name)
  114. {
  115. int ret = 0;
  116. int sock_fd;
  117. /* caching is not warranted - no users which repeatedly call it */
  118. #ifdef UNUSED
  119. static char ncache[16];
  120. static int icache;
  121. struct idxmap *im;
  122. int i;
  123. if (name == NULL)
  124. goto out;
  125. if (icache && strcmp(name, ncache) == 0) {
  126. ret = icache;
  127. goto out;
  128. }
  129. if (idxmap) {
  130. for (i = 0; i < 16; i++) {
  131. for (im = idxmap[i]; im; im = im->next) {
  132. if (strcmp(im->name, name) == 0) {
  133. icache = im->index;
  134. strcpy(ncache, name);
  135. ret = im->index;
  136. goto out;
  137. }
  138. }
  139. }
  140. }
  141. /* We have not found the interface in our cache, but the kernel
  142. * may still know about it. One reason is that we may be using
  143. * module on-demand loading, which means that the kernel will
  144. * load the module and make the interface exist only when
  145. * we explicitely request it (check for dev_load() in net/core/dev.c).
  146. * I can think of other similar scenario, but they are less common...
  147. * Jean II */
  148. #endif
  149. sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  150. if (sock_fd >= 0) {
  151. struct ifreq ifr;
  152. int tmp;
  153. strncpy_IFNAMSIZ(ifr.ifr_name, name);
  154. ifr.ifr_ifindex = -1;
  155. tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
  156. close(sock_fd);
  157. if (tmp >= 0)
  158. /* In theory, we should redump the interface list
  159. * to update our cache, this is left as an exercise
  160. * to the reader... Jean II */
  161. ret = ifr.ifr_ifindex;
  162. }
  163. /* out:*/
  164. if (ret <= 0)
  165. bb_error_msg_and_die("can't find device '%s'", name);
  166. return ret;
  167. }
  168. int FAST_FUNC ll_init_map(struct rtnl_handle *rth)
  169. {
  170. xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
  171. xrtnl_dump_filter(rth, ll_remember_index, NULL);
  172. return 0;
  173. }