neighbour.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _NET_NEIGHBOUR_H
  3. #define _NET_NEIGHBOUR_H
  4. #include <linux/neighbour.h>
  5. /*
  6. * Generic neighbour manipulation
  7. *
  8. * Authors:
  9. * Pedro Roque <roque@di.fc.ul.pt>
  10. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  11. *
  12. * Changes:
  13. *
  14. * Harald Welte: <laforge@gnumonks.org>
  15. * - Add neighbour cache statistics like rtstat
  16. */
  17. #include <linux/atomic.h>
  18. #include <linux/refcount.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/rcupdate.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/bitmap.h>
  24. #include <linux/err.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/workqueue.h>
  27. #include <net/rtnetlink.h>
  28. /*
  29. * NUD stands for "neighbor unreachability detection"
  30. */
  31. #define NUD_IN_TIMER (NUD_INCOMPLETE|NUD_REACHABLE|NUD_DELAY|NUD_PROBE)
  32. #define NUD_VALID (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
  33. #define NUD_CONNECTED (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)
  34. struct neighbour;
  35. enum {
  36. NEIGH_VAR_MCAST_PROBES,
  37. NEIGH_VAR_UCAST_PROBES,
  38. NEIGH_VAR_APP_PROBES,
  39. NEIGH_VAR_MCAST_REPROBES,
  40. NEIGH_VAR_RETRANS_TIME,
  41. NEIGH_VAR_BASE_REACHABLE_TIME,
  42. NEIGH_VAR_DELAY_PROBE_TIME,
  43. NEIGH_VAR_GC_STALETIME,
  44. NEIGH_VAR_QUEUE_LEN_BYTES,
  45. NEIGH_VAR_PROXY_QLEN,
  46. NEIGH_VAR_ANYCAST_DELAY,
  47. NEIGH_VAR_PROXY_DELAY,
  48. NEIGH_VAR_LOCKTIME,
  49. #define NEIGH_VAR_DATA_MAX (NEIGH_VAR_LOCKTIME + 1)
  50. /* Following are used as a second way to access one of the above */
  51. NEIGH_VAR_QUEUE_LEN, /* same data as NEIGH_VAR_QUEUE_LEN_BYTES */
  52. NEIGH_VAR_RETRANS_TIME_MS, /* same data as NEIGH_VAR_RETRANS_TIME */
  53. NEIGH_VAR_BASE_REACHABLE_TIME_MS, /* same data as NEIGH_VAR_BASE_REACHABLE_TIME */
  54. /* Following are used by "default" only */
  55. NEIGH_VAR_GC_INTERVAL,
  56. NEIGH_VAR_GC_THRESH1,
  57. NEIGH_VAR_GC_THRESH2,
  58. NEIGH_VAR_GC_THRESH3,
  59. NEIGH_VAR_MAX
  60. };
  61. struct neigh_parms {
  62. possible_net_t net;
  63. struct net_device *dev;
  64. struct list_head list;
  65. int (*neigh_setup)(struct neighbour *);
  66. void (*neigh_cleanup)(struct neighbour *);
  67. struct neigh_table *tbl;
  68. void *sysctl_table;
  69. int dead;
  70. refcount_t refcnt;
  71. struct rcu_head rcu_head;
  72. int reachable_time;
  73. int data[NEIGH_VAR_DATA_MAX];
  74. DECLARE_BITMAP(data_state, NEIGH_VAR_DATA_MAX);
  75. };
  76. static inline void neigh_var_set(struct neigh_parms *p, int index, int val)
  77. {
  78. set_bit(index, p->data_state);
  79. p->data[index] = val;
  80. }
  81. #define NEIGH_VAR(p, attr) ((p)->data[NEIGH_VAR_ ## attr])
  82. /* In ndo_neigh_setup, NEIGH_VAR_INIT should be used.
  83. * In other cases, NEIGH_VAR_SET should be used.
  84. */
  85. #define NEIGH_VAR_INIT(p, attr, val) (NEIGH_VAR(p, attr) = val)
  86. #define NEIGH_VAR_SET(p, attr, val) neigh_var_set(p, NEIGH_VAR_ ## attr, val)
  87. static inline void neigh_parms_data_state_setall(struct neigh_parms *p)
  88. {
  89. bitmap_fill(p->data_state, NEIGH_VAR_DATA_MAX);
  90. }
  91. static inline void neigh_parms_data_state_cleanall(struct neigh_parms *p)
  92. {
  93. bitmap_zero(p->data_state, NEIGH_VAR_DATA_MAX);
  94. }
  95. struct neigh_statistics {
  96. unsigned long allocs; /* number of allocated neighs */
  97. unsigned long destroys; /* number of destroyed neighs */
  98. unsigned long hash_grows; /* number of hash resizes */
  99. unsigned long res_failed; /* number of failed resolutions */
  100. unsigned long lookups; /* number of lookups */
  101. unsigned long hits; /* number of hits (among lookups) */
  102. unsigned long rcv_probes_mcast; /* number of received mcast ipv6 */
  103. unsigned long rcv_probes_ucast; /* number of received ucast ipv6 */
  104. unsigned long periodic_gc_runs; /* number of periodic GC runs */
  105. unsigned long forced_gc_runs; /* number of forced GC runs */
  106. unsigned long unres_discards; /* number of unresolved drops */
  107. unsigned long table_fulls; /* times even gc couldn't help */
  108. };
  109. #define NEIGH_CACHE_STAT_INC(tbl, field) this_cpu_inc((tbl)->stats->field)
  110. struct neighbour {
  111. struct neighbour __rcu *next;
  112. struct neigh_table *tbl;
  113. struct neigh_parms *parms;
  114. unsigned long confirmed;
  115. unsigned long updated;
  116. rwlock_t lock;
  117. refcount_t refcnt;
  118. struct sk_buff_head arp_queue;
  119. unsigned int arp_queue_len_bytes;
  120. struct timer_list timer;
  121. unsigned long used;
  122. atomic_t probes;
  123. __u8 flags;
  124. __u8 nud_state;
  125. __u8 type;
  126. __u8 dead;
  127. seqlock_t ha_lock;
  128. unsigned char ha[ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))];
  129. struct hh_cache hh;
  130. int (*output)(struct neighbour *, struct sk_buff *);
  131. const struct neigh_ops *ops;
  132. struct rcu_head rcu;
  133. struct net_device *dev;
  134. u8 primary_key[0];
  135. } __randomize_layout;
  136. struct neigh_ops {
  137. int family;
  138. void (*solicit)(struct neighbour *, struct sk_buff *);
  139. void (*error_report)(struct neighbour *, struct sk_buff *);
  140. int (*output)(struct neighbour *, struct sk_buff *);
  141. int (*connected_output)(struct neighbour *, struct sk_buff *);
  142. };
  143. struct pneigh_entry {
  144. struct pneigh_entry *next;
  145. possible_net_t net;
  146. struct net_device *dev;
  147. u8 flags;
  148. u8 key[0];
  149. };
  150. /*
  151. * neighbour table manipulation
  152. */
  153. #define NEIGH_NUM_HASH_RND 4
  154. struct neigh_hash_table {
  155. struct neighbour __rcu **hash_buckets;
  156. unsigned int hash_shift;
  157. __u32 hash_rnd[NEIGH_NUM_HASH_RND];
  158. struct rcu_head rcu;
  159. };
  160. struct neigh_table {
  161. int family;
  162. int entry_size;
  163. int key_len;
  164. __be16 protocol;
  165. __u32 (*hash)(const void *pkey,
  166. const struct net_device *dev,
  167. __u32 *hash_rnd);
  168. bool (*key_eq)(const struct neighbour *, const void *pkey);
  169. int (*constructor)(struct neighbour *);
  170. int (*pconstructor)(struct pneigh_entry *);
  171. void (*pdestructor)(struct pneigh_entry *);
  172. void (*proxy_redo)(struct sk_buff *skb);
  173. char *id;
  174. struct neigh_parms parms;
  175. struct list_head parms_list;
  176. int gc_interval;
  177. int gc_thresh1;
  178. int gc_thresh2;
  179. int gc_thresh3;
  180. unsigned long last_flush;
  181. struct delayed_work gc_work;
  182. struct timer_list proxy_timer;
  183. struct sk_buff_head proxy_queue;
  184. atomic_t entries;
  185. rwlock_t lock;
  186. unsigned long last_rand;
  187. struct neigh_statistics __percpu *stats;
  188. struct neigh_hash_table __rcu *nht;
  189. struct pneigh_entry **phash_buckets;
  190. };
  191. enum {
  192. NEIGH_ARP_TABLE = 0,
  193. NEIGH_ND_TABLE = 1,
  194. NEIGH_DN_TABLE = 2,
  195. NEIGH_NR_TABLES,
  196. NEIGH_LINK_TABLE = NEIGH_NR_TABLES /* Pseudo table for neigh_xmit */
  197. };
  198. static inline int neigh_parms_family(struct neigh_parms *p)
  199. {
  200. return p->tbl->family;
  201. }
  202. #define NEIGH_PRIV_ALIGN sizeof(long long)
  203. #define NEIGH_ENTRY_SIZE(size) ALIGN((size), NEIGH_PRIV_ALIGN)
  204. static inline void *neighbour_priv(const struct neighbour *n)
  205. {
  206. return (char *)n + n->tbl->entry_size;
  207. }
  208. /* flags for neigh_update() */
  209. #define NEIGH_UPDATE_F_OVERRIDE 0x00000001
  210. #define NEIGH_UPDATE_F_WEAK_OVERRIDE 0x00000002
  211. #define NEIGH_UPDATE_F_OVERRIDE_ISROUTER 0x00000004
  212. #define NEIGH_UPDATE_F_ISROUTER 0x40000000
  213. #define NEIGH_UPDATE_F_ADMIN 0x80000000
  214. static inline bool neigh_key_eq16(const struct neighbour *n, const void *pkey)
  215. {
  216. return *(const u16 *)n->primary_key == *(const u16 *)pkey;
  217. }
  218. static inline bool neigh_key_eq32(const struct neighbour *n, const void *pkey)
  219. {
  220. return *(const u32 *)n->primary_key == *(const u32 *)pkey;
  221. }
  222. static inline bool neigh_key_eq128(const struct neighbour *n, const void *pkey)
  223. {
  224. const u32 *n32 = (const u32 *)n->primary_key;
  225. const u32 *p32 = pkey;
  226. return ((n32[0] ^ p32[0]) | (n32[1] ^ p32[1]) |
  227. (n32[2] ^ p32[2]) | (n32[3] ^ p32[3])) == 0;
  228. }
  229. static inline struct neighbour *___neigh_lookup_noref(
  230. struct neigh_table *tbl,
  231. bool (*key_eq)(const struct neighbour *n, const void *pkey),
  232. __u32 (*hash)(const void *pkey,
  233. const struct net_device *dev,
  234. __u32 *hash_rnd),
  235. const void *pkey,
  236. struct net_device *dev)
  237. {
  238. struct neigh_hash_table *nht = rcu_dereference_bh(tbl->nht);
  239. struct neighbour *n;
  240. u32 hash_val;
  241. hash_val = hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
  242. for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
  243. n != NULL;
  244. n = rcu_dereference_bh(n->next)) {
  245. if (n->dev == dev && key_eq(n, pkey))
  246. return n;
  247. }
  248. return NULL;
  249. }
  250. static inline struct neighbour *__neigh_lookup_noref(struct neigh_table *tbl,
  251. const void *pkey,
  252. struct net_device *dev)
  253. {
  254. return ___neigh_lookup_noref(tbl, tbl->key_eq, tbl->hash, pkey, dev);
  255. }
  256. void neigh_table_init(int index, struct neigh_table *tbl);
  257. int neigh_table_clear(int index, struct neigh_table *tbl);
  258. struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
  259. struct net_device *dev);
  260. struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
  261. const void *pkey);
  262. struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
  263. struct net_device *dev, bool want_ref);
  264. static inline struct neighbour *neigh_create(struct neigh_table *tbl,
  265. const void *pkey,
  266. struct net_device *dev)
  267. {
  268. return __neigh_create(tbl, pkey, dev, true);
  269. }
  270. void neigh_destroy(struct neighbour *neigh);
  271. int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
  272. int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, u32 flags,
  273. u32 nlmsg_pid);
  274. void __neigh_set_probe_once(struct neighbour *neigh);
  275. bool neigh_remove_one(struct neighbour *ndel, struct neigh_table *tbl);
  276. void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
  277. int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
  278. int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb);
  279. int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb);
  280. int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb);
  281. struct neighbour *neigh_event_ns(struct neigh_table *tbl,
  282. u8 *lladdr, void *saddr,
  283. struct net_device *dev);
  284. struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
  285. struct neigh_table *tbl);
  286. void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
  287. static inline
  288. struct net *neigh_parms_net(const struct neigh_parms *parms)
  289. {
  290. return read_pnet(&parms->net);
  291. }
  292. unsigned long neigh_rand_reach_time(unsigned long base);
  293. void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
  294. struct sk_buff *skb);
  295. struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, struct net *net,
  296. const void *key, struct net_device *dev,
  297. int creat);
  298. struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl, struct net *net,
  299. const void *key, struct net_device *dev);
  300. int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *key,
  301. struct net_device *dev);
  302. static inline struct net *pneigh_net(const struct pneigh_entry *pneigh)
  303. {
  304. return read_pnet(&pneigh->net);
  305. }
  306. void neigh_app_ns(struct neighbour *n);
  307. void neigh_for_each(struct neigh_table *tbl,
  308. void (*cb)(struct neighbour *, void *), void *cookie);
  309. void __neigh_for_each_release(struct neigh_table *tbl,
  310. int (*cb)(struct neighbour *));
  311. int neigh_xmit(int fam, struct net_device *, const void *, struct sk_buff *);
  312. void pneigh_for_each(struct neigh_table *tbl,
  313. void (*cb)(struct pneigh_entry *));
  314. struct neigh_seq_state {
  315. struct seq_net_private p;
  316. struct neigh_table *tbl;
  317. struct neigh_hash_table *nht;
  318. void *(*neigh_sub_iter)(struct neigh_seq_state *state,
  319. struct neighbour *n, loff_t *pos);
  320. unsigned int bucket;
  321. unsigned int flags;
  322. #define NEIGH_SEQ_NEIGH_ONLY 0x00000001
  323. #define NEIGH_SEQ_IS_PNEIGH 0x00000002
  324. #define NEIGH_SEQ_SKIP_NOARP 0x00000004
  325. };
  326. void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *,
  327. unsigned int);
  328. void *neigh_seq_next(struct seq_file *, void *, loff_t *);
  329. void neigh_seq_stop(struct seq_file *, void *);
  330. int neigh_proc_dointvec(struct ctl_table *ctl, int write,
  331. void __user *buffer, size_t *lenp, loff_t *ppos);
  332. int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
  333. void __user *buffer,
  334. size_t *lenp, loff_t *ppos);
  335. int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
  336. void __user *buffer,
  337. size_t *lenp, loff_t *ppos);
  338. int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
  339. proc_handler *proc_handler);
  340. void neigh_sysctl_unregister(struct neigh_parms *p);
  341. static inline void __neigh_parms_put(struct neigh_parms *parms)
  342. {
  343. refcount_dec(&parms->refcnt);
  344. }
  345. static inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms)
  346. {
  347. refcount_inc(&parms->refcnt);
  348. return parms;
  349. }
  350. /*
  351. * Neighbour references
  352. */
  353. static inline void neigh_release(struct neighbour *neigh)
  354. {
  355. if (refcount_dec_and_test(&neigh->refcnt))
  356. neigh_destroy(neigh);
  357. }
  358. static inline struct neighbour * neigh_clone(struct neighbour *neigh)
  359. {
  360. if (neigh)
  361. refcount_inc(&neigh->refcnt);
  362. return neigh;
  363. }
  364. #define neigh_hold(n) refcount_inc(&(n)->refcnt)
  365. static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
  366. {
  367. unsigned long now = jiffies;
  368. if (READ_ONCE(neigh->used) != now)
  369. WRITE_ONCE(neigh->used, now);
  370. if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
  371. return __neigh_event_send(neigh, skb);
  372. return 0;
  373. }
  374. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  375. static inline int neigh_hh_bridge(struct hh_cache *hh, struct sk_buff *skb)
  376. {
  377. unsigned int seq, hh_alen;
  378. do {
  379. seq = read_seqbegin(&hh->hh_lock);
  380. hh_alen = HH_DATA_ALIGN(ETH_HLEN);
  381. memcpy(skb->data - hh_alen, hh->hh_data, ETH_ALEN + hh_alen - ETH_HLEN);
  382. } while (read_seqretry(&hh->hh_lock, seq));
  383. return 0;
  384. }
  385. #endif
  386. static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
  387. {
  388. unsigned int hh_alen = 0;
  389. unsigned int seq;
  390. unsigned int hh_len;
  391. do {
  392. seq = read_seqbegin(&hh->hh_lock);
  393. hh_len = READ_ONCE(hh->hh_len);
  394. if (likely(hh_len <= HH_DATA_MOD)) {
  395. hh_alen = HH_DATA_MOD;
  396. /* skb_push() would proceed silently if we have room for
  397. * the unaligned size but not for the aligned size:
  398. * check headroom explicitly.
  399. */
  400. if (likely(skb_headroom(skb) >= HH_DATA_MOD)) {
  401. /* this is inlined by gcc */
  402. memcpy(skb->data - HH_DATA_MOD, hh->hh_data,
  403. HH_DATA_MOD);
  404. }
  405. } else {
  406. hh_alen = HH_DATA_ALIGN(hh_len);
  407. if (likely(skb_headroom(skb) >= hh_alen)) {
  408. memcpy(skb->data - hh_alen, hh->hh_data,
  409. hh_alen);
  410. }
  411. }
  412. } while (read_seqretry(&hh->hh_lock, seq));
  413. if (WARN_ON_ONCE(skb_headroom(skb) < hh_alen)) {
  414. kfree_skb(skb);
  415. return NET_XMIT_DROP;
  416. }
  417. __skb_push(skb, hh_len);
  418. return dev_queue_xmit(skb);
  419. }
  420. static inline int neigh_output(struct neighbour *n, struct sk_buff *skb)
  421. {
  422. const struct hh_cache *hh = &n->hh;
  423. if ((n->nud_state & NUD_CONNECTED) && hh->hh_len)
  424. return neigh_hh_output(hh, skb);
  425. else
  426. return n->output(n, skb);
  427. }
  428. static inline struct neighbour *
  429. __neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
  430. {
  431. struct neighbour *n = neigh_lookup(tbl, pkey, dev);
  432. if (n || !creat)
  433. return n;
  434. n = neigh_create(tbl, pkey, dev);
  435. return IS_ERR(n) ? NULL : n;
  436. }
  437. static inline struct neighbour *
  438. __neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
  439. struct net_device *dev)
  440. {
  441. struct neighbour *n = neigh_lookup(tbl, pkey, dev);
  442. if (n)
  443. return n;
  444. return neigh_create(tbl, pkey, dev);
  445. }
  446. struct neighbour_cb {
  447. unsigned long sched_next;
  448. unsigned int flags;
  449. };
  450. #define LOCALLY_ENQUEUED 0x1
  451. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  452. static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
  453. const struct net_device *dev)
  454. {
  455. unsigned int seq;
  456. do {
  457. seq = read_seqbegin(&n->ha_lock);
  458. memcpy(dst, n->ha, dev->addr_len);
  459. } while (read_seqretry(&n->ha_lock, seq));
  460. }
  461. #endif