neighbour.h 9.9 KB

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