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