]>
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 | ||
e9a4f295 | 88 | if (hard_iface->if_status == BATADV_IF_ACTIVE && |
e6c10f43 | 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) { |
e9a4f295 SE |
160 | if ((hard_iface->if_status != BATADV_IF_ACTIVE) && |
161 | (hard_iface->if_status != BATADV_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) { |
e9a4f295 SE |
192 | if ((hard_iface->if_status != BATADV_IF_ACTIVE) && |
193 | (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) | |
c6c8fea2 SE |
194 | continue; |
195 | ||
e6c10f43 | 196 | if (hard_iface->soft_iface != soft_iface) |
c6c8fea2 SE |
197 | continue; |
198 | ||
c11fdfae SE |
199 | min_mtu = min_t(int, |
200 | hard_iface->net_dev->mtu - BATADV_HEADER_LEN, | |
c6c8fea2 SE |
201 | min_mtu); |
202 | } | |
203 | rcu_read_unlock(); | |
204 | out: | |
205 | return min_mtu; | |
206 | } | |
207 | ||
208 | /* adjusts the MTU if a new interface with a smaller MTU appeared. */ | |
9563877e | 209 | void batadv_update_min_mtu(struct net_device *soft_iface) |
c6c8fea2 SE |
210 | { |
211 | int min_mtu; | |
212 | ||
9563877e | 213 | min_mtu = batadv_hardif_min_mtu(soft_iface); |
c6c8fea2 SE |
214 | if (soft_iface->mtu != min_mtu) |
215 | soft_iface->mtu = min_mtu; | |
216 | } | |
217 | ||
18a1cb6e | 218 | static void batadv_hardif_activate_interface(struct hard_iface *hard_iface) |
c6c8fea2 SE |
219 | { |
220 | struct bat_priv *bat_priv; | |
32ae9b22 | 221 | struct hard_iface *primary_if = NULL; |
c6c8fea2 | 222 | |
e9a4f295 | 223 | if (hard_iface->if_status != BATADV_IF_INACTIVE) |
32ae9b22 | 224 | goto out; |
c6c8fea2 | 225 | |
e6c10f43 | 226 | bat_priv = netdev_priv(hard_iface->soft_iface); |
c6c8fea2 | 227 | |
c3229398 | 228 | bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface); |
e9a4f295 | 229 | hard_iface->if_status = BATADV_IF_TO_BE_ACTIVATED; |
c6c8fea2 | 230 | |
9cfc7bd6 | 231 | /* the first active interface becomes our primary interface or |
015758d0 | 232 | * the next active interface after the old primary interface was removed |
c6c8fea2 | 233 | */ |
e5d89254 | 234 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 | 235 | if (!primary_if) |
18a1cb6e | 236 | batadv_primary_if_select(bat_priv, hard_iface); |
c6c8fea2 | 237 | |
3e34819e SE |
238 | batadv_info(hard_iface->soft_iface, "Interface activated: %s\n", |
239 | hard_iface->net_dev->name); | |
c6c8fea2 | 240 | |
9563877e | 241 | batadv_update_min_mtu(hard_iface->soft_iface); |
32ae9b22 ML |
242 | |
243 | out: | |
244 | if (primary_if) | |
e5d89254 | 245 | batadv_hardif_free_ref(primary_if); |
c6c8fea2 SE |
246 | } |
247 | ||
18a1cb6e | 248 | static void batadv_hardif_deactivate_interface(struct hard_iface *hard_iface) |
c6c8fea2 | 249 | { |
e9a4f295 SE |
250 | if ((hard_iface->if_status != BATADV_IF_ACTIVE) && |
251 | (hard_iface->if_status != BATADV_IF_TO_BE_ACTIVATED)) | |
c6c8fea2 SE |
252 | return; |
253 | ||
e9a4f295 | 254 | hard_iface->if_status = BATADV_IF_INACTIVE; |
c6c8fea2 | 255 | |
3e34819e SE |
256 | batadv_info(hard_iface->soft_iface, "Interface deactivated: %s\n", |
257 | hard_iface->net_dev->name); | |
c6c8fea2 | 258 | |
9563877e | 259 | batadv_update_min_mtu(hard_iface->soft_iface); |
c6c8fea2 SE |
260 | } |
261 | ||
9563877e SE |
262 | int batadv_hardif_enable_interface(struct hard_iface *hard_iface, |
263 | const char *iface_name) | |
c6c8fea2 SE |
264 | { |
265 | struct bat_priv *bat_priv; | |
e44d8fe2 | 266 | struct net_device *soft_iface; |
7e071c79 | 267 | __be16 ethertype = __constant_htons(BATADV_ETH_P_BATMAN); |
e44d8fe2 | 268 | int ret; |
c6c8fea2 | 269 | |
e9a4f295 | 270 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
c6c8fea2 SE |
271 | goto out; |
272 | ||
e6c10f43 | 273 | if (!atomic_inc_not_zero(&hard_iface->refcount)) |
ed75ccbe ML |
274 | goto out; |
275 | ||
6e242f90 ML |
276 | /* hard-interface is part of a bridge */ |
277 | if (hard_iface->net_dev->priv_flags & IFF_BRIDGE_PORT) | |
86ceb360 | 278 | 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 |
279 | hard_iface->net_dev->name); |
280 | ||
e44d8fe2 | 281 | soft_iface = dev_get_by_name(&init_net, iface_name); |
c6c8fea2 | 282 | |
e44d8fe2 | 283 | if (!soft_iface) { |
04b482a2 | 284 | soft_iface = batadv_softif_create(iface_name); |
c6c8fea2 | 285 | |
e44d8fe2 SE |
286 | if (!soft_iface) { |
287 | ret = -ENOMEM; | |
c6c8fea2 | 288 | goto err; |
e44d8fe2 | 289 | } |
c6c8fea2 SE |
290 | |
291 | /* dev_get_by_name() increases the reference counter for us */ | |
e44d8fe2 SE |
292 | dev_hold(soft_iface); |
293 | } | |
294 | ||
04b482a2 | 295 | if (!batadv_softif_is_valid(soft_iface)) { |
86ceb360 | 296 | pr_err("Can't create batman mesh interface %s: already exists as regular interface\n", |
e44d8fe2 | 297 | soft_iface->name); |
e44d8fe2 | 298 | ret = -EINVAL; |
77af7575 | 299 | goto err_dev; |
c6c8fea2 SE |
300 | } |
301 | ||
e44d8fe2 | 302 | hard_iface->soft_iface = soft_iface; |
e6c10f43 | 303 | bat_priv = netdev_priv(hard_iface->soft_iface); |
d0b9fd89 | 304 | |
77af7575 | 305 | ret = bat_priv->bat_algo_ops->bat_iface_enable(hard_iface); |
5346c35e | 306 | if (ret < 0) |
77af7575 | 307 | goto err_dev; |
c6c8fea2 | 308 | |
e6c10f43 | 309 | hard_iface->if_num = bat_priv->num_ifaces; |
c6c8fea2 | 310 | bat_priv->num_ifaces++; |
e9a4f295 | 311 | hard_iface->if_status = BATADV_IF_INACTIVE; |
7d211efc | 312 | batadv_orig_hash_add_if(hard_iface, bat_priv->num_ifaces); |
c6c8fea2 | 313 | |
7e071c79 | 314 | hard_iface->batman_adv_ptype.type = ethertype; |
3193e8fd | 315 | hard_iface->batman_adv_ptype.func = batadv_batman_skb_recv; |
e6c10f43 ML |
316 | hard_iface->batman_adv_ptype.dev = hard_iface->net_dev; |
317 | dev_add_pack(&hard_iface->batman_adv_ptype); | |
c6c8fea2 | 318 | |
e6c10f43 | 319 | atomic_set(&hard_iface->frag_seqno, 1); |
3e34819e SE |
320 | batadv_info(hard_iface->soft_iface, "Adding interface: %s\n", |
321 | hard_iface->net_dev->name); | |
c6c8fea2 | 322 | |
e6c10f43 | 323 | if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu < |
c11fdfae | 324 | ETH_DATA_LEN + BATADV_HEADER_LEN) |
3e34819e SE |
325 | batadv_info(hard_iface->soft_iface, |
326 | "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", | |
327 | hard_iface->net_dev->name, hard_iface->net_dev->mtu, | |
c11fdfae | 328 | ETH_DATA_LEN + BATADV_HEADER_LEN); |
c6c8fea2 | 329 | |
e6c10f43 | 330 | if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu < |
c11fdfae | 331 | ETH_DATA_LEN + BATADV_HEADER_LEN) |
3e34819e SE |
332 | batadv_info(hard_iface->soft_iface, |
333 | "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", | |
334 | hard_iface->net_dev->name, hard_iface->net_dev->mtu, | |
c11fdfae | 335 | ETH_DATA_LEN + BATADV_HEADER_LEN); |
c6c8fea2 | 336 | |
18a1cb6e SE |
337 | if (batadv_hardif_is_iface_up(hard_iface)) |
338 | batadv_hardif_activate_interface(hard_iface); | |
c6c8fea2 | 339 | else |
3e34819e SE |
340 | batadv_err(hard_iface->soft_iface, |
341 | "Not using interface %s (retrying later): interface not active\n", | |
342 | hard_iface->net_dev->name); | |
c6c8fea2 SE |
343 | |
344 | /* begin scheduling originator messages on that interface */ | |
9455e34c | 345 | batadv_schedule_bat_ogm(hard_iface); |
c6c8fea2 SE |
346 | |
347 | out: | |
348 | return 0; | |
349 | ||
77af7575 ML |
350 | err_dev: |
351 | dev_put(soft_iface); | |
c6c8fea2 | 352 | err: |
e5d89254 | 353 | batadv_hardif_free_ref(hard_iface); |
e44d8fe2 | 354 | return ret; |
c6c8fea2 SE |
355 | } |
356 | ||
9563877e | 357 | void batadv_hardif_disable_interface(struct hard_iface *hard_iface) |
c6c8fea2 | 358 | { |
e6c10f43 | 359 | struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
32ae9b22 | 360 | struct hard_iface *primary_if = NULL; |
c6c8fea2 | 361 | |
e9a4f295 | 362 | if (hard_iface->if_status == BATADV_IF_ACTIVE) |
18a1cb6e | 363 | batadv_hardif_deactivate_interface(hard_iface); |
c6c8fea2 | 364 | |
e9a4f295 | 365 | if (hard_iface->if_status != BATADV_IF_INACTIVE) |
32ae9b22 | 366 | goto out; |
c6c8fea2 | 367 | |
3e34819e SE |
368 | batadv_info(hard_iface->soft_iface, "Removing interface: %s\n", |
369 | hard_iface->net_dev->name); | |
e6c10f43 | 370 | dev_remove_pack(&hard_iface->batman_adv_ptype); |
c6c8fea2 SE |
371 | |
372 | bat_priv->num_ifaces--; | |
7d211efc | 373 | batadv_orig_hash_del_if(hard_iface, bat_priv->num_ifaces); |
c6c8fea2 | 374 | |
e5d89254 | 375 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 | 376 | if (hard_iface == primary_if) { |
e6c10f43 | 377 | struct hard_iface *new_if; |
c6c8fea2 | 378 | |
18a1cb6e SE |
379 | new_if = batadv_hardif_get_active(hard_iface->soft_iface); |
380 | batadv_primary_if_select(bat_priv, new_if); | |
c6c8fea2 SE |
381 | |
382 | if (new_if) | |
e5d89254 | 383 | batadv_hardif_free_ref(new_if); |
c6c8fea2 SE |
384 | } |
385 | ||
00a50076 | 386 | bat_priv->bat_algo_ops->bat_iface_disable(hard_iface); |
e9a4f295 | 387 | hard_iface->if_status = BATADV_IF_NOT_IN_USE; |
c6c8fea2 | 388 | |
e6c10f43 | 389 | /* delete all references to this hard_iface */ |
7d211efc | 390 | batadv_purge_orig_ref(bat_priv); |
9455e34c | 391 | batadv_purge_outstanding_packets(bat_priv, hard_iface); |
e6c10f43 | 392 | dev_put(hard_iface->soft_iface); |
c6c8fea2 SE |
393 | |
394 | /* nobody uses this interface anymore */ | |
395 | if (!bat_priv->num_ifaces) | |
04b482a2 | 396 | batadv_softif_destroy(hard_iface->soft_iface); |
c6c8fea2 | 397 | |
e6c10f43 | 398 | hard_iface->soft_iface = NULL; |
e5d89254 | 399 | batadv_hardif_free_ref(hard_iface); |
32ae9b22 ML |
400 | |
401 | out: | |
402 | if (primary_if) | |
e5d89254 | 403 | batadv_hardif_free_ref(primary_if); |
c6c8fea2 SE |
404 | } |
405 | ||
18a1cb6e SE |
406 | static struct hard_iface * |
407 | batadv_hardif_add_interface(struct net_device *net_dev) | |
c6c8fea2 | 408 | { |
e6c10f43 | 409 | struct hard_iface *hard_iface; |
c6c8fea2 SE |
410 | int ret; |
411 | ||
c3caf519 SE |
412 | ASSERT_RTNL(); |
413 | ||
18a1cb6e | 414 | ret = batadv_is_valid_iface(net_dev); |
c6c8fea2 SE |
415 | if (ret != 1) |
416 | goto out; | |
417 | ||
418 | dev_hold(net_dev); | |
419 | ||
704509b8 | 420 | hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC); |
320f422f | 421 | if (!hard_iface) |
c6c8fea2 | 422 | goto release_dev; |
c6c8fea2 | 423 | |
5853e22c | 424 | ret = batadv_sysfs_add_hardif(&hard_iface->hardif_obj, net_dev); |
c6c8fea2 SE |
425 | if (ret) |
426 | goto free_if; | |
427 | ||
e6c10f43 ML |
428 | hard_iface->if_num = -1; |
429 | hard_iface->net_dev = net_dev; | |
430 | hard_iface->soft_iface = NULL; | |
e9a4f295 | 431 | hard_iface->if_status = BATADV_IF_NOT_IN_USE; |
e6c10f43 | 432 | INIT_LIST_HEAD(&hard_iface->list); |
ed75ccbe | 433 | /* extra reference for return */ |
e6c10f43 | 434 | atomic_set(&hard_iface->refcount, 2); |
c6c8fea2 | 435 | |
18a1cb6e | 436 | batadv_check_known_mac_addr(hard_iface->net_dev); |
3193e8fd | 437 | list_add_tail_rcu(&hard_iface->list, &batadv_hardif_list); |
c6c8fea2 | 438 | |
9cfc7bd6 | 439 | /* This can't be called via a bat_priv callback because |
8140625e ML |
440 | * we have no bat_priv yet. |
441 | */ | |
442 | atomic_set(&hard_iface->seqno, 1); | |
443 | hard_iface->packet_buff = NULL; | |
444 | ||
e6c10f43 | 445 | return hard_iface; |
c6c8fea2 SE |
446 | |
447 | free_if: | |
e6c10f43 | 448 | kfree(hard_iface); |
c6c8fea2 SE |
449 | release_dev: |
450 | dev_put(net_dev); | |
451 | out: | |
452 | return NULL; | |
453 | } | |
454 | ||
18a1cb6e | 455 | static void batadv_hardif_remove_interface(struct hard_iface *hard_iface) |
c6c8fea2 | 456 | { |
c3caf519 SE |
457 | ASSERT_RTNL(); |
458 | ||
c6c8fea2 | 459 | /* first deactivate interface */ |
e9a4f295 | 460 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
9563877e | 461 | batadv_hardif_disable_interface(hard_iface); |
c6c8fea2 | 462 | |
e9a4f295 | 463 | if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) |
c6c8fea2 SE |
464 | return; |
465 | ||
e9a4f295 | 466 | hard_iface->if_status = BATADV_IF_TO_BE_REMOVED; |
5853e22c | 467 | batadv_sysfs_del_hardif(&hard_iface->hardif_obj); |
e5d89254 | 468 | batadv_hardif_free_ref(hard_iface); |
c6c8fea2 SE |
469 | } |
470 | ||
9563877e | 471 | void batadv_hardif_remove_interfaces(void) |
c6c8fea2 | 472 | { |
e6c10f43 | 473 | struct hard_iface *hard_iface, *hard_iface_tmp; |
c6c8fea2 | 474 | |
c3caf519 | 475 | rtnl_lock(); |
e6c10f43 | 476 | list_for_each_entry_safe(hard_iface, hard_iface_tmp, |
3193e8fd | 477 | &batadv_hardif_list, list) { |
e6c10f43 | 478 | list_del_rcu(&hard_iface->list); |
18a1cb6e | 479 | batadv_hardif_remove_interface(hard_iface); |
c6c8fea2 SE |
480 | } |
481 | rtnl_unlock(); | |
482 | } | |
483 | ||
18a1cb6e SE |
484 | static int batadv_hard_if_event(struct notifier_block *this, |
485 | unsigned long event, void *ptr) | |
c6c8fea2 | 486 | { |
5f718c20 | 487 | struct net_device *net_dev = ptr; |
9563877e | 488 | struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev); |
32ae9b22 | 489 | struct hard_iface *primary_if = NULL; |
c6c8fea2 SE |
490 | struct bat_priv *bat_priv; |
491 | ||
e6c10f43 | 492 | if (!hard_iface && event == NETDEV_REGISTER) |
18a1cb6e | 493 | hard_iface = batadv_hardif_add_interface(net_dev); |
c6c8fea2 | 494 | |
e6c10f43 | 495 | if (!hard_iface) |
c6c8fea2 SE |
496 | goto out; |
497 | ||
498 | switch (event) { | |
499 | case NETDEV_UP: | |
18a1cb6e | 500 | batadv_hardif_activate_interface(hard_iface); |
c6c8fea2 SE |
501 | break; |
502 | case NETDEV_GOING_DOWN: | |
503 | case NETDEV_DOWN: | |
18a1cb6e | 504 | batadv_hardif_deactivate_interface(hard_iface); |
c6c8fea2 SE |
505 | break; |
506 | case NETDEV_UNREGISTER: | |
e6c10f43 | 507 | list_del_rcu(&hard_iface->list); |
c6c8fea2 | 508 | |
18a1cb6e | 509 | batadv_hardif_remove_interface(hard_iface); |
c6c8fea2 SE |
510 | break; |
511 | case NETDEV_CHANGEMTU: | |
e6c10f43 | 512 | if (hard_iface->soft_iface) |
9563877e | 513 | batadv_update_min_mtu(hard_iface->soft_iface); |
c6c8fea2 SE |
514 | break; |
515 | case NETDEV_CHANGEADDR: | |
e9a4f295 | 516 | if (hard_iface->if_status == BATADV_IF_NOT_IN_USE) |
c6c8fea2 SE |
517 | goto hardif_put; |
518 | ||
18a1cb6e | 519 | batadv_check_known_mac_addr(hard_iface->net_dev); |
c6c8fea2 | 520 | |
e6c10f43 | 521 | bat_priv = netdev_priv(hard_iface->soft_iface); |
c3229398 | 522 | bat_priv->bat_algo_ops->bat_iface_update_mac(hard_iface); |
01c4224b | 523 | |
e5d89254 | 524 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 ML |
525 | if (!primary_if) |
526 | goto hardif_put; | |
527 | ||
528 | if (hard_iface == primary_if) | |
18a1cb6e | 529 | batadv_primary_if_update_addr(bat_priv, NULL); |
c6c8fea2 SE |
530 | break; |
531 | default: | |
532 | break; | |
f81c6224 | 533 | } |
c6c8fea2 SE |
534 | |
535 | hardif_put: | |
e5d89254 | 536 | batadv_hardif_free_ref(hard_iface); |
c6c8fea2 | 537 | out: |
32ae9b22 | 538 | if (primary_if) |
e5d89254 | 539 | batadv_hardif_free_ref(primary_if); |
c6c8fea2 SE |
540 | return NOTIFY_DONE; |
541 | } | |
542 | ||
bc279080 | 543 | /* This function returns true if the interface represented by ifindex is a |
9cfc7bd6 SE |
544 | * 802.11 wireless device |
545 | */ | |
9563877e | 546 | bool batadv_is_wifi_iface(int ifindex) |
bc279080 AQ |
547 | { |
548 | struct net_device *net_device = NULL; | |
549 | bool ret = false; | |
550 | ||
42d0b044 | 551 | if (ifindex == BATADV_NULL_IFINDEX) |
bc279080 AQ |
552 | goto out; |
553 | ||
554 | net_device = dev_get_by_index(&init_net, ifindex); | |
555 | if (!net_device) | |
556 | goto out; | |
557 | ||
558 | #ifdef CONFIG_WIRELESS_EXT | |
559 | /* pre-cfg80211 drivers have to implement WEXT, so it is possible to | |
9cfc7bd6 SE |
560 | * check for wireless_handlers != NULL |
561 | */ | |
bc279080 AQ |
562 | if (net_device->wireless_handlers) |
563 | ret = true; | |
564 | else | |
565 | #endif | |
566 | /* cfg80211 drivers have to set ieee80211_ptr */ | |
567 | if (net_device->ieee80211_ptr) | |
568 | ret = true; | |
569 | out: | |
570 | if (net_device) | |
571 | dev_put(net_device); | |
572 | return ret; | |
573 | } | |
574 | ||
9563877e | 575 | struct notifier_block batadv_hard_if_notifier = { |
18a1cb6e | 576 | .notifier_call = batadv_hard_if_event, |
c6c8fea2 | 577 | }; |