xref: /openbmc/linux/net/ipv6/raw.c (revision ec095263)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	RAW sockets for IPv6
41da177e4SLinus Torvalds  *	Linux INET6 implementation
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	Authors:
71da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	Adapted from linux/net/ipv4/raw.c
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *	Fixes:
121da177e4SLinus Torvalds  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
131da177e4SLinus Torvalds  *	YOSHIFUJI,H.@USAGI	:	raw checksum (RFC2292(bis) compliance)
141da177e4SLinus Torvalds  *	Kazunori MIYAZAWA @USAGI:	change process style to use ip6_append_data
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/errno.h>
181da177e4SLinus Torvalds #include <linux/types.h>
191da177e4SLinus Torvalds #include <linux/socket.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/sockios.h>
221da177e4SLinus Torvalds #include <linux/net.h>
231da177e4SLinus Torvalds #include <linux/in6.h>
241da177e4SLinus Torvalds #include <linux/netdevice.h>
251da177e4SLinus Torvalds #include <linux/if_arp.h>
261da177e4SLinus Torvalds #include <linux/icmpv6.h>
271da177e4SLinus Torvalds #include <linux/netfilter.h>
281da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
293305b80cSHerbert Xu #include <linux/skbuff.h>
30e2d57766SDavid S. Miller #include <linux/compat.h>
3129778becSAlex W Slater #include <linux/uaccess.h>
321da177e4SLinus Torvalds #include <asm/ioctls.h>
331da177e4SLinus Torvalds 
34457c4cbcSEric W. Biederman #include <net/net_namespace.h>
351da177e4SLinus Torvalds #include <net/ip.h>
361da177e4SLinus Torvalds #include <net/sock.h>
371da177e4SLinus Torvalds #include <net/snmp.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <net/ipv6.h>
401da177e4SLinus Torvalds #include <net/ndisc.h>
411da177e4SLinus Torvalds #include <net/protocol.h>
421da177e4SLinus Torvalds #include <net/ip6_route.h>
431da177e4SLinus Torvalds #include <net/ip6_checksum.h>
441da177e4SLinus Torvalds #include <net/addrconf.h>
451da177e4SLinus Torvalds #include <net/transp_v6.h>
461da177e4SLinus Torvalds #include <net/udp.h>
471da177e4SLinus Torvalds #include <net/inet_common.h>
48c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
4907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
507be96f76SMasahide NAKAMURA #include <net/mip6.h>
517be96f76SMasahide NAKAMURA #endif
527bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
531da177e4SLinus Torvalds 
54b673e4dfSPavel Emelyanov #include <net/raw.h>
551da177e4SLinus Torvalds #include <net/rawv6.h>
561da177e4SLinus Torvalds #include <net/xfrm.h>
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds #include <linux/proc_fs.h>
591da177e4SLinus Torvalds #include <linux/seq_file.h>
60bc3b2d7fSPaul Gortmaker #include <linux/export.h>
611da177e4SLinus Torvalds 
62d1c53c8eSWerner Almesberger #define	ICMPV6_HDRLEN	4	/* ICMPv6 header, RFC 4443 Section 2.1 */
63d1c53c8eSWerner Almesberger 
64432490f9SCyrill Gorcunov struct raw_hashinfo raw_v6_hashinfo = {
65938b93adSRobert P. J. Day 	.lock = __RW_LOCK_UNLOCKED(raw_v6_hashinfo.lock),
66b673e4dfSPavel Emelyanov };
67432490f9SCyrill Gorcunov EXPORT_SYMBOL_GPL(raw_v6_hashinfo);
681da177e4SLinus Torvalds 
69432490f9SCyrill Gorcunov struct sock *__raw_v6_lookup(struct net *net, struct sock *sk,
70b71d1d42SEric Dumazet 		unsigned short num, const struct in6_addr *loc_addr,
715108ab4bSDavid Ahern 		const struct in6_addr *rmt_addr, int dif, int sdif)
721da177e4SLinus Torvalds {
73a50feda5SEric Dumazet 	bool is_multicast = ipv6_addr_is_multicast(loc_addr);
741da177e4SLinus Torvalds 
75b67bfe0dSSasha Levin 	sk_for_each_from(sk)
76c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num == num) {
771da177e4SLinus Torvalds 
78878628fbSYOSHIFUJI Hideaki 			if (!net_eq(sock_net(sk), net))
79be185884SPavel Emelyanov 				continue;
80be185884SPavel Emelyanov 
81efe4208fSEric Dumazet 			if (!ipv6_addr_any(&sk->sk_v6_daddr) &&
82efe4208fSEric Dumazet 			    !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr))
831da177e4SLinus Torvalds 				continue;
841da177e4SLinus Torvalds 
857055420fSDuncan Eastoe 			if (!raw_sk_bound_dev_eq(net, sk->sk_bound_dev_if,
867055420fSDuncan Eastoe 						 dif, sdif))
870bd1b59bSAndrew McDonald 				continue;
880bd1b59bSAndrew McDonald 
89efe4208fSEric Dumazet 			if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
90efe4208fSEric Dumazet 				if (ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))
911da177e4SLinus Torvalds 					goto found;
921da177e4SLinus Torvalds 				if (is_multicast &&
931da177e4SLinus Torvalds 				    inet6_mc_check(sk, loc_addr, rmt_addr))
941da177e4SLinus Torvalds 					goto found;
951da177e4SLinus Torvalds 				continue;
961da177e4SLinus Torvalds 			}
971da177e4SLinus Torvalds 			goto found;
981da177e4SLinus Torvalds 		}
991da177e4SLinus Torvalds 	sk = NULL;
1001da177e4SLinus Torvalds found:
1011da177e4SLinus Torvalds 	return sk;
1021da177e4SLinus Torvalds }
103432490f9SCyrill Gorcunov EXPORT_SYMBOL_GPL(__raw_v6_lookup);
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds  *	0 - deliver
1071da177e4SLinus Torvalds  *	1 - block
1081da177e4SLinus Torvalds  */
1091b05c4b5SEric Dumazet static int icmpv6_filter(const struct sock *sk, const struct sk_buff *skb)
1101da177e4SLinus Torvalds {
1119cc08af3SWerner Almesberger 	struct icmp6hdr _hdr;
1121b05c4b5SEric Dumazet 	const struct icmp6hdr *hdr;
1131da177e4SLinus Torvalds 
114d1c53c8eSWerner Almesberger 	/* We require only the four bytes of the ICMPv6 header, not any
115d1c53c8eSWerner Almesberger 	 * additional bytes of message body in "struct icmp6hdr".
116d1c53c8eSWerner Almesberger 	 */
1171b05c4b5SEric Dumazet 	hdr = skb_header_pointer(skb, skb_transport_offset(skb),
118d1c53c8eSWerner Almesberger 				 ICMPV6_HDRLEN, &_hdr);
1191b05c4b5SEric Dumazet 	if (hdr) {
1201b05c4b5SEric Dumazet 		const __u32 *data = &raw6_sk(sk)->filter.data[0];
1211b05c4b5SEric Dumazet 		unsigned int type = hdr->icmp6_type;
1221da177e4SLinus Torvalds 
1231b05c4b5SEric Dumazet 		return (data[type >> 5] & (1U << (type & 31))) != 0;
1241da177e4SLinus Torvalds 	}
1251b05c4b5SEric Dumazet 	return 1;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
12807a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
129f2eda47dSEric Dumazet typedef int mh_filter_t(struct sock *sock, struct sk_buff *skb);
13059fbb3a6SMasahide NAKAMURA 
131f2eda47dSEric Dumazet static mh_filter_t __rcu *mh_filter __read_mostly;
132f2eda47dSEric Dumazet 
133f2eda47dSEric Dumazet int rawv6_mh_filter_register(mh_filter_t filter)
13459fbb3a6SMasahide NAKAMURA {
135cf778b00SEric Dumazet 	rcu_assign_pointer(mh_filter, filter);
13659fbb3a6SMasahide NAKAMURA 	return 0;
13759fbb3a6SMasahide NAKAMURA }
13859fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_register);
13959fbb3a6SMasahide NAKAMURA 
140f2eda47dSEric Dumazet int rawv6_mh_filter_unregister(mh_filter_t filter)
14159fbb3a6SMasahide NAKAMURA {
142a9b3cd7fSStephen Hemminger 	RCU_INIT_POINTER(mh_filter, NULL);
14359fbb3a6SMasahide NAKAMURA 	synchronize_rcu();
14459fbb3a6SMasahide NAKAMURA 	return 0;
14559fbb3a6SMasahide NAKAMURA }
14659fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_unregister);
14759fbb3a6SMasahide NAKAMURA 
14859fbb3a6SMasahide NAKAMURA #endif
14959fbb3a6SMasahide NAKAMURA 
1501da177e4SLinus Torvalds /*
1511da177e4SLinus Torvalds  *	demultiplex raw sockets.
1521da177e4SLinus Torvalds  *	(should consider queueing the skb in the sock receive_queue
1531da177e4SLinus Torvalds  *	without calling rawv6.c)
1541da177e4SLinus Torvalds  *
1551da177e4SLinus Torvalds  *	Caller owns SKB so we must make clones.
1561da177e4SLinus Torvalds  */
157a50feda5SEric Dumazet static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
1581da177e4SLinus Torvalds {
159b71d1d42SEric Dumazet 	const struct in6_addr *saddr;
160b71d1d42SEric Dumazet 	const struct in6_addr *daddr;
1611da177e4SLinus Torvalds 	struct sock *sk;
162a50feda5SEric Dumazet 	bool delivered = false;
1631da177e4SLinus Torvalds 	__u8 hash;
164be185884SPavel Emelyanov 	struct net *net;
1651da177e4SLinus Torvalds 
1660660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->saddr;
1671da177e4SLinus Torvalds 	daddr = saddr + 1;
1681da177e4SLinus Torvalds 
169f9242b6bSDavid S. Miller 	hash = nexthdr & (RAW_HTABLE_SIZE - 1);
1701da177e4SLinus Torvalds 
171b673e4dfSPavel Emelyanov 	read_lock(&raw_v6_hashinfo.lock);
172b673e4dfSPavel Emelyanov 	sk = sk_head(&raw_v6_hashinfo.ht[hash]);
1731da177e4SLinus Torvalds 
17463159f29SIan Morris 	if (!sk)
1751da177e4SLinus Torvalds 		goto out;
1761da177e4SLinus Torvalds 
177c346dca1SYOSHIFUJI Hideaki 	net = dev_net(skb->dev);
1785108ab4bSDavid Ahern 	sk = __raw_v6_lookup(net, sk, nexthdr, daddr, saddr,
1795108ab4bSDavid Ahern 			     inet6_iif(skb), inet6_sdif(skb));
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	while (sk) {
1827be96f76SMasahide NAKAMURA 		int filtered;
1837be96f76SMasahide NAKAMURA 
184a50feda5SEric Dumazet 		delivered = true;
1857be96f76SMasahide NAKAMURA 		switch (nexthdr) {
1867be96f76SMasahide NAKAMURA 		case IPPROTO_ICMPV6:
1877be96f76SMasahide NAKAMURA 			filtered = icmpv6_filter(sk, skb);
1887be96f76SMasahide NAKAMURA 			break;
18959fbb3a6SMasahide NAKAMURA 
19007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
1917be96f76SMasahide NAKAMURA 		case IPPROTO_MH:
19259fbb3a6SMasahide NAKAMURA 		{
1937be96f76SMasahide NAKAMURA 			/* XXX: To validate MH only once for each packet,
1947be96f76SMasahide NAKAMURA 			 * this is placed here. It should be after checking
1957be96f76SMasahide NAKAMURA 			 * xfrm policy, however it doesn't. The checking xfrm
1967be96f76SMasahide NAKAMURA 			 * policy is placed in rawv6_rcv() because it is
1977be96f76SMasahide NAKAMURA 			 * required for each socket.
1987be96f76SMasahide NAKAMURA 			 */
199f2eda47dSEric Dumazet 			mh_filter_t *filter;
20059fbb3a6SMasahide NAKAMURA 
20159fbb3a6SMasahide NAKAMURA 			filter = rcu_dereference(mh_filter);
202f2eda47dSEric Dumazet 			filtered = filter ? (*filter)(sk, skb) : 0;
2037be96f76SMasahide NAKAMURA 			break;
20459fbb3a6SMasahide NAKAMURA 		}
2057be96f76SMasahide NAKAMURA #endif
2067be96f76SMasahide NAKAMURA 		default:
2077be96f76SMasahide NAKAMURA 			filtered = 0;
2087be96f76SMasahide NAKAMURA 			break;
2097be96f76SMasahide NAKAMURA 		}
2107be96f76SMasahide NAKAMURA 
2117be96f76SMasahide NAKAMURA 		if (filtered < 0)
2127be96f76SMasahide NAKAMURA 			break;
2137be96f76SMasahide NAKAMURA 		if (filtered == 0) {
2141da177e4SLinus Torvalds 			struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 			/* Not releasing hash table! */
2179fb9cbb1SYasuyuki Kozakai 			if (clone) {
218895b5c9fSFlorian Westphal 				nf_reset_ct(clone);
2191da177e4SLinus Torvalds 				rawv6_rcv(sk, clone);
2201da177e4SLinus Torvalds 			}
2219fb9cbb1SYasuyuki Kozakai 		}
222be185884SPavel Emelyanov 		sk = __raw_v6_lookup(net, sk_next(sk), nexthdr, daddr, saddr,
2235108ab4bSDavid Ahern 				     inet6_iif(skb), inet6_sdif(skb));
2241da177e4SLinus Torvalds 	}
2251da177e4SLinus Torvalds out:
226b673e4dfSPavel Emelyanov 	read_unlock(&raw_v6_hashinfo.lock);
227d13964f4SPatrick McHardy 	return delivered;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
230a50feda5SEric Dumazet bool raw6_local_deliver(struct sk_buff *skb, int nexthdr)
23169d6da0bSPavel Emelyanov {
23269d6da0bSPavel Emelyanov 	struct sock *raw_sk;
23369d6da0bSPavel Emelyanov 
234f9242b6bSDavid S. Miller 	raw_sk = sk_head(&raw_v6_hashinfo.ht[nexthdr & (RAW_HTABLE_SIZE - 1)]);
23569d6da0bSPavel Emelyanov 	if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
23669d6da0bSPavel Emelyanov 		raw_sk = NULL;
23769d6da0bSPavel Emelyanov 
23869d6da0bSPavel Emelyanov 	return raw_sk != NULL;
23969d6da0bSPavel Emelyanov }
24069d6da0bSPavel Emelyanov 
2411da177e4SLinus Torvalds /* This cleans up af_inet6 a bit. -DaveM */
2421da177e4SLinus Torvalds static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2451da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
2461da177e4SLinus Torvalds 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
247e69a4adcSAl Viro 	__be32 v4addr = 0;
2481da177e4SLinus Torvalds 	int addr_type;
2491da177e4SLinus Torvalds 	int err;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	if (addr_len < SIN6_LEN_RFC2133)
2521da177e4SLinus Torvalds 		return -EINVAL;
25382b276cdSHannes Frederic Sowa 
25482b276cdSHannes Frederic Sowa 	if (addr->sin6_family != AF_INET6)
25582b276cdSHannes Frederic Sowa 		return -EINVAL;
25682b276cdSHannes Frederic Sowa 
2571da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&addr->sin6_addr);
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	/* Raw sockets are IPv6 only */
2601da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED)
261fd5c0027SEric Dumazet 		return -EADDRNOTAVAIL;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	lock_sock(sk);
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	err = -EINVAL;
2661da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE)
2671da177e4SLinus Torvalds 		goto out;
2681da177e4SLinus Torvalds 
269fd5c0027SEric Dumazet 	rcu_read_lock();
2701da177e4SLinus Torvalds 	/* Check if the address belongs to the host. */
2711da177e4SLinus Torvalds 	if (addr_type != IPV6_ADDR_ANY) {
2721da177e4SLinus Torvalds 		struct net_device *dev = NULL;
2731da177e4SLinus Torvalds 
274842df073SHannes Frederic Sowa 		if (__ipv6_addr_needs_scope_id(addr_type)) {
2751da177e4SLinus Torvalds 			if (addr_len >= sizeof(struct sockaddr_in6) &&
2761da177e4SLinus Torvalds 			    addr->sin6_scope_id) {
2771da177e4SLinus Torvalds 				/* Override any existing binding, if another
2781da177e4SLinus Torvalds 				 * one is supplied by user.
2791da177e4SLinus Torvalds 				 */
2801da177e4SLinus Torvalds 				sk->sk_bound_dev_if = addr->sin6_scope_id;
2811da177e4SLinus Torvalds 			}
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 			/* Binding to link-local address requires an interface */
2841da177e4SLinus Torvalds 			if (!sk->sk_bound_dev_if)
285fd5c0027SEric Dumazet 				goto out_unlock;
28672f7cfabSMike Manning 		}
2871da177e4SLinus Torvalds 
28872f7cfabSMike Manning 		if (sk->sk_bound_dev_if) {
2891da177e4SLinus Torvalds 			err = -ENODEV;
290fd5c0027SEric Dumazet 			dev = dev_get_by_index_rcu(sock_net(sk),
291fd5c0027SEric Dumazet 						   sk->sk_bound_dev_if);
292fd5c0027SEric Dumazet 			if (!dev)
293fd5c0027SEric Dumazet 				goto out_unlock;
2941da177e4SLinus Torvalds 		}
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 		/* ipv4 addr of the socket is invalid.  Only the
2971da177e4SLinus Torvalds 		 * unspecified and mapped address have a v4 equivalent.
2981da177e4SLinus Torvalds 		 */
2991da177e4SLinus Torvalds 		v4addr = LOOPBACK4_IPV6;
30035a256feSTom Herbert 		if (!(addr_type & IPV6_ADDR_MULTICAST) &&
301630e4576SMaciej Żenczykowski 		    !ipv6_can_nonlocal_bind(sock_net(sk), inet)) {
3021da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
3033b1e0a65SYOSHIFUJI Hideaki 			if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr,
304bfeade08SDaniel Lezcano 					   dev, 0)) {
305fd5c0027SEric Dumazet 				goto out_unlock;
3061da177e4SLinus Torvalds 			}
3071da177e4SLinus Torvalds 		}
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds 
310c720c7e8SEric Dumazet 	inet->inet_rcv_saddr = inet->inet_saddr = v4addr;
311efe4208fSEric Dumazet 	sk->sk_v6_rcv_saddr = addr->sin6_addr;
3121da177e4SLinus Torvalds 	if (!(addr_type & IPV6_ADDR_MULTICAST))
3134e3fd7a0SAlexey Dobriyan 		np->saddr = addr->sin6_addr;
3141da177e4SLinus Torvalds 	err = 0;
315fd5c0027SEric Dumazet out_unlock:
316fd5c0027SEric Dumazet 	rcu_read_unlock();
3171da177e4SLinus Torvalds out:
3181da177e4SLinus Torvalds 	release_sock(sk);
3191da177e4SLinus Torvalds 	return err;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
32269d6da0bSPavel Emelyanov static void rawv6_err(struct sock *sk, struct sk_buff *skb,
3231da177e4SLinus Torvalds 	       struct inet6_skb_parm *opt,
324d5fdd6baSBrian Haley 	       u8 type, u8 code, int offset, __be32 info)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
3271da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
3281da177e4SLinus Torvalds 	int err;
3291da177e4SLinus Torvalds 	int harderr;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	/* Report error on raw socket, if:
3321da177e4SLinus Torvalds 	   1. User requested recverr.
3331da177e4SLinus Torvalds 	   2. Socket is connected (otherwise the error indication
3341da177e4SLinus Torvalds 	      is useless without recverr and error is hard.
3351da177e4SLinus Torvalds 	 */
3361da177e4SLinus Torvalds 	if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
3371da177e4SLinus Torvalds 		return;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	harderr = icmpv6_err_convert(type, code, &err);
34081aded24SDavid S. Miller 	if (type == ICMPV6_PKT_TOOBIG) {
34181aded24SDavid S. Miller 		ip6_sk_update_pmtu(skb, sk, info);
3421da177e4SLinus Torvalds 		harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
34381aded24SDavid S. Miller 	}
3448d65b119SDuan Jiong 	if (type == NDISC_REDIRECT) {
345ec18d9a2SDavid S. Miller 		ip6_sk_redirect(skb, sk);
3468d65b119SDuan Jiong 		return;
3478d65b119SDuan Jiong 	}
3481da177e4SLinus Torvalds 	if (np->recverr) {
3491da177e4SLinus Torvalds 		u8 *payload = skb->data;
3501da177e4SLinus Torvalds 		if (!inet->hdrincl)
3511da177e4SLinus Torvalds 			payload += offset;
3521da177e4SLinus Torvalds 		ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	if (np->recverr || harderr) {
3561da177e4SLinus Torvalds 		sk->sk_err = err;
357e3ae2365SAlexander Aring 		sk_error_report(sk);
3581da177e4SLinus Torvalds 	}
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
36169d6da0bSPavel Emelyanov void raw6_icmp_error(struct sk_buff *skb, int nexthdr,
362d5fdd6baSBrian Haley 		u8 type, u8 code, int inner_offset, __be32 info)
36369d6da0bSPavel Emelyanov {
36469d6da0bSPavel Emelyanov 	struct sock *sk;
36569d6da0bSPavel Emelyanov 	int hash;
366b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
367be185884SPavel Emelyanov 	struct net *net;
36869d6da0bSPavel Emelyanov 
369b673e4dfSPavel Emelyanov 	hash = nexthdr & (RAW_HTABLE_SIZE - 1);
37069d6da0bSPavel Emelyanov 
371b673e4dfSPavel Emelyanov 	read_lock(&raw_v6_hashinfo.lock);
372b673e4dfSPavel Emelyanov 	sk = sk_head(&raw_v6_hashinfo.ht[hash]);
37353b24b8fSIan Morris 	if (sk) {
37405f175cdSYOSHIFUJI Hideaki 		/* Note: ipv6_hdr(skb) != skb->data */
375b71d1d42SEric Dumazet 		const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data;
37605f175cdSYOSHIFUJI Hideaki 		saddr = &ip6h->saddr;
37705f175cdSYOSHIFUJI Hideaki 		daddr = &ip6h->daddr;
378c346dca1SYOSHIFUJI Hideaki 		net = dev_net(skb->dev);
37969d6da0bSPavel Emelyanov 
380be185884SPavel Emelyanov 		while ((sk = __raw_v6_lookup(net, sk, nexthdr, saddr, daddr,
3815108ab4bSDavid Ahern 					     inet6_iif(skb), inet6_iif(skb)))) {
38269d6da0bSPavel Emelyanov 			rawv6_err(sk, skb, NULL, type, code,
38369d6da0bSPavel Emelyanov 					inner_offset, info);
38469d6da0bSPavel Emelyanov 			sk = sk_next(sk);
38569d6da0bSPavel Emelyanov 		}
38669d6da0bSPavel Emelyanov 	}
387b673e4dfSPavel Emelyanov 	read_unlock(&raw_v6_hashinfo.lock);
38869d6da0bSPavel Emelyanov }
38969d6da0bSPavel Emelyanov 
3901da177e4SLinus Torvalds static inline int rawv6_rcv_skb(struct sock *sk, struct sk_buff *skb)
3911da177e4SLinus Torvalds {
39233d480ceSEric Dumazet 	if ((raw6_sk(sk)->checksum || rcu_access_pointer(sk->sk_filter)) &&
393fb286bb2SHerbert Xu 	    skb_checksum_complete(skb)) {
394a92aa318SWang Chen 		atomic_inc(&sk->sk_drops);
3951da177e4SLinus Torvalds 		kfree_skb(skb);
3963cc76caaSYang Hongyang 		return NET_RX_DROP;
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	/* Charge it to the socket. */
400d826eb14SEric Dumazet 	skb_dst_drop(skb);
401d826eb14SEric Dumazet 	if (sock_queue_rcv_skb(sk, skb) < 0) {
4021da177e4SLinus Torvalds 		kfree_skb(skb);
4033cc76caaSYang Hongyang 		return NET_RX_DROP;
4041da177e4SLinus Torvalds 	}
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	return 0;
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds /*
4101da177e4SLinus Torvalds  *	This is next to useless...
4111da177e4SLinus Torvalds  *	if we demultiplex in network layer we don't need the extra call
4121da177e4SLinus Torvalds  *	just to queue the skb...
4131da177e4SLinus Torvalds  *	maybe we could have the network decide upon a hint if it
4141da177e4SLinus Torvalds  *	should call raw_rcv for demultiplexing
4151da177e4SLinus Torvalds  */
4161da177e4SLinus Torvalds int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4191da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
422a92aa318SWang Chen 		atomic_inc(&sk->sk_drops);
4231da177e4SLinus Torvalds 		kfree_skb(skb);
4241da177e4SLinus Torvalds 		return NET_RX_DROP;
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	if (!rp->checksum)
4281da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_UNNECESSARY;
4291da177e4SLinus Torvalds 
43084fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
431d56f90a7SArnaldo Carvalho de Melo 		skb_postpull_rcsum(skb, skb_network_header(skb),
432cfe1fc77SArnaldo Carvalho de Melo 				   skb_network_header_len(skb));
4330660e03fSArnaldo Carvalho de Melo 		if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4340660e03fSArnaldo Carvalho de Melo 				     &ipv6_hdr(skb)->daddr,
435c720c7e8SEric Dumazet 				     skb->len, inet->inet_num, skb->csum))
436fb286bb2SHerbert Xu 			skb->ip_summed = CHECKSUM_UNNECESSARY;
4371da177e4SLinus Torvalds 	}
43860476372SHerbert Xu 	if (!skb_csum_unnecessary(skb))
4390660e03fSArnaldo Carvalho de Melo 		skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4400660e03fSArnaldo Carvalho de Melo 							 &ipv6_hdr(skb)->daddr,
4410660e03fSArnaldo Carvalho de Melo 							 skb->len,
442c720c7e8SEric Dumazet 							 inet->inet_num, 0));
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	if (inet->hdrincl) {
445fb286bb2SHerbert Xu 		if (skb_checksum_complete(skb)) {
446a92aa318SWang Chen 			atomic_inc(&sk->sk_drops);
4471da177e4SLinus Torvalds 			kfree_skb(skb);
4483cc76caaSYang Hongyang 			return NET_RX_DROP;
4491da177e4SLinus Torvalds 		}
4501da177e4SLinus Torvalds 	}
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	rawv6_rcv_skb(sk, skb);
4531da177e4SLinus Torvalds 	return 0;
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds /*
4581da177e4SLinus Torvalds  *	This should be easy, if there is something there
4591da177e4SLinus Torvalds  *	we return it, otherwise we block.
4601da177e4SLinus Torvalds  */
4611da177e4SLinus Torvalds 
4621b784140SYing Xue static int rawv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
463*ec095263SOliver Hartkopp 			 int flags, int *addr_len)
4641da177e4SLinus Torvalds {
4651da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
466342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
4671da177e4SLinus Torvalds 	struct sk_buff *skb;
4681da177e4SLinus Torvalds 	size_t copied;
4691da177e4SLinus Torvalds 	int err;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	if (flags & MSG_OOB)
4721da177e4SLinus Torvalds 		return -EOPNOTSUPP;
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	if (flags & MSG_ERRQUEUE)
47585fbaa75SHannes Frederic Sowa 		return ipv6_recv_error(sk, msg, len, addr_len);
4761da177e4SLinus Torvalds 
4774b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
47885fbaa75SHannes Frederic Sowa 		return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
4794b340ae2SBrian Haley 
480f4b41f06SOliver Hartkopp 	skb = skb_recv_datagram(sk, flags, &err);
4811da177e4SLinus Torvalds 	if (!skb)
4821da177e4SLinus Torvalds 		goto out;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	copied = skb->len;
4851da177e4SLinus Torvalds 	if (copied > len) {
4861da177e4SLinus Torvalds 		copied = len;
4871da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 
49060476372SHerbert Xu 	if (skb_csum_unnecessary(skb)) {
49151f3d02bSDavid S. Miller 		err = skb_copy_datagram_msg(skb, 0, msg, copied);
4921da177e4SLinus Torvalds 	} else if (msg->msg_flags&MSG_TRUNC) {
493fb286bb2SHerbert Xu 		if (__skb_checksum_complete(skb))
4941da177e4SLinus Torvalds 			goto csum_copy_err;
49551f3d02bSDavid S. Miller 		err = skb_copy_datagram_msg(skb, 0, msg, copied);
4961da177e4SLinus Torvalds 	} else {
497227158dbSAl Viro 		err = skb_copy_and_csum_datagram_msg(skb, 0, msg);
4981da177e4SLinus Torvalds 		if (err == -EINVAL)
4991da177e4SLinus Torvalds 			goto csum_copy_err;
5001da177e4SLinus Torvalds 	}
5011da177e4SLinus Torvalds 	if (err)
5021da177e4SLinus Torvalds 		goto out_free;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	/* Copy the address. */
5051da177e4SLinus Torvalds 	if (sin6) {
5061da177e4SLinus Torvalds 		sin6->sin6_family = AF_INET6;
507f59fc7f3STetsuo Handa 		sin6->sin6_port = 0;
5084e3fd7a0SAlexey Dobriyan 		sin6->sin6_addr = ipv6_hdr(skb)->saddr;
5091da177e4SLinus Torvalds 		sin6->sin6_flowinfo = 0;
510842df073SHannes Frederic Sowa 		sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
5114330487aSDuan Jiong 							  inet6_iif(skb));
512bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin6);
5131da177e4SLinus Torvalds 	}
5141da177e4SLinus Torvalds 
5153b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	if (np->rxopt.all)
51873df66f8STom Parkin 		ip6_datagram_recv_ctl(sk, msg, skb);
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	err = copied;
5211da177e4SLinus Torvalds 	if (flags & MSG_TRUNC)
5221da177e4SLinus Torvalds 		err = skb->len;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds out_free:
5251da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
5261da177e4SLinus Torvalds out:
5271da177e4SLinus Torvalds 	return err;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds csum_copy_err:
5303305b80cSHerbert Xu 	skb_kill_datagram(sk, skb, flags);
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	/* Error for blocking case is chosen to masquerade
5331da177e4SLinus Torvalds 	   as some normal condition.
5341da177e4SLinus Torvalds 	 */
5351da177e4SLinus Torvalds 	err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
5363305b80cSHerbert Xu 	goto out;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
5394c9483b2SDavid S. Miller static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
540357b40a1SHerbert Xu 				     struct raw6_sock *rp)
5411da177e4SLinus Torvalds {
5421da177e4SLinus Torvalds 	struct sk_buff *skb;
5431da177e4SLinus Torvalds 	int err = 0;
544357b40a1SHerbert Xu 	int offset;
545357b40a1SHerbert Xu 	int len;
546679a8738SHerbert Xu 	int total_len;
547868c86bcSAl Viro 	__wsum tmp_csum;
548868c86bcSAl Viro 	__sum16 csum;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	if (!rp->checksum)
5511da177e4SLinus Torvalds 		goto send;
5521da177e4SLinus Torvalds 
55343728fa5SFabian Frederick 	skb = skb_peek(&sk->sk_write_queue);
55443728fa5SFabian Frederick 	if (!skb)
5551da177e4SLinus Torvalds 		goto out;
5561da177e4SLinus Torvalds 
557357b40a1SHerbert Xu 	offset = rp->offset;
558299b0767SSteffen Klassert 	total_len = inet_sk(sk)->cork.base.length;
559679a8738SHerbert Xu 	if (offset >= total_len - 1) {
5601da177e4SLinus Torvalds 		err = -EINVAL;
561357b40a1SHerbert Xu 		ip6_flush_pending_frames(sk);
5621da177e4SLinus Torvalds 		goto out;
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	/* should be check HW csum miyazawa */
5661da177e4SLinus Torvalds 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
5671da177e4SLinus Torvalds 		/*
5681da177e4SLinus Torvalds 		 * Only one fragment on the socket.
5691da177e4SLinus Torvalds 		 */
5701da177e4SLinus Torvalds 		tmp_csum = skb->csum;
5711da177e4SLinus Torvalds 	} else {
572357b40a1SHerbert Xu 		struct sk_buff *csum_skb = NULL;
5731da177e4SLinus Torvalds 		tmp_csum = 0;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 		skb_queue_walk(&sk->sk_write_queue, skb) {
5761da177e4SLinus Torvalds 			tmp_csum = csum_add(tmp_csum, skb->csum);
577357b40a1SHerbert Xu 
578357b40a1SHerbert Xu 			if (csum_skb)
579357b40a1SHerbert Xu 				continue;
580357b40a1SHerbert Xu 
581ea2ae17dSArnaldo Carvalho de Melo 			len = skb->len - skb_transport_offset(skb);
582357b40a1SHerbert Xu 			if (offset >= len) {
583357b40a1SHerbert Xu 				offset -= len;
584357b40a1SHerbert Xu 				continue;
5851da177e4SLinus Torvalds 			}
586357b40a1SHerbert Xu 
587357b40a1SHerbert Xu 			csum_skb = skb;
5881da177e4SLinus Torvalds 		}
5891da177e4SLinus Torvalds 
590357b40a1SHerbert Xu 		skb = csum_skb;
591357b40a1SHerbert Xu 	}
592357b40a1SHerbert Xu 
593ea2ae17dSArnaldo Carvalho de Melo 	offset += skb_transport_offset(skb);
594a98f9175SDave Jones 	err = skb_copy_bits(skb, offset, &csum, 2);
595a98f9175SDave Jones 	if (err < 0) {
596a98f9175SDave Jones 		ip6_flush_pending_frames(sk);
597a98f9175SDave Jones 		goto out;
598a98f9175SDave Jones 	}
599357b40a1SHerbert Xu 
6001da177e4SLinus Torvalds 	/* in case cksum was not initialized */
601357b40a1SHerbert Xu 	if (unlikely(csum))
6025f92a738SAl Viro 		tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
6031da177e4SLinus Torvalds 
6044c9483b2SDavid S. Miller 	csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
6054c9483b2SDavid S. Miller 			       total_len, fl6->flowi6_proto, tmp_csum);
6061da177e4SLinus Torvalds 
6074c9483b2SDavid S. Miller 	if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP)
608f6ab0288SAl Viro 		csum = CSUM_MANGLED_0;
609357b40a1SHerbert Xu 
6108242fc33SHimangi Saraogi 	BUG_ON(skb_store_bits(skb, offset, &csum, 2));
611357b40a1SHerbert Xu 
6121da177e4SLinus Torvalds send:
6131da177e4SLinus Torvalds 	err = ip6_push_pending_frames(sk);
6141da177e4SLinus Torvalds out:
6151da177e4SLinus Torvalds 	return err;
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
618c3c1a7dbSAl Viro static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length,
6194c9483b2SDavid S. Miller 			struct flowi6 *fl6, struct dst_entry **dstp,
620a818f75eSJesus Sanchez-Palencia 			unsigned int flags, const struct sockcm_cookie *sockc)
6211da177e4SLinus Torvalds {
6223320da89SHerbert Xu 	struct ipv6_pinfo *np = inet6_sk(sk);
623adb28c9dSEric W. Biederman 	struct net *net = sock_net(sk);
6241da177e4SLinus Torvalds 	struct ipv6hdr *iph;
6251da177e4SLinus Torvalds 	struct sk_buff *skb;
6261da177e4SLinus Torvalds 	int err;
6271789a640SEric Dumazet 	struct rt6_info *rt = (struct rt6_info *)*dstp;
628a7ae1992SHerbert Xu 	int hlen = LL_RESERVED_SPACE(rt->dst.dev);
629a7ae1992SHerbert Xu 	int tlen = rt->dst.dev->needed_tailroom;
6301da177e4SLinus Torvalds 
631d8d1f30bSChangli Gao 	if (length > rt->dst.dev->mtu) {
6324c9483b2SDavid S. Miller 		ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu);
6331da177e4SLinus Torvalds 		return -EMSGSIZE;
6341da177e4SLinus Torvalds 	}
63586f4c90aSAlexander Potapenko 	if (length < sizeof(struct ipv6hdr))
63686f4c90aSAlexander Potapenko 		return -EINVAL;
6371da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
6381da177e4SLinus Torvalds 		goto out;
6391da177e4SLinus Torvalds 
640f5184d26SJohannes Berg 	skb = sock_alloc_send_skb(sk,
641a7ae1992SHerbert Xu 				  length + hlen + tlen + 15,
6421da177e4SLinus Torvalds 				  flags & MSG_DONTWAIT, &err);
64363159f29SIan Morris 	if (!skb)
6441da177e4SLinus Torvalds 		goto error;
645a7ae1992SHerbert Xu 	skb_reserve(skb, hlen);
6461da177e4SLinus Torvalds 
6479c9c9ad5SHannes Frederic Sowa 	skb->protocol = htons(ETH_P_IPV6);
6481da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
649c6af0c22SWillem de Bruijn 	skb->mark = sockc->mark;
650a818f75eSJesus Sanchez-Palencia 	skb->tstamp = sockc->transmit_time;
6511da177e4SLinus Torvalds 
6521ced98e8SArnaldo Carvalho de Melo 	skb_put(skb, length);
6531ced98e8SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
6540660e03fSArnaldo Carvalho de Melo 	iph = ipv6_hdr(skb);
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	skb->ip_summed = CHECKSUM_NONE;
6571da177e4SLinus Torvalds 
6588f932f76SWillem de Bruijn 	skb_setup_tx_timestamp(skb, sockc->tsflags);
659fbfb2321SWillem de Bruijn 
6600dec879fSJulian Anastasov 	if (flags & MSG_CONFIRM)
6610dec879fSJulian Anastasov 		skb_set_dst_pending_confirm(skb, 1);
6620dec879fSJulian Anastasov 
663b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
66421226abbSAl Viro 	err = memcpy_from_msg(iph, msg, length);
665a688caa3SWei Wang 	if (err) {
666a688caa3SWei Wang 		err = -EFAULT;
667a688caa3SWei Wang 		kfree_skb(skb);
668a688caa3SWei Wang 		goto error;
669a688caa3SWei Wang 	}
670a688caa3SWei Wang 
671a688caa3SWei Wang 	skb_dst_set(skb, &rt->dst);
672a688caa3SWei Wang 	*dstp = NULL;
6731da177e4SLinus Torvalds 
674a8e3e1a9SDavid Ahern 	/* if egress device is enslaved to an L3 master device pass the
675a8e3e1a9SDavid Ahern 	 * skb to its handler for processing
676a8e3e1a9SDavid Ahern 	 */
677a8e3e1a9SDavid Ahern 	skb = l3mdev_ip6_out(sk, skb);
678a8e3e1a9SDavid Ahern 	if (unlikely(!skb))
679a8e3e1a9SDavid Ahern 		return 0;
680a8e3e1a9SDavid Ahern 
681a688caa3SWei Wang 	/* Acquire rcu_read_lock() in case we need to use rt->rt6i_idev
682a688caa3SWei Wang 	 * in the error path. Since skb has been freed, the dst could
683a688caa3SWei Wang 	 * have been queued for deletion.
684a688caa3SWei Wang 	 */
685a688caa3SWei Wang 	rcu_read_lock();
686adb28c9dSEric W. Biederman 	IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
68729a26a56SEric W. Biederman 	err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb,
68813206b6bSEric W. Biederman 		      NULL, rt->dst.dev, dst_output);
6891da177e4SLinus Torvalds 	if (err > 0)
6906ce9e7b5SEric Dumazet 		err = net_xmit_errno(err);
691a688caa3SWei Wang 	if (err) {
692a688caa3SWei Wang 		IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
693a688caa3SWei Wang 		rcu_read_unlock();
694a688caa3SWei Wang 		goto error_check;
695a688caa3SWei Wang 	}
696a688caa3SWei Wang 	rcu_read_unlock();
6971da177e4SLinus Torvalds out:
6981da177e4SLinus Torvalds 	return 0;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds error:
701adb28c9dSEric W. Biederman 	IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
702a688caa3SWei Wang error_check:
7036ce9e7b5SEric Dumazet 	if (err == -ENOBUFS && !np->recverr)
7046ce9e7b5SEric Dumazet 		err = 0;
7051da177e4SLinus Torvalds 	return err;
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
70819e3c66bSAl Viro struct raw6_frag_vec {
70919e3c66bSAl Viro 	struct msghdr *msg;
71019e3c66bSAl Viro 	int hlen;
71119e3c66bSAl Viro 	char c[4];
71219e3c66bSAl Viro };
71319e3c66bSAl Viro 
71419e3c66bSAl Viro static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6)
7151da177e4SLinus Torvalds {
71619e3c66bSAl Viro 	int err = 0;
7174c9483b2SDavid S. Miller 	switch (fl6->flowi6_proto) {
7181da177e4SLinus Torvalds 	case IPPROTO_ICMPV6:
71919e3c66bSAl Viro 		rfv->hlen = 2;
72019e3c66bSAl Viro 		err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
72119e3c66bSAl Viro 		if (!err) {
72219e3c66bSAl Viro 			fl6->fl6_icmp_type = rfv->c[0];
72319e3c66bSAl Viro 			fl6->fl6_icmp_code = rfv->c[1];
7241da177e4SLinus Torvalds 		}
7251da177e4SLinus Torvalds 		break;
7266e8f4d48SMasahide NAKAMURA 	case IPPROTO_MH:
72719e3c66bSAl Viro 		rfv->hlen = 4;
72819e3c66bSAl Viro 		err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
72919e3c66bSAl Viro 		if (!err)
73019e3c66bSAl Viro 			fl6->fl6_mh_type = rfv->c[2];
73119e3c66bSAl Viro 	}
73219e3c66bSAl Viro 	return err;
73319e3c66bSAl Viro }
7346e8f4d48SMasahide NAKAMURA 
73519e3c66bSAl Viro static int raw6_getfrag(void *from, char *to, int offset, int len, int odd,
73619e3c66bSAl Viro 		       struct sk_buff *skb)
73719e3c66bSAl Viro {
73819e3c66bSAl Viro 	struct raw6_frag_vec *rfv = from;
73919e3c66bSAl Viro 
74019e3c66bSAl Viro 	if (offset < rfv->hlen) {
74119e3c66bSAl Viro 		int copy = min(rfv->hlen - offset, len);
74219e3c66bSAl Viro 
74319e3c66bSAl Viro 		if (skb->ip_summed == CHECKSUM_PARTIAL)
74419e3c66bSAl Viro 			memcpy(to, rfv->c + offset, copy);
74519e3c66bSAl Viro 		else
74619e3c66bSAl Viro 			skb->csum = csum_block_add(
74719e3c66bSAl Viro 				skb->csum,
74819e3c66bSAl Viro 				csum_partial_copy_nocheck(rfv->c + offset,
749cc44c17bSAl Viro 							  to, copy),
75019e3c66bSAl Viro 				odd);
75119e3c66bSAl Viro 
75219e3c66bSAl Viro 		odd = 0;
75319e3c66bSAl Viro 		offset += copy;
75419e3c66bSAl Viro 		to += copy;
75519e3c66bSAl Viro 		len -= copy;
75619e3c66bSAl Viro 
75719e3c66bSAl Viro 		if (!len)
758a27b58feSHeiko Carstens 			return 0;
7591da177e4SLinus Torvalds 	}
7601da177e4SLinus Torvalds 
76119e3c66bSAl Viro 	offset -= rfv->hlen;
76219e3c66bSAl Viro 
763f69e6d13SAl Viro 	return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
76419e3c66bSAl Viro }
76519e3c66bSAl Viro 
7661b784140SYing Xue static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
7671da177e4SLinus Torvalds {
76845f6fad8SEric Dumazet 	struct ipv6_txoptions *opt_to_free = NULL;
7691da177e4SLinus Torvalds 	struct ipv6_txoptions opt_space;
770342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
77120c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
7721da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
7731da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
7741da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
7751da177e4SLinus Torvalds 	struct ipv6_txoptions *opt = NULL;
7761da177e4SLinus Torvalds 	struct ip6_flowlabel *flowlabel = NULL;
7771da177e4SLinus Torvalds 	struct dst_entry *dst = NULL;
77819e3c66bSAl Viro 	struct raw6_frag_vec rfv;
7794c9483b2SDavid S. Miller 	struct flowi6 fl6;
78026879da5SWei Wang 	struct ipcm6_cookie ipc6;
7811da177e4SLinus Torvalds 	int addr_len = msg->msg_namelen;
78259e3e4b5SOlivier Matz 	int hdrincl;
7831da177e4SLinus Torvalds 	u16 proto;
7841da177e4SLinus Torvalds 	int err;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	/* Rough check on arithmetic overflow,
787b59e139bSYOSHIFUJI Hideaki 	   better check is made in ip6_append_data().
7881da177e4SLinus Torvalds 	 */
789b59e139bSYOSHIFUJI Hideaki 	if (len > INT_MAX)
7901da177e4SLinus Torvalds 		return -EMSGSIZE;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	/* Mirror BSD error message compatibility */
7931da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
7941da177e4SLinus Torvalds 		return -EOPNOTSUPP;
7951da177e4SLinus Torvalds 
79659e3e4b5SOlivier Matz 	/* hdrincl should be READ_ONCE(inet->hdrincl)
79759e3e4b5SOlivier Matz 	 * but READ_ONCE() doesn't work with bit fields.
79859e3e4b5SOlivier Matz 	 * Doing this indirectly yields the same result.
79959e3e4b5SOlivier Matz 	 */
80059e3e4b5SOlivier Matz 	hdrincl = inet->hdrincl;
80159e3e4b5SOlivier Matz 	hdrincl = READ_ONCE(hdrincl);
80259e3e4b5SOlivier Matz 
8031da177e4SLinus Torvalds 	/*
8041da177e4SLinus Torvalds 	 *	Get and verify the address.
8051da177e4SLinus Torvalds 	 */
8064c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
8071da177e4SLinus Torvalds 
8084c9483b2SDavid S. Miller 	fl6.flowi6_mark = sk->sk_mark;
809e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sk->sk_uid;
8104a19ec58SLaszlo Attila Toth 
811b515430aSWillem de Bruijn 	ipcm6_init(&ipc6);
8125fdaa88dSWillem de Bruijn 	ipc6.sockc.tsflags = sk->sk_tsflags;
813c6af0c22SWillem de Bruijn 	ipc6.sockc.mark = sk->sk_mark;
81426879da5SWei Wang 
8151da177e4SLinus Torvalds 	if (sin6) {
8161da177e4SLinus Torvalds 		if (addr_len < SIN6_LEN_RFC2133)
8171da177e4SLinus Torvalds 			return -EINVAL;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 		if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
820a02cec21SEric Dumazet 			return -EAFNOSUPPORT;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 		/* port is the proto value [0..255] carried in nexthdr */
8231da177e4SLinus Torvalds 		proto = ntohs(sin6->sin6_port);
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 		if (!proto)
826c720c7e8SEric Dumazet 			proto = inet->inet_num;
827c720c7e8SEric Dumazet 		else if (proto != inet->inet_num)
828a02cec21SEric Dumazet 			return -EINVAL;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 		if (proto > 255)
831a02cec21SEric Dumazet 			return -EINVAL;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 		daddr = &sin6->sin6_addr;
8341da177e4SLinus Torvalds 		if (np->sndflow) {
8354c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
8364c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
8374c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
83859c820b2SWillem de Bruijn 				if (IS_ERR(flowlabel))
8391da177e4SLinus Torvalds 					return -EINVAL;
8401da177e4SLinus Torvalds 			}
8411da177e4SLinus Torvalds 		}
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 		/*
8441da177e4SLinus Torvalds 		 * Otherwise it will be difficult to maintain
8451da177e4SLinus Torvalds 		 * sk->sk_dst_cache.
8461da177e4SLinus Torvalds 		 */
8471da177e4SLinus Torvalds 		if (sk->sk_state == TCP_ESTABLISHED &&
848efe4208fSEric Dumazet 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
849efe4208fSEric Dumazet 			daddr = &sk->sk_v6_daddr;
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 		if (addr_len >= sizeof(struct sockaddr_in6) &&
8521da177e4SLinus Torvalds 		    sin6->sin6_scope_id &&
853842df073SHannes Frederic Sowa 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
8544c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
8551da177e4SLinus Torvalds 	} else {
8561da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED)
8571da177e4SLinus Torvalds 			return -EDESTADDRREQ;
8581da177e4SLinus Torvalds 
859c720c7e8SEric Dumazet 		proto = inet->inet_num;
860efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
8614c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
8621da177e4SLinus Torvalds 	}
8631da177e4SLinus Torvalds 
8644c9483b2SDavid S. Miller 	if (fl6.flowi6_oif == 0)
8654c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	if (msg->msg_controllen) {
8681da177e4SLinus Torvalds 		opt = &opt_space;
8691da177e4SLinus Torvalds 		memset(opt, 0, sizeof(struct ipv6_txoptions));
8701da177e4SLinus Torvalds 		opt->tot_len = sizeof(struct ipv6_txoptions);
87126879da5SWei Wang 		ipc6.opt = opt;
8721da177e4SLinus Torvalds 
8735fdaa88dSWillem de Bruijn 		err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6);
8741da177e4SLinus Torvalds 		if (err < 0) {
8751da177e4SLinus Torvalds 			fl6_sock_release(flowlabel);
8761da177e4SLinus Torvalds 			return err;
8771da177e4SLinus Torvalds 		}
8784c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
8794c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
88059c820b2SWillem de Bruijn 			if (IS_ERR(flowlabel))
8811da177e4SLinus Torvalds 				return -EINVAL;
8821da177e4SLinus Torvalds 		}
8831da177e4SLinus Torvalds 		if (!(opt->opt_nflen|opt->opt_flen))
8841da177e4SLinus Torvalds 			opt = NULL;
8851da177e4SLinus Torvalds 	}
88645f6fad8SEric Dumazet 	if (!opt) {
88745f6fad8SEric Dumazet 		opt = txopt_get(np);
88845f6fad8SEric Dumazet 		opt_to_free = opt;
88945f6fad8SEric Dumazet 	}
890df9890c3SYOSHIFUJI Hideaki 	if (flowlabel)
8911da177e4SLinus Torvalds 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
892df9890c3SYOSHIFUJI Hideaki 	opt = ipv6_fixup_options(&opt_space, opt);
8931da177e4SLinus Torvalds 
8944c9483b2SDavid S. Miller 	fl6.flowi6_proto = proto;
895c6af0c22SWillem de Bruijn 	fl6.flowi6_mark = ipc6.sockc.mark;
896b9aa52c4SOlivier Matz 
897b9aa52c4SOlivier Matz 	if (!hdrincl) {
89819e3c66bSAl Viro 		rfv.msg = msg;
89919e3c66bSAl Viro 		rfv.hlen = 0;
90019e3c66bSAl Viro 		err = rawv6_probe_proto_opt(&rfv, &fl6);
901a27b58feSHeiko Carstens 		if (err)
902a27b58feSHeiko Carstens 			goto out;
903b9aa52c4SOlivier Matz 	}
9041da177e4SLinus Torvalds 
905876c7f41SBrian Haley 	if (!ipv6_addr_any(daddr))
9064e3fd7a0SAlexey Dobriyan 		fl6.daddr = *daddr;
907876c7f41SBrian Haley 	else
9084c9483b2SDavid S. Miller 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
9094c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
9104e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
9111da177e4SLinus Torvalds 
9124c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
9131da177e4SLinus Torvalds 
9144c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
9154c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
916c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
917c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
9183df98d79SPaul Moore 	security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
9191da177e4SLinus Torvalds 
92059e3e4b5SOlivier Matz 	if (hdrincl)
92148e8aa6eSMartin KaFai Lau 		fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH;
92248e8aa6eSMartin KaFai Lau 
92338b7097bSHannes Frederic Sowa 	if (ipc6.tclass < 0)
92438b7097bSHannes Frederic Sowa 		ipc6.tclass = np->tclass;
92538b7097bSHannes Frederic Sowa 
92638b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
92738b7097bSHannes Frederic Sowa 
928c4e85f73SSabrina Dubroca 	dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
92968d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
93068d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
9311da177e4SLinus Torvalds 		goto out;
93214e50e57SDavid S. Miller 	}
93326879da5SWei Wang 	if (ipc6.hlimit < 0)
93426879da5SWei Wang 		ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
9351da177e4SLinus Torvalds 
93626879da5SWei Wang 	if (ipc6.dontfrag < 0)
93726879da5SWei Wang 		ipc6.dontfrag = np->dontfrag;
93813b52cd4SBrian Haley 
9391da177e4SLinus Torvalds 	if (msg->msg_flags&MSG_CONFIRM)
9401da177e4SLinus Torvalds 		goto do_confirm;
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds back_from_confirm:
94359e3e4b5SOlivier Matz 	if (hdrincl)
944a818f75eSJesus Sanchez-Palencia 		err = rawv6_send_hdrinc(sk, msg, len, &fl6, &dst,
9455fdaa88dSWillem de Bruijn 					msg->msg_flags, &ipc6.sockc);
9461789a640SEric Dumazet 	else {
94726879da5SWei Wang 		ipc6.opt = opt;
9481da177e4SLinus Torvalds 		lock_sock(sk);
94919e3c66bSAl Viro 		err = ip6_append_data(sk, raw6_getfrag, &rfv,
95026879da5SWei Wang 			len, 0, &ipc6, &fl6, (struct rt6_info *)dst,
9515fdaa88dSWillem de Bruijn 			msg->msg_flags);
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 		if (err)
9541da177e4SLinus Torvalds 			ip6_flush_pending_frames(sk);
9551da177e4SLinus Torvalds 		else if (!(msg->msg_flags & MSG_MORE))
9564c9483b2SDavid S. Miller 			err = rawv6_push_pending_frames(sk, &fl6, rp);
9573ef9d943SYOSHIFUJI Hideaki 		release_sock(sk);
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds done:
9606d3e85ecSNicolas DICHTEL 	dst_release(dst);
9611da177e4SLinus Torvalds out:
9621da177e4SLinus Torvalds 	fl6_sock_release(flowlabel);
96345f6fad8SEric Dumazet 	txopt_put(opt_to_free);
9641da177e4SLinus Torvalds 	return err < 0 ? err : len;
9651da177e4SLinus Torvalds do_confirm:
9660dec879fSJulian Anastasov 	if (msg->msg_flags & MSG_PROBE)
9670dec879fSJulian Anastasov 		dst_confirm_neigh(dst, &fl6.daddr);
9681da177e4SLinus Torvalds 	if (!(msg->msg_flags & MSG_PROBE) || len)
9691da177e4SLinus Torvalds 		goto back_from_confirm;
9701da177e4SLinus Torvalds 	err = 0;
9711da177e4SLinus Torvalds 	goto done;
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
975a7b75c5aSChristoph Hellwig 			       sockptr_t optval, int optlen)
9761da177e4SLinus Torvalds {
9771da177e4SLinus Torvalds 	switch (optname) {
9781da177e4SLinus Torvalds 	case ICMPV6_FILTER:
9791da177e4SLinus Torvalds 		if (optlen > sizeof(struct icmp6_filter))
9801da177e4SLinus Torvalds 			optlen = sizeof(struct icmp6_filter);
981a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&raw6_sk(sk)->filter, optval, optlen))
9821da177e4SLinus Torvalds 			return -EFAULT;
9831da177e4SLinus Torvalds 		return 0;
9841da177e4SLinus Torvalds 	default:
9851da177e4SLinus Torvalds 		return -ENOPROTOOPT;
9863ff50b79SStephen Hemminger 	}
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	return 0;
9891da177e4SLinus Torvalds }
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
9921da177e4SLinus Torvalds 			       char __user *optval, int __user *optlen)
9931da177e4SLinus Torvalds {
9941da177e4SLinus Torvalds 	int len;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	switch (optname) {
9971da177e4SLinus Torvalds 	case ICMPV6_FILTER:
9981da177e4SLinus Torvalds 		if (get_user(len, optlen))
9991da177e4SLinus Torvalds 			return -EFAULT;
10001da177e4SLinus Torvalds 		if (len < 0)
10011da177e4SLinus Torvalds 			return -EINVAL;
10021da177e4SLinus Torvalds 		if (len > sizeof(struct icmp6_filter))
10031da177e4SLinus Torvalds 			len = sizeof(struct icmp6_filter);
10041da177e4SLinus Torvalds 		if (put_user(len, optlen))
10051da177e4SLinus Torvalds 			return -EFAULT;
10061da177e4SLinus Torvalds 		if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
10071da177e4SLinus Torvalds 			return -EFAULT;
10081da177e4SLinus Torvalds 		return 0;
10091da177e4SLinus Torvalds 	default:
10101da177e4SLinus Torvalds 		return -ENOPROTOOPT;
10113ff50b79SStephen Hemminger 	}
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 	return 0;
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 
10173fdadf7dSDmitry Mishin static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
1018a7b75c5aSChristoph Hellwig 			       sockptr_t optval, unsigned int optlen)
10191da177e4SLinus Torvalds {
10201da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
10211da177e4SLinus Torvalds 	int val;
10221da177e4SLinus Torvalds 
1023fb7bc920STamir Duberstein 	if (optlen < sizeof(val))
1024fb7bc920STamir Duberstein 		return -EINVAL;
1025fb7bc920STamir Duberstein 
1026a7b75c5aSChristoph Hellwig 	if (copy_from_sockptr(&val, optval, sizeof(val)))
10271da177e4SLinus Torvalds 		return -EFAULT;
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	switch (optname) {
1030715f504bSHannes Frederic Sowa 	case IPV6_HDRINCL:
1031715f504bSHannes Frederic Sowa 		if (sk->sk_type != SOCK_RAW)
1032715f504bSHannes Frederic Sowa 			return -EINVAL;
1033715f504bSHannes Frederic Sowa 		inet_sk(sk)->hdrincl = !!val;
1034715f504bSHannes Frederic Sowa 		return 0;
10351da177e4SLinus Torvalds 	case IPV6_CHECKSUM:
1036c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 &&
10371a98d05fSYOSHIFUJI Hideaki 		    level == IPPROTO_IPV6) {
10381a98d05fSYOSHIFUJI Hideaki 			/*
10391a98d05fSYOSHIFUJI Hideaki 			 * RFC3542 tells that IPV6_CHECKSUM socket
10401a98d05fSYOSHIFUJI Hideaki 			 * option in the IPPROTO_IPV6 level is not
10411a98d05fSYOSHIFUJI Hideaki 			 * allowed on ICMPv6 sockets.
10421a98d05fSYOSHIFUJI Hideaki 			 * If you want to set it, use IPPROTO_RAW
10431a98d05fSYOSHIFUJI Hideaki 			 * level IPV6_CHECKSUM socket option
10441a98d05fSYOSHIFUJI Hideaki 			 * (Linux extension).
10451a98d05fSYOSHIFUJI Hideaki 			 */
10461a98d05fSYOSHIFUJI Hideaki 			return -EINVAL;
10471a98d05fSYOSHIFUJI Hideaki 		}
10481a98d05fSYOSHIFUJI Hideaki 
10491da177e4SLinus Torvalds 		/* You may get strange result with a positive odd offset;
10501da177e4SLinus Torvalds 		   RFC2292bis agrees with me. */
10511da177e4SLinus Torvalds 		if (val > 0 && (val&1))
1052a02cec21SEric Dumazet 			return -EINVAL;
10531da177e4SLinus Torvalds 		if (val < 0) {
10541da177e4SLinus Torvalds 			rp->checksum = 0;
10551da177e4SLinus Torvalds 		} else {
10561da177e4SLinus Torvalds 			rp->checksum = 1;
10571da177e4SLinus Torvalds 			rp->offset = val;
10581da177e4SLinus Torvalds 		}
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 		return 0;
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	default:
1063a02cec21SEric Dumazet 		return -ENOPROTOOPT;
10641da177e4SLinus Torvalds 	}
10651da177e4SLinus Torvalds }
10661da177e4SLinus Torvalds 
10673fdadf7dSDmitry Mishin static int rawv6_setsockopt(struct sock *sk, int level, int optname,
1068a7b75c5aSChristoph Hellwig 			    sockptr_t optval, unsigned int optlen)
10691da177e4SLinus Torvalds {
10701da177e4SLinus Torvalds 	switch (level) {
10711da177e4SLinus Torvalds 	case SOL_RAW:
10721da177e4SLinus Torvalds 		break;
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	case SOL_ICMPV6:
1075c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
10761da177e4SLinus Torvalds 			return -EOPNOTSUPP;
1077207ec0abSJoe Perches 		return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
10781da177e4SLinus Torvalds 	case SOL_IPV6:
1079715f504bSHannes Frederic Sowa 		if (optname == IPV6_CHECKSUM ||
1080715f504bSHannes Frederic Sowa 		    optname == IPV6_HDRINCL)
10811da177e4SLinus Torvalds 			break;
1082a8eceea8SJoe Perches 		fallthrough;
10831da177e4SLinus Torvalds 	default:
1084207ec0abSJoe Perches 		return ipv6_setsockopt(sk, level, optname, optval, optlen);
10853ff50b79SStephen Hemminger 	}
10863ff50b79SStephen Hemminger 
10873fdadf7dSDmitry Mishin 	return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
10883fdadf7dSDmitry Mishin }
10893fdadf7dSDmitry Mishin 
10903fdadf7dSDmitry Mishin static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
10913fdadf7dSDmitry Mishin 			    char __user *optval, int __user *optlen)
10923fdadf7dSDmitry Mishin {
10933fdadf7dSDmitry Mishin 	struct raw6_sock *rp = raw6_sk(sk);
10943fdadf7dSDmitry Mishin 	int val, len;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	if (get_user(len, optlen))
10971da177e4SLinus Torvalds 		return -EFAULT;
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	switch (optname) {
1100715f504bSHannes Frederic Sowa 	case IPV6_HDRINCL:
1101715f504bSHannes Frederic Sowa 		val = inet_sk(sk)->hdrincl;
1102715f504bSHannes Frederic Sowa 		break;
11031da177e4SLinus Torvalds 	case IPV6_CHECKSUM:
11041a98d05fSYOSHIFUJI Hideaki 		/*
11051a98d05fSYOSHIFUJI Hideaki 		 * We allow getsockopt() for IPPROTO_IPV6-level
11061a98d05fSYOSHIFUJI Hideaki 		 * IPV6_CHECKSUM socket option on ICMPv6 sockets
11071a98d05fSYOSHIFUJI Hideaki 		 * since RFC3542 is silent about it.
11081a98d05fSYOSHIFUJI Hideaki 		 */
11091da177e4SLinus Torvalds 		if (rp->checksum == 0)
11101da177e4SLinus Torvalds 			val = -1;
11111da177e4SLinus Torvalds 		else
11121da177e4SLinus Torvalds 			val = rp->offset;
11131da177e4SLinus Torvalds 		break;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	default:
11161da177e4SLinus Torvalds 		return -ENOPROTOOPT;
11171da177e4SLinus Torvalds 	}
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	len = min_t(unsigned int, sizeof(int), len);
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	if (put_user(len, optlen))
11221da177e4SLinus Torvalds 		return -EFAULT;
11231da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, len))
11241da177e4SLinus Torvalds 		return -EFAULT;
11251da177e4SLinus Torvalds 	return 0;
11261da177e4SLinus Torvalds }
11271da177e4SLinus Torvalds 
11283fdadf7dSDmitry Mishin static int rawv6_getsockopt(struct sock *sk, int level, int optname,
11293fdadf7dSDmitry Mishin 			  char __user *optval, int __user *optlen)
11303fdadf7dSDmitry Mishin {
11313fdadf7dSDmitry Mishin 	switch (level) {
11323fdadf7dSDmitry Mishin 	case SOL_RAW:
11333fdadf7dSDmitry Mishin 		break;
11343fdadf7dSDmitry Mishin 
11353fdadf7dSDmitry Mishin 	case SOL_ICMPV6:
1136c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
11373fdadf7dSDmitry Mishin 			return -EOPNOTSUPP;
1138207ec0abSJoe Perches 		return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
11393fdadf7dSDmitry Mishin 	case SOL_IPV6:
1140715f504bSHannes Frederic Sowa 		if (optname == IPV6_CHECKSUM ||
1141715f504bSHannes Frederic Sowa 		    optname == IPV6_HDRINCL)
11423fdadf7dSDmitry Mishin 			break;
1143a8eceea8SJoe Perches 		fallthrough;
11443fdadf7dSDmitry Mishin 	default:
1145207ec0abSJoe Perches 		return ipv6_getsockopt(sk, level, optname, optval, optlen);
11463ff50b79SStephen Hemminger 	}
11473ff50b79SStephen Hemminger 
11483fdadf7dSDmitry Mishin 	return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
11493fdadf7dSDmitry Mishin }
11503fdadf7dSDmitry Mishin 
11511da177e4SLinus Torvalds static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
11521da177e4SLinus Torvalds {
11531da177e4SLinus Torvalds 	switch (cmd) {
1154207ec0abSJoe Perches 	case SIOCOUTQ: {
115531e6d363SEric Dumazet 		int amount = sk_wmem_alloc_get(sk);
115631e6d363SEric Dumazet 
11571da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
11581da177e4SLinus Torvalds 	}
1159207ec0abSJoe Perches 	case SIOCINQ: {
11601da177e4SLinus Torvalds 		struct sk_buff *skb;
11611da177e4SLinus Torvalds 		int amount = 0;
11621da177e4SLinus Torvalds 
1163e0f9f858SHerbert Xu 		spin_lock_bh(&sk->sk_receive_queue.lock);
11641da177e4SLinus Torvalds 		skb = skb_peek(&sk->sk_receive_queue);
116553b24b8fSIan Morris 		if (skb)
1166105f5528SJamie Bainbridge 			amount = skb->len;
1167e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_receive_queue.lock);
11681da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
11691da177e4SLinus Torvalds 	}
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	default:
11727bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
11737bc570c8SYOSHIFUJI Hideaki 		return ip6mr_ioctl(sk, cmd, (void __user *)arg);
11747bc570c8SYOSHIFUJI Hideaki #else
11751da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
11767bc570c8SYOSHIFUJI Hideaki #endif
11771da177e4SLinus Torvalds 	}
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds 
1180e2d57766SDavid S. Miller #ifdef CONFIG_COMPAT
1181e2d57766SDavid S. Miller static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
1182e2d57766SDavid S. Miller {
1183e2d57766SDavid S. Miller 	switch (cmd) {
1184e2d57766SDavid S. Miller 	case SIOCOUTQ:
1185e2d57766SDavid S. Miller 	case SIOCINQ:
1186e2d57766SDavid S. Miller 		return -ENOIOCTLCMD;
1187e2d57766SDavid S. Miller 	default:
1188e2d57766SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
1189e2d57766SDavid S. Miller 		return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg));
1190e2d57766SDavid S. Miller #else
1191e2d57766SDavid S. Miller 		return -ENOIOCTLCMD;
1192e2d57766SDavid S. Miller #endif
1193e2d57766SDavid S. Miller 	}
1194e2d57766SDavid S. Miller }
1195e2d57766SDavid S. Miller #endif
1196e2d57766SDavid S. Miller 
11971da177e4SLinus Torvalds static void rawv6_close(struct sock *sk, long timeout)
11981da177e4SLinus Torvalds {
1199c720c7e8SEric Dumazet 	if (inet_sk(sk)->inet_num == IPPROTO_RAW)
1200725a8ff0SDenis V. Lunev 		ip6_ra_control(sk, -1);
12017bc570c8SYOSHIFUJI Hideaki 	ip6mr_sk_done(sk);
12021da177e4SLinus Torvalds 	sk_common_release(sk);
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds 
12057d06b2e0SBrian Haley static void raw6_destroy(struct sock *sk)
120622dd4850SDenis V. Lunev {
120722dd4850SDenis V. Lunev 	lock_sock(sk);
120822dd4850SDenis V. Lunev 	ip6_flush_pending_frames(sk);
120922dd4850SDenis V. Lunev 	release_sock(sk);
1210f23d60deSDavid S. Miller 
12117d06b2e0SBrian Haley 	inet6_destroy_sock(sk);
121222dd4850SDenis V. Lunev }
121322dd4850SDenis V. Lunev 
12141da177e4SLinus Torvalds static int rawv6_init_sk(struct sock *sk)
12151da177e4SLinus Torvalds {
12161da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
1217f48d5ff1SMasahide NAKAMURA 
1218c720c7e8SEric Dumazet 	switch (inet_sk(sk)->inet_num) {
1219f48d5ff1SMasahide NAKAMURA 	case IPPROTO_ICMPV6:
12201da177e4SLinus Torvalds 		rp->checksum = 1;
12211da177e4SLinus Torvalds 		rp->offset   = 2;
1222f48d5ff1SMasahide NAKAMURA 		break;
1223f48d5ff1SMasahide NAKAMURA 	case IPPROTO_MH:
1224f48d5ff1SMasahide NAKAMURA 		rp->checksum = 1;
1225f48d5ff1SMasahide NAKAMURA 		rp->offset   = 4;
1226f48d5ff1SMasahide NAKAMURA 		break;
1227f48d5ff1SMasahide NAKAMURA 	default:
1228f48d5ff1SMasahide NAKAMURA 		break;
12291da177e4SLinus Torvalds 	}
1230a02cec21SEric Dumazet 	return 0;
12311da177e4SLinus Torvalds }
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds struct proto rawv6_prot = {
12341da177e4SLinus Torvalds 	.name		   = "RAWv6",
12351da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
12361da177e4SLinus Torvalds 	.close		   = rawv6_close,
123722dd4850SDenis V. Lunev 	.destroy	   = raw6_destroy,
123882b276cdSHannes Frederic Sowa 	.connect	   = ip6_datagram_connect_v6_only,
1239286c72deSEric Dumazet 	.disconnect	   = __udp_disconnect,
12401da177e4SLinus Torvalds 	.ioctl		   = rawv6_ioctl,
12411da177e4SLinus Torvalds 	.init		   = rawv6_init_sk,
12421da177e4SLinus Torvalds 	.setsockopt	   = rawv6_setsockopt,
12431da177e4SLinus Torvalds 	.getsockopt	   = rawv6_getsockopt,
12441da177e4SLinus Torvalds 	.sendmsg	   = rawv6_sendmsg,
12451da177e4SLinus Torvalds 	.recvmsg	   = rawv6_recvmsg,
12461da177e4SLinus Torvalds 	.bind		   = rawv6_bind,
12471da177e4SLinus Torvalds 	.backlog_rcv	   = rawv6_rcv_skb,
1248fc8717baSPavel Emelyanov 	.hash		   = raw_hash_sk,
1249fc8717baSPavel Emelyanov 	.unhash		   = raw_unhash_sk,
12501da177e4SLinus Torvalds 	.obj_size	   = sizeof(struct raw6_sock),
12518c2bc895SDavid Windsor 	.useroffset	   = offsetof(struct raw6_sock, filter),
12528c2bc895SDavid Windsor 	.usersize	   = sizeof_field(struct raw6_sock, filter),
1253fc8717baSPavel Emelyanov 	.h.raw_hash	   = &raw_v6_hashinfo,
1254543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
1255e2d57766SDavid S. Miller 	.compat_ioctl	   = compat_rawv6_ioctl,
1256543d9cfeSArnaldo Carvalho de Melo #endif
1257432490f9SCyrill Gorcunov 	.diag_destroy	   = raw_abort,
12581da177e4SLinus Torvalds };
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12611da177e4SLinus Torvalds static int raw6_seq_show(struct seq_file *seq, void *v)
12621da177e4SLinus Torvalds {
126317ef66afSLorenzo Colitti 	if (v == SEQ_START_TOKEN) {
126417ef66afSLorenzo Colitti 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
126517ef66afSLorenzo Colitti 	} else {
126617ef66afSLorenzo Colitti 		struct sock *sp = v;
126717ef66afSLorenzo Colitti 		__u16 srcp  = inet_sk(sp)->inet_num;
126817ef66afSLorenzo Colitti 		ip6_dgram_sock_seq_show(seq, v, srcp, 0,
126917ef66afSLorenzo Colitti 					raw_seq_private(seq)->bucket);
127017ef66afSLorenzo Colitti 	}
12711da177e4SLinus Torvalds 	return 0;
12721da177e4SLinus Torvalds }
12731da177e4SLinus Torvalds 
127456b3d975SPhilippe De Muyter static const struct seq_operations raw6_seq_ops = {
127542a73808SPavel Emelyanov 	.start =	raw_seq_start,
127642a73808SPavel Emelyanov 	.next =		raw_seq_next,
127742a73808SPavel Emelyanov 	.stop =		raw_seq_stop,
12781da177e4SLinus Torvalds 	.show =		raw6_seq_show,
12791da177e4SLinus Torvalds };
12801da177e4SLinus Torvalds 
12812c8c1e72SAlexey Dobriyan static int __net_init raw6_init_net(struct net *net)
1282a308da16SPavel Emelyanov {
1283c3506372SChristoph Hellwig 	if (!proc_create_net_data("raw6", 0444, net->proc_net, &raw6_seq_ops,
1284c3506372SChristoph Hellwig 			sizeof(struct raw_iter_state), &raw_v6_hashinfo))
1285a308da16SPavel Emelyanov 		return -ENOMEM;
1286a308da16SPavel Emelyanov 
1287a308da16SPavel Emelyanov 	return 0;
1288a308da16SPavel Emelyanov }
1289a308da16SPavel Emelyanov 
12902c8c1e72SAlexey Dobriyan static void __net_exit raw6_exit_net(struct net *net)
1291a308da16SPavel Emelyanov {
1292ece31ffdSGao feng 	remove_proc_entry("raw6", net->proc_net);
1293a308da16SPavel Emelyanov }
1294a308da16SPavel Emelyanov 
1295a308da16SPavel Emelyanov static struct pernet_operations raw6_net_ops = {
1296a308da16SPavel Emelyanov 	.init = raw6_init_net,
1297a308da16SPavel Emelyanov 	.exit = raw6_exit_net,
1298a308da16SPavel Emelyanov };
1299a308da16SPavel Emelyanov 
13001da177e4SLinus Torvalds int __init raw6_proc_init(void)
13011da177e4SLinus Torvalds {
1302a308da16SPavel Emelyanov 	return register_pernet_subsys(&raw6_net_ops);
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds void raw6_proc_exit(void)
13061da177e4SLinus Torvalds {
1307a308da16SPavel Emelyanov 	unregister_pernet_subsys(&raw6_net_ops);
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
13107f4e4868SDaniel Lezcano 
1311a11e1d43SLinus Torvalds /* Same as inet6_dgram_ops, sans udp_poll.  */
131277d4b1d3SEric Dumazet const struct proto_ops inet6_sockraw_ops = {
13137f4e4868SDaniel Lezcano 	.family		   = PF_INET6,
13147f4e4868SDaniel Lezcano 	.owner		   = THIS_MODULE,
13157f4e4868SDaniel Lezcano 	.release	   = inet6_release,
13167f4e4868SDaniel Lezcano 	.bind		   = inet6_bind,
13177f4e4868SDaniel Lezcano 	.connect	   = inet_dgram_connect,	/* ok		*/
13187f4e4868SDaniel Lezcano 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
13197f4e4868SDaniel Lezcano 	.accept		   = sock_no_accept,		/* a do nothing	*/
13207f4e4868SDaniel Lezcano 	.getname	   = inet6_getname,
1321a11e1d43SLinus Torvalds 	.poll		   = datagram_poll,		/* ok		*/
13227f4e4868SDaniel Lezcano 	.ioctl		   = inet6_ioctl,		/* must change  */
1323c7cbdbf2SArnd Bergmann 	.gettstamp	   = sock_gettstamp,
13247f4e4868SDaniel Lezcano 	.listen		   = sock_no_listen,		/* ok		*/
13257f4e4868SDaniel Lezcano 	.shutdown	   = inet_shutdown,		/* ok		*/
13267f4e4868SDaniel Lezcano 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
13277f4e4868SDaniel Lezcano 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
13287f4e4868SDaniel Lezcano 	.sendmsg	   = inet_sendmsg,		/* ok		*/
13297f4e4868SDaniel Lezcano 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
13307f4e4868SDaniel Lezcano 	.mmap		   = sock_no_mmap,
13317f4e4868SDaniel Lezcano 	.sendpage	   = sock_no_sendpage,
13327f4e4868SDaniel Lezcano #ifdef CONFIG_COMPAT
13333986912fSChristoph Hellwig 	.compat_ioctl	   = inet6_compat_ioctl,
13347f4e4868SDaniel Lezcano #endif
13357f4e4868SDaniel Lezcano };
13367f4e4868SDaniel Lezcano 
13377f4e4868SDaniel Lezcano static struct inet_protosw rawv6_protosw = {
13387f4e4868SDaniel Lezcano 	.type		= SOCK_RAW,
13397f4e4868SDaniel Lezcano 	.protocol	= IPPROTO_IP,	/* wild card */
13407f4e4868SDaniel Lezcano 	.prot		= &rawv6_prot,
13417f4e4868SDaniel Lezcano 	.ops		= &inet6_sockraw_ops,
13427f4e4868SDaniel Lezcano 	.flags		= INET_PROTOSW_REUSE,
13437f4e4868SDaniel Lezcano };
13447f4e4868SDaniel Lezcano 
13457f4e4868SDaniel Lezcano int __init rawv6_init(void)
13467f4e4868SDaniel Lezcano {
13473d2f6d41SJulia Lawall 	return inet6_register_protosw(&rawv6_protosw);
13487f4e4868SDaniel Lezcano }
13497f4e4868SDaniel Lezcano 
135009f7709fSDaniel Lezcano void rawv6_exit(void)
13517f4e4868SDaniel Lezcano {
13527f4e4868SDaniel Lezcano 	inet6_unregister_protosw(&rawv6_protosw);
13537f4e4868SDaniel Lezcano }
1354