Commit | Line | Data |
---|---|---|
0046b040 | 1 | /* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
2 | * |
3 | * Marek Lindner, Simon Wunderlich | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of version 2 of the GNU General Public | |
7 | * License as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but | |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
ebf38fb7 | 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
c6c8fea2 SE |
16 | */ |
17 | ||
c6c8fea2 | 18 | #include "hard-interface.h" |
1e2c2a4f | 19 | #include "main.h" |
c6c8fea2 | 20 | |
7a659d56 | 21 | #include <linux/atomic.h> |
1e2c2a4f SE |
22 | #include <linux/bug.h> |
23 | #include <linux/byteorder/generic.h> | |
24 | #include <linux/errno.h> | |
25 | #include <linux/fs.h> | |
fcafa5e7 | 26 | #include <linux/if.h> |
c6c8fea2 | 27 | #include <linux/if_arp.h> |
af5d4f77 | 28 | #include <linux/if_ether.h> |
1e2c2a4f | 29 | #include <linux/kernel.h> |
7a659d56 | 30 | #include <linux/kref.h> |
1e2c2a4f SE |
31 | #include <linux/list.h> |
32 | #include <linux/netdevice.h> | |
33 | #include <linux/printk.h> | |
34 | #include <linux/rculist.h> | |
35 | #include <linux/rtnetlink.h> | |
36 | #include <linux/slab.h> | |
cef63419 | 37 | #include <linux/spinlock.h> |
1e2c2a4f | 38 | #include <linux/workqueue.h> |
1e2c2a4f | 39 | |
a2d08166 | 40 | #include "bat_v.h" |
1e2c2a4f SE |
41 | #include "bridge_loop_avoidance.h" |
42 | #include "debugfs.h" | |
43 | #include "distributed-arp-table.h" | |
44 | #include "gateway_client.h" | |
ba412080 | 45 | #include "log.h" |
1e2c2a4f SE |
46 | #include "originator.h" |
47 | #include "packet.h" | |
48 | #include "send.h" | |
49 | #include "soft-interface.h" | |
50 | #include "sysfs.h" | |
51 | #include "translation-table.h" | |
c6c8fea2 | 52 | |
140ed8e8 SE |
53 | /** |
54 | * batadv_hardif_release - release hard interface from lists and queue for | |
55 | * free after rcu grace period | |
7a659d56 | 56 | * @ref: kref pointer of the hard interface |
140ed8e8 | 57 | */ |
7a659d56 | 58 | void batadv_hardif_release(struct kref *ref) |
c6c8fea2 | 59 | { |
7a659d56 SE |
60 | struct batadv_hard_iface *hard_iface; |
61 | ||
62 | hard_iface = container_of(ref, struct batadv_hard_iface, refcount); | |
e6c10f43 | 63 | dev_put(hard_iface->net_dev); |
140ed8e8 SE |
64 | |
65 | kfree_rcu(hard_iface, rcu); | |
c6c8fea2 SE |
66 | } |
67 | ||
56303d34 SE |
68 | struct batadv_hard_iface * |
69 | batadv_hardif_get_by_netdev(const struct net_device *net_dev) | |
c6c8fea2 | 70 | { |
56303d34 | 71 | struct batadv_hard_iface *hard_iface; |
c6c8fea2 SE |
72 | |
73 | rcu_read_lock(); | |
3193e8fd | 74 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
e6c10f43 | 75 | if (hard_iface->net_dev == net_dev && |
7a659d56 | 76 | kref_get_unless_zero(&hard_iface->refcount)) |
c6c8fea2 SE |
77 | goto out; |
78 | } | |
79 | ||
e6c10f43 | 80 | hard_iface = NULL; |
c6c8fea2 SE |
81 | |
82 | out: | |
c6c8fea2 | 83 | rcu_read_unlock(); |
e6c10f43 | 84 | return hard_iface; |
c6c8fea2 SE |
85 | } |
86 | ||
1bc4e2b0 AL |
87 | /** |
88 | * batadv_mutual_parents - check if two devices are each others parent | |
89 | * @dev1: 1st net_device | |
90 | * @dev2: 2nd net_device | |
91 | * | |
92 | * veth devices come in pairs and each is the parent of the other! | |
93 | * | |
94 | * Return: true if the devices are each others parent, otherwise false | |
95 | */ | |
96 | static bool batadv_mutual_parents(const struct net_device *dev1, | |
97 | const struct net_device *dev2) | |
98 | { | |
99 | int dev1_parent_iflink = dev_get_iflink(dev1); | |
100 | int dev2_parent_iflink = dev_get_iflink(dev2); | |
101 | ||
102 | if (!dev1_parent_iflink || !dev2_parent_iflink) | |
103 | return false; | |
104 | ||
105 | return (dev1_parent_iflink == dev2->ifindex) && | |
106 | (dev2_parent_iflink == dev1->ifindex); | |
107 | } | |
108 | ||
b7eddd0b AQ |
109 | /** |
110 | * batadv_is_on_batman_iface - check if a device is a batman iface descendant | |
111 | * @net_dev: the device to check | |
112 | * | |
113 | * If the user creates any virtual device on top of a batman-adv interface, it | |
114 | * is important to prevent this new interface to be used to create a new mesh | |
115 | * network (this behaviour would lead to a batman-over-batman configuration). | |
116 | * This function recursively checks all the fathers of the device passed as | |
117 | * argument looking for a batman-adv soft interface. | |
118 | * | |
62fe710f | 119 | * Return: true if the device is descendant of a batman-adv mesh interface (or |
b7eddd0b AQ |
120 | * if it is a batman-adv interface itself), false otherwise |
121 | */ | |
122 | static bool batadv_is_on_batman_iface(const struct net_device *net_dev) | |
123 | { | |
124 | struct net_device *parent_dev; | |
2cd45a06 | 125 | struct net *net = dev_net(net_dev); |
b7eddd0b AQ |
126 | bool ret; |
127 | ||
128 | /* check if this is a batman-adv mesh interface */ | |
129 | if (batadv_softif_is_valid(net_dev)) | |
130 | return true; | |
131 | ||
132 | /* no more parents..stop recursion */ | |
a54acb3a ND |
133 | if (dev_get_iflink(net_dev) == 0 || |
134 | dev_get_iflink(net_dev) == net_dev->ifindex) | |
b7eddd0b AQ |
135 | return false; |
136 | ||
137 | /* recurse over the parent device */ | |
2cd45a06 | 138 | parent_dev = __dev_get_by_index(net, dev_get_iflink(net_dev)); |
b7eddd0b AQ |
139 | /* if we got a NULL parent_dev there is something broken.. */ |
140 | if (WARN(!parent_dev, "Cannot find parent device")) | |
141 | return false; | |
142 | ||
1bc4e2b0 AL |
143 | if (batadv_mutual_parents(net_dev, parent_dev)) |
144 | return false; | |
145 | ||
b7eddd0b AQ |
146 | ret = batadv_is_on_batman_iface(parent_dev); |
147 | ||
b7eddd0b AQ |
148 | return ret; |
149 | } | |
150 | ||
4b426b10 | 151 | static bool batadv_is_valid_iface(const struct net_device *net_dev) |
c6c8fea2 SE |
152 | { |
153 | if (net_dev->flags & IFF_LOOPBACK) | |
4b426b10 | 154 | return false; |
c6c8fea2 SE |
155 | |
156 | if (net_dev->type != ARPHRD_ETHER) | |
4b426b10 | 157 | return false; |
c6c8fea2 SE |
158 | |
159 | if (net_dev->addr_len != ETH_ALEN) | |
4b426b10 | 160 | return false; |
c6c8fea2 SE |
161 | |
162 | /* no batman over batman */ | |
b7eddd0b | 163 | if (batadv_is_on_batman_iface(net_dev)) |
4b426b10 | 164 | return false; |
c6c8fea2 | 165 | |
4b426b10 | 166 | return true; |
c6c8fea2 SE |
167 | } |
168 | ||
de68d100 MS |
169 | /** |
170 | * batadv_is_wifi_netdev - check if the given net_device struct is a wifi | |
171 | * interface | |
172 | * @net_device: the device to check | |
173 | * | |
62fe710f | 174 | * Return: true if the net device is a 802.11 wireless device, false otherwise. |
de68d100 | 175 | */ |
0c69aecc | 176 | bool batadv_is_wifi_netdev(struct net_device *net_device) |
de68d100 | 177 | { |
0c69aecc AQ |
178 | if (!net_device) |
179 | return false; | |
180 | ||
de68d100 MS |
181 | #ifdef CONFIG_WIRELESS_EXT |
182 | /* pre-cfg80211 drivers have to implement WEXT, so it is possible to | |
183 | * check for wireless_handlers != NULL | |
184 | */ | |
185 | if (net_device->wireless_handlers) | |
186 | return true; | |
187 | #endif | |
188 | ||
189 | /* cfg80211 drivers have to set ieee80211_ptr */ | |
190 | if (net_device->ieee80211_ptr) | |
191 | return true; | |
192 | ||
193 | return false; | |
194 | } | |
195 | ||
56303d34 | 196 | static struct batadv_hard_iface * |
18a1cb6e | 197 | batadv_hardif_get_active(const struct net_device *soft_iface) |
c6c8fea2 | 198 | { |
56303d34 | 199 | struct batadv_hard_iface *hard_iface; |
c6c8fea2 SE |
200 | |
201 | rcu_read_lock(); | |
3193e8fd | 202 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
e6c10f43 | 203 | if (hard_iface->soft_iface != soft_iface) |
c6c8fea2 SE |
204 | continue; |
205 | ||
e9a4f295 | 206 | if (hard_iface->if_status == BATADV_IF_ACTIVE && |
7a659d56 | 207 | kref_get_unless_zero(&hard_iface->refcount)) |
c6c8fea2 SE |
208 | goto out; |
209 | } | |
210 | ||
e6c10f43 | 211 | hard_iface = NULL; |
c6c8fea2 SE |
212 | |
213 | out: | |
c6c8fea2 | 214 | rcu_read_unlock(); |
e6c10f43 | 215 | return hard_iface; |
c6c8fea2 SE |
216 | } |
217 | ||
56303d34 SE |
218 | static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv, |
219 | struct batadv_hard_iface *oldif) | |
c6c8fea2 | 220 | { |
56303d34 | 221 | struct batadv_hard_iface *primary_if; |
32ae9b22 | 222 | |
e5d89254 | 223 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 ML |
224 | if (!primary_if) |
225 | goto out; | |
c6c8fea2 | 226 | |
785ea114 | 227 | batadv_dat_init_own_addr(bat_priv, primary_if); |
08adf151 | 228 | batadv_bla_update_orig_address(bat_priv, primary_if, oldif); |
32ae9b22 ML |
229 | out: |
230 | if (primary_if) | |
82047ad7 | 231 | batadv_hardif_put(primary_if); |
c6c8fea2 SE |
232 | } |
233 | ||
56303d34 SE |
234 | static void batadv_primary_if_select(struct batadv_priv *bat_priv, |
235 | struct batadv_hard_iface *new_hard_iface) | |
c6c8fea2 | 236 | { |
56303d34 | 237 | struct batadv_hard_iface *curr_hard_iface; |
c6c8fea2 | 238 | |
c3caf519 | 239 | ASSERT_RTNL(); |
c6c8fea2 | 240 | |
17a86915 SE |
241 | if (new_hard_iface) |
242 | kref_get(&new_hard_iface->refcount); | |
c6c8fea2 | 243 | |
728cbc6a | 244 | curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1); |
32ae9b22 | 245 | rcu_assign_pointer(bat_priv->primary_if, new_hard_iface); |
c6c8fea2 | 246 | |
32ae9b22 | 247 | if (!new_hard_iface) |
23721387 | 248 | goto out; |
32ae9b22 | 249 | |
29824a55 | 250 | bat_priv->algo_ops->iface.primary_set(new_hard_iface); |
18a1cb6e | 251 | batadv_primary_if_update_addr(bat_priv, curr_hard_iface); |
23721387 SW |
252 | |
253 | out: | |
254 | if (curr_hard_iface) | |
82047ad7 | 255 | batadv_hardif_put(curr_hard_iface); |
c6c8fea2 SE |
256 | } |
257 | ||
56303d34 SE |
258 | static bool |
259 | batadv_hardif_is_iface_up(const struct batadv_hard_iface *hard_iface) | |
c6c8fea2 | 260 | { |
e6c10f43 | 261 | if (hard_iface->net_dev->flags & IFF_UP) |
c6c8fea2 SE |
262 | return true; |
263 | ||
264 | return false; | |
265 | } | |
266 | ||
18a1cb6e | 267 | static void batadv_check_known_mac_addr(const struct net_device *net_dev) |
c6c8fea2 | 268 | { |
56303d34 | 269 | const struct batadv_hard_iface *hard_iface; |
c6c8fea2 SE |
270 | |
271 | rcu_read_lock(); | |
3193e8fd | 272 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
e9a4f295 SE |
273 | if ((hard_iface->if_status != BATADV_IF_ACTIVE) && |
274 | (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) | |
c6c8fea2 SE |
275 | continue; |
276 | ||
e6c10f43 | 277 | if (hard_iface->net_dev == net_dev) |
c6c8fea2 SE |
278 | continue; |
279 | ||
1eda58bf SE |
280 | if (!batadv_compare_eth(hard_iface->net_dev->dev_addr, |
281 | net_dev->dev_addr)) | |
c6c8fea2 SE |
282 | continue; |
283 | ||
67969581 SE |
284 | pr_warn("The newly added mac address (%pM) already exists on: %s\n", |
285 | net_dev->dev_addr, hard_iface->net_dev->name); | |
286 | pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n"); | |
c6c8fea2 SE |
287 | } |
288 | rcu_read_unlock(); | |
289 | } | |
290 | ||
7bca68c7 SE |
291 | /** |
292 | * batadv_hardif_recalc_extra_skbroom() - Recalculate skbuff extra head/tailroom | |
293 | * @soft_iface: netdev struct of the mesh interface | |
294 | */ | |
295 | static void batadv_hardif_recalc_extra_skbroom(struct net_device *soft_iface) | |
296 | { | |
297 | const struct batadv_hard_iface *hard_iface; | |
298 | unsigned short lower_header_len = ETH_HLEN; | |
299 | unsigned short lower_headroom = 0; | |
300 | unsigned short lower_tailroom = 0; | |
301 | unsigned short needed_headroom; | |
302 | ||
303 | rcu_read_lock(); | |
304 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { | |
305 | if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) | |
306 | continue; | |
307 | ||
308 | if (hard_iface->soft_iface != soft_iface) | |
309 | continue; | |
310 | ||
311 | lower_header_len = max_t(unsigned short, lower_header_len, | |
312 | hard_iface->net_dev->hard_header_len); | |
313 | ||
314 | lower_headroom = max_t(unsigned short, lower_headroom, | |
315 | hard_iface->net_dev->needed_headroom); | |
316 | ||
317 | lower_tailroom = max_t(unsigned short, lower_tailroom, | |
318 | hard_iface->net_dev->needed_tailroom); | |
319 | } | |
320 | rcu_read_unlock(); | |
321 | ||
322 | needed_headroom = lower_headroom + (lower_header_len - ETH_HLEN); | |
323 | needed_headroom += batadv_max_header_len(); | |
324 | ||
325 | soft_iface->needed_headroom = needed_headroom; | |
326 | soft_iface->needed_tailroom = lower_tailroom; | |
327 | } | |
328 | ||
9563877e | 329 | int batadv_hardif_min_mtu(struct net_device *soft_iface) |
c6c8fea2 | 330 | { |
a19d3d85 | 331 | struct batadv_priv *bat_priv = netdev_priv(soft_iface); |
56303d34 | 332 | const struct batadv_hard_iface *hard_iface; |
930cd6e4 | 333 | int min_mtu = INT_MAX; |
c6c8fea2 SE |
334 | |
335 | rcu_read_lock(); | |
3193e8fd | 336 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
e9a4f295 SE |
337 | if ((hard_iface->if_status != BATADV_IF_ACTIVE) && |
338 | (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) | |
c6c8fea2 SE |
339 | continue; |
340 | ||
e6c10f43 | 341 | if (hard_iface->soft_iface != soft_iface) |
c6c8fea2 SE |
342 | continue; |
343 | ||
a19d3d85 | 344 | min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu); |
c6c8fea2 SE |
345 | } |
346 | rcu_read_unlock(); | |
a19d3d85 | 347 | |
a19d3d85 ML |
348 | if (atomic_read(&bat_priv->fragmentation) == 0) |
349 | goto out; | |
350 | ||
351 | /* with fragmentation enabled the maximum size of internally generated | |
352 | * packets such as translation table exchanges or tvlv containers, etc | |
353 | * has to be calculated | |
354 | */ | |
355 | min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE); | |
356 | min_mtu -= sizeof(struct batadv_frag_packet); | |
357 | min_mtu *= BATADV_FRAG_MAX_FRAGMENTS; | |
a19d3d85 | 358 | |
c6c8fea2 | 359 | out: |
930cd6e4 AQ |
360 | /* report to the other components the maximum amount of bytes that |
361 | * batman-adv can send over the wire (without considering the payload | |
362 | * overhead). For example, this value is used by TT to compute the | |
363 | * maximum local table table size | |
364 | */ | |
365 | atomic_set(&bat_priv->packet_size_max, min_mtu); | |
366 | ||
367 | /* the real soft-interface MTU is computed by removing the payload | |
368 | * overhead from the maximum amount of bytes that was just computed. | |
369 | * | |
370 | * However batman-adv does not support MTUs bigger than ETH_DATA_LEN | |
371 | */ | |
372 | return min_t(int, min_mtu - batadv_max_header_len(), ETH_DATA_LEN); | |
c6c8fea2 SE |
373 | } |
374 | ||
375 | /* adjusts the MTU if a new interface with a smaller MTU appeared. */ | |
9563877e | 376 | void batadv_update_min_mtu(struct net_device *soft_iface) |
c6c8fea2 | 377 | { |
a19d3d85 | 378 | soft_iface->mtu = batadv_hardif_min_mtu(soft_iface); |
c6c8fea2 | 379 | |
a19d3d85 ML |
380 | /* Check if the local translate table should be cleaned up to match a |
381 | * new (and smaller) MTU. | |
382 | */ | |
383 | batadv_tt_local_resize_to_mtu(soft_iface); | |
c6c8fea2 SE |
384 | } |
385 | ||
56303d34 SE |
386 | static void |
387 | batadv_hardif_activate_interface(struct batadv_hard_iface *hard_iface) | |
c6c8fea2 | 388 | { |
56303d34 SE |
389 | struct batadv_priv *bat_priv; |
390 | struct batadv_hard_iface *primary_if = NULL; | |
c6c8fea2 | 391 | |
e9a4f295 | 392 | if (hard_iface->if_status != BATADV_IF_INACTIVE) |
32ae9b22 | 393 | goto out; |
c6c8fea2 | 394 | |
e6c10f43 | 395 | bat_priv = netdev_priv(hard_iface->soft_iface); |
c6c8fea2 | 396 | |
29824a55 | 397 | bat_priv->algo_ops->iface.update_mac(hard_iface); |
e9a4f295 | 398 | hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED; |
c6c8fea2 | 399 | |
9cfc7bd6 | 400 | /* the first active interface becomes our primary interface or |
015758d0 | 401 | * the next active interface after the old primary interface was removed |
c6c8fea2 | 402 | */ |
e5d89254 | 403 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 | 404 | if (!primary_if) |
18a1cb6e | 405 | batadv_primary_if_select(bat_priv, hard_iface); |
c6c8fea2 | 406 | |
3e34819e SE |
407 | batadv_info(hard_iface->soft_iface, "Interface activated: %s\n", |
408 | hard_iface->net_dev->name); | |
c6c8fea2 | 409 | |
9563877e | 410 | batadv_update_min_mtu(hard_iface->soft_iface); |
32ae9b22 | 411 | |
29824a55 AQ |
412 | if (bat_priv->algo_ops->iface.activate) |
413 | bat_priv->algo_ops->iface.activate(hard_iface); | |
b6cf5d49 | 414 | |
32ae9b22 ML |
415 | out: |
416 | if (primary_if) | |
82047ad7 | 417 | batadv_hardif_put(primary_if); |
c6c8fea2 SE |
418 | } |
419 | ||
56303d34 SE |
420 | static void |
421 | batadv_hardif_deactivate_interface(struct batadv_hard_iface *hard_iface) | |
c6c8fea2 | 422 | { |
e9a4f295 SE |
423 | if ((hard_iface->if_status != BATADV_IF_ACTIVE) && |
424 | (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) | |
c6c8fea2 SE |
425 | return; |
426 | ||
e9a4f295 | 427 | hard_iface->if_status = BATADV_IF_INACTIVE; |
c6c8fea2 | 428 | |
3e34819e SE |
429 | batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n", |
430 | hard_iface->net_dev->name); | |
c6c8fea2 | 431 | |
9563877e | 432 | batadv_update_min_mtu(hard_iface->soft_iface); |
c6c8fea2 SE |
433 | } |
434 | ||
cb4b0d48 AQ |
435 | /** |
436 | * batadv_master_del_slave - remove hard_iface from the current master interface | |
437 | * @slave: the interface enslaved in another master | |
438 | * @master: the master from which slave has to be removed | |
439 | * | |
440 | * Invoke ndo_del_slave on master passing slave as argument. In this way slave | |
441 | * is free'd and master can correctly change its internal state. | |
62fe710f SE |
442 | * |
443 | * Return: 0 on success, a negative value representing the error otherwise | |
cb4b0d48 AQ |
444 | */ |
445 | static int batadv_master_del_slave(struct batadv_hard_iface *slave, | |
446 | struct net_device *master) | |
447 | { | |
448 | int ret; | |
449 | ||
450 | if (!master) | |
451 | return 0; | |
452 | ||
453 | ret = -EBUSY; | |
454 | if (master->netdev_ops->ndo_del_slave) | |
455 | ret = master->netdev_ops->ndo_del_slave(master, slave->net_dev); | |
456 | ||
457 | return ret; | |
458 | } | |
459 | ||
56303d34 | 460 | int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface, |
2cd45a06 | 461 | struct net *net, const char *iface_name) |
c6c8fea2 | 462 | { |
56303d34 | 463 | struct batadv_priv *bat_priv; |
cb4b0d48 | 464 | struct net_device *soft_iface, *master; |
293e9338 | 465 | __be16 ethertype = htons(ETH_P_BATMAN); |
411d6ed9 | 466 | int max_header_len = batadv_max_header_len(); |
e44d8fe2 | 467 | int ret; |
c6c8fea2 | 468 | |
e9a4f295 | 469 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
c6c8fea2 SE |
470 | goto out; |
471 | ||
17a86915 | 472 | kref_get(&hard_iface->refcount); |
ed75ccbe | 473 | |
2cd45a06 | 474 | soft_iface = dev_get_by_name(net, iface_name); |
c6c8fea2 | 475 | |
e44d8fe2 | 476 | if (!soft_iface) { |
2cd45a06 | 477 | soft_iface = batadv_softif_create(net, iface_name); |
c6c8fea2 | 478 | |
e44d8fe2 SE |
479 | if (!soft_iface) { |
480 | ret = -ENOMEM; | |
c6c8fea2 | 481 | goto err; |
e44d8fe2 | 482 | } |
c6c8fea2 SE |
483 | |
484 | /* dev_get_by_name() increases the reference counter for us */ | |
e44d8fe2 SE |
485 | dev_hold(soft_iface); |
486 | } | |
487 | ||
04b482a2 | 488 | if (!batadv_softif_is_valid(soft_iface)) { |
86ceb360 | 489 | pr_err("Can't create batman mesh interface %s: already exists as regular interface\n", |
e44d8fe2 | 490 | soft_iface->name); |
e44d8fe2 | 491 | ret = -EINVAL; |
77af7575 | 492 | goto err_dev; |
c6c8fea2 SE |
493 | } |
494 | ||
cb4b0d48 AQ |
495 | /* check if the interface is enslaved in another virtual one and |
496 | * in that case unlink it first | |
497 | */ | |
498 | master = netdev_master_upper_dev_get(hard_iface->net_dev); | |
499 | ret = batadv_master_del_slave(hard_iface, master); | |
500 | if (ret) | |
501 | goto err_dev; | |
502 | ||
e44d8fe2 | 503 | hard_iface->soft_iface = soft_iface; |
e6c10f43 | 504 | bat_priv = netdev_priv(hard_iface->soft_iface); |
d0b9fd89 | 505 | |
6dffb044 | 506 | ret = netdev_master_upper_dev_link(hard_iface->net_dev, |
29bf24af | 507 | soft_iface, NULL, NULL); |
3dbd550b SE |
508 | if (ret) |
509 | goto err_dev; | |
510 | ||
29824a55 | 511 | ret = bat_priv->algo_ops->iface.enable(hard_iface); |
5346c35e | 512 | if (ret < 0) |
3dbd550b | 513 | goto err_upper; |
c6c8fea2 | 514 | |
e6c10f43 | 515 | hard_iface->if_num = bat_priv->num_ifaces; |
c6c8fea2 | 516 | bat_priv->num_ifaces++; |
e9a4f295 | 517 | hard_iface->if_status = BATADV_IF_INACTIVE; |
62446307 SW |
518 | ret = batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces); |
519 | if (ret < 0) { | |
29824a55 | 520 | bat_priv->algo_ops->iface.disable(hard_iface); |
62446307 SW |
521 | bat_priv->num_ifaces--; |
522 | hard_iface->if_status = BATADV_IF_NOT_IN_USE; | |
3dbd550b | 523 | goto err_upper; |
62446307 | 524 | } |
c6c8fea2 | 525 | |
d7d6de95 | 526 | kref_get(&hard_iface->refcount); |
7e071c79 | 527 | hard_iface->batman_adv_ptype.type = ethertype; |
3193e8fd | 528 | hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv; |
e6c10f43 ML |
529 | hard_iface->batman_adv_ptype.dev = hard_iface->net_dev; |
530 | dev_add_pack(&hard_iface->batman_adv_ptype); | |
c6c8fea2 | 531 | |
3e34819e SE |
532 | batadv_info(hard_iface->soft_iface, "Adding interface: %s\n", |
533 | hard_iface->net_dev->name); | |
c6c8fea2 | 534 | |
0aca2369 | 535 | if (atomic_read(&bat_priv->fragmentation) && |
411d6ed9 | 536 | hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len) |
3e34819e | 537 | batadv_info(hard_iface->soft_iface, |
411d6ed9 | 538 | "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. Packets going over this interface will be fragmented on layer2 which could impact the performance. Setting the MTU to %i would solve the problem.\n", |
3e34819e | 539 | hard_iface->net_dev->name, hard_iface->net_dev->mtu, |
411d6ed9 | 540 | ETH_DATA_LEN + max_header_len); |
c6c8fea2 | 541 | |
0aca2369 | 542 | if (!atomic_read(&bat_priv->fragmentation) && |
411d6ed9 | 543 | hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len) |
3e34819e | 544 | batadv_info(hard_iface->soft_iface, |
411d6ed9 | 545 | "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. If you experience problems getting traffic through try increasing the MTU to %i.\n", |
3e34819e | 546 | hard_iface->net_dev->name, hard_iface->net_dev->mtu, |
411d6ed9 | 547 | ETH_DATA_LEN + max_header_len); |
c6c8fea2 | 548 | |
18a1cb6e SE |
549 | if (batadv_hardif_is_iface_up(hard_iface)) |
550 | batadv_hardif_activate_interface(hard_iface); | |
c6c8fea2 | 551 | else |
3e34819e SE |
552 | batadv_err(hard_iface->soft_iface, |
553 | "Not using interface %s (retrying later): interface not active\n", | |
554 | hard_iface->net_dev->name); | |
c6c8fea2 | 555 | |
7bca68c7 SE |
556 | batadv_hardif_recalc_extra_skbroom(soft_iface); |
557 | ||
c6c8fea2 SE |
558 | out: |
559 | return 0; | |
560 | ||
3dbd550b SE |
561 | err_upper: |
562 | netdev_upper_dev_unlink(hard_iface->net_dev, soft_iface); | |
77af7575 | 563 | err_dev: |
3dbd550b | 564 | hard_iface->soft_iface = NULL; |
77af7575 | 565 | dev_put(soft_iface); |
c6c8fea2 | 566 | err: |
82047ad7 | 567 | batadv_hardif_put(hard_iface); |
e44d8fe2 | 568 | return ret; |
c6c8fea2 SE |
569 | } |
570 | ||
a15fd361 SE |
571 | void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface, |
572 | enum batadv_hard_if_cleanup autodel) | |
c6c8fea2 | 573 | { |
56303d34 SE |
574 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
575 | struct batadv_hard_iface *primary_if = NULL; | |
c6c8fea2 | 576 | |
f2d23861 | 577 | batadv_hardif_deactivate_interface(hard_iface); |
c6c8fea2 | 578 | |
e9a4f295 | 579 | if (hard_iface->if_status != BATADV_IF_INACTIVE) |
32ae9b22 | 580 | goto out; |
c6c8fea2 | 581 | |
3e34819e SE |
582 | batadv_info(hard_iface->soft_iface, "Removing interface: %s\n", |
583 | hard_iface->net_dev->name); | |
e6c10f43 | 584 | dev_remove_pack(&hard_iface->batman_adv_ptype); |
d7d6de95 | 585 | batadv_hardif_put(hard_iface); |
c6c8fea2 SE |
586 | |
587 | bat_priv->num_ifaces--; | |
7d211efc | 588 | batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces); |
c6c8fea2 | 589 | |
e5d89254 | 590 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 | 591 | if (hard_iface == primary_if) { |
56303d34 | 592 | struct batadv_hard_iface *new_if; |
c6c8fea2 | 593 | |
18a1cb6e SE |
594 | new_if = batadv_hardif_get_active(hard_iface->soft_iface); |
595 | batadv_primary_if_select(bat_priv, new_if); | |
c6c8fea2 SE |
596 | |
597 | if (new_if) | |
82047ad7 | 598 | batadv_hardif_put(new_if); |
c6c8fea2 SE |
599 | } |
600 | ||
29824a55 | 601 | bat_priv->algo_ops->iface.disable(hard_iface); |
e9a4f295 | 602 | hard_iface->if_status = BATADV_IF_NOT_IN_USE; |
c6c8fea2 | 603 | |
e6c10f43 | 604 | /* delete all references to this hard_iface */ |
7d211efc | 605 | batadv_purge_orig_ref(bat_priv); |
9455e34c | 606 | batadv_purge_outstanding_packets(bat_priv, hard_iface); |
e6c10f43 | 607 | dev_put(hard_iface->soft_iface); |
c6c8fea2 | 608 | |
a5256f7e | 609 | netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface); |
7bca68c7 | 610 | batadv_hardif_recalc_extra_skbroom(hard_iface->soft_iface); |
a5256f7e | 611 | |
c6c8fea2 | 612 | /* nobody uses this interface anymore */ |
8257f55a AQ |
613 | if (!bat_priv->num_ifaces) { |
614 | batadv_gw_check_client_stop(bat_priv); | |
615 | ||
616 | if (autodel == BATADV_IF_CLEANUP_AUTO) | |
617 | batadv_softif_destroy_sysfs(hard_iface->soft_iface); | |
618 | } | |
c6c8fea2 | 619 | |
e6c10f43 | 620 | hard_iface->soft_iface = NULL; |
82047ad7 | 621 | batadv_hardif_put(hard_iface); |
32ae9b22 ML |
622 | |
623 | out: | |
624 | if (primary_if) | |
82047ad7 | 625 | batadv_hardif_put(primary_if); |
c6c8fea2 SE |
626 | } |
627 | ||
5bc44dc8 SW |
628 | /** |
629 | * batadv_hardif_remove_interface_finish - cleans up the remains of a hardif | |
630 | * @work: work queue item | |
631 | * | |
632 | * Free the parts of the hard interface which can not be removed under | |
633 | * rtnl lock (to prevent deadlock situations). | |
634 | */ | |
635 | static void batadv_hardif_remove_interface_finish(struct work_struct *work) | |
636 | { | |
637 | struct batadv_hard_iface *hard_iface; | |
638 | ||
639 | hard_iface = container_of(work, struct batadv_hard_iface, | |
640 | cleanup_work); | |
641 | ||
5bc7c1eb | 642 | batadv_debugfs_del_hardif(hard_iface); |
5bc44dc8 | 643 | batadv_sysfs_del_hardif(&hard_iface->hardif_obj); |
82047ad7 | 644 | batadv_hardif_put(hard_iface); |
5bc44dc8 SW |
645 | } |
646 | ||
56303d34 | 647 | static struct batadv_hard_iface * |
18a1cb6e | 648 | batadv_hardif_add_interface(struct net_device *net_dev) |
c6c8fea2 | 649 | { |
56303d34 | 650 | struct batadv_hard_iface *hard_iface; |
c6c8fea2 SE |
651 | int ret; |
652 | ||
c3caf519 SE |
653 | ASSERT_RTNL(); |
654 | ||
4b426b10 | 655 | if (!batadv_is_valid_iface(net_dev)) |
c6c8fea2 SE |
656 | goto out; |
657 | ||
658 | dev_hold(net_dev); | |
659 | ||
7db3fc29 | 660 | hard_iface = kzalloc(sizeof(*hard_iface), GFP_ATOMIC); |
320f422f | 661 | if (!hard_iface) |
c6c8fea2 | 662 | goto release_dev; |
c6c8fea2 | 663 | |
5853e22c | 664 | ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev); |
c6c8fea2 SE |
665 | if (ret) |
666 | goto free_if; | |
667 | ||
e6c10f43 ML |
668 | hard_iface->if_num = -1; |
669 | hard_iface->net_dev = net_dev; | |
670 | hard_iface->soft_iface = NULL; | |
e9a4f295 | 671 | hard_iface->if_status = BATADV_IF_NOT_IN_USE; |
5bc7c1eb SW |
672 | |
673 | ret = batadv_debugfs_add_hardif(hard_iface); | |
674 | if (ret) | |
675 | goto free_sysfs; | |
676 | ||
e6c10f43 | 677 | INIT_LIST_HEAD(&hard_iface->list); |
cef63419 | 678 | INIT_HLIST_HEAD(&hard_iface->neigh_list); |
5bc44dc8 SW |
679 | INIT_WORK(&hard_iface->cleanup_work, |
680 | batadv_hardif_remove_interface_finish); | |
681 | ||
cef63419 ML |
682 | spin_lock_init(&hard_iface->neigh_list_lock); |
683 | ||
caf65bfc MS |
684 | hard_iface->num_bcasts = BATADV_NUM_BCASTS_DEFAULT; |
685 | if (batadv_is_wifi_netdev(net_dev)) | |
686 | hard_iface->num_bcasts = BATADV_NUM_BCASTS_WIRELESS; | |
687 | ||
7db682d1 ML |
688 | batadv_v_hardif_init(hard_iface); |
689 | ||
ed75ccbe | 690 | /* extra reference for return */ |
7a659d56 SE |
691 | kref_init(&hard_iface->refcount); |
692 | kref_get(&hard_iface->refcount); | |
c6c8fea2 | 693 | |
18a1cb6e | 694 | batadv_check_known_mac_addr(hard_iface->net_dev); |
3193e8fd | 695 | list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list); |
c6c8fea2 | 696 | |
e6c10f43 | 697 | return hard_iface; |
c6c8fea2 | 698 | |
5bc7c1eb SW |
699 | free_sysfs: |
700 | batadv_sysfs_del_hardif(&hard_iface->hardif_obj); | |
c6c8fea2 | 701 | free_if: |
e6c10f43 | 702 | kfree(hard_iface); |
c6c8fea2 SE |
703 | release_dev: |
704 | dev_put(net_dev); | |
705 | out: | |
706 | return NULL; | |
707 | } | |
708 | ||
56303d34 | 709 | static void batadv_hardif_remove_interface(struct batadv_hard_iface *hard_iface) |
c6c8fea2 | 710 | { |
c3caf519 SE |
711 | ASSERT_RTNL(); |
712 | ||
c6c8fea2 | 713 | /* first deactivate interface */ |
e9a4f295 | 714 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
a15fd361 SE |
715 | batadv_hardif_disable_interface(hard_iface, |
716 | BATADV_IF_CLEANUP_AUTO); | |
c6c8fea2 | 717 | |
e9a4f295 | 718 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
c6c8fea2 SE |
719 | return; |
720 | ||
e9a4f295 | 721 | hard_iface->if_status = BATADV_IF_TO_BE_REMOVED; |
5bc44dc8 | 722 | queue_work(batadv_event_workqueue, &hard_iface->cleanup_work); |
c6c8fea2 SE |
723 | } |
724 | ||
9563877e | 725 | void batadv_hardif_remove_interfaces(void) |
c6c8fea2 | 726 | { |
56303d34 | 727 | struct batadv_hard_iface *hard_iface, *hard_iface_tmp; |
c6c8fea2 | 728 | |
c3caf519 | 729 | rtnl_lock(); |
e6c10f43 | 730 | list_for_each_entry_safe(hard_iface, hard_iface_tmp, |
3193e8fd | 731 | &batadv_hardif_list, list) { |
e6c10f43 | 732 | list_del_rcu(&hard_iface->list); |
18a1cb6e | 733 | batadv_hardif_remove_interface(hard_iface); |
c6c8fea2 SE |
734 | } |
735 | rtnl_unlock(); | |
736 | } | |
737 | ||
18a1cb6e SE |
738 | static int batadv_hard_if_event(struct notifier_block *this, |
739 | unsigned long event, void *ptr) | |
c6c8fea2 | 740 | { |
351638e7 | 741 | struct net_device *net_dev = netdev_notifier_info_to_dev(ptr); |
56303d34 SE |
742 | struct batadv_hard_iface *hard_iface; |
743 | struct batadv_hard_iface *primary_if = NULL; | |
744 | struct batadv_priv *bat_priv; | |
c6c8fea2 | 745 | |
37130293 SE |
746 | if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) { |
747 | batadv_sysfs_add_meshif(net_dev); | |
5d2c05b2 AQ |
748 | bat_priv = netdev_priv(net_dev); |
749 | batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS); | |
37130293 SE |
750 | return NOTIFY_DONE; |
751 | } | |
752 | ||
56303d34 | 753 | hard_iface = batadv_hardif_get_by_netdev(net_dev); |
a1a66b11 AL |
754 | if (!hard_iface && (event == NETDEV_REGISTER || |
755 | event == NETDEV_POST_TYPE_CHANGE)) | |
18a1cb6e | 756 | hard_iface = batadv_hardif_add_interface(net_dev); |
c6c8fea2 | 757 | |
e6c10f43 | 758 | if (!hard_iface) |
c6c8fea2 SE |
759 | goto out; |
760 | ||
761 | switch (event) { | |
762 | case NETDEV_UP: | |
18a1cb6e | 763 | batadv_hardif_activate_interface(hard_iface); |
c6c8fea2 SE |
764 | break; |
765 | case NETDEV_GOING_DOWN: | |
766 | case NETDEV_DOWN: | |
18a1cb6e | 767 | batadv_hardif_deactivate_interface(hard_iface); |
c6c8fea2 SE |
768 | break; |
769 | case NETDEV_UNREGISTER: | |
a1a66b11 | 770 | case NETDEV_PRE_TYPE_CHANGE: |
e6c10f43 | 771 | list_del_rcu(&hard_iface->list); |
c6c8fea2 | 772 | |
18a1cb6e | 773 | batadv_hardif_remove_interface(hard_iface); |
c6c8fea2 SE |
774 | break; |
775 | case NETDEV_CHANGEMTU: | |
e6c10f43 | 776 | if (hard_iface->soft_iface) |
9563877e | 777 | batadv_update_min_mtu(hard_iface->soft_iface); |
c6c8fea2 SE |
778 | break; |
779 | case NETDEV_CHANGEADDR: | |
e9a4f295 | 780 | if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) |
c6c8fea2 SE |
781 | goto hardif_put; |
782 | ||
18a1cb6e | 783 | batadv_check_known_mac_addr(hard_iface->net_dev); |
c6c8fea2 | 784 | |
e6c10f43 | 785 | bat_priv = netdev_priv(hard_iface->soft_iface); |
29824a55 | 786 | bat_priv->algo_ops->iface.update_mac(hard_iface); |
01c4224b | 787 | |
e5d89254 | 788 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 ML |
789 | if (!primary_if) |
790 | goto hardif_put; | |
791 | ||
792 | if (hard_iface == primary_if) | |
18a1cb6e | 793 | batadv_primary_if_update_addr(bat_priv, NULL); |
c6c8fea2 SE |
794 | break; |
795 | default: | |
796 | break; | |
f81c6224 | 797 | } |
c6c8fea2 SE |
798 | |
799 | hardif_put: | |
82047ad7 | 800 | batadv_hardif_put(hard_iface); |
c6c8fea2 | 801 | out: |
32ae9b22 | 802 | if (primary_if) |
82047ad7 | 803 | batadv_hardif_put(primary_if); |
c6c8fea2 SE |
804 | return NOTIFY_DONE; |
805 | } | |
806 | ||
9563877e | 807 | struct notifier_block batadv_hard_if_notifier = { |
18a1cb6e | 808 | .notifier_call = batadv_hard_if_event, |
c6c8fea2 | 809 | }; |