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