1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2008-2011 Lennart Poettering
7 Copyright 2014 Tom Gundersen
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "sd-netlink.h"
25 #include "alloc-util.h"
26 #include "local-addresses.h"
28 #include "netlink-util.h"
30 static int address_compare(const void *_a
, const void *_b
) {
31 const struct local_address
*a
= _a
, *b
= _b
;
33 /* Order lowest scope first, IPv4 before IPv6, lowest interface index first */
35 if (a
->family
== AF_INET
&& b
->family
== AF_INET6
)
37 if (a
->family
== AF_INET6
&& b
->family
== AF_INET
)
40 if (a
->scope
< b
->scope
)
42 if (a
->scope
> b
->scope
)
45 if (a
->metric
< b
->metric
)
47 if (a
->metric
> b
->metric
)
50 if (a
->ifindex
< b
->ifindex
)
52 if (a
->ifindex
> b
->ifindex
)
55 return memcmp(&a
->address
, &b
->address
, FAMILY_ADDRESS_SIZE(a
->family
));
58 int local_addresses(sd_netlink
*context
, int ifindex
, int af
, struct local_address
**ret
) {
59 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
, *reply
= NULL
;
60 _cleanup_netlink_unref_ sd_netlink
*rtnl
= NULL
;
61 _cleanup_free_
struct local_address
*list
= NULL
;
62 size_t n_list
= 0, n_allocated
= 0;
63 sd_netlink_message
*m
;
69 rtnl
= sd_netlink_ref(context
);
71 r
= sd_netlink_open(&rtnl
);
76 r
= sd_rtnl_message_new_addr(rtnl
, &req
, RTM_GETADDR
, 0, af
);
80 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
84 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
85 struct local_address
*a
;
90 r
= sd_netlink_message_get_errno(m
);
94 r
= sd_netlink_message_get_type(m
, &type
);
97 if (type
!= RTM_NEWADDR
)
100 r
= sd_rtnl_message_addr_get_ifindex(m
, &ifi
);
103 if (ifindex
> 0 && ifi
!= ifindex
)
106 r
= sd_rtnl_message_addr_get_family(m
, &family
);
109 if (af
!= AF_UNSPEC
&& af
!= family
)
112 r
= sd_rtnl_message_addr_get_flags(m
, &flags
);
115 if (flags
& IFA_F_DEPRECATED
)
118 if (!GREEDY_REALLOC0(list
, n_allocated
, n_list
+1))
123 r
= sd_rtnl_message_addr_get_scope(m
, &a
->scope
);
127 if (ifindex
== 0 && (a
->scope
== RT_SCOPE_HOST
|| a
->scope
== RT_SCOPE_NOWHERE
))
133 r
= sd_netlink_message_read_in_addr(m
, IFA_LOCAL
, &a
->address
.in
);
135 r
= sd_netlink_message_read_in_addr(m
, IFA_ADDRESS
, &a
->address
.in
);
142 r
= sd_netlink_message_read_in6_addr(m
, IFA_LOCAL
, &a
->address
.in6
);
144 r
= sd_netlink_message_read_in6_addr(m
, IFA_ADDRESS
, &a
->address
.in6
);
161 qsort(list
, n_list
, sizeof(struct local_address
), address_compare
);
169 int local_gateways(sd_netlink
*context
, int ifindex
, int af
, struct local_address
**ret
) {
170 _cleanup_netlink_message_unref_ sd_netlink_message
*req
= NULL
, *reply
= NULL
;
171 _cleanup_netlink_unref_ sd_netlink
*rtnl
= NULL
;
172 _cleanup_free_
struct local_address
*list
= NULL
;
173 sd_netlink_message
*m
= NULL
;
174 size_t n_list
= 0, n_allocated
= 0;
180 rtnl
= sd_netlink_ref(context
);
182 r
= sd_netlink_open(&rtnl
);
187 r
= sd_rtnl_message_new_route(rtnl
, &req
, RTM_GETROUTE
, af
, RTPROT_UNSPEC
);
191 r
= sd_netlink_message_request_dump(req
, true);
195 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
199 for (m
= reply
; m
; m
= sd_netlink_message_next(m
)) {
200 struct local_address
*a
;
202 unsigned char dst_len
, src_len
;
206 r
= sd_netlink_message_get_errno(m
);
210 r
= sd_netlink_message_get_type(m
, &type
);
213 if (type
!= RTM_NEWROUTE
)
216 /* We only care for default routes */
217 r
= sd_rtnl_message_route_get_dst_prefixlen(m
, &dst_len
);
223 r
= sd_rtnl_message_route_get_src_prefixlen(m
, &src_len
);
229 r
= sd_netlink_message_read_u32(m
, RTA_OIF
, &ifi
);
232 if (ifindex
> 0 && (int) ifi
!= ifindex
)
235 r
= sd_rtnl_message_route_get_family(m
, &family
);
238 if (af
!= AF_UNSPEC
&& af
!= family
)
241 if (!GREEDY_REALLOC0(list
, n_allocated
, n_list
+ 1))
248 r
= sd_netlink_message_read_in_addr(m
, RTA_GATEWAY
, &a
->address
.in
);
254 r
= sd_netlink_message_read_in6_addr(m
, RTA_GATEWAY
, &a
->address
.in6
);
263 sd_netlink_message_read_u32(m
, RTA_PRIORITY
, &a
->metric
);
272 qsort(list
, n_list
, sizeof(struct local_address
), address_compare
);