ll_map.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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[16];
  27. static struct idxmap *find_by_index(int idx)
  28. {
  29. struct idxmap *im;
  30. for (im = idxmap[idx & 0xF]; im; im = im->next)
  31. if (im->index == idx)
  32. return im;
  33. return NULL;
  34. }
  35. int ll_remember_index(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
  36. struct nlmsghdr *n,
  37. void *arg ATTRIBUTE_UNUSED)
  38. {
  39. int h;
  40. struct ifinfomsg *ifi = NLMSG_DATA(n);
  41. struct idxmap *im, **imp;
  42. struct rtattr *tb[IFLA_MAX+1];
  43. if (n->nlmsg_type != RTM_NEWLINK)
  44. return 0;
  45. if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
  46. return -1;
  47. memset(tb, 0, sizeof(tb));
  48. parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
  49. if (tb[IFLA_IFNAME] == NULL)
  50. return 0;
  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 > 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 *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 *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 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 xll_name_to_index(const char *const 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. for (i = 0; i < 16; i++) {
  130. for (im = idxmap[i]; im; im = im->next) {
  131. if (strcmp(im->name, name) == 0) {
  132. icache = im->index;
  133. strcpy(ncache, name);
  134. ret = im->index;
  135. goto out;
  136. }
  137. }
  138. }
  139. /* We have not found the interface in our cache, but the kernel
  140. * may still know about it. One reason is that we may be using
  141. * module on-demand loading, which means that the kernel will
  142. * load the module and make the interface exist only when
  143. * we explicitely request it (check for dev_load() in net/core/dev.c).
  144. * I can think of other similar scenario, but they are less common...
  145. * Jean II */
  146. #endif
  147. sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  148. if (sock_fd) {
  149. struct ifreq ifr;
  150. int tmp;
  151. strncpy(ifr.ifr_name, name, IFNAMSIZ);
  152. ifr.ifr_ifindex = -1;
  153. tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr);
  154. close(sock_fd);
  155. if (tmp >= 0)
  156. /* In theory, we should redump the interface list
  157. * to update our cache, this is left as an exercise
  158. * to the reader... Jean II */
  159. ret = ifr.ifr_ifindex;
  160. }
  161. /* out:*/
  162. if (ret <= 0)
  163. bb_error_msg_and_die("cannot find device \"%s\"", name);
  164. return ret;
  165. }
  166. int ll_init_map(struct rtnl_handle *rth)
  167. {
  168. xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK);
  169. xrtnl_dump_filter(rth, ll_remember_index, &idxmap);
  170. return 0;
  171. }