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