xref: /openbmc/linux/net/ipv6/raw.c (revision 3ef9d943)
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 
381da177e4SLinus Torvalds #include <net/ip.h>
391da177e4SLinus Torvalds #include <net/sock.h>
401da177e4SLinus Torvalds #include <net/snmp.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <net/ipv6.h>
431da177e4SLinus Torvalds #include <net/ndisc.h>
441da177e4SLinus Torvalds #include <net/protocol.h>
451da177e4SLinus Torvalds #include <net/ip6_route.h>
461da177e4SLinus Torvalds #include <net/ip6_checksum.h>
471da177e4SLinus Torvalds #include <net/addrconf.h>
481da177e4SLinus Torvalds #include <net/transp_v6.h>
491da177e4SLinus Torvalds #include <net/udp.h>
501da177e4SLinus Torvalds #include <net/inet_common.h>
51c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
5259fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
537be96f76SMasahide NAKAMURA #include <net/mip6.h>
547be96f76SMasahide NAKAMURA #endif
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #include <net/rawv6.h>
571da177e4SLinus Torvalds #include <net/xfrm.h>
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #include <linux/proc_fs.h>
601da177e4SLinus Torvalds #include <linux/seq_file.h>
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds struct hlist_head raw_v6_htable[RAWV6_HTABLE_SIZE];
631da177e4SLinus Torvalds DEFINE_RWLOCK(raw_v6_lock);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds static void raw_v6_hash(struct sock *sk)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds 	struct hlist_head *list = &raw_v6_htable[inet_sk(sk)->num &
681da177e4SLinus Torvalds 						 (RAWV6_HTABLE_SIZE - 1)];
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 	write_lock_bh(&raw_v6_lock);
711da177e4SLinus Torvalds 	sk_add_node(sk, list);
721da177e4SLinus Torvalds 	sock_prot_inc_use(sk->sk_prot);
731da177e4SLinus Torvalds 	write_unlock_bh(&raw_v6_lock);
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static void raw_v6_unhash(struct sock *sk)
771da177e4SLinus Torvalds {
781da177e4SLinus Torvalds 	write_lock_bh(&raw_v6_lock);
791da177e4SLinus Torvalds 	if (sk_del_node_init(sk))
801da177e4SLinus Torvalds 		sock_prot_dec_use(sk->sk_prot);
811da177e4SLinus Torvalds 	write_unlock_bh(&raw_v6_lock);
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /* Grumble... icmp and ip_input want to get at this... */
861da177e4SLinus Torvalds struct sock *__raw_v6_lookup(struct sock *sk, unsigned short num,
870bd1b59bSAndrew McDonald 			     struct in6_addr *loc_addr, struct in6_addr *rmt_addr,
880bd1b59bSAndrew McDonald 			     int dif)
891da177e4SLinus Torvalds {
901da177e4SLinus Torvalds 	struct hlist_node *node;
911da177e4SLinus Torvalds 	int is_multicast = ipv6_addr_is_multicast(loc_addr);
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	sk_for_each_from(sk, node)
941da177e4SLinus Torvalds 		if (inet_sk(sk)->num == num) {
951da177e4SLinus Torvalds 			struct ipv6_pinfo *np = inet6_sk(sk);
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 			if (!ipv6_addr_any(&np->daddr) &&
981da177e4SLinus Torvalds 			    !ipv6_addr_equal(&np->daddr, rmt_addr))
991da177e4SLinus Torvalds 				continue;
1001da177e4SLinus Torvalds 
1010bd1b59bSAndrew McDonald 			if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
1020bd1b59bSAndrew McDonald 				continue;
1030bd1b59bSAndrew McDonald 
1041da177e4SLinus Torvalds 			if (!ipv6_addr_any(&np->rcv_saddr)) {
1051da177e4SLinus Torvalds 				if (ipv6_addr_equal(&np->rcv_saddr, loc_addr))
1061da177e4SLinus Torvalds 					goto found;
1071da177e4SLinus Torvalds 				if (is_multicast &&
1081da177e4SLinus Torvalds 				    inet6_mc_check(sk, loc_addr, rmt_addr))
1091da177e4SLinus Torvalds 					goto found;
1101da177e4SLinus Torvalds 				continue;
1111da177e4SLinus Torvalds 			}
1121da177e4SLinus Torvalds 			goto found;
1131da177e4SLinus Torvalds 		}
1141da177e4SLinus Torvalds 	sk = NULL;
1151da177e4SLinus Torvalds found:
1161da177e4SLinus Torvalds 	return sk;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /*
1201da177e4SLinus Torvalds  *	0 - deliver
1211da177e4SLinus Torvalds  *	1 - block
1221da177e4SLinus Torvalds  */
1231da177e4SLinus Torvalds static __inline__ int icmpv6_filter(struct sock *sk, struct sk_buff *skb)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	struct icmp6hdr *icmph;
1261da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	if (pskb_may_pull(skb, sizeof(struct icmp6hdr))) {
1291da177e4SLinus Torvalds 		__u32 *data = &rp->filter.data[0];
1301da177e4SLinus Torvalds 		int bit_nr;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 		icmph = (struct icmp6hdr *) skb->data;
1331da177e4SLinus Torvalds 		bit_nr = icmph->icmp6_type;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 		return (data[bit_nr >> 5] & (1 << (bit_nr & 31))) != 0;
1361da177e4SLinus Torvalds 	}
1371da177e4SLinus Torvalds 	return 0;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
14059fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
14159fbb3a6SMasahide NAKAMURA static int (*mh_filter)(struct sock *sock, struct sk_buff *skb);
14259fbb3a6SMasahide NAKAMURA 
14359fbb3a6SMasahide NAKAMURA int rawv6_mh_filter_register(int (*filter)(struct sock *sock,
14459fbb3a6SMasahide NAKAMURA 					   struct sk_buff *skb))
14559fbb3a6SMasahide NAKAMURA {
14659fbb3a6SMasahide NAKAMURA 	rcu_assign_pointer(mh_filter, filter);
14759fbb3a6SMasahide NAKAMURA 	return 0;
14859fbb3a6SMasahide NAKAMURA }
14959fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_register);
15059fbb3a6SMasahide NAKAMURA 
15159fbb3a6SMasahide NAKAMURA int rawv6_mh_filter_unregister(int (*filter)(struct sock *sock,
15259fbb3a6SMasahide NAKAMURA 					     struct sk_buff *skb))
15359fbb3a6SMasahide NAKAMURA {
15459fbb3a6SMasahide NAKAMURA 	rcu_assign_pointer(mh_filter, NULL);
15559fbb3a6SMasahide NAKAMURA 	synchronize_rcu();
15659fbb3a6SMasahide NAKAMURA 	return 0;
15759fbb3a6SMasahide NAKAMURA }
15859fbb3a6SMasahide NAKAMURA EXPORT_SYMBOL(rawv6_mh_filter_unregister);
15959fbb3a6SMasahide NAKAMURA 
16059fbb3a6SMasahide NAKAMURA #endif
16159fbb3a6SMasahide NAKAMURA 
1621da177e4SLinus Torvalds /*
1631da177e4SLinus Torvalds  *	demultiplex raw sockets.
1641da177e4SLinus Torvalds  *	(should consider queueing the skb in the sock receive_queue
1651da177e4SLinus Torvalds  *	without calling rawv6.c)
1661da177e4SLinus Torvalds  *
1671da177e4SLinus Torvalds  *	Caller owns SKB so we must make clones.
1681da177e4SLinus Torvalds  */
169d13964f4SPatrick McHardy int ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	struct in6_addr *saddr;
1721da177e4SLinus Torvalds 	struct in6_addr *daddr;
1731da177e4SLinus Torvalds 	struct sock *sk;
174d13964f4SPatrick McHardy 	int delivered = 0;
1751da177e4SLinus Torvalds 	__u8 hash;
1761da177e4SLinus Torvalds 
1770660e03fSArnaldo Carvalho de Melo 	saddr = &ipv6_hdr(skb)->saddr;
1781da177e4SLinus Torvalds 	daddr = saddr + 1;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	hash = nexthdr & (MAX_INET_PROTOS - 1);
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	read_lock(&raw_v6_lock);
1831da177e4SLinus Torvalds 	sk = sk_head(&raw_v6_htable[hash]);
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	/*
1861da177e4SLinus Torvalds 	 *	The first socket found will be delivered after
1871da177e4SLinus Torvalds 	 *	delivery to transport protocols.
1881da177e4SLinus Torvalds 	 */
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	if (sk == NULL)
1911da177e4SLinus Torvalds 		goto out;
1921da177e4SLinus Torvalds 
1932dac4b96SYOSHIFUJI Hideaki 	sk = __raw_v6_lookup(sk, nexthdr, daddr, saddr, IP6CB(skb)->iif);
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	while (sk) {
1967be96f76SMasahide NAKAMURA 		int filtered;
1977be96f76SMasahide NAKAMURA 
198d13964f4SPatrick McHardy 		delivered = 1;
1997be96f76SMasahide NAKAMURA 		switch (nexthdr) {
2007be96f76SMasahide NAKAMURA 		case IPPROTO_ICMPV6:
2017be96f76SMasahide NAKAMURA 			filtered = icmpv6_filter(sk, skb);
2027be96f76SMasahide NAKAMURA 			break;
20359fbb3a6SMasahide NAKAMURA 
20459fbb3a6SMasahide NAKAMURA #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
2057be96f76SMasahide NAKAMURA 		case IPPROTO_MH:
20659fbb3a6SMasahide NAKAMURA 		{
2077be96f76SMasahide NAKAMURA 			/* XXX: To validate MH only once for each packet,
2087be96f76SMasahide NAKAMURA 			 * this is placed here. It should be after checking
2097be96f76SMasahide NAKAMURA 			 * xfrm policy, however it doesn't. The checking xfrm
2107be96f76SMasahide NAKAMURA 			 * policy is placed in rawv6_rcv() because it is
2117be96f76SMasahide NAKAMURA 			 * required for each socket.
2127be96f76SMasahide NAKAMURA 			 */
21359fbb3a6SMasahide NAKAMURA 			int (*filter)(struct sock *sock, struct sk_buff *skb);
21459fbb3a6SMasahide NAKAMURA 
21559fbb3a6SMasahide NAKAMURA 			filter = rcu_dereference(mh_filter);
21659fbb3a6SMasahide NAKAMURA 			filtered = filter ? filter(sk, skb) : 0;
2177be96f76SMasahide NAKAMURA 			break;
21859fbb3a6SMasahide NAKAMURA 		}
2197be96f76SMasahide NAKAMURA #endif
2207be96f76SMasahide NAKAMURA 		default:
2217be96f76SMasahide NAKAMURA 			filtered = 0;
2227be96f76SMasahide NAKAMURA 			break;
2237be96f76SMasahide NAKAMURA 		}
2247be96f76SMasahide NAKAMURA 
2257be96f76SMasahide NAKAMURA 		if (filtered < 0)
2267be96f76SMasahide NAKAMURA 			break;
2277be96f76SMasahide NAKAMURA 		if (filtered == 0) {
2281da177e4SLinus Torvalds 			struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 			/* Not releasing hash table! */
2319fb9cbb1SYasuyuki Kozakai 			if (clone) {
2329fb9cbb1SYasuyuki Kozakai 				nf_reset(clone);
2331da177e4SLinus Torvalds 				rawv6_rcv(sk, clone);
2341da177e4SLinus Torvalds 			}
2359fb9cbb1SYasuyuki Kozakai 		}
2360bd1b59bSAndrew McDonald 		sk = __raw_v6_lookup(sk_next(sk), nexthdr, daddr, saddr,
2372dac4b96SYOSHIFUJI Hideaki 				     IP6CB(skb)->iif);
2381da177e4SLinus Torvalds 	}
2391da177e4SLinus Torvalds out:
2401da177e4SLinus Torvalds 	read_unlock(&raw_v6_lock);
241d13964f4SPatrick McHardy 	return delivered;
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds /* This cleans up af_inet6 a bit. -DaveM */
2451da177e4SLinus Torvalds static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
2481da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
2491da177e4SLinus Torvalds 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
250e69a4adcSAl Viro 	__be32 v4addr = 0;
2511da177e4SLinus Torvalds 	int addr_type;
2521da177e4SLinus Torvalds 	int err;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	if (addr_len < SIN6_LEN_RFC2133)
2551da177e4SLinus Torvalds 		return -EINVAL;
2561da177e4SLinus Torvalds 	addr_type = ipv6_addr_type(&addr->sin6_addr);
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	/* Raw sockets are IPv6 only */
2591da177e4SLinus Torvalds 	if (addr_type == IPV6_ADDR_MAPPED)
2601da177e4SLinus Torvalds 		return(-EADDRNOTAVAIL);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	lock_sock(sk);
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	err = -EINVAL;
2651da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE)
2661da177e4SLinus Torvalds 		goto out;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	/* Check if the address belongs to the host. */
2691da177e4SLinus Torvalds 	if (addr_type != IPV6_ADDR_ANY) {
2701da177e4SLinus Torvalds 		struct net_device *dev = NULL;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 		if (addr_type & IPV6_ADDR_LINKLOCAL) {
2731da177e4SLinus Torvalds 			if (addr_len >= sizeof(struct sockaddr_in6) &&
2741da177e4SLinus Torvalds 			    addr->sin6_scope_id) {
2751da177e4SLinus Torvalds 				/* Override any existing binding, if another
2761da177e4SLinus Torvalds 				 * one is supplied by user.
2771da177e4SLinus Torvalds 				 */
2781da177e4SLinus Torvalds 				sk->sk_bound_dev_if = addr->sin6_scope_id;
2791da177e4SLinus Torvalds 			}
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 			/* Binding to link-local address requires an interface */
2821da177e4SLinus Torvalds 			if (!sk->sk_bound_dev_if)
2831da177e4SLinus Torvalds 				goto out;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 			dev = dev_get_by_index(sk->sk_bound_dev_if);
2861da177e4SLinus Torvalds 			if (!dev) {
2871da177e4SLinus Torvalds 				err = -ENODEV;
2881da177e4SLinus Torvalds 				goto out;
2891da177e4SLinus Torvalds 			}
2901da177e4SLinus Torvalds 		}
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 		/* ipv4 addr of the socket is invalid.  Only the
2931da177e4SLinus Torvalds 		 * unspecified and mapped address have a v4 equivalent.
2941da177e4SLinus Torvalds 		 */
2951da177e4SLinus Torvalds 		v4addr = LOOPBACK4_IPV6;
2961da177e4SLinus Torvalds 		if (!(addr_type & IPV6_ADDR_MULTICAST))	{
2971da177e4SLinus Torvalds 			err = -EADDRNOTAVAIL;
2981da177e4SLinus Torvalds 			if (!ipv6_chk_addr(&addr->sin6_addr, dev, 0)) {
2991da177e4SLinus Torvalds 				if (dev)
3001da177e4SLinus Torvalds 					dev_put(dev);
3011da177e4SLinus Torvalds 				goto out;
3021da177e4SLinus Torvalds 			}
3031da177e4SLinus Torvalds 		}
3041da177e4SLinus Torvalds 		if (dev)
3051da177e4SLinus Torvalds 			dev_put(dev);
3061da177e4SLinus Torvalds 	}
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	inet->rcv_saddr = inet->saddr = v4addr;
3091da177e4SLinus Torvalds 	ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
3101da177e4SLinus Torvalds 	if (!(addr_type & IPV6_ADDR_MULTICAST))
3111da177e4SLinus Torvalds 		ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
3121da177e4SLinus Torvalds 	err = 0;
3131da177e4SLinus Torvalds out:
3141da177e4SLinus Torvalds 	release_sock(sk);
3151da177e4SLinus Torvalds 	return err;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds void rawv6_err(struct sock *sk, struct sk_buff *skb,
3191da177e4SLinus Torvalds 	       struct inet6_skb_parm *opt,
32004ce6909SAl Viro 	       int type, int code, int offset, __be32 info)
3211da177e4SLinus Torvalds {
3221da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
3231da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
3241da177e4SLinus Torvalds 	int err;
3251da177e4SLinus Torvalds 	int harderr;
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	/* Report error on raw socket, if:
3281da177e4SLinus Torvalds 	   1. User requested recverr.
3291da177e4SLinus Torvalds 	   2. Socket is connected (otherwise the error indication
3301da177e4SLinus Torvalds 	      is useless without recverr and error is hard.
3311da177e4SLinus Torvalds 	 */
3321da177e4SLinus Torvalds 	if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
3331da177e4SLinus Torvalds 		return;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	harderr = icmpv6_err_convert(type, code, &err);
3361da177e4SLinus Torvalds 	if (type == ICMPV6_PKT_TOOBIG)
3371da177e4SLinus Torvalds 		harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	if (np->recverr) {
3401da177e4SLinus Torvalds 		u8 *payload = skb->data;
3411da177e4SLinus Torvalds 		if (!inet->hdrincl)
3421da177e4SLinus Torvalds 			payload += offset;
3431da177e4SLinus Torvalds 		ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
3441da177e4SLinus Torvalds 	}
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	if (np->recverr || harderr) {
3471da177e4SLinus Torvalds 		sk->sk_err = err;
3481da177e4SLinus Torvalds 		sk->sk_error_report(sk);
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds static inline int rawv6_rcv_skb(struct sock * sk, struct sk_buff * skb)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	if ((raw6_sk(sk)->checksum || sk->sk_filter) &&
355fb286bb2SHerbert Xu 	    skb_checksum_complete(skb)) {
3561da177e4SLinus Torvalds 		/* FIXME: increment a raw6 drops counter here */
3571da177e4SLinus Torvalds 		kfree_skb(skb);
3581da177e4SLinus Torvalds 		return 0;
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	/* Charge it to the socket. */
3621da177e4SLinus Torvalds 	if (sock_queue_rcv_skb(sk,skb)<0) {
3631da177e4SLinus Torvalds 		/* FIXME: increment a raw6 drops counter here */
3641da177e4SLinus Torvalds 		kfree_skb(skb);
3651da177e4SLinus Torvalds 		return 0;
3661da177e4SLinus Torvalds 	}
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	return 0;
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds /*
3721da177e4SLinus Torvalds  *	This is next to useless...
3731da177e4SLinus Torvalds  *	if we demultiplex in network layer we don't need the extra call
3741da177e4SLinus Torvalds  *	just to queue the skb...
3751da177e4SLinus Torvalds  *	maybe we could have the network decide upon a hint if it
3761da177e4SLinus Torvalds  *	should call raw_rcv for demultiplexing
3771da177e4SLinus Torvalds  */
3781da177e4SLinus Torvalds int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
3811da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
3841da177e4SLinus Torvalds 		kfree_skb(skb);
3851da177e4SLinus Torvalds 		return NET_RX_DROP;
3861da177e4SLinus Torvalds 	}
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	if (!rp->checksum)
3891da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_UNNECESSARY;
3901da177e4SLinus Torvalds 
39184fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
392d56f90a7SArnaldo Carvalho de Melo 		skb_postpull_rcsum(skb, skb_network_header(skb),
393cfe1fc77SArnaldo Carvalho de Melo 				   skb_network_header_len(skb));
3940660e03fSArnaldo Carvalho de Melo 		if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3950660e03fSArnaldo Carvalho de Melo 				     &ipv6_hdr(skb)->daddr,
396fb286bb2SHerbert Xu 				     skb->len, inet->num, skb->csum))
397fb286bb2SHerbert Xu 			skb->ip_summed = CHECKSUM_UNNECESSARY;
3981da177e4SLinus Torvalds 	}
39960476372SHerbert Xu 	if (!skb_csum_unnecessary(skb))
4000660e03fSArnaldo Carvalho de Melo 		skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4010660e03fSArnaldo Carvalho de Melo 							 &ipv6_hdr(skb)->daddr,
4020660e03fSArnaldo Carvalho de Melo 							 skb->len,
4030660e03fSArnaldo Carvalho de Melo 							 inet->num, 0));
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	if (inet->hdrincl) {
406fb286bb2SHerbert Xu 		if (skb_checksum_complete(skb)) {
4071da177e4SLinus Torvalds 			/* FIXME: increment a raw6 drops counter here */
4081da177e4SLinus Torvalds 			kfree_skb(skb);
4091da177e4SLinus Torvalds 			return 0;
4101da177e4SLinus Torvalds 		}
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	rawv6_rcv_skb(sk, skb);
4141da177e4SLinus Torvalds 	return 0;
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds /*
4191da177e4SLinus Torvalds  *	This should be easy, if there is something there
4201da177e4SLinus Torvalds  *	we return it, otherwise we block.
4211da177e4SLinus Torvalds  */
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk,
4241da177e4SLinus Torvalds 		  struct msghdr *msg, size_t len,
4251da177e4SLinus Torvalds 		  int noblock, int flags, int *addr_len)
4261da177e4SLinus Torvalds {
4271da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
4281da177e4SLinus Torvalds 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)msg->msg_name;
4291da177e4SLinus Torvalds 	struct sk_buff *skb;
4301da177e4SLinus Torvalds 	size_t copied;
4311da177e4SLinus Torvalds 	int err;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	if (flags & MSG_OOB)
4341da177e4SLinus Torvalds 		return -EOPNOTSUPP;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	if (addr_len)
4371da177e4SLinus Torvalds 		*addr_len=sizeof(*sin6);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	if (flags & MSG_ERRQUEUE)
4401da177e4SLinus Torvalds 		return ipv6_recv_error(sk, msg, len);
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	skb = skb_recv_datagram(sk, flags, noblock, &err);
4431da177e4SLinus Torvalds 	if (!skb)
4441da177e4SLinus Torvalds 		goto out;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	copied = skb->len;
4471da177e4SLinus Torvalds 	if (copied > len) {
4481da177e4SLinus Torvalds 		copied = len;
4491da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
4501da177e4SLinus Torvalds 	}
4511da177e4SLinus Torvalds 
45260476372SHerbert Xu 	if (skb_csum_unnecessary(skb)) {
4531da177e4SLinus Torvalds 		err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
4541da177e4SLinus Torvalds 	} else if (msg->msg_flags&MSG_TRUNC) {
455fb286bb2SHerbert Xu 		if (__skb_checksum_complete(skb))
4561da177e4SLinus Torvalds 			goto csum_copy_err;
4571da177e4SLinus Torvalds 		err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
4581da177e4SLinus Torvalds 	} else {
4591da177e4SLinus Torvalds 		err = skb_copy_and_csum_datagram_iovec(skb, 0, msg->msg_iov);
4601da177e4SLinus Torvalds 		if (err == -EINVAL)
4611da177e4SLinus Torvalds 			goto csum_copy_err;
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 	if (err)
4641da177e4SLinus Torvalds 		goto out_free;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	/* Copy the address. */
4671da177e4SLinus Torvalds 	if (sin6) {
4681da177e4SLinus Torvalds 		sin6->sin6_family = AF_INET6;
469f59fc7f3STetsuo Handa 		sin6->sin6_port = 0;
4700660e03fSArnaldo Carvalho de Melo 		ipv6_addr_copy(&sin6->sin6_addr, &ipv6_hdr(skb)->saddr);
4711da177e4SLinus Torvalds 		sin6->sin6_flowinfo = 0;
4721da177e4SLinus Torvalds 		sin6->sin6_scope_id = 0;
4731da177e4SLinus Torvalds 		if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
4741da177e4SLinus Torvalds 			sin6->sin6_scope_id = IP6CB(skb)->iif;
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	sock_recv_timestamp(msg, sk, skb);
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	if (np->rxopt.all)
4801da177e4SLinus Torvalds 		datagram_recv_ctl(sk, msg, skb);
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	err = copied;
4831da177e4SLinus Torvalds 	if (flags & MSG_TRUNC)
4841da177e4SLinus Torvalds 		err = skb->len;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds out_free:
4871da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
4881da177e4SLinus Torvalds out:
4891da177e4SLinus Torvalds 	return err;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds csum_copy_err:
4923305b80cSHerbert Xu 	skb_kill_datagram(sk, skb, flags);
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	/* Error for blocking case is chosen to masquerade
4951da177e4SLinus Torvalds 	   as some normal condition.
4961da177e4SLinus Torvalds 	 */
4971da177e4SLinus Torvalds 	err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
4981da177e4SLinus Torvalds 	/* FIXME: increment a raw6 drops counter here */
4993305b80cSHerbert Xu 	goto out;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds static int rawv6_push_pending_frames(struct sock *sk, struct flowi *fl,
503357b40a1SHerbert Xu 				     struct raw6_sock *rp)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	struct sk_buff *skb;
5061da177e4SLinus Torvalds 	int err = 0;
507357b40a1SHerbert Xu 	int offset;
508357b40a1SHerbert Xu 	int len;
509679a8738SHerbert Xu 	int total_len;
510868c86bcSAl Viro 	__wsum tmp_csum;
511868c86bcSAl Viro 	__sum16 csum;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	if (!rp->checksum)
5141da177e4SLinus Torvalds 		goto send;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
5171da177e4SLinus Torvalds 		goto out;
5181da177e4SLinus Torvalds 
519357b40a1SHerbert Xu 	offset = rp->offset;
520d56f90a7SArnaldo Carvalho de Melo 	total_len = inet_sk(sk)->cork.length - (skb_network_header(skb) -
521d56f90a7SArnaldo Carvalho de Melo 						skb->data);
522679a8738SHerbert Xu 	if (offset >= total_len - 1) {
5231da177e4SLinus Torvalds 		err = -EINVAL;
524357b40a1SHerbert Xu 		ip6_flush_pending_frames(sk);
5251da177e4SLinus Torvalds 		goto out;
5261da177e4SLinus Torvalds 	}
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	/* should be check HW csum miyazawa */
5291da177e4SLinus Torvalds 	if (skb_queue_len(&sk->sk_write_queue) == 1) {
5301da177e4SLinus Torvalds 		/*
5311da177e4SLinus Torvalds 		 * Only one fragment on the socket.
5321da177e4SLinus Torvalds 		 */
5331da177e4SLinus Torvalds 		tmp_csum = skb->csum;
5341da177e4SLinus Torvalds 	} else {
535357b40a1SHerbert Xu 		struct sk_buff *csum_skb = NULL;
5361da177e4SLinus Torvalds 		tmp_csum = 0;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 		skb_queue_walk(&sk->sk_write_queue, skb) {
5391da177e4SLinus Torvalds 			tmp_csum = csum_add(tmp_csum, skb->csum);
540357b40a1SHerbert Xu 
541357b40a1SHerbert Xu 			if (csum_skb)
542357b40a1SHerbert Xu 				continue;
543357b40a1SHerbert Xu 
544ea2ae17dSArnaldo Carvalho de Melo 			len = skb->len - skb_transport_offset(skb);
545357b40a1SHerbert Xu 			if (offset >= len) {
546357b40a1SHerbert Xu 				offset -= len;
547357b40a1SHerbert Xu 				continue;
5481da177e4SLinus Torvalds 			}
549357b40a1SHerbert Xu 
550357b40a1SHerbert Xu 			csum_skb = skb;
5511da177e4SLinus Torvalds 		}
5521da177e4SLinus Torvalds 
553357b40a1SHerbert Xu 		skb = csum_skb;
554357b40a1SHerbert Xu 	}
555357b40a1SHerbert Xu 
556ea2ae17dSArnaldo Carvalho de Melo 	offset += skb_transport_offset(skb);
557357b40a1SHerbert Xu 	if (skb_copy_bits(skb, offset, &csum, 2))
558357b40a1SHerbert Xu 		BUG();
559357b40a1SHerbert Xu 
5601da177e4SLinus Torvalds 	/* in case cksum was not initialized */
561357b40a1SHerbert Xu 	if (unlikely(csum))
5625f92a738SAl Viro 		tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
5631da177e4SLinus Torvalds 
564868c86bcSAl Viro 	csum = csum_ipv6_magic(&fl->fl6_src,
5651da177e4SLinus Torvalds 				   &fl->fl6_dst,
566679a8738SHerbert Xu 				   total_len, fl->proto, tmp_csum);
5671da177e4SLinus Torvalds 
568f6ab0288SAl Viro 	if (csum == 0 && fl->proto == IPPROTO_UDP)
569f6ab0288SAl Viro 		csum = CSUM_MANGLED_0;
570357b40a1SHerbert Xu 
571357b40a1SHerbert Xu 	if (skb_store_bits(skb, offset, &csum, 2))
572357b40a1SHerbert Xu 		BUG();
573357b40a1SHerbert Xu 
5741da177e4SLinus Torvalds send:
5751da177e4SLinus Torvalds 	err = ip6_push_pending_frames(sk);
5761da177e4SLinus Torvalds out:
5771da177e4SLinus Torvalds 	return err;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds static int rawv6_send_hdrinc(struct sock *sk, void *from, int length,
5811da177e4SLinus Torvalds 			struct flowi *fl, struct rt6_info *rt,
5821da177e4SLinus Torvalds 			unsigned int flags)
5831da177e4SLinus Torvalds {
5843320da89SHerbert Xu 	struct ipv6_pinfo *np = inet6_sk(sk);
5851da177e4SLinus Torvalds 	struct ipv6hdr *iph;
5861da177e4SLinus Torvalds 	struct sk_buff *skb;
5871da177e4SLinus Torvalds 	unsigned int hh_len;
5881da177e4SLinus Torvalds 	int err;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	if (length > rt->u.dst.dev->mtu) {
5911da177e4SLinus Torvalds 		ipv6_local_error(sk, EMSGSIZE, fl, rt->u.dst.dev->mtu);
5921da177e4SLinus Torvalds 		return -EMSGSIZE;
5931da177e4SLinus Torvalds 	}
5941da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
5951da177e4SLinus Torvalds 		goto out;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	skb = sock_alloc_send_skb(sk, length+hh_len+15,
6001da177e4SLinus Torvalds 				  flags&MSG_DONTWAIT, &err);
6011da177e4SLinus Torvalds 	if (skb == NULL)
6021da177e4SLinus Torvalds 		goto error;
6031da177e4SLinus Torvalds 	skb_reserve(skb, hh_len);
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
6061da177e4SLinus Torvalds 	skb->dst = dst_clone(&rt->u.dst);
6071da177e4SLinus Torvalds 
6081ced98e8SArnaldo Carvalho de Melo 	skb_put(skb, length);
6091ced98e8SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
6100660e03fSArnaldo Carvalho de Melo 	iph = ipv6_hdr(skb);
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	skb->ip_summed = CHECKSUM_NONE;
6131da177e4SLinus Torvalds 
614b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
6151da177e4SLinus Torvalds 	err = memcpy_fromiovecend((void *)iph, from, 0, length);
6161da177e4SLinus Torvalds 	if (err)
6171da177e4SLinus Torvalds 		goto error_fault;
6181da177e4SLinus Torvalds 
619a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTREQUESTS);
6201da177e4SLinus Torvalds 	err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
6211da177e4SLinus Torvalds 		      dst_output);
6221da177e4SLinus Torvalds 	if (err > 0)
6233320da89SHerbert Xu 		err = np->recverr ? net_xmit_errno(err) : 0;
6241da177e4SLinus Torvalds 	if (err)
6251da177e4SLinus Torvalds 		goto error;
6261da177e4SLinus Torvalds out:
6271da177e4SLinus Torvalds 	return 0;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds error_fault:
6301da177e4SLinus Torvalds 	err = -EFAULT;
6311da177e4SLinus Torvalds 	kfree_skb(skb);
6321da177e4SLinus Torvalds error:
633a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS(rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
6341da177e4SLinus Torvalds 	return err;
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
637a27b58feSHeiko Carstens static int rawv6_probe_proto_opt(struct flowi *fl, struct msghdr *msg)
6381da177e4SLinus Torvalds {
6391da177e4SLinus Torvalds 	struct iovec *iov;
6401da177e4SLinus Torvalds 	u8 __user *type = NULL;
6411da177e4SLinus Torvalds 	u8 __user *code = NULL;
6426e8f4d48SMasahide NAKAMURA 	u8 len = 0;
6431da177e4SLinus Torvalds 	int probed = 0;
6441da177e4SLinus Torvalds 	int i;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	if (!msg->msg_iov)
647a27b58feSHeiko Carstens 		return 0;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	for (i = 0; i < msg->msg_iovlen; i++) {
6501da177e4SLinus Torvalds 		iov = &msg->msg_iov[i];
6511da177e4SLinus Torvalds 		if (!iov)
6521da177e4SLinus Torvalds 			continue;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 		switch (fl->proto) {
6551da177e4SLinus Torvalds 		case IPPROTO_ICMPV6:
6561da177e4SLinus Torvalds 			/* check if one-byte field is readable or not. */
6571da177e4SLinus Torvalds 			if (iov->iov_base && iov->iov_len < 1)
6581da177e4SLinus Torvalds 				break;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 			if (!type) {
6611da177e4SLinus Torvalds 				type = iov->iov_base;
6621da177e4SLinus Torvalds 				/* check if code field is readable or not. */
6631da177e4SLinus Torvalds 				if (iov->iov_len > 1)
6641da177e4SLinus Torvalds 					code = type + 1;
6651da177e4SLinus Torvalds 			} else if (!code)
6661da177e4SLinus Torvalds 				code = iov->iov_base;
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 			if (type && code) {
669a27b58feSHeiko Carstens 				if (get_user(fl->fl_icmp_type, type) ||
670a27b58feSHeiko Carstens 				    get_user(fl->fl_icmp_code, code))
671a27b58feSHeiko Carstens 					return -EFAULT;
6721da177e4SLinus Torvalds 				probed = 1;
6731da177e4SLinus Torvalds 			}
6741da177e4SLinus Torvalds 			break;
6756e8f4d48SMasahide NAKAMURA 		case IPPROTO_MH:
6766e8f4d48SMasahide NAKAMURA 			if (iov->iov_base && iov->iov_len < 1)
6776e8f4d48SMasahide NAKAMURA 				break;
6786e8f4d48SMasahide NAKAMURA 			/* check if type field is readable or not. */
6796e8f4d48SMasahide NAKAMURA 			if (iov->iov_len > 2 - len) {
6806e8f4d48SMasahide NAKAMURA 				u8 __user *p = iov->iov_base;
681a27b58feSHeiko Carstens 				if (get_user(fl->fl_mh_type, &p[2 - len]))
682a27b58feSHeiko Carstens 					return -EFAULT;
6836e8f4d48SMasahide NAKAMURA 				probed = 1;
6846e8f4d48SMasahide NAKAMURA 			} else
6856e8f4d48SMasahide NAKAMURA 				len += iov->iov_len;
6866e8f4d48SMasahide NAKAMURA 
6876e8f4d48SMasahide NAKAMURA 			break;
6881da177e4SLinus Torvalds 		default:
6891da177e4SLinus Torvalds 			probed = 1;
6901da177e4SLinus Torvalds 			break;
6911da177e4SLinus Torvalds 		}
6921da177e4SLinus Torvalds 		if (probed)
6931da177e4SLinus Torvalds 			break;
6941da177e4SLinus Torvalds 	}
695a27b58feSHeiko Carstens 	return 0;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
6991da177e4SLinus Torvalds 		   struct msghdr *msg, size_t len)
7001da177e4SLinus Torvalds {
7011da177e4SLinus Torvalds 	struct ipv6_txoptions opt_space;
7021da177e4SLinus Torvalds 	struct sockaddr_in6 * sin6 = (struct sockaddr_in6 *) msg->msg_name;
7031da177e4SLinus Torvalds 	struct in6_addr *daddr, *final_p = NULL, final;
7041da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
7051da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
7061da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
7071da177e4SLinus Torvalds 	struct ipv6_txoptions *opt = NULL;
7081da177e4SLinus Torvalds 	struct ip6_flowlabel *flowlabel = NULL;
7091da177e4SLinus Torvalds 	struct dst_entry *dst = NULL;
7101da177e4SLinus Torvalds 	struct flowi fl;
7111da177e4SLinus Torvalds 	int addr_len = msg->msg_namelen;
7121da177e4SLinus Torvalds 	int hlimit = -1;
71341a1f8eaSYOSHIFUJI Hideaki 	int tclass = -1;
7141da177e4SLinus Torvalds 	u16 proto;
7151da177e4SLinus Torvalds 	int err;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	/* Rough check on arithmetic overflow,
718b59e139bSYOSHIFUJI Hideaki 	   better check is made in ip6_append_data().
7191da177e4SLinus Torvalds 	 */
720b59e139bSYOSHIFUJI Hideaki 	if (len > INT_MAX)
7211da177e4SLinus Torvalds 		return -EMSGSIZE;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	/* Mirror BSD error message compatibility */
7241da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
7251da177e4SLinus Torvalds 		return -EOPNOTSUPP;
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	/*
7281da177e4SLinus Torvalds 	 *	Get and verify the address.
7291da177e4SLinus Torvalds 	 */
7301da177e4SLinus Torvalds 	memset(&fl, 0, sizeof(fl));
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	if (sin6) {
7331da177e4SLinus Torvalds 		if (addr_len < SIN6_LEN_RFC2133)
7341da177e4SLinus Torvalds 			return -EINVAL;
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 		if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
7371da177e4SLinus Torvalds 			return(-EAFNOSUPPORT);
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 		/* port is the proto value [0..255] carried in nexthdr */
7401da177e4SLinus Torvalds 		proto = ntohs(sin6->sin6_port);
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 		if (!proto)
7431da177e4SLinus Torvalds 			proto = inet->num;
7441da177e4SLinus Torvalds 		else if (proto != inet->num)
7451da177e4SLinus Torvalds 			return(-EINVAL);
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 		if (proto > 255)
7481da177e4SLinus Torvalds 			return(-EINVAL);
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 		daddr = &sin6->sin6_addr;
7511da177e4SLinus Torvalds 		if (np->sndflow) {
7521da177e4SLinus Torvalds 			fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
7531da177e4SLinus Torvalds 			if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
7541da177e4SLinus Torvalds 				flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
7551da177e4SLinus Torvalds 				if (flowlabel == NULL)
7561da177e4SLinus Torvalds 					return -EINVAL;
7571da177e4SLinus Torvalds 				daddr = &flowlabel->dst;
7581da177e4SLinus Torvalds 			}
7591da177e4SLinus Torvalds 		}
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 		/*
7621da177e4SLinus Torvalds 		 * Otherwise it will be difficult to maintain
7631da177e4SLinus Torvalds 		 * sk->sk_dst_cache.
7641da177e4SLinus Torvalds 		 */
7651da177e4SLinus Torvalds 		if (sk->sk_state == TCP_ESTABLISHED &&
7661da177e4SLinus Torvalds 		    ipv6_addr_equal(daddr, &np->daddr))
7671da177e4SLinus Torvalds 			daddr = &np->daddr;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 		if (addr_len >= sizeof(struct sockaddr_in6) &&
7701da177e4SLinus Torvalds 		    sin6->sin6_scope_id &&
7711da177e4SLinus Torvalds 		    ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
7721da177e4SLinus Torvalds 			fl.oif = sin6->sin6_scope_id;
7731da177e4SLinus Torvalds 	} else {
7741da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED)
7751da177e4SLinus Torvalds 			return -EDESTADDRREQ;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 		proto = inet->num;
7781da177e4SLinus Torvalds 		daddr = &np->daddr;
7791da177e4SLinus Torvalds 		fl.fl6_flowlabel = np->flow_label;
7801da177e4SLinus Torvalds 	}
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	if (ipv6_addr_any(daddr)) {
7831da177e4SLinus Torvalds 		/*
7841da177e4SLinus Torvalds 		 * unspecified destination address
7851da177e4SLinus Torvalds 		 * treated as error... is this correct ?
7861da177e4SLinus Torvalds 		 */
7871da177e4SLinus Torvalds 		fl6_sock_release(flowlabel);
7881da177e4SLinus Torvalds 		return(-EINVAL);
7891da177e4SLinus Torvalds 	}
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	if (fl.oif == 0)
7921da177e4SLinus Torvalds 		fl.oif = sk->sk_bound_dev_if;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	if (msg->msg_controllen) {
7951da177e4SLinus Torvalds 		opt = &opt_space;
7961da177e4SLinus Torvalds 		memset(opt, 0, sizeof(struct ipv6_txoptions));
7971da177e4SLinus Torvalds 		opt->tot_len = sizeof(struct ipv6_txoptions);
7981da177e4SLinus Torvalds 
79941a1f8eaSYOSHIFUJI Hideaki 		err = datagram_send_ctl(msg, &fl, opt, &hlimit, &tclass);
8001da177e4SLinus Torvalds 		if (err < 0) {
8011da177e4SLinus Torvalds 			fl6_sock_release(flowlabel);
8021da177e4SLinus Torvalds 			return err;
8031da177e4SLinus Torvalds 		}
8041da177e4SLinus Torvalds 		if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
8051da177e4SLinus Torvalds 			flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
8061da177e4SLinus Torvalds 			if (flowlabel == NULL)
8071da177e4SLinus Torvalds 				return -EINVAL;
8081da177e4SLinus Torvalds 		}
8091da177e4SLinus Torvalds 		if (!(opt->opt_nflen|opt->opt_flen))
8101da177e4SLinus Torvalds 			opt = NULL;
8111da177e4SLinus Torvalds 	}
8121da177e4SLinus Torvalds 	if (opt == NULL)
8131da177e4SLinus Torvalds 		opt = np->opt;
814df9890c3SYOSHIFUJI Hideaki 	if (flowlabel)
8151da177e4SLinus Torvalds 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
816df9890c3SYOSHIFUJI Hideaki 	opt = ipv6_fixup_options(&opt_space, opt);
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	fl.proto = proto;
819a27b58feSHeiko Carstens 	err = rawv6_probe_proto_opt(&fl, msg);
820a27b58feSHeiko Carstens 	if (err)
821a27b58feSHeiko Carstens 		goto out;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	ipv6_addr_copy(&fl.fl6_dst, daddr);
8241da177e4SLinus Torvalds 	if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
8251da177e4SLinus Torvalds 		ipv6_addr_copy(&fl.fl6_src, &np->saddr);
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	/* merge ip6_build_xmit from ip6_output */
8281da177e4SLinus Torvalds 	if (opt && opt->srcrt) {
8291da177e4SLinus Torvalds 		struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
8301da177e4SLinus Torvalds 		ipv6_addr_copy(&final, &fl.fl6_dst);
8311da177e4SLinus Torvalds 		ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
8321da177e4SLinus Torvalds 		final_p = &final;
8331da177e4SLinus Torvalds 	}
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst))
8361da177e4SLinus Torvalds 		fl.oif = np->mcast_oif;
837beb8d13bSVenkat Yekkirala 	security_sk_classify_flow(sk, &fl);
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	err = ip6_dst_lookup(sk, &dst, &fl);
8401da177e4SLinus Torvalds 	if (err)
8411da177e4SLinus Torvalds 		goto out;
8421da177e4SLinus Torvalds 	if (final_p)
8431da177e4SLinus Torvalds 		ipv6_addr_copy(&fl.fl6_dst, final_p);
8441da177e4SLinus Torvalds 
84514e50e57SDavid S. Miller 	if ((err = __xfrm_lookup(&dst, &fl, sk, 1)) < 0) {
84614e50e57SDavid S. Miller 		if (err == -EREMOTE)
84714e50e57SDavid S. Miller 			err = ip6_dst_blackhole(sk, &dst, &fl);
84814e50e57SDavid S. Miller 		if (err < 0)
8491da177e4SLinus Torvalds 			goto out;
85014e50e57SDavid S. Miller 	}
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	if (hlimit < 0) {
8531da177e4SLinus Torvalds 		if (ipv6_addr_is_multicast(&fl.fl6_dst))
8541da177e4SLinus Torvalds 			hlimit = np->mcast_hops;
8551da177e4SLinus Torvalds 		else
8561da177e4SLinus Torvalds 			hlimit = np->hop_limit;
8571da177e4SLinus Torvalds 		if (hlimit < 0)
8581da177e4SLinus Torvalds 			hlimit = dst_metric(dst, RTAX_HOPLIMIT);
8591da177e4SLinus Torvalds 		if (hlimit < 0)
8601da177e4SLinus Torvalds 			hlimit = ipv6_get_hoplimit(dst->dev);
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds 
86341a1f8eaSYOSHIFUJI Hideaki 	if (tclass < 0) {
864e012d51cSYOSHIFUJI Hideaki 		tclass = np->tclass;
86541a1f8eaSYOSHIFUJI Hideaki 		if (tclass < 0)
86641a1f8eaSYOSHIFUJI Hideaki 			tclass = 0;
86741a1f8eaSYOSHIFUJI Hideaki 	}
86841a1f8eaSYOSHIFUJI Hideaki 
8691da177e4SLinus Torvalds 	if (msg->msg_flags&MSG_CONFIRM)
8701da177e4SLinus Torvalds 		goto do_confirm;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds back_from_confirm:
8731da177e4SLinus Torvalds 	if (inet->hdrincl) {
8741da177e4SLinus Torvalds 		err = rawv6_send_hdrinc(sk, msg->msg_iov, len, &fl, (struct rt6_info*)dst, msg->msg_flags);
8751da177e4SLinus Torvalds 	} else {
8761da177e4SLinus Torvalds 		lock_sock(sk);
87741a1f8eaSYOSHIFUJI Hideaki 		err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov,
87841a1f8eaSYOSHIFUJI Hideaki 			len, 0, hlimit, tclass, opt, &fl, (struct rt6_info*)dst,
87941a1f8eaSYOSHIFUJI Hideaki 			msg->msg_flags);
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 		if (err)
8821da177e4SLinus Torvalds 			ip6_flush_pending_frames(sk);
8831da177e4SLinus Torvalds 		else if (!(msg->msg_flags & MSG_MORE))
884357b40a1SHerbert Xu 			err = rawv6_push_pending_frames(sk, &fl, rp);
8853ef9d943SYOSHIFUJI Hideaki 		release_sock(sk);
8861da177e4SLinus Torvalds 	}
8871da177e4SLinus Torvalds done:
8886d3e85ecSNicolas DICHTEL 	dst_release(dst);
8891da177e4SLinus Torvalds out:
8901da177e4SLinus Torvalds 	fl6_sock_release(flowlabel);
8911da177e4SLinus Torvalds 	return err<0?err:len;
8921da177e4SLinus Torvalds do_confirm:
8931da177e4SLinus Torvalds 	dst_confirm(dst);
8941da177e4SLinus Torvalds 	if (!(msg->msg_flags & MSG_PROBE) || len)
8951da177e4SLinus Torvalds 		goto back_from_confirm;
8961da177e4SLinus Torvalds 	err = 0;
8971da177e4SLinus Torvalds 	goto done;
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
9011da177e4SLinus Torvalds 			       char __user *optval, int optlen)
9021da177e4SLinus Torvalds {
9031da177e4SLinus Torvalds 	switch (optname) {
9041da177e4SLinus Torvalds 	case ICMPV6_FILTER:
9051da177e4SLinus Torvalds 		if (optlen > sizeof(struct icmp6_filter))
9061da177e4SLinus Torvalds 			optlen = sizeof(struct icmp6_filter);
9071da177e4SLinus Torvalds 		if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
9081da177e4SLinus Torvalds 			return -EFAULT;
9091da177e4SLinus Torvalds 		return 0;
9101da177e4SLinus Torvalds 	default:
9111da177e4SLinus Torvalds 		return -ENOPROTOOPT;
9123ff50b79SStephen Hemminger 	}
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	return 0;
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
9181da177e4SLinus Torvalds 			       char __user *optval, int __user *optlen)
9191da177e4SLinus Torvalds {
9201da177e4SLinus Torvalds 	int len;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	switch (optname) {
9231da177e4SLinus Torvalds 	case ICMPV6_FILTER:
9241da177e4SLinus Torvalds 		if (get_user(len, optlen))
9251da177e4SLinus Torvalds 			return -EFAULT;
9261da177e4SLinus Torvalds 		if (len < 0)
9271da177e4SLinus Torvalds 			return -EINVAL;
9281da177e4SLinus Torvalds 		if (len > sizeof(struct icmp6_filter))
9291da177e4SLinus Torvalds 			len = sizeof(struct icmp6_filter);
9301da177e4SLinus Torvalds 		if (put_user(len, optlen))
9311da177e4SLinus Torvalds 			return -EFAULT;
9321da177e4SLinus Torvalds 		if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
9331da177e4SLinus Torvalds 			return -EFAULT;
9341da177e4SLinus Torvalds 		return 0;
9351da177e4SLinus Torvalds 	default:
9361da177e4SLinus Torvalds 		return -ENOPROTOOPT;
9373ff50b79SStephen Hemminger 	}
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	return 0;
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 
9433fdadf7dSDmitry Mishin static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
9441da177e4SLinus Torvalds 			    char __user *optval, int optlen)
9451da177e4SLinus Torvalds {
9461da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
9471da177e4SLinus Torvalds 	int val;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	if (get_user(val, (int __user *)optval))
9501da177e4SLinus Torvalds 		return -EFAULT;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	switch (optname) {
9531da177e4SLinus Torvalds 		case IPV6_CHECKSUM:
9541da177e4SLinus Torvalds 			/* You may get strange result with a positive odd offset;
9551da177e4SLinus Torvalds 			   RFC2292bis agrees with me. */
9561da177e4SLinus Torvalds 			if (val > 0 && (val&1))
9571da177e4SLinus Torvalds 				return(-EINVAL);
9581da177e4SLinus Torvalds 			if (val < 0) {
9591da177e4SLinus Torvalds 				rp->checksum = 0;
9601da177e4SLinus Torvalds 			} else {
9611da177e4SLinus Torvalds 				rp->checksum = 1;
9621da177e4SLinus Torvalds 				rp->offset = val;
9631da177e4SLinus Torvalds 			}
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 			return 0;
9661da177e4SLinus Torvalds 			break;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 		default:
9691da177e4SLinus Torvalds 			return(-ENOPROTOOPT);
9701da177e4SLinus Torvalds 	}
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
9733fdadf7dSDmitry Mishin static int rawv6_setsockopt(struct sock *sk, int level, int optname,
9743fdadf7dSDmitry Mishin 			  char __user *optval, int optlen)
9751da177e4SLinus Torvalds {
9761da177e4SLinus Torvalds 	switch(level) {
9771da177e4SLinus Torvalds 		case SOL_RAW:
9781da177e4SLinus Torvalds 			break;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 		case SOL_ICMPV6:
9811da177e4SLinus Torvalds 			if (inet_sk(sk)->num != IPPROTO_ICMPV6)
9821da177e4SLinus Torvalds 				return -EOPNOTSUPP;
9833fdadf7dSDmitry Mishin 			return rawv6_seticmpfilter(sk, level, optname, optval,
9841da177e4SLinus Torvalds 						   optlen);
9851da177e4SLinus Torvalds 		case SOL_IPV6:
9861da177e4SLinus Torvalds 			if (optname == IPV6_CHECKSUM)
9871da177e4SLinus Torvalds 				break;
9881da177e4SLinus Torvalds 		default:
9893fdadf7dSDmitry Mishin 			return ipv6_setsockopt(sk, level, optname, optval,
9901da177e4SLinus Torvalds 					       optlen);
9913ff50b79SStephen Hemminger 	}
9923ff50b79SStephen Hemminger 
9933fdadf7dSDmitry Mishin 	return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
9943fdadf7dSDmitry Mishin }
9953fdadf7dSDmitry Mishin 
9963fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
9973fdadf7dSDmitry Mishin static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname,
9983fdadf7dSDmitry Mishin 				   char __user *optval, int optlen)
9993fdadf7dSDmitry Mishin {
10003fdadf7dSDmitry Mishin 	switch (level) {
10013fdadf7dSDmitry Mishin 	case SOL_RAW:
10023fdadf7dSDmitry Mishin 		break;
10033fdadf7dSDmitry Mishin 	case SOL_ICMPV6:
10043fdadf7dSDmitry Mishin 		if (inet_sk(sk)->num != IPPROTO_ICMPV6)
10053fdadf7dSDmitry Mishin 			return -EOPNOTSUPP;
1006543d9cfeSArnaldo Carvalho de Melo 		return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
10073fdadf7dSDmitry Mishin 	case SOL_IPV6:
10083fdadf7dSDmitry Mishin 		if (optname == IPV6_CHECKSUM)
10093fdadf7dSDmitry Mishin 			break;
10103fdadf7dSDmitry Mishin 	default:
1011543d9cfeSArnaldo Carvalho de Melo 		return compat_ipv6_setsockopt(sk, level, optname,
1012543d9cfeSArnaldo Carvalho de Melo 					      optval, optlen);
10133ff50b79SStephen Hemminger 	}
10143fdadf7dSDmitry Mishin 	return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
10153fdadf7dSDmitry Mishin }
10163fdadf7dSDmitry Mishin #endif
10173fdadf7dSDmitry Mishin 
10183fdadf7dSDmitry Mishin static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
10193fdadf7dSDmitry Mishin 			    char __user *optval, int __user *optlen)
10203fdadf7dSDmitry Mishin {
10213fdadf7dSDmitry Mishin 	struct raw6_sock *rp = raw6_sk(sk);
10223fdadf7dSDmitry Mishin 	int val, len;
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	if (get_user(len,optlen))
10251da177e4SLinus Torvalds 		return -EFAULT;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	switch (optname) {
10281da177e4SLinus Torvalds 	case IPV6_CHECKSUM:
10291da177e4SLinus Torvalds 		if (rp->checksum == 0)
10301da177e4SLinus Torvalds 			val = -1;
10311da177e4SLinus Torvalds 		else
10321da177e4SLinus Torvalds 			val = rp->offset;
10331da177e4SLinus Torvalds 		break;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	default:
10361da177e4SLinus Torvalds 		return -ENOPROTOOPT;
10371da177e4SLinus Torvalds 	}
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 	len = min_t(unsigned int, sizeof(int), len);
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 	if (put_user(len, optlen))
10421da177e4SLinus Torvalds 		return -EFAULT;
10431da177e4SLinus Torvalds 	if (copy_to_user(optval,&val,len))
10441da177e4SLinus Torvalds 		return -EFAULT;
10451da177e4SLinus Torvalds 	return 0;
10461da177e4SLinus Torvalds }
10471da177e4SLinus Torvalds 
10483fdadf7dSDmitry Mishin static int rawv6_getsockopt(struct sock *sk, int level, int optname,
10493fdadf7dSDmitry Mishin 			  char __user *optval, int __user *optlen)
10503fdadf7dSDmitry Mishin {
10513fdadf7dSDmitry Mishin 	switch(level) {
10523fdadf7dSDmitry Mishin 		case SOL_RAW:
10533fdadf7dSDmitry Mishin 			break;
10543fdadf7dSDmitry Mishin 
10553fdadf7dSDmitry Mishin 		case SOL_ICMPV6:
10563fdadf7dSDmitry Mishin 			if (inet_sk(sk)->num != IPPROTO_ICMPV6)
10573fdadf7dSDmitry Mishin 				return -EOPNOTSUPP;
10583fdadf7dSDmitry Mishin 			return rawv6_geticmpfilter(sk, level, optname, optval,
10593fdadf7dSDmitry Mishin 						   optlen);
10603fdadf7dSDmitry Mishin 		case SOL_IPV6:
10613fdadf7dSDmitry Mishin 			if (optname == IPV6_CHECKSUM)
10623fdadf7dSDmitry Mishin 				break;
10633fdadf7dSDmitry Mishin 		default:
10643fdadf7dSDmitry Mishin 			return ipv6_getsockopt(sk, level, optname, optval,
10653fdadf7dSDmitry Mishin 					       optlen);
10663ff50b79SStephen Hemminger 	}
10673ff50b79SStephen Hemminger 
10683fdadf7dSDmitry Mishin 	return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
10693fdadf7dSDmitry Mishin }
10703fdadf7dSDmitry Mishin 
10713fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
10723fdadf7dSDmitry Mishin static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname,
10733fdadf7dSDmitry Mishin 				   char __user *optval, int __user *optlen)
10743fdadf7dSDmitry Mishin {
10753fdadf7dSDmitry Mishin 	switch (level) {
10763fdadf7dSDmitry Mishin 	case SOL_RAW:
10773fdadf7dSDmitry Mishin 		break;
10783fdadf7dSDmitry Mishin 	case SOL_ICMPV6:
10793fdadf7dSDmitry Mishin 		if (inet_sk(sk)->num != IPPROTO_ICMPV6)
10803fdadf7dSDmitry Mishin 			return -EOPNOTSUPP;
1081543d9cfeSArnaldo Carvalho de Melo 		return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
10823fdadf7dSDmitry Mishin 	case SOL_IPV6:
10833fdadf7dSDmitry Mishin 		if (optname == IPV6_CHECKSUM)
10843fdadf7dSDmitry Mishin 			break;
10853fdadf7dSDmitry Mishin 	default:
1086543d9cfeSArnaldo Carvalho de Melo 		return compat_ipv6_getsockopt(sk, level, optname,
1087543d9cfeSArnaldo Carvalho de Melo 					      optval, optlen);
10883ff50b79SStephen Hemminger 	}
10893fdadf7dSDmitry Mishin 	return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
10903fdadf7dSDmitry Mishin }
10913fdadf7dSDmitry Mishin #endif
10923fdadf7dSDmitry Mishin 
10931da177e4SLinus Torvalds static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
10941da177e4SLinus Torvalds {
10951da177e4SLinus Torvalds 	switch(cmd) {
10961da177e4SLinus Torvalds 		case SIOCOUTQ:
10971da177e4SLinus Torvalds 		{
10981da177e4SLinus Torvalds 			int amount = atomic_read(&sk->sk_wmem_alloc);
10991da177e4SLinus Torvalds 			return put_user(amount, (int __user *)arg);
11001da177e4SLinus Torvalds 		}
11011da177e4SLinus Torvalds 		case SIOCINQ:
11021da177e4SLinus Torvalds 		{
11031da177e4SLinus Torvalds 			struct sk_buff *skb;
11041da177e4SLinus Torvalds 			int amount = 0;
11051da177e4SLinus Torvalds 
1106e0f9f858SHerbert Xu 			spin_lock_bh(&sk->sk_receive_queue.lock);
11071da177e4SLinus Torvalds 			skb = skb_peek(&sk->sk_receive_queue);
11081da177e4SLinus Torvalds 			if (skb != NULL)
110927a884dcSArnaldo Carvalho de Melo 				amount = skb->tail - skb->transport_header;
1110e0f9f858SHerbert Xu 			spin_unlock_bh(&sk->sk_receive_queue.lock);
11111da177e4SLinus Torvalds 			return put_user(amount, (int __user *)arg);
11121da177e4SLinus Torvalds 		}
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds 		default:
11151da177e4SLinus Torvalds 			return -ENOIOCTLCMD;
11161da177e4SLinus Torvalds 	}
11171da177e4SLinus Torvalds }
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds static void rawv6_close(struct sock *sk, long timeout)
11201da177e4SLinus Torvalds {
11211da177e4SLinus Torvalds 	if (inet_sk(sk)->num == IPPROTO_RAW)
11221da177e4SLinus Torvalds 		ip6_ra_control(sk, -1, NULL);
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 	sk_common_release(sk);
11251da177e4SLinus Torvalds }
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds static int rawv6_init_sk(struct sock *sk)
11281da177e4SLinus Torvalds {
11291da177e4SLinus Torvalds 	struct raw6_sock *rp = raw6_sk(sk);
1130f48d5ff1SMasahide NAKAMURA 
1131f48d5ff1SMasahide NAKAMURA 	switch (inet_sk(sk)->num) {
1132f48d5ff1SMasahide NAKAMURA 	case IPPROTO_ICMPV6:
11331da177e4SLinus Torvalds 		rp->checksum = 1;
11341da177e4SLinus Torvalds 		rp->offset   = 2;
1135f48d5ff1SMasahide NAKAMURA 		break;
1136f48d5ff1SMasahide NAKAMURA 	case IPPROTO_MH:
1137f48d5ff1SMasahide NAKAMURA 		rp->checksum = 1;
1138f48d5ff1SMasahide NAKAMURA 		rp->offset   = 4;
1139f48d5ff1SMasahide NAKAMURA 		break;
1140f48d5ff1SMasahide NAKAMURA 	default:
1141f48d5ff1SMasahide NAKAMURA 		break;
11421da177e4SLinus Torvalds 	}
11431da177e4SLinus Torvalds 	return(0);
11441da177e4SLinus Torvalds }
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds struct proto rawv6_prot = {
11471da177e4SLinus Torvalds 	.name		   = "RAWv6",
11481da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
11491da177e4SLinus Torvalds 	.close		   = rawv6_close,
11501da177e4SLinus Torvalds 	.connect	   = ip6_datagram_connect,
11511da177e4SLinus Torvalds 	.disconnect	   = udp_disconnect,
11521da177e4SLinus Torvalds 	.ioctl		   = rawv6_ioctl,
11531da177e4SLinus Torvalds 	.init		   = rawv6_init_sk,
11541da177e4SLinus Torvalds 	.destroy	   = inet6_destroy_sock,
11551da177e4SLinus Torvalds 	.setsockopt	   = rawv6_setsockopt,
11561da177e4SLinus Torvalds 	.getsockopt	   = rawv6_getsockopt,
11571da177e4SLinus Torvalds 	.sendmsg	   = rawv6_sendmsg,
11581da177e4SLinus Torvalds 	.recvmsg	   = rawv6_recvmsg,
11591da177e4SLinus Torvalds 	.bind		   = rawv6_bind,
11601da177e4SLinus Torvalds 	.backlog_rcv	   = rawv6_rcv_skb,
11611da177e4SLinus Torvalds 	.hash		   = raw_v6_hash,
11621da177e4SLinus Torvalds 	.unhash		   = raw_v6_unhash,
11631da177e4SLinus Torvalds 	.obj_size	   = sizeof(struct raw6_sock),
1164543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
1165543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_rawv6_setsockopt,
1166543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_rawv6_getsockopt,
1167543d9cfeSArnaldo Carvalho de Melo #endif
11681da177e4SLinus Torvalds };
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
11711da177e4SLinus Torvalds struct raw6_iter_state {
11721da177e4SLinus Torvalds 	int bucket;
11731da177e4SLinus Torvalds };
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds #define raw6_seq_private(seq) ((struct raw6_iter_state *)(seq)->private)
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds static struct sock *raw6_get_first(struct seq_file *seq)
11781da177e4SLinus Torvalds {
11791da177e4SLinus Torvalds 	struct sock *sk;
11801da177e4SLinus Torvalds 	struct hlist_node *node;
11811da177e4SLinus Torvalds 	struct raw6_iter_state* state = raw6_seq_private(seq);
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	for (state->bucket = 0; state->bucket < RAWV6_HTABLE_SIZE; ++state->bucket)
11841da177e4SLinus Torvalds 		sk_for_each(sk, node, &raw_v6_htable[state->bucket])
11851da177e4SLinus Torvalds 			if (sk->sk_family == PF_INET6)
11861da177e4SLinus Torvalds 				goto out;
11871da177e4SLinus Torvalds 	sk = NULL;
11881da177e4SLinus Torvalds out:
11891da177e4SLinus Torvalds 	return sk;
11901da177e4SLinus Torvalds }
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds static struct sock *raw6_get_next(struct seq_file *seq, struct sock *sk)
11931da177e4SLinus Torvalds {
11941da177e4SLinus Torvalds 	struct raw6_iter_state* state = raw6_seq_private(seq);
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	do {
11971da177e4SLinus Torvalds 		sk = sk_next(sk);
11981da177e4SLinus Torvalds try_again:
11991da177e4SLinus Torvalds 		;
12001da177e4SLinus Torvalds 	} while (sk && sk->sk_family != PF_INET6);
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	if (!sk && ++state->bucket < RAWV6_HTABLE_SIZE) {
12031da177e4SLinus Torvalds 		sk = sk_head(&raw_v6_htable[state->bucket]);
12041da177e4SLinus Torvalds 		goto try_again;
12051da177e4SLinus Torvalds 	}
12061da177e4SLinus Torvalds 	return sk;
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds static struct sock *raw6_get_idx(struct seq_file *seq, loff_t pos)
12101da177e4SLinus Torvalds {
12111da177e4SLinus Torvalds 	struct sock *sk = raw6_get_first(seq);
12121da177e4SLinus Torvalds 	if (sk)
12131da177e4SLinus Torvalds 		while (pos && (sk = raw6_get_next(seq, sk)) != NULL)
12141da177e4SLinus Torvalds 			--pos;
12151da177e4SLinus Torvalds 	return pos ? NULL : sk;
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds static void *raw6_seq_start(struct seq_file *seq, loff_t *pos)
12191da177e4SLinus Torvalds {
12201da177e4SLinus Torvalds 	read_lock(&raw_v6_lock);
12211da177e4SLinus Torvalds 	return *pos ? raw6_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
12221da177e4SLinus Torvalds }
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds static void *raw6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
12251da177e4SLinus Torvalds {
12261da177e4SLinus Torvalds 	struct sock *sk;
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
12291da177e4SLinus Torvalds 		sk = raw6_get_first(seq);
12301da177e4SLinus Torvalds 	else
12311da177e4SLinus Torvalds 		sk = raw6_get_next(seq, v);
12321da177e4SLinus Torvalds 	++*pos;
12331da177e4SLinus Torvalds 	return sk;
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds static void raw6_seq_stop(struct seq_file *seq, void *v)
12371da177e4SLinus Torvalds {
12381da177e4SLinus Torvalds 	read_unlock(&raw_v6_lock);
12391da177e4SLinus Torvalds }
12401da177e4SLinus Torvalds 
12411da177e4SLinus Torvalds static void raw6_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
12421da177e4SLinus Torvalds {
12431da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sp);
12441da177e4SLinus Torvalds 	struct in6_addr *dest, *src;
12451da177e4SLinus Torvalds 	__u16 destp, srcp;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	dest  = &np->daddr;
12481da177e4SLinus Torvalds 	src   = &np->rcv_saddr;
12491da177e4SLinus Torvalds 	destp = 0;
12501da177e4SLinus Torvalds 	srcp  = inet_sk(sp)->num;
12511da177e4SLinus Torvalds 	seq_printf(seq,
12521da177e4SLinus Torvalds 		   "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
12531da177e4SLinus Torvalds 		   "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
12541da177e4SLinus Torvalds 		   i,
12551da177e4SLinus Torvalds 		   src->s6_addr32[0], src->s6_addr32[1],
12561da177e4SLinus Torvalds 		   src->s6_addr32[2], src->s6_addr32[3], srcp,
12571da177e4SLinus Torvalds 		   dest->s6_addr32[0], dest->s6_addr32[1],
12581da177e4SLinus Torvalds 		   dest->s6_addr32[2], dest->s6_addr32[3], destp,
12591da177e4SLinus Torvalds 		   sp->sk_state,
12601da177e4SLinus Torvalds 		   atomic_read(&sp->sk_wmem_alloc),
12611da177e4SLinus Torvalds 		   atomic_read(&sp->sk_rmem_alloc),
12621da177e4SLinus Torvalds 		   0, 0L, 0,
12631da177e4SLinus Torvalds 		   sock_i_uid(sp), 0,
12641da177e4SLinus Torvalds 		   sock_i_ino(sp),
12651da177e4SLinus Torvalds 		   atomic_read(&sp->sk_refcnt), sp);
12661da177e4SLinus Torvalds }
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds static int raw6_seq_show(struct seq_file *seq, void *v)
12691da177e4SLinus Torvalds {
12701da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
12711da177e4SLinus Torvalds 		seq_printf(seq,
12721da177e4SLinus Torvalds 			   "  sl  "
12731da177e4SLinus Torvalds 			   "local_address                         "
12741da177e4SLinus Torvalds 			   "remote_address                        "
12751da177e4SLinus Torvalds 			   "st tx_queue rx_queue tr tm->when retrnsmt"
12761da177e4SLinus Torvalds 			   "   uid  timeout inode\n");
12771da177e4SLinus Torvalds 	else
12781da177e4SLinus Torvalds 		raw6_sock_seq_show(seq, v, raw6_seq_private(seq)->bucket);
12791da177e4SLinus Torvalds 	return 0;
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
128256b3d975SPhilippe De Muyter static const struct seq_operations raw6_seq_ops = {
12831da177e4SLinus Torvalds 	.start =	raw6_seq_start,
12841da177e4SLinus Torvalds 	.next =		raw6_seq_next,
12851da177e4SLinus Torvalds 	.stop =		raw6_seq_stop,
12861da177e4SLinus Torvalds 	.show =		raw6_seq_show,
12871da177e4SLinus Torvalds };
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds static int raw6_seq_open(struct inode *inode, struct file *file)
12901da177e4SLinus Torvalds {
12911da177e4SLinus Torvalds 	struct seq_file *seq;
12921da177e4SLinus Torvalds 	int rc = -ENOMEM;
12930c600edaSIngo Oeser 	struct raw6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
12941da177e4SLinus Torvalds 	if (!s)
12951da177e4SLinus Torvalds 		goto out;
12961da177e4SLinus Torvalds 	rc = seq_open(file, &raw6_seq_ops);
12971da177e4SLinus Torvalds 	if (rc)
12981da177e4SLinus Torvalds 		goto out_kfree;
12991da177e4SLinus Torvalds 	seq = file->private_data;
13001da177e4SLinus Torvalds 	seq->private = s;
13011da177e4SLinus Torvalds out:
13021da177e4SLinus Torvalds 	return rc;
13031da177e4SLinus Torvalds out_kfree:
13041da177e4SLinus Torvalds 	kfree(s);
13051da177e4SLinus Torvalds 	goto out;
13061da177e4SLinus Torvalds }
13071da177e4SLinus Torvalds 
13089a32144eSArjan van de Ven static const struct file_operations raw6_seq_fops = {
13091da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
13101da177e4SLinus Torvalds 	.open =		raw6_seq_open,
13111da177e4SLinus Torvalds 	.read =		seq_read,
13121da177e4SLinus Torvalds 	.llseek =	seq_lseek,
13131da177e4SLinus Torvalds 	.release =	seq_release_private,
13141da177e4SLinus Torvalds };
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds int __init raw6_proc_init(void)
13171da177e4SLinus Torvalds {
13181da177e4SLinus Torvalds 	if (!proc_net_fops_create("raw6", S_IRUGO, &raw6_seq_fops))
13191da177e4SLinus Torvalds 		return -ENOMEM;
13201da177e4SLinus Torvalds 	return 0;
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds void raw6_proc_exit(void)
13241da177e4SLinus Torvalds {
13251da177e4SLinus Torvalds 	proc_net_remove("raw6");
13261da177e4SLinus Torvalds }
13271da177e4SLinus Torvalds #endif	/* CONFIG_PROC_FS */
1328