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