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 */
13struct 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
98struct ipv6_params {
99 __s32 disable_ipv6;
100 __s32 autoconf;
101};
102extern struct ipv6_params ipv6_defaults;
103#include <linux/tcp.h>
104#include <linux/udp.h>
105
106#include <net/inet_sock.h>
107
108static inline struct ipv6hdr *ipv6_hdr(const struct sk_buff *skb)
109{
110 return (struct ipv6hdr *)skb_network_header(skb);
111}
112
113static inline struct ipv6hdr *inner_ipv6_hdr(const struct sk_buff *skb)
114{
115 return (struct ipv6hdr *)skb_inner_network_header(skb);
116}
117
118static inline struct ipv6hdr *ipipv6_hdr(const struct sk_buff *skb)
119{
120 return (struct ipv6hdr *)skb_transport_header(skb);
121}
122
123static 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
134struct 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)
164static inline bool ipv6_l3mdev_skb(__u16 flags)
165{
166 return flags & IP6SKB_L3SLAVE;
167}
168#else
169static 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
178static 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
185static 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 */
191static 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
200struct tcp6_request_sock {
201 struct tcp_request_sock tcp6rsk_tcp;
202};
203
204struct ipv6_mc_socklist;
205struct ipv6_ac_socklist;
206struct ipv6_fl_socklist;
207
208struct 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 */
216struct ipv6_pinfo {
217 /* Used in tx path (inet6_csk_route_socket(), ip6_xmit()) */
218 struct in6_addr saddr;
219 __be32 flow_label;
220 u32 dst_cookie;
221 struct ipv6_txoptions __rcu *opt;
222 s16 hop_limit;
223 u8 pmtudisc;
224 u8 tclass;
225#ifdef CONFIG_IPV6_SUBTREES
226 bool saddr_cache;
227#endif
228 bool daddr_cache;
229
230 u8 mcast_hops;
231 u32 frag_size;
232
233 int ucast_oif;
234 int mcast_oif;
235
236 /* pktoption flags */
237 union {
238 struct {
239 u16 srcrt:1,
240 osrcrt:1,
241 rxinfo:1,
242 rxoinfo:1,
243 rxhlim:1,
244 rxohlim:1,
245 hopopts:1,
246 ohopopts:1,
247 dstopts:1,
248 odstopts:1,
249 rxflow:1,
250 rxtclass:1,
251 rxpmtu:1,
252 rxorigdstaddr:1,
253 recvfragsize:1;
254 /* 1 bits hole */
255 } bits;
256 u16 all;
257 } rxopt;
258
259 /* sockopt flags */
260 u8 srcprefs; /* 001: prefer temporary address
261 * 010: prefer public address
262 * 100: prefer care-of address
263 */
264 u8 min_hopcount;
265 __be32 rcv_flowinfo;
266 struct in6_pktinfo sticky_pktinfo;
267
268 struct sk_buff *pktoptions;
269 struct sk_buff *rxpmtu;
270 struct inet6_cork cork;
271
272 struct ipv6_mc_socklist __rcu *ipv6_mc_list;
273 struct ipv6_ac_socklist *ipv6_ac_list;
274 struct ipv6_fl_socklist __rcu *ipv6_fl_list;
275};
276
277/* We currently use available bits from inet_sk(sk)->inet_flags,
278 * this could change in the future.
279 */
280#define inet6_test_bit(nr, sk) \
281 test_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
282#define inet6_set_bit(nr, sk) \
283 set_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
284#define inet6_clear_bit(nr, sk) \
285 clear_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
286#define inet6_assign_bit(nr, sk, val) \
287 assign_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags, val)
288
289/* WARNING: don't change the layout of the members in {raw,udp,tcp}6_sock! */
290struct raw6_sock {
291 /* inet_sock has to be the first member of raw6_sock */
292 struct inet_sock inet;
293 __u32 checksum; /* perform checksum */
294 __u32 offset; /* checksum offset */
295 struct icmp6_filter filter;
296 __u32 ip6mr_table;
297 struct numa_drop_counters drop_counters;
298 struct ipv6_pinfo inet6;
299};
300
301struct udp6_sock {
302 struct udp_sock udp;
303
304 struct ipv6_pinfo inet6;
305};
306
307struct tcp6_sock {
308 struct tcp_sock tcp;
309
310 struct ipv6_pinfo inet6;
311};
312
313extern int inet6_sk_rebuild_header(struct sock *sk);
314
315struct tcp6_timewait_sock {
316 struct tcp_timewait_sock tcp6tw_tcp;
317};
318
319#if IS_ENABLED(CONFIG_IPV6)
320bool ipv6_mod_enabled(void);
321
322static inline struct ipv6_pinfo *inet6_sk(const struct sock *__sk)
323{
324 return sk_fullsock(sk: __sk) ? inet_sk(__sk)->pinet6 : NULL;
325}
326
327#define raw6_sk(ptr) container_of_const(ptr, struct raw6_sock, inet.sk)
328
329#define ipv6_only_sock(sk) (sk->sk_ipv6only)
330#define ipv6_sk_rxinfo(sk) ((sk)->sk_family == PF_INET6 && \
331 inet6_sk(sk)->rxopt.bits.rxinfo)
332
333static inline const struct in6_addr *inet6_rcv_saddr(const struct sock *sk)
334{
335 if (sk->sk_family == AF_INET6)
336 return &sk->sk_v6_rcv_saddr;
337 return NULL;
338}
339
340static inline int inet_v6_ipv6only(const struct sock *sk)
341{
342 /* ipv6only field is at same position for timewait and other sockets */
343 return ipv6_only_sock(sk);
344}
345#else
346#define ipv6_only_sock(sk) 0
347#define ipv6_sk_rxinfo(sk) 0
348
349static inline bool ipv6_mod_enabled(void)
350{
351 return false;
352}
353
354static inline struct ipv6_pinfo * inet6_sk(const struct sock *__sk)
355{
356 return NULL;
357}
358
359static inline struct raw6_sock *raw6_sk(const struct sock *sk)
360{
361 return NULL;
362}
363
364#define inet6_rcv_saddr(__sk) NULL
365#define inet_v6_ipv6only(__sk) 0
366#endif /* IS_ENABLED(CONFIG_IPV6) */
367#endif /* _IPV6_H */
368