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