main.c 36 KB

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