neighbour.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 <linux/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. void (*neigh_cleanup)(struct neighbour *);
  39. struct neigh_table *tbl;
  40. void *sysctl_table;
  41. int dead;
  42. atomic_t refcnt;
  43. struct rcu_head rcu_head;
  44. int base_reachable_time;
  45. int retrans_time;
  46. int gc_staletime;
  47. int reachable_time;
  48. int delay_probe_time;
  49. int queue_len_bytes;
  50. int ucast_probes;
  51. int app_probes;
  52. int mcast_probes;
  53. int anycast_delay;
  54. int proxy_delay;
  55. int proxy_qlen;
  56. int locktime;
  57. };
  58. struct neigh_statistics {
  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; /* number 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. unsigned long unres_discards; /* number of unresolved drops */
  70. };
  71. #define NEIGH_CACHE_STAT_INC(tbl, field) this_cpu_inc((tbl)->stats->field)
  72. struct neighbour {
  73. struct neighbour __rcu *next;
  74. struct neigh_table *tbl;
  75. struct neigh_parms *parms;
  76. unsigned long confirmed;
  77. unsigned long updated;
  78. rwlock_t lock;
  79. atomic_t refcnt;
  80. struct sk_buff_head arp_queue;
  81. unsigned int arp_queue_len_bytes;
  82. struct timer_list timer;
  83. unsigned long used;
  84. atomic_t probes;
  85. __u8 flags;
  86. __u8 nud_state;
  87. __u8 type;
  88. __u8 dead;
  89. seqlock_t ha_lock;
  90. unsigned char ha[ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))];
  91. struct hh_cache hh;
  92. int (*output)(struct neighbour *, struct sk_buff *);
  93. const struct neigh_ops *ops;
  94. struct rcu_head rcu;
  95. struct net_device *dev;
  96. u8 primary_key[0];
  97. };
  98. struct neigh_ops {
  99. int family;
  100. void (*solicit)(struct neighbour *, struct sk_buff *);
  101. void (*error_report)(struct neighbour *, struct sk_buff *);
  102. int (*output)(struct neighbour *, struct sk_buff *);
  103. int (*connected_output)(struct neighbour *, struct sk_buff *);
  104. };
  105. struct pneigh_entry {
  106. struct pneigh_entry *next;
  107. #ifdef CONFIG_NET_NS
  108. struct net *net;
  109. #endif
  110. struct net_device *dev;
  111. u8 flags;
  112. u8 key[0];
  113. };
  114. /*
  115. * neighbour table manipulation
  116. */
  117. struct neigh_hash_table {
  118. struct neighbour __rcu **hash_buckets;
  119. unsigned int hash_shift;
  120. __u32 hash_rnd;
  121. struct rcu_head rcu;
  122. };
  123. struct neigh_table {
  124. struct neigh_table *next;
  125. int family;
  126. int entry_size;
  127. int key_len;
  128. __u32 (*hash)(const void *pkey,
  129. const struct net_device *dev,
  130. __u32 hash_rnd);
  131. int (*constructor)(struct neighbour *);
  132. int (*pconstructor)(struct pneigh_entry *);
  133. void (*pdestructor)(struct pneigh_entry *);
  134. void (*proxy_redo)(struct sk_buff *skb);
  135. char *id;
  136. struct neigh_parms parms;
  137. /* HACK. gc_* should follow parms without a gap! */
  138. int gc_interval;
  139. int gc_thresh1;
  140. int gc_thresh2;
  141. int gc_thresh3;
  142. unsigned long last_flush;
  143. struct delayed_work gc_work;
  144. struct timer_list proxy_timer;
  145. struct sk_buff_head proxy_queue;
  146. atomic_t entries;
  147. rwlock_t lock;
  148. unsigned long last_rand;
  149. struct neigh_statistics __percpu *stats;
  150. struct neigh_hash_table __rcu *nht;
  151. struct pneigh_entry **phash_buckets;
  152. };
  153. #define NEIGH_PRIV_ALIGN sizeof(long long)
  154. static inline void *neighbour_priv(const struct neighbour *n)
  155. {
  156. return (char *)n + ALIGN(sizeof(*n) + n->tbl->key_len, NEIGH_PRIV_ALIGN);
  157. }
  158. /* flags for neigh_update() */
  159. #define NEIGH_UPDATE_F_OVERRIDE 0x00000001
  160. #define NEIGH_UPDATE_F_WEAK_OVERRIDE 0x00000002
  161. #define NEIGH_UPDATE_F_OVERRIDE_ISROUTER 0x00000004
  162. #define NEIGH_UPDATE_F_ISROUTER 0x40000000
  163. #define NEIGH_UPDATE_F_ADMIN 0x80000000
  164. extern void neigh_table_init(struct neigh_table *tbl);
  165. extern void neigh_table_init_no_netlink(struct neigh_table *tbl);
  166. extern int neigh_table_clear(struct neigh_table *tbl);
  167. extern struct neighbour * neigh_lookup(struct neigh_table *tbl,
  168. const void *pkey,
  169. struct net_device *dev);
  170. extern struct neighbour * neigh_lookup_nodev(struct neigh_table *tbl,
  171. struct net *net,
  172. const void *pkey);
  173. extern struct neighbour * neigh_create(struct neigh_table *tbl,
  174. const void *pkey,
  175. struct net_device *dev);
  176. extern void neigh_destroy(struct neighbour *neigh);
  177. extern int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
  178. extern int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
  179. u32 flags);
  180. extern void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
  181. extern int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
  182. extern int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb);
  183. extern int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb);
  184. extern int neigh_compat_output(struct neighbour *neigh, struct sk_buff *skb);
  185. extern int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb);
  186. extern struct neighbour *neigh_event_ns(struct neigh_table *tbl,
  187. u8 *lladdr, void *saddr,
  188. struct net_device *dev);
  189. extern struct neigh_parms *neigh_parms_alloc(struct net_device *dev, struct neigh_table *tbl);
  190. extern void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
  191. static inline
  192. struct net *neigh_parms_net(const struct neigh_parms *parms)
  193. {
  194. return read_pnet(&parms->net);
  195. }
  196. extern unsigned long neigh_rand_reach_time(unsigned long base);
  197. extern void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
  198. struct sk_buff *skb);
  199. extern struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, struct net *net, const void *key, struct net_device *dev, int creat);
  200. extern struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl,
  201. struct net *net,
  202. const void *key,
  203. struct net_device *dev);
  204. extern int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *key, struct net_device *dev);
  205. static inline
  206. struct net *pneigh_net(const struct pneigh_entry *pneigh)
  207. {
  208. return read_pnet(&pneigh->net);
  209. }
  210. extern void neigh_app_ns(struct neighbour *n);
  211. extern void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie);
  212. extern void __neigh_for_each_release(struct neigh_table *tbl, int (*cb)(struct neighbour *));
  213. extern void pneigh_for_each(struct neigh_table *tbl, void (*cb)(struct pneigh_entry *));
  214. struct neigh_seq_state {
  215. struct seq_net_private p;
  216. struct neigh_table *tbl;
  217. struct neigh_hash_table *nht;
  218. void *(*neigh_sub_iter)(struct neigh_seq_state *state,
  219. struct neighbour *n, loff_t *pos);
  220. unsigned int bucket;
  221. unsigned int flags;
  222. #define NEIGH_SEQ_NEIGH_ONLY 0x00000001
  223. #define NEIGH_SEQ_IS_PNEIGH 0x00000002
  224. #define NEIGH_SEQ_SKIP_NOARP 0x00000004
  225. };
  226. extern void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *, unsigned int);
  227. extern void *neigh_seq_next(struct seq_file *, void *, loff_t *);
  228. extern void neigh_seq_stop(struct seq_file *, void *);
  229. extern int neigh_sysctl_register(struct net_device *dev,
  230. struct neigh_parms *p,
  231. char *p_name,
  232. proc_handler *proc_handler);
  233. extern void neigh_sysctl_unregister(struct neigh_parms *p);
  234. static inline void __neigh_parms_put(struct neigh_parms *parms)
  235. {
  236. atomic_dec(&parms->refcnt);
  237. }
  238. static inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms)
  239. {
  240. atomic_inc(&parms->refcnt);
  241. return parms;
  242. }
  243. /*
  244. * Neighbour references
  245. */
  246. static inline void neigh_release(struct neighbour *neigh)
  247. {
  248. if (atomic_dec_and_test(&neigh->refcnt))
  249. neigh_destroy(neigh);
  250. }
  251. static inline struct neighbour * neigh_clone(struct neighbour *neigh)
  252. {
  253. if (neigh)
  254. atomic_inc(&neigh->refcnt);
  255. return neigh;
  256. }
  257. #define neigh_hold(n) atomic_inc(&(n)->refcnt)
  258. static inline void neigh_confirm(struct neighbour *neigh)
  259. {
  260. if (neigh)
  261. neigh->confirmed = jiffies;
  262. }
  263. static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
  264. {
  265. unsigned long now = jiffies;
  266. if (neigh->used != now)
  267. neigh->used = now;
  268. if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
  269. return __neigh_event_send(neigh, skb);
  270. return 0;
  271. }
  272. #ifdef CONFIG_BRIDGE_NETFILTER
  273. static inline int neigh_hh_bridge(struct hh_cache *hh, struct sk_buff *skb)
  274. {
  275. unsigned seq, hh_alen;
  276. do {
  277. seq = read_seqbegin(&hh->hh_lock);
  278. hh_alen = HH_DATA_ALIGN(ETH_HLEN);
  279. memcpy(skb->data - hh_alen, hh->hh_data, ETH_ALEN + hh_alen - ETH_HLEN);
  280. } while (read_seqretry(&hh->hh_lock, seq));
  281. return 0;
  282. }
  283. #endif
  284. static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
  285. {
  286. unsigned seq;
  287. int hh_len;
  288. do {
  289. int hh_alen;
  290. seq = read_seqbegin(&hh->hh_lock);
  291. hh_len = hh->hh_len;
  292. hh_alen = HH_DATA_ALIGN(hh_len);
  293. memcpy(skb->data - hh_alen, hh->hh_data, hh_alen);
  294. } while (read_seqretry(&hh->hh_lock, seq));
  295. skb_push(skb, hh_len);
  296. return dev_queue_xmit(skb);
  297. }
  298. static inline int neigh_output(struct neighbour *n, struct sk_buff *skb)
  299. {
  300. struct hh_cache *hh = &n->hh;
  301. if ((n->nud_state & NUD_CONNECTED) && hh->hh_len)
  302. return neigh_hh_output(hh, skb);
  303. else
  304. return n->output(n, skb);
  305. }
  306. static inline struct neighbour *
  307. __neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
  308. {
  309. struct neighbour *n = neigh_lookup(tbl, pkey, dev);
  310. if (n || !creat)
  311. return n;
  312. n = neigh_create(tbl, pkey, dev);
  313. return IS_ERR(n) ? NULL : n;
  314. }
  315. static inline struct neighbour *
  316. __neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
  317. struct net_device *dev)
  318. {
  319. struct neighbour *n = neigh_lookup(tbl, pkey, dev);
  320. if (n)
  321. return n;
  322. return neigh_create(tbl, pkey, dev);
  323. }
  324. struct neighbour_cb {
  325. unsigned long sched_next;
  326. unsigned int flags;
  327. };
  328. #define LOCALLY_ENQUEUED 0x1
  329. #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
  330. static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
  331. const struct net_device *dev)
  332. {
  333. unsigned int seq;
  334. do {
  335. seq = read_seqbegin(&n->ha_lock);
  336. memcpy(dst, n->ha, dev->addr_len);
  337. } while (read_seqretry(&n->ha_lock, seq));
  338. }
  339. #endif