neighbour.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #ifndef _NET_NEIGHBOUR_H
  2. #define _NET_NEIGHBOUR_H
  3. /*
  4. * Generic neighbour manipulation
  5. *
  6. * Authors:
  7. * Pedro Roque <roque@di.fc.ul.pt>
  8. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  9. *
  10. * Changes:
  11. *
  12. * Harald Welte: <laforge@gnumonks.org>
  13. * - Add neighbour cache statistics like rtstat
  14. */
  15. /* The following flags & states are exported to user space,
  16. so that they should be moved to include/linux/ directory.
  17. */
  18. /*
  19. * Neighbor Cache Entry Flags
  20. */
  21. #define NTF_PROXY 0x08 /* == ATF_PUBL */
  22. #define NTF_ROUTER 0x80
  23. /*
  24. * Neighbor Cache Entry States.
  25. */
  26. #define NUD_INCOMPLETE 0x01
  27. #define NUD_REACHABLE 0x02
  28. #define NUD_STALE 0x04
  29. #define NUD_DELAY 0x08
  30. #define NUD_PROBE 0x10
  31. #define NUD_FAILED 0x20
  32. /* Dummy states */
  33. #define NUD_NOARP 0x40
  34. #define NUD_PERMANENT 0x80
  35. #define NUD_NONE 0x00
  36. /* NUD_NOARP & NUD_PERMANENT are pseudostates, they never change
  37. and make no address resolution or NUD.
  38. NUD_PERMANENT is also cannot be deleted by garbage collectors.
  39. */
  40. #ifdef __KERNEL__
  41. #include <asm/atomic.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/rcupdate.h>
  45. #include <linux/seq_file.h>
  46. #include <linux/err.h>
  47. #include <linux/sysctl.h>
  48. #define NUD_IN_TIMER (NUD_INCOMPLETE|NUD_REACHABLE|NUD_DELAY|NUD_PROBE)
  49. #define NUD_VALID (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
  50. #define NUD_CONNECTED (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)
  51. struct neighbour;
  52. struct neigh_parms
  53. {
  54. struct net_device *dev;
  55. struct neigh_parms *next;
  56. int (*neigh_setup)(struct neighbour *);
  57. struct neigh_table *tbl;
  58. void *sysctl_table;
  59. int dead;
  60. atomic_t refcnt;
  61. struct rcu_head rcu_head;
  62. int base_reachable_time;
  63. int retrans_time;
  64. int gc_staletime;
  65. int reachable_time;
  66. int delay_probe_time;
  67. int queue_len;
  68. int ucast_probes;
  69. int app_probes;
  70. int mcast_probes;
  71. int anycast_delay;
  72. int proxy_delay;
  73. int proxy_qlen;
  74. int locktime;
  75. };
  76. struct neigh_statistics
  77. {
  78. unsigned long allocs; /* number of allocated neighs */
  79. unsigned long destroys; /* number of destroyed neighs */
  80. unsigned long hash_grows; /* number of hash resizes */
  81. unsigned long res_failed; /* nomber of failed resolutions */
  82. unsigned long lookups; /* number of lookups */
  83. unsigned long hits; /* number of hits (among lookups) */
  84. unsigned long rcv_probes_mcast; /* number of received mcast ipv6 */
  85. unsigned long rcv_probes_ucast; /* number of received ucast ipv6 */
  86. unsigned long periodic_gc_runs; /* number of periodic GC runs */
  87. unsigned long forced_gc_runs; /* number of forced GC runs */
  88. };
  89. #define NEIGH_CACHE_STAT_INC(tbl, field) \
  90. do { \
  91. preempt_disable(); \
  92. (per_cpu_ptr((tbl)->stats, smp_processor_id())->field)++; \
  93. preempt_enable(); \
  94. } while (0)
  95. struct neighbour
  96. {
  97. struct neighbour *next;
  98. struct neigh_table *tbl;
  99. struct neigh_parms *parms;
  100. struct net_device *dev;
  101. unsigned long used;
  102. unsigned long confirmed;
  103. unsigned long updated;
  104. __u8 flags;
  105. __u8 nud_state;
  106. __u8 type;
  107. __u8 dead;
  108. atomic_t probes;
  109. rwlock_t lock;
  110. unsigned char ha[(MAX_ADDR_LEN+sizeof(unsigned long)-1)&~(sizeof(unsigned long)-1)];
  111. struct hh_cache *hh;
  112. atomic_t refcnt;
  113. int (*output)(struct sk_buff *skb);
  114. struct sk_buff_head arp_queue;
  115. struct timer_list timer;
  116. struct neigh_ops *ops;
  117. u8 primary_key[0];
  118. };
  119. struct neigh_ops
  120. {
  121. int family;
  122. void (*destructor)(struct neighbour *);
  123. void (*solicit)(struct neighbour *, struct sk_buff*);
  124. void (*error_report)(struct neighbour *, struct sk_buff*);
  125. int (*output)(struct sk_buff*);
  126. int (*connected_output)(struct sk_buff*);
  127. int (*hh_output)(struct sk_buff*);
  128. int (*queue_xmit)(struct sk_buff*);
  129. };
  130. struct pneigh_entry
  131. {
  132. struct pneigh_entry *next;
  133. struct net_device *dev;
  134. u8 key[0];
  135. };
  136. /*
  137. * neighbour table manipulation
  138. */
  139. struct neigh_table
  140. {
  141. struct neigh_table *next;
  142. int family;
  143. int entry_size;
  144. int key_len;
  145. __u32 (*hash)(const void *pkey, const struct net_device *);
  146. int (*constructor)(struct neighbour *);
  147. int (*pconstructor)(struct pneigh_entry *);
  148. void (*pdestructor)(struct pneigh_entry *);
  149. void (*proxy_redo)(struct sk_buff *skb);
  150. char *id;
  151. struct neigh_parms parms;
  152. /* HACK. gc_* shoul follow parms without a gap! */
  153. int gc_interval;
  154. int gc_thresh1;
  155. int gc_thresh2;
  156. int gc_thresh3;
  157. unsigned long last_flush;
  158. struct timer_list gc_timer;
  159. struct timer_list proxy_timer;
  160. struct sk_buff_head proxy_queue;
  161. atomic_t entries;
  162. rwlock_t lock;
  163. unsigned long last_rand;
  164. kmem_cache_t *kmem_cachep;
  165. struct neigh_statistics *stats;
  166. struct neighbour **hash_buckets;
  167. unsigned int hash_mask;
  168. __u32 hash_rnd;
  169. unsigned int hash_chain_gc;
  170. struct pneigh_entry **phash_buckets;
  171. #ifdef CONFIG_PROC_FS
  172. struct proc_dir_entry *pde;
  173. #endif
  174. };
  175. /* flags for neigh_update() */
  176. #define NEIGH_UPDATE_F_OVERRIDE 0x00000001
  177. #define NEIGH_UPDATE_F_WEAK_OVERRIDE 0x00000002
  178. #define NEIGH_UPDATE_F_OVERRIDE_ISROUTER 0x00000004
  179. #define NEIGH_UPDATE_F_ISROUTER 0x40000000
  180. #define NEIGH_UPDATE_F_ADMIN 0x80000000
  181. extern void neigh_table_init(struct neigh_table *tbl);
  182. extern int neigh_table_clear(struct neigh_table *tbl);
  183. extern struct neighbour * neigh_lookup(struct neigh_table *tbl,
  184. const void *pkey,
  185. struct net_device *dev);
  186. extern struct neighbour * neigh_lookup_nodev(struct neigh_table *tbl,
  187. const void *pkey);
  188. extern struct neighbour * neigh_create(struct neigh_table *tbl,
  189. const void *pkey,
  190. struct net_device *dev);
  191. extern void neigh_destroy(struct neighbour *neigh);
  192. extern int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
  193. extern int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
  194. u32 flags);
  195. extern void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
  196. extern int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
  197. extern int neigh_resolve_output(struct sk_buff *skb);
  198. extern int neigh_connected_output(struct sk_buff *skb);
  199. extern int neigh_compat_output(struct sk_buff *skb);
  200. extern struct neighbour *neigh_event_ns(struct neigh_table *tbl,
  201. u8 *lladdr, void *saddr,
  202. struct net_device *dev);
  203. extern struct neigh_parms *neigh_parms_alloc(struct net_device *dev, struct neigh_table *tbl);
  204. extern void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
  205. extern void neigh_parms_destroy(struct neigh_parms *parms);
  206. extern unsigned long neigh_rand_reach_time(unsigned long base);
  207. extern void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
  208. struct sk_buff *skb);
  209. extern struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, const void *key, struct net_device *dev, int creat);
  210. extern int pneigh_delete(struct neigh_table *tbl, const void *key, struct net_device *dev);
  211. struct netlink_callback;
  212. struct nlmsghdr;
  213. extern int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb);
  214. extern int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
  215. extern int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
  216. extern void neigh_app_ns(struct neighbour *n);
  217. extern int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb);
  218. extern int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
  219. extern void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie);
  220. extern void __neigh_for_each_release(struct neigh_table *tbl, int (*cb)(struct neighbour *));
  221. extern void pneigh_for_each(struct neigh_table *tbl, void (*cb)(struct pneigh_entry *));
  222. struct neigh_seq_state {
  223. struct neigh_table *tbl;
  224. void *(*neigh_sub_iter)(struct neigh_seq_state *state,
  225. struct neighbour *n, loff_t *pos);
  226. unsigned int bucket;
  227. unsigned int flags;
  228. #define NEIGH_SEQ_NEIGH_ONLY 0x00000001
  229. #define NEIGH_SEQ_IS_PNEIGH 0x00000002
  230. #define NEIGH_SEQ_SKIP_NOARP 0x00000004
  231. };
  232. extern void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *, unsigned int);
  233. extern void *neigh_seq_next(struct seq_file *, void *, loff_t *);
  234. extern void neigh_seq_stop(struct seq_file *, void *);
  235. extern int neigh_sysctl_register(struct net_device *dev,
  236. struct neigh_parms *p,
  237. int p_id, int pdev_id,
  238. char *p_name,
  239. proc_handler *proc_handler,
  240. ctl_handler *strategy);
  241. extern void neigh_sysctl_unregister(struct neigh_parms *p);
  242. static inline void __neigh_parms_put(struct neigh_parms *parms)
  243. {
  244. atomic_dec(&parms->refcnt);
  245. }
  246. static inline void neigh_parms_put(struct neigh_parms *parms)
  247. {
  248. if (atomic_dec_and_test(&parms->refcnt))
  249. neigh_parms_destroy(parms);
  250. }
  251. static inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms)
  252. {
  253. atomic_inc(&parms->refcnt);
  254. return parms;
  255. }
  256. /*
  257. * Neighbour references
  258. */
  259. static inline void neigh_release(struct neighbour *neigh)
  260. {
  261. if (atomic_dec_and_test(&neigh->refcnt))
  262. neigh_destroy(neigh);
  263. }
  264. static inline struct neighbour * neigh_clone(struct neighbour *neigh)
  265. {
  266. if (neigh)
  267. atomic_inc(&neigh->refcnt);
  268. return neigh;
  269. }
  270. #define neigh_hold(n) atomic_inc(&(n)->refcnt)
  271. static inline void neigh_confirm(struct neighbour *neigh)
  272. {
  273. if (neigh)
  274. neigh->confirmed = jiffies;
  275. }
  276. static inline int neigh_is_connected(struct neighbour *neigh)
  277. {
  278. return neigh->nud_state&NUD_CONNECTED;
  279. }
  280. static inline int neigh_is_valid(struct neighbour *neigh)
  281. {
  282. return neigh->nud_state&NUD_VALID;
  283. }
  284. static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
  285. {
  286. neigh->used = jiffies;
  287. if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
  288. return __neigh_event_send(neigh, skb);
  289. return 0;
  290. }
  291. static inline struct neighbour *
  292. __neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
  293. {
  294. struct neighbour *n = neigh_lookup(tbl, pkey, dev);
  295. if (n || !creat)
  296. return n;
  297. n = neigh_create(tbl, pkey, dev);
  298. return IS_ERR(n) ? NULL : n;
  299. }
  300. static inline struct neighbour *
  301. __neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
  302. struct net_device *dev)
  303. {
  304. struct neighbour *n = neigh_lookup(tbl, pkey, dev);
  305. if (n)
  306. return n;
  307. return neigh_create(tbl, pkey, dev);
  308. }
  309. struct neighbour_cb {
  310. unsigned long sched_next;
  311. unsigned int flags;
  312. };
  313. #define LOCALLY_ENQUEUED 0x1
  314. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  315. #endif
  316. #endif