xref: /openbmc/linux/net/ipv6/raw.c (revision 3b1e0a65)
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  *	$Id: raw.c,v 1.51 2002/02/01 22:01:04 davem Exp $
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *	Fixes:
131da177e4SLinus Torvalds  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
141da177e4SLinus Torvalds  *	YOSHIFUJI,H.@USAGI	:	raw checksum (RFC2292(bis) compliance)
151da177e4SLinus Torvalds  *	Kazunori MIYAZAWA @USAGI:	change process style to use ip6_append_data
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
181da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
191da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
201da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
211da177e4SLinus Torvalds  */
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/errno.h>
241da177e4SLinus Torvalds #include <linux/types.h>
251da177e4SLinus Torvalds #include <linux/socket.h>
261da177e4SLinus Torvalds #include <linux/sockios.h>
271da177e4SLinus Torvalds #include <linux/net.h>
281da177e4SLinus Torvalds #include <linux/in6.h>
291da177e4SLinus Torvalds #include <linux/netdevice.h>
301da177e4SLinus Torvalds #include <linux/if_arp.h>
311da177e4SLinus Torvalds #include <linux/icmpv6.h>
321da177e4SLinus Torvalds #include <linux/netfilter.h>
331da177e4SLinus Torvalds #include <linux/netfilter_ipv6.h>
343305b80cSHerbert Xu #include <linux/skbuff.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>
5359fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
547be96f76SMasahide NAKAMURA #include <net/mip6.h>
557be96f76SMasahide NAKAMURA #endif
561da177e4SLinus Torvalds 
57b673e4dfSPavel Emelyanov #include <net/raw.h>
581da177e4SLinus Torvalds #include <net/rawv6.h>
591da177e4SLinus Torvalds #include <net/xfrm.h>
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #include <linux/proc_fs.h>
621da177e4SLinus Torvalds #include <linux/seq_file.h>
631da177e4SLinus Torvalds 
64b673e4dfSPavel Emelyanov static struct raw_hashinfo raw_v6_hashinfo = {
65938b93adSRobert P. J. Day 	.lock = __RW_LOCK_UNLOCKED(raw_v6_hashinfo.lock),
66b673e4dfSPavel Emelyanov };
671da177e4SLinus Torvalds 
68be185884SPavel Emelyanov static struct sock *__raw_v6_lookup(struct net *net, struct sock *sk,
69be185884SPavel Emelyanov 		unsigned short num, struct in6_addr *loc_addr,
70be185884SPavel Emelyanov 		struct in6_addr *rmt_addr, int dif)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	struct hlist_node *node;
731da177e4SLinus Torvalds 	int is_multicast = ipv6_addr_is_multicast(loc_addr);
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	sk_for_each_from(sk, node)
761da177e4SLinus Torvalds 		if (inet_sk(sk)->num == num) {
771da177e4SLinus Torvalds 			struct ipv6_pinfo *np = inet6_sk(sk);
781da177e4SLinus Torvalds 
793b1e0a65SYOSHIFUJI Hideaki 			if (sock_net(sk) != net)
80be185884SPavel Emelyanov 				continue;
81be185884SPavel Emelyanov 
821da177e4SLinus Torvalds 			if (!ipv6_addr_any(&np->daddr) &&
831da177e4SLinus Torvalds 			    !ipv6_addr_equal(&np->daddr, rmt_addr))
841da177e4SLinus Torvalds 				continue;
851da177e4SLinus Torvalds 
860bd1b59bSAndrew McDonald 			if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
870bd1b59bSAndrew McDonald 				continue;
880bd1b59bSAndrew McDonald 
891da177e4SLinus Torvalds 			if (!ipv6_addr_any(&np->rcv_saddr)) {
901da177e4SLinus Torvalds 				if (ipv6_addr_equal(&np->rcv_saddr, loc_addr))
911da177e4SLinus Torvalds 					goto found;
921da177e4SLinus Torvalds 				if (is_multicast &&
931da177e4SLinus Torvalds 				    inet6_mc_check(sk, loc_addr, rmt_addr))
941da177e4SLinus Torvalds 					goto found;
951da177e4SLinus Torvalds 				continue;
961da177e4SLinus Torvalds 			}
971da177e4SLinus Torvalds 			goto found;
981da177e4SLinus Torvalds 		}
991da177e4SLinus Torvalds 	sk = NULL;
1001da177e4SLinus Torvalds found:
1011da177e4SLinus Torvalds 	return sk;
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  *	0 - deliver
1061da177e4SLinus Torvalds  *	1 - block
1071da177e4SLinus Torvalds  */
1081da177e4SLinus Torvalds static __inline__ int icmpv6_filter(struct sock *sk, struct sk_buff *skb)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds 	struct icmp6hdr *icmph;
1111da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	if (pskb_may_pull(skb, sizeof(struct icmp6hdr))) {
1141da177e4SLinus Torvalds 		__u32 *data = &rp->filter.data[0];
1151da177e4SLinus Torvalds 		int bit_nr;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 		icmph = (struct icmp6hdr *) skb->data;
1181da177e4SLinus Torvalds 		bit_nr = icmph->icmp6_type;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 		return (data[bit_nr >> 5] & (1 << (bit_nr & 31))) != 0;
1211da177e4SLinus Torvalds 	}
1221da177e4SLinus Torvalds 	return 0;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
12559fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
12659fbb3a6SMasahide NAKAMURA static int (*mh_filter)(struct sock *sock, struct sk_buff *skb);
12759fbb3a6SMasahide NAKAMURA 
12859fbb3a6SMasahide NAKAMURA int rawv6_mh_filter_register(int (*filter)(struct sock *sock,
12959fbb3a6SMasahide NAKAMURA 					   struct sk_buff *skb))
13059fbb3a6SMasahide NAKAMURA {
13159fbb3a6SMasahide NAKAMURA 	rcu_assign_pointer(mh_filter, filter);
13259fbb3a6SMasahide NAKAMURA 	return 0;
13359fbb3a6SMasahide NAKAMURA }
13459fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_register);
13559fbb3a6SMasahide NAKAMURA 
13659fbb3a6SMasahide NAKAMURA int rawv6_mh_filter_unregister(int (*filter)(struct sock *sock,
13759fbb3a6SMasahide NAKAMURA 					     struct sk_buff *skb))
13859fbb3a6SMasahide NAKAMURA {
13959fbb3a6SMasahide NAKAMURA 	rcu_assign_pointer(mh_filter, NULL);
14059fbb3a6SMasahide NAKAMURA 	synchronize_rcu();
14159fbb3a6SMasahide NAKAMURA 	return 0;
14259fbb3a6SMasahide NAKAMURA }
14359fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_unregister);
14459fbb3a6SMasahide NAKAMURA 
14559fbb3a6SMasahide NAKAMURA #endif
14659fbb3a6SMasahide NAKAMURA 
1471da177e4SLinus Torvalds /*
1481da177e4SLinus Torvalds  *	demultiplex raw sockets.
1491da177e4SLinus Torvalds  *	(should consider queueing the skb in the sock receive_queue
1501da177e4SLinus Torvalds  *	without calling rawv6.c)
1511da177e4SLinus Torvalds  *
1521da177e4SLinus Torvalds  *	Caller owns SKB so we must make clones.
1531da177e4SLinus Torvalds  */
15469d6da0bSPavel Emelyanov static int ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	struct in6_addr *saddr;
1571da177e4SLinus Torvalds 	struct in6_addr *daddr;
1581da177e4SLinus Torvalds 	struct sock *sk;
159d13964f4SPatrick McHardy 	int delivered = 0;
1601da177e4SLinus Torvalds 	__u8 hash;
161be185884SPavel Emelyanov 	struct net *net;
1621da177e4SLinus Torvalds 
1630660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->saddr;
1641da177e4SLinus Torvalds 	daddr = saddr + 1;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	hash = nexthdr & (MAX_INET_PROTOS - 1);
1671da177e4SLinus Torvalds 
168b673e4dfSPavel Emelyanov 	read_lock(&raw_v6_hashinfo.lock);
169b673e4dfSPavel Emelyanov 	sk = sk_head(&raw_v6_hashinfo.ht[hash]);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	/*
1721da177e4SLinus Torvalds 	 *	The first socket found will be delivered after
1731da177e4SLinus Torvalds 	 *	delivery to transport protocols.
1741da177e4SLinus Torvalds 	 */
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	if (sk == NULL)
1771da177e4SLinus Torvalds 		goto out;
1781da177e4SLinus Torvalds 
179c346dca1SYOSHIFUJI Hideaki 	net = dev_net(skb->dev);
180be185884SPavel Emelyanov 	sk = __raw_v6_lookup(net, sk, nexthdr, daddr, saddr, IP6CB(skb)->iif);
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	while (sk) {
1837be96f76SMasahide NAKAMURA 		int filtered;
1847be96f76SMasahide NAKAMURA 
185d13964f4SPatrick McHardy 		delivered = 1;
1867be96f76SMasahide NAKAMURA 		switch (nexthdr) {
1877be96f76SMasahide NAKAMURA 		case IPPROTO_ICMPV6:
1887be96f76SMasahide NAKAMURA 			filtered = icmpv6_filter(sk, skb);
1897be96f76SMasahide NAKAMURA 			break;
19059fbb3a6SMasahide NAKAMURA 
19159fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
1927be96f76SMasahide NAKAMURA 		case IPPROTO_MH:
19359fbb3a6SMasahide NAKAMURA 		{
1947be96f76SMasahide NAKAMURA 			/* XXX: To validate MH only once for each packet,
1957be96f76SMasahide NAKAMURA 			 * this is placed here. It should be after checking
1967be96f76SMasahide NAKAMURA 			 * xfrm policy, however it doesn't. The checking xfrm
1977be96f76SMasahide NAKAMURA 			 * policy is placed in rawv6_rcv() because it is
1987be96f76SMasahide NAKAMURA 			 * required for each socket.
1997be96f76SMasahide NAKAMURA 			 */
20059fbb3a6SMasahide NAKAMURA 			int (*filter)(struct sock *sock, struct sk_buff *skb);
20159fbb3a6SMasahide NAKAMURA 
20259fbb3a6SMasahide NAKAMURA 			filter = rcu_dereference(mh_filter);
20359fbb3a6SMasahide NAKAMURA 			filtered = filter ? filter(sk, skb) : 0;
2047be96f76SMasahide NAKAMURA 			break;
20559fbb3a6SMasahide NAKAMURA 		}
2067be96f76SMasahide NAKAMURA #endif
2077be96f76SMasahide NAKAMURA 		default:
2087be96f76SMasahide NAKAMURA 			filtered = 0;
2097be96f76SMasahide NAKAMURA 			break;
2107be96f76SMasahide NAKAMURA 		}
2117be96f76SMasahide NAKAMURA 
2127be96f76SMasahide NAKAMURA 		if (filtered < 0)
2137be96f76SMasahide NAKAMURA 			break;
2147be96f76SMasahide NAKAMURA 		if (filtered == 0) {
2151da177e4SLinus Torvalds 			struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 			/* Not releasing hash table! */
2189fb9cbb1SYasuyuki Kozakai 			if (clone) {
2199fb9cbb1SYasuyuki Kozakai 				nf_reset(clone);
2201da177e4SLinus Torvalds 				rawv6_rcv(sk, clone);
2211da177e4SLinus Torvalds 			}
2229fb9cbb1SYasuyuki Kozakai 		}
223be185884SPavel Emelyanov 		sk = __raw_v6_lookup(net, sk_next(sk), nexthdr, daddr, saddr,
2242dac4b96SYOSHIFUJI Hideaki 				     IP6CB(skb)->iif);
2251da177e4SLinus Torvalds 	}
2261da177e4SLinus Torvalds out:
227b673e4dfSPavel Emelyanov 	read_unlock(&raw_v6_hashinfo.lock);
228d13964f4SPatrick McHardy 	return delivered;
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
23169d6da0bSPavel Emelyanov int raw6_local_deliver(struct sk_buff *skb, int nexthdr)
23269d6da0bSPavel Emelyanov {
23369d6da0bSPavel Emelyanov 	struct sock *raw_sk;
23469d6da0bSPavel Emelyanov 
235b673e4dfSPavel Emelyanov 	raw_sk = sk_head(&raw_v6_hashinfo.ht[nexthdr & (MAX_INET_PROTOS - 1)]);
23669d6da0bSPavel Emelyanov 	if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
23769d6da0bSPavel Emelyanov 		raw_sk = NULL;
23869d6da0bSPavel Emelyanov 
23969d6da0bSPavel Emelyanov 	return raw_sk != NULL;
24069d6da0bSPavel Emelyanov }
24169d6da0bSPavel Emelyanov 
2421da177e4SLinus Torvalds /* This cleans up af_inet6 a bit. -DaveM */
2431da177e4SLinus Torvalds static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2461da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
2471da177e4SLinus Torvalds 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
248e69a4adcSAl Viro 	__be32 v4addr = 0;
2491da177e4SLinus Torvalds 	int addr_type;
2501da177e4SLinus Torvalds 	int err;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	if (addr_len < SIN6_LEN_RFC2133)
2531da177e4SLinus Torvalds 		return -EINVAL;
2541da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&addr->sin6_addr);
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	/* Raw sockets are IPv6 only */
2571da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED)
2581da177e4SLinus Torvalds 		return(-EADDRNOTAVAIL);
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	lock_sock(sk);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	err = -EINVAL;
2631da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE)
2641da177e4SLinus Torvalds 		goto out;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	/* Check if the address belongs to the host. */
2671da177e4SLinus Torvalds 	if (addr_type != IPV6_ADDR_ANY) {
2681da177e4SLinus Torvalds 		struct net_device *dev = NULL;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 		if (addr_type & IPV6_ADDR_LINKLOCAL) {
2711da177e4SLinus Torvalds 			if (addr_len >= sizeof(struct sockaddr_in6) &&
2721da177e4SLinus Torvalds 			    addr->sin6_scope_id) {
2731da177e4SLinus Torvalds 				/* Override any existing binding, if another
2741da177e4SLinus Torvalds 				 * one is supplied by user.
2751da177e4SLinus Torvalds 				 */
2761da177e4SLinus Torvalds 				sk->sk_bound_dev_if = addr->sin6_scope_id;
2771da177e4SLinus Torvalds 			}
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 			/* Binding to link-local address requires an interface */
2801da177e4SLinus Torvalds 			if (!sk->sk_bound_dev_if)
2811da177e4SLinus Torvalds 				goto out;
2821da177e4SLinus Torvalds 
2833b1e0a65SYOSHIFUJI Hideaki 			dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
2841da177e4SLinus Torvalds 			if (!dev) {
2851da177e4SLinus Torvalds 				err = -ENODEV;
2861da177e4SLinus Torvalds 				goto out;
2871da177e4SLinus Torvalds 			}
2881da177e4SLinus Torvalds 		}
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 		/* ipv4 addr of the socket is invalid.  Only the
2911da177e4SLinus Torvalds 		 * unspecified and mapped address have a v4 equivalent.
2921da177e4SLinus Torvalds 		 */
2931da177e4SLinus Torvalds 		v4addr = LOOPBACK4_IPV6;
2941da177e4SLinus Torvalds 		if (!(addr_type & IPV6_ADDR_MULTICAST))	{
2951da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
2963b1e0a65SYOSHIFUJI Hideaki 			if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr,
297bfeade08SDaniel Lezcano 					   dev, 0)) {
2981da177e4SLinus Torvalds 				if (dev)
2991da177e4SLinus Torvalds 					dev_put(dev);
3001da177e4SLinus Torvalds 				goto out;
3011da177e4SLinus Torvalds 			}
3021da177e4SLinus Torvalds 		}
3031da177e4SLinus Torvalds 		if (dev)
3041da177e4SLinus Torvalds 			dev_put(dev);
3051da177e4SLinus Torvalds 	}
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	inet->rcv_saddr = inet->saddr = v4addr;
3081da177e4SLinus Torvalds 	ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
3091da177e4SLinus Torvalds 	if (!(addr_type & IPV6_ADDR_MULTICAST))
3101da177e4SLinus Torvalds 		ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
3111da177e4SLinus Torvalds 	err = 0;
3121da177e4SLinus Torvalds out:
3131da177e4SLinus Torvalds 	release_sock(sk);
3141da177e4SLinus Torvalds 	return err;
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
31769d6da0bSPavel Emelyanov static void rawv6_err(struct sock *sk, struct sk_buff *skb,
3181da177e4SLinus Torvalds 	       struct inet6_skb_parm *opt,
31904ce6909SAl Viro 	       int type, int code, int offset, __be32 info)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
3221da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
3231da177e4SLinus Torvalds 	int err;
3241da177e4SLinus Torvalds 	int harderr;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/* Report error on raw socket, if:
3271da177e4SLinus Torvalds 	   1. User requested recverr.
3281da177e4SLinus Torvalds 	   2. Socket is connected (otherwise the error indication
3291da177e4SLinus Torvalds 	      is useless without recverr and error is hard.
3301da177e4SLinus Torvalds 	 */
3311da177e4SLinus Torvalds 	if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
3321da177e4SLinus Torvalds 		return;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	harderr = icmpv6_err_convert(type, code, &err);
3351da177e4SLinus Torvalds 	if (type == ICMPV6_PKT_TOOBIG)
3361da177e4SLinus Torvalds 		harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	if (np->recverr) {
3391da177e4SLinus Torvalds 		u8 *payload = skb->data;
3401da177e4SLinus Torvalds 		if (!inet->hdrincl)
3411da177e4SLinus Torvalds 			payload += offset;
3421da177e4SLinus Torvalds 		ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
3431da177e4SLinus Torvalds 	}
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	if (np->recverr || harderr) {
3461da177e4SLinus Torvalds 		sk->sk_err = err;
3471da177e4SLinus Torvalds 		sk->sk_error_report(sk);
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
35169d6da0bSPavel Emelyanov void raw6_icmp_error(struct sk_buff *skb, int nexthdr,
35269d6da0bSPavel Emelyanov 		int type, int code, int inner_offset, __be32 info)
35369d6da0bSPavel Emelyanov {
35469d6da0bSPavel Emelyanov 	struct sock *sk;
35569d6da0bSPavel Emelyanov 	int hash;
35669d6da0bSPavel Emelyanov 	struct in6_addr *saddr, *daddr;
357be185884SPavel Emelyanov 	struct net *net;
35869d6da0bSPavel Emelyanov 
359b673e4dfSPavel Emelyanov 	hash = nexthdr & (RAW_HTABLE_SIZE - 1);
36069d6da0bSPavel Emelyanov 
361b673e4dfSPavel Emelyanov 	read_lock(&raw_v6_hashinfo.lock);
362b673e4dfSPavel Emelyanov 	sk = sk_head(&raw_v6_hashinfo.ht[hash]);
36369d6da0bSPavel Emelyanov 	if (sk != NULL) {
36469d6da0bSPavel Emelyanov 		saddr = &ipv6_hdr(skb)->saddr;
36569d6da0bSPavel Emelyanov 		daddr = &ipv6_hdr(skb)->daddr;
366c346dca1SYOSHIFUJI Hideaki 		net = dev_net(skb->dev);
36769d6da0bSPavel Emelyanov 
368be185884SPavel Emelyanov 		while ((sk = __raw_v6_lookup(net, sk, nexthdr, saddr, daddr,
36969d6da0bSPavel Emelyanov 						IP6CB(skb)->iif))) {
37069d6da0bSPavel Emelyanov 			rawv6_err(sk, skb, NULL, type, code,
37169d6da0bSPavel Emelyanov 					inner_offset, info);
37269d6da0bSPavel Emelyanov 			sk = sk_next(sk);
37369d6da0bSPavel Emelyanov 		}
37469d6da0bSPavel Emelyanov 	}
375b673e4dfSPavel Emelyanov 	read_unlock(&raw_v6_hashinfo.lock);
37669d6da0bSPavel Emelyanov }
37769d6da0bSPavel Emelyanov 
3781da177e4SLinus Torvalds static inline int rawv6_rcv_skb(struct sock * sk, struct sk_buff * skb)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	if ((raw6_sk(sk)->checksum || sk->sk_filter) &&
381fb286bb2SHerbert Xu 	    skb_checksum_complete(skb)) {
382a92aa318SWang Chen 		atomic_inc(&sk->sk_drops);
3831da177e4SLinus Torvalds 		kfree_skb(skb);
3841da177e4SLinus Torvalds 		return 0;
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/* Charge it to the socket. */
3881da177e4SLinus Torvalds 	if (sock_queue_rcv_skb(sk,skb)<0) {
389a92aa318SWang Chen 		atomic_inc(&sk->sk_drops);
3901da177e4SLinus Torvalds 		kfree_skb(skb);
3911da177e4SLinus Torvalds 		return 0;
3921da177e4SLinus Torvalds 	}
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	return 0;
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds /*
3981da177e4SLinus Torvalds  *	This is next to useless...
3991da177e4SLinus Torvalds  *	if we demultiplex in network layer we don't need the extra call
4001da177e4SLinus Torvalds  *	just to queue the skb...
4011da177e4SLinus Torvalds  *	maybe we could have the network decide upon a hint if it
4021da177e4SLinus Torvalds  *	should call raw_rcv for demultiplexing
4031da177e4SLinus Torvalds  */
4041da177e4SLinus Torvalds int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
4051da177e4SLinus Torvalds {
4061da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4071da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
410a92aa318SWang Chen 		atomic_inc(&sk->sk_drops);
4111da177e4SLinus Torvalds 		kfree_skb(skb);
4121da177e4SLinus Torvalds 		return NET_RX_DROP;
4131da177e4SLinus Torvalds 	}
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	if (!rp->checksum)
4161da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_UNNECESSARY;
4171da177e4SLinus Torvalds 
41884fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
419d56f90a7SArnaldo Carvalho de Melo 		skb_postpull_rcsum(skb, skb_network_header(skb),
420cfe1fc77SArnaldo Carvalho de Melo 				   skb_network_header_len(skb));
4210660e03fSArnaldo Carvalho de Melo 		if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4220660e03fSArnaldo Carvalho de Melo 				     &ipv6_hdr(skb)->daddr,
423fb286bb2SHerbert Xu 				     skb->len, inet->num, skb->csum))
424fb286bb2SHerbert Xu 			skb->ip_summed = CHECKSUM_UNNECESSARY;
4251da177e4SLinus Torvalds 	}
42660476372SHerbert Xu 	if (!skb_csum_unnecessary(skb))
4270660e03fSArnaldo Carvalho de Melo 		skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4280660e03fSArnaldo Carvalho de Melo 							 &ipv6_hdr(skb)->daddr,
4290660e03fSArnaldo Carvalho de Melo 							 skb->len,
4300660e03fSArnaldo Carvalho de Melo 							 inet->num, 0));
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	if (inet->hdrincl) {
433fb286bb2SHerbert Xu 		if (skb_checksum_complete(skb)) {
434a92aa318SWang Chen 			atomic_inc(&sk->sk_drops);
4351da177e4SLinus Torvalds 			kfree_skb(skb);
4361da177e4SLinus Torvalds 			return 0;
4371da177e4SLinus Torvalds 		}
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	rawv6_rcv_skb(sk, skb);
4411da177e4SLinus Torvalds 	return 0;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds /*
4461da177e4SLinus Torvalds  *	This should be easy, if there is something there
4471da177e4SLinus Torvalds  *	we return it, otherwise we block.
4481da177e4SLinus Torvalds  */
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk,
4511da177e4SLinus Torvalds 		  struct msghdr *msg, size_t len,
4521da177e4SLinus Torvalds 		  int noblock, int flags, int *addr_len)
4531da177e4SLinus Torvalds {
4541da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
4551da177e4SLinus Torvalds 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)msg->msg_name;
4561da177e4SLinus Torvalds 	struct sk_buff *skb;
4571da177e4SLinus Torvalds 	size_t copied;
4581da177e4SLinus Torvalds 	int err;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	if (flags & MSG_OOB)
4611da177e4SLinus Torvalds 		return -EOPNOTSUPP;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	if (addr_len)
4641da177e4SLinus Torvalds 		*addr_len=sizeof(*sin6);
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	if (flags & MSG_ERRQUEUE)
4671da177e4SLinus Torvalds 		return ipv6_recv_error(sk, msg, len);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	skb = skb_recv_datagram(sk, flags, noblock, &err);
4701da177e4SLinus Torvalds 	if (!skb)
4711da177e4SLinus Torvalds 		goto out;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	copied = skb->len;
4741da177e4SLinus Torvalds 	if (copied > len) {
4751da177e4SLinus Torvalds 		copied = len;
4761da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
4771da177e4SLinus Torvalds 	}
4781da177e4SLinus Torvalds 
47960476372SHerbert Xu 	if (skb_csum_unnecessary(skb)) {
4801da177e4SLinus Torvalds 		err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
4811da177e4SLinus Torvalds 	} else if (msg->msg_flags&MSG_TRUNC) {
482fb286bb2SHerbert Xu 		if (__skb_checksum_complete(skb))
4831da177e4SLinus Torvalds 			goto csum_copy_err;
4841da177e4SLinus Torvalds 		err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
4851da177e4SLinus Torvalds 	} else {
4861da177e4SLinus Torvalds 		err = skb_copy_and_csum_datagram_iovec(skb, 0, msg->msg_iov);
4871da177e4SLinus Torvalds 		if (err == -EINVAL)
4881da177e4SLinus Torvalds 			goto csum_copy_err;
4891da177e4SLinus Torvalds 	}
4901da177e4SLinus Torvalds 	if (err)
4911da177e4SLinus Torvalds 		goto out_free;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	/* Copy the address. */
4941da177e4SLinus Torvalds 	if (sin6) {
4951da177e4SLinus Torvalds 		sin6->sin6_family = AF_INET6;
496f59fc7f3STetsuo Handa 		sin6->sin6_port = 0;
4970660e03fSArnaldo Carvalho de Melo 		ipv6_addr_copy(&sin6->sin6_addr, &ipv6_hdr(skb)->saddr);
4981da177e4SLinus Torvalds 		sin6->sin6_flowinfo = 0;
4991da177e4SLinus Torvalds 		sin6->sin6_scope_id = 0;
5001da177e4SLinus Torvalds 		if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
5011da177e4SLinus Torvalds 			sin6->sin6_scope_id = IP6CB(skb)->iif;
5021da177e4SLinus Torvalds 	}
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	sock_recv_timestamp(msg, sk, skb);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	if (np->rxopt.all)
5071da177e4SLinus Torvalds 		datagram_recv_ctl(sk, msg, skb);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	err = copied;
5101da177e4SLinus Torvalds 	if (flags & MSG_TRUNC)
5111da177e4SLinus Torvalds 		err = skb->len;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds out_free:
5141da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
5151da177e4SLinus Torvalds out:
5161da177e4SLinus Torvalds 	return err;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds csum_copy_err:
5193305b80cSHerbert Xu 	skb_kill_datagram(sk, skb, flags);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	/* Error for blocking case is chosen to masquerade
5221da177e4SLinus Torvalds 	   as some normal condition.
5231da177e4SLinus Torvalds 	 */
5241da177e4SLinus Torvalds 	err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
525a92aa318SWang Chen 	atomic_inc(&sk->sk_drops);
5263305b80cSHerbert Xu 	goto out;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds static int rawv6_push_pending_frames(struct sock *sk, struct flowi *fl,
530357b40a1SHerbert Xu 				     struct raw6_sock *rp)
5311da177e4SLinus Torvalds {
5321da177e4SLinus Torvalds 	struct sk_buff *skb;
5331da177e4SLinus Torvalds 	int err = 0;
534357b40a1SHerbert Xu 	int offset;
535357b40a1SHerbert Xu 	int len;
536679a8738SHerbert Xu 	int total_len;
537868c86bcSAl Viro 	__wsum tmp_csum;
538868c86bcSAl Viro 	__sum16 csum;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	if (!rp->checksum)
5411da177e4SLinus Torvalds 		goto send;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
5441da177e4SLinus Torvalds 		goto out;
5451da177e4SLinus Torvalds 
546357b40a1SHerbert Xu 	offset = rp->offset;
547d56f90a7SArnaldo Carvalho de Melo 	total_len = inet_sk(sk)->cork.length - (skb_network_header(skb) -
548d56f90a7SArnaldo Carvalho de Melo 						skb->data);
549679a8738SHerbert Xu 	if (offset >= total_len - 1) {
5501da177e4SLinus Torvalds 		err = -EINVAL;
551357b40a1SHerbert Xu 		ip6_flush_pending_frames(sk);
5521da177e4SLinus Torvalds 		goto out;
5531da177e4SLinus Torvalds 	}
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	/* should be check HW csum miyazawa */
5561da177e4SLinus Torvalds 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
5571da177e4SLinus Torvalds 		/*
5581da177e4SLinus Torvalds 		 * Only one fragment on the socket.
5591da177e4SLinus Torvalds 		 */
5601da177e4SLinus Torvalds 		tmp_csum = skb->csum;
5611da177e4SLinus Torvalds 	} else {
562357b40a1SHerbert Xu 		struct sk_buff *csum_skb = NULL;
5631da177e4SLinus Torvalds 		tmp_csum = 0;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 		skb_queue_walk(&sk->sk_write_queue, skb) {
5661da177e4SLinus Torvalds 			tmp_csum = csum_add(tmp_csum, skb->csum);
567357b40a1SHerbert Xu 
568357b40a1SHerbert Xu 			if (csum_skb)
569357b40a1SHerbert Xu 				continue;
570357b40a1SHerbert Xu 
571ea2ae17dSArnaldo Carvalho de Melo 			len = skb->len - skb_transport_offset(skb);
572357b40a1SHerbert Xu 			if (offset >= len) {
573357b40a1SHerbert Xu 				offset -= len;
574357b40a1SHerbert Xu 				continue;
5751da177e4SLinus Torvalds 			}
576357b40a1SHerbert Xu 
577357b40a1SHerbert Xu 			csum_skb = skb;
5781da177e4SLinus Torvalds 		}
5791da177e4SLinus Torvalds 
580357b40a1SHerbert Xu 		skb = csum_skb;
581357b40a1SHerbert Xu 	}
582357b40a1SHerbert Xu 
583ea2ae17dSArnaldo Carvalho de Melo 	offset += skb_transport_offset(skb);
584357b40a1SHerbert Xu 	if (skb_copy_bits(skb, offset, &csum, 2))
585357b40a1SHerbert Xu 		BUG();
586357b40a1SHerbert Xu 
5871da177e4SLinus Torvalds 	/* in case cksum was not initialized */
588357b40a1SHerbert Xu 	if (unlikely(csum))
5895f92a738SAl Viro 		tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
5901da177e4SLinus Torvalds 
591868c86bcSAl Viro 	csum = csum_ipv6_magic(&fl->fl6_src,
5921da177e4SLinus Torvalds 				   &fl->fl6_dst,
593679a8738SHerbert Xu 				   total_len, fl->proto, tmp_csum);
5941da177e4SLinus Torvalds 
595f6ab0288SAl Viro 	if (csum == 0 && fl->proto == IPPROTO_UDP)
596f6ab0288SAl Viro 		csum = CSUM_MANGLED_0;
597357b40a1SHerbert Xu 
598357b40a1SHerbert Xu 	if (skb_store_bits(skb, offset, &csum, 2))
599357b40a1SHerbert Xu 		BUG();
600357b40a1SHerbert Xu 
6011da177e4SLinus Torvalds send:
6021da177e4SLinus Torvalds 	err = ip6_push_pending_frames(sk);
6031da177e4SLinus Torvalds out:
6041da177e4SLinus Torvalds 	return err;
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds static int rawv6_send_hdrinc(struct sock *sk, void *from, int length,
6081da177e4SLinus Torvalds 			struct flowi *fl, struct rt6_info *rt,
6091da177e4SLinus Torvalds 			unsigned int flags)
6101da177e4SLinus Torvalds {
6113320da89SHerbert Xu 	struct ipv6_pinfo *np = inet6_sk(sk);
6121da177e4SLinus Torvalds 	struct ipv6hdr *iph;
6131da177e4SLinus Torvalds 	struct sk_buff *skb;
6141da177e4SLinus Torvalds 	unsigned int hh_len;
6151da177e4SLinus Torvalds 	int err;
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	if (length > rt->u.dst.dev->mtu) {
6181da177e4SLinus Torvalds 		ipv6_local_error(sk, EMSGSIZE, fl, rt->u.dst.dev->mtu);
6191da177e4SLinus Torvalds 		return -EMSGSIZE;
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
6221da177e4SLinus Torvalds 		goto out;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	skb = sock_alloc_send_skb(sk, length+hh_len+15,
6271da177e4SLinus Torvalds 				  flags&MSG_DONTWAIT, &err);
6281da177e4SLinus Torvalds 	if (skb == NULL)
6291da177e4SLinus Torvalds 		goto error;
6301da177e4SLinus Torvalds 	skb_reserve(skb, hh_len);
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
6334a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
6341da177e4SLinus Torvalds 	skb->dst = dst_clone(&rt->u.dst);
6351da177e4SLinus Torvalds 
6361ced98e8SArnaldo Carvalho de Melo 	skb_put(skb, length);
6371ced98e8SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
6380660e03fSArnaldo Carvalho de Melo 	iph = ipv6_hdr(skb);
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	skb->ip_summed = CHECKSUM_NONE;
6411da177e4SLinus Torvalds 
642b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
6431da177e4SLinus Torvalds 	err = memcpy_fromiovecend((void *)iph, from, 0, length);
6441da177e4SLinus Torvalds 	if (err)
6451da177e4SLinus Torvalds 		goto error_fault;
6461da177e4SLinus Torvalds 
647a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTREQUESTS);
6486e23ae2aSPatrick McHardy 	err = NF_HOOK(PF_INET6, NF_INET_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
6491da177e4SLinus Torvalds 		      dst_output);
6501da177e4SLinus Torvalds 	if (err > 0)
6513320da89SHerbert Xu 		err = np->recverr ? net_xmit_errno(err) : 0;
6521da177e4SLinus Torvalds 	if (err)
6531da177e4SLinus Torvalds 		goto error;
6541da177e4SLinus Torvalds out:
6551da177e4SLinus Torvalds 	return 0;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds error_fault:
6581da177e4SLinus Torvalds 	err = -EFAULT;
6591da177e4SLinus Torvalds 	kfree_skb(skb);
6601da177e4SLinus Torvalds error:
661a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
6621da177e4SLinus Torvalds 	return err;
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
665a27b58feSHeiko Carstens static int rawv6_probe_proto_opt(struct flowi *fl, struct msghdr *msg)
6661da177e4SLinus Torvalds {
6671da177e4SLinus Torvalds 	struct iovec *iov;
6681da177e4SLinus Torvalds 	u8 __user *type = NULL;
6691da177e4SLinus Torvalds 	u8 __user *code = NULL;
6706e8f4d48SMasahide NAKAMURA 	u8 len = 0;
6711da177e4SLinus Torvalds 	int probed = 0;
6721da177e4SLinus Torvalds 	int i;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	if (!msg->msg_iov)
675a27b58feSHeiko Carstens 		return 0;
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	for (i = 0; i < msg->msg_iovlen; i++) {
6781da177e4SLinus Torvalds 		iov = &msg->msg_iov[i];
6791da177e4SLinus Torvalds 		if (!iov)
6801da177e4SLinus Torvalds 			continue;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds 		switch (fl->proto) {
6831da177e4SLinus Torvalds 		case IPPROTO_ICMPV6:
6841da177e4SLinus Torvalds 			/* check if one-byte field is readable or not. */
6851da177e4SLinus Torvalds 			if (iov->iov_base && iov->iov_len < 1)
6861da177e4SLinus Torvalds 				break;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 			if (!type) {
6891da177e4SLinus Torvalds 				type = iov->iov_base;
6901da177e4SLinus Torvalds 				/* check if code field is readable or not. */
6911da177e4SLinus Torvalds 				if (iov->iov_len > 1)
6921da177e4SLinus Torvalds 					code = type + 1;
6931da177e4SLinus Torvalds 			} else if (!code)
6941da177e4SLinus Torvalds 				code = iov->iov_base;
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 			if (type && code) {
697a27b58feSHeiko Carstens 				if (get_user(fl->fl_icmp_type, type) ||
698a27b58feSHeiko Carstens 				    get_user(fl->fl_icmp_code, code))
699a27b58feSHeiko Carstens 					return -EFAULT;
7001da177e4SLinus Torvalds 				probed = 1;
7011da177e4SLinus Torvalds 			}
7021da177e4SLinus Torvalds 			break;
7036e8f4d48SMasahide NAKAMURA 		case IPPROTO_MH:
7046e8f4d48SMasahide NAKAMURA 			if (iov->iov_base && iov->iov_len < 1)
7056e8f4d48SMasahide NAKAMURA 				break;
7066e8f4d48SMasahide NAKAMURA 			/* check if type field is readable or not. */
7076e8f4d48SMasahide NAKAMURA 			if (iov->iov_len > 2 - len) {
7086e8f4d48SMasahide NAKAMURA 				u8 __user *p = iov->iov_base;
709a27b58feSHeiko Carstens 				if (get_user(fl->fl_mh_type, &p[2 - len]))
710a27b58feSHeiko Carstens 					return -EFAULT;
7116e8f4d48SMasahide NAKAMURA 				probed = 1;
7126e8f4d48SMasahide NAKAMURA 			} else
7136e8f4d48SMasahide NAKAMURA 				len += iov->iov_len;
7146e8f4d48SMasahide NAKAMURA 
7156e8f4d48SMasahide NAKAMURA 			break;
7161da177e4SLinus Torvalds 		default:
7171da177e4SLinus Torvalds 			probed = 1;
7181da177e4SLinus Torvalds 			break;
7191da177e4SLinus Torvalds 		}
7201da177e4SLinus Torvalds 		if (probed)
7211da177e4SLinus Torvalds 			break;
7221da177e4SLinus Torvalds 	}
723a27b58feSHeiko Carstens 	return 0;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
7271da177e4SLinus Torvalds 		   struct msghdr *msg, size_t len)
7281da177e4SLinus Torvalds {
7291da177e4SLinus Torvalds 	struct ipv6_txoptions opt_space;
7301da177e4SLinus Torvalds 	struct sockaddr_in6 * sin6 = (struct sockaddr_in6 *) msg->msg_name;
7311da177e4SLinus Torvalds 	struct in6_addr *daddr, *final_p = NULL, final;
7321da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
7331da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
7341da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
7351da177e4SLinus Torvalds 	struct ipv6_txoptions *opt = NULL;
7361da177e4SLinus Torvalds 	struct ip6_flowlabel *flowlabel = NULL;
7371da177e4SLinus Torvalds 	struct dst_entry *dst = NULL;
7381da177e4SLinus Torvalds 	struct flowi fl;
7391da177e4SLinus Torvalds 	int addr_len = msg->msg_namelen;
7401da177e4SLinus Torvalds 	int hlimit = -1;
74141a1f8eaSYOSHIFUJI Hideaki 	int tclass = -1;
7421da177e4SLinus Torvalds 	u16 proto;
7431da177e4SLinus Torvalds 	int err;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	/* Rough check on arithmetic overflow,
746b59e139bSYOSHIFUJI Hideaki 	   better check is made in ip6_append_data().
7471da177e4SLinus Torvalds 	 */
748b59e139bSYOSHIFUJI Hideaki 	if (len > INT_MAX)
7491da177e4SLinus Torvalds 		return -EMSGSIZE;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	/* Mirror BSD error message compatibility */
7521da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
7531da177e4SLinus Torvalds 		return -EOPNOTSUPP;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	/*
7561da177e4SLinus Torvalds 	 *	Get and verify the address.
7571da177e4SLinus Torvalds 	 */
7581da177e4SLinus Torvalds 	memset(&fl, 0, sizeof(fl));
7591da177e4SLinus Torvalds 
7604a19ec58SLaszlo Attila Toth 	fl.mark = sk->sk_mark;
7614a19ec58SLaszlo Attila Toth 
7621da177e4SLinus Torvalds 	if (sin6) {
7631da177e4SLinus Torvalds 		if (addr_len < SIN6_LEN_RFC2133)
7641da177e4SLinus Torvalds 			return -EINVAL;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 		if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
7671da177e4SLinus Torvalds 			return(-EAFNOSUPPORT);
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 		/* port is the proto value [0..255] carried in nexthdr */
7701da177e4SLinus Torvalds 		proto = ntohs(sin6->sin6_port);
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 		if (!proto)
7731da177e4SLinus Torvalds 			proto = inet->num;
7741da177e4SLinus Torvalds 		else if (proto != inet->num)
7751da177e4SLinus Torvalds 			return(-EINVAL);
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 		if (proto > 255)
7781da177e4SLinus Torvalds 			return(-EINVAL);
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 		daddr = &sin6->sin6_addr;
7811da177e4SLinus Torvalds 		if (np->sndflow) {
7821da177e4SLinus Torvalds 			fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
7831da177e4SLinus Torvalds 			if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
7841da177e4SLinus Torvalds 				flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
7851da177e4SLinus Torvalds 				if (flowlabel == NULL)
7861da177e4SLinus Torvalds 					return -EINVAL;
7871da177e4SLinus Torvalds 				daddr = &flowlabel->dst;
7881da177e4SLinus Torvalds 			}
7891da177e4SLinus Torvalds 		}
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 		/*
7921da177e4SLinus Torvalds 		 * Otherwise it will be difficult to maintain
7931da177e4SLinus Torvalds 		 * sk->sk_dst_cache.
7941da177e4SLinus Torvalds 		 */
7951da177e4SLinus Torvalds 		if (sk->sk_state == TCP_ESTABLISHED &&
7961da177e4SLinus Torvalds 		    ipv6_addr_equal(daddr, &np->daddr))
7971da177e4SLinus Torvalds 			daddr = &np->daddr;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 		if (addr_len >= sizeof(struct sockaddr_in6) &&
8001da177e4SLinus Torvalds 		    sin6->sin6_scope_id &&
8011da177e4SLinus Torvalds 		    ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
8021da177e4SLinus Torvalds 			fl.oif = sin6->sin6_scope_id;
8031da177e4SLinus Torvalds 	} else {
8041da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED)
8051da177e4SLinus Torvalds 			return -EDESTADDRREQ;
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 		proto = inet->num;
8081da177e4SLinus Torvalds 		daddr = &np->daddr;
8091da177e4SLinus Torvalds 		fl.fl6_flowlabel = np->flow_label;
8101da177e4SLinus Torvalds 	}
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	if (ipv6_addr_any(daddr)) {
8131da177e4SLinus Torvalds 		/*
8141da177e4SLinus Torvalds 		 * unspecified destination address
8151da177e4SLinus Torvalds 		 * treated as error... is this correct ?
8161da177e4SLinus Torvalds 		 */
8171da177e4SLinus Torvalds 		fl6_sock_release(flowlabel);
8181da177e4SLinus Torvalds 		return(-EINVAL);
8191da177e4SLinus Torvalds 	}
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	if (fl.oif == 0)
8221da177e4SLinus Torvalds 		fl.oif = sk->sk_bound_dev_if;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	if (msg->msg_controllen) {
8251da177e4SLinus Torvalds 		opt = &opt_space;
8261da177e4SLinus Torvalds 		memset(opt, 0, sizeof(struct ipv6_txoptions));
8271da177e4SLinus Torvalds 		opt->tot_len = sizeof(struct ipv6_txoptions);
8281da177e4SLinus Torvalds 
82941a1f8eaSYOSHIFUJI Hideaki 		err = datagram_send_ctl(msg, &fl, opt, &hlimit, &tclass);
8301da177e4SLinus Torvalds 		if (err < 0) {
8311da177e4SLinus Torvalds 			fl6_sock_release(flowlabel);
8321da177e4SLinus Torvalds 			return err;
8331da177e4SLinus Torvalds 		}
8341da177e4SLinus Torvalds 		if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
8351da177e4SLinus Torvalds 			flowlabel = fl6_sock_lookup(sk, fl.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 
8481da177e4SLinus Torvalds 	fl.proto = proto;
849a27b58feSHeiko Carstens 	err = rawv6_probe_proto_opt(&fl, msg);
850a27b58feSHeiko Carstens 	if (err)
851a27b58feSHeiko Carstens 		goto out;
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	ipv6_addr_copy(&fl.fl6_dst, daddr);
8541da177e4SLinus Torvalds 	if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
8551da177e4SLinus Torvalds 		ipv6_addr_copy(&fl.fl6_src, &np->saddr);
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	/* merge ip6_build_xmit from ip6_output */
8581da177e4SLinus Torvalds 	if (opt && opt->srcrt) {
8591da177e4SLinus Torvalds 		struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
8601da177e4SLinus Torvalds 		ipv6_addr_copy(&final, &fl.fl6_dst);
8611da177e4SLinus Torvalds 		ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
8621da177e4SLinus Torvalds 		final_p = &final;
8631da177e4SLinus Torvalds 	}
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds 	if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst))
8661da177e4SLinus Torvalds 		fl.oif = np->mcast_oif;
867beb8d13bSVenkat Yekkirala 	security_sk_classify_flow(sk, &fl);
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds 	err = ip6_dst_lookup(sk, &dst, &fl);
8701da177e4SLinus Torvalds 	if (err)
8711da177e4SLinus Torvalds 		goto out;
8721da177e4SLinus Torvalds 	if (final_p)
8731da177e4SLinus Torvalds 		ipv6_addr_copy(&fl.fl6_dst, final_p);
8741da177e4SLinus Torvalds 
875bb72845eSHerbert Xu 	if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) {
87614e50e57SDavid S. Miller 		if (err == -EREMOTE)
87714e50e57SDavid S. Miller 			err = ip6_dst_blackhole(sk, &dst, &fl);
87814e50e57SDavid S. Miller 		if (err < 0)
8791da177e4SLinus Torvalds 			goto out;
88014e50e57SDavid S. Miller 	}
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	if (hlimit < 0) {
8831da177e4SLinus Torvalds 		if (ipv6_addr_is_multicast(&fl.fl6_dst))
8841da177e4SLinus Torvalds 			hlimit = np->mcast_hops;
8851da177e4SLinus Torvalds 		else
8861da177e4SLinus Torvalds 			hlimit = np->hop_limit;
8871da177e4SLinus Torvalds 		if (hlimit < 0)
8886b75d090SYOSHIFUJI Hideaki 			hlimit = ip6_dst_hoplimit(dst);
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds 
89141a1f8eaSYOSHIFUJI Hideaki 	if (tclass < 0) {
892e012d51cSYOSHIFUJI Hideaki 		tclass = np->tclass;
89341a1f8eaSYOSHIFUJI Hideaki 		if (tclass < 0)
89441a1f8eaSYOSHIFUJI Hideaki 			tclass = 0;
89541a1f8eaSYOSHIFUJI Hideaki 	}
89641a1f8eaSYOSHIFUJI Hideaki 
8971da177e4SLinus Torvalds 	if (msg->msg_flags&MSG_CONFIRM)
8981da177e4SLinus Torvalds 		goto do_confirm;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds back_from_confirm:
9011da177e4SLinus Torvalds 	if (inet->hdrincl) {
9021da177e4SLinus Torvalds 		err = rawv6_send_hdrinc(sk, msg->msg_iov, len, &fl, (struct rt6_info*)dst, msg->msg_flags);
9031da177e4SLinus Torvalds 	} else {
9041da177e4SLinus Torvalds 		lock_sock(sk);
90541a1f8eaSYOSHIFUJI Hideaki 		err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov,
90641a1f8eaSYOSHIFUJI Hideaki 			len, 0, hlimit, tclass, opt, &fl, (struct rt6_info*)dst,
90741a1f8eaSYOSHIFUJI Hideaki 			msg->msg_flags);
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 		if (err)
9101da177e4SLinus Torvalds 			ip6_flush_pending_frames(sk);
9111da177e4SLinus Torvalds 		else if (!(msg->msg_flags & MSG_MORE))
912357b40a1SHerbert Xu 			err = rawv6_push_pending_frames(sk, &fl, rp);
9133ef9d943SYOSHIFUJI Hideaki 		release_sock(sk);
9141da177e4SLinus Torvalds 	}
9151da177e4SLinus Torvalds done:
9166d3e85ecSNicolas DICHTEL 	dst_release(dst);
9171da177e4SLinus Torvalds out:
9181da177e4SLinus Torvalds 	fl6_sock_release(flowlabel);
9191da177e4SLinus Torvalds 	return err<0?err:len;
9201da177e4SLinus Torvalds do_confirm:
9211da177e4SLinus Torvalds 	dst_confirm(dst);
9221da177e4SLinus Torvalds 	if (!(msg->msg_flags & MSG_PROBE) || len)
9231da177e4SLinus Torvalds 		goto back_from_confirm;
9241da177e4SLinus Torvalds 	err = 0;
9251da177e4SLinus Torvalds 	goto done;
9261da177e4SLinus Torvalds }
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
9291da177e4SLinus Torvalds 			       char __user *optval, int optlen)
9301da177e4SLinus Torvalds {
9311da177e4SLinus Torvalds 	switch (optname) {
9321da177e4SLinus Torvalds 	case ICMPV6_FILTER:
9331da177e4SLinus Torvalds 		if (optlen > sizeof(struct icmp6_filter))
9341da177e4SLinus Torvalds 			optlen = sizeof(struct icmp6_filter);
9351da177e4SLinus Torvalds 		if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
9361da177e4SLinus Torvalds 			return -EFAULT;
9371da177e4SLinus Torvalds 		return 0;
9381da177e4SLinus Torvalds 	default:
9391da177e4SLinus Torvalds 		return -ENOPROTOOPT;
9403ff50b79SStephen Hemminger 	}
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	return 0;
9431da177e4SLinus Torvalds }
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
9461da177e4SLinus Torvalds 			       char __user *optval, int __user *optlen)
9471da177e4SLinus Torvalds {
9481da177e4SLinus Torvalds 	int len;
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	switch (optname) {
9511da177e4SLinus Torvalds 	case ICMPV6_FILTER:
9521da177e4SLinus Torvalds 		if (get_user(len, optlen))
9531da177e4SLinus Torvalds 			return -EFAULT;
9541da177e4SLinus Torvalds 		if (len < 0)
9551da177e4SLinus Torvalds 			return -EINVAL;
9561da177e4SLinus Torvalds 		if (len > sizeof(struct icmp6_filter))
9571da177e4SLinus Torvalds 			len = sizeof(struct icmp6_filter);
9581da177e4SLinus Torvalds 		if (put_user(len, optlen))
9591da177e4SLinus Torvalds 			return -EFAULT;
9601da177e4SLinus Torvalds 		if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
9611da177e4SLinus Torvalds 			return -EFAULT;
9621da177e4SLinus Torvalds 		return 0;
9631da177e4SLinus Torvalds 	default:
9641da177e4SLinus Torvalds 		return -ENOPROTOOPT;
9653ff50b79SStephen Hemminger 	}
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	return 0;
9681da177e4SLinus Torvalds }
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 
9713fdadf7dSDmitry Mishin static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
9721da177e4SLinus Torvalds 			    char __user *optval, int optlen)
9731da177e4SLinus Torvalds {
9741da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
9751da177e4SLinus Torvalds 	int val;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	if (get_user(val, (int __user *)optval))
9781da177e4SLinus Torvalds 		return -EFAULT;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	switch (optname) {
9811da177e4SLinus Torvalds 		case IPV6_CHECKSUM:
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))
9851da177e4SLinus Torvalds 				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 			break;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 		default:
9971da177e4SLinus Torvalds 			return(-ENOPROTOOPT);
9981da177e4SLinus Torvalds 	}
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds 
10013fdadf7dSDmitry Mishin static int rawv6_setsockopt(struct sock *sk, int level, int optname,
10023fdadf7dSDmitry Mishin 			  char __user *optval, int optlen)
10031da177e4SLinus Torvalds {
10041da177e4SLinus Torvalds 	switch(level) {
10051da177e4SLinus Torvalds 		case SOL_RAW:
10061da177e4SLinus Torvalds 			break;
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 		case SOL_ICMPV6:
10091da177e4SLinus Torvalds 			if (inet_sk(sk)->num != IPPROTO_ICMPV6)
10101da177e4SLinus Torvalds 				return -EOPNOTSUPP;
10113fdadf7dSDmitry Mishin 			return rawv6_seticmpfilter(sk, level, optname, optval,
10121da177e4SLinus Torvalds 						   optlen);
10131da177e4SLinus Torvalds 		case SOL_IPV6:
10141da177e4SLinus Torvalds 			if (optname == IPV6_CHECKSUM)
10151da177e4SLinus Torvalds 				break;
10161da177e4SLinus Torvalds 		default:
10173fdadf7dSDmitry Mishin 			return ipv6_setsockopt(sk, level, optname, optval,
10181da177e4SLinus Torvalds 					       optlen);
10193ff50b79SStephen Hemminger 	}
10203ff50b79SStephen Hemminger 
10213fdadf7dSDmitry Mishin 	return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
10223fdadf7dSDmitry Mishin }
10233fdadf7dSDmitry Mishin 
10243fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
10253fdadf7dSDmitry Mishin static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname,
10263fdadf7dSDmitry Mishin 				   char __user *optval, int optlen)
10273fdadf7dSDmitry Mishin {
10283fdadf7dSDmitry Mishin 	switch (level) {
10293fdadf7dSDmitry Mishin 	case SOL_RAW:
10303fdadf7dSDmitry Mishin 		break;
10313fdadf7dSDmitry Mishin 	case SOL_ICMPV6:
10323fdadf7dSDmitry Mishin 		if (inet_sk(sk)->num != IPPROTO_ICMPV6)
10333fdadf7dSDmitry Mishin 			return -EOPNOTSUPP;
1034543d9cfeSArnaldo Carvalho de Melo 		return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
10353fdadf7dSDmitry Mishin 	case SOL_IPV6:
10363fdadf7dSDmitry Mishin 		if (optname == IPV6_CHECKSUM)
10373fdadf7dSDmitry Mishin 			break;
10383fdadf7dSDmitry Mishin 	default:
1039543d9cfeSArnaldo Carvalho de Melo 		return compat_ipv6_setsockopt(sk, level, optname,
1040543d9cfeSArnaldo Carvalho de Melo 					      optval, optlen);
10413ff50b79SStephen Hemminger 	}
10423fdadf7dSDmitry Mishin 	return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
10433fdadf7dSDmitry Mishin }
10443fdadf7dSDmitry Mishin #endif
10453fdadf7dSDmitry Mishin 
10463fdadf7dSDmitry Mishin static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
10473fdadf7dSDmitry Mishin 			    char __user *optval, int __user *optlen)
10483fdadf7dSDmitry Mishin {
10493fdadf7dSDmitry Mishin 	struct raw6_sock *rp = raw6_sk(sk);
10503fdadf7dSDmitry Mishin 	int val, len;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	if (get_user(len,optlen))
10531da177e4SLinus Torvalds 		return -EFAULT;
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	switch (optname) {
10561da177e4SLinus Torvalds 	case IPV6_CHECKSUM:
10571da177e4SLinus Torvalds 		if (rp->checksum == 0)
10581da177e4SLinus Torvalds 			val = -1;
10591da177e4SLinus Torvalds 		else
10601da177e4SLinus Torvalds 			val = rp->offset;
10611da177e4SLinus Torvalds 		break;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	default:
10641da177e4SLinus Torvalds 		return -ENOPROTOOPT;
10651da177e4SLinus Torvalds 	}
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	len = min_t(unsigned int, sizeof(int), len);
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds 	if (put_user(len, optlen))
10701da177e4SLinus Torvalds 		return -EFAULT;
10711da177e4SLinus Torvalds 	if (copy_to_user(optval,&val,len))
10721da177e4SLinus Torvalds 		return -EFAULT;
10731da177e4SLinus Torvalds 	return 0;
10741da177e4SLinus Torvalds }
10751da177e4SLinus Torvalds 
10763fdadf7dSDmitry Mishin static int rawv6_getsockopt(struct sock *sk, int level, int optname,
10773fdadf7dSDmitry Mishin 			  char __user *optval, int __user *optlen)
10783fdadf7dSDmitry Mishin {
10793fdadf7dSDmitry Mishin 	switch(level) {
10803fdadf7dSDmitry Mishin 		case SOL_RAW:
10813fdadf7dSDmitry Mishin 			break;
10823fdadf7dSDmitry Mishin 
10833fdadf7dSDmitry Mishin 		case SOL_ICMPV6:
10843fdadf7dSDmitry Mishin 			if (inet_sk(sk)->num != IPPROTO_ICMPV6)
10853fdadf7dSDmitry Mishin 				return -EOPNOTSUPP;
10863fdadf7dSDmitry Mishin 			return rawv6_geticmpfilter(sk, level, optname, optval,
10873fdadf7dSDmitry Mishin 						   optlen);
10883fdadf7dSDmitry Mishin 		case SOL_IPV6:
10893fdadf7dSDmitry Mishin 			if (optname == IPV6_CHECKSUM)
10903fdadf7dSDmitry Mishin 				break;
10913fdadf7dSDmitry Mishin 		default:
10923fdadf7dSDmitry Mishin 			return ipv6_getsockopt(sk, level, optname, optval,
10933fdadf7dSDmitry Mishin 					       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:
11073fdadf7dSDmitry Mishin 		if (inet_sk(sk)->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) {
11241da177e4SLinus Torvalds 		case SIOCOUTQ:
11251da177e4SLinus Torvalds 		{
11261da177e4SLinus Torvalds 			int amount = atomic_read(&sk->sk_wmem_alloc);
11271da177e4SLinus Torvalds 			return put_user(amount, (int __user *)arg);
11281da177e4SLinus Torvalds 		}
11291da177e4SLinus Torvalds 		case SIOCINQ:
11301da177e4SLinus Torvalds 		{
11311da177e4SLinus Torvalds 			struct sk_buff *skb;
11321da177e4SLinus Torvalds 			int amount = 0;
11331da177e4SLinus Torvalds 
1134e0f9f858SHerbert Xu 			spin_lock_bh(&sk->sk_receive_queue.lock);
11351da177e4SLinus Torvalds 			skb = skb_peek(&sk->sk_receive_queue);
11361da177e4SLinus Torvalds 			if (skb != NULL)
113727a884dcSArnaldo Carvalho de Melo 				amount = skb->tail - skb->transport_header;
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:
11431da177e4SLinus Torvalds 			return -ENOIOCTLCMD;
11441da177e4SLinus Torvalds 	}
11451da177e4SLinus Torvalds }
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds static void rawv6_close(struct sock *sk, long timeout)
11481da177e4SLinus Torvalds {
11491da177e4SLinus Torvalds 	if (inet_sk(sk)->num == IPPROTO_RAW)
11501da177e4SLinus Torvalds 		ip6_ra_control(sk, -1, NULL);
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	sk_common_release(sk);
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds static int rawv6_init_sk(struct sock *sk)
11561da177e4SLinus Torvalds {
11571da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
1158f48d5ff1SMasahide NAKAMURA 
1159f48d5ff1SMasahide NAKAMURA 	switch (inet_sk(sk)->num) {
1160f48d5ff1SMasahide NAKAMURA 	case IPPROTO_ICMPV6:
11611da177e4SLinus Torvalds 		rp->checksum = 1;
11621da177e4SLinus Torvalds 		rp->offset   = 2;
1163f48d5ff1SMasahide NAKAMURA 		break;
1164f48d5ff1SMasahide NAKAMURA 	case IPPROTO_MH:
1165f48d5ff1SMasahide NAKAMURA 		rp->checksum = 1;
1166f48d5ff1SMasahide NAKAMURA 		rp->offset   = 4;
1167f48d5ff1SMasahide NAKAMURA 		break;
1168f48d5ff1SMasahide NAKAMURA 	default:
1169f48d5ff1SMasahide NAKAMURA 		break;
11701da177e4SLinus Torvalds 	}
11711da177e4SLinus Torvalds 	return(0);
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds 
1174c5a432f1SEric Dumazet DEFINE_PROTO_INUSE(rawv6)
1175c5a432f1SEric Dumazet 
11761da177e4SLinus Torvalds struct proto rawv6_prot = {
11771da177e4SLinus Torvalds 	.name		   = "RAWv6",
11781da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
11791da177e4SLinus Torvalds 	.close		   = rawv6_close,
11801da177e4SLinus Torvalds 	.connect	   = ip6_datagram_connect,
11811da177e4SLinus Torvalds 	.disconnect	   = udp_disconnect,
11821da177e4SLinus Torvalds 	.ioctl		   = rawv6_ioctl,
11831da177e4SLinus Torvalds 	.init		   = rawv6_init_sk,
11841da177e4SLinus Torvalds 	.destroy	   = inet6_destroy_sock,
11851da177e4SLinus Torvalds 	.setsockopt	   = rawv6_setsockopt,
11861da177e4SLinus Torvalds 	.getsockopt	   = rawv6_getsockopt,
11871da177e4SLinus Torvalds 	.sendmsg	   = rawv6_sendmsg,
11881da177e4SLinus Torvalds 	.recvmsg	   = rawv6_recvmsg,
11891da177e4SLinus Torvalds 	.bind		   = rawv6_bind,
11901da177e4SLinus Torvalds 	.backlog_rcv	   = rawv6_rcv_skb,
1191fc8717baSPavel Emelyanov 	.hash		   = raw_hash_sk,
1192fc8717baSPavel Emelyanov 	.unhash		   = raw_unhash_sk,
11931da177e4SLinus Torvalds 	.obj_size	   = sizeof(struct raw6_sock),
1194fc8717baSPavel Emelyanov 	.h.raw_hash	   = &raw_v6_hashinfo,
1195543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
1196543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_rawv6_setsockopt,
1197543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_rawv6_getsockopt,
1198543d9cfeSArnaldo Carvalho de Melo #endif
1199c5a432f1SEric Dumazet 	REF_PROTO_INUSE(rawv6)
12001da177e4SLinus Torvalds };
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
12031da177e4SLinus Torvalds static void raw6_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
12041da177e4SLinus Torvalds {
12051da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sp);
12061da177e4SLinus Torvalds 	struct in6_addr *dest, *src;
12071da177e4SLinus Torvalds 	__u16 destp, srcp;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	dest  = &np->daddr;
12101da177e4SLinus Torvalds 	src   = &np->rcv_saddr;
12111da177e4SLinus Torvalds 	destp = 0;
12121da177e4SLinus Torvalds 	srcp  = inet_sk(sp)->num;
12131da177e4SLinus Torvalds 	seq_printf(seq,
12141da177e4SLinus Torvalds 		   "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1215a92aa318SWang Chen 		   "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
12161da177e4SLinus Torvalds 		   i,
12171da177e4SLinus Torvalds 		   src->s6_addr32[0], src->s6_addr32[1],
12181da177e4SLinus Torvalds 		   src->s6_addr32[2], src->s6_addr32[3], srcp,
12191da177e4SLinus Torvalds 		   dest->s6_addr32[0], dest->s6_addr32[1],
12201da177e4SLinus Torvalds 		   dest->s6_addr32[2], dest->s6_addr32[3], destp,
12211da177e4SLinus Torvalds 		   sp->sk_state,
12221da177e4SLinus Torvalds 		   atomic_read(&sp->sk_wmem_alloc),
12231da177e4SLinus Torvalds 		   atomic_read(&sp->sk_rmem_alloc),
12241da177e4SLinus Torvalds 		   0, 0L, 0,
12251da177e4SLinus Torvalds 		   sock_i_uid(sp), 0,
12261da177e4SLinus Torvalds 		   sock_i_ino(sp),
1227a92aa318SWang Chen 		   atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds static int raw6_seq_show(struct seq_file *seq, void *v)
12311da177e4SLinus Torvalds {
12321da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
12331da177e4SLinus Torvalds 		seq_printf(seq,
12341da177e4SLinus Torvalds 			   "  sl  "
12351da177e4SLinus Torvalds 			   "local_address                         "
12361da177e4SLinus Torvalds 			   "remote_address                        "
12371da177e4SLinus Torvalds 			   "st tx_queue rx_queue tr tm->when retrnsmt"
1238a92aa318SWang Chen 			   "   uid  timeout inode  drops\n");
12391da177e4SLinus Torvalds 	else
124042a73808SPavel Emelyanov 		raw6_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
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 
1264a308da16SPavel Emelyanov static int raw6_init_net(struct net *net)
1265a308da16SPavel Emelyanov {
1266a308da16SPavel Emelyanov 	if (!proc_net_fops_create(net, "raw6", S_IRUGO, &raw6_seq_fops))
1267a308da16SPavel Emelyanov 		return -ENOMEM;
1268a308da16SPavel Emelyanov 
1269a308da16SPavel Emelyanov 	return 0;
1270a308da16SPavel Emelyanov }
1271a308da16SPavel Emelyanov 
1272a308da16SPavel Emelyanov static void raw6_exit_net(struct net *net)
1273a308da16SPavel Emelyanov {
1274a308da16SPavel Emelyanov 	proc_net_remove(net, "raw6");
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 	.capability	= CAP_NET_RAW,
13257f4e4868SDaniel Lezcano 	.no_check	= UDP_CSUM_DEFAULT,
13267f4e4868SDaniel Lezcano 	.flags		= INET_PROTOSW_REUSE,
13277f4e4868SDaniel Lezcano };
13287f4e4868SDaniel Lezcano 
13297f4e4868SDaniel Lezcano int __init rawv6_init(void)
13307f4e4868SDaniel Lezcano {
13317f4e4868SDaniel Lezcano 	int ret;
13327f4e4868SDaniel Lezcano 
13337f4e4868SDaniel Lezcano 	ret = inet6_register_protosw(&rawv6_protosw);
13347f4e4868SDaniel Lezcano 	if (ret)
13357f4e4868SDaniel Lezcano 		goto out;
13367f4e4868SDaniel Lezcano out:
13377f4e4868SDaniel Lezcano 	return ret;
13387f4e4868SDaniel Lezcano }
13397f4e4868SDaniel Lezcano 
134009f7709fSDaniel Lezcano void rawv6_exit(void)
13417f4e4868SDaniel Lezcano {
13427f4e4868SDaniel Lezcano 	inet6_unregister_protosw(&rawv6_protosw);
13437f4e4868SDaniel Lezcano }
1344