3
0

ll_map.c 4.3 KB

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