]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
8041b5ba | 2 | |
1c4baffc | 3 | #include "sd-netlink.h" |
b5efdb8a LP |
4 | |
5 | #include "alloc-util.h" | |
e80af1bd | 6 | #include "local-addresses.h" |
cf0fbc49 TA |
7 | #include "macro.h" |
8 | #include "netlink-util.h" | |
760877e9 | 9 | #include "sort-util.h" |
8041b5ba | 10 | |
93bab288 YW |
11 | static int address_compare(const struct local_address *a, const struct local_address *b) { |
12 | int r; | |
5502f0d9 LP |
13 | |
14 | /* Order lowest scope first, IPv4 before IPv6, lowest interface index first */ | |
15 | ||
e9140aff LP |
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 | ||
93bab288 YW |
21 | r = CMP(a->scope, b->scope); |
22 | if (r != 0) | |
23 | return r; | |
5502f0d9 | 24 | |
93bab288 YW |
25 | r = CMP(a->metric, b->metric); |
26 | if (r != 0) | |
27 | return r; | |
5502f0d9 | 28 | |
93bab288 YW |
29 | r = CMP(a->ifindex, b->ifindex); |
30 | if (r != 0) | |
31 | return r; | |
5502f0d9 | 32 | |
00d75e57 | 33 | return memcmp(&a->address, &b->address, FAMILY_ADDRESS_SIZE(a->family)); |
5502f0d9 LP |
34 | } |
35 | ||
1c4baffc | 36 | int local_addresses(sd_netlink *context, int ifindex, int af, struct local_address **ret) { |
4afd3348 LP |
37 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
38 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
e80af1bd | 39 | _cleanup_free_ struct local_address *list = NULL; |
5502f0d9 | 40 | size_t n_list = 0, n_allocated = 0; |
1c4baffc | 41 | sd_netlink_message *m; |
d1ca51b1 | 42 | int r; |
d73c3269 | 43 | |
e80af1bd LP |
44 | assert(ret); |
45 | ||
ee8c4568 | 46 | if (context) |
1c4baffc | 47 | rtnl = sd_netlink_ref(context); |
ee8c4568 | 48 | else { |
1c4baffc | 49 | r = sd_netlink_open(&rtnl); |
ee8c4568 LP |
50 | if (r < 0) |
51 | return r; | |
52 | } | |
8041b5ba | 53 | |
1d050e1e | 54 | r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, af); |
d1ca51b1 TG |
55 | if (r < 0) |
56 | return r; | |
8041b5ba | 57 | |
1c4baffc | 58 | r = sd_netlink_call(rtnl, req, 0, &reply); |
d1ca51b1 TG |
59 | if (r < 0) |
60 | return r; | |
d1ca51b1 | 61 | |
1c4baffc | 62 | for (m = reply; m; m = sd_netlink_message_next(m)) { |
e80af1bd | 63 | struct local_address *a; |
d1ca51b1 | 64 | unsigned char flags; |
5502f0d9 | 65 | uint16_t type; |
1d050e1e | 66 | int ifi, family; |
d1ca51b1 | 67 | |
1c4baffc | 68 | r = sd_netlink_message_get_errno(m); |
d1ca51b1 TG |
69 | if (r < 0) |
70 | return r; | |
71 | ||
1c4baffc | 72 | r = sd_netlink_message_get_type(m, &type); |
d1ca51b1 TG |
73 | if (r < 0) |
74 | return r; | |
d1ca51b1 | 75 | if (type != RTM_NEWADDR) |
8041b5ba LP |
76 | continue; |
77 | ||
ee8c4568 LP |
78 | r = sd_rtnl_message_addr_get_ifindex(m, &ifi); |
79 | if (r < 0) | |
80 | return r; | |
1d050e1e LP |
81 | if (ifindex > 0 && ifi != ifindex) |
82 | continue; | |
ee8c4568 | 83 | |
1d050e1e LP |
84 | r = sd_rtnl_message_addr_get_family(m, &family); |
85 | if (r < 0) | |
86 | return r; | |
87 | if (af != AF_UNSPEC && af != family) | |
ee8c4568 LP |
88 | continue; |
89 | ||
5502f0d9 | 90 | r = sd_rtnl_message_addr_get_flags(m, &flags); |
d1ca51b1 TG |
91 | if (r < 0) |
92 | return r; | |
5502f0d9 | 93 | if (flags & IFA_F_DEPRECATED) |
d73c3269 | 94 | continue; |
8041b5ba | 95 | |
e9140aff | 96 | if (!GREEDY_REALLOC0(list, n_allocated, n_list+1)) |
5502f0d9 LP |
97 | return -ENOMEM; |
98 | ||
99 | a = list + n_list; | |
100 | ||
101 | r = sd_rtnl_message_addr_get_scope(m, &a->scope); | |
d1ca51b1 TG |
102 | if (r < 0) |
103 | return r; | |
8041b5ba | 104 | |
945c2931 | 105 | if (ifindex == 0 && IN_SET(a->scope, RT_SCOPE_HOST, RT_SCOPE_NOWHERE)) |
d73c3269 | 106 | continue; |
8041b5ba | 107 | |
1d050e1e | 108 | switch (family) { |
5502f0d9 | 109 | |
d1ca51b1 | 110 | case AF_INET: |
1c4baffc | 111 | r = sd_netlink_message_read_in_addr(m, IFA_LOCAL, &a->address.in); |
d1ca51b1 | 112 | if (r < 0) { |
1c4baffc | 113 | r = sd_netlink_message_read_in_addr(m, IFA_ADDRESS, &a->address.in); |
d1ca51b1 TG |
114 | if (r < 0) |
115 | continue; | |
116 | } | |
117 | break; | |
5502f0d9 | 118 | |
d1ca51b1 | 119 | case AF_INET6: |
1c4baffc | 120 | r = sd_netlink_message_read_in6_addr(m, IFA_LOCAL, &a->address.in6); |
d1ca51b1 | 121 | if (r < 0) { |
1c4baffc | 122 | r = sd_netlink_message_read_in6_addr(m, IFA_ADDRESS, &a->address.in6); |
d1ca51b1 TG |
123 | if (r < 0) |
124 | continue; | |
125 | } | |
126 | break; | |
5502f0d9 | 127 | |
d1ca51b1 | 128 | default: |
d73c3269 | 129 | continue; |
d73c3269 | 130 | } |
8041b5ba | 131 | |
ee8c4568 | 132 | a->ifindex = ifi; |
1d050e1e | 133 | a->family = family; |
8041b5ba | 134 | |
d1ca51b1 | 135 | n_list++; |
5502f0d9 | 136 | }; |
8041b5ba | 137 | |
93bab288 | 138 | typesafe_qsort(list, n_list, address_compare); |
e9140aff | 139 | |
1cc6c93a | 140 | *ret = TAKE_PTR(list); |
e9140aff LP |
141 | |
142 | return (int) n_list; | |
143 | } | |
144 | ||
1c4baffc | 145 | int local_gateways(sd_netlink *context, int ifindex, int af, struct local_address **ret) { |
4afd3348 LP |
146 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
147 | _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL; | |
e9140aff | 148 | _cleanup_free_ struct local_address *list = NULL; |
1c4baffc | 149 | sd_netlink_message *m = NULL; |
e9140aff LP |
150 | size_t n_list = 0, n_allocated = 0; |
151 | int r; | |
152 | ||
153 | assert(ret); | |
154 | ||
155 | if (context) | |
1c4baffc | 156 | rtnl = sd_netlink_ref(context); |
e9140aff | 157 | else { |
1c4baffc | 158 | r = sd_netlink_open(&rtnl); |
e9140aff LP |
159 | if (r < 0) |
160 | return r; | |
161 | } | |
162 | ||
1d050e1e | 163 | r = sd_rtnl_message_new_route(rtnl, &req, RTM_GETROUTE, af, RTPROT_UNSPEC); |
e9140aff LP |
164 | if (r < 0) |
165 | return r; | |
166 | ||
1c4baffc | 167 | r = sd_netlink_message_request_dump(req, true); |
e9140aff LP |
168 | if (r < 0) |
169 | return r; | |
170 | ||
1c4baffc | 171 | r = sd_netlink_call(rtnl, req, 0, &reply); |
e9140aff LP |
172 | if (r < 0) |
173 | return r; | |
174 | ||
1c4baffc | 175 | for (m = reply; m; m = sd_netlink_message_next(m)) { |
e9140aff LP |
176 | struct local_address *a; |
177 | uint16_t type; | |
d1b014df | 178 | unsigned char dst_len, src_len, table; |
e9140aff | 179 | uint32_t ifi; |
1d050e1e | 180 | int family; |
e9140aff | 181 | |
1c4baffc | 182 | r = sd_netlink_message_get_errno(m); |
e9140aff LP |
183 | if (r < 0) |
184 | return r; | |
185 | ||
1c4baffc | 186 | r = sd_netlink_message_get_type(m, &type); |
e9140aff LP |
187 | if (r < 0) |
188 | return r; | |
e9140aff LP |
189 | if (type != RTM_NEWROUTE) |
190 | continue; | |
191 | ||
a98433c0 | 192 | /* We only care for default routes */ |
584d0d2a | 193 | r = sd_rtnl_message_route_get_dst_prefixlen(m, &dst_len); |
e9140aff LP |
194 | if (r < 0) |
195 | return r; | |
e9140aff LP |
196 | if (dst_len != 0) |
197 | continue; | |
198 | ||
584d0d2a | 199 | r = sd_rtnl_message_route_get_src_prefixlen(m, &src_len); |
a98433c0 LP |
200 | if (r < 0) |
201 | return r; | |
202 | if (src_len != 0) | |
203 | continue; | |
204 | ||
d1b014df LP |
205 | r = sd_rtnl_message_route_get_table(m, &table); |
206 | if (r < 0) | |
207 | return r; | |
208 | if (table != RT_TABLE_MAIN) | |
209 | continue; | |
210 | ||
1c4baffc | 211 | r = sd_netlink_message_read_u32(m, RTA_OIF, &ifi); |
568fc5c3 LP |
212 | if (r == -ENODATA) /* Not all routes have an RTA_OIF attribute (for example nexthop ones) */ |
213 | continue; | |
e9140aff LP |
214 | if (r < 0) |
215 | return r; | |
e9140aff LP |
216 | if (ifindex > 0 && (int) ifi != ifindex) |
217 | continue; | |
218 | ||
1d050e1e LP |
219 | r = sd_rtnl_message_route_get_family(m, &family); |
220 | if (r < 0) | |
221 | return r; | |
222 | if (af != AF_UNSPEC && af != family) | |
223 | continue; | |
224 | ||
e9140aff LP |
225 | if (!GREEDY_REALLOC0(list, n_allocated, n_list + 1)) |
226 | return -ENOMEM; | |
227 | ||
228 | a = list + n_list; | |
229 | ||
1d050e1e | 230 | switch (family) { |
e9140aff | 231 | case AF_INET: |
1c4baffc | 232 | r = sd_netlink_message_read_in_addr(m, RTA_GATEWAY, &a->address.in); |
e9140aff LP |
233 | if (r < 0) |
234 | continue; | |
235 | ||
236 | break; | |
237 | case AF_INET6: | |
1c4baffc | 238 | r = sd_netlink_message_read_in6_addr(m, RTA_GATEWAY, &a->address.in6); |
e9140aff LP |
239 | if (r < 0) |
240 | continue; | |
241 | ||
242 | break; | |
243 | default: | |
244 | continue; | |
245 | } | |
246 | ||
1c4baffc | 247 | sd_netlink_message_read_u32(m, RTA_PRIORITY, &a->metric); |
e9140aff LP |
248 | |
249 | a->ifindex = ifi; | |
1d050e1e | 250 | a->family = family; |
e9140aff | 251 | |
1d050e1e | 252 | n_list++; |
e9140aff LP |
253 | } |
254 | ||
93bab288 | 255 | typesafe_qsort(list, n_list, address_compare); |
d73c3269 | 256 | |
1cc6c93a | 257 | *ret = TAKE_PTR(list); |
d73c3269 | 258 | |
e80af1bd | 259 | return (int) n_list; |
8041b5ba | 260 | } |