neighbour.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 <asm/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/err.h>
  22. #include <linux/sysctl.h>
  23. #include <linux/workqueue.h>
  24. #include <net/rtnetlink.h>
  25. /*
  26. * NUD stands for "neighbor unreachability detection"
  27. */
  28. #define NUD_IN_TIMER (NUD_INCOMPLETE|NUD_REACHABLE|NUD_DELAY|NUD_PROBE)
  29. #define NUD_VALID (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
  30. #define NUD_CONNECTED (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)
  31. struct neighbour;
  32. struct neigh_parms
  33. {
  34. #ifdef CONFIG_NET_NS
  35. struct net *net;
  36. #endif
  37. struct net_device *dev;
  38. struct neigh_parms *next;
  39. int (*neigh_setup)(struct neighbour *);
  40. void (*neigh_cleanup)(struct neighbour *);
  41. struct neigh_table *tbl;
  42. void *sysctl_table;
  43. int dead;
  44. atomic_t refcnt;
  45. struct rcu_head rcu_head;
  46. int base_reachable_time;
  47. int retrans_time;
  48. int gc_staletime;
  49. int reachable_time;
  50. int delay_probe_time;
  51. int queue_len;
  52. int ucast_probes;
  53. int app_probes;
  54. int mcast_probes;
  55. int anycast_delay;
  56. int proxy_delay;
  57. int proxy_qlen;
  58. int locktime;
  59. };
  60. struct neigh_statistics
  61. {
  62. unsigned long allocs; /* number of allocated neighs */
  63. unsigned long destroys; /* number of destroyed neighs */
  64. unsigned long hash_grows; /* number of hash resizes */
  65. unsigned long res_failed; /* number of failed resolutions */
  66. unsigned long lookups; /* number of lookups */
  67. unsigned long hits; /* number of hits (among lookups) */
  68. unsigned long rcv_probes_mcast; /* number of received mcast ipv6 */
  69. unsigned long rcv_probes_ucast; /* number of received ucast ipv6 */
  70. unsigned long periodic_gc_runs; /* number of periodic GC runs */
  71. unsigned long forced_gc_runs; /* number of forced GC runs */
  72. unsigned long unres_discards; /* number of unresolved drops */
  73. };
  74. #define NEIGH_CACHE_STAT_INC(tbl, field) this_cpu_inc((tbl)->stats->field)
  75. struct neighbour
  76. {
  77. struct neighbour *next;
  78. struct neigh_table *tbl;
  79. struct neigh_parms *parms;
  80. struct net_device *dev;
  81. unsigned long used;
  82. unsigned long confirmed;
  83. unsigned long updated;
  84. __u8 flags;
  85. __u8 nud_state;
  86. __u8 type;
  87. __u8 dead;
  88. atomic_t probes;
  89. rwlock_t lock;
  90. unsigned char ha[ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))];
  91. struct hh_cache *hh;
  92. atomic_t refcnt;
  93. int (*output)(struct sk_buff *skb);
  94. struct sk_buff_head arp_queue;
  95. struct timer_list timer;
  96. const struct neigh_ops *ops;
  97. u8 primary_key[0];
  98. };
  99. struct neigh_ops
  100. {
  101. int family;
  102. void (*solicit)(struct neighbour *, struct sk_buff*);
  103. void (*error_report)(struct neighbour *, struct sk_buff*);
  104. int (*output)(struct sk_buff*);
  105. int (*connected_output)(struct sk_buff*);
  106. int (*hh_output)(struct sk_buff*);
  107. int (*queue_xmit)(struct sk_buff*);
  108. };
  109. struct pneigh_entry
  110. {
  111. struct pneigh_entry *next;
  112. #ifdef CONFIG_NET_NS
  113. struct net *net;
  114. #endif
  115. struct net_device *dev;
  116. u8 flags;
  117. u8 key[0];
  118. };
  119. /*
  120. * neighbour table manipulation
  121. */
  122. struct neigh_table
  123. {
  124. struct neigh_table *next;
  125. int family;
  126. int entry_size;
  127. int key_len;
  128. __u32 (*hash)(const void *pkey, const struct net_device *);
  129. int (*constructor)(struct neighbour *);
  130. int (*pconstructor)(struct pneigh_entry *);
  131. void (*pdestructor)(struct pneigh_entry *);
  132. void (*proxy_redo)(struct sk_buff *skb);
  133. char *id;
  134. struct neigh_parms parms;
  135. /* HACK. gc_* shoul follow parms without a gap! */
  136. int gc_interval;
  137. int gc_thresh1;
  138. int gc_thresh2;
  139. int gc_thresh3;
  140. unsigned long last_flush;
  141. struct delayed_work gc_work;
  142. struct timer_list proxy_timer;
  143. struct sk_buff_head proxy_queue;
  144. atomic_t entries;
  145. rwlock_t lock;
  146. unsigned long last_rand;
  147. struct kmem_cache *kmem_cachep;
  148. struct neigh_statistics *stats;
  149. struct neighbour **hash_buckets;
  150. unsigned int hash_mask;
  151. __u32 hash_rnd;
  152. struct pneigh_entry **phash_buckets;
  153. };
  154. /* flags for neigh_update() */
  155. #define NEIGH_UPDATE_F_OVERRIDE 0x00000001
  156. #define NEIGH_UPDATE_F_WEAK_OVERRIDE 0x00000002
  157. #define NEIGH_UPDATE_F_OVERRIDE_ISROUTER 0x00000004
  158. #define NEIGH_UPDATE_F_ISROUTER 0x40000000
  159. #define NEIGH_UPDATE_F_ADMIN 0x80000000
  160. extern void neigh_table_init(struct neigh_table *tbl);
  161. extern void neigh_table_init_no_netlink(struct neigh_table *tbl);
  162. extern int neigh_table_clear(struct neigh_table *tbl);
  163. extern struct neighbour * neigh_lookup(struct neigh_table *tbl,
  164. const void *pkey,
  165. struct net_device *dev);
  166. extern struct neighbour * neigh_lookup_nodev(struct neigh_table *tbl,
  167. struct net *net,
  168. const void *pkey);
  169. extern struct neighbour * neigh_create(struct neigh_table *tbl,
  170. const void *pkey,
  171. struct net_device *dev);
  172. extern void neigh_destroy(struct neighbour *neigh);
  173. extern int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
  174. extern int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
  175. u32 flags);
  176. extern void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
  177. extern int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
  178. extern int neigh_resolve_output(struct sk_buff *skb);
  179. extern int neigh_connected_output(struct sk_buff *skb);
  180. extern int neigh_compat_output(struct sk_buff *skb);
  181. extern struct neighbour *neigh_event_ns(struct neigh_table *tbl,
  182. u8 *lladdr, void *saddr,
  183. struct net_device *dev);
  184. extern struct neigh_parms *neigh_parms_alloc(struct net_device *dev, struct neigh_table *tbl);
  185. extern void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
  186. static inline
  187. struct net *neigh_parms_net(const struct neigh_parms *parms)
  188. {
  189. return read_pnet(&parms->net);
  190. }
  191. extern unsigned long neigh_rand_reach_time(unsigned long base);
  192. extern void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
  193. struct sk_buff *skb);
  194. extern struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, struct net *net, const void *key, struct net_device *dev, int creat);
  195. extern struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
  196. struct net *net,
  197. const void *key,
  198. struct net_device *dev);
  199. extern int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *key, struct net_device *dev);
  200. static inline
  201. struct net *pneigh_net(const struct pneigh_entry *pneigh)
  202. {
  203. return read_pnet(&pneigh->net);
  204. }
  205. extern void neigh_app_ns(struct neighbour *n);
  206. extern void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie);
  207. extern void __neigh_for_each_release(struct neigh_table *tbl, int (*cb)(struct neighbour *));
  208. extern void pneigh_for_each(struct neigh_table *tbl, void (*cb)(struct pneigh_entry *));
  209. struct neigh_seq_state {
  210. struct seq_net_private p;
  211. struct neigh_table *tbl;
  212. void *(*neigh_sub_iter)(struct neigh_seq_state *state,
  213. struct neighbour *n, loff_t *pos);
  214. unsigned int bucket;
  215. unsigned int flags;
  216. #define NEIGH_SEQ_NEIGH_ONLY 0x00000001
  217. #define NEIGH_SEQ_IS_PNEIGH 0x00000002
  218. #define NEIGH_SEQ_SKIP_NOARP 0x00000004
  219. };
  220. extern void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *, unsigned int);
  221. extern void *neigh_seq_next(struct seq_file *, void *, loff_t *);
  222. extern void neigh_seq_stop(struct seq_file *, void *);
  223. extern int neigh_sysctl_register(struct net_device *dev,
  224. struct neigh_parms *p,
  225. int p_id, int pdev_id,
  226. char *p_name,
  227. proc_handler *proc_handler,
  228. ctl_handler *strategy);
  229. extern void neigh_sysctl_unregister(struct neigh_parms *p);
  230. static inline void __neigh_parms_put(struct neigh_parms *parms)
  231. {
  232. atomic_dec(&parms->refcnt);
  233. }
  234. static inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms)
  235. {
  236. atomic_inc(&parms->refcnt);
  237. return parms;
  238. }
  239. /*
  240. * Neighbour references
  241. */
  242. static inline void neigh_release(struct neighbour *neigh)
  243. {
  244. if (atomic_dec_and_test(&neigh->refcnt))
  245. neigh_destroy(neigh);
  246. }
  247. static inline struct neighbour * neigh_clone(struct neighbour *neigh)
  248. {
  249. if (neigh)
  250. atomic_inc(&neigh->refcnt);
  251. return neigh;
  252. }
  253. #define neigh_hold(n) atomic_inc(&(n)->refcnt)
  254. static inline void neigh_confirm(struct neighbour *neigh)
  255. {
  256. if (neigh)
  257. neigh->confirmed = jiffies;
  258. }
  259. static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
  260. {
  261. neigh->used = jiffies;
  262. if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
  263. return __neigh_event_send(neigh, skb);
  264. return 0;
  265. }
  266. static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
  267. {
  268. unsigned seq;
  269. int hh_len;
  270. do {
  271. int hh_alen;
  272. seq = read_seqbegin(&hh->hh_lock);
  273. hh_len = hh->hh_len;
  274. hh_alen = HH_DATA_ALIGN(hh_len);
  275. memcpy(skb->data - hh_alen, hh->hh_data, hh_alen);
  276. } while (read_seqretry(&hh->hh_lock, seq));
  277. skb_push(skb, hh_len);
  278. return hh->hh_output(skb);
  279. }
  280. static inline struct neighbour *
  281. __neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
  282. {
  283. struct neighbour *n = neigh_lookup(tbl, pkey, dev);
  284. if (n || !creat)
  285. return n;
  286. n = neigh_create(tbl, pkey, dev);
  287. return IS_ERR(n) ? NULL : n;
  288. }
  289. static inline struct neighbour *
  290. __neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
  291. struct net_device *dev)
  292. {
  293. struct neighbour *n = neigh_lookup(tbl, pkey, dev);
  294. if (n)
  295. return n;
  296. return neigh_create(tbl, pkey, dev);
  297. }
  298. struct neighbour_cb {
  299. unsigned long sched_next;
  300. unsigned int flags;
  301. };
  302. #define LOCALLY_ENQUEUED 0x1
  303. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  304. #endif