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