0001
0002 #ifndef _NET_NEIGHBOUR_H
0003 #define _NET_NEIGHBOUR_H
0004
0005 #include <linux/neighbour.h>
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <linux/atomic.h>
0021 #include <linux/refcount.h>
0022 #include <linux/netdevice.h>
0023 #include <linux/skbuff.h>
0024 #include <linux/rcupdate.h>
0025 #include <linux/seq_file.h>
0026 #include <linux/bitmap.h>
0027
0028 #include <linux/err.h>
0029 #include <linux/sysctl.h>
0030 #include <linux/workqueue.h>
0031 #include <net/rtnetlink.h>
0032
0033
0034
0035
0036
0037 #define NUD_IN_TIMER (NUD_INCOMPLETE|NUD_REACHABLE|NUD_DELAY|NUD_PROBE)
0038 #define NUD_VALID (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
0039 #define NUD_CONNECTED (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)
0040
0041 struct neighbour;
0042
0043 enum {
0044 NEIGH_VAR_MCAST_PROBES,
0045 NEIGH_VAR_UCAST_PROBES,
0046 NEIGH_VAR_APP_PROBES,
0047 NEIGH_VAR_MCAST_REPROBES,
0048 NEIGH_VAR_RETRANS_TIME,
0049 NEIGH_VAR_BASE_REACHABLE_TIME,
0050 NEIGH_VAR_DELAY_PROBE_TIME,
0051 NEIGH_VAR_INTERVAL_PROBE_TIME_MS,
0052 NEIGH_VAR_GC_STALETIME,
0053 NEIGH_VAR_QUEUE_LEN_BYTES,
0054 NEIGH_VAR_PROXY_QLEN,
0055 NEIGH_VAR_ANYCAST_DELAY,
0056 NEIGH_VAR_PROXY_DELAY,
0057 NEIGH_VAR_LOCKTIME,
0058 #define NEIGH_VAR_DATA_MAX (NEIGH_VAR_LOCKTIME + 1)
0059
0060 NEIGH_VAR_QUEUE_LEN,
0061 NEIGH_VAR_RETRANS_TIME_MS,
0062 NEIGH_VAR_BASE_REACHABLE_TIME_MS,
0063
0064 NEIGH_VAR_GC_INTERVAL,
0065 NEIGH_VAR_GC_THRESH1,
0066 NEIGH_VAR_GC_THRESH2,
0067 NEIGH_VAR_GC_THRESH3,
0068 NEIGH_VAR_MAX
0069 };
0070
0071 struct neigh_parms {
0072 possible_net_t net;
0073 struct net_device *dev;
0074 netdevice_tracker dev_tracker;
0075 struct list_head list;
0076 int (*neigh_setup)(struct neighbour *);
0077 struct neigh_table *tbl;
0078
0079 void *sysctl_table;
0080
0081 int dead;
0082 refcount_t refcnt;
0083 struct rcu_head rcu_head;
0084
0085 int reachable_time;
0086 int qlen;
0087 int data[NEIGH_VAR_DATA_MAX];
0088 DECLARE_BITMAP(data_state, NEIGH_VAR_DATA_MAX);
0089 };
0090
0091 static inline void neigh_var_set(struct neigh_parms *p, int index, int val)
0092 {
0093 set_bit(index, p->data_state);
0094 p->data[index] = val;
0095 }
0096
0097 #define NEIGH_VAR(p, attr) ((p)->data[NEIGH_VAR_ ## attr])
0098
0099
0100
0101
0102 #define NEIGH_VAR_INIT(p, attr, val) (NEIGH_VAR(p, attr) = val)
0103 #define NEIGH_VAR_SET(p, attr, val) neigh_var_set(p, NEIGH_VAR_ ## attr, val)
0104
0105 static inline void neigh_parms_data_state_setall(struct neigh_parms *p)
0106 {
0107 bitmap_fill(p->data_state, NEIGH_VAR_DATA_MAX);
0108 }
0109
0110 static inline void neigh_parms_data_state_cleanall(struct neigh_parms *p)
0111 {
0112 bitmap_zero(p->data_state, NEIGH_VAR_DATA_MAX);
0113 }
0114
0115 struct neigh_statistics {
0116 unsigned long allocs;
0117 unsigned long destroys;
0118 unsigned long hash_grows;
0119
0120 unsigned long res_failed;
0121
0122 unsigned long lookups;
0123 unsigned long hits;
0124
0125 unsigned long rcv_probes_mcast;
0126 unsigned long rcv_probes_ucast;
0127
0128 unsigned long periodic_gc_runs;
0129 unsigned long forced_gc_runs;
0130
0131 unsigned long unres_discards;
0132 unsigned long table_fulls;
0133 };
0134
0135 #define NEIGH_CACHE_STAT_INC(tbl, field) this_cpu_inc((tbl)->stats->field)
0136
0137 struct neighbour {
0138 struct neighbour __rcu *next;
0139 struct neigh_table *tbl;
0140 struct neigh_parms *parms;
0141 unsigned long confirmed;
0142 unsigned long updated;
0143 rwlock_t lock;
0144 refcount_t refcnt;
0145 unsigned int arp_queue_len_bytes;
0146 struct sk_buff_head arp_queue;
0147 struct timer_list timer;
0148 unsigned long used;
0149 atomic_t probes;
0150 u8 nud_state;
0151 u8 type;
0152 u8 dead;
0153 u8 protocol;
0154 u32 flags;
0155 seqlock_t ha_lock;
0156 unsigned char ha[ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))] __aligned(8);
0157 struct hh_cache hh;
0158 int (*output)(struct neighbour *, struct sk_buff *);
0159 const struct neigh_ops *ops;
0160 struct list_head gc_list;
0161 struct list_head managed_list;
0162 struct rcu_head rcu;
0163 struct net_device *dev;
0164 netdevice_tracker dev_tracker;
0165 u8 primary_key[0];
0166 } __randomize_layout;
0167
0168 struct neigh_ops {
0169 int family;
0170 void (*solicit)(struct neighbour *, struct sk_buff *);
0171 void (*error_report)(struct neighbour *, struct sk_buff *);
0172 int (*output)(struct neighbour *, struct sk_buff *);
0173 int (*connected_output)(struct neighbour *, struct sk_buff *);
0174 };
0175
0176 struct pneigh_entry {
0177 struct pneigh_entry *next;
0178 possible_net_t net;
0179 struct net_device *dev;
0180 netdevice_tracker dev_tracker;
0181 u32 flags;
0182 u8 protocol;
0183 u8 key[];
0184 };
0185
0186
0187
0188
0189
0190 #define NEIGH_NUM_HASH_RND 4
0191
0192 struct neigh_hash_table {
0193 struct neighbour __rcu **hash_buckets;
0194 unsigned int hash_shift;
0195 __u32 hash_rnd[NEIGH_NUM_HASH_RND];
0196 struct rcu_head rcu;
0197 };
0198
0199
0200 struct neigh_table {
0201 int family;
0202 unsigned int entry_size;
0203 unsigned int key_len;
0204 __be16 protocol;
0205 __u32 (*hash)(const void *pkey,
0206 const struct net_device *dev,
0207 __u32 *hash_rnd);
0208 bool (*key_eq)(const struct neighbour *, const void *pkey);
0209 int (*constructor)(struct neighbour *);
0210 int (*pconstructor)(struct pneigh_entry *);
0211 void (*pdestructor)(struct pneigh_entry *);
0212 void (*proxy_redo)(struct sk_buff *skb);
0213 int (*is_multicast)(const void *pkey);
0214 bool (*allow_add)(const struct net_device *dev,
0215 struct netlink_ext_ack *extack);
0216 char *id;
0217 struct neigh_parms parms;
0218 struct list_head parms_list;
0219 int gc_interval;
0220 int gc_thresh1;
0221 int gc_thresh2;
0222 int gc_thresh3;
0223 unsigned long last_flush;
0224 struct delayed_work gc_work;
0225 struct delayed_work managed_work;
0226 struct timer_list proxy_timer;
0227 struct sk_buff_head proxy_queue;
0228 atomic_t entries;
0229 atomic_t gc_entries;
0230 struct list_head gc_list;
0231 struct list_head managed_list;
0232 rwlock_t lock;
0233 unsigned long last_rand;
0234 struct neigh_statistics __percpu *stats;
0235 struct neigh_hash_table __rcu *nht;
0236 struct pneigh_entry **phash_buckets;
0237 };
0238
0239 enum {
0240 NEIGH_ARP_TABLE = 0,
0241 NEIGH_ND_TABLE = 1,
0242 NEIGH_DN_TABLE = 2,
0243 NEIGH_NR_TABLES,
0244 NEIGH_LINK_TABLE = NEIGH_NR_TABLES
0245 };
0246
0247 static inline int neigh_parms_family(struct neigh_parms *p)
0248 {
0249 return p->tbl->family;
0250 }
0251
0252 #define NEIGH_PRIV_ALIGN sizeof(long long)
0253 #define NEIGH_ENTRY_SIZE(size) ALIGN((size), NEIGH_PRIV_ALIGN)
0254
0255 static inline void *neighbour_priv(const struct neighbour *n)
0256 {
0257 return (char *)n + n->tbl->entry_size;
0258 }
0259
0260
0261 #define NEIGH_UPDATE_F_OVERRIDE BIT(0)
0262 #define NEIGH_UPDATE_F_WEAK_OVERRIDE BIT(1)
0263 #define NEIGH_UPDATE_F_OVERRIDE_ISROUTER BIT(2)
0264 #define NEIGH_UPDATE_F_USE BIT(3)
0265 #define NEIGH_UPDATE_F_MANAGED BIT(4)
0266 #define NEIGH_UPDATE_F_EXT_LEARNED BIT(5)
0267 #define NEIGH_UPDATE_F_ISROUTER BIT(6)
0268 #define NEIGH_UPDATE_F_ADMIN BIT(7)
0269
0270
0271 #define NTF_OLD_MASK 0xff
0272 #define NTF_EXT_SHIFT 8
0273 #define NTF_EXT_MASK (NTF_EXT_MANAGED)
0274
0275 #define NTF_MANAGED (NTF_EXT_MANAGED << NTF_EXT_SHIFT)
0276
0277 extern const struct nla_policy nda_policy[];
0278
0279 static inline bool neigh_key_eq16(const struct neighbour *n, const void *pkey)
0280 {
0281 return *(const u16 *)n->primary_key == *(const u16 *)pkey;
0282 }
0283
0284 static inline bool neigh_key_eq32(const struct neighbour *n, const void *pkey)
0285 {
0286 return *(const u32 *)n->primary_key == *(const u32 *)pkey;
0287 }
0288
0289 static inline bool neigh_key_eq128(const struct neighbour *n, const void *pkey)
0290 {
0291 const u32 *n32 = (const u32 *)n->primary_key;
0292 const u32 *p32 = pkey;
0293
0294 return ((n32[0] ^ p32[0]) | (n32[1] ^ p32[1]) |
0295 (n32[2] ^ p32[2]) | (n32[3] ^ p32[3])) == 0;
0296 }
0297
0298 static inline struct neighbour *___neigh_lookup_noref(
0299 struct neigh_table *tbl,
0300 bool (*key_eq)(const struct neighbour *n, const void *pkey),
0301 __u32 (*hash)(const void *pkey,
0302 const struct net_device *dev,
0303 __u32 *hash_rnd),
0304 const void *pkey,
0305 struct net_device *dev)
0306 {
0307 struct neigh_hash_table *nht = rcu_dereference_bh(tbl->nht);
0308 struct neighbour *n;
0309 u32 hash_val;
0310
0311 hash_val = hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
0312 for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
0313 n != NULL;
0314 n = rcu_dereference_bh(n->next)) {
0315 if (n->dev == dev && key_eq(n, pkey))
0316 return n;
0317 }
0318
0319 return NULL;
0320 }
0321
0322 static inline struct neighbour *__neigh_lookup_noref(struct neigh_table *tbl,
0323 const void *pkey,
0324 struct net_device *dev)
0325 {
0326 return ___neigh_lookup_noref(tbl, tbl->key_eq, tbl->hash, pkey, dev);
0327 }
0328
0329 static inline void neigh_confirm(struct neighbour *n)
0330 {
0331 if (n) {
0332 unsigned long now = jiffies;
0333
0334
0335 if (READ_ONCE(n->confirmed) != now)
0336 WRITE_ONCE(n->confirmed, now);
0337 }
0338 }
0339
0340 void neigh_table_init(int index, struct neigh_table *tbl);
0341 int neigh_table_clear(int index, struct neigh_table *tbl);
0342 struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
0343 struct net_device *dev);
0344 struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
0345 const void *pkey);
0346 struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
0347 struct net_device *dev, bool want_ref);
0348 static inline struct neighbour *neigh_create(struct neigh_table *tbl,
0349 const void *pkey,
0350 struct net_device *dev)
0351 {
0352 return __neigh_create(tbl, pkey, dev, true);
0353 }
0354 void neigh_destroy(struct neighbour *neigh);
0355 int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb,
0356 const bool immediate_ok);
0357 int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, u32 flags,
0358 u32 nlmsg_pid);
0359 void __neigh_set_probe_once(struct neighbour *neigh);
0360 bool neigh_remove_one(struct neighbour *ndel, struct neigh_table *tbl);
0361 void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
0362 int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
0363 int neigh_carrier_down(struct neigh_table *tbl, struct net_device *dev);
0364 int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb);
0365 int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb);
0366 int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb);
0367 struct neighbour *neigh_event_ns(struct neigh_table *tbl,
0368 u8 *lladdr, void *saddr,
0369 struct net_device *dev);
0370
0371 struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
0372 struct neigh_table *tbl);
0373 void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
0374
0375 static inline
0376 struct net *neigh_parms_net(const struct neigh_parms *parms)
0377 {
0378 return read_pnet(&parms->net);
0379 }
0380
0381 unsigned long neigh_rand_reach_time(unsigned long base);
0382
0383 void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
0384 struct sk_buff *skb);
0385 struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, struct net *net,
0386 const void *key, struct net_device *dev,
0387 int creat);
0388 struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl, struct net *net,
0389 const void *key, struct net_device *dev);
0390 int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *key,
0391 struct net_device *dev);
0392
0393 static inline struct net *pneigh_net(const struct pneigh_entry *pneigh)
0394 {
0395 return read_pnet(&pneigh->net);
0396 }
0397
0398 void neigh_app_ns(struct neighbour *n);
0399 void neigh_for_each(struct neigh_table *tbl,
0400 void (*cb)(struct neighbour *, void *), void *cookie);
0401 void __neigh_for_each_release(struct neigh_table *tbl,
0402 int (*cb)(struct neighbour *));
0403 int neigh_xmit(int fam, struct net_device *, const void *, struct sk_buff *);
0404 void pneigh_for_each(struct neigh_table *tbl,
0405 void (*cb)(struct pneigh_entry *));
0406
0407 struct neigh_seq_state {
0408 struct seq_net_private p;
0409 struct neigh_table *tbl;
0410 struct neigh_hash_table *nht;
0411 void *(*neigh_sub_iter)(struct neigh_seq_state *state,
0412 struct neighbour *n, loff_t *pos);
0413 unsigned int bucket;
0414 unsigned int flags;
0415 #define NEIGH_SEQ_NEIGH_ONLY 0x00000001
0416 #define NEIGH_SEQ_IS_PNEIGH 0x00000002
0417 #define NEIGH_SEQ_SKIP_NOARP 0x00000004
0418 };
0419 void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *,
0420 unsigned int);
0421 void *neigh_seq_next(struct seq_file *, void *, loff_t *);
0422 void neigh_seq_stop(struct seq_file *, void *);
0423
0424 int neigh_proc_dointvec(struct ctl_table *ctl, int write,
0425 void *buffer, size_t *lenp, loff_t *ppos);
0426 int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
0427 void *buffer,
0428 size_t *lenp, loff_t *ppos);
0429 int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
0430 void *buffer, size_t *lenp, loff_t *ppos);
0431
0432 int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
0433 proc_handler *proc_handler);
0434 void neigh_sysctl_unregister(struct neigh_parms *p);
0435
0436 static inline void __neigh_parms_put(struct neigh_parms *parms)
0437 {
0438 refcount_dec(&parms->refcnt);
0439 }
0440
0441 static inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms)
0442 {
0443 refcount_inc(&parms->refcnt);
0444 return parms;
0445 }
0446
0447
0448
0449
0450
0451 static inline void neigh_release(struct neighbour *neigh)
0452 {
0453 if (refcount_dec_and_test(&neigh->refcnt))
0454 neigh_destroy(neigh);
0455 }
0456
0457 static inline struct neighbour * neigh_clone(struct neighbour *neigh)
0458 {
0459 if (neigh)
0460 refcount_inc(&neigh->refcnt);
0461 return neigh;
0462 }
0463
0464 #define neigh_hold(n) refcount_inc(&(n)->refcnt)
0465
0466 static __always_inline int neigh_event_send_probe(struct neighbour *neigh,
0467 struct sk_buff *skb,
0468 const bool immediate_ok)
0469 {
0470 unsigned long now = jiffies;
0471
0472 if (READ_ONCE(neigh->used) != now)
0473 WRITE_ONCE(neigh->used, now);
0474 if (!(neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE)))
0475 return __neigh_event_send(neigh, skb, immediate_ok);
0476 return 0;
0477 }
0478
0479 static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
0480 {
0481 return neigh_event_send_probe(neigh, skb, true);
0482 }
0483
0484 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
0485 static inline int neigh_hh_bridge(struct hh_cache *hh, struct sk_buff *skb)
0486 {
0487 unsigned int seq, hh_alen;
0488
0489 do {
0490 seq = read_seqbegin(&hh->hh_lock);
0491 hh_alen = HH_DATA_ALIGN(ETH_HLEN);
0492 memcpy(skb->data - hh_alen, hh->hh_data, ETH_ALEN + hh_alen - ETH_HLEN);
0493 } while (read_seqretry(&hh->hh_lock, seq));
0494 return 0;
0495 }
0496 #endif
0497
0498 static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
0499 {
0500 unsigned int hh_alen = 0;
0501 unsigned int seq;
0502 unsigned int hh_len;
0503
0504 do {
0505 seq = read_seqbegin(&hh->hh_lock);
0506 hh_len = READ_ONCE(hh->hh_len);
0507 if (likely(hh_len <= HH_DATA_MOD)) {
0508 hh_alen = HH_DATA_MOD;
0509
0510
0511
0512
0513
0514 if (likely(skb_headroom(skb) >= HH_DATA_MOD)) {
0515
0516 memcpy(skb->data - HH_DATA_MOD, hh->hh_data,
0517 HH_DATA_MOD);
0518 }
0519 } else {
0520 hh_alen = HH_DATA_ALIGN(hh_len);
0521
0522 if (likely(skb_headroom(skb) >= hh_alen)) {
0523 memcpy(skb->data - hh_alen, hh->hh_data,
0524 hh_alen);
0525 }
0526 }
0527 } while (read_seqretry(&hh->hh_lock, seq));
0528
0529 if (WARN_ON_ONCE(skb_headroom(skb) < hh_alen)) {
0530 kfree_skb(skb);
0531 return NET_XMIT_DROP;
0532 }
0533
0534 __skb_push(skb, hh_len);
0535 return dev_queue_xmit(skb);
0536 }
0537
0538 static inline int neigh_output(struct neighbour *n, struct sk_buff *skb,
0539 bool skip_cache)
0540 {
0541 const struct hh_cache *hh = &n->hh;
0542
0543
0544
0545
0546 if (!skip_cache &&
0547 (READ_ONCE(n->nud_state) & NUD_CONNECTED) &&
0548 READ_ONCE(hh->hh_len))
0549 return neigh_hh_output(hh, skb);
0550
0551 return n->output(n, skb);
0552 }
0553
0554 static inline struct neighbour *
0555 __neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
0556 {
0557 struct neighbour *n = neigh_lookup(tbl, pkey, dev);
0558
0559 if (n || !creat)
0560 return n;
0561
0562 n = neigh_create(tbl, pkey, dev);
0563 return IS_ERR(n) ? NULL : n;
0564 }
0565
0566 static inline struct neighbour *
0567 __neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
0568 struct net_device *dev)
0569 {
0570 struct neighbour *n = neigh_lookup(tbl, pkey, dev);
0571
0572 if (n)
0573 return n;
0574
0575 return neigh_create(tbl, pkey, dev);
0576 }
0577
0578 struct neighbour_cb {
0579 unsigned long sched_next;
0580 unsigned int flags;
0581 };
0582
0583 #define LOCALLY_ENQUEUED 0x1
0584
0585 #define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
0586
0587 static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
0588 const struct net_device *dev)
0589 {
0590 unsigned int seq;
0591
0592 do {
0593 seq = read_seqbegin(&n->ha_lock);
0594 memcpy(dst, n->ha, dev->addr_len);
0595 } while (read_seqretry(&n->ha_lock, seq));
0596 }
0597
0598 static inline void neigh_update_is_router(struct neighbour *neigh, u32 flags,
0599 int *notify)
0600 {
0601 u8 ndm_flags = 0;
0602
0603 ndm_flags |= (flags & NEIGH_UPDATE_F_ISROUTER) ? NTF_ROUTER : 0;
0604 if ((neigh->flags ^ ndm_flags) & NTF_ROUTER) {
0605 if (ndm_flags & NTF_ROUTER)
0606 neigh->flags |= NTF_ROUTER;
0607 else
0608 neigh->flags &= ~NTF_ROUTER;
0609 *notify = 1;
0610 }
0611 }
0612 #endif