xref: /openbmc/linux/net/ipv6/raw.c (revision cb3e9864)
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();
158f289c02bSKuniyuki 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 	struct net *net = dev_net(skb->dev);
3360daf07e5SEric Dumazet 	struct hlist_nulls_head *hlist;
3370daf07e5SEric Dumazet 	struct hlist_nulls_node *hnode;
33869d6da0bSPavel Emelyanov 	struct sock *sk;
33969d6da0bSPavel Emelyanov 	int hash;
34069d6da0bSPavel Emelyanov 
341b673e4dfSPavel Emelyanov 	hash = nexthdr & (RAW_HTABLE_SIZE - 1);
3420daf07e5SEric Dumazet 	hlist = &raw_v6_hashinfo.ht[hash];
3430daf07e5SEric Dumazet 	rcu_read_lock();
344f289c02bSKuniyuki Iwashima 	sk_nulls_for_each(sk, hnode, hlist) {
34505f175cdSYOSHIFUJI Hideaki 		/* Note: ipv6_hdr(skb) != skb->data */
346b71d1d42SEric Dumazet 		const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data;
34769d6da0bSPavel Emelyanov 
348ba44f818SEric Dumazet 		if (!raw_v6_match(net, sk, nexthdr, &ip6h->saddr, &ip6h->daddr,
349ba44f818SEric Dumazet 				  inet6_iif(skb), inet6_iif(skb)))
350ba44f818SEric Dumazet 			continue;
351ba44f818SEric Dumazet 		rawv6_err(sk, skb, NULL, type, code, inner_offset, info);
35269d6da0bSPavel Emelyanov 	}
3530daf07e5SEric Dumazet 	rcu_read_unlock();
35469d6da0bSPavel Emelyanov }
35569d6da0bSPavel Emelyanov 
3561da177e4SLinus Torvalds static inline int rawv6_rcv_skb(struct sock *sk, struct sk_buff *skb)
3571da177e4SLinus Torvalds {
35833d480ceSEric Dumazet 	if ((raw6_sk(sk)->checksum || rcu_access_pointer(sk->sk_filter)) &&
359fb286bb2SHerbert Xu 	    skb_checksum_complete(skb)) {
360a92aa318SWang Chen 		atomic_inc(&sk->sk_drops);
3611da177e4SLinus Torvalds 		kfree_skb(skb);
3623cc76caaSYang Hongyang 		return NET_RX_DROP;
3631da177e4SLinus Torvalds 	}
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	/* Charge it to the socket. */
366d826eb14SEric Dumazet 	skb_dst_drop(skb);
367d826eb14SEric Dumazet 	if (sock_queue_rcv_skb(sk, skb) < 0) {
3681da177e4SLinus Torvalds 		kfree_skb(skb);
3693cc76caaSYang Hongyang 		return NET_RX_DROP;
3701da177e4SLinus Torvalds 	}
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	return 0;
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds /*
3761da177e4SLinus Torvalds  *	This is next to useless...
3771da177e4SLinus Torvalds  *	if we demultiplex in network layer we don't need the extra call
3781da177e4SLinus Torvalds  *	just to queue the skb...
3791da177e4SLinus Torvalds  *	maybe we could have the network decide upon a hint if it
3801da177e4SLinus Torvalds  *	should call raw_rcv for demultiplexing
3811da177e4SLinus Torvalds  */
3821da177e4SLinus Torvalds int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
3851da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
388a92aa318SWang Chen 		atomic_inc(&sk->sk_drops);
3891da177e4SLinus Torvalds 		kfree_skb(skb);
3901da177e4SLinus Torvalds 		return NET_RX_DROP;
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	if (!rp->checksum)
3941da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_UNNECESSARY;
3951da177e4SLinus Torvalds 
39684fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
397d56f90a7SArnaldo Carvalho de Melo 		skb_postpull_rcsum(skb, skb_network_header(skb),
398cfe1fc77SArnaldo Carvalho de Melo 				   skb_network_header_len(skb));
3990660e03fSArnaldo Carvalho de Melo 		if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4000660e03fSArnaldo Carvalho de Melo 				     &ipv6_hdr(skb)->daddr,
401c720c7e8SEric Dumazet 				     skb->len, inet->inet_num, skb->csum))
402fb286bb2SHerbert Xu 			skb->ip_summed = CHECKSUM_UNNECESSARY;
4031da177e4SLinus Torvalds 	}
40460476372SHerbert Xu 	if (!skb_csum_unnecessary(skb))
4050660e03fSArnaldo Carvalho de Melo 		skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4060660e03fSArnaldo Carvalho de Melo 							 &ipv6_hdr(skb)->daddr,
4070660e03fSArnaldo Carvalho de Melo 							 skb->len,
408c720c7e8SEric Dumazet 							 inet->inet_num, 0));
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	if (inet->hdrincl) {
411fb286bb2SHerbert Xu 		if (skb_checksum_complete(skb)) {
412a92aa318SWang Chen 			atomic_inc(&sk->sk_drops);
4131da177e4SLinus Torvalds 			kfree_skb(skb);
4143cc76caaSYang Hongyang 			return NET_RX_DROP;
4151da177e4SLinus Torvalds 		}
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	rawv6_rcv_skb(sk, skb);
4191da177e4SLinus Torvalds 	return 0;
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds /*
4241da177e4SLinus Torvalds  *	This should be easy, if there is something there
4251da177e4SLinus Torvalds  *	we return it, otherwise we block.
4261da177e4SLinus Torvalds  */
4271da177e4SLinus Torvalds 
4281b784140SYing Xue static int rawv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
429ec095263SOliver Hartkopp 			 int flags, int *addr_len)
4301da177e4SLinus Torvalds {
4311da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
432342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
4331da177e4SLinus Torvalds 	struct sk_buff *skb;
4341da177e4SLinus Torvalds 	size_t copied;
4351da177e4SLinus Torvalds 	int err;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	if (flags & MSG_OOB)
4381da177e4SLinus Torvalds 		return -EOPNOTSUPP;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	if (flags & MSG_ERRQUEUE)
44185fbaa75SHannes Frederic Sowa 		return ipv6_recv_error(sk, msg, len, addr_len);
4421da177e4SLinus Torvalds 
4434b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
44485fbaa75SHannes Frederic Sowa 		return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
4454b340ae2SBrian Haley 
446f4b41f06SOliver Hartkopp 	skb = skb_recv_datagram(sk, flags, &err);
4471da177e4SLinus Torvalds 	if (!skb)
4481da177e4SLinus Torvalds 		goto out;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	copied = skb->len;
4511da177e4SLinus Torvalds 	if (copied > len) {
4521da177e4SLinus Torvalds 		copied = len;
4531da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
4541da177e4SLinus Torvalds 	}
4551da177e4SLinus Torvalds 
45660476372SHerbert Xu 	if (skb_csum_unnecessary(skb)) {
45751f3d02bSDavid S. Miller 		err = skb_copy_datagram_msg(skb, 0, msg, copied);
4581da177e4SLinus Torvalds 	} else if (msg->msg_flags&MSG_TRUNC) {
459fb286bb2SHerbert Xu 		if (__skb_checksum_complete(skb))
4601da177e4SLinus Torvalds 			goto csum_copy_err;
46151f3d02bSDavid S. Miller 		err = skb_copy_datagram_msg(skb, 0, msg, copied);
4621da177e4SLinus Torvalds 	} else {
463227158dbSAl Viro 		err = skb_copy_and_csum_datagram_msg(skb, 0, msg);
4641da177e4SLinus Torvalds 		if (err == -EINVAL)
4651da177e4SLinus Torvalds 			goto csum_copy_err;
4661da177e4SLinus Torvalds 	}
4671da177e4SLinus Torvalds 	if (err)
4681da177e4SLinus Torvalds 		goto out_free;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	/* Copy the address. */
4711da177e4SLinus Torvalds 	if (sin6) {
4721da177e4SLinus Torvalds 		sin6->sin6_family = AF_INET6;
473f59fc7f3STetsuo Handa 		sin6->sin6_port = 0;
4744e3fd7a0SAlexey Dobriyan 		sin6->sin6_addr = ipv6_hdr(skb)->saddr;
4751da177e4SLinus Torvalds 		sin6->sin6_flowinfo = 0;
476842df073SHannes Frederic Sowa 		sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
4774330487aSDuan Jiong 							  inet6_iif(skb));
478bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin6);
4791da177e4SLinus Torvalds 	}
4801da177e4SLinus Torvalds 
4816fd1d51cSErin MacNeil 	sock_recv_cmsgs(msg, sk, skb);
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (np->rxopt.all)
48473df66f8STom Parkin 		ip6_datagram_recv_ctl(sk, msg, skb);
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	err = copied;
4871da177e4SLinus Torvalds 	if (flags & MSG_TRUNC)
4881da177e4SLinus Torvalds 		err = skb->len;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds out_free:
4911da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
4921da177e4SLinus Torvalds out:
4931da177e4SLinus Torvalds 	return err;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds csum_copy_err:
4963305b80cSHerbert Xu 	skb_kill_datagram(sk, skb, flags);
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	/* Error for blocking case is chosen to masquerade
4991da177e4SLinus Torvalds 	   as some normal condition.
5001da177e4SLinus Torvalds 	 */
5011da177e4SLinus Torvalds 	err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
5023305b80cSHerbert Xu 	goto out;
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
5054c9483b2SDavid S. Miller static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
506357b40a1SHerbert Xu 				     struct raw6_sock *rp)
5071da177e4SLinus Torvalds {
508*cb3e9864SHerbert Xu 	struct ipv6_txoptions *opt;
5091da177e4SLinus Torvalds 	struct sk_buff *skb;
5101da177e4SLinus Torvalds 	int err = 0;
511357b40a1SHerbert Xu 	int offset;
512357b40a1SHerbert Xu 	int len;
513679a8738SHerbert Xu 	int total_len;
514868c86bcSAl Viro 	__wsum tmp_csum;
515868c86bcSAl Viro 	__sum16 csum;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	if (!rp->checksum)
5181da177e4SLinus Torvalds 		goto send;
5191da177e4SLinus Torvalds 
52043728fa5SFabian Frederick 	skb = skb_peek(&sk->sk_write_queue);
52143728fa5SFabian Frederick 	if (!skb)
5221da177e4SLinus Torvalds 		goto out;
5231da177e4SLinus Torvalds 
524357b40a1SHerbert Xu 	offset = rp->offset;
525299b0767SSteffen Klassert 	total_len = inet_sk(sk)->cork.base.length;
526*cb3e9864SHerbert Xu 	opt = inet6_sk(sk)->cork.opt;
527*cb3e9864SHerbert Xu 	total_len -= opt ? opt->opt_flen : 0;
528*cb3e9864SHerbert Xu 
529679a8738SHerbert Xu 	if (offset >= total_len - 1) {
5301da177e4SLinus Torvalds 		err = -EINVAL;
531357b40a1SHerbert Xu 		ip6_flush_pending_frames(sk);
5321da177e4SLinus Torvalds 		goto out;
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	/* should be check HW csum miyazawa */
5361da177e4SLinus Torvalds 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
5371da177e4SLinus Torvalds 		/*
5381da177e4SLinus Torvalds 		 * Only one fragment on the socket.
5391da177e4SLinus Torvalds 		 */
5401da177e4SLinus Torvalds 		tmp_csum = skb->csum;
5411da177e4SLinus Torvalds 	} else {
542357b40a1SHerbert Xu 		struct sk_buff *csum_skb = NULL;
5431da177e4SLinus Torvalds 		tmp_csum = 0;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 		skb_queue_walk(&sk->sk_write_queue, skb) {
5461da177e4SLinus Torvalds 			tmp_csum = csum_add(tmp_csum, skb->csum);
547357b40a1SHerbert Xu 
548357b40a1SHerbert Xu 			if (csum_skb)
549357b40a1SHerbert Xu 				continue;
550357b40a1SHerbert Xu 
551ea2ae17dSArnaldo Carvalho de Melo 			len = skb->len - skb_transport_offset(skb);
552357b40a1SHerbert Xu 			if (offset >= len) {
553357b40a1SHerbert Xu 				offset -= len;
554357b40a1SHerbert Xu 				continue;
5551da177e4SLinus Torvalds 			}
556357b40a1SHerbert Xu 
557357b40a1SHerbert Xu 			csum_skb = skb;
5581da177e4SLinus Torvalds 		}
5591da177e4SLinus Torvalds 
560357b40a1SHerbert Xu 		skb = csum_skb;
561357b40a1SHerbert Xu 	}
562357b40a1SHerbert Xu 
563ea2ae17dSArnaldo Carvalho de Melo 	offset += skb_transport_offset(skb);
564a98f9175SDave Jones 	err = skb_copy_bits(skb, offset, &csum, 2);
565a98f9175SDave Jones 	if (err < 0) {
566a98f9175SDave Jones 		ip6_flush_pending_frames(sk);
567a98f9175SDave Jones 		goto out;
568a98f9175SDave Jones 	}
569357b40a1SHerbert Xu 
5701da177e4SLinus Torvalds 	/* in case cksum was not initialized */
571357b40a1SHerbert Xu 	if (unlikely(csum))
5725f92a738SAl Viro 		tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
5731da177e4SLinus Torvalds 
5744c9483b2SDavid S. Miller 	csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
5754c9483b2SDavid S. Miller 			       total_len, fl6->flowi6_proto, tmp_csum);
5761da177e4SLinus Torvalds 
5774c9483b2SDavid S. Miller 	if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP)
578f6ab0288SAl Viro 		csum = CSUM_MANGLED_0;
579357b40a1SHerbert Xu 
5808242fc33SHimangi Saraogi 	BUG_ON(skb_store_bits(skb, offset, &csum, 2));
581357b40a1SHerbert Xu 
5821da177e4SLinus Torvalds send:
5831da177e4SLinus Torvalds 	err = ip6_push_pending_frames(sk);
5841da177e4SLinus Torvalds out:
5851da177e4SLinus Torvalds 	return err;
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
588c3c1a7dbSAl Viro static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length,
5894c9483b2SDavid S. Miller 			struct flowi6 *fl6, struct dst_entry **dstp,
590a818f75eSJesus Sanchez-Palencia 			unsigned int flags, const struct sockcm_cookie *sockc)
5911da177e4SLinus Torvalds {
5923320da89SHerbert Xu 	struct ipv6_pinfo *np = inet6_sk(sk);
593adb28c9dSEric W. Biederman 	struct net *net = sock_net(sk);
5941da177e4SLinus Torvalds 	struct ipv6hdr *iph;
5951da177e4SLinus Torvalds 	struct sk_buff *skb;
5961da177e4SLinus Torvalds 	int err;
5971789a640SEric Dumazet 	struct rt6_info *rt = (struct rt6_info *)*dstp;
598a7ae1992SHerbert Xu 	int hlen = LL_RESERVED_SPACE(rt->dst.dev);
599a7ae1992SHerbert Xu 	int tlen = rt->dst.dev->needed_tailroom;
6001da177e4SLinus Torvalds 
601d8d1f30bSChangli Gao 	if (length > rt->dst.dev->mtu) {
6024c9483b2SDavid S. Miller 		ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu);
6031da177e4SLinus Torvalds 		return -EMSGSIZE;
6041da177e4SLinus Torvalds 	}
60586f4c90aSAlexander Potapenko 	if (length < sizeof(struct ipv6hdr))
60686f4c90aSAlexander Potapenko 		return -EINVAL;
6071da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
6081da177e4SLinus Torvalds 		goto out;
6091da177e4SLinus Torvalds 
610f5184d26SJohannes Berg 	skb = sock_alloc_send_skb(sk,
611a7ae1992SHerbert Xu 				  length + hlen + tlen + 15,
6121da177e4SLinus Torvalds 				  flags & MSG_DONTWAIT, &err);
61363159f29SIan Morris 	if (!skb)
6141da177e4SLinus Torvalds 		goto error;
615a7ae1992SHerbert Xu 	skb_reserve(skb, hlen);
6161da177e4SLinus Torvalds 
6179c9c9ad5SHannes Frederic Sowa 	skb->protocol = htons(ETH_P_IPV6);
6181da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
619c6af0c22SWillem de Bruijn 	skb->mark = sockc->mark;
620a818f75eSJesus Sanchez-Palencia 	skb->tstamp = sockc->transmit_time;
6211da177e4SLinus Torvalds 
6221ced98e8SArnaldo Carvalho de Melo 	skb_put(skb, length);
6231ced98e8SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
6240660e03fSArnaldo Carvalho de Melo 	iph = ipv6_hdr(skb);
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	skb->ip_summed = CHECKSUM_NONE;
6271da177e4SLinus Torvalds 
6288f932f76SWillem de Bruijn 	skb_setup_tx_timestamp(skb, sockc->tsflags);
629fbfb2321SWillem de Bruijn 
6300dec879fSJulian Anastasov 	if (flags & MSG_CONFIRM)
6310dec879fSJulian Anastasov 		skb_set_dst_pending_confirm(skb, 1);
6320dec879fSJulian Anastasov 
633b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
63421226abbSAl Viro 	err = memcpy_from_msg(iph, msg, length);
635a688caa3SWei Wang 	if (err) {
636a688caa3SWei Wang 		err = -EFAULT;
637a688caa3SWei Wang 		kfree_skb(skb);
638a688caa3SWei Wang 		goto error;
639a688caa3SWei Wang 	}
640a688caa3SWei Wang 
641a688caa3SWei Wang 	skb_dst_set(skb, &rt->dst);
642a688caa3SWei Wang 	*dstp = NULL;
6431da177e4SLinus Torvalds 
644a8e3e1a9SDavid Ahern 	/* if egress device is enslaved to an L3 master device pass the
645a8e3e1a9SDavid Ahern 	 * skb to its handler for processing
646a8e3e1a9SDavid Ahern 	 */
647a8e3e1a9SDavid Ahern 	skb = l3mdev_ip6_out(sk, skb);
648a8e3e1a9SDavid Ahern 	if (unlikely(!skb))
649a8e3e1a9SDavid Ahern 		return 0;
650a8e3e1a9SDavid Ahern 
651a688caa3SWei Wang 	/* Acquire rcu_read_lock() in case we need to use rt->rt6i_idev
652a688caa3SWei Wang 	 * in the error path. Since skb has been freed, the dst could
653a688caa3SWei Wang 	 * have been queued for deletion.
654a688caa3SWei Wang 	 */
655a688caa3SWei Wang 	rcu_read_lock();
656adb28c9dSEric W. Biederman 	IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
65729a26a56SEric W. Biederman 	err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb,
65813206b6bSEric W. Biederman 		      NULL, rt->dst.dev, dst_output);
6591da177e4SLinus Torvalds 	if (err > 0)
6606ce9e7b5SEric Dumazet 		err = net_xmit_errno(err);
661a688caa3SWei Wang 	if (err) {
662a688caa3SWei Wang 		IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
663a688caa3SWei Wang 		rcu_read_unlock();
664a688caa3SWei Wang 		goto error_check;
665a688caa3SWei Wang 	}
666a688caa3SWei Wang 	rcu_read_unlock();
6671da177e4SLinus Torvalds out:
6681da177e4SLinus Torvalds 	return 0;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds error:
671adb28c9dSEric W. Biederman 	IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
672a688caa3SWei Wang error_check:
6736ce9e7b5SEric Dumazet 	if (err == -ENOBUFS && !np->recverr)
6746ce9e7b5SEric Dumazet 		err = 0;
6751da177e4SLinus Torvalds 	return err;
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
67819e3c66bSAl Viro struct raw6_frag_vec {
67919e3c66bSAl Viro 	struct msghdr *msg;
68019e3c66bSAl Viro 	int hlen;
68119e3c66bSAl Viro 	char c[4];
68219e3c66bSAl Viro };
68319e3c66bSAl Viro 
68419e3c66bSAl Viro static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6)
6851da177e4SLinus Torvalds {
68619e3c66bSAl Viro 	int err = 0;
6874c9483b2SDavid S. Miller 	switch (fl6->flowi6_proto) {
6881da177e4SLinus Torvalds 	case IPPROTO_ICMPV6:
68919e3c66bSAl Viro 		rfv->hlen = 2;
69019e3c66bSAl Viro 		err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
69119e3c66bSAl Viro 		if (!err) {
69219e3c66bSAl Viro 			fl6->fl6_icmp_type = rfv->c[0];
69319e3c66bSAl Viro 			fl6->fl6_icmp_code = rfv->c[1];
6941da177e4SLinus Torvalds 		}
6951da177e4SLinus Torvalds 		break;
6966e8f4d48SMasahide NAKAMURA 	case IPPROTO_MH:
69719e3c66bSAl Viro 		rfv->hlen = 4;
69819e3c66bSAl Viro 		err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
69919e3c66bSAl Viro 		if (!err)
70019e3c66bSAl Viro 			fl6->fl6_mh_type = rfv->c[2];
70119e3c66bSAl Viro 	}
70219e3c66bSAl Viro 	return err;
70319e3c66bSAl Viro }
7046e8f4d48SMasahide NAKAMURA 
70519e3c66bSAl Viro static int raw6_getfrag(void *from, char *to, int offset, int len, int odd,
70619e3c66bSAl Viro 		       struct sk_buff *skb)
70719e3c66bSAl Viro {
70819e3c66bSAl Viro 	struct raw6_frag_vec *rfv = from;
70919e3c66bSAl Viro 
71019e3c66bSAl Viro 	if (offset < rfv->hlen) {
71119e3c66bSAl Viro 		int copy = min(rfv->hlen - offset, len);
71219e3c66bSAl Viro 
71319e3c66bSAl Viro 		if (skb->ip_summed == CHECKSUM_PARTIAL)
71419e3c66bSAl Viro 			memcpy(to, rfv->c + offset, copy);
71519e3c66bSAl Viro 		else
71619e3c66bSAl Viro 			skb->csum = csum_block_add(
71719e3c66bSAl Viro 				skb->csum,
71819e3c66bSAl Viro 				csum_partial_copy_nocheck(rfv->c + offset,
719cc44c17bSAl Viro 							  to, copy),
72019e3c66bSAl Viro 				odd);
72119e3c66bSAl Viro 
72219e3c66bSAl Viro 		odd = 0;
72319e3c66bSAl Viro 		offset += copy;
72419e3c66bSAl Viro 		to += copy;
72519e3c66bSAl Viro 		len -= copy;
72619e3c66bSAl Viro 
72719e3c66bSAl Viro 		if (!len)
728a27b58feSHeiko Carstens 			return 0;
7291da177e4SLinus Torvalds 	}
7301da177e4SLinus Torvalds 
73119e3c66bSAl Viro 	offset -= rfv->hlen;
73219e3c66bSAl Viro 
733f69e6d13SAl Viro 	return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
73419e3c66bSAl Viro }
73519e3c66bSAl Viro 
7361b784140SYing Xue static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
7371da177e4SLinus Torvalds {
73845f6fad8SEric Dumazet 	struct ipv6_txoptions *opt_to_free = NULL;
7391da177e4SLinus Torvalds 	struct ipv6_txoptions opt_space;
740342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
74120c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
7421da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
7431da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
7441da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
7451da177e4SLinus Torvalds 	struct ipv6_txoptions *opt = NULL;
7461da177e4SLinus Torvalds 	struct ip6_flowlabel *flowlabel = NULL;
7471da177e4SLinus Torvalds 	struct dst_entry *dst = NULL;
74819e3c66bSAl Viro 	struct raw6_frag_vec rfv;
7494c9483b2SDavid S. Miller 	struct flowi6 fl6;
75026879da5SWei Wang 	struct ipcm6_cookie ipc6;
7511da177e4SLinus Torvalds 	int addr_len = msg->msg_namelen;
75259e3e4b5SOlivier Matz 	int hdrincl;
7531da177e4SLinus Torvalds 	u16 proto;
7541da177e4SLinus Torvalds 	int err;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	/* Rough check on arithmetic overflow,
757b59e139bSYOSHIFUJI Hideaki 	   better check is made in ip6_append_data().
7581da177e4SLinus Torvalds 	 */
759b59e139bSYOSHIFUJI Hideaki 	if (len > INT_MAX)
7601da177e4SLinus Torvalds 		return -EMSGSIZE;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	/* Mirror BSD error message compatibility */
7631da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
7641da177e4SLinus Torvalds 		return -EOPNOTSUPP;
7651da177e4SLinus Torvalds 
76659e3e4b5SOlivier Matz 	/* hdrincl should be READ_ONCE(inet->hdrincl)
76759e3e4b5SOlivier Matz 	 * but READ_ONCE() doesn't work with bit fields.
76859e3e4b5SOlivier Matz 	 * Doing this indirectly yields the same result.
76959e3e4b5SOlivier Matz 	 */
77059e3e4b5SOlivier Matz 	hdrincl = inet->hdrincl;
77159e3e4b5SOlivier Matz 	hdrincl = READ_ONCE(hdrincl);
77259e3e4b5SOlivier Matz 
7731da177e4SLinus Torvalds 	/*
7741da177e4SLinus Torvalds 	 *	Get and verify the address.
7751da177e4SLinus Torvalds 	 */
7764c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
7771da177e4SLinus Torvalds 
7784c9483b2SDavid S. Miller 	fl6.flowi6_mark = sk->sk_mark;
779e2d118a1SLorenzo Colitti 	fl6.flowi6_uid = sk->sk_uid;
7804a19ec58SLaszlo Attila Toth 
781b515430aSWillem de Bruijn 	ipcm6_init(&ipc6);
7825fdaa88dSWillem de Bruijn 	ipc6.sockc.tsflags = sk->sk_tsflags;
783c6af0c22SWillem de Bruijn 	ipc6.sockc.mark = sk->sk_mark;
78426879da5SWei Wang 
7851da177e4SLinus Torvalds 	if (sin6) {
7861da177e4SLinus Torvalds 		if (addr_len < SIN6_LEN_RFC2133)
7871da177e4SLinus Torvalds 			return -EINVAL;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 		if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
790a02cec21SEric Dumazet 			return -EAFNOSUPPORT;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 		/* port is the proto value [0..255] carried in nexthdr */
7931da177e4SLinus Torvalds 		proto = ntohs(sin6->sin6_port);
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 		if (!proto)
796c720c7e8SEric Dumazet 			proto = inet->inet_num;
797c720c7e8SEric Dumazet 		else if (proto != inet->inet_num)
798a02cec21SEric Dumazet 			return -EINVAL;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 		if (proto > 255)
801a02cec21SEric Dumazet 			return -EINVAL;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 		daddr = &sin6->sin6_addr;
8041da177e4SLinus Torvalds 		if (np->sndflow) {
8054c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
8064c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
8074c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
80859c820b2SWillem de Bruijn 				if (IS_ERR(flowlabel))
8091da177e4SLinus Torvalds 					return -EINVAL;
8101da177e4SLinus Torvalds 			}
8111da177e4SLinus Torvalds 		}
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 		/*
8141da177e4SLinus Torvalds 		 * Otherwise it will be difficult to maintain
8151da177e4SLinus Torvalds 		 * sk->sk_dst_cache.
8161da177e4SLinus Torvalds 		 */
8171da177e4SLinus Torvalds 		if (sk->sk_state == TCP_ESTABLISHED &&
818efe4208fSEric Dumazet 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
819efe4208fSEric Dumazet 			daddr = &sk->sk_v6_daddr;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 		if (addr_len >= sizeof(struct sockaddr_in6) &&
8221da177e4SLinus Torvalds 		    sin6->sin6_scope_id &&
823842df073SHannes Frederic Sowa 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
8244c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
8251da177e4SLinus Torvalds 	} else {
8261da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED)
8271da177e4SLinus Torvalds 			return -EDESTADDRREQ;
8281da177e4SLinus Torvalds 
829c720c7e8SEric Dumazet 		proto = inet->inet_num;
830efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
8314c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 
8344c9483b2SDavid S. Miller 	if (fl6.flowi6_oif == 0)
8354c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	if (msg->msg_controllen) {
8381da177e4SLinus Torvalds 		opt = &opt_space;
8391da177e4SLinus Torvalds 		memset(opt, 0, sizeof(struct ipv6_txoptions));
8401da177e4SLinus Torvalds 		opt->tot_len = sizeof(struct ipv6_txoptions);
84126879da5SWei Wang 		ipc6.opt = opt;
8421da177e4SLinus Torvalds 
8435fdaa88dSWillem de Bruijn 		err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6);
8441da177e4SLinus Torvalds 		if (err < 0) {
8451da177e4SLinus Torvalds 			fl6_sock_release(flowlabel);
8461da177e4SLinus Torvalds 			return err;
8471da177e4SLinus Torvalds 		}
8484c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
8494c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
85059c820b2SWillem de Bruijn 			if (IS_ERR(flowlabel))
8511da177e4SLinus Torvalds 				return -EINVAL;
8521da177e4SLinus Torvalds 		}
8531da177e4SLinus Torvalds 		if (!(opt->opt_nflen|opt->opt_flen))
8541da177e4SLinus Torvalds 			opt = NULL;
8551da177e4SLinus Torvalds 	}
85645f6fad8SEric Dumazet 	if (!opt) {
85745f6fad8SEric Dumazet 		opt = txopt_get(np);
85845f6fad8SEric Dumazet 		opt_to_free = opt;
85945f6fad8SEric Dumazet 	}
860df9890c3SYOSHIFUJI Hideaki 	if (flowlabel)
8611da177e4SLinus Torvalds 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
862df9890c3SYOSHIFUJI Hideaki 	opt = ipv6_fixup_options(&opt_space, opt);
8631da177e4SLinus Torvalds 
8644c9483b2SDavid S. Miller 	fl6.flowi6_proto = proto;
865c6af0c22SWillem de Bruijn 	fl6.flowi6_mark = ipc6.sockc.mark;
866b9aa52c4SOlivier Matz 
867b9aa52c4SOlivier Matz 	if (!hdrincl) {
86819e3c66bSAl Viro 		rfv.msg = msg;
86919e3c66bSAl Viro 		rfv.hlen = 0;
87019e3c66bSAl Viro 		err = rawv6_probe_proto_opt(&rfv, &fl6);
871a27b58feSHeiko Carstens 		if (err)
872a27b58feSHeiko Carstens 			goto out;
873b9aa52c4SOlivier Matz 	}
8741da177e4SLinus Torvalds 
875876c7f41SBrian Haley 	if (!ipv6_addr_any(daddr))
8764e3fd7a0SAlexey Dobriyan 		fl6.daddr = *daddr;
877876c7f41SBrian Haley 	else
8784c9483b2SDavid S. Miller 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
8794c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
8804e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
8811da177e4SLinus Torvalds 
8824c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
8831da177e4SLinus Torvalds 
8844c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
8854c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
886c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
887c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
8883df98d79SPaul Moore 	security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
8891da177e4SLinus Torvalds 
89059e3e4b5SOlivier Matz 	if (hdrincl)
89148e8aa6eSMartin KaFai Lau 		fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH;
89248e8aa6eSMartin KaFai Lau 
89338b7097bSHannes Frederic Sowa 	if (ipc6.tclass < 0)
89438b7097bSHannes Frederic Sowa 		ipc6.tclass = np->tclass;
89538b7097bSHannes Frederic Sowa 
89638b7097bSHannes Frederic Sowa 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
89738b7097bSHannes Frederic Sowa 
898c4e85f73SSabrina Dubroca 	dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
89968d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
90068d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
9011da177e4SLinus Torvalds 		goto out;
90214e50e57SDavid S. Miller 	}
90326879da5SWei Wang 	if (ipc6.hlimit < 0)
90426879da5SWei Wang 		ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
9051da177e4SLinus Torvalds 
90626879da5SWei Wang 	if (ipc6.dontfrag < 0)
90726879da5SWei Wang 		ipc6.dontfrag = np->dontfrag;
90813b52cd4SBrian Haley 
9091da177e4SLinus Torvalds 	if (msg->msg_flags&MSG_CONFIRM)
9101da177e4SLinus Torvalds 		goto do_confirm;
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds back_from_confirm:
91359e3e4b5SOlivier Matz 	if (hdrincl)
914a818f75eSJesus Sanchez-Palencia 		err = rawv6_send_hdrinc(sk, msg, len, &fl6, &dst,
9155fdaa88dSWillem de Bruijn 					msg->msg_flags, &ipc6.sockc);
9161789a640SEric Dumazet 	else {
91726879da5SWei Wang 		ipc6.opt = opt;
9181da177e4SLinus Torvalds 		lock_sock(sk);
91919e3c66bSAl Viro 		err = ip6_append_data(sk, raw6_getfrag, &rfv,
92026879da5SWei Wang 			len, 0, &ipc6, &fl6, (struct rt6_info *)dst,
9215fdaa88dSWillem de Bruijn 			msg->msg_flags);
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 		if (err)
9241da177e4SLinus Torvalds 			ip6_flush_pending_frames(sk);
9251da177e4SLinus Torvalds 		else if (!(msg->msg_flags & MSG_MORE))
9264c9483b2SDavid S. Miller 			err = rawv6_push_pending_frames(sk, &fl6, rp);
9273ef9d943SYOSHIFUJI Hideaki 		release_sock(sk);
9281da177e4SLinus Torvalds 	}
9291da177e4SLinus Torvalds done:
9306d3e85ecSNicolas DICHTEL 	dst_release(dst);
9311da177e4SLinus Torvalds out:
9321da177e4SLinus Torvalds 	fl6_sock_release(flowlabel);
93345f6fad8SEric Dumazet 	txopt_put(opt_to_free);
9341da177e4SLinus Torvalds 	return err < 0 ? err : len;
9351da177e4SLinus Torvalds do_confirm:
9360dec879fSJulian Anastasov 	if (msg->msg_flags & MSG_PROBE)
9370dec879fSJulian Anastasov 		dst_confirm_neigh(dst, &fl6.daddr);
9381da177e4SLinus Torvalds 	if (!(msg->msg_flags & MSG_PROBE) || len)
9391da177e4SLinus Torvalds 		goto back_from_confirm;
9401da177e4SLinus Torvalds 	err = 0;
9411da177e4SLinus Torvalds 	goto done;
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
945a7b75c5aSChristoph Hellwig 			       sockptr_t optval, int optlen)
9461da177e4SLinus Torvalds {
9471da177e4SLinus Torvalds 	switch (optname) {
9481da177e4SLinus Torvalds 	case ICMPV6_FILTER:
9491da177e4SLinus Torvalds 		if (optlen > sizeof(struct icmp6_filter))
9501da177e4SLinus Torvalds 			optlen = sizeof(struct icmp6_filter);
951a7b75c5aSChristoph Hellwig 		if (copy_from_sockptr(&raw6_sk(sk)->filter, optval, optlen))
9521da177e4SLinus Torvalds 			return -EFAULT;
9531da177e4SLinus Torvalds 		return 0;
9541da177e4SLinus Torvalds 	default:
9551da177e4SLinus Torvalds 		return -ENOPROTOOPT;
9563ff50b79SStephen Hemminger 	}
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	return 0;
9591da177e4SLinus Torvalds }
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
9621da177e4SLinus Torvalds 			       char __user *optval, int __user *optlen)
9631da177e4SLinus Torvalds {
9641da177e4SLinus Torvalds 	int len;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	switch (optname) {
9671da177e4SLinus Torvalds 	case ICMPV6_FILTER:
9681da177e4SLinus Torvalds 		if (get_user(len, optlen))
9691da177e4SLinus Torvalds 			return -EFAULT;
9701da177e4SLinus Torvalds 		if (len < 0)
9711da177e4SLinus Torvalds 			return -EINVAL;
9721da177e4SLinus Torvalds 		if (len > sizeof(struct icmp6_filter))
9731da177e4SLinus Torvalds 			len = sizeof(struct icmp6_filter);
9741da177e4SLinus Torvalds 		if (put_user(len, optlen))
9751da177e4SLinus Torvalds 			return -EFAULT;
9761da177e4SLinus Torvalds 		if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
9771da177e4SLinus Torvalds 			return -EFAULT;
9781da177e4SLinus Torvalds 		return 0;
9791da177e4SLinus Torvalds 	default:
9801da177e4SLinus Torvalds 		return -ENOPROTOOPT;
9813ff50b79SStephen Hemminger 	}
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	return 0;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 
9873fdadf7dSDmitry Mishin static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
988a7b75c5aSChristoph Hellwig 			       sockptr_t optval, unsigned int optlen)
9891da177e4SLinus Torvalds {
9901da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
9911da177e4SLinus Torvalds 	int val;
9921da177e4SLinus Torvalds 
993fb7bc920STamir Duberstein 	if (optlen < sizeof(val))
994fb7bc920STamir Duberstein 		return -EINVAL;
995fb7bc920STamir Duberstein 
996a7b75c5aSChristoph Hellwig 	if (copy_from_sockptr(&val, optval, sizeof(val)))
9971da177e4SLinus Torvalds 		return -EFAULT;
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	switch (optname) {
1000715f504bSHannes Frederic Sowa 	case IPV6_HDRINCL:
1001715f504bSHannes Frederic Sowa 		if (sk->sk_type != SOCK_RAW)
1002715f504bSHannes Frederic Sowa 			return -EINVAL;
1003715f504bSHannes Frederic Sowa 		inet_sk(sk)->hdrincl = !!val;
1004715f504bSHannes Frederic Sowa 		return 0;
10051da177e4SLinus Torvalds 	case IPV6_CHECKSUM:
1006c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 &&
10071a98d05fSYOSHIFUJI Hideaki 		    level == IPPROTO_IPV6) {
10081a98d05fSYOSHIFUJI Hideaki 			/*
10091a98d05fSYOSHIFUJI Hideaki 			 * RFC3542 tells that IPV6_CHECKSUM socket
10101a98d05fSYOSHIFUJI Hideaki 			 * option in the IPPROTO_IPV6 level is not
10111a98d05fSYOSHIFUJI Hideaki 			 * allowed on ICMPv6 sockets.
10121a98d05fSYOSHIFUJI Hideaki 			 * If you want to set it, use IPPROTO_RAW
10131a98d05fSYOSHIFUJI Hideaki 			 * level IPV6_CHECKSUM socket option
10141a98d05fSYOSHIFUJI Hideaki 			 * (Linux extension).
10151a98d05fSYOSHIFUJI Hideaki 			 */
10161a98d05fSYOSHIFUJI Hideaki 			return -EINVAL;
10171a98d05fSYOSHIFUJI Hideaki 		}
10181a98d05fSYOSHIFUJI Hideaki 
10191da177e4SLinus Torvalds 		/* You may get strange result with a positive odd offset;
10201da177e4SLinus Torvalds 		   RFC2292bis agrees with me. */
10211da177e4SLinus Torvalds 		if (val > 0 && (val&1))
1022a02cec21SEric Dumazet 			return -EINVAL;
10231da177e4SLinus Torvalds 		if (val < 0) {
10241da177e4SLinus Torvalds 			rp->checksum = 0;
10251da177e4SLinus Torvalds 		} else {
10261da177e4SLinus Torvalds 			rp->checksum = 1;
10271da177e4SLinus Torvalds 			rp->offset = val;
10281da177e4SLinus Torvalds 		}
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 		return 0;
10311da177e4SLinus Torvalds 
10321da177e4SLinus Torvalds 	default:
1033a02cec21SEric Dumazet 		return -ENOPROTOOPT;
10341da177e4SLinus Torvalds 	}
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
10373fdadf7dSDmitry Mishin static int rawv6_setsockopt(struct sock *sk, int level, int optname,
1038a7b75c5aSChristoph Hellwig 			    sockptr_t optval, unsigned int optlen)
10391da177e4SLinus Torvalds {
10401da177e4SLinus Torvalds 	switch (level) {
10411da177e4SLinus Torvalds 	case SOL_RAW:
10421da177e4SLinus Torvalds 		break;
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	case SOL_ICMPV6:
1045c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
10461da177e4SLinus Torvalds 			return -EOPNOTSUPP;
1047207ec0abSJoe Perches 		return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
10481da177e4SLinus Torvalds 	case SOL_IPV6:
1049715f504bSHannes Frederic Sowa 		if (optname == IPV6_CHECKSUM ||
1050715f504bSHannes Frederic Sowa 		    optname == IPV6_HDRINCL)
10511da177e4SLinus Torvalds 			break;
1052a8eceea8SJoe Perches 		fallthrough;
10531da177e4SLinus Torvalds 	default:
1054207ec0abSJoe Perches 		return ipv6_setsockopt(sk, level, optname, optval, optlen);
10553ff50b79SStephen Hemminger 	}
10563ff50b79SStephen Hemminger 
10573fdadf7dSDmitry Mishin 	return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
10583fdadf7dSDmitry Mishin }
10593fdadf7dSDmitry Mishin 
10603fdadf7dSDmitry Mishin static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
10613fdadf7dSDmitry Mishin 			    char __user *optval, int __user *optlen)
10623fdadf7dSDmitry Mishin {
10633fdadf7dSDmitry Mishin 	struct raw6_sock *rp = raw6_sk(sk);
10643fdadf7dSDmitry Mishin 	int val, len;
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	if (get_user(len, optlen))
10671da177e4SLinus Torvalds 		return -EFAULT;
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	switch (optname) {
1070715f504bSHannes Frederic Sowa 	case IPV6_HDRINCL:
1071715f504bSHannes Frederic Sowa 		val = inet_sk(sk)->hdrincl;
1072715f504bSHannes Frederic Sowa 		break;
10731da177e4SLinus Torvalds 	case IPV6_CHECKSUM:
10741a98d05fSYOSHIFUJI Hideaki 		/*
10751a98d05fSYOSHIFUJI Hideaki 		 * We allow getsockopt() for IPPROTO_IPV6-level
10761a98d05fSYOSHIFUJI Hideaki 		 * IPV6_CHECKSUM socket option on ICMPv6 sockets
10771a98d05fSYOSHIFUJI Hideaki 		 * since RFC3542 is silent about it.
10781a98d05fSYOSHIFUJI Hideaki 		 */
10791da177e4SLinus Torvalds 		if (rp->checksum == 0)
10801da177e4SLinus Torvalds 			val = -1;
10811da177e4SLinus Torvalds 		else
10821da177e4SLinus Torvalds 			val = rp->offset;
10831da177e4SLinus Torvalds 		break;
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	default:
10861da177e4SLinus Torvalds 		return -ENOPROTOOPT;
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	len = min_t(unsigned int, sizeof(int), len);
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	if (put_user(len, optlen))
10921da177e4SLinus Torvalds 		return -EFAULT;
10931da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, len))
10941da177e4SLinus Torvalds 		return -EFAULT;
10951da177e4SLinus Torvalds 	return 0;
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds 
10983fdadf7dSDmitry Mishin static int rawv6_getsockopt(struct sock *sk, int level, int optname,
10993fdadf7dSDmitry Mishin 			  char __user *optval, int __user *optlen)
11003fdadf7dSDmitry Mishin {
11013fdadf7dSDmitry Mishin 	switch (level) {
11023fdadf7dSDmitry Mishin 	case SOL_RAW:
11033fdadf7dSDmitry Mishin 		break;
11043fdadf7dSDmitry Mishin 
11053fdadf7dSDmitry Mishin 	case SOL_ICMPV6:
1106c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
11073fdadf7dSDmitry Mishin 			return -EOPNOTSUPP;
1108207ec0abSJoe Perches 		return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
11093fdadf7dSDmitry Mishin 	case SOL_IPV6:
1110715f504bSHannes Frederic Sowa 		if (optname == IPV6_CHECKSUM ||
1111715f504bSHannes Frederic Sowa 		    optname == IPV6_HDRINCL)
11123fdadf7dSDmitry Mishin 			break;
1113a8eceea8SJoe Perches 		fallthrough;
11143fdadf7dSDmitry Mishin 	default:
1115207ec0abSJoe Perches 		return ipv6_getsockopt(sk, level, optname, optval, optlen);
11163ff50b79SStephen Hemminger 	}
11173ff50b79SStephen Hemminger 
11183fdadf7dSDmitry Mishin 	return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
11193fdadf7dSDmitry Mishin }
11203fdadf7dSDmitry Mishin 
11211da177e4SLinus Torvalds static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
11221da177e4SLinus Torvalds {
11231da177e4SLinus Torvalds 	switch (cmd) {
1124207ec0abSJoe Perches 	case SIOCOUTQ: {
112531e6d363SEric Dumazet 		int amount = sk_wmem_alloc_get(sk);
112631e6d363SEric Dumazet 
11271da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
11281da177e4SLinus Torvalds 	}
1129207ec0abSJoe Perches 	case SIOCINQ: {
11301da177e4SLinus Torvalds 		struct sk_buff *skb;
11311da177e4SLinus Torvalds 		int amount = 0;
11321da177e4SLinus Torvalds 
1133e0f9f858SHerbert Xu 		spin_lock_bh(&sk->sk_receive_queue.lock);
11341da177e4SLinus Torvalds 		skb = skb_peek(&sk->sk_receive_queue);
113553b24b8fSIan Morris 		if (skb)
1136105f5528SJamie Bainbridge 			amount = skb->len;
1137e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_receive_queue.lock);
11381da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
11391da177e4SLinus Torvalds 	}
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	default:
11427bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
11437bc570c8SYOSHIFUJI Hideaki 		return ip6mr_ioctl(sk, cmd, (void __user *)arg);
11447bc570c8SYOSHIFUJI Hideaki #else
11451da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
11467bc570c8SYOSHIFUJI Hideaki #endif
11471da177e4SLinus Torvalds 	}
11481da177e4SLinus Torvalds }
11491da177e4SLinus Torvalds 
1150e2d57766SDavid S. Miller #ifdef CONFIG_COMPAT
1151e2d57766SDavid S. Miller static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
1152e2d57766SDavid S. Miller {
1153e2d57766SDavid S. Miller 	switch (cmd) {
1154e2d57766SDavid S. Miller 	case SIOCOUTQ:
1155e2d57766SDavid S. Miller 	case SIOCINQ:
1156e2d57766SDavid S. Miller 		return -ENOIOCTLCMD;
1157e2d57766SDavid S. Miller 	default:
1158e2d57766SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
1159e2d57766SDavid S. Miller 		return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg));
1160e2d57766SDavid S. Miller #else
1161e2d57766SDavid S. Miller 		return -ENOIOCTLCMD;
1162e2d57766SDavid S. Miller #endif
1163e2d57766SDavid S. Miller 	}
1164e2d57766SDavid S. Miller }
1165e2d57766SDavid S. Miller #endif
1166e2d57766SDavid S. Miller 
11671da177e4SLinus Torvalds static void rawv6_close(struct sock *sk, long timeout)
11681da177e4SLinus Torvalds {
1169c720c7e8SEric Dumazet 	if (inet_sk(sk)->inet_num == IPPROTO_RAW)
1170725a8ff0SDenis V. Lunev 		ip6_ra_control(sk, -1);
11717bc570c8SYOSHIFUJI Hideaki 	ip6mr_sk_done(sk);
11721da177e4SLinus Torvalds 	sk_common_release(sk);
11731da177e4SLinus Torvalds }
11741da177e4SLinus Torvalds 
11757d06b2e0SBrian Haley static void raw6_destroy(struct sock *sk)
117622dd4850SDenis V. Lunev {
117722dd4850SDenis V. Lunev 	lock_sock(sk);
117822dd4850SDenis V. Lunev 	ip6_flush_pending_frames(sk);
117922dd4850SDenis V. Lunev 	release_sock(sk);
118022dd4850SDenis V. Lunev }
118122dd4850SDenis V. Lunev 
11821da177e4SLinus Torvalds static int rawv6_init_sk(struct sock *sk)
11831da177e4SLinus Torvalds {
11841da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
1185f48d5ff1SMasahide NAKAMURA 
1186c720c7e8SEric Dumazet 	switch (inet_sk(sk)->inet_num) {
1187f48d5ff1SMasahide NAKAMURA 	case IPPROTO_ICMPV6:
11881da177e4SLinus Torvalds 		rp->checksum = 1;
11891da177e4SLinus Torvalds 		rp->offset   = 2;
1190f48d5ff1SMasahide NAKAMURA 		break;
1191f48d5ff1SMasahide NAKAMURA 	case IPPROTO_MH:
1192f48d5ff1SMasahide NAKAMURA 		rp->checksum = 1;
1193f48d5ff1SMasahide NAKAMURA 		rp->offset   = 4;
1194f48d5ff1SMasahide NAKAMURA 		break;
1195f48d5ff1SMasahide NAKAMURA 	default:
1196f48d5ff1SMasahide NAKAMURA 		break;
11971da177e4SLinus Torvalds 	}
1198a02cec21SEric Dumazet 	return 0;
11991da177e4SLinus Torvalds }
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds struct proto rawv6_prot = {
12021da177e4SLinus Torvalds 	.name		   = "RAWv6",
12031da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
12041da177e4SLinus Torvalds 	.close		   = rawv6_close,
120522dd4850SDenis V. Lunev 	.destroy	   = raw6_destroy,
120682b276cdSHannes Frederic Sowa 	.connect	   = ip6_datagram_connect_v6_only,
1207286c72deSEric Dumazet 	.disconnect	   = __udp_disconnect,
12081da177e4SLinus Torvalds 	.ioctl		   = rawv6_ioctl,
12091da177e4SLinus Torvalds 	.init		   = rawv6_init_sk,
12101da177e4SLinus Torvalds 	.setsockopt	   = rawv6_setsockopt,
12111da177e4SLinus Torvalds 	.getsockopt	   = rawv6_getsockopt,
12121da177e4SLinus Torvalds 	.sendmsg	   = rawv6_sendmsg,
12131da177e4SLinus Torvalds 	.recvmsg	   = rawv6_recvmsg,
12141da177e4SLinus Torvalds 	.bind		   = rawv6_bind,
12151da177e4SLinus Torvalds 	.backlog_rcv	   = rawv6_rcv_skb,
1216fc8717baSPavel Emelyanov 	.hash		   = raw_hash_sk,
1217fc8717baSPavel Emelyanov 	.unhash		   = raw_unhash_sk,
12181da177e4SLinus Torvalds 	.obj_size	   = sizeof(struct raw6_sock),
12198c2bc895SDavid Windsor 	.useroffset	   = offsetof(struct raw6_sock, filter),
12208c2bc895SDavid Windsor 	.usersize	   = sizeof_field(struct raw6_sock, filter),
1221fc8717baSPavel Emelyanov 	.h.raw_hash	   = &raw_v6_hashinfo,
1222543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
1223e2d57766SDavid S. Miller 	.compat_ioctl	   = compat_rawv6_ioctl,
1224543d9cfeSArnaldo Carvalho de Melo #endif
1225432490f9SCyrill Gorcunov 	.diag_destroy	   = raw_abort,
12261da177e4SLinus Torvalds };
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12291da177e4SLinus Torvalds static int raw6_seq_show(struct seq_file *seq, void *v)
12301da177e4SLinus Torvalds {
123117ef66afSLorenzo Colitti 	if (v == SEQ_START_TOKEN) {
123217ef66afSLorenzo Colitti 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
123317ef66afSLorenzo Colitti 	} else {
123417ef66afSLorenzo Colitti 		struct sock *sp = v;
123517ef66afSLorenzo Colitti 		__u16 srcp  = inet_sk(sp)->inet_num;
123617ef66afSLorenzo Colitti 		ip6_dgram_sock_seq_show(seq, v, srcp, 0,
123717ef66afSLorenzo Colitti 					raw_seq_private(seq)->bucket);
123817ef66afSLorenzo Colitti 	}
12391da177e4SLinus Torvalds 	return 0;
12401da177e4SLinus Torvalds }
12411da177e4SLinus Torvalds 
124256b3d975SPhilippe De Muyter static const struct seq_operations raw6_seq_ops = {
124342a73808SPavel Emelyanov 	.start =	raw_seq_start,
124442a73808SPavel Emelyanov 	.next =		raw_seq_next,
124542a73808SPavel Emelyanov 	.stop =		raw_seq_stop,
12461da177e4SLinus Torvalds 	.show =		raw6_seq_show,
12471da177e4SLinus Torvalds };
12481da177e4SLinus Torvalds 
12492c8c1e72SAlexey Dobriyan static int __net_init raw6_init_net(struct net *net)
1250a308da16SPavel Emelyanov {
1251c3506372SChristoph Hellwig 	if (!proc_create_net_data("raw6", 0444, net->proc_net, &raw6_seq_ops,
1252c3506372SChristoph Hellwig 			sizeof(struct raw_iter_state), &raw_v6_hashinfo))
1253a308da16SPavel Emelyanov 		return -ENOMEM;
1254a308da16SPavel Emelyanov 
1255a308da16SPavel Emelyanov 	return 0;
1256a308da16SPavel Emelyanov }
1257a308da16SPavel Emelyanov 
12582c8c1e72SAlexey Dobriyan static void __net_exit raw6_exit_net(struct net *net)
1259a308da16SPavel Emelyanov {
1260ece31ffdSGao feng 	remove_proc_entry("raw6", net->proc_net);
1261a308da16SPavel Emelyanov }
1262a308da16SPavel Emelyanov 
1263a308da16SPavel Emelyanov static struct pernet_operations raw6_net_ops = {
1264a308da16SPavel Emelyanov 	.init = raw6_init_net,
1265a308da16SPavel Emelyanov 	.exit = raw6_exit_net,
1266a308da16SPavel Emelyanov };
1267a308da16SPavel Emelyanov 
12681da177e4SLinus Torvalds int __init raw6_proc_init(void)
12691da177e4SLinus Torvalds {
1270a308da16SPavel Emelyanov 	return register_pernet_subsys(&raw6_net_ops);
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds void raw6_proc_exit(void)
12741da177e4SLinus Torvalds {
1275a308da16SPavel Emelyanov 	unregister_pernet_subsys(&raw6_net_ops);
12761da177e4SLinus Torvalds }
12771da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
12787f4e4868SDaniel Lezcano 
1279a11e1d43SLinus Torvalds /* Same as inet6_dgram_ops, sans udp_poll.  */
128077d4b1d3SEric Dumazet const struct proto_ops inet6_sockraw_ops = {
12817f4e4868SDaniel Lezcano 	.family		   = PF_INET6,
12827f4e4868SDaniel Lezcano 	.owner		   = THIS_MODULE,
12837f4e4868SDaniel Lezcano 	.release	   = inet6_release,
12847f4e4868SDaniel Lezcano 	.bind		   = inet6_bind,
12857f4e4868SDaniel Lezcano 	.connect	   = inet_dgram_connect,	/* ok		*/
12867f4e4868SDaniel Lezcano 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
12877f4e4868SDaniel Lezcano 	.accept		   = sock_no_accept,		/* a do nothing	*/
12887f4e4868SDaniel Lezcano 	.getname	   = inet6_getname,
1289a11e1d43SLinus Torvalds 	.poll		   = datagram_poll,		/* ok		*/
12907f4e4868SDaniel Lezcano 	.ioctl		   = inet6_ioctl,		/* must change  */
1291c7cbdbf2SArnd Bergmann 	.gettstamp	   = sock_gettstamp,
12927f4e4868SDaniel Lezcano 	.listen		   = sock_no_listen,		/* ok		*/
12937f4e4868SDaniel Lezcano 	.shutdown	   = inet_shutdown,		/* ok		*/
12947f4e4868SDaniel Lezcano 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
12957f4e4868SDaniel Lezcano 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
12967f4e4868SDaniel Lezcano 	.sendmsg	   = inet_sendmsg,		/* ok		*/
12977f4e4868SDaniel Lezcano 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
12987f4e4868SDaniel Lezcano 	.mmap		   = sock_no_mmap,
12997f4e4868SDaniel Lezcano 	.sendpage	   = sock_no_sendpage,
13007f4e4868SDaniel Lezcano #ifdef CONFIG_COMPAT
13013986912fSChristoph Hellwig 	.compat_ioctl	   = inet6_compat_ioctl,
13027f4e4868SDaniel Lezcano #endif
13037f4e4868SDaniel Lezcano };
13047f4e4868SDaniel Lezcano 
13057f4e4868SDaniel Lezcano static struct inet_protosw rawv6_protosw = {
13067f4e4868SDaniel Lezcano 	.type		= SOCK_RAW,
13077f4e4868SDaniel Lezcano 	.protocol	= IPPROTO_IP,	/* wild card */
13087f4e4868SDaniel Lezcano 	.prot		= &rawv6_prot,
13097f4e4868SDaniel Lezcano 	.ops		= &inet6_sockraw_ops,
13107f4e4868SDaniel Lezcano 	.flags		= INET_PROTOSW_REUSE,
13117f4e4868SDaniel Lezcano };
13127f4e4868SDaniel Lezcano 
13137f4e4868SDaniel Lezcano int __init rawv6_init(void)
13147f4e4868SDaniel Lezcano {
13153d2f6d41SJulia Lawall 	return inet6_register_protosw(&rawv6_protosw);
13167f4e4868SDaniel Lezcano }
13177f4e4868SDaniel Lezcano 
131809f7709fSDaniel Lezcano void rawv6_exit(void)
13197f4e4868SDaniel Lezcano {
13207f4e4868SDaniel Lezcano 	inet6_unregister_protosw(&rawv6_protosw);
13217f4e4868SDaniel Lezcano }
1322