^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright (C) 2007-2020 B.A.T.M.A.N. contributors:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Marek Lindner, Simon Wunderlich
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "main.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/build_bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/byteorder/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/crc32c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/genetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/if_ether.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/if_vlan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/kobject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/kref.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/rculist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <net/dsfield.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <uapi/linux/batadv_packet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <uapi/linux/batman_adv.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include "bat_algo.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include "bat_iv_ogm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include "bat_v.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include "bridge_loop_avoidance.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include "debugfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include "distributed-arp-table.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include "gateway_client.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include "gateway_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include "hard-interface.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include "icmp_socket.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include "log.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include "multicast.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include "netlink.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include "network-coding.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include "originator.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include "routing.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include "send.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include "soft-interface.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include "tp_meter.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #include "translation-table.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* List manipulations on hardif_list have to be rtnl_lock()'ed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * list traversals just rcu-locked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct list_head batadv_hardif_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) unsigned int batadv_hardif_generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int (*batadv_rx_handler[256])(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct batadv_hard_iface *recv_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) unsigned char batadv_broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct workqueue_struct *batadv_event_workqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static void batadv_recv_handler_init(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define BATADV_UEV_TYPE_VAR "BATTYPE="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define BATADV_UEV_ACTION_VAR "BATACTION="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define BATADV_UEV_DATA_VAR "BATDATA="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static char *batadv_uev_action_str[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) "add",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) "del",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) "change",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) "loopdetect",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static char *batadv_uev_type_str[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) "gw",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) "bla",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int __init batadv_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ret = batadv_tt_cache_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) INIT_LIST_HEAD(&batadv_hardif_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) batadv_algo_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) batadv_recv_handler_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) batadv_v_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) batadv_iv_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) batadv_nc_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) batadv_tp_meter_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) batadv_event_workqueue = create_singlethread_workqueue("bat_events");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (!batadv_event_workqueue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) goto err_create_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) batadv_socket_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) batadv_debugfs_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) register_netdevice_notifier(&batadv_hard_if_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) rtnl_link_register(&batadv_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) batadv_netlink_register();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) BATADV_SOURCE_VERSION, BATADV_COMPAT_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) err_create_wq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) batadv_tt_cache_destroy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static void __exit batadv_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) batadv_debugfs_destroy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) batadv_netlink_unregister();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) rtnl_link_unregister(&batadv_link_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) unregister_netdevice_notifier(&batadv_hard_if_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) flush_workqueue(batadv_event_workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) destroy_workqueue(batadv_event_workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) batadv_event_workqueue = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) rcu_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) batadv_tt_cache_destroy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * batadv_mesh_init() - Initialize soft interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * @soft_iface: netdev struct of the soft interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * Return: 0 on success or negative error number in case of failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int batadv_mesh_init(struct net_device *soft_iface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct batadv_priv *bat_priv = netdev_priv(soft_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) spin_lock_init(&bat_priv->forw_bat_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) spin_lock_init(&bat_priv->forw_bcast_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) spin_lock_init(&bat_priv->tt.changes_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) spin_lock_init(&bat_priv->tt.req_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) spin_lock_init(&bat_priv->tt.roam_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) spin_lock_init(&bat_priv->tt.last_changeset_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) spin_lock_init(&bat_priv->tt.commit_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) spin_lock_init(&bat_priv->gw.list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #ifdef CONFIG_BATMAN_ADV_MCAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) spin_lock_init(&bat_priv->mcast.mla_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) spin_lock_init(&bat_priv->mcast.want_lists_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) spin_lock_init(&bat_priv->tvlv.container_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) spin_lock_init(&bat_priv->tvlv.handler_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) spin_lock_init(&bat_priv->softif_vlan_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) spin_lock_init(&bat_priv->tp_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) INIT_HLIST_HEAD(&bat_priv->gw.gateway_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) #ifdef CONFIG_BATMAN_ADV_MCAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) INIT_HLIST_HEAD(&bat_priv->mcast.want_all_unsnoopables_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) INIT_HLIST_HEAD(&bat_priv->mcast.want_all_ipv4_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) INIT_HLIST_HEAD(&bat_priv->mcast.want_all_ipv6_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) INIT_LIST_HEAD(&bat_priv->tt.changes_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) INIT_HLIST_HEAD(&bat_priv->tt.req_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) INIT_LIST_HEAD(&bat_priv->tt.roam_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #ifdef CONFIG_BATMAN_ADV_MCAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) INIT_HLIST_HEAD(&bat_priv->mcast.mla_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) INIT_HLIST_HEAD(&bat_priv->tvlv.container_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) INIT_HLIST_HEAD(&bat_priv->tvlv.handler_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) INIT_HLIST_HEAD(&bat_priv->softif_vlan_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) INIT_HLIST_HEAD(&bat_priv->tp_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) bat_priv->gw.generation = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) ret = batadv_originator_init(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) goto err_orig;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) ret = batadv_tt_init(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) goto err_tt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) ret = batadv_v_mesh_init(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) goto err_v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) ret = batadv_bla_init(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) goto err_bla;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ret = batadv_dat_init(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) goto err_dat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ret = batadv_nc_mesh_init(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) goto err_nc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) batadv_gw_init(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) batadv_mcast_init(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) atomic_set(&bat_priv->gw.reselect, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) atomic_set(&bat_priv->mesh_state, BATADV_MESH_ACTIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) err_nc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) batadv_dat_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) err_dat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) batadv_bla_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) err_bla:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) batadv_v_mesh_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) err_v:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) batadv_tt_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) err_tt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) batadv_originator_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) err_orig:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) batadv_purge_outstanding_packets(bat_priv, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * batadv_mesh_free() - Deinitialize soft interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * @soft_iface: netdev struct of the soft interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) void batadv_mesh_free(struct net_device *soft_iface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct batadv_priv *bat_priv = netdev_priv(soft_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) batadv_purge_outstanding_packets(bat_priv, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) batadv_gw_node_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) batadv_v_mesh_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) batadv_nc_mesh_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) batadv_dat_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) batadv_bla_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) batadv_mcast_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* Free the TT and the originator tables only after having terminated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * all the other depending components which may use these structures for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * their purposes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) batadv_tt_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /* Since the originator table clean up routine is accessing the TT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * tables as well, it has to be invoked after the TT tables have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * freed and marked as empty. This ensures that no cleanup RCU callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * accessing the TT data are scheduled for later execution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) batadv_originator_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) batadv_gw_free(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) free_percpu(bat_priv->bat_counters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) bat_priv->bat_counters = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * batadv_is_my_mac() - check if the given mac address belongs to any of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * real interfaces in the current mesh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * @addr: the address to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * Return: 'true' if the mac address was found, false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) bool batadv_is_my_mac(struct batadv_priv *bat_priv, const u8 *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) const struct batadv_hard_iface *hard_iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) bool is_my_mac = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (hard_iface->if_status != BATADV_IF_ACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (hard_iface->soft_iface != bat_priv->soft_iface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) is_my_mac = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return is_my_mac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) #ifdef CONFIG_BATMAN_ADV_DEBUGFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * batadv_seq_print_text_primary_if_get() - called from debugfs table printing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * function that requires the primary interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * @seq: debugfs table seq_file struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * Return: primary interface if found or NULL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct batadv_hard_iface *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) batadv_seq_print_text_primary_if_get(struct seq_file *seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) struct net_device *net_dev = (struct net_device *)seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) struct batadv_priv *bat_priv = netdev_priv(net_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) struct batadv_hard_iface *primary_if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) primary_if = batadv_primary_if_get_selected(bat_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (!primary_if) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) seq_printf(seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) net_dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (primary_if->if_status == BATADV_IF_ACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) seq_printf(seq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) "BATMAN mesh %s disabled - primary interface not active\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) net_dev->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) batadv_hardif_put(primary_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) primary_if = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return primary_if;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * batadv_max_header_len() - calculate maximum encapsulation overhead for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * payload packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * Return: the maximum encapsulation overhead in bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) int batadv_max_header_len(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) int header_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) header_len = max_t(int, header_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) sizeof(struct batadv_unicast_packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) header_len = max_t(int, header_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) sizeof(struct batadv_unicast_4addr_packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) header_len = max_t(int, header_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) sizeof(struct batadv_bcast_packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) #ifdef CONFIG_BATMAN_ADV_NC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) header_len = max_t(int, header_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) sizeof(struct batadv_coded_packet));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return header_len + ETH_HLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * batadv_skb_set_priority() - sets skb priority according to packet content
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * @skb: the packet to be sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * @offset: offset to the packet content
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * This function sets a value between 256 and 263 (802.1d priority), which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * can be interpreted by the cfg80211 or other drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) void batadv_skb_set_priority(struct sk_buff *skb, int offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) struct iphdr ip_hdr_tmp, *ip_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) struct ipv6hdr ip6_hdr_tmp, *ip6_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct ethhdr ethhdr_tmp, *ethhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) struct vlan_ethhdr *vhdr, vhdr_tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) u32 prio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) /* already set, do nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (skb->priority >= 256 && skb->priority <= 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) ethhdr = skb_header_pointer(skb, offset, sizeof(*ethhdr), ðhdr_tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (!ethhdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) switch (ethhdr->h_proto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) case htons(ETH_P_8021Q):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) vhdr = skb_header_pointer(skb, offset + sizeof(*vhdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) sizeof(*vhdr), &vhdr_tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (!vhdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) prio = ntohs(vhdr->h_vlan_TCI) & VLAN_PRIO_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) prio = prio >> VLAN_PRIO_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) case htons(ETH_P_IP):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) ip_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) sizeof(*ip_hdr), &ip_hdr_tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (!ip_hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) prio = (ipv4_get_dsfield(ip_hdr) & 0xfc) >> 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) case htons(ETH_P_IPV6):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) ip6_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) sizeof(*ip6_hdr), &ip6_hdr_tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (!ip6_hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) prio = (ipv6_get_dsfield(ip6_hdr) & 0xfc) >> 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) skb->priority = prio + 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) static int batadv_recv_unhandled_packet(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct batadv_hard_iface *recv_if)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /* incoming packets with the batman ethertype received on any active hard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * batadv_batman_skb_recv() - Handle incoming message from an hard interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * @skb: the received packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * @dev: the net device that the packet was received on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * @ptype: packet type of incoming packet (ETH_P_BATMAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * @orig_dev: the original receive net device (e.g. bonded device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) struct packet_type *ptype,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct net_device *orig_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) struct batadv_priv *bat_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) struct batadv_ogm_packet *batadv_ogm_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) struct batadv_hard_iface *hard_iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) u8 idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) hard_iface = container_of(ptype, struct batadv_hard_iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) batman_adv_ptype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) /* Prevent processing a packet received on an interface which is getting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * shut down otherwise the packet may trigger de-reference errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) * further down in the receive path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (!kref_get_unless_zero(&hard_iface->refcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) skb = skb_share_check(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) /* skb was released by skb_share_check() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) goto err_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) /* packet should hold at least type and version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (unlikely(!pskb_may_pull(skb, 2)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) /* expect a valid ethernet header here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (!hard_iface->soft_iface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) bat_priv = netdev_priv(hard_iface->soft_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) /* discard frames on not active interfaces */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (hard_iface->if_status != BATADV_IF_ACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) if (batadv_ogm_packet->version != BATADV_COMPAT_VERSION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) "Drop packet: incompatible batman version (%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) batadv_ogm_packet->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) /* reset control block to avoid left overs from previous users */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) memset(skb->cb, 0, sizeof(struct batadv_skb_cb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) idx = batadv_ogm_packet->packet_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) (*batadv_rx_handler[idx])(skb, hard_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) batadv_hardif_put(hard_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) /* return NET_RX_SUCCESS in any case as we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) * most probably dropped the packet for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * routing-logical reasons.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) err_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) batadv_hardif_put(hard_iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) static void batadv_recv_handler_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) batadv_rx_handler[i] = batadv_recv_unhandled_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) for (i = BATADV_UNICAST_MIN; i <= BATADV_UNICAST_MAX; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) batadv_rx_handler[i] = batadv_recv_unhandled_unicast_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) /* compile time checks for sizes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) BUILD_BUG_ON(sizeof(struct batadv_bla_claim_dst) != 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) BUILD_BUG_ON(sizeof(struct batadv_ogm_packet) != 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) BUILD_BUG_ON(sizeof(struct batadv_icmp_header) != 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) BUILD_BUG_ON(sizeof(struct batadv_icmp_packet) != 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) BUILD_BUG_ON(sizeof(struct batadv_icmp_packet_rr) != 116);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) BUILD_BUG_ON(sizeof(struct batadv_unicast_packet) != 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) BUILD_BUG_ON(sizeof(struct batadv_unicast_4addr_packet) != 18);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) BUILD_BUG_ON(sizeof(struct batadv_frag_packet) != 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) BUILD_BUG_ON(sizeof(struct batadv_bcast_packet) != 14);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) BUILD_BUG_ON(sizeof(struct batadv_coded_packet) != 46);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) BUILD_BUG_ON(sizeof(struct batadv_unicast_tvlv_packet) != 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) BUILD_BUG_ON(sizeof(struct batadv_tvlv_hdr) != 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) BUILD_BUG_ON(sizeof(struct batadv_tvlv_gateway_data) != 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_vlan_data) != 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_change) != 12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) BUILD_BUG_ON(sizeof(struct batadv_tvlv_roam_adv) != 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) i = sizeof_field(struct sk_buff, cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) BUILD_BUG_ON(sizeof(struct batadv_skb_cb) > i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) /* broadcast packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) /* unicast packets ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) /* unicast with 4 addresses packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) /* unicast packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) /* unicast tvlv packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) batadv_rx_handler[BATADV_UNICAST_TVLV] = batadv_recv_unicast_tvlv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) /* batman icmp packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) /* Fragmented packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_frag_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) * batadv_recv_handler_register() - Register handler for batman-adv packet type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) * @packet_type: batadv_packettype which should be handled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) * @recv_handler: receive handler for the packet type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) * Return: 0 on success or negative error number in case of failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) batadv_recv_handler_register(u8 packet_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) int (*recv_handler)(struct sk_buff *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) struct batadv_hard_iface *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) int (*curr)(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) struct batadv_hard_iface *recv_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) curr = batadv_rx_handler[packet_type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (curr != batadv_recv_unhandled_packet &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) curr != batadv_recv_unhandled_unicast_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) batadv_rx_handler[packet_type] = recv_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) * batadv_recv_handler_unregister() - Unregister handler for packet type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) * @packet_type: batadv_packettype which should no longer be handled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) void batadv_recv_handler_unregister(u8 packet_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * batadv_skb_crc32() - calculate CRC32 of the whole packet and skip bytes in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) * the header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) * @skb: skb pointing to fragmented socket buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) * @payload_ptr: Pointer to position inside the head buffer of the skb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) * marking the start of the data to be CRC'ed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) * payload_ptr must always point to an address in the skb head buffer and not to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) * a fragment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * Return: big endian crc32c of the checksummed data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) u32 crc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) unsigned int from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) unsigned int to = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) struct skb_seq_state st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) const u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) unsigned int consumed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) from = (unsigned int)(payload_ptr - skb->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) skb_prepare_seq_read(skb, from, to, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) while ((len = skb_seq_read(consumed, &data, &st)) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) crc = crc32c(crc, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) consumed += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return htonl(crc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) * batadv_get_vid() - extract the VLAN identifier from skb if any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * @skb: the buffer containing the packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * @header_len: length of the batman header preceding the ethernet header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) * Return: VID with the BATADV_VLAN_HAS_TAG flag when the packet embedded in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) * skb is vlan tagged. Otherwise BATADV_NO_FLAGS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) struct ethhdr *ethhdr = (struct ethhdr *)(skb->data + header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) struct vlan_ethhdr *vhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) unsigned short vid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) if (ethhdr->h_proto != htons(ETH_P_8021Q))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) return BATADV_NO_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) if (!pskb_may_pull(skb, header_len + VLAN_ETH_HLEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) return BATADV_NO_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) vhdr = (struct vlan_ethhdr *)(skb->data + header_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) vid |= BATADV_VLAN_HAS_TAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) return vid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) * batadv_vlan_ap_isola_get() - return AP isolation status for the given vlan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * @vid: the VLAN identifier for which the AP isolation attributed as to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * looked up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * Return: true if AP isolation is on for the VLAN identified by vid, false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) * otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) bool ap_isolation_enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) struct batadv_softif_vlan *vlan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) /* if the AP isolation is requested on a VLAN, then check for its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) * setting in the proper VLAN private data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) vlan = batadv_softif_vlan_get(bat_priv, vid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (vlan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) batadv_softif_vlan_put(vlan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) return ap_isolation_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * batadv_throw_uevent() - Send an uevent with batman-adv specific env data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * @bat_priv: the bat priv with all the soft interface information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) * @type: subsystem type of event. Stored in uevent's BATTYPE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) * @action: action type of event. Stored in uevent's BATACTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * @data: string with additional information to the event (ignored for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * BATADV_UEV_DEL). Stored in uevent's BATDATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * Return: 0 on success or negative error number in case of failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) enum batadv_uev_action action, const char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) struct kobject *bat_kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) char *uevent_env[4] = { NULL, NULL, NULL, NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) bat_kobj = &bat_priv->soft_iface->dev.kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) uevent_env[0] = kasprintf(GFP_ATOMIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) "%s%s", BATADV_UEV_TYPE_VAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) batadv_uev_type_str[type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) if (!uevent_env[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) uevent_env[1] = kasprintf(GFP_ATOMIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) "%s%s", BATADV_UEV_ACTION_VAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) batadv_uev_action_str[action]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) if (!uevent_env[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) /* If the event is DEL, ignore the data field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (action != BATADV_UEV_DEL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) uevent_env[2] = kasprintf(GFP_ATOMIC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) "%s%s", BATADV_UEV_DATA_VAR, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (!uevent_env[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) kfree(uevent_env[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) kfree(uevent_env[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) kfree(uevent_env[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) batadv_uev_type_str[type],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) batadv_uev_action_str[action],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) (action == BATADV_UEV_DEL ? "NULL" : data), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) module_init(batadv_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) module_exit(batadv_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) MODULE_AUTHOR(BATADV_DRIVER_AUTHOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) MODULE_DESCRIPTION(BATADV_DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) MODULE_SUPPORTED_DEVICE(BATADV_DRIVER_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) MODULE_VERSION(BATADV_SOURCE_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) MODULE_ALIAS_RTNL_LINK("batadv");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) MODULE_ALIAS_GENL_FAMILY(BATADV_NL_NAME);