]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-fdb.c
network: make ndisc related handlers return negative errno but caller ignore the...
[thirdparty/systemd.git] / src / network / networkd-fdb.c
CommitLineData
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"
fc2f9534 12#include "netlink-util.h"
fc2f9534 13#include "networkd-fdb.h"
23f53b99 14#include "networkd-manager.h"
b5efdb8a 15#include "util.h"
0e83e7a5 16#include "vlan-util.h"
b98b483b 17
8c34b963
LP
18#define STATIC_FDB_ENTRIES_PER_NETWORK_MAX 1024U
19
b98b483b 20/* create a new FDB entry or get an existing one. */
8c34b963
LP
21int fdb_entry_new_static(
22 Network *network,
a60a720c 23 unsigned section,
8c34b963
LP
24 FdbEntry **ret) {
25
8e766630 26 _cleanup_(fdb_entry_freep) FdbEntry *fdb_entry = NULL;
b98b483b
AR
27 struct ether_addr *mac_addr = NULL;
28
29 assert(network);
8c34b963 30 assert(ret);
b98b483b
AR
31
32 /* search entry in hashmap first. */
9ed794a3 33 if (section) {
b98b483b
AR
34 fdb_entry = hashmap_get(network->fdb_entries_by_section, UINT_TO_PTR(section));
35 if (fdb_entry) {
1cc6c93a 36 *ret = TAKE_PTR(fdb_entry);
b98b483b
AR
37
38 return 0;
39 }
40 }
41
8c34b963
LP
42 if (network->n_static_fdb_entries >= STATIC_FDB_ENTRIES_PER_NETWORK_MAX)
43 return -E2BIG;
44
b98b483b
AR
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);
b98b483b
AR
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);
8c34b963 63 network->n_static_fdb_entries++;
b98b483b
AR
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. */
1cc6c93a 72 *ret = TAKE_PTR(fdb_entry);
b98b483b
AR
73
74 return 0;
75}
76
1c4baffc 77static int set_fdb_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
ea6ec096 78 Link *link = userdata;
b98b483b
AR
79 int r;
80
ea6ec096 81 assert(link);
b98b483b 82
1c4baffc 83 r = sd_netlink_message_get_errno(m);
ea6ec096 84 if (r < 0 && r != -EEXIST)
6a7a4e4d 85 log_link_error_errno(link, r, "Could not add FDB entry: %m");
b98b483b
AR
86
87 return 1;
88}
89
90/* send a request to the kernel to add a FDB entry in its static MAC table. */
a60a720c 91int fdb_entry_configure(Link *link, FdbEntry *fdb_entry) {
4afd3348 92 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
1c4baffc 93 sd_netlink *rtnl;
b98b483b 94 int r;
f6bb7ac5
TJ
95 uint8_t flags;
96 Bridge *bridge;
b98b483b 97
ea6ec096 98 assert(link);
f6bb7ac5 99 assert(link->network);
ea6ec096 100 assert(link->manager);
b98b483b 101 assert(fdb_entry);
ea6ec096
TG
102
103 rtnl = link->manager->rtnl;
f6bb7ac5 104 bridge = BRIDGE(link->network->bridge);
b98b483b
AR
105
106 /* create new RTM message */
ea6ec096 107 r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_NEWNEIGH, link->ifindex, PF_BRIDGE);
b98b483b
AR
108 if (r < 0)
109 return rtnl_log_create_error(r);
110
f6bb7ac5
TJ
111 if (bridge)
112 flags = NTF_MASTER;
113 else
114 flags = NTF_SELF;
115
116 r = sd_rtnl_message_neigh_set_flags(req, flags);
b98b483b
AR
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
1c4baffc 125 r = sd_netlink_message_append_ether_addr(req, NDA_LLADDR, fdb_entry->mac_addr);
b98b483b
AR
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) {
1c4baffc 131 r = sd_netlink_message_append_u16(req, NDA_VLAN, fdb_entry->vlan_id);
b98b483b
AR
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. */
ee38400b 137 r = sd_netlink_call_async(rtnl, NULL, req, set_fdb_handler,
8190a388 138 link_netlink_destroy_callback, link, 0, __func__);
6a7a4e4d
LP
139 if (r < 0)
140 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
b98b483b 141
1046bf9b
YW
142 link_ref(link);
143
b98b483b
AR
144 return 0;
145}
146
147/* remove and FDB entry. */
148void fdb_entry_free(FdbEntry *fdb_entry) {
9ed794a3 149 if (!fdb_entry)
b98b483b
AR
150 return;
151
9ed794a3 152 if (fdb_entry->network) {
8c34b963
LP
153 LIST_REMOVE(static_fdb_entries, fdb_entry->network->static_fdb_entries, fdb_entry);
154
155 assert(fdb_entry->network->n_static_fdb_entries > 0);
156 fdb_entry->network->n_static_fdb_entries--;
b98b483b 157
8519d8f5 158 if (fdb_entry->section)
8c34b963 159 hashmap_remove(fdb_entry->network->fdb_entries_by_section, UINT_TO_PTR(fdb_entry->section));
b98b483b
AR
160 }
161
162 free(fdb_entry->mac_addr);
163
164 free(fdb_entry);
165}
166
167/* parse the HW address from config files. */
8519d8f5
LP
168int config_parse_fdb_hwaddr(
169 const char *unit,
170 const char *filename,
171 unsigned line,
172 const char *section,
173 unsigned section_line,
174 const char *lvalue,
175 int ltype,
176 const char *rvalue,
177 void *data,
178 void *userdata) {
179
b98b483b 180 Network *network = userdata;
8e766630 181 _cleanup_(fdb_entry_freep) FdbEntry *fdb_entry = NULL;
b98b483b
AR
182 int r;
183
184 assert(filename);
185 assert(section);
186 assert(lvalue);
187 assert(rvalue);
188 assert(data);
189
190 r = fdb_entry_new_static(network, section_line, &fdb_entry);
6a7a4e4d
LP
191 if (r < 0)
192 return log_oom();
b98b483b
AR
193
194 /* read in the MAC address for the FDB table. */
195 r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
196 &fdb_entry->mac_addr->ether_addr_octet[0],
197 &fdb_entry->mac_addr->ether_addr_octet[1],
198 &fdb_entry->mac_addr->ether_addr_octet[2],
199 &fdb_entry->mac_addr->ether_addr_octet[3],
200 &fdb_entry->mac_addr->ether_addr_octet[4],
201 &fdb_entry->mac_addr->ether_addr_octet[5]);
202
8627d112 203 if (r != ETHER_ADDR_LEN) {
12ca818f 204 log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue);
b98b483b
AR
205 return 0;
206 }
207
208 fdb_entry = NULL;
209
210 return 0;
211}
212
213/* parse the VLAN Id from config files. */
8519d8f5
LP
214int config_parse_fdb_vlan_id(
215 const char *unit,
216 const char *filename,
217 unsigned line,
218 const char *section,
219 unsigned section_line,
220 const char *lvalue,
221 int ltype,
222 const char *rvalue,
223 void *data,
224 void *userdata) {
225
b98b483b 226 Network *network = userdata;
8e766630 227 _cleanup_(fdb_entry_freep) FdbEntry *fdb_entry = NULL;
b98b483b
AR
228 int r;
229
230 assert(filename);
231 assert(section);
232 assert(lvalue);
233 assert(rvalue);
234 assert(data);
235
236 r = fdb_entry_new_static(network, section_line, &fdb_entry);
6a7a4e4d
LP
237 if (r < 0)
238 return log_oom();
b98b483b 239
0e83e7a5
TJ
240 r = config_parse_vlanid(unit, filename, line, section,
241 section_line, lvalue, ltype,
242 rvalue, &fdb_entry->vlan_id, userdata);
6a7a4e4d 243 if (r < 0)
b98b483b 244 return r;
b98b483b
AR
245
246 fdb_entry = NULL;
247
248 return 0;
249}