main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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.mla_lock);
  139. spin_lock_init(&bat_priv->mcast.want_lists_lock);
  140. #endif
  141. spin_lock_init(&bat_priv->tvlv.container_list_lock);
  142. spin_lock_init(&bat_priv->tvlv.handler_list_lock);
  143. spin_lock_init(&bat_priv->softif_vlan_list_lock);
  144. spin_lock_init(&bat_priv->tp_list_lock);
  145. INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
  146. INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
  147. INIT_HLIST_HEAD(&bat_priv->gw.gateway_list);
  148. #ifdef CONFIG_BATMAN_ADV_MCAST
  149. INIT_HLIST_HEAD(&bat_priv->mcast.want_all_unsnoopables_list);
  150. INIT_HLIST_HEAD(&bat_priv->mcast.want_all_ipv4_list);
  151. INIT_HLIST_HEAD(&bat_priv->mcast.want_all_ipv6_list);
  152. #endif
  153. INIT_LIST_HEAD(&bat_priv->tt.changes_list);
  154. INIT_HLIST_HEAD(&bat_priv->tt.req_list);
  155. INIT_LIST_HEAD(&bat_priv->tt.roam_list);
  156. #ifdef CONFIG_BATMAN_ADV_MCAST
  157. INIT_HLIST_HEAD(&bat_priv->mcast.mla_list);
  158. #endif
  159. INIT_HLIST_HEAD(&bat_priv->tvlv.container_list);
  160. INIT_HLIST_HEAD(&bat_priv->tvlv.handler_list);
  161. INIT_HLIST_HEAD(&bat_priv->softif_vlan_list);
  162. INIT_HLIST_HEAD(&bat_priv->tp_list);
  163. ret = batadv_v_mesh_init(bat_priv);
  164. if (ret < 0)
  165. goto err;
  166. ret = batadv_originator_init(bat_priv);
  167. if (ret < 0)
  168. goto err;
  169. ret = batadv_tt_init(bat_priv);
  170. if (ret < 0)
  171. goto err;
  172. ret = batadv_bla_init(bat_priv);
  173. if (ret < 0)
  174. goto err;
  175. ret = batadv_dat_init(bat_priv);
  176. if (ret < 0)
  177. goto err;
  178. ret = batadv_nc_mesh_init(bat_priv);
  179. if (ret < 0)
  180. goto err;
  181. batadv_gw_init(bat_priv);
  182. batadv_mcast_init(bat_priv);
  183. atomic_set(&bat_priv->gw.reselect, 0);
  184. atomic_set(&bat_priv->mesh_state, BATADV_MESH_ACTIVE);
  185. return 0;
  186. err:
  187. batadv_mesh_free(soft_iface);
  188. return ret;
  189. }
  190. /**
  191. * batadv_mesh_free() - Deinitialize soft interface
  192. * @soft_iface: netdev struct of the soft interface
  193. */
  194. void batadv_mesh_free(struct net_device *soft_iface)
  195. {
  196. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  197. atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
  198. batadv_purge_outstanding_packets(bat_priv, NULL);
  199. batadv_gw_node_free(bat_priv);
  200. batadv_v_mesh_free(bat_priv);
  201. batadv_nc_mesh_free(bat_priv);
  202. batadv_dat_free(bat_priv);
  203. batadv_bla_free(bat_priv);
  204. batadv_mcast_free(bat_priv);
  205. /* Free the TT and the originator tables only after having terminated
  206. * all the other depending components which may use these structures for
  207. * their purposes.
  208. */
  209. batadv_tt_free(bat_priv);
  210. /* Since the originator table clean up routine is accessing the TT
  211. * tables as well, it has to be invoked after the TT tables have been
  212. * freed and marked as empty. This ensures that no cleanup RCU callbacks
  213. * accessing the TT data are scheduled for later execution.
  214. */
  215. batadv_originator_free(bat_priv);
  216. batadv_gw_free(bat_priv);
  217. free_percpu(bat_priv->bat_counters);
  218. bat_priv->bat_counters = NULL;
  219. atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
  220. }
  221. /**
  222. * batadv_is_my_mac() - check if the given mac address belongs to any of the
  223. * real interfaces in the current mesh
  224. * @bat_priv: the bat priv with all the soft interface information
  225. * @addr: the address to check
  226. *
  227. * Return: 'true' if the mac address was found, false otherwise.
  228. */
  229. bool batadv_is_my_mac(struct batadv_priv *bat_priv, const u8 *addr)
  230. {
  231. const struct batadv_hard_iface *hard_iface;
  232. bool is_my_mac = false;
  233. rcu_read_lock();
  234. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  235. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  236. continue;
  237. if (hard_iface->soft_iface != bat_priv->soft_iface)
  238. continue;
  239. if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) {
  240. is_my_mac = true;
  241. break;
  242. }
  243. }
  244. rcu_read_unlock();
  245. return is_my_mac;
  246. }
  247. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  248. /**
  249. * batadv_seq_print_text_primary_if_get() - called from debugfs table printing
  250. * function that requires the primary interface
  251. * @seq: debugfs table seq_file struct
  252. *
  253. * Return: primary interface if found or NULL otherwise.
  254. */
  255. struct batadv_hard_iface *
  256. batadv_seq_print_text_primary_if_get(struct seq_file *seq)
  257. {
  258. struct net_device *net_dev = (struct net_device *)seq->private;
  259. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  260. struct batadv_hard_iface *primary_if;
  261. primary_if = batadv_primary_if_get_selected(bat_priv);
  262. if (!primary_if) {
  263. seq_printf(seq,
  264. "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
  265. net_dev->name);
  266. goto out;
  267. }
  268. if (primary_if->if_status == BATADV_IF_ACTIVE)
  269. goto out;
  270. seq_printf(seq,
  271. "BATMAN mesh %s disabled - primary interface not active\n",
  272. net_dev->name);
  273. batadv_hardif_put(primary_if);
  274. primary_if = NULL;
  275. out:
  276. return primary_if;
  277. }
  278. #endif
  279. /**
  280. * batadv_max_header_len() - calculate maximum encapsulation overhead for a
  281. * payload packet
  282. *
  283. * Return: the maximum encapsulation overhead in bytes.
  284. */
  285. int batadv_max_header_len(void)
  286. {
  287. int header_len = 0;
  288. header_len = max_t(int, header_len,
  289. sizeof(struct batadv_unicast_packet));
  290. header_len = max_t(int, header_len,
  291. sizeof(struct batadv_unicast_4addr_packet));
  292. header_len = max_t(int, header_len,
  293. sizeof(struct batadv_bcast_packet));
  294. #ifdef CONFIG_BATMAN_ADV_NC
  295. header_len = max_t(int, header_len,
  296. sizeof(struct batadv_coded_packet));
  297. #endif
  298. return header_len + ETH_HLEN;
  299. }
  300. /**
  301. * batadv_skb_set_priority() - sets skb priority according to packet content
  302. * @skb: the packet to be sent
  303. * @offset: offset to the packet content
  304. *
  305. * This function sets a value between 256 and 263 (802.1d priority), which
  306. * can be interpreted by the cfg80211 or other drivers.
  307. */
  308. void batadv_skb_set_priority(struct sk_buff *skb, int offset)
  309. {
  310. struct iphdr ip_hdr_tmp, *ip_hdr;
  311. struct ipv6hdr ip6_hdr_tmp, *ip6_hdr;
  312. struct ethhdr ethhdr_tmp, *ethhdr;
  313. struct vlan_ethhdr *vhdr, vhdr_tmp;
  314. u32 prio;
  315. /* already set, do nothing */
  316. if (skb->priority >= 256 && skb->priority <= 263)
  317. return;
  318. ethhdr = skb_header_pointer(skb, offset, sizeof(*ethhdr), &ethhdr_tmp);
  319. if (!ethhdr)
  320. return;
  321. switch (ethhdr->h_proto) {
  322. case htons(ETH_P_8021Q):
  323. vhdr = skb_header_pointer(skb, offset + sizeof(*vhdr),
  324. sizeof(*vhdr), &vhdr_tmp);
  325. if (!vhdr)
  326. return;
  327. prio = ntohs(vhdr->h_vlan_TCI) & VLAN_PRIO_MASK;
  328. prio = prio >> VLAN_PRIO_SHIFT;
  329. break;
  330. case htons(ETH_P_IP):
  331. ip_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
  332. sizeof(*ip_hdr), &ip_hdr_tmp);
  333. if (!ip_hdr)
  334. return;
  335. prio = (ipv4_get_dsfield(ip_hdr) & 0xfc) >> 5;
  336. break;
  337. case htons(ETH_P_IPV6):
  338. ip6_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
  339. sizeof(*ip6_hdr), &ip6_hdr_tmp);
  340. if (!ip6_hdr)
  341. return;
  342. prio = (ipv6_get_dsfield(ip6_hdr) & 0xfc) >> 5;
  343. break;
  344. default:
  345. return;
  346. }
  347. skb->priority = prio + 256;
  348. }
  349. static int batadv_recv_unhandled_packet(struct sk_buff *skb,
  350. struct batadv_hard_iface *recv_if)
  351. {
  352. kfree_skb(skb);
  353. return NET_RX_DROP;
  354. }
  355. /* incoming packets with the batman ethertype received on any active hard
  356. * interface
  357. */
  358. /**
  359. * batadv_batman_skb_recv() - Handle incoming message from an hard interface
  360. * @skb: the received packet
  361. * @dev: the net device that the packet was received on
  362. * @ptype: packet type of incoming packet (ETH_P_BATMAN)
  363. * @orig_dev: the original receive net device (e.g. bonded device)
  364. *
  365. * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
  366. */
  367. int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
  368. struct packet_type *ptype,
  369. struct net_device *orig_dev)
  370. {
  371. struct batadv_priv *bat_priv;
  372. struct batadv_ogm_packet *batadv_ogm_packet;
  373. struct batadv_hard_iface *hard_iface;
  374. u8 idx;
  375. hard_iface = container_of(ptype, struct batadv_hard_iface,
  376. batman_adv_ptype);
  377. /* Prevent processing a packet received on an interface which is getting
  378. * shut down otherwise the packet may trigger de-reference errors
  379. * further down in the receive path.
  380. */
  381. if (!kref_get_unless_zero(&hard_iface->refcount))
  382. goto err_out;
  383. skb = skb_share_check(skb, GFP_ATOMIC);
  384. /* skb was released by skb_share_check() */
  385. if (!skb)
  386. goto err_put;
  387. /* packet should hold at least type and version */
  388. if (unlikely(!pskb_may_pull(skb, 2)))
  389. goto err_free;
  390. /* expect a valid ethernet header here. */
  391. if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
  392. goto err_free;
  393. if (!hard_iface->soft_iface)
  394. goto err_free;
  395. bat_priv = netdev_priv(hard_iface->soft_iface);
  396. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  397. goto err_free;
  398. /* discard frames on not active interfaces */
  399. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  400. goto err_free;
  401. batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data;
  402. if (batadv_ogm_packet->version != BATADV_COMPAT_VERSION) {
  403. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  404. "Drop packet: incompatible batman version (%i)\n",
  405. batadv_ogm_packet->version);
  406. goto err_free;
  407. }
  408. /* reset control block to avoid left overs from previous users */
  409. memset(skb->cb, 0, sizeof(struct batadv_skb_cb));
  410. idx = batadv_ogm_packet->packet_type;
  411. (*batadv_rx_handler[idx])(skb, hard_iface);
  412. batadv_hardif_put(hard_iface);
  413. /* return NET_RX_SUCCESS in any case as we
  414. * most probably dropped the packet for
  415. * routing-logical reasons.
  416. */
  417. return NET_RX_SUCCESS;
  418. err_free:
  419. kfree_skb(skb);
  420. err_put:
  421. batadv_hardif_put(hard_iface);
  422. err_out:
  423. return NET_RX_DROP;
  424. }
  425. static void batadv_recv_handler_init(void)
  426. {
  427. int i;
  428. for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
  429. batadv_rx_handler[i] = batadv_recv_unhandled_packet;
  430. for (i = BATADV_UNICAST_MIN; i <= BATADV_UNICAST_MAX; i++)
  431. batadv_rx_handler[i] = batadv_recv_unhandled_unicast_packet;
  432. /* compile time checks for sizes */
  433. BUILD_BUG_ON(sizeof(struct batadv_bla_claim_dst) != 6);
  434. BUILD_BUG_ON(sizeof(struct batadv_ogm_packet) != 24);
  435. BUILD_BUG_ON(sizeof(struct batadv_icmp_header) != 20);
  436. BUILD_BUG_ON(sizeof(struct batadv_icmp_packet) != 20);
  437. BUILD_BUG_ON(sizeof(struct batadv_icmp_packet_rr) != 116);
  438. BUILD_BUG_ON(sizeof(struct batadv_unicast_packet) != 10);
  439. BUILD_BUG_ON(sizeof(struct batadv_unicast_4addr_packet) != 18);
  440. BUILD_BUG_ON(sizeof(struct batadv_frag_packet) != 20);
  441. BUILD_BUG_ON(sizeof(struct batadv_bcast_packet) != 14);
  442. BUILD_BUG_ON(sizeof(struct batadv_coded_packet) != 46);
  443. BUILD_BUG_ON(sizeof(struct batadv_unicast_tvlv_packet) != 20);
  444. BUILD_BUG_ON(sizeof(struct batadv_tvlv_hdr) != 4);
  445. BUILD_BUG_ON(sizeof(struct batadv_tvlv_gateway_data) != 8);
  446. BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_vlan_data) != 8);
  447. BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_change) != 12);
  448. BUILD_BUG_ON(sizeof(struct batadv_tvlv_roam_adv) != 8);
  449. i = FIELD_SIZEOF(struct sk_buff, cb);
  450. BUILD_BUG_ON(sizeof(struct batadv_skb_cb) > i);
  451. /* broadcast packet */
  452. batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet;
  453. /* unicast packets ... */
  454. /* unicast with 4 addresses packet */
  455. batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet;
  456. /* unicast packet */
  457. batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet;
  458. /* unicast tvlv packet */
  459. batadv_rx_handler[BATADV_UNICAST_TVLV] = batadv_recv_unicast_tvlv;
  460. /* batman icmp packet */
  461. batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet;
  462. /* Fragmented packets */
  463. batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_frag_packet;
  464. }
  465. /**
  466. * batadv_recv_handler_register() - Register handler for batman-adv packet type
  467. * @packet_type: batadv_packettype which should be handled
  468. * @recv_handler: receive handler for the packet type
  469. *
  470. * Return: 0 on success or negative error number in case of failure
  471. */
  472. int
  473. batadv_recv_handler_register(u8 packet_type,
  474. int (*recv_handler)(struct sk_buff *,
  475. struct batadv_hard_iface *))
  476. {
  477. int (*curr)(struct sk_buff *skb,
  478. struct batadv_hard_iface *recv_if);
  479. curr = batadv_rx_handler[packet_type];
  480. if (curr != batadv_recv_unhandled_packet &&
  481. curr != batadv_recv_unhandled_unicast_packet)
  482. return -EBUSY;
  483. batadv_rx_handler[packet_type] = recv_handler;
  484. return 0;
  485. }
  486. /**
  487. * batadv_recv_handler_unregister() - Unregister handler for packet type
  488. * @packet_type: batadv_packettype which should no longer be handled
  489. */
  490. void batadv_recv_handler_unregister(u8 packet_type)
  491. {
  492. batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet;
  493. }
  494. /**
  495. * batadv_skb_crc32() - calculate CRC32 of the whole packet and skip bytes in
  496. * the header
  497. * @skb: skb pointing to fragmented socket buffers
  498. * @payload_ptr: Pointer to position inside the head buffer of the skb
  499. * marking the start of the data to be CRC'ed
  500. *
  501. * payload_ptr must always point to an address in the skb head buffer and not to
  502. * a fragment.
  503. *
  504. * Return: big endian crc32c of the checksummed data
  505. */
  506. __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr)
  507. {
  508. u32 crc = 0;
  509. unsigned int from;
  510. unsigned int to = skb->len;
  511. struct skb_seq_state st;
  512. const u8 *data;
  513. unsigned int len;
  514. unsigned int consumed = 0;
  515. from = (unsigned int)(payload_ptr - skb->data);
  516. skb_prepare_seq_read(skb, from, to, &st);
  517. while ((len = skb_seq_read(consumed, &data, &st)) != 0) {
  518. crc = crc32c(crc, data, len);
  519. consumed += len;
  520. }
  521. return htonl(crc);
  522. }
  523. /**
  524. * batadv_get_vid() - extract the VLAN identifier from skb if any
  525. * @skb: the buffer containing the packet
  526. * @header_len: length of the batman header preceding the ethernet header
  527. *
  528. * Return: VID with the BATADV_VLAN_HAS_TAG flag when the packet embedded in the
  529. * skb is vlan tagged. Otherwise BATADV_NO_FLAGS.
  530. */
  531. unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len)
  532. {
  533. struct ethhdr *ethhdr = (struct ethhdr *)(skb->data + header_len);
  534. struct vlan_ethhdr *vhdr;
  535. unsigned short vid;
  536. if (ethhdr->h_proto != htons(ETH_P_8021Q))
  537. return BATADV_NO_FLAGS;
  538. if (!pskb_may_pull(skb, header_len + VLAN_ETH_HLEN))
  539. return BATADV_NO_FLAGS;
  540. vhdr = (struct vlan_ethhdr *)(skb->data + header_len);
  541. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  542. vid |= BATADV_VLAN_HAS_TAG;
  543. return vid;
  544. }
  545. /**
  546. * batadv_vlan_ap_isola_get() - return AP isolation status for the given vlan
  547. * @bat_priv: the bat priv with all the soft interface information
  548. * @vid: the VLAN identifier for which the AP isolation attributed as to be
  549. * looked up
  550. *
  551. * Return: true if AP isolation is on for the VLAN idenfied by vid, false
  552. * otherwise
  553. */
  554. bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid)
  555. {
  556. bool ap_isolation_enabled = false;
  557. struct batadv_softif_vlan *vlan;
  558. /* if the AP isolation is requested on a VLAN, then check for its
  559. * setting in the proper VLAN private data structure
  560. */
  561. vlan = batadv_softif_vlan_get(bat_priv, vid);
  562. if (vlan) {
  563. ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
  564. batadv_softif_vlan_put(vlan);
  565. }
  566. return ap_isolation_enabled;
  567. }
  568. module_init(batadv_init);
  569. module_exit(batadv_exit);
  570. MODULE_LICENSE("GPL");
  571. MODULE_AUTHOR(BATADV_DRIVER_AUTHOR);
  572. MODULE_DESCRIPTION(BATADV_DRIVER_DESC);
  573. MODULE_SUPPORTED_DEVICE(BATADV_DRIVER_DEVICE);
  574. MODULE_VERSION(BATADV_SOURCE_VERSION);
  575. MODULE_ALIAS_RTNL_LINK("batadv");
  576. MODULE_ALIAS_GENL_FAMILY(BATADV_NL_NAME);