distributed-arp-table.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Antonio Quartulli
  5. */
  6. #include "distributed-arp-table.h"
  7. #include "main.h"
  8. #include <linux/unaligned.h>
  9. #include <linux/atomic.h>
  10. #include <linux/bitops.h>
  11. #include <linux/byteorder/generic.h>
  12. #include <linux/container_of.h>
  13. #include <linux/errno.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/gfp.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/if_ether.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/in.h>
  20. #include <linux/ip.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/kref.h>
  23. #include <linux/list.h>
  24. #include <linux/netlink.h>
  25. #include <linux/rculist.h>
  26. #include <linux/rcupdate.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/stddef.h>
  31. #include <linux/string.h>
  32. #include <linux/udp.h>
  33. #include <linux/workqueue.h>
  34. #include <net/arp.h>
  35. #include <net/genetlink.h>
  36. #include <net/netlink.h>
  37. #include <net/sock.h>
  38. #include <uapi/linux/batman_adv.h>
  39. #include "bridge_loop_avoidance.h"
  40. #include "hard-interface.h"
  41. #include "hash.h"
  42. #include "log.h"
  43. #include "netlink.h"
  44. #include "originator.h"
  45. #include "send.h"
  46. #include "soft-interface.h"
  47. #include "translation-table.h"
  48. #include "tvlv.h"
  49. enum batadv_bootpop {
  50. BATADV_BOOTREPLY = 2,
  51. };
  52. enum batadv_boothtype {
  53. BATADV_HTYPE_ETHERNET = 1,
  54. };
  55. enum batadv_dhcpoptioncode {
  56. BATADV_DHCP_OPT_PAD = 0,
  57. BATADV_DHCP_OPT_MSG_TYPE = 53,
  58. BATADV_DHCP_OPT_END = 255,
  59. };
  60. enum batadv_dhcptype {
  61. BATADV_DHCPACK = 5,
  62. };
  63. /* { 99, 130, 83, 99 } */
  64. #define BATADV_DHCP_MAGIC 1669485411
  65. struct batadv_dhcp_packet {
  66. __u8 op;
  67. __u8 htype;
  68. __u8 hlen;
  69. __u8 hops;
  70. __be32 xid;
  71. __be16 secs;
  72. __be16 flags;
  73. __be32 ciaddr;
  74. __be32 yiaddr;
  75. __be32 siaddr;
  76. __be32 giaddr;
  77. __u8 chaddr[16];
  78. __u8 sname[64];
  79. __u8 file[128];
  80. __be32 magic;
  81. /* __u8 options[]; */
  82. };
  83. #define BATADV_DHCP_YIADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->yiaddr)
  84. #define BATADV_DHCP_CHADDR_LEN sizeof(((struct batadv_dhcp_packet *)0)->chaddr)
  85. static void batadv_dat_purge(struct work_struct *work);
  86. /**
  87. * batadv_dat_start_timer() - initialise the DAT periodic worker
  88. * @bat_priv: the bat priv with all the soft interface information
  89. */
  90. static void batadv_dat_start_timer(struct batadv_priv *bat_priv)
  91. {
  92. queue_delayed_work(batadv_event_workqueue, &bat_priv->dat.work,
  93. msecs_to_jiffies(10000));
  94. }
  95. /**
  96. * batadv_dat_entry_release() - release dat_entry from lists and queue for free
  97. * after rcu grace period
  98. * @ref: kref pointer of the dat_entry
  99. */
  100. static void batadv_dat_entry_release(struct kref *ref)
  101. {
  102. struct batadv_dat_entry *dat_entry;
  103. dat_entry = container_of(ref, struct batadv_dat_entry, refcount);
  104. kfree_rcu(dat_entry, rcu);
  105. }
  106. /**
  107. * batadv_dat_entry_put() - decrement the dat_entry refcounter and possibly
  108. * release it
  109. * @dat_entry: dat_entry to be free'd
  110. */
  111. static void batadv_dat_entry_put(struct batadv_dat_entry *dat_entry)
  112. {
  113. if (!dat_entry)
  114. return;
  115. kref_put(&dat_entry->refcount, batadv_dat_entry_release);
  116. }
  117. /**
  118. * batadv_dat_to_purge() - check whether a dat_entry has to be purged or not
  119. * @dat_entry: the entry to check
  120. *
  121. * Return: true if the entry has to be purged now, false otherwise.
  122. */
  123. static bool batadv_dat_to_purge(struct batadv_dat_entry *dat_entry)
  124. {
  125. return batadv_has_timed_out(dat_entry->last_update,
  126. BATADV_DAT_ENTRY_TIMEOUT);
  127. }
  128. /**
  129. * __batadv_dat_purge() - delete entries from the DAT local storage
  130. * @bat_priv: the bat priv with all the soft interface information
  131. * @to_purge: function in charge to decide whether an entry has to be purged or
  132. * not. This function takes the dat_entry as argument and has to
  133. * returns a boolean value: true is the entry has to be deleted,
  134. * false otherwise
  135. *
  136. * Loops over each entry in the DAT local storage and deletes it if and only if
  137. * the to_purge function passed as argument returns true.
  138. */
  139. static void __batadv_dat_purge(struct batadv_priv *bat_priv,
  140. bool (*to_purge)(struct batadv_dat_entry *))
  141. {
  142. spinlock_t *list_lock; /* protects write access to the hash lists */
  143. struct batadv_dat_entry *dat_entry;
  144. struct hlist_node *node_tmp;
  145. struct hlist_head *head;
  146. u32 i;
  147. if (!bat_priv->dat.hash)
  148. return;
  149. for (i = 0; i < bat_priv->dat.hash->size; i++) {
  150. head = &bat_priv->dat.hash->table[i];
  151. list_lock = &bat_priv->dat.hash->list_locks[i];
  152. spin_lock_bh(list_lock);
  153. hlist_for_each_entry_safe(dat_entry, node_tmp, head,
  154. hash_entry) {
  155. /* if a helper function has been passed as parameter,
  156. * ask it if the entry has to be purged or not
  157. */
  158. if (to_purge && !to_purge(dat_entry))
  159. continue;
  160. hlist_del_rcu(&dat_entry->hash_entry);
  161. batadv_dat_entry_put(dat_entry);
  162. }
  163. spin_unlock_bh(list_lock);
  164. }
  165. }
  166. /**
  167. * batadv_dat_purge() - periodic task that deletes old entries from the local
  168. * DAT hash table
  169. * @work: kernel work struct
  170. */
  171. static void batadv_dat_purge(struct work_struct *work)
  172. {
  173. struct delayed_work *delayed_work;
  174. struct batadv_priv_dat *priv_dat;
  175. struct batadv_priv *bat_priv;
  176. delayed_work = to_delayed_work(work);
  177. priv_dat = container_of(delayed_work, struct batadv_priv_dat, work);
  178. bat_priv = container_of(priv_dat, struct batadv_priv, dat);
  179. __batadv_dat_purge(bat_priv, batadv_dat_to_purge);
  180. batadv_dat_start_timer(bat_priv);
  181. }
  182. /**
  183. * batadv_compare_dat() - comparing function used in the local DAT hash table
  184. * @node: node in the local table
  185. * @data2: second object to compare the node to
  186. *
  187. * Return: true if the two entries are the same, false otherwise.
  188. */
  189. static bool batadv_compare_dat(const struct hlist_node *node, const void *data2)
  190. {
  191. const void *data1 = container_of(node, struct batadv_dat_entry,
  192. hash_entry);
  193. return memcmp(data1, data2, sizeof(__be32)) == 0;
  194. }
  195. /**
  196. * batadv_arp_hw_src() - extract the hw_src field from an ARP packet
  197. * @skb: ARP packet
  198. * @hdr_size: size of the possible header before the ARP packet
  199. *
  200. * Return: the value of the hw_src field in the ARP packet.
  201. */
  202. static u8 *batadv_arp_hw_src(struct sk_buff *skb, int hdr_size)
  203. {
  204. u8 *addr;
  205. addr = (u8 *)(skb->data + hdr_size);
  206. addr += ETH_HLEN + sizeof(struct arphdr);
  207. return addr;
  208. }
  209. /**
  210. * batadv_arp_ip_src() - extract the ip_src field from an ARP packet
  211. * @skb: ARP packet
  212. * @hdr_size: size of the possible header before the ARP packet
  213. *
  214. * Return: the value of the ip_src field in the ARP packet.
  215. */
  216. static __be32 batadv_arp_ip_src(struct sk_buff *skb, int hdr_size)
  217. {
  218. return *(__force __be32 *)(batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN);
  219. }
  220. /**
  221. * batadv_arp_hw_dst() - extract the hw_dst field from an ARP packet
  222. * @skb: ARP packet
  223. * @hdr_size: size of the possible header before the ARP packet
  224. *
  225. * Return: the value of the hw_dst field in the ARP packet.
  226. */
  227. static u8 *batadv_arp_hw_dst(struct sk_buff *skb, int hdr_size)
  228. {
  229. return batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN + 4;
  230. }
  231. /**
  232. * batadv_arp_ip_dst() - extract the ip_dst field from an ARP packet
  233. * @skb: ARP packet
  234. * @hdr_size: size of the possible header before the ARP packet
  235. *
  236. * Return: the value of the ip_dst field in the ARP packet.
  237. */
  238. static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
  239. {
  240. u8 *dst = batadv_arp_hw_src(skb, hdr_size) + ETH_ALEN * 2 + 4;
  241. return *(__force __be32 *)dst;
  242. }
  243. /**
  244. * batadv_hash_dat() - compute the hash value for an IP address
  245. * @data: data to hash
  246. * @size: size of the hash table
  247. *
  248. * Return: the selected index in the hash table for the given data.
  249. */
  250. static u32 batadv_hash_dat(const void *data, u32 size)
  251. {
  252. u32 hash = 0;
  253. const struct batadv_dat_entry *dat = data;
  254. const unsigned char *key;
  255. __be16 vid;
  256. u32 i;
  257. key = (__force const unsigned char *)&dat->ip;
  258. for (i = 0; i < sizeof(dat->ip); i++) {
  259. hash += key[i];
  260. hash += (hash << 10);
  261. hash ^= (hash >> 6);
  262. }
  263. vid = htons(dat->vid);
  264. key = (__force const unsigned char *)&vid;
  265. for (i = 0; i < sizeof(dat->vid); i++) {
  266. hash += key[i];
  267. hash += (hash << 10);
  268. hash ^= (hash >> 6);
  269. }
  270. hash += (hash << 3);
  271. hash ^= (hash >> 11);
  272. hash += (hash << 15);
  273. return hash % size;
  274. }
  275. /**
  276. * batadv_dat_entry_hash_find() - look for a given dat_entry in the local hash
  277. * table
  278. * @bat_priv: the bat priv with all the soft interface information
  279. * @ip: search key
  280. * @vid: VLAN identifier
  281. *
  282. * Return: the dat_entry if found, NULL otherwise.
  283. */
  284. static struct batadv_dat_entry *
  285. batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
  286. unsigned short vid)
  287. {
  288. struct hlist_head *head;
  289. struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
  290. struct batadv_hashtable *hash = bat_priv->dat.hash;
  291. u32 index;
  292. if (!hash)
  293. return NULL;
  294. to_find.ip = ip;
  295. to_find.vid = vid;
  296. index = batadv_hash_dat(&to_find, hash->size);
  297. head = &hash->table[index];
  298. rcu_read_lock();
  299. hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
  300. if (dat_entry->ip != ip)
  301. continue;
  302. if (!kref_get_unless_zero(&dat_entry->refcount))
  303. continue;
  304. dat_entry_tmp = dat_entry;
  305. break;
  306. }
  307. rcu_read_unlock();
  308. return dat_entry_tmp;
  309. }
  310. /**
  311. * batadv_dat_entry_add() - add a new dat entry or update it if already exists
  312. * @bat_priv: the bat priv with all the soft interface information
  313. * @ip: ipv4 to add/edit
  314. * @mac_addr: mac address to assign to the given ipv4
  315. * @vid: VLAN identifier
  316. */
  317. static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
  318. u8 *mac_addr, unsigned short vid)
  319. {
  320. struct batadv_dat_entry *dat_entry;
  321. int hash_added;
  322. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
  323. /* if this entry is already known, just update it */
  324. if (dat_entry) {
  325. if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
  326. ether_addr_copy(dat_entry->mac_addr, mac_addr);
  327. dat_entry->last_update = jiffies;
  328. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  329. "Entry updated: %pI4 %pM (vid: %d)\n",
  330. &dat_entry->ip, dat_entry->mac_addr,
  331. batadv_print_vid(vid));
  332. goto out;
  333. }
  334. dat_entry = kmalloc(sizeof(*dat_entry), GFP_ATOMIC);
  335. if (!dat_entry)
  336. goto out;
  337. dat_entry->ip = ip;
  338. dat_entry->vid = vid;
  339. ether_addr_copy(dat_entry->mac_addr, mac_addr);
  340. dat_entry->last_update = jiffies;
  341. kref_init(&dat_entry->refcount);
  342. kref_get(&dat_entry->refcount);
  343. hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
  344. batadv_hash_dat, dat_entry,
  345. &dat_entry->hash_entry);
  346. if (unlikely(hash_added != 0)) {
  347. /* remove the reference for the hash */
  348. batadv_dat_entry_put(dat_entry);
  349. goto out;
  350. }
  351. batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
  352. &dat_entry->ip, dat_entry->mac_addr, batadv_print_vid(vid));
  353. out:
  354. batadv_dat_entry_put(dat_entry);
  355. }
  356. #ifdef CONFIG_BATMAN_ADV_DEBUG
  357. /**
  358. * batadv_dbg_arp() - print a debug message containing all the ARP packet
  359. * details
  360. * @bat_priv: the bat priv with all the soft interface information
  361. * @skb: ARP packet
  362. * @hdr_size: size of the possible header before the ARP packet
  363. * @msg: message to print together with the debugging information
  364. */
  365. static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
  366. int hdr_size, char *msg)
  367. {
  368. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  369. struct batadv_bcast_packet *bcast_pkt;
  370. u8 *orig_addr;
  371. __be32 ip_src, ip_dst;
  372. if (msg)
  373. batadv_dbg(BATADV_DBG_DAT, bat_priv, "%s\n", msg);
  374. ip_src = batadv_arp_ip_src(skb, hdr_size);
  375. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  376. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  377. "ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]\n",
  378. batadv_arp_hw_src(skb, hdr_size), &ip_src,
  379. batadv_arp_hw_dst(skb, hdr_size), &ip_dst);
  380. if (hdr_size < sizeof(struct batadv_unicast_packet))
  381. return;
  382. unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  383. switch (unicast_4addr_packet->u.packet_type) {
  384. case BATADV_UNICAST:
  385. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  386. "* encapsulated within a UNICAST packet\n");
  387. break;
  388. case BATADV_UNICAST_4ADDR:
  389. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  390. "* encapsulated within a UNICAST_4ADDR packet (src: %pM)\n",
  391. unicast_4addr_packet->src);
  392. switch (unicast_4addr_packet->subtype) {
  393. case BATADV_P_DAT_DHT_PUT:
  394. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_PUT\n");
  395. break;
  396. case BATADV_P_DAT_DHT_GET:
  397. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DAT_DHT_GET\n");
  398. break;
  399. case BATADV_P_DAT_CACHE_REPLY:
  400. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  401. "* type: DAT_CACHE_REPLY\n");
  402. break;
  403. case BATADV_P_DATA:
  404. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: DATA\n");
  405. break;
  406. default:
  407. batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
  408. unicast_4addr_packet->u.packet_type);
  409. }
  410. break;
  411. case BATADV_BCAST:
  412. bcast_pkt = (struct batadv_bcast_packet *)unicast_4addr_packet;
  413. orig_addr = bcast_pkt->orig;
  414. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  415. "* encapsulated within a BCAST packet (src: %pM)\n",
  416. orig_addr);
  417. break;
  418. default:
  419. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  420. "* encapsulated within an unknown packet type (0x%x)\n",
  421. unicast_4addr_packet->u.packet_type);
  422. }
  423. }
  424. #else
  425. static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
  426. int hdr_size, char *msg)
  427. {
  428. }
  429. #endif /* CONFIG_BATMAN_ADV_DEBUG */
  430. /**
  431. * batadv_is_orig_node_eligible() - check whether a node can be a DHT candidate
  432. * @res: the array with the already selected candidates
  433. * @select: number of already selected candidates
  434. * @tmp_max: address of the currently evaluated node
  435. * @max: current round max address
  436. * @last_max: address of the last selected candidate
  437. * @candidate: orig_node under evaluation
  438. * @max_orig_node: last selected candidate
  439. *
  440. * Return: true if the node has been elected as next candidate or false
  441. * otherwise.
  442. */
  443. static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
  444. int select, batadv_dat_addr_t tmp_max,
  445. batadv_dat_addr_t max,
  446. batadv_dat_addr_t last_max,
  447. struct batadv_orig_node *candidate,
  448. struct batadv_orig_node *max_orig_node)
  449. {
  450. bool ret = false;
  451. int j;
  452. /* check if orig node candidate is running DAT */
  453. if (!test_bit(BATADV_ORIG_CAPA_HAS_DAT, &candidate->capabilities))
  454. goto out;
  455. /* Check if this node has already been selected... */
  456. for (j = 0; j < select; j++)
  457. if (res[j].orig_node == candidate)
  458. break;
  459. /* ..and possibly skip it */
  460. if (j < select)
  461. goto out;
  462. /* sanity check: has it already been selected? This should not happen */
  463. if (tmp_max > last_max)
  464. goto out;
  465. /* check if during this iteration an originator with a closer dht
  466. * address has already been found
  467. */
  468. if (tmp_max < max)
  469. goto out;
  470. /* this is an hash collision with the temporary selected node. Choose
  471. * the one with the lowest address
  472. */
  473. if (tmp_max == max && max_orig_node &&
  474. batadv_compare_eth(candidate->orig, max_orig_node->orig))
  475. goto out;
  476. ret = true;
  477. out:
  478. return ret;
  479. }
  480. /**
  481. * batadv_choose_next_candidate() - select the next DHT candidate
  482. * @bat_priv: the bat priv with all the soft interface information
  483. * @cands: candidates array
  484. * @select: number of candidates already present in the array
  485. * @ip_key: key to look up in the DHT
  486. * @last_max: pointer where the address of the selected candidate will be saved
  487. */
  488. static void batadv_choose_next_candidate(struct batadv_priv *bat_priv,
  489. struct batadv_dat_candidate *cands,
  490. int select, batadv_dat_addr_t ip_key,
  491. batadv_dat_addr_t *last_max)
  492. {
  493. batadv_dat_addr_t max = 0;
  494. batadv_dat_addr_t tmp_max = 0;
  495. struct batadv_orig_node *orig_node, *max_orig_node = NULL;
  496. struct batadv_hashtable *hash = bat_priv->orig_hash;
  497. struct hlist_head *head;
  498. int i;
  499. /* if no node is eligible as candidate, leave the candidate type as
  500. * NOT_FOUND
  501. */
  502. cands[select].type = BATADV_DAT_CANDIDATE_NOT_FOUND;
  503. /* iterate over the originator list and find the node with the closest
  504. * dat_address which has not been selected yet
  505. */
  506. for (i = 0; i < hash->size; i++) {
  507. head = &hash->table[i];
  508. rcu_read_lock();
  509. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  510. /* the dht space is a ring using unsigned addresses */
  511. tmp_max = BATADV_DAT_ADDR_MAX - orig_node->dat_addr +
  512. ip_key;
  513. if (!batadv_is_orig_node_eligible(cands, select,
  514. tmp_max, max,
  515. *last_max, orig_node,
  516. max_orig_node))
  517. continue;
  518. if (!kref_get_unless_zero(&orig_node->refcount))
  519. continue;
  520. max = tmp_max;
  521. batadv_orig_node_put(max_orig_node);
  522. max_orig_node = orig_node;
  523. }
  524. rcu_read_unlock();
  525. }
  526. if (max_orig_node) {
  527. cands[select].type = BATADV_DAT_CANDIDATE_ORIG;
  528. cands[select].orig_node = max_orig_node;
  529. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  530. "dat_select_candidates() %d: selected %pM addr=%u dist=%u\n",
  531. select, max_orig_node->orig, max_orig_node->dat_addr,
  532. max);
  533. }
  534. *last_max = max;
  535. }
  536. /**
  537. * batadv_dat_select_candidates() - select the nodes which the DHT message has
  538. * to be sent to
  539. * @bat_priv: the bat priv with all the soft interface information
  540. * @ip_dst: ipv4 to look up in the DHT
  541. * @vid: VLAN identifier
  542. *
  543. * An originator O is selected if and only if its DHT_ID value is one of three
  544. * closest values (from the LEFT, with wrap around if needed) then the hash
  545. * value of the key. ip_dst is the key.
  546. *
  547. * Return: the candidate array of size BATADV_DAT_CANDIDATE_NUM.
  548. */
  549. static struct batadv_dat_candidate *
  550. batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,
  551. unsigned short vid)
  552. {
  553. int select;
  554. batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
  555. struct batadv_dat_candidate *res;
  556. struct batadv_dat_entry dat;
  557. if (!bat_priv->orig_hash)
  558. return NULL;
  559. res = kmalloc_array(BATADV_DAT_CANDIDATES_NUM, sizeof(*res),
  560. GFP_ATOMIC);
  561. if (!res)
  562. return NULL;
  563. dat.ip = ip_dst;
  564. dat.vid = vid;
  565. ip_key = (batadv_dat_addr_t)batadv_hash_dat(&dat,
  566. BATADV_DAT_ADDR_MAX);
  567. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  568. "%s(): IP=%pI4 hash(IP)=%u\n", __func__, &ip_dst,
  569. ip_key);
  570. for (select = 0; select < BATADV_DAT_CANDIDATES_NUM; select++)
  571. batadv_choose_next_candidate(bat_priv, res, select, ip_key,
  572. &last_max);
  573. return res;
  574. }
  575. /**
  576. * batadv_dat_forward_data() - copy and send payload to the selected candidates
  577. * @bat_priv: the bat priv with all the soft interface information
  578. * @skb: payload to send
  579. * @ip: the DHT key
  580. * @vid: VLAN identifier
  581. * @packet_subtype: unicast4addr packet subtype to use
  582. *
  583. * This function copies the skb with pskb_copy() and is sent as a unicast packet
  584. * to each of the selected candidates.
  585. *
  586. * Return: true if the packet is sent to at least one candidate, false
  587. * otherwise.
  588. */
  589. static bool batadv_dat_forward_data(struct batadv_priv *bat_priv,
  590. struct sk_buff *skb, __be32 ip,
  591. unsigned short vid, int packet_subtype)
  592. {
  593. int i;
  594. bool ret = false;
  595. int send_status;
  596. struct batadv_neigh_node *neigh_node = NULL;
  597. struct sk_buff *tmp_skb;
  598. struct batadv_dat_candidate *cand;
  599. cand = batadv_dat_select_candidates(bat_priv, ip, vid);
  600. if (!cand)
  601. return ret;
  602. batadv_dbg(BATADV_DBG_DAT, bat_priv, "DHT_SEND for %pI4\n", &ip);
  603. for (i = 0; i < BATADV_DAT_CANDIDATES_NUM; i++) {
  604. if (cand[i].type == BATADV_DAT_CANDIDATE_NOT_FOUND)
  605. continue;
  606. neigh_node = batadv_orig_router_get(cand[i].orig_node,
  607. BATADV_IF_DEFAULT);
  608. if (!neigh_node)
  609. goto free_orig;
  610. tmp_skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
  611. if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
  612. cand[i].orig_node,
  613. packet_subtype)) {
  614. kfree_skb(tmp_skb);
  615. goto free_neigh;
  616. }
  617. send_status = batadv_send_unicast_skb(tmp_skb, neigh_node);
  618. if (send_status == NET_XMIT_SUCCESS) {
  619. /* count the sent packet */
  620. switch (packet_subtype) {
  621. case BATADV_P_DAT_DHT_GET:
  622. batadv_inc_counter(bat_priv,
  623. BATADV_CNT_DAT_GET_TX);
  624. break;
  625. case BATADV_P_DAT_DHT_PUT:
  626. batadv_inc_counter(bat_priv,
  627. BATADV_CNT_DAT_PUT_TX);
  628. break;
  629. }
  630. /* packet sent to a candidate: return true */
  631. ret = true;
  632. }
  633. free_neigh:
  634. batadv_neigh_node_put(neigh_node);
  635. free_orig:
  636. batadv_orig_node_put(cand[i].orig_node);
  637. }
  638. kfree(cand);
  639. return ret;
  640. }
  641. /**
  642. * batadv_dat_tvlv_container_update() - update the dat tvlv container after dat
  643. * setting change
  644. * @bat_priv: the bat priv with all the soft interface information
  645. */
  646. static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
  647. {
  648. char dat_mode;
  649. dat_mode = atomic_read(&bat_priv->distributed_arp_table);
  650. switch (dat_mode) {
  651. case 0:
  652. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  653. break;
  654. case 1:
  655. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
  656. NULL, 0);
  657. break;
  658. }
  659. }
  660. /**
  661. * batadv_dat_status_update() - update the dat tvlv container after dat
  662. * setting change
  663. * @net_dev: the soft interface net device
  664. */
  665. void batadv_dat_status_update(struct net_device *net_dev)
  666. {
  667. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  668. batadv_dat_tvlv_container_update(bat_priv);
  669. }
  670. /**
  671. * batadv_dat_tvlv_ogm_handler_v1() - process incoming dat tvlv container
  672. * @bat_priv: the bat priv with all the soft interface information
  673. * @orig: the orig_node of the ogm
  674. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  675. * @tvlv_value: tvlv buffer containing the gateway data
  676. * @tvlv_value_len: tvlv buffer length
  677. */
  678. static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  679. struct batadv_orig_node *orig,
  680. u8 flags,
  681. void *tvlv_value, u16 tvlv_value_len)
  682. {
  683. if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
  684. clear_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
  685. else
  686. set_bit(BATADV_ORIG_CAPA_HAS_DAT, &orig->capabilities);
  687. }
  688. /**
  689. * batadv_dat_hash_free() - free the local DAT hash table
  690. * @bat_priv: the bat priv with all the soft interface information
  691. */
  692. static void batadv_dat_hash_free(struct batadv_priv *bat_priv)
  693. {
  694. if (!bat_priv->dat.hash)
  695. return;
  696. __batadv_dat_purge(bat_priv, NULL);
  697. batadv_hash_destroy(bat_priv->dat.hash);
  698. bat_priv->dat.hash = NULL;
  699. }
  700. /**
  701. * batadv_dat_init() - initialise the DAT internals
  702. * @bat_priv: the bat priv with all the soft interface information
  703. *
  704. * Return: 0 in case of success, a negative error code otherwise
  705. */
  706. int batadv_dat_init(struct batadv_priv *bat_priv)
  707. {
  708. if (bat_priv->dat.hash)
  709. return 0;
  710. bat_priv->dat.hash = batadv_hash_new(1024);
  711. if (!bat_priv->dat.hash)
  712. return -ENOMEM;
  713. INIT_DELAYED_WORK(&bat_priv->dat.work, batadv_dat_purge);
  714. batadv_dat_start_timer(bat_priv);
  715. batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
  716. NULL, NULL, BATADV_TVLV_DAT, 1,
  717. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  718. batadv_dat_tvlv_container_update(bat_priv);
  719. return 0;
  720. }
  721. /**
  722. * batadv_dat_free() - free the DAT internals
  723. * @bat_priv: the bat priv with all the soft interface information
  724. */
  725. void batadv_dat_free(struct batadv_priv *bat_priv)
  726. {
  727. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  728. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
  729. cancel_delayed_work_sync(&bat_priv->dat.work);
  730. batadv_dat_hash_free(bat_priv);
  731. }
  732. /**
  733. * batadv_dat_cache_dump_entry() - dump one entry of the DAT cache table to a
  734. * netlink socket
  735. * @msg: buffer for the message
  736. * @portid: netlink port
  737. * @cb: Control block containing additional options
  738. * @dat_entry: entry to dump
  739. *
  740. * Return: 0 or error code.
  741. */
  742. static int
  743. batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid,
  744. struct netlink_callback *cb,
  745. struct batadv_dat_entry *dat_entry)
  746. {
  747. int msecs;
  748. void *hdr;
  749. hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
  750. &batadv_netlink_family, NLM_F_MULTI,
  751. BATADV_CMD_GET_DAT_CACHE);
  752. if (!hdr)
  753. return -ENOBUFS;
  754. genl_dump_check_consistent(cb, hdr);
  755. msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
  756. if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
  757. dat_entry->ip) ||
  758. nla_put(msg, BATADV_ATTR_DAT_CACHE_HWADDRESS, ETH_ALEN,
  759. dat_entry->mac_addr) ||
  760. nla_put_u16(msg, BATADV_ATTR_DAT_CACHE_VID, dat_entry->vid) ||
  761. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
  762. genlmsg_cancel(msg, hdr);
  763. return -EMSGSIZE;
  764. }
  765. genlmsg_end(msg, hdr);
  766. return 0;
  767. }
  768. /**
  769. * batadv_dat_cache_dump_bucket() - dump one bucket of the DAT cache table to
  770. * a netlink socket
  771. * @msg: buffer for the message
  772. * @portid: netlink port
  773. * @cb: Control block containing additional options
  774. * @hash: hash to dump
  775. * @bucket: bucket index to dump
  776. * @idx_skip: How many entries to skip
  777. *
  778. * Return: 0 or error code.
  779. */
  780. static int
  781. batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid,
  782. struct netlink_callback *cb,
  783. struct batadv_hashtable *hash, unsigned int bucket,
  784. int *idx_skip)
  785. {
  786. struct batadv_dat_entry *dat_entry;
  787. int idx = 0;
  788. spin_lock_bh(&hash->list_locks[bucket]);
  789. cb->seq = atomic_read(&hash->generation) << 1 | 1;
  790. hlist_for_each_entry(dat_entry, &hash->table[bucket], hash_entry) {
  791. if (idx < *idx_skip)
  792. goto skip;
  793. if (batadv_dat_cache_dump_entry(msg, portid, cb, dat_entry)) {
  794. spin_unlock_bh(&hash->list_locks[bucket]);
  795. *idx_skip = idx;
  796. return -EMSGSIZE;
  797. }
  798. skip:
  799. idx++;
  800. }
  801. spin_unlock_bh(&hash->list_locks[bucket]);
  802. return 0;
  803. }
  804. /**
  805. * batadv_dat_cache_dump() - dump DAT cache table to a netlink socket
  806. * @msg: buffer for the message
  807. * @cb: callback structure containing arguments
  808. *
  809. * Return: message length.
  810. */
  811. int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
  812. {
  813. struct batadv_hard_iface *primary_if = NULL;
  814. int portid = NETLINK_CB(cb->skb).portid;
  815. struct net *net = sock_net(cb->skb->sk);
  816. struct net_device *soft_iface;
  817. struct batadv_hashtable *hash;
  818. struct batadv_priv *bat_priv;
  819. int bucket = cb->args[0];
  820. int idx = cb->args[1];
  821. int ifindex;
  822. int ret = 0;
  823. ifindex = batadv_netlink_get_ifindex(cb->nlh,
  824. BATADV_ATTR_MESH_IFINDEX);
  825. if (!ifindex)
  826. return -EINVAL;
  827. soft_iface = dev_get_by_index(net, ifindex);
  828. if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
  829. ret = -ENODEV;
  830. goto out;
  831. }
  832. bat_priv = netdev_priv(soft_iface);
  833. hash = bat_priv->dat.hash;
  834. primary_if = batadv_primary_if_get_selected(bat_priv);
  835. if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
  836. ret = -ENOENT;
  837. goto out;
  838. }
  839. while (bucket < hash->size) {
  840. if (batadv_dat_cache_dump_bucket(msg, portid, cb, hash, bucket,
  841. &idx))
  842. break;
  843. bucket++;
  844. idx = 0;
  845. }
  846. cb->args[0] = bucket;
  847. cb->args[1] = idx;
  848. ret = msg->len;
  849. out:
  850. batadv_hardif_put(primary_if);
  851. dev_put(soft_iface);
  852. return ret;
  853. }
  854. /**
  855. * batadv_arp_get_type() - parse an ARP packet and gets the type
  856. * @bat_priv: the bat priv with all the soft interface information
  857. * @skb: packet to analyse
  858. * @hdr_size: size of the possible header before the ARP packet in the skb
  859. *
  860. * Return: the ARP type if the skb contains a valid ARP packet, 0 otherwise.
  861. */
  862. static u16 batadv_arp_get_type(struct batadv_priv *bat_priv,
  863. struct sk_buff *skb, int hdr_size)
  864. {
  865. struct arphdr *arphdr;
  866. struct ethhdr *ethhdr;
  867. __be32 ip_src, ip_dst;
  868. u8 *hw_src, *hw_dst;
  869. u16 type = 0;
  870. /* pull the ethernet header */
  871. if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
  872. goto out;
  873. ethhdr = (struct ethhdr *)(skb->data + hdr_size);
  874. if (ethhdr->h_proto != htons(ETH_P_ARP))
  875. goto out;
  876. /* pull the ARP payload */
  877. if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN +
  878. arp_hdr_len(skb->dev))))
  879. goto out;
  880. arphdr = (struct arphdr *)(skb->data + hdr_size + ETH_HLEN);
  881. /* check whether the ARP packet carries a valid IP information */
  882. if (arphdr->ar_hrd != htons(ARPHRD_ETHER))
  883. goto out;
  884. if (arphdr->ar_pro != htons(ETH_P_IP))
  885. goto out;
  886. if (arphdr->ar_hln != ETH_ALEN)
  887. goto out;
  888. if (arphdr->ar_pln != 4)
  889. goto out;
  890. /* Check for bad reply/request. If the ARP message is not sane, DAT
  891. * will simply ignore it
  892. */
  893. ip_src = batadv_arp_ip_src(skb, hdr_size);
  894. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  895. if (ipv4_is_loopback(ip_src) || ipv4_is_multicast(ip_src) ||
  896. ipv4_is_loopback(ip_dst) || ipv4_is_multicast(ip_dst) ||
  897. ipv4_is_zeronet(ip_src) || ipv4_is_lbcast(ip_src) ||
  898. ipv4_is_zeronet(ip_dst) || ipv4_is_lbcast(ip_dst))
  899. goto out;
  900. hw_src = batadv_arp_hw_src(skb, hdr_size);
  901. if (is_zero_ether_addr(hw_src) || is_multicast_ether_addr(hw_src))
  902. goto out;
  903. /* don't care about the destination MAC address in ARP requests */
  904. if (arphdr->ar_op != htons(ARPOP_REQUEST)) {
  905. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  906. if (is_zero_ether_addr(hw_dst) ||
  907. is_multicast_ether_addr(hw_dst))
  908. goto out;
  909. }
  910. type = ntohs(arphdr->ar_op);
  911. out:
  912. return type;
  913. }
  914. /**
  915. * batadv_dat_get_vid() - extract the VLAN identifier from skb if any
  916. * @skb: the buffer containing the packet to extract the VID from
  917. * @hdr_size: the size of the batman-adv header encapsulating the packet
  918. *
  919. * Return: If the packet embedded in the skb is vlan tagged this function
  920. * returns the VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS
  921. * is returned.
  922. */
  923. static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
  924. {
  925. unsigned short vid;
  926. vid = batadv_get_vid(skb, *hdr_size);
  927. /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
  928. * If the header contained in the packet is a VLAN one (which is longer)
  929. * hdr_size is updated so that the functions will still skip the
  930. * correct amount of bytes.
  931. */
  932. if (vid & BATADV_VLAN_HAS_TAG)
  933. *hdr_size += VLAN_HLEN;
  934. return vid;
  935. }
  936. /**
  937. * batadv_dat_arp_create_reply() - create an ARP Reply
  938. * @bat_priv: the bat priv with all the soft interface information
  939. * @ip_src: ARP sender IP
  940. * @ip_dst: ARP target IP
  941. * @hw_src: Ethernet source and ARP sender MAC
  942. * @hw_dst: Ethernet destination and ARP target MAC
  943. * @vid: VLAN identifier (optional, set to zero otherwise)
  944. *
  945. * Creates an ARP Reply from the given values, optionally encapsulated in a
  946. * VLAN header.
  947. *
  948. * Return: An skb containing an ARP Reply.
  949. */
  950. static struct sk_buff *
  951. batadv_dat_arp_create_reply(struct batadv_priv *bat_priv, __be32 ip_src,
  952. __be32 ip_dst, u8 *hw_src, u8 *hw_dst,
  953. unsigned short vid)
  954. {
  955. struct sk_buff *skb;
  956. skb = arp_create(ARPOP_REPLY, ETH_P_ARP, ip_dst, bat_priv->soft_iface,
  957. ip_src, hw_dst, hw_src, hw_dst);
  958. if (!skb)
  959. return NULL;
  960. skb_reset_mac_header(skb);
  961. if (vid & BATADV_VLAN_HAS_TAG)
  962. skb = vlan_insert_tag(skb, htons(ETH_P_8021Q),
  963. vid & VLAN_VID_MASK);
  964. return skb;
  965. }
  966. /**
  967. * batadv_dat_snoop_outgoing_arp_request() - snoop the ARP request and try to
  968. * answer using DAT
  969. * @bat_priv: the bat priv with all the soft interface information
  970. * @skb: packet to check
  971. *
  972. * Return: true if the message has been sent to the dht candidates, false
  973. * otherwise. In case of a positive return value the message has to be enqueued
  974. * to permit the fallback.
  975. */
  976. bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
  977. struct sk_buff *skb)
  978. {
  979. u16 type = 0;
  980. __be32 ip_dst, ip_src;
  981. u8 *hw_src;
  982. bool ret = false;
  983. struct batadv_dat_entry *dat_entry = NULL;
  984. struct sk_buff *skb_new;
  985. struct net_device *soft_iface = bat_priv->soft_iface;
  986. int hdr_size = 0;
  987. unsigned short vid;
  988. if (!atomic_read(&bat_priv->distributed_arp_table))
  989. goto out;
  990. vid = batadv_dat_get_vid(skb, &hdr_size);
  991. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  992. /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
  993. * message to the selected DHT candidates
  994. */
  995. if (type != ARPOP_REQUEST)
  996. goto out;
  997. batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REQUEST");
  998. ip_src = batadv_arp_ip_src(skb, hdr_size);
  999. hw_src = batadv_arp_hw_src(skb, hdr_size);
  1000. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  1001. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  1002. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  1003. if (dat_entry) {
  1004. /* If the ARP request is destined for a local client the local
  1005. * client will answer itself. DAT would only generate a
  1006. * duplicate packet.
  1007. *
  1008. * Moreover, if the soft-interface is enslaved into a bridge, an
  1009. * additional DAT answer may trigger kernel warnings about
  1010. * a packet coming from the wrong port.
  1011. */
  1012. if (batadv_is_my_client(bat_priv, dat_entry->mac_addr, vid)) {
  1013. ret = true;
  1014. goto out;
  1015. }
  1016. /* If BLA is enabled, only send ARP replies if we have claimed
  1017. * the destination for the ARP request or if no one else of
  1018. * the backbone gws belonging to our backbone has claimed the
  1019. * destination.
  1020. */
  1021. if (!batadv_bla_check_claim(bat_priv,
  1022. dat_entry->mac_addr, vid)) {
  1023. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1024. "Device %pM claimed by another backbone gw. Don't send ARP reply!",
  1025. dat_entry->mac_addr);
  1026. ret = true;
  1027. goto out;
  1028. }
  1029. skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
  1030. dat_entry->mac_addr,
  1031. hw_src, vid);
  1032. if (!skb_new)
  1033. goto out;
  1034. skb_new->protocol = eth_type_trans(skb_new, soft_iface);
  1035. batadv_inc_counter(bat_priv, BATADV_CNT_RX);
  1036. batadv_add_counter(bat_priv, BATADV_CNT_RX_BYTES,
  1037. skb->len + ETH_HLEN + hdr_size);
  1038. netif_rx(skb_new);
  1039. batadv_dbg(BATADV_DBG_DAT, bat_priv, "ARP request replied locally\n");
  1040. ret = true;
  1041. } else {
  1042. /* Send the request to the DHT */
  1043. ret = batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
  1044. BATADV_P_DAT_DHT_GET);
  1045. }
  1046. out:
  1047. batadv_dat_entry_put(dat_entry);
  1048. return ret;
  1049. }
  1050. /**
  1051. * batadv_dat_snoop_incoming_arp_request() - snoop the ARP request and try to
  1052. * answer using the local DAT storage
  1053. * @bat_priv: the bat priv with all the soft interface information
  1054. * @skb: packet to check
  1055. * @hdr_size: size of the encapsulation header
  1056. *
  1057. * Return: true if the request has been answered, false otherwise.
  1058. */
  1059. bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
  1060. struct sk_buff *skb, int hdr_size)
  1061. {
  1062. u16 type;
  1063. __be32 ip_src, ip_dst;
  1064. u8 *hw_src;
  1065. struct sk_buff *skb_new;
  1066. struct batadv_dat_entry *dat_entry = NULL;
  1067. bool ret = false;
  1068. unsigned short vid;
  1069. int err;
  1070. if (!atomic_read(&bat_priv->distributed_arp_table))
  1071. goto out;
  1072. vid = batadv_dat_get_vid(skb, &hdr_size);
  1073. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  1074. if (type != ARPOP_REQUEST)
  1075. goto out;
  1076. hw_src = batadv_arp_hw_src(skb, hdr_size);
  1077. ip_src = batadv_arp_ip_src(skb, hdr_size);
  1078. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  1079. batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REQUEST");
  1080. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  1081. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  1082. if (!dat_entry)
  1083. goto out;
  1084. skb_new = batadv_dat_arp_create_reply(bat_priv, ip_dst, ip_src,
  1085. dat_entry->mac_addr, hw_src, vid);
  1086. if (!skb_new)
  1087. goto out;
  1088. /* To preserve backwards compatibility, the node has choose the outgoing
  1089. * format based on the incoming request packet type. The assumption is
  1090. * that a node not using the 4addr packet format doesn't support it.
  1091. */
  1092. if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
  1093. err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
  1094. BATADV_P_DAT_CACHE_REPLY,
  1095. NULL, vid);
  1096. else
  1097. err = batadv_send_skb_via_tt(bat_priv, skb_new, NULL, vid);
  1098. if (err != NET_XMIT_DROP) {
  1099. batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
  1100. ret = true;
  1101. }
  1102. out:
  1103. batadv_dat_entry_put(dat_entry);
  1104. if (ret)
  1105. kfree_skb(skb);
  1106. return ret;
  1107. }
  1108. /**
  1109. * batadv_dat_snoop_outgoing_arp_reply() - snoop the ARP reply and fill the DHT
  1110. * @bat_priv: the bat priv with all the soft interface information
  1111. * @skb: packet to check
  1112. */
  1113. void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
  1114. struct sk_buff *skb)
  1115. {
  1116. u16 type;
  1117. __be32 ip_src, ip_dst;
  1118. u8 *hw_src, *hw_dst;
  1119. int hdr_size = 0;
  1120. unsigned short vid;
  1121. if (!atomic_read(&bat_priv->distributed_arp_table))
  1122. return;
  1123. vid = batadv_dat_get_vid(skb, &hdr_size);
  1124. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  1125. if (type != ARPOP_REPLY)
  1126. return;
  1127. batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing outgoing ARP REPLY");
  1128. hw_src = batadv_arp_hw_src(skb, hdr_size);
  1129. ip_src = batadv_arp_ip_src(skb, hdr_size);
  1130. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  1131. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  1132. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  1133. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
  1134. /* Send the ARP reply to the candidates for both the IP addresses that
  1135. * the node obtained from the ARP reply
  1136. */
  1137. batadv_dat_forward_data(bat_priv, skb, ip_src, vid,
  1138. BATADV_P_DAT_DHT_PUT);
  1139. batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
  1140. BATADV_P_DAT_DHT_PUT);
  1141. }
  1142. /**
  1143. * batadv_dat_snoop_incoming_arp_reply() - snoop the ARP reply and fill the
  1144. * local DAT storage only
  1145. * @bat_priv: the bat priv with all the soft interface information
  1146. * @skb: packet to check
  1147. * @hdr_size: size of the encapsulation header
  1148. *
  1149. * Return: true if the packet was snooped and consumed by DAT. False if the
  1150. * packet has to be delivered to the interface
  1151. */
  1152. bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
  1153. struct sk_buff *skb, int hdr_size)
  1154. {
  1155. struct batadv_dat_entry *dat_entry = NULL;
  1156. u16 type;
  1157. __be32 ip_src, ip_dst;
  1158. u8 *hw_src, *hw_dst;
  1159. bool dropped = false;
  1160. unsigned short vid;
  1161. if (!atomic_read(&bat_priv->distributed_arp_table))
  1162. goto out;
  1163. vid = batadv_dat_get_vid(skb, &hdr_size);
  1164. type = batadv_arp_get_type(bat_priv, skb, hdr_size);
  1165. if (type != ARPOP_REPLY)
  1166. goto out;
  1167. batadv_dbg_arp(bat_priv, skb, hdr_size, "Parsing incoming ARP REPLY");
  1168. hw_src = batadv_arp_hw_src(skb, hdr_size);
  1169. ip_src = batadv_arp_ip_src(skb, hdr_size);
  1170. hw_dst = batadv_arp_hw_dst(skb, hdr_size);
  1171. ip_dst = batadv_arp_ip_dst(skb, hdr_size);
  1172. /* If ip_dst is already in cache and has the right mac address,
  1173. * drop this frame if this ARP reply is destined for us because it's
  1174. * most probably an ARP reply generated by another node of the DHT.
  1175. * We have most probably received already a reply earlier. Delivering
  1176. * this frame would lead to doubled receive of an ARP reply.
  1177. */
  1178. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_src, vid);
  1179. if (dat_entry && batadv_compare_eth(hw_src, dat_entry->mac_addr)) {
  1180. batadv_dbg(BATADV_DBG_DAT, bat_priv, "Doubled ARP reply removed: ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]; dat_entry: %pM-%pI4\n",
  1181. hw_src, &ip_src, hw_dst, &ip_dst,
  1182. dat_entry->mac_addr, &dat_entry->ip);
  1183. dropped = true;
  1184. }
  1185. /* Update our internal cache with both the IP addresses the node got
  1186. * within the ARP reply
  1187. */
  1188. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  1189. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
  1190. if (dropped)
  1191. goto out;
  1192. /* If BLA is enabled, only forward ARP replies if we have claimed the
  1193. * source of the ARP reply or if no one else of the same backbone has
  1194. * already claimed that client. This prevents that different gateways
  1195. * to the same backbone all forward the ARP reply leading to multiple
  1196. * replies in the backbone.
  1197. */
  1198. if (!batadv_bla_check_claim(bat_priv, hw_src, vid)) {
  1199. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1200. "Device %pM claimed by another backbone gw. Drop ARP reply.\n",
  1201. hw_src);
  1202. dropped = true;
  1203. goto out;
  1204. }
  1205. /* if this REPLY is directed to a client of mine, let's deliver the
  1206. * packet to the interface
  1207. */
  1208. dropped = !batadv_is_my_client(bat_priv, hw_dst, vid);
  1209. /* if this REPLY is sent on behalf of a client of mine, let's drop the
  1210. * packet because the client will reply by itself
  1211. */
  1212. dropped |= batadv_is_my_client(bat_priv, hw_src, vid);
  1213. out:
  1214. if (dropped)
  1215. kfree_skb(skb);
  1216. batadv_dat_entry_put(dat_entry);
  1217. /* if dropped == false -> deliver to the interface */
  1218. return dropped;
  1219. }
  1220. /**
  1221. * batadv_dat_check_dhcp_ipudp() - check skb for IP+UDP headers valid for DHCP
  1222. * @skb: the packet to check
  1223. * @ip_src: a buffer to store the IPv4 source address in
  1224. *
  1225. * Checks whether the given skb has an IP and UDP header valid for a DHCP
  1226. * message from a DHCP server. And if so, stores the IPv4 source address in
  1227. * the provided buffer.
  1228. *
  1229. * Return: True if valid, false otherwise.
  1230. */
  1231. static bool
  1232. batadv_dat_check_dhcp_ipudp(struct sk_buff *skb, __be32 *ip_src)
  1233. {
  1234. unsigned int offset = skb_network_offset(skb);
  1235. struct udphdr *udphdr, _udphdr;
  1236. struct iphdr *iphdr, _iphdr;
  1237. iphdr = skb_header_pointer(skb, offset, sizeof(_iphdr), &_iphdr);
  1238. if (!iphdr || iphdr->version != 4 || iphdr->ihl * 4 < sizeof(_iphdr))
  1239. return false;
  1240. if (iphdr->protocol != IPPROTO_UDP)
  1241. return false;
  1242. offset += iphdr->ihl * 4;
  1243. skb_set_transport_header(skb, offset);
  1244. udphdr = skb_header_pointer(skb, offset, sizeof(_udphdr), &_udphdr);
  1245. if (!udphdr || udphdr->source != htons(67))
  1246. return false;
  1247. *ip_src = get_unaligned(&iphdr->saddr);
  1248. return true;
  1249. }
  1250. /**
  1251. * batadv_dat_check_dhcp() - examine packet for valid DHCP message
  1252. * @skb: the packet to check
  1253. * @proto: ethernet protocol hint (behind a potential vlan)
  1254. * @ip_src: a buffer to store the IPv4 source address in
  1255. *
  1256. * Checks whether the given skb is a valid DHCP packet. And if so, stores the
  1257. * IPv4 source address in the provided buffer.
  1258. *
  1259. * Caller needs to ensure that the skb network header is set correctly.
  1260. *
  1261. * Return: If skb is a valid DHCP packet, then returns its op code
  1262. * (e.g. BOOTREPLY vs. BOOTREQUEST). Otherwise returns -EINVAL.
  1263. */
  1264. static int
  1265. batadv_dat_check_dhcp(struct sk_buff *skb, __be16 proto, __be32 *ip_src)
  1266. {
  1267. __be32 *magic, _magic;
  1268. unsigned int offset;
  1269. struct {
  1270. __u8 op;
  1271. __u8 htype;
  1272. __u8 hlen;
  1273. __u8 hops;
  1274. } *dhcp_h, _dhcp_h;
  1275. if (proto != htons(ETH_P_IP))
  1276. return -EINVAL;
  1277. if (!batadv_dat_check_dhcp_ipudp(skb, ip_src))
  1278. return -EINVAL;
  1279. offset = skb_transport_offset(skb) + sizeof(struct udphdr);
  1280. if (skb->len < offset + sizeof(struct batadv_dhcp_packet))
  1281. return -EINVAL;
  1282. dhcp_h = skb_header_pointer(skb, offset, sizeof(_dhcp_h), &_dhcp_h);
  1283. if (!dhcp_h || dhcp_h->htype != BATADV_HTYPE_ETHERNET ||
  1284. dhcp_h->hlen != ETH_ALEN)
  1285. return -EINVAL;
  1286. offset += offsetof(struct batadv_dhcp_packet, magic);
  1287. magic = skb_header_pointer(skb, offset, sizeof(_magic), &_magic);
  1288. if (!magic || get_unaligned(magic) != htonl(BATADV_DHCP_MAGIC))
  1289. return -EINVAL;
  1290. return dhcp_h->op;
  1291. }
  1292. /**
  1293. * batadv_dat_get_dhcp_message_type() - get message type of a DHCP packet
  1294. * @skb: the DHCP packet to parse
  1295. *
  1296. * Iterates over the DHCP options of the given DHCP packet to find a
  1297. * DHCP Message Type option and parse it.
  1298. *
  1299. * Caller needs to ensure that the given skb is a valid DHCP packet and
  1300. * that the skb transport header is set correctly.
  1301. *
  1302. * Return: The found DHCP message type value, if found. -EINVAL otherwise.
  1303. */
  1304. static int batadv_dat_get_dhcp_message_type(struct sk_buff *skb)
  1305. {
  1306. unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
  1307. u8 *type, _type;
  1308. struct {
  1309. u8 type;
  1310. u8 len;
  1311. } *tl, _tl;
  1312. offset += sizeof(struct batadv_dhcp_packet);
  1313. while ((tl = skb_header_pointer(skb, offset, sizeof(_tl), &_tl))) {
  1314. if (tl->type == BATADV_DHCP_OPT_MSG_TYPE)
  1315. break;
  1316. if (tl->type == BATADV_DHCP_OPT_END)
  1317. break;
  1318. if (tl->type == BATADV_DHCP_OPT_PAD)
  1319. offset++;
  1320. else
  1321. offset += tl->len + sizeof(_tl);
  1322. }
  1323. /* Option Overload Code not supported */
  1324. if (!tl || tl->type != BATADV_DHCP_OPT_MSG_TYPE ||
  1325. tl->len != sizeof(_type))
  1326. return -EINVAL;
  1327. offset += sizeof(_tl);
  1328. type = skb_header_pointer(skb, offset, sizeof(_type), &_type);
  1329. if (!type)
  1330. return -EINVAL;
  1331. return *type;
  1332. }
  1333. /**
  1334. * batadv_dat_dhcp_get_yiaddr() - get yiaddr from a DHCP packet
  1335. * @skb: the DHCP packet to parse
  1336. * @buf: a buffer to store the yiaddr in
  1337. *
  1338. * Caller needs to ensure that the given skb is a valid DHCP packet and
  1339. * that the skb transport header is set correctly.
  1340. *
  1341. * Return: True on success, false otherwise.
  1342. */
  1343. static bool batadv_dat_dhcp_get_yiaddr(struct sk_buff *skb, __be32 *buf)
  1344. {
  1345. unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
  1346. __be32 *yiaddr;
  1347. offset += offsetof(struct batadv_dhcp_packet, yiaddr);
  1348. yiaddr = skb_header_pointer(skb, offset, BATADV_DHCP_YIADDR_LEN, buf);
  1349. if (!yiaddr)
  1350. return false;
  1351. if (yiaddr != buf)
  1352. *buf = get_unaligned(yiaddr);
  1353. return true;
  1354. }
  1355. /**
  1356. * batadv_dat_get_dhcp_chaddr() - get chaddr from a DHCP packet
  1357. * @skb: the DHCP packet to parse
  1358. * @buf: a buffer to store the chaddr in
  1359. *
  1360. * Caller needs to ensure that the given skb is a valid DHCP packet and
  1361. * that the skb transport header is set correctly.
  1362. *
  1363. * Return: True on success, false otherwise
  1364. */
  1365. static bool batadv_dat_get_dhcp_chaddr(struct sk_buff *skb, u8 *buf)
  1366. {
  1367. unsigned int offset = skb_transport_offset(skb) + sizeof(struct udphdr);
  1368. u8 *chaddr;
  1369. offset += offsetof(struct batadv_dhcp_packet, chaddr);
  1370. chaddr = skb_header_pointer(skb, offset, BATADV_DHCP_CHADDR_LEN, buf);
  1371. if (!chaddr)
  1372. return false;
  1373. if (chaddr != buf)
  1374. memcpy(buf, chaddr, BATADV_DHCP_CHADDR_LEN);
  1375. return true;
  1376. }
  1377. /**
  1378. * batadv_dat_put_dhcp() - puts addresses from a DHCP packet into the DHT and
  1379. * DAT cache
  1380. * @bat_priv: the bat priv with all the soft interface information
  1381. * @chaddr: the DHCP client MAC address
  1382. * @yiaddr: the DHCP client IP address
  1383. * @hw_dst: the DHCP server MAC address
  1384. * @ip_dst: the DHCP server IP address
  1385. * @vid: VLAN identifier
  1386. *
  1387. * Adds given MAC/IP pairs to the local DAT cache and propagates them further
  1388. * into the DHT.
  1389. *
  1390. * For the DHT propagation, client MAC + IP will appear as the ARP Reply
  1391. * transmitter (and hw_dst/ip_dst as the target).
  1392. */
  1393. static void batadv_dat_put_dhcp(struct batadv_priv *bat_priv, u8 *chaddr,
  1394. __be32 yiaddr, u8 *hw_dst, __be32 ip_dst,
  1395. unsigned short vid)
  1396. {
  1397. struct sk_buff *skb;
  1398. skb = batadv_dat_arp_create_reply(bat_priv, yiaddr, ip_dst, chaddr,
  1399. hw_dst, vid);
  1400. if (!skb)
  1401. return;
  1402. skb_set_network_header(skb, ETH_HLEN);
  1403. batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
  1404. batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
  1405. batadv_dat_forward_data(bat_priv, skb, yiaddr, vid,
  1406. BATADV_P_DAT_DHT_PUT);
  1407. batadv_dat_forward_data(bat_priv, skb, ip_dst, vid,
  1408. BATADV_P_DAT_DHT_PUT);
  1409. consume_skb(skb);
  1410. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1411. "Snooped from outgoing DHCPACK (server address): %pI4, %pM (vid: %i)\n",
  1412. &ip_dst, hw_dst, batadv_print_vid(vid));
  1413. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1414. "Snooped from outgoing DHCPACK (client address): %pI4, %pM (vid: %i)\n",
  1415. &yiaddr, chaddr, batadv_print_vid(vid));
  1416. }
  1417. /**
  1418. * batadv_dat_check_dhcp_ack() - examine packet for valid DHCP message
  1419. * @skb: the packet to check
  1420. * @proto: ethernet protocol hint (behind a potential vlan)
  1421. * @ip_src: a buffer to store the IPv4 source address in
  1422. * @chaddr: a buffer to store the DHCP Client Hardware Address in
  1423. * @yiaddr: a buffer to store the DHCP Your IP Address in
  1424. *
  1425. * Checks whether the given skb is a valid DHCPACK. And if so, stores the
  1426. * IPv4 server source address (ip_src), client MAC address (chaddr) and client
  1427. * IPv4 address (yiaddr) in the provided buffers.
  1428. *
  1429. * Caller needs to ensure that the skb network header is set correctly.
  1430. *
  1431. * Return: True if the skb is a valid DHCPACK. False otherwise.
  1432. */
  1433. static bool
  1434. batadv_dat_check_dhcp_ack(struct sk_buff *skb, __be16 proto, __be32 *ip_src,
  1435. u8 *chaddr, __be32 *yiaddr)
  1436. {
  1437. int type;
  1438. type = batadv_dat_check_dhcp(skb, proto, ip_src);
  1439. if (type != BATADV_BOOTREPLY)
  1440. return false;
  1441. type = batadv_dat_get_dhcp_message_type(skb);
  1442. if (type != BATADV_DHCPACK)
  1443. return false;
  1444. if (!batadv_dat_dhcp_get_yiaddr(skb, yiaddr))
  1445. return false;
  1446. if (!batadv_dat_get_dhcp_chaddr(skb, chaddr))
  1447. return false;
  1448. return true;
  1449. }
  1450. /**
  1451. * batadv_dat_snoop_outgoing_dhcp_ack() - snoop DHCPACK and fill DAT with it
  1452. * @bat_priv: the bat priv with all the soft interface information
  1453. * @skb: the packet to snoop
  1454. * @proto: ethernet protocol hint (behind a potential vlan)
  1455. * @vid: VLAN identifier
  1456. *
  1457. * This function first checks whether the given skb is a valid DHCPACK. If
  1458. * so then its source MAC and IP as well as its DHCP Client Hardware Address
  1459. * field and DHCP Your IP Address field are added to the local DAT cache and
  1460. * propagated into the DHT.
  1461. *
  1462. * Caller needs to ensure that the skb mac and network headers are set
  1463. * correctly.
  1464. */
  1465. void batadv_dat_snoop_outgoing_dhcp_ack(struct batadv_priv *bat_priv,
  1466. struct sk_buff *skb,
  1467. __be16 proto,
  1468. unsigned short vid)
  1469. {
  1470. u8 chaddr[BATADV_DHCP_CHADDR_LEN];
  1471. __be32 ip_src, yiaddr;
  1472. if (!atomic_read(&bat_priv->distributed_arp_table))
  1473. return;
  1474. if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
  1475. return;
  1476. batadv_dat_put_dhcp(bat_priv, chaddr, yiaddr, eth_hdr(skb)->h_source,
  1477. ip_src, vid);
  1478. }
  1479. /**
  1480. * batadv_dat_snoop_incoming_dhcp_ack() - snoop DHCPACK and fill DAT cache
  1481. * @bat_priv: the bat priv with all the soft interface information
  1482. * @skb: the packet to snoop
  1483. * @hdr_size: header size, up to the tail of the batman-adv header
  1484. *
  1485. * This function first checks whether the given skb is a valid DHCPACK. If
  1486. * so then its source MAC and IP as well as its DHCP Client Hardware Address
  1487. * field and DHCP Your IP Address field are added to the local DAT cache.
  1488. */
  1489. void batadv_dat_snoop_incoming_dhcp_ack(struct batadv_priv *bat_priv,
  1490. struct sk_buff *skb, int hdr_size)
  1491. {
  1492. u8 chaddr[BATADV_DHCP_CHADDR_LEN];
  1493. struct ethhdr *ethhdr;
  1494. __be32 ip_src, yiaddr;
  1495. unsigned short vid;
  1496. __be16 proto;
  1497. u8 *hw_src;
  1498. if (!atomic_read(&bat_priv->distributed_arp_table))
  1499. return;
  1500. if (unlikely(!pskb_may_pull(skb, hdr_size + ETH_HLEN)))
  1501. return;
  1502. ethhdr = (struct ethhdr *)(skb->data + hdr_size);
  1503. skb_set_network_header(skb, hdr_size + ETH_HLEN);
  1504. proto = ethhdr->h_proto;
  1505. if (!batadv_dat_check_dhcp_ack(skb, proto, &ip_src, chaddr, &yiaddr))
  1506. return;
  1507. hw_src = ethhdr->h_source;
  1508. vid = batadv_dat_get_vid(skb, &hdr_size);
  1509. batadv_dat_entry_add(bat_priv, yiaddr, chaddr, vid);
  1510. batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
  1511. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1512. "Snooped from incoming DHCPACK (server address): %pI4, %pM (vid: %i)\n",
  1513. &ip_src, hw_src, batadv_print_vid(vid));
  1514. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1515. "Snooped from incoming DHCPACK (client address): %pI4, %pM (vid: %i)\n",
  1516. &yiaddr, chaddr, batadv_print_vid(vid));
  1517. }
  1518. /**
  1519. * batadv_dat_drop_broadcast_packet() - check if an ARP request has to be
  1520. * dropped (because the node has already obtained the reply via DAT) or not
  1521. * @bat_priv: the bat priv with all the soft interface information
  1522. * @forw_packet: the broadcast packet
  1523. *
  1524. * Return: true if the node can drop the packet, false otherwise.
  1525. */
  1526. bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
  1527. struct batadv_forw_packet *forw_packet)
  1528. {
  1529. u16 type;
  1530. __be32 ip_dst;
  1531. struct batadv_dat_entry *dat_entry = NULL;
  1532. bool ret = false;
  1533. int hdr_size = sizeof(struct batadv_bcast_packet);
  1534. unsigned short vid;
  1535. if (!atomic_read(&bat_priv->distributed_arp_table))
  1536. goto out;
  1537. /* If this packet is an ARP_REQUEST and the node already has the
  1538. * information that it is going to ask, then the packet can be dropped
  1539. */
  1540. if (batadv_forw_packet_is_rebroadcast(forw_packet))
  1541. goto out;
  1542. vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
  1543. type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
  1544. if (type != ARPOP_REQUEST)
  1545. goto out;
  1546. ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
  1547. dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
  1548. /* check if the node already got this entry */
  1549. if (!dat_entry) {
  1550. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1551. "ARP Request for %pI4: fallback\n", &ip_dst);
  1552. goto out;
  1553. }
  1554. batadv_dbg(BATADV_DBG_DAT, bat_priv,
  1555. "ARP Request for %pI4: fallback prevented\n", &ip_dst);
  1556. ret = true;
  1557. out:
  1558. batadv_dat_entry_put(dat_entry);
  1559. return ret;
  1560. }