main.c 32 KB

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