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