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 *net;
  31. struct net_device *dev;
  32. struct neigh_parms *next;
  33. int (*neigh_setup)(struct neighbour *);
  34. void (*neigh_cleanup)(struct neighbour *);
  35. struct neigh_table *tbl;
  36. void *sysctl_table;
  37. int dead;
  38. atomic_t refcnt;
  39. struct rcu_head rcu_head;
  40. int base_reachable_time;
  41. int retrans_time;
  42. int gc_staletime;
  43. int reachable_time;
  44. int delay_probe_time;
  45. int queue_len;
  46. int ucast_probes;
  47. int app_probes;
  48. int mcast_probes;
  49. int anycast_delay;
  50. int proxy_delay;
  51. int proxy_qlen;
  52. int locktime;
  53. };
  54. struct neigh_statistics
  55. {
  56. unsigned long allocs; /* number of allocated neighs */
  57. unsigned long destroys; /* number of destroyed neighs */
  58. unsigned long hash_grows; /* number of hash resizes */
  59. unsigned long res_failed; /* nomber of failed resolutions */
  60. unsigned long lookups; /* number of lookups */
  61. unsigned long hits; /* number of hits (among lookups) */
  62. unsigned long rcv_probes_mcast; /* number of received mcast ipv6 */
  63. unsigned long rcv_probes_ucast; /* number of received ucast ipv6 */
  64. unsigned long periodic_gc_runs; /* number of periodic GC runs */
  65. unsigned long forced_gc_runs; /* number of forced GC runs */
  66. };
  67. #define NEIGH_CACHE_STAT_INC(tbl, field) \
  68. do { \
  69. preempt_disable(); \
  70. (per_cpu_ptr((tbl)->stats, smp_processor_id())->field)++; \
  71. preempt_enable(); \
  72. } while (0)
  73. struct neighbour
  74. {
  75. struct neighbour *next;
  76. struct neigh_table *tbl;
  77. struct neigh_parms *parms;
  78. struct net_device *dev;
  79. unsigned long used;
  80. unsigned long confirmed;
  81. unsigned long updated;
  82. __u8 flags;
  83. __u8 nud_state;
  84. __u8 type;
  85. __u8 dead;
  86. atomic_t probes;
  87. rwlock_t lock;
  88. unsigned char ha[ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))];
  89. struct hh_cache *hh;
  90. atomic_t refcnt;
  91. int (*output)(struct sk_buff *skb);
  92. struct sk_buff_head arp_queue;
  93. struct timer_list timer;
  94. struct neigh_ops *ops;
  95. u8 primary_key[0];
  96. };
  97. struct neigh_ops
  98. {
  99. int family;
  100. void (*solicit)(struct neighbour *, struct sk_buff*);
  101. void (*error_report)(struct neighbour *, struct sk_buff*);
  102. int (*output)(struct sk_buff*);
  103. int (*connected_output)(struct sk_buff*);
  104. int (*hh_output)(struct sk_buff*);
  105. int (*queue_xmit)(struct sk_buff*);
  106. };
  107. struct pneigh_entry
  108. {
  109. struct pneigh_entry *next;
  110. struct net *net;
  111. struct net_device *dev;
  112. u8 flags;
  113. u8 key[0];
  114. };
  115. /*
  116. * neighbour table manipulation
  117. */
  118. struct neigh_table
  119. {
  120. struct neigh_table *next;
  121. int family;
  122. int entry_size;
  123. int key_len;
  124. __u32 (*hash)(const void *pkey, const struct net_device *);
  125. int (*constructor)(struct neighbour *);
  126. int (*pconstructor)(struct pneigh_entry *);
  127. void (*pdestructor)(struct pneigh_entry *);
  128. void (*proxy_redo)(struct sk_buff *skb);
  129. char *id;
  130. struct neigh_parms parms;
  131. /* HACK. gc_* shoul follow parms without a gap! */
  132. int gc_interval;
  133. int gc_thresh1;
  134. int gc_thresh2;
  135. int gc_thresh3;
  136. unsigned long last_flush;
  137. struct timer_list gc_timer;
  138. struct timer_list proxy_timer;
  139. struct sk_buff_head proxy_queue;
  140. atomic_t entries;
  141. rwlock_t lock;
  142. unsigned long last_rand;
  143. struct kmem_cache *kmem_cachep;
  144. struct neigh_statistics *stats;
  145. struct neighbour **hash_buckets;
  146. unsigned int hash_mask;
  147. __u32 hash_rnd;
  148. unsigned int hash_chain_gc;
  149. struct pneigh_entry **phash_buckets;
  150. #ifdef CONFIG_PROC_FS
  151. struct proc_dir_entry *pde;
  152. #endif
  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. extern void neigh_parms_destroy(struct neigh_parms *parms);
  187. extern unsigned long neigh_rand_reach_time(unsigned long base);
  188. extern void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
  189. struct sk_buff *skb);
  190. extern struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, struct net *net, const void *key, struct net_device *dev, int creat);
  191. extern int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *key, struct net_device *dev);
  192. extern void neigh_app_ns(struct neighbour *n);
  193. extern void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie);
  194. extern void __neigh_for_each_release(struct neigh_table *tbl, int (*cb)(struct neighbour *));
  195. extern void pneigh_for_each(struct neigh_table *tbl, void (*cb)(struct pneigh_entry *));
  196. struct neigh_seq_state {
  197. struct seq_net_private p;
  198. struct neigh_table *tbl;
  199. void *(*neigh_sub_iter)(struct neigh_seq_state *state,
  200. struct neighbour *n, loff_t *pos);
  201. unsigned int bucket;
  202. unsigned int flags;
  203. #define NEIGH_SEQ_NEIGH_ONLY 0x00000001
  204. #define NEIGH_SEQ_IS_PNEIGH 0x00000002
  205. #define NEIGH_SEQ_SKIP_NOARP 0x00000004
  206. };
  207. extern void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *, unsigned int);
  208. extern void *neigh_seq_next(struct seq_file *, void *, loff_t *);
  209. extern void neigh_seq_stop(struct seq_file *, void *);
  210. extern int neigh_sysctl_register(struct net_device *dev,
  211. struct neigh_parms *p,
  212. int p_id, int pdev_id,
  213. char *p_name,
  214. proc_handler *proc_handler,
  215. ctl_handler *strategy);
  216. extern void neigh_sysctl_unregister(struct neigh_parms *p);
  217. static inline void __neigh_parms_put(struct neigh_parms *parms)
  218. {
  219. atomic_dec(&parms->refcnt);
  220. }
  221. static inline void neigh_parms_put(struct neigh_parms *parms)
  222. {
  223. if (atomic_dec_and_test(&parms->refcnt))
  224. neigh_parms_destroy(parms);
  225. }
  226. static inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms)
  227. {
  228. atomic_inc(&parms->refcnt);
  229. return parms;
  230. }
  231. /*
  232. * Neighbour references
  233. */
  234. static inline void neigh_release(struct neighbour *neigh)
  235. {
  236. if (atomic_dec_and_test(&neigh->refcnt))
  237. neigh_destroy(neigh);
  238. }
  239. static inline struct neighbour * neigh_clone(struct neighbour *neigh)
  240. {
  241. if (neigh)
  242. atomic_inc(&neigh->refcnt);
  243. return neigh;
  244. }
  245. #define neigh_hold(n) atomic_inc(&(n)->refcnt)
  246. static inline void neigh_confirm(struct neighbour *neigh)
  247. {
  248. if (neigh)
  249. neigh->confirmed = jiffies;
  250. }
  251. static inline int neigh_is_connected(struct neighbour *neigh)
  252. {
  253. return neigh->nud_state&NUD_CONNECTED;
  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