2 This file is part of systemd.
4 Copyright 2008-2011 Lennart Poettering
5 Copyright 2014 Tom Gundersen
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include "sd-netlink.h"
23 #include "alloc-util.h"
24 #include "local-addresses.h"
26 #include "netlink-util.h"
28 static int address_compare(const void *_a
, const void *_b
) {
29 const struct local_address
*a
= _a
, *b
= _b
;
31 /* Order lowest scope first, IPv4 before IPv6, lowest interface index first */
33 if (a
->family
== AF_INET
&& b
->family
== AF_INET6
)
35 if (a
->family
== AF_INET6
&& b
->family
== AF_INET
)
38 if (a
->scope
< b
->scope
)
40 if (a
->scope
> b
->scope
)
43 if (a
->metric
< b
->metric
)
45 if (a
->metric
> b
->metric
)
48 if (a
->ifindex
< b
->ifindex
)
50 if (a
->ifindex
> b
->ifindex
)
53 return memcmp(&a
->address
, &b
->address
, FAMILY_ADDRESS_SIZE(a
->family
));
56 int local_addresses(sd_netlink
*context
, int ifindex
, int af
, struct local_address
**ret
) {
57 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
58 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
59 _cleanup_free_
struct local_address
*list
= NULL
;
60 size_t n_list
= 0, n_allocated
= 0;
61 sd_netlink_message
*m
;
67 rtnl
= sd_netlink_ref(context
);
69 r
= sd_netlink_open(&rtnl
);
74 r
= sd_rtnl_message_new_addr(rtnl
, &req
, RTM_GETADDR
, 0, af
);
78 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
82 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
83 struct local_address
*a
;
88 r
= sd_netlink_message_get_errno(m
);
92 r
= sd_netlink_message_get_type(m
, &type
);
95 if (type
!= RTM_NEWADDR
)
98 r
= sd_rtnl_message_addr_get_ifindex(m
, &ifi
);
101 if (ifindex
> 0 && ifi
!= ifindex
)
104 r
= sd_rtnl_message_addr_get_family(m
, &family
);
107 if (af
!= AF_UNSPEC
&& af
!= family
)
110 r
= sd_rtnl_message_addr_get_flags(m
, &flags
);
113 if (flags
& IFA_F_DEPRECATED
)
116 if (!GREEDY_REALLOC0(list
, n_allocated
, n_list
+1))
121 r
= sd_rtnl_message_addr_get_scope(m
, &a
->scope
);
125 if (ifindex
== 0 && (a
->scope
== RT_SCOPE_HOST
|| a
->scope
== RT_SCOPE_NOWHERE
))
131 r
= sd_netlink_message_read_in_addr(m
, IFA_LOCAL
, &a
->address
.in
);
133 r
= sd_netlink_message_read_in_addr(m
, IFA_ADDRESS
, &a
->address
.in
);
140 r
= sd_netlink_message_read_in6_addr(m
, IFA_LOCAL
, &a
->address
.in6
);
142 r
= sd_netlink_message_read_in6_addr(m
, IFA_ADDRESS
, &a
->address
.in6
);
158 qsort_safe(list
, n_list
, sizeof(struct local_address
), address_compare
);
166 int local_gateways(sd_netlink
*context
, int ifindex
, int af
, struct local_address
**ret
) {
167 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
168 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
169 _cleanup_free_
struct local_address
*list
= NULL
;
170 sd_netlink_message
*m
= NULL
;
171 size_t n_list
= 0, n_allocated
= 0;
177 rtnl
= sd_netlink_ref(context
);
179 r
= sd_netlink_open(&rtnl
);
184 r
= sd_rtnl_message_new_route(rtnl
, &req
, RTM_GETROUTE
, af
, RTPROT_UNSPEC
);
188 r
= sd_netlink_message_request_dump(req
, true);
192 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
196 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
197 struct local_address
*a
;
199 unsigned char dst_len
, src_len
;
203 r
= sd_netlink_message_get_errno(m
);
207 r
= sd_netlink_message_get_type(m
, &type
);
210 if (type
!= RTM_NEWROUTE
)
213 /* We only care for default routes */
214 r
= sd_rtnl_message_route_get_dst_prefixlen(m
, &dst_len
);
220 r
= sd_rtnl_message_route_get_src_prefixlen(m
, &src_len
);
226 r
= sd_netlink_message_read_u32(m
, RTA_OIF
, &ifi
);
229 if (ifindex
> 0 && (int) ifi
!= ifindex
)
232 r
= sd_rtnl_message_route_get_family(m
, &family
);
235 if (af
!= AF_UNSPEC
&& af
!= family
)
238 if (!GREEDY_REALLOC0(list
, n_allocated
, n_list
+ 1))
245 r
= sd_netlink_message_read_in_addr(m
, RTA_GATEWAY
, &a
->address
.in
);
251 r
= sd_netlink_message_read_in6_addr(m
, RTA_GATEWAY
, &a
->address
.in6
);
260 sd_netlink_message_read_u32(m
, RTA_PRIORITY
, &a
->metric
);
269 qsort(list
, n_list
, sizeof(struct local_address
), address_compare
);