neighbour.h 9.5 KB

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