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