xref: /linux/include/linux/ipv6.h (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _IPV6_H
3 #define _IPV6_H
4 
5 #include <uapi/linux/ipv6.h>
6 #include <linux/cache.h>
7 
8 #define ipv6_optlen(p)  (((p)->hdrlen+1) << 3)
9 #define ipv6_authlen(p) (((p)->hdrlen+2) << 2)
10 /*
11  * This structure contains configuration options per IPv6 link.
12  */
13 struct ipv6_devconf {
14 	/* RX & TX fastpath fields. */
15 	__cacheline_group_begin(ipv6_devconf_read_txrx);
16 	__s32		disable_ipv6;
17 	__s32		hop_limit;
18 	__s32		mtu6;
19 	__s32		forwarding;
20 	__s32		force_forwarding;
21 	__s32		disable_policy;
22 	__s32		proxy_ndp;
23 	__cacheline_group_end(ipv6_devconf_read_txrx);
24 
25 	__s32		accept_ra;
26 	__s32		accept_redirects;
27 	__s32		autoconf;
28 	__s32		dad_transmits;
29 	__s32		rtr_solicits;
30 	__s32		rtr_solicit_interval;
31 	__s32		rtr_solicit_max_interval;
32 	__s32		rtr_solicit_delay;
33 	__s32		force_mld_version;
34 	__s32		mldv1_unsolicited_report_interval;
35 	__s32		mldv2_unsolicited_report_interval;
36 	__s32		use_tempaddr;
37 	__s32		temp_valid_lft;
38 	__s32		temp_prefered_lft;
39 	__s32		regen_min_advance;
40 	__s32		regen_max_retry;
41 	__s32		max_desync_factor;
42 	__s32		max_addresses;
43 	__s32		accept_ra_defrtr;
44 	__u32		ra_defrtr_metric;
45 	__s32		accept_ra_min_hop_limit;
46 	__s32		accept_ra_min_lft;
47 	__s32		accept_ra_pinfo;
48 	__s32		ignore_routes_with_linkdown;
49 #ifdef CONFIG_IPV6_ROUTER_PREF
50 	__s32		accept_ra_rtr_pref;
51 	__s32		rtr_probe_interval;
52 #ifdef CONFIG_IPV6_ROUTE_INFO
53 	__s32		accept_ra_rt_info_min_plen;
54 	__s32		accept_ra_rt_info_max_plen;
55 #endif
56 #endif
57 	__s32		accept_source_route;
58 	__s32		accept_ra_from_local;
59 #ifdef CONFIG_IPV6_OPTIMISTIC_DAD
60 	__s32		optimistic_dad;
61 	__s32		use_optimistic;
62 #endif
63 #ifdef CONFIG_IPV6_MROUTE
64 	atomic_t	mc_forwarding;
65 #endif
66 	__s32		drop_unicast_in_l2_multicast;
67 	__s32		accept_dad;
68 	__s32		force_tllao;
69 	__s32           ndisc_notify;
70 	__s32		suppress_frag_ndisc;
71 	__s32		accept_ra_mtu;
72 	__s32		drop_unsolicited_na;
73 	__s32		accept_untracked_na;
74 	struct ipv6_stable_secret {
75 		bool initialized;
76 		struct in6_addr secret;
77 	} stable_secret;
78 	__s32		use_oif_addrs_only;
79 	__s32		keep_addr_on_down;
80 	__s32		seg6_enabled;
81 #ifdef CONFIG_IPV6_SEG6_HMAC
82 	__s32		seg6_require_hmac;
83 #endif
84 	__u32		enhanced_dad;
85 	__u32		addr_gen_mode;
86 	__s32           ndisc_tclass;
87 	__s32		rpl_seg_enabled;
88 	__u32		ioam6_id;
89 	__u32		ioam6_id_wide;
90 	__u8		ioam6_enabled;
91 	__u8		ndisc_evict_nocarrier;
92 	__u8		ra_honor_pio_life;
93 	__u8		ra_honor_pio_pflag;
94 
95 	struct ctl_table_header *sysctl_header;
96 };
97 
98 struct ipv6_params {
99 	__s32 disable_ipv6;
100 	__s32 autoconf;
101 };
102 extern struct ipv6_params ipv6_defaults;
103 #include <linux/tcp.h>
104 #include <linux/udp.h>
105 
106 #include <net/inet_sock.h>
107 
ipv6_hdr(const struct sk_buff * skb)108 static inline struct ipv6hdr *ipv6_hdr(const struct sk_buff *skb)
109 {
110 	return (struct ipv6hdr *)skb_network_header(skb);
111 }
112 
inner_ipv6_hdr(const struct sk_buff * skb)113 static inline struct ipv6hdr *inner_ipv6_hdr(const struct sk_buff *skb)
114 {
115 	return (struct ipv6hdr *)skb_inner_network_header(skb);
116 }
117 
ipipv6_hdr(const struct sk_buff * skb)118 static inline struct ipv6hdr *ipipv6_hdr(const struct sk_buff *skb)
119 {
120 	return (struct ipv6hdr *)skb_transport_header(skb);
121 }
122 
ipv6_transport_len(const struct sk_buff * skb)123 static inline unsigned int ipv6_transport_len(const struct sk_buff *skb)
124 {
125 	return ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr) -
126 	       skb_network_header_len(skb);
127 }
128 
129 /*
130    This structure contains results of exthdrs parsing
131    as offsets from skb->nh.
132  */
133 
134 struct inet6_skb_parm {
135 	int			iif;
136 	__be16			ra;
137 	__u16			dst0;
138 	__u16			srcrt;
139 	__u16			dst1;
140 	__u16			lastopt;
141 	__u16			nhoff;
142 	__u16			flags;
143 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
144 	__u16			dsthao;
145 #endif
146 	__u16			frag_max_size;
147 	__u16			srhoff;
148 
149 #define IP6SKB_XFRM_TRANSFORMED	1
150 #define IP6SKB_FORWARDED	2
151 #define IP6SKB_REROUTED		4
152 #define IP6SKB_ROUTERALERT	8
153 #define IP6SKB_FRAGMENTED      16
154 #define IP6SKB_HOPBYHOP        32
155 #define IP6SKB_L3SLAVE         64
156 #define IP6SKB_JUMBOGRAM      128
157 #define IP6SKB_SEG6	      256
158 #define IP6SKB_FAKEJUMBO      512
159 #define IP6SKB_MULTIPATH      1024
160 #define IP6SKB_MCROUTE        2048
161 };
162 
163 #if defined(CONFIG_NET_L3_MASTER_DEV)
ipv6_l3mdev_skb(__u16 flags)164 static inline bool ipv6_l3mdev_skb(__u16 flags)
165 {
166 	return flags & IP6SKB_L3SLAVE;
167 }
168 #else
ipv6_l3mdev_skb(__u16 flags)169 static inline bool ipv6_l3mdev_skb(__u16 flags)
170 {
171 	return false;
172 }
173 #endif
174 
175 #define IP6CB(skb)	((struct inet6_skb_parm*)((skb)->cb))
176 #define IP6CBMTU(skb)	((struct ip6_mtuinfo *)((skb)->cb))
177 
inet6_iif(const struct sk_buff * skb)178 static inline int inet6_iif(const struct sk_buff *skb)
179 {
180 	bool l3_slave = ipv6_l3mdev_skb(IP6CB(skb)->flags);
181 
182 	return l3_slave ? skb->skb_iif : IP6CB(skb)->iif;
183 }
184 
inet6_is_jumbogram(const struct sk_buff * skb)185 static inline bool inet6_is_jumbogram(const struct sk_buff *skb)
186 {
187 	return !!(IP6CB(skb)->flags & IP6SKB_JUMBOGRAM);
188 }
189 
190 /* can not be used in TCP layer after tcp_v6_fill_cb */
inet6_sdif(const struct sk_buff * skb)191 static inline int inet6_sdif(const struct sk_buff *skb)
192 {
193 #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
194 	if (skb && ipv6_l3mdev_skb(IP6CB(skb)->flags))
195 		return IP6CB(skb)->iif;
196 #endif
197 	return 0;
198 }
199 
200 struct tcp6_request_sock {
201 	struct tcp_request_sock	  tcp6rsk_tcp;
202 };
203 
204 struct ipv6_mc_socklist;
205 struct ipv6_ac_socklist;
206 struct ipv6_fl_socklist;
207 
208 struct inet6_cork {
209 	struct ipv6_txoptions *opt;
210 	u8 hop_limit;
211 	u8 tclass;
212 	u8 dontfrag:1;
213 };
214 
215 /* struct ipv6_pinfo - ipv6 private area */
216 struct ipv6_pinfo {
217 	struct in6_addr 	saddr;
218 	struct in6_pktinfo	sticky_pktinfo;
219 	const struct in6_addr		*daddr_cache;
220 #ifdef CONFIG_IPV6_SUBTREES
221 	const struct in6_addr		*saddr_cache;
222 #endif
223 
224 	__be32			flow_label;
225 	__u32			frag_size;
226 
227 	s16			hop_limit;
228 	u8			mcast_hops;
229 
230 	int			ucast_oif;
231 	int			mcast_oif;
232 
233 	/* pktoption flags */
234 	union {
235 		struct {
236 			__u16	srcrt:1,
237 				osrcrt:1,
238 			        rxinfo:1,
239 			        rxoinfo:1,
240 				rxhlim:1,
241 				rxohlim:1,
242 				hopopts:1,
243 				ohopopts:1,
244 				dstopts:1,
245 				odstopts:1,
246                                 rxflow:1,
247 				rxtclass:1,
248 				rxpmtu:1,
249 				rxorigdstaddr:1,
250 				recvfragsize:1;
251 				/* 1 bits hole */
252 		} bits;
253 		__u16		all;
254 	} rxopt;
255 
256 	/* sockopt flags */
257 	__u8			srcprefs;	/* 001: prefer temporary address
258 						 * 010: prefer public address
259 						 * 100: prefer care-of address
260 						 */
261 	__u8			pmtudisc;
262 	__u8			min_hopcount;
263 	__u8			tclass;
264 	__be32			rcv_flowinfo;
265 
266 	__u32			dst_cookie;
267 
268 	struct ipv6_mc_socklist	__rcu *ipv6_mc_list;
269 	struct ipv6_ac_socklist	*ipv6_ac_list;
270 	struct ipv6_fl_socklist __rcu *ipv6_fl_list;
271 
272 	struct ipv6_txoptions __rcu	*opt;
273 	struct sk_buff		*pktoptions;
274 	struct sk_buff		*rxpmtu;
275 	struct inet6_cork	cork;
276 };
277 
278 /* We currently use available bits from inet_sk(sk)->inet_flags,
279  * this could change in the future.
280  */
281 #define inet6_test_bit(nr, sk)			\
282 	test_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
283 #define inet6_set_bit(nr, sk)			\
284 	set_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
285 #define inet6_clear_bit(nr, sk)			\
286 	clear_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
287 #define inet6_assign_bit(nr, sk, val)		\
288 	assign_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags, val)
289 
290 /* WARNING: don't change the layout of the members in {raw,udp,tcp}6_sock! */
291 struct raw6_sock {
292 	/* inet_sock has to be the first member of raw6_sock */
293 	struct inet_sock	inet;
294 	__u32			checksum;	/* perform checksum */
295 	__u32			offset;		/* checksum offset  */
296 	struct icmp6_filter	filter;
297 	__u32			ip6mr_table;
298 
299 	struct ipv6_pinfo	inet6;
300 };
301 
302 struct udp6_sock {
303 	struct udp_sock	  udp;
304 
305 	struct ipv6_pinfo inet6;
306 };
307 
308 struct tcp6_sock {
309 	struct tcp_sock	  tcp;
310 
311 	struct ipv6_pinfo inet6;
312 };
313 
314 extern int inet6_sk_rebuild_header(struct sock *sk);
315 
316 struct tcp6_timewait_sock {
317 	struct tcp_timewait_sock   tcp6tw_tcp;
318 };
319 
320 #if IS_ENABLED(CONFIG_IPV6)
321 bool ipv6_mod_enabled(void);
322 
inet6_sk(const struct sock * __sk)323 static inline struct ipv6_pinfo *inet6_sk(const struct sock *__sk)
324 {
325 	return sk_fullsock(__sk) ? inet_sk(__sk)->pinet6 : NULL;
326 }
327 
328 #define raw6_sk(ptr) container_of_const(ptr, struct raw6_sock, inet.sk)
329 
330 #define ipv6_only_sock(sk)	(sk->sk_ipv6only)
331 #define ipv6_sk_rxinfo(sk)	((sk)->sk_family == PF_INET6 && \
332 				 inet6_sk(sk)->rxopt.bits.rxinfo)
333 
inet6_rcv_saddr(const struct sock * sk)334 static inline const struct in6_addr *inet6_rcv_saddr(const struct sock *sk)
335 {
336 	if (sk->sk_family == AF_INET6)
337 		return &sk->sk_v6_rcv_saddr;
338 	return NULL;
339 }
340 
inet_v6_ipv6only(const struct sock * sk)341 static inline int inet_v6_ipv6only(const struct sock *sk)
342 {
343 	/* ipv6only field is at same position for timewait and other sockets */
344 	return ipv6_only_sock(sk);
345 }
346 #else
347 #define ipv6_only_sock(sk)	0
348 #define ipv6_sk_rxinfo(sk)	0
349 
ipv6_mod_enabled(void)350 static inline bool ipv6_mod_enabled(void)
351 {
352 	return false;
353 }
354 
inet6_sk(const struct sock * __sk)355 static inline struct ipv6_pinfo * inet6_sk(const struct sock *__sk)
356 {
357 	return NULL;
358 }
359 
raw6_sk(const struct sock * sk)360 static inline struct raw6_sock *raw6_sk(const struct sock *sk)
361 {
362 	return NULL;
363 }
364 
365 #define inet6_rcv_saddr(__sk)	NULL
366 #define inet_v6_ipv6only(__sk)		0
367 #endif /* IS_ENABLED(CONFIG_IPV6) */
368 #endif /* _IPV6_H */
369