neighbour.h
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:7k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. #ifndef _NET_NEIGHBOUR_H
  2. #define _NET_NEIGHBOUR_H
  3. /*
  4.  * Generic neighbour manipulation
  5.  *
  6.  * Authors:
  7.  * Pedro Roque <roque@di.fc.ul.pt>
  8.  * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  9.  */
  10. /* The following flags & states are exported to user space,
  11.    so that they should be moved to include/linux/ directory.
  12.  */
  13. /*
  14.  * Neighbor Cache Entry Flags
  15.  */
  16. #define NTF_PROXY 0x08 /* == ATF_PUBL */
  17. #define NTF_ROUTER 0x80
  18. /*
  19.  * Neighbor Cache Entry States.
  20.  */
  21. #define NUD_INCOMPLETE 0x01
  22. #define NUD_REACHABLE 0x02
  23. #define NUD_STALE 0x04
  24. #define NUD_DELAY 0x08
  25. #define NUD_PROBE 0x10
  26. #define NUD_FAILED 0x20
  27. /* Dummy states */
  28. #define NUD_NOARP 0x40
  29. #define NUD_PERMANENT 0x80
  30. #define NUD_NONE 0x00
  31. /* NUD_NOARP & NUD_PERMANENT are pseudostates, they never change
  32.    and make no address resolution or NUD.
  33.    NUD_PERMANENT is also cannot be deleted by garbage collectors.
  34.  */
  35. #ifdef __KERNEL__
  36. #include <asm/atomic.h>
  37. #include <linux/skbuff.h>
  38. #define NUD_IN_TIMER (NUD_INCOMPLETE|NUD_DELAY|NUD_PROBE)
  39. #define NUD_VALID (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
  40. #define NUD_CONNECTED (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)
  41. struct neigh_parms
  42. {
  43. struct neigh_parms *next;
  44. int (*neigh_setup)(struct neighbour *);
  45. struct neigh_table *tbl;
  46. int entries;
  47. void *priv;
  48. void *sysctl_table;
  49. int base_reachable_time;
  50. int retrans_time;
  51. int gc_staletime;
  52. int reachable_time;
  53. int delay_probe_time;
  54. int queue_len;
  55. int ucast_probes;
  56. int app_probes;
  57. int mcast_probes;
  58. int anycast_delay;
  59. int proxy_delay;
  60. int proxy_qlen;
  61. int locktime;
  62. };
  63. struct neigh_statistics
  64. {
  65. unsigned long allocs;
  66. unsigned long res_failed;
  67. unsigned long rcv_probes_mcast;
  68. unsigned long rcv_probes_ucast;
  69. };
  70. struct neighbour
  71. {
  72. struct neighbour *next;
  73. struct neigh_table *tbl;
  74. struct neigh_parms *parms;
  75. struct net_device *dev;
  76. unsigned long used;
  77. unsigned long confirmed;
  78. unsigned long updated;
  79. __u8 flags;
  80. __u8 nud_state;
  81. __u8 type;
  82. __u8 dead;
  83. atomic_t probes;
  84. rwlock_t lock;
  85. unsigned char ha[(MAX_ADDR_LEN+sizeof(unsigned long)-1)&~(sizeof(unsigned long)-1)];
  86. struct hh_cache *hh;
  87. atomic_t refcnt;
  88. int (*output)(struct sk_buff *skb);
  89. struct sk_buff_head arp_queue;
  90. struct timer_list timer;
  91. struct neigh_ops *ops;
  92. u8 primary_key[0];
  93. };
  94. struct neigh_ops
  95. {
  96. int family;
  97. void (*destructor)(struct neighbour *);
  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 key[0];
  110. };
  111. #define NEIGH_HASHMASK 0x1F
  112. #define PNEIGH_HASHMASK 0xF
  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. int entries;
  139. rwlock_t lock;
  140. unsigned long last_rand;
  141. struct neigh_parms *parms_list;
  142. kmem_cache_t *kmem_cachep;
  143. struct tasklet_struct gc_task;
  144. struct neigh_statistics stats;
  145. struct neighbour *hash_buckets[NEIGH_HASHMASK+1];
  146. struct pneigh_entry *phash_buckets[PNEIGH_HASHMASK+1];
  147. };
  148. extern void neigh_table_init(struct neigh_table *tbl);
  149. extern int neigh_table_clear(struct neigh_table *tbl);
  150. extern struct neighbour * neigh_lookup(struct neigh_table *tbl,
  151.      const void *pkey,
  152.      struct net_device *dev);
  153. extern struct neighbour * neigh_create(struct neigh_table *tbl,
  154.      const void *pkey,
  155.      struct net_device *dev);
  156. extern void neigh_destroy(struct neighbour *neigh);
  157. extern int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
  158. extern int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, int override, int arp);
  159. extern int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
  160. extern int neigh_resolve_output(struct sk_buff *skb);
  161. extern int neigh_connected_output(struct sk_buff *skb);
  162. extern int neigh_compat_output(struct sk_buff *skb);
  163. extern struct neighbour  *neigh_event_ns(struct neigh_table *tbl,
  164. u8 *lladdr, void *saddr,
  165. struct net_device *dev);
  166. extern struct neigh_parms *neigh_parms_alloc(struct net_device *dev, struct neigh_table *tbl);
  167. extern void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
  168. extern unsigned long neigh_rand_reach_time(unsigned long base);
  169. extern void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
  170.        struct sk_buff *skb);
  171. extern struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, const void *key, struct net_device *dev, int creat);
  172. extern int pneigh_delete(struct neigh_table *tbl, const void *key, struct net_device *dev);
  173. struct netlink_callback;
  174. struct nlmsghdr;
  175. extern int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb);
  176. extern int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
  177. extern int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
  178. extern void neigh_app_ns(struct neighbour *n);
  179. extern int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
  180.       int p_id, int pdev_id, char *p_name);
  181. extern void neigh_sysctl_unregister(struct neigh_parms *p);
  182. /*
  183.  * Neighbour references
  184.  */
  185. static inline void neigh_release(struct neighbour *neigh)
  186. {
  187. if (atomic_dec_and_test(&neigh->refcnt))
  188. neigh_destroy(neigh);
  189. }
  190. static inline struct neighbour * neigh_clone(struct neighbour *neigh)
  191. {
  192. if (neigh)
  193. atomic_inc(&neigh->refcnt);
  194. return neigh;
  195. }
  196. #define neigh_hold(n) atomic_inc(&(n)->refcnt)
  197. static inline void neigh_confirm(struct neighbour *neigh)
  198. {
  199. if (neigh)
  200. neigh->confirmed = jiffies;
  201. }
  202. static inline int neigh_is_connected(struct neighbour *neigh)
  203. {
  204. return neigh->nud_state&NUD_CONNECTED;
  205. }
  206. static inline int neigh_is_valid(struct neighbour *neigh)
  207. {
  208. return neigh->nud_state&NUD_VALID;
  209. }
  210. static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
  211. {
  212. neigh->used = jiffies;
  213. if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
  214. return __neigh_event_send(neigh, skb);
  215. return 0;
  216. }
  217. static inline struct neighbour *
  218. __neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
  219. {
  220. struct neighbour *n = neigh_lookup(tbl, pkey, dev);
  221. if (n || !creat)
  222. return n;
  223. n = neigh_create(tbl, pkey, dev);
  224. return IS_ERR(n) ? NULL : n;
  225. }
  226. static inline struct neighbour *
  227. __neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
  228.   struct net_device *dev)
  229. {
  230. struct neighbour *n = neigh_lookup(tbl, pkey, dev);
  231. if (n)
  232. return n;
  233. return neigh_create(tbl, pkey, dev);
  234. }
  235. #endif
  236. #endif