1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *	inet6 interface/address list definitions
4  *	Linux INET6 implementation
5  *
6  *	Authors:
7  *	Pedro Roque		<roque@di.fc.ul.pt>
8  */
9 
10 #ifndef _NET_IF_INET6_H
11 #define _NET_IF_INET6_H
12 
13 #include <net/snmp.h>
14 #include <linux/ipv6.h>
15 #include <linux/refcount.h>
16 
17 /* inet6_dev.if_flags */
18 
19 #define IF_RA_OTHERCONF	0x80
20 #define IF_RA_MANAGED	0x40
21 #define IF_RA_RCVD	0x20
22 #define IF_RS_SENT	0x10
23 #define IF_READY	0x80000000
24 
25 /* prefix flags */
26 #define IF_PREFIX_ONLINK	0x01
27 #define IF_PREFIX_AUTOCONF	0x02
28 
29 enum {
30 	INET6_IFADDR_STATE_PREDAD,
31 	INET6_IFADDR_STATE_DAD,
32 	INET6_IFADDR_STATE_POSTDAD,
33 	INET6_IFADDR_STATE_ERRDAD,
34 	INET6_IFADDR_STATE_DEAD,
35 };
36 
37 struct inet6_ifaddr {
38 	struct in6_addr		addr;
39 	__u32			prefix_len;
40 	__u32			rt_priority;
41 
42 	/* In seconds, relative to tstamp. Expiry is at tstamp + HZ * lft. */
43 	__u32			valid_lft;
44 	__u32			prefered_lft;
45 	refcount_t		refcnt;
46 	spinlock_t		lock;
47 
48 	int			state;
49 
50 	__u32			flags;
51 	__u8			dad_probes;
52 	__u8			stable_privacy_retry;
53 
54 	__u16			scope;
55 	__u64			dad_nonce;
56 
57 	unsigned long		cstamp;	/* created timestamp */
58 	unsigned long		tstamp; /* updated timestamp */
59 
60 	struct delayed_work	dad_work;
61 
62 	struct inet6_dev	*idev;
63 	struct fib6_info	*rt;
64 
65 	struct hlist_node	addr_lst;
66 	struct list_head	if_list;
67 
68 	struct list_head	tmp_list;
69 	struct inet6_ifaddr	*ifpub;
70 	int			regen_count;
71 
72 	bool			tokenized;
73 
74 	struct rcu_head		rcu;
75 	struct in6_addr		peer_addr;
76 };
77 
78 struct ip6_sf_socklist {
79 	unsigned int		sl_max;
80 	unsigned int		sl_count;
81 	struct in6_addr		sl_addr[0];
82 };
83 
84 #define IP6_SFLSIZE(count)	(sizeof(struct ip6_sf_socklist) + \
85 	(count) * sizeof(struct in6_addr))
86 
87 #define IP6_SFBLOCK	10	/* allocate this many at once */
88 
89 struct ipv6_mc_socklist {
90 	struct in6_addr		addr;
91 	int			ifindex;
92 	unsigned int		sfmode;		/* MCAST_{INCLUDE,EXCLUDE} */
93 	struct ipv6_mc_socklist __rcu *next;
94 	rwlock_t		sflock;
95 	struct ip6_sf_socklist	*sflist;
96 	struct rcu_head		rcu;
97 };
98 
99 struct ip6_sf_list {
100 	struct ip6_sf_list	*sf_next;
101 	struct in6_addr		sf_addr;
102 	unsigned long		sf_count[2];	/* include/exclude counts */
103 	unsigned char		sf_gsresp;	/* include in g & s response? */
104 	unsigned char		sf_oldin;	/* change state */
105 	unsigned char		sf_crcount;	/* retrans. left to send */
106 };
107 
108 #define MAF_TIMER_RUNNING	0x01
109 #define MAF_LAST_REPORTER	0x02
110 #define MAF_LOADED		0x04
111 #define MAF_NOREPORT		0x08
112 #define MAF_GSQUERY		0x10
113 
114 struct ifmcaddr6 {
115 	struct in6_addr		mca_addr;
116 	struct inet6_dev	*idev;
117 	struct ifmcaddr6	*next;
118 	struct ip6_sf_list	*mca_sources;
119 	struct ip6_sf_list	*mca_tomb;
120 	unsigned int		mca_sfmode;
121 	unsigned char		mca_crcount;
122 	unsigned long		mca_sfcount[2];
123 	struct timer_list	mca_timer;
124 	unsigned int		mca_flags;
125 	int			mca_users;
126 	refcount_t		mca_refcnt;
127 	spinlock_t		mca_lock;
128 	unsigned long		mca_cstamp;
129 	unsigned long		mca_tstamp;
130 };
131 
132 /* Anycast stuff */
133 
134 struct ipv6_ac_socklist {
135 	struct in6_addr		acl_addr;
136 	int			acl_ifindex;
137 	struct ipv6_ac_socklist *acl_next;
138 };
139 
140 struct ifacaddr6 {
141 	struct in6_addr		aca_addr;
142 	struct fib6_info	*aca_rt;
143 	struct ifacaddr6	*aca_next;
144 	struct hlist_node	aca_addr_lst;
145 	int			aca_users;
146 	refcount_t		aca_refcnt;
147 	unsigned long		aca_cstamp;
148 	unsigned long		aca_tstamp;
149 	struct rcu_head		rcu;
150 };
151 
152 #define	IFA_HOST	IPV6_ADDR_LOOPBACK
153 #define	IFA_LINK	IPV6_ADDR_LINKLOCAL
154 #define	IFA_SITE	IPV6_ADDR_SITELOCAL
155 
156 struct ipv6_devstat {
157 	struct proc_dir_entry	*proc_dir_entry;
158 	DEFINE_SNMP_STAT(struct ipstats_mib, ipv6);
159 	DEFINE_SNMP_STAT_ATOMIC(struct icmpv6_mib_device, icmpv6dev);
160 	DEFINE_SNMP_STAT_ATOMIC(struct icmpv6msg_mib_device, icmpv6msgdev);
161 };
162 
163 struct inet6_dev {
164 	struct net_device	*dev;
165 
166 	struct list_head	addr_list;
167 
168 	struct ifmcaddr6	*mc_list;
169 	struct ifmcaddr6	*mc_tomb;
170 	spinlock_t		mc_lock;
171 
172 	unsigned char		mc_qrv;		/* Query Robustness Variable */
173 	unsigned char		mc_gq_running;
174 	unsigned char		mc_ifc_count;
175 	unsigned char		mc_dad_count;
176 
177 	unsigned long		mc_v1_seen;	/* Max time we stay in MLDv1 mode */
178 	unsigned long		mc_qi;		/* Query Interval */
179 	unsigned long		mc_qri;		/* Query Response Interval */
180 	unsigned long		mc_maxdelay;
181 
182 	struct timer_list	mc_gq_timer;	/* general query timer */
183 	struct timer_list	mc_ifc_timer;	/* interface change timer */
184 	struct timer_list	mc_dad_timer;	/* dad complete mc timer */
185 
186 	struct ifacaddr6	*ac_list;
187 	rwlock_t		lock;
188 	refcount_t		refcnt;
189 	__u32			if_flags;
190 	int			dead;
191 
192 	u32			desync_factor;
193 	u8			rndid[8];
194 	struct list_head	tempaddr_list;
195 
196 	struct in6_addr		token;
197 
198 	struct neigh_parms	*nd_parms;
199 	struct ipv6_devconf	cnf;
200 	struct ipv6_devstat	stats;
201 
202 	struct timer_list	rs_timer;
203 	__s32			rs_interval;	/* in jiffies */
204 	__u8			rs_probes;
205 
206 	unsigned long		tstamp; /* ipv6InterfaceTable update timestamp */
207 	struct rcu_head		rcu;
208 };
209 
ipv6_eth_mc_map(const struct in6_addr * addr,char * buf)210 static inline void ipv6_eth_mc_map(const struct in6_addr *addr, char *buf)
211 {
212 	/*
213 	 *	+-------+-------+-------+-------+-------+-------+
214 	 *      |   33  |   33  | DST13 | DST14 | DST15 | DST16 |
215 	 *      +-------+-------+-------+-------+-------+-------+
216 	 */
217 
218 	buf[0]= 0x33;
219 	buf[1]= 0x33;
220 
221 	memcpy(buf + 2, &addr->s6_addr32[3], sizeof(__u32));
222 }
223 
ipv6_arcnet_mc_map(const struct in6_addr * addr,char * buf)224 static inline void ipv6_arcnet_mc_map(const struct in6_addr *addr, char *buf)
225 {
226 	buf[0] = 0x00;
227 }
228 
ipv6_ib_mc_map(const struct in6_addr * addr,const unsigned char * broadcast,char * buf)229 static inline void ipv6_ib_mc_map(const struct in6_addr *addr,
230 				  const unsigned char *broadcast, char *buf)
231 {
232 	unsigned char scope = broadcast[5] & 0xF;
233 
234 	buf[0]  = 0;		/* Reserved */
235 	buf[1]  = 0xff;		/* Multicast QPN */
236 	buf[2]  = 0xff;
237 	buf[3]  = 0xff;
238 	buf[4]  = 0xff;
239 	buf[5]  = 0x10 | scope;	/* scope from broadcast address */
240 	buf[6]  = 0x60;		/* IPv6 signature */
241 	buf[7]  = 0x1b;
242 	buf[8]  = broadcast[8];	/* P_Key */
243 	buf[9]  = broadcast[9];
244 	memcpy(buf + 10, addr->s6_addr + 6, 10);
245 }
246 
ipv6_ipgre_mc_map(const struct in6_addr * addr,const unsigned char * broadcast,char * buf)247 static inline int ipv6_ipgre_mc_map(const struct in6_addr *addr,
248 				    const unsigned char *broadcast, char *buf)
249 {
250 	if ((broadcast[0] | broadcast[1] | broadcast[2] | broadcast[3]) != 0) {
251 		memcpy(buf, broadcast, 4);
252 	} else {
253 		/* v4mapped? */
254 		if ((addr->s6_addr32[0] | addr->s6_addr32[1] |
255 		     (addr->s6_addr32[2] ^ htonl(0x0000ffff))) != 0)
256 			return -EINVAL;
257 		memcpy(buf, &addr->s6_addr32[3], 4);
258 	}
259 	return 0;
260 }
261 
262 #endif
263