Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _NET_NEIGHBOUR_H |
| 2 | #define _NET_NEIGHBOUR_H |
| 3 | |
Thomas Graf | 9067c72 | 2006-08-07 17:57:44 -0700 | [diff] [blame] | 4 | #include <linux/neighbour.h> |
| 5 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | /* |
| 7 | * Generic neighbour manipulation |
| 8 | * |
| 9 | * Authors: |
| 10 | * Pedro Roque <roque@di.fc.ul.pt> |
| 11 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
| 12 | * |
| 13 | * Changes: |
| 14 | * |
| 15 | * Harald Welte: <laforge@gnumonks.org> |
| 16 | * - Add neighbour cache statistics like rtstat |
| 17 | */ |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/netdevice.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 21 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/rcupdate.h> |
| 23 | #include <linux/seq_file.h> |
| 24 | |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/sysctl.h> |
Thomas Graf | c8822a4 | 2007-03-22 11:50:06 -0700 | [diff] [blame] | 27 | #include <net/rtnetlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #define NUD_IN_TIMER (NUD_INCOMPLETE|NUD_REACHABLE|NUD_DELAY|NUD_PROBE) |
| 30 | #define NUD_VALID (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY) |
| 31 | #define NUD_CONNECTED (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE) |
| 32 | |
| 33 | struct neighbour; |
| 34 | |
| 35 | struct neigh_parms |
| 36 | { |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 37 | struct net *net; |
Thomas Graf | c7fb64d | 2005-06-18 22:50:55 -0700 | [diff] [blame] | 38 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | struct neigh_parms *next; |
| 40 | int (*neigh_setup)(struct neighbour *); |
Alexey Kuznetsov | ecbb416 | 2007-03-24 12:52:16 -0700 | [diff] [blame] | 41 | void (*neigh_cleanup)(struct neighbour *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | struct neigh_table *tbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | void *sysctl_table; |
| 45 | |
| 46 | int dead; |
| 47 | atomic_t refcnt; |
| 48 | struct rcu_head rcu_head; |
| 49 | |
| 50 | int base_reachable_time; |
| 51 | int retrans_time; |
| 52 | int gc_staletime; |
| 53 | int reachable_time; |
| 54 | int delay_probe_time; |
| 55 | |
| 56 | int queue_len; |
| 57 | int ucast_probes; |
| 58 | int app_probes; |
| 59 | int mcast_probes; |
| 60 | int anycast_delay; |
| 61 | int proxy_delay; |
| 62 | int proxy_qlen; |
| 63 | int locktime; |
| 64 | }; |
| 65 | |
| 66 | struct neigh_statistics |
| 67 | { |
| 68 | unsigned long allocs; /* number of allocated neighs */ |
| 69 | unsigned long destroys; /* number of destroyed neighs */ |
| 70 | unsigned long hash_grows; /* number of hash resizes */ |
| 71 | |
| 72 | unsigned long res_failed; /* nomber of failed resolutions */ |
| 73 | |
| 74 | unsigned long lookups; /* number of lookups */ |
| 75 | unsigned long hits; /* number of hits (among lookups) */ |
| 76 | |
| 77 | unsigned long rcv_probes_mcast; /* number of received mcast ipv6 */ |
| 78 | unsigned long rcv_probes_ucast; /* number of received ucast ipv6 */ |
| 79 | |
| 80 | unsigned long periodic_gc_runs; /* number of periodic GC runs */ |
| 81 | unsigned long forced_gc_runs; /* number of forced GC runs */ |
| 82 | }; |
| 83 | |
| 84 | #define NEIGH_CACHE_STAT_INC(tbl, field) \ |
| 85 | do { \ |
| 86 | preempt_disable(); \ |
| 87 | (per_cpu_ptr((tbl)->stats, smp_processor_id())->field)++; \ |
| 88 | preempt_enable(); \ |
| 89 | } while (0) |
| 90 | |
| 91 | struct neighbour |
| 92 | { |
| 93 | struct neighbour *next; |
| 94 | struct neigh_table *tbl; |
| 95 | struct neigh_parms *parms; |
| 96 | struct net_device *dev; |
| 97 | unsigned long used; |
| 98 | unsigned long confirmed; |
| 99 | unsigned long updated; |
| 100 | __u8 flags; |
| 101 | __u8 nud_state; |
| 102 | __u8 type; |
| 103 | __u8 dead; |
| 104 | atomic_t probes; |
| 105 | rwlock_t lock; |
Stephen Hemminger | d924424 | 2006-08-10 23:03:23 -0700 | [diff] [blame] | 106 | unsigned char ha[ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | struct hh_cache *hh; |
| 108 | atomic_t refcnt; |
| 109 | int (*output)(struct sk_buff *skb); |
| 110 | struct sk_buff_head arp_queue; |
| 111 | struct timer_list timer; |
| 112 | struct neigh_ops *ops; |
| 113 | u8 primary_key[0]; |
| 114 | }; |
| 115 | |
| 116 | struct neigh_ops |
| 117 | { |
| 118 | int family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | void (*solicit)(struct neighbour *, struct sk_buff*); |
| 120 | void (*error_report)(struct neighbour *, struct sk_buff*); |
| 121 | int (*output)(struct sk_buff*); |
| 122 | int (*connected_output)(struct sk_buff*); |
| 123 | int (*hh_output)(struct sk_buff*); |
| 124 | int (*queue_xmit)(struct sk_buff*); |
| 125 | }; |
| 126 | |
| 127 | struct pneigh_entry |
| 128 | { |
| 129 | struct pneigh_entry *next; |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 130 | struct net *net; |
| 131 | struct net_device *dev; |
Ville Nuorvala | 62dd931 | 2006-09-22 14:43:19 -0700 | [diff] [blame] | 132 | u8 flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | u8 key[0]; |
| 134 | }; |
| 135 | |
| 136 | /* |
| 137 | * neighbour table manipulation |
| 138 | */ |
| 139 | |
| 140 | |
| 141 | struct neigh_table |
| 142 | { |
| 143 | struct neigh_table *next; |
| 144 | int family; |
| 145 | int entry_size; |
| 146 | int key_len; |
| 147 | __u32 (*hash)(const void *pkey, const struct net_device *); |
| 148 | int (*constructor)(struct neighbour *); |
| 149 | int (*pconstructor)(struct pneigh_entry *); |
| 150 | void (*pdestructor)(struct pneigh_entry *); |
| 151 | void (*proxy_redo)(struct sk_buff *skb); |
| 152 | char *id; |
| 153 | struct neigh_parms parms; |
| 154 | /* HACK. gc_* shoul follow parms without a gap! */ |
| 155 | int gc_interval; |
| 156 | int gc_thresh1; |
| 157 | int gc_thresh2; |
| 158 | int gc_thresh3; |
| 159 | unsigned long last_flush; |
| 160 | struct timer_list gc_timer; |
| 161 | struct timer_list proxy_timer; |
| 162 | struct sk_buff_head proxy_queue; |
| 163 | atomic_t entries; |
| 164 | rwlock_t lock; |
| 165 | unsigned long last_rand; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 166 | struct kmem_cache *kmem_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | struct neigh_statistics *stats; |
| 168 | struct neighbour **hash_buckets; |
| 169 | unsigned int hash_mask; |
| 170 | __u32 hash_rnd; |
| 171 | unsigned int hash_chain_gc; |
| 172 | struct pneigh_entry **phash_buckets; |
| 173 | #ifdef CONFIG_PROC_FS |
| 174 | struct proc_dir_entry *pde; |
| 175 | #endif |
| 176 | }; |
| 177 | |
| 178 | /* flags for neigh_update() */ |
| 179 | #define NEIGH_UPDATE_F_OVERRIDE 0x00000001 |
| 180 | #define NEIGH_UPDATE_F_WEAK_OVERRIDE 0x00000002 |
| 181 | #define NEIGH_UPDATE_F_OVERRIDE_ISROUTER 0x00000004 |
| 182 | #define NEIGH_UPDATE_F_ISROUTER 0x40000000 |
| 183 | #define NEIGH_UPDATE_F_ADMIN 0x80000000 |
| 184 | |
| 185 | extern void neigh_table_init(struct neigh_table *tbl); |
Simon Kelley | bd89efc | 2006-05-12 14:56:08 -0700 | [diff] [blame] | 186 | extern void neigh_table_init_no_netlink(struct neigh_table *tbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | extern int neigh_table_clear(struct neigh_table *tbl); |
| 188 | extern struct neighbour * neigh_lookup(struct neigh_table *tbl, |
| 189 | const void *pkey, |
| 190 | struct net_device *dev); |
| 191 | extern struct neighbour * neigh_lookup_nodev(struct neigh_table *tbl, |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 192 | struct net *net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | const void *pkey); |
| 194 | extern struct neighbour * neigh_create(struct neigh_table *tbl, |
| 195 | const void *pkey, |
| 196 | struct net_device *dev); |
| 197 | extern void neigh_destroy(struct neighbour *neigh); |
| 198 | extern int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb); |
| 199 | extern int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, |
| 200 | u32 flags); |
| 201 | extern void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev); |
| 202 | extern int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev); |
| 203 | extern int neigh_resolve_output(struct sk_buff *skb); |
| 204 | extern int neigh_connected_output(struct sk_buff *skb); |
| 205 | extern int neigh_compat_output(struct sk_buff *skb); |
| 206 | extern struct neighbour *neigh_event_ns(struct neigh_table *tbl, |
| 207 | u8 *lladdr, void *saddr, |
| 208 | struct net_device *dev); |
| 209 | |
| 210 | extern struct neigh_parms *neigh_parms_alloc(struct net_device *dev, struct neigh_table *tbl); |
| 211 | extern void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms); |
| 212 | extern void neigh_parms_destroy(struct neigh_parms *parms); |
| 213 | extern unsigned long neigh_rand_reach_time(unsigned long base); |
| 214 | |
| 215 | extern void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p, |
| 216 | struct sk_buff *skb); |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 217 | extern struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, struct net *net, const void *key, struct net_device *dev, int creat); |
| 218 | extern int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *key, struct net_device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | extern void neigh_app_ns(struct neighbour *n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | extern void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie); |
| 222 | extern void __neigh_for_each_release(struct neigh_table *tbl, int (*cb)(struct neighbour *)); |
| 223 | extern void pneigh_for_each(struct neigh_table *tbl, void (*cb)(struct pneigh_entry *)); |
| 224 | |
| 225 | struct neigh_seq_state { |
Eric W. Biederman | 426b530 | 2008-01-24 00:13:18 -0800 | [diff] [blame] | 226 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | struct neigh_table *tbl; |
| 228 | void *(*neigh_sub_iter)(struct neigh_seq_state *state, |
| 229 | struct neighbour *n, loff_t *pos); |
| 230 | unsigned int bucket; |
| 231 | unsigned int flags; |
| 232 | #define NEIGH_SEQ_NEIGH_ONLY 0x00000001 |
| 233 | #define NEIGH_SEQ_IS_PNEIGH 0x00000002 |
| 234 | #define NEIGH_SEQ_SKIP_NOARP 0x00000004 |
| 235 | }; |
| 236 | extern void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *, unsigned int); |
| 237 | extern void *neigh_seq_next(struct seq_file *, void *, loff_t *); |
| 238 | extern void neigh_seq_stop(struct seq_file *, void *); |
| 239 | |
| 240 | extern int neigh_sysctl_register(struct net_device *dev, |
| 241 | struct neigh_parms *p, |
| 242 | int p_id, int pdev_id, |
| 243 | char *p_name, |
| 244 | proc_handler *proc_handler, |
| 245 | ctl_handler *strategy); |
| 246 | extern void neigh_sysctl_unregister(struct neigh_parms *p); |
| 247 | |
| 248 | static inline void __neigh_parms_put(struct neigh_parms *parms) |
| 249 | { |
| 250 | atomic_dec(&parms->refcnt); |
| 251 | } |
| 252 | |
| 253 | static inline void neigh_parms_put(struct neigh_parms *parms) |
| 254 | { |
| 255 | if (atomic_dec_and_test(&parms->refcnt)) |
| 256 | neigh_parms_destroy(parms); |
| 257 | } |
| 258 | |
| 259 | static inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms) |
| 260 | { |
| 261 | atomic_inc(&parms->refcnt); |
| 262 | return parms; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Neighbour references |
| 267 | */ |
| 268 | |
| 269 | static inline void neigh_release(struct neighbour *neigh) |
| 270 | { |
| 271 | if (atomic_dec_and_test(&neigh->refcnt)) |
| 272 | neigh_destroy(neigh); |
| 273 | } |
| 274 | |
| 275 | static inline struct neighbour * neigh_clone(struct neighbour *neigh) |
| 276 | { |
| 277 | if (neigh) |
| 278 | atomic_inc(&neigh->refcnt); |
| 279 | return neigh; |
| 280 | } |
| 281 | |
| 282 | #define neigh_hold(n) atomic_inc(&(n)->refcnt) |
| 283 | |
| 284 | static inline void neigh_confirm(struct neighbour *neigh) |
| 285 | { |
| 286 | if (neigh) |
| 287 | neigh->confirmed = jiffies; |
| 288 | } |
| 289 | |
| 290 | static inline int neigh_is_connected(struct neighbour *neigh) |
| 291 | { |
| 292 | return neigh->nud_state&NUD_CONNECTED; |
| 293 | } |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
| 296 | static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb) |
| 297 | { |
| 298 | neigh->used = jiffies; |
| 299 | if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE))) |
| 300 | return __neigh_event_send(neigh, skb); |
| 301 | return 0; |
| 302 | } |
| 303 | |
Stephen Hemminger | 3644f0c | 2006-12-07 15:08:17 -0800 | [diff] [blame] | 304 | static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb) |
| 305 | { |
| 306 | unsigned seq; |
| 307 | int hh_len; |
| 308 | |
| 309 | do { |
| 310 | int hh_alen; |
| 311 | |
| 312 | seq = read_seqbegin(&hh->hh_lock); |
| 313 | hh_len = hh->hh_len; |
| 314 | hh_alen = HH_DATA_ALIGN(hh_len); |
| 315 | memcpy(skb->data - hh_alen, hh->hh_data, hh_alen); |
| 316 | } while (read_seqretry(&hh->hh_lock, seq)); |
| 317 | |
| 318 | skb_push(skb, hh_len); |
| 319 | return hh->hh_output(skb); |
| 320 | } |
| 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | static inline struct neighbour * |
| 323 | __neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat) |
| 324 | { |
| 325 | struct neighbour *n = neigh_lookup(tbl, pkey, dev); |
| 326 | |
| 327 | if (n || !creat) |
| 328 | return n; |
| 329 | |
| 330 | n = neigh_create(tbl, pkey, dev); |
| 331 | return IS_ERR(n) ? NULL : n; |
| 332 | } |
| 333 | |
| 334 | static inline struct neighbour * |
| 335 | __neigh_lookup_errno(struct neigh_table *tbl, const void *pkey, |
| 336 | struct net_device *dev) |
| 337 | { |
| 338 | struct neighbour *n = neigh_lookup(tbl, pkey, dev); |
| 339 | |
| 340 | if (n) |
| 341 | return n; |
| 342 | |
| 343 | return neigh_create(tbl, pkey, dev); |
| 344 | } |
| 345 | |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 346 | struct neighbour_cb { |
| 347 | unsigned long sched_next; |
| 348 | unsigned int flags; |
| 349 | }; |
| 350 | |
| 351 | #define LOCALLY_ENQUEUED 0x1 |
| 352 | |
| 353 | #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | #endif |