Commit | Line | Data |
---|---|---|
9cfc7bd6 | 1 | /* Copyright (C) 2007-2012 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 | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
17 | * 02110-1301, USA | |
c6c8fea2 SE |
18 | */ |
19 | ||
20 | #include "main.h" | |
21 | #include "hard-interface.h" | |
22 | #include "soft-interface.h" | |
23 | #include "send.h" | |
24 | #include "translation-table.h" | |
25 | #include "routing.h" | |
26 | #include "bat_sysfs.h" | |
27 | #include "originator.h" | |
28 | #include "hash.h" | |
23721387 | 29 | #include "bridge_loop_avoidance.h" |
c6c8fea2 SE |
30 | |
31 | #include <linux/if_arp.h> | |
32 | ||
9563877e | 33 | void batadv_hardif_free_rcu(struct rcu_head *rcu) |
c6c8fea2 | 34 | { |
e6c10f43 | 35 | struct hard_iface *hard_iface; |
c6c8fea2 | 36 | |
e6c10f43 ML |
37 | hard_iface = container_of(rcu, struct hard_iface, rcu); |
38 | dev_put(hard_iface->net_dev); | |
39 | kfree(hard_iface); | |
c6c8fea2 SE |
40 | } |
41 | ||
9563877e | 42 | struct hard_iface *batadv_hardif_get_by_netdev(const struct net_device *net_dev) |
c6c8fea2 | 43 | { |
e6c10f43 | 44 | struct hard_iface *hard_iface; |
c6c8fea2 SE |
45 | |
46 | rcu_read_lock(); | |
3193e8fd | 47 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
e6c10f43 ML |
48 | if (hard_iface->net_dev == net_dev && |
49 | atomic_inc_not_zero(&hard_iface->refcount)) | |
c6c8fea2 SE |
50 | goto out; |
51 | } | |
52 | ||
e6c10f43 | 53 | hard_iface = NULL; |
c6c8fea2 SE |
54 | |
55 | out: | |
c6c8fea2 | 56 | rcu_read_unlock(); |
e6c10f43 | 57 | return hard_iface; |
c6c8fea2 SE |
58 | } |
59 | ||
18a1cb6e | 60 | static int batadv_is_valid_iface(const struct net_device *net_dev) |
c6c8fea2 SE |
61 | { |
62 | if (net_dev->flags & IFF_LOOPBACK) | |
63 | return 0; | |
64 | ||
65 | if (net_dev->type != ARPHRD_ETHER) | |
66 | return 0; | |
67 | ||
68 | if (net_dev->addr_len != ETH_ALEN) | |
69 | return 0; | |
70 | ||
71 | /* no batman over batman */ | |
04b482a2 | 72 | if (batadv_softif_is_valid(net_dev)) |
c6c8fea2 | 73 | return 0; |
c6c8fea2 | 74 | |
c6c8fea2 SE |
75 | return 1; |
76 | } | |
77 | ||
18a1cb6e SE |
78 | static struct hard_iface * |
79 | batadv_hardif_get_active(const struct net_device *soft_iface) | |
c6c8fea2 | 80 | { |
e6c10f43 | 81 | struct hard_iface *hard_iface; |
c6c8fea2 SE |
82 | |
83 | rcu_read_lock(); | |
3193e8fd | 84 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
e6c10f43 | 85 | if (hard_iface->soft_iface != soft_iface) |
c6c8fea2 SE |
86 | continue; |
87 | ||
e6c10f43 ML |
88 | if (hard_iface->if_status == IF_ACTIVE && |
89 | atomic_inc_not_zero(&hard_iface->refcount)) | |
c6c8fea2 SE |
90 | goto out; |
91 | } | |
92 | ||
e6c10f43 | 93 | hard_iface = NULL; |
c6c8fea2 SE |
94 | |
95 | out: | |
c6c8fea2 | 96 | rcu_read_unlock(); |
e6c10f43 | 97 | return hard_iface; |
c6c8fea2 SE |
98 | } |
99 | ||
18a1cb6e SE |
100 | static void batadv_primary_if_update_addr(struct bat_priv *bat_priv, |
101 | struct hard_iface *oldif) | |
c6c8fea2 SE |
102 | { |
103 | struct vis_packet *vis_packet; | |
32ae9b22 ML |
104 | struct hard_iface *primary_if; |
105 | ||
e5d89254 | 106 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 ML |
107 | if (!primary_if) |
108 | goto out; | |
c6c8fea2 SE |
109 | |
110 | vis_packet = (struct vis_packet *) | |
111 | bat_priv->my_vis_info->skb_packet->data; | |
32ae9b22 | 112 | memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN); |
c6c8fea2 | 113 | memcpy(vis_packet->sender_orig, |
32ae9b22 ML |
114 | primary_if->net_dev->dev_addr, ETH_ALEN); |
115 | ||
08adf151 | 116 | batadv_bla_update_orig_address(bat_priv, primary_if, oldif); |
32ae9b22 ML |
117 | out: |
118 | if (primary_if) | |
e5d89254 | 119 | batadv_hardif_free_ref(primary_if); |
c6c8fea2 SE |
120 | } |
121 | ||
18a1cb6e SE |
122 | static void batadv_primary_if_select(struct bat_priv *bat_priv, |
123 | struct hard_iface *new_hard_iface) | |
c6c8fea2 | 124 | { |
32ae9b22 | 125 | struct hard_iface *curr_hard_iface; |
c6c8fea2 | 126 | |
c3caf519 | 127 | ASSERT_RTNL(); |
c6c8fea2 | 128 | |
32ae9b22 ML |
129 | if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount)) |
130 | new_hard_iface = NULL; | |
c6c8fea2 | 131 | |
728cbc6a | 132 | curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1); |
32ae9b22 | 133 | rcu_assign_pointer(bat_priv->primary_if, new_hard_iface); |
c6c8fea2 | 134 | |
32ae9b22 | 135 | if (!new_hard_iface) |
23721387 | 136 | goto out; |
32ae9b22 | 137 | |
cd8b78e7 | 138 | bat_priv->bat_algo_ops->bat_primary_iface_set(new_hard_iface); |
18a1cb6e | 139 | batadv_primary_if_update_addr(bat_priv, curr_hard_iface); |
23721387 SW |
140 | |
141 | out: | |
142 | if (curr_hard_iface) | |
e5d89254 | 143 | batadv_hardif_free_ref(curr_hard_iface); |
c6c8fea2 SE |
144 | } |
145 | ||
18a1cb6e | 146 | static bool batadv_hardif_is_iface_up(const struct hard_iface *hard_iface) |
c6c8fea2 | 147 | { |
e6c10f43 | 148 | if (hard_iface->net_dev->flags & IFF_UP) |
c6c8fea2 SE |
149 | return true; |
150 | ||
151 | return false; | |
152 | } | |
153 | ||
18a1cb6e | 154 | static void batadv_check_known_mac_addr(const struct net_device *net_dev) |
c6c8fea2 | 155 | { |
747e4221 | 156 | const struct hard_iface *hard_iface; |
c6c8fea2 SE |
157 | |
158 | rcu_read_lock(); | |
3193e8fd | 159 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
e6c10f43 ML |
160 | if ((hard_iface->if_status != IF_ACTIVE) && |
161 | (hard_iface->if_status != IF_TO_BE_ACTIVATED)) | |
c6c8fea2 SE |
162 | continue; |
163 | ||
e6c10f43 | 164 | if (hard_iface->net_dev == net_dev) |
c6c8fea2 SE |
165 | continue; |
166 | ||
1eda58bf SE |
167 | if (!batadv_compare_eth(hard_iface->net_dev->dev_addr, |
168 | net_dev->dev_addr)) | |
c6c8fea2 SE |
169 | continue; |
170 | ||
67969581 SE |
171 | pr_warn("The newly added mac address (%pM) already exists on: %s\n", |
172 | net_dev->dev_addr, hard_iface->net_dev->name); | |
173 | pr_warn("It is strongly recommended to keep mac addresses unique to avoid problems!\n"); | |
c6c8fea2 SE |
174 | } |
175 | rcu_read_unlock(); | |
176 | } | |
177 | ||
9563877e | 178 | int batadv_hardif_min_mtu(struct net_device *soft_iface) |
c6c8fea2 | 179 | { |
747e4221 SE |
180 | const struct bat_priv *bat_priv = netdev_priv(soft_iface); |
181 | const struct hard_iface *hard_iface; | |
c6c8fea2 | 182 | /* allow big frames if all devices are capable to do so |
9cfc7bd6 SE |
183 | * (have MTU > 1500 + BAT_HEADER_LEN) |
184 | */ | |
c6c8fea2 SE |
185 | int min_mtu = ETH_DATA_LEN; |
186 | ||
187 | if (atomic_read(&bat_priv->fragmentation)) | |
188 | goto out; | |
189 | ||
190 | rcu_read_lock(); | |
3193e8fd | 191 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
e6c10f43 ML |
192 | if ((hard_iface->if_status != IF_ACTIVE) && |
193 | (hard_iface->if_status != IF_TO_BE_ACTIVATED)) | |
c6c8fea2 SE |
194 | continue; |
195 | ||
e6c10f43 | 196 | if (hard_iface->soft_iface != soft_iface) |
c6c8fea2 SE |
197 | continue; |
198 | ||
e6c10f43 | 199 | min_mtu = min_t(int, hard_iface->net_dev->mtu - BAT_HEADER_LEN, |
c6c8fea2 SE |
200 | min_mtu); |
201 | } | |
202 | rcu_read_unlock(); | |
203 | out: | |
204 | return min_mtu; | |
205 | } | |
206 | ||
207 | /* adjusts the MTU if a new interface with a smaller MTU appeared. */ | |
9563877e | 208 | void batadv_update_min_mtu(struct net_device *soft_iface) |
c6c8fea2 SE |
209 | { |
210 | int min_mtu; | |
211 | ||
9563877e | 212 | min_mtu = batadv_hardif_min_mtu(soft_iface); |
c6c8fea2 SE |
213 | if (soft_iface->mtu != min_mtu) |
214 | soft_iface->mtu = min_mtu; | |
215 | } | |
216 | ||
18a1cb6e | 217 | static void batadv_hardif_activate_interface(struct hard_iface *hard_iface) |
c6c8fea2 SE |
218 | { |
219 | struct bat_priv *bat_priv; | |
32ae9b22 | 220 | struct hard_iface *primary_if = NULL; |
c6c8fea2 | 221 | |
e6c10f43 | 222 | if (hard_iface->if_status != IF_INACTIVE) |
32ae9b22 | 223 | goto out; |
c6c8fea2 | 224 | |
e6c10f43 | 225 | bat_priv = netdev_priv(hard_iface->soft_iface); |
c6c8fea2 | 226 | |
c3229398 | 227 | bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface); |
e6c10f43 | 228 | hard_iface->if_status = IF_TO_BE_ACTIVATED; |
c6c8fea2 | 229 | |
9cfc7bd6 | 230 | /* the first active interface becomes our primary interface or |
015758d0 | 231 | * the next active interface after the old primary interface was removed |
c6c8fea2 | 232 | */ |
e5d89254 | 233 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 | 234 | if (!primary_if) |
18a1cb6e | 235 | batadv_primary_if_select(bat_priv, hard_iface); |
c6c8fea2 | 236 | |
3e34819e SE |
237 | batadv_info(hard_iface->soft_iface, "Interface activated: %s\n", |
238 | hard_iface->net_dev->name); | |
c6c8fea2 | 239 | |
9563877e | 240 | batadv_update_min_mtu(hard_iface->soft_iface); |
32ae9b22 ML |
241 | |
242 | out: | |
243 | if (primary_if) | |
e5d89254 | 244 | batadv_hardif_free_ref(primary_if); |
c6c8fea2 SE |
245 | } |
246 | ||
18a1cb6e | 247 | static void batadv_hardif_deactivate_interface(struct hard_iface *hard_iface) |
c6c8fea2 | 248 | { |
e6c10f43 ML |
249 | if ((hard_iface->if_status != IF_ACTIVE) && |
250 | (hard_iface->if_status != IF_TO_BE_ACTIVATED)) | |
c6c8fea2 SE |
251 | return; |
252 | ||
e6c10f43 | 253 | hard_iface->if_status = IF_INACTIVE; |
c6c8fea2 | 254 | |
3e34819e SE |
255 | batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n", |
256 | hard_iface->net_dev->name); | |
c6c8fea2 | 257 | |
9563877e | 258 | batadv_update_min_mtu(hard_iface->soft_iface); |
c6c8fea2 SE |
259 | } |
260 | ||
9563877e SE |
261 | int batadv_hardif_enable_interface(struct hard_iface *hard_iface, |
262 | const char *iface_name) | |
c6c8fea2 SE |
263 | { |
264 | struct bat_priv *bat_priv; | |
e44d8fe2 | 265 | struct net_device *soft_iface; |
7e071c79 | 266 | __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN); |
e44d8fe2 | 267 | int ret; |
c6c8fea2 | 268 | |
e6c10f43 | 269 | if (hard_iface->if_status != IF_NOT_IN_USE) |
c6c8fea2 SE |
270 | goto out; |
271 | ||
e6c10f43 | 272 | if (!atomic_inc_not_zero(&hard_iface->refcount)) |
ed75ccbe ML |
273 | goto out; |
274 | ||
6e242f90 ML |
275 | /* hard-interface is part of a bridge */ |
276 | if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT) | |
86ceb360 | 277 | pr_err("You are about to enable batman-adv on '%s' which already is part of a bridge. Unless you know exactly what you are doing this is probably wrong and won't work the way you think it would.\n", |
6e242f90 ML |
278 | hard_iface->net_dev->name); |
279 | ||
e44d8fe2 | 280 | soft_iface = dev_get_by_name(&init_net, iface_name); |
c6c8fea2 | 281 | |
e44d8fe2 | 282 | if (!soft_iface) { |
04b482a2 | 283 | soft_iface = batadv_softif_create(iface_name); |
c6c8fea2 | 284 | |
e44d8fe2 SE |
285 | if (!soft_iface) { |
286 | ret = -ENOMEM; | |
c6c8fea2 | 287 | goto err; |
e44d8fe2 | 288 | } |
c6c8fea2 SE |
289 | |
290 | /* dev_get_by_name() increases the reference counter for us */ | |
e44d8fe2 SE |
291 | dev_hold(soft_iface); |
292 | } | |
293 | ||
04b482a2 | 294 | if (!batadv_softif_is_valid(soft_iface)) { |
86ceb360 | 295 | pr_err("Can't create batman mesh interface %s: already exists as regular interface\n", |
e44d8fe2 | 296 | soft_iface->name); |
e44d8fe2 | 297 | ret = -EINVAL; |
77af7575 | 298 | goto err_dev; |
c6c8fea2 SE |
299 | } |
300 | ||
e44d8fe2 | 301 | hard_iface->soft_iface = soft_iface; |
e6c10f43 | 302 | bat_priv = netdev_priv(hard_iface->soft_iface); |
d0b9fd89 | 303 | |
77af7575 | 304 | ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface); |
5346c35e | 305 | if (ret < 0) |
77af7575 | 306 | goto err_dev; |
c6c8fea2 | 307 | |
e6c10f43 | 308 | hard_iface->if_num = bat_priv->num_ifaces; |
c6c8fea2 | 309 | bat_priv->num_ifaces++; |
e6c10f43 | 310 | hard_iface->if_status = IF_INACTIVE; |
7d211efc | 311 | batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces); |
c6c8fea2 | 312 | |
7e071c79 | 313 | hard_iface->batman_adv_ptype.type = ethertype; |
3193e8fd | 314 | hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv; |
e6c10f43 ML |
315 | hard_iface->batman_adv_ptype.dev = hard_iface->net_dev; |
316 | dev_add_pack(&hard_iface->batman_adv_ptype); | |
c6c8fea2 | 317 | |
e6c10f43 | 318 | atomic_set(&hard_iface->frag_seqno, 1); |
3e34819e SE |
319 | batadv_info(hard_iface->soft_iface, "Adding interface: %s\n", |
320 | hard_iface->net_dev->name); | |
c6c8fea2 | 321 | |
e6c10f43 | 322 | if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu < |
c6c8fea2 | 323 | ETH_DATA_LEN + BAT_HEADER_LEN) |
3e34819e SE |
324 | batadv_info(hard_iface->soft_iface, |
325 | "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 %zi would solve the problem.\n", | |
326 | hard_iface->net_dev->name, hard_iface->net_dev->mtu, | |
327 | ETH_DATA_LEN + BAT_HEADER_LEN); | |
c6c8fea2 | 328 | |
e6c10f43 | 329 | if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu < |
c6c8fea2 | 330 | ETH_DATA_LEN + BAT_HEADER_LEN) |
3e34819e SE |
331 | batadv_info(hard_iface->soft_iface, |
332 | "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 %zi.\n", | |
333 | hard_iface->net_dev->name, hard_iface->net_dev->mtu, | |
334 | ETH_DATA_LEN + BAT_HEADER_LEN); | |
c6c8fea2 | 335 | |
18a1cb6e SE |
336 | if (batadv_hardif_is_iface_up(hard_iface)) |
337 | batadv_hardif_activate_interface(hard_iface); | |
c6c8fea2 | 338 | else |
3e34819e SE |
339 | batadv_err(hard_iface->soft_iface, |
340 | "Not using interface %s (retrying later): interface not active\n", | |
341 | hard_iface->net_dev->name); | |
c6c8fea2 SE |
342 | |
343 | /* begin scheduling originator messages on that interface */ | |
9455e34c | 344 | batadv_schedule_bat_ogm(hard_iface); |
c6c8fea2 SE |
345 | |
346 | out: | |
347 | return 0; | |
348 | ||
77af7575 ML |
349 | err_dev: |
350 | dev_put(soft_iface); | |
c6c8fea2 | 351 | err: |
e5d89254 | 352 | batadv_hardif_free_ref(hard_iface); |
e44d8fe2 | 353 | return ret; |
c6c8fea2 SE |
354 | } |
355 | ||
9563877e | 356 | void batadv_hardif_disable_interface(struct hard_iface *hard_iface) |
c6c8fea2 | 357 | { |
e6c10f43 | 358 | struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
32ae9b22 | 359 | struct hard_iface *primary_if = NULL; |
c6c8fea2 | 360 | |
e6c10f43 | 361 | if (hard_iface->if_status == IF_ACTIVE) |
18a1cb6e | 362 | batadv_hardif_deactivate_interface(hard_iface); |
c6c8fea2 | 363 | |
e6c10f43 | 364 | if (hard_iface->if_status != IF_INACTIVE) |
32ae9b22 | 365 | goto out; |
c6c8fea2 | 366 | |
3e34819e SE |
367 | batadv_info(hard_iface->soft_iface, "Removing interface: %s\n", |
368 | hard_iface->net_dev->name); | |
e6c10f43 | 369 | dev_remove_pack(&hard_iface->batman_adv_ptype); |
c6c8fea2 SE |
370 | |
371 | bat_priv->num_ifaces--; | |
7d211efc | 372 | batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces); |
c6c8fea2 | 373 | |
e5d89254 | 374 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 | 375 | if (hard_iface == primary_if) { |
e6c10f43 | 376 | struct hard_iface *new_if; |
c6c8fea2 | 377 | |
18a1cb6e SE |
378 | new_if = batadv_hardif_get_active(hard_iface->soft_iface); |
379 | batadv_primary_if_select(bat_priv, new_if); | |
c6c8fea2 SE |
380 | |
381 | if (new_if) | |
e5d89254 | 382 | batadv_hardif_free_ref(new_if); |
c6c8fea2 SE |
383 | } |
384 | ||
00a50076 | 385 | bat_priv->bat_algo_ops->bat_iface_disable(hard_iface); |
e6c10f43 | 386 | hard_iface->if_status = IF_NOT_IN_USE; |
c6c8fea2 | 387 | |
e6c10f43 | 388 | /* delete all references to this hard_iface */ |
7d211efc | 389 | batadv_purge_orig_ref(bat_priv); |
9455e34c | 390 | batadv_purge_outstanding_packets(bat_priv, hard_iface); |
e6c10f43 | 391 | dev_put(hard_iface->soft_iface); |
c6c8fea2 SE |
392 | |
393 | /* nobody uses this interface anymore */ | |
394 | if (!bat_priv->num_ifaces) | |
04b482a2 | 395 | batadv_softif_destroy(hard_iface->soft_iface); |
c6c8fea2 | 396 | |
e6c10f43 | 397 | hard_iface->soft_iface = NULL; |
e5d89254 | 398 | batadv_hardif_free_ref(hard_iface); |
32ae9b22 ML |
399 | |
400 | out: | |
401 | if (primary_if) | |
e5d89254 | 402 | batadv_hardif_free_ref(primary_if); |
c6c8fea2 SE |
403 | } |
404 | ||
18a1cb6e SE |
405 | static struct hard_iface * |
406 | batadv_hardif_add_interface(struct net_device *net_dev) | |
c6c8fea2 | 407 | { |
e6c10f43 | 408 | struct hard_iface *hard_iface; |
c6c8fea2 SE |
409 | int ret; |
410 | ||
c3caf519 SE |
411 | ASSERT_RTNL(); |
412 | ||
18a1cb6e | 413 | ret = batadv_is_valid_iface(net_dev); |
c6c8fea2 SE |
414 | if (ret != 1) |
415 | goto out; | |
416 | ||
417 | dev_hold(net_dev); | |
418 | ||
704509b8 | 419 | hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC); |
320f422f | 420 | if (!hard_iface) |
c6c8fea2 | 421 | goto release_dev; |
c6c8fea2 | 422 | |
5853e22c | 423 | ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev); |
c6c8fea2 SE |
424 | if (ret) |
425 | goto free_if; | |
426 | ||
e6c10f43 ML |
427 | hard_iface->if_num = -1; |
428 | hard_iface->net_dev = net_dev; | |
429 | hard_iface->soft_iface = NULL; | |
430 | hard_iface->if_status = IF_NOT_IN_USE; | |
431 | INIT_LIST_HEAD(&hard_iface->list); | |
ed75ccbe | 432 | /* extra reference for return */ |
e6c10f43 | 433 | atomic_set(&hard_iface->refcount, 2); |
c6c8fea2 | 434 | |
18a1cb6e | 435 | batadv_check_known_mac_addr(hard_iface->net_dev); |
3193e8fd | 436 | list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list); |
c6c8fea2 | 437 | |
9cfc7bd6 | 438 | /* This can't be called via a bat_priv callback because |
8140625e ML |
439 | * we have no bat_priv yet. |
440 | */ | |
441 | atomic_set(&hard_iface->seqno, 1); | |
442 | hard_iface->packet_buff = NULL; | |
443 | ||
e6c10f43 | 444 | return hard_iface; |
c6c8fea2 SE |
445 | |
446 | free_if: | |
e6c10f43 | 447 | kfree(hard_iface); |
c6c8fea2 SE |
448 | release_dev: |
449 | dev_put(net_dev); | |
450 | out: | |
451 | return NULL; | |
452 | } | |
453 | ||
18a1cb6e | 454 | static void batadv_hardif_remove_interface(struct hard_iface *hard_iface) |
c6c8fea2 | 455 | { |
c3caf519 SE |
456 | ASSERT_RTNL(); |
457 | ||
c6c8fea2 | 458 | /* first deactivate interface */ |
e6c10f43 | 459 | if (hard_iface->if_status != IF_NOT_IN_USE) |
9563877e | 460 | batadv_hardif_disable_interface(hard_iface); |
c6c8fea2 | 461 | |
e6c10f43 | 462 | if (hard_iface->if_status != IF_NOT_IN_USE) |
c6c8fea2 SE |
463 | return; |
464 | ||
e6c10f43 | 465 | hard_iface->if_status = IF_TO_BE_REMOVED; |
5853e22c | 466 | batadv_sysfs_del_hardif(&hard_iface->hardif_obj); |
e5d89254 | 467 | batadv_hardif_free_ref(hard_iface); |
c6c8fea2 SE |
468 | } |
469 | ||
9563877e | 470 | void batadv_hardif_remove_interfaces(void) |
c6c8fea2 | 471 | { |
e6c10f43 | 472 | struct hard_iface *hard_iface, *hard_iface_tmp; |
c6c8fea2 | 473 | |
c3caf519 | 474 | rtnl_lock(); |
e6c10f43 | 475 | list_for_each_entry_safe(hard_iface, hard_iface_tmp, |
3193e8fd | 476 | &batadv_hardif_list, list) { |
e6c10f43 | 477 | list_del_rcu(&hard_iface->list); |
18a1cb6e | 478 | batadv_hardif_remove_interface(hard_iface); |
c6c8fea2 SE |
479 | } |
480 | rtnl_unlock(); | |
481 | } | |
482 | ||
18a1cb6e SE |
483 | static int batadv_hard_if_event(struct notifier_block *this, |
484 | unsigned long event, void *ptr) | |
c6c8fea2 | 485 | { |
5f718c20 | 486 | struct net_device *net_dev = ptr; |
9563877e | 487 | struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev); |
32ae9b22 | 488 | struct hard_iface *primary_if = NULL; |
c6c8fea2 SE |
489 | struct bat_priv *bat_priv; |
490 | ||
e6c10f43 | 491 | if (!hard_iface && event == NETDEV_REGISTER) |
18a1cb6e | 492 | hard_iface = batadv_hardif_add_interface(net_dev); |
c6c8fea2 | 493 | |
e6c10f43 | 494 | if (!hard_iface) |
c6c8fea2 SE |
495 | goto out; |
496 | ||
497 | switch (event) { | |
498 | case NETDEV_UP: | |
18a1cb6e | 499 | batadv_hardif_activate_interface(hard_iface); |
c6c8fea2 SE |
500 | break; |
501 | case NETDEV_GOING_DOWN: | |
502 | case NETDEV_DOWN: | |
18a1cb6e | 503 | batadv_hardif_deactivate_interface(hard_iface); |
c6c8fea2 SE |
504 | break; |
505 | case NETDEV_UNREGISTER: | |
e6c10f43 | 506 | list_del_rcu(&hard_iface->list); |
c6c8fea2 | 507 | |
18a1cb6e | 508 | batadv_hardif_remove_interface(hard_iface); |
c6c8fea2 SE |
509 | break; |
510 | case NETDEV_CHANGEMTU: | |
e6c10f43 | 511 | if (hard_iface->soft_iface) |
9563877e | 512 | batadv_update_min_mtu(hard_iface->soft_iface); |
c6c8fea2 SE |
513 | break; |
514 | case NETDEV_CHANGEADDR: | |
e6c10f43 | 515 | if (hard_iface->if_status == IF_NOT_IN_USE) |
c6c8fea2 SE |
516 | goto hardif_put; |
517 | ||
18a1cb6e | 518 | batadv_check_known_mac_addr(hard_iface->net_dev); |
c6c8fea2 | 519 | |
e6c10f43 | 520 | bat_priv = netdev_priv(hard_iface->soft_iface); |
c3229398 | 521 | bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface); |
01c4224b | 522 | |
e5d89254 | 523 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 ML |
524 | if (!primary_if) |
525 | goto hardif_put; | |
526 | ||
527 | if (hard_iface == primary_if) | |
18a1cb6e | 528 | batadv_primary_if_update_addr(bat_priv, NULL); |
c6c8fea2 SE |
529 | break; |
530 | default: | |
531 | break; | |
f81c6224 | 532 | } |
c6c8fea2 SE |
533 | |
534 | hardif_put: | |
e5d89254 | 535 | batadv_hardif_free_ref(hard_iface); |
c6c8fea2 | 536 | out: |
32ae9b22 | 537 | if (primary_if) |
e5d89254 | 538 | batadv_hardif_free_ref(primary_if); |
c6c8fea2 SE |
539 | return NOTIFY_DONE; |
540 | } | |
541 | ||
bc279080 | 542 | /* This function returns true if the interface represented by ifindex is a |
9cfc7bd6 SE |
543 | * 802.11 wireless device |
544 | */ | |
9563877e | 545 | bool batadv_is_wifi_iface(int ifindex) |
bc279080 AQ |
546 | { |
547 | struct net_device *net_device = NULL; | |
548 | bool ret = false; | |
549 | ||
550 | if (ifindex == NULL_IFINDEX) | |
551 | goto out; | |
552 | ||
553 | net_device = dev_get_by_index(&init_net, ifindex); | |
554 | if (!net_device) | |
555 | goto out; | |
556 | ||
557 | #ifdef CONFIG_WIRELESS_EXT | |
558 | /* pre-cfg80211 drivers have to implement WEXT, so it is possible to | |
9cfc7bd6 SE |
559 | * check for wireless_handlers != NULL |
560 | */ | |
bc279080 AQ |
561 | if (net_device->wireless_handlers) |
562 | ret = true; | |
563 | else | |
564 | #endif | |
565 | /* cfg80211 drivers have to set ieee80211_ptr */ | |
566 | if (net_device->ieee80211_ptr) | |
567 | ret = true; | |
568 | out: | |
569 | if (net_device) | |
570 | dev_put(net_device); | |
571 | return ret; | |
572 | } | |
573 | ||
9563877e | 574 | struct notifier_block batadv_hard_if_notifier = { |
18a1cb6e | 575 | .notifier_call = batadv_hard_if_event, |
c6c8fea2 | 576 | }; |