]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-fdb.c
networkd: bridge fdb add support to configure VXLAN VNI
[thirdparty/systemd.git] / src / network / networkd-fdb.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2014 Intel Corporation. All rights reserved.
4 ***/
5
6 #include <net/ethernet.h>
7 #include <net/if.h>
8
9 #include "alloc-util.h"
10 #include "conf-parser.h"
11 #include "netdev/bridge.h"
12 #include "netdev/vxlan.h"
13 #include "netlink-util.h"
14 #include "networkd-fdb.h"
15 #include "networkd-manager.h"
16 #include "parse-util.h"
17 #include "string-util.h"
18 #include "util.h"
19 #include "vlan-util.h"
20
21 #define STATIC_FDB_ENTRIES_PER_NETWORK_MAX 1024U
22
23 /* create a new FDB entry or get an existing one. */
24 static int fdb_entry_new_static(
25 Network *network,
26 const char *filename,
27 unsigned section_line,
28 FdbEntry **ret) {
29
30 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
31 _cleanup_(fdb_entry_freep) FdbEntry *fdb_entry = NULL;
32 _cleanup_free_ struct ether_addr *mac_addr = NULL;
33 int r;
34
35 assert(network);
36 assert(ret);
37 assert(!!filename == (section_line > 0));
38
39 /* search entry in hashmap first. */
40 if (filename) {
41 r = network_config_section_new(filename, section_line, &n);
42 if (r < 0)
43 return r;
44
45 fdb_entry = hashmap_get(network->fdb_entries_by_section, n);
46 if (fdb_entry) {
47 *ret = TAKE_PTR(fdb_entry);
48
49 return 0;
50 }
51 }
52
53 if (network->n_static_fdb_entries >= STATIC_FDB_ENTRIES_PER_NETWORK_MAX)
54 return -E2BIG;
55
56 /* allocate space for MAC address. */
57 mac_addr = new0(struct ether_addr, 1);
58 if (!mac_addr)
59 return -ENOMEM;
60
61 /* allocate space for and FDB entry. */
62 fdb_entry = new(FdbEntry, 1);
63 if (!fdb_entry)
64 return -ENOMEM;
65
66 /* init FDB structure. */
67 *fdb_entry = (FdbEntry) {
68 .network = network,
69 .mac_addr = TAKE_PTR(mac_addr),
70 .vni = VXLAN_VID_MAX + 1,
71 };
72
73 LIST_PREPEND(static_fdb_entries, network->static_fdb_entries, fdb_entry);
74 network->n_static_fdb_entries++;
75
76 if (filename) {
77 fdb_entry->section = TAKE_PTR(n);
78
79 r = hashmap_ensure_allocated(&network->fdb_entries_by_section, &network_config_hash_ops);
80 if (r < 0)
81 return r;
82
83 r = hashmap_put(network->fdb_entries_by_section, fdb_entry->section, fdb_entry);
84 if (r < 0)
85 return r;
86 }
87
88 /* return allocated FDB structure. */
89 *ret = TAKE_PTR(fdb_entry);
90
91 return 0;
92 }
93
94 static int set_fdb_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
95 int r;
96
97 assert(link);
98
99 r = sd_netlink_message_get_errno(m);
100 if (r < 0 && r != -EEXIST)
101 log_link_error_errno(link, r, "Could not add FDB entry: %m");
102
103 return 1;
104 }
105
106 /* send a request to the kernel to add a FDB entry in its static MAC table. */
107 int fdb_entry_configure(Link *link, FdbEntry *fdb_entry) {
108 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
109 sd_netlink *rtnl;
110 Bridge *bridge;
111 uint8_t flags;
112 int r;
113
114 assert(link);
115 assert(link->network);
116 assert(link->manager);
117 assert(fdb_entry);
118
119 rtnl = link->manager->rtnl;
120 bridge = BRIDGE(link->network->bridge);
121
122 /* create new RTM message */
123 r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_NEWNEIGH, link->ifindex, PF_BRIDGE);
124 if (r < 0)
125 return rtnl_log_create_error(r);
126
127 if (bridge)
128 flags = NTF_MASTER;
129 else
130 flags = NTF_SELF;
131
132 r = sd_rtnl_message_neigh_set_flags(req, flags);
133 if (r < 0)
134 return rtnl_log_create_error(r);
135
136 /* only NUD_PERMANENT state supported. */
137 r = sd_rtnl_message_neigh_set_state(req, NUD_NOARP | NUD_PERMANENT);
138 if (r < 0)
139 return rtnl_log_create_error(r);
140
141 r = sd_netlink_message_append_ether_addr(req, NDA_LLADDR, fdb_entry->mac_addr);
142 if (r < 0)
143 return rtnl_log_create_error(r);
144
145 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
146 if (fdb_entry->vlan_id > 0) {
147 r = sd_netlink_message_append_u16(req, NDA_VLAN, fdb_entry->vlan_id);
148 if (r < 0)
149 return rtnl_log_create_error(r);
150 }
151
152 if (!in_addr_is_null(fdb_entry->family, &fdb_entry->destination_addr)) {
153 r = netlink_message_append_in_addr_union(req, NDA_DST, fdb_entry->family, &fdb_entry->destination_addr);
154 if (r < 0)
155 return log_link_error_errno(link, r, "Could not append NDA_DST attribute: %m");
156 }
157
158 if (fdb_entry->vni <= VXLAN_VID_MAX) {
159 r = sd_netlink_message_append_u32(req, NDA_VNI, fdb_entry->vni);
160 if (r < 0)
161 return log_link_error_errno(link, r, "Could not append NDA_VNI attribute: %m");
162 }
163
164 /* send message to the kernel to update its internal static MAC table. */
165 r = netlink_call_async(rtnl, NULL, req, set_fdb_handler,
166 link_netlink_destroy_callback, link);
167 if (r < 0)
168 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
169
170 link_ref(link);
171
172 return 0;
173 }
174
175 /* remove and FDB entry. */
176 void fdb_entry_free(FdbEntry *fdb_entry) {
177 if (!fdb_entry)
178 return;
179
180 if (fdb_entry->network) {
181 LIST_REMOVE(static_fdb_entries, fdb_entry->network->static_fdb_entries, fdb_entry);
182 assert(fdb_entry->network->n_static_fdb_entries > 0);
183 fdb_entry->network->n_static_fdb_entries--;
184
185 if (fdb_entry->section)
186 hashmap_remove(fdb_entry->network->fdb_entries_by_section, fdb_entry->section);
187 }
188
189 network_config_section_free(fdb_entry->section);
190 free(fdb_entry->mac_addr);
191 free(fdb_entry);
192 }
193
194 /* parse the HW address from config files. */
195 int config_parse_fdb_hwaddr(
196 const char *unit,
197 const char *filename,
198 unsigned line,
199 const char *section,
200 unsigned section_line,
201 const char *lvalue,
202 int ltype,
203 const char *rvalue,
204 void *data,
205 void *userdata) {
206
207 Network *network = userdata;
208 _cleanup_(fdb_entry_free_or_set_invalidp) FdbEntry *fdb_entry = NULL;
209 int r;
210
211 assert(filename);
212 assert(section);
213 assert(lvalue);
214 assert(rvalue);
215 assert(data);
216
217 r = fdb_entry_new_static(network, filename, section_line, &fdb_entry);
218 if (r < 0)
219 return log_oom();
220
221 /* read in the MAC address for the FDB table. */
222 r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
223 &fdb_entry->mac_addr->ether_addr_octet[0],
224 &fdb_entry->mac_addr->ether_addr_octet[1],
225 &fdb_entry->mac_addr->ether_addr_octet[2],
226 &fdb_entry->mac_addr->ether_addr_octet[3],
227 &fdb_entry->mac_addr->ether_addr_octet[4],
228 &fdb_entry->mac_addr->ether_addr_octet[5]);
229
230 if (r != ETHER_ADDR_LEN) {
231 log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue);
232 return 0;
233 }
234
235 fdb_entry = NULL;
236
237 return 0;
238 }
239
240 /* parse the VLAN Id from config files. */
241 int config_parse_fdb_vlan_id(
242 const char *unit,
243 const char *filename,
244 unsigned line,
245 const char *section,
246 unsigned section_line,
247 const char *lvalue,
248 int ltype,
249 const char *rvalue,
250 void *data,
251 void *userdata) {
252
253 Network *network = userdata;
254 _cleanup_(fdb_entry_free_or_set_invalidp) FdbEntry *fdb_entry = NULL;
255 int r;
256
257 assert(filename);
258 assert(section);
259 assert(lvalue);
260 assert(rvalue);
261 assert(data);
262
263 r = fdb_entry_new_static(network, filename, section_line, &fdb_entry);
264 if (r < 0)
265 return log_oom();
266
267 r = config_parse_vlanid(unit, filename, line, section,
268 section_line, lvalue, ltype,
269 rvalue, &fdb_entry->vlan_id, userdata);
270 if (r < 0)
271 return r;
272
273 fdb_entry = NULL;
274
275 return 0;
276 }
277
278 int config_parse_fdb_destination(
279 const char *unit,
280 const char *filename,
281 unsigned line,
282 const char *section,
283 unsigned section_line,
284 const char *lvalue,
285 int ltype,
286 const char *rvalue,
287 void *data,
288 void *userdata) {
289
290 _cleanup_(fdb_entry_free_or_set_invalidp) FdbEntry *fdb_entry = NULL;
291 Network *network = userdata;
292 int r;
293
294 assert(filename);
295 assert(section);
296 assert(lvalue);
297 assert(rvalue);
298 assert(data);
299
300 r = fdb_entry_new_static(network, filename, section_line, &fdb_entry);
301 if (r < 0)
302 return log_oom();
303
304 r = in_addr_from_string_auto(rvalue, &fdb_entry->family, &fdb_entry->destination_addr);
305 if (r < 0)
306 return log_syntax(unit, LOG_ERR, filename, line, r,
307 "FDB destination IP address is invalid, ignoring assignment: %s",
308 rvalue);
309
310 fdb_entry = NULL;
311
312 return 0;
313 }
314
315 int config_parse_fdb_vxlan_vni(
316 const char *unit,
317 const char *filename,
318 unsigned line,
319 const char *section,
320 unsigned section_line,
321 const char *lvalue,
322 int ltype,
323 const char *rvalue,
324 void *data,
325 void *userdata) {
326
327 _cleanup_(fdb_entry_free_or_set_invalidp) FdbEntry *fdb_entry = NULL;
328 Network *network = userdata;
329 uint32_t vni;
330 int r;
331
332 assert(filename);
333 assert(section);
334 assert(lvalue);
335 assert(rvalue);
336 assert(data);
337
338 r = fdb_entry_new_static(network, filename, section_line, &fdb_entry);
339 if (r < 0)
340 return log_oom();
341
342 r = safe_atou32(rvalue, &vni);
343 if (r < 0) {
344 log_syntax(unit, LOG_ERR, filename, line, r,
345 "Failed to parse VXLAN Network Identifier (VNI), ignoring assignment: %s",
346 rvalue);
347 return 0;
348 }
349
350 if (vni > VXLAN_VID_MAX) {
351 log_syntax(unit, LOG_ERR, filename, line, 0,
352 "FDB invalid VXLAN Network Identifier (VNI), ignoring assignment: %s",
353 rvalue);
354 return 0;
355 }
356
357 fdb_entry->vni = vni;
358 fdb_entry = NULL;
359
360 return 0;
361 }