main.c 33 KB

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