main.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  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. !bat_algo_ops->bat_neigh_cmp) {
  415. pr_info("Routing algo '%s' does not implement required ops\n",
  416. bat_algo_ops->name);
  417. ret = -EINVAL;
  418. goto out;
  419. }
  420. INIT_HLIST_NODE(&bat_algo_ops->list);
  421. hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
  422. ret = 0;
  423. out:
  424. return ret;
  425. }
  426. int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
  427. {
  428. struct batadv_algo_ops *bat_algo_ops;
  429. int ret = -EINVAL;
  430. bat_algo_ops = batadv_algo_get(name);
  431. if (!bat_algo_ops)
  432. goto out;
  433. bat_priv->bat_algo_ops = bat_algo_ops;
  434. ret = 0;
  435. out:
  436. return ret;
  437. }
  438. int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
  439. {
  440. struct batadv_algo_ops *bat_algo_ops;
  441. seq_puts(seq, "Available routing algorithms:\n");
  442. hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
  443. seq_printf(seq, "%s\n", bat_algo_ops->name);
  444. }
  445. return 0;
  446. }
  447. /**
  448. * batadv_skb_crc32 - calculate CRC32 of the whole packet and skip bytes in
  449. * the header
  450. * @skb: skb pointing to fragmented socket buffers
  451. * @payload_ptr: Pointer to position inside the head buffer of the skb
  452. * marking the start of the data to be CRC'ed
  453. *
  454. * payload_ptr must always point to an address in the skb head buffer and not to
  455. * a fragment.
  456. */
  457. __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr)
  458. {
  459. u32 crc = 0;
  460. unsigned int from;
  461. unsigned int to = skb->len;
  462. struct skb_seq_state st;
  463. const u8 *data;
  464. unsigned int len;
  465. unsigned int consumed = 0;
  466. from = (unsigned int)(payload_ptr - skb->data);
  467. skb_prepare_seq_read(skb, from, to, &st);
  468. while ((len = skb_seq_read(consumed, &data, &st)) != 0) {
  469. crc = crc32c(crc, data, len);
  470. consumed += len;
  471. }
  472. return htonl(crc);
  473. }
  474. /**
  475. * batadv_tvlv_handler_free_ref - decrement the tvlv handler refcounter and
  476. * possibly free it
  477. * @tvlv_handler: the tvlv handler to free
  478. */
  479. static void
  480. batadv_tvlv_handler_free_ref(struct batadv_tvlv_handler *tvlv_handler)
  481. {
  482. if (atomic_dec_and_test(&tvlv_handler->refcount))
  483. kfree_rcu(tvlv_handler, rcu);
  484. }
  485. /**
  486. * batadv_tvlv_handler_get - retrieve tvlv handler from the tvlv handler list
  487. * based on the provided type and version (both need to match)
  488. * @bat_priv: the bat priv with all the soft interface information
  489. * @type: tvlv handler type to look for
  490. * @version: tvlv handler version to look for
  491. *
  492. * Returns tvlv handler if found or NULL otherwise.
  493. */
  494. static struct batadv_tvlv_handler
  495. *batadv_tvlv_handler_get(struct batadv_priv *bat_priv,
  496. uint8_t type, uint8_t version)
  497. {
  498. struct batadv_tvlv_handler *tvlv_handler_tmp, *tvlv_handler = NULL;
  499. rcu_read_lock();
  500. hlist_for_each_entry_rcu(tvlv_handler_tmp,
  501. &bat_priv->tvlv.handler_list, list) {
  502. if (tvlv_handler_tmp->type != type)
  503. continue;
  504. if (tvlv_handler_tmp->version != version)
  505. continue;
  506. if (!atomic_inc_not_zero(&tvlv_handler_tmp->refcount))
  507. continue;
  508. tvlv_handler = tvlv_handler_tmp;
  509. break;
  510. }
  511. rcu_read_unlock();
  512. return tvlv_handler;
  513. }
  514. /**
  515. * batadv_tvlv_container_free_ref - decrement the tvlv container refcounter and
  516. * possibly free it
  517. * @tvlv_handler: the tvlv container to free
  518. */
  519. static void batadv_tvlv_container_free_ref(struct batadv_tvlv_container *tvlv)
  520. {
  521. if (atomic_dec_and_test(&tvlv->refcount))
  522. kfree(tvlv);
  523. }
  524. /**
  525. * batadv_tvlv_container_get - retrieve tvlv container from the tvlv container
  526. * list based on the provided type and version (both need to match)
  527. * @bat_priv: the bat priv with all the soft interface information
  528. * @type: tvlv container type to look for
  529. * @version: tvlv container version to look for
  530. *
  531. * Has to be called with the appropriate locks being acquired
  532. * (tvlv.container_list_lock).
  533. *
  534. * Returns tvlv container if found or NULL otherwise.
  535. */
  536. static struct batadv_tvlv_container
  537. *batadv_tvlv_container_get(struct batadv_priv *bat_priv,
  538. uint8_t type, uint8_t version)
  539. {
  540. struct batadv_tvlv_container *tvlv_tmp, *tvlv = NULL;
  541. hlist_for_each_entry(tvlv_tmp, &bat_priv->tvlv.container_list, list) {
  542. if (tvlv_tmp->tvlv_hdr.type != type)
  543. continue;
  544. if (tvlv_tmp->tvlv_hdr.version != version)
  545. continue;
  546. if (!atomic_inc_not_zero(&tvlv_tmp->refcount))
  547. continue;
  548. tvlv = tvlv_tmp;
  549. break;
  550. }
  551. return tvlv;
  552. }
  553. /**
  554. * batadv_tvlv_container_list_size - calculate the size of the tvlv container
  555. * list entries
  556. * @bat_priv: the bat priv with all the soft interface information
  557. *
  558. * Has to be called with the appropriate locks being acquired
  559. * (tvlv.container_list_lock).
  560. *
  561. * Returns size of all currently registered tvlv containers in bytes.
  562. */
  563. static uint16_t batadv_tvlv_container_list_size(struct batadv_priv *bat_priv)
  564. {
  565. struct batadv_tvlv_container *tvlv;
  566. uint16_t tvlv_len = 0;
  567. hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
  568. tvlv_len += sizeof(struct batadv_tvlv_hdr);
  569. tvlv_len += ntohs(tvlv->tvlv_hdr.len);
  570. }
  571. return tvlv_len;
  572. }
  573. /**
  574. * batadv_tvlv_container_remove - remove tvlv container from the tvlv container
  575. * list
  576. * @tvlv: the to be removed tvlv container
  577. *
  578. * Has to be called with the appropriate locks being acquired
  579. * (tvlv.container_list_lock).
  580. */
  581. static void batadv_tvlv_container_remove(struct batadv_tvlv_container *tvlv)
  582. {
  583. if (!tvlv)
  584. return;
  585. hlist_del(&tvlv->list);
  586. /* first call to decrement the counter, second call to free */
  587. batadv_tvlv_container_free_ref(tvlv);
  588. batadv_tvlv_container_free_ref(tvlv);
  589. }
  590. /**
  591. * batadv_tvlv_container_unregister - unregister tvlv container based on the
  592. * provided type and version (both need to match)
  593. * @bat_priv: the bat priv with all the soft interface information
  594. * @type: tvlv container type to unregister
  595. * @version: tvlv container type to unregister
  596. */
  597. void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv,
  598. uint8_t type, uint8_t version)
  599. {
  600. struct batadv_tvlv_container *tvlv;
  601. spin_lock_bh(&bat_priv->tvlv.container_list_lock);
  602. tvlv = batadv_tvlv_container_get(bat_priv, type, version);
  603. batadv_tvlv_container_remove(tvlv);
  604. spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
  605. }
  606. /**
  607. * batadv_tvlv_container_register - register tvlv type, version and content
  608. * to be propagated with each (primary interface) OGM
  609. * @bat_priv: the bat priv with all the soft interface information
  610. * @type: tvlv container type
  611. * @version: tvlv container version
  612. * @tvlv_value: tvlv container content
  613. * @tvlv_value_len: tvlv container content length
  614. *
  615. * If a container of the same type and version was already registered the new
  616. * content is going to replace the old one.
  617. */
  618. void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
  619. uint8_t type, uint8_t version,
  620. void *tvlv_value, uint16_t tvlv_value_len)
  621. {
  622. struct batadv_tvlv_container *tvlv_old, *tvlv_new;
  623. if (!tvlv_value)
  624. tvlv_value_len = 0;
  625. tvlv_new = kzalloc(sizeof(*tvlv_new) + tvlv_value_len, GFP_ATOMIC);
  626. if (!tvlv_new)
  627. return;
  628. tvlv_new->tvlv_hdr.version = version;
  629. tvlv_new->tvlv_hdr.type = type;
  630. tvlv_new->tvlv_hdr.len = htons(tvlv_value_len);
  631. memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len));
  632. INIT_HLIST_NODE(&tvlv_new->list);
  633. atomic_set(&tvlv_new->refcount, 1);
  634. spin_lock_bh(&bat_priv->tvlv.container_list_lock);
  635. tvlv_old = batadv_tvlv_container_get(bat_priv, type, version);
  636. batadv_tvlv_container_remove(tvlv_old);
  637. hlist_add_head(&tvlv_new->list, &bat_priv->tvlv.container_list);
  638. spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
  639. }
  640. /**
  641. * batadv_tvlv_realloc_packet_buff - reallocate packet buffer to accomodate
  642. * requested packet size
  643. * @packet_buff: packet buffer
  644. * @packet_buff_len: packet buffer size
  645. * @packet_min_len: requested packet minimum size
  646. * @additional_packet_len: requested additional packet size on top of minimum
  647. * size
  648. *
  649. * Returns true of the packet buffer could be changed to the requested size,
  650. * false otherwise.
  651. */
  652. static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff,
  653. int *packet_buff_len,
  654. int min_packet_len,
  655. int additional_packet_len)
  656. {
  657. unsigned char *new_buff;
  658. new_buff = kmalloc(min_packet_len + additional_packet_len, GFP_ATOMIC);
  659. /* keep old buffer if kmalloc should fail */
  660. if (new_buff) {
  661. memcpy(new_buff, *packet_buff, min_packet_len);
  662. kfree(*packet_buff);
  663. *packet_buff = new_buff;
  664. *packet_buff_len = min_packet_len + additional_packet_len;
  665. return true;
  666. }
  667. return false;
  668. }
  669. /**
  670. * batadv_tvlv_container_ogm_append - append tvlv container content to given
  671. * OGM packet buffer
  672. * @bat_priv: the bat priv with all the soft interface information
  673. * @packet_buff: ogm packet buffer
  674. * @packet_buff_len: ogm packet buffer size including ogm header and tvlv
  675. * content
  676. * @packet_min_len: ogm header size to be preserved for the OGM itself
  677. *
  678. * The ogm packet might be enlarged or shrunk depending on the current size
  679. * and the size of the to-be-appended tvlv containers.
  680. *
  681. * Returns size of all appended tvlv containers in bytes.
  682. */
  683. uint16_t batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv,
  684. unsigned char **packet_buff,
  685. int *packet_buff_len,
  686. int packet_min_len)
  687. {
  688. struct batadv_tvlv_container *tvlv;
  689. struct batadv_tvlv_hdr *tvlv_hdr;
  690. uint16_t tvlv_value_len;
  691. void *tvlv_value;
  692. bool ret;
  693. spin_lock_bh(&bat_priv->tvlv.container_list_lock);
  694. tvlv_value_len = batadv_tvlv_container_list_size(bat_priv);
  695. ret = batadv_tvlv_realloc_packet_buff(packet_buff, packet_buff_len,
  696. packet_min_len, tvlv_value_len);
  697. if (!ret)
  698. goto end;
  699. if (!tvlv_value_len)
  700. goto end;
  701. tvlv_value = (*packet_buff) + packet_min_len;
  702. hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
  703. tvlv_hdr = tvlv_value;
  704. tvlv_hdr->type = tvlv->tvlv_hdr.type;
  705. tvlv_hdr->version = tvlv->tvlv_hdr.version;
  706. tvlv_hdr->len = tvlv->tvlv_hdr.len;
  707. tvlv_value = tvlv_hdr + 1;
  708. memcpy(tvlv_value, tvlv + 1, ntohs(tvlv->tvlv_hdr.len));
  709. tvlv_value = (uint8_t *)tvlv_value + ntohs(tvlv->tvlv_hdr.len);
  710. }
  711. end:
  712. spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
  713. return tvlv_value_len;
  714. }
  715. /**
  716. * batadv_tvlv_call_handler - parse the given tvlv buffer to call the
  717. * appropriate handlers
  718. * @bat_priv: the bat priv with all the soft interface information
  719. * @tvlv_handler: tvlv callback function handling the tvlv content
  720. * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet
  721. * @orig_node: orig node emitting the ogm packet
  722. * @src: source mac address of the unicast packet
  723. * @dst: destination mac address of the unicast packet
  724. * @tvlv_value: tvlv content
  725. * @tvlv_value_len: tvlv content length
  726. *
  727. * Returns success if handler was not found or the return value of the handler
  728. * callback.
  729. */
  730. static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv,
  731. struct batadv_tvlv_handler *tvlv_handler,
  732. bool ogm_source,
  733. struct batadv_orig_node *orig_node,
  734. uint8_t *src, uint8_t *dst,
  735. void *tvlv_value, uint16_t tvlv_value_len)
  736. {
  737. if (!tvlv_handler)
  738. return NET_RX_SUCCESS;
  739. if (ogm_source) {
  740. if (!tvlv_handler->ogm_handler)
  741. return NET_RX_SUCCESS;
  742. if (!orig_node)
  743. return NET_RX_SUCCESS;
  744. tvlv_handler->ogm_handler(bat_priv, orig_node,
  745. BATADV_NO_FLAGS,
  746. tvlv_value, tvlv_value_len);
  747. tvlv_handler->flags |= BATADV_TVLV_HANDLER_OGM_CALLED;
  748. } else {
  749. if (!src)
  750. return NET_RX_SUCCESS;
  751. if (!dst)
  752. return NET_RX_SUCCESS;
  753. if (!tvlv_handler->unicast_handler)
  754. return NET_RX_SUCCESS;
  755. return tvlv_handler->unicast_handler(bat_priv, src,
  756. dst, tvlv_value,
  757. tvlv_value_len);
  758. }
  759. return NET_RX_SUCCESS;
  760. }
  761. /**
  762. * batadv_tvlv_containers_process - parse the given tvlv buffer to call the
  763. * appropriate handlers
  764. * @bat_priv: the bat priv with all the soft interface information
  765. * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet
  766. * @orig_node: orig node emitting the ogm packet
  767. * @src: source mac address of the unicast packet
  768. * @dst: destination mac address of the unicast packet
  769. * @tvlv_value: tvlv content
  770. * @tvlv_value_len: tvlv content length
  771. *
  772. * Returns success when processing an OGM or the return value of all called
  773. * handler callbacks.
  774. */
  775. int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
  776. bool ogm_source,
  777. struct batadv_orig_node *orig_node,
  778. uint8_t *src, uint8_t *dst,
  779. void *tvlv_value, uint16_t tvlv_value_len)
  780. {
  781. struct batadv_tvlv_handler *tvlv_handler;
  782. struct batadv_tvlv_hdr *tvlv_hdr;
  783. uint16_t tvlv_value_cont_len;
  784. uint8_t cifnotfound = BATADV_TVLV_HANDLER_OGM_CIFNOTFND;
  785. int ret = NET_RX_SUCCESS;
  786. while (tvlv_value_len >= sizeof(*tvlv_hdr)) {
  787. tvlv_hdr = tvlv_value;
  788. tvlv_value_cont_len = ntohs(tvlv_hdr->len);
  789. tvlv_value = tvlv_hdr + 1;
  790. tvlv_value_len -= sizeof(*tvlv_hdr);
  791. if (tvlv_value_cont_len > tvlv_value_len)
  792. break;
  793. tvlv_handler = batadv_tvlv_handler_get(bat_priv,
  794. tvlv_hdr->type,
  795. tvlv_hdr->version);
  796. ret |= batadv_tvlv_call_handler(bat_priv, tvlv_handler,
  797. ogm_source, orig_node,
  798. src, dst, tvlv_value,
  799. tvlv_value_cont_len);
  800. if (tvlv_handler)
  801. batadv_tvlv_handler_free_ref(tvlv_handler);
  802. tvlv_value = (uint8_t *)tvlv_value + tvlv_value_cont_len;
  803. tvlv_value_len -= tvlv_value_cont_len;
  804. }
  805. if (!ogm_source)
  806. return ret;
  807. rcu_read_lock();
  808. hlist_for_each_entry_rcu(tvlv_handler,
  809. &bat_priv->tvlv.handler_list, list) {
  810. if ((tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) &&
  811. !(tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CALLED))
  812. tvlv_handler->ogm_handler(bat_priv, orig_node,
  813. cifnotfound, NULL, 0);
  814. tvlv_handler->flags &= ~BATADV_TVLV_HANDLER_OGM_CALLED;
  815. }
  816. rcu_read_unlock();
  817. return NET_RX_SUCCESS;
  818. }
  819. /**
  820. * batadv_tvlv_ogm_receive - process an incoming ogm and call the appropriate
  821. * handlers
  822. * @bat_priv: the bat priv with all the soft interface information
  823. * @batadv_ogm_packet: ogm packet containing the tvlv containers
  824. * @orig_node: orig node emitting the ogm packet
  825. */
  826. void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
  827. struct batadv_ogm_packet *batadv_ogm_packet,
  828. struct batadv_orig_node *orig_node)
  829. {
  830. void *tvlv_value;
  831. uint16_t tvlv_value_len;
  832. if (!batadv_ogm_packet)
  833. return;
  834. tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len);
  835. if (!tvlv_value_len)
  836. return;
  837. tvlv_value = batadv_ogm_packet + 1;
  838. batadv_tvlv_containers_process(bat_priv, true, orig_node, NULL, NULL,
  839. tvlv_value, tvlv_value_len);
  840. }
  841. /**
  842. * batadv_tvlv_handler_register - register tvlv handler based on the provided
  843. * type and version (both need to match) for ogm tvlv payload and/or unicast
  844. * payload
  845. * @bat_priv: the bat priv with all the soft interface information
  846. * @optr: ogm tvlv handler callback function. This function receives the orig
  847. * node, flags and the tvlv content as argument to process.
  848. * @uptr: unicast tvlv handler callback function. This function receives the
  849. * source & destination of the unicast packet as well as the tvlv content
  850. * to process.
  851. * @type: tvlv handler type to be registered
  852. * @version: tvlv handler version to be registered
  853. * @flags: flags to enable or disable TVLV API behavior
  854. */
  855. void batadv_tvlv_handler_register(struct batadv_priv *bat_priv,
  856. void (*optr)(struct batadv_priv *bat_priv,
  857. struct batadv_orig_node *orig,
  858. uint8_t flags,
  859. void *tvlv_value,
  860. uint16_t tvlv_value_len),
  861. int (*uptr)(struct batadv_priv *bat_priv,
  862. uint8_t *src, uint8_t *dst,
  863. void *tvlv_value,
  864. uint16_t tvlv_value_len),
  865. uint8_t type, uint8_t version, uint8_t flags)
  866. {
  867. struct batadv_tvlv_handler *tvlv_handler;
  868. tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
  869. if (tvlv_handler) {
  870. batadv_tvlv_handler_free_ref(tvlv_handler);
  871. return;
  872. }
  873. tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC);
  874. if (!tvlv_handler)
  875. return;
  876. tvlv_handler->ogm_handler = optr;
  877. tvlv_handler->unicast_handler = uptr;
  878. tvlv_handler->type = type;
  879. tvlv_handler->version = version;
  880. tvlv_handler->flags = flags;
  881. atomic_set(&tvlv_handler->refcount, 1);
  882. INIT_HLIST_NODE(&tvlv_handler->list);
  883. spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
  884. hlist_add_head_rcu(&tvlv_handler->list, &bat_priv->tvlv.handler_list);
  885. spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
  886. }
  887. /**
  888. * batadv_tvlv_handler_unregister - unregister tvlv handler based on the
  889. * provided type and version (both need to match)
  890. * @bat_priv: the bat priv with all the soft interface information
  891. * @type: tvlv handler type to be unregistered
  892. * @version: tvlv handler version to be unregistered
  893. */
  894. void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
  895. uint8_t type, uint8_t version)
  896. {
  897. struct batadv_tvlv_handler *tvlv_handler;
  898. tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
  899. if (!tvlv_handler)
  900. return;
  901. batadv_tvlv_handler_free_ref(tvlv_handler);
  902. spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
  903. hlist_del_rcu(&tvlv_handler->list);
  904. spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
  905. batadv_tvlv_handler_free_ref(tvlv_handler);
  906. }
  907. /**
  908. * batadv_tvlv_unicast_send - send a unicast packet with tvlv payload to the
  909. * specified host
  910. * @bat_priv: the bat priv with all the soft interface information
  911. * @src: source mac address of the unicast packet
  912. * @dst: destination mac address of the unicast packet
  913. * @type: tvlv type
  914. * @version: tvlv version
  915. * @tvlv_value: tvlv content
  916. * @tvlv_value_len: tvlv content length
  917. */
  918. void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, uint8_t *src,
  919. uint8_t *dst, uint8_t type, uint8_t version,
  920. void *tvlv_value, uint16_t tvlv_value_len)
  921. {
  922. struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
  923. struct batadv_tvlv_hdr *tvlv_hdr;
  924. struct batadv_orig_node *orig_node;
  925. struct sk_buff *skb = NULL;
  926. unsigned char *tvlv_buff;
  927. unsigned int tvlv_len;
  928. ssize_t hdr_len = sizeof(*unicast_tvlv_packet);
  929. bool ret = false;
  930. orig_node = batadv_orig_hash_find(bat_priv, dst);
  931. if (!orig_node)
  932. goto out;
  933. tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len;
  934. skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len);
  935. if (!skb)
  936. goto out;
  937. skb->priority = TC_PRIO_CONTROL;
  938. skb_reserve(skb, ETH_HLEN);
  939. tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len);
  940. unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff;
  941. unicast_tvlv_packet->header.packet_type = BATADV_UNICAST_TVLV;
  942. unicast_tvlv_packet->header.version = BATADV_COMPAT_VERSION;
  943. unicast_tvlv_packet->header.ttl = BATADV_TTL;
  944. unicast_tvlv_packet->reserved = 0;
  945. unicast_tvlv_packet->tvlv_len = htons(tvlv_len);
  946. unicast_tvlv_packet->align = 0;
  947. memcpy(unicast_tvlv_packet->src, src, ETH_ALEN);
  948. memcpy(unicast_tvlv_packet->dst, dst, ETH_ALEN);
  949. tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1);
  950. tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff;
  951. tvlv_hdr->version = version;
  952. tvlv_hdr->type = type;
  953. tvlv_hdr->len = htons(tvlv_value_len);
  954. tvlv_buff += sizeof(*tvlv_hdr);
  955. memcpy(tvlv_buff, tvlv_value, tvlv_value_len);
  956. if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
  957. ret = true;
  958. out:
  959. if (skb && !ret)
  960. kfree_skb(skb);
  961. if (orig_node)
  962. batadv_orig_node_free_ref(orig_node);
  963. }
  964. /**
  965. * batadv_get_vid - extract the VLAN identifier from skb if any
  966. * @skb: the buffer containing the packet
  967. * @header_len: length of the batman header preceding the ethernet header
  968. *
  969. * If the packet embedded in the skb is vlan tagged this function returns the
  970. * VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS is returned.
  971. */
  972. unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len)
  973. {
  974. struct ethhdr *ethhdr = (struct ethhdr *)(skb->data + header_len);
  975. struct vlan_ethhdr *vhdr;
  976. unsigned short vid;
  977. if (ethhdr->h_proto != htons(ETH_P_8021Q))
  978. return BATADV_NO_FLAGS;
  979. if (!pskb_may_pull(skb, header_len + VLAN_ETH_HLEN))
  980. return BATADV_NO_FLAGS;
  981. vhdr = (struct vlan_ethhdr *)(skb->data + header_len);
  982. vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
  983. vid |= BATADV_VLAN_HAS_TAG;
  984. return vid;
  985. }
  986. static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
  987. {
  988. struct batadv_algo_ops *bat_algo_ops;
  989. char *algo_name = (char *)val;
  990. size_t name_len = strlen(algo_name);
  991. if (name_len > 0 && algo_name[name_len - 1] == '\n')
  992. algo_name[name_len - 1] = '\0';
  993. bat_algo_ops = batadv_algo_get(algo_name);
  994. if (!bat_algo_ops) {
  995. pr_err("Routing algorithm '%s' is not supported\n", algo_name);
  996. return -EINVAL;
  997. }
  998. return param_set_copystring(algo_name, kp);
  999. }
  1000. static const struct kernel_param_ops batadv_param_ops_ra = {
  1001. .set = batadv_param_set_ra,
  1002. .get = param_get_string,
  1003. };
  1004. static struct kparam_string batadv_param_string_ra = {
  1005. .maxlen = sizeof(batadv_routing_algo),
  1006. .string = batadv_routing_algo,
  1007. };
  1008. module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
  1009. 0644);
  1010. module_init(batadv_init);
  1011. module_exit(batadv_exit);
  1012. MODULE_LICENSE("GPL");
  1013. MODULE_AUTHOR(BATADV_DRIVER_AUTHOR);
  1014. MODULE_DESCRIPTION(BATADV_DRIVER_DESC);
  1015. MODULE_SUPPORTED_DEVICE(BATADV_DRIVER_DEVICE);
  1016. MODULE_VERSION(BATADV_SOURCE_VERSION);