Debian Codegraph @

src/network/networkd-ipv6-proxy-ndp.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <netinet/in.h>
4
5 #include "sd-netlink.h"
6
7 #include "networkd-ipv6-proxy-ndp.h"
8 #include "networkd-link.h"
9 #include "networkd-manager.h"
10 #include "networkd-network.h"
11 #include "networkd-queue.h"
12 #include "set.h"
13 #include "socket-util.h"
14 #include "string-util.h"
15
16 void network_adjust_ipv6_proxy_ndp(Network *network) {
17 assert(network);
18
19 if (set_isempty(network->ipv6_proxy_ndp_addresses))
20 return;
21
22 if (!socket_ipv6_is_supported()) {
23 log_once(LOG_WARNING,
24 "%s: IPv6 proxy NDP addresses are set, but IPv6 is not supported by kernel, "
25 "Ignoring IPv6 proxy NDP addresses.", network->filename);
26 network->ipv6_proxy_ndp_addresses = set_free(network->ipv6_proxy_ndp_addresses);
27 return;
28 }
29
30 if (network->ipv6_proxy_ndp == 0) {
31 log_warning("%s: IPv6ProxyNDP= is disabled. Ignoring IPv6ProxyNDPAddress=.", network->filename);
32 network->ipv6_proxy_ndp_addresses = set_free(network->ipv6_proxy_ndp_addresses);
33 }
34 }
35
36 static int ipv6_proxy_ndp_address_configure_handler(
37 sd_netlink *rtnl,
38 sd_netlink_message *m,
39 Request *req,
40 Link *link,
41 struct in6_addr *address) {
42
43 int r;
44
45 assert(m);
46 assert(link);
47
48 r = sd_netlink_message_get_errno(m);
49 if (r < 0)
50 log_link_message_warning_errno(link, m, r, "Could not add IPv6 proxy ndp address entry, ignoring");
51
52 if (link->static_ipv6_proxy_ndp_messages == 0) {
53 log_link_debug(link, "IPv6 proxy NDP addresses set.");
54 link->static_ipv6_proxy_ndp_configured = true;
55 link_check_ready(link);
56 }
57
58 return 1;
59 }
60
61 /* send a request to the kernel to add an IPv6 Proxy entry to the neighbour table */
62 static int ipv6_proxy_ndp_address_configure(const struct in6_addr *address, Link *link, Request *req) {
63 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
64 int r;
65
66 assert(address);
67 assert(link);
68 assert(link->manager);
69 assert(link->manager->rtnl);
70 assert(req);
71
72 /* create new netlink message */
73 r = sd_rtnl_message_new_neigh(link->manager->rtnl, &m, RTM_NEWNEIGH, link->ifindex, AF_INET6);
74 if (r < 0)
75 return r;
76
77 r = sd_rtnl_message_neigh_set_flags(m, NTF_PROXY);
78 if (r < 0)
79 return r;
80
81 r = sd_netlink_message_append_in6_addr(m, NDA_DST, address);
82 if (r < 0)
83 return r;
84
85 return request_call_netlink_async(link->manager->rtnl, m, req);
86 }
87
88 static int ipv6_proxy_ndp_address_process_request(Request *req, Link *link, struct in6_addr *address) {
89 int r;
90
91 assert(req);
92 assert(link);
93 assert(address);
94
95 if (!link_is_ready_to_configure(link, false))
96 return 0;
97
98 r = ipv6_proxy_ndp_address_configure(address, link, req);
99 if (r < 0)
100 return log_link_warning_errno(link, r, "Failed to configure IPv6 proxy NDP address: %m");
101
102 return 1;
103 }
104
105 int link_request_static_ipv6_proxy_ndp_addresses(Link *link) {
106 struct in6_addr *address;
107 int r;
108
109 assert(link);
110 assert(link->network);
111
112 link->static_ipv6_proxy_ndp_configured = false;
113
114 SET_FOREACH(address, link->network->ipv6_proxy_ndp_addresses) {
115 r = link_queue_request_safe(link, REQUEST_TYPE_IPV6_PROXY_NDP,
116 address, NULL,
117 in6_addr_hash_func,
118 in6_addr_compare_func,
119 ipv6_proxy_ndp_address_process_request,
120 &link->static_ipv6_proxy_ndp_messages,
121 ipv6_proxy_ndp_address_configure_handler,
122 NULL);
123 if (r < 0)
124 return log_link_warning_errno(link, r, "Failed to request IPv6 proxy NDP address: %m");
125 }
126
127 if (link->static_ipv6_proxy_ndp_messages == 0) {
128 link->static_ipv6_proxy_ndp_configured = true;
129 link_check_ready(link);
130 } else {
131 log_link_debug(link, "Setting IPv6 proxy NDP addresses.");
132 link_set_state(link, LINK_STATE_CONFIGURING);
133 }
134
135 return 0;
136 }
137
138 int config_parse_ipv6_proxy_ndp_address(
139 const char *unit,
140 const char *filename,
141 unsigned line,
142 const char *section,
143 unsigned section_line,
144 const char *lvalue,
145 int ltype,
146 const char *rvalue,
147 void *data,
148 void *userdata) {
149
150 _cleanup_free_ struct in6_addr *address = NULL;
151 Network *network = ASSERT_PTR(userdata);
152 union in_addr_union buffer;
153 int r;
154
155 assert(filename);
156 assert(rvalue);
157
158 if (isempty(rvalue)) {
159 network->ipv6_proxy_ndp_addresses = set_free(network->ipv6_proxy_ndp_addresses);
160 return 0;
161 }
162
163 r = in_addr_from_string(AF_INET6, rvalue, &buffer);
164 if (r < 0) {
165 log_syntax(unit, LOG_WARNING, filename, line, r,
166 "Failed to parse IPv6 proxy NDP address, ignoring: %s", rvalue);
167 return 0;
168 }
169
170 if (in_addr_is_null(AF_INET6, &buffer)) {
171 log_syntax(unit, LOG_WARNING, filename, line, 0,
172 "IPv6 proxy NDP address cannot be the ANY address, ignoring: %s", rvalue);
173 return 0;
174 }
175
176 address = newdup(struct in6_addr, &buffer.in6, 1);
177 if (!address)
178 return log_oom();
179
180 r = set_ensure_consume(&network->ipv6_proxy_ndp_addresses, &in6_addr_hash_ops_free, TAKE_PTR(address));
181 if (r < 0)
182 return log_oom();
183
184 return 0;
185 }