neighbour.h 9.7 KB

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