]>
Commit | Line | Data |
---|---|---|
b98b483b AR |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright (C) 2014 Intel Corporation. All rights reserved. | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
b98b483b | 22 | #include <net/ethernet.h> |
cf0fbc49 | 23 | #include <net/if.h> |
b98b483b | 24 | |
b5efdb8a | 25 | #include "alloc-util.h" |
b98b483b | 26 | #include "conf-parser.h" |
fc2f9534 | 27 | #include "netlink-util.h" |
fc2f9534 | 28 | #include "networkd-fdb.h" |
b5efdb8a LP |
29 | #include "networkd.h" |
30 | #include "util.h" | |
b98b483b AR |
31 | |
32 | /* create a new FDB entry or get an existing one. */ | |
33 | int fdb_entry_new_static(Network *const network, | |
34 | const unsigned section, | |
35 | FdbEntry **ret) { | |
36 | _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL; | |
37 | struct ether_addr *mac_addr = NULL; | |
38 | ||
39 | assert(network); | |
40 | ||
41 | /* search entry in hashmap first. */ | |
42 | if(section) { | |
43 | fdb_entry = hashmap_get(network->fdb_entries_by_section, UINT_TO_PTR(section)); | |
44 | if (fdb_entry) { | |
45 | *ret = fdb_entry; | |
46 | fdb_entry = NULL; | |
47 | ||
48 | return 0; | |
49 | } | |
50 | } | |
51 | ||
52 | /* allocate space for MAC address. */ | |
53 | mac_addr = new0(struct ether_addr, 1); | |
54 | if (!mac_addr) | |
55 | return -ENOMEM; | |
56 | ||
57 | /* allocate space for and FDB entry. */ | |
58 | fdb_entry = new0(FdbEntry, 1); | |
59 | ||
60 | if (!fdb_entry) { | |
61 | /* free previously allocated space for mac_addr. */ | |
62 | free(mac_addr); | |
63 | return -ENOMEM; | |
64 | } | |
65 | ||
66 | /* init FDB structure. */ | |
67 | fdb_entry->network = network; | |
68 | fdb_entry->mac_addr = mac_addr; | |
69 | ||
70 | LIST_PREPEND(static_fdb_entries, network->static_fdb_entries, fdb_entry); | |
71 | ||
72 | if (section) { | |
73 | fdb_entry->section = section; | |
74 | hashmap_put(network->fdb_entries_by_section, | |
75 | UINT_TO_PTR(fdb_entry->section), fdb_entry); | |
76 | } | |
77 | ||
78 | /* return allocated FDB structure. */ | |
79 | *ret = fdb_entry; | |
80 | fdb_entry = NULL; | |
81 | ||
82 | return 0; | |
83 | } | |
84 | ||
1c4baffc | 85 | static int set_fdb_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
ea6ec096 | 86 | Link *link = userdata; |
b98b483b AR |
87 | int r; |
88 | ||
ea6ec096 | 89 | assert(link); |
b98b483b | 90 | |
1c4baffc | 91 | r = sd_netlink_message_get_errno(m); |
ea6ec096 | 92 | if (r < 0 && r != -EEXIST) |
6a7a4e4d | 93 | log_link_error_errno(link, r, "Could not add FDB entry: %m"); |
b98b483b AR |
94 | |
95 | return 1; | |
96 | } | |
97 | ||
98 | /* send a request to the kernel to add a FDB entry in its static MAC table. */ | |
a245ced0 | 99 | int fdb_entry_configure(Link *const link, FdbEntry *const fdb_entry) { |
4afd3348 | 100 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
1c4baffc | 101 | sd_netlink *rtnl; |
b98b483b AR |
102 | int r; |
103 | ||
ea6ec096 TG |
104 | assert(link); |
105 | assert(link->manager); | |
b98b483b | 106 | assert(fdb_entry); |
ea6ec096 TG |
107 | |
108 | rtnl = link->manager->rtnl; | |
b98b483b AR |
109 | |
110 | /* create new RTM message */ | |
ea6ec096 | 111 | r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_NEWNEIGH, link->ifindex, PF_BRIDGE); |
b98b483b AR |
112 | if (r < 0) |
113 | return rtnl_log_create_error(r); | |
114 | ||
115 | /* only NTF_SELF flag supported. */ | |
116 | r = sd_rtnl_message_neigh_set_flags(req, NTF_SELF); | |
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. */ | |
1c4baffc | 137 | r = sd_netlink_call_async(rtnl, req, set_fdb_handler, link, 0, NULL); |
6a7a4e4d LP |
138 | if (r < 0) |
139 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
b98b483b AR |
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, | |
151 | fdb_entry); | |
152 | ||
8519d8f5 LP |
153 | if (fdb_entry->section) |
154 | hashmap_remove(fdb_entry->network->fdb_entries_by_section, | |
155 | UINT_TO_PTR(fdb_entry->section)); | |
b98b483b AR |
156 | } |
157 | ||
158 | free(fdb_entry->mac_addr); | |
159 | ||
160 | free(fdb_entry); | |
161 | } | |
162 | ||
163 | /* parse the HW address from config files. */ | |
8519d8f5 LP |
164 | int config_parse_fdb_hwaddr( |
165 | const char *unit, | |
166 | const char *filename, | |
167 | unsigned line, | |
168 | const char *section, | |
169 | unsigned section_line, | |
170 | const char *lvalue, | |
171 | int ltype, | |
172 | const char *rvalue, | |
173 | void *data, | |
174 | void *userdata) { | |
175 | ||
b98b483b AR |
176 | Network *network = userdata; |
177 | _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL; | |
178 | int r; | |
179 | ||
180 | assert(filename); | |
181 | assert(section); | |
182 | assert(lvalue); | |
183 | assert(rvalue); | |
184 | assert(data); | |
185 | ||
186 | r = fdb_entry_new_static(network, section_line, &fdb_entry); | |
6a7a4e4d LP |
187 | if (r < 0) |
188 | return log_oom(); | |
b98b483b AR |
189 | |
190 | /* read in the MAC address for the FDB table. */ | |
191 | r = sscanf(rvalue, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", | |
192 | &fdb_entry->mac_addr->ether_addr_octet[0], | |
193 | &fdb_entry->mac_addr->ether_addr_octet[1], | |
194 | &fdb_entry->mac_addr->ether_addr_octet[2], | |
195 | &fdb_entry->mac_addr->ether_addr_octet[3], | |
196 | &fdb_entry->mac_addr->ether_addr_octet[4], | |
197 | &fdb_entry->mac_addr->ether_addr_octet[5]); | |
198 | ||
8519d8f5 | 199 | if (ETHER_ADDR_LEN != r) { |
12ca818f | 200 | log_syntax(unit, LOG_ERR, filename, line, 0, "Not a valid MAC address, ignoring assignment: %s", rvalue); |
b98b483b AR |
201 | return 0; |
202 | } | |
203 | ||
204 | fdb_entry = NULL; | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
209 | /* parse the VLAN Id from config files. */ | |
8519d8f5 LP |
210 | int config_parse_fdb_vlan_id( |
211 | const char *unit, | |
212 | const char *filename, | |
213 | unsigned line, | |
214 | const char *section, | |
215 | unsigned section_line, | |
216 | const char *lvalue, | |
217 | int ltype, | |
218 | const char *rvalue, | |
219 | void *data, | |
220 | void *userdata) { | |
221 | ||
b98b483b AR |
222 | Network *network = userdata; |
223 | _cleanup_fdbentry_free_ FdbEntry *fdb_entry = NULL; | |
224 | int r; | |
225 | ||
226 | assert(filename); | |
227 | assert(section); | |
228 | assert(lvalue); | |
229 | assert(rvalue); | |
230 | assert(data); | |
231 | ||
232 | r = fdb_entry_new_static(network, section_line, &fdb_entry); | |
6a7a4e4d LP |
233 | if (r < 0) |
234 | return log_oom(); | |
b98b483b AR |
235 | |
236 | r = config_parse_unsigned(unit, filename, line, section, | |
237 | section_line, lvalue, ltype, | |
238 | rvalue, &fdb_entry->vlan_id, userdata); | |
6a7a4e4d | 239 | if (r < 0) |
b98b483b | 240 | return r; |
b98b483b AR |
241 | |
242 | fdb_entry = NULL; | |
243 | ||
244 | return 0; | |
245 | } |