1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2008-2011 Lennart Poettering
6 Copyright 2014 Tom Gundersen
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.
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.
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/>.
22 #include "sd-netlink.h"
24 #include "alloc-util.h"
25 #include "local-addresses.h"
27 #include "netlink-util.h"
29 static int address_compare(const void *_a
, const void *_b
) {
30 const struct local_address
*a
= _a
, *b
= _b
;
32 /* Order lowest scope first, IPv4 before IPv6, lowest interface index first */
34 if (a
->family
== AF_INET
&& b
->family
== AF_INET6
)
36 if (a
->family
== AF_INET6
&& b
->family
== AF_INET
)
39 if (a
->scope
< b
->scope
)
41 if (a
->scope
> b
->scope
)
44 if (a
->metric
< b
->metric
)
46 if (a
->metric
> b
->metric
)
49 if (a
->ifindex
< b
->ifindex
)
51 if (a
->ifindex
> b
->ifindex
)
54 return memcmp(&a
->address
, &b
->address
, FAMILY_ADDRESS_SIZE(a
->family
));
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
;
68 rtnl
= sd_netlink_ref(context
);
70 r
= sd_netlink_open(&rtnl
);
75 r
= sd_rtnl_message_new_addr(rtnl
, &req
, RTM_GETADDR
, 0, af
);
79 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
83 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
84 struct local_address
*a
;
89 r
= sd_netlink_message_get_errno(m
);
93 r
= sd_netlink_message_get_type(m
, &type
);
96 if (type
!= RTM_NEWADDR
)
99 r
= sd_rtnl_message_addr_get_ifindex(m
, &ifi
);
102 if (ifindex
> 0 && ifi
!= ifindex
)
105 r
= sd_rtnl_message_addr_get_family(m
, &family
);
108 if (af
!= AF_UNSPEC
&& af
!= family
)
111 r
= sd_rtnl_message_addr_get_flags(m
, &flags
);
114 if (flags
& IFA_F_DEPRECATED
)
117 if (!GREEDY_REALLOC0(list
, n_allocated
, n_list
+1))
122 r
= sd_rtnl_message_addr_get_scope(m
, &a
->scope
);
126 if (ifindex
== 0 && IN_SET(a
->scope
, RT_SCOPE_HOST
, RT_SCOPE_NOWHERE
))
132 r
= sd_netlink_message_read_in_addr(m
, IFA_LOCAL
, &a
->address
.in
);
134 r
= sd_netlink_message_read_in_addr(m
, IFA_ADDRESS
, &a
->address
.in
);
141 r
= sd_netlink_message_read_in6_addr(m
, IFA_LOCAL
, &a
->address
.in6
);
143 r
= sd_netlink_message_read_in6_addr(m
, IFA_ADDRESS
, &a
->address
.in6
);
159 qsort_safe(list
, n_list
, sizeof(struct local_address
), address_compare
);
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;
178 rtnl
= sd_netlink_ref(context
);
180 r
= sd_netlink_open(&rtnl
);
185 r
= sd_rtnl_message_new_route(rtnl
, &req
, RTM_GETROUTE
, af
, RTPROT_UNSPEC
);
189 r
= sd_netlink_message_request_dump(req
, true);
193 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
197 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
198 struct local_address
*a
;
200 unsigned char dst_len
, src_len
;
204 r
= sd_netlink_message_get_errno(m
);
208 r
= sd_netlink_message_get_type(m
, &type
);
211 if (type
!= RTM_NEWROUTE
)
214 /* We only care for default routes */
215 r
= sd_rtnl_message_route_get_dst_prefixlen(m
, &dst_len
);
221 r
= sd_rtnl_message_route_get_src_prefixlen(m
, &src_len
);
227 r
= sd_netlink_message_read_u32(m
, RTA_OIF
, &ifi
);
230 if (ifindex
> 0 && (int) ifi
!= ifindex
)
233 r
= sd_rtnl_message_route_get_family(m
, &family
);
236 if (af
!= AF_UNSPEC
&& af
!= family
)
239 if (!GREEDY_REALLOC0(list
, n_allocated
, n_list
+ 1))
246 r
= sd_netlink_message_read_in_addr(m
, RTA_GATEWAY
, &a
->address
.in
);
252 r
= sd_netlink_message_read_in6_addr(m
, RTA_GATEWAY
, &a
->address
.in6
);
261 sd_netlink_message_read_u32(m
, RTA_PRIORITY
, &a
->metric
);
270 qsort(list
, n_list
, sizeof(struct local_address
), address_compare
);