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