"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame^] | 14 | //#include <sys/socket.h> /* socket() */ |
| 15 | #include <net/if.h> /* struct ifreq and co. */ |
| 16 | //#include <sys/ioctl.h> /* ioctl() & SIOCGIFINDEX */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 17 | |
Denis Vlasenko | 9a7d38f | 2007-05-31 22:42:12 +0000 | [diff] [blame^] | 18 | #include "libbb.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 19 | #include "libnetlink.h" |
Bernhard Reutner-Fischer | 1d62d3b | 2005-10-08 20:47:15 +0000 | [diff] [blame] | 20 | #include "ll_map.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | 7935a5a | 2006-09-30 00:18:16 +0000 | [diff] [blame] | 22 | struct idxmap { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 23 | struct idxmap * next; |
| 24 | int index; |
| 25 | int type; |
| 26 | int alen; |
| 27 | unsigned flags; |
| 28 | unsigned char addr[8]; |
| 29 | char name[16]; |
| 30 | }; |
| 31 | |
| 32 | static struct idxmap *idxmap[16]; |
| 33 | |
| 34 | int ll_remember_index(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
| 35 | { |
| 36 | int h; |
| 37 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
| 38 | struct idxmap *im, **imp; |
| 39 | struct rtattr *tb[IFLA_MAX+1]; |
| 40 | |
| 41 | if (n->nlmsg_type != RTM_NEWLINK) |
| 42 | return 0; |
| 43 | |
| 44 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi))) |
| 45 | return -1; |
| 46 | |
| 47 | |
| 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 | |
| 53 | h = ifi->ifi_index&0xF; |
| 54 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 55 | for (imp = &idxmap[h]; (im = *imp) != NULL; imp = &im->next) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 56 | if (im->index == ifi->ifi_index) |
| 57 | break; |
| 58 | |
| 59 | if (im == NULL) { |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 60 | im = xmalloc(sizeof(*im)); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 61 | im->next = *imp; |
| 62 | im->index = ifi->ifi_index; |
| 63 | *imp = im; |
| 64 | } |
| 65 | |
| 66 | im->type = ifi->ifi_type; |
| 67 | im->flags = ifi->ifi_flags; |
| 68 | if (tb[IFLA_ADDRESS]) { |
| 69 | int alen; |
| 70 | im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]); |
| 71 | if (alen > sizeof(im->addr)) |
| 72 | alen = sizeof(im->addr); |
| 73 | memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen); |
| 74 | } else { |
| 75 | im->alen = 0; |
| 76 | memset(im->addr, 0, sizeof(im->addr)); |
| 77 | } |
| 78 | strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME])); |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | const char *ll_idx_n2a(int idx, char *buf) |
| 83 | { |
| 84 | struct idxmap *im; |
| 85 | |
| 86 | if (idx == 0) |
| 87 | return "*"; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 88 | for (im = idxmap[idx & 0xF]; im; im = im->next) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 89 | if (im->index == idx) |
| 90 | return im->name; |
| 91 | snprintf(buf, 16, "if%d", idx); |
| 92 | return buf; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | const char *ll_index_to_name(int idx) |
| 97 | { |
| 98 | static char nbuf[16]; |
| 99 | |
| 100 | return ll_idx_n2a(idx, nbuf); |
| 101 | } |
| 102 | |
| 103 | int ll_index_to_type(int idx) |
| 104 | { |
| 105 | struct idxmap *im; |
| 106 | |
| 107 | if (idx == 0) |
| 108 | return -1; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 109 | for (im = idxmap[idx & 0xF]; im; im = im->next) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 110 | if (im->index == idx) |
| 111 | return im->type; |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | unsigned ll_index_to_flags(int idx) |
| 116 | { |
| 117 | struct idxmap *im; |
| 118 | |
| 119 | if (idx == 0) |
| 120 | return 0; |
| 121 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 122 | for (im = idxmap[idx & 0xF]; im; im = im->next) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 123 | if (im->index == idx) |
| 124 | return im->flags; |
| 125 | return 0; |
| 126 | } |
| 127 | |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 128 | // TODO: caching is not warranted - no users which repeatedly call it |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 129 | int xll_name_to_index(const char * const name) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 130 | { |
| 131 | static char ncache[16]; |
| 132 | static int icache; |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 133 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 134 | struct idxmap *im; |
Denis Vlasenko | 7935a5a | 2006-09-30 00:18:16 +0000 | [diff] [blame] | 135 | int sock_fd; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 136 | int i; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 137 | int ret = 0; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 138 | |
| 139 | if (name == NULL) |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 140 | goto out; |
| 141 | if (icache && strcmp(name, ncache) == 0) { |
| 142 | ret = icache; |
| 143 | goto out; |
| 144 | } |
Denis Vlasenko | 540a2a1 | 2007-04-07 01:14:45 +0000 | [diff] [blame] | 145 | for (i = 0; i < 16; i++) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 146 | for (im = idxmap[i]; im; im = im->next) { |
| 147 | if (strcmp(im->name, name) == 0) { |
| 148 | icache = im->index; |
| 149 | strcpy(ncache, name); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 150 | ret = im->index; |
| 151 | goto out; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
Denis Vlasenko | 7935a5a | 2006-09-30 00:18:16 +0000 | [diff] [blame] | 155 | /* We have not found the interface in our cache, but the kernel |
| 156 | * may still know about it. One reason is that we may be using |
| 157 | * module on-demand loading, which means that the kernel will |
| 158 | * load the module and make the interface exist only when |
| 159 | * we explicitely request it (check for dev_load() in net/core/dev.c). |
| 160 | * I can think of other similar scenario, but they are less common... |
| 161 | * Jean II */ |
| 162 | sock_fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 163 | if (sock_fd) { |
| 164 | struct ifreq ifr; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 165 | int tmp; |
Denis Vlasenko | 7935a5a | 2006-09-30 00:18:16 +0000 | [diff] [blame] | 166 | strncpy(ifr.ifr_name, name, IFNAMSIZ); |
| 167 | ifr.ifr_ifindex = -1; |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 168 | tmp = ioctl(sock_fd, SIOCGIFINDEX, &ifr); |
Denis Vlasenko | 7935a5a | 2006-09-30 00:18:16 +0000 | [diff] [blame] | 169 | close(sock_fd); |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 170 | if (tmp >= 0) |
Denis Vlasenko | 7935a5a | 2006-09-30 00:18:16 +0000 | [diff] [blame] | 171 | /* In theory, we should redump the interface list |
| 172 | * to update our cache, this is left as an exercise |
| 173 | * to the reader... Jean II */ |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 174 | ret = ifr.ifr_ifindex; |
Denis Vlasenko | 7935a5a | 2006-09-30 00:18:16 +0000 | [diff] [blame] | 175 | } |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 176 | out: |
| 177 | if (ret <= 0) |
| 178 | bb_error_msg_and_die("cannot find device \"%s\"", name); |
| 179 | return ret; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | int ll_init_map(struct rtnl_handle *rth) |
| 183 | { |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 184 | xrtnl_wilddump_request(rth, AF_UNSPEC, RTM_GETLINK); |
| 185 | xrtnl_dump_filter(rth, ll_remember_index, &idxmap); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 186 | return 0; |
| 187 | } |