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