ALSA: usb-audio: Fix an out-of-bound read in create_composite_quirks
[linux/fpc-iii.git] / net / batman-adv / soft-interface.c
blob9f1fe6169befa6eec2796afa11400c8704e782dc
1 /* Copyright (C) 2007-2015 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "soft-interface.h"
19 #include "main.h"
21 #include <linux/atomic.h>
22 #include <linux/byteorder/generic.h>
23 #include <linux/cache.h>
24 #include <linux/compiler.h>
25 #include <linux/errno.h>
26 #include <linux/etherdevice.h>
27 #include <linux/ethtool.h>
28 #include <linux/fs.h>
29 #include <linux/if_ether.h>
30 #include <linux/if_vlan.h>
31 #include <linux/jiffies.h>
32 #include <linux/kernel.h>
33 #include <linux/list.h>
34 #include <linux/lockdep.h>
35 #include <linux/netdevice.h>
36 #include <linux/percpu.h>
37 #include <linux/printk.h>
38 #include <linux/random.h>
39 #include <linux/rculist.h>
40 #include <linux/rcupdate.h>
41 #include <linux/skbuff.h>
42 #include <linux/slab.h>
43 #include <linux/socket.h>
44 #include <linux/spinlock.h>
45 #include <linux/stddef.h>
46 #include <linux/string.h>
47 #include <linux/types.h>
48 #include <linux/workqueue.h>
50 #include "bridge_loop_avoidance.h"
51 #include "debugfs.h"
52 #include "distributed-arp-table.h"
53 #include "gateway_client.h"
54 #include "gateway_common.h"
55 #include "hard-interface.h"
56 #include "multicast.h"
57 #include "network-coding.h"
58 #include "packet.h"
59 #include "send.h"
60 #include "sysfs.h"
61 #include "translation-table.h"
63 static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
64 static void batadv_get_drvinfo(struct net_device *dev,
65 struct ethtool_drvinfo *info);
66 static u32 batadv_get_msglevel(struct net_device *dev);
67 static void batadv_set_msglevel(struct net_device *dev, u32 value);
68 static u32 batadv_get_link(struct net_device *dev);
69 static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data);
70 static void batadv_get_ethtool_stats(struct net_device *dev,
71 struct ethtool_stats *stats, u64 *data);
72 static int batadv_get_sset_count(struct net_device *dev, int stringset);
74 static const struct ethtool_ops batadv_ethtool_ops = {
75 .get_settings = batadv_get_settings,
76 .get_drvinfo = batadv_get_drvinfo,
77 .get_msglevel = batadv_get_msglevel,
78 .set_msglevel = batadv_set_msglevel,
79 .get_link = batadv_get_link,
80 .get_strings = batadv_get_strings,
81 .get_ethtool_stats = batadv_get_ethtool_stats,
82 .get_sset_count = batadv_get_sset_count,
85 int batadv_skb_head_push(struct sk_buff *skb, unsigned int len)
87 int result;
89 /* TODO: We must check if we can release all references to non-payload
90 * data using skb_header_release in our skbs to allow skb_cow_header to
91 * work optimally. This means that those skbs are not allowed to read
92 * or write any data which is before the current position of skb->data
93 * after that call and thus allow other skbs with the same data buffer
94 * to write freely in that area.
96 result = skb_cow_head(skb, len);
97 if (result < 0)
98 return result;
100 skb_push(skb, len);
101 return 0;
104 static int batadv_interface_open(struct net_device *dev)
106 netif_start_queue(dev);
107 return 0;
110 static int batadv_interface_release(struct net_device *dev)
112 netif_stop_queue(dev);
113 return 0;
116 static struct net_device_stats *batadv_interface_stats(struct net_device *dev)
118 struct batadv_priv *bat_priv = netdev_priv(dev);
119 struct net_device_stats *stats = &bat_priv->stats;
121 stats->tx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_TX);
122 stats->tx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_TX_BYTES);
123 stats->tx_dropped = batadv_sum_counter(bat_priv, BATADV_CNT_TX_DROPPED);
124 stats->rx_packets = batadv_sum_counter(bat_priv, BATADV_CNT_RX);
125 stats->rx_bytes = batadv_sum_counter(bat_priv, BATADV_CNT_RX_BYTES);
126 return stats;
129 static int batadv_interface_set_mac_addr(struct net_device *dev, void *p)
131 struct batadv_priv *bat_priv = netdev_priv(dev);
132 struct batadv_softif_vlan *vlan;
133 struct sockaddr *addr = p;
134 u8 old_addr[ETH_ALEN];
136 if (!is_valid_ether_addr(addr->sa_data))
137 return -EADDRNOTAVAIL;
139 ether_addr_copy(old_addr, dev->dev_addr);
140 ether_addr_copy(dev->dev_addr, addr->sa_data);
142 /* only modify transtable if it has been initialized before */
143 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
144 return 0;
146 rcu_read_lock();
147 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
148 batadv_tt_local_remove(bat_priv, old_addr, vlan->vid,
149 "mac address changed", false);
150 batadv_tt_local_add(dev, addr->sa_data, vlan->vid,
151 BATADV_NULL_IFINDEX, BATADV_NO_MARK);
153 rcu_read_unlock();
155 return 0;
158 static int batadv_interface_change_mtu(struct net_device *dev, int new_mtu)
160 /* check ranges */
161 if ((new_mtu < 68) || (new_mtu > batadv_hardif_min_mtu(dev)))
162 return -EINVAL;
164 dev->mtu = new_mtu;
166 return 0;
170 * batadv_interface_set_rx_mode - set the rx mode of a device
171 * @dev: registered network device to modify
173 * We do not actually need to set any rx filters for the virtual batman
174 * soft interface. However a dummy handler enables a user to set static
175 * multicast listeners for instance.
177 static void batadv_interface_set_rx_mode(struct net_device *dev)
181 static int batadv_interface_tx(struct sk_buff *skb,
182 struct net_device *soft_iface)
184 struct ethhdr *ethhdr;
185 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
186 struct batadv_hard_iface *primary_if = NULL;
187 struct batadv_bcast_packet *bcast_packet;
188 __be16 ethertype = htons(ETH_P_BATMAN);
189 static const u8 stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00,
190 0x00, 0x00};
191 static const u8 ectp_addr[ETH_ALEN] = {0xCF, 0x00, 0x00, 0x00,
192 0x00, 0x00};
193 enum batadv_dhcp_recipient dhcp_rcp = BATADV_DHCP_NO;
194 u8 *dst_hint = NULL, chaddr[ETH_ALEN];
195 struct vlan_ethhdr *vhdr;
196 unsigned int header_len = 0;
197 int data_len = skb->len, ret;
198 unsigned long brd_delay = 1;
199 bool do_bcast = false, client_added;
200 unsigned short vid;
201 u32 seqno;
202 int gw_mode;
203 enum batadv_forw_mode forw_mode;
204 struct batadv_orig_node *mcast_single_orig = NULL;
205 int network_offset = ETH_HLEN;
207 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
208 goto dropped;
210 soft_iface->trans_start = jiffies;
211 vid = batadv_get_vid(skb, 0);
212 ethhdr = eth_hdr(skb);
214 switch (ntohs(ethhdr->h_proto)) {
215 case ETH_P_8021Q:
216 vhdr = vlan_eth_hdr(skb);
218 if (vhdr->h_vlan_encapsulated_proto != ethertype) {
219 network_offset += VLAN_HLEN;
220 break;
223 /* fall through */
224 case ETH_P_BATMAN:
225 goto dropped;
228 skb_set_network_header(skb, network_offset);
230 if (batadv_bla_tx(bat_priv, skb, vid))
231 goto dropped;
233 /* skb->data might have been reallocated by batadv_bla_tx() */
234 ethhdr = eth_hdr(skb);
236 /* Register the client MAC in the transtable */
237 if (!is_multicast_ether_addr(ethhdr->h_source)) {
238 client_added = batadv_tt_local_add(soft_iface, ethhdr->h_source,
239 vid, skb->skb_iif,
240 skb->mark);
241 if (!client_added)
242 goto dropped;
245 /* don't accept stp packets. STP does not help in meshes.
246 * better use the bridge loop avoidance ...
248 * The same goes for ECTP sent at least by some Cisco Switches,
249 * it might confuse the mesh when used with bridge loop avoidance.
251 if (batadv_compare_eth(ethhdr->h_dest, stp_addr))
252 goto dropped;
254 if (batadv_compare_eth(ethhdr->h_dest, ectp_addr))
255 goto dropped;
257 gw_mode = atomic_read(&bat_priv->gw_mode);
258 if (is_multicast_ether_addr(ethhdr->h_dest)) {
259 /* if gw mode is off, broadcast every packet */
260 if (gw_mode == BATADV_GW_MODE_OFF) {
261 do_bcast = true;
262 goto send;
265 dhcp_rcp = batadv_gw_dhcp_recipient_get(skb, &header_len,
266 chaddr);
267 /* skb->data may have been modified by
268 * batadv_gw_dhcp_recipient_get()
270 ethhdr = eth_hdr(skb);
271 /* if gw_mode is on, broadcast any non-DHCP message.
272 * All the DHCP packets are going to be sent as unicast
274 if (dhcp_rcp == BATADV_DHCP_NO) {
275 do_bcast = true;
276 goto send;
279 if (dhcp_rcp == BATADV_DHCP_TO_CLIENT)
280 dst_hint = chaddr;
281 else if ((gw_mode == BATADV_GW_MODE_SERVER) &&
282 (dhcp_rcp == BATADV_DHCP_TO_SERVER))
283 /* gateways should not forward any DHCP message if
284 * directed to a DHCP server
286 goto dropped;
288 send:
289 if (do_bcast && !is_broadcast_ether_addr(ethhdr->h_dest)) {
290 forw_mode = batadv_mcast_forw_mode(bat_priv, skb,
291 &mcast_single_orig);
292 if (forw_mode == BATADV_FORW_NONE)
293 goto dropped;
295 if (forw_mode == BATADV_FORW_SINGLE)
296 do_bcast = false;
300 batadv_skb_set_priority(skb, 0);
302 /* ethernet packet should be broadcasted */
303 if (do_bcast) {
304 primary_if = batadv_primary_if_get_selected(bat_priv);
305 if (!primary_if)
306 goto dropped;
308 /* in case of ARP request, we do not immediately broadcasti the
309 * packet, instead we first wait for DAT to try to retrieve the
310 * correct ARP entry
312 if (batadv_dat_snoop_outgoing_arp_request(bat_priv, skb))
313 brd_delay = msecs_to_jiffies(ARP_REQ_DELAY);
315 if (batadv_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
316 goto dropped;
318 bcast_packet = (struct batadv_bcast_packet *)skb->data;
319 bcast_packet->version = BATADV_COMPAT_VERSION;
320 bcast_packet->ttl = BATADV_TTL;
322 /* batman packet type: broadcast */
323 bcast_packet->packet_type = BATADV_BCAST;
324 bcast_packet->reserved = 0;
326 /* hw address of first interface is the orig mac because only
327 * this mac is known throughout the mesh
329 ether_addr_copy(bcast_packet->orig,
330 primary_if->net_dev->dev_addr);
332 /* set broadcast sequence number */
333 seqno = atomic_inc_return(&bat_priv->bcast_seqno);
334 bcast_packet->seqno = htonl(seqno);
336 batadv_add_bcast_packet_to_list(bat_priv, skb, brd_delay);
338 /* a copy is stored in the bcast list, therefore removing
339 * the original skb.
341 kfree_skb(skb);
343 /* unicast packet */
344 } else {
345 /* DHCP packets going to a server will use the GW feature */
346 if (dhcp_rcp == BATADV_DHCP_TO_SERVER) {
347 ret = batadv_gw_out_of_range(bat_priv, skb);
348 if (ret)
349 goto dropped;
350 ret = batadv_send_skb_via_gw(bat_priv, skb, vid);
351 } else if (mcast_single_orig) {
352 ret = batadv_send_skb_unicast(bat_priv, skb,
353 BATADV_UNICAST, 0,
354 mcast_single_orig, vid);
355 } else {
356 if (batadv_dat_snoop_outgoing_arp_request(bat_priv,
357 skb))
358 goto dropped;
360 batadv_dat_snoop_outgoing_arp_reply(bat_priv, skb);
362 ret = batadv_send_skb_via_tt(bat_priv, skb, dst_hint,
363 vid);
365 if (ret == NET_XMIT_DROP)
366 goto dropped_freed;
369 batadv_inc_counter(bat_priv, BATADV_CNT_TX);
370 batadv_add_counter(bat_priv, BATADV_CNT_TX_BYTES, data_len);
371 goto end;
373 dropped:
374 kfree_skb(skb);
375 dropped_freed:
376 batadv_inc_counter(bat_priv, BATADV_CNT_TX_DROPPED);
377 end:
378 if (primary_if)
379 batadv_hardif_free_ref(primary_if);
380 return NETDEV_TX_OK;
383 void batadv_interface_rx(struct net_device *soft_iface,
384 struct sk_buff *skb, struct batadv_hard_iface *recv_if,
385 int hdr_size, struct batadv_orig_node *orig_node)
387 struct batadv_bcast_packet *batadv_bcast_packet;
388 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
389 __be16 ethertype = htons(ETH_P_BATMAN);
390 struct vlan_ethhdr *vhdr;
391 struct ethhdr *ethhdr;
392 unsigned short vid;
393 bool is_bcast;
395 batadv_bcast_packet = (struct batadv_bcast_packet *)skb->data;
396 is_bcast = (batadv_bcast_packet->packet_type == BATADV_BCAST);
398 /* check if enough space is available for pulling, and pull */
399 if (!pskb_may_pull(skb, hdr_size))
400 goto dropped;
402 skb_pull_rcsum(skb, hdr_size);
403 skb_reset_mac_header(skb);
405 /* clean the netfilter state now that the batman-adv header has been
406 * removed
408 nf_reset(skb);
410 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
411 goto dropped;
413 vid = batadv_get_vid(skb, 0);
414 ethhdr = eth_hdr(skb);
416 switch (ntohs(ethhdr->h_proto)) {
417 case ETH_P_8021Q:
418 if (!pskb_may_pull(skb, VLAN_ETH_HLEN))
419 goto dropped;
421 vhdr = (struct vlan_ethhdr *)skb->data;
423 if (vhdr->h_vlan_encapsulated_proto != ethertype)
424 break;
426 /* fall through */
427 case ETH_P_BATMAN:
428 goto dropped;
431 /* skb->dev & skb->pkt_type are set here */
432 skb->protocol = eth_type_trans(skb, soft_iface);
433 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
435 batadv_inc_counter(bat_priv, BATADV_CNT_RX);
436 batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
437 skb->len + ETH_HLEN);
439 soft_iface->last_rx = jiffies;
441 /* Let the bridge loop avoidance check the packet. If will
442 * not handle it, we can safely push it up.
444 if (batadv_bla_rx(bat_priv, skb, vid, is_bcast))
445 goto out;
447 if (orig_node)
448 batadv_tt_add_temporary_global_entry(bat_priv, orig_node,
449 ethhdr->h_source, vid);
451 if (is_multicast_ether_addr(ethhdr->h_dest)) {
452 /* set the mark on broadcast packets if AP isolation is ON and
453 * the packet is coming from an "isolated" client
455 if (batadv_vlan_ap_isola_get(bat_priv, vid) &&
456 batadv_tt_global_is_isolated(bat_priv, ethhdr->h_source,
457 vid)) {
458 /* save bits in skb->mark not covered by the mask and
459 * apply the mark on the rest
461 skb->mark &= ~bat_priv->isolation_mark_mask;
462 skb->mark |= bat_priv->isolation_mark;
464 } else if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source,
465 ethhdr->h_dest, vid)) {
466 goto dropped;
469 netif_rx(skb);
470 goto out;
472 dropped:
473 kfree_skb(skb);
474 out:
475 return;
479 * batadv_softif_vlan_free_ref - decrease the vlan object refcounter and
480 * possibly free it
481 * @softif_vlan: the vlan object to release
483 void batadv_softif_vlan_free_ref(struct batadv_softif_vlan *vlan)
485 if (!vlan)
486 return;
488 if (atomic_dec_and_test(&vlan->refcount)) {
489 spin_lock_bh(&vlan->bat_priv->softif_vlan_list_lock);
490 hlist_del_rcu(&vlan->list);
491 spin_unlock_bh(&vlan->bat_priv->softif_vlan_list_lock);
493 kfree_rcu(vlan, rcu);
498 * batadv_softif_vlan_get - get the vlan object for a specific vid
499 * @bat_priv: the bat priv with all the soft interface information
500 * @vid: the identifier of the vlan object to retrieve
502 * Returns the private data of the vlan matching the vid passed as argument or
503 * NULL otherwise. The refcounter of the returned object is incremented by 1.
505 struct batadv_softif_vlan *batadv_softif_vlan_get(struct batadv_priv *bat_priv,
506 unsigned short vid)
508 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
510 rcu_read_lock();
511 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
512 if (vlan_tmp->vid != vid)
513 continue;
515 if (!atomic_inc_not_zero(&vlan_tmp->refcount))
516 continue;
518 vlan = vlan_tmp;
519 break;
521 rcu_read_unlock();
523 return vlan;
527 * batadv_create_vlan - allocate the needed resources for a new vlan
528 * @bat_priv: the bat priv with all the soft interface information
529 * @vid: the VLAN identifier
531 * Returns 0 on success, a negative error otherwise.
533 int batadv_softif_create_vlan(struct batadv_priv *bat_priv, unsigned short vid)
535 struct batadv_softif_vlan *vlan;
536 int err;
538 vlan = batadv_softif_vlan_get(bat_priv, vid);
539 if (vlan) {
540 batadv_softif_vlan_free_ref(vlan);
541 return -EEXIST;
544 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
545 if (!vlan)
546 return -ENOMEM;
548 vlan->bat_priv = bat_priv;
549 vlan->vid = vid;
550 atomic_set(&vlan->refcount, 1);
552 atomic_set(&vlan->ap_isolation, 0);
554 err = batadv_sysfs_add_vlan(bat_priv->soft_iface, vlan);
555 if (err) {
556 kfree(vlan);
557 return err;
560 spin_lock_bh(&bat_priv->softif_vlan_list_lock);
561 hlist_add_head_rcu(&vlan->list, &bat_priv->softif_vlan_list);
562 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
564 /* add a new TT local entry. This one will be marked with the NOPURGE
565 * flag
567 batadv_tt_local_add(bat_priv->soft_iface,
568 bat_priv->soft_iface->dev_addr, vid,
569 BATADV_NULL_IFINDEX, BATADV_NO_MARK);
571 return 0;
575 * batadv_softif_destroy_vlan - remove and destroy a softif_vlan object
576 * @bat_priv: the bat priv with all the soft interface information
577 * @vlan: the object to remove
579 static void batadv_softif_destroy_vlan(struct batadv_priv *bat_priv,
580 struct batadv_softif_vlan *vlan)
582 /* explicitly remove the associated TT local entry because it is marked
583 * with the NOPURGE flag
585 batadv_tt_local_remove(bat_priv, bat_priv->soft_iface->dev_addr,
586 vlan->vid, "vlan interface destroyed", false);
588 batadv_sysfs_del_vlan(bat_priv, vlan);
589 batadv_softif_vlan_free_ref(vlan);
593 * batadv_interface_add_vid - ndo_add_vid API implementation
594 * @dev: the netdev of the mesh interface
595 * @vid: identifier of the new vlan
597 * Set up all the internal structures for handling the new vlan on top of the
598 * mesh interface
600 * Returns 0 on success or a negative error code in case of failure.
602 static int batadv_interface_add_vid(struct net_device *dev, __be16 proto,
603 unsigned short vid)
605 struct batadv_priv *bat_priv = netdev_priv(dev);
606 struct batadv_softif_vlan *vlan;
607 int ret;
609 /* only 802.1Q vlans are supported.
610 * batman-adv does not know how to handle other types
612 if (proto != htons(ETH_P_8021Q))
613 return -EINVAL;
615 vid |= BATADV_VLAN_HAS_TAG;
617 /* if a new vlan is getting created and it already exists, it means that
618 * it was not deleted yet. batadv_softif_vlan_get() increases the
619 * refcount in order to revive the object.
621 * if it does not exist then create it.
623 vlan = batadv_softif_vlan_get(bat_priv, vid);
624 if (!vlan)
625 return batadv_softif_create_vlan(bat_priv, vid);
627 /* recreate the sysfs object if it was already destroyed (and it should
628 * be since we received a kill_vid() for this vlan
630 if (!vlan->kobj) {
631 ret = batadv_sysfs_add_vlan(bat_priv->soft_iface, vlan);
632 if (ret) {
633 batadv_softif_vlan_free_ref(vlan);
634 return ret;
638 /* add a new TT local entry. This one will be marked with the NOPURGE
639 * flag. This must be added again, even if the vlan object already
640 * exists, because the entry was deleted by kill_vid()
642 batadv_tt_local_add(bat_priv->soft_iface,
643 bat_priv->soft_iface->dev_addr, vid,
644 BATADV_NULL_IFINDEX, BATADV_NO_MARK);
646 return 0;
650 * batadv_interface_kill_vid - ndo_kill_vid API implementation
651 * @dev: the netdev of the mesh interface
652 * @vid: identifier of the deleted vlan
654 * Destroy all the internal structures used to handle the vlan identified by vid
655 * on top of the mesh interface
657 * Returns 0 on success, -EINVAL if the specified prototype is not ETH_P_8021Q
658 * or -ENOENT if the specified vlan id wasn't registered.
660 static int batadv_interface_kill_vid(struct net_device *dev, __be16 proto,
661 unsigned short vid)
663 struct batadv_priv *bat_priv = netdev_priv(dev);
664 struct batadv_softif_vlan *vlan;
666 /* only 802.1Q vlans are supported. batman-adv does not know how to
667 * handle other types
669 if (proto != htons(ETH_P_8021Q))
670 return -EINVAL;
672 vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
673 if (!vlan)
674 return -ENOENT;
676 batadv_softif_destroy_vlan(bat_priv, vlan);
678 /* finally free the vlan object */
679 batadv_softif_vlan_free_ref(vlan);
681 return 0;
684 /* batman-adv network devices have devices nesting below it and are a special
685 * "super class" of normal network devices; split their locks off into a
686 * separate class since they always nest.
688 static struct lock_class_key batadv_netdev_xmit_lock_key;
689 static struct lock_class_key batadv_netdev_addr_lock_key;
692 * batadv_set_lockdep_class_one - Set lockdep class for a single tx queue
693 * @dev: device which owns the tx queue
694 * @txq: tx queue to modify
695 * @_unused: always NULL
697 static void batadv_set_lockdep_class_one(struct net_device *dev,
698 struct netdev_queue *txq,
699 void *_unused)
701 lockdep_set_class(&txq->_xmit_lock, &batadv_netdev_xmit_lock_key);
705 * batadv_set_lockdep_class - Set txq and addr_list lockdep class
706 * @dev: network device to modify
708 static void batadv_set_lockdep_class(struct net_device *dev)
710 lockdep_set_class(&dev->addr_list_lock, &batadv_netdev_addr_lock_key);
711 netdev_for_each_tx_queue(dev, batadv_set_lockdep_class_one, NULL);
715 * batadv_softif_destroy_finish - cleans up the remains of a softif
716 * @work: work queue item
718 * Free the parts of the soft interface which can not be removed under
719 * rtnl lock (to prevent deadlock situations).
721 static void batadv_softif_destroy_finish(struct work_struct *work)
723 struct batadv_softif_vlan *vlan;
724 struct batadv_priv *bat_priv;
725 struct net_device *soft_iface;
727 bat_priv = container_of(work, struct batadv_priv,
728 cleanup_work);
729 soft_iface = bat_priv->soft_iface;
731 /* destroy the "untagged" VLAN */
732 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
733 if (vlan) {
734 batadv_softif_destroy_vlan(bat_priv, vlan);
735 batadv_softif_vlan_free_ref(vlan);
738 batadv_sysfs_del_meshif(soft_iface);
739 unregister_netdev(soft_iface);
743 * batadv_softif_init_late - late stage initialization of soft interface
744 * @dev: registered network device to modify
746 * Returns error code on failures
748 static int batadv_softif_init_late(struct net_device *dev)
750 struct batadv_priv *bat_priv;
751 u32 random_seqno;
752 int ret;
753 size_t cnt_len = sizeof(u64) * BATADV_CNT_NUM;
755 batadv_set_lockdep_class(dev);
757 bat_priv = netdev_priv(dev);
758 bat_priv->soft_iface = dev;
759 INIT_WORK(&bat_priv->cleanup_work, batadv_softif_destroy_finish);
761 /* batadv_interface_stats() needs to be available as soon as
762 * register_netdevice() has been called
764 bat_priv->bat_counters = __alloc_percpu(cnt_len, __alignof__(u64));
765 if (!bat_priv->bat_counters)
766 return -ENOMEM;
768 atomic_set(&bat_priv->aggregated_ogms, 1);
769 atomic_set(&bat_priv->bonding, 0);
770 #ifdef CONFIG_BATMAN_ADV_BLA
771 atomic_set(&bat_priv->bridge_loop_avoidance, 1);
772 #endif
773 #ifdef CONFIG_BATMAN_ADV_DAT
774 atomic_set(&bat_priv->distributed_arp_table, 1);
775 #endif
776 #ifdef CONFIG_BATMAN_ADV_MCAST
777 bat_priv->mcast.flags = BATADV_NO_FLAGS;
778 atomic_set(&bat_priv->multicast_mode, 1);
779 atomic_set(&bat_priv->mcast.num_disabled, 0);
780 atomic_set(&bat_priv->mcast.num_want_all_unsnoopables, 0);
781 atomic_set(&bat_priv->mcast.num_want_all_ipv4, 0);
782 atomic_set(&bat_priv->mcast.num_want_all_ipv6, 0);
783 #endif
784 atomic_set(&bat_priv->gw_mode, BATADV_GW_MODE_OFF);
785 atomic_set(&bat_priv->gw_sel_class, 20);
786 atomic_set(&bat_priv->gw.bandwidth_down, 100);
787 atomic_set(&bat_priv->gw.bandwidth_up, 20);
788 atomic_set(&bat_priv->orig_interval, 1000);
789 atomic_set(&bat_priv->hop_penalty, 30);
790 #ifdef CONFIG_BATMAN_ADV_DEBUG
791 atomic_set(&bat_priv->log_level, 0);
792 #endif
793 atomic_set(&bat_priv->fragmentation, 1);
794 atomic_set(&bat_priv->packet_size_max, ETH_DATA_LEN);
795 atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN);
796 atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN);
798 atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
799 atomic_set(&bat_priv->bcast_seqno, 1);
800 atomic_set(&bat_priv->tt.vn, 0);
801 atomic_set(&bat_priv->tt.local_changes, 0);
802 atomic_set(&bat_priv->tt.ogm_append_cnt, 0);
803 #ifdef CONFIG_BATMAN_ADV_BLA
804 atomic_set(&bat_priv->bla.num_requests, 0);
805 #endif
806 bat_priv->tt.last_changeset = NULL;
807 bat_priv->tt.last_changeset_len = 0;
808 bat_priv->isolation_mark = 0;
809 bat_priv->isolation_mark_mask = 0;
811 /* randomize initial seqno to avoid collision */
812 get_random_bytes(&random_seqno, sizeof(random_seqno));
813 atomic_set(&bat_priv->frag_seqno, random_seqno);
815 bat_priv->primary_if = NULL;
816 bat_priv->num_ifaces = 0;
818 batadv_nc_init_bat_priv(bat_priv);
820 ret = batadv_algo_select(bat_priv, batadv_routing_algo);
821 if (ret < 0)
822 goto free_bat_counters;
824 ret = batadv_debugfs_add_meshif(dev);
825 if (ret < 0)
826 goto free_bat_counters;
828 ret = batadv_mesh_init(dev);
829 if (ret < 0)
830 goto unreg_debugfs;
832 return 0;
834 unreg_debugfs:
835 batadv_debugfs_del_meshif(dev);
836 free_bat_counters:
837 free_percpu(bat_priv->bat_counters);
838 bat_priv->bat_counters = NULL;
840 return ret;
844 * batadv_softif_slave_add - Add a slave interface to a batadv_soft_interface
845 * @dev: batadv_soft_interface used as master interface
846 * @slave_dev: net_device which should become the slave interface
848 * Return 0 if successful or error otherwise.
850 static int batadv_softif_slave_add(struct net_device *dev,
851 struct net_device *slave_dev)
853 struct batadv_hard_iface *hard_iface;
854 int ret = -EINVAL;
856 hard_iface = batadv_hardif_get_by_netdev(slave_dev);
857 if (!hard_iface || hard_iface->soft_iface)
858 goto out;
860 ret = batadv_hardif_enable_interface(hard_iface, dev->name);
862 out:
863 if (hard_iface)
864 batadv_hardif_free_ref(hard_iface);
865 return ret;
869 * batadv_softif_slave_del - Delete a slave iface from a batadv_soft_interface
870 * @dev: batadv_soft_interface used as master interface
871 * @slave_dev: net_device which should be removed from the master interface
873 * Return 0 if successful or error otherwise.
875 static int batadv_softif_slave_del(struct net_device *dev,
876 struct net_device *slave_dev)
878 struct batadv_hard_iface *hard_iface;
879 int ret = -EINVAL;
881 hard_iface = batadv_hardif_get_by_netdev(slave_dev);
883 if (!hard_iface || hard_iface->soft_iface != dev)
884 goto out;
886 batadv_hardif_disable_interface(hard_iface, BATADV_IF_CLEANUP_KEEP);
887 ret = 0;
889 out:
890 if (hard_iface)
891 batadv_hardif_free_ref(hard_iface);
892 return ret;
895 static const struct net_device_ops batadv_netdev_ops = {
896 .ndo_init = batadv_softif_init_late,
897 .ndo_open = batadv_interface_open,
898 .ndo_stop = batadv_interface_release,
899 .ndo_get_stats = batadv_interface_stats,
900 .ndo_vlan_rx_add_vid = batadv_interface_add_vid,
901 .ndo_vlan_rx_kill_vid = batadv_interface_kill_vid,
902 .ndo_set_mac_address = batadv_interface_set_mac_addr,
903 .ndo_change_mtu = batadv_interface_change_mtu,
904 .ndo_set_rx_mode = batadv_interface_set_rx_mode,
905 .ndo_start_xmit = batadv_interface_tx,
906 .ndo_validate_addr = eth_validate_addr,
907 .ndo_add_slave = batadv_softif_slave_add,
908 .ndo_del_slave = batadv_softif_slave_del,
912 * batadv_softif_free - Deconstructor of batadv_soft_interface
913 * @dev: Device to cleanup and remove
915 static void batadv_softif_free(struct net_device *dev)
917 batadv_debugfs_del_meshif(dev);
918 batadv_mesh_free(dev);
920 /* some scheduled RCU callbacks need the bat_priv struct to accomplish
921 * their tasks. Wait for them all to be finished before freeing the
922 * netdev and its private data (bat_priv)
924 rcu_barrier();
926 free_netdev(dev);
930 * batadv_softif_init_early - early stage initialization of soft interface
931 * @dev: registered network device to modify
933 static void batadv_softif_init_early(struct net_device *dev)
935 struct batadv_priv *priv = netdev_priv(dev);
937 ether_setup(dev);
939 dev->netdev_ops = &batadv_netdev_ops;
940 dev->destructor = batadv_softif_free;
941 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
942 dev->priv_flags |= IFF_NO_QUEUE;
944 /* can't call min_mtu, because the needed variables
945 * have not been initialized yet
947 dev->mtu = ETH_DATA_LEN;
949 /* generate random address */
950 eth_hw_addr_random(dev);
952 dev->ethtool_ops = &batadv_ethtool_ops;
954 memset(priv, 0, sizeof(*priv));
957 struct net_device *batadv_softif_create(const char *name)
959 struct net_device *soft_iface;
960 int ret;
962 soft_iface = alloc_netdev(sizeof(struct batadv_priv), name,
963 NET_NAME_UNKNOWN, batadv_softif_init_early);
964 if (!soft_iface)
965 return NULL;
967 soft_iface->rtnl_link_ops = &batadv_link_ops;
969 ret = register_netdevice(soft_iface);
970 if (ret < 0) {
971 pr_err("Unable to register the batman interface '%s': %i\n",
972 name, ret);
973 free_netdev(soft_iface);
974 return NULL;
977 return soft_iface;
981 * batadv_softif_destroy_sysfs - deletion of batadv_soft_interface via sysfs
982 * @soft_iface: the to-be-removed batman-adv interface
984 void batadv_softif_destroy_sysfs(struct net_device *soft_iface)
986 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
988 queue_work(batadv_event_workqueue, &bat_priv->cleanup_work);
992 * batadv_softif_destroy_netlink - deletion of batadv_soft_interface via netlink
993 * @soft_iface: the to-be-removed batman-adv interface
994 * @head: list pointer
996 static void batadv_softif_destroy_netlink(struct net_device *soft_iface,
997 struct list_head *head)
999 struct batadv_hard_iface *hard_iface;
1001 list_for_each_entry(hard_iface, &batadv_hardif_list, list) {
1002 if (hard_iface->soft_iface == soft_iface)
1003 batadv_hardif_disable_interface(hard_iface,
1004 BATADV_IF_CLEANUP_KEEP);
1007 batadv_sysfs_del_meshif(soft_iface);
1008 unregister_netdevice_queue(soft_iface, head);
1011 int batadv_softif_is_valid(const struct net_device *net_dev)
1013 if (net_dev->netdev_ops->ndo_start_xmit == batadv_interface_tx)
1014 return 1;
1016 return 0;
1019 struct rtnl_link_ops batadv_link_ops __read_mostly = {
1020 .kind = "batadv",
1021 .priv_size = sizeof(struct batadv_priv),
1022 .setup = batadv_softif_init_early,
1023 .dellink = batadv_softif_destroy_netlink,
1026 /* ethtool */
1027 static int batadv_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1029 cmd->supported = 0;
1030 cmd->advertising = 0;
1031 ethtool_cmd_speed_set(cmd, SPEED_10);
1032 cmd->duplex = DUPLEX_FULL;
1033 cmd->port = PORT_TP;
1034 cmd->phy_address = 0;
1035 cmd->transceiver = XCVR_INTERNAL;
1036 cmd->autoneg = AUTONEG_DISABLE;
1037 cmd->maxtxpkt = 0;
1038 cmd->maxrxpkt = 0;
1040 return 0;
1043 static void batadv_get_drvinfo(struct net_device *dev,
1044 struct ethtool_drvinfo *info)
1046 strlcpy(info->driver, "B.A.T.M.A.N. advanced", sizeof(info->driver));
1047 strlcpy(info->version, BATADV_SOURCE_VERSION, sizeof(info->version));
1048 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
1049 strlcpy(info->bus_info, "batman", sizeof(info->bus_info));
1052 static u32 batadv_get_msglevel(struct net_device *dev)
1054 return -EOPNOTSUPP;
1057 static void batadv_set_msglevel(struct net_device *dev, u32 value)
1061 static u32 batadv_get_link(struct net_device *dev)
1063 return 1;
1066 /* Inspired by drivers/net/ethernet/dlink/sundance.c:1702
1067 * Declare each description string in struct.name[] to get fixed sized buffer
1068 * and compile time checking for strings longer than ETH_GSTRING_LEN.
1070 static const struct {
1071 const char name[ETH_GSTRING_LEN];
1072 } batadv_counters_strings[] = {
1073 { "tx" },
1074 { "tx_bytes" },
1075 { "tx_dropped" },
1076 { "rx" },
1077 { "rx_bytes" },
1078 { "forward" },
1079 { "forward_bytes" },
1080 { "mgmt_tx" },
1081 { "mgmt_tx_bytes" },
1082 { "mgmt_rx" },
1083 { "mgmt_rx_bytes" },
1084 { "frag_tx" },
1085 { "frag_tx_bytes" },
1086 { "frag_rx" },
1087 { "frag_rx_bytes" },
1088 { "frag_fwd" },
1089 { "frag_fwd_bytes" },
1090 { "tt_request_tx" },
1091 { "tt_request_rx" },
1092 { "tt_response_tx" },
1093 { "tt_response_rx" },
1094 { "tt_roam_adv_tx" },
1095 { "tt_roam_adv_rx" },
1096 #ifdef CONFIG_BATMAN_ADV_DAT
1097 { "dat_get_tx" },
1098 { "dat_get_rx" },
1099 { "dat_put_tx" },
1100 { "dat_put_rx" },
1101 { "dat_cached_reply_tx" },
1102 #endif
1103 #ifdef CONFIG_BATMAN_ADV_NC
1104 { "nc_code" },
1105 { "nc_code_bytes" },
1106 { "nc_recode" },
1107 { "nc_recode_bytes" },
1108 { "nc_buffer" },
1109 { "nc_decode" },
1110 { "nc_decode_bytes" },
1111 { "nc_decode_failed" },
1112 { "nc_sniffed" },
1113 #endif
1116 static void batadv_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1118 if (stringset == ETH_SS_STATS)
1119 memcpy(data, batadv_counters_strings,
1120 sizeof(batadv_counters_strings));
1123 static void batadv_get_ethtool_stats(struct net_device *dev,
1124 struct ethtool_stats *stats, u64 *data)
1126 struct batadv_priv *bat_priv = netdev_priv(dev);
1127 int i;
1129 for (i = 0; i < BATADV_CNT_NUM; i++)
1130 data[i] = batadv_sum_counter(bat_priv, i);
1133 static int batadv_get_sset_count(struct net_device *dev, int stringset)
1135 if (stringset == ETH_SS_STATS)
1136 return BATADV_CNT_NUM;
1138 return -EOPNOTSUPP;