]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
b98b483b | 2 | /*** |
810adae9 | 3 | Copyright © 2014 Intel Corporation. All rights reserved. |
b98b483b AR |
4 | ***/ |
5 | ||
b98b483b | 6 | #include <net/ethernet.h> |
cf0fbc49 | 7 | #include <net/if.h> |
b98b483b | 8 | |
b5efdb8a | 9 | #include "alloc-util.h" |
b98b483b | 10 | #include "conf-parser.h" |
23f53b99 | 11 | #include "netdev/bridge.h" |
61b824c5 | 12 | #include "netdev/vxlan.h" |
fc2f9534 | 13 | #include "netlink-util.h" |
fc2f9534 | 14 | #include "networkd-fdb.h" |
23f53b99 | 15 | #include "networkd-manager.h" |
61b824c5 SS |
16 | #include "parse-util.h" |
17 | #include "string-util.h" | |
b5efdb8a | 18 | #include "util.h" |
0e83e7a5 | 19 | #include "vlan-util.h" |
b98b483b | 20 | |
8c34b963 LP |
21 | #define STATIC_FDB_ENTRIES_PER_NETWORK_MAX 1024U |
22 | ||
b98b483b | 23 | /* create a new FDB entry or get an existing one. */ |
9560e5b3 | 24 | static int fdb_entry_new_static( |
8c34b963 | 25 | Network *network, |
1a75764a YW |
26 | const char *filename, |
27 | unsigned section_line, | |
8c34b963 LP |
28 | FdbEntry **ret) { |
29 | ||
1a75764a | 30 | _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL; |
8e766630 | 31 | _cleanup_(fdb_entry_freep) FdbEntry *fdb_entry = NULL; |
1a75764a YW |
32 | _cleanup_free_ struct ether_addr *mac_addr = NULL; |
33 | int r; | |
b98b483b AR |
34 | |
35 | assert(network); | |
8c34b963 | 36 | assert(ret); |
1a75764a | 37 | assert(!!filename == (section_line > 0)); |
b98b483b AR |
38 | |
39 | /* search entry in hashmap first. */ | |
1a75764a YW |
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); | |
b98b483b | 46 | if (fdb_entry) { |
1cc6c93a | 47 | *ret = TAKE_PTR(fdb_entry); |
b98b483b AR |
48 | |
49 | return 0; | |
50 | } | |
51 | } | |
52 | ||
8c34b963 LP |
53 | if (network->n_static_fdb_entries >= STATIC_FDB_ENTRIES_PER_NETWORK_MAX) |
54 | return -E2BIG; | |
55 | ||
b98b483b AR |
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. */ | |
1a75764a YW |
62 | fdb_entry = new(FdbEntry, 1); |
63 | if (!fdb_entry) | |
b98b483b | 64 | return -ENOMEM; |
b98b483b AR |
65 | |
66 | /* init FDB structure. */ | |
1a75764a YW |
67 | *fdb_entry = (FdbEntry) { |
68 | .network = network, | |
69 | .mac_addr = TAKE_PTR(mac_addr), | |
61b824c5 | 70 | .vni = VXLAN_VID_MAX + 1, |
1a75764a | 71 | }; |
b98b483b AR |
72 | |
73 | LIST_PREPEND(static_fdb_entries, network->static_fdb_entries, fdb_entry); | |
8c34b963 | 74 | network->n_static_fdb_entries++; |
b98b483b | 75 | |
1a75764a YW |
76 | if (filename) { |
77 | fdb_entry->section = TAKE_PTR(n); | |
78 | ||
3e570042 YW |
79 | r = hashmap_ensure_allocated(&network->fdb_entries_by_section, &network_config_hash_ops); |
80 | if (r < 0) | |
81 | return r; | |
82 | ||
1a75764a YW |
83 | r = hashmap_put(network->fdb_entries_by_section, fdb_entry->section, fdb_entry); |
84 | if (r < 0) | |
85 | return r; | |
b98b483b AR |
86 | } |
87 | ||
88 | /* return allocated FDB structure. */ | |
1cc6c93a | 89 | *ret = TAKE_PTR(fdb_entry); |
b98b483b AR |
90 | |
91 | return 0; | |
92 | } | |
93 | ||
302a796f | 94 | static int set_fdb_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
b98b483b AR |
95 | int r; |
96 | ||
ea6ec096 | 97 | assert(link); |
b98b483b | 98 | |
1c4baffc | 99 | r = sd_netlink_message_get_errno(m); |
ea6ec096 | 100 | if (r < 0 && r != -EEXIST) |
6a7a4e4d | 101 | log_link_error_errno(link, r, "Could not add FDB entry: %m"); |
b98b483b AR |
102 | |
103 | return 1; | |
104 | } | |
105 | ||
106 | /* send a request to the kernel to add a FDB entry in its static MAC table. */ | |
a60a720c | 107 | int fdb_entry_configure(Link *link, FdbEntry *fdb_entry) { |
4afd3348 | 108 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
1c4baffc | 109 | sd_netlink *rtnl; |
f6bb7ac5 | 110 | Bridge *bridge; |
61b824c5 SS |
111 | uint8_t flags; |
112 | int r; | |
b98b483b | 113 | |
ea6ec096 | 114 | assert(link); |
f6bb7ac5 | 115 | assert(link->network); |
ea6ec096 | 116 | assert(link->manager); |
b98b483b | 117 | assert(fdb_entry); |
ea6ec096 TG |
118 | |
119 | rtnl = link->manager->rtnl; | |
f6bb7ac5 | 120 | bridge = BRIDGE(link->network->bridge); |
b98b483b AR |
121 | |
122 | /* create new RTM message */ | |
ea6ec096 | 123 | r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_NEWNEIGH, link->ifindex, PF_BRIDGE); |
b98b483b AR |
124 | if (r < 0) |
125 | return rtnl_log_create_error(r); | |
126 | ||
f6bb7ac5 TJ |
127 | if (bridge) |
128 | flags = NTF_MASTER; | |
129 | else | |
130 | flags = NTF_SELF; | |
131 | ||
132 | r = sd_rtnl_message_neigh_set_flags(req, flags); | |
b98b483b AR |
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 | ||
1c4baffc | 141 | r = sd_netlink_message_append_ether_addr(req, NDA_LLADDR, fdb_entry->mac_addr); |
b98b483b AR |
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. */ | |
f143c650 | 146 | if (fdb_entry->vlan_id > 0) { |
1c4baffc | 147 | r = sd_netlink_message_append_u16(req, NDA_VLAN, fdb_entry->vlan_id); |
b98b483b AR |
148 | if (r < 0) |
149 | return rtnl_log_create_error(r); | |
150 | } | |
151 | ||
c2c2793f SS |
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 | ||
61b824c5 SS |
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 | ||
b98b483b | 164 | /* send message to the kernel to update its internal static MAC table. */ |
302a796f YW |
165 | r = netlink_call_async(rtnl, NULL, req, set_fdb_handler, |
166 | link_netlink_destroy_callback, link); | |
6a7a4e4d LP |
167 | if (r < 0) |
168 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
b98b483b | 169 | |
1046bf9b YW |
170 | link_ref(link); |
171 | ||
b98b483b AR |
172 | return 0; |
173 | } | |
174 | ||
175 | /* remove and FDB entry. */ | |
176 | void fdb_entry_free(FdbEntry *fdb_entry) { | |
9ed794a3 | 177 | if (!fdb_entry) |
b98b483b AR |
178 | return; |
179 | ||
9ed794a3 | 180 | if (fdb_entry->network) { |
8c34b963 | 181 | LIST_REMOVE(static_fdb_entries, fdb_entry->network->static_fdb_entries, fdb_entry); |
8c34b963 LP |
182 | assert(fdb_entry->network->n_static_fdb_entries > 0); |
183 | fdb_entry->network->n_static_fdb_entries--; | |
b98b483b | 184 | |
8519d8f5 | 185 | if (fdb_entry->section) |
1a75764a | 186 | hashmap_remove(fdb_entry->network->fdb_entries_by_section, fdb_entry->section); |
b98b483b AR |
187 | } |
188 | ||
1a75764a | 189 | network_config_section_free(fdb_entry->section); |
b98b483b | 190 | free(fdb_entry->mac_addr); |
b98b483b AR |
191 | free(fdb_entry); |
192 | } | |
193 | ||
194 | /* parse the HW address from config files. */ | |
8519d8f5 LP |
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 | ||
b98b483b | 207 | Network *network = userdata; |
fcbf4cb7 | 208 | _cleanup_(fdb_entry_free_or_set_invalidp) FdbEntry *fdb_entry = NULL; |
b98b483b AR |
209 | int r; |
210 | ||
211 | assert(filename); | |
212 | assert(section); | |
213 | assert(lvalue); | |
214 | assert(rvalue); | |
215 | assert(data); | |
216 | ||
1a75764a | 217 | r = fdb_entry_new_static(network, filename, section_line, &fdb_entry); |
6a7a4e4d LP |
218 | if (r < 0) |
219 | return log_oom(); | |
b98b483b AR |
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 | ||
8627d112 | 230 | if (r != ETHER_ADDR_LEN) { |
12ca818f | 231 | log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue); |
b98b483b AR |
232 | return 0; |
233 | } | |
234 | ||
235 | fdb_entry = NULL; | |
236 | ||
237 | return 0; | |
238 | } | |
239 | ||
240 | /* parse the VLAN Id from config files. */ | |
8519d8f5 LP |
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 | ||
b98b483b | 253 | Network *network = userdata; |
fcbf4cb7 | 254 | _cleanup_(fdb_entry_free_or_set_invalidp) FdbEntry *fdb_entry = NULL; |
b98b483b AR |
255 | int r; |
256 | ||
257 | assert(filename); | |
258 | assert(section); | |
259 | assert(lvalue); | |
260 | assert(rvalue); | |
261 | assert(data); | |
262 | ||
1a75764a | 263 | r = fdb_entry_new_static(network, filename, section_line, &fdb_entry); |
6a7a4e4d LP |
264 | if (r < 0) |
265 | return log_oom(); | |
b98b483b | 266 | |
0e83e7a5 TJ |
267 | r = config_parse_vlanid(unit, filename, line, section, |
268 | section_line, lvalue, ltype, | |
269 | rvalue, &fdb_entry->vlan_id, userdata); | |
6a7a4e4d | 270 | if (r < 0) |
b98b483b | 271 | return r; |
b98b483b AR |
272 | |
273 | fdb_entry = NULL; | |
274 | ||
275 | return 0; | |
276 | } | |
c2c2793f SS |
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 | } | |
61b824c5 SS |
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 | } |