neighbour.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. void (*neigh_destructor)(struct neighbour *);
  58. struct neigh_table *tbl;
  59. void *sysctl_table;
  60. int dead;
  61. atomic_t refcnt;
  62. struct rcu_head rcu_head;
  63. int base_reachable_time;
  64. int retrans_time;
  65. int gc_staletime;
  66. int reachable_time;
  67. int delay_probe_time;
  68. int queue_len;
  69. int ucast_probes;
  70. int app_probes;
  71. int mcast_probes;
  72. int anycast_delay;
  73. int proxy_delay;
  74. int proxy_qlen;
  75. int locktime;
  76. };
  77. struct neigh_statistics
  78. {
  79. unsigned long allocs; /* number of allocated neighs */
  80. unsigned long destroys; /* number of destroyed neighs */
  81. unsigned long hash_grows; /* number of hash resizes */
  82. unsigned long res_failed; /* nomber of failed resolutions */
  83. unsigned long lookups; /* number of lookups */
  84. unsigned long hits; /* number of hits (among lookups) */
  85. unsigned long rcv_probes_mcast; /* number of received mcast ipv6 */
  86. unsigned long rcv_probes_ucast; /* number of received ucast ipv6 */
  87. unsigned long periodic_gc_runs; /* number of periodic GC runs */
  88. unsigned long forced_gc_runs; /* number of forced GC runs */
  89. };
  90. #define NEIGH_CACHE_STAT_INC(tbl, field) \
  91. do { \
  92. preempt_disable(); \
  93. (per_cpu_ptr((tbl)->stats, smp_processor_id())->field)++; \
  94. preempt_enable(); \
  95. } while (0)
  96. struct neighbour
  97. {
  98. struct neighbour *next;
  99. struct neigh_table *tbl;
  100. struct neigh_parms *parms;
  101. struct net_device *dev;
  102. unsigned long used;
  103. unsigned long confirmed;
  104. unsigned long updated;
  105. __u8 flags;
  106. __u8 nud_state;
  107. __u8 type;
  108. __u8 dead;
  109. atomic_t probes;
  110. rwlock_t lock;
  111. unsigned char ha[(MAX_ADDR_LEN+sizeof(unsigned long)-1)&~(sizeof(unsigned long)-1)];
  112. struct hh_cache *hh;
  113. atomic_t refcnt;
  114. int (*output)(struct sk_buff *skb);
  115. struct sk_buff_head arp_queue;
  116. struct timer_list timer;
  117. struct neigh_ops *ops;
  118. u8 primary_key[0];
  119. };
  120. struct neigh_ops
  121. {
  122. int family;
  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 void neigh_table_init_no_netlink(struct neigh_table *tbl);
  183. extern int neigh_table_clear(struct neigh_table *tbl);
  184. extern struct neighbour * neigh_lookup(struct neigh_table *tbl,
  185. const void *pkey,
  186. struct net_device *dev);
  187. extern struct neighbour * neigh_lookup_nodev(struct neigh_table *tbl,
  188. const void *pkey);
  189. extern struct neighbour * neigh_create(struct neigh_table *tbl,
  190. const void *pkey,
  191. struct net_device *dev);
  192. extern void neigh_destroy(struct neighbour *neigh);
  193. extern int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
  194. extern int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
  195. u32 flags);
  196. extern void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
  197. extern int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
  198. extern int neigh_resolve_output(struct sk_buff *skb);
  199. extern int neigh_connected_output(struct sk_buff *skb);
  200. extern int neigh_compat_output(struct sk_buff *skb);
  201. extern struct neighbour *neigh_event_ns(struct neigh_table *tbl,
  202. u8 *lladdr, void *saddr,
  203. struct net_device *dev);
  204. extern struct neigh_parms *neigh_parms_alloc(struct net_device *dev, struct neigh_table *tbl);
  205. extern void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
  206. extern void neigh_parms_destroy(struct neigh_parms *parms);
  207. extern unsigned long neigh_rand_reach_time(unsigned long base);
  208. extern void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
  209. struct sk_buff *skb);
  210. extern struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, const void *key, struct net_device *dev, int creat);
  211. extern int pneigh_delete(struct neigh_table *tbl, const void *key, struct net_device *dev);
  212. struct netlink_callback;
  213. struct nlmsghdr;
  214. extern int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb);
  215. extern int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
  216. extern int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
  217. extern void neigh_app_ns(struct neighbour *n);
  218. extern int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb);
  219. extern int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
  220. extern void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie);
  221. extern void __neigh_for_each_release(struct neigh_table *tbl, int (*cb)(struct neighbour *));
  222. extern void pneigh_for_each(struct neigh_table *tbl, void (*cb)(struct pneigh_entry *));
  223. struct neigh_seq_state {
  224. struct neigh_table *tbl;
  225. void *(*neigh_sub_iter)(struct neigh_seq_state *state,
  226. struct neighbour *n, loff_t *pos);
  227. unsigned int bucket;
  228. unsigned int flags;
  229. #define NEIGH_SEQ_NEIGH_ONLY 0x00000001
  230. #define NEIGH_SEQ_IS_PNEIGH 0x00000002
  231. #define NEIGH_SEQ_SKIP_NOARP 0x00000004
  232. };
  233. extern void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *, unsigned int);
  234. extern void *neigh_seq_next(struct seq_file *, void *, loff_t *);
  235. extern void neigh_seq_stop(struct seq_file *, void *);
  236. extern int neigh_sysctl_register(struct net_device *dev,
  237. struct neigh_parms *p,
  238. int p_id, int pdev_id,
  239. char *p_name,
  240. proc_handler *proc_handler,
  241. ctl_handler *strategy);
  242. extern void neigh_sysctl_unregister(struct neigh_parms *p);
  243. static inline void __neigh_parms_put(struct neigh_parms *parms)
  244. {
  245. atomic_dec(&parms->refcnt);
  246. }
  247. static inline void neigh_parms_put(struct neigh_parms *parms)
  248. {
  249. if (atomic_dec_and_test(&parms->refcnt))
  250. neigh_parms_destroy(parms);
  251. }
  252. static inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms)
  253. {
  254. atomic_inc(&parms->refcnt);
  255. return parms;
  256. }
  257. /*
  258. * Neighbour references
  259. */
  260. static inline void neigh_release(struct neighbour *neigh)
  261. {
  262. if (atomic_dec_and_test(&neigh->refcnt))
  263. neigh_destroy(neigh);
  264. }
  265. static inline struct neighbour * neigh_clone(struct neighbour *neigh)
  266. {
  267. if (neigh)
  268. atomic_inc(&neigh->refcnt);
  269. return neigh;
  270. }
  271. #define neigh_hold(n) atomic_inc(&(n)->refcnt)
  272. static inline void neigh_confirm(struct neighbour *neigh)
  273. {
  274. if (neigh)
  275. neigh->confirmed = jiffies;
  276. }
  277. static inline int neigh_is_connected(struct neighbour *neigh)
  278. {
  279. return neigh->nud_state&NUD_CONNECTED;
  280. }
  281. static inline int neigh_is_valid(struct neighbour *neigh)
  282. {
  283. return neigh->nud_state&NUD_VALID;
  284. }
  285. static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
  286. {
  287. neigh->used = jiffies;
  288. if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
  289. return __neigh_event_send(neigh, skb);
  290. return 0;
  291. }
  292. static inline struct neighbour *
  293. __neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
  294. {
  295. struct neighbour *n = neigh_lookup(tbl, pkey, dev);
  296. if (n || !creat)
  297. return n;
  298. n = neigh_create(tbl, pkey, dev);
  299. return IS_ERR(n) ? NULL : n;
  300. }
  301. static inline struct neighbour *
  302. __neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
  303. struct net_device *dev)
  304. {
  305. struct neighbour *n = neigh_lookup(tbl, pkey, dev);
  306. if (n)
  307. return n;
  308. return neigh_create(tbl, pkey, dev);
  309. }
  310. struct neighbour_cb {
  311. unsigned long sched_next;
  312. unsigned int flags;
  313. };
  314. #define LOCALLY_ENQUEUED 0x1
  315. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  316. #endif
  317. #endif