main.c 36 KB

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