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. static
  76. const char FAST_FUNC *ll_idx_n2a(int idx/*, char *buf*/)
  77. {
  78. struct idxmap *im;
  79. if (idx == 0)
  80. return "*";
  81. im = find_by_index(idx);
  82. if (im)
  83. return im->name;
  84. //snprintf(buf, 16, "if%d", idx);
  85. //return buf;
  86. return auto_string(xasprintf("if%d", idx));
  87. }
  88. const char FAST_FUNC *ll_index_to_name(int idx)
  89. {
  90. //static char nbuf[16];
  91. return ll_idx_n2a(idx/*, nbuf*/);
  92. }
  93. #ifdef UNUSED
  94. int ll_index_to_type(int idx)
  95. {
  96. struct idxmap *im;
  97. if (idx == 0)
  98. return -1;
  99. im = find_by_index(idx);
  100. if (im)
  101. return im->type;
  102. return -1;
  103. }
  104. #endif
  105. unsigned FAST_FUNC ll_index_to_flags(int idx)
  106. {
  107. struct idxmap *im;
  108. if (idx == 0)
  109. return 0;
  110. im = find_by_index(idx);
  111. if (im)
  112. return im->flags;
  113. return 0;
  114. }
  115. int FAST_FUNC xll_name_to_index(const char *name)
  116. {
  117. int ret = 0;
  118. int sock_fd;
  119. /* caching is not warranted - no users which repeatedly call it */
  120. #ifdef UNUSED
  121. static char ncache[16];
  122. static int icache;
  123. struct idxmap *im;
  124. int i;
  125. if (name == NULL)
  126. goto out;
  127. if (icache && strcmp(name, ncache) == 0) {
  128. ret = icache;
  129. goto out;
  130. }
  131. if (idxmap) {
  132. for (i = 0; i < 16; i++) {
  133. for (im = idxmap[i]; im; im = im->next) {
  134. if (strcmp(im->name, name) == 0) {
  135. icache = im->index;
  136. strcpy(ncache, name);
  137. ret = im->index;
  138. goto out;
  139. }
  140. }
  141. }
  142. }
  143. /* We have not found the interface in our cache, but the kernel
  144. * may still know about it. One reason is that we may be using
  145. * module on-demand loading, which means that the kernel will
  146. * load the module and make the interface exist only when
  147. * we explicitely request it (check for dev_load() in net/core/dev.c).
  148. * I can think of other similar scenario, but they are less common...
  149. * Jean II */
  150. #endif
  151. sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  152. if (sock_fd >= 0) {
  153. struct ifreq ifr;
  154. int tmp;
  155. strncpy_IFNAMSIZ(ifr.ifr_name, name);
  156. ifr.ifr_ifindex = -1;
  157. tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
  158. close(sock_fd);
  159. if (tmp >= 0)
  160. /* In theory, we should redump the interface list
  161. * to update our cache, this is left as an exercise
  162. * to the reader... Jean II */
  163. ret = ifr.ifr_ifindex;
  164. }
  165. /* out:*/
  166. if (ret <= 0)
  167. bb_error_msg_and_die("can't find device '%s'", name);
  168. return ret;
  169. }
  170. int FAST_FUNC ll_init_map(struct rtnl_handle *rth)
  171. {
  172. xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
  173. xrtnl_dump_filter(rth, ll_remember_index, NULL);
  174. return 0;
  175. }