distributed-arp-table.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /* Copyright (C) 2011-2013 B.A.T.M.A.N. contributors:
  2. *
  3. * Antonio Quartulli
  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/if_ether.h>
  20. #include <linux/if_arp.h>
  21. #include <net/arp.h>
  22. #include "main.h"
  23. #include "hash.h"
  24. #include "distributed-arp-table.h"
  25. #include "hard-interface.h"
  26. #include "originator.h"
  27. #include "send.h"
  28. #include "types.h"
  29. #include "translation-table.h"
  30. #include "unicast.h"
  31. static void batadv_dat_purge(struct work_struct *work);
  32. /**
  33. * batadv_dat_start_timer - initialise the DAT periodic worker
  34. * @bat_priv: the bat priv with all the soft interface information
  35. */
  36. static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
  37. {
  38. INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
  39. queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
  40. msecs_to_jiffies(10000));
  41. }
  42. /**
  43. * batadv_dat_entry_free_ref - decrement the dat_entry refcounter and possibly
  44. * free it
  45. * @dat_entry: the entry to free
  46. */
  47. static void batadv_dat_entry_free_ref(struct batadv_dat_entry *dat_entry)
  48. {
  49. if (atomic_dec_and_test(&dat_entry->refcount))
  50. kfree_rcu(dat_entry, rcu);
  51. }
  52. /**
  53. * batadv_dat_to_purge - check whether a dat_entry has to be purged or not
  54. * @dat_entry: the entry to check
  55. *
  56. * Returns true if the entry has to be purged now, false otherwise.
  57. */
  58. static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
  59. {
  60. return batadv_has_timed_out(dat_entry->last_update,
  61. BATADV_DAT_ENTRY_TIMEOUT);
  62. }
  63. /**
  64. * __batadv_dat_purge - delete entries from the DAT local storage
  65. * @bat_priv: the bat priv with all the soft interface information
  66. * @to_purge: function in charge to decide whether an entry has to be purged or
  67. * not. This function takes the dat_entry as argument and has to
  68. * returns a boolean value: true is the entry has to be deleted,
  69. * false otherwise
  70. *
  71. * Loops over each entry in the DAT local storage and deletes it if and only if
  72. * the to_purge function passed as argument returns true.
  73. */
  74. static void __batadv_dat_purge(struct batadv_priv *bat_priv,
  75. bool (*to_purge)(struct batadv_dat_entry *))
  76. {
  77. spinlock_t *list_lock; /* protects write access to the hash lists */
  78. struct batadv_dat_entry *dat_entry;
  79. struct hlist_node *node_tmp;
  80. struct hlist_head *head;
  81. uint32_t i;
  82. if (!bat_priv->dat.hash)
  83. return;
  84. for (i = 0; i < bat_priv->dat.hash->size; i++) {
  85. head = &bat_priv->dat.hash->table[i];
  86. list_lock = &bat_priv->dat.hash->list_locks[i];
  87. spin_lock_bh(list_lock);
  88. hlist_for_each_entry_safe(dat_entry, node_tmp, head,
  89. hash_entry) {
  90. /* if a helper function has been passed as parameter,
  91. * ask it if the entry has to be purged or not
  92. */
  93. if (to_purge && !to_purge(dat_entry))
  94. continue;
  95. hlist_del_rcu(&dat_entry->hash_entry);
  96. batadv_dat_entry_free_ref(dat_entry);
  97. }
  98. spin_unlock_bh(list_lock);
  99. }
  100. }
  101. /**
  102. * batadv_dat_purge - periodic task that deletes old entries from the local DAT
  103. * hash table
  104. * @work: kernel work struct
  105. */
  106. static void batadv_dat_purge(struct work_struct *work)
  107. {
  108. struct delayed_work *delayed_work;
  109. struct batadv_priv_dat *priv_dat;
  110. struct batadv_priv *bat_priv;
  111. delayed_work = container_of(work, struct delayed_work, work);
  112. priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
  113. bat_priv = container_of(priv_dat, struct batadv_priv, dat);
  114. __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
  115. batadv_dat_start_timer(bat_priv);
  116. }
  117. /**
  118. * batadv_compare_dat - comparing function used in the local DAT hash table
  119. * @node: node in the local table
  120. * @data2: second object to compare the node to
  121. *
  122. * Returns 1 if the two entries are the same, 0 otherwise.
  123. */
  124. static int batadv_compare_dat(const struct hlist_node *node, const void *data2)
  125. {
  126. const void *data1 = container_of(node, struct batadv_dat_entry,
  127. hash_entry);
  128. return (memcmp(data1, data2, sizeof(__be32)) == 0 ? 1 : 0);
  129. }
  130. /**
  131. * batadv_arp_hw_src - extract the hw_src field from an ARP packet
  132. * @skb: ARP packet
  133. * @hdr_size: size of the possible header before the ARP packet
  134. *
  135. * Returns the value of the hw_src field in the ARP packet.
  136. */
  137. static uint8_t *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
  138. {
  139. uint8_t *addr;
  140. addr = (uint8_t *)(skb->data + hdr_size);
  141. addr += ETH_HLEN + sizeof(struct arphdr);
  142. return addr;
  143. }
  144. /**
  145. * batadv_arp_ip_src - extract the ip_src field from an ARP packet
  146. * @skb: ARP packet
  147. * @hdr_size: size of the possible header before the ARP packet
  148. *
  149. * Returns the value of the ip_src field in the ARP packet.
  150. */
  151. static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
  152. {
  153. return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
  154. }
  155. /**
  156. * batadv_arp_hw_dst - extract the hw_dst field from an ARP packet
  157. * @skb: ARP packet
  158. * @hdr_size: size of the possible header before the ARP packet
  159. *
  160. * Returns the value of the hw_dst field in the ARP packet.
  161. */
  162. static uint8_t *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
  163. {
  164. return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
  165. }
  166. /**
  167. * batadv_arp_ip_dst - extract the ip_dst field from an ARP packet
  168. * @skb: ARP packet
  169. * @hdr_size: size of the possible header before the ARP packet
  170. *
  171. * Returns the value of the ip_dst field in the ARP packet.
  172. */
  173. static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
  174. {
  175. return *(__be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4);
  176. }
  177. /**
  178. * batadv_hash_dat - compute the hash value for an IP address
  179. * @data: data to hash
  180. * @size: size of the hash table
  181. *
  182. * Returns the selected index in the hash table for the given data.
  183. */
  184. static uint32_t batadv_hash_dat(const void *data, uint32_t size)
  185. {
  186. const unsigned char *key = data;
  187. uint32_t hash = 0;
  188. size_t i;
  189. for (i = 0; i < 4; i++) {
  190. hash += key[i];
  191. hash += (hash << 10);
  192. hash ^= (hash >> 6);
  193. }
  194. hash += (hash << 3);
  195. hash ^= (hash >> 11);
  196. hash += (hash << 15);
  197. return hash % size;
  198. }
  199. /**
  200. * batadv_dat_entry_hash_find - look for a given dat_entry in the local hash
  201. * table
  202. * @bat_priv: the bat priv with all the soft interface information
  203. * @ip: search key
  204. *
  205. * Returns the dat_entry if found, NULL otherwise.
  206. */
  207. static struct batadv_dat_entry *
  208. batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip)
  209. {
  210. struct hlist_head *head;
  211. struct batadv_dat_entry *dat_entry, *dat_entry_tmp = NULL;
  212. struct batadv_hashtable *hash = bat_priv->dat.hash;
  213. uint32_t index;
  214. if (!hash)
  215. return NULL;
  216. index = batadv_hash_dat(&ip, hash->size);
  217. head = &hash->table[index];
  218. rcu_read_lock();
  219. hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
  220. if (dat_entry->ip != ip)
  221. continue;
  222. if (!atomic_inc_not_zero(&dat_entry->refcount))
  223. continue;
  224. dat_entry_tmp = dat_entry;
  225. break;
  226. }
  227. rcu_read_unlock();
  228. return dat_entry_tmp;
  229. }
  230. /**
  231. * batadv_dat_entry_add - add a new dat entry or update it if already exists
  232. * @bat_priv: the bat priv with all the soft interface information
  233. * @ip: ipv4 to add/edit
  234. * @mac_addr: mac address to assign to the given ipv4
  235. */
  236. static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
  237. uint8_t *mac_addr)
  238. {
  239. struct batadv_dat_entry *dat_entry;
  240. int hash_added;
  241. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip);
  242. /* if this entry is already known, just update it */
  243. if (dat_entry) {
  244. if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
  245. memcpy(dat_entry->mac_addr, mac_addr, ETH_ALEN);
  246. dat_entry->last_update = jiffies;
  247. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  248. "Entry updated: %pI4 %pM\n", &dat_entry->ip,
  249. dat_entry->mac_addr);
  250. goto out;
  251. }
  252. dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
  253. if (!dat_entry)
  254. goto out;
  255. dat_entry->ip = ip;
  256. memcpy(dat_entry->mac_addr, mac_addr, ETH_ALEN);
  257. dat_entry->last_update = jiffies;
  258. atomic_set(&dat_entry->refcount, 2);
  259. hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
  260. batadv_hash_dat, &dat_entry->ip,
  261. &dat_entry->hash_entry);
  262. if (unlikely(hash_added != 0)) {
  263. /* remove the reference for the hash */
  264. batadv_dat_entry_free_ref(dat_entry);
  265. goto out;
  266. }
  267. batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM\n",
  268. &dat_entry->ip, dat_entry->mac_addr);
  269. out:
  270. if (dat_entry)
  271. batadv_dat_entry_free_ref(dat_entry);
  272. }
  273. #ifdef CONFIG_BATMAN_ADV_DEBUG
  274. /**
  275. * batadv_dbg_arp - print a debug message containing all the ARP packet details
  276. * @bat_priv: the bat priv with all the soft interface information
  277. * @skb: ARP packet
  278. * @type: ARP type
  279. * @hdr_size: size of the possible header before the ARP packet
  280. * @msg: message to print together with the debugging information
  281. */
  282. static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
  283. uint16_t type, int hdr_size, char *msg)
  284. {
  285. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  286. struct batadv_bcast_packet *bcast_pkt;
  287. uint8_t *orig_addr;
  288. __be32 ip_src, ip_dst;
  289. if (msg)
  290. batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
  291. ip_src = batadv_arp_ip_src(skb, hdr_size);
  292. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  293. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  294. "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
  295. batadv_arp_hw_src(skb, hdr_size), &ip_src,
  296. batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
  297. if (hdr_size == 0)
  298. return;
  299. unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  300. switch (unicast_4addr_packet->u.header.packet_type) {
  301. case BATADV_UNICAST:
  302. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  303. "* encapsulated within a UNICAST packet\n");
  304. break;
  305. case BATADV_UNICAST_4ADDR:
  306. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  307. "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
  308. unicast_4addr_packet->src);
  309. switch (unicast_4addr_packet->subtype) {
  310. case BATADV_P_DAT_DHT_PUT:
  311. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
  312. break;
  313. case BATADV_P_DAT_DHT_GET:
  314. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
  315. break;
  316. case BATADV_P_DAT_CACHE_REPLY:
  317. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  318. "* type: DAT_CACHE_REPLY\n");
  319. break;
  320. case BATADV_P_DATA:
  321. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
  322. break;
  323. default:
  324. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
  325. unicast_4addr_packet->u.header.packet_type);
  326. }
  327. break;
  328. case BATADV_BCAST:
  329. bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
  330. orig_addr = bcast_pkt->orig;
  331. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  332. "* encapsulated within a BCAST packet (src: %pM)\n",
  333. orig_addr);
  334. break;
  335. default:
  336. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  337. "* encapsulated within an unknown packet type (0x%x)\n",
  338. unicast_4addr_packet->u.header.packet_type);
  339. }
  340. }
  341. #else
  342. static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
  343. uint16_t type, int hdr_size, char *msg)
  344. {
  345. }
  346. #endif /* CONFIG_BATMAN_ADV_DEBUG */
  347. /**
  348. * batadv_is_orig_node_eligible - check whether a node can be a DHT candidate
  349. * @res: the array with the already selected candidates
  350. * @select: number of already selected candidates
  351. * @tmp_max: address of the currently evaluated node
  352. * @max: current round max address
  353. * @last_max: address of the last selected candidate
  354. * @candidate: orig_node under evaluation
  355. * @max_orig_node: last selected candidate
  356. *
  357. * Returns true if the node has been elected as next candidate or false
  358. * otherwise.
  359. */
  360. static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
  361. int select, batadv_dat_addr_t tmp_max,
  362. batadv_dat_addr_t max,
  363. batadv_dat_addr_t last_max,
  364. struct batadv_orig_node *candidate,
  365. struct batadv_orig_node *max_orig_node)
  366. {
  367. bool ret = false;
  368. int j;
  369. /* check if orig node candidate is running DAT */
  370. if (!(candidate->capabilities & BATADV_ORIG_CAPA_HAS_DAT))
  371. goto out;
  372. /* Check if this node has already been selected... */
  373. for (j = 0; j < select; j++)
  374. if (res[j].orig_node == candidate)
  375. break;
  376. /* ..and possibly skip it */
  377. if (j < select)
  378. goto out;
  379. /* sanity check: has it already been selected? This should not happen */
  380. if (tmp_max > last_max)
  381. goto out;
  382. /* check if during this iteration an originator with a closer dht
  383. * address has already been found
  384. */
  385. if (tmp_max < max)
  386. goto out;
  387. /* this is an hash collision with the temporary selected node. Choose
  388. * the one with the lowest address
  389. */
  390. if ((tmp_max == max) && max_orig_node &&
  391. (batadv_compare_eth(candidate->orig, max_orig_node->orig) > 0))
  392. goto out;
  393. ret = true;
  394. out:
  395. return ret;
  396. }
  397. /**
  398. * batadv_choose_next_candidate - select the next DHT candidate
  399. * @bat_priv: the bat priv with all the soft interface information
  400. * @cands: candidates array
  401. * @select: number of candidates already present in the array
  402. * @ip_key: key to look up in the DHT
  403. * @last_max: pointer where the address of the selected candidate will be saved
  404. */
  405. static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
  406. struct batadv_dat_candidate *cands,
  407. int select, batadv_dat_addr_t ip_key,
  408. batadv_dat_addr_t *last_max)
  409. {
  410. batadv_dat_addr_t max = 0, tmp_max = 0;
  411. struct batadv_orig_node *orig_node, *max_orig_node = NULL;
  412. struct batadv_hashtable *hash = bat_priv->orig_hash;
  413. struct hlist_head *head;
  414. int i;
  415. /* if no node is eligible as candidate, leave the candidate type as
  416. * NOT_FOUND
  417. */
  418. cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
  419. /* iterate over the originator list and find the node with the closest
  420. * dat_address which has not been selected yet
  421. */
  422. for (i = 0; i < hash->size; i++) {
  423. head = &hash->table[i];
  424. rcu_read_lock();
  425. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  426. /* the dht space is a ring using unsigned addresses */
  427. tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
  428. ip_key;
  429. if (!batadv_is_orig_node_eligible(cands, select,
  430. tmp_max, max,
  431. *last_max, orig_node,
  432. max_orig_node))
  433. continue;
  434. if (!atomic_inc_not_zero(&orig_node->refcount))
  435. continue;
  436. max = tmp_max;
  437. if (max_orig_node)
  438. batadv_orig_node_free_ref(max_orig_node);
  439. max_orig_node = orig_node;
  440. }
  441. rcu_read_unlock();
  442. }
  443. if (max_orig_node) {
  444. cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
  445. cands[select].orig_node = max_orig_node;
  446. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  447. "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
  448. select, max_orig_node->orig, max_orig_node->dat_addr,
  449. max);
  450. }
  451. *last_max = max;
  452. }
  453. /**
  454. * batadv_dat_select_candidates - select the nodes which the DHT message has to
  455. * be sent to
  456. * @bat_priv: the bat priv with all the soft interface information
  457. * @ip_dst: ipv4 to look up in the DHT
  458. *
  459. * An originator O is selected if and only if its DHT_ID value is one of three
  460. * closest values (from the LEFT, with wrap around if needed) then the hash
  461. * value of the key. ip_dst is the key.
  462. *
  463. * Returns the candidate array of size BATADV_DAT_CANDIDATE_NUM.
  464. */
  465. static struct batadv_dat_candidate *
  466. batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst)
  467. {
  468. int select;
  469. batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
  470. struct batadv_dat_candidate *res;
  471. if (!bat_priv->orig_hash)
  472. return NULL;
  473. res = kmalloc(BATADV_DAT_CANDIDATES_NUM * sizeof(*res), GFP_ATOMIC);
  474. if (!res)
  475. return NULL;
  476. ip_key = (batadv_dat_addr_t)batadv_hash_dat(&ip_dst,
  477. BATADV_DAT_ADDR_MAX);
  478. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  479. "dat_select_candidates(): IP=%pI4 hash(IP)=%u\n", &ip_dst,
  480. ip_key);
  481. for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
  482. batadv_choose_next_candidate(bat_priv, res, select, ip_key,
  483. &last_max);
  484. return res;
  485. }
  486. /**
  487. * batadv_dat_send_data - send a payload to the selected candidates
  488. * @bat_priv: the bat priv with all the soft interface information
  489. * @skb: payload to send
  490. * @ip: the DHT key
  491. * @packet_subtype: unicast4addr packet subtype to use
  492. *
  493. * This function copies the skb with pskb_copy() and is sent as unicast packet
  494. * to each of the selected candidates.
  495. *
  496. * Returns true if the packet is sent to at least one candidate, false
  497. * otherwise.
  498. */
  499. static bool batadv_dat_send_data(struct batadv_priv *bat_priv,
  500. struct sk_buff *skb, __be32 ip,
  501. int packet_subtype)
  502. {
  503. int i;
  504. bool ret = false;
  505. int send_status;
  506. struct batadv_neigh_node *neigh_node = NULL;
  507. struct sk_buff *tmp_skb;
  508. struct batadv_dat_candidate *cand;
  509. cand = batadv_dat_select_candidates(bat_priv, ip);
  510. if (!cand)
  511. goto out;
  512. batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
  513. for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
  514. if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
  515. continue;
  516. neigh_node = batadv_orig_node_get_router(cand[i].orig_node);
  517. if (!neigh_node)
  518. goto free_orig;
  519. tmp_skb = pskb_copy(skb, GFP_ATOMIC);
  520. if (!batadv_unicast_4addr_prepare_skb(bat_priv, tmp_skb,
  521. cand[i].orig_node,
  522. packet_subtype)) {
  523. kfree_skb(tmp_skb);
  524. goto free_neigh;
  525. }
  526. send_status = batadv_send_skb_packet(tmp_skb,
  527. neigh_node->if_incoming,
  528. neigh_node->addr);
  529. if (send_status == NET_XMIT_SUCCESS) {
  530. /* count the sent packet */
  531. switch (packet_subtype) {
  532. case BATADV_P_DAT_DHT_GET:
  533. batadv_inc_counter(bat_priv,
  534. BATADV_CNT_DAT_GET_TX);
  535. break;
  536. case BATADV_P_DAT_DHT_PUT:
  537. batadv_inc_counter(bat_priv,
  538. BATADV_CNT_DAT_PUT_TX);
  539. break;
  540. }
  541. /* packet sent to a candidate: return true */
  542. ret = true;
  543. }
  544. free_neigh:
  545. batadv_neigh_node_free_ref(neigh_node);
  546. free_orig:
  547. batadv_orig_node_free_ref(cand[i].orig_node);
  548. }
  549. out:
  550. kfree(cand);
  551. return ret;
  552. }
  553. /**
  554. * batadv_dat_tvlv_container_update - update the dat tvlv container after dat
  555. * setting change
  556. * @bat_priv: the bat priv with all the soft interface information
  557. */
  558. static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
  559. {
  560. char dat_mode;
  561. dat_mode = atomic_read(&bat_priv->distributed_arp_table);
  562. switch (dat_mode) {
  563. case 0:
  564. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  565. break;
  566. case 1:
  567. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
  568. NULL, 0);
  569. break;
  570. }
  571. }
  572. /**
  573. * batadv_dat_status_update - update the dat tvlv container after dat
  574. * setting change
  575. * @net_dev: the soft interface net device
  576. */
  577. void batadv_dat_status_update(struct net_device *net_dev)
  578. {
  579. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  580. batadv_dat_tvlv_container_update(bat_priv);
  581. }
  582. /**
  583. * batadv_gw_tvlv_ogm_handler_v1 - process incoming dat tvlv container
  584. * @bat_priv: the bat priv with all the soft interface information
  585. * @orig: the orig_node of the ogm
  586. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  587. * @tvlv_value: tvlv buffer containing the gateway data
  588. * @tvlv_value_len: tvlv buffer length
  589. */
  590. static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  591. struct batadv_orig_node *orig,
  592. uint8_t flags,
  593. void *tvlv_value,
  594. uint16_t tvlv_value_len)
  595. {
  596. if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
  597. orig->capabilities &= ~BATADV_ORIG_CAPA_HAS_DAT;
  598. else
  599. orig->capabilities |= BATADV_ORIG_CAPA_HAS_DAT;
  600. }
  601. /**
  602. * batadv_dat_hash_free - free the local DAT hash table
  603. * @bat_priv: the bat priv with all the soft interface information
  604. */
  605. static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
  606. {
  607. if (!bat_priv->dat.hash)
  608. return;
  609. __batadv_dat_purge(bat_priv, NULL);
  610. batadv_hash_destroy(bat_priv->dat.hash);
  611. bat_priv->dat.hash = NULL;
  612. }
  613. /**
  614. * batadv_dat_init - initialise the DAT internals
  615. * @bat_priv: the bat priv with all the soft interface information
  616. */
  617. int batadv_dat_init(struct batadv_priv *bat_priv)
  618. {
  619. if (bat_priv->dat.hash)
  620. return 0;
  621. bat_priv->dat.hash = batadv_hash_new(1024);
  622. if (!bat_priv->dat.hash)
  623. return -ENOMEM;
  624. batadv_dat_start_timer(bat_priv);
  625. batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
  626. NULL, BATADV_TVLV_DAT, 1,
  627. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  628. batadv_dat_tvlv_container_update(bat_priv);
  629. return 0;
  630. }
  631. /**
  632. * batadv_dat_free - free the DAT internals
  633. * @bat_priv: the bat priv with all the soft interface information
  634. */
  635. void batadv_dat_free(struct batadv_priv *bat_priv)
  636. {
  637. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  638. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  639. cancel_delayed_work_sync(&bat_priv->dat.work);
  640. batadv_dat_hash_free(bat_priv);
  641. }
  642. /**
  643. * batadv_dat_cache_seq_print_text - print the local DAT hash table
  644. * @seq: seq file to print on
  645. * @offset: not used
  646. */
  647. int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
  648. {
  649. struct net_device *net_dev = (struct net_device *)seq->private;
  650. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  651. struct batadv_hashtable *hash = bat_priv->dat.hash;
  652. struct batadv_dat_entry *dat_entry;
  653. struct batadv_hard_iface *primary_if;
  654. struct hlist_head *head;
  655. unsigned long last_seen_jiffies;
  656. int last_seen_msecs, last_seen_secs, last_seen_mins;
  657. uint32_t i;
  658. primary_if = batadv_seq_print_text_primary_if_get(seq);
  659. if (!primary_if)
  660. goto out;
  661. seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name);
  662. seq_printf(seq, " %-7s %-13s %5s\n", "IPv4", "MAC",
  663. "last-seen");
  664. for (i = 0; i < hash->size; i++) {
  665. head = &hash->table[i];
  666. rcu_read_lock();
  667. hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
  668. last_seen_jiffies = jiffies - dat_entry->last_update;
  669. last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
  670. last_seen_mins = last_seen_msecs / 60000;
  671. last_seen_msecs = last_seen_msecs % 60000;
  672. last_seen_secs = last_seen_msecs / 1000;
  673. seq_printf(seq, " * %15pI4 %14pM %6i:%02i\n",
  674. &dat_entry->ip, dat_entry->mac_addr,
  675. last_seen_mins, last_seen_secs);
  676. }
  677. rcu_read_unlock();
  678. }
  679. out:
  680. if (primary_if)
  681. batadv_hardif_free_ref(primary_if);
  682. return 0;
  683. }
  684. /**
  685. * batadv_arp_get_type - parse an ARP packet and gets the type
  686. * @bat_priv: the bat priv with all the soft interface information
  687. * @skb: packet to analyse
  688. * @hdr_size: size of the possible header before the ARP packet in the skb
  689. *
  690. * Returns the ARP type if the skb contains a valid ARP packet, 0 otherwise.
  691. */
  692. static uint16_t batadv_arp_get_type(struct batadv_priv *bat_priv,
  693. struct sk_buff *skb, int hdr_size)
  694. {
  695. struct arphdr *arphdr;
  696. struct ethhdr *ethhdr;
  697. __be32 ip_src, ip_dst;
  698. uint8_t *hw_src, *hw_dst;
  699. uint16_t type = 0;
  700. /* pull the ethernet header */
  701. if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
  702. goto out;
  703. ethhdr = (struct ethhdr *)(skb->data + hdr_size);
  704. if (ethhdr->h_proto != htons(ETH_P_ARP))
  705. goto out;
  706. /* pull the ARP payload */
  707. if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
  708. arp_hdr_len(skb->dev))))
  709. goto out;
  710. arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
  711. /* check whether the ARP packet carries a valid IP information */
  712. if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
  713. goto out;
  714. if (arphdr->ar_pro != htons(ETH_P_IP))
  715. goto out;
  716. if (arphdr->ar_hln != ETH_ALEN)
  717. goto out;
  718. if (arphdr->ar_pln != 4)
  719. goto out;
  720. /* Check for bad reply/request. If the ARP message is not sane, DAT
  721. * will simply ignore it
  722. */
  723. ip_src = batadv_arp_ip_src(skb, hdr_size);
  724. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  725. if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
  726. ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
  727. ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
  728. ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
  729. goto out;
  730. hw_src = batadv_arp_hw_src(skb, hdr_size);
  731. if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
  732. goto out;
  733. /* don't care about the destination MAC address in ARP requests */
  734. if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
  735. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  736. if (is_zero_ether_addr(hw_dst) ||
  737. is_multicast_ether_addr(hw_dst))
  738. goto out;
  739. }
  740. type = ntohs(arphdr->ar_op);
  741. out:
  742. return type;
  743. }
  744. /**
  745. * batadv_dat_snoop_outgoing_arp_request - snoop the ARP request and try to
  746. * answer using DAT
  747. * @bat_priv: the bat priv with all the soft interface information
  748. * @skb: packet to check
  749. *
  750. * Returns true if the message has been sent to the dht candidates, false
  751. * otherwise. In case of a positive return value the message has to be enqueued
  752. * to permit the fallback.
  753. */
  754. bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
  755. struct sk_buff *skb)
  756. {
  757. uint16_t type = 0;
  758. __be32 ip_dst, ip_src;
  759. uint8_t *hw_src;
  760. bool ret = false;
  761. struct batadv_dat_entry *dat_entry = NULL;
  762. struct sk_buff *skb_new;
  763. if (!atomic_read(&bat_priv->distributed_arp_table))
  764. goto out;
  765. type = batadv_arp_get_type(bat_priv, skb, 0);
  766. /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
  767. * message to the selected DHT candidates
  768. */
  769. if (type != ARPOP_REQUEST)
  770. goto out;
  771. batadv_dbg_arp(bat_priv, skb, type, 0, "Parsing outgoing ARP REQUEST");
  772. ip_src = batadv_arp_ip_src(skb, 0);
  773. hw_src = batadv_arp_hw_src(skb, 0);
  774. ip_dst = batadv_arp_ip_dst(skb, 0);
  775. batadv_dat_entry_add(bat_priv, ip_src, hw_src);
  776. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst);
  777. if (dat_entry) {
  778. /* If the ARP request is destined for a local client the local
  779. * client will answer itself. DAT would only generate a
  780. * duplicate packet.
  781. *
  782. * Moreover, if the soft-interface is enslaved into a bridge, an
  783. * additional DAT answer may trigger kernel warnings about
  784. * a packet coming from the wrong port.
  785. */
  786. if (batadv_is_my_client(bat_priv, dat_entry->mac_addr)) {
  787. ret = true;
  788. goto out;
  789. }
  790. skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src,
  791. bat_priv->soft_iface, ip_dst, hw_src,
  792. dat_entry->mac_addr, hw_src);
  793. if (!skb_new)
  794. goto out;
  795. skb_reset_mac_header(skb_new);
  796. skb_new->protocol = eth_type_trans(skb_new,
  797. bat_priv->soft_iface);
  798. bat_priv->stats.rx_packets++;
  799. bat_priv->stats.rx_bytes += skb->len + ETH_HLEN;
  800. bat_priv->soft_iface->last_rx = jiffies;
  801. netif_rx(skb_new);
  802. batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
  803. ret = true;
  804. } else {
  805. /* Send the request to the DHT */
  806. ret = batadv_dat_send_data(bat_priv, skb, ip_dst,
  807. BATADV_P_DAT_DHT_GET);
  808. }
  809. out:
  810. if (dat_entry)
  811. batadv_dat_entry_free_ref(dat_entry);
  812. return ret;
  813. }
  814. /**
  815. * batadv_dat_snoop_incoming_arp_request - snoop the ARP request and try to
  816. * answer using the local DAT storage
  817. * @bat_priv: the bat priv with all the soft interface information
  818. * @skb: packet to check
  819. * @hdr_size: size of the encapsulation header
  820. *
  821. * Returns true if the request has been answered, false otherwise.
  822. */
  823. bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
  824. struct sk_buff *skb, int hdr_size)
  825. {
  826. uint16_t type;
  827. __be32 ip_src, ip_dst;
  828. uint8_t *hw_src;
  829. struct sk_buff *skb_new;
  830. struct batadv_dat_entry *dat_entry = NULL;
  831. bool ret = false;
  832. int err;
  833. if (!atomic_read(&bat_priv->distributed_arp_table))
  834. goto out;
  835. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  836. if (type != ARPOP_REQUEST)
  837. goto out;
  838. hw_src = batadv_arp_hw_src(skb, hdr_size);
  839. ip_src = batadv_arp_ip_src(skb, hdr_size);
  840. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  841. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  842. "Parsing incoming ARP REQUEST");
  843. batadv_dat_entry_add(bat_priv, ip_src, hw_src);
  844. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst);
  845. if (!dat_entry)
  846. goto out;
  847. skb_new = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_src,
  848. bat_priv->soft_iface, ip_dst, hw_src,
  849. dat_entry->mac_addr, hw_src);
  850. if (!skb_new)
  851. goto out;
  852. /* To preserve backwards compatibility, the node has choose the outgoing
  853. * format based on the incoming request packet type. The assumption is
  854. * that a node not using the 4addr packet format doesn't support it.
  855. */
  856. if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
  857. err = batadv_unicast_4addr_send_skb(bat_priv, skb_new,
  858. BATADV_P_DAT_CACHE_REPLY);
  859. else
  860. err = batadv_unicast_send_skb(bat_priv, skb_new);
  861. if (!err) {
  862. batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
  863. ret = true;
  864. }
  865. out:
  866. if (dat_entry)
  867. batadv_dat_entry_free_ref(dat_entry);
  868. if (ret)
  869. kfree_skb(skb);
  870. return ret;
  871. }
  872. /**
  873. * batadv_dat_snoop_outgoing_arp_reply - snoop the ARP reply and fill the DHT
  874. * @bat_priv: the bat priv with all the soft interface information
  875. * @skb: packet to check
  876. */
  877. void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
  878. struct sk_buff *skb)
  879. {
  880. uint16_t type;
  881. __be32 ip_src, ip_dst;
  882. uint8_t *hw_src, *hw_dst;
  883. if (!atomic_read(&bat_priv->distributed_arp_table))
  884. return;
  885. type = batadv_arp_get_type(bat_priv, skb, 0);
  886. if (type != ARPOP_REPLY)
  887. return;
  888. batadv_dbg_arp(bat_priv, skb, type, 0, "Parsing outgoing ARP REPLY");
  889. hw_src = batadv_arp_hw_src(skb, 0);
  890. ip_src = batadv_arp_ip_src(skb, 0);
  891. hw_dst = batadv_arp_hw_dst(skb, 0);
  892. ip_dst = batadv_arp_ip_dst(skb, 0);
  893. batadv_dat_entry_add(bat_priv, ip_src, hw_src);
  894. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst);
  895. /* Send the ARP reply to the candidates for both the IP addresses that
  896. * the node obtained from the ARP reply
  897. */
  898. batadv_dat_send_data(bat_priv, skb, ip_src, BATADV_P_DAT_DHT_PUT);
  899. batadv_dat_send_data(bat_priv, skb, ip_dst, BATADV_P_DAT_DHT_PUT);
  900. }
  901. /**
  902. * batadv_dat_snoop_incoming_arp_reply - snoop the ARP reply and fill the local
  903. * DAT storage only
  904. * @bat_priv: the bat priv with all the soft interface information
  905. * @skb: packet to check
  906. * @hdr_size: size of the encapsulation header
  907. */
  908. bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
  909. struct sk_buff *skb, int hdr_size)
  910. {
  911. uint16_t type;
  912. __be32 ip_src, ip_dst;
  913. uint8_t *hw_src, *hw_dst;
  914. bool ret = false;
  915. if (!atomic_read(&bat_priv->distributed_arp_table))
  916. goto out;
  917. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  918. if (type != ARPOP_REPLY)
  919. goto out;
  920. batadv_dbg_arp(bat_priv, skb, type, hdr_size,
  921. "Parsing incoming ARP REPLY");
  922. hw_src = batadv_arp_hw_src(skb, hdr_size);
  923. ip_src = batadv_arp_ip_src(skb, hdr_size);
  924. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  925. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  926. /* Update our internal cache with both the IP addresses the node got
  927. * within the ARP reply
  928. */
  929. batadv_dat_entry_add(bat_priv, ip_src, hw_src);
  930. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst);
  931. /* if this REPLY is directed to a client of mine, let's deliver the
  932. * packet to the interface
  933. */
  934. ret = !batadv_is_my_client(bat_priv, hw_dst);
  935. out:
  936. if (ret)
  937. kfree_skb(skb);
  938. /* if ret == false -> packet has to be delivered to the interface */
  939. return ret;
  940. }
  941. /**
  942. * batadv_dat_drop_broadcast_packet - check if an ARP request has to be dropped
  943. * (because the node has already obtained the reply via DAT) or not
  944. * @bat_priv: the bat priv with all the soft interface information
  945. * @forw_packet: the broadcast packet
  946. *
  947. * Returns true if the node can drop the packet, false otherwise.
  948. */
  949. bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
  950. struct batadv_forw_packet *forw_packet)
  951. {
  952. uint16_t type;
  953. __be32 ip_dst;
  954. struct batadv_dat_entry *dat_entry = NULL;
  955. bool ret = false;
  956. const size_t bcast_len = sizeof(struct batadv_bcast_packet);
  957. if (!atomic_read(&bat_priv->distributed_arp_table))
  958. goto out;
  959. /* If this packet is an ARP_REQUEST and the node already has the
  960. * information that it is going to ask, then the packet can be dropped
  961. */
  962. if (forw_packet->num_packets)
  963. goto out;
  964. type = batadv_arp_get_type(bat_priv, forw_packet->skb, bcast_len);
  965. if (type != ARPOP_REQUEST)
  966. goto out;
  967. ip_dst = batadv_arp_ip_dst(forw_packet->skb, bcast_len);
  968. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst);
  969. /* check if the node already got this entry */
  970. if (!dat_entry) {
  971. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  972. "ARP Request for %pI4: fallback\n", &ip_dst);
  973. goto out;
  974. }
  975. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  976. "ARP Request for %pI4: fallback prevented\n", &ip_dst);
  977. ret = true;
  978. out:
  979. if (dat_entry)
  980. batadv_dat_entry_free_ref(dat_entry);
  981. return ret;
  982. }