main.c 34 KB

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