xref: /openbmc/linux/net/ipv6/raw.c (revision 19e3c66b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	RAW sockets for IPv6
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	Adapted from linux/net/ipv4/raw.c
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *	Fixes:
111da177e4SLinus Torvalds  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
121da177e4SLinus Torvalds  *	YOSHIFUJI,H.@USAGI	:	raw checksum (RFC2292(bis) compliance)
131da177e4SLinus Torvalds  *	Kazunori MIYAZAWA @USAGI:	change process style to use ip6_append_data
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
161da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
171da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
181da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <linux/errno.h>
221da177e4SLinus Torvalds #include <linux/types.h>
231da177e4SLinus Torvalds #include <linux/socket.h>
245a0e3ad6STejun Heo #include <linux/slab.h>
251da177e4SLinus Torvalds #include <linux/sockios.h>
261da177e4SLinus Torvalds #include <linux/net.h>
271da177e4SLinus Torvalds #include <linux/in6.h>
281da177e4SLinus Torvalds #include <linux/netdevice.h>
291da177e4SLinus Torvalds #include <linux/if_arp.h>
301da177e4SLinus Torvalds #include <linux/icmpv6.h>
311da177e4SLinus Torvalds #include <linux/netfilter.h>
321da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
333305b80cSHerbert Xu #include <linux/skbuff.h>
34e2d57766SDavid S. Miller #include <linux/compat.h>
351da177e4SLinus Torvalds #include <asm/uaccess.h>
361da177e4SLinus Torvalds #include <asm/ioctls.h>
371da177e4SLinus Torvalds 
38457c4cbcSEric W. Biederman #include <net/net_namespace.h>
391da177e4SLinus Torvalds #include <net/ip.h>
401da177e4SLinus Torvalds #include <net/sock.h>
411da177e4SLinus Torvalds #include <net/snmp.h>
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include <net/ipv6.h>
441da177e4SLinus Torvalds #include <net/ndisc.h>
451da177e4SLinus Torvalds #include <net/protocol.h>
461da177e4SLinus Torvalds #include <net/ip6_route.h>
471da177e4SLinus Torvalds #include <net/ip6_checksum.h>
481da177e4SLinus Torvalds #include <net/addrconf.h>
491da177e4SLinus Torvalds #include <net/transp_v6.h>
501da177e4SLinus Torvalds #include <net/udp.h>
511da177e4SLinus Torvalds #include <net/inet_common.h>
52c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
5307a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
547be96f76SMasahide NAKAMURA #include <net/mip6.h>
557be96f76SMasahide NAKAMURA #endif
567bc570c8SYOSHIFUJI Hideaki #include <linux/mroute6.h>
571da177e4SLinus Torvalds 
58b673e4dfSPavel Emelyanov #include <net/raw.h>
591da177e4SLinus Torvalds #include <net/rawv6.h>
601da177e4SLinus Torvalds #include <net/xfrm.h>
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #include <linux/proc_fs.h>
631da177e4SLinus Torvalds #include <linux/seq_file.h>
64bc3b2d7fSPaul Gortmaker #include <linux/export.h>
651da177e4SLinus Torvalds 
66d1c53c8eSWerner Almesberger #define	ICMPV6_HDRLEN	4	/* ICMPv6 header, RFC 4443 Section 2.1 */
67d1c53c8eSWerner Almesberger 
68b673e4dfSPavel Emelyanov static struct raw_hashinfo raw_v6_hashinfo = {
69938b93adSRobert P. J. Day 	.lock = __RW_LOCK_UNLOCKED(raw_v6_hashinfo.lock),
70b673e4dfSPavel Emelyanov };
711da177e4SLinus Torvalds 
72be185884SPavel Emelyanov static struct sock *__raw_v6_lookup(struct net *net, struct sock *sk,
73b71d1d42SEric Dumazet 		unsigned short num, const struct in6_addr *loc_addr,
74b71d1d42SEric Dumazet 		const struct in6_addr *rmt_addr, int dif)
751da177e4SLinus Torvalds {
76a50feda5SEric Dumazet 	bool is_multicast = ipv6_addr_is_multicast(loc_addr);
771da177e4SLinus Torvalds 
78b67bfe0dSSasha Levin 	sk_for_each_from(sk)
79c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num == num) {
801da177e4SLinus Torvalds 
81878628fbSYOSHIFUJI Hideaki 			if (!net_eq(sock_net(sk), net))
82be185884SPavel Emelyanov 				continue;
83be185884SPavel Emelyanov 
84efe4208fSEric Dumazet 			if (!ipv6_addr_any(&sk->sk_v6_daddr) &&
85efe4208fSEric Dumazet 			    !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr))
861da177e4SLinus Torvalds 				continue;
871da177e4SLinus Torvalds 
880bd1b59bSAndrew McDonald 			if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
890bd1b59bSAndrew McDonald 				continue;
900bd1b59bSAndrew McDonald 
91efe4208fSEric Dumazet 			if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
92efe4208fSEric Dumazet 				if (ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))
931da177e4SLinus Torvalds 					goto found;
941da177e4SLinus Torvalds 				if (is_multicast &&
951da177e4SLinus Torvalds 				    inet6_mc_check(sk, loc_addr, rmt_addr))
961da177e4SLinus Torvalds 					goto found;
971da177e4SLinus Torvalds 				continue;
981da177e4SLinus Torvalds 			}
991da177e4SLinus Torvalds 			goto found;
1001da177e4SLinus Torvalds 		}
1011da177e4SLinus Torvalds 	sk = NULL;
1021da177e4SLinus Torvalds found:
1031da177e4SLinus Torvalds 	return sk;
1041da177e4SLinus Torvalds }
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /*
1071da177e4SLinus Torvalds  *	0 - deliver
1081da177e4SLinus Torvalds  *	1 - block
1091da177e4SLinus Torvalds  */
1101b05c4b5SEric Dumazet static int icmpv6_filter(const struct sock *sk, const struct sk_buff *skb)
1111da177e4SLinus Torvalds {
1129cc08af3SWerner Almesberger 	struct icmp6hdr _hdr;
1131b05c4b5SEric Dumazet 	const struct icmp6hdr *hdr;
1141da177e4SLinus Torvalds 
115d1c53c8eSWerner Almesberger 	/* We require only the four bytes of the ICMPv6 header, not any
116d1c53c8eSWerner Almesberger 	 * additional bytes of message body in "struct icmp6hdr".
117d1c53c8eSWerner Almesberger 	 */
1181b05c4b5SEric Dumazet 	hdr = skb_header_pointer(skb, skb_transport_offset(skb),
119d1c53c8eSWerner Almesberger 				 ICMPV6_HDRLEN, &_hdr);
1201b05c4b5SEric Dumazet 	if (hdr) {
1211b05c4b5SEric Dumazet 		const __u32 *data = &raw6_sk(sk)->filter.data[0];
1221b05c4b5SEric Dumazet 		unsigned int type = hdr->icmp6_type;
1231da177e4SLinus Torvalds 
1241b05c4b5SEric Dumazet 		return (data[type >> 5] & (1U << (type & 31))) != 0;
1251da177e4SLinus Torvalds 	}
1261b05c4b5SEric Dumazet 	return 1;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
12907a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
130f2eda47dSEric Dumazet typedef int mh_filter_t(struct sock *sock, struct sk_buff *skb);
13159fbb3a6SMasahide NAKAMURA 
132f2eda47dSEric Dumazet static mh_filter_t __rcu *mh_filter __read_mostly;
133f2eda47dSEric Dumazet 
134f2eda47dSEric Dumazet int rawv6_mh_filter_register(mh_filter_t filter)
13559fbb3a6SMasahide NAKAMURA {
136cf778b00SEric Dumazet 	rcu_assign_pointer(mh_filter, filter);
13759fbb3a6SMasahide NAKAMURA 	return 0;
13859fbb3a6SMasahide NAKAMURA }
13959fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_register);
14059fbb3a6SMasahide NAKAMURA 
141f2eda47dSEric Dumazet int rawv6_mh_filter_unregister(mh_filter_t filter)
14259fbb3a6SMasahide NAKAMURA {
143a9b3cd7fSStephen Hemminger 	RCU_INIT_POINTER(mh_filter, NULL);
14459fbb3a6SMasahide NAKAMURA 	synchronize_rcu();
14559fbb3a6SMasahide NAKAMURA 	return 0;
14659fbb3a6SMasahide NAKAMURA }
14759fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_unregister);
14859fbb3a6SMasahide NAKAMURA 
14959fbb3a6SMasahide NAKAMURA #endif
15059fbb3a6SMasahide NAKAMURA 
1511da177e4SLinus Torvalds /*
1521da177e4SLinus Torvalds  *	demultiplex raw sockets.
1531da177e4SLinus Torvalds  *	(should consider queueing the skb in the sock receive_queue
1541da177e4SLinus Torvalds  *	without calling rawv6.c)
1551da177e4SLinus Torvalds  *
1561da177e4SLinus Torvalds  *	Caller owns SKB so we must make clones.
1571da177e4SLinus Torvalds  */
158a50feda5SEric Dumazet static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
1591da177e4SLinus Torvalds {
160b71d1d42SEric Dumazet 	const struct in6_addr *saddr;
161b71d1d42SEric Dumazet 	const struct in6_addr *daddr;
1621da177e4SLinus Torvalds 	struct sock *sk;
163a50feda5SEric Dumazet 	bool delivered = false;
1641da177e4SLinus Torvalds 	__u8 hash;
165be185884SPavel Emelyanov 	struct net *net;
1661da177e4SLinus Torvalds 
1670660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->saddr;
1681da177e4SLinus Torvalds 	daddr = saddr + 1;
1691da177e4SLinus Torvalds 
170f9242b6bSDavid S. Miller 	hash = nexthdr & (RAW_HTABLE_SIZE - 1);
1711da177e4SLinus Torvalds 
172b673e4dfSPavel Emelyanov 	read_lock(&raw_v6_hashinfo.lock);
173b673e4dfSPavel Emelyanov 	sk = sk_head(&raw_v6_hashinfo.ht[hash]);
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	if (sk == NULL)
1761da177e4SLinus Torvalds 		goto out;
1771da177e4SLinus Torvalds 
178c346dca1SYOSHIFUJI Hideaki 	net = dev_net(skb->dev);
1794330487aSDuan Jiong 	sk = __raw_v6_lookup(net, sk, nexthdr, daddr, saddr, inet6_iif(skb));
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	while (sk) {
1827be96f76SMasahide NAKAMURA 		int filtered;
1837be96f76SMasahide NAKAMURA 
184a50feda5SEric Dumazet 		delivered = true;
1857be96f76SMasahide NAKAMURA 		switch (nexthdr) {
1867be96f76SMasahide NAKAMURA 		case IPPROTO_ICMPV6:
1877be96f76SMasahide NAKAMURA 			filtered = icmpv6_filter(sk, skb);
1887be96f76SMasahide NAKAMURA 			break;
18959fbb3a6SMasahide NAKAMURA 
19007a93626SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6_MIP6)
1917be96f76SMasahide NAKAMURA 		case IPPROTO_MH:
19259fbb3a6SMasahide NAKAMURA 		{
1937be96f76SMasahide NAKAMURA 			/* XXX: To validate MH only once for each packet,
1947be96f76SMasahide NAKAMURA 			 * this is placed here. It should be after checking
1957be96f76SMasahide NAKAMURA 			 * xfrm policy, however it doesn't. The checking xfrm
1967be96f76SMasahide NAKAMURA 			 * policy is placed in rawv6_rcv() because it is
1977be96f76SMasahide NAKAMURA 			 * required for each socket.
1987be96f76SMasahide NAKAMURA 			 */
199f2eda47dSEric Dumazet 			mh_filter_t *filter;
20059fbb3a6SMasahide NAKAMURA 
20159fbb3a6SMasahide NAKAMURA 			filter = rcu_dereference(mh_filter);
202f2eda47dSEric Dumazet 			filtered = filter ? (*filter)(sk, skb) : 0;
2037be96f76SMasahide NAKAMURA 			break;
20459fbb3a6SMasahide NAKAMURA 		}
2057be96f76SMasahide NAKAMURA #endif
2067be96f76SMasahide NAKAMURA 		default:
2077be96f76SMasahide NAKAMURA 			filtered = 0;
2087be96f76SMasahide NAKAMURA 			break;
2097be96f76SMasahide NAKAMURA 		}
2107be96f76SMasahide NAKAMURA 
2117be96f76SMasahide NAKAMURA 		if (filtered < 0)
2127be96f76SMasahide NAKAMURA 			break;
2137be96f76SMasahide NAKAMURA 		if (filtered == 0) {
2141da177e4SLinus Torvalds 			struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 			/* Not releasing hash table! */
2179fb9cbb1SYasuyuki Kozakai 			if (clone) {
2189fb9cbb1SYasuyuki Kozakai 				nf_reset(clone);
2191da177e4SLinus Torvalds 				rawv6_rcv(sk, clone);
2201da177e4SLinus Torvalds 			}
2219fb9cbb1SYasuyuki Kozakai 		}
222be185884SPavel Emelyanov 		sk = __raw_v6_lookup(net, sk_next(sk), nexthdr, daddr, saddr,
2234330487aSDuan Jiong 				     inet6_iif(skb));
2241da177e4SLinus Torvalds 	}
2251da177e4SLinus Torvalds out:
226b673e4dfSPavel Emelyanov 	read_unlock(&raw_v6_hashinfo.lock);
227d13964f4SPatrick McHardy 	return delivered;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
230a50feda5SEric Dumazet bool raw6_local_deliver(struct sk_buff *skb, int nexthdr)
23169d6da0bSPavel Emelyanov {
23269d6da0bSPavel Emelyanov 	struct sock *raw_sk;
23369d6da0bSPavel Emelyanov 
234f9242b6bSDavid S. Miller 	raw_sk = sk_head(&raw_v6_hashinfo.ht[nexthdr & (RAW_HTABLE_SIZE - 1)]);
23569d6da0bSPavel Emelyanov 	if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
23669d6da0bSPavel Emelyanov 		raw_sk = NULL;
23769d6da0bSPavel Emelyanov 
23869d6da0bSPavel Emelyanov 	return raw_sk != NULL;
23969d6da0bSPavel Emelyanov }
24069d6da0bSPavel Emelyanov 
2411da177e4SLinus Torvalds /* This cleans up af_inet6 a bit. -DaveM */
2421da177e4SLinus Torvalds static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2451da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
2461da177e4SLinus Torvalds 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
247e69a4adcSAl Viro 	__be32 v4addr = 0;
2481da177e4SLinus Torvalds 	int addr_type;
2491da177e4SLinus Torvalds 	int err;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	if (addr_len < SIN6_LEN_RFC2133)
2521da177e4SLinus Torvalds 		return -EINVAL;
25382b276cdSHannes Frederic Sowa 
25482b276cdSHannes Frederic Sowa 	if (addr->sin6_family != AF_INET6)
25582b276cdSHannes Frederic Sowa 		return -EINVAL;
25682b276cdSHannes Frederic Sowa 
2571da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&addr->sin6_addr);
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	/* Raw sockets are IPv6 only */
2601da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED)
261fd5c0027SEric Dumazet 		return -EADDRNOTAVAIL;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	lock_sock(sk);
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	err = -EINVAL;
2661da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE)
2671da177e4SLinus Torvalds 		goto out;
2681da177e4SLinus Torvalds 
269fd5c0027SEric Dumazet 	rcu_read_lock();
2701da177e4SLinus Torvalds 	/* Check if the address belongs to the host. */
2711da177e4SLinus Torvalds 	if (addr_type != IPV6_ADDR_ANY) {
2721da177e4SLinus Torvalds 		struct net_device *dev = NULL;
2731da177e4SLinus Torvalds 
274842df073SHannes Frederic Sowa 		if (__ipv6_addr_needs_scope_id(addr_type)) {
2751da177e4SLinus Torvalds 			if (addr_len >= sizeof(struct sockaddr_in6) &&
2761da177e4SLinus Torvalds 			    addr->sin6_scope_id) {
2771da177e4SLinus Torvalds 				/* Override any existing binding, if another
2781da177e4SLinus Torvalds 				 * one is supplied by user.
2791da177e4SLinus Torvalds 				 */
2801da177e4SLinus Torvalds 				sk->sk_bound_dev_if = addr->sin6_scope_id;
2811da177e4SLinus Torvalds 			}
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 			/* Binding to link-local address requires an interface */
2841da177e4SLinus Torvalds 			if (!sk->sk_bound_dev_if)
285fd5c0027SEric Dumazet 				goto out_unlock;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 			err = -ENODEV;
288fd5c0027SEric Dumazet 			dev = dev_get_by_index_rcu(sock_net(sk),
289fd5c0027SEric Dumazet 						   sk->sk_bound_dev_if);
290fd5c0027SEric Dumazet 			if (!dev)
291fd5c0027SEric Dumazet 				goto out_unlock;
2921da177e4SLinus Torvalds 		}
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 		/* ipv4 addr of the socket is invalid.  Only the
2951da177e4SLinus Torvalds 		 * unspecified and mapped address have a v4 equivalent.
2961da177e4SLinus Torvalds 		 */
2971da177e4SLinus Torvalds 		v4addr = LOOPBACK4_IPV6;
2981da177e4SLinus Torvalds 		if (!(addr_type & IPV6_ADDR_MULTICAST))	{
2991da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
3003b1e0a65SYOSHIFUJI Hideaki 			if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr,
301bfeade08SDaniel Lezcano 					   dev, 0)) {
302fd5c0027SEric Dumazet 				goto out_unlock;
3031da177e4SLinus Torvalds 			}
3041da177e4SLinus Torvalds 		}
3051da177e4SLinus Torvalds 	}
3061da177e4SLinus Torvalds 
307c720c7e8SEric Dumazet 	inet->inet_rcv_saddr = inet->inet_saddr = v4addr;
308efe4208fSEric Dumazet 	sk->sk_v6_rcv_saddr = addr->sin6_addr;
3091da177e4SLinus Torvalds 	if (!(addr_type & IPV6_ADDR_MULTICAST))
3104e3fd7a0SAlexey Dobriyan 		np->saddr = addr->sin6_addr;
3111da177e4SLinus Torvalds 	err = 0;
312fd5c0027SEric Dumazet out_unlock:
313fd5c0027SEric Dumazet 	rcu_read_unlock();
3141da177e4SLinus Torvalds out:
3151da177e4SLinus Torvalds 	release_sock(sk);
3161da177e4SLinus Torvalds 	return err;
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
31969d6da0bSPavel Emelyanov static void rawv6_err(struct sock *sk, struct sk_buff *skb,
3201da177e4SLinus Torvalds 	       struct inet6_skb_parm *opt,
321d5fdd6baSBrian Haley 	       u8 type, u8 code, int offset, __be32 info)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
3241da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
3251da177e4SLinus Torvalds 	int err;
3261da177e4SLinus Torvalds 	int harderr;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	/* Report error on raw socket, if:
3291da177e4SLinus Torvalds 	   1. User requested recverr.
3301da177e4SLinus Torvalds 	   2. Socket is connected (otherwise the error indication
3311da177e4SLinus Torvalds 	      is useless without recverr and error is hard.
3321da177e4SLinus Torvalds 	 */
3331da177e4SLinus Torvalds 	if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
3341da177e4SLinus Torvalds 		return;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	harderr = icmpv6_err_convert(type, code, &err);
33781aded24SDavid S. Miller 	if (type == ICMPV6_PKT_TOOBIG) {
33881aded24SDavid S. Miller 		ip6_sk_update_pmtu(skb, sk, info);
3391da177e4SLinus Torvalds 		harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
34081aded24SDavid S. Miller 	}
3418d65b119SDuan Jiong 	if (type == NDISC_REDIRECT) {
342ec18d9a2SDavid S. Miller 		ip6_sk_redirect(skb, sk);
3438d65b119SDuan Jiong 		return;
3448d65b119SDuan Jiong 	}
3451da177e4SLinus Torvalds 	if (np->recverr) {
3461da177e4SLinus Torvalds 		u8 *payload = skb->data;
3471da177e4SLinus Torvalds 		if (!inet->hdrincl)
3481da177e4SLinus Torvalds 			payload += offset;
3491da177e4SLinus Torvalds 		ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	if (np->recverr || harderr) {
3531da177e4SLinus Torvalds 		sk->sk_err = err;
3541da177e4SLinus Torvalds 		sk->sk_error_report(sk);
3551da177e4SLinus Torvalds 	}
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
35869d6da0bSPavel Emelyanov void raw6_icmp_error(struct sk_buff *skb, int nexthdr,
359d5fdd6baSBrian Haley 		u8 type, u8 code, int inner_offset, __be32 info)
36069d6da0bSPavel Emelyanov {
36169d6da0bSPavel Emelyanov 	struct sock *sk;
36269d6da0bSPavel Emelyanov 	int hash;
363b71d1d42SEric Dumazet 	const struct in6_addr *saddr, *daddr;
364be185884SPavel Emelyanov 	struct net *net;
36569d6da0bSPavel Emelyanov 
366b673e4dfSPavel Emelyanov 	hash = nexthdr & (RAW_HTABLE_SIZE - 1);
36769d6da0bSPavel Emelyanov 
368b673e4dfSPavel Emelyanov 	read_lock(&raw_v6_hashinfo.lock);
369b673e4dfSPavel Emelyanov 	sk = sk_head(&raw_v6_hashinfo.ht[hash]);
37069d6da0bSPavel Emelyanov 	if (sk != NULL) {
37105f175cdSYOSHIFUJI Hideaki 		/* Note: ipv6_hdr(skb) != skb->data */
372b71d1d42SEric Dumazet 		const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data;
37305f175cdSYOSHIFUJI Hideaki 		saddr = &ip6h->saddr;
37405f175cdSYOSHIFUJI Hideaki 		daddr = &ip6h->daddr;
375c346dca1SYOSHIFUJI Hideaki 		net = dev_net(skb->dev);
37669d6da0bSPavel Emelyanov 
377be185884SPavel Emelyanov 		while ((sk = __raw_v6_lookup(net, sk, nexthdr, saddr, daddr,
3784330487aSDuan Jiong 						inet6_iif(skb)))) {
37969d6da0bSPavel Emelyanov 			rawv6_err(sk, skb, NULL, type, code,
38069d6da0bSPavel Emelyanov 					inner_offset, info);
38169d6da0bSPavel Emelyanov 			sk = sk_next(sk);
38269d6da0bSPavel Emelyanov 		}
38369d6da0bSPavel Emelyanov 	}
384b673e4dfSPavel Emelyanov 	read_unlock(&raw_v6_hashinfo.lock);
38569d6da0bSPavel Emelyanov }
38669d6da0bSPavel Emelyanov 
3871da177e4SLinus Torvalds static inline int rawv6_rcv_skb(struct sock *sk, struct sk_buff *skb)
3881da177e4SLinus Torvalds {
38933d480ceSEric Dumazet 	if ((raw6_sk(sk)->checksum || rcu_access_pointer(sk->sk_filter)) &&
390fb286bb2SHerbert Xu 	    skb_checksum_complete(skb)) {
391a92aa318SWang Chen 		atomic_inc(&sk->sk_drops);
3921da177e4SLinus Torvalds 		kfree_skb(skb);
3933cc76caaSYang Hongyang 		return NET_RX_DROP;
3941da177e4SLinus Torvalds 	}
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	/* Charge it to the socket. */
397d826eb14SEric Dumazet 	skb_dst_drop(skb);
398d826eb14SEric Dumazet 	if (sock_queue_rcv_skb(sk, skb) < 0) {
3991da177e4SLinus Torvalds 		kfree_skb(skb);
4003cc76caaSYang Hongyang 		return NET_RX_DROP;
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	return 0;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds /*
4071da177e4SLinus Torvalds  *	This is next to useless...
4081da177e4SLinus Torvalds  *	if we demultiplex in network layer we don't need the extra call
4091da177e4SLinus Torvalds  *	just to queue the skb...
4101da177e4SLinus Torvalds  *	maybe we could have the network decide upon a hint if it
4111da177e4SLinus Torvalds  *	should call raw_rcv for demultiplexing
4121da177e4SLinus Torvalds  */
4131da177e4SLinus Torvalds int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4161da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
419a92aa318SWang Chen 		atomic_inc(&sk->sk_drops);
4201da177e4SLinus Torvalds 		kfree_skb(skb);
4211da177e4SLinus Torvalds 		return NET_RX_DROP;
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	if (!rp->checksum)
4251da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_UNNECESSARY;
4261da177e4SLinus Torvalds 
42784fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
428d56f90a7SArnaldo Carvalho de Melo 		skb_postpull_rcsum(skb, skb_network_header(skb),
429cfe1fc77SArnaldo Carvalho de Melo 				   skb_network_header_len(skb));
4300660e03fSArnaldo Carvalho de Melo 		if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4310660e03fSArnaldo Carvalho de Melo 				     &ipv6_hdr(skb)->daddr,
432c720c7e8SEric Dumazet 				     skb->len, inet->inet_num, skb->csum))
433fb286bb2SHerbert Xu 			skb->ip_summed = CHECKSUM_UNNECESSARY;
4341da177e4SLinus Torvalds 	}
43560476372SHerbert Xu 	if (!skb_csum_unnecessary(skb))
4360660e03fSArnaldo Carvalho de Melo 		skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4370660e03fSArnaldo Carvalho de Melo 							 &ipv6_hdr(skb)->daddr,
4380660e03fSArnaldo Carvalho de Melo 							 skb->len,
439c720c7e8SEric Dumazet 							 inet->inet_num, 0));
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	if (inet->hdrincl) {
442fb286bb2SHerbert Xu 		if (skb_checksum_complete(skb)) {
443a92aa318SWang Chen 			atomic_inc(&sk->sk_drops);
4441da177e4SLinus Torvalds 			kfree_skb(skb);
4453cc76caaSYang Hongyang 			return NET_RX_DROP;
4461da177e4SLinus Torvalds 		}
4471da177e4SLinus Torvalds 	}
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	rawv6_rcv_skb(sk, skb);
4501da177e4SLinus Torvalds 	return 0;
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds /*
4551da177e4SLinus Torvalds  *	This should be easy, if there is something there
4561da177e4SLinus Torvalds  *	we return it, otherwise we block.
4571da177e4SLinus Torvalds  */
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk,
4601da177e4SLinus Torvalds 		  struct msghdr *msg, size_t len,
4611da177e4SLinus Torvalds 		  int noblock, int flags, int *addr_len)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
464342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
4651da177e4SLinus Torvalds 	struct sk_buff *skb;
4661da177e4SLinus Torvalds 	size_t copied;
4671da177e4SLinus Torvalds 	int err;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	if (flags & MSG_OOB)
4701da177e4SLinus Torvalds 		return -EOPNOTSUPP;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	if (flags & MSG_ERRQUEUE)
47385fbaa75SHannes Frederic Sowa 		return ipv6_recv_error(sk, msg, len, addr_len);
4741da177e4SLinus Torvalds 
4754b340ae2SBrian Haley 	if (np->rxpmtu && np->rxopt.bits.rxpmtu)
47685fbaa75SHannes Frederic Sowa 		return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
4774b340ae2SBrian Haley 
4781da177e4SLinus Torvalds 	skb = skb_recv_datagram(sk, flags, noblock, &err);
4791da177e4SLinus Torvalds 	if (!skb)
4801da177e4SLinus Torvalds 		goto out;
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	copied = skb->len;
4831da177e4SLinus Torvalds 	if (copied > len) {
4841da177e4SLinus Torvalds 		copied = len;
4851da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
4861da177e4SLinus Torvalds 	}
4871da177e4SLinus Torvalds 
48860476372SHerbert Xu 	if (skb_csum_unnecessary(skb)) {
48951f3d02bSDavid S. Miller 		err = skb_copy_datagram_msg(skb, 0, msg, copied);
4901da177e4SLinus Torvalds 	} else if (msg->msg_flags&MSG_TRUNC) {
491fb286bb2SHerbert Xu 		if (__skb_checksum_complete(skb))
4921da177e4SLinus Torvalds 			goto csum_copy_err;
49351f3d02bSDavid S. Miller 		err = skb_copy_datagram_msg(skb, 0, msg, copied);
4941da177e4SLinus Torvalds 	} else {
495227158dbSAl Viro 		err = skb_copy_and_csum_datagram_msg(skb, 0, msg);
4961da177e4SLinus Torvalds 		if (err == -EINVAL)
4971da177e4SLinus Torvalds 			goto csum_copy_err;
4981da177e4SLinus Torvalds 	}
4991da177e4SLinus Torvalds 	if (err)
5001da177e4SLinus Torvalds 		goto out_free;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	/* Copy the address. */
5031da177e4SLinus Torvalds 	if (sin6) {
5041da177e4SLinus Torvalds 		sin6->sin6_family = AF_INET6;
505f59fc7f3STetsuo Handa 		sin6->sin6_port = 0;
5064e3fd7a0SAlexey Dobriyan 		sin6->sin6_addr = ipv6_hdr(skb)->saddr;
5071da177e4SLinus Torvalds 		sin6->sin6_flowinfo = 0;
508842df073SHannes Frederic Sowa 		sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
5094330487aSDuan Jiong 							  inet6_iif(skb));
510bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin6);
5111da177e4SLinus Torvalds 	}
5121da177e4SLinus Torvalds 
5133b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	if (np->rxopt.all)
51673df66f8STom Parkin 		ip6_datagram_recv_ctl(sk, msg, skb);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	err = copied;
5191da177e4SLinus Torvalds 	if (flags & MSG_TRUNC)
5201da177e4SLinus Torvalds 		err = skb->len;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds out_free:
5231da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
5241da177e4SLinus Torvalds out:
5251da177e4SLinus Torvalds 	return err;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds csum_copy_err:
5283305b80cSHerbert Xu 	skb_kill_datagram(sk, skb, flags);
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	/* Error for blocking case is chosen to masquerade
5311da177e4SLinus Torvalds 	   as some normal condition.
5321da177e4SLinus Torvalds 	 */
5331da177e4SLinus Torvalds 	err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
5343305b80cSHerbert Xu 	goto out;
5351da177e4SLinus Torvalds }
5361da177e4SLinus Torvalds 
5374c9483b2SDavid S. Miller static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
538357b40a1SHerbert Xu 				     struct raw6_sock *rp)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds 	struct sk_buff *skb;
5411da177e4SLinus Torvalds 	int err = 0;
542357b40a1SHerbert Xu 	int offset;
543357b40a1SHerbert Xu 	int len;
544679a8738SHerbert Xu 	int total_len;
545868c86bcSAl Viro 	__wsum tmp_csum;
546868c86bcSAl Viro 	__sum16 csum;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	if (!rp->checksum)
5491da177e4SLinus Torvalds 		goto send;
5501da177e4SLinus Torvalds 
55143728fa5SFabian Frederick 	skb = skb_peek(&sk->sk_write_queue);
55243728fa5SFabian Frederick 	if (!skb)
5531da177e4SLinus Torvalds 		goto out;
5541da177e4SLinus Torvalds 
555357b40a1SHerbert Xu 	offset = rp->offset;
556299b0767SSteffen Klassert 	total_len = inet_sk(sk)->cork.base.length;
557679a8738SHerbert Xu 	if (offset >= total_len - 1) {
5581da177e4SLinus Torvalds 		err = -EINVAL;
559357b40a1SHerbert Xu 		ip6_flush_pending_frames(sk);
5601da177e4SLinus Torvalds 		goto out;
5611da177e4SLinus Torvalds 	}
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	/* should be check HW csum miyazawa */
5641da177e4SLinus Torvalds 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
5651da177e4SLinus Torvalds 		/*
5661da177e4SLinus Torvalds 		 * Only one fragment on the socket.
5671da177e4SLinus Torvalds 		 */
5681da177e4SLinus Torvalds 		tmp_csum = skb->csum;
5691da177e4SLinus Torvalds 	} else {
570357b40a1SHerbert Xu 		struct sk_buff *csum_skb = NULL;
5711da177e4SLinus Torvalds 		tmp_csum = 0;
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 		skb_queue_walk(&sk->sk_write_queue, skb) {
5741da177e4SLinus Torvalds 			tmp_csum = csum_add(tmp_csum, skb->csum);
575357b40a1SHerbert Xu 
576357b40a1SHerbert Xu 			if (csum_skb)
577357b40a1SHerbert Xu 				continue;
578357b40a1SHerbert Xu 
579ea2ae17dSArnaldo Carvalho de Melo 			len = skb->len - skb_transport_offset(skb);
580357b40a1SHerbert Xu 			if (offset >= len) {
581357b40a1SHerbert Xu 				offset -= len;
582357b40a1SHerbert Xu 				continue;
5831da177e4SLinus Torvalds 			}
584357b40a1SHerbert Xu 
585357b40a1SHerbert Xu 			csum_skb = skb;
5861da177e4SLinus Torvalds 		}
5871da177e4SLinus Torvalds 
588357b40a1SHerbert Xu 		skb = csum_skb;
589357b40a1SHerbert Xu 	}
590357b40a1SHerbert Xu 
591ea2ae17dSArnaldo Carvalho de Melo 	offset += skb_transport_offset(skb);
5928242fc33SHimangi Saraogi 	BUG_ON(skb_copy_bits(skb, offset, &csum, 2));
593357b40a1SHerbert Xu 
5941da177e4SLinus Torvalds 	/* in case cksum was not initialized */
595357b40a1SHerbert Xu 	if (unlikely(csum))
5965f92a738SAl Viro 		tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
5971da177e4SLinus Torvalds 
5984c9483b2SDavid S. Miller 	csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
5994c9483b2SDavid S. Miller 			       total_len, fl6->flowi6_proto, tmp_csum);
6001da177e4SLinus Torvalds 
6014c9483b2SDavid S. Miller 	if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP)
602f6ab0288SAl Viro 		csum = CSUM_MANGLED_0;
603357b40a1SHerbert Xu 
6048242fc33SHimangi Saraogi 	BUG_ON(skb_store_bits(skb, offset, &csum, 2));
605357b40a1SHerbert Xu 
6061da177e4SLinus Torvalds send:
6071da177e4SLinus Torvalds 	err = ip6_push_pending_frames(sk);
6081da177e4SLinus Torvalds out:
6091da177e4SLinus Torvalds 	return err;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds static int rawv6_send_hdrinc(struct sock *sk, void *from, int length,
6134c9483b2SDavid S. Miller 			struct flowi6 *fl6, struct dst_entry **dstp,
6141da177e4SLinus Torvalds 			unsigned int flags)
6151da177e4SLinus Torvalds {
6163320da89SHerbert Xu 	struct ipv6_pinfo *np = inet6_sk(sk);
6171da177e4SLinus Torvalds 	struct ipv6hdr *iph;
6181da177e4SLinus Torvalds 	struct sk_buff *skb;
6191da177e4SLinus Torvalds 	int err;
6201789a640SEric Dumazet 	struct rt6_info *rt = (struct rt6_info *)*dstp;
621a7ae1992SHerbert Xu 	int hlen = LL_RESERVED_SPACE(rt->dst.dev);
622a7ae1992SHerbert Xu 	int tlen = rt->dst.dev->needed_tailroom;
6231da177e4SLinus Torvalds 
624d8d1f30bSChangli Gao 	if (length > rt->dst.dev->mtu) {
6254c9483b2SDavid S. Miller 		ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu);
6261da177e4SLinus Torvalds 		return -EMSGSIZE;
6271da177e4SLinus Torvalds 	}
6281da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
6291da177e4SLinus Torvalds 		goto out;
6301da177e4SLinus Torvalds 
631f5184d26SJohannes Berg 	skb = sock_alloc_send_skb(sk,
632a7ae1992SHerbert Xu 				  length + hlen + tlen + 15,
6331da177e4SLinus Torvalds 				  flags & MSG_DONTWAIT, &err);
6341da177e4SLinus Torvalds 	if (skb == NULL)
6351da177e4SLinus Torvalds 		goto error;
636a7ae1992SHerbert Xu 	skb_reserve(skb, hlen);
6371da177e4SLinus Torvalds 
6389c9c9ad5SHannes Frederic Sowa 	skb->protocol = htons(ETH_P_IPV6);
6391da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
6404a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
641d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
6421789a640SEric Dumazet 	*dstp = NULL;
6431da177e4SLinus Torvalds 
6441ced98e8SArnaldo Carvalho de Melo 	skb_put(skb, length);
6451ced98e8SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
6460660e03fSArnaldo Carvalho de Melo 	iph = ipv6_hdr(skb);
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	skb->ip_summed = CHECKSUM_NONE;
6491da177e4SLinus Torvalds 
650b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
6511da177e4SLinus Torvalds 	err = memcpy_fromiovecend((void *)iph, from, 0, length);
6521da177e4SLinus Torvalds 	if (err)
6531da177e4SLinus Torvalds 		goto error_fault;
6541da177e4SLinus Torvalds 
655edf391ffSNeil Horman 	IP6_UPD_PO_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
656b2e0b385SJan Engelhardt 	err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
657d8d1f30bSChangli Gao 		      rt->dst.dev, dst_output);
6581da177e4SLinus Torvalds 	if (err > 0)
6596ce9e7b5SEric Dumazet 		err = net_xmit_errno(err);
6601da177e4SLinus Torvalds 	if (err)
6611da177e4SLinus Torvalds 		goto error;
6621da177e4SLinus Torvalds out:
6631da177e4SLinus Torvalds 	return 0;
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds error_fault:
6661da177e4SLinus Torvalds 	err = -EFAULT;
6671da177e4SLinus Torvalds 	kfree_skb(skb);
6681da177e4SLinus Torvalds error:
6693bd653c8SDenis V. Lunev 	IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
6706ce9e7b5SEric Dumazet 	if (err == -ENOBUFS && !np->recverr)
6716ce9e7b5SEric Dumazet 		err = 0;
6721da177e4SLinus Torvalds 	return err;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds 
67519e3c66bSAl Viro struct raw6_frag_vec {
67619e3c66bSAl Viro 	struct msghdr *msg;
67719e3c66bSAl Viro 	int hlen;
67819e3c66bSAl Viro 	char c[4];
67919e3c66bSAl Viro };
68019e3c66bSAl Viro 
68119e3c66bSAl Viro static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6)
6821da177e4SLinus Torvalds {
68319e3c66bSAl Viro 	int err = 0;
6844c9483b2SDavid S. Miller 	switch (fl6->flowi6_proto) {
6851da177e4SLinus Torvalds 	case IPPROTO_ICMPV6:
68619e3c66bSAl Viro 		rfv->hlen = 2;
68719e3c66bSAl Viro 		err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
68819e3c66bSAl Viro 		if (!err) {
68919e3c66bSAl Viro 			fl6->fl6_icmp_type = rfv->c[0];
69019e3c66bSAl Viro 			fl6->fl6_icmp_code = rfv->c[1];
6911da177e4SLinus Torvalds 		}
6921da177e4SLinus Torvalds 		break;
6936e8f4d48SMasahide NAKAMURA 	case IPPROTO_MH:
69419e3c66bSAl Viro 		rfv->hlen = 4;
69519e3c66bSAl Viro 		err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
69619e3c66bSAl Viro 		if (!err)
69719e3c66bSAl Viro 			fl6->fl6_mh_type = rfv->c[2];
69819e3c66bSAl Viro 	}
69919e3c66bSAl Viro 	return err;
70019e3c66bSAl Viro }
7016e8f4d48SMasahide NAKAMURA 
70219e3c66bSAl Viro static int raw6_getfrag(void *from, char *to, int offset, int len, int odd,
70319e3c66bSAl Viro 		       struct sk_buff *skb)
70419e3c66bSAl Viro {
70519e3c66bSAl Viro 	struct raw6_frag_vec *rfv = from;
70619e3c66bSAl Viro 
70719e3c66bSAl Viro 	if (offset < rfv->hlen) {
70819e3c66bSAl Viro 		int copy = min(rfv->hlen - offset, len);
70919e3c66bSAl Viro 
71019e3c66bSAl Viro 		if (skb->ip_summed == CHECKSUM_PARTIAL)
71119e3c66bSAl Viro 			memcpy(to, rfv->c + offset, copy);
71219e3c66bSAl Viro 		else
71319e3c66bSAl Viro 			skb->csum = csum_block_add(
71419e3c66bSAl Viro 				skb->csum,
71519e3c66bSAl Viro 				csum_partial_copy_nocheck(rfv->c + offset,
71619e3c66bSAl Viro 							  to, copy, 0),
71719e3c66bSAl Viro 				odd);
71819e3c66bSAl Viro 
71919e3c66bSAl Viro 		odd = 0;
72019e3c66bSAl Viro 		offset += copy;
72119e3c66bSAl Viro 		to += copy;
72219e3c66bSAl Viro 		len -= copy;
72319e3c66bSAl Viro 
72419e3c66bSAl Viro 		if (!len)
725a27b58feSHeiko Carstens 			return 0;
7261da177e4SLinus Torvalds 	}
7271da177e4SLinus Torvalds 
72819e3c66bSAl Viro 	offset -= rfv->hlen;
72919e3c66bSAl Viro 
73019e3c66bSAl Viro 	return ip_generic_getfrag(rfv->msg->msg_iov, to, offset, len, odd, skb);
73119e3c66bSAl Viro }
73219e3c66bSAl Viro 
7331da177e4SLinus Torvalds static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
7341da177e4SLinus Torvalds 		   struct msghdr *msg, size_t len)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds 	struct ipv6_txoptions opt_space;
737342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
73820c59de2SArnaud Ebalard 	struct in6_addr *daddr, *final_p, final;
7391da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
7401da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
7411da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
7421da177e4SLinus Torvalds 	struct ipv6_txoptions *opt = NULL;
7431da177e4SLinus Torvalds 	struct ip6_flowlabel *flowlabel = NULL;
7441da177e4SLinus Torvalds 	struct dst_entry *dst = NULL;
74519e3c66bSAl Viro 	struct raw6_frag_vec rfv;
7464c9483b2SDavid S. Miller 	struct flowi6 fl6;
7471da177e4SLinus Torvalds 	int addr_len = msg->msg_namelen;
7481da177e4SLinus Torvalds 	int hlimit = -1;
74941a1f8eaSYOSHIFUJI Hideaki 	int tclass = -1;
75013b52cd4SBrian Haley 	int dontfrag = -1;
7511da177e4SLinus Torvalds 	u16 proto;
7521da177e4SLinus Torvalds 	int err;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	/* Rough check on arithmetic overflow,
755b59e139bSYOSHIFUJI Hideaki 	   better check is made in ip6_append_data().
7561da177e4SLinus Torvalds 	 */
757b59e139bSYOSHIFUJI Hideaki 	if (len > INT_MAX)
7581da177e4SLinus Torvalds 		return -EMSGSIZE;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	/* Mirror BSD error message compatibility */
7611da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
7621da177e4SLinus Torvalds 		return -EOPNOTSUPP;
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	/*
7651da177e4SLinus Torvalds 	 *	Get and verify the address.
7661da177e4SLinus Torvalds 	 */
7674c9483b2SDavid S. Miller 	memset(&fl6, 0, sizeof(fl6));
7681da177e4SLinus Torvalds 
7694c9483b2SDavid S. Miller 	fl6.flowi6_mark = sk->sk_mark;
7704a19ec58SLaszlo Attila Toth 
7711da177e4SLinus Torvalds 	if (sin6) {
7721da177e4SLinus Torvalds 		if (addr_len < SIN6_LEN_RFC2133)
7731da177e4SLinus Torvalds 			return -EINVAL;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 		if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
776a02cec21SEric Dumazet 			return -EAFNOSUPPORT;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 		/* port is the proto value [0..255] carried in nexthdr */
7791da177e4SLinus Torvalds 		proto = ntohs(sin6->sin6_port);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 		if (!proto)
782c720c7e8SEric Dumazet 			proto = inet->inet_num;
783c720c7e8SEric Dumazet 		else if (proto != inet->inet_num)
784a02cec21SEric Dumazet 			return -EINVAL;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 		if (proto > 255)
787a02cec21SEric Dumazet 			return -EINVAL;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 		daddr = &sin6->sin6_addr;
7901da177e4SLinus Torvalds 		if (np->sndflow) {
7914c9483b2SDavid S. Miller 			fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
7924c9483b2SDavid S. Miller 			if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
7934c9483b2SDavid S. Miller 				flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
7941da177e4SLinus Torvalds 				if (flowlabel == NULL)
7951da177e4SLinus Torvalds 					return -EINVAL;
7961da177e4SLinus Torvalds 			}
7971da177e4SLinus Torvalds 		}
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 		/*
8001da177e4SLinus Torvalds 		 * Otherwise it will be difficult to maintain
8011da177e4SLinus Torvalds 		 * sk->sk_dst_cache.
8021da177e4SLinus Torvalds 		 */
8031da177e4SLinus Torvalds 		if (sk->sk_state == TCP_ESTABLISHED &&
804efe4208fSEric Dumazet 		    ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
805efe4208fSEric Dumazet 			daddr = &sk->sk_v6_daddr;
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 		if (addr_len >= sizeof(struct sockaddr_in6) &&
8081da177e4SLinus Torvalds 		    sin6->sin6_scope_id &&
809842df073SHannes Frederic Sowa 		    __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
8104c9483b2SDavid S. Miller 			fl6.flowi6_oif = sin6->sin6_scope_id;
8111da177e4SLinus Torvalds 	} else {
8121da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED)
8131da177e4SLinus Torvalds 			return -EDESTADDRREQ;
8141da177e4SLinus Torvalds 
815c720c7e8SEric Dumazet 		proto = inet->inet_num;
816efe4208fSEric Dumazet 		daddr = &sk->sk_v6_daddr;
8174c9483b2SDavid S. Miller 		fl6.flowlabel = np->flow_label;
8181da177e4SLinus Torvalds 	}
8191da177e4SLinus Torvalds 
8204c9483b2SDavid S. Miller 	if (fl6.flowi6_oif == 0)
8214c9483b2SDavid S. Miller 		fl6.flowi6_oif = sk->sk_bound_dev_if;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	if (msg->msg_controllen) {
8241da177e4SLinus Torvalds 		opt = &opt_space;
8251da177e4SLinus Torvalds 		memset(opt, 0, sizeof(struct ipv6_txoptions));
8261da177e4SLinus Torvalds 		opt->tot_len = sizeof(struct ipv6_txoptions);
8271da177e4SLinus Torvalds 
82873df66f8STom Parkin 		err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
829ec0506dbSMaciej Żenczykowski 					    &hlimit, &tclass, &dontfrag);
8301da177e4SLinus Torvalds 		if (err < 0) {
8311da177e4SLinus Torvalds 			fl6_sock_release(flowlabel);
8321da177e4SLinus Torvalds 			return err;
8331da177e4SLinus Torvalds 		}
8344c9483b2SDavid S. Miller 		if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
8354c9483b2SDavid S. Miller 			flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
8361da177e4SLinus Torvalds 			if (flowlabel == NULL)
8371da177e4SLinus Torvalds 				return -EINVAL;
8381da177e4SLinus Torvalds 		}
8391da177e4SLinus Torvalds 		if (!(opt->opt_nflen|opt->opt_flen))
8401da177e4SLinus Torvalds 			opt = NULL;
8411da177e4SLinus Torvalds 	}
8421da177e4SLinus Torvalds 	if (opt == NULL)
8431da177e4SLinus Torvalds 		opt = np->opt;
844df9890c3SYOSHIFUJI Hideaki 	if (flowlabel)
8451da177e4SLinus Torvalds 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
846df9890c3SYOSHIFUJI Hideaki 	opt = ipv6_fixup_options(&opt_space, opt);
8471da177e4SLinus Torvalds 
8484c9483b2SDavid S. Miller 	fl6.flowi6_proto = proto;
84919e3c66bSAl Viro 	rfv.msg = msg;
85019e3c66bSAl Viro 	rfv.hlen = 0;
85119e3c66bSAl Viro 	err = rawv6_probe_proto_opt(&rfv, &fl6);
852a27b58feSHeiko Carstens 	if (err)
853a27b58feSHeiko Carstens 		goto out;
8541da177e4SLinus Torvalds 
855876c7f41SBrian Haley 	if (!ipv6_addr_any(daddr))
8564e3fd7a0SAlexey Dobriyan 		fl6.daddr = *daddr;
857876c7f41SBrian Haley 	else
8584c9483b2SDavid S. Miller 		fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
8594c9483b2SDavid S. Miller 	if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
8604e3fd7a0SAlexey Dobriyan 		fl6.saddr = np->saddr;
8611da177e4SLinus Torvalds 
8624c9483b2SDavid S. Miller 	final_p = fl6_update_dst(&fl6, opt, &final);
8631da177e4SLinus Torvalds 
8644c9483b2SDavid S. Miller 	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
8654c9483b2SDavid S. Miller 		fl6.flowi6_oif = np->mcast_oif;
866c4062dfcSErich E. Hoover 	else if (!fl6.flowi6_oif)
867c4062dfcSErich E. Hoover 		fl6.flowi6_oif = np->ucast_oif;
8684c9483b2SDavid S. Miller 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
8691da177e4SLinus Torvalds 
8700e0d44abSSteffen Klassert 	dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
87168d0c6d3SDavid S. Miller 	if (IS_ERR(dst)) {
87268d0c6d3SDavid S. Miller 		err = PTR_ERR(dst);
8731da177e4SLinus Torvalds 		goto out;
87414e50e57SDavid S. Miller 	}
8751da177e4SLinus Torvalds 	if (hlimit < 0)
8765c98631cSLorenzo Colitti 		hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
8771da177e4SLinus Torvalds 
87841a1f8eaSYOSHIFUJI Hideaki 	if (tclass < 0)
879e651f03aSGerrit Renker 		tclass = np->tclass;
88041a1f8eaSYOSHIFUJI Hideaki 
88113b52cd4SBrian Haley 	if (dontfrag < 0)
88213b52cd4SBrian Haley 		dontfrag = np->dontfrag;
88313b52cd4SBrian Haley 
8841da177e4SLinus Torvalds 	if (msg->msg_flags&MSG_CONFIRM)
8851da177e4SLinus Torvalds 		goto do_confirm;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds back_from_confirm:
8881789a640SEric Dumazet 	if (inet->hdrincl)
8894c9483b2SDavid S. Miller 		err = rawv6_send_hdrinc(sk, msg->msg_iov, len, &fl6, &dst, msg->msg_flags);
8901789a640SEric Dumazet 	else {
8911da177e4SLinus Torvalds 		lock_sock(sk);
89219e3c66bSAl Viro 		err = ip6_append_data(sk, raw6_getfrag, &rfv,
8934c9483b2SDavid S. Miller 			len, 0, hlimit, tclass, opt, &fl6, (struct rt6_info *)dst,
89413b52cd4SBrian Haley 			msg->msg_flags, dontfrag);
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 		if (err)
8971da177e4SLinus Torvalds 			ip6_flush_pending_frames(sk);
8981da177e4SLinus Torvalds 		else if (!(msg->msg_flags & MSG_MORE))
8994c9483b2SDavid S. Miller 			err = rawv6_push_pending_frames(sk, &fl6, rp);
9003ef9d943SYOSHIFUJI Hideaki 		release_sock(sk);
9011da177e4SLinus Torvalds 	}
9021da177e4SLinus Torvalds done:
9036d3e85ecSNicolas DICHTEL 	dst_release(dst);
9041da177e4SLinus Torvalds out:
9051da177e4SLinus Torvalds 	fl6_sock_release(flowlabel);
9061da177e4SLinus Torvalds 	return err < 0 ? err : len;
9071da177e4SLinus Torvalds do_confirm:
9081da177e4SLinus Torvalds 	dst_confirm(dst);
9091da177e4SLinus Torvalds 	if (!(msg->msg_flags & MSG_PROBE) || len)
9101da177e4SLinus Torvalds 		goto back_from_confirm;
9111da177e4SLinus Torvalds 	err = 0;
9121da177e4SLinus Torvalds 	goto done;
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
9161da177e4SLinus Torvalds 			       char __user *optval, int optlen)
9171da177e4SLinus Torvalds {
9181da177e4SLinus Torvalds 	switch (optname) {
9191da177e4SLinus Torvalds 	case ICMPV6_FILTER:
9201da177e4SLinus Torvalds 		if (optlen > sizeof(struct icmp6_filter))
9211da177e4SLinus Torvalds 			optlen = sizeof(struct icmp6_filter);
9221da177e4SLinus Torvalds 		if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
9231da177e4SLinus Torvalds 			return -EFAULT;
9241da177e4SLinus Torvalds 		return 0;
9251da177e4SLinus Torvalds 	default:
9261da177e4SLinus Torvalds 		return -ENOPROTOOPT;
9273ff50b79SStephen Hemminger 	}
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	return 0;
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
9331da177e4SLinus Torvalds 			       char __user *optval, int __user *optlen)
9341da177e4SLinus Torvalds {
9351da177e4SLinus Torvalds 	int len;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	switch (optname) {
9381da177e4SLinus Torvalds 	case ICMPV6_FILTER:
9391da177e4SLinus Torvalds 		if (get_user(len, optlen))
9401da177e4SLinus Torvalds 			return -EFAULT;
9411da177e4SLinus Torvalds 		if (len < 0)
9421da177e4SLinus Torvalds 			return -EINVAL;
9431da177e4SLinus Torvalds 		if (len > sizeof(struct icmp6_filter))
9441da177e4SLinus Torvalds 			len = sizeof(struct icmp6_filter);
9451da177e4SLinus Torvalds 		if (put_user(len, optlen))
9461da177e4SLinus Torvalds 			return -EFAULT;
9471da177e4SLinus Torvalds 		if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
9481da177e4SLinus Torvalds 			return -EFAULT;
9491da177e4SLinus Torvalds 		return 0;
9501da177e4SLinus Torvalds 	default:
9511da177e4SLinus Torvalds 		return -ENOPROTOOPT;
9523ff50b79SStephen Hemminger 	}
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	return 0;
9551da177e4SLinus Torvalds }
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 
9583fdadf7dSDmitry Mishin static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
959b7058842SDavid S. Miller 			    char __user *optval, unsigned int optlen)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
9621da177e4SLinus Torvalds 	int val;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	if (get_user(val, (int __user *)optval))
9651da177e4SLinus Torvalds 		return -EFAULT;
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	switch (optname) {
9681da177e4SLinus Torvalds 	case IPV6_CHECKSUM:
969c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 &&
9701a98d05fSYOSHIFUJI Hideaki 		    level == IPPROTO_IPV6) {
9711a98d05fSYOSHIFUJI Hideaki 			/*
9721a98d05fSYOSHIFUJI Hideaki 			 * RFC3542 tells that IPV6_CHECKSUM socket
9731a98d05fSYOSHIFUJI Hideaki 			 * option in the IPPROTO_IPV6 level is not
9741a98d05fSYOSHIFUJI Hideaki 			 * allowed on ICMPv6 sockets.
9751a98d05fSYOSHIFUJI Hideaki 			 * If you want to set it, use IPPROTO_RAW
9761a98d05fSYOSHIFUJI Hideaki 			 * level IPV6_CHECKSUM socket option
9771a98d05fSYOSHIFUJI Hideaki 			 * (Linux extension).
9781a98d05fSYOSHIFUJI Hideaki 			 */
9791a98d05fSYOSHIFUJI Hideaki 			return -EINVAL;
9801a98d05fSYOSHIFUJI Hideaki 		}
9811a98d05fSYOSHIFUJI Hideaki 
9821da177e4SLinus Torvalds 		/* You may get strange result with a positive odd offset;
9831da177e4SLinus Torvalds 		   RFC2292bis agrees with me. */
9841da177e4SLinus Torvalds 		if (val > 0 && (val&1))
985a02cec21SEric Dumazet 			return -EINVAL;
9861da177e4SLinus Torvalds 		if (val < 0) {
9871da177e4SLinus Torvalds 			rp->checksum = 0;
9881da177e4SLinus Torvalds 		} else {
9891da177e4SLinus Torvalds 			rp->checksum = 1;
9901da177e4SLinus Torvalds 			rp->offset = val;
9911da177e4SLinus Torvalds 		}
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 		return 0;
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	default:
996a02cec21SEric Dumazet 		return -ENOPROTOOPT;
9971da177e4SLinus Torvalds 	}
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
10003fdadf7dSDmitry Mishin static int rawv6_setsockopt(struct sock *sk, int level, int optname,
1001b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
10021da177e4SLinus Torvalds {
10031da177e4SLinus Torvalds 	switch (level) {
10041da177e4SLinus Torvalds 	case SOL_RAW:
10051da177e4SLinus Torvalds 		break;
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	case SOL_ICMPV6:
1008c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
10091da177e4SLinus Torvalds 			return -EOPNOTSUPP;
1010207ec0abSJoe Perches 		return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
10111da177e4SLinus Torvalds 	case SOL_IPV6:
10121da177e4SLinus Torvalds 		if (optname == IPV6_CHECKSUM)
10131da177e4SLinus Torvalds 			break;
10141da177e4SLinus Torvalds 	default:
1015207ec0abSJoe Perches 		return ipv6_setsockopt(sk, level, optname, optval, optlen);
10163ff50b79SStephen Hemminger 	}
10173ff50b79SStephen Hemminger 
10183fdadf7dSDmitry Mishin 	return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
10193fdadf7dSDmitry Mishin }
10203fdadf7dSDmitry Mishin 
10213fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
10223fdadf7dSDmitry Mishin static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname,
1023b7058842SDavid S. Miller 				   char __user *optval, unsigned int optlen)
10243fdadf7dSDmitry Mishin {
10253fdadf7dSDmitry Mishin 	switch (level) {
10263fdadf7dSDmitry Mishin 	case SOL_RAW:
10273fdadf7dSDmitry Mishin 		break;
10283fdadf7dSDmitry Mishin 	case SOL_ICMPV6:
1029c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
10303fdadf7dSDmitry Mishin 			return -EOPNOTSUPP;
1031543d9cfeSArnaldo Carvalho de Melo 		return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
10323fdadf7dSDmitry Mishin 	case SOL_IPV6:
10333fdadf7dSDmitry Mishin 		if (optname == IPV6_CHECKSUM)
10343fdadf7dSDmitry Mishin 			break;
10353fdadf7dSDmitry Mishin 	default:
1036543d9cfeSArnaldo Carvalho de Melo 		return compat_ipv6_setsockopt(sk, level, optname,
1037543d9cfeSArnaldo Carvalho de Melo 					      optval, optlen);
10383ff50b79SStephen Hemminger 	}
10393fdadf7dSDmitry Mishin 	return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
10403fdadf7dSDmitry Mishin }
10413fdadf7dSDmitry Mishin #endif
10423fdadf7dSDmitry Mishin 
10433fdadf7dSDmitry Mishin static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
10443fdadf7dSDmitry Mishin 			    char __user *optval, int __user *optlen)
10453fdadf7dSDmitry Mishin {
10463fdadf7dSDmitry Mishin 	struct raw6_sock *rp = raw6_sk(sk);
10473fdadf7dSDmitry Mishin 	int val, len;
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	if (get_user(len, optlen))
10501da177e4SLinus Torvalds 		return -EFAULT;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	switch (optname) {
10531da177e4SLinus Torvalds 	case IPV6_CHECKSUM:
10541a98d05fSYOSHIFUJI Hideaki 		/*
10551a98d05fSYOSHIFUJI Hideaki 		 * We allow getsockopt() for IPPROTO_IPV6-level
10561a98d05fSYOSHIFUJI Hideaki 		 * IPV6_CHECKSUM socket option on ICMPv6 sockets
10571a98d05fSYOSHIFUJI Hideaki 		 * since RFC3542 is silent about it.
10581a98d05fSYOSHIFUJI Hideaki 		 */
10591da177e4SLinus Torvalds 		if (rp->checksum == 0)
10601da177e4SLinus Torvalds 			val = -1;
10611da177e4SLinus Torvalds 		else
10621da177e4SLinus Torvalds 			val = rp->offset;
10631da177e4SLinus Torvalds 		break;
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	default:
10661da177e4SLinus Torvalds 		return -ENOPROTOOPT;
10671da177e4SLinus Torvalds 	}
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	len = min_t(unsigned int, sizeof(int), len);
10701da177e4SLinus Torvalds 
10711da177e4SLinus Torvalds 	if (put_user(len, optlen))
10721da177e4SLinus Torvalds 		return -EFAULT;
10731da177e4SLinus Torvalds 	if (copy_to_user(optval, &val, len))
10741da177e4SLinus Torvalds 		return -EFAULT;
10751da177e4SLinus Torvalds 	return 0;
10761da177e4SLinus Torvalds }
10771da177e4SLinus Torvalds 
10783fdadf7dSDmitry Mishin static int rawv6_getsockopt(struct sock *sk, int level, int optname,
10793fdadf7dSDmitry Mishin 			  char __user *optval, int __user *optlen)
10803fdadf7dSDmitry Mishin {
10813fdadf7dSDmitry Mishin 	switch (level) {
10823fdadf7dSDmitry Mishin 	case SOL_RAW:
10833fdadf7dSDmitry Mishin 		break;
10843fdadf7dSDmitry Mishin 
10853fdadf7dSDmitry Mishin 	case SOL_ICMPV6:
1086c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
10873fdadf7dSDmitry Mishin 			return -EOPNOTSUPP;
1088207ec0abSJoe Perches 		return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
10893fdadf7dSDmitry Mishin 	case SOL_IPV6:
10903fdadf7dSDmitry Mishin 		if (optname == IPV6_CHECKSUM)
10913fdadf7dSDmitry Mishin 			break;
10923fdadf7dSDmitry Mishin 	default:
1093207ec0abSJoe Perches 		return ipv6_getsockopt(sk, level, optname, optval, optlen);
10943ff50b79SStephen Hemminger 	}
10953ff50b79SStephen Hemminger 
10963fdadf7dSDmitry Mishin 	return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
10973fdadf7dSDmitry Mishin }
10983fdadf7dSDmitry Mishin 
10993fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
11003fdadf7dSDmitry Mishin static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname,
11013fdadf7dSDmitry Mishin 				   char __user *optval, int __user *optlen)
11023fdadf7dSDmitry Mishin {
11033fdadf7dSDmitry Mishin 	switch (level) {
11043fdadf7dSDmitry Mishin 	case SOL_RAW:
11053fdadf7dSDmitry Mishin 		break;
11063fdadf7dSDmitry Mishin 	case SOL_ICMPV6:
1107c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
11083fdadf7dSDmitry Mishin 			return -EOPNOTSUPP;
1109543d9cfeSArnaldo Carvalho de Melo 		return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
11103fdadf7dSDmitry Mishin 	case SOL_IPV6:
11113fdadf7dSDmitry Mishin 		if (optname == IPV6_CHECKSUM)
11123fdadf7dSDmitry Mishin 			break;
11133fdadf7dSDmitry Mishin 	default:
1114543d9cfeSArnaldo Carvalho de Melo 		return compat_ipv6_getsockopt(sk, level, optname,
1115543d9cfeSArnaldo Carvalho de Melo 					      optval, optlen);
11163ff50b79SStephen Hemminger 	}
11173fdadf7dSDmitry Mishin 	return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
11183fdadf7dSDmitry Mishin }
11193fdadf7dSDmitry Mishin #endif
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);
11351da177e4SLinus Torvalds 		if (skb != NULL)
113629a3cad5SSimon Horman 			amount = skb_tail_pointer(skb) -
113729a3cad5SSimon Horman 				skb_transport_header(skb);
1138e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_receive_queue.lock);
11391da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
11401da177e4SLinus Torvalds 	}
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	default:
11437bc570c8SYOSHIFUJI Hideaki #ifdef CONFIG_IPV6_MROUTE
11447bc570c8SYOSHIFUJI Hideaki 		return ip6mr_ioctl(sk, cmd, (void __user *)arg);
11457bc570c8SYOSHIFUJI Hideaki #else
11461da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
11477bc570c8SYOSHIFUJI Hideaki #endif
11481da177e4SLinus Torvalds 	}
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
1151e2d57766SDavid S. Miller #ifdef CONFIG_COMPAT
1152e2d57766SDavid S. Miller static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
1153e2d57766SDavid S. Miller {
1154e2d57766SDavid S. Miller 	switch (cmd) {
1155e2d57766SDavid S. Miller 	case SIOCOUTQ:
1156e2d57766SDavid S. Miller 	case SIOCINQ:
1157e2d57766SDavid S. Miller 		return -ENOIOCTLCMD;
1158e2d57766SDavid S. Miller 	default:
1159e2d57766SDavid S. Miller #ifdef CONFIG_IPV6_MROUTE
1160e2d57766SDavid S. Miller 		return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg));
1161e2d57766SDavid S. Miller #else
1162e2d57766SDavid S. Miller 		return -ENOIOCTLCMD;
1163e2d57766SDavid S. Miller #endif
1164e2d57766SDavid S. Miller 	}
1165e2d57766SDavid S. Miller }
1166e2d57766SDavid S. Miller #endif
1167e2d57766SDavid S. Miller 
11681da177e4SLinus Torvalds static void rawv6_close(struct sock *sk, long timeout)
11691da177e4SLinus Torvalds {
1170c720c7e8SEric Dumazet 	if (inet_sk(sk)->inet_num == IPPROTO_RAW)
1171725a8ff0SDenis V. Lunev 		ip6_ra_control(sk, -1);
11727bc570c8SYOSHIFUJI Hideaki 	ip6mr_sk_done(sk);
11731da177e4SLinus Torvalds 	sk_common_release(sk);
11741da177e4SLinus Torvalds }
11751da177e4SLinus Torvalds 
11767d06b2e0SBrian Haley static void raw6_destroy(struct sock *sk)
117722dd4850SDenis V. Lunev {
117822dd4850SDenis V. Lunev 	lock_sock(sk);
117922dd4850SDenis V. Lunev 	ip6_flush_pending_frames(sk);
118022dd4850SDenis V. Lunev 	release_sock(sk);
1181f23d60deSDavid S. Miller 
11827d06b2e0SBrian Haley 	inet6_destroy_sock(sk);
118322dd4850SDenis V. Lunev }
118422dd4850SDenis V. Lunev 
11851da177e4SLinus Torvalds static int rawv6_init_sk(struct sock *sk)
11861da177e4SLinus Torvalds {
11871da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
1188f48d5ff1SMasahide NAKAMURA 
1189c720c7e8SEric Dumazet 	switch (inet_sk(sk)->inet_num) {
1190f48d5ff1SMasahide NAKAMURA 	case IPPROTO_ICMPV6:
11911da177e4SLinus Torvalds 		rp->checksum = 1;
11921da177e4SLinus Torvalds 		rp->offset   = 2;
1193f48d5ff1SMasahide NAKAMURA 		break;
1194f48d5ff1SMasahide NAKAMURA 	case IPPROTO_MH:
1195f48d5ff1SMasahide NAKAMURA 		rp->checksum = 1;
1196f48d5ff1SMasahide NAKAMURA 		rp->offset   = 4;
1197f48d5ff1SMasahide NAKAMURA 		break;
1198f48d5ff1SMasahide NAKAMURA 	default:
1199f48d5ff1SMasahide NAKAMURA 		break;
12001da177e4SLinus Torvalds 	}
1201a02cec21SEric Dumazet 	return 0;
12021da177e4SLinus Torvalds }
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds struct proto rawv6_prot = {
12051da177e4SLinus Torvalds 	.name		   = "RAWv6",
12061da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
12071da177e4SLinus Torvalds 	.close		   = rawv6_close,
120822dd4850SDenis V. Lunev 	.destroy	   = raw6_destroy,
120982b276cdSHannes Frederic Sowa 	.connect	   = ip6_datagram_connect_v6_only,
12101da177e4SLinus Torvalds 	.disconnect	   = udp_disconnect,
12111da177e4SLinus Torvalds 	.ioctl		   = rawv6_ioctl,
12121da177e4SLinus Torvalds 	.init		   = rawv6_init_sk,
12131da177e4SLinus Torvalds 	.setsockopt	   = rawv6_setsockopt,
12141da177e4SLinus Torvalds 	.getsockopt	   = rawv6_getsockopt,
12151da177e4SLinus Torvalds 	.sendmsg	   = rawv6_sendmsg,
12161da177e4SLinus Torvalds 	.recvmsg	   = rawv6_recvmsg,
12171da177e4SLinus Torvalds 	.bind		   = rawv6_bind,
12181da177e4SLinus Torvalds 	.backlog_rcv	   = rawv6_rcv_skb,
1219fc8717baSPavel Emelyanov 	.hash		   = raw_hash_sk,
1220fc8717baSPavel Emelyanov 	.unhash		   = raw_unhash_sk,
12211da177e4SLinus Torvalds 	.obj_size	   = sizeof(struct raw6_sock),
1222fc8717baSPavel Emelyanov 	.h.raw_hash	   = &raw_v6_hashinfo,
1223543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
1224543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_rawv6_setsockopt,
1225543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_rawv6_getsockopt,
1226e2d57766SDavid S. Miller 	.compat_ioctl	   = compat_rawv6_ioctl,
1227543d9cfeSArnaldo Carvalho de Melo #endif
12281da177e4SLinus Torvalds };
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12311da177e4SLinus Torvalds static int raw6_seq_show(struct seq_file *seq, void *v)
12321da177e4SLinus Torvalds {
123317ef66afSLorenzo Colitti 	if (v == SEQ_START_TOKEN) {
123417ef66afSLorenzo Colitti 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
123517ef66afSLorenzo Colitti 	} else {
123617ef66afSLorenzo Colitti 		struct sock *sp = v;
123717ef66afSLorenzo Colitti 		__u16 srcp  = inet_sk(sp)->inet_num;
123817ef66afSLorenzo Colitti 		ip6_dgram_sock_seq_show(seq, v, srcp, 0,
123917ef66afSLorenzo Colitti 					raw_seq_private(seq)->bucket);
124017ef66afSLorenzo Colitti 	}
12411da177e4SLinus Torvalds 	return 0;
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds 
124456b3d975SPhilippe De Muyter static const struct seq_operations raw6_seq_ops = {
124542a73808SPavel Emelyanov 	.start =	raw_seq_start,
124642a73808SPavel Emelyanov 	.next =		raw_seq_next,
124742a73808SPavel Emelyanov 	.stop =		raw_seq_stop,
12481da177e4SLinus Torvalds 	.show =		raw6_seq_show,
12491da177e4SLinus Torvalds };
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds static int raw6_seq_open(struct inode *inode, struct file *file)
12521da177e4SLinus Torvalds {
12533046d767SDenis V. Lunev 	return raw_seq_open(inode, file, &raw_v6_hashinfo, &raw6_seq_ops);
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
12569a32144eSArjan van de Ven static const struct file_operations raw6_seq_fops = {
12571da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
12581da177e4SLinus Torvalds 	.open =		raw6_seq_open,
12591da177e4SLinus Torvalds 	.read =		seq_read,
12601da177e4SLinus Torvalds 	.llseek =	seq_lseek,
1261f51d599fSPavel Emelyanov 	.release =	seq_release_net,
12621da177e4SLinus Torvalds };
12631da177e4SLinus Torvalds 
12642c8c1e72SAlexey Dobriyan static int __net_init raw6_init_net(struct net *net)
1265a308da16SPavel Emelyanov {
1266d4beaa66SGao feng 	if (!proc_create("raw6", S_IRUGO, net->proc_net, &raw6_seq_fops))
1267a308da16SPavel Emelyanov 		return -ENOMEM;
1268a308da16SPavel Emelyanov 
1269a308da16SPavel Emelyanov 	return 0;
1270a308da16SPavel Emelyanov }
1271a308da16SPavel Emelyanov 
12722c8c1e72SAlexey Dobriyan static void __net_exit raw6_exit_net(struct net *net)
1273a308da16SPavel Emelyanov {
1274ece31ffdSGao feng 	remove_proc_entry("raw6", net->proc_net);
1275a308da16SPavel Emelyanov }
1276a308da16SPavel Emelyanov 
1277a308da16SPavel Emelyanov static struct pernet_operations raw6_net_ops = {
1278a308da16SPavel Emelyanov 	.init = raw6_init_net,
1279a308da16SPavel Emelyanov 	.exit = raw6_exit_net,
1280a308da16SPavel Emelyanov };
1281a308da16SPavel Emelyanov 
12821da177e4SLinus Torvalds int __init raw6_proc_init(void)
12831da177e4SLinus Torvalds {
1284a308da16SPavel Emelyanov 	return register_pernet_subsys(&raw6_net_ops);
12851da177e4SLinus Torvalds }
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds void raw6_proc_exit(void)
12881da177e4SLinus Torvalds {
1289a308da16SPavel Emelyanov 	unregister_pernet_subsys(&raw6_net_ops);
12901da177e4SLinus Torvalds }
12911da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
12927f4e4868SDaniel Lezcano 
12937f4e4868SDaniel Lezcano /* Same as inet6_dgram_ops, sans udp_poll.  */
12947f4e4868SDaniel Lezcano static const struct proto_ops inet6_sockraw_ops = {
12957f4e4868SDaniel Lezcano 	.family		   = PF_INET6,
12967f4e4868SDaniel Lezcano 	.owner		   = THIS_MODULE,
12977f4e4868SDaniel Lezcano 	.release	   = inet6_release,
12987f4e4868SDaniel Lezcano 	.bind		   = inet6_bind,
12997f4e4868SDaniel Lezcano 	.connect	   = inet_dgram_connect,	/* ok		*/
13007f4e4868SDaniel Lezcano 	.socketpair	   = sock_no_socketpair,	/* a do nothing	*/
13017f4e4868SDaniel Lezcano 	.accept		   = sock_no_accept,		/* a do nothing	*/
13027f4e4868SDaniel Lezcano 	.getname	   = inet6_getname,
13037f4e4868SDaniel Lezcano 	.poll		   = datagram_poll,		/* ok		*/
13047f4e4868SDaniel Lezcano 	.ioctl		   = inet6_ioctl,		/* must change  */
13057f4e4868SDaniel Lezcano 	.listen		   = sock_no_listen,		/* ok		*/
13067f4e4868SDaniel Lezcano 	.shutdown	   = inet_shutdown,		/* ok		*/
13077f4e4868SDaniel Lezcano 	.setsockopt	   = sock_common_setsockopt,	/* ok		*/
13087f4e4868SDaniel Lezcano 	.getsockopt	   = sock_common_getsockopt,	/* ok		*/
13097f4e4868SDaniel Lezcano 	.sendmsg	   = inet_sendmsg,		/* ok		*/
13107f4e4868SDaniel Lezcano 	.recvmsg	   = sock_common_recvmsg,	/* ok		*/
13117f4e4868SDaniel Lezcano 	.mmap		   = sock_no_mmap,
13127f4e4868SDaniel Lezcano 	.sendpage	   = sock_no_sendpage,
13137f4e4868SDaniel Lezcano #ifdef CONFIG_COMPAT
13147f4e4868SDaniel Lezcano 	.compat_setsockopt = compat_sock_common_setsockopt,
13157f4e4868SDaniel Lezcano 	.compat_getsockopt = compat_sock_common_getsockopt,
13167f4e4868SDaniel Lezcano #endif
13177f4e4868SDaniel Lezcano };
13187f4e4868SDaniel Lezcano 
13197f4e4868SDaniel Lezcano static struct inet_protosw rawv6_protosw = {
13207f4e4868SDaniel Lezcano 	.type		= SOCK_RAW,
13217f4e4868SDaniel Lezcano 	.protocol	= IPPROTO_IP,	/* wild card */
13227f4e4868SDaniel Lezcano 	.prot		= &rawv6_prot,
13237f4e4868SDaniel Lezcano 	.ops		= &inet6_sockraw_ops,
13247f4e4868SDaniel Lezcano 	.flags		= INET_PROTOSW_REUSE,
13257f4e4868SDaniel Lezcano };
13267f4e4868SDaniel Lezcano 
13277f4e4868SDaniel Lezcano int __init rawv6_init(void)
13287f4e4868SDaniel Lezcano {
13297f4e4868SDaniel Lezcano 	int ret;
13307f4e4868SDaniel Lezcano 
13317f4e4868SDaniel Lezcano 	ret = inet6_register_protosw(&rawv6_protosw);
13327f4e4868SDaniel Lezcano 	if (ret)
13337f4e4868SDaniel Lezcano 		goto out;
13347f4e4868SDaniel Lezcano out:
13357f4e4868SDaniel Lezcano 	return ret;
13367f4e4868SDaniel Lezcano }
13377f4e4868SDaniel Lezcano 
133809f7709fSDaniel Lezcano void rawv6_exit(void)
13397f4e4868SDaniel Lezcano {
13407f4e4868SDaniel Lezcano 	inet6_unregister_protosw(&rawv6_protosw);
13417f4e4868SDaniel Lezcano }
1342