main.c 32 KB

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