]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
8041b5ba | 2 | /*** |
cbc06dcd | 3 | This file is part of systemd. |
8041b5ba LP |
4 | |
5 | Copyright 2008-2011 Lennart Poettering | |
d1ca51b1 | 6 | Copyright 2014 Tom Gundersen |
8041b5ba | 7 | |
cbc06dcd TG |
8 | systemd is free software; you can redistribute it and/or modify it |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
8041b5ba | 12 | |
cbc06dcd TG |
13 | systemd is distributed in the hope that it will be useful, but |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
8041b5ba LP |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | Lesser General Public License for more details. | |
17 | ||
cbc06dcd TG |
18 | You should have received a copy of the GNU Lesser General Public License |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
8041b5ba LP |
20 | ***/ |
21 | ||
1c4baffc | 22 | #include "sd-netlink.h" |
b5efdb8a LP |
23 | |
24 | #include "alloc-util.h" | |
e80af1bd | 25 | #include "local-addresses.h" |
cf0fbc49 TA |
26 | #include "macro.h" |
27 | #include "netlink-util.h" | |
8041b5ba | 28 | |
5502f0d9 | 29 | static int address_compare(const void *_a, const void *_b) { |
e80af1bd | 30 | const struct local_address *a = _a, *b = _b; |
5502f0d9 LP |
31 | |
32 | /* Order lowest scope first, IPv4 before IPv6, lowest interface index first */ | |
33 | ||
e9140aff LP |
34 | if (a->family == AF_INET && b->family == AF_INET6) |
35 | return -1; | |
36 | if (a->family == AF_INET6 && b->family == AF_INET) | |
37 | return 1; | |
38 | ||
5502f0d9 LP |
39 | if (a->scope < b->scope) |
40 | return -1; | |
41 | if (a->scope > b->scope) | |
42 | return 1; | |
43 | ||
e9140aff | 44 | if (a->metric < b->metric) |
5502f0d9 | 45 | return -1; |
e9140aff | 46 | if (a->metric > b->metric) |
5502f0d9 LP |
47 | return 1; |
48 | ||
49 | if (a->ifindex < b->ifindex) | |
50 | return -1; | |
51 | if (a->ifindex > b->ifindex) | |
52 | return 1; | |
53 | ||
00d75e57 | 54 | return memcmp(&a->address, &b->address, FAMILY_ADDRESS_SIZE(a->family)); |
5502f0d9 LP |
55 | } |
56 | ||
1c4baffc | 57 | int local_addresses(sd_netlink *context, int ifindex, int af, struct local_address **ret) { |
4afd3348 LP |
58 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
59 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
e80af1bd | 60 | _cleanup_free_ struct local_address *list = NULL; |
5502f0d9 | 61 | size_t n_list = 0, n_allocated = 0; |
1c4baffc | 62 | sd_netlink_message *m; |
d1ca51b1 | 63 | int r; |
d73c3269 | 64 | |
e80af1bd LP |
65 | assert(ret); |
66 | ||
ee8c4568 | 67 | if (context) |
1c4baffc | 68 | rtnl = sd_netlink_ref(context); |
ee8c4568 | 69 | else { |
1c4baffc | 70 | r = sd_netlink_open(&rtnl); |
ee8c4568 LP |
71 | if (r < 0) |
72 | return r; | |
73 | } | |
8041b5ba | 74 | |
1d050e1e | 75 | r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, af); |
d1ca51b1 TG |
76 | if (r < 0) |
77 | return r; | |
8041b5ba | 78 | |
1c4baffc | 79 | r = sd_netlink_call(rtnl, req, 0, &reply); |
d1ca51b1 TG |
80 | if (r < 0) |
81 | return r; | |
d1ca51b1 | 82 | |
1c4baffc | 83 | for (m = reply; m; m = sd_netlink_message_next(m)) { |
e80af1bd | 84 | struct local_address *a; |
d1ca51b1 | 85 | unsigned char flags; |
5502f0d9 | 86 | uint16_t type; |
1d050e1e | 87 | int ifi, family; |
d1ca51b1 | 88 | |
1c4baffc | 89 | r = sd_netlink_message_get_errno(m); |
d1ca51b1 TG |
90 | if (r < 0) |
91 | return r; | |
92 | ||
1c4baffc | 93 | r = sd_netlink_message_get_type(m, &type); |
d1ca51b1 TG |
94 | if (r < 0) |
95 | return r; | |
d1ca51b1 | 96 | if (type != RTM_NEWADDR) |
8041b5ba LP |
97 | continue; |
98 | ||
ee8c4568 LP |
99 | r = sd_rtnl_message_addr_get_ifindex(m, &ifi); |
100 | if (r < 0) | |
101 | return r; | |
1d050e1e LP |
102 | if (ifindex > 0 && ifi != ifindex) |
103 | continue; | |
ee8c4568 | 104 | |
1d050e1e LP |
105 | r = sd_rtnl_message_addr_get_family(m, &family); |
106 | if (r < 0) | |
107 | return r; | |
108 | if (af != AF_UNSPEC && af != family) | |
ee8c4568 LP |
109 | continue; |
110 | ||
5502f0d9 | 111 | r = sd_rtnl_message_addr_get_flags(m, &flags); |
d1ca51b1 TG |
112 | if (r < 0) |
113 | return r; | |
5502f0d9 | 114 | if (flags & IFA_F_DEPRECATED) |
d73c3269 | 115 | continue; |
8041b5ba | 116 | |
e9140aff | 117 | if (!GREEDY_REALLOC0(list, n_allocated, n_list+1)) |
5502f0d9 LP |
118 | return -ENOMEM; |
119 | ||
120 | a = list + n_list; | |
121 | ||
122 | r = sd_rtnl_message_addr_get_scope(m, &a->scope); | |
d1ca51b1 TG |
123 | if (r < 0) |
124 | return r; | |
8041b5ba | 125 | |
945c2931 | 126 | if (ifindex == 0 && IN_SET(a->scope, RT_SCOPE_HOST, RT_SCOPE_NOWHERE)) |
d73c3269 | 127 | continue; |
8041b5ba | 128 | |
1d050e1e | 129 | switch (family) { |
5502f0d9 | 130 | |
d1ca51b1 | 131 | case AF_INET: |
1c4baffc | 132 | r = sd_netlink_message_read_in_addr(m, IFA_LOCAL, &a->address.in); |
d1ca51b1 | 133 | if (r < 0) { |
1c4baffc | 134 | r = sd_netlink_message_read_in_addr(m, IFA_ADDRESS, &a->address.in); |
d1ca51b1 TG |
135 | if (r < 0) |
136 | continue; | |
137 | } | |
138 | break; | |
5502f0d9 | 139 | |
d1ca51b1 | 140 | case AF_INET6: |
1c4baffc | 141 | r = sd_netlink_message_read_in6_addr(m, IFA_LOCAL, &a->address.in6); |
d1ca51b1 | 142 | if (r < 0) { |
1c4baffc | 143 | r = sd_netlink_message_read_in6_addr(m, IFA_ADDRESS, &a->address.in6); |
d1ca51b1 TG |
144 | if (r < 0) |
145 | continue; | |
146 | } | |
147 | break; | |
5502f0d9 | 148 | |
d1ca51b1 | 149 | default: |
d73c3269 | 150 | continue; |
d73c3269 | 151 | } |
8041b5ba | 152 | |
ee8c4568 | 153 | a->ifindex = ifi; |
1d050e1e | 154 | a->family = family; |
8041b5ba | 155 | |
d1ca51b1 | 156 | n_list++; |
5502f0d9 | 157 | }; |
8041b5ba | 158 | |
68a9c7c4 | 159 | qsort_safe(list, n_list, sizeof(struct local_address), address_compare); |
e9140aff LP |
160 | |
161 | *ret = list; | |
162 | list = NULL; | |
163 | ||
164 | return (int) n_list; | |
165 | } | |
166 | ||
1c4baffc | 167 | int local_gateways(sd_netlink *context, int ifindex, int af, struct local_address **ret) { |
4afd3348 LP |
168 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
169 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
e9140aff | 170 | _cleanup_free_ struct local_address *list = NULL; |
1c4baffc | 171 | sd_netlink_message *m = NULL; |
e9140aff LP |
172 | size_t n_list = 0, n_allocated = 0; |
173 | int r; | |
174 | ||
175 | assert(ret); | |
176 | ||
177 | if (context) | |
1c4baffc | 178 | rtnl = sd_netlink_ref(context); |
e9140aff | 179 | else { |
1c4baffc | 180 | r = sd_netlink_open(&rtnl); |
e9140aff LP |
181 | if (r < 0) |
182 | return r; | |
183 | } | |
184 | ||
1d050e1e | 185 | r = sd_rtnl_message_new_route(rtnl, &req, RTM_GETROUTE, af, RTPROT_UNSPEC); |
e9140aff LP |
186 | if (r < 0) |
187 | return r; | |
188 | ||
1c4baffc | 189 | r = sd_netlink_message_request_dump(req, true); |
e9140aff LP |
190 | if (r < 0) |
191 | return r; | |
192 | ||
1c4baffc | 193 | r = sd_netlink_call(rtnl, req, 0, &reply); |
e9140aff LP |
194 | if (r < 0) |
195 | return r; | |
196 | ||
1c4baffc | 197 | for (m = reply; m; m = sd_netlink_message_next(m)) { |
e9140aff LP |
198 | struct local_address *a; |
199 | uint16_t type; | |
a98433c0 | 200 | unsigned char dst_len, src_len; |
e9140aff | 201 | uint32_t ifi; |
1d050e1e | 202 | int family; |
e9140aff | 203 | |
1c4baffc | 204 | r = sd_netlink_message_get_errno(m); |
e9140aff LP |
205 | if (r < 0) |
206 | return r; | |
207 | ||
1c4baffc | 208 | r = sd_netlink_message_get_type(m, &type); |
e9140aff LP |
209 | if (r < 0) |
210 | return r; | |
e9140aff LP |
211 | if (type != RTM_NEWROUTE) |
212 | continue; | |
213 | ||
a98433c0 | 214 | /* We only care for default routes */ |
584d0d2a | 215 | r = sd_rtnl_message_route_get_dst_prefixlen(m, &dst_len); |
e9140aff LP |
216 | if (r < 0) |
217 | return r; | |
e9140aff LP |
218 | if (dst_len != 0) |
219 | continue; | |
220 | ||
584d0d2a | 221 | r = sd_rtnl_message_route_get_src_prefixlen(m, &src_len); |
a98433c0 LP |
222 | if (r < 0) |
223 | return r; | |
224 | if (src_len != 0) | |
225 | continue; | |
226 | ||
1c4baffc | 227 | r = sd_netlink_message_read_u32(m, RTA_OIF, &ifi); |
e9140aff LP |
228 | if (r < 0) |
229 | return r; | |
e9140aff LP |
230 | if (ifindex > 0 && (int) ifi != ifindex) |
231 | continue; | |
232 | ||
1d050e1e LP |
233 | r = sd_rtnl_message_route_get_family(m, &family); |
234 | if (r < 0) | |
235 | return r; | |
236 | if (af != AF_UNSPEC && af != family) | |
237 | continue; | |
238 | ||
e9140aff LP |
239 | if (!GREEDY_REALLOC0(list, n_allocated, n_list + 1)) |
240 | return -ENOMEM; | |
241 | ||
242 | a = list + n_list; | |
243 | ||
1d050e1e | 244 | switch (family) { |
e9140aff | 245 | case AF_INET: |
1c4baffc | 246 | r = sd_netlink_message_read_in_addr(m, RTA_GATEWAY, &a->address.in); |
e9140aff LP |
247 | if (r < 0) |
248 | continue; | |
249 | ||
250 | break; | |
251 | case AF_INET6: | |
1c4baffc | 252 | r = sd_netlink_message_read_in6_addr(m, RTA_GATEWAY, &a->address.in6); |
e9140aff LP |
253 | if (r < 0) |
254 | continue; | |
255 | ||
256 | break; | |
257 | default: | |
258 | continue; | |
259 | } | |
260 | ||
1c4baffc | 261 | sd_netlink_message_read_u32(m, RTA_PRIORITY, &a->metric); |
e9140aff LP |
262 | |
263 | a->ifindex = ifi; | |
1d050e1e | 264 | a->family = family; |
e9140aff | 265 | |
1d050e1e | 266 | n_list++; |
e9140aff LP |
267 | } |
268 | ||
269 | if (n_list > 0) | |
e80af1bd | 270 | qsort(list, n_list, sizeof(struct local_address), address_compare); |
d73c3269 | 271 | |
e80af1bd | 272 | *ret = list; |
d1ca51b1 | 273 | list = NULL; |
d73c3269 | 274 | |
e80af1bd | 275 | return (int) n_list; |
8041b5ba | 276 | } |