main.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. /* Copyright (C) 2007-2015 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "main.h"
  18. #include <linux/atomic.h>
  19. #include <linux/bug.h>
  20. #include <linux/byteorder/generic.h>
  21. #include <linux/crc32c.h>
  22. #include <linux/errno.h>
  23. #include <linux/fs.h>
  24. #include <linux/if_ether.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/init.h>
  27. #include <linux/ip.h>
  28. #include <linux/ipv6.h>
  29. #include <linux/kernel.h>
  30. #include <linux/list.h>
  31. #include <linux/lockdep.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/pkt_sched.h>
  36. #include <linux/rculist.h>
  37. #include <linux/rcupdate.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/slab.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 "bat_algo.h"
  48. #include "bridge_loop_avoidance.h"
  49. #include "debugfs.h"
  50. #include "distributed-arp-table.h"
  51. #include "gateway_client.h"
  52. #include "gateway_common.h"
  53. #include "hard-interface.h"
  54. #include "icmp_socket.h"
  55. #include "multicast.h"
  56. #include "network-coding.h"
  57. #include "originator.h"
  58. #include "packet.h"
  59. #include "routing.h"
  60. #include "send.h"
  61. #include "soft-interface.h"
  62. #include "translation-table.h"
  63. /* List manipulations on hardif_list have to be rtnl_lock()'ed,
  64. * list traversals just rcu-locked
  65. */
  66. struct list_head batadv_hardif_list;
  67. static int (*batadv_rx_handler[256])(struct sk_buff *,
  68. struct batadv_hard_iface *);
  69. char batadv_routing_algo[20] = "BATMAN_IV";
  70. static struct hlist_head batadv_algo_list;
  71. unsigned char batadv_broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  72. struct workqueue_struct *batadv_event_workqueue;
  73. static void batadv_recv_handler_init(void);
  74. static int __init batadv_init(void)
  75. {
  76. INIT_LIST_HEAD(&batadv_hardif_list);
  77. INIT_HLIST_HEAD(&batadv_algo_list);
  78. batadv_recv_handler_init();
  79. batadv_iv_init();
  80. batadv_nc_init();
  81. batadv_event_workqueue = create_singlethread_workqueue("bat_events");
  82. if (!batadv_event_workqueue)
  83. return -ENOMEM;
  84. batadv_socket_init();
  85. batadv_debugfs_init();
  86. register_netdevice_notifier(&batadv_hard_if_notifier);
  87. rtnl_link_register(&batadv_link_ops);
  88. pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n",
  89. BATADV_SOURCE_VERSION, BATADV_COMPAT_VERSION);
  90. return 0;
  91. }
  92. static void __exit batadv_exit(void)
  93. {
  94. batadv_debugfs_destroy();
  95. rtnl_link_unregister(&batadv_link_ops);
  96. unregister_netdevice_notifier(&batadv_hard_if_notifier);
  97. batadv_hardif_remove_interfaces();
  98. flush_workqueue(batadv_event_workqueue);
  99. destroy_workqueue(batadv_event_workqueue);
  100. batadv_event_workqueue = NULL;
  101. rcu_barrier();
  102. }
  103. int batadv_mesh_init(struct net_device *soft_iface)
  104. {
  105. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  106. int ret;
  107. spin_lock_init(&bat_priv->forw_bat_list_lock);
  108. spin_lock_init(&bat_priv->forw_bcast_list_lock);
  109. spin_lock_init(&bat_priv->tt.changes_list_lock);
  110. spin_lock_init(&bat_priv->tt.req_list_lock);
  111. spin_lock_init(&bat_priv->tt.roam_list_lock);
  112. spin_lock_init(&bat_priv->tt.last_changeset_lock);
  113. spin_lock_init(&bat_priv->tt.commit_lock);
  114. spin_lock_init(&bat_priv->gw.list_lock);
  115. #ifdef CONFIG_BATMAN_ADV_MCAST
  116. spin_lock_init(&bat_priv->mcast.want_lists_lock);
  117. #endif
  118. spin_lock_init(&bat_priv->tvlv.container_list_lock);
  119. spin_lock_init(&bat_priv->tvlv.handler_list_lock);
  120. spin_lock_init(&bat_priv->softif_vlan_list_lock);
  121. INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
  122. INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
  123. INIT_HLIST_HEAD(&bat_priv->gw.list);
  124. #ifdef CONFIG_BATMAN_ADV_MCAST
  125. INIT_HLIST_HEAD(&bat_priv->mcast.want_all_unsnoopables_list);
  126. INIT_HLIST_HEAD(&bat_priv->mcast.want_all_ipv4_list);
  127. INIT_HLIST_HEAD(&bat_priv->mcast.want_all_ipv6_list);
  128. #endif
  129. INIT_LIST_HEAD(&bat_priv->tt.changes_list);
  130. INIT_HLIST_HEAD(&bat_priv->tt.req_list);
  131. INIT_LIST_HEAD(&bat_priv->tt.roam_list);
  132. #ifdef CONFIG_BATMAN_ADV_MCAST
  133. INIT_HLIST_HEAD(&bat_priv->mcast.mla_list);
  134. #endif
  135. INIT_HLIST_HEAD(&bat_priv->tvlv.container_list);
  136. INIT_HLIST_HEAD(&bat_priv->tvlv.handler_list);
  137. INIT_HLIST_HEAD(&bat_priv->softif_vlan_list);
  138. ret = batadv_originator_init(bat_priv);
  139. if (ret < 0)
  140. goto err;
  141. ret = batadv_tt_init(bat_priv);
  142. if (ret < 0)
  143. goto err;
  144. ret = batadv_bla_init(bat_priv);
  145. if (ret < 0)
  146. goto err;
  147. ret = batadv_dat_init(bat_priv);
  148. if (ret < 0)
  149. goto err;
  150. ret = batadv_nc_mesh_init(bat_priv);
  151. if (ret < 0)
  152. goto err;
  153. batadv_gw_init(bat_priv);
  154. batadv_mcast_init(bat_priv);
  155. atomic_set(&bat_priv->gw.reselect, 0);
  156. atomic_set(&bat_priv->mesh_state, BATADV_MESH_ACTIVE);
  157. return 0;
  158. err:
  159. batadv_mesh_free(soft_iface);
  160. return ret;
  161. }
  162. void batadv_mesh_free(struct net_device *soft_iface)
  163. {
  164. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  165. atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
  166. batadv_purge_outstanding_packets(bat_priv, NULL);
  167. batadv_gw_node_free(bat_priv);
  168. batadv_nc_mesh_free(bat_priv);
  169. batadv_dat_free(bat_priv);
  170. batadv_bla_free(bat_priv);
  171. batadv_mcast_free(bat_priv);
  172. /* Free the TT and the originator tables only after having terminated
  173. * all the other depending components which may use these structures for
  174. * their purposes.
  175. */
  176. batadv_tt_free(bat_priv);
  177. /* Since the originator table clean up routine is accessing the TT
  178. * tables as well, it has to be invoked after the TT tables have been
  179. * freed and marked as empty. This ensures that no cleanup RCU callbacks
  180. * accessing the TT data are scheduled for later execution.
  181. */
  182. batadv_originator_free(bat_priv);
  183. batadv_gw_free(bat_priv);
  184. free_percpu(bat_priv->bat_counters);
  185. bat_priv->bat_counters = NULL;
  186. atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
  187. }
  188. /**
  189. * batadv_is_my_mac - check if the given mac address belongs to any of the real
  190. * interfaces in the current mesh
  191. * @bat_priv: the bat priv with all the soft interface information
  192. * @addr: the address to check
  193. *
  194. * Returns 'true' if the mac address was found, false otherwise.
  195. */
  196. bool batadv_is_my_mac(struct batadv_priv *bat_priv, const u8 *addr)
  197. {
  198. const struct batadv_hard_iface *hard_iface;
  199. bool is_my_mac = false;
  200. rcu_read_lock();
  201. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  202. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  203. continue;
  204. if (hard_iface->soft_iface != bat_priv->soft_iface)
  205. continue;
  206. if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) {
  207. is_my_mac = true;
  208. break;
  209. }
  210. }
  211. rcu_read_unlock();
  212. return is_my_mac;
  213. }
  214. /**
  215. * batadv_seq_print_text_primary_if_get - called from debugfs table printing
  216. * function that requires the primary interface
  217. * @seq: debugfs table seq_file struct
  218. *
  219. * Returns primary interface if found or NULL otherwise.
  220. */
  221. struct batadv_hard_iface *
  222. batadv_seq_print_text_primary_if_get(struct seq_file *seq)
  223. {
  224. struct net_device *net_dev = (struct net_device *)seq->private;
  225. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  226. struct batadv_hard_iface *primary_if;
  227. primary_if = batadv_primary_if_get_selected(bat_priv);
  228. if (!primary_if) {
  229. seq_printf(seq,
  230. "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
  231. net_dev->name);
  232. goto out;
  233. }
  234. if (primary_if->if_status == BATADV_IF_ACTIVE)
  235. goto out;
  236. seq_printf(seq,
  237. "BATMAN mesh %s disabled - primary interface not active\n",
  238. net_dev->name);
  239. batadv_hardif_free_ref(primary_if);
  240. primary_if = NULL;
  241. out:
  242. return primary_if;
  243. }
  244. /**
  245. * batadv_max_header_len - calculate maximum encapsulation overhead for a
  246. * payload packet
  247. *
  248. * Return the maximum encapsulation overhead in bytes.
  249. */
  250. int batadv_max_header_len(void)
  251. {
  252. int header_len = 0;
  253. header_len = max_t(int, header_len,
  254. sizeof(struct batadv_unicast_packet));
  255. header_len = max_t(int, header_len,
  256. sizeof(struct batadv_unicast_4addr_packet));
  257. header_len = max_t(int, header_len,
  258. sizeof(struct batadv_bcast_packet));
  259. #ifdef CONFIG_BATMAN_ADV_NC
  260. header_len = max_t(int, header_len,
  261. sizeof(struct batadv_coded_packet));
  262. #endif
  263. return header_len + ETH_HLEN;
  264. }
  265. /**
  266. * batadv_skb_set_priority - sets skb priority according to packet content
  267. * @skb: the packet to be sent
  268. * @offset: offset to the packet content
  269. *
  270. * This function sets a value between 256 and 263 (802.1d priority), which
  271. * can be interpreted by the cfg80211 or other drivers.
  272. */
  273. void batadv_skb_set_priority(struct sk_buff *skb, int offset)
  274. {
  275. struct iphdr ip_hdr_tmp, *ip_hdr;
  276. struct ipv6hdr ip6_hdr_tmp, *ip6_hdr;
  277. struct ethhdr ethhdr_tmp, *ethhdr;
  278. struct vlan_ethhdr *vhdr, vhdr_tmp;
  279. u32 prio;
  280. /* already set, do nothing */
  281. if (skb->priority >= 256 && skb->priority <= 263)
  282. return;
  283. ethhdr = skb_header_pointer(skb, offset, sizeof(*ethhdr), &ethhdr_tmp);
  284. if (!ethhdr)
  285. return;
  286. switch (ethhdr->h_proto) {
  287. case htons(ETH_P_8021Q):
  288. vhdr = skb_header_pointer(skb, offset + sizeof(*vhdr),
  289. sizeof(*vhdr), &vhdr_tmp);
  290. if (!vhdr)
  291. return;
  292. prio = ntohs(vhdr->h_vlan_TCI) & VLAN_PRIO_MASK;
  293. prio = prio >> VLAN_PRIO_SHIFT;
  294. break;
  295. case htons(ETH_P_IP):
  296. ip_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
  297. sizeof(*ip_hdr), &ip_hdr_tmp);
  298. if (!ip_hdr)
  299. return;
  300. prio = (ipv4_get_dsfield(ip_hdr) & 0xfc) >> 5;
  301. break;
  302. case htons(ETH_P_IPV6):
  303. ip6_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
  304. sizeof(*ip6_hdr), &ip6_hdr_tmp);
  305. if (!ip6_hdr)
  306. return;
  307. prio = (ipv6_get_dsfield(ip6_hdr) & 0xfc) >> 5;
  308. break;
  309. default:
  310. return;
  311. }
  312. skb->priority = prio + 256;
  313. }
  314. static int batadv_recv_unhandled_packet(struct sk_buff *skb,
  315. struct batadv_hard_iface *recv_if)
  316. {
  317. return NET_RX_DROP;
  318. }
  319. /* incoming packets with the batman ethertype received on any active hard
  320. * interface
  321. */
  322. int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
  323. struct packet_type *ptype,
  324. struct net_device *orig_dev)
  325. {
  326. struct batadv_priv *bat_priv;
  327. struct batadv_ogm_packet *batadv_ogm_packet;
  328. struct batadv_hard_iface *hard_iface;
  329. u8 idx;
  330. int ret;
  331. hard_iface = container_of(ptype, struct batadv_hard_iface,
  332. batman_adv_ptype);
  333. skb = skb_share_check(skb, GFP_ATOMIC);
  334. /* skb was released by skb_share_check() */
  335. if (!skb)
  336. goto err_out;
  337. /* packet should hold at least type and version */
  338. if (unlikely(!pskb_may_pull(skb, 2)))
  339. goto err_free;
  340. /* expect a valid ethernet header here. */
  341. if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
  342. goto err_free;
  343. if (!hard_iface->soft_iface)
  344. goto err_free;
  345. bat_priv = netdev_priv(hard_iface->soft_iface);
  346. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  347. goto err_free;
  348. /* discard frames on not active interfaces */
  349. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  350. goto err_free;
  351. batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data;
  352. if (batadv_ogm_packet->version != BATADV_COMPAT_VERSION) {
  353. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  354. "Drop packet: incompatible batman version (%i)\n",
  355. batadv_ogm_packet->version);
  356. goto err_free;
  357. }
  358. /* reset control block to avoid left overs from previous users */
  359. memset(skb->cb, 0, sizeof(struct batadv_skb_cb));
  360. /* all receive handlers return whether they received or reused
  361. * the supplied skb. if not, we have to free the skb.
  362. */
  363. idx = batadv_ogm_packet->packet_type;
  364. ret = (*batadv_rx_handler[idx])(skb, hard_iface);
  365. if (ret == NET_RX_DROP)
  366. kfree_skb(skb);
  367. /* return NET_RX_SUCCESS in any case as we
  368. * most probably dropped the packet for
  369. * routing-logical reasons.
  370. */
  371. return NET_RX_SUCCESS;
  372. err_free:
  373. kfree_skb(skb);
  374. err_out:
  375. return NET_RX_DROP;
  376. }
  377. static void batadv_recv_handler_init(void)
  378. {
  379. int i;
  380. for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
  381. batadv_rx_handler[i] = batadv_recv_unhandled_packet;
  382. for (i = BATADV_UNICAST_MIN; i <= BATADV_UNICAST_MAX; i++)
  383. batadv_rx_handler[i] = batadv_recv_unhandled_unicast_packet;
  384. /* compile time checks for sizes */
  385. BUILD_BUG_ON(sizeof(struct batadv_bla_claim_dst) != 6);
  386. BUILD_BUG_ON(sizeof(struct batadv_ogm_packet) != 24);
  387. BUILD_BUG_ON(sizeof(struct batadv_icmp_header) != 20);
  388. BUILD_BUG_ON(sizeof(struct batadv_icmp_packet) != 20);
  389. BUILD_BUG_ON(sizeof(struct batadv_icmp_packet_rr) != 116);
  390. BUILD_BUG_ON(sizeof(struct batadv_unicast_packet) != 10);
  391. BUILD_BUG_ON(sizeof(struct batadv_unicast_4addr_packet) != 18);
  392. BUILD_BUG_ON(sizeof(struct batadv_frag_packet) != 20);
  393. BUILD_BUG_ON(sizeof(struct batadv_bcast_packet) != 14);
  394. BUILD_BUG_ON(sizeof(struct batadv_coded_packet) != 46);
  395. BUILD_BUG_ON(sizeof(struct batadv_unicast_tvlv_packet) != 20);
  396. BUILD_BUG_ON(sizeof(struct batadv_tvlv_hdr) != 4);
  397. BUILD_BUG_ON(sizeof(struct batadv_tvlv_gateway_data) != 8);
  398. BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_vlan_data) != 8);
  399. BUILD_BUG_ON(sizeof(struct batadv_tvlv_tt_change) != 12);
  400. BUILD_BUG_ON(sizeof(struct batadv_tvlv_roam_adv) != 8);
  401. /* broadcast packet */
  402. batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet;
  403. /* unicast packets ... */
  404. /* unicast with 4 addresses packet */
  405. batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet;
  406. /* unicast packet */
  407. batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet;
  408. /* unicast tvlv packet */
  409. batadv_rx_handler[BATADV_UNICAST_TVLV] = batadv_recv_unicast_tvlv;
  410. /* batman icmp packet */
  411. batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet;
  412. /* Fragmented packets */
  413. batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_frag_packet;
  414. }
  415. int
  416. batadv_recv_handler_register(u8 packet_type,
  417. int (*recv_handler)(struct sk_buff *,
  418. struct batadv_hard_iface *))
  419. {
  420. int (*curr)(struct sk_buff *,
  421. struct batadv_hard_iface *);
  422. curr = batadv_rx_handler[packet_type];
  423. if ((curr != batadv_recv_unhandled_packet) &&
  424. (curr != batadv_recv_unhandled_unicast_packet))
  425. return -EBUSY;
  426. batadv_rx_handler[packet_type] = recv_handler;
  427. return 0;
  428. }
  429. void batadv_recv_handler_unregister(u8 packet_type)
  430. {
  431. batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet;
  432. }
  433. static struct batadv_algo_ops *batadv_algo_get(char *name)
  434. {
  435. struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
  436. hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
  437. if (strcmp(bat_algo_ops_tmp->name, name) != 0)
  438. continue;
  439. bat_algo_ops = bat_algo_ops_tmp;
  440. break;
  441. }
  442. return bat_algo_ops;
  443. }
  444. int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
  445. {
  446. struct batadv_algo_ops *bat_algo_ops_tmp;
  447. bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
  448. if (bat_algo_ops_tmp) {
  449. pr_info("Trying to register already registered routing algorithm: %s\n",
  450. bat_algo_ops->name);
  451. return -EEXIST;
  452. }
  453. /* all algorithms must implement all ops (for now) */
  454. if (!bat_algo_ops->bat_iface_enable ||
  455. !bat_algo_ops->bat_iface_disable ||
  456. !bat_algo_ops->bat_iface_update_mac ||
  457. !bat_algo_ops->bat_primary_iface_set ||
  458. !bat_algo_ops->bat_ogm_schedule ||
  459. !bat_algo_ops->bat_ogm_emit ||
  460. !bat_algo_ops->bat_neigh_cmp ||
  461. !bat_algo_ops->bat_neigh_is_equiv_or_better) {
  462. pr_info("Routing algo '%s' does not implement required ops\n",
  463. bat_algo_ops->name);
  464. return -EINVAL;
  465. }
  466. INIT_HLIST_NODE(&bat_algo_ops->list);
  467. hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
  468. return 0;
  469. }
  470. int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
  471. {
  472. struct batadv_algo_ops *bat_algo_ops;
  473. bat_algo_ops = batadv_algo_get(name);
  474. if (!bat_algo_ops)
  475. return -EINVAL;
  476. bat_priv->bat_algo_ops = bat_algo_ops;
  477. return 0;
  478. }
  479. int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
  480. {
  481. struct batadv_algo_ops *bat_algo_ops;
  482. seq_puts(seq, "Available routing algorithms:\n");
  483. hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
  484. seq_printf(seq, " * %s\n", bat_algo_ops->name);
  485. }
  486. return 0;
  487. }
  488. /**
  489. * batadv_skb_crc32 - calculate CRC32 of the whole packet and skip bytes in
  490. * the header
  491. * @skb: skb pointing to fragmented socket buffers
  492. * @payload_ptr: Pointer to position inside the head buffer of the skb
  493. * marking the start of the data to be CRC'ed
  494. *
  495. * payload_ptr must always point to an address in the skb head buffer and not to
  496. * a fragment.
  497. */
  498. __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr)
  499. {
  500. u32 crc = 0;
  501. unsigned int from;
  502. unsigned int to = skb->len;
  503. struct skb_seq_state st;
  504. const u8 *data;
  505. unsigned int len;
  506. unsigned int consumed = 0;
  507. from = (unsigned int)(payload_ptr - skb->data);
  508. skb_prepare_seq_read(skb, from, to, &st);
  509. while ((len = skb_seq_read(consumed, &data, &st)) != 0) {
  510. crc = crc32c(crc, data, len);
  511. consumed += len;
  512. }
  513. return htonl(crc);
  514. }
  515. /**
  516. * batadv_tvlv_handler_free_ref - decrement the tvlv handler refcounter and
  517. * possibly free it
  518. * @tvlv_handler: the tvlv handler to free
  519. */
  520. static void
  521. batadv_tvlv_handler_free_ref(struct batadv_tvlv_handler *tvlv_handler)
  522. {
  523. if (atomic_dec_and_test(&tvlv_handler->refcount))
  524. kfree_rcu(tvlv_handler, rcu);
  525. }
  526. /**
  527. * batadv_tvlv_handler_get - retrieve tvlv handler from the tvlv handler list
  528. * based on the provided type and version (both need to match)
  529. * @bat_priv: the bat priv with all the soft interface information
  530. * @type: tvlv handler type to look for
  531. * @version: tvlv handler version to look for
  532. *
  533. * Returns tvlv handler if found or NULL otherwise.
  534. */
  535. static struct batadv_tvlv_handler
  536. *batadv_tvlv_handler_get(struct batadv_priv *bat_priv, u8 type, u8 version)
  537. {
  538. struct batadv_tvlv_handler *tvlv_handler_tmp, *tvlv_handler = NULL;
  539. rcu_read_lock();
  540. hlist_for_each_entry_rcu(tvlv_handler_tmp,
  541. &bat_priv->tvlv.handler_list, list) {
  542. if (tvlv_handler_tmp->type != type)
  543. continue;
  544. if (tvlv_handler_tmp->version != version)
  545. continue;
  546. if (!atomic_inc_not_zero(&tvlv_handler_tmp->refcount))
  547. continue;
  548. tvlv_handler = tvlv_handler_tmp;
  549. break;
  550. }
  551. rcu_read_unlock();
  552. return tvlv_handler;
  553. }
  554. /**
  555. * batadv_tvlv_container_free_ref - decrement the tvlv container refcounter and
  556. * possibly free it
  557. * @tvlv: the tvlv container to free
  558. */
  559. static void batadv_tvlv_container_free_ref(struct batadv_tvlv_container *tvlv)
  560. {
  561. if (atomic_dec_and_test(&tvlv->refcount))
  562. kfree(tvlv);
  563. }
  564. /**
  565. * batadv_tvlv_container_get - retrieve tvlv container from the tvlv container
  566. * list based on the provided type and version (both need to match)
  567. * @bat_priv: the bat priv with all the soft interface information
  568. * @type: tvlv container type to look for
  569. * @version: tvlv container version to look for
  570. *
  571. * Has to be called with the appropriate locks being acquired
  572. * (tvlv.container_list_lock).
  573. *
  574. * Returns tvlv container if found or NULL otherwise.
  575. */
  576. static struct batadv_tvlv_container
  577. *batadv_tvlv_container_get(struct batadv_priv *bat_priv, u8 type, u8 version)
  578. {
  579. struct batadv_tvlv_container *tvlv_tmp, *tvlv = NULL;
  580. hlist_for_each_entry(tvlv_tmp, &bat_priv->tvlv.container_list, list) {
  581. if (tvlv_tmp->tvlv_hdr.type != type)
  582. continue;
  583. if (tvlv_tmp->tvlv_hdr.version != version)
  584. continue;
  585. if (!atomic_inc_not_zero(&tvlv_tmp->refcount))
  586. continue;
  587. tvlv = tvlv_tmp;
  588. break;
  589. }
  590. return tvlv;
  591. }
  592. /**
  593. * batadv_tvlv_container_list_size - calculate the size of the tvlv container
  594. * list entries
  595. * @bat_priv: the bat priv with all the soft interface information
  596. *
  597. * Has to be called with the appropriate locks being acquired
  598. * (tvlv.container_list_lock).
  599. *
  600. * Returns size of all currently registered tvlv containers in bytes.
  601. */
  602. static u16 batadv_tvlv_container_list_size(struct batadv_priv *bat_priv)
  603. {
  604. struct batadv_tvlv_container *tvlv;
  605. u16 tvlv_len = 0;
  606. hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
  607. tvlv_len += sizeof(struct batadv_tvlv_hdr);
  608. tvlv_len += ntohs(tvlv->tvlv_hdr.len);
  609. }
  610. return tvlv_len;
  611. }
  612. /**
  613. * batadv_tvlv_container_remove - remove tvlv container from the tvlv container
  614. * list
  615. * @bat_priv: the bat priv with all the soft interface information
  616. * @tvlv: the to be removed tvlv container
  617. *
  618. * Has to be called with the appropriate locks being acquired
  619. * (tvlv.container_list_lock).
  620. */
  621. static void batadv_tvlv_container_remove(struct batadv_priv *bat_priv,
  622. struct batadv_tvlv_container *tvlv)
  623. {
  624. lockdep_assert_held(&bat_priv->tvlv.handler_list_lock);
  625. if (!tvlv)
  626. return;
  627. hlist_del(&tvlv->list);
  628. /* first call to decrement the counter, second call to free */
  629. batadv_tvlv_container_free_ref(tvlv);
  630. batadv_tvlv_container_free_ref(tvlv);
  631. }
  632. /**
  633. * batadv_tvlv_container_unregister - unregister tvlv container based on the
  634. * provided type and version (both need to match)
  635. * @bat_priv: the bat priv with all the soft interface information
  636. * @type: tvlv container type to unregister
  637. * @version: tvlv container type to unregister
  638. */
  639. void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv,
  640. u8 type, u8 version)
  641. {
  642. struct batadv_tvlv_container *tvlv;
  643. spin_lock_bh(&bat_priv->tvlv.container_list_lock);
  644. tvlv = batadv_tvlv_container_get(bat_priv, type, version);
  645. batadv_tvlv_container_remove(bat_priv, tvlv);
  646. spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
  647. }
  648. /**
  649. * batadv_tvlv_container_register - register tvlv type, version and content
  650. * to be propagated with each (primary interface) OGM
  651. * @bat_priv: the bat priv with all the soft interface information
  652. * @type: tvlv container type
  653. * @version: tvlv container version
  654. * @tvlv_value: tvlv container content
  655. * @tvlv_value_len: tvlv container content length
  656. *
  657. * If a container of the same type and version was already registered the new
  658. * content is going to replace the old one.
  659. */
  660. void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
  661. u8 type, u8 version,
  662. void *tvlv_value, u16 tvlv_value_len)
  663. {
  664. struct batadv_tvlv_container *tvlv_old, *tvlv_new;
  665. if (!tvlv_value)
  666. tvlv_value_len = 0;
  667. tvlv_new = kzalloc(sizeof(*tvlv_new) + tvlv_value_len, GFP_ATOMIC);
  668. if (!tvlv_new)
  669. return;
  670. tvlv_new->tvlv_hdr.version = version;
  671. tvlv_new->tvlv_hdr.type = type;
  672. tvlv_new->tvlv_hdr.len = htons(tvlv_value_len);
  673. memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len));
  674. INIT_HLIST_NODE(&tvlv_new->list);
  675. atomic_set(&tvlv_new->refcount, 1);
  676. spin_lock_bh(&bat_priv->tvlv.container_list_lock);
  677. tvlv_old = batadv_tvlv_container_get(bat_priv, type, version);
  678. batadv_tvlv_container_remove(bat_priv, tvlv_old);
  679. hlist_add_head(&tvlv_new->list, &bat_priv->tvlv.container_list);
  680. spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
  681. }
  682. /**
  683. * batadv_tvlv_realloc_packet_buff - reallocate packet buffer to accommodate
  684. * requested packet size
  685. * @packet_buff: packet buffer
  686. * @packet_buff_len: packet buffer size
  687. * @min_packet_len: requested packet minimum size
  688. * @additional_packet_len: requested additional packet size on top of minimum
  689. * size
  690. *
  691. * Returns true of the packet buffer could be changed to the requested size,
  692. * false otherwise.
  693. */
  694. static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff,
  695. int *packet_buff_len,
  696. int min_packet_len,
  697. int additional_packet_len)
  698. {
  699. unsigned char *new_buff;
  700. new_buff = kmalloc(min_packet_len + additional_packet_len, GFP_ATOMIC);
  701. /* keep old buffer if kmalloc should fail */
  702. if (!new_buff)
  703. return false;
  704. memcpy(new_buff, *packet_buff, min_packet_len);
  705. kfree(*packet_buff);
  706. *packet_buff = new_buff;
  707. *packet_buff_len = min_packet_len + additional_packet_len;
  708. return true;
  709. }
  710. /**
  711. * batadv_tvlv_container_ogm_append - append tvlv container content to given
  712. * OGM packet buffer
  713. * @bat_priv: the bat priv with all the soft interface information
  714. * @packet_buff: ogm packet buffer
  715. * @packet_buff_len: ogm packet buffer size including ogm header and tvlv
  716. * content
  717. * @packet_min_len: ogm header size to be preserved for the OGM itself
  718. *
  719. * The ogm packet might be enlarged or shrunk depending on the current size
  720. * and the size of the to-be-appended tvlv containers.
  721. *
  722. * Returns size of all appended tvlv containers in bytes.
  723. */
  724. u16 batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv,
  725. unsigned char **packet_buff,
  726. int *packet_buff_len, int packet_min_len)
  727. {
  728. struct batadv_tvlv_container *tvlv;
  729. struct batadv_tvlv_hdr *tvlv_hdr;
  730. u16 tvlv_value_len;
  731. void *tvlv_value;
  732. bool ret;
  733. spin_lock_bh(&bat_priv->tvlv.container_list_lock);
  734. tvlv_value_len = batadv_tvlv_container_list_size(bat_priv);
  735. ret = batadv_tvlv_realloc_packet_buff(packet_buff, packet_buff_len,
  736. packet_min_len, tvlv_value_len);
  737. if (!ret)
  738. goto end;
  739. if (!tvlv_value_len)
  740. goto end;
  741. tvlv_value = (*packet_buff) + packet_min_len;
  742. hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
  743. tvlv_hdr = tvlv_value;
  744. tvlv_hdr->type = tvlv->tvlv_hdr.type;
  745. tvlv_hdr->version = tvlv->tvlv_hdr.version;
  746. tvlv_hdr->len = tvlv->tvlv_hdr.len;
  747. tvlv_value = tvlv_hdr + 1;
  748. memcpy(tvlv_value, tvlv + 1, ntohs(tvlv->tvlv_hdr.len));
  749. tvlv_value = (u8 *)tvlv_value + ntohs(tvlv->tvlv_hdr.len);
  750. }
  751. end:
  752. spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
  753. return tvlv_value_len;
  754. }
  755. /**
  756. * batadv_tvlv_call_handler - parse the given tvlv buffer to call the
  757. * appropriate handlers
  758. * @bat_priv: the bat priv with all the soft interface information
  759. * @tvlv_handler: tvlv callback function handling the tvlv content
  760. * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet
  761. * @orig_node: orig node emitting the ogm packet
  762. * @src: source mac address of the unicast packet
  763. * @dst: destination mac address of the unicast packet
  764. * @tvlv_value: tvlv content
  765. * @tvlv_value_len: tvlv content length
  766. *
  767. * Returns success if handler was not found or the return value of the handler
  768. * callback.
  769. */
  770. static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv,
  771. struct batadv_tvlv_handler *tvlv_handler,
  772. bool ogm_source,
  773. struct batadv_orig_node *orig_node,
  774. u8 *src, u8 *dst,
  775. void *tvlv_value, u16 tvlv_value_len)
  776. {
  777. if (!tvlv_handler)
  778. return NET_RX_SUCCESS;
  779. if (ogm_source) {
  780. if (!tvlv_handler->ogm_handler)
  781. return NET_RX_SUCCESS;
  782. if (!orig_node)
  783. return NET_RX_SUCCESS;
  784. tvlv_handler->ogm_handler(bat_priv, orig_node,
  785. BATADV_NO_FLAGS,
  786. tvlv_value, tvlv_value_len);
  787. tvlv_handler->flags |= BATADV_TVLV_HANDLER_OGM_CALLED;
  788. } else {
  789. if (!src)
  790. return NET_RX_SUCCESS;
  791. if (!dst)
  792. return NET_RX_SUCCESS;
  793. if (!tvlv_handler->unicast_handler)
  794. return NET_RX_SUCCESS;
  795. return tvlv_handler->unicast_handler(bat_priv, src,
  796. dst, tvlv_value,
  797. tvlv_value_len);
  798. }
  799. return NET_RX_SUCCESS;
  800. }
  801. /**
  802. * batadv_tvlv_containers_process - parse the given tvlv buffer to call the
  803. * appropriate handlers
  804. * @bat_priv: the bat priv with all the soft interface information
  805. * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet
  806. * @orig_node: orig node emitting the ogm packet
  807. * @src: source mac address of the unicast packet
  808. * @dst: destination mac address of the unicast packet
  809. * @tvlv_value: tvlv content
  810. * @tvlv_value_len: tvlv content length
  811. *
  812. * Returns success when processing an OGM or the return value of all called
  813. * handler callbacks.
  814. */
  815. int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
  816. bool ogm_source,
  817. struct batadv_orig_node *orig_node,
  818. u8 *src, u8 *dst,
  819. void *tvlv_value, u16 tvlv_value_len)
  820. {
  821. struct batadv_tvlv_handler *tvlv_handler;
  822. struct batadv_tvlv_hdr *tvlv_hdr;
  823. u16 tvlv_value_cont_len;
  824. u8 cifnotfound = BATADV_TVLV_HANDLER_OGM_CIFNOTFND;
  825. int ret = NET_RX_SUCCESS;
  826. while (tvlv_value_len >= sizeof(*tvlv_hdr)) {
  827. tvlv_hdr = tvlv_value;
  828. tvlv_value_cont_len = ntohs(tvlv_hdr->len);
  829. tvlv_value = tvlv_hdr + 1;
  830. tvlv_value_len -= sizeof(*tvlv_hdr);
  831. if (tvlv_value_cont_len > tvlv_value_len)
  832. break;
  833. tvlv_handler = batadv_tvlv_handler_get(bat_priv,
  834. tvlv_hdr->type,
  835. tvlv_hdr->version);
  836. ret |= batadv_tvlv_call_handler(bat_priv, tvlv_handler,
  837. ogm_source, orig_node,
  838. src, dst, tvlv_value,
  839. tvlv_value_cont_len);
  840. if (tvlv_handler)
  841. batadv_tvlv_handler_free_ref(tvlv_handler);
  842. tvlv_value = (u8 *)tvlv_value + tvlv_value_cont_len;
  843. tvlv_value_len -= tvlv_value_cont_len;
  844. }
  845. if (!ogm_source)
  846. return ret;
  847. rcu_read_lock();
  848. hlist_for_each_entry_rcu(tvlv_handler,
  849. &bat_priv->tvlv.handler_list, list) {
  850. if ((tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) &&
  851. !(tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CALLED))
  852. tvlv_handler->ogm_handler(bat_priv, orig_node,
  853. cifnotfound, NULL, 0);
  854. tvlv_handler->flags &= ~BATADV_TVLV_HANDLER_OGM_CALLED;
  855. }
  856. rcu_read_unlock();
  857. return NET_RX_SUCCESS;
  858. }
  859. /**
  860. * batadv_tvlv_ogm_receive - process an incoming ogm and call the appropriate
  861. * handlers
  862. * @bat_priv: the bat priv with all the soft interface information
  863. * @batadv_ogm_packet: ogm packet containing the tvlv containers
  864. * @orig_node: orig node emitting the ogm packet
  865. */
  866. void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
  867. struct batadv_ogm_packet *batadv_ogm_packet,
  868. struct batadv_orig_node *orig_node)
  869. {
  870. void *tvlv_value;
  871. u16 tvlv_value_len;
  872. if (!batadv_ogm_packet)
  873. return;
  874. tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len);
  875. if (!tvlv_value_len)
  876. return;
  877. tvlv_value = batadv_ogm_packet + 1;
  878. batadv_tvlv_containers_process(bat_priv, true, orig_node, NULL, NULL,
  879. tvlv_value, tvlv_value_len);
  880. }
  881. /**
  882. * batadv_tvlv_handler_register - register tvlv handler based on the provided
  883. * type and version (both need to match) for ogm tvlv payload and/or unicast
  884. * payload
  885. * @bat_priv: the bat priv with all the soft interface information
  886. * @optr: ogm tvlv handler callback function. This function receives the orig
  887. * node, flags and the tvlv content as argument to process.
  888. * @uptr: unicast tvlv handler callback function. This function receives the
  889. * source & destination of the unicast packet as well as the tvlv content
  890. * to process.
  891. * @type: tvlv handler type to be registered
  892. * @version: tvlv handler version to be registered
  893. * @flags: flags to enable or disable TVLV API behavior
  894. */
  895. void batadv_tvlv_handler_register(struct batadv_priv *bat_priv,
  896. void (*optr)(struct batadv_priv *bat_priv,
  897. struct batadv_orig_node *orig,
  898. u8 flags,
  899. void *tvlv_value,
  900. u16 tvlv_value_len),
  901. int (*uptr)(struct batadv_priv *bat_priv,
  902. u8 *src, u8 *dst,
  903. void *tvlv_value,
  904. u16 tvlv_value_len),
  905. u8 type, u8 version, u8 flags)
  906. {
  907. struct batadv_tvlv_handler *tvlv_handler;
  908. tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
  909. if (tvlv_handler) {
  910. batadv_tvlv_handler_free_ref(tvlv_handler);
  911. return;
  912. }
  913. tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC);
  914. if (!tvlv_handler)
  915. return;
  916. tvlv_handler->ogm_handler = optr;
  917. tvlv_handler->unicast_handler = uptr;
  918. tvlv_handler->type = type;
  919. tvlv_handler->version = version;
  920. tvlv_handler->flags = flags;
  921. atomic_set(&tvlv_handler->refcount, 1);
  922. INIT_HLIST_NODE(&tvlv_handler->list);
  923. spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
  924. hlist_add_head_rcu(&tvlv_handler->list, &bat_priv->tvlv.handler_list);
  925. spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
  926. }
  927. /**
  928. * batadv_tvlv_handler_unregister - unregister tvlv handler based on the
  929. * provided type and version (both need to match)
  930. * @bat_priv: the bat priv with all the soft interface information
  931. * @type: tvlv handler type to be unregistered
  932. * @version: tvlv handler version to be unregistered
  933. */
  934. void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
  935. u8 type, u8 version)
  936. {
  937. struct batadv_tvlv_handler *tvlv_handler;
  938. tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
  939. if (!tvlv_handler)
  940. return;
  941. batadv_tvlv_handler_free_ref(tvlv_handler);
  942. spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
  943. hlist_del_rcu(&tvlv_handler->list);
  944. spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
  945. batadv_tvlv_handler_free_ref(tvlv_handler);
  946. }
  947. /**
  948. * batadv_tvlv_unicast_send - send a unicast packet with tvlv payload to the
  949. * specified host
  950. * @bat_priv: the bat priv with all the soft interface information
  951. * @src: source mac address of the unicast packet
  952. * @dst: destination mac address of the unicast packet
  953. * @type: tvlv type
  954. * @version: tvlv version
  955. * @tvlv_value: tvlv content
  956. * @tvlv_value_len: tvlv content length
  957. */
  958. void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, u8 *src,
  959. u8 *dst, u8 type, u8 version,
  960. void *tvlv_value, u16 tvlv_value_len)
  961. {
  962. struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
  963. struct batadv_tvlv_hdr *tvlv_hdr;
  964. struct batadv_orig_node *orig_node;
  965. struct sk_buff *skb = NULL;
  966. unsigned char *tvlv_buff;
  967. unsigned int tvlv_len;
  968. ssize_t hdr_len = sizeof(*unicast_tvlv_packet);
  969. bool ret = false;
  970. orig_node = batadv_orig_hash_find(bat_priv, dst);
  971. if (!orig_node)
  972. goto out;
  973. tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len;
  974. skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len);
  975. if (!skb)
  976. goto out;
  977. skb->priority = TC_PRIO_CONTROL;
  978. skb_reserve(skb, ETH_HLEN);
  979. tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len);
  980. unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff;
  981. unicast_tvlv_packet->packet_type = BATADV_UNICAST_TVLV;
  982. unicast_tvlv_packet->version = BATADV_COMPAT_VERSION;
  983. unicast_tvlv_packet->ttl = BATADV_TTL;
  984. unicast_tvlv_packet->reserved = 0;
  985. unicast_tvlv_packet->tvlv_len = htons(tvlv_len);
  986. unicast_tvlv_packet->align = 0;
  987. ether_addr_copy(unicast_tvlv_packet->src, src);
  988. ether_addr_copy(unicast_tvlv_packet->dst, dst);
  989. tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1);
  990. tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff;
  991. tvlv_hdr->version = version;
  992. tvlv_hdr->type = type;
  993. tvlv_hdr->len = htons(tvlv_value_len);
  994. tvlv_buff += sizeof(*tvlv_hdr);
  995. memcpy(tvlv_buff, tvlv_value, tvlv_value_len);
  996. if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
  997. ret = true;
  998. out:
  999. if (skb && !ret)
  1000. kfree_skb(skb);
  1001. if (orig_node)
  1002. batadv_orig_node_free_ref(orig_node);
  1003. }
  1004. /**
  1005. * batadv_get_vid - extract the VLAN identifier from skb if any
  1006. * @skb: the buffer containing the packet
  1007. * @header_len: length of the batman header preceding the ethernet header
  1008. *
  1009. * If the packet embedded in the skb is vlan tagged this function returns the
  1010. * VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS is returned.
  1011. */
  1012. unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len)
  1013. {
  1014. struct ethhdr *ethhdr = (struct ethhdr *)(skb->data + header_len);
  1015. struct vlan_ethhdr *vhdr;
  1016. unsigned short vid;
  1017. if (ethhdr->h_proto != htons(ETH_P_8021Q))
  1018. return BATADV_NO_FLAGS;
  1019. if (!pskb_may_pull(skb, header_len + VLAN_ETH_HLEN))
  1020. return BATADV_NO_FLAGS;
  1021. vhdr = (struct vlan_ethhdr *)(skb->data + header_len);
  1022. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  1023. vid |= BATADV_VLAN_HAS_TAG;
  1024. return vid;
  1025. }
  1026. /**
  1027. * batadv_vlan_ap_isola_get - return the AP isolation status for the given vlan
  1028. * @bat_priv: the bat priv with all the soft interface information
  1029. * @vid: the VLAN identifier for which the AP isolation attributed as to be
  1030. * looked up
  1031. *
  1032. * Returns true if AP isolation is on for the VLAN idenfied by vid, false
  1033. * otherwise
  1034. */
  1035. bool batadv_vlan_ap_isola_get(struct batadv_priv *bat_priv, unsigned short vid)
  1036. {
  1037. bool ap_isolation_enabled = false;
  1038. struct batadv_softif_vlan *vlan;
  1039. /* if the AP isolation is requested on a VLAN, then check for its
  1040. * setting in the proper VLAN private data structure
  1041. */
  1042. vlan = batadv_softif_vlan_get(bat_priv, vid);
  1043. if (vlan) {
  1044. ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
  1045. batadv_softif_vlan_free_ref(vlan);
  1046. }
  1047. return ap_isolation_enabled;
  1048. }
  1049. static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
  1050. {
  1051. struct batadv_algo_ops *bat_algo_ops;
  1052. char *algo_name = (char *)val;
  1053. size_t name_len = strlen(algo_name);
  1054. if (name_len > 0 && algo_name[name_len - 1] == '\n')
  1055. algo_name[name_len - 1] = '\0';
  1056. bat_algo_ops = batadv_algo_get(algo_name);
  1057. if (!bat_algo_ops) {
  1058. pr_err("Routing algorithm '%s' is not supported\n", algo_name);
  1059. return -EINVAL;
  1060. }
  1061. return param_set_copystring(algo_name, kp);
  1062. }
  1063. static const struct kernel_param_ops batadv_param_ops_ra = {
  1064. .set = batadv_param_set_ra,
  1065. .get = param_get_string,
  1066. };
  1067. static struct kparam_string batadv_param_string_ra = {
  1068. .maxlen = sizeof(batadv_routing_algo),
  1069. .string = batadv_routing_algo,
  1070. };
  1071. module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
  1072. 0644);
  1073. module_init(batadv_init);
  1074. module_exit(batadv_exit);
  1075. MODULE_LICENSE("GPL");
  1076. MODULE_AUTHOR(BATADV_DRIVER_AUTHOR);
  1077. MODULE_DESCRIPTION(BATADV_DRIVER_DESC);
  1078. MODULE_SUPPORTED_DEVICE(BATADV_DRIVER_DEVICE);
  1079. MODULE_VERSION(BATADV_SOURCE_VERSION);