| 1 |
/* iftable - table of network interfaces |
| 2 |
* |
| 3 |
* (C) 2004 by Astaro AG, written by Harald Welte <[email protected]> |
| 4 |
* (C) 2008 by Pablo Neira Ayuso <[email protected]> |
| 5 |
* |
| 6 |
* This software is Free Software and licensed under GNU GPLv2+. |
| 7 |
*/ |
| 8 |
|
| 9 |
/* IFINDEX handling */ |
| 10 |
|
| 11 |
#include <unistd.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <string.h> |
| 15 |
#include <sys/types.h> |
| 16 |
#include <netinet/in.h> |
| 17 |
#include <arpa/inet.h> |
| 18 |
#include <errno.h> |
| 19 |
#include <assert.h> |
| 20 |
|
| 21 |
#include <linux/netdevice.h> |
| 22 |
|
| 23 |
#include <libnfnetlink/libnfnetlink.h> |
| 24 |
#include "rtnl.h" |
| 25 |
#include "linux_list.h" |
| 26 |
|
| 27 |
/** |
| 28 |
* \defgroup iftable Functions in iftable.c [DEPRECATED] |
| 29 |
* This documentation is provided for the benefit of maintainers of legacy code. |
| 30 |
* |
| 31 |
* New applications should use |
| 32 |
* [libmnl](https://netfilter.org/projects/libmnl/doxygen/html/). |
| 33 |
* @{ |
| 34 |
*/ |
| 35 |
|
| 36 |
struct ifindex_node { |
| 37 |
struct list_head head; |
| 38 |
|
| 39 |
uint32_t index; |
| 40 |
uint32_t type; |
| 41 |
uint32_t alen; |
| 42 |
uint32_t flags; |
| 43 |
char addr[8]; |
| 44 |
char name[16]; |
| 45 |
}; |
| 46 |
|
| 47 |
struct nlif_handle { |
| 48 |
struct list_head ifindex_hash[16]; |
| 49 |
struct rtnl_handle *rtnl_handle; |
| 50 |
struct rtnl_handler ifadd_handler; |
| 51 |
struct rtnl_handler ifdel_handler; |
| 52 |
}; |
| 53 |
|
| 54 |
/* iftable_add - Add/Update an entry to/in the interface table |
| 55 |
* @n: netlink message header of a RTM_NEWLINK message |
| 56 |
* @arg: not used |
| 57 |
* |
| 58 |
* This function adds/updates an entry in the intrface table. |
| 59 |
* Returns -1 on error, 1 on success. |
| 60 |
*/ |
| 61 |
static int iftable_add(struct nlmsghdr *n, void *arg) |
| 62 |
{ |
| 63 |
unsigned int hash, found = 0; |
| 64 |
struct ifinfomsg *ifi_msg = NLMSG_DATA(n); |
| 65 |
struct ifindex_node *this; |
| 66 |
struct rtattr *cb[IFLA_MAX+1]; |
| 67 |
struct nlif_handle *h = arg; |
| 68 |
|
| 69 |
if (n->nlmsg_type != RTM_NEWLINK) |
| 70 |
return -1; |
| 71 |
|
| 72 |
if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi_msg))) |
| 73 |
return -1; |
| 74 |
|
| 75 |
rtnl_parse_rtattr(cb, IFLA_MAX, IFLA_RTA(ifi_msg), IFLA_PAYLOAD(n)); |
| 76 |
|
| 77 |
if (!cb[IFLA_IFNAME]) |
| 78 |
return -1; |
| 79 |
|
| 80 |
hash = ifi_msg->ifi_index & 0xF; |
| 81 |
list_for_each_entry(this, &h->ifindex_hash[hash], head) { |
| 82 |
if (this->index == ifi_msg->ifi_index) { |
| 83 |
found = 1; |
| 84 |
break; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (!found) { |
| 89 |
this = malloc(sizeof(*this)); |
| 90 |
if (!this) |
| 91 |
return -1; |
| 92 |
|
| 93 |
this->index = ifi_msg->ifi_index; |
| 94 |
} |
| 95 |
|
| 96 |
this->type = ifi_msg->ifi_type; |
| 97 |
this->flags = ifi_msg->ifi_flags; |
| 98 |
if (cb[IFLA_ADDRESS]) { |
| 99 |
unsigned int alen; |
| 100 |
this->alen = alen = RTA_PAYLOAD(cb[IFLA_ADDRESS]); |
| 101 |
if (alen > sizeof(this->addr)) |
| 102 |
alen = sizeof(this->addr); |
| 103 |
memcpy(this->addr, RTA_DATA(cb[IFLA_ADDRESS]), alen); |
| 104 |
} else { |
| 105 |
this->alen = 0; |
| 106 |
memset(this->addr, 0, sizeof(this->addr)); |
| 107 |
} |
| 108 |
strcpy(this->name, RTA_DATA(cb[IFLA_IFNAME])); |
| 109 |
|
| 110 |
if (!found) |
| 111 |
list_add(&this->head, &h->ifindex_hash[hash]); |
| 112 |
|
| 113 |
return 1; |
| 114 |
} |
| 115 |
|
| 116 |
/* iftable_del - Delete an entry from the interface table |
| 117 |
* @n: netlink message header of a RTM_DELLINK nlmsg |
| 118 |
* @arg: not used |
| 119 |
* |
| 120 |
* Delete an entry from the interface table. |
| 121 |
* Returns -1 on error, 0 if no matching entry was found or 1 on success. |
| 122 |
*/ |
| 123 |
static int iftable_del(struct nlmsghdr *n, void *arg) |
| 124 |
{ |
| 125 |
struct ifinfomsg *ifi_msg = NLMSG_DATA(n); |
| 126 |
struct rtattr *cb[IFLA_MAX+1]; |
| 127 |
struct nlif_handle *h = arg; |
| 128 |
struct ifindex_node *this, *tmp; |
| 129 |
unsigned int hash; |
| 130 |
|
| 131 |
if (n->nlmsg_type != RTM_DELLINK) |
| 132 |
return -1; |
| 133 |
|
| 134 |
if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi_msg))) |
| 135 |
return -1; |
| 136 |
|
| 137 |
rtnl_parse_rtattr(cb, IFLA_MAX, IFLA_RTA(ifi_msg), IFLA_PAYLOAD(n)); |
| 138 |
|
| 139 |
hash = ifi_msg->ifi_index & 0xF; |
| 140 |
list_for_each_entry_safe(this, tmp, &h->ifindex_hash[hash], head) { |
| 141 |
if (this->index == ifi_msg->ifi_index) { |
| 142 |
list_del(&this->head); |
| 143 |
free(this); |
| 144 |
return 1; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
/** Get the name for an ifindex |
| 152 |
* |
| 153 |
* \param nlif_handle A pointer to a ::nlif_handle created |
| 154 |
* \param index ifindex to be resolved |
| 155 |
* \param name interface name, pass a buffer of IFNAMSIZ size |
| 156 |
* \return -1 on error, 1 on success |
| 157 |
*/ |
| 158 |
int nlif_index2name(struct nlif_handle *h, |
| 159 |
unsigned int index, |
| 160 |
char *name) |
| 161 |
{ |
| 162 |
unsigned int hash; |
| 163 |
struct ifindex_node *this; |
| 164 |
|
| 165 |
assert(h != NULL); |
| 166 |
assert(name != NULL); |
| 167 |
|
| 168 |
if (index == 0) { |
| 169 |
strcpy(name, "*"); |
| 170 |
return 1; |
| 171 |
} |
| 172 |
|
| 173 |
hash = index & 0xF; |
| 174 |
list_for_each_entry(this, &h->ifindex_hash[hash], head) { |
| 175 |
if (this->index == index) { |
| 176 |
strcpy(name, this->name); |
| 177 |
return 1; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
errno = ENOENT; |
| 182 |
return -1; |
| 183 |
} |
| 184 |
|
| 185 |
/** Get the flags for an ifindex |
| 186 |
* |
| 187 |
* \param nlif_handle A pointer to a ::nlif_handle created |
| 188 |
* \param index ifindex to be resolved |
| 189 |
* \param flags pointer to variable used to store the interface flags |
| 190 |
* \return -1 on error, 1 on success |
| 191 |
*/ |
| 192 |
int nlif_get_ifflags(const struct nlif_handle *h, |
| 193 |
unsigned int index, |
| 194 |
unsigned int *flags) |
| 195 |
{ |
| 196 |
unsigned int hash; |
| 197 |
struct ifindex_node *this; |
| 198 |
|
| 199 |
assert(h != NULL); |
| 200 |
assert(flags != NULL); |
| 201 |
|
| 202 |
if (index == 0) { |
| 203 |
errno = ENOENT; |
| 204 |
return -1; |
| 205 |
} |
| 206 |
|
| 207 |
hash = index & 0xF; |
| 208 |
list_for_each_entry(this, &h->ifindex_hash[hash], head) { |
| 209 |
if (this->index == index) { |
| 210 |
*flags = this->flags; |
| 211 |
return 1; |
| 212 |
} |
| 213 |
} |
| 214 |
errno = ENOENT; |
| 215 |
return -1; |
| 216 |
} |
| 217 |
|
| 218 |
/** Initialize interface table |
| 219 |
* |
| 220 |
* Initialize rtnl interface and interface table |
| 221 |
* Call this before any nlif_* function |
| 222 |
* |
| 223 |
* \return file descriptor to netlink socket |
| 224 |
*/ |
| 225 |
struct nlif_handle *nlif_open(void) |
| 226 |
{ |
| 227 |
int i; |
| 228 |
struct nlif_handle *h; |
| 229 |
|
| 230 |
h = calloc(1, sizeof(struct nlif_handle)); |
| 231 |
if (h == NULL) |
| 232 |
goto err; |
| 233 |
|
| 234 |
for (i=0; i<16; i++) |
| 235 |
INIT_LIST_HEAD(&h->ifindex_hash[i]); |
| 236 |
|
| 237 |
h->ifadd_handler.nlmsg_type = RTM_NEWLINK; |
| 238 |
h->ifadd_handler.handlefn = iftable_add; |
| 239 |
h->ifadd_handler.arg = h; |
| 240 |
h->ifdel_handler.nlmsg_type = RTM_DELLINK; |
| 241 |
h->ifdel_handler.handlefn = iftable_del; |
| 242 |
h->ifdel_handler.arg = h; |
| 243 |
|
| 244 |
h->rtnl_handle = rtnl_open(); |
| 245 |
if (h->rtnl_handle == NULL) |
| 246 |
goto err; |
| 247 |
|
| 248 |
if (rtnl_handler_register(h->rtnl_handle, &h->ifadd_handler) < 0) |
| 249 |
goto err_close; |
| 250 |
|
| 251 |
if (rtnl_handler_register(h->rtnl_handle, &h->ifdel_handler) < 0) |
| 252 |
goto err_unregister; |
| 253 |
|
| 254 |
return h; |
| 255 |
|
| 256 |
err_unregister: |
| 257 |
rtnl_handler_unregister(h->rtnl_handle, &h->ifadd_handler); |
| 258 |
err_close: |
| 259 |
rtnl_close(h->rtnl_handle); |
| 260 |
free(h); |
| 261 |
err: |
| 262 |
return NULL; |
| 263 |
} |
| 264 |
|
| 265 |
/** Destructor of interface table |
| 266 |
* |
| 267 |
* \param nlif_handle A pointer to a ::nlif_handle created |
| 268 |
* via nlif_open() |
| 269 |
*/ |
| 270 |
void nlif_close(struct nlif_handle *h) |
| 271 |
{ |
| 272 |
int i; |
| 273 |
struct ifindex_node *this, *tmp; |
| 274 |
|
| 275 |
assert(h != NULL); |
| 276 |
|
| 277 |
rtnl_handler_unregister(h->rtnl_handle, &h->ifadd_handler); |
| 278 |
rtnl_handler_unregister(h->rtnl_handle, &h->ifdel_handler); |
| 279 |
rtnl_close(h->rtnl_handle); |
| 280 |
|
| 281 |
for (i=0; i<16; i++) { |
| 282 |
list_for_each_entry_safe(this, tmp, &h->ifindex_hash[i], head) { |
| 283 |
list_del(&this->head); |
| 284 |
free(this); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
free(h); |
| 289 |
h = NULL; /* bugtrap */ |
| 290 |
} |
| 291 |
|
| 292 |
/** Receive message from netlink and update interface table |
| 293 |
* |
| 294 |
* \param nlif_handle A pointer to a ::nlif_handle created |
| 295 |
* \return 0 if OK |
| 296 |
*/ |
| 297 |
int nlif_catch(struct nlif_handle *h) |
| 298 |
{ |
| 299 |
assert(h != NULL); |
| 300 |
|
| 301 |
if (h->rtnl_handle) |
| 302 |
return rtnl_receive(h->rtnl_handle); |
| 303 |
|
| 304 |
return -1; |
| 305 |
} |
| 306 |
|
| 307 |
static int nlif_catch_multi(struct nlif_handle *h) |
| 308 |
{ |
| 309 |
assert(h != NULL); |
| 310 |
|
| 311 |
if (h->rtnl_handle) |
| 312 |
return rtnl_receive_multi(h->rtnl_handle); |
| 313 |
|
| 314 |
return -1; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* nlif_query - request a dump of interfaces available in the system |
| 319 |
* @h: pointer to a valid nlif_handler |
| 320 |
*/ |
| 321 |
int nlif_query(struct nlif_handle *h) |
| 322 |
{ |
| 323 |
assert(h != NULL); |
| 324 |
|
| 325 |
if (rtnl_dump_type(h->rtnl_handle, RTM_GETLINK) < 0) |
| 326 |
return -1; |
| 327 |
|
| 328 |
return nlif_catch_multi(h); |
| 329 |
} |
| 330 |
|
| 331 |
/** Returns socket descriptor for the netlink socket |
| 332 |
* |
| 333 |
* \param nlif_handle A pointer to a ::nlif_handle created |
| 334 |
* \return The fd or -1 if there's an error |
| 335 |
*/ |
| 336 |
int nlif_fd(struct nlif_handle *h) |
| 337 |
{ |
| 338 |
assert(h != NULL); |
| 339 |
|
| 340 |
if (h->rtnl_handle) |
| 341 |
return h->rtnl_handle->rtnl_fd; |
| 342 |
|
| 343 |
return -1; |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* @} |
| 348 |
*/ |