]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-fdb.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / network / networkd-fdb.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright (C) 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 "netlink-util.h"
13 #include "networkd-fdb.h"
14 #include "networkd-manager.h"
15 #include "util.h"
16 #include "vlan-util.h"
17
18 #define STATIC_FDB_ENTRIES_PER_NETWORK_MAX 1024U
19
20 /* create a new FDB entry or get an existing one. */
21 int fdb_entry_new_static(
22 Network *network,
23 unsigned section,
24 FdbEntry **ret) {
25
26 _cleanup_(fdb_entry_freep) FdbEntry *fdb_entry = NULL;
27 struct ether_addr *mac_addr = NULL;
28
29 assert(network);
30 assert(ret);
31
32 /* search entry in hashmap first. */
33 if (section) {
34 fdb_entry = hashmap_get(network->fdb_entries_by_section, UINT_TO_PTR(section));
35 if (fdb_entry) {
36 *ret = TAKE_PTR(fdb_entry);
37
38 return 0;
39 }
40 }
41
42 if (network->n_static_fdb_entries >= STATIC_FDB_ENTRIES_PER_NETWORK_MAX)
43 return -E2BIG;
44
45 /* allocate space for MAC address. */
46 mac_addr = new0(struct ether_addr, 1);
47 if (!mac_addr)
48 return -ENOMEM;
49
50 /* allocate space for and FDB entry. */
51 fdb_entry = new0(FdbEntry, 1);
52 if (!fdb_entry) {
53 /* free previously allocated space for mac_addr. */
54 free(mac_addr);
55 return -ENOMEM;
56 }
57
58 /* init FDB structure. */
59 fdb_entry->network = network;
60 fdb_entry->mac_addr = mac_addr;
61
62 LIST_PREPEND(static_fdb_entries, network->static_fdb_entries, fdb_entry);
63 network->n_static_fdb_entries++;
64
65 if (section) {
66 fdb_entry->section = section;
67 hashmap_put(network->fdb_entries_by_section,
68 UINT_TO_PTR(fdb_entry->section), fdb_entry);
69 }
70
71 /* return allocated FDB structure. */
72 *ret = TAKE_PTR(fdb_entry);
73
74 return 0;
75 }
76
77 static int set_fdb_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
78 Link *link = userdata;
79 int r;
80
81 assert(link);
82
83 r = sd_netlink_message_get_errno(m);
84 if (r < 0 && r != -EEXIST)
85 log_link_error_errno(link, r, "Could not add FDB entry: %m");
86
87 return 1;
88 }
89
90 /* send a request to the kernel to add a FDB entry in its static MAC table. */
91 int fdb_entry_configure(Link *link, FdbEntry *fdb_entry) {
92 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
93 sd_netlink *rtnl;
94 int r;
95 uint8_t flags;
96 Bridge *bridge;
97
98 assert(link);
99 assert(link->network);
100 assert(link->manager);
101 assert(fdb_entry);
102
103 rtnl = link->manager->rtnl;
104 bridge = BRIDGE(link->network->bridge);
105
106 /* create new RTM message */
107 r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_NEWNEIGH, link->ifindex, PF_BRIDGE);
108 if (r < 0)
109 return rtnl_log_create_error(r);
110
111 if (bridge)
112 flags = NTF_MASTER;
113 else
114 flags = NTF_SELF;
115
116 r = sd_rtnl_message_neigh_set_flags(req, flags);
117 if (r < 0)
118 return rtnl_log_create_error(r);
119
120 /* only NUD_PERMANENT state supported. */
121 r = sd_rtnl_message_neigh_set_state(req, NUD_NOARP | NUD_PERMANENT);
122 if (r < 0)
123 return rtnl_log_create_error(r);
124
125 r = sd_netlink_message_append_ether_addr(req, NDA_LLADDR, fdb_entry->mac_addr);
126 if (r < 0)
127 return rtnl_log_create_error(r);
128
129 /* VLAN Id is optional. We'll add VLAN Id only if it's specified. */
130 if (0 != fdb_entry->vlan_id) {
131 r = sd_netlink_message_append_u16(req, NDA_VLAN, fdb_entry->vlan_id);
132 if (r < 0)
133 return rtnl_log_create_error(r);
134 }
135
136 /* send message to the kernel to update its internal static MAC table. */
137 r = sd_netlink_call_async(rtnl, req, set_fdb_handler, link, 0, NULL);
138 if (r < 0)
139 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
140
141 return 0;
142 }
143
144 /* remove and FDB entry. */
145 void fdb_entry_free(FdbEntry *fdb_entry) {
146 if (!fdb_entry)
147 return;
148
149 if (fdb_entry->network) {
150 LIST_REMOVE(static_fdb_entries, fdb_entry->network->static_fdb_entries, fdb_entry);
151
152 assert(fdb_entry->network->n_static_fdb_entries > 0);
153 fdb_entry->network->n_static_fdb_entries--;
154
155 if (fdb_entry->section)
156 hashmap_remove(fdb_entry->network->fdb_entries_by_section, UINT_TO_PTR(fdb_entry->section));
157 }
158
159 free(fdb_entry->mac_addr);
160
161 free(fdb_entry);
162 }
163
164 /* parse the HW address from config files. */
165 int config_parse_fdb_hwaddr(
166 const char *unit,
167 const char *filename,
168 unsigned line,
169 const char *section,
170 unsigned section_line,
171 const char *lvalue,
172 int ltype,
173 const char *rvalue,
174 void *data,
175 void *userdata) {
176
177 Network *network = userdata;
178 _cleanup_(fdb_entry_freep) FdbEntry *fdb_entry = NULL;
179 int r;
180
181 assert(filename);
182 assert(section);
183 assert(lvalue);
184 assert(rvalue);
185 assert(data);
186
187 r = fdb_entry_new_static(network, section_line, &fdb_entry);
188 if (r < 0)
189 return log_oom();
190
191 /* read in the MAC address for the FDB table. */
192 r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
193 &fdb_entry->mac_addr->ether_addr_octet[0],
194 &fdb_entry->mac_addr->ether_addr_octet[1],
195 &fdb_entry->mac_addr->ether_addr_octet[2],
196 &fdb_entry->mac_addr->ether_addr_octet[3],
197 &fdb_entry->mac_addr->ether_addr_octet[4],
198 &fdb_entry->mac_addr->ether_addr_octet[5]);
199
200 if (ETHER_ADDR_LEN != r) {
201 log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue);
202 return 0;
203 }
204
205 fdb_entry = NULL;
206
207 return 0;
208 }
209
210 /* parse the VLAN Id from config files. */
211 int config_parse_fdb_vlan_id(
212 const char *unit,
213 const char *filename,
214 unsigned line,
215 const char *section,
216 unsigned section_line,
217 const char *lvalue,
218 int ltype,
219 const char *rvalue,
220 void *data,
221 void *userdata) {
222
223 Network *network = userdata;
224 _cleanup_(fdb_entry_freep) FdbEntry *fdb_entry = NULL;
225 int r;
226
227 assert(filename);
228 assert(section);
229 assert(lvalue);
230 assert(rvalue);
231 assert(data);
232
233 r = fdb_entry_new_static(network, section_line, &fdb_entry);
234 if (r < 0)
235 return log_oom();
236
237 r = config_parse_vlanid(unit, filename, line, section,
238 section_line, lvalue, ltype,
239 rvalue, &fdb_entry->vlan_id, userdata);
240 if (r < 0)
241 return r;
242
243 fdb_entry = NULL;
244
245 return 0;
246 }