xref: /linux/net/ipv4/raw.c (revision 9bbc768aa911a3ef336272eaa6d220abfba8ce50)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		RAW - implementation of IP "raw" sockets.
71da177e4SLinus Torvalds  *
802c30a84SJesper Juhl  * Authors:	Ross Biro
91da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Fixes:
121da177e4SLinus Torvalds  *		Alan Cox	:	verify_area() fixed up
131da177e4SLinus Torvalds  *		Alan Cox	:	ICMP error handling
141da177e4SLinus Torvalds  *		Alan Cox	:	EMSGSIZE if you send too big a packet
151da177e4SLinus Torvalds  *		Alan Cox	: 	Now uses generic datagrams and shared
161da177e4SLinus Torvalds  *					skbuff library. No more peek crashes,
171da177e4SLinus Torvalds  *					no more backlogs
181da177e4SLinus Torvalds  *		Alan Cox	:	Checks sk->broadcast.
191da177e4SLinus Torvalds  *		Alan Cox	:	Uses skb_free_datagram/skb_copy_datagram
201da177e4SLinus Torvalds  *		Alan Cox	:	Raw passes ip options too
211da177e4SLinus Torvalds  *		Alan Cox	:	Setsocketopt added
221da177e4SLinus Torvalds  *		Alan Cox	:	Fixed error return for broadcasts
231da177e4SLinus Torvalds  *		Alan Cox	:	Removed wake_up calls
241da177e4SLinus Torvalds  *		Alan Cox	:	Use ttl/tos
251da177e4SLinus Torvalds  *		Alan Cox	:	Cleaned up old debugging
261da177e4SLinus Torvalds  *		Alan Cox	:	Use new kernel side addresses
271da177e4SLinus Torvalds  *	Arnt Gulbrandsen	:	Fixed MSG_DONTROUTE in raw sockets.
281da177e4SLinus Torvalds  *		Alan Cox	:	BSD style RAW socket demultiplexing.
291da177e4SLinus Torvalds  *		Alan Cox	:	Beginnings of mrouted support.
301da177e4SLinus Torvalds  *		Alan Cox	:	Added IP_HDRINCL option.
311da177e4SLinus Torvalds  *		Alan Cox	:	Skip broadcast check if BSDism set.
321da177e4SLinus Torvalds  *		David S. Miller	:	New socket lookup architecture.
331da177e4SLinus Torvalds  *
341da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
351da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
361da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
371da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
381da177e4SLinus Torvalds  */
391da177e4SLinus Torvalds 
40715b49efSAlan Cox #include <linux/types.h>
411da177e4SLinus Torvalds #include <asm/atomic.h>
421da177e4SLinus Torvalds #include <asm/byteorder.h>
431da177e4SLinus Torvalds #include <asm/current.h>
441da177e4SLinus Torvalds #include <asm/uaccess.h>
451da177e4SLinus Torvalds #include <asm/ioctls.h>
461da177e4SLinus Torvalds #include <linux/stddef.h>
471da177e4SLinus Torvalds #include <linux/slab.h>
481da177e4SLinus Torvalds #include <linux/errno.h>
491da177e4SLinus Torvalds #include <linux/aio.h>
501da177e4SLinus Torvalds #include <linux/kernel.h>
511da177e4SLinus Torvalds #include <linux/spinlock.h>
521da177e4SLinus Torvalds #include <linux/sockios.h>
531da177e4SLinus Torvalds #include <linux/socket.h>
541da177e4SLinus Torvalds #include <linux/in.h>
551da177e4SLinus Torvalds #include <linux/mroute.h>
561da177e4SLinus Torvalds #include <linux/netdevice.h>
571da177e4SLinus Torvalds #include <linux/in_route.h>
581da177e4SLinus Torvalds #include <linux/route.h>
591da177e4SLinus Torvalds #include <linux/skbuff.h>
60457c4cbcSEric W. Biederman #include <net/net_namespace.h>
611da177e4SLinus Torvalds #include <net/dst.h>
621da177e4SLinus Torvalds #include <net/sock.h>
631da177e4SLinus Torvalds #include <linux/gfp.h>
641da177e4SLinus Torvalds #include <linux/ip.h>
651da177e4SLinus Torvalds #include <linux/net.h>
661da177e4SLinus Torvalds #include <net/ip.h>
671da177e4SLinus Torvalds #include <net/icmp.h>
681da177e4SLinus Torvalds #include <net/udp.h>
691da177e4SLinus Torvalds #include <net/raw.h>
701da177e4SLinus Torvalds #include <net/snmp.h>
71c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
721da177e4SLinus Torvalds #include <net/inet_common.h>
731da177e4SLinus Torvalds #include <net/checksum.h>
741da177e4SLinus Torvalds #include <net/xfrm.h>
751da177e4SLinus Torvalds #include <linux/rtnetlink.h>
761da177e4SLinus Torvalds #include <linux/proc_fs.h>
771da177e4SLinus Torvalds #include <linux/seq_file.h>
781da177e4SLinus Torvalds #include <linux/netfilter.h>
791da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
801da177e4SLinus Torvalds 
81b673e4dfSPavel Emelyanov static struct raw_hashinfo raw_v4_hashinfo = {
82938b93adSRobert P. J. Day 	.lock = __RW_LOCK_UNLOCKED(raw_v4_hashinfo.lock),
83b673e4dfSPavel Emelyanov };
841da177e4SLinus Torvalds 
85fc8717baSPavel Emelyanov void raw_hash_sk(struct sock *sk)
861da177e4SLinus Torvalds {
87fc8717baSPavel Emelyanov 	struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
8865b4c50bSPavel Emelyanov 	struct hlist_head *head;
891da177e4SLinus Torvalds 
90c720c7e8SEric Dumazet 	head = &h->ht[inet_sk(sk)->inet_num & (RAW_HTABLE_SIZE - 1)];
9165b4c50bSPavel Emelyanov 
9265b4c50bSPavel Emelyanov 	write_lock_bh(&h->lock);
931da177e4SLinus Torvalds 	sk_add_node(sk, head);
94c29a0bc4SPavel Emelyanov 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
9565b4c50bSPavel Emelyanov 	write_unlock_bh(&h->lock);
9665b4c50bSPavel Emelyanov }
9765b4c50bSPavel Emelyanov EXPORT_SYMBOL_GPL(raw_hash_sk);
9865b4c50bSPavel Emelyanov 
99fc8717baSPavel Emelyanov void raw_unhash_sk(struct sock *sk)
100ab70768eSPavel Emelyanov {
101fc8717baSPavel Emelyanov 	struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
102fc8717baSPavel Emelyanov 
103ab70768eSPavel Emelyanov 	write_lock_bh(&h->lock);
104ab70768eSPavel Emelyanov 	if (sk_del_node_init(sk))
105c29a0bc4SPavel Emelyanov 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
106ab70768eSPavel Emelyanov 	write_unlock_bh(&h->lock);
107ab70768eSPavel Emelyanov }
108ab70768eSPavel Emelyanov EXPORT_SYMBOL_GPL(raw_unhash_sk);
109ab70768eSPavel Emelyanov 
110be185884SPavel Emelyanov static struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
111be185884SPavel Emelyanov 		unsigned short num, __be32 raddr, __be32 laddr, int dif)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	struct hlist_node *node;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	sk_for_each_from(sk, node) {
1161da177e4SLinus Torvalds 		struct inet_sock *inet = inet_sk(sk);
1171da177e4SLinus Torvalds 
118c720c7e8SEric Dumazet 		if (net_eq(sock_net(sk), net) && inet->inet_num == num	&&
119c720c7e8SEric Dumazet 		    !(inet->inet_daddr && inet->inet_daddr != raddr) 	&&
120c720c7e8SEric Dumazet 		    !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
1211da177e4SLinus Torvalds 		    !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
1221da177e4SLinus Torvalds 			goto found; /* gotcha */
1231da177e4SLinus Torvalds 	}
1241da177e4SLinus Torvalds 	sk = NULL;
1251da177e4SLinus Torvalds found:
1261da177e4SLinus Torvalds 	return sk;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*
1301da177e4SLinus Torvalds  *	0 - deliver
1311da177e4SLinus Torvalds  *	1 - block
1321da177e4SLinus Torvalds  */
1331da177e4SLinus Torvalds static __inline__ int icmp_filter(struct sock *sk, struct sk_buff *skb)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	int type;
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, sizeof(struct icmphdr)))
1381da177e4SLinus Torvalds 		return 1;
1391da177e4SLinus Torvalds 
14088c7664fSArnaldo Carvalho de Melo 	type = icmp_hdr(skb)->type;
1411da177e4SLinus Torvalds 	if (type < 32) {
1421da177e4SLinus Torvalds 		__u32 data = raw_sk(sk)->filter.data;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 		return ((1 << type) & data) != 0;
1451da177e4SLinus Torvalds 	}
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	/* Do not block unknown ICMP types */
1481da177e4SLinus Torvalds 	return 0;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds /* IP input processing comes here for RAW socket delivery.
1521da177e4SLinus Torvalds  * Caller owns SKB, so we must make clones.
1531da177e4SLinus Torvalds  *
1541da177e4SLinus Torvalds  * RFC 1122: SHOULD pass TOS value up to the transport layer.
1551da177e4SLinus Torvalds  * -> It does. And not only TOS, but all IP header.
1561da177e4SLinus Torvalds  */
1577bc54c90SPavel Emelyanov static int raw_v4_input(struct sk_buff *skb, struct iphdr *iph, int hash)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	struct sock *sk;
1601da177e4SLinus Torvalds 	struct hlist_head *head;
161d13964f4SPatrick McHardy 	int delivered = 0;
162be185884SPavel Emelyanov 	struct net *net;
1631da177e4SLinus Torvalds 
164b673e4dfSPavel Emelyanov 	read_lock(&raw_v4_hashinfo.lock);
165b673e4dfSPavel Emelyanov 	head = &raw_v4_hashinfo.ht[hash];
1661da177e4SLinus Torvalds 	if (hlist_empty(head))
1671da177e4SLinus Torvalds 		goto out;
168be185884SPavel Emelyanov 
169c346dca1SYOSHIFUJI Hideaki 	net = dev_net(skb->dev);
170be185884SPavel Emelyanov 	sk = __raw_v4_lookup(net, __sk_head(head), iph->protocol,
1711da177e4SLinus Torvalds 			     iph->saddr, iph->daddr,
1721da177e4SLinus Torvalds 			     skb->dev->ifindex);
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	while (sk) {
175d13964f4SPatrick McHardy 		delivered = 1;
1761da177e4SLinus Torvalds 		if (iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) {
1771da177e4SLinus Torvalds 			struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 			/* Not releasing hash table! */
1801da177e4SLinus Torvalds 			if (clone)
1811da177e4SLinus Torvalds 				raw_rcv(sk, clone);
1821da177e4SLinus Torvalds 		}
183be185884SPavel Emelyanov 		sk = __raw_v4_lookup(net, sk_next(sk), iph->protocol,
1841da177e4SLinus Torvalds 				     iph->saddr, iph->daddr,
1851da177e4SLinus Torvalds 				     skb->dev->ifindex);
1861da177e4SLinus Torvalds 	}
1871da177e4SLinus Torvalds out:
188b673e4dfSPavel Emelyanov 	read_unlock(&raw_v4_hashinfo.lock);
189d13964f4SPatrick McHardy 	return delivered;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1927bc54c90SPavel Emelyanov int raw_local_deliver(struct sk_buff *skb, int protocol)
1937bc54c90SPavel Emelyanov {
1947bc54c90SPavel Emelyanov 	int hash;
1957bc54c90SPavel Emelyanov 	struct sock *raw_sk;
1967bc54c90SPavel Emelyanov 
197b673e4dfSPavel Emelyanov 	hash = protocol & (RAW_HTABLE_SIZE - 1);
198b673e4dfSPavel Emelyanov 	raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
1997bc54c90SPavel Emelyanov 
2007bc54c90SPavel Emelyanov 	/* If there maybe a raw socket we must check - if not we
2017bc54c90SPavel Emelyanov 	 * don't care less
2027bc54c90SPavel Emelyanov 	 */
2037bc54c90SPavel Emelyanov 	if (raw_sk && !raw_v4_input(skb, ip_hdr(skb), hash))
2047bc54c90SPavel Emelyanov 		raw_sk = NULL;
2057bc54c90SPavel Emelyanov 
2067bc54c90SPavel Emelyanov 	return raw_sk != NULL;
2077bc54c90SPavel Emelyanov 
2087bc54c90SPavel Emelyanov }
2097bc54c90SPavel Emelyanov 
2107bc54c90SPavel Emelyanov static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
21388c7664fSArnaldo Carvalho de Melo 	const int type = icmp_hdr(skb)->type;
21488c7664fSArnaldo Carvalho de Melo 	const int code = icmp_hdr(skb)->code;
2151da177e4SLinus Torvalds 	int err = 0;
2161da177e4SLinus Torvalds 	int harderr = 0;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	/* Report error on raw socket, if:
2191da177e4SLinus Torvalds 	   1. User requested ip_recverr.
2201da177e4SLinus Torvalds 	   2. Socket is connected (otherwise the error indication
2211da177e4SLinus Torvalds 	      is useless without ip_recverr and error is hard.
2221da177e4SLinus Torvalds 	 */
2231da177e4SLinus Torvalds 	if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED)
2241da177e4SLinus Torvalds 		return;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	switch (type) {
2271da177e4SLinus Torvalds 	default:
2281da177e4SLinus Torvalds 	case ICMP_TIME_EXCEEDED:
2291da177e4SLinus Torvalds 		err = EHOSTUNREACH;
2301da177e4SLinus Torvalds 		break;
2311da177e4SLinus Torvalds 	case ICMP_SOURCE_QUENCH:
2321da177e4SLinus Torvalds 		return;
2331da177e4SLinus Torvalds 	case ICMP_PARAMETERPROB:
2341da177e4SLinus Torvalds 		err = EPROTO;
2351da177e4SLinus Torvalds 		harderr = 1;
2361da177e4SLinus Torvalds 		break;
2371da177e4SLinus Torvalds 	case ICMP_DEST_UNREACH:
2381da177e4SLinus Torvalds 		err = EHOSTUNREACH;
2391da177e4SLinus Torvalds 		if (code > NR_ICMP_UNREACH)
2401da177e4SLinus Torvalds 			break;
2411da177e4SLinus Torvalds 		err = icmp_err_convert[code].errno;
2421da177e4SLinus Torvalds 		harderr = icmp_err_convert[code].fatal;
2431da177e4SLinus Torvalds 		if (code == ICMP_FRAG_NEEDED) {
2441da177e4SLinus Torvalds 			harderr = inet->pmtudisc != IP_PMTUDISC_DONT;
2451da177e4SLinus Torvalds 			err = EMSGSIZE;
2461da177e4SLinus Torvalds 		}
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	if (inet->recverr) {
2501da177e4SLinus Torvalds 		struct iphdr *iph = (struct iphdr *)skb->data;
2511da177e4SLinus Torvalds 		u8 *payload = skb->data + (iph->ihl << 2);
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 		if (inet->hdrincl)
2541da177e4SLinus Torvalds 			payload = skb->data;
2551da177e4SLinus Torvalds 		ip_icmp_error(sk, skb, err, 0, info, payload);
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	if (inet->recverr || harderr) {
2591da177e4SLinus Torvalds 		sk->sk_err = err;
2601da177e4SLinus Torvalds 		sk->sk_error_report(sk);
2611da177e4SLinus Torvalds 	}
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
2647bc54c90SPavel Emelyanov void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info)
2657bc54c90SPavel Emelyanov {
2667bc54c90SPavel Emelyanov 	int hash;
2677bc54c90SPavel Emelyanov 	struct sock *raw_sk;
2687bc54c90SPavel Emelyanov 	struct iphdr *iph;
269be185884SPavel Emelyanov 	struct net *net;
2707bc54c90SPavel Emelyanov 
271b673e4dfSPavel Emelyanov 	hash = protocol & (RAW_HTABLE_SIZE - 1);
2727bc54c90SPavel Emelyanov 
273b673e4dfSPavel Emelyanov 	read_lock(&raw_v4_hashinfo.lock);
274b673e4dfSPavel Emelyanov 	raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
2757bc54c90SPavel Emelyanov 	if (raw_sk != NULL) {
2767bc54c90SPavel Emelyanov 		iph = (struct iphdr *)skb->data;
277c346dca1SYOSHIFUJI Hideaki 		net = dev_net(skb->dev);
278be185884SPavel Emelyanov 
279be185884SPavel Emelyanov 		while ((raw_sk = __raw_v4_lookup(net, raw_sk, protocol,
280be185884SPavel Emelyanov 						iph->daddr, iph->saddr,
2817bc54c90SPavel Emelyanov 						skb->dev->ifindex)) != NULL) {
2827bc54c90SPavel Emelyanov 			raw_err(raw_sk, skb, info);
2837bc54c90SPavel Emelyanov 			raw_sk = sk_next(raw_sk);
2847bc54c90SPavel Emelyanov 			iph = (struct iphdr *)skb->data;
2857bc54c90SPavel Emelyanov 		}
2867bc54c90SPavel Emelyanov 	}
287b673e4dfSPavel Emelyanov 	read_unlock(&raw_v4_hashinfo.lock);
2887bc54c90SPavel Emelyanov }
2897bc54c90SPavel Emelyanov 
2901da177e4SLinus Torvalds static int raw_rcv_skb(struct sock * sk, struct sk_buff * skb)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	/* Charge it to the socket. */
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	if (sock_queue_rcv_skb(sk, skb) < 0) {
2951da177e4SLinus Torvalds 		kfree_skb(skb);
2961da177e4SLinus Torvalds 		return NET_RX_DROP;
2971da177e4SLinus Torvalds 	}
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	return NET_RX_SUCCESS;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds int raw_rcv(struct sock *sk, struct sk_buff *skb)
3031da177e4SLinus Torvalds {
3041da177e4SLinus Torvalds 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
30533c732c3SWang Chen 		atomic_inc(&sk->sk_drops);
3061da177e4SLinus Torvalds 		kfree_skb(skb);
3071da177e4SLinus Torvalds 		return NET_RX_DROP;
3081da177e4SLinus Torvalds 	}
309b59c2701SPatrick McHardy 	nf_reset(skb);
3101da177e4SLinus Torvalds 
311d56f90a7SArnaldo Carvalho de Melo 	skb_push(skb, skb->data - skb_network_header(skb));
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	raw_rcv_skb(sk, skb);
3141da177e4SLinus Torvalds 	return 0;
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
317f7d7fc03SJesper Juhl static int raw_send_hdrinc(struct sock *sk, void *from, size_t length,
3181da177e4SLinus Torvalds 			struct rtable *rt,
3191da177e4SLinus Torvalds 			unsigned int flags)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
3220388b004SPavel Emelyanov 	struct net *net = sock_net(sk);
3231da177e4SLinus Torvalds 	struct iphdr *iph;
3241da177e4SLinus Torvalds 	struct sk_buff *skb;
325f844c74fSHerbert Xu 	unsigned int iphlen;
3261da177e4SLinus Torvalds 	int err;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	if (length > rt->u.dst.dev->mtu) {
329c720c7e8SEric Dumazet 		ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->inet_dport,
3301da177e4SLinus Torvalds 			       rt->u.dst.dev->mtu);
3311da177e4SLinus Torvalds 		return -EMSGSIZE;
3321da177e4SLinus Torvalds 	}
3331da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
3341da177e4SLinus Torvalds 		goto out;
3351da177e4SLinus Torvalds 
336f5184d26SJohannes Berg 	skb = sock_alloc_send_skb(sk,
337f5184d26SJohannes Berg 				  length + LL_ALLOCATED_SPACE(rt->u.dst.dev) + 15,
3381da177e4SLinus Torvalds 				  flags & MSG_DONTWAIT, &err);
3391da177e4SLinus Torvalds 	if (skb == NULL)
3401da177e4SLinus Torvalds 		goto error;
341f5184d26SJohannes Berg 	skb_reserve(skb, LL_RESERVED_SPACE(rt->u.dst.dev));
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
3444a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
345adf30907SEric Dumazet 	skb_dst_set(skb, dst_clone(&rt->u.dst));
3461da177e4SLinus Torvalds 
3477e28ecc2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
348eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
3497e28ecc2SArnaldo Carvalho de Melo 	skb_put(skb, length);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	skb->ip_summed = CHECKSUM_NONE;
3521da177e4SLinus Torvalds 
353b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
35455888dfbSNeil Horman 	err = -EFAULT;
35555888dfbSNeil Horman 	if (memcpy_fromiovecend((void *)iph, from, 0, length))
35655888dfbSNeil Horman 		goto error_free;
3571da177e4SLinus Torvalds 
358f844c74fSHerbert Xu 	iphlen = iph->ihl * 4;
35955888dfbSNeil Horman 
36055888dfbSNeil Horman 	/*
36155888dfbSNeil Horman 	 * We don't want to modify the ip header, but we do need to
36255888dfbSNeil Horman 	 * be sure that it won't cause problems later along the network
36355888dfbSNeil Horman 	 * stack.  Specifically we want to make sure that iph->ihl is a
36455888dfbSNeil Horman 	 * sane value.  If ihl points beyond the length of the buffer passed
36555888dfbSNeil Horman 	 * in, reject the frame as invalid
36655888dfbSNeil Horman 	 */
36755888dfbSNeil Horman 	err = -EINVAL;
36855888dfbSNeil Horman 	if (iphlen > length)
36955888dfbSNeil Horman 		goto error_free;
37055888dfbSNeil Horman 
37155888dfbSNeil Horman 	if (iphlen >= sizeof(*iph)) {
3721da177e4SLinus Torvalds 		if (!iph->saddr)
3731da177e4SLinus Torvalds 			iph->saddr = rt->rt_src;
3741da177e4SLinus Torvalds 		iph->check   = 0;
3751da177e4SLinus Torvalds 		iph->tot_len = htons(length);
3761da177e4SLinus Torvalds 		if (!iph->id)
3771da177e4SLinus Torvalds 			ip_select_ident(iph, &rt->u.dst, NULL);
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 		iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
3801da177e4SLinus Torvalds 	}
38196793b48SDavid L Stevens 	if (iph->protocol == IPPROTO_ICMP)
3820388b004SPavel Emelyanov 		icmp_out_count(net, ((struct icmphdr *)
38396793b48SDavid L Stevens 			skb_transport_header(skb))->type);
3841da177e4SLinus Torvalds 
385*9bbc768aSJan Engelhardt 	err = NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_OUT, skb, NULL,
386*9bbc768aSJan Engelhardt 		      rt->u.dst.dev, dst_output);
3871da177e4SLinus Torvalds 	if (err > 0)
3886ce9e7b5SEric Dumazet 		err = net_xmit_errno(err);
3891da177e4SLinus Torvalds 	if (err)
3901da177e4SLinus Torvalds 		goto error;
3911da177e4SLinus Torvalds out:
3921da177e4SLinus Torvalds 	return 0;
3931da177e4SLinus Torvalds 
39455888dfbSNeil Horman error_free:
3951da177e4SLinus Torvalds 	kfree_skb(skb);
3961da177e4SLinus Torvalds error:
3975e38e270SPavel Emelyanov 	IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
3986ce9e7b5SEric Dumazet 	if (err == -ENOBUFS && !inet->recverr)
3996ce9e7b5SEric Dumazet 		err = 0;
4001da177e4SLinus Torvalds 	return err;
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
403a27b58feSHeiko Carstens static int raw_probe_proto_opt(struct flowi *fl, struct msghdr *msg)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	struct iovec *iov;
4061da177e4SLinus Torvalds 	u8 __user *type = NULL;
4071da177e4SLinus Torvalds 	u8 __user *code = NULL;
4081da177e4SLinus Torvalds 	int probed = 0;
40993765d8aSJesper Juhl 	unsigned int i;
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	if (!msg->msg_iov)
412a27b58feSHeiko Carstens 		return 0;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	for (i = 0; i < msg->msg_iovlen; i++) {
4151da177e4SLinus Torvalds 		iov = &msg->msg_iov[i];
4161da177e4SLinus Torvalds 		if (!iov)
4171da177e4SLinus Torvalds 			continue;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 		switch (fl->proto) {
4201da177e4SLinus Torvalds 		case IPPROTO_ICMP:
4211da177e4SLinus Torvalds 			/* check if one-byte field is readable or not. */
4221da177e4SLinus Torvalds 			if (iov->iov_base && iov->iov_len < 1)
4231da177e4SLinus Torvalds 				break;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 			if (!type) {
4261da177e4SLinus Torvalds 				type = iov->iov_base;
4271da177e4SLinus Torvalds 				/* check if code field is readable or not. */
4281da177e4SLinus Torvalds 				if (iov->iov_len > 1)
4291da177e4SLinus Torvalds 					code = type + 1;
4301da177e4SLinus Torvalds 			} else if (!code)
4311da177e4SLinus Torvalds 				code = iov->iov_base;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 			if (type && code) {
434a27b58feSHeiko Carstens 				if (get_user(fl->fl_icmp_type, type) ||
435a27b58feSHeiko Carstens 				    get_user(fl->fl_icmp_code, code))
436a27b58feSHeiko Carstens 					return -EFAULT;
4371da177e4SLinus Torvalds 				probed = 1;
4381da177e4SLinus Torvalds 			}
4391da177e4SLinus Torvalds 			break;
4401da177e4SLinus Torvalds 		default:
4411da177e4SLinus Torvalds 			probed = 1;
4421da177e4SLinus Torvalds 			break;
4431da177e4SLinus Torvalds 		}
4441da177e4SLinus Torvalds 		if (probed)
4451da177e4SLinus Torvalds 			break;
4461da177e4SLinus Torvalds 	}
447a27b58feSHeiko Carstens 	return 0;
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
4511da177e4SLinus Torvalds 		       size_t len)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4541da177e4SLinus Torvalds 	struct ipcm_cookie ipc;
4551da177e4SLinus Torvalds 	struct rtable *rt = NULL;
4561da177e4SLinus Torvalds 	int free = 0;
4573ca3c68eSAl Viro 	__be32 daddr;
458c1d18f9fSAl Viro 	__be32 saddr;
4591da177e4SLinus Torvalds 	u8  tos;
4601da177e4SLinus Torvalds 	int err;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	err = -EMSGSIZE;
463926d4b81SJesper Juhl 	if (len > 0xFFFF)
4641da177e4SLinus Torvalds 		goto out;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	/*
4671da177e4SLinus Torvalds 	 *	Check the flags.
4681da177e4SLinus Torvalds 	 */
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	err = -EOPNOTSUPP;
4711da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)	/* Mirror BSD error message */
4721da177e4SLinus Torvalds 		goto out;               /* compatibility */
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	/*
4751da177e4SLinus Torvalds 	 *	Get and verify the address.
4761da177e4SLinus Torvalds 	 */
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 	if (msg->msg_namelen) {
4791da177e4SLinus Torvalds 		struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
4801da177e4SLinus Torvalds 		err = -EINVAL;
4811da177e4SLinus Torvalds 		if (msg->msg_namelen < sizeof(*usin))
4821da177e4SLinus Torvalds 			goto out;
4831da177e4SLinus Torvalds 		if (usin->sin_family != AF_INET) {
4841da177e4SLinus Torvalds 			static int complained;
4851da177e4SLinus Torvalds 			if (!complained++)
4861da177e4SLinus Torvalds 				printk(KERN_INFO "%s forgot to set AF_INET in "
4871da177e4SLinus Torvalds 						 "raw sendmsg. Fix it!\n",
4881da177e4SLinus Torvalds 						 current->comm);
4891da177e4SLinus Torvalds 			err = -EAFNOSUPPORT;
4901da177e4SLinus Torvalds 			if (usin->sin_family)
4911da177e4SLinus Torvalds 				goto out;
4921da177e4SLinus Torvalds 		}
4931da177e4SLinus Torvalds 		daddr = usin->sin_addr.s_addr;
4941da177e4SLinus Torvalds 		/* ANK: I did not forget to get protocol from port field.
4951da177e4SLinus Torvalds 		 * I just do not know, who uses this weirdness.
4961da177e4SLinus Torvalds 		 * IP_HDRINCL is much more convenient.
4971da177e4SLinus Torvalds 		 */
4981da177e4SLinus Torvalds 	} else {
4991da177e4SLinus Torvalds 		err = -EDESTADDRREQ;
5001da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED)
5011da177e4SLinus Torvalds 			goto out;
502c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 
505c720c7e8SEric Dumazet 	ipc.addr = inet->inet_saddr;
5061da177e4SLinus Torvalds 	ipc.opt = NULL;
50751f31cabSPatrick Ohly 	ipc.shtx.flags = 0;
5081da177e4SLinus Torvalds 	ipc.oif = sk->sk_bound_dev_if;
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	if (msg->msg_controllen) {
5113b1e0a65SYOSHIFUJI Hideaki 		err = ip_cmsg_send(sock_net(sk), msg, &ipc);
5121da177e4SLinus Torvalds 		if (err)
5131da177e4SLinus Torvalds 			goto out;
5141da177e4SLinus Torvalds 		if (ipc.opt)
5151da177e4SLinus Torvalds 			free = 1;
5161da177e4SLinus Torvalds 	}
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	saddr = ipc.addr;
5191da177e4SLinus Torvalds 	ipc.addr = daddr;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	if (!ipc.opt)
5221da177e4SLinus Torvalds 		ipc.opt = inet->opt;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	if (ipc.opt) {
5251da177e4SLinus Torvalds 		err = -EINVAL;
5261da177e4SLinus Torvalds 		/* Linux does not mangle headers on raw sockets,
5271da177e4SLinus Torvalds 		 * so that IP options + IP_HDRINCL is non-sense.
5281da177e4SLinus Torvalds 		 */
5291da177e4SLinus Torvalds 		if (inet->hdrincl)
5301da177e4SLinus Torvalds 			goto done;
5311da177e4SLinus Torvalds 		if (ipc.opt->srr) {
5321da177e4SLinus Torvalds 			if (!daddr)
5331da177e4SLinus Torvalds 				goto done;
5341da177e4SLinus Torvalds 			daddr = ipc.opt->faddr;
5351da177e4SLinus Torvalds 		}
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds 	tos = RT_CONN_FLAGS(sk);
5381da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_DONTROUTE)
5391da177e4SLinus Torvalds 		tos |= RTO_ONLINK;
5401da177e4SLinus Torvalds 
541f97c1e0cSJoe Perches 	if (ipv4_is_multicast(daddr)) {
5421da177e4SLinus Torvalds 		if (!ipc.oif)
5431da177e4SLinus Torvalds 			ipc.oif = inet->mc_index;
5441da177e4SLinus Torvalds 		if (!saddr)
5451da177e4SLinus Torvalds 			saddr = inet->mc_addr;
5461da177e4SLinus Torvalds 	}
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	{
5491da177e4SLinus Torvalds 		struct flowi fl = { .oif = ipc.oif,
5504a19ec58SLaszlo Attila Toth 				    .mark = sk->sk_mark,
5511da177e4SLinus Torvalds 				    .nl_u = { .ip4_u =
5521da177e4SLinus Torvalds 					      { .daddr = daddr,
5531da177e4SLinus Torvalds 						.saddr = saddr,
5541da177e4SLinus Torvalds 						.tos = tos } },
5551da177e4SLinus Torvalds 				    .proto = inet->hdrincl ? IPPROTO_RAW :
5561da177e4SLinus Torvalds 							     sk->sk_protocol,
5571da177e4SLinus Torvalds 				  };
558a27b58feSHeiko Carstens 		if (!inet->hdrincl) {
559a27b58feSHeiko Carstens 			err = raw_probe_proto_opt(&fl, msg);
560a27b58feSHeiko Carstens 			if (err)
561a27b58feSHeiko Carstens 				goto done;
562a27b58feSHeiko Carstens 		}
5631da177e4SLinus Torvalds 
564beb8d13bSVenkat Yekkirala 		security_sk_classify_flow(sk, &fl);
5653b1e0a65SYOSHIFUJI Hideaki 		err = ip_route_output_flow(sock_net(sk), &rt, &fl, sk, 1);
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds 	if (err)
5681da177e4SLinus Torvalds 		goto done;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	err = -EACCES;
5711da177e4SLinus Torvalds 	if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
5721da177e4SLinus Torvalds 		goto done;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_CONFIRM)
5751da177e4SLinus Torvalds 		goto do_confirm;
5761da177e4SLinus Torvalds back_from_confirm:
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	if (inet->hdrincl)
5791da177e4SLinus Torvalds 		err = raw_send_hdrinc(sk, msg->msg_iov, len,
5801da177e4SLinus Torvalds 					rt, msg->msg_flags);
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	 else {
5831da177e4SLinus Torvalds 		if (!ipc.addr)
5841da177e4SLinus Torvalds 			ipc.addr = rt->rt_dst;
5851da177e4SLinus Torvalds 		lock_sock(sk);
5861da177e4SLinus Torvalds 		err = ip_append_data(sk, ip_generic_getfrag, msg->msg_iov, len, 0,
5872e77d89bSEric Dumazet 					&ipc, &rt, msg->msg_flags);
5881da177e4SLinus Torvalds 		if (err)
5891da177e4SLinus Torvalds 			ip_flush_pending_frames(sk);
5906ce9e7b5SEric Dumazet 		else if (!(msg->msg_flags & MSG_MORE)) {
5911da177e4SLinus Torvalds 			err = ip_push_pending_frames(sk);
5926ce9e7b5SEric Dumazet 			if (err == -ENOBUFS && !inet->recverr)
5936ce9e7b5SEric Dumazet 				err = 0;
5946ce9e7b5SEric Dumazet 		}
5951da177e4SLinus Torvalds 		release_sock(sk);
5961da177e4SLinus Torvalds 	}
5971da177e4SLinus Torvalds done:
5981da177e4SLinus Torvalds 	if (free)
5991da177e4SLinus Torvalds 		kfree(ipc.opt);
6001da177e4SLinus Torvalds 	ip_rt_put(rt);
6011da177e4SLinus Torvalds 
6025418c692SJesper Juhl out:
6035418c692SJesper Juhl 	if (err < 0)
6045418c692SJesper Juhl 		return err;
6055418c692SJesper Juhl 	return len;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds do_confirm:
6081da177e4SLinus Torvalds 	dst_confirm(&rt->u.dst);
6091da177e4SLinus Torvalds 	if (!(msg->msg_flags & MSG_PROBE) || len)
6101da177e4SLinus Torvalds 		goto back_from_confirm;
6111da177e4SLinus Torvalds 	err = 0;
6121da177e4SLinus Torvalds 	goto done;
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds static void raw_close(struct sock *sk, long timeout)
6161da177e4SLinus Torvalds {
6171da177e4SLinus Torvalds 	/*
6181da177e4SLinus Torvalds 	 * Raw sockets may have direct kernel refereneces. Kill them.
6191da177e4SLinus Torvalds 	 */
6201da177e4SLinus Torvalds 	ip_ra_control(sk, 0, NULL);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	sk_common_release(sk);
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
6257d06b2e0SBrian Haley static void raw_destroy(struct sock *sk)
62622dd4850SDenis V. Lunev {
62722dd4850SDenis V. Lunev 	lock_sock(sk);
62822dd4850SDenis V. Lunev 	ip_flush_pending_frames(sk);
62922dd4850SDenis V. Lunev 	release_sock(sk);
63022dd4850SDenis V. Lunev }
63122dd4850SDenis V. Lunev 
6321da177e4SLinus Torvalds /* This gets rid of all the nasties in af_inet. -DaveM */
6331da177e4SLinus Torvalds static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
6361da177e4SLinus Torvalds 	struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
6371da177e4SLinus Torvalds 	int ret = -EINVAL;
6381da177e4SLinus Torvalds 	int chk_addr_ret;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
6411da177e4SLinus Torvalds 		goto out;
6423b1e0a65SYOSHIFUJI Hideaki 	chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
6431da177e4SLinus Torvalds 	ret = -EADDRNOTAVAIL;
6441da177e4SLinus Torvalds 	if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
6451da177e4SLinus Torvalds 	    chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
6461da177e4SLinus Torvalds 		goto out;
647c720c7e8SEric Dumazet 	inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
6481da177e4SLinus Torvalds 	if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
649c720c7e8SEric Dumazet 		inet->inet_saddr = 0;  /* Use device */
6501da177e4SLinus Torvalds 	sk_dst_reset(sk);
6511da177e4SLinus Torvalds 	ret = 0;
6521da177e4SLinus Torvalds out:	return ret;
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds /*
6561da177e4SLinus Torvalds  *	This should be easy, if there is something there
6571da177e4SLinus Torvalds  *	we return it, otherwise we block.
6581da177e4SLinus Torvalds  */
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
6611da177e4SLinus Torvalds 		       size_t len, int noblock, int flags, int *addr_len)
6621da177e4SLinus Torvalds {
6631da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
6641da177e4SLinus Torvalds 	size_t copied = 0;
6651da177e4SLinus Torvalds 	int err = -EOPNOTSUPP;
6661da177e4SLinus Torvalds 	struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
6671da177e4SLinus Torvalds 	struct sk_buff *skb;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	if (flags & MSG_OOB)
6701da177e4SLinus Torvalds 		goto out;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	if (addr_len)
6731da177e4SLinus Torvalds 		*addr_len = sizeof(*sin);
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	if (flags & MSG_ERRQUEUE) {
6761da177e4SLinus Torvalds 		err = ip_recv_error(sk, msg, len);
6771da177e4SLinus Torvalds 		goto out;
6781da177e4SLinus Torvalds 	}
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	skb = skb_recv_datagram(sk, flags, noblock, &err);
6811da177e4SLinus Torvalds 	if (!skb)
6821da177e4SLinus Torvalds 		goto out;
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	copied = skb->len;
6851da177e4SLinus Torvalds 	if (len < copied) {
6861da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
6871da177e4SLinus Torvalds 		copied = len;
6881da177e4SLinus Torvalds 	}
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
6911da177e4SLinus Torvalds 	if (err)
6921da177e4SLinus Torvalds 		goto done;
6931da177e4SLinus Torvalds 
6943b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	/* Copy the address. */
6971da177e4SLinus Torvalds 	if (sin) {
6981da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
699eddc9ec5SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
700f59fc7f3STetsuo Handa 		sin->sin_port = 0;
7011da177e4SLinus Torvalds 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
7021da177e4SLinus Torvalds 	}
7031da177e4SLinus Torvalds 	if (inet->cmsg_flags)
7041da177e4SLinus Torvalds 		ip_cmsg_recv(msg, skb);
7051da177e4SLinus Torvalds 	if (flags & MSG_TRUNC)
7061da177e4SLinus Torvalds 		copied = skb->len;
7071da177e4SLinus Torvalds done:
7081da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
7095418c692SJesper Juhl out:
7105418c692SJesper Juhl 	if (err)
7115418c692SJesper Juhl 		return err;
7125418c692SJesper Juhl 	return copied;
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds static int raw_init(struct sock *sk)
7161da177e4SLinus Torvalds {
7171da177e4SLinus Torvalds 	struct raw_sock *rp = raw_sk(sk);
7181da177e4SLinus Torvalds 
719c720c7e8SEric Dumazet 	if (inet_sk(sk)->inet_num == IPPROTO_ICMP)
7201da177e4SLinus Torvalds 		memset(&rp->filter, 0, sizeof(rp->filter));
7211da177e4SLinus Torvalds 	return 0;
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds static int raw_seticmpfilter(struct sock *sk, char __user *optval, int optlen)
7251da177e4SLinus Torvalds {
7261da177e4SLinus Torvalds 	if (optlen > sizeof(struct icmp_filter))
7271da177e4SLinus Torvalds 		optlen = sizeof(struct icmp_filter);
7281da177e4SLinus Torvalds 	if (copy_from_user(&raw_sk(sk)->filter, optval, optlen))
7291da177e4SLinus Torvalds 		return -EFAULT;
7301da177e4SLinus Torvalds 	return 0;
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
7341da177e4SLinus Torvalds {
7351da177e4SLinus Torvalds 	int len, ret = -EFAULT;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	if (get_user(len, optlen))
7381da177e4SLinus Torvalds 		goto out;
7391da177e4SLinus Torvalds 	ret = -EINVAL;
7401da177e4SLinus Torvalds 	if (len < 0)
7411da177e4SLinus Torvalds 		goto out;
7421da177e4SLinus Torvalds 	if (len > sizeof(struct icmp_filter))
7431da177e4SLinus Torvalds 		len = sizeof(struct icmp_filter);
7441da177e4SLinus Torvalds 	ret = -EFAULT;
7451da177e4SLinus Torvalds 	if (put_user(len, optlen) ||
7461da177e4SLinus Torvalds 	    copy_to_user(optval, &raw_sk(sk)->filter, len))
7471da177e4SLinus Torvalds 		goto out;
7481da177e4SLinus Torvalds 	ret = 0;
7491da177e4SLinus Torvalds out:	return ret;
7501da177e4SLinus Torvalds }
7511da177e4SLinus Torvalds 
7523fdadf7dSDmitry Mishin static int do_raw_setsockopt(struct sock *sk, int level, int optname,
753b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
7541da177e4SLinus Torvalds {
7551da177e4SLinus Torvalds 	if (optname == ICMP_FILTER) {
756c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
7571da177e4SLinus Torvalds 			return -EOPNOTSUPP;
7581da177e4SLinus Torvalds 		else
7591da177e4SLinus Torvalds 			return raw_seticmpfilter(sk, optval, optlen);
7601da177e4SLinus Torvalds 	}
7611da177e4SLinus Torvalds 	return -ENOPROTOOPT;
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
7643fdadf7dSDmitry Mishin static int raw_setsockopt(struct sock *sk, int level, int optname,
765b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
7661da177e4SLinus Torvalds {
7671da177e4SLinus Torvalds 	if (level != SOL_RAW)
7683fdadf7dSDmitry Mishin 		return ip_setsockopt(sk, level, optname, optval, optlen);
7693fdadf7dSDmitry Mishin 	return do_raw_setsockopt(sk, level, optname, optval, optlen);
7703fdadf7dSDmitry Mishin }
7711da177e4SLinus Torvalds 
7723fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
7733fdadf7dSDmitry Mishin static int compat_raw_setsockopt(struct sock *sk, int level, int optname,
774b7058842SDavid S. Miller 				 char __user *optval, unsigned int optlen)
7753fdadf7dSDmitry Mishin {
7763fdadf7dSDmitry Mishin 	if (level != SOL_RAW)
777543d9cfeSArnaldo Carvalho de Melo 		return compat_ip_setsockopt(sk, level, optname, optval, optlen);
7783fdadf7dSDmitry Mishin 	return do_raw_setsockopt(sk, level, optname, optval, optlen);
7793fdadf7dSDmitry Mishin }
7803fdadf7dSDmitry Mishin #endif
7813fdadf7dSDmitry Mishin 
7823fdadf7dSDmitry Mishin static int do_raw_getsockopt(struct sock *sk, int level, int optname,
7833fdadf7dSDmitry Mishin 			  char __user *optval, int __user *optlen)
7843fdadf7dSDmitry Mishin {
7851da177e4SLinus Torvalds 	if (optname == ICMP_FILTER) {
786c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
7871da177e4SLinus Torvalds 			return -EOPNOTSUPP;
7881da177e4SLinus Torvalds 		else
7891da177e4SLinus Torvalds 			return raw_geticmpfilter(sk, optval, optlen);
7901da177e4SLinus Torvalds 	}
7911da177e4SLinus Torvalds 	return -ENOPROTOOPT;
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
7943fdadf7dSDmitry Mishin static int raw_getsockopt(struct sock *sk, int level, int optname,
7953fdadf7dSDmitry Mishin 			  char __user *optval, int __user *optlen)
7963fdadf7dSDmitry Mishin {
7973fdadf7dSDmitry Mishin 	if (level != SOL_RAW)
7983fdadf7dSDmitry Mishin 		return ip_getsockopt(sk, level, optname, optval, optlen);
7993fdadf7dSDmitry Mishin 	return do_raw_getsockopt(sk, level, optname, optval, optlen);
8003fdadf7dSDmitry Mishin }
8013fdadf7dSDmitry Mishin 
8023fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
8033fdadf7dSDmitry Mishin static int compat_raw_getsockopt(struct sock *sk, int level, int optname,
8043fdadf7dSDmitry Mishin 				 char __user *optval, int __user *optlen)
8053fdadf7dSDmitry Mishin {
8063fdadf7dSDmitry Mishin 	if (level != SOL_RAW)
807543d9cfeSArnaldo Carvalho de Melo 		return compat_ip_getsockopt(sk, level, optname, optval, optlen);
8083fdadf7dSDmitry Mishin 	return do_raw_getsockopt(sk, level, optname, optval, optlen);
8093fdadf7dSDmitry Mishin }
8103fdadf7dSDmitry Mishin #endif
8113fdadf7dSDmitry Mishin 
8121da177e4SLinus Torvalds static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
8131da177e4SLinus Torvalds {
8141da177e4SLinus Torvalds 	switch (cmd) {
8151da177e4SLinus Torvalds 		case SIOCOUTQ: {
81631e6d363SEric Dumazet 			int amount = sk_wmem_alloc_get(sk);
81731e6d363SEric Dumazet 
8181da177e4SLinus Torvalds 			return put_user(amount, (int __user *)arg);
8191da177e4SLinus Torvalds 		}
8201da177e4SLinus Torvalds 		case SIOCINQ: {
8211da177e4SLinus Torvalds 			struct sk_buff *skb;
8221da177e4SLinus Torvalds 			int amount = 0;
8231da177e4SLinus Torvalds 
824e0f9f858SHerbert Xu 			spin_lock_bh(&sk->sk_receive_queue.lock);
8251da177e4SLinus Torvalds 			skb = skb_peek(&sk->sk_receive_queue);
8261da177e4SLinus Torvalds 			if (skb != NULL)
8271da177e4SLinus Torvalds 				amount = skb->len;
828e0f9f858SHerbert Xu 			spin_unlock_bh(&sk->sk_receive_queue.lock);
8291da177e4SLinus Torvalds 			return put_user(amount, (int __user *)arg);
8301da177e4SLinus Torvalds 		}
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 		default:
8331da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
8341da177e4SLinus Torvalds 			return ipmr_ioctl(sk, cmd, (void __user *)arg);
8351da177e4SLinus Torvalds #else
8361da177e4SLinus Torvalds 			return -ENOIOCTLCMD;
8371da177e4SLinus Torvalds #endif
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds }
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds struct proto raw_prot = {
8421da177e4SLinus Torvalds 	.name		   = "RAW",
8431da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
8441da177e4SLinus Torvalds 	.close		   = raw_close,
84522dd4850SDenis V. Lunev 	.destroy	   = raw_destroy,
8461da177e4SLinus Torvalds 	.connect	   = ip4_datagram_connect,
8471da177e4SLinus Torvalds 	.disconnect	   = udp_disconnect,
8481da177e4SLinus Torvalds 	.ioctl		   = raw_ioctl,
8491da177e4SLinus Torvalds 	.init		   = raw_init,
8501da177e4SLinus Torvalds 	.setsockopt	   = raw_setsockopt,
8511da177e4SLinus Torvalds 	.getsockopt	   = raw_getsockopt,
8521da177e4SLinus Torvalds 	.sendmsg	   = raw_sendmsg,
8531da177e4SLinus Torvalds 	.recvmsg	   = raw_recvmsg,
8541da177e4SLinus Torvalds 	.bind		   = raw_bind,
8551da177e4SLinus Torvalds 	.backlog_rcv	   = raw_rcv_skb,
856fc8717baSPavel Emelyanov 	.hash		   = raw_hash_sk,
857fc8717baSPavel Emelyanov 	.unhash		   = raw_unhash_sk,
8581da177e4SLinus Torvalds 	.obj_size	   = sizeof(struct raw_sock),
859fc8717baSPavel Emelyanov 	.h.raw_hash	   = &raw_v4_hashinfo,
860543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
861543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_raw_setsockopt,
862543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_raw_getsockopt,
863543d9cfeSArnaldo Carvalho de Melo #endif
8641da177e4SLinus Torvalds };
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
8671da177e4SLinus Torvalds static struct sock *raw_get_first(struct seq_file *seq)
8681da177e4SLinus Torvalds {
8691da177e4SLinus Torvalds 	struct sock *sk;
8701da177e4SLinus Torvalds 	struct raw_iter_state *state = raw_seq_private(seq);
8711da177e4SLinus Torvalds 
872b673e4dfSPavel Emelyanov 	for (state->bucket = 0; state->bucket < RAW_HTABLE_SIZE;
873b673e4dfSPavel Emelyanov 			++state->bucket) {
8741da177e4SLinus Torvalds 		struct hlist_node *node;
8751da177e4SLinus Torvalds 
87642a73808SPavel Emelyanov 		sk_for_each(sk, node, &state->h->ht[state->bucket])
8771218854aSYOSHIFUJI Hideaki 			if (sock_net(sk) == seq_file_net(seq))
8781da177e4SLinus Torvalds 				goto found;
8791da177e4SLinus Torvalds 	}
8801da177e4SLinus Torvalds 	sk = NULL;
8811da177e4SLinus Torvalds found:
8821da177e4SLinus Torvalds 	return sk;
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
8861da177e4SLinus Torvalds {
8871da177e4SLinus Torvalds 	struct raw_iter_state *state = raw_seq_private(seq);
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	do {
8901da177e4SLinus Torvalds 		sk = sk_next(sk);
8911da177e4SLinus Torvalds try_again:
8921da177e4SLinus Torvalds 		;
8931218854aSYOSHIFUJI Hideaki 	} while (sk && sock_net(sk) != seq_file_net(seq));
8941da177e4SLinus Torvalds 
895b673e4dfSPavel Emelyanov 	if (!sk && ++state->bucket < RAW_HTABLE_SIZE) {
89642a73808SPavel Emelyanov 		sk = sk_head(&state->h->ht[state->bucket]);
8971da177e4SLinus Torvalds 		goto try_again;
8981da177e4SLinus Torvalds 	}
8991da177e4SLinus Torvalds 	return sk;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	struct sock *sk = raw_get_first(seq);
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	if (sk)
9071da177e4SLinus Torvalds 		while (pos && (sk = raw_get_next(seq, sk)) != NULL)
9081da177e4SLinus Torvalds 			--pos;
9091da177e4SLinus Torvalds 	return pos ? NULL : sk;
9101da177e4SLinus Torvalds }
9111da177e4SLinus Torvalds 
91242a73808SPavel Emelyanov void *raw_seq_start(struct seq_file *seq, loff_t *pos)
9131da177e4SLinus Torvalds {
91442a73808SPavel Emelyanov 	struct raw_iter_state *state = raw_seq_private(seq);
91542a73808SPavel Emelyanov 
91642a73808SPavel Emelyanov 	read_lock(&state->h->lock);
9171da177e4SLinus Torvalds 	return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
9181da177e4SLinus Torvalds }
91942a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_start);
9201da177e4SLinus Torvalds 
92142a73808SPavel Emelyanov void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
9221da177e4SLinus Torvalds {
9231da177e4SLinus Torvalds 	struct sock *sk;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
9261da177e4SLinus Torvalds 		sk = raw_get_first(seq);
9271da177e4SLinus Torvalds 	else
9281da177e4SLinus Torvalds 		sk = raw_get_next(seq, v);
9291da177e4SLinus Torvalds 	++*pos;
9301da177e4SLinus Torvalds 	return sk;
9311da177e4SLinus Torvalds }
93242a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_next);
9331da177e4SLinus Torvalds 
93442a73808SPavel Emelyanov void raw_seq_stop(struct seq_file *seq, void *v)
9351da177e4SLinus Torvalds {
93642a73808SPavel Emelyanov 	struct raw_iter_state *state = raw_seq_private(seq);
93742a73808SPavel Emelyanov 
93842a73808SPavel Emelyanov 	read_unlock(&state->h->lock);
9391da177e4SLinus Torvalds }
94042a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_stop);
9411da177e4SLinus Torvalds 
9428cd850efSDenis V. Lunev static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
9431da177e4SLinus Torvalds {
9441da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sp);
945c720c7e8SEric Dumazet 	__be32 dest = inet->inet_daddr,
946c720c7e8SEric Dumazet 	       src = inet->inet_rcv_saddr;
9471da177e4SLinus Torvalds 	__u16 destp = 0,
948c720c7e8SEric Dumazet 	      srcp  = inet->inet_num;
9491da177e4SLinus Torvalds 
9508cd850efSDenis V. Lunev 	seq_printf(seq, "%4d: %08X:%04X %08X:%04X"
95168be802cSEric Dumazet 		" %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
9521da177e4SLinus Torvalds 		i, src, srcp, dest, destp, sp->sk_state,
95331e6d363SEric Dumazet 		sk_wmem_alloc_get(sp),
95431e6d363SEric Dumazet 		sk_rmem_alloc_get(sp),
9551da177e4SLinus Torvalds 		0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
95633c732c3SWang Chen 		atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds static int raw_seq_show(struct seq_file *seq, void *v)
9601da177e4SLinus Torvalds {
9611da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
9628cd850efSDenis V. Lunev 		seq_printf(seq, "  sl  local_address rem_address   st tx_queue "
9631da177e4SLinus Torvalds 				"rx_queue tr tm->when retrnsmt   uid  timeout "
964cb61cb9bSEric Dumazet 				"inode ref pointer drops\n");
9658cd850efSDenis V. Lunev 	else
9668cd850efSDenis V. Lunev 		raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
9671da177e4SLinus Torvalds 	return 0;
9681da177e4SLinus Torvalds }
9691da177e4SLinus Torvalds 
970f690808eSStephen Hemminger static const struct seq_operations raw_seq_ops = {
9711da177e4SLinus Torvalds 	.start = raw_seq_start,
9721da177e4SLinus Torvalds 	.next  = raw_seq_next,
9731da177e4SLinus Torvalds 	.stop  = raw_seq_stop,
9741da177e4SLinus Torvalds 	.show  = raw_seq_show,
9751da177e4SLinus Torvalds };
9761da177e4SLinus Torvalds 
9773046d767SDenis V. Lunev int raw_seq_open(struct inode *ino, struct file *file,
9783046d767SDenis V. Lunev 		 struct raw_hashinfo *h, const struct seq_operations *ops)
9791da177e4SLinus Torvalds {
980f51d599fSPavel Emelyanov 	int err;
98142a73808SPavel Emelyanov 	struct raw_iter_state *i;
98242a73808SPavel Emelyanov 
9833046d767SDenis V. Lunev 	err = seq_open_net(ino, file, ops, sizeof(struct raw_iter_state));
984f51d599fSPavel Emelyanov 	if (err < 0)
985f51d599fSPavel Emelyanov 		return err;
98642a73808SPavel Emelyanov 
987f51d599fSPavel Emelyanov 	i = raw_seq_private((struct seq_file *)file->private_data);
98842a73808SPavel Emelyanov 	i->h = h;
98942a73808SPavel Emelyanov 	return 0;
99042a73808SPavel Emelyanov }
99142a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_open);
99242a73808SPavel Emelyanov 
99342a73808SPavel Emelyanov static int raw_v4_seq_open(struct inode *inode, struct file *file)
99442a73808SPavel Emelyanov {
9953046d767SDenis V. Lunev 	return raw_seq_open(inode, file, &raw_v4_hashinfo, &raw_seq_ops);
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
9989a32144eSArjan van de Ven static const struct file_operations raw_seq_fops = {
9991da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
100042a73808SPavel Emelyanov 	.open	 = raw_v4_seq_open,
10011da177e4SLinus Torvalds 	.read	 = seq_read,
10021da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
1003f51d599fSPavel Emelyanov 	.release = seq_release_net,
10041da177e4SLinus Torvalds };
10051da177e4SLinus Torvalds 
1006a308da16SPavel Emelyanov static __net_init int raw_init_net(struct net *net)
1007a308da16SPavel Emelyanov {
1008a308da16SPavel Emelyanov 	if (!proc_net_fops_create(net, "raw", S_IRUGO, &raw_seq_fops))
1009a308da16SPavel Emelyanov 		return -ENOMEM;
1010a308da16SPavel Emelyanov 
1011a308da16SPavel Emelyanov 	return 0;
1012a308da16SPavel Emelyanov }
1013a308da16SPavel Emelyanov 
1014a308da16SPavel Emelyanov static __net_exit void raw_exit_net(struct net *net)
1015a308da16SPavel Emelyanov {
1016a308da16SPavel Emelyanov 	proc_net_remove(net, "raw");
1017a308da16SPavel Emelyanov }
1018a308da16SPavel Emelyanov 
1019a308da16SPavel Emelyanov static __net_initdata struct pernet_operations raw_net_ops = {
1020a308da16SPavel Emelyanov 	.init = raw_init_net,
1021a308da16SPavel Emelyanov 	.exit = raw_exit_net,
1022a308da16SPavel Emelyanov };
1023a308da16SPavel Emelyanov 
10241da177e4SLinus Torvalds int __init raw_proc_init(void)
10251da177e4SLinus Torvalds {
1026a308da16SPavel Emelyanov 	return register_pernet_subsys(&raw_net_ops);
10271da177e4SLinus Torvalds }
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds void __init raw_proc_exit(void)
10301da177e4SLinus Torvalds {
1031a308da16SPavel Emelyanov 	unregister_pernet_subsys(&raw_net_ops);
10321da177e4SLinus Torvalds }
10331da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
1034