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