main.c 34 KB

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