4ae511fc7aeb3f9a3f4bfe5148d0af5d69f1c512
[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 "string-table.h"
19 #include "util.h"
20 #include "vlan-util.h"
21
22 #define STATIC_FDB_ENTRIES_PER_NETWORK_MAX 1024U
23
24 static const char* const fdb_ntf_flags_table[_NEIGHBOR_CACHE_ENTRY_FLAGS_MAX] = {
25         [NEIGHBOR_CACHE_ENTRY_FLAGS_USE] = "use",
26         [NEIGHBOR_CACHE_ENTRY_FLAGS_SELF] = "self",
27         [NEIGHBOR_CACHE_ENTRY_FLAGS_MASTER] = "master",
28         [NEIGHBOR_CACHE_ENTRY_FLAGS_ROUTER] = "router",
29 };
30
31 DEFINE_STRING_TABLE_LOOKUP(fdb_ntf_flags, NeighborCacheEntryFlags);
32
33 /* create a new FDB entry or get an existing one. */
34 static int fdb_entry_new_static(
35                 Network *network,
36                 const char *filename,
37                 unsigned section_line,
38                 FdbEntry **ret) {
39
40         _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
41         _cleanup_(fdb_entry_freep) FdbEntry *fdb_entry = NULL;
42         _cleanup_free_ struct ether_addr *mac_addr = NULL;
43         int r;
44
45         assert(network);
46         assert(ret);
47         assert(!!filename == (section_line > 0));
48
49         /* search entry in hashmap first. */
50         if (filename) {
51                 r = network_config_section_new(filename, section_line, &n);
52                 if (r < 0)
53                         return r;
54
55                 fdb_entry = hashmap_get(network->fdb_entries_by_section, n);
56                 if (fdb_entry) {
57                         *ret = TAKE_PTR(fdb_entry);
58
59                         return 0;
60                 }
61         }
62
63         if (network->n_static_fdb_entries >= STATIC_FDB_ENTRIES_PER_NETWORK_MAX)
64                 return -E2BIG;
65
66         /* allocate space for MAC address. */
67         mac_addr = new0(struct ether_addr, 1);
68         if (!mac_addr)
69                 return -ENOMEM;
70
71         /* allocate space for and FDB entry. */
72         fdb_entry = new(FdbEntry, 1);
73         if (!fdb_entry)
74                 return -ENOMEM;
75
76         /* init FDB structure. */
77         *fdb_entry = (FdbEntry) {
78                 .network = network,
79                 .mac_addr = TAKE_PTR(mac_addr),
80                 .vni = VXLAN_VID_MAX + 1,
81                 .fdb_ntf_flags = NEIGHBOR_CACHE_ENTRY_FLAGS_SELF,
82         };
83
84         LIST_PREPEND(static_fdb_entries, network->static_fdb_entries, fdb_entry);
85         network->n_static_fdb_entries++;
86
87         if (filename) {
88                 fdb_entry->section = TAKE_PTR(n);
89
90                 r = hashmap_ensure_allocated(&network->fdb_entries_by_section, &network_config_hash_ops);
91                 if (r < 0)
92                         return r;
93
94                 r = hashmap_put(network->fdb_entries_by_section, fdb_entry->section, fdb_entry);
95                 if (r < 0)
96                         return r;
97         }
98
99         /* return allocated FDB structure. */
100         *ret = TAKE_PTR(fdb_entry);
101
102         return 0;
103 }
104
105 static int set_fdb_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
106         int r;
107
108         assert(link);
109
110         r = sd_netlink_message_get_errno(m);
111         if (r < 0 && r != -EEXIST)
112                 log_link_error_errno(link, r, "Could not add FDB entry: %m");
113
114         return 1;
115 }
116
117 /* send a request to the kernel to add a FDB entry in its static MAC table. */
118 int fdb_entry_configure(Link *link, FdbEntry *fdb_entry) {
119         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
120         int r;
121
122         assert(link);
123         assert(link->network);
124         assert(link->manager);
125         assert(fdb_entry);
126
127         /* create new RTM message */
128         r = sd_rtnl_message_new_neigh(link->manager->rtnl, &req, RTM_NEWNEIGH, link->ifindex, PF_BRIDGE);
129         if (r < 0)
130                 return rtnl_log_create_error(r);
131
132         r = sd_rtnl_message_neigh_set_flags(req, fdb_entry->fdb_ntf_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(link->manager->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 }
362
363
364 int config_parse_fdb_ntf_flags(
365                 const char *unit,
366                 const char *filename,
367                 unsigned line,
368                 const char *section,
369                 unsigned section_line,
370                 const char *lvalue,
371                 int ltype,
372                 const char *rvalue,
373                 void *data,
374                 void *userdata) {
375
376         _cleanup_(fdb_entry_free_or_set_invalidp) FdbEntry *fdb_entry = NULL;
377         Network *network = userdata;
378         NeighborCacheEntryFlags f;
379         int r;
380
381         assert(filename);
382         assert(section);
383         assert(lvalue);
384         assert(rvalue);
385         assert(data);
386
387         r = fdb_entry_new_static(network, filename, section_line, &fdb_entry);
388         if (r < 0)
389                 return log_oom();
390
391         f = fdb_ntf_flags_from_string(rvalue);
392         if (f < 0) {
393                 log_syntax(unit, LOG_ERR, filename, line, 0,
394                            "FDB failed to parse AssociatedWith=, ignoring assignment: %s",
395                            rvalue);
396                 return 0;
397         }
398
399         fdb_entry->fdb_ntf_flags = f;
400         fdb_entry = NULL;
401
402         return 0;
403 }