neighbour.h 9.4 KB

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