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