18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _NET_NEIGHBOUR_H
38c2ecf20Sopenharmony_ci#define _NET_NEIGHBOUR_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <linux/neighbour.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci *	Generic neighbour manipulation
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *	Authors:
118c2ecf20Sopenharmony_ci *	Pedro Roque		<roque@di.fc.ul.pt>
128c2ecf20Sopenharmony_ci *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * 	Changes:
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *	Harald Welte:		<laforge@gnumonks.org>
178c2ecf20Sopenharmony_ci *		- Add neighbour cache statistics like rtstat
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/atomic.h>
218c2ecf20Sopenharmony_ci#include <linux/refcount.h>
228c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
238c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
248c2ecf20Sopenharmony_ci#include <linux/rcupdate.h>
258c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
268c2ecf20Sopenharmony_ci#include <linux/bitmap.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <linux/err.h>
298c2ecf20Sopenharmony_ci#include <linux/sysctl.h>
308c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
318c2ecf20Sopenharmony_ci#include <net/rtnetlink.h>
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/*
348c2ecf20Sopenharmony_ci * NUD stands for "neighbor unreachability detection"
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define NUD_IN_TIMER	(NUD_INCOMPLETE|NUD_REACHABLE|NUD_DELAY|NUD_PROBE)
388c2ecf20Sopenharmony_ci#define NUD_VALID	(NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
398c2ecf20Sopenharmony_ci#define NUD_CONNECTED	(NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistruct neighbour;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cienum {
448c2ecf20Sopenharmony_ci	NEIGH_VAR_MCAST_PROBES,
458c2ecf20Sopenharmony_ci	NEIGH_VAR_UCAST_PROBES,
468c2ecf20Sopenharmony_ci	NEIGH_VAR_APP_PROBES,
478c2ecf20Sopenharmony_ci	NEIGH_VAR_MCAST_REPROBES,
488c2ecf20Sopenharmony_ci	NEIGH_VAR_RETRANS_TIME,
498c2ecf20Sopenharmony_ci	NEIGH_VAR_BASE_REACHABLE_TIME,
508c2ecf20Sopenharmony_ci	NEIGH_VAR_DELAY_PROBE_TIME,
518c2ecf20Sopenharmony_ci	NEIGH_VAR_GC_STALETIME,
528c2ecf20Sopenharmony_ci	NEIGH_VAR_QUEUE_LEN_BYTES,
538c2ecf20Sopenharmony_ci	NEIGH_VAR_PROXY_QLEN,
548c2ecf20Sopenharmony_ci	NEIGH_VAR_ANYCAST_DELAY,
558c2ecf20Sopenharmony_ci	NEIGH_VAR_PROXY_DELAY,
568c2ecf20Sopenharmony_ci	NEIGH_VAR_LOCKTIME,
578c2ecf20Sopenharmony_ci#define NEIGH_VAR_DATA_MAX (NEIGH_VAR_LOCKTIME + 1)
588c2ecf20Sopenharmony_ci	/* Following are used as a second way to access one of the above */
598c2ecf20Sopenharmony_ci	NEIGH_VAR_QUEUE_LEN, /* same data as NEIGH_VAR_QUEUE_LEN_BYTES */
608c2ecf20Sopenharmony_ci	NEIGH_VAR_RETRANS_TIME_MS, /* same data as NEIGH_VAR_RETRANS_TIME */
618c2ecf20Sopenharmony_ci	NEIGH_VAR_BASE_REACHABLE_TIME_MS, /* same data as NEIGH_VAR_BASE_REACHABLE_TIME */
628c2ecf20Sopenharmony_ci	/* Following are used by "default" only */
638c2ecf20Sopenharmony_ci	NEIGH_VAR_GC_INTERVAL,
648c2ecf20Sopenharmony_ci	NEIGH_VAR_GC_THRESH1,
658c2ecf20Sopenharmony_ci	NEIGH_VAR_GC_THRESH2,
668c2ecf20Sopenharmony_ci	NEIGH_VAR_GC_THRESH3,
678c2ecf20Sopenharmony_ci	NEIGH_VAR_MAX
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistruct neigh_parms {
718c2ecf20Sopenharmony_ci	possible_net_t net;
728c2ecf20Sopenharmony_ci	struct net_device *dev;
738c2ecf20Sopenharmony_ci	struct list_head list;
748c2ecf20Sopenharmony_ci	int	(*neigh_setup)(struct neighbour *);
758c2ecf20Sopenharmony_ci	struct neigh_table *tbl;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	void	*sysctl_table;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	int dead;
808c2ecf20Sopenharmony_ci	refcount_t refcnt;
818c2ecf20Sopenharmony_ci	struct rcu_head rcu_head;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	int	reachable_time;
848c2ecf20Sopenharmony_ci	int	data[NEIGH_VAR_DATA_MAX];
858c2ecf20Sopenharmony_ci	DECLARE_BITMAP(data_state, NEIGH_VAR_DATA_MAX);
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic inline void neigh_var_set(struct neigh_parms *p, int index, int val)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	set_bit(index, p->data_state);
918c2ecf20Sopenharmony_ci	p->data[index] = val;
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define NEIGH_VAR(p, attr) ((p)->data[NEIGH_VAR_ ## attr])
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/* In ndo_neigh_setup, NEIGH_VAR_INIT should be used.
978c2ecf20Sopenharmony_ci * In other cases, NEIGH_VAR_SET should be used.
988c2ecf20Sopenharmony_ci */
998c2ecf20Sopenharmony_ci#define NEIGH_VAR_INIT(p, attr, val) (NEIGH_VAR(p, attr) = val)
1008c2ecf20Sopenharmony_ci#define NEIGH_VAR_SET(p, attr, val) neigh_var_set(p, NEIGH_VAR_ ## attr, val)
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic inline void neigh_parms_data_state_setall(struct neigh_parms *p)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	bitmap_fill(p->data_state, NEIGH_VAR_DATA_MAX);
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic inline void neigh_parms_data_state_cleanall(struct neigh_parms *p)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	bitmap_zero(p->data_state, NEIGH_VAR_DATA_MAX);
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistruct neigh_statistics {
1138c2ecf20Sopenharmony_ci	unsigned long allocs;		/* number of allocated neighs */
1148c2ecf20Sopenharmony_ci	unsigned long destroys;		/* number of destroyed neighs */
1158c2ecf20Sopenharmony_ci	unsigned long hash_grows;	/* number of hash resizes */
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	unsigned long res_failed;	/* number of failed resolutions */
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	unsigned long lookups;		/* number of lookups */
1208c2ecf20Sopenharmony_ci	unsigned long hits;		/* number of hits (among lookups) */
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	unsigned long rcv_probes_mcast;	/* number of received mcast ipv6 */
1238c2ecf20Sopenharmony_ci	unsigned long rcv_probes_ucast; /* number of received ucast ipv6 */
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	unsigned long periodic_gc_runs;	/* number of periodic GC runs */
1268c2ecf20Sopenharmony_ci	unsigned long forced_gc_runs;	/* number of forced GC runs */
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	unsigned long unres_discards;	/* number of unresolved drops */
1298c2ecf20Sopenharmony_ci	unsigned long table_fulls;      /* times even gc couldn't help */
1308c2ecf20Sopenharmony_ci};
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#define NEIGH_CACHE_STAT_INC(tbl, field) this_cpu_inc((tbl)->stats->field)
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistruct neighbour {
1358c2ecf20Sopenharmony_ci	struct neighbour __rcu	*next;
1368c2ecf20Sopenharmony_ci	struct neigh_table	*tbl;
1378c2ecf20Sopenharmony_ci	struct neigh_parms	*parms;
1388c2ecf20Sopenharmony_ci	unsigned long		confirmed;
1398c2ecf20Sopenharmony_ci	unsigned long		updated;
1408c2ecf20Sopenharmony_ci	rwlock_t		lock;
1418c2ecf20Sopenharmony_ci	refcount_t		refcnt;
1428c2ecf20Sopenharmony_ci	unsigned int		arp_queue_len_bytes;
1438c2ecf20Sopenharmony_ci	struct sk_buff_head	arp_queue;
1448c2ecf20Sopenharmony_ci	struct timer_list	timer;
1458c2ecf20Sopenharmony_ci	unsigned long		used;
1468c2ecf20Sopenharmony_ci	atomic_t		probes;
1478c2ecf20Sopenharmony_ci	__u8			flags;
1488c2ecf20Sopenharmony_ci	__u8			nud_state;
1498c2ecf20Sopenharmony_ci	__u8			type;
1508c2ecf20Sopenharmony_ci	__u8			dead;
1518c2ecf20Sopenharmony_ci	u8			protocol;
1528c2ecf20Sopenharmony_ci	seqlock_t		ha_lock;
1538c2ecf20Sopenharmony_ci	unsigned char		ha[ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))] __aligned(8);
1548c2ecf20Sopenharmony_ci	struct hh_cache		hh;
1558c2ecf20Sopenharmony_ci	int			(*output)(struct neighbour *, struct sk_buff *);
1568c2ecf20Sopenharmony_ci	const struct neigh_ops	*ops;
1578c2ecf20Sopenharmony_ci	struct list_head	gc_list;
1588c2ecf20Sopenharmony_ci	struct rcu_head		rcu;
1598c2ecf20Sopenharmony_ci	struct net_device	*dev;
1608c2ecf20Sopenharmony_ci	u8			primary_key[0];
1618c2ecf20Sopenharmony_ci} __randomize_layout;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistruct neigh_ops {
1648c2ecf20Sopenharmony_ci	int			family;
1658c2ecf20Sopenharmony_ci	void			(*solicit)(struct neighbour *, struct sk_buff *);
1668c2ecf20Sopenharmony_ci	void			(*error_report)(struct neighbour *, struct sk_buff *);
1678c2ecf20Sopenharmony_ci	int			(*output)(struct neighbour *, struct sk_buff *);
1688c2ecf20Sopenharmony_ci	int			(*connected_output)(struct neighbour *, struct sk_buff *);
1698c2ecf20Sopenharmony_ci};
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistruct pneigh_entry {
1728c2ecf20Sopenharmony_ci	struct pneigh_entry	*next;
1738c2ecf20Sopenharmony_ci	possible_net_t		net;
1748c2ecf20Sopenharmony_ci	struct net_device	*dev;
1758c2ecf20Sopenharmony_ci	u8			flags;
1768c2ecf20Sopenharmony_ci	u8			protocol;
1778c2ecf20Sopenharmony_ci	u32			key[];
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci/*
1818c2ecf20Sopenharmony_ci *	neighbour table manipulation
1828c2ecf20Sopenharmony_ci */
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci#define NEIGH_NUM_HASH_RND	4
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistruct neigh_hash_table {
1878c2ecf20Sopenharmony_ci	struct neighbour __rcu	**hash_buckets;
1888c2ecf20Sopenharmony_ci	unsigned int		hash_shift;
1898c2ecf20Sopenharmony_ci	__u32			hash_rnd[NEIGH_NUM_HASH_RND];
1908c2ecf20Sopenharmony_ci	struct rcu_head		rcu;
1918c2ecf20Sopenharmony_ci};
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistruct neigh_table {
1958c2ecf20Sopenharmony_ci	int			family;
1968c2ecf20Sopenharmony_ci	unsigned int		entry_size;
1978c2ecf20Sopenharmony_ci	unsigned int		key_len;
1988c2ecf20Sopenharmony_ci	__be16			protocol;
1998c2ecf20Sopenharmony_ci	__u32			(*hash)(const void *pkey,
2008c2ecf20Sopenharmony_ci					const struct net_device *dev,
2018c2ecf20Sopenharmony_ci					__u32 *hash_rnd);
2028c2ecf20Sopenharmony_ci	bool			(*key_eq)(const struct neighbour *, const void *pkey);
2038c2ecf20Sopenharmony_ci	int			(*constructor)(struct neighbour *);
2048c2ecf20Sopenharmony_ci	int			(*pconstructor)(struct pneigh_entry *);
2058c2ecf20Sopenharmony_ci	void			(*pdestructor)(struct pneigh_entry *);
2068c2ecf20Sopenharmony_ci	void			(*proxy_redo)(struct sk_buff *skb);
2078c2ecf20Sopenharmony_ci	int			(*is_multicast)(const void *pkey);
2088c2ecf20Sopenharmony_ci	bool			(*allow_add)(const struct net_device *dev,
2098c2ecf20Sopenharmony_ci					     struct netlink_ext_ack *extack);
2108c2ecf20Sopenharmony_ci	char			*id;
2118c2ecf20Sopenharmony_ci	struct neigh_parms	parms;
2128c2ecf20Sopenharmony_ci	struct list_head	parms_list;
2138c2ecf20Sopenharmony_ci	int			gc_interval;
2148c2ecf20Sopenharmony_ci	int			gc_thresh1;
2158c2ecf20Sopenharmony_ci	int			gc_thresh2;
2168c2ecf20Sopenharmony_ci	int			gc_thresh3;
2178c2ecf20Sopenharmony_ci	unsigned long		last_flush;
2188c2ecf20Sopenharmony_ci	struct delayed_work	gc_work;
2198c2ecf20Sopenharmony_ci	struct timer_list 	proxy_timer;
2208c2ecf20Sopenharmony_ci	struct sk_buff_head	proxy_queue;
2218c2ecf20Sopenharmony_ci	atomic_t		entries;
2228c2ecf20Sopenharmony_ci	atomic_t		gc_entries;
2238c2ecf20Sopenharmony_ci	struct list_head	gc_list;
2248c2ecf20Sopenharmony_ci	rwlock_t		lock;
2258c2ecf20Sopenharmony_ci	unsigned long		last_rand;
2268c2ecf20Sopenharmony_ci	struct neigh_statistics	__percpu *stats;
2278c2ecf20Sopenharmony_ci	struct neigh_hash_table __rcu *nht;
2288c2ecf20Sopenharmony_ci	struct pneigh_entry	**phash_buckets;
2298c2ecf20Sopenharmony_ci};
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cienum {
2328c2ecf20Sopenharmony_ci	NEIGH_ARP_TABLE = 0,
2338c2ecf20Sopenharmony_ci	NEIGH_ND_TABLE = 1,
2348c2ecf20Sopenharmony_ci	NEIGH_DN_TABLE = 2,
2358c2ecf20Sopenharmony_ci#ifdef CONFIG_NEWIP
2368c2ecf20Sopenharmony_ci	NEIGH_NND_TABLE = 3,	/* NIP */
2378c2ecf20Sopenharmony_ci#endif
2388c2ecf20Sopenharmony_ci	NEIGH_NR_TABLES,
2398c2ecf20Sopenharmony_ci	NEIGH_LINK_TABLE = NEIGH_NR_TABLES /* Pseudo table for neigh_xmit */
2408c2ecf20Sopenharmony_ci};
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic inline int neigh_parms_family(struct neigh_parms *p)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	return p->tbl->family;
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci#define NEIGH_PRIV_ALIGN	sizeof(long long)
2488c2ecf20Sopenharmony_ci#define NEIGH_ENTRY_SIZE(size)	ALIGN((size), NEIGH_PRIV_ALIGN)
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic inline void *neighbour_priv(const struct neighbour *n)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci	return (char *)n + n->tbl->entry_size;
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci/* flags for neigh_update() */
2568c2ecf20Sopenharmony_ci#define NEIGH_UPDATE_F_OVERRIDE			0x00000001
2578c2ecf20Sopenharmony_ci#define NEIGH_UPDATE_F_WEAK_OVERRIDE		0x00000002
2588c2ecf20Sopenharmony_ci#define NEIGH_UPDATE_F_OVERRIDE_ISROUTER	0x00000004
2598c2ecf20Sopenharmony_ci#define NEIGH_UPDATE_F_USE			0x10000000
2608c2ecf20Sopenharmony_ci#define NEIGH_UPDATE_F_EXT_LEARNED		0x20000000
2618c2ecf20Sopenharmony_ci#define NEIGH_UPDATE_F_ISROUTER			0x40000000
2628c2ecf20Sopenharmony_ci#define NEIGH_UPDATE_F_ADMIN			0x80000000
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ciextern const struct nla_policy nda_policy[];
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic inline bool neigh_key_eq32(const struct neighbour *n, const void *pkey)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	return *(const u32 *)n->primary_key == *(const u32 *)pkey;
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic inline bool neigh_key_eq128(const struct neighbour *n, const void *pkey)
2728c2ecf20Sopenharmony_ci{
2738c2ecf20Sopenharmony_ci	const u32 *n32 = (const u32 *)n->primary_key;
2748c2ecf20Sopenharmony_ci	const u32 *p32 = pkey;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	return ((n32[0] ^ p32[0]) | (n32[1] ^ p32[1]) |
2778c2ecf20Sopenharmony_ci		(n32[2] ^ p32[2]) | (n32[3] ^ p32[3])) == 0;
2788c2ecf20Sopenharmony_ci}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_cistatic inline struct neighbour *___neigh_lookup_noref(
2818c2ecf20Sopenharmony_ci	struct neigh_table *tbl,
2828c2ecf20Sopenharmony_ci	bool (*key_eq)(const struct neighbour *n, const void *pkey),
2838c2ecf20Sopenharmony_ci	__u32 (*hash)(const void *pkey,
2848c2ecf20Sopenharmony_ci		      const struct net_device *dev,
2858c2ecf20Sopenharmony_ci		      __u32 *hash_rnd),
2868c2ecf20Sopenharmony_ci	const void *pkey,
2878c2ecf20Sopenharmony_ci	struct net_device *dev)
2888c2ecf20Sopenharmony_ci{
2898c2ecf20Sopenharmony_ci	struct neigh_hash_table *nht = rcu_dereference_bh(tbl->nht);
2908c2ecf20Sopenharmony_ci	struct neighbour *n;
2918c2ecf20Sopenharmony_ci	u32 hash_val;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	hash_val = hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
2948c2ecf20Sopenharmony_ci	for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
2958c2ecf20Sopenharmony_ci	     n != NULL;
2968c2ecf20Sopenharmony_ci	     n = rcu_dereference_bh(n->next)) {
2978c2ecf20Sopenharmony_ci		if (n->dev == dev && key_eq(n, pkey))
2988c2ecf20Sopenharmony_ci			return n;
2998c2ecf20Sopenharmony_ci	}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	return NULL;
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic inline struct neighbour *__neigh_lookup_noref(struct neigh_table *tbl,
3058c2ecf20Sopenharmony_ci						     const void *pkey,
3068c2ecf20Sopenharmony_ci						     struct net_device *dev)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	return ___neigh_lookup_noref(tbl, tbl->key_eq, tbl->hash, pkey, dev);
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_civoid neigh_table_init(int index, struct neigh_table *tbl);
3128c2ecf20Sopenharmony_ciint neigh_table_clear(int index, struct neigh_table *tbl);
3138c2ecf20Sopenharmony_cistruct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
3148c2ecf20Sopenharmony_ci			       struct net_device *dev);
3158c2ecf20Sopenharmony_cistruct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
3168c2ecf20Sopenharmony_ci				 struct net_device *dev, bool want_ref);
3178c2ecf20Sopenharmony_cistatic inline struct neighbour *neigh_create(struct neigh_table *tbl,
3188c2ecf20Sopenharmony_ci					     const void *pkey,
3198c2ecf20Sopenharmony_ci					     struct net_device *dev)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	return __neigh_create(tbl, pkey, dev, true);
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_civoid neigh_destroy(struct neighbour *neigh);
3248c2ecf20Sopenharmony_ciint __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
3258c2ecf20Sopenharmony_ciint neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, u32 flags,
3268c2ecf20Sopenharmony_ci		 u32 nlmsg_pid);
3278c2ecf20Sopenharmony_civoid __neigh_set_probe_once(struct neighbour *neigh);
3288c2ecf20Sopenharmony_cibool neigh_remove_one(struct neighbour *ndel, struct neigh_table *tbl);
3298c2ecf20Sopenharmony_civoid neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
3308c2ecf20Sopenharmony_ciint neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
3318c2ecf20Sopenharmony_ciint neigh_carrier_down(struct neigh_table *tbl, struct net_device *dev);
3328c2ecf20Sopenharmony_ciint neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb);
3338c2ecf20Sopenharmony_ciint neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb);
3348c2ecf20Sopenharmony_ciint neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb);
3358c2ecf20Sopenharmony_cistruct neighbour *neigh_event_ns(struct neigh_table *tbl,
3368c2ecf20Sopenharmony_ci						u8 *lladdr, void *saddr,
3378c2ecf20Sopenharmony_ci						struct net_device *dev);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistruct neigh_parms *neigh_parms_alloc(struct net_device *dev,
3408c2ecf20Sopenharmony_ci				      struct neigh_table *tbl);
3418c2ecf20Sopenharmony_civoid neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic inline
3448c2ecf20Sopenharmony_cistruct net *neigh_parms_net(const struct neigh_parms *parms)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	return read_pnet(&parms->net);
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ciunsigned long neigh_rand_reach_time(unsigned long base);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_civoid pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
3528c2ecf20Sopenharmony_ci		    struct sk_buff *skb);
3538c2ecf20Sopenharmony_cistruct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, struct net *net,
3548c2ecf20Sopenharmony_ci				   const void *key, struct net_device *dev,
3558c2ecf20Sopenharmony_ci				   int creat);
3568c2ecf20Sopenharmony_cistruct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl, struct net *net,
3578c2ecf20Sopenharmony_ci				     const void *key, struct net_device *dev);
3588c2ecf20Sopenharmony_ciint pneigh_delete(struct neigh_table *tbl, struct net *net, const void *key,
3598c2ecf20Sopenharmony_ci		  struct net_device *dev);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_cistatic inline struct net *pneigh_net(const struct pneigh_entry *pneigh)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	return read_pnet(&pneigh->net);
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_civoid neigh_app_ns(struct neighbour *n);
3678c2ecf20Sopenharmony_civoid neigh_for_each(struct neigh_table *tbl,
3688c2ecf20Sopenharmony_ci		    void (*cb)(struct neighbour *, void *), void *cookie);
3698c2ecf20Sopenharmony_civoid __neigh_for_each_release(struct neigh_table *tbl,
3708c2ecf20Sopenharmony_ci			      int (*cb)(struct neighbour *));
3718c2ecf20Sopenharmony_ciint neigh_xmit(int fam, struct net_device *, const void *, struct sk_buff *);
3728c2ecf20Sopenharmony_civoid pneigh_for_each(struct neigh_table *tbl,
3738c2ecf20Sopenharmony_ci		     void (*cb)(struct pneigh_entry *));
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistruct neigh_seq_state {
3768c2ecf20Sopenharmony_ci	struct seq_net_private p;
3778c2ecf20Sopenharmony_ci	struct neigh_table *tbl;
3788c2ecf20Sopenharmony_ci	struct neigh_hash_table *nht;
3798c2ecf20Sopenharmony_ci	void *(*neigh_sub_iter)(struct neigh_seq_state *state,
3808c2ecf20Sopenharmony_ci				struct neighbour *n, loff_t *pos);
3818c2ecf20Sopenharmony_ci	unsigned int bucket;
3828c2ecf20Sopenharmony_ci	unsigned int flags;
3838c2ecf20Sopenharmony_ci#define NEIGH_SEQ_NEIGH_ONLY	0x00000001
3848c2ecf20Sopenharmony_ci#define NEIGH_SEQ_IS_PNEIGH	0x00000002
3858c2ecf20Sopenharmony_ci#define NEIGH_SEQ_SKIP_NOARP	0x00000004
3868c2ecf20Sopenharmony_ci};
3878c2ecf20Sopenharmony_civoid *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *,
3888c2ecf20Sopenharmony_ci		      unsigned int);
3898c2ecf20Sopenharmony_civoid *neigh_seq_next(struct seq_file *, void *, loff_t *);
3908c2ecf20Sopenharmony_civoid neigh_seq_stop(struct seq_file *, void *);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ciint neigh_proc_dointvec(struct ctl_table *ctl, int write,
3938c2ecf20Sopenharmony_ci			void *buffer, size_t *lenp, loff_t *ppos);
3948c2ecf20Sopenharmony_ciint neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
3958c2ecf20Sopenharmony_ci				void *buffer,
3968c2ecf20Sopenharmony_ci				size_t *lenp, loff_t *ppos);
3978c2ecf20Sopenharmony_ciint neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
3988c2ecf20Sopenharmony_ci				   void *buffer, size_t *lenp, loff_t *ppos);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ciint neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
4018c2ecf20Sopenharmony_ci			  proc_handler *proc_handler);
4028c2ecf20Sopenharmony_civoid neigh_sysctl_unregister(struct neigh_parms *p);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistatic inline void __neigh_parms_put(struct neigh_parms *parms)
4058c2ecf20Sopenharmony_ci{
4068c2ecf20Sopenharmony_ci	refcount_dec(&parms->refcnt);
4078c2ecf20Sopenharmony_ci}
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cistatic inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci	refcount_inc(&parms->refcnt);
4128c2ecf20Sopenharmony_ci	return parms;
4138c2ecf20Sopenharmony_ci}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci/*
4168c2ecf20Sopenharmony_ci *	Neighbour references
4178c2ecf20Sopenharmony_ci */
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic inline void neigh_release(struct neighbour *neigh)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	if (refcount_dec_and_test(&neigh->refcnt))
4228c2ecf20Sopenharmony_ci		neigh_destroy(neigh);
4238c2ecf20Sopenharmony_ci}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cistatic inline struct neighbour * neigh_clone(struct neighbour *neigh)
4268c2ecf20Sopenharmony_ci{
4278c2ecf20Sopenharmony_ci	if (neigh)
4288c2ecf20Sopenharmony_ci		refcount_inc(&neigh->refcnt);
4298c2ecf20Sopenharmony_ci	return neigh;
4308c2ecf20Sopenharmony_ci}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci#define neigh_hold(n)	refcount_inc(&(n)->refcnt)
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_cistatic inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
4358c2ecf20Sopenharmony_ci{
4368c2ecf20Sopenharmony_ci	unsigned long now = jiffies;
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	if (READ_ONCE(neigh->used) != now)
4398c2ecf20Sopenharmony_ci		WRITE_ONCE(neigh->used, now);
4408c2ecf20Sopenharmony_ci	if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
4418c2ecf20Sopenharmony_ci		return __neigh_event_send(neigh, skb);
4428c2ecf20Sopenharmony_ci	return 0;
4438c2ecf20Sopenharmony_ci}
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4468c2ecf20Sopenharmony_cistatic inline int neigh_hh_bridge(struct hh_cache *hh, struct sk_buff *skb)
4478c2ecf20Sopenharmony_ci{
4488c2ecf20Sopenharmony_ci	unsigned int seq, hh_alen;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	do {
4518c2ecf20Sopenharmony_ci		seq = read_seqbegin(&hh->hh_lock);
4528c2ecf20Sopenharmony_ci		hh_alen = HH_DATA_ALIGN(ETH_HLEN);
4538c2ecf20Sopenharmony_ci		memcpy(skb->data - hh_alen, hh->hh_data, ETH_ALEN + hh_alen - ETH_HLEN);
4548c2ecf20Sopenharmony_ci	} while (read_seqretry(&hh->hh_lock, seq));
4558c2ecf20Sopenharmony_ci	return 0;
4568c2ecf20Sopenharmony_ci}
4578c2ecf20Sopenharmony_ci#endif
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_cistatic inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
4608c2ecf20Sopenharmony_ci{
4618c2ecf20Sopenharmony_ci	unsigned int hh_alen = 0;
4628c2ecf20Sopenharmony_ci	unsigned int seq;
4638c2ecf20Sopenharmony_ci	unsigned int hh_len;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	do {
4668c2ecf20Sopenharmony_ci		seq = read_seqbegin(&hh->hh_lock);
4678c2ecf20Sopenharmony_ci		hh_len = READ_ONCE(hh->hh_len);
4688c2ecf20Sopenharmony_ci		if (likely(hh_len <= HH_DATA_MOD)) {
4698c2ecf20Sopenharmony_ci			hh_alen = HH_DATA_MOD;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci			/* skb_push() would proceed silently if we have room for
4728c2ecf20Sopenharmony_ci			 * the unaligned size but not for the aligned size:
4738c2ecf20Sopenharmony_ci			 * check headroom explicitly.
4748c2ecf20Sopenharmony_ci			 */
4758c2ecf20Sopenharmony_ci			if (likely(skb_headroom(skb) >= HH_DATA_MOD)) {
4768c2ecf20Sopenharmony_ci				/* this is inlined by gcc */
4778c2ecf20Sopenharmony_ci				memcpy(skb->data - HH_DATA_MOD, hh->hh_data,
4788c2ecf20Sopenharmony_ci				       HH_DATA_MOD);
4798c2ecf20Sopenharmony_ci			}
4808c2ecf20Sopenharmony_ci		} else {
4818c2ecf20Sopenharmony_ci			hh_alen = HH_DATA_ALIGN(hh_len);
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci			if (likely(skb_headroom(skb) >= hh_alen)) {
4848c2ecf20Sopenharmony_ci				memcpy(skb->data - hh_alen, hh->hh_data,
4858c2ecf20Sopenharmony_ci				       hh_alen);
4868c2ecf20Sopenharmony_ci			}
4878c2ecf20Sopenharmony_ci		}
4888c2ecf20Sopenharmony_ci	} while (read_seqretry(&hh->hh_lock, seq));
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(skb_headroom(skb) < hh_alen)) {
4918c2ecf20Sopenharmony_ci		kfree_skb(skb);
4928c2ecf20Sopenharmony_ci		return NET_XMIT_DROP;
4938c2ecf20Sopenharmony_ci	}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	__skb_push(skb, hh_len);
4968c2ecf20Sopenharmony_ci	return dev_queue_xmit(skb);
4978c2ecf20Sopenharmony_ci}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_cistatic inline int neigh_output(struct neighbour *n, struct sk_buff *skb,
5008c2ecf20Sopenharmony_ci			       bool skip_cache)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	const struct hh_cache *hh = &n->hh;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	/* n->nud_state and hh->hh_len could be changed under us.
5058c2ecf20Sopenharmony_ci	 * neigh_hh_output() is taking care of the race later.
5068c2ecf20Sopenharmony_ci	 */
5078c2ecf20Sopenharmony_ci	if (!skip_cache &&
5088c2ecf20Sopenharmony_ci	    (READ_ONCE(n->nud_state) & NUD_CONNECTED) &&
5098c2ecf20Sopenharmony_ci	    READ_ONCE(hh->hh_len))
5108c2ecf20Sopenharmony_ci		return neigh_hh_output(hh, skb);
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	return n->output(n, skb);
5138c2ecf20Sopenharmony_ci}
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_cistatic inline struct neighbour *
5168c2ecf20Sopenharmony_ci__neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
5178c2ecf20Sopenharmony_ci{
5188c2ecf20Sopenharmony_ci	struct neighbour *n = neigh_lookup(tbl, pkey, dev);
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	if (n || !creat)
5218c2ecf20Sopenharmony_ci		return n;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	n = neigh_create(tbl, pkey, dev);
5248c2ecf20Sopenharmony_ci	return IS_ERR(n) ? NULL : n;
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_cistatic inline struct neighbour *
5288c2ecf20Sopenharmony_ci__neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
5298c2ecf20Sopenharmony_ci  struct net_device *dev)
5308c2ecf20Sopenharmony_ci{
5318c2ecf20Sopenharmony_ci	struct neighbour *n = neigh_lookup(tbl, pkey, dev);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	if (n)
5348c2ecf20Sopenharmony_ci		return n;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	return neigh_create(tbl, pkey, dev);
5378c2ecf20Sopenharmony_ci}
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_cistruct neighbour_cb {
5408c2ecf20Sopenharmony_ci	unsigned long sched_next;
5418c2ecf20Sopenharmony_ci	unsigned int flags;
5428c2ecf20Sopenharmony_ci};
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci#define LOCALLY_ENQUEUED 0x1
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci#define NEIGH_CB(skb)	((struct neighbour_cb *)(skb)->cb)
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_cistatic inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
5498c2ecf20Sopenharmony_ci				     const struct net_device *dev)
5508c2ecf20Sopenharmony_ci{
5518c2ecf20Sopenharmony_ci	unsigned int seq;
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	do {
5548c2ecf20Sopenharmony_ci		seq = read_seqbegin(&n->ha_lock);
5558c2ecf20Sopenharmony_ci		memcpy(dst, n->ha, dev->addr_len);
5568c2ecf20Sopenharmony_ci	} while (read_seqretry(&n->ha_lock, seq));
5578c2ecf20Sopenharmony_ci}
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_cistatic inline void neigh_update_is_router(struct neighbour *neigh, u32 flags,
5608c2ecf20Sopenharmony_ci					  int *notify)
5618c2ecf20Sopenharmony_ci{
5628c2ecf20Sopenharmony_ci	u8 ndm_flags = 0;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	ndm_flags |= (flags & NEIGH_UPDATE_F_ISROUTER) ? NTF_ROUTER : 0;
5658c2ecf20Sopenharmony_ci	if ((neigh->flags ^ ndm_flags) & NTF_ROUTER) {
5668c2ecf20Sopenharmony_ci		if (ndm_flags & NTF_ROUTER)
5678c2ecf20Sopenharmony_ci			neigh->flags |= NTF_ROUTER;
5688c2ecf20Sopenharmony_ci		else
5698c2ecf20Sopenharmony_ci			neigh->flags &= ~NTF_ROUTER;
5708c2ecf20Sopenharmony_ci		*notify = 1;
5718c2ecf20Sopenharmony_ci	}
5728c2ecf20Sopenharmony_ci}
5738c2ecf20Sopenharmony_ci#endif
574