]>
Commit | Line | Data |
---|---|---|
c6c8fea2 | 1 | /* |
64afe353 | 2 | * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
3 | * |
4 | * Marek Lindner, Simon Wunderlich | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of version 2 of the GNU General Public | |
8 | * License as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
18 | * 02110-1301, USA | |
19 | * | |
20 | */ | |
21 | ||
22 | #include "main.h" | |
23 | #include "hard-interface.h" | |
24 | #include "soft-interface.h" | |
25 | #include "send.h" | |
26 | #include "translation-table.h" | |
27 | #include "routing.h" | |
28 | #include "bat_sysfs.h" | |
29 | #include "originator.h" | |
30 | #include "hash.h" | |
d0b9fd89 | 31 | #include "bat_ogm.h" |
c6c8fea2 SE |
32 | |
33 | #include <linux/if_arp.h> | |
34 | ||
fb86d764 SE |
35 | |
36 | static int batman_skb_recv(struct sk_buff *skb, | |
37 | struct net_device *dev, | |
38 | struct packet_type *ptype, | |
39 | struct net_device *orig_dev); | |
40 | ||
ed75ccbe | 41 | void hardif_free_rcu(struct rcu_head *rcu) |
c6c8fea2 | 42 | { |
e6c10f43 | 43 | struct hard_iface *hard_iface; |
c6c8fea2 | 44 | |
e6c10f43 ML |
45 | hard_iface = container_of(rcu, struct hard_iface, rcu); |
46 | dev_put(hard_iface->net_dev); | |
47 | kfree(hard_iface); | |
c6c8fea2 SE |
48 | } |
49 | ||
747e4221 | 50 | struct hard_iface *hardif_get_by_netdev(const struct net_device *net_dev) |
c6c8fea2 | 51 | { |
e6c10f43 | 52 | struct hard_iface *hard_iface; |
c6c8fea2 SE |
53 | |
54 | rcu_read_lock(); | |
e6c10f43 ML |
55 | list_for_each_entry_rcu(hard_iface, &hardif_list, list) { |
56 | if (hard_iface->net_dev == net_dev && | |
57 | atomic_inc_not_zero(&hard_iface->refcount)) | |
c6c8fea2 SE |
58 | goto out; |
59 | } | |
60 | ||
e6c10f43 | 61 | hard_iface = NULL; |
c6c8fea2 SE |
62 | |
63 | out: | |
c6c8fea2 | 64 | rcu_read_unlock(); |
e6c10f43 | 65 | return hard_iface; |
c6c8fea2 SE |
66 | } |
67 | ||
747e4221 | 68 | static int is_valid_iface(const struct net_device *net_dev) |
c6c8fea2 SE |
69 | { |
70 | if (net_dev->flags & IFF_LOOPBACK) | |
71 | return 0; | |
72 | ||
73 | if (net_dev->type != ARPHRD_ETHER) | |
74 | return 0; | |
75 | ||
76 | if (net_dev->addr_len != ETH_ALEN) | |
77 | return 0; | |
78 | ||
79 | /* no batman over batman */ | |
e44d8fe2 | 80 | if (softif_is_valid(net_dev)) |
c6c8fea2 | 81 | return 0; |
c6c8fea2 SE |
82 | |
83 | /* Device is being bridged */ | |
84 | /* if (net_dev->priv_flags & IFF_BRIDGE_PORT) | |
85 | return 0; */ | |
86 | ||
87 | return 1; | |
88 | } | |
89 | ||
747e4221 | 90 | static struct hard_iface *hardif_get_active(const struct net_device *soft_iface) |
c6c8fea2 | 91 | { |
e6c10f43 | 92 | struct hard_iface *hard_iface; |
c6c8fea2 SE |
93 | |
94 | rcu_read_lock(); | |
e6c10f43 ML |
95 | list_for_each_entry_rcu(hard_iface, &hardif_list, list) { |
96 | if (hard_iface->soft_iface != soft_iface) | |
c6c8fea2 SE |
97 | continue; |
98 | ||
e6c10f43 ML |
99 | if (hard_iface->if_status == IF_ACTIVE && |
100 | atomic_inc_not_zero(&hard_iface->refcount)) | |
c6c8fea2 SE |
101 | goto out; |
102 | } | |
103 | ||
e6c10f43 | 104 | hard_iface = NULL; |
c6c8fea2 SE |
105 | |
106 | out: | |
c6c8fea2 | 107 | rcu_read_unlock(); |
e6c10f43 | 108 | return hard_iface; |
c6c8fea2 SE |
109 | } |
110 | ||
32ae9b22 | 111 | static void primary_if_update_addr(struct bat_priv *bat_priv) |
c6c8fea2 SE |
112 | { |
113 | struct vis_packet *vis_packet; | |
32ae9b22 ML |
114 | struct hard_iface *primary_if; |
115 | ||
116 | primary_if = primary_if_get_selected(bat_priv); | |
117 | if (!primary_if) | |
118 | goto out; | |
c6c8fea2 SE |
119 | |
120 | vis_packet = (struct vis_packet *) | |
121 | bat_priv->my_vis_info->skb_packet->data; | |
32ae9b22 | 122 | memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN); |
c6c8fea2 | 123 | memcpy(vis_packet->sender_orig, |
32ae9b22 ML |
124 | primary_if->net_dev->dev_addr, ETH_ALEN); |
125 | ||
126 | out: | |
127 | if (primary_if) | |
128 | hardif_free_ref(primary_if); | |
c6c8fea2 SE |
129 | } |
130 | ||
32ae9b22 ML |
131 | static void primary_if_select(struct bat_priv *bat_priv, |
132 | struct hard_iface *new_hard_iface) | |
c6c8fea2 | 133 | { |
32ae9b22 | 134 | struct hard_iface *curr_hard_iface; |
c6c8fea2 | 135 | |
c3caf519 | 136 | ASSERT_RTNL(); |
c6c8fea2 | 137 | |
32ae9b22 ML |
138 | if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount)) |
139 | new_hard_iface = NULL; | |
c6c8fea2 | 140 | |
728cbc6a | 141 | curr_hard_iface = rcu_dereference_protected(bat_priv->primary_if, 1); |
32ae9b22 | 142 | rcu_assign_pointer(bat_priv->primary_if, new_hard_iface); |
c6c8fea2 | 143 | |
32ae9b22 ML |
144 | if (curr_hard_iface) |
145 | hardif_free_ref(curr_hard_iface); | |
c6c8fea2 | 146 | |
32ae9b22 | 147 | if (!new_hard_iface) |
c3caf519 | 148 | return; |
32ae9b22 | 149 | |
d0b9fd89 | 150 | bat_ogm_init_primary(new_hard_iface); |
32ae9b22 | 151 | primary_if_update_addr(bat_priv); |
c6c8fea2 SE |
152 | } |
153 | ||
747e4221 | 154 | static bool hardif_is_iface_up(const struct hard_iface *hard_iface) |
c6c8fea2 | 155 | { |
e6c10f43 | 156 | if (hard_iface->net_dev->flags & IFF_UP) |
c6c8fea2 SE |
157 | return true; |
158 | ||
159 | return false; | |
160 | } | |
161 | ||
747e4221 | 162 | static void check_known_mac_addr(const struct net_device *net_dev) |
c6c8fea2 | 163 | { |
747e4221 | 164 | const struct hard_iface *hard_iface; |
c6c8fea2 SE |
165 | |
166 | rcu_read_lock(); | |
e6c10f43 ML |
167 | list_for_each_entry_rcu(hard_iface, &hardif_list, list) { |
168 | if ((hard_iface->if_status != IF_ACTIVE) && | |
169 | (hard_iface->if_status != IF_TO_BE_ACTIVATED)) | |
c6c8fea2 SE |
170 | continue; |
171 | ||
e6c10f43 | 172 | if (hard_iface->net_dev == net_dev) |
c6c8fea2 SE |
173 | continue; |
174 | ||
e6c10f43 ML |
175 | if (!compare_eth(hard_iface->net_dev->dev_addr, |
176 | net_dev->dev_addr)) | |
c6c8fea2 SE |
177 | continue; |
178 | ||
179 | pr_warning("The newly added mac address (%pM) already exists " | |
180 | "on: %s\n", net_dev->dev_addr, | |
e6c10f43 | 181 | hard_iface->net_dev->name); |
c6c8fea2 SE |
182 | pr_warning("It is strongly recommended to keep mac addresses " |
183 | "unique to avoid problems!\n"); | |
184 | } | |
185 | rcu_read_unlock(); | |
186 | } | |
187 | ||
188 | int hardif_min_mtu(struct net_device *soft_iface) | |
189 | { | |
747e4221 SE |
190 | const struct bat_priv *bat_priv = netdev_priv(soft_iface); |
191 | const struct hard_iface *hard_iface; | |
c6c8fea2 SE |
192 | /* allow big frames if all devices are capable to do so |
193 | * (have MTU > 1500 + BAT_HEADER_LEN) */ | |
194 | int min_mtu = ETH_DATA_LEN; | |
195 | ||
196 | if (atomic_read(&bat_priv->fragmentation)) | |
197 | goto out; | |
198 | ||
199 | rcu_read_lock(); | |
e6c10f43 ML |
200 | list_for_each_entry_rcu(hard_iface, &hardif_list, list) { |
201 | if ((hard_iface->if_status != IF_ACTIVE) && | |
202 | (hard_iface->if_status != IF_TO_BE_ACTIVATED)) | |
c6c8fea2 SE |
203 | continue; |
204 | ||
e6c10f43 | 205 | if (hard_iface->soft_iface != soft_iface) |
c6c8fea2 SE |
206 | continue; |
207 | ||
e6c10f43 | 208 | min_mtu = min_t(int, hard_iface->net_dev->mtu - BAT_HEADER_LEN, |
c6c8fea2 SE |
209 | min_mtu); |
210 | } | |
211 | rcu_read_unlock(); | |
212 | out: | |
213 | return min_mtu; | |
214 | } | |
215 | ||
216 | /* adjusts the MTU if a new interface with a smaller MTU appeared. */ | |
217 | void update_min_mtu(struct net_device *soft_iface) | |
218 | { | |
219 | int min_mtu; | |
220 | ||
221 | min_mtu = hardif_min_mtu(soft_iface); | |
222 | if (soft_iface->mtu != min_mtu) | |
223 | soft_iface->mtu = min_mtu; | |
224 | } | |
225 | ||
e6c10f43 | 226 | static void hardif_activate_interface(struct hard_iface *hard_iface) |
c6c8fea2 SE |
227 | { |
228 | struct bat_priv *bat_priv; | |
32ae9b22 | 229 | struct hard_iface *primary_if = NULL; |
c6c8fea2 | 230 | |
e6c10f43 | 231 | if (hard_iface->if_status != IF_INACTIVE) |
32ae9b22 | 232 | goto out; |
c6c8fea2 | 233 | |
e6c10f43 | 234 | bat_priv = netdev_priv(hard_iface->soft_iface); |
c6c8fea2 | 235 | |
d0b9fd89 | 236 | bat_ogm_update_mac(hard_iface); |
e6c10f43 | 237 | hard_iface->if_status = IF_TO_BE_ACTIVATED; |
c6c8fea2 SE |
238 | |
239 | /** | |
240 | * the first active interface becomes our primary interface or | |
015758d0 | 241 | * the next active interface after the old primary interface was removed |
c6c8fea2 | 242 | */ |
32ae9b22 ML |
243 | primary_if = primary_if_get_selected(bat_priv); |
244 | if (!primary_if) | |
245 | primary_if_select(bat_priv, hard_iface); | |
c6c8fea2 | 246 | |
e6c10f43 ML |
247 | bat_info(hard_iface->soft_iface, "Interface activated: %s\n", |
248 | hard_iface->net_dev->name); | |
c6c8fea2 | 249 | |
e6c10f43 | 250 | update_min_mtu(hard_iface->soft_iface); |
32ae9b22 ML |
251 | |
252 | out: | |
253 | if (primary_if) | |
254 | hardif_free_ref(primary_if); | |
c6c8fea2 SE |
255 | } |
256 | ||
e6c10f43 | 257 | static void hardif_deactivate_interface(struct hard_iface *hard_iface) |
c6c8fea2 | 258 | { |
e6c10f43 ML |
259 | if ((hard_iface->if_status != IF_ACTIVE) && |
260 | (hard_iface->if_status != IF_TO_BE_ACTIVATED)) | |
c6c8fea2 SE |
261 | return; |
262 | ||
e6c10f43 | 263 | hard_iface->if_status = IF_INACTIVE; |
c6c8fea2 | 264 | |
e6c10f43 ML |
265 | bat_info(hard_iface->soft_iface, "Interface deactivated: %s\n", |
266 | hard_iface->net_dev->name); | |
c6c8fea2 | 267 | |
e6c10f43 | 268 | update_min_mtu(hard_iface->soft_iface); |
c6c8fea2 SE |
269 | } |
270 | ||
747e4221 SE |
271 | int hardif_enable_interface(struct hard_iface *hard_iface, |
272 | const char *iface_name) | |
c6c8fea2 SE |
273 | { |
274 | struct bat_priv *bat_priv; | |
e44d8fe2 SE |
275 | struct net_device *soft_iface; |
276 | int ret; | |
c6c8fea2 | 277 | |
e6c10f43 | 278 | if (hard_iface->if_status != 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) | |
286 | pr_err("You are about to enable batman-adv on '%s' which " | |
287 | "already is part of a bridge. Unless you know exactly " | |
288 | "what you are doing this is probably wrong and won't " | |
289 | "work the way you think it would.\n", | |
290 | hard_iface->net_dev->name); | |
291 | ||
e44d8fe2 | 292 | soft_iface = dev_get_by_name(&init_net, iface_name); |
c6c8fea2 | 293 | |
e44d8fe2 SE |
294 | if (!soft_iface) { |
295 | soft_iface = softif_create(iface_name); | |
c6c8fea2 | 296 | |
e44d8fe2 SE |
297 | if (!soft_iface) { |
298 | ret = -ENOMEM; | |
c6c8fea2 | 299 | goto err; |
e44d8fe2 | 300 | } |
c6c8fea2 SE |
301 | |
302 | /* dev_get_by_name() increases the reference counter for us */ | |
e44d8fe2 SE |
303 | dev_hold(soft_iface); |
304 | } | |
305 | ||
306 | if (!softif_is_valid(soft_iface)) { | |
307 | pr_err("Can't create batman mesh interface %s: " | |
308 | "already exists as regular interface\n", | |
309 | soft_iface->name); | |
310 | dev_put(soft_iface); | |
311 | ret = -EINVAL; | |
312 | goto err; | |
c6c8fea2 SE |
313 | } |
314 | ||
e44d8fe2 | 315 | hard_iface->soft_iface = soft_iface; |
e6c10f43 | 316 | bat_priv = netdev_priv(hard_iface->soft_iface); |
d0b9fd89 ML |
317 | |
318 | bat_ogm_init(hard_iface); | |
c6c8fea2 | 319 | |
e6c10f43 ML |
320 | if (!hard_iface->packet_buff) { |
321 | bat_err(hard_iface->soft_iface, "Can't add interface packet " | |
322 | "(%s): out of memory\n", hard_iface->net_dev->name); | |
e44d8fe2 | 323 | ret = -ENOMEM; |
c6c8fea2 SE |
324 | goto err; |
325 | } | |
326 | ||
e6c10f43 | 327 | hard_iface->if_num = bat_priv->num_ifaces; |
c6c8fea2 | 328 | bat_priv->num_ifaces++; |
e6c10f43 ML |
329 | hard_iface->if_status = IF_INACTIVE; |
330 | orig_hash_add_if(hard_iface, bat_priv->num_ifaces); | |
c6c8fea2 | 331 | |
e6c10f43 ML |
332 | hard_iface->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN); |
333 | hard_iface->batman_adv_ptype.func = batman_skb_recv; | |
334 | hard_iface->batman_adv_ptype.dev = hard_iface->net_dev; | |
335 | dev_add_pack(&hard_iface->batman_adv_ptype); | |
c6c8fea2 | 336 | |
e6c10f43 ML |
337 | atomic_set(&hard_iface->seqno, 1); |
338 | atomic_set(&hard_iface->frag_seqno, 1); | |
339 | bat_info(hard_iface->soft_iface, "Adding interface: %s\n", | |
340 | hard_iface->net_dev->name); | |
c6c8fea2 | 341 | |
e6c10f43 | 342 | if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu < |
c6c8fea2 | 343 | ETH_DATA_LEN + BAT_HEADER_LEN) |
e6c10f43 | 344 | bat_info(hard_iface->soft_iface, |
c6c8fea2 SE |
345 | "The MTU of interface %s is too small (%i) to handle " |
346 | "the transport of batman-adv packets. Packets going " | |
347 | "over this interface will be fragmented on layer2 " | |
348 | "which could impact the performance. Setting the MTU " | |
349 | "to %zi would solve the problem.\n", | |
e6c10f43 | 350 | hard_iface->net_dev->name, hard_iface->net_dev->mtu, |
c6c8fea2 SE |
351 | ETH_DATA_LEN + BAT_HEADER_LEN); |
352 | ||
e6c10f43 | 353 | if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu < |
c6c8fea2 | 354 | ETH_DATA_LEN + BAT_HEADER_LEN) |
e6c10f43 | 355 | bat_info(hard_iface->soft_iface, |
c6c8fea2 SE |
356 | "The MTU of interface %s is too small (%i) to handle " |
357 | "the transport of batman-adv packets. If you experience" | |
358 | " problems getting traffic through try increasing the " | |
359 | "MTU to %zi.\n", | |
e6c10f43 | 360 | hard_iface->net_dev->name, hard_iface->net_dev->mtu, |
c6c8fea2 SE |
361 | ETH_DATA_LEN + BAT_HEADER_LEN); |
362 | ||
e6c10f43 ML |
363 | if (hardif_is_iface_up(hard_iface)) |
364 | hardif_activate_interface(hard_iface); | |
c6c8fea2 | 365 | else |
e6c10f43 | 366 | bat_err(hard_iface->soft_iface, "Not using interface %s " |
c6c8fea2 | 367 | "(retrying later): interface not active\n", |
e6c10f43 | 368 | hard_iface->net_dev->name); |
c6c8fea2 SE |
369 | |
370 | /* begin scheduling originator messages on that interface */ | |
b9dacc52 | 371 | schedule_bat_ogm(hard_iface); |
c6c8fea2 SE |
372 | |
373 | out: | |
374 | return 0; | |
375 | ||
376 | err: | |
e6c10f43 | 377 | hardif_free_ref(hard_iface); |
e44d8fe2 | 378 | return ret; |
c6c8fea2 SE |
379 | } |
380 | ||
e6c10f43 | 381 | void hardif_disable_interface(struct hard_iface *hard_iface) |
c6c8fea2 | 382 | { |
e6c10f43 | 383 | struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
32ae9b22 | 384 | struct hard_iface *primary_if = NULL; |
c6c8fea2 | 385 | |
e6c10f43 ML |
386 | if (hard_iface->if_status == IF_ACTIVE) |
387 | hardif_deactivate_interface(hard_iface); | |
c6c8fea2 | 388 | |
e6c10f43 | 389 | if (hard_iface->if_status != IF_INACTIVE) |
32ae9b22 | 390 | goto out; |
c6c8fea2 | 391 | |
e6c10f43 ML |
392 | bat_info(hard_iface->soft_iface, "Removing interface: %s\n", |
393 | hard_iface->net_dev->name); | |
394 | dev_remove_pack(&hard_iface->batman_adv_ptype); | |
c6c8fea2 SE |
395 | |
396 | bat_priv->num_ifaces--; | |
e6c10f43 | 397 | orig_hash_del_if(hard_iface, bat_priv->num_ifaces); |
c6c8fea2 | 398 | |
32ae9b22 ML |
399 | primary_if = primary_if_get_selected(bat_priv); |
400 | if (hard_iface == primary_if) { | |
e6c10f43 | 401 | struct hard_iface *new_if; |
c6c8fea2 | 402 | |
e6c10f43 | 403 | new_if = hardif_get_active(hard_iface->soft_iface); |
32ae9b22 | 404 | primary_if_select(bat_priv, new_if); |
c6c8fea2 SE |
405 | |
406 | if (new_if) | |
ed75ccbe | 407 | hardif_free_ref(new_if); |
c6c8fea2 SE |
408 | } |
409 | ||
e6c10f43 ML |
410 | kfree(hard_iface->packet_buff); |
411 | hard_iface->packet_buff = NULL; | |
412 | hard_iface->if_status = IF_NOT_IN_USE; | |
c6c8fea2 | 413 | |
e6c10f43 | 414 | /* delete all references to this hard_iface */ |
c6c8fea2 | 415 | purge_orig_ref(bat_priv); |
e6c10f43 ML |
416 | purge_outstanding_packets(bat_priv, hard_iface); |
417 | dev_put(hard_iface->soft_iface); | |
c6c8fea2 SE |
418 | |
419 | /* nobody uses this interface anymore */ | |
420 | if (!bat_priv->num_ifaces) | |
e6c10f43 | 421 | softif_destroy(hard_iface->soft_iface); |
c6c8fea2 | 422 | |
e6c10f43 ML |
423 | hard_iface->soft_iface = NULL; |
424 | hardif_free_ref(hard_iface); | |
32ae9b22 ML |
425 | |
426 | out: | |
427 | if (primary_if) | |
428 | hardif_free_ref(primary_if); | |
c6c8fea2 SE |
429 | } |
430 | ||
e6c10f43 | 431 | static struct hard_iface *hardif_add_interface(struct net_device *net_dev) |
c6c8fea2 | 432 | { |
e6c10f43 | 433 | struct hard_iface *hard_iface; |
c6c8fea2 SE |
434 | int ret; |
435 | ||
c3caf519 SE |
436 | ASSERT_RTNL(); |
437 | ||
c6c8fea2 SE |
438 | ret = is_valid_iface(net_dev); |
439 | if (ret != 1) | |
440 | goto out; | |
441 | ||
442 | dev_hold(net_dev); | |
443 | ||
704509b8 | 444 | hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC); |
320f422f | 445 | if (!hard_iface) |
c6c8fea2 | 446 | goto release_dev; |
c6c8fea2 | 447 | |
e6c10f43 | 448 | ret = sysfs_add_hardif(&hard_iface->hardif_obj, net_dev); |
c6c8fea2 SE |
449 | if (ret) |
450 | goto free_if; | |
451 | ||
e6c10f43 ML |
452 | hard_iface->if_num = -1; |
453 | hard_iface->net_dev = net_dev; | |
454 | hard_iface->soft_iface = NULL; | |
455 | hard_iface->if_status = IF_NOT_IN_USE; | |
456 | INIT_LIST_HEAD(&hard_iface->list); | |
ed75ccbe | 457 | /* extra reference for return */ |
e6c10f43 | 458 | atomic_set(&hard_iface->refcount, 2); |
c6c8fea2 | 459 | |
e6c10f43 | 460 | check_known_mac_addr(hard_iface->net_dev); |
e6c10f43 | 461 | list_add_tail_rcu(&hard_iface->list, &hardif_list); |
c6c8fea2 | 462 | |
e6c10f43 | 463 | return hard_iface; |
c6c8fea2 SE |
464 | |
465 | free_if: | |
e6c10f43 | 466 | kfree(hard_iface); |
c6c8fea2 SE |
467 | release_dev: |
468 | dev_put(net_dev); | |
469 | out: | |
470 | return NULL; | |
471 | } | |
472 | ||
e6c10f43 | 473 | static void hardif_remove_interface(struct hard_iface *hard_iface) |
c6c8fea2 | 474 | { |
c3caf519 SE |
475 | ASSERT_RTNL(); |
476 | ||
c6c8fea2 | 477 | /* first deactivate interface */ |
e6c10f43 ML |
478 | if (hard_iface->if_status != IF_NOT_IN_USE) |
479 | hardif_disable_interface(hard_iface); | |
c6c8fea2 | 480 | |
e6c10f43 | 481 | if (hard_iface->if_status != IF_NOT_IN_USE) |
c6c8fea2 SE |
482 | return; |
483 | ||
e6c10f43 ML |
484 | hard_iface->if_status = IF_TO_BE_REMOVED; |
485 | sysfs_del_hardif(&hard_iface->hardif_obj); | |
486 | hardif_free_ref(hard_iface); | |
c6c8fea2 SE |
487 | } |
488 | ||
489 | void hardif_remove_interfaces(void) | |
490 | { | |
e6c10f43 | 491 | struct hard_iface *hard_iface, *hard_iface_tmp; |
c6c8fea2 | 492 | |
c3caf519 | 493 | rtnl_lock(); |
e6c10f43 ML |
494 | list_for_each_entry_safe(hard_iface, hard_iface_tmp, |
495 | &hardif_list, list) { | |
496 | list_del_rcu(&hard_iface->list); | |
e6c10f43 | 497 | hardif_remove_interface(hard_iface); |
c6c8fea2 SE |
498 | } |
499 | rtnl_unlock(); | |
500 | } | |
501 | ||
502 | static int hard_if_event(struct notifier_block *this, | |
503 | unsigned long event, void *ptr) | |
504 | { | |
5f718c20 | 505 | struct net_device *net_dev = ptr; |
e6c10f43 | 506 | struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev); |
32ae9b22 | 507 | struct hard_iface *primary_if = NULL; |
c6c8fea2 SE |
508 | struct bat_priv *bat_priv; |
509 | ||
e6c10f43 ML |
510 | if (!hard_iface && event == NETDEV_REGISTER) |
511 | hard_iface = hardif_add_interface(net_dev); | |
c6c8fea2 | 512 | |
e6c10f43 | 513 | if (!hard_iface) |
c6c8fea2 SE |
514 | goto out; |
515 | ||
516 | switch (event) { | |
517 | case NETDEV_UP: | |
e6c10f43 | 518 | hardif_activate_interface(hard_iface); |
c6c8fea2 SE |
519 | break; |
520 | case NETDEV_GOING_DOWN: | |
521 | case NETDEV_DOWN: | |
e6c10f43 | 522 | hardif_deactivate_interface(hard_iface); |
c6c8fea2 SE |
523 | break; |
524 | case NETDEV_UNREGISTER: | |
e6c10f43 | 525 | list_del_rcu(&hard_iface->list); |
c6c8fea2 | 526 | |
e6c10f43 | 527 | hardif_remove_interface(hard_iface); |
c6c8fea2 SE |
528 | break; |
529 | case NETDEV_CHANGEMTU: | |
e6c10f43 ML |
530 | if (hard_iface->soft_iface) |
531 | update_min_mtu(hard_iface->soft_iface); | |
c6c8fea2 SE |
532 | break; |
533 | case NETDEV_CHANGEADDR: | |
e6c10f43 | 534 | if (hard_iface->if_status == IF_NOT_IN_USE) |
c6c8fea2 SE |
535 | goto hardif_put; |
536 | ||
e6c10f43 | 537 | check_known_mac_addr(hard_iface->net_dev); |
d0b9fd89 | 538 | bat_ogm_update_mac(hard_iface); |
c6c8fea2 | 539 | |
e6c10f43 | 540 | bat_priv = netdev_priv(hard_iface->soft_iface); |
32ae9b22 ML |
541 | primary_if = primary_if_get_selected(bat_priv); |
542 | if (!primary_if) | |
543 | goto hardif_put; | |
544 | ||
545 | if (hard_iface == primary_if) | |
546 | primary_if_update_addr(bat_priv); | |
c6c8fea2 SE |
547 | break; |
548 | default: | |
549 | break; | |
f81c6224 | 550 | } |
c6c8fea2 SE |
551 | |
552 | hardif_put: | |
e6c10f43 | 553 | hardif_free_ref(hard_iface); |
c6c8fea2 | 554 | out: |
32ae9b22 ML |
555 | if (primary_if) |
556 | hardif_free_ref(primary_if); | |
c6c8fea2 SE |
557 | return NOTIFY_DONE; |
558 | } | |
559 | ||
015758d0 | 560 | /* incoming packets with the batman ethertype received on any active hard |
c6c8fea2 | 561 | * interface */ |
fb86d764 SE |
562 | static int batman_skb_recv(struct sk_buff *skb, struct net_device *dev, |
563 | struct packet_type *ptype, | |
564 | struct net_device *orig_dev) | |
c6c8fea2 SE |
565 | { |
566 | struct bat_priv *bat_priv; | |
b6da4bf5 | 567 | struct batman_ogm_packet *batman_ogm_packet; |
e6c10f43 | 568 | struct hard_iface *hard_iface; |
c6c8fea2 SE |
569 | int ret; |
570 | ||
e6c10f43 | 571 | hard_iface = container_of(ptype, struct hard_iface, batman_adv_ptype); |
c6c8fea2 SE |
572 | skb = skb_share_check(skb, GFP_ATOMIC); |
573 | ||
574 | /* skb was released by skb_share_check() */ | |
575 | if (!skb) | |
576 | goto err_out; | |
577 | ||
578 | /* packet should hold at least type and version */ | |
579 | if (unlikely(!pskb_may_pull(skb, 2))) | |
580 | goto err_free; | |
581 | ||
582 | /* expect a valid ethernet header here. */ | |
583 | if (unlikely(skb->mac_len != sizeof(struct ethhdr) | |
584 | || !skb_mac_header(skb))) | |
585 | goto err_free; | |
586 | ||
e6c10f43 | 587 | if (!hard_iface->soft_iface) |
c6c8fea2 SE |
588 | goto err_free; |
589 | ||
e6c10f43 | 590 | bat_priv = netdev_priv(hard_iface->soft_iface); |
c6c8fea2 SE |
591 | |
592 | if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE) | |
593 | goto err_free; | |
594 | ||
595 | /* discard frames on not active interfaces */ | |
e6c10f43 | 596 | if (hard_iface->if_status != IF_ACTIVE) |
c6c8fea2 SE |
597 | goto err_free; |
598 | ||
b6da4bf5 | 599 | batman_ogm_packet = (struct batman_ogm_packet *)skb->data; |
c6c8fea2 | 600 | |
76543d14 | 601 | if (batman_ogm_packet->header.version != COMPAT_VERSION) { |
c6c8fea2 SE |
602 | bat_dbg(DBG_BATMAN, bat_priv, |
603 | "Drop packet: incompatible batman version (%i)\n", | |
76543d14 | 604 | batman_ogm_packet->header.version); |
c6c8fea2 SE |
605 | goto err_free; |
606 | } | |
607 | ||
608 | /* all receive handlers return whether they received or reused | |
609 | * the supplied skb. if not, we have to free the skb. */ | |
610 | ||
76543d14 | 611 | switch (batman_ogm_packet->header.packet_type) { |
c6c8fea2 | 612 | /* batman originator packet */ |
b6da4bf5 | 613 | case BAT_OGM: |
fc957275 | 614 | ret = recv_bat_ogm_packet(skb, hard_iface); |
c6c8fea2 SE |
615 | break; |
616 | ||
617 | /* batman icmp packet */ | |
618 | case BAT_ICMP: | |
e6c10f43 | 619 | ret = recv_icmp_packet(skb, hard_iface); |
c6c8fea2 SE |
620 | break; |
621 | ||
622 | /* unicast packet */ | |
623 | case BAT_UNICAST: | |
e6c10f43 | 624 | ret = recv_unicast_packet(skb, hard_iface); |
c6c8fea2 SE |
625 | break; |
626 | ||
627 | /* fragmented unicast packet */ | |
628 | case BAT_UNICAST_FRAG: | |
e6c10f43 | 629 | ret = recv_ucast_frag_packet(skb, hard_iface); |
c6c8fea2 SE |
630 | break; |
631 | ||
632 | /* broadcast packet */ | |
633 | case BAT_BCAST: | |
e6c10f43 | 634 | ret = recv_bcast_packet(skb, hard_iface); |
c6c8fea2 SE |
635 | break; |
636 | ||
637 | /* vis packet */ | |
638 | case BAT_VIS: | |
e6c10f43 | 639 | ret = recv_vis_packet(skb, hard_iface); |
c6c8fea2 | 640 | break; |
a73105b8 AQ |
641 | /* Translation table query (request or response) */ |
642 | case BAT_TT_QUERY: | |
643 | ret = recv_tt_query(skb, hard_iface); | |
644 | break; | |
cc47f66e AQ |
645 | /* Roaming advertisement */ |
646 | case BAT_ROAM_ADV: | |
647 | ret = recv_roam_adv(skb, hard_iface); | |
648 | break; | |
c6c8fea2 SE |
649 | default: |
650 | ret = NET_RX_DROP; | |
651 | } | |
652 | ||
653 | if (ret == NET_RX_DROP) | |
654 | kfree_skb(skb); | |
655 | ||
656 | /* return NET_RX_SUCCESS in any case as we | |
657 | * most probably dropped the packet for | |
658 | * routing-logical reasons. */ | |
659 | ||
660 | return NET_RX_SUCCESS; | |
661 | ||
662 | err_free: | |
663 | kfree_skb(skb); | |
664 | err_out: | |
665 | return NET_RX_DROP; | |
666 | } | |
667 | ||
bc279080 AQ |
668 | /* This function returns true if the interface represented by ifindex is a |
669 | * 802.11 wireless device */ | |
670 | bool is_wifi_iface(int ifindex) | |
671 | { | |
672 | struct net_device *net_device = NULL; | |
673 | bool ret = false; | |
674 | ||
675 | if (ifindex == NULL_IFINDEX) | |
676 | goto out; | |
677 | ||
678 | net_device = dev_get_by_index(&init_net, ifindex); | |
679 | if (!net_device) | |
680 | goto out; | |
681 | ||
682 | #ifdef CONFIG_WIRELESS_EXT | |
683 | /* pre-cfg80211 drivers have to implement WEXT, so it is possible to | |
684 | * check for wireless_handlers != NULL */ | |
685 | if (net_device->wireless_handlers) | |
686 | ret = true; | |
687 | else | |
688 | #endif | |
689 | /* cfg80211 drivers have to set ieee80211_ptr */ | |
690 | if (net_device->ieee80211_ptr) | |
691 | ret = true; | |
692 | out: | |
693 | if (net_device) | |
694 | dev_put(net_device); | |
695 | return ret; | |
696 | } | |
697 | ||
c6c8fea2 SE |
698 | struct notifier_block hard_if_notifier = { |
699 | .notifier_call = hard_if_event, | |
700 | }; |