main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2007-2018 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/build_bug.h>
  21. #include <linux/byteorder/generic.h>
  22. #include <linux/crc32c.h>
  23. #include <linux/errno.h>
  24. #include <linux/genetlink.h>
  25. #include <linux/gfp.h>
  26. #include <linux/if_ether.h>
  27. #include <linux/if_vlan.h>
  28. #include <linux/init.h>
  29. #include <linux/ip.h>
  30. #include <linux/ipv6.h>
  31. #include <linux/kernel.h>
  32. #include <linux/kref.h>
  33. #include <linux/list.h>
  34. #include <linux/module.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/printk.h>
  37. #include <linux/rculist.h>
  38. #include <linux/rcupdate.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/skbuff.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/stddef.h>
  43. #include <linux/string.h>
  44. #include <linux/workqueue.h>
  45. #include <net/dsfield.h>
  46. #include <net/rtnetlink.h>
  47. #include <uapi/linux/batadv_packet.h>
  48. #include <uapi/linux/batman_adv.h>
  49. #include "bat_algo.h"
  50. #include "bat_iv_ogm.h"
  51. #include "bat_v.h"
  52. #include "bridge_loop_avoidance.h"
  53. #include "debugfs.h"
  54. #include "distributed-arp-table.h"
  55. #include "gateway_client.h"
  56. #include "gateway_common.h"
  57. #include "hard-interface.h"
  58. #include "icmp_socket.h"
  59. #include "log.h"
  60. #include "multicast.h"
  61. #include "netlink.h"
  62. #include "network-coding.h"
  63. #include "originator.h"
  64. #include "routing.h"
  65. #include "send.h"
  66. #include "soft-interface.h"
  67. #include "tp_meter.h"
  68. #include "translation-table.h"
  69. /* List manipulations on hardif_list have to be rtnl_lock()'ed,
  70. * list traversals just rcu-locked
  71. */
  72. struct list_head batadv_hardif_list;
  73. static int (*batadv_rx_handler[256])(struct sk_buff *skb,
  74. struct batadv_hard_iface *recv_if);
  75. unsigned char batadv_broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  76. struct workqueue_struct *batadv_event_workqueue;
  77. static void batadv_recv_handler_init(void);
  78. static int __init batadv_init(void)
  79. {
  80. int ret;
  81. ret = batadv_tt_cache_init();
  82. if (ret < 0)
  83. return ret;
  84. INIT_LIST_HEAD(&batadv_hardif_list);
  85. batadv_algo_init();
  86. batadv_recv_handler_init();
  87. batadv_v_init();
  88. batadv_iv_init();
  89. batadv_nc_init();
  90. batadv_tp_meter_init();
  91. batadv_event_workqueue = create_singlethread_workqueue("bat_events");
  92. if (!batadv_event_workqueue)
  93. goto err_create_wq;
  94. batadv_socket_init();
  95. batadv_debugfs_init();
  96. register_netdevice_notifier(&batadv_hard_if_notifier);
  97. rtnl_link_register(&batadv_link_ops);
  98. batadv_netlink_register();
  99. pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n",
  100. BATADV_SOURCE_VERSION, BATADV_COMPAT_VERSION);
  101. return 0;
  102. err_create_wq:
  103. batadv_tt_cache_destroy();
  104. return -ENOMEM;
  105. }
  106. static void __exit batadv_exit(void)
  107. {
  108. batadv_debugfs_destroy();
  109. batadv_netlink_unregister();
  110. rtnl_link_unregister(&batadv_link_ops);
  111. unregister_netdevice_notifier(&batadv_hard_if_notifier);
  112. batadv_hardif_remove_interfaces();
  113. flush_workqueue(batadv_event_workqueue);
  114. destroy_workqueue(batadv_event_workqueue);
  115. batadv_event_workqueue = NULL;
  116. rcu_barrier();
  117. batadv_tt_cache_destroy();
  118. }
  119. /**
  120. * batadv_mesh_init() - Initialize soft interface
  121. * @soft_iface: netdev struct of the soft interface
  122. *
  123. * Return: 0 on success or negative error number in case of failure
  124. */
  125. int batadv_mesh_init(struct net_device *soft_iface)
  126. {
  127. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  128. int ret;
  129. spin_lock_init(&bat_priv->forw_bat_list_lock);
  130. spin_lock_init(&bat_priv->forw_bcast_list_lock);
  131. spin_lock_init(&bat_priv->tt.changes_list_lock);
  132. spin_lock_init(&bat_priv->tt.req_list_lock);
  133. spin_lock_init(&bat_priv->tt.roam_list_lock);
  134. spin_lock_init(&bat_priv->tt.last_changeset_lock);
  135. spin_lock_init(&bat_priv->tt.commit_lock);
  136. spin_lock_init(&bat_priv->gw.list_lock);
  137. #ifdef CONFIG_BATMAN_ADV_MCAST
  138. spin_lock_init(&bat_priv->mcast.want_lists_lock);
  139. #endif
  140. spin_lock_init(&bat_priv->tvlv.container_list_lock);
  141. spin_lock_init(&bat_priv->tvlv.handler_list_lock);
  142. spin_lock_init(&bat_priv->softif_vlan_list_lock);
  143. spin_lock_init(&bat_priv->tp_list_lock);
  144. INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
  145. INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
  146. INIT_HLIST_HEAD(&bat_priv->gw.gateway_list);
  147. #ifdef CONFIG_BATMAN_ADV_MCAST
  148. INIT_HLIST_HEAD(&bat_priv->mcast.want_all_unsnoopables_list);
  149. INIT_HLIST_HEAD(&bat_priv->mcast.want_all_ipv4_list);
  150. INIT_HLIST_HEAD(&bat_priv->mcast.want_all_ipv6_list);
  151. #endif
  152. INIT_LIST_HEAD(&bat_priv->tt.changes_list);
  153. INIT_HLIST_HEAD(&bat_priv->tt.req_list);
  154. INIT_LIST_HEAD(&bat_priv->tt.roam_list);
  155. #ifdef CONFIG_BATMAN_ADV_MCAST
  156. INIT_HLIST_HEAD(&bat_priv->mcast.mla_list);
  157. #endif
  158. INIT_HLIST_HEAD(&bat_priv->tvlv.container_list);
  159. INIT_HLIST_HEAD(&bat_priv->tvlv.handler_list);
  160. INIT_HLIST_HEAD(&bat_priv->softif_vlan_list);
  161. INIT_HLIST_HEAD(&bat_priv->tp_list);
  162. ret = batadv_v_mesh_init(bat_priv);
  163. if (ret < 0)
  164. goto err;
  165. ret = batadv_originator_init(bat_priv);
  166. if (ret < 0)
  167. goto err;
  168. ret = batadv_tt_init(bat_priv);
  169. if (ret < 0)
  170. goto err;
  171. ret = batadv_bla_init(bat_priv);
  172. if (ret < 0)
  173. goto err;
  174. ret = batadv_dat_init(bat_priv);
  175. if (ret < 0)
  176. goto err;
  177. ret = batadv_nc_mesh_init(bat_priv);
  178. if (ret < 0)
  179. goto err;
  180. batadv_gw_init(bat_priv);
  181. batadv_mcast_init(bat_priv);
  182. atomic_set(&bat_priv->gw.reselect, 0);
  183. atomic_set(&bat_priv->mesh_state, BATADV_MESH_ACTIVE);
  184. return 0;
  185. err:
  186. batadv_mesh_free(soft_iface);
  187. return ret;
  188. }
  189. /**
  190. * batadv_mesh_free() - Deinitialize soft interface
  191. * @soft_iface: netdev struct of the soft interface
  192. */
  193. void batadv_mesh_free(struct net_device *soft_iface)
  194. {
  195. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  196. atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
  197. batadv_purge_outstanding_packets(bat_priv, NULL);
  198. batadv_gw_node_free(bat_priv);
  199. batadv_v_mesh_free(bat_priv);
  200. batadv_nc_mesh_free(bat_priv);
  201. batadv_dat_free(bat_priv);
  202. batadv_bla_free(bat_priv);
  203. batadv_mcast_free(bat_priv);
  204. /* Free the TT and the originator tables only after having terminated
  205. * all the other depending components which may use these structures for
  206. * their purposes.
  207. */
  208. batadv_tt_free(bat_priv);
  209. /* Since the originator table clean up routine is accessing the TT
  210. * tables as well, it has to be invoked after the TT tables have been
  211. * freed and marked as empty. This ensures that no cleanup RCU callbacks
  212. * accessing the TT data are scheduled for later execution.
  213. */
  214. batadv_originator_free(bat_priv);
  215. batadv_gw_free(bat_priv);
  216. free_percpu(bat_priv->bat_counters);
  217. bat_priv->bat_counters = NULL;
  218. atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
  219. }
  220. /**
  221. * batadv_is_my_mac() - check if the given mac address belongs to any of the
  222. * real interfaces in the current mesh
  223. * @bat_priv: the bat priv with all the soft interface information
  224. * @addr: the address to check
  225. *
  226. * Return: 'true' if the mac address was found, false otherwise.
  227. */
  228. bool batadv_is_my_mac(struct batadv_priv *bat_priv, const u8 *addr)
  229. {
  230. const struct batadv_hard_iface *hard_iface;
  231. bool is_my_mac = false;
  232. rcu_read_lock();
  233. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  234. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  235. continue;
  236. if (hard_iface->soft_iface != bat_priv->soft_iface)
  237. continue;
  238. if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) {
  239. is_my_mac = true;
  240. break;
  241. }
  242. }
  243. rcu_read_unlock();
  244. return is_my_mac;
  245. }
  246. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  247. /**
  248. * batadv_seq_print_text_primary_if_get() - called from debugfs table printing
  249. * function that requires the primary interface
  250. * @seq: debugfs table seq_file struct
  251. *
  252. * Return: primary interface if found or NULL otherwise.
  253. */
  254. struct batadv_hard_iface *
  255. batadv_seq_print_text_primary_if_get(struct seq_file *seq)
  256. {
  257. struct net_device *net_dev = (struct net_device *)seq->private;
  258. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  259. struct batadv_hard_iface *primary_if;
  260. primary_if = batadv_primary_if_get_selected(bat_priv);
  261. if (!primary_if) {
  262. seq_printf(seq,
  263. "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
  264. net_dev->name);
  265. goto out;
  266. }
  267. if (primary_if->if_status == BATADV_IF_ACTIVE)
  268. goto out;
  269. seq_printf(seq,
  270. "BATMAN mesh %s disabled - primary interface not active\n",
  271. net_dev->name);
  272. batadv_hardif_put(primary_if);
  273. primary_if = NULL;
  274. out:
  275. return primary_if;
  276. }
  277. #endif
  278. /**
  279. * batadv_max_header_len() - calculate maximum encapsulation overhead for a
  280. * payload packet
  281. *
  282. * Return: the maximum encapsulation overhead in bytes.
  283. */
  284. int batadv_max_header_len(void)
  285. {
  286. int header_len = 0;
  287. header_len = max_t(int, header_len,
  288. sizeof(struct batadv_unicast_packet));
  289. header_len = max_t(int, header_len,
  290. sizeof(struct batadv_unicast_4addr_packet));
  291. header_len = max_t(int, header_len,
  292. sizeof(struct batadv_bcast_packet));
  293. #ifdef CONFIG_BATMAN_ADV_NC
  294. header_len = max_t(int, header_len,
  295. sizeof(struct batadv_coded_packet));
  296. #endif
  297. return header_len + ETH_HLEN;
  298. }
  299. /**
  300. * batadv_skb_set_priority() - sets skb priority according to packet content
  301. * @skb: the packet to be sent
  302. * @offset: offset to the packet content
  303. *
  304. * This function sets a value between 256 and 263 (802.1d priority), which
  305. * can be interpreted by the cfg80211 or other drivers.
  306. */
  307. void batadv_skb_set_priority(struct sk_buff *skb, int offset)
  308. {
  309. struct iphdr ip_hdr_tmp, *ip_hdr;
  310. struct ipv6hdr ip6_hdr_tmp, *ip6_hdr;
  311. struct ethhdr ethhdr_tmp, *ethhdr;
  312. struct vlan_ethhdr *vhdr, vhdr_tmp;
  313. u32 prio;
  314. /* already set, do nothing */
  315. if (skb->priority >= 256 && skb->priority <= 263)
  316. return;
  317. ethhdr = skb_header_pointer(skb, offset, sizeof(*ethhdr), &ethhdr_tmp);
  318. if (!ethhdr)
  319. return;
  320. switch (ethhdr->h_proto) {
  321. case htons(ETH_P_8021Q):
  322. vhdr = skb_header_pointer(skb, offset + sizeof(*vhdr),
  323. sizeof(*vhdr), &vhdr_tmp);
  324. if (!vhdr)
  325. return;
  326. prio = ntohs(vhdr->h_vlan_TCI) & VLAN_PRIO_MASK;
  327. prio = prio >> VLAN_PRIO_SHIFT;
  328. break;
  329. case htons(ETH_P_IP):
  330. ip_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
  331. sizeof(*ip_hdr), &ip_hdr_tmp);
  332. if (!ip_hdr)
  333. return;
  334. prio = (ipv4_get_dsfield(ip_hdr) & 0xfc) >> 5;
  335. break;
  336. case htons(ETH_P_IPV6):
  337. ip6_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
  338. sizeof(*ip6_hdr), &ip6_hdr_tmp);
  339. if (!ip6_hdr)
  340. return;
  341. prio = (ipv6_get_dsfield(ip6_hdr) & 0xfc) >> 5;
  342. break;
  343. default:
  344. return;
  345. }
  346. skb->priority = prio + 256;
  347. }
  348. static int batadv_recv_unhandled_packet(struct sk_buff *skb,
  349. struct batadv_hard_iface *recv_if)
  350. {
  351. kfree_skb(skb);
  352. return NET_RX_DROP;
  353. }
  354. /* incoming packets with the batman ethertype received on any active hard
  355. * interface
  356. */
  357. /**
  358. * batadv_batman_skb_recv() - Handle incoming message from an hard interface
  359. * @skb: the received packet
  360. * @dev: the net device that the packet was received on
  361. * @ptype: packet type of incoming packet (ETH_P_BATMAN)
  362. * @orig_dev: the original receive net device (e.g. bonded device)
  363. *
  364. * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
  365. */
  366. int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
  367. struct packet_type *ptype,
  368. struct net_device *orig_dev)
  369. {
  370. struct batadv_priv *bat_priv;
  371. struct batadv_ogm_packet *batadv_ogm_packet;
  372. struct batadv_hard_iface *hard_iface;
  373. u8 idx;
  374. hard_iface = container_of(ptype, struct batadv_hard_iface,
  375. batman_adv_ptype);
  376. /* Prevent processing a packet received on an interface which is getting
  377. * shut down otherwise the packet may trigger de-reference errors
  378. * further down in the receive path.
  379. */
  380. if (!kref_get_unless_zero(&hard_iface->refcount))
  381. goto err_out;
  382. skb = skb_share_check(skb, GFP_ATOMIC);
  383. /* skb was released by skb_share_check() */
  384. if (!skb)
  385. goto err_put;
  386. /* packet should hold at least type and version */
  387. if (unlikely(!pskb_may_pull(skb, 2)))
  388. goto err_free;
  389. /* expect a valid ethernet header here. */
  390. if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
  391. goto err_free;
  392. if (!hard_iface->soft_iface)
  393. goto err_free;
  394. bat_priv = netdev_priv(hard_iface->soft_iface);
  395. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  396. goto err_free;
  397. /* discard frames on not active interfaces */
  398. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  399. goto err_free;
  400. batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data;
  401. if (batadv_ogm_packet->version != BATADV_COMPAT_VERSION) {
  402. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  403. "Drop packet: incompatible batman version (%i)\n",
  404. batadv_ogm_packet->version);
  405. goto err_free;
  406. }
  407. /* reset control block to avoid left overs from previous users */
  408. memset(skb->cb, 0, sizeof(struct batadv_skb_cb));
  409. idx = batadv_ogm_packet->packet_type;
  410. (*batadv_rx_handler[idx])(skb, hard_iface);
  411. batadv_hardif_put(hard_iface);
  412. /* return NET_RX_SUCCESS in any case as we
  413. * most probably dropped the packet for
  414. * routing-logical reasons.
  415. */
  416. return NET_RX_SUCCESS;
  417. err_free:
  418. kfree_skb(skb);
  419. err_put:
  420. batadv_hardif_put(hard_iface);
  421. err_out:
  422. return NET_RX_DROP;
  423. }
  424. static void batadv_recv_handler_init(void)
  425. {
  426. int i;
  427. for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
  428. batadv_rx_handler[i] = batadv_recv_unhandled_packet;
  429. for (i = BATADV_UNICAST_MIN; i <= BATADV_UNICAST_MAX; i++)
  430. batadv_rx_handler[i] = batadv_recv_unhandled_unicast_packet;
  431. /* compile time checks for sizes */
  432. BUILD_BUG_ON(sizeof(struct batadv_bla_claim_dst) != 6);
  433. BUILD_BUG_ON(sizeof(struct batadv_ogm_packet) != 24);
  434. BUILD_BUG_ON(sizeof(struct batadv_icmp_header) != 20);
  435. BUILD_BUG_ON(sizeof(struct batadv_icmp_packet) != 20);
  436. BUILD_BUG_ON(sizeof(struct batadv_icmp_packet_rr) != 116);
  437. BUILD_BUG_ON(sizeof(struct batadv_unicast_packet) != 10);
  438. BUILD_BUG_ON(sizeof(struct batadv_unicast_4addr_packet) != 18);
  439. BUILD_BUG_ON(sizeof(struct batadv_frag_packet) != 20);
  440. BUILD_BUG_ON(sizeof(struct batadv_bcast_packet) != 14);
  441. BUILD_BUG_ON(sizeof(struct batadv_coded_packet) != 46);
  442. BUILD_BUG_ON(sizeof(struct batadv_unicast_tvlv_packet) != 20);
  443. BUILD_BUG_ON(sizeof(struct batadv_tvlv_hdr) != 4);
  444. BUILD_BUG_ON(sizeof(struct batadv_tvlv_gateway_data) != 8);
  445. BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_vlan_data) != 8);
  446. BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_change) != 12);
  447. BUILD_BUG_ON(sizeof(struct batadv_tvlv_roam_adv) != 8);
  448. i = FIELD_SIZEOF(struct sk_buff, cb);
  449. BUILD_BUG_ON(sizeof(struct batadv_skb_cb) > i);
  450. /* broadcast packet */
  451. batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet;
  452. /* unicast packets ... */
  453. /* unicast with 4 addresses packet */
  454. batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet;
  455. /* unicast packet */
  456. batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet;
  457. /* unicast tvlv packet */
  458. batadv_rx_handler[BATADV_UNICAST_TVLV] = batadv_recv_unicast_tvlv;
  459. /* batman icmp packet */
  460. batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet;
  461. /* Fragmented packets */
  462. batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_frag_packet;
  463. }
  464. /**
  465. * batadv_recv_handler_register() - Register handler for batman-adv packet type
  466. * @packet_type: batadv_packettype which should be handled
  467. * @recv_handler: receive handler for the packet type
  468. *
  469. * Return: 0 on success or negative error number in case of failure
  470. */
  471. int
  472. batadv_recv_handler_register(u8 packet_type,
  473. int (*recv_handler)(struct sk_buff *,
  474. struct batadv_hard_iface *))
  475. {
  476. int (*curr)(struct sk_buff *skb,
  477. struct batadv_hard_iface *recv_if);
  478. curr = batadv_rx_handler[packet_type];
  479. if (curr != batadv_recv_unhandled_packet &&
  480. curr != batadv_recv_unhandled_unicast_packet)
  481. return -EBUSY;
  482. batadv_rx_handler[packet_type] = recv_handler;
  483. return 0;
  484. }
  485. /**
  486. * batadv_recv_handler_unregister() - Unregister handler for packet type
  487. * @packet_type: batadv_packettype which should no longer be handled
  488. */
  489. void batadv_recv_handler_unregister(u8 packet_type)
  490. {
  491. batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet;
  492. }
  493. /**
  494. * batadv_skb_crc32() - calculate CRC32 of the whole packet and skip bytes in
  495. * the header
  496. * @skb: skb pointing to fragmented socket buffers
  497. * @payload_ptr: Pointer to position inside the head buffer of the skb
  498. * marking the start of the data to be CRC'ed
  499. *
  500. * payload_ptr must always point to an address in the skb head buffer and not to
  501. * a fragment.
  502. *
  503. * Return: big endian crc32c of the checksummed data
  504. */
  505. __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr)
  506. {
  507. u32 crc = 0;
  508. unsigned int from;
  509. unsigned int to = skb->len;
  510. struct skb_seq_state st;
  511. const u8 *data;
  512. unsigned int len;
  513. unsigned int consumed = 0;
  514. from = (unsigned int)(payload_ptr - skb->data);
  515. skb_prepare_seq_read(skb, from, to, &st);
  516. while ((len = skb_seq_read(consumed, &data, &st)) != 0) {
  517. crc = crc32c(crc, data, len);
  518. consumed += len;
  519. }
  520. return htonl(crc);
  521. }
  522. /**
  523. * batadv_get_vid() - extract the VLAN identifier from skb if any
  524. * @skb: the buffer containing the packet
  525. * @header_len: length of the batman header preceding the ethernet header
  526. *
  527. * Return: VID with the BATADV_VLAN_HAS_TAG flag when the packet embedded in the
  528. * skb is vlan tagged. Otherwise BATADV_NO_FLAGS.
  529. */
  530. unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len)
  531. {
  532. struct ethhdr *ethhdr = (struct ethhdr *)(skb->data + header_len);
  533. struct vlan_ethhdr *vhdr;
  534. unsigned short vid;
  535. if (ethhdr->h_proto != htons(ETH_P_8021Q))
  536. return BATADV_NO_FLAGS;
  537. if (!pskb_may_pull(skb, header_len + VLAN_ETH_HLEN))
  538. return BATADV_NO_FLAGS;
  539. vhdr = (struct vlan_ethhdr *)(skb->data + header_len);
  540. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  541. vid |= BATADV_VLAN_HAS_TAG;
  542. return vid;
  543. }
  544. /**
  545. * batadv_vlan_ap_isola_get() - return AP isolation status for the given vlan
  546. * @bat_priv: the bat priv with all the soft interface information
  547. * @vid: the VLAN identifier for which the AP isolation attributed as to be
  548. * looked up
  549. *
  550. * Return: true if AP isolation is on for the VLAN idenfied by vid, false
  551. * otherwise
  552. */
  553. bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid)
  554. {
  555. bool ap_isolation_enabled = false;
  556. struct batadv_softif_vlan *vlan;
  557. /* if the AP isolation is requested on a VLAN, then check for its
  558. * setting in the proper VLAN private data structure
  559. */
  560. vlan = batadv_softif_vlan_get(bat_priv, vid);
  561. if (vlan) {
  562. ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
  563. batadv_softif_vlan_put(vlan);
  564. }
  565. return ap_isolation_enabled;
  566. }
  567. module_init(batadv_init);
  568. module_exit(batadv_exit);
  569. MODULE_LICENSE("GPL");
  570. MODULE_AUTHOR(BATADV_DRIVER_AUTHOR);
  571. MODULE_DESCRIPTION(BATADV_DRIVER_DESC);
  572. MODULE_SUPPORTED_DEVICE(BATADV_DRIVER_DEVICE);
  573. MODULE_VERSION(BATADV_SOURCE_VERSION);
  574. MODULE_ALIAS_RTNL_LINK("batadv");
  575. MODULE_ALIAS_GENL_FAMILY(BATADV_NL_NAME);