neighbour.h 15 KB

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