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