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