xref: /openbmc/linux/net/ipv6/raw.c (revision f289c02b)
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 
640daf07e5SEric Dumazet struct raw_hashinfo raw_v6_hashinfo;
65432490f9SCyrill Gorcunov EXPORT_SYMBOL_GPL(raw_v6_hashinfo);
661da177e4SLinus Torvalds 
67ba44f818SEric Dumazet bool raw_v6_match(struct net *net, struct sock *sk, unsigned short num,
68ba44f818SEric Dumazet 		  const struct in6_addr *loc_addr,
695108ab4bSDavid Ahern 		  const struct in6_addr *rmt_addr, int dif, int sdif)
701da177e4SLinus Torvalds {
71ba44f818SEric Dumazet 	if (inet_sk(sk)->inet_num != num ||
72ba44f818SEric Dumazet 	    !net_eq(sock_net(sk), net) ||
73ba44f818SEric Dumazet 	    (!ipv6_addr_any(&sk->sk_v6_daddr) &&
74ba44f818SEric Dumazet 	     !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
75ba44f818SEric Dumazet 	    !raw_sk_bound_dev_eq(net, sk->sk_bound_dev_if,
767055420fSDuncan Eastoe 				 dif, sdif))
77ba44f818SEric Dumazet 		return false;
780bd1b59bSAndrew McDonald 
79ba44f818SEric Dumazet 	if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
80ba44f818SEric Dumazet 	    ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr) ||
81ba44f818SEric Dumazet 	    (ipv6_addr_is_multicast(loc_addr) &&
82ba44f818SEric Dumazet 	     inet6_mc_check(sk, loc_addr, rmt_addr)))
83ba44f818SEric Dumazet 		return true;
84ba44f818SEric Dumazet 
85ba44f818SEric Dumazet 	return false;
861da177e4SLinus Torvalds }
87ba44f818SEric Dumazet EXPORT_SYMBOL_GPL(raw_v6_match);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds /*
901da177e4SLinus Torvalds  *	0 - deliver
911da177e4SLinus Torvalds  *	1 - block
921da177e4SLinus Torvalds  */
931b05c4b5SEric Dumazet static int icmpv6_filter(const struct sock *sk, const struct sk_buff *skb)
941da177e4SLinus Torvalds {
959cc08af3SWerner Almesberger 	struct icmp6hdr _hdr;
961b05c4b5SEric Dumazet 	const struct icmp6hdr *hdr;
971da177e4SLinus Torvalds 
98d1c53c8eSWerner Almesberger 	/* We require only the four bytes of the ICMPv6 header, not any
99d1c53c8eSWerner Almesberger 	 * additional bytes of message body in "struct icmp6hdr".
100d1c53c8eSWerner Almesberger 	 */
1011b05c4b5SEric Dumazet 	hdr = skb_header_pointer(skb, skb_transport_offset(skb),
102d1c53c8eSWerner Almesberger 				 ICMPV6_HDRLEN, &_hdr);
1031b05c4b5SEric Dumazet 	if (hdr) {
1041b05c4b5SEric Dumazet 		const __u32 *data = &raw6_sk(sk)->filter.data[0];
1051b05c4b5SEric Dumazet 		unsigned int type = hdr->icmp6_type;
1061da177e4SLinus Torvalds 
1071b05c4b5SEric Dumazet 		return (data[type >> 5] & (1U << (type & 31))) != 0;
1081da177e4SLinus Torvalds 	}
1091b05c4b5SEric Dumazet 	return 1;
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
11207a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
113f2eda47dSEric Dumazet typedef int mh_filter_t(struct sock *sock, struct sk_buff *skb);
11459fbb3a6SMasahide NAKAMURA 
115f2eda47dSEric Dumazet static mh_filter_t __rcu *mh_filter __read_mostly;
116f2eda47dSEric Dumazet 
117f2eda47dSEric Dumazet int rawv6_mh_filter_register(mh_filter_t filter)
11859fbb3a6SMasahide NAKAMURA {
119cf778b00SEric Dumazet 	rcu_assign_pointer(mh_filter, filter);
12059fbb3a6SMasahide NAKAMURA 	return 0;
12159fbb3a6SMasahide NAKAMURA }
12259fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_register);
12359fbb3a6SMasahide NAKAMURA 
124f2eda47dSEric Dumazet int rawv6_mh_filter_unregister(mh_filter_t filter)
12559fbb3a6SMasahide NAKAMURA {
126a9b3cd7fSStephen Hemminger 	RCU_INIT_POINTER(mh_filter, NULL);
12759fbb3a6SMasahide NAKAMURA 	synchronize_rcu();
12859fbb3a6SMasahide NAKAMURA 	return 0;
12959fbb3a6SMasahide NAKAMURA }
13059fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_unregister);
13159fbb3a6SMasahide NAKAMURA 
13259fbb3a6SMasahide NAKAMURA #endif
13359fbb3a6SMasahide NAKAMURA 
1341da177e4SLinus Torvalds /*
1351da177e4SLinus Torvalds  *	demultiplex raw sockets.
1361da177e4SLinus Torvalds  *	(should consider queueing the skb in the sock receive_queue
1371da177e4SLinus Torvalds  *	without calling rawv6.c)
1381da177e4SLinus Torvalds  *
1391da177e4SLinus Torvalds  *	Caller owns SKB so we must make clones.
1401da177e4SLinus Torvalds  */
141a50feda5SEric Dumazet static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
1421da177e4SLinus Torvalds {
143ba44f818SEric Dumazet 	struct net *net = dev_net(skb->dev);
1440daf07e5SEric Dumazet 	struct hlist_nulls_head *hlist;
1450daf07e5SEric Dumazet 	struct hlist_nulls_node *hnode;
146b71d1d42SEric Dumazet 	const struct in6_addr *saddr;
147b71d1d42SEric Dumazet 	const struct in6_addr *daddr;
1481da177e4SLinus Torvalds 	struct sock *sk;
149a50feda5SEric Dumazet 	bool delivered = false;
1501da177e4SLinus Torvalds 	__u8 hash;
1511da177e4SLinus Torvalds 
1520660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->saddr;
1531da177e4SLinus Torvalds 	daddr = saddr + 1;
1541da177e4SLinus Torvalds 
155f9242b6bSDavid S. Miller 	hash = nexthdr & (RAW_HTABLE_SIZE - 1);
1560daf07e5SEric Dumazet 	hlist = &raw_v6_hashinfo.ht[hash];
1570daf07e5SEric Dumazet 	rcu_read_lock();
158*f289c02bSKuniyuki Iwashima 	sk_nulls_for_each(sk, hnode, hlist) {
1597be96f76SMasahide NAKAMURA 		int filtered;
1607be96f76SMasahide NAKAMURA 
161ba44f818SEric Dumazet 		if (!raw_v6_match(net, sk, nexthdr, daddr, saddr,
162ba44f818SEric Dumazet 				  inet6_iif(skb), inet6_sdif(skb)))
163ba44f818SEric Dumazet 			continue;
164a50feda5SEric Dumazet 		delivered = true;
1657be96f76SMasahide NAKAMURA 		switch (nexthdr) {
1667be96f76SMasahide NAKAMURA 		case IPPROTO_ICMPV6:
1677be96f76SMasahide NAKAMURA 			filtered = icmpv6_filter(sk, skb);
1687be96f76SMasahide NAKAMURA 			break;
16959fbb3a6SMasahide NAKAMURA 
17007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
1717be96f76SMasahide NAKAMURA 		case IPPROTO_MH:
17259fbb3a6SMasahide NAKAMURA 		{
1737be96f76SMasahide NAKAMURA 			/* XXX: To validate MH only once for each packet,
1747be96f76SMasahide NAKAMURA 			 * this is placed here. It should be after checking
1757be96f76SMasahide NAKAMURA 			 * xfrm policy, however it doesn't. The checking xfrm
1767be96f76SMasahide NAKAMURA 			 * policy is placed in rawv6_rcv() because it is
1777be96f76SMasahide NAKAMURA 			 * required for each socket.
1787be96f76SMasahide NAKAMURA 			 */
179f2eda47dSEric Dumazet 			mh_filter_t *filter;
18059fbb3a6SMasahide NAKAMURA 
18159fbb3a6SMasahide NAKAMURA 			filter = rcu_dereference(mh_filter);
182f2eda47dSEric Dumazet 			filtered = filter ? (*filter)(sk, skb) : 0;
1837be96f76SMasahide NAKAMURA 			break;
18459fbb3a6SMasahide NAKAMURA 		}
1857be96f76SMasahide NAKAMURA #endif
1867be96f76SMasahide NAKAMURA 		default:
1877be96f76SMasahide NAKAMURA 			filtered = 0;
1887be96f76SMasahide NAKAMURA 			break;
1897be96f76SMasahide NAKAMURA 		}
1907be96f76SMasahide NAKAMURA 
1917be96f76SMasahide NAKAMURA 		if (filtered < 0)
1927be96f76SMasahide NAKAMURA 			break;
1937be96f76SMasahide NAKAMURA 		if (filtered == 0) {
1941da177e4SLinus Torvalds 			struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 			/* Not releasing hash table! */
1979fb9cbb1SYasuyuki Kozakai 			if (clone) {
198895b5c9fSFlorian Westphal 				nf_reset_ct(clone);
1991da177e4SLinus Torvalds 				rawv6_rcv(sk, clone);
2001da177e4SLinus Torvalds 			}
2019fb9cbb1SYasuyuki Kozakai 		}
2021da177e4SLinus Torvalds 	}
2030daf07e5SEric Dumazet 	rcu_read_unlock();
204d13964f4SPatrick McHardy 	return delivered;
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
207a50feda5SEric Dumazet bool raw6_local_deliver(struct sk_buff *skb, int nexthdr)
20869d6da0bSPavel Emelyanov {
209ba44f818SEric Dumazet 	return ipv6_raw_deliver(skb, nexthdr);
21069d6da0bSPavel Emelyanov }
21169d6da0bSPavel Emelyanov 
2121da177e4SLinus Torvalds /* This cleans up af_inet6 a bit. -DaveM */
2131da177e4SLinus Torvalds static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2161da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
2171da177e4SLinus Torvalds 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
218e69a4adcSAl Viro 	__be32 v4addr = 0;
2191da177e4SLinus Torvalds 	int addr_type;
2201da177e4SLinus Torvalds 	int err;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	if (addr_len < SIN6_LEN_RFC2133)
2231da177e4SLinus Torvalds 		return -EINVAL;
22482b276cdSHannes Frederic Sowa 
22582b276cdSHannes Frederic Sowa 	if (addr->sin6_family != AF_INET6)
22682b276cdSHannes Frederic Sowa 		return -EINVAL;
22782b276cdSHannes Frederic Sowa 
2281da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&addr->sin6_addr);
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	/* Raw sockets are IPv6 only */
2311da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED)
232fd5c0027SEric Dumazet 		return -EADDRNOTAVAIL;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	lock_sock(sk);
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	err = -EINVAL;
2371da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE)
2381da177e4SLinus Torvalds 		goto out;
2391da177e4SLinus Torvalds 
240fd5c0027SEric Dumazet 	rcu_read_lock();
2411da177e4SLinus Torvalds 	/* Check if the address belongs to the host. */
2421da177e4SLinus Torvalds 	if (addr_type != IPV6_ADDR_ANY) {
2431da177e4SLinus Torvalds 		struct net_device *dev = NULL;
2441da177e4SLinus Torvalds 
245842df073SHannes Frederic Sowa 		if (__ipv6_addr_needs_scope_id(addr_type)) {
2461da177e4SLinus Torvalds 			if (addr_len >= sizeof(struct sockaddr_in6) &&
2471da177e4SLinus Torvalds 			    addr->sin6_scope_id) {
2481da177e4SLinus Torvalds 				/* Override any existing binding, if another
2491da177e4SLinus Torvalds 				 * one is supplied by user.
2501da177e4SLinus Torvalds 				 */
2511da177e4SLinus Torvalds 				sk->sk_bound_dev_if = addr->sin6_scope_id;
2521da177e4SLinus Torvalds 			}
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 			/* Binding to link-local address requires an interface */
2551da177e4SLinus Torvalds 			if (!sk->sk_bound_dev_if)
256fd5c0027SEric Dumazet 				goto out_unlock;
25772f7cfabSMike Manning 		}
2581da177e4SLinus Torvalds 
25972f7cfabSMike Manning 		if (sk->sk_bound_dev_if) {
2601da177e4SLinus Torvalds 			err = -ENODEV;
261fd5c0027SEric Dumazet 			dev = dev_get_by_index_rcu(sock_net(sk),
262fd5c0027SEric Dumazet 						   sk->sk_bound_dev_if);
263fd5c0027SEric Dumazet 			if (!dev)
264fd5c0027SEric Dumazet 				goto out_unlock;
2651da177e4SLinus Torvalds 		}
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 		/* ipv4 addr of the socket is invalid.  Only the
2681da177e4SLinus Torvalds 		 * unspecified and mapped address have a v4 equivalent.
2691da177e4SLinus Torvalds 		 */
2701da177e4SLinus Torvalds 		v4addr = LOOPBACK4_IPV6;
27135a256feSTom Herbert 		if (!(addr_type & IPV6_ADDR_MULTICAST) &&
272630e4576SMaciej Żenczykowski 		    !ipv6_can_nonlocal_bind(sock_net(sk), inet)) {
2731da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
2743b1e0a65SYOSHIFUJI Hideaki 			if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr,
275bfeade08SDaniel Lezcano 					   dev, 0)) {
276fd5c0027SEric Dumazet 				goto out_unlock;
2771da177e4SLinus Torvalds 			}
2781da177e4SLinus Torvalds 		}
2791da177e4SLinus Torvalds 	}
2801da177e4SLinus Torvalds 
281c720c7e8SEric Dumazet 	inet->inet_rcv_saddr = inet->inet_saddr = v4addr;
282efe4208fSEric Dumazet 	sk->sk_v6_rcv_saddr = addr->sin6_addr;
2831da177e4SLinus Torvalds 	if (!(addr_type & IPV6_ADDR_MULTICAST))
2844e3fd7a0SAlexey Dobriyan 		np->saddr = addr->sin6_addr;
2851da177e4SLinus Torvalds 	err = 0;
286fd5c0027SEric Dumazet out_unlock:
287fd5c0027SEric Dumazet 	rcu_read_unlock();
2881da177e4SLinus Torvalds out:
2891da177e4SLinus Torvalds 	release_sock(sk);
2901da177e4SLinus Torvalds 	return err;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
29369d6da0bSPavel Emelyanov static void rawv6_err(struct sock *sk, struct sk_buff *skb,
2941da177e4SLinus Torvalds 	       struct inet6_skb_parm *opt,
295d5fdd6baSBrian Haley 	       u8 type, u8 code, int offset, __be32 info)
2961da177e4SLinus Torvalds {
2971da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2981da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
2991da177e4SLinus Torvalds 	int err;
3001da177e4SLinus Torvalds 	int harderr;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	/* Report error on raw socket, if:
3031da177e4SLinus Torvalds 	   1. User requested recverr.
3041da177e4SLinus Torvalds 	   2. Socket is connected (otherwise the error indication
3051da177e4SLinus Torvalds 	      is useless without recverr and error is hard.
3061da177e4SLinus Torvalds 	 */
3071da177e4SLinus Torvalds 	if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
3081da177e4SLinus Torvalds 		return;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	harderr = icmpv6_err_convert(type, code, &err);
31181aded24SDavid S. Miller 	if (type == ICMPV6_PKT_TOOBIG) {
31281aded24SDavid S. Miller 		ip6_sk_update_pmtu(skb, sk, info);
3131da177e4SLinus Torvalds 		harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
31481aded24SDavid S. Miller 	}
3158d65b119SDuan Jiong 	if (type == NDISC_REDIRECT) {
316ec18d9a2SDavid S. Miller 		ip6_sk_redirect(skb, sk);
3178d65b119SDuan Jiong 		return;
3188d65b119SDuan Jiong 	}
3191da177e4SLinus Torvalds 	if (np->recverr) {
3201da177e4SLinus Torvalds 		u8 *payload = skb->data;
3211da177e4SLinus Torvalds 		if (!inet->hdrincl)
3221da177e4SLinus Torvalds 			payload += offset;
3231da177e4SLinus Torvalds 		ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
3241da177e4SLinus Torvalds 	}
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	if (np->recverr || harderr) {
3271da177e4SLinus Torvalds 		sk->sk_err = err;
328e3ae2365SAlexander Aring 		sk_error_report(sk);
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
33269d6da0bSPavel Emelyanov void raw6_icmp_error(struct sk_buff *skb, int nexthdr,
333d5fdd6baSBrian Haley 		u8 type, u8 code, int inner_offset, __be32 info)
33469d6da0bSPavel Emelyanov {
335ba44f818SEric Dumazet 	const struct in6_addr *saddr, *daddr;
336ba44f818SEric Dumazet 	struct net *net = dev_net(skb->dev);
3370daf07e5SEric Dumazet 	struct hlist_nulls_head *hlist;
3380daf07e5SEric Dumazet 	struct hlist_nulls_node *hnode;
33969d6da0bSPavel Emelyanov 	struct sock *sk;
34069d6da0bSPavel Emelyanov 	int hash;
34169d6da0bSPavel Emelyanov 
342b673e4dfSPavel Emelyanov 	hash = nexthdr & (RAW_HTABLE_SIZE - 1);
3430daf07e5SEric Dumazet 	hlist = &raw_v6_hashinfo.ht[hash];
3440daf07e5SEric Dumazet 	rcu_read_lock();
345*f289c02bSKuniyuki Iwashima 	sk_nulls_for_each(sk, hnode, hlist) {
34605f175cdSYOSHIFUJI Hideaki 		/* Note: ipv6_hdr(skb) != skb->data */
347b71d1d42SEric Dumazet 		const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data;
34805f175cdSYOSHIFUJI Hideaki 		saddr = &ip6h->saddr;
34905f175cdSYOSHIFUJI Hideaki 		daddr = &ip6h->daddr;
35069d6da0bSPavel Emelyanov 
351ba44f818SEric Dumazet 		if (!raw_v6_match(net, sk, nexthdr, &ip6h->saddr, &ip6h->daddr,
352ba44f818SEric Dumazet 				  inet6_iif(skb), inet6_iif(skb)))
353ba44f818SEric Dumazet 			continue;
354ba44f818SEric Dumazet 		rawv6_err(sk, skb, NULL, type, code, inner_offset, info);
35569d6da0bSPavel Emelyanov 	}
3560daf07e5SEric Dumazet 	rcu_read_unlock();
35769d6da0bSPavel Emelyanov }
35869d6da0bSPavel Emelyanov 
3591da177e4SLinus Torvalds static inline int rawv6_rcv_skb(struct sock *sk, struct sk_buff *skb)
3601da177e4SLinus Torvalds {
36133d480ceSEric Dumazet 	if ((raw6_sk(sk)->checksum || rcu_access_pointer(sk->sk_filter)) &&
362fb286bb2SHerbert Xu 	    skb_checksum_complete(skb)) {
363a92aa318SWang Chen 		atomic_inc(&sk->sk_drops);
3641da177e4SLinus Torvalds 		kfree_skb(skb);
3653cc76caaSYang Hongyang 		return NET_RX_DROP;
3661da177e4SLinus Torvalds 	}
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	/* Charge it to the socket. */
369d826eb14SEric Dumazet 	skb_dst_drop(skb);
370d826eb14SEric Dumazet 	if (sock_queue_rcv_skb(sk, skb) < 0) {
3711da177e4SLinus Torvalds 		kfree_skb(skb);
3723cc76caaSYang Hongyang 		return NET_RX_DROP;
3731da177e4SLinus Torvalds 	}
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	return 0;
3761da177e4SLinus Torvalds }
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds /*
3791da177e4SLinus Torvalds  *	This is next to useless...
3801da177e4SLinus Torvalds  *	if we demultiplex in network layer we don't need the extra call
3811da177e4SLinus Torvalds  *	just to queue the skb...
3821da177e4SLinus Torvalds  *	maybe we could have the network decide upon a hint if it
3831da177e4SLinus Torvalds  *	should call raw_rcv for demultiplexing
3841da177e4SLinus Torvalds  */
3851da177e4SLinus Torvalds int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
3861da177e4SLinus Torvalds {
3871da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
3881da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
391a92aa318SWang Chen 		atomic_inc(&sk->sk_drops);
3921da177e4SLinus Torvalds 		kfree_skb(skb);
3931da177e4SLinus Torvalds 		return NET_RX_DROP;
3941da177e4SLinus Torvalds 	}
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	if (!rp->checksum)
3971da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_UNNECESSARY;
3981da177e4SLinus Torvalds 
39984fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
400d56f90a7SArnaldo Carvalho de Melo 		skb_postpull_rcsum(skb, skb_network_header(skb),
401cfe1fc77SArnaldo Carvalho de Melo 				   skb_network_header_len(skb));
4020660e03fSArnaldo Carvalho de Melo 		if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4030660e03fSArnaldo Carvalho de Melo 				     &ipv6_hdr(skb)->daddr,
404c720c7e8SEric Dumazet 				     skb->len, inet->inet_num, skb->csum))
405fb286bb2SHerbert Xu 			skb->ip_summed = CHECKSUM_UNNECESSARY;
4061da177e4SLinus Torvalds 	}
40760476372SHerbert Xu 	if (!skb_csum_unnecessary(skb))
4080660e03fSArnaldo Carvalho de Melo 		skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4090660e03fSArnaldo Carvalho de Melo 							 &ipv6_hdr(skb)->daddr,
4100660e03fSArnaldo Carvalho de Melo 							 skb->len,
411c720c7e8SEric Dumazet 							 inet->inet_num, 0));
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	if (inet->hdrincl) {
414fb286bb2SHerbert Xu 		if (skb_checksum_complete(skb)) {
415a92aa318SWang Chen 			atomic_inc(&sk->sk_drops);
4161da177e4SLinus Torvalds 			kfree_skb(skb);
4173cc76caaSYang Hongyang 			return NET_RX_DROP;
4181da177e4SLinus Torvalds 		}
4191da177e4SLinus Torvalds 	}
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	rawv6_rcv_skb(sk, skb);
4221da177e4SLinus Torvalds 	return 0;
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds /*
4271da177e4SLinus Torvalds  *	This should be easy, if there is something there
4281da177e4SLinus Torvalds  *	we return it, otherwise we block.
4291da177e4SLinus Torvalds  */
4301da177e4SLinus Torvalds 
4311b784140SYing Xue static int rawv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
432ec095263SOliver Hartkopp 			 int flags, int *addr_len)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
435342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
4361da177e4SLinus Torvalds 	struct sk_buff *skb;
4371da177e4SLinus Torvalds 	size_t copied;
4381da177e4SLinus Torvalds 	int err;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	if (flags & MSG_OOB)
4411da177e4SLinus Torvalds 		return -EOPNOTSUPP;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	if (flags & MSG_ERRQUEUE)
44485fbaa75SHannes Frederic Sowa 		return ipv6_recv_error(sk, msg, len, addr_len);
4451da177e4SLinus Torvalds 
4464b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
44785fbaa75SHannes Frederic Sowa 		return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
4484b340ae2SBrian Haley 
449f4b41f06SOliver Hartkopp 	skb = skb_recv_datagram(sk, flags, &err);
4501da177e4SLinus Torvalds 	if (!skb)
4511da177e4SLinus Torvalds 		goto out;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	copied = skb->len;
4541da177e4SLinus Torvalds 	if (copied > len) {
4551da177e4SLinus Torvalds 		copied = len;
4561da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
4571da177e4SLinus Torvalds 	}
4581da177e4SLinus Torvalds 
45960476372SHerbert Xu 	if (skb_csum_unnecessary(skb)) {
46051f3d02bSDavid S. Miller 		err = skb_copy_datagram_msg(skb, 0, msg, copied);
4611da177e4SLinus Torvalds 	} else if (msg->msg_flags&MSG_TRUNC) {
462fb286bb2SHerbert Xu 		if (__skb_checksum_complete(skb))
4631da177e4SLinus Torvalds 			goto csum_copy_err;
46451f3d02bSDavid S. Miller 		err = skb_copy_datagram_msg(skb, 0, msg, copied);
4651da177e4SLinus Torvalds 	} else {
466227158dbSAl Viro 		err = skb_copy_and_csum_datagram_msg(skb, 0, msg);
4671da177e4SLinus Torvalds 		if (err == -EINVAL)
4681da177e4SLinus Torvalds 			goto csum_copy_err;
4691da177e4SLinus Torvalds 	}
4701da177e4SLinus Torvalds 	if (err)
4711da177e4SLinus Torvalds 		goto out_free;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	/* Copy the address. */
4741da177e4SLinus Torvalds 	if (sin6) {
4751da177e4SLinus Torvalds 		sin6->sin6_family = AF_INET6;
476f59fc7f3STetsuo Handa 		sin6->sin6_port = 0;
4774e3fd7a0SAlexey Dobriyan 		sin6->sin6_addr = ipv6_hdr(skb)->saddr;
4781da177e4SLinus Torvalds 		sin6->sin6_flowinfo = 0;
479842df073SHannes Frederic Sowa 		sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
4804330487aSDuan Jiong 							  inet6_iif(skb));
481bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin6);
4821da177e4SLinus Torvalds 	}
4831da177e4SLinus Torvalds 
4846fd1d51cSErin MacNeil 	sock_recv_cmsgs(msg, sk, skb);
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	if (np->rxopt.all)
48773df66f8STom Parkin 		ip6_datagram_recv_ctl(sk, msg, skb);
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	err = copied;
4901da177e4SLinus Torvalds 	if (flags & MSG_TRUNC)
4911da177e4SLinus Torvalds 		err = skb->len;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds out_free:
4941da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
4951da177e4SLinus Torvalds out:
4961da177e4SLinus Torvalds 	return err;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds csum_copy_err:
4993305b80cSHerbert Xu 	skb_kill_datagram(sk, skb, flags);
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	/* Error for blocking case is chosen to masquerade
5021da177e4SLinus Torvalds 	   as some normal condition.
5031da177e4SLinus Torvalds 	 */
5041da177e4SLinus Torvalds 	err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
5053305b80cSHerbert Xu 	goto out;
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
5084c9483b2SDavid S. Miller static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
509357b40a1SHerbert Xu 				     struct raw6_sock *rp)
5101da177e4SLinus Torvalds {
5111da177e4SLinus Torvalds 	struct sk_buff *skb;
5121da177e4SLinus Torvalds 	int err = 0;
513357b40a1SHerbert Xu 	int offset;
514357b40a1SHerbert Xu 	int len;
515679a8738SHerbert Xu 	int total_len;
516868c86bcSAl Viro 	__wsum tmp_csum;
517868c86bcSAl Viro 	__sum16 csum;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	if (!rp->checksum)
5201da177e4SLinus Torvalds 		goto send;
5211da177e4SLinus Torvalds 
52243728fa5SFabian Frederick 	skb = skb_peek(&sk->sk_write_queue);
52343728fa5SFabian Frederick 	if (!skb)
5241da177e4SLinus Torvalds 		goto out;
5251da177e4SLinus Torvalds 
526357b40a1SHerbert Xu 	offset = rp->offset;
527299b0767SSteffen Klassert 	total_len = inet_sk(sk)->cork.base.length;
528679a8738SHerbert Xu 	if (offset >= total_len - 1) {
5291da177e4SLinus Torvalds 		err = -EINVAL;
530357b40a1SHerbert Xu 		ip6_flush_pending_frames(sk);
5311da177e4SLinus Torvalds 		goto out;
5321da177e4SLinus Torvalds 	}
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	/* should be check HW csum miyazawa */
5351da177e4SLinus Torvalds 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
5361da177e4SLinus Torvalds 		/*
5371da177e4SLinus Torvalds 		 * Only one fragment on the socket.
5381da177e4SLinus Torvalds 		 */
5391da177e4SLinus Torvalds 		tmp_csum = skb->csum;
5401da177e4SLinus Torvalds 	} else {
541357b40a1SHerbert Xu 		struct sk_buff *csum_skb = NULL;
5421da177e4SLinus Torvalds 		tmp_csum = 0;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 		skb_queue_walk(&sk->sk_write_queue, skb) {
5451da177e4SLinus Torvalds 			tmp_csum = csum_add(tmp_csum, skb->csum);
546357b40a1SHerbert Xu 
547357b40a1SHerbert Xu 			if (csum_skb)
548357b40a1SHerbert Xu 				continue;
549357b40a1SHerbert Xu 
550ea2ae17dSArnaldo Carvalho de Melo 			len = skb->len - skb_transport_offset(skb);
551357b40a1SHerbert Xu 			if (offset >= len) {
552357b40a1SHerbert Xu 				offset -= len;
553357b40a1SHerbert Xu 				continue;
5541da177e4SLinus Torvalds 			}
555357b40a1SHerbert Xu 
556357b40a1SHerbert Xu 			csum_skb = skb;
5571da177e4SLinus Torvalds 		}
5581da177e4SLinus Torvalds 
559357b40a1SHerbert Xu 		skb = csum_skb;
560357b40a1SHerbert Xu 	}
561357b40a1SHerbert Xu 
562ea2ae17dSArnaldo Carvalho de Melo 	offset += skb_transport_offset(skb);
563a98f9175SDave Jones 	err = skb_copy_bits(skb, offset, &csum, 2);
564a98f9175SDave Jones 	if (err < 0) {
565a98f9175SDave Jones 		ip6_flush_pending_frames(sk);
566a98f9175SDave Jones 		goto out;
567a98f9175SDave Jones 	}
568357b40a1SHerbert Xu 
5691da177e4SLinus Torvalds 	/* in case cksum was not initialized */
570357b40a1SHerbert Xu 	if (unlikely(csum))
5715f92a738SAl Viro 		tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
5721da177e4SLinus Torvalds 
5734c9483b2SDavid S. Miller 	csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
5744c9483b2SDavid S. Miller 			       total_len, fl6->flowi6_proto, tmp_csum);
5751da177e4SLinus Torvalds 
5764c9483b2SDavid S. Miller 	if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP)
577f6ab0288SAl Viro 		csum = CSUM_MANGLED_0;
578357b40a1SHerbert Xu 
5798242fc33SHimangi Saraogi 	BUG_ON(skb_store_bits(skb, offset, &csum, 2));
580357b40a1SHerbert Xu 
5811da177e4SLinus Torvalds send:
5821da177e4SLinus Torvalds 	err = ip6_push_pending_frames(sk);
5831da177e4SLinus Torvalds out:
5841da177e4SLinus Torvalds 	return err;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
587c3c1a7dbSAl Viro static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length,
5884c9483b2SDavid S. Miller 			struct flowi6 *fl6, struct dst_entry **dstp,
589a818f75eSJesus Sanchez-Palencia 			unsigned int flags, const struct sockcm_cookie *sockc)
5901da177e4SLinus Torvalds {
5913320da89SHerbert Xu 	struct ipv6_pinfo *np = inet6_sk(sk);
592adb28c9dSEric W. Biederman 	struct net *net = sock_net(sk);
5931da177e4SLinus Torvalds 	struct ipv6hdr *iph;
5941da177e4SLinus Torvalds 	struct sk_buff *skb;
5951da177e4SLinus Torvalds 	int err;
5961789a640SEric Dumazet 	struct rt6_info *rt = (struct rt6_info *)*dstp;
597a7ae1992SHerbert Xu 	int hlen = LL_RESERVED_SPACE(rt->dst.dev);
598a7ae1992SHerbert Xu 	int tlen = rt->dst.dev->needed_tailroom;
5991da177e4SLinus Torvalds 
600d8d1f30bSChangli Gao 	if (length > rt->dst.dev->mtu) {
6014c9483b2SDavid S. Miller 		ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu);
6021da177e4SLinus Torvalds 		return -EMSGSIZE;
6031da177e4SLinus Torvalds 	}
60486f4c90aSAlexander Potapenko 	if (length < sizeof(struct ipv6hdr))
60586f4c90aSAlexander Potapenko 		return -EINVAL;
6061da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
6071da177e4SLinus Torvalds 		goto out;
6081da177e4SLinus Torvalds 
609f5184d26SJohannes Berg 	skb = sock_alloc_send_skb(sk,
610a7ae1992SHerbert Xu 				  length + hlen + tlen + 15,
6111da177e4SLinus Torvalds 				  flags & MSG_DONTWAIT, &err);
61263159f29SIan Morris 	if (!skb)
6131da177e4SLinus Torvalds 		goto error;
614a7ae1992SHerbert Xu 	skb_reserve(skb, hlen);
6151da177e4SLinus Torvalds 
6169c9c9ad5SHannes Frederic Sowa 	skb->protocol = htons(ETH_P_IPV6);
6171da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
618c6af0c22SWillem de Bruijn 	skb->mark = sockc->mark;
619a818f75eSJesus Sanchez-Palencia 	skb->tstamp = sockc->transmit_time;
6201da177e4SLinus Torvalds 
6211ced98e8SArnaldo Carvalho de Melo 	skb_put(skb, length);
6221ced98e8SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
6230660e03fSArnaldo Carvalho de Melo 	iph = ipv6_hdr(skb);
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	skb->ip_summed = CHECKSUM_NONE;
6261da177e4SLinus Torvalds 
6278f932f76SWillem de Bruijn 	skb_setup_tx_timestamp(skb, sockc->tsflags);
628fbfb2321SWillem de Bruijn 
6290dec879fSJulian Anastasov 	if (flags & MSG_CONFIRM)
6300dec879fSJulian Anastasov 		skb_set_dst_pending_confirm(skb, 1);
6310dec879fSJulian Anastasov 
632b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
63321226abbSAl Viro 	err = memcpy_from_msg(iph, msg, length);
634a688caa3SWei Wang 	if (err) {
635a688caa3SWei Wang 		err = -EFAULT;
636a688caa3SWei Wang 		kfree_skb(skb);
637a688caa3SWei Wang 		goto error;
638a688caa3SWei Wang 	}
639a688caa3SWei Wang 
640a688caa3SWei Wang 	skb_dst_set(skb, &rt->dst);
641a688caa3SWei Wang 	*dstp = NULL;
6421da177e4SLinus Torvalds 
643a8e3e1a9SDavid Ahern 	/* if egress device is enslaved to an L3 master device pass the
644a8e3e1a9SDavid Ahern 	 * skb to its handler for processing
645a8e3e1a9SDavid Ahern 	 */
646a8e3e1a9SDavid Ahern 	skb = l3mdev_ip6_out(sk, skb);
647a8e3e1a9SDavid Ahern 	if (unlikely(!skb))
648a8e3e1a9SDavid Ahern 		return 0;
649a8e3e1a9SDavid Ahern 
650a688caa3SWei Wang 	/* Acquire rcu_read_lock() in case we need to use rt->rt6i_idev
651a688caa3SWei Wang 	 * in the error path. Since skb has been freed, the dst could
652a688caa3SWei Wang 	 * have been queued for deletion.
653a688caa3SWei Wang 	 */
654a688caa3SWei Wang 	rcu_read_lock();
655adb28c9dSEric W. Biederman 	IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
65629a26a56SEric W. Biederman 	err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb,
65713206b6bSEric W. Biederman 		      NULL, rt->dst.dev, dst_output);
6581da177e4SLinus Torvalds 	if (err > 0)
6596ce9e7b5SEric Dumazet 		err = net_xmit_errno(err);
660a688caa3SWei Wang 	if (err) {
661a688caa3SWei Wang 		IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
662a688caa3SWei Wang 		rcu_read_unlock();
663a688caa3SWei Wang 		goto error_check;
664a688caa3SWei Wang 	}
665a688caa3SWei Wang 	rcu_read_unlock();
6661da177e4SLinus Torvalds out:
6671da177e4SLinus Torvalds 	return 0;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds error:
670adb28c9dSEric W. Biederman 	IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
671a688caa3SWei Wang error_check:
6726ce9e7b5SEric Dumazet 	if (err == -ENOBUFS && !np->recverr)
6736ce9e7b5SEric Dumazet 		err = 0;
6741da177e4SLinus Torvalds 	return err;
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
67719e3c66bSAl Viro struct raw6_frag_vec {
67819e3c66bSAl Viro 	struct msghdr *msg;
67919e3c66bSAl Viro 	int hlen;
68019e3c66bSAl Viro 	char c[4];
68119e3c66bSAl Viro };
68219e3c66bSAl Viro 
68319e3c66bSAl Viro static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6)
6841da177e4SLinus Torvalds {
68519e3c66bSAl Viro 	int err = 0;
6864c9483b2SDavid S. Miller 	switch (fl6->flowi6_proto) {
6871da177e4SLinus Torvalds 	case IPPROTO_ICMPV6:
68819e3c66bSAl Viro 		rfv->hlen = 2;
68919e3c66bSAl Viro 		err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
69019e3c66bSAl Viro 		if (!err) {
69119e3c66bSAl Viro 			fl6->fl6_icmp_type = rfv->c[0];
69219e3c66bSAl Viro 			fl6->fl6_icmp_code = rfv->c[1];
6931da177e4SLinus Torvalds 		}
6941da177e4SLinus Torvalds 		break;
6956e8f4d48SMasahide NAKAMURA 	case IPPROTO_MH:
69619e3c66bSAl Viro 		rfv->hlen = 4;
69719e3c66bSAl Viro 		err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
69819e3c66bSAl Viro 		if (!err)
69919e3c66bSAl Viro 			fl6->fl6_mh_type = rfv->c[2];
70019e3c66bSAl Viro 	}
70119e3c66bSAl Viro 	return err;
70219e3c66bSAl Viro }
7036e8f4d48SMasahide NAKAMURA 
70419e3c66bSAl Viro static int raw6_getfrag(void *from, char *to, int offset, int len, int odd,
70519e3c66bSAl Viro 		       struct sk_buff *skb)
70619e3c66bSAl Viro {
70719e3c66bSAl Viro 	struct raw6_frag_vec *rfv = from;
70819e3c66bSAl Viro 
70919e3c66bSAl Viro 	if (offset < rfv->hlen) {
71019e3c66bSAl Viro 		int copy = min(rfv->hlen - offset, len);
71119e3c66bSAl Viro 
71219e3c66bSAl Viro 		if (skb->ip_summed == CHECKSUM_PARTIAL)
71319e3c66bSAl Viro 			memcpy(to, rfv->c + offset, copy);
71419e3c66bSAl Viro 		else
71519e3c66bSAl Viro 			skb->csum = csum_block_add(
71619e3c66bSAl Viro 				skb->csum,
71719e3c66bSAl Viro 				csum_partial_copy_nocheck(rfv->c + offset,
718cc44c17bSAl Viro 							  to, copy),
71919e3c66bSAl Viro 				odd);
72019e3c66bSAl Viro 
72119e3c66bSAl Viro 		odd = 0;
72219e3c66bSAl Viro 		offset += copy;
72319e3c66bSAl Viro 		to += copy;
72419e3c66bSAl Viro 		len -= copy;
72519e3c66bSAl Viro 
72619e3c66bSAl Viro 		if (!len)
727a27b58feSHeiko Carstens 			return 0;
7281da177e4SLinus Torvalds 	}
7291da177e4SLinus Torvalds 
73019e3c66bSAl Viro 	offset -= rfv->hlen;
73119e3c66bSAl Viro 
732f69e6d13SAl Viro 	return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
73319e3c66bSAl Viro }
73419e3c66bSAl Viro 
7351b784140SYing Xue static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
7361da177e4SLinus Torvalds {
73745f6fad8SEric Dumazet 	struct ipv6_txoptions *opt_to_free = NULL;
7381da177e4SLinus Torvalds 	struct ipv6_txoptions opt_space;
739342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
74020c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
7411da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
7421da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
7431da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
7441da177e4SLinus Torvalds 	struct ipv6_txoptions *opt = NULL;
7451da177e4SLinus Torvalds 	struct ip6_flowlabel *flowlabel = NULL;
7461da177e4SLinus Torvalds 	struct dst_entry *dst = NULL;
74719e3c66bSAl Viro 	struct raw6_frag_vec rfv;
7484c9483b2SDavid S. Miller 	struct flowi6 fl6;
74926879da5SWei Wang 	struct ipcm6_cookie ipc6;
7501da177e4SLinus Torvalds 	int addr_len = msg->msg_namelen;
75159e3e4b5SOlivier Matz 	int hdrincl;
7521da177e4SLinus Torvalds 	u16 proto;
7531da177e4SLinus Torvalds 	int err;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	/* Rough check on arithmetic overflow,
756b59e139bSYOSHIFUJI Hideaki 	   better check is made in ip6_append_data().
7571da177e4SLinus Torvalds 	 */
758b59e139bSYOSHIFUJI Hideaki 	if (len > INT_MAX)
7591da177e4SLinus Torvalds 		return -EMSGSIZE;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	/* Mirror BSD error message compatibility */
7621da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
7631da177e4SLinus Torvalds 		return -EOPNOTSUPP;
7641da177e4SLinus Torvalds 
76559e3e4b5SOlivier Matz 	/* hdrincl should be READ_ONCE(inet->hdrincl)
76659e3e4b5SOlivier Matz 	 * but READ_ONCE() doesn't work with bit fields.
76759e3e4b5SOlivier Matz 	 * Doing this indirectly yields the same result.
76859e3e4b5SOlivier Matz 	 */
76959e3e4b5SOlivier Matz 	hdrincl = inet->hdrincl;
77059e3e4b5SOlivier Matz 	hdrincl = READ_ONCE(hdrincl);
77159e3e4b5SOlivier Matz 
7721da177e4SLinus Torvalds 	/*
7731da177e4SLinus Torvalds 	 *	Get and verify the address.
7741da177e4SLinus Torvalds 	 */
7754c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
7761da177e4SLinus Torvalds 
7774c9483b2SDavid S. Miller 	fl6.flowi6_mark = sk->sk_mark;
778e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sk->sk_uid;
7794a19ec58SLaszlo Attila Toth 
780b515430aSWillem de Bruijn 	ipcm6_init(&ipc6);
7815fdaa88dSWillem de Bruijn 	ipc6.sockc.tsflags = sk->sk_tsflags;
782c6af0c22SWillem de Bruijn 	ipc6.sockc.mark = sk->sk_mark;
78326879da5SWei Wang 
7841da177e4SLinus Torvalds 	if (sin6) {
7851da177e4SLinus Torvalds 		if (addr_len < SIN6_LEN_RFC2133)
7861da177e4SLinus Torvalds 			return -EINVAL;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 		if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
789a02cec21SEric Dumazet 			return -EAFNOSUPPORT;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 		/* port is the proto value [0..255] carried in nexthdr */
7921da177e4SLinus Torvalds 		proto = ntohs(sin6->sin6_port);
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 		if (!proto)
795c720c7e8SEric Dumazet 			proto = inet->inet_num;
796c720c7e8SEric Dumazet 		else if (proto != inet->inet_num)
797a02cec21SEric Dumazet 			return -EINVAL;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 		if (proto > 255)
800a02cec21SEric Dumazet 			return -EINVAL;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 		daddr = &sin6->sin6_addr;
8031da177e4SLinus Torvalds 		if (np->sndflow) {
8044c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
8054c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
8064c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
80759c820b2SWillem de Bruijn 				if (IS_ERR(flowlabel))
8081da177e4SLinus Torvalds 					return -EINVAL;
8091da177e4SLinus Torvalds 			}
8101da177e4SLinus Torvalds 		}
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 		/*
8131da177e4SLinus Torvalds 		 * Otherwise it will be difficult to maintain
8141da177e4SLinus Torvalds 		 * sk->sk_dst_cache.
8151da177e4SLinus Torvalds 		 */
8161da177e4SLinus Torvalds 		if (sk->sk_state == TCP_ESTABLISHED &&
817efe4208fSEric Dumazet 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
818efe4208fSEric Dumazet 			daddr = &sk->sk_v6_daddr;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds 		if (addr_len >= sizeof(struct sockaddr_in6) &&
8211da177e4SLinus Torvalds 		    sin6->sin6_scope_id &&
822842df073SHannes Frederic Sowa 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
8234c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
8241da177e4SLinus Torvalds 	} else {
8251da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED)
8261da177e4SLinus Torvalds 			return -EDESTADDRREQ;
8271da177e4SLinus Torvalds 
828c720c7e8SEric Dumazet 		proto = inet->inet_num;
829efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
8304c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
8311da177e4SLinus Torvalds 	}
8321da177e4SLinus Torvalds 
8334c9483b2SDavid S. Miller 	if (fl6.flowi6_oif == 0)
8344c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	if (msg->msg_controllen) {
8371da177e4SLinus Torvalds 		opt = &opt_space;
8381da177e4SLinus Torvalds 		memset(opt, 0, sizeof(struct ipv6_txoptions));
8391da177e4SLinus Torvalds 		opt->tot_len = sizeof(struct ipv6_txoptions);
84026879da5SWei Wang 		ipc6.opt = opt;
8411da177e4SLinus Torvalds 
8425fdaa88dSWillem de Bruijn 		err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6);
8431da177e4SLinus Torvalds 		if (err < 0) {
8441da177e4SLinus Torvalds 			fl6_sock_release(flowlabel);
8451da177e4SLinus Torvalds 			return err;
8461da177e4SLinus Torvalds 		}
8474c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
8484c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
84959c820b2SWillem de Bruijn 			if (IS_ERR(flowlabel))
8501da177e4SLinus Torvalds 				return -EINVAL;
8511da177e4SLinus Torvalds 		}
8521da177e4SLinus Torvalds 		if (!(opt->opt_nflen|opt->opt_flen))
8531da177e4SLinus Torvalds 			opt = NULL;
8541da177e4SLinus Torvalds 	}
85545f6fad8SEric Dumazet 	if (!opt) {
85645f6fad8SEric Dumazet 		opt = txopt_get(np);
85745f6fad8SEric Dumazet 		opt_to_free = opt;
85845f6fad8SEric Dumazet 	}
859df9890c3SYOSHIFUJI Hideaki 	if (flowlabel)
8601da177e4SLinus Torvalds 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
861df9890c3SYOSHIFUJI Hideaki 	opt = ipv6_fixup_options(&opt_space, opt);
8621da177e4SLinus Torvalds 
8634c9483b2SDavid S. Miller 	fl6.flowi6_proto = proto;
864c6af0c22SWillem de Bruijn 	fl6.flowi6_mark = ipc6.sockc.mark;
865b9aa52c4SOlivier Matz 
866b9aa52c4SOlivier Matz 	if (!hdrincl) {
86719e3c66bSAl Viro 		rfv.msg = msg;
86819e3c66bSAl Viro 		rfv.hlen = 0;
86919e3c66bSAl Viro 		err = rawv6_probe_proto_opt(&rfv, &fl6);
870a27b58feSHeiko Carstens 		if (err)
871a27b58feSHeiko Carstens 			goto out;
872b9aa52c4SOlivier Matz 	}
8731da177e4SLinus Torvalds 
874876c7f41SBrian Haley 	if (!ipv6_addr_any(daddr))
8754e3fd7a0SAlexey Dobriyan 		fl6.daddr = *daddr;
876876c7f41SBrian Haley 	else
8774c9483b2SDavid S. Miller 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
8784c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
8794e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
8801da177e4SLinus Torvalds 
8814c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
8821da177e4SLinus Torvalds 
8834c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
8844c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
885c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
886c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
8873df98d79SPaul Moore 	security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
8881da177e4SLinus Torvalds 
88959e3e4b5SOlivier Matz 	if (hdrincl)
89048e8aa6eSMartin KaFai Lau 		fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH;
89148e8aa6eSMartin KaFai Lau 
89238b7097bSHannes Frederic Sowa 	if (ipc6.tclass < 0)
89338b7097bSHannes Frederic Sowa 		ipc6.tclass = np->tclass;
89438b7097bSHannes Frederic Sowa 
89538b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
89638b7097bSHannes Frederic Sowa 
897c4e85f73SSabrina Dubroca 	dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
89868d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
89968d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
9001da177e4SLinus Torvalds 		goto out;
90114e50e57SDavid S. Miller 	}
90226879da5SWei Wang 	if (ipc6.hlimit < 0)
90326879da5SWei Wang 		ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
9041da177e4SLinus Torvalds 
90526879da5SWei Wang 	if (ipc6.dontfrag < 0)
90626879da5SWei Wang 		ipc6.dontfrag = np->dontfrag;
90713b52cd4SBrian Haley 
9081da177e4SLinus Torvalds 	if (msg->msg_flags&MSG_CONFIRM)
9091da177e4SLinus Torvalds 		goto do_confirm;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds back_from_confirm:
91259e3e4b5SOlivier Matz 	if (hdrincl)
913a818f75eSJesus Sanchez-Palencia 		err = rawv6_send_hdrinc(sk, msg, len, &fl6, &dst,
9145fdaa88dSWillem de Bruijn 					msg->msg_flags, &ipc6.sockc);
9151789a640SEric Dumazet 	else {
91626879da5SWei Wang 		ipc6.opt = opt;
9171da177e4SLinus Torvalds 		lock_sock(sk);
91819e3c66bSAl Viro 		err = ip6_append_data(sk, raw6_getfrag, &rfv,
91926879da5SWei Wang 			len, 0, &ipc6, &fl6, (struct rt6_info *)dst,
9205fdaa88dSWillem de Bruijn 			msg->msg_flags);
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 		if (err)
9231da177e4SLinus Torvalds 			ip6_flush_pending_frames(sk);
9241da177e4SLinus Torvalds 		else if (!(msg->msg_flags & MSG_MORE))
9254c9483b2SDavid S. Miller 			err = rawv6_push_pending_frames(sk, &fl6, rp);
9263ef9d943SYOSHIFUJI Hideaki 		release_sock(sk);
9271da177e4SLinus Torvalds 	}
9281da177e4SLinus Torvalds done:
9296d3e85ecSNicolas DICHTEL 	dst_release(dst);
9301da177e4SLinus Torvalds out:
9311da177e4SLinus Torvalds 	fl6_sock_release(flowlabel);
93245f6fad8SEric Dumazet 	txopt_put(opt_to_free);
9331da177e4SLinus Torvalds 	return err < 0 ? err : len;
9341da177e4SLinus Torvalds do_confirm:
9350dec879fSJulian Anastasov 	if (msg->msg_flags & MSG_PROBE)
9360dec879fSJulian Anastasov 		dst_confirm_neigh(dst, &fl6.daddr);
9371da177e4SLinus Torvalds 	if (!(msg->msg_flags & MSG_PROBE) || len)
9381da177e4SLinus Torvalds 		goto back_from_confirm;
9391da177e4SLinus Torvalds 	err = 0;
9401da177e4SLinus Torvalds 	goto done;
9411da177e4SLinus Torvalds }
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
944a7b75c5aSChristoph Hellwig 			       sockptr_t optval, int optlen)
9451da177e4SLinus Torvalds {
9461da177e4SLinus Torvalds 	switch (optname) {
9471da177e4SLinus Torvalds 	case ICMPV6_FILTER:
9481da177e4SLinus Torvalds 		if (optlen > sizeof(struct icmp6_filter))
9491da177e4SLinus Torvalds 			optlen = sizeof(struct icmp6_filter);
950a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&raw6_sk(sk)->filter, optval, optlen))
9511da177e4SLinus Torvalds 			return -EFAULT;
9521da177e4SLinus Torvalds 		return 0;
9531da177e4SLinus Torvalds 	default:
9541da177e4SLinus Torvalds 		return -ENOPROTOOPT;
9553ff50b79SStephen Hemminger 	}
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	return 0;
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
9611da177e4SLinus Torvalds 			       char __user *optval, int __user *optlen)
9621da177e4SLinus Torvalds {
9631da177e4SLinus Torvalds 	int len;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	switch (optname) {
9661da177e4SLinus Torvalds 	case ICMPV6_FILTER:
9671da177e4SLinus Torvalds 		if (get_user(len, optlen))
9681da177e4SLinus Torvalds 			return -EFAULT;
9691da177e4SLinus Torvalds 		if (len < 0)
9701da177e4SLinus Torvalds 			return -EINVAL;
9711da177e4SLinus Torvalds 		if (len > sizeof(struct icmp6_filter))
9721da177e4SLinus Torvalds 			len = sizeof(struct icmp6_filter);
9731da177e4SLinus Torvalds 		if (put_user(len, optlen))
9741da177e4SLinus Torvalds 			return -EFAULT;
9751da177e4SLinus Torvalds 		if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
9761da177e4SLinus Torvalds 			return -EFAULT;
9771da177e4SLinus Torvalds 		return 0;
9781da177e4SLinus Torvalds 	default:
9791da177e4SLinus Torvalds 		return -ENOPROTOOPT;
9803ff50b79SStephen Hemminger 	}
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	return 0;
9831da177e4SLinus Torvalds }
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 
9863fdadf7dSDmitry Mishin static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
987a7b75c5aSChristoph Hellwig 			       sockptr_t optval, unsigned int optlen)
9881da177e4SLinus Torvalds {
9891da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
9901da177e4SLinus Torvalds 	int val;
9911da177e4SLinus Torvalds 
992fb7bc920STamir Duberstein 	if (optlen < sizeof(val))
993fb7bc920STamir Duberstein 		return -EINVAL;
994fb7bc920STamir Duberstein 
995a7b75c5aSChristoph Hellwig 	if (copy_from_sockptr(&val, optval, sizeof(val)))
9961da177e4SLinus Torvalds 		return -EFAULT;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	switch (optname) {
999715f504bSHannes Frederic Sowa 	case IPV6_HDRINCL:
1000715f504bSHannes Frederic Sowa 		if (sk->sk_type != SOCK_RAW)
1001715f504bSHannes Frederic Sowa 			return -EINVAL;
1002715f504bSHannes Frederic Sowa 		inet_sk(sk)->hdrincl = !!val;
1003715f504bSHannes Frederic Sowa 		return 0;
10041da177e4SLinus Torvalds 	case IPV6_CHECKSUM:
1005c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 &&
10061a98d05fSYOSHIFUJI Hideaki 		    level == IPPROTO_IPV6) {
10071a98d05fSYOSHIFUJI Hideaki 			/*
10081a98d05fSYOSHIFUJI Hideaki 			 * RFC3542 tells that IPV6_CHECKSUM socket
10091a98d05fSYOSHIFUJI Hideaki 			 * option in the IPPROTO_IPV6 level is not
10101a98d05fSYOSHIFUJI Hideaki 			 * allowed on ICMPv6 sockets.
10111a98d05fSYOSHIFUJI Hideaki 			 * If you want to set it, use IPPROTO_RAW
10121a98d05fSYOSHIFUJI Hideaki 			 * level IPV6_CHECKSUM socket option
10131a98d05fSYOSHIFUJI Hideaki 			 * (Linux extension).
10141a98d05fSYOSHIFUJI Hideaki 			 */
10151a98d05fSYOSHIFUJI Hideaki 			return -EINVAL;
10161a98d05fSYOSHIFUJI Hideaki 		}
10171a98d05fSYOSHIFUJI Hideaki 
10181da177e4SLinus Torvalds 		/* You may get strange result with a positive odd offset;
10191da177e4SLinus Torvalds 		   RFC2292bis agrees with me. */
10201da177e4SLinus Torvalds 		if (val > 0 && (val&1))
1021a02cec21SEric Dumazet 			return -EINVAL;
10221da177e4SLinus Torvalds 		if (val < 0) {
10231da177e4SLinus Torvalds 			rp->checksum = 0;
10241da177e4SLinus Torvalds 		} else {
10251da177e4SLinus Torvalds 			rp->checksum = 1;
10261da177e4SLinus Torvalds 			rp->offset = val;
10271da177e4SLinus Torvalds 		}
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 		return 0;
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	default:
1032a02cec21SEric Dumazet 		return -ENOPROTOOPT;
10331da177e4SLinus Torvalds 	}
10341da177e4SLinus Torvalds }
10351da177e4SLinus Torvalds 
10363fdadf7dSDmitry Mishin static int rawv6_setsockopt(struct sock *sk, int level, int optname,
1037a7b75c5aSChristoph Hellwig 			    sockptr_t optval, unsigned int optlen)
10381da177e4SLinus Torvalds {
10391da177e4SLinus Torvalds 	switch (level) {
10401da177e4SLinus Torvalds 	case SOL_RAW:
10411da177e4SLinus Torvalds 		break;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	case SOL_ICMPV6:
1044c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
10451da177e4SLinus Torvalds 			return -EOPNOTSUPP;
1046207ec0abSJoe Perches 		return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
10471da177e4SLinus Torvalds 	case SOL_IPV6:
1048715f504bSHannes Frederic Sowa 		if (optname == IPV6_CHECKSUM ||
1049715f504bSHannes Frederic Sowa 		    optname == IPV6_HDRINCL)
10501da177e4SLinus Torvalds 			break;
1051a8eceea8SJoe Perches 		fallthrough;
10521da177e4SLinus Torvalds 	default:
1053207ec0abSJoe Perches 		return ipv6_setsockopt(sk, level, optname, optval, optlen);
10543ff50b79SStephen Hemminger 	}
10553ff50b79SStephen Hemminger 
10563fdadf7dSDmitry Mishin 	return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
10573fdadf7dSDmitry Mishin }
10583fdadf7dSDmitry Mishin 
10593fdadf7dSDmitry Mishin static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
10603fdadf7dSDmitry Mishin 			    char __user *optval, int __user *optlen)
10613fdadf7dSDmitry Mishin {
10623fdadf7dSDmitry Mishin 	struct raw6_sock *rp = raw6_sk(sk);
10633fdadf7dSDmitry Mishin 	int val, len;
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	if (get_user(len, optlen))
10661da177e4SLinus Torvalds 		return -EFAULT;
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	switch (optname) {
1069715f504bSHannes Frederic Sowa 	case IPV6_HDRINCL:
1070715f504bSHannes Frederic Sowa 		val = inet_sk(sk)->hdrincl;
1071715f504bSHannes Frederic Sowa 		break;
10721da177e4SLinus Torvalds 	case IPV6_CHECKSUM:
10731a98d05fSYOSHIFUJI Hideaki 		/*
10741a98d05fSYOSHIFUJI Hideaki 		 * We allow getsockopt() for IPPROTO_IPV6-level
10751a98d05fSYOSHIFUJI Hideaki 		 * IPV6_CHECKSUM socket option on ICMPv6 sockets
10761a98d05fSYOSHIFUJI Hideaki 		 * since RFC3542 is silent about it.
10771a98d05fSYOSHIFUJI Hideaki 		 */
10781da177e4SLinus Torvalds 		if (rp->checksum == 0)
10791da177e4SLinus Torvalds 			val = -1;
10801da177e4SLinus Torvalds 		else
10811da177e4SLinus Torvalds 			val = rp->offset;
10821da177e4SLinus Torvalds 		break;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	default:
10851da177e4SLinus Torvalds 		return -ENOPROTOOPT;
10861da177e4SLinus Torvalds 	}
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	len = min_t(unsigned int, sizeof(int), len);
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	if (put_user(len, optlen))
10911da177e4SLinus Torvalds 		return -EFAULT;
10921da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, len))
10931da177e4SLinus Torvalds 		return -EFAULT;
10941da177e4SLinus Torvalds 	return 0;
10951da177e4SLinus Torvalds }
10961da177e4SLinus Torvalds 
10973fdadf7dSDmitry Mishin static int rawv6_getsockopt(struct sock *sk, int level, int optname,
10983fdadf7dSDmitry Mishin 			  char __user *optval, int __user *optlen)
10993fdadf7dSDmitry Mishin {
11003fdadf7dSDmitry Mishin 	switch (level) {
11013fdadf7dSDmitry Mishin 	case SOL_RAW:
11023fdadf7dSDmitry Mishin 		break;
11033fdadf7dSDmitry Mishin 
11043fdadf7dSDmitry Mishin 	case SOL_ICMPV6:
1105c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
11063fdadf7dSDmitry Mishin 			return -EOPNOTSUPP;
1107207ec0abSJoe Perches 		return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
11083fdadf7dSDmitry Mishin 	case SOL_IPV6:
1109715f504bSHannes Frederic Sowa 		if (optname == IPV6_CHECKSUM ||
1110715f504bSHannes Frederic Sowa 		    optname == IPV6_HDRINCL)
11113fdadf7dSDmitry Mishin 			break;
1112a8eceea8SJoe Perches 		fallthrough;
11133fdadf7dSDmitry Mishin 	default:
1114207ec0abSJoe Perches 		return ipv6_getsockopt(sk, level, optname, optval, optlen);
11153ff50b79SStephen Hemminger 	}
11163ff50b79SStephen Hemminger 
11173fdadf7dSDmitry Mishin 	return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
11183fdadf7dSDmitry Mishin }
11193fdadf7dSDmitry Mishin 
11201da177e4SLinus Torvalds static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
11211da177e4SLinus Torvalds {
11221da177e4SLinus Torvalds 	switch (cmd) {
1123207ec0abSJoe Perches 	case SIOCOUTQ: {
112431e6d363SEric Dumazet 		int amount = sk_wmem_alloc_get(sk);
112531e6d363SEric Dumazet 
11261da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
11271da177e4SLinus Torvalds 	}
1128207ec0abSJoe Perches 	case SIOCINQ: {
11291da177e4SLinus Torvalds 		struct sk_buff *skb;
11301da177e4SLinus Torvalds 		int amount = 0;
11311da177e4SLinus Torvalds 
1132e0f9f858SHerbert Xu 		spin_lock_bh(&sk->sk_receive_queue.lock);
11331da177e4SLinus Torvalds 		skb = skb_peek(&sk->sk_receive_queue);
113453b24b8fSIan Morris 		if (skb)
1135105f5528SJamie Bainbridge 			amount = skb->len;
1136e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_receive_queue.lock);
11371da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
11381da177e4SLinus Torvalds 	}
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	default:
11417bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
11427bc570c8SYOSHIFUJI Hideaki 		return ip6mr_ioctl(sk, cmd, (void __user *)arg);
11437bc570c8SYOSHIFUJI Hideaki #else
11441da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
11457bc570c8SYOSHIFUJI Hideaki #endif
11461da177e4SLinus Torvalds 	}
11471da177e4SLinus Torvalds }
11481da177e4SLinus Torvalds 
1149e2d57766SDavid S. Miller #ifdef CONFIG_COMPAT
1150e2d57766SDavid S. Miller static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
1151e2d57766SDavid S. Miller {
1152e2d57766SDavid S. Miller 	switch (cmd) {
1153e2d57766SDavid S. Miller 	case SIOCOUTQ:
1154e2d57766SDavid S. Miller 	case SIOCINQ:
1155e2d57766SDavid S. Miller 		return -ENOIOCTLCMD;
1156e2d57766SDavid S. Miller 	default:
1157e2d57766SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
1158e2d57766SDavid S. Miller 		return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg));
1159e2d57766SDavid S. Miller #else
1160e2d57766SDavid S. Miller 		return -ENOIOCTLCMD;
1161e2d57766SDavid S. Miller #endif
1162e2d57766SDavid S. Miller 	}
1163e2d57766SDavid S. Miller }
1164e2d57766SDavid S. Miller #endif
1165e2d57766SDavid S. Miller 
11661da177e4SLinus Torvalds static void rawv6_close(struct sock *sk, long timeout)
11671da177e4SLinus Torvalds {
1168c720c7e8SEric Dumazet 	if (inet_sk(sk)->inet_num == IPPROTO_RAW)
1169725a8ff0SDenis V. Lunev 		ip6_ra_control(sk, -1);
11707bc570c8SYOSHIFUJI Hideaki 	ip6mr_sk_done(sk);
11711da177e4SLinus Torvalds 	sk_common_release(sk);
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds 
11747d06b2e0SBrian Haley static void raw6_destroy(struct sock *sk)
117522dd4850SDenis V. Lunev {
117622dd4850SDenis V. Lunev 	lock_sock(sk);
117722dd4850SDenis V. Lunev 	ip6_flush_pending_frames(sk);
117822dd4850SDenis V. Lunev 	release_sock(sk);
1179f23d60deSDavid S. Miller 
11807d06b2e0SBrian Haley 	inet6_destroy_sock(sk);
118122dd4850SDenis V. Lunev }
118222dd4850SDenis V. Lunev 
11831da177e4SLinus Torvalds static int rawv6_init_sk(struct sock *sk)
11841da177e4SLinus Torvalds {
11851da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
1186f48d5ff1SMasahide NAKAMURA 
1187c720c7e8SEric Dumazet 	switch (inet_sk(sk)->inet_num) {
1188f48d5ff1SMasahide NAKAMURA 	case IPPROTO_ICMPV6:
11891da177e4SLinus Torvalds 		rp->checksum = 1;
11901da177e4SLinus Torvalds 		rp->offset   = 2;
1191f48d5ff1SMasahide NAKAMURA 		break;
1192f48d5ff1SMasahide NAKAMURA 	case IPPROTO_MH:
1193f48d5ff1SMasahide NAKAMURA 		rp->checksum = 1;
1194f48d5ff1SMasahide NAKAMURA 		rp->offset   = 4;
1195f48d5ff1SMasahide NAKAMURA 		break;
1196f48d5ff1SMasahide NAKAMURA 	default:
1197f48d5ff1SMasahide NAKAMURA 		break;
11981da177e4SLinus Torvalds 	}
1199a02cec21SEric Dumazet 	return 0;
12001da177e4SLinus Torvalds }
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds struct proto rawv6_prot = {
12031da177e4SLinus Torvalds 	.name		   = "RAWv6",
12041da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
12051da177e4SLinus Torvalds 	.close		   = rawv6_close,
120622dd4850SDenis V. Lunev 	.destroy	   = raw6_destroy,
120782b276cdSHannes Frederic Sowa 	.connect	   = ip6_datagram_connect_v6_only,
1208286c72deSEric Dumazet 	.disconnect	   = __udp_disconnect,
12091da177e4SLinus Torvalds 	.ioctl		   = rawv6_ioctl,
12101da177e4SLinus Torvalds 	.init		   = rawv6_init_sk,
12111da177e4SLinus Torvalds 	.setsockopt	   = rawv6_setsockopt,
12121da177e4SLinus Torvalds 	.getsockopt	   = rawv6_getsockopt,
12131da177e4SLinus Torvalds 	.sendmsg	   = rawv6_sendmsg,
12141da177e4SLinus Torvalds 	.recvmsg	   = rawv6_recvmsg,
12151da177e4SLinus Torvalds 	.bind		   = rawv6_bind,
12161da177e4SLinus Torvalds 	.backlog_rcv	   = rawv6_rcv_skb,
1217fc8717baSPavel Emelyanov 	.hash		   = raw_hash_sk,
1218fc8717baSPavel Emelyanov 	.unhash		   = raw_unhash_sk,
12191da177e4SLinus Torvalds 	.obj_size	   = sizeof(struct raw6_sock),
12208c2bc895SDavid Windsor 	.useroffset	   = offsetof(struct raw6_sock, filter),
12218c2bc895SDavid Windsor 	.usersize	   = sizeof_field(struct raw6_sock, filter),
1222fc8717baSPavel Emelyanov 	.h.raw_hash	   = &raw_v6_hashinfo,
1223543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
1224e2d57766SDavid S. Miller 	.compat_ioctl	   = compat_rawv6_ioctl,
1225543d9cfeSArnaldo Carvalho de Melo #endif
1226432490f9SCyrill Gorcunov 	.diag_destroy	   = raw_abort,
12271da177e4SLinus Torvalds };
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12301da177e4SLinus Torvalds static int raw6_seq_show(struct seq_file *seq, void *v)
12311da177e4SLinus Torvalds {
123217ef66afSLorenzo Colitti 	if (v == SEQ_START_TOKEN) {
123317ef66afSLorenzo Colitti 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
123417ef66afSLorenzo Colitti 	} else {
123517ef66afSLorenzo Colitti 		struct sock *sp = v;
123617ef66afSLorenzo Colitti 		__u16 srcp  = inet_sk(sp)->inet_num;
123717ef66afSLorenzo Colitti 		ip6_dgram_sock_seq_show(seq, v, srcp, 0,
123817ef66afSLorenzo Colitti 					raw_seq_private(seq)->bucket);
123917ef66afSLorenzo Colitti 	}
12401da177e4SLinus Torvalds 	return 0;
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
124356b3d975SPhilippe De Muyter static const struct seq_operations raw6_seq_ops = {
124442a73808SPavel Emelyanov 	.start =	raw_seq_start,
124542a73808SPavel Emelyanov 	.next =		raw_seq_next,
124642a73808SPavel Emelyanov 	.stop =		raw_seq_stop,
12471da177e4SLinus Torvalds 	.show =		raw6_seq_show,
12481da177e4SLinus Torvalds };
12491da177e4SLinus Torvalds 
12502c8c1e72SAlexey Dobriyan static int __net_init raw6_init_net(struct net *net)
1251a308da16SPavel Emelyanov {
1252c3506372SChristoph Hellwig 	if (!proc_create_net_data("raw6", 0444, net->proc_net, &raw6_seq_ops,
1253c3506372SChristoph Hellwig 			sizeof(struct raw_iter_state), &raw_v6_hashinfo))
1254a308da16SPavel Emelyanov 		return -ENOMEM;
1255a308da16SPavel Emelyanov 
1256a308da16SPavel Emelyanov 	return 0;
1257a308da16SPavel Emelyanov }
1258a308da16SPavel Emelyanov 
12592c8c1e72SAlexey Dobriyan static void __net_exit raw6_exit_net(struct net *net)
1260a308da16SPavel Emelyanov {
1261ece31ffdSGao feng 	remove_proc_entry("raw6", net->proc_net);
1262a308da16SPavel Emelyanov }
1263a308da16SPavel Emelyanov 
1264a308da16SPavel Emelyanov static struct pernet_operations raw6_net_ops = {
1265a308da16SPavel Emelyanov 	.init = raw6_init_net,
1266a308da16SPavel Emelyanov 	.exit = raw6_exit_net,
1267a308da16SPavel Emelyanov };
1268a308da16SPavel Emelyanov 
12691da177e4SLinus Torvalds int __init raw6_proc_init(void)
12701da177e4SLinus Torvalds {
1271a308da16SPavel Emelyanov 	return register_pernet_subsys(&raw6_net_ops);
12721da177e4SLinus Torvalds }
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds void raw6_proc_exit(void)
12751da177e4SLinus Torvalds {
1276a308da16SPavel Emelyanov 	unregister_pernet_subsys(&raw6_net_ops);
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
12797f4e4868SDaniel Lezcano 
1280a11e1d43SLinus Torvalds /* Same as inet6_dgram_ops, sans udp_poll.  */
128177d4b1d3SEric Dumazet const struct proto_ops inet6_sockraw_ops = {
12827f4e4868SDaniel Lezcano 	.family		   = PF_INET6,
12837f4e4868SDaniel Lezcano 	.owner		   = THIS_MODULE,
12847f4e4868SDaniel Lezcano 	.release	   = inet6_release,
12857f4e4868SDaniel Lezcano 	.bind		   = inet6_bind,
12867f4e4868SDaniel Lezcano 	.connect	   = inet_dgram_connect,	/* ok		*/
12877f4e4868SDaniel Lezcano 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
12887f4e4868SDaniel Lezcano 	.accept		   = sock_no_accept,		/* a do nothing	*/
12897f4e4868SDaniel Lezcano 	.getname	   = inet6_getname,
1290a11e1d43SLinus Torvalds 	.poll		   = datagram_poll,		/* ok		*/
12917f4e4868SDaniel Lezcano 	.ioctl		   = inet6_ioctl,		/* must change  */
1292c7cbdbf2SArnd Bergmann 	.gettstamp	   = sock_gettstamp,
12937f4e4868SDaniel Lezcano 	.listen		   = sock_no_listen,		/* ok		*/
12947f4e4868SDaniel Lezcano 	.shutdown	   = inet_shutdown,		/* ok		*/
12957f4e4868SDaniel Lezcano 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
12967f4e4868SDaniel Lezcano 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
12977f4e4868SDaniel Lezcano 	.sendmsg	   = inet_sendmsg,		/* ok		*/
12987f4e4868SDaniel Lezcano 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
12997f4e4868SDaniel Lezcano 	.mmap		   = sock_no_mmap,
13007f4e4868SDaniel Lezcano 	.sendpage	   = sock_no_sendpage,
13017f4e4868SDaniel Lezcano #ifdef CONFIG_COMPAT
13023986912fSChristoph Hellwig 	.compat_ioctl	   = inet6_compat_ioctl,
13037f4e4868SDaniel Lezcano #endif
13047f4e4868SDaniel Lezcano };
13057f4e4868SDaniel Lezcano 
13067f4e4868SDaniel Lezcano static struct inet_protosw rawv6_protosw = {
13077f4e4868SDaniel Lezcano 	.type		= SOCK_RAW,
13087f4e4868SDaniel Lezcano 	.protocol	= IPPROTO_IP,	/* wild card */
13097f4e4868SDaniel Lezcano 	.prot		= &rawv6_prot,
13107f4e4868SDaniel Lezcano 	.ops		= &inet6_sockraw_ops,
13117f4e4868SDaniel Lezcano 	.flags		= INET_PROTOSW_REUSE,
13127f4e4868SDaniel Lezcano };
13137f4e4868SDaniel Lezcano 
13147f4e4868SDaniel Lezcano int __init rawv6_init(void)
13157f4e4868SDaniel Lezcano {
13163d2f6d41SJulia Lawall 	return inet6_register_protosw(&rawv6_protosw);
13177f4e4868SDaniel Lezcano }
13187f4e4868SDaniel Lezcano 
131909f7709fSDaniel Lezcano void rawv6_exit(void)
13207f4e4868SDaniel Lezcano {
13217f4e4868SDaniel Lezcano 	inet6_unregister_protosw(&rawv6_protosw);
13227f4e4868SDaniel Lezcano }
1323