]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/netdev/vxlan.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / network / netdev / vxlan.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <net/if.h>
4
5 #include "sd-netlink.h"
6
7 #include "conf-parser.h"
8 #include "alloc-util.h"
9 #include "extract-word.h"
10 #include "string-util.h"
11 #include "strv.h"
12 #include "parse-util.h"
13 #include "missing.h"
14
15 #include "networkd-link.h"
16 #include "netdev/vxlan.h"
17
18 static int netdev_vxlan_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
19 VxLan *v;
20 int r;
21
22 assert(netdev);
23 assert(link);
24 assert(m);
25
26 v = VXLAN(netdev);
27
28 assert(v);
29
30 if (v->id <= VXLAN_VID_MAX) {
31 r = sd_netlink_message_append_u32(m, IFLA_VXLAN_ID, v->id);
32 if (r < 0)
33 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_ID attribute: %m");
34 }
35
36 if (in_addr_is_null(v->remote_family, &v->remote) == 0) {
37 if (v->remote_family == AF_INET)
38 r = sd_netlink_message_append_in_addr(m, IFLA_VXLAN_GROUP, &v->remote.in);
39 else
40 r = sd_netlink_message_append_in6_addr(m, IFLA_VXLAN_GROUP6, &v->remote.in6);
41 if (r < 0)
42 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_GROUP attribute: %m");
43 }
44
45 if (in_addr_is_null(v->local_family, &v->local) == 0) {
46 if (v->local_family == AF_INET)
47 r = sd_netlink_message_append_in_addr(m, IFLA_VXLAN_LOCAL, &v->local.in);
48 else
49 r = sd_netlink_message_append_in6_addr(m, IFLA_VXLAN_LOCAL6, &v->local.in6);
50 if (r < 0)
51 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_LOCAL attribute: %m");
52 }
53
54 r = sd_netlink_message_append_u32(m, IFLA_VXLAN_LINK, link->ifindex);
55 if (r < 0)
56 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_LINK attribute: %m");
57
58 if (v->ttl != 0) {
59 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_TTL, v->ttl);
60 if (r < 0)
61 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_TTL attribute: %m");
62 }
63
64 if (v->tos != 0) {
65 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_TOS, v->tos);
66 if (r < 0)
67 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_TOS attribute: %m");
68 }
69
70 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_LEARNING, v->learning);
71 if (r < 0)
72 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_LEARNING attribute: %m");
73
74 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_RSC, v->route_short_circuit);
75 if (r < 0)
76 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_RSC attribute: %m");
77
78 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_PROXY, v->arp_proxy);
79 if (r < 0)
80 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_PROXY attribute: %m");
81
82 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_L2MISS, v->l2miss);
83 if (r < 0)
84 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_L2MISS attribute: %m");
85
86 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_L3MISS, v->l3miss);
87 if (r < 0)
88 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_L3MISS attribute: %m");
89
90 if (v->fdb_ageing != 0) {
91 r = sd_netlink_message_append_u32(m, IFLA_VXLAN_AGEING, v->fdb_ageing / USEC_PER_SEC);
92 if (r < 0)
93 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_AGEING attribute: %m");
94 }
95
96 if (v->max_fdb != 0) {
97 r = sd_netlink_message_append_u32(m, IFLA_VXLAN_LIMIT, v->max_fdb);
98 if (r < 0)
99 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_LIMIT attribute: %m");
100 }
101
102 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_UDP_CSUM, v->udpcsum);
103 if (r < 0)
104 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_UDP_CSUM attribute: %m");
105
106 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, v->udp6zerocsumtx);
107 if (r < 0)
108 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_UDP_ZERO_CSUM6_TX attribute: %m");
109
110 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, v->udp6zerocsumrx);
111 if (r < 0)
112 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_UDP_ZERO_CSUM6_RX attribute: %m");
113
114 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_REMCSUM_TX, v->remote_csum_tx);
115 if (r < 0)
116 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_REMCSUM_TX attribute: %m");
117
118 r = sd_netlink_message_append_u8(m, IFLA_VXLAN_REMCSUM_RX, v->remote_csum_rx);
119 if (r < 0)
120 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_REMCSUM_RX attribute: %m");
121
122 r = sd_netlink_message_append_u16(m, IFLA_VXLAN_PORT, htobe16(v->dest_port));
123 if (r < 0)
124 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_PORT attribute: %m");
125
126 if (v->port_range.low != 0 || v->port_range.high != 0) {
127 struct ifla_vxlan_port_range port_range;
128
129 port_range.low = htobe16(v->port_range.low);
130 port_range.high = htobe16(v->port_range.high);
131
132 r = sd_netlink_message_append_data(m, IFLA_VXLAN_PORT_RANGE, &port_range, sizeof(port_range));
133 if (r < 0)
134 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_PORT_RANGE attribute: %m");
135 }
136
137 r = sd_netlink_message_append_u32(m, IFLA_VXLAN_LABEL, htobe32(v->flow_label));
138 if (r < 0)
139 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_LABEL attribute: %m");
140
141 if (v->group_policy) {
142 r = sd_netlink_message_append_flag(m, IFLA_VXLAN_GBP);
143 if (r < 0)
144 return log_netdev_error_errno(netdev, r, "Could not append IFLA_VXLAN_GBP attribute: %m");
145 }
146
147 return r;
148 }
149
150 int config_parse_vxlan_address(const char *unit,
151 const char *filename,
152 unsigned line,
153 const char *section,
154 unsigned section_line,
155 const char *lvalue,
156 int ltype,
157 const char *rvalue,
158 void *data,
159 void *userdata) {
160 VxLan *v = userdata;
161 union in_addr_union *addr = data, buffer;
162 int r, f;
163
164 assert(filename);
165 assert(lvalue);
166 assert(rvalue);
167 assert(data);
168
169 r = in_addr_from_string_auto(rvalue, &f, &buffer);
170 if (r < 0) {
171 log_syntax(unit, LOG_ERR, filename, line, r, "vxlan '%s' address is invalid, ignoring assignment: %s", lvalue, rvalue);
172 return 0;
173 }
174
175 r = in_addr_is_multicast(f, &buffer);
176
177 if (streq(lvalue, "Group")) {
178 if (r <= 0) {
179 log_syntax(unit, LOG_ERR, filename, line, 0, "vxlan %s invalid multicast address, ignoring assignment: %s", lvalue, rvalue);
180 return 0;
181 }
182
183 v->remote_family = f;
184 } else {
185 if (r > 0) {
186 log_syntax(unit, LOG_ERR, filename, line, 0, "vxlan %s cannot be a multicast address, ignoring assignment: %s", lvalue, rvalue);
187 return 0;
188 }
189
190 if (streq(lvalue, "Remote"))
191 v->remote_family = f;
192 else
193 v->local_family = f;
194 }
195
196 *addr = buffer;
197
198 return 0;
199 }
200
201 int config_parse_port_range(const char *unit,
202 const char *filename,
203 unsigned line,
204 const char *section,
205 unsigned section_line,
206 const char *lvalue,
207 int ltype,
208 const char *rvalue,
209 void *data,
210 void *userdata) {
211 VxLan *v = userdata;
212 uint16_t low, high;
213 int r;
214
215 assert(filename);
216 assert(lvalue);
217 assert(rvalue);
218 assert(data);
219
220 r = parse_ip_port_range(rvalue, &low, &high);
221 if (r < 0) {
222 log_syntax(unit, LOG_ERR, filename, line, r,
223 "Failed to parse VXLAN port range '%s'. Port should be greater than 0 and less than 65535.", rvalue);
224 return 0;
225 }
226
227 v->port_range.low = low;
228 v->port_range.high = high;
229
230 return 0;
231 }
232
233 int config_parse_flow_label(const char *unit,
234 const char *filename,
235 unsigned line,
236 const char *section,
237 unsigned section_line,
238 const char *lvalue,
239 int ltype,
240 const char *rvalue,
241 void *data,
242 void *userdata) {
243 VxLan *v = userdata;
244 unsigned f;
245 int r;
246
247 assert(filename);
248 assert(lvalue);
249 assert(rvalue);
250 assert(data);
251
252 r = safe_atou(rvalue, &f);
253 if (r < 0) {
254 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse VXLAN flow label '%s'.", rvalue);
255 return 0;
256 }
257
258 if (f & ~VXLAN_FLOW_LABEL_MAX_MASK) {
259 log_syntax(unit, LOG_ERR, filename, line, r,
260 "VXLAN flow label '%s' not valid. Flow label range should be [0-1048575].", rvalue);
261 return 0;
262 }
263
264 v->flow_label = f;
265
266 return 0;
267 }
268
269 static int netdev_vxlan_verify(NetDev *netdev, const char *filename) {
270 VxLan *v = VXLAN(netdev);
271
272 assert(netdev);
273 assert(v);
274 assert(filename);
275
276 if (v->id > VXLAN_VID_MAX) {
277 log_warning("VXLAN without valid Id configured in %s. Ignoring", filename);
278 return -EINVAL;
279 }
280
281 return 0;
282 }
283
284 static void vxlan_init(NetDev *netdev) {
285 VxLan *v;
286
287 assert(netdev);
288
289 v = VXLAN(netdev);
290
291 assert(v);
292
293 v->id = VXLAN_VID_MAX + 1;
294 v->learning = true;
295 v->udpcsum = false;
296 v->udp6zerocsumtx = false;
297 v->udp6zerocsumrx = false;
298 }
299
300 const NetDevVTable vxlan_vtable = {
301 .object_size = sizeof(VxLan),
302 .init = vxlan_init,
303 .sections = "Match\0NetDev\0VXLAN\0",
304 .fill_message_create = netdev_vxlan_fill_message_create,
305 .create_type = NETDEV_CREATE_STACKED,
306 .config_verify = netdev_vxlan_verify,
307 };