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