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