| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2015 Pablo Neira Ayuso <pablo@netfilter.org> | ||
| 3 | * | ||
| 4 | * This program is free software; you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License version 2 (or any | ||
| 6 | * later) as published by the Free Software Foundation. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include <nft.h> | ||
| 10 | |||
| 11 | #include <stdio.h> | ||
| 12 | #include <net/if.h> | ||
| 13 | #include <time.h> | ||
| 14 | #include <errno.h> | ||
| 15 | |||
| 16 | #include <libmnl/libmnl.h> | ||
| 17 | #include <linux/rtnetlink.h> | ||
| 18 | |||
| 19 | #include <nftables.h> | ||
| 20 | #include <list.h> | ||
| 21 | #include <netlink.h> | ||
| 22 | #include <iface.h> | ||
| 23 | |||
| 24 | static LIST_HEAD(iface_list); | ||
| 25 | static bool iface_cache_init; | ||
| 26 | |||
| 27 | 38658 | static int data_attr_cb(const struct nlattr *attr, void *data) | |
| 28 | { | ||
| 29 | 38658 | const struct nlattr **tb = data; | |
| 30 | 38658 | int type = mnl_attr_get_type(attr); | |
| 31 | |||
| 32 |
2/2✓ Branch 0 (4→5) taken 2024 times.
✓ Branch 1 (4→6) taken 36634 times.
|
38658 | if (mnl_attr_type_valid(attr, IFLA_MAX) < 0) |
| 33 | 2024 | return MNL_CB_OK; | |
| 34 | |||
| 35 |
2/2✓ Branch 0 (6→7) taken 1012 times.
✓ Branch 1 (6→12) taken 35622 times.
|
36634 | switch(type) { |
| 36 | 1012 | case IFLA_IFNAME: | |
| 37 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→11) taken 1012 times.
|
1012 | if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) |
| 38 | ✗ | netlink_abi_error(); | |
| 39 | 1012 | break; | |
| 40 | 35622 | default: | |
| 41 | 35622 | return MNL_CB_OK; | |
| 42 | } | ||
| 43 | 1012 | tb[type] = attr; | |
| 44 | 1012 | return MNL_CB_OK; | |
| 45 | } | ||
| 46 | |||
| 47 | 1012 | static int data_cb(const struct nlmsghdr *nlh, void *data) | |
| 48 | { | ||
| 49 | 1012 | struct nlattr *tb[IFLA_MAX + 1] = {}; | |
| 50 | 1012 | struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh); | |
| 51 | struct iface *iface; | ||
| 52 | |||
| 53 | 1012 | iface = xmalloc(sizeof(struct iface)); | |
| 54 | 1012 | iface->ifindex = ifm->ifi_index; | |
| 55 | 1012 | mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, tb); | |
| 56 | 1012 | snprintf(iface->name, IFNAMSIZ, "%s", mnl_attr_get_str(tb[IFLA_IFNAME])); | |
| 57 | 1012 | list_add(&iface->list, &iface_list); | |
| 58 | |||
| 59 | 1012 | return MNL_CB_OK; | |
| 60 | } | ||
| 61 | |||
| 62 | 810 | static int iface_mnl_talk(struct mnl_socket *nl, uint32_t portid) | |
| 63 | 810 | { | |
| 64 |
1/2✓ Branch 0 (3→4) taken 810 times.
✗ Branch 1 (3→5) not taken.
|
810 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 65 | struct nlmsghdr *nlh; | ||
| 66 | struct rtgenmsg *rt; | ||
| 67 | 810 | bool eintr = false; | |
| 68 | uint32_t seq; | ||
| 69 | int ret; | ||
| 70 | |||
| 71 | 810 | nlh = mnl_nlmsg_put_header(buf); | |
| 72 | 810 | nlh->nlmsg_type = RTM_GETLINK; | |
| 73 | 810 | nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; | |
| 74 | 810 | nlh->nlmsg_seq = seq = time(NULL); | |
| 75 | 810 | rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg)); | |
| 76 | 810 | rt->rtgen_family = AF_PACKET; | |
| 77 | |||
| 78 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 810 times.
|
810 | if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) |
| 79 | ✗ | return -1; | |
| 80 | |||
| 81 | 810 | ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); | |
| 82 |
1/2✓ Branch 0 (23→14) taken 1720 times.
✗ Branch 1 (23→24) not taken.
|
1720 | while (ret > 0) { |
| 83 | 1720 | ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL); | |
| 84 |
2/2✓ Branch 0 (15→16) taken 810 times.
✓ Branch 1 (15→17) taken 910 times.
|
1720 | if (ret == 0) |
| 85 | 810 | break; | |
| 86 |
1/2✗ Branch 0 (17→18) not taken.
✓ Branch 1 (17→21) taken 910 times.
|
910 | if (ret < 0) { |
| 87 | ✗ | if (errno != EINTR) | |
| 88 | ✗ | return ret; | |
| 89 | |||
| 90 | /* process all pending messages before reporting EINTR */ | ||
| 91 | ✗ | eintr = true; | |
| 92 | } | ||
| 93 | 910 | ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); | |
| 94 | } | ||
| 95 | |||
| 96 |
1/2✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→26) taken 810 times.
|
810 | if (eintr) { |
| 97 | ✗ | ret = -1; | |
| 98 | ✗ | errno = EINTR; | |
| 99 | } | ||
| 100 | |||
| 101 | 810 | return ret; | |
| 102 | } | ||
| 103 | |||
| 104 | 810 | void iface_cache_update(void) | |
| 105 | { | ||
| 106 | struct mnl_socket *nl; | ||
| 107 | uint32_t portid; | ||
| 108 | int ret; | ||
| 109 | |||
| 110 | 810 | nl = mnl_socket_open(NETLINK_ROUTE); | |
| 111 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 810 times.
|
810 | if (nl == NULL) |
| 112 | ✗ | netlink_init_error(); | |
| 113 | |||
| 114 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→10) taken 810 times.
|
810 | if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) |
| 115 | ✗ | netlink_init_error(); | |
| 116 | |||
| 117 | 810 | portid = mnl_socket_get_portid(nl); | |
| 118 | |||
| 119 | do { | ||
| 120 | 810 | ret = iface_mnl_talk(nl, portid); | |
| 121 |
1/4✗ Branch 0 (12→13) not taken.
✓ Branch 1 (12→14) taken 810 times.
✗ Branch 2 (13→11) not taken.
✗ Branch 3 (13→14) not taken.
|
810 | } while (ret < 0 && errno == EINTR); |
| 122 | |||
| 123 |
1/2✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→17) taken 810 times.
|
810 | if (ret == -1) |
| 124 | ✗ | netlink_init_error(); | |
| 125 | |||
| 126 | 810 | mnl_socket_close(nl); | |
| 127 | |||
| 128 | 810 | iface_cache_init = true; | |
| 129 | 810 | } | |
| 130 | |||
| 131 | 69381 | void iface_cache_release(void) | |
| 132 | { | ||
| 133 | struct iface *iface, *next; | ||
| 134 | |||
| 135 |
2/2✓ Branch 0 (2→3) taken 68571 times.
✓ Branch 1 (2→4) taken 810 times.
|
69381 | if (!iface_cache_init) |
| 136 | 68571 | return; | |
| 137 | |||
| 138 |
2/2✓ Branch 0 (7→5) taken 1012 times.
✓ Branch 1 (7→8) taken 810 times.
|
1822 | list_for_each_entry_safe(iface, next, &iface_list, list) { |
| 139 | 1012 | list_del(&iface->list); | |
| 140 | 1012 | free(iface); | |
| 141 | } | ||
| 142 | 810 | iface_cache_init = false; | |
| 143 | } | ||
| 144 | |||
| 145 | 1332 | unsigned int nft_if_nametoindex(const char *name) | |
| 146 | { | ||
| 147 | struct iface *iface; | ||
| 148 | |||
| 149 |
2/2✓ Branch 0 (2→3) taken 458 times.
✓ Branch 1 (2→4) taken 874 times.
|
1332 | if (!iface_cache_init) |
| 150 | 458 | iface_cache_update(); | |
| 151 | |||
| 152 |
2/2✓ Branch 0 (8→5) taken 1332 times.
✓ Branch 1 (8→9) taken 25 times.
|
1357 | list_for_each_entry(iface, &iface_list, list) { |
| 153 |
2/2✓ Branch 0 (5→6) taken 1307 times.
✓ Branch 1 (5→7) taken 25 times.
|
1332 | if (strncmp(name, iface->name, IFNAMSIZ) == 0) |
| 154 | 1307 | return iface->ifindex; | |
| 155 | } | ||
| 156 | 25 | return 0; | |
| 157 | } | ||
| 158 | |||
| 159 | 439 | char *nft_if_indextoname(unsigned int ifindex, char *name) | |
| 160 | { | ||
| 161 | struct iface *iface; | ||
| 162 | |||
| 163 |
2/2✓ Branch 0 (2→3) taken 344 times.
✓ Branch 1 (2→4) taken 95 times.
|
439 | if (!iface_cache_init) |
| 164 | 344 | iface_cache_update(); | |
| 165 | |||
| 166 |
2/2✓ Branch 0 (8→5) taken 439 times.
✓ Branch 1 (8→9) taken 8 times.
|
447 | list_for_each_entry(iface, &iface_list, list) { |
| 167 |
2/2✓ Branch 0 (5→6) taken 431 times.
✓ Branch 1 (5→7) taken 8 times.
|
439 | if (iface->ifindex == ifindex) { |
| 168 | 431 | snprintf(name, IFNAMSIZ, "%s", iface->name); | |
| 169 | 431 | return name; | |
| 170 | } | ||
| 171 | } | ||
| 172 | 8 | return NULL; | |
| 173 | } | ||
| 174 | |||
| 175 | 218 | const struct iface *iface_cache_get_next_entry(const struct iface *prev) | |
| 176 | { | ||
| 177 |
2/2✓ Branch 0 (2→3) taken 8 times.
✓ Branch 1 (2→4) taken 210 times.
|
218 | if (!iface_cache_init) |
| 178 | 8 | iface_cache_update(); | |
| 179 | |||
| 180 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 218 times.
|
218 | if (list_empty(&iface_list)) |
| 181 | ✗ | return NULL; | |
| 182 | |||
| 183 |
2/2✓ Branch 0 (7→8) taken 8 times.
✓ Branch 1 (7→9) taken 210 times.
|
218 | if (!prev) |
| 184 | 8 | return list_first_entry(&iface_list, struct iface, list); | |
| 185 | |||
| 186 |
2/2✓ Branch 0 (10→11) taken 8 times.
✓ Branch 1 (10→12) taken 202 times.
|
210 | if (list_is_last(&prev->list, &iface_list)) |
| 187 | 8 | return NULL; | |
| 188 | |||
| 189 | 202 | return list_next_entry(prev, list); | |
| 190 | } | ||
| 191 |