]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/local-addresses.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / shared / local-addresses.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-netlink.h"
4
5 #include "alloc-util.h"
6 #include "local-addresses.h"
7 #include "macro.h"
8 #include "netlink-util.h"
9 #include "sort-util.h"
10
11 static int address_compare(const struct local_address *a, const struct local_address *b) {
12 int r;
13
14 /* Order lowest scope first, IPv4 before IPv6, lowest interface index first */
15
16 if (a->family == AF_INET && b->family == AF_INET6)
17 return -1;
18 if (a->family == AF_INET6 && b->family == AF_INET)
19 return 1;
20
21 r = CMP(a->scope, b->scope);
22 if (r != 0)
23 return r;
24
25 r = CMP(a->metric, b->metric);
26 if (r != 0)
27 return r;
28
29 r = CMP(a->ifindex, b->ifindex);
30 if (r != 0)
31 return r;
32
33 return memcmp(&a->address, &b->address, FAMILY_ADDRESS_SIZE(a->family));
34 }
35
36 int local_addresses(sd_netlink *context, int ifindex, int af, struct local_address **ret) {
37 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
38 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
39 _cleanup_free_ struct local_address *list = NULL;
40 size_t n_list = 0, n_allocated = 0;
41 sd_netlink_message *m;
42 int r;
43
44 assert(ret);
45
46 if (context)
47 rtnl = sd_netlink_ref(context);
48 else {
49 r = sd_netlink_open(&rtnl);
50 if (r < 0)
51 return r;
52 }
53
54 r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, af);
55 if (r < 0)
56 return r;
57
58 r = sd_netlink_call(rtnl, req, 0, &reply);
59 if (r < 0)
60 return r;
61
62 for (m = reply; m; m = sd_netlink_message_next(m)) {
63 struct local_address *a;
64 unsigned char flags;
65 uint16_t type;
66 int ifi, family;
67
68 r = sd_netlink_message_get_errno(m);
69 if (r < 0)
70 return r;
71
72 r = sd_netlink_message_get_type(m, &type);
73 if (r < 0)
74 return r;
75 if (type != RTM_NEWADDR)
76 continue;
77
78 r = sd_rtnl_message_addr_get_ifindex(m, &ifi);
79 if (r < 0)
80 return r;
81 if (ifindex > 0 && ifi != ifindex)
82 continue;
83
84 r = sd_rtnl_message_addr_get_family(m, &family);
85 if (r < 0)
86 return r;
87 if (af != AF_UNSPEC && af != family)
88 continue;
89
90 r = sd_rtnl_message_addr_get_flags(m, &flags);
91 if (r < 0)
92 return r;
93 if (flags & IFA_F_DEPRECATED)
94 continue;
95
96 if (!GREEDY_REALLOC0(list, n_allocated, n_list+1))
97 return -ENOMEM;
98
99 a = list + n_list;
100
101 r = sd_rtnl_message_addr_get_scope(m, &a->scope);
102 if (r < 0)
103 return r;
104
105 if (ifindex == 0 && IN_SET(a->scope, RT_SCOPE_HOST, RT_SCOPE_NOWHERE))
106 continue;
107
108 switch (family) {
109
110 case AF_INET:
111 r = sd_netlink_message_read_in_addr(m, IFA_LOCAL, &a->address.in);
112 if (r < 0) {
113 r = sd_netlink_message_read_in_addr(m, IFA_ADDRESS, &a->address.in);
114 if (r < 0)
115 continue;
116 }
117 break;
118
119 case AF_INET6:
120 r = sd_netlink_message_read_in6_addr(m, IFA_LOCAL, &a->address.in6);
121 if (r < 0) {
122 r = sd_netlink_message_read_in6_addr(m, IFA_ADDRESS, &a->address.in6);
123 if (r < 0)
124 continue;
125 }
126 break;
127
128 default:
129 continue;
130 }
131
132 a->ifindex = ifi;
133 a->family = family;
134
135 n_list++;
136 };
137
138 typesafe_qsort(list, n_list, address_compare);
139
140 *ret = TAKE_PTR(list);
141
142 return (int) n_list;
143 }
144
145 static int add_local_gateway(
146 struct local_address **list,
147 size_t *n_list,
148 size_t *n_allocated,
149 int af,
150 int ifindex,
151 uint32_t metric,
152 const RouteVia *via) {
153
154 assert(list);
155 assert(n_list);
156 assert(n_allocated);
157 assert(via);
158
159 if (af != AF_UNSPEC && af != via->family)
160 return 0;
161
162 if (!GREEDY_REALLOC(*list, *n_allocated, *n_list + 1))
163 return -ENOMEM;
164
165 (*list)[(*n_list)++] = (struct local_address) {
166 .ifindex = ifindex,
167 .metric = metric,
168 .family = via->family,
169 .address = via->address,
170 };
171
172 return 0;
173 }
174
175 int local_gateways(sd_netlink *context, int ifindex, int af, struct local_address **ret) {
176 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
177 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
178 _cleanup_free_ struct local_address *list = NULL;
179 size_t n_list = 0, n_allocated = 0;
180 int r;
181
182 assert(ret);
183
184 if (context)
185 rtnl = sd_netlink_ref(context);
186 else {
187 r = sd_netlink_open(&rtnl);
188 if (r < 0)
189 return r;
190 }
191
192 r = sd_rtnl_message_new_route(rtnl, &req, RTM_GETROUTE, af, RTPROT_UNSPEC);
193 if (r < 0)
194 return r;
195
196 r = sd_netlink_message_request_dump(req, true);
197 if (r < 0)
198 return r;
199
200 r = sd_netlink_call(rtnl, req, 0, &reply);
201 if (r < 0)
202 return r;
203
204 for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) {
205 _cleanup_ordered_set_free_free_ OrderedSet *multipath_routes = NULL;
206 _cleanup_free_ void *rta_multipath = NULL;
207 union in_addr_union gateway;
208 uint16_t type;
209 unsigned char dst_len, src_len, table;
210 uint32_t ifi, metric = 0;
211 size_t rta_len;
212 int family;
213 RouteVia via;
214
215 r = sd_netlink_message_get_errno(m);
216 if (r < 0)
217 return r;
218
219 r = sd_netlink_message_get_type(m, &type);
220 if (r < 0)
221 return r;
222 if (type != RTM_NEWROUTE)
223 continue;
224
225 /* We only care for default routes */
226 r = sd_rtnl_message_route_get_dst_prefixlen(m, &dst_len);
227 if (r < 0)
228 return r;
229 if (dst_len != 0)
230 continue;
231
232 r = sd_rtnl_message_route_get_src_prefixlen(m, &src_len);
233 if (r < 0)
234 return r;
235 if (src_len != 0)
236 continue;
237
238 r = sd_rtnl_message_route_get_table(m, &table);
239 if (r < 0)
240 return r;
241 if (table != RT_TABLE_MAIN)
242 continue;
243
244 r = sd_netlink_message_read_u32(m, RTA_PRIORITY, &metric);
245 if (r < 0 && r != -ENODATA)
246 return r;
247
248 r = sd_rtnl_message_route_get_family(m, &family);
249 if (r < 0)
250 return r;
251 if (!IN_SET(family, AF_INET, AF_INET6))
252 continue;
253
254 r = sd_netlink_message_read_u32(m, RTA_OIF, &ifi);
255 if (r < 0 && r != -ENODATA)
256 return r;
257 if (r >= 0) {
258 if (ifi <= 0)
259 return -EINVAL;
260 if (ifindex > 0 && (int) ifi != ifindex)
261 continue;
262
263 r = netlink_message_read_in_addr_union(m, RTA_GATEWAY, family, &gateway);
264 if (r < 0 && r != -ENODATA)
265 return r;
266 if (r >= 0) {
267 via.family = family;
268 via.address = gateway;
269 r = add_local_gateway(&list, &n_list, &n_allocated, af, ifi, metric, &via);
270 if (r < 0)
271 return r;
272
273 continue;
274 }
275
276 if (family != AF_INET)
277 continue;
278
279 r = sd_netlink_message_read(m, RTA_VIA, sizeof(via), &via);
280 if (r < 0 && r != -ENODATA)
281 return r;
282 if (r >= 0) {
283 r = add_local_gateway(&list, &n_list, &n_allocated, af, ifi, metric, &via);
284 if (r < 0)
285 return r;
286
287 continue;
288 }
289 }
290
291 r = sd_netlink_message_read_data(m, RTA_MULTIPATH, &rta_len, &rta_multipath);
292 if (r < 0 && r != -ENODATA)
293 return r;
294 if (r >= 0) {
295 MultipathRoute *mr;
296
297 r = rtattr_read_nexthop(rta_multipath, rta_len, family, &multipath_routes);
298 if (r < 0)
299 return r;
300
301 ORDERED_SET_FOREACH(mr, multipath_routes) {
302 if (ifindex > 0 && mr->ifindex != ifindex)
303 continue;
304
305 r = add_local_gateway(&list, &n_list, &n_allocated, af, ifi, metric, &mr->gateway);
306 if (r < 0)
307 return r;
308 }
309 }
310 }
311
312 typesafe_qsort(list, n_list, address_compare);
313
314 *ret = TAKE_PTR(list);
315
316 return (int) n_list;
317 }