]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-fdb.c
Merge pull request #8807 from ChrisLesiak/systemd-update-done-mtime-fix
[thirdparty/systemd.git] / src / network / networkd-fdb.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
b98b483b
AR
2/***
3 This file is part of systemd.
4
5 Copyright (C) 2014 Intel Corporation. All rights reserved.
b98b483b
AR
6***/
7
b98b483b 8#include <net/ethernet.h>
cf0fbc49 9#include <net/if.h>
b98b483b 10
b5efdb8a 11#include "alloc-util.h"
b98b483b 12#include "conf-parser.h"
23f53b99 13#include "netdev/bridge.h"
fc2f9534 14#include "netlink-util.h"
fc2f9534 15#include "networkd-fdb.h"
23f53b99 16#include "networkd-manager.h"
b5efdb8a 17#include "util.h"
0e83e7a5 18#include "vlan-util.h"
b98b483b 19
8c34b963
LP
20#define STATIC_FDB_ENTRIES_PER_NETWORK_MAX 1024U
21
b98b483b 22/* create a new FDB entry or get an existing one. */
8c34b963
LP
23int fdb_entry_new_static(
24 Network *network,
a60a720c 25 unsigned section,
8c34b963
LP
26 FdbEntry **ret) {
27
b98b483b
AR
28 _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL;
29 struct ether_addr *mac_addr = NULL;
30
31 assert(network);
8c34b963 32 assert(ret);
b98b483b
AR
33
34 /* search entry in hashmap first. */
9ed794a3 35 if (section) {
b98b483b
AR
36 fdb_entry = hashmap_get(network->fdb_entries_by_section, UINT_TO_PTR(section));
37 if (fdb_entry) {
1cc6c93a 38 *ret = TAKE_PTR(fdb_entry);
b98b483b
AR
39
40 return 0;
41 }
42 }
43
8c34b963
LP
44 if (network->n_static_fdb_entries >= STATIC_FDB_ENTRIES_PER_NETWORK_MAX)
45 return -E2BIG;
46
b98b483b
AR
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);
b98b483b
AR
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);
8c34b963 65 network->n_static_fdb_entries++;
b98b483b
AR
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. */
1cc6c93a 74 *ret = TAKE_PTR(fdb_entry);
b98b483b
AR
75
76 return 0;
77}
78
1c4baffc 79static int set_fdb_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
ea6ec096 80 Link *link = userdata;
b98b483b
AR
81 int r;
82
ea6ec096 83 assert(link);
b98b483b 84
1c4baffc 85 r = sd_netlink_message_get_errno(m);
ea6ec096 86 if (r < 0 && r != -EEXIST)
6a7a4e4d 87 log_link_error_errno(link, r, "Could not add FDB entry: %m");
b98b483b
AR
88
89 return 1;
90}
91
92/* send a request to the kernel to add a FDB entry in its static MAC table. */
a60a720c 93int fdb_entry_configure(Link *link, FdbEntry *fdb_entry) {
4afd3348 94 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1c4baffc 95 sd_netlink *rtnl;
b98b483b 96 int r;
f6bb7ac5
TJ
97 uint8_t flags;
98 Bridge *bridge;
b98b483b 99
ea6ec096 100 assert(link);
f6bb7ac5 101 assert(link->network);
ea6ec096 102 assert(link->manager);
b98b483b 103 assert(fdb_entry);
ea6ec096
TG
104
105 rtnl = link->manager->rtnl;
f6bb7ac5 106 bridge = BRIDGE(link->network->bridge);
b98b483b
AR
107
108 /* create new RTM message */
ea6ec096 109 r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_NEWNEIGH, link->ifindex, PF_BRIDGE);
b98b483b
AR
110 if (r < 0)
111 return rtnl_log_create_error(r);
112
f6bb7ac5
TJ
113 if (bridge)
114 flags = NTF_MASTER;
115 else
116 flags = NTF_SELF;
117
118 r = sd_rtnl_message_neigh_set_flags(req, flags);
b98b483b
AR
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
1c4baffc 127 r = sd_netlink_message_append_ether_addr(req, NDA_LLADDR, fdb_entry->mac_addr);
b98b483b
AR
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) {
1c4baffc 133 r = sd_netlink_message_append_u16(req, NDA_VLAN, fdb_entry->vlan_id);
b98b483b
AR
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. */
1c4baffc 139 r = sd_netlink_call_async(rtnl, req, set_fdb_handler, link, 0, NULL);
6a7a4e4d
LP
140 if (r < 0)
141 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
b98b483b
AR
142
143 return 0;
144}
145
146/* remove and FDB entry. */
147void fdb_entry_free(FdbEntry *fdb_entry) {
9ed794a3 148 if (!fdb_entry)
b98b483b
AR
149 return;
150
9ed794a3 151 if (fdb_entry->network) {
8c34b963
LP
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--;
b98b483b 156
8519d8f5 157 if (fdb_entry->section)
8c34b963 158 hashmap_remove(fdb_entry->network->fdb_entries_by_section, UINT_TO_PTR(fdb_entry->section));
b98b483b
AR
159 }
160
161 free(fdb_entry->mac_addr);
162
163 free(fdb_entry);
164}
165
166/* parse the HW address from config files. */
8519d8f5
LP
167int 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
b98b483b
AR
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);
6a7a4e4d
LP
190 if (r < 0)
191 return log_oom();
b98b483b
AR
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
8519d8f5 202 if (ETHER_ADDR_LEN != r) {
12ca818f 203 log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue);
b98b483b
AR
204 return 0;
205 }
206
207 fdb_entry = NULL;
208
209 return 0;
210}
211
212/* parse the VLAN Id from config files. */
8519d8f5
LP
213int 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
b98b483b
AR
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);
6a7a4e4d
LP
236 if (r < 0)
237 return log_oom();
b98b483b 238
0e83e7a5
TJ
239 r = config_parse_vlanid(unit, filename, line, section,
240 section_line, lvalue, ltype,
241 rvalue, &fdb_entry->vlan_id, userdata);
6a7a4e4d 242 if (r < 0)
b98b483b 243 return r;
b98b483b
AR
244
245 fdb_entry = NULL;
246
247 return 0;
248}