neighbour.h 11 KB

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