xref: /linux/net/ipv4/raw.c (revision 21226abb4e9f14d88238964d89b279e461ddc30c)
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>
4160063497SArun Sharma #include <linux/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>
51bc3b2d7fSPaul Gortmaker #include <linux/export.h>
521da177e4SLinus Torvalds #include <linux/spinlock.h>
531da177e4SLinus Torvalds #include <linux/sockios.h>
541da177e4SLinus Torvalds #include <linux/socket.h>
551da177e4SLinus Torvalds #include <linux/in.h>
561da177e4SLinus Torvalds #include <linux/mroute.h>
571da177e4SLinus Torvalds #include <linux/netdevice.h>
581da177e4SLinus Torvalds #include <linux/in_route.h>
591da177e4SLinus Torvalds #include <linux/route.h>
601da177e4SLinus Torvalds #include <linux/skbuff.h>
61f5220d63SQuentin Armitage #include <linux/igmp.h>
62457c4cbcSEric W. Biederman #include <net/net_namespace.h>
631da177e4SLinus Torvalds #include <net/dst.h>
641da177e4SLinus Torvalds #include <net/sock.h>
651da177e4SLinus Torvalds #include <linux/ip.h>
661da177e4SLinus Torvalds #include <linux/net.h>
671da177e4SLinus Torvalds #include <net/ip.h>
681da177e4SLinus Torvalds #include <net/icmp.h>
691da177e4SLinus Torvalds #include <net/udp.h>
701da177e4SLinus Torvalds #include <net/raw.h>
711da177e4SLinus Torvalds #include <net/snmp.h>
72c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
731da177e4SLinus Torvalds #include <net/inet_common.h>
741da177e4SLinus Torvalds #include <net/checksum.h>
751da177e4SLinus Torvalds #include <net/xfrm.h>
761da177e4SLinus Torvalds #include <linux/rtnetlink.h>
771da177e4SLinus Torvalds #include <linux/proc_fs.h>
781da177e4SLinus Torvalds #include <linux/seq_file.h>
791da177e4SLinus Torvalds #include <linux/netfilter.h>
801da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
81709b46e8SEric W. Biederman #include <linux/compat.h>
82c008ba5bSHerbert Xu #include <linux/uio.h>
83c008ba5bSHerbert Xu 
84c008ba5bSHerbert Xu struct raw_frag_vec {
85b61e9dccSAl Viro 	struct msghdr *msg;
86c008ba5bSHerbert Xu 	union {
87c008ba5bSHerbert Xu 		struct icmphdr icmph;
88c008ba5bSHerbert Xu 		char c[1];
89c008ba5bSHerbert Xu 	} hdr;
90c008ba5bSHerbert Xu 	int hlen;
91c008ba5bSHerbert Xu };
921da177e4SLinus Torvalds 
93b673e4dfSPavel Emelyanov static struct raw_hashinfo raw_v4_hashinfo = {
94938b93adSRobert P. J. Day 	.lock = __RW_LOCK_UNLOCKED(raw_v4_hashinfo.lock),
95b673e4dfSPavel Emelyanov };
961da177e4SLinus Torvalds 
97fc8717baSPavel Emelyanov void raw_hash_sk(struct sock *sk)
981da177e4SLinus Torvalds {
99fc8717baSPavel Emelyanov 	struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
10065b4c50bSPavel Emelyanov 	struct hlist_head *head;
1011da177e4SLinus Torvalds 
102c720c7e8SEric Dumazet 	head = &h->ht[inet_sk(sk)->inet_num & (RAW_HTABLE_SIZE - 1)];
10365b4c50bSPavel Emelyanov 
10465b4c50bSPavel Emelyanov 	write_lock_bh(&h->lock);
1051da177e4SLinus Torvalds 	sk_add_node(sk, head);
106c29a0bc4SPavel Emelyanov 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
10765b4c50bSPavel Emelyanov 	write_unlock_bh(&h->lock);
10865b4c50bSPavel Emelyanov }
10965b4c50bSPavel Emelyanov EXPORT_SYMBOL_GPL(raw_hash_sk);
11065b4c50bSPavel Emelyanov 
111fc8717baSPavel Emelyanov void raw_unhash_sk(struct sock *sk)
112ab70768eSPavel Emelyanov {
113fc8717baSPavel Emelyanov 	struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
114fc8717baSPavel Emelyanov 
115ab70768eSPavel Emelyanov 	write_lock_bh(&h->lock);
116ab70768eSPavel Emelyanov 	if (sk_del_node_init(sk))
117c29a0bc4SPavel Emelyanov 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
118ab70768eSPavel Emelyanov 	write_unlock_bh(&h->lock);
119ab70768eSPavel Emelyanov }
120ab70768eSPavel Emelyanov EXPORT_SYMBOL_GPL(raw_unhash_sk);
121ab70768eSPavel Emelyanov 
122be185884SPavel Emelyanov static struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
123be185884SPavel Emelyanov 		unsigned short num, __be32 raddr, __be32 laddr, int dif)
1241da177e4SLinus Torvalds {
125b67bfe0dSSasha Levin 	sk_for_each_from(sk) {
1261da177e4SLinus Torvalds 		struct inet_sock *inet = inet_sk(sk);
1271da177e4SLinus Torvalds 
128c720c7e8SEric Dumazet 		if (net_eq(sock_net(sk), net) && inet->inet_num == num	&&
129c720c7e8SEric Dumazet 		    !(inet->inet_daddr && inet->inet_daddr != raddr) 	&&
130c720c7e8SEric Dumazet 		    !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
1311da177e4SLinus Torvalds 		    !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
1321da177e4SLinus Torvalds 			goto found; /* gotcha */
1331da177e4SLinus Torvalds 	}
1341da177e4SLinus Torvalds 	sk = NULL;
1351da177e4SLinus Torvalds found:
1361da177e4SLinus Torvalds 	return sk;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds  *	0 - deliver
1411da177e4SLinus Torvalds  *	1 - block
1421da177e4SLinus Torvalds  */
143ab43ed8bSEric Dumazet static int icmp_filter(const struct sock *sk, const struct sk_buff *skb)
1441da177e4SLinus Torvalds {
145ab43ed8bSEric Dumazet 	struct icmphdr _hdr;
146ab43ed8bSEric Dumazet 	const struct icmphdr *hdr;
1471da177e4SLinus Torvalds 
148ab43ed8bSEric Dumazet 	hdr = skb_header_pointer(skb, skb_transport_offset(skb),
149ab43ed8bSEric Dumazet 				 sizeof(_hdr), &_hdr);
150ab43ed8bSEric Dumazet 	if (!hdr)
1511da177e4SLinus Torvalds 		return 1;
1521da177e4SLinus Torvalds 
153ab43ed8bSEric Dumazet 	if (hdr->type < 32) {
1541da177e4SLinus Torvalds 		__u32 data = raw_sk(sk)->filter.data;
1551da177e4SLinus Torvalds 
156ab43ed8bSEric Dumazet 		return ((1U << hdr->type) & data) != 0;
1571da177e4SLinus Torvalds 	}
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	/* Do not block unknown ICMP types */
1601da177e4SLinus Torvalds 	return 0;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds /* IP input processing comes here for RAW socket delivery.
1641da177e4SLinus Torvalds  * Caller owns SKB, so we must make clones.
1651da177e4SLinus Torvalds  *
1661da177e4SLinus Torvalds  * RFC 1122: SHOULD pass TOS value up to the transport layer.
1671da177e4SLinus Torvalds  * -> It does. And not only TOS, but all IP header.
1681da177e4SLinus Torvalds  */
169b71d1d42SEric Dumazet static int raw_v4_input(struct sk_buff *skb, const struct iphdr *iph, int hash)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	struct sock *sk;
1721da177e4SLinus Torvalds 	struct hlist_head *head;
173d13964f4SPatrick McHardy 	int delivered = 0;
174be185884SPavel Emelyanov 	struct net *net;
1751da177e4SLinus Torvalds 
176b673e4dfSPavel Emelyanov 	read_lock(&raw_v4_hashinfo.lock);
177b673e4dfSPavel Emelyanov 	head = &raw_v4_hashinfo.ht[hash];
1781da177e4SLinus Torvalds 	if (hlist_empty(head))
1791da177e4SLinus Torvalds 		goto out;
180be185884SPavel Emelyanov 
181c346dca1SYOSHIFUJI Hideaki 	net = dev_net(skb->dev);
182be185884SPavel Emelyanov 	sk = __raw_v4_lookup(net, __sk_head(head), iph->protocol,
1831da177e4SLinus Torvalds 			     iph->saddr, iph->daddr,
1841da177e4SLinus Torvalds 			     skb->dev->ifindex);
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	while (sk) {
187d13964f4SPatrick McHardy 		delivered = 1;
188f5220d63SQuentin Armitage 		if ((iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) &&
189f5220d63SQuentin Armitage 		    ip_mc_sf_allow(sk, iph->daddr, iph->saddr,
190f5220d63SQuentin Armitage 				   skb->dev->ifindex)) {
1911da177e4SLinus Torvalds 			struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 			/* Not releasing hash table! */
1941da177e4SLinus Torvalds 			if (clone)
1951da177e4SLinus Torvalds 				raw_rcv(sk, clone);
1961da177e4SLinus Torvalds 		}
197be185884SPavel Emelyanov 		sk = __raw_v4_lookup(net, sk_next(sk), iph->protocol,
1981da177e4SLinus Torvalds 				     iph->saddr, iph->daddr,
1991da177e4SLinus Torvalds 				     skb->dev->ifindex);
2001da177e4SLinus Torvalds 	}
2011da177e4SLinus Torvalds out:
202b673e4dfSPavel Emelyanov 	read_unlock(&raw_v4_hashinfo.lock);
203d13964f4SPatrick McHardy 	return delivered;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2067bc54c90SPavel Emelyanov int raw_local_deliver(struct sk_buff *skb, int protocol)
2077bc54c90SPavel Emelyanov {
2087bc54c90SPavel Emelyanov 	int hash;
2097bc54c90SPavel Emelyanov 	struct sock *raw_sk;
2107bc54c90SPavel Emelyanov 
211b673e4dfSPavel Emelyanov 	hash = protocol & (RAW_HTABLE_SIZE - 1);
212b673e4dfSPavel Emelyanov 	raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
2137bc54c90SPavel Emelyanov 
2147bc54c90SPavel Emelyanov 	/* If there maybe a raw socket we must check - if not we
2157bc54c90SPavel Emelyanov 	 * don't care less
2167bc54c90SPavel Emelyanov 	 */
2177bc54c90SPavel Emelyanov 	if (raw_sk && !raw_v4_input(skb, ip_hdr(skb), hash))
2187bc54c90SPavel Emelyanov 		raw_sk = NULL;
2197bc54c90SPavel Emelyanov 
2207bc54c90SPavel Emelyanov 	return raw_sk != NULL;
2217bc54c90SPavel Emelyanov 
2227bc54c90SPavel Emelyanov }
2237bc54c90SPavel Emelyanov 
2247bc54c90SPavel Emelyanov static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)
2251da177e4SLinus Torvalds {
2261da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
22788c7664fSArnaldo Carvalho de Melo 	const int type = icmp_hdr(skb)->type;
22888c7664fSArnaldo Carvalho de Melo 	const int code = icmp_hdr(skb)->code;
2291da177e4SLinus Torvalds 	int err = 0;
2301da177e4SLinus Torvalds 	int harderr = 0;
2311da177e4SLinus Torvalds 
23236393395SDavid S. Miller 	if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
23336393395SDavid S. Miller 		ipv4_sk_update_pmtu(skb, sk, info);
2348d65b119SDuan Jiong 	else if (type == ICMP_REDIRECT) {
23555be7a9cSDavid S. Miller 		ipv4_sk_redirect(skb, sk);
2368d65b119SDuan Jiong 		return;
2378d65b119SDuan Jiong 	}
23836393395SDavid S. Miller 
2391da177e4SLinus Torvalds 	/* Report error on raw socket, if:
2401da177e4SLinus Torvalds 	   1. User requested ip_recverr.
2411da177e4SLinus Torvalds 	   2. Socket is connected (otherwise the error indication
2421da177e4SLinus Torvalds 	      is useless without ip_recverr and error is hard.
2431da177e4SLinus Torvalds 	 */
2441da177e4SLinus Torvalds 	if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED)
2451da177e4SLinus Torvalds 		return;
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	switch (type) {
2481da177e4SLinus Torvalds 	default:
2491da177e4SLinus Torvalds 	case ICMP_TIME_EXCEEDED:
2501da177e4SLinus Torvalds 		err = EHOSTUNREACH;
2511da177e4SLinus Torvalds 		break;
2521da177e4SLinus Torvalds 	case ICMP_SOURCE_QUENCH:
2531da177e4SLinus Torvalds 		return;
2541da177e4SLinus Torvalds 	case ICMP_PARAMETERPROB:
2551da177e4SLinus Torvalds 		err = EPROTO;
2561da177e4SLinus Torvalds 		harderr = 1;
2571da177e4SLinus Torvalds 		break;
2581da177e4SLinus Torvalds 	case ICMP_DEST_UNREACH:
2591da177e4SLinus Torvalds 		err = EHOSTUNREACH;
2601da177e4SLinus Torvalds 		if (code > NR_ICMP_UNREACH)
2611da177e4SLinus Torvalds 			break;
2621da177e4SLinus Torvalds 		err = icmp_err_convert[code].errno;
2631da177e4SLinus Torvalds 		harderr = icmp_err_convert[code].fatal;
2641da177e4SLinus Torvalds 		if (code == ICMP_FRAG_NEEDED) {
2651da177e4SLinus Torvalds 			harderr = inet->pmtudisc != IP_PMTUDISC_DONT;
2661da177e4SLinus Torvalds 			err = EMSGSIZE;
2671da177e4SLinus Torvalds 		}
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	if (inet->recverr) {
271b71d1d42SEric Dumazet 		const struct iphdr *iph = (const struct iphdr *)skb->data;
2721da177e4SLinus Torvalds 		u8 *payload = skb->data + (iph->ihl << 2);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 		if (inet->hdrincl)
2751da177e4SLinus Torvalds 			payload = skb->data;
2761da177e4SLinus Torvalds 		ip_icmp_error(sk, skb, err, 0, info, payload);
2771da177e4SLinus Torvalds 	}
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	if (inet->recverr || harderr) {
2801da177e4SLinus Torvalds 		sk->sk_err = err;
2811da177e4SLinus Torvalds 		sk->sk_error_report(sk);
2821da177e4SLinus Torvalds 	}
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
2857bc54c90SPavel Emelyanov void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info)
2867bc54c90SPavel Emelyanov {
2877bc54c90SPavel Emelyanov 	int hash;
2887bc54c90SPavel Emelyanov 	struct sock *raw_sk;
289b71d1d42SEric Dumazet 	const struct iphdr *iph;
290be185884SPavel Emelyanov 	struct net *net;
2917bc54c90SPavel Emelyanov 
292b673e4dfSPavel Emelyanov 	hash = protocol & (RAW_HTABLE_SIZE - 1);
2937bc54c90SPavel Emelyanov 
294b673e4dfSPavel Emelyanov 	read_lock(&raw_v4_hashinfo.lock);
295b673e4dfSPavel Emelyanov 	raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
2967bc54c90SPavel Emelyanov 	if (raw_sk != NULL) {
297b71d1d42SEric Dumazet 		iph = (const struct iphdr *)skb->data;
298c346dca1SYOSHIFUJI Hideaki 		net = dev_net(skb->dev);
299be185884SPavel Emelyanov 
300be185884SPavel Emelyanov 		while ((raw_sk = __raw_v4_lookup(net, raw_sk, protocol,
301be185884SPavel Emelyanov 						iph->daddr, iph->saddr,
3027bc54c90SPavel Emelyanov 						skb->dev->ifindex)) != NULL) {
3037bc54c90SPavel Emelyanov 			raw_err(raw_sk, skb, info);
3047bc54c90SPavel Emelyanov 			raw_sk = sk_next(raw_sk);
305b71d1d42SEric Dumazet 			iph = (const struct iphdr *)skb->data;
3067bc54c90SPavel Emelyanov 		}
3077bc54c90SPavel Emelyanov 	}
308b673e4dfSPavel Emelyanov 	read_unlock(&raw_v4_hashinfo.lock);
3097bc54c90SPavel Emelyanov }
3107bc54c90SPavel Emelyanov 
3111da177e4SLinus Torvalds static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	/* Charge it to the socket. */
3141da177e4SLinus Torvalds 
315fbf8866dSShawn Bohrer 	ipv4_pktinfo_prepare(sk, skb);
316d826eb14SEric Dumazet 	if (sock_queue_rcv_skb(sk, skb) < 0) {
3171da177e4SLinus Torvalds 		kfree_skb(skb);
3181da177e4SLinus Torvalds 		return NET_RX_DROP;
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	return NET_RX_SUCCESS;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds int raw_rcv(struct sock *sk, struct sk_buff *skb)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
32733c732c3SWang Chen 		atomic_inc(&sk->sk_drops);
3281da177e4SLinus Torvalds 		kfree_skb(skb);
3291da177e4SLinus Torvalds 		return NET_RX_DROP;
3301da177e4SLinus Torvalds 	}
331b59c2701SPatrick McHardy 	nf_reset(skb);
3321da177e4SLinus Torvalds 
333d56f90a7SArnaldo Carvalho de Melo 	skb_push(skb, skb->data - skb_network_header(skb));
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	raw_rcv_skb(sk, skb);
3361da177e4SLinus Torvalds 	return 0;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
33977968b78SDavid S. Miller static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
3407ae9abfdSAl Viro 			   struct msghdr *msg, size_t length,
3411789a640SEric Dumazet 			   struct rtable **rtp,
3421da177e4SLinus Torvalds 			   unsigned int flags)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
3450388b004SPavel Emelyanov 	struct net *net = sock_net(sk);
3461da177e4SLinus Torvalds 	struct iphdr *iph;
3471da177e4SLinus Torvalds 	struct sk_buff *skb;
348f844c74fSHerbert Xu 	unsigned int iphlen;
3491da177e4SLinus Torvalds 	int err;
3501789a640SEric Dumazet 	struct rtable *rt = *rtp;
35166088243SHerbert Xu 	int hlen, tlen;
3521da177e4SLinus Torvalds 
353d8d1f30bSChangli Gao 	if (length > rt->dst.dev->mtu) {
35477968b78SDavid S. Miller 		ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
355d8d1f30bSChangli Gao 			       rt->dst.dev->mtu);
3561da177e4SLinus Torvalds 		return -EMSGSIZE;
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
3591da177e4SLinus Torvalds 		goto out;
3601da177e4SLinus Torvalds 
36166088243SHerbert Xu 	hlen = LL_RESERVED_SPACE(rt->dst.dev);
36266088243SHerbert Xu 	tlen = rt->dst.dev->needed_tailroom;
363f5184d26SJohannes Berg 	skb = sock_alloc_send_skb(sk,
36466088243SHerbert Xu 				  length + hlen + tlen + 15,
3651da177e4SLinus Torvalds 				  flags & MSG_DONTWAIT, &err);
3661da177e4SLinus Torvalds 	if (skb == NULL)
3671da177e4SLinus Torvalds 		goto error;
36866088243SHerbert Xu 	skb_reserve(skb, hlen);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
3714a19ec58SLaszlo Attila Toth 	skb->mark = sk->sk_mark;
372d8d1f30bSChangli Gao 	skb_dst_set(skb, &rt->dst);
3731789a640SEric Dumazet 	*rtp = NULL;
3741da177e4SLinus Torvalds 
3757e28ecc2SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
376eddc9ec5SArnaldo Carvalho de Melo 	iph = ip_hdr(skb);
3777e28ecc2SArnaldo Carvalho de Melo 	skb_put(skb, length);
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	skb->ip_summed = CHECKSUM_NONE;
3801da177e4SLinus Torvalds 
38111878b40SWillem de Bruijn 	sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
38211878b40SWillem de Bruijn 
383b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = skb->network_header;
38455888dfbSNeil Horman 	err = -EFAULT;
385*21226abbSAl Viro 	if (memcpy_from_msg(iph, msg, length))
38655888dfbSNeil Horman 		goto error_free;
3871da177e4SLinus Torvalds 
388f844c74fSHerbert Xu 	iphlen = iph->ihl * 4;
38955888dfbSNeil Horman 
39055888dfbSNeil Horman 	/*
39155888dfbSNeil Horman 	 * We don't want to modify the ip header, but we do need to
39255888dfbSNeil Horman 	 * be sure that it won't cause problems later along the network
39355888dfbSNeil Horman 	 * stack.  Specifically we want to make sure that iph->ihl is a
39455888dfbSNeil Horman 	 * sane value.  If ihl points beyond the length of the buffer passed
39555888dfbSNeil Horman 	 * in, reject the frame as invalid
39655888dfbSNeil Horman 	 */
39755888dfbSNeil Horman 	err = -EINVAL;
39855888dfbSNeil Horman 	if (iphlen > length)
39955888dfbSNeil Horman 		goto error_free;
40055888dfbSNeil Horman 
40155888dfbSNeil Horman 	if (iphlen >= sizeof(*iph)) {
4021da177e4SLinus Torvalds 		if (!iph->saddr)
40377968b78SDavid S. Miller 			iph->saddr = fl4->saddr;
4041da177e4SLinus Torvalds 		iph->check   = 0;
4051da177e4SLinus Torvalds 		iph->tot_len = htons(length);
4061da177e4SLinus Torvalds 		if (!iph->id)
40773f156a6SEric Dumazet 			ip_select_ident(skb, NULL);
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 		iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
4101da177e4SLinus Torvalds 	}
41196793b48SDavid L Stevens 	if (iph->protocol == IPPROTO_ICMP)
4120388b004SPavel Emelyanov 		icmp_out_count(net, ((struct icmphdr *)
41396793b48SDavid L Stevens 			skb_transport_header(skb))->type);
4141da177e4SLinus Torvalds 
4159bbc768aSJan Engelhardt 	err = NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_OUT, skb, NULL,
416d8d1f30bSChangli Gao 		      rt->dst.dev, dst_output);
4171da177e4SLinus Torvalds 	if (err > 0)
4186ce9e7b5SEric Dumazet 		err = net_xmit_errno(err);
4191da177e4SLinus Torvalds 	if (err)
4201da177e4SLinus Torvalds 		goto error;
4211da177e4SLinus Torvalds out:
4221da177e4SLinus Torvalds 	return 0;
4231da177e4SLinus Torvalds 
42455888dfbSNeil Horman error_free:
4251da177e4SLinus Torvalds 	kfree_skb(skb);
4261da177e4SLinus Torvalds error:
4275e38e270SPavel Emelyanov 	IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
4286ce9e7b5SEric Dumazet 	if (err == -ENOBUFS && !inet->recverr)
4296ce9e7b5SEric Dumazet 		err = 0;
4301da177e4SLinus Torvalds 	return err;
4311da177e4SLinus Torvalds }
4321da177e4SLinus Torvalds 
433c008ba5bSHerbert Xu static int raw_probe_proto_opt(struct raw_frag_vec *rfv, struct flowi4 *fl4)
4341da177e4SLinus Torvalds {
43532b5913aSHerbert Xu 	int err;
4361da177e4SLinus Torvalds 
43732b5913aSHerbert Xu 	if (fl4->flowi4_proto != IPPROTO_ICMP)
438a27b58feSHeiko Carstens 		return 0;
4391da177e4SLinus Torvalds 
44032b5913aSHerbert Xu 	/* We only need the first two bytes. */
441c008ba5bSHerbert Xu 	rfv->hlen = 2;
442c008ba5bSHerbert Xu 
443b61e9dccSAl Viro 	err = memcpy_from_msg(rfv->hdr.c, rfv->msg, rfv->hlen);
44432b5913aSHerbert Xu 	if (err)
44532b5913aSHerbert Xu 		return err;
4461da177e4SLinus Torvalds 
447c008ba5bSHerbert Xu 	fl4->fl4_icmp_type = rfv->hdr.icmph.type;
448c008ba5bSHerbert Xu 	fl4->fl4_icmp_code = rfv->hdr.icmph.code;
4491da177e4SLinus Torvalds 
450a27b58feSHeiko Carstens 	return 0;
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
453c008ba5bSHerbert Xu static int raw_getfrag(void *from, char *to, int offset, int len, int odd,
454c008ba5bSHerbert Xu 		       struct sk_buff *skb)
455c008ba5bSHerbert Xu {
456c008ba5bSHerbert Xu 	struct raw_frag_vec *rfv = from;
457c008ba5bSHerbert Xu 
458c008ba5bSHerbert Xu 	if (offset < rfv->hlen) {
459c008ba5bSHerbert Xu 		int copy = min(rfv->hlen - offset, len);
460c008ba5bSHerbert Xu 
461c008ba5bSHerbert Xu 		if (skb->ip_summed == CHECKSUM_PARTIAL)
462c008ba5bSHerbert Xu 			memcpy(to, rfv->hdr.c + offset, copy);
463c008ba5bSHerbert Xu 		else
464c008ba5bSHerbert Xu 			skb->csum = csum_block_add(
465c008ba5bSHerbert Xu 				skb->csum,
466c008ba5bSHerbert Xu 				csum_partial_copy_nocheck(rfv->hdr.c + offset,
467c008ba5bSHerbert Xu 							  to, copy, 0),
468c008ba5bSHerbert Xu 				odd);
469c008ba5bSHerbert Xu 
470c008ba5bSHerbert Xu 		odd = 0;
471c008ba5bSHerbert Xu 		offset += copy;
472c008ba5bSHerbert Xu 		to += copy;
473c008ba5bSHerbert Xu 		len -= copy;
474c008ba5bSHerbert Xu 
475c008ba5bSHerbert Xu 		if (!len)
476c008ba5bSHerbert Xu 			return 0;
477c008ba5bSHerbert Xu 	}
478c008ba5bSHerbert Xu 
479c008ba5bSHerbert Xu 	offset -= rfv->hlen;
480c008ba5bSHerbert Xu 
481f69e6d13SAl Viro 	return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
482c008ba5bSHerbert Xu }
483c008ba5bSHerbert Xu 
4841da177e4SLinus Torvalds static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
4851da177e4SLinus Torvalds 		       size_t len)
4861da177e4SLinus Torvalds {
4871da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
4881da177e4SLinus Torvalds 	struct ipcm_cookie ipc;
4891da177e4SLinus Torvalds 	struct rtable *rt = NULL;
49077968b78SDavid S. Miller 	struct flowi4 fl4;
4911da177e4SLinus Torvalds 	int free = 0;
4923ca3c68eSAl Viro 	__be32 daddr;
493c1d18f9fSAl Viro 	__be32 saddr;
4941da177e4SLinus Torvalds 	u8  tos;
4951da177e4SLinus Torvalds 	int err;
496f6d8bd05SEric Dumazet 	struct ip_options_data opt_copy;
497c008ba5bSHerbert Xu 	struct raw_frag_vec rfv;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	err = -EMSGSIZE;
500926d4b81SJesper Juhl 	if (len > 0xFFFF)
5011da177e4SLinus Torvalds 		goto out;
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	/*
5041da177e4SLinus Torvalds 	 *	Check the flags.
5051da177e4SLinus Torvalds 	 */
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	err = -EOPNOTSUPP;
5081da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)	/* Mirror BSD error message */
5091da177e4SLinus Torvalds 		goto out;               /* compatibility */
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	/*
5121da177e4SLinus Torvalds 	 *	Get and verify the address.
5131da177e4SLinus Torvalds 	 */
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	if (msg->msg_namelen) {
516342dfc30SSteffen Hurrle 		DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
5171da177e4SLinus Torvalds 		err = -EINVAL;
5181da177e4SLinus Torvalds 		if (msg->msg_namelen < sizeof(*usin))
5191da177e4SLinus Torvalds 			goto out;
5201da177e4SLinus Torvalds 		if (usin->sin_family != AF_INET) {
521058bd4d2SJoe Perches 			pr_info_once("%s: %s forgot to set AF_INET. Fix it!\n",
522058bd4d2SJoe Perches 				     __func__, current->comm);
5231da177e4SLinus Torvalds 			err = -EAFNOSUPPORT;
5241da177e4SLinus Torvalds 			if (usin->sin_family)
5251da177e4SLinus Torvalds 				goto out;
5261da177e4SLinus Torvalds 		}
5271da177e4SLinus Torvalds 		daddr = usin->sin_addr.s_addr;
5281da177e4SLinus Torvalds 		/* ANK: I did not forget to get protocol from port field.
5291da177e4SLinus Torvalds 		 * I just do not know, who uses this weirdness.
5301da177e4SLinus Torvalds 		 * IP_HDRINCL is much more convenient.
5311da177e4SLinus Torvalds 		 */
5321da177e4SLinus Torvalds 	} else {
5331da177e4SLinus Torvalds 		err = -EDESTADDRREQ;
5341da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED)
5351da177e4SLinus Torvalds 			goto out;
536c720c7e8SEric Dumazet 		daddr = inet->inet_daddr;
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 
539c720c7e8SEric Dumazet 	ipc.addr = inet->inet_saddr;
5401da177e4SLinus Torvalds 	ipc.opt = NULL;
5412244d07bSOliver Hartkopp 	ipc.tx_flags = 0;
542aa661581SFrancesco Fusco 	ipc.ttl = 0;
543aa661581SFrancesco Fusco 	ipc.tos = -1;
5441da177e4SLinus Torvalds 	ipc.oif = sk->sk_bound_dev_if;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	if (msg->msg_controllen) {
547c8e6ad08SHannes Frederic Sowa 		err = ip_cmsg_send(sock_net(sk), msg, &ipc, false);
5481da177e4SLinus Torvalds 		if (err)
5491da177e4SLinus Torvalds 			goto out;
5501da177e4SLinus Torvalds 		if (ipc.opt)
5511da177e4SLinus Torvalds 			free = 1;
5521da177e4SLinus Torvalds 	}
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	saddr = ipc.addr;
5551da177e4SLinus Torvalds 	ipc.addr = daddr;
5561da177e4SLinus Torvalds 
557f6d8bd05SEric Dumazet 	if (!ipc.opt) {
558f6d8bd05SEric Dumazet 		struct ip_options_rcu *inet_opt;
559f6d8bd05SEric Dumazet 
560f6d8bd05SEric Dumazet 		rcu_read_lock();
561f6d8bd05SEric Dumazet 		inet_opt = rcu_dereference(inet->inet_opt);
562f6d8bd05SEric Dumazet 		if (inet_opt) {
563f6d8bd05SEric Dumazet 			memcpy(&opt_copy, inet_opt,
564f6d8bd05SEric Dumazet 			       sizeof(*inet_opt) + inet_opt->opt.optlen);
565f6d8bd05SEric Dumazet 			ipc.opt = &opt_copy.opt;
566f6d8bd05SEric Dumazet 		}
567f6d8bd05SEric Dumazet 		rcu_read_unlock();
568f6d8bd05SEric Dumazet 	}
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	if (ipc.opt) {
5711da177e4SLinus Torvalds 		err = -EINVAL;
5721da177e4SLinus Torvalds 		/* Linux does not mangle headers on raw sockets,
5731da177e4SLinus Torvalds 		 * so that IP options + IP_HDRINCL is non-sense.
5741da177e4SLinus Torvalds 		 */
5751da177e4SLinus Torvalds 		if (inet->hdrincl)
5761da177e4SLinus Torvalds 			goto done;
577f6d8bd05SEric Dumazet 		if (ipc.opt->opt.srr) {
5781da177e4SLinus Torvalds 			if (!daddr)
5791da177e4SLinus Torvalds 				goto done;
580f6d8bd05SEric Dumazet 			daddr = ipc.opt->opt.faddr;
5811da177e4SLinus Torvalds 		}
5821da177e4SLinus Torvalds 	}
583aa661581SFrancesco Fusco 	tos = get_rtconn_flags(&ipc, sk);
5841da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_DONTROUTE)
5851da177e4SLinus Torvalds 		tos |= RTO_ONLINK;
5861da177e4SLinus Torvalds 
587f97c1e0cSJoe Perches 	if (ipv4_is_multicast(daddr)) {
5881da177e4SLinus Torvalds 		if (!ipc.oif)
5891da177e4SLinus Torvalds 			ipc.oif = inet->mc_index;
5901da177e4SLinus Torvalds 		if (!saddr)
5911da177e4SLinus Torvalds 			saddr = inet->mc_addr;
59276e21053SErich E. Hoover 	} else if (!ipc.oif)
59376e21053SErich E. Hoover 		ipc.oif = inet->uc_index;
5941da177e4SLinus Torvalds 
595ef164ae3SDavid S. Miller 	flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
596ef164ae3SDavid S. Miller 			   RT_SCOPE_UNIVERSE,
597ef164ae3SDavid S. Miller 			   inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol,
5980e0d44abSSteffen Klassert 			   inet_sk_flowi_flags(sk) |
599c27c9322SChris Clark 			    (inet->hdrincl ? FLOWI_FLAG_KNOWN_NH : 0),
60047670b76SJulian Anastasov 			   daddr, saddr, 0, 0);
601ef164ae3SDavid S. Miller 
602a27b58feSHeiko Carstens 	if (!inet->hdrincl) {
603b61e9dccSAl Viro 		rfv.msg = msg;
604c008ba5bSHerbert Xu 		rfv.hlen = 0;
605c008ba5bSHerbert Xu 
606c008ba5bSHerbert Xu 		err = raw_probe_proto_opt(&rfv, &fl4);
607a27b58feSHeiko Carstens 		if (err)
608a27b58feSHeiko Carstens 			goto done;
609a27b58feSHeiko Carstens 	}
6101da177e4SLinus Torvalds 
6119d6ec938SDavid S. Miller 	security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
6129d6ec938SDavid S. Miller 	rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
613b23dd4feSDavid S. Miller 	if (IS_ERR(rt)) {
614b23dd4feSDavid S. Miller 		err = PTR_ERR(rt);
6154910ac6cSDavid S. Miller 		rt = NULL;
6161da177e4SLinus Torvalds 		goto done;
617b23dd4feSDavid S. Miller 	}
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	err = -EACCES;
6201da177e4SLinus Torvalds 	if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
6211da177e4SLinus Torvalds 		goto done;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_CONFIRM)
6241da177e4SLinus Torvalds 		goto do_confirm;
6251da177e4SLinus Torvalds back_from_confirm:
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	if (inet->hdrincl)
6287ae9abfdSAl Viro 		err = raw_send_hdrinc(sk, &fl4, msg, len,
6291789a640SEric Dumazet 				      &rt, msg->msg_flags);
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	 else {
63211878b40SWillem de Bruijn 		sock_tx_timestamp(sk, &ipc.tx_flags);
63311878b40SWillem de Bruijn 
6341da177e4SLinus Torvalds 		if (!ipc.addr)
63577968b78SDavid S. Miller 			ipc.addr = fl4.daddr;
6361da177e4SLinus Torvalds 		lock_sock(sk);
637c008ba5bSHerbert Xu 		err = ip_append_data(sk, &fl4, raw_getfrag,
638c008ba5bSHerbert Xu 				     &rfv, len, 0,
6392e77d89bSEric Dumazet 				     &ipc, &rt, msg->msg_flags);
6401da177e4SLinus Torvalds 		if (err)
6411da177e4SLinus Torvalds 			ip_flush_pending_frames(sk);
6426ce9e7b5SEric Dumazet 		else if (!(msg->msg_flags & MSG_MORE)) {
64377968b78SDavid S. Miller 			err = ip_push_pending_frames(sk, &fl4);
6446ce9e7b5SEric Dumazet 			if (err == -ENOBUFS && !inet->recverr)
6456ce9e7b5SEric Dumazet 				err = 0;
6466ce9e7b5SEric Dumazet 		}
6471da177e4SLinus Torvalds 		release_sock(sk);
6481da177e4SLinus Torvalds 	}
6491da177e4SLinus Torvalds done:
6501da177e4SLinus Torvalds 	if (free)
6511da177e4SLinus Torvalds 		kfree(ipc.opt);
6521da177e4SLinus Torvalds 	ip_rt_put(rt);
6531da177e4SLinus Torvalds 
6545418c692SJesper Juhl out:
6555418c692SJesper Juhl 	if (err < 0)
6565418c692SJesper Juhl 		return err;
6575418c692SJesper Juhl 	return len;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds do_confirm:
660d8d1f30bSChangli Gao 	dst_confirm(&rt->dst);
6611da177e4SLinus Torvalds 	if (!(msg->msg_flags & MSG_PROBE) || len)
6621da177e4SLinus Torvalds 		goto back_from_confirm;
6631da177e4SLinus Torvalds 	err = 0;
6641da177e4SLinus Torvalds 	goto done;
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds static void raw_close(struct sock *sk, long timeout)
6681da177e4SLinus Torvalds {
6691da177e4SLinus Torvalds 	/*
67025985edcSLucas De Marchi 	 * Raw sockets may have direct kernel references. Kill them.
6711da177e4SLinus Torvalds 	 */
6721da177e4SLinus Torvalds 	ip_ra_control(sk, 0, NULL);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	sk_common_release(sk);
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
6777d06b2e0SBrian Haley static void raw_destroy(struct sock *sk)
67822dd4850SDenis V. Lunev {
67922dd4850SDenis V. Lunev 	lock_sock(sk);
68022dd4850SDenis V. Lunev 	ip_flush_pending_frames(sk);
68122dd4850SDenis V. Lunev 	release_sock(sk);
68222dd4850SDenis V. Lunev }
68322dd4850SDenis V. Lunev 
6841da177e4SLinus Torvalds /* This gets rid of all the nasties in af_inet. -DaveM */
6851da177e4SLinus Torvalds static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
6861da177e4SLinus Torvalds {
6871da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
6881da177e4SLinus Torvalds 	struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
6891da177e4SLinus Torvalds 	int ret = -EINVAL;
6901da177e4SLinus Torvalds 	int chk_addr_ret;
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
6931da177e4SLinus Torvalds 		goto out;
6943b1e0a65SYOSHIFUJI Hideaki 	chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
6951da177e4SLinus Torvalds 	ret = -EADDRNOTAVAIL;
6961da177e4SLinus Torvalds 	if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
6971da177e4SLinus Torvalds 	    chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
6981da177e4SLinus Torvalds 		goto out;
699c720c7e8SEric Dumazet 	inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
7001da177e4SLinus Torvalds 	if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
701c720c7e8SEric Dumazet 		inet->inet_saddr = 0;  /* Use device */
7021da177e4SLinus Torvalds 	sk_dst_reset(sk);
7031da177e4SLinus Torvalds 	ret = 0;
7041da177e4SLinus Torvalds out:	return ret;
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds /*
7081da177e4SLinus Torvalds  *	This should be easy, if there is something there
7091da177e4SLinus Torvalds  *	we return it, otherwise we block.
7101da177e4SLinus Torvalds  */
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
7131da177e4SLinus Torvalds 		       size_t len, int noblock, int flags, int *addr_len)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
7161da177e4SLinus Torvalds 	size_t copied = 0;
7171da177e4SLinus Torvalds 	int err = -EOPNOTSUPP;
718342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
7191da177e4SLinus Torvalds 	struct sk_buff *skb;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	if (flags & MSG_OOB)
7221da177e4SLinus Torvalds 		goto out;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	if (flags & MSG_ERRQUEUE) {
72585fbaa75SHannes Frederic Sowa 		err = ip_recv_error(sk, msg, len, addr_len);
7261da177e4SLinus Torvalds 		goto out;
7271da177e4SLinus Torvalds 	}
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	skb = skb_recv_datagram(sk, flags, noblock, &err);
7301da177e4SLinus Torvalds 	if (!skb)
7311da177e4SLinus Torvalds 		goto out;
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	copied = skb->len;
7341da177e4SLinus Torvalds 	if (len < copied) {
7351da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
7361da177e4SLinus Torvalds 		copied = len;
7371da177e4SLinus Torvalds 	}
7381da177e4SLinus Torvalds 
73951f3d02bSDavid S. Miller 	err = skb_copy_datagram_msg(skb, 0, msg, copied);
7401da177e4SLinus Torvalds 	if (err)
7411da177e4SLinus Torvalds 		goto done;
7421da177e4SLinus Torvalds 
7433b885787SNeil Horman 	sock_recv_ts_and_drops(msg, sk, skb);
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	/* Copy the address. */
7461da177e4SLinus Torvalds 	if (sin) {
7471da177e4SLinus Torvalds 		sin->sin_family = AF_INET;
748eddc9ec5SArnaldo Carvalho de Melo 		sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
749f59fc7f3STetsuo Handa 		sin->sin_port = 0;
7501da177e4SLinus Torvalds 		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
751bceaa902SHannes Frederic Sowa 		*addr_len = sizeof(*sin);
7521da177e4SLinus Torvalds 	}
7531da177e4SLinus Torvalds 	if (inet->cmsg_flags)
7541da177e4SLinus Torvalds 		ip_cmsg_recv(msg, skb);
7551da177e4SLinus Torvalds 	if (flags & MSG_TRUNC)
7561da177e4SLinus Torvalds 		copied = skb->len;
7571da177e4SLinus Torvalds done:
7581da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
7595418c692SJesper Juhl out:
7605418c692SJesper Juhl 	if (err)
7615418c692SJesper Juhl 		return err;
7625418c692SJesper Juhl 	return copied;
7631da177e4SLinus Torvalds }
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds static int raw_init(struct sock *sk)
7661da177e4SLinus Torvalds {
7671da177e4SLinus Torvalds 	struct raw_sock *rp = raw_sk(sk);
7681da177e4SLinus Torvalds 
769c720c7e8SEric Dumazet 	if (inet_sk(sk)->inet_num == IPPROTO_ICMP)
7701da177e4SLinus Torvalds 		memset(&rp->filter, 0, sizeof(rp->filter));
7711da177e4SLinus Torvalds 	return 0;
7721da177e4SLinus Torvalds }
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds static int raw_seticmpfilter(struct sock *sk, char __user *optval, int optlen)
7751da177e4SLinus Torvalds {
7761da177e4SLinus Torvalds 	if (optlen > sizeof(struct icmp_filter))
7771da177e4SLinus Torvalds 		optlen = sizeof(struct icmp_filter);
7781da177e4SLinus Torvalds 	if (copy_from_user(&raw_sk(sk)->filter, optval, optlen))
7791da177e4SLinus Torvalds 		return -EFAULT;
7801da177e4SLinus Torvalds 	return 0;
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
7841da177e4SLinus Torvalds {
7851da177e4SLinus Torvalds 	int len, ret = -EFAULT;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	if (get_user(len, optlen))
7881da177e4SLinus Torvalds 		goto out;
7891da177e4SLinus Torvalds 	ret = -EINVAL;
7901da177e4SLinus Torvalds 	if (len < 0)
7911da177e4SLinus Torvalds 		goto out;
7921da177e4SLinus Torvalds 	if (len > sizeof(struct icmp_filter))
7931da177e4SLinus Torvalds 		len = sizeof(struct icmp_filter);
7941da177e4SLinus Torvalds 	ret = -EFAULT;
7951da177e4SLinus Torvalds 	if (put_user(len, optlen) ||
7961da177e4SLinus Torvalds 	    copy_to_user(optval, &raw_sk(sk)->filter, len))
7971da177e4SLinus Torvalds 		goto out;
7981da177e4SLinus Torvalds 	ret = 0;
7991da177e4SLinus Torvalds out:	return ret;
8001da177e4SLinus Torvalds }
8011da177e4SLinus Torvalds 
8023fdadf7dSDmitry Mishin static int do_raw_setsockopt(struct sock *sk, int level, int optname,
803b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
8041da177e4SLinus Torvalds {
8051da177e4SLinus Torvalds 	if (optname == ICMP_FILTER) {
806c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
8071da177e4SLinus Torvalds 			return -EOPNOTSUPP;
8081da177e4SLinus Torvalds 		else
8091da177e4SLinus Torvalds 			return raw_seticmpfilter(sk, optval, optlen);
8101da177e4SLinus Torvalds 	}
8111da177e4SLinus Torvalds 	return -ENOPROTOOPT;
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
8143fdadf7dSDmitry Mishin static int raw_setsockopt(struct sock *sk, int level, int optname,
815b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
8161da177e4SLinus Torvalds {
8171da177e4SLinus Torvalds 	if (level != SOL_RAW)
8183fdadf7dSDmitry Mishin 		return ip_setsockopt(sk, level, optname, optval, optlen);
8193fdadf7dSDmitry Mishin 	return do_raw_setsockopt(sk, level, optname, optval, optlen);
8203fdadf7dSDmitry Mishin }
8211da177e4SLinus Torvalds 
8223fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
8233fdadf7dSDmitry Mishin static int compat_raw_setsockopt(struct sock *sk, int level, int optname,
824b7058842SDavid S. Miller 				 char __user *optval, unsigned int optlen)
8253fdadf7dSDmitry Mishin {
8263fdadf7dSDmitry Mishin 	if (level != SOL_RAW)
827543d9cfeSArnaldo Carvalho de Melo 		return compat_ip_setsockopt(sk, level, optname, optval, optlen);
8283fdadf7dSDmitry Mishin 	return do_raw_setsockopt(sk, level, optname, optval, optlen);
8293fdadf7dSDmitry Mishin }
8303fdadf7dSDmitry Mishin #endif
8313fdadf7dSDmitry Mishin 
8323fdadf7dSDmitry Mishin static int do_raw_getsockopt(struct sock *sk, int level, int optname,
8333fdadf7dSDmitry Mishin 			  char __user *optval, int __user *optlen)
8343fdadf7dSDmitry Mishin {
8351da177e4SLinus Torvalds 	if (optname == ICMP_FILTER) {
836c720c7e8SEric Dumazet 		if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
8371da177e4SLinus Torvalds 			return -EOPNOTSUPP;
8381da177e4SLinus Torvalds 		else
8391da177e4SLinus Torvalds 			return raw_geticmpfilter(sk, optval, optlen);
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 	return -ENOPROTOOPT;
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
8443fdadf7dSDmitry Mishin static int raw_getsockopt(struct sock *sk, int level, int optname,
8453fdadf7dSDmitry Mishin 			  char __user *optval, int __user *optlen)
8463fdadf7dSDmitry Mishin {
8473fdadf7dSDmitry Mishin 	if (level != SOL_RAW)
8483fdadf7dSDmitry Mishin 		return ip_getsockopt(sk, level, optname, optval, optlen);
8493fdadf7dSDmitry Mishin 	return do_raw_getsockopt(sk, level, optname, optval, optlen);
8503fdadf7dSDmitry Mishin }
8513fdadf7dSDmitry Mishin 
8523fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
8533fdadf7dSDmitry Mishin static int compat_raw_getsockopt(struct sock *sk, int level, int optname,
8543fdadf7dSDmitry Mishin 				 char __user *optval, int __user *optlen)
8553fdadf7dSDmitry Mishin {
8563fdadf7dSDmitry Mishin 	if (level != SOL_RAW)
857543d9cfeSArnaldo Carvalho de Melo 		return compat_ip_getsockopt(sk, level, optname, optval, optlen);
8583fdadf7dSDmitry Mishin 	return do_raw_getsockopt(sk, level, optname, optval, optlen);
8593fdadf7dSDmitry Mishin }
8603fdadf7dSDmitry Mishin #endif
8613fdadf7dSDmitry Mishin 
8621da177e4SLinus Torvalds static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
8631da177e4SLinus Torvalds {
8641da177e4SLinus Torvalds 	switch (cmd) {
8651da177e4SLinus Torvalds 	case SIOCOUTQ: {
86631e6d363SEric Dumazet 		int amount = sk_wmem_alloc_get(sk);
86731e6d363SEric Dumazet 
8681da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
8691da177e4SLinus Torvalds 	}
8701da177e4SLinus Torvalds 	case SIOCINQ: {
8711da177e4SLinus Torvalds 		struct sk_buff *skb;
8721da177e4SLinus Torvalds 		int amount = 0;
8731da177e4SLinus Torvalds 
874e0f9f858SHerbert Xu 		spin_lock_bh(&sk->sk_receive_queue.lock);
8751da177e4SLinus Torvalds 		skb = skb_peek(&sk->sk_receive_queue);
8761da177e4SLinus Torvalds 		if (skb != NULL)
8771da177e4SLinus Torvalds 			amount = skb->len;
878e0f9f858SHerbert Xu 		spin_unlock_bh(&sk->sk_receive_queue.lock);
8791da177e4SLinus Torvalds 		return put_user(amount, (int __user *)arg);
8801da177e4SLinus Torvalds 	}
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	default:
8831da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
8841da177e4SLinus Torvalds 		return ipmr_ioctl(sk, cmd, (void __user *)arg);
8851da177e4SLinus Torvalds #else
8861da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
8871da177e4SLinus Torvalds #endif
8881da177e4SLinus Torvalds 	}
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
891709b46e8SEric W. Biederman #ifdef CONFIG_COMPAT
892709b46e8SEric W. Biederman static int compat_raw_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
893709b46e8SEric W. Biederman {
894709b46e8SEric W. Biederman 	switch (cmd) {
895709b46e8SEric W. Biederman 	case SIOCOUTQ:
896709b46e8SEric W. Biederman 	case SIOCINQ:
897709b46e8SEric W. Biederman 		return -ENOIOCTLCMD;
898709b46e8SEric W. Biederman 	default:
899709b46e8SEric W. Biederman #ifdef CONFIG_IP_MROUTE
900709b46e8SEric W. Biederman 		return ipmr_compat_ioctl(sk, cmd, compat_ptr(arg));
901709b46e8SEric W. Biederman #else
902709b46e8SEric W. Biederman 		return -ENOIOCTLCMD;
903709b46e8SEric W. Biederman #endif
904709b46e8SEric W. Biederman 	}
905709b46e8SEric W. Biederman }
906709b46e8SEric W. Biederman #endif
907709b46e8SEric W. Biederman 
9081da177e4SLinus Torvalds struct proto raw_prot = {
9091da177e4SLinus Torvalds 	.name		   = "RAW",
9101da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
9111da177e4SLinus Torvalds 	.close		   = raw_close,
91222dd4850SDenis V. Lunev 	.destroy	   = raw_destroy,
9131da177e4SLinus Torvalds 	.connect	   = ip4_datagram_connect,
9141da177e4SLinus Torvalds 	.disconnect	   = udp_disconnect,
9151da177e4SLinus Torvalds 	.ioctl		   = raw_ioctl,
9161da177e4SLinus Torvalds 	.init		   = raw_init,
9171da177e4SLinus Torvalds 	.setsockopt	   = raw_setsockopt,
9181da177e4SLinus Torvalds 	.getsockopt	   = raw_getsockopt,
9191da177e4SLinus Torvalds 	.sendmsg	   = raw_sendmsg,
9201da177e4SLinus Torvalds 	.recvmsg	   = raw_recvmsg,
9211da177e4SLinus Torvalds 	.bind		   = raw_bind,
9221da177e4SLinus Torvalds 	.backlog_rcv	   = raw_rcv_skb,
9238141ed9fSSteffen Klassert 	.release_cb	   = ip4_datagram_release_cb,
924fc8717baSPavel Emelyanov 	.hash		   = raw_hash_sk,
925fc8717baSPavel Emelyanov 	.unhash		   = raw_unhash_sk,
9261da177e4SLinus Torvalds 	.obj_size	   = sizeof(struct raw_sock),
927fc8717baSPavel Emelyanov 	.h.raw_hash	   = &raw_v4_hashinfo,
928543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
929543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_raw_setsockopt,
930543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_raw_getsockopt,
931709b46e8SEric W. Biederman 	.compat_ioctl	   = compat_raw_ioctl,
932543d9cfeSArnaldo Carvalho de Melo #endif
9331da177e4SLinus Torvalds };
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
9361da177e4SLinus Torvalds static struct sock *raw_get_first(struct seq_file *seq)
9371da177e4SLinus Torvalds {
9381da177e4SLinus Torvalds 	struct sock *sk;
9391da177e4SLinus Torvalds 	struct raw_iter_state *state = raw_seq_private(seq);
9401da177e4SLinus Torvalds 
941b673e4dfSPavel Emelyanov 	for (state->bucket = 0; state->bucket < RAW_HTABLE_SIZE;
942b673e4dfSPavel Emelyanov 			++state->bucket) {
943b67bfe0dSSasha Levin 		sk_for_each(sk, &state->h->ht[state->bucket])
9441218854aSYOSHIFUJI Hideaki 			if (sock_net(sk) == seq_file_net(seq))
9451da177e4SLinus Torvalds 				goto found;
9461da177e4SLinus Torvalds 	}
9471da177e4SLinus Torvalds 	sk = NULL;
9481da177e4SLinus Torvalds found:
9491da177e4SLinus Torvalds 	return sk;
9501da177e4SLinus Torvalds }
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
9531da177e4SLinus Torvalds {
9541da177e4SLinus Torvalds 	struct raw_iter_state *state = raw_seq_private(seq);
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	do {
9571da177e4SLinus Torvalds 		sk = sk_next(sk);
9581da177e4SLinus Torvalds try_again:
9591da177e4SLinus Torvalds 		;
9601218854aSYOSHIFUJI Hideaki 	} while (sk && sock_net(sk) != seq_file_net(seq));
9611da177e4SLinus Torvalds 
962b673e4dfSPavel Emelyanov 	if (!sk && ++state->bucket < RAW_HTABLE_SIZE) {
96342a73808SPavel Emelyanov 		sk = sk_head(&state->h->ht[state->bucket]);
9641da177e4SLinus Torvalds 		goto try_again;
9651da177e4SLinus Torvalds 	}
9661da177e4SLinus Torvalds 	return sk;
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
9701da177e4SLinus Torvalds {
9711da177e4SLinus Torvalds 	struct sock *sk = raw_get_first(seq);
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	if (sk)
9741da177e4SLinus Torvalds 		while (pos && (sk = raw_get_next(seq, sk)) != NULL)
9751da177e4SLinus Torvalds 			--pos;
9761da177e4SLinus Torvalds 	return pos ? NULL : sk;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
97942a73808SPavel Emelyanov void *raw_seq_start(struct seq_file *seq, loff_t *pos)
9801da177e4SLinus Torvalds {
98142a73808SPavel Emelyanov 	struct raw_iter_state *state = raw_seq_private(seq);
98242a73808SPavel Emelyanov 
98342a73808SPavel Emelyanov 	read_lock(&state->h->lock);
9841da177e4SLinus Torvalds 	return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
9851da177e4SLinus Torvalds }
98642a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_start);
9871da177e4SLinus Torvalds 
98842a73808SPavel Emelyanov void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
9891da177e4SLinus Torvalds {
9901da177e4SLinus Torvalds 	struct sock *sk;
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
9931da177e4SLinus Torvalds 		sk = raw_get_first(seq);
9941da177e4SLinus Torvalds 	else
9951da177e4SLinus Torvalds 		sk = raw_get_next(seq, v);
9961da177e4SLinus Torvalds 	++*pos;
9971da177e4SLinus Torvalds 	return sk;
9981da177e4SLinus Torvalds }
99942a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_next);
10001da177e4SLinus Torvalds 
100142a73808SPavel Emelyanov void raw_seq_stop(struct seq_file *seq, void *v)
10021da177e4SLinus Torvalds {
100342a73808SPavel Emelyanov 	struct raw_iter_state *state = raw_seq_private(seq);
100442a73808SPavel Emelyanov 
100542a73808SPavel Emelyanov 	read_unlock(&state->h->lock);
10061da177e4SLinus Torvalds }
100742a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_stop);
10081da177e4SLinus Torvalds 
10098cd850efSDenis V. Lunev static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
10101da177e4SLinus Torvalds {
10111da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sp);
1012c720c7e8SEric Dumazet 	__be32 dest = inet->inet_daddr,
1013c720c7e8SEric Dumazet 	       src = inet->inet_rcv_saddr;
10141da177e4SLinus Torvalds 	__u16 destp = 0,
1015c720c7e8SEric Dumazet 	      srcp  = inet->inet_num;
10161da177e4SLinus Torvalds 
10178cd850efSDenis V. Lunev 	seq_printf(seq, "%4d: %08X:%04X %08X:%04X"
1018d14c5ab6SFrancesco Fusco 		" %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
10191da177e4SLinus Torvalds 		i, src, srcp, dest, destp, sp->sk_state,
102031e6d363SEric Dumazet 		sk_wmem_alloc_get(sp),
102131e6d363SEric Dumazet 		sk_rmem_alloc_get(sp),
1022a7cb5a49SEric W. Biederman 		0, 0L, 0,
1023a7cb5a49SEric W. Biederman 		from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
1024a7cb5a49SEric W. Biederman 		0, sock_i_ino(sp),
102533c732c3SWang Chen 		atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
10261da177e4SLinus Torvalds }
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds static int raw_seq_show(struct seq_file *seq, void *v)
10291da177e4SLinus Torvalds {
10301da177e4SLinus Torvalds 	if (v == SEQ_START_TOKEN)
10318cd850efSDenis V. Lunev 		seq_printf(seq, "  sl  local_address rem_address   st tx_queue "
10321da177e4SLinus Torvalds 				"rx_queue tr tm->when retrnsmt   uid  timeout "
1033cb61cb9bSEric Dumazet 				"inode ref pointer drops\n");
10348cd850efSDenis V. Lunev 	else
10358cd850efSDenis V. Lunev 		raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
10361da177e4SLinus Torvalds 	return 0;
10371da177e4SLinus Torvalds }
10381da177e4SLinus Torvalds 
1039f690808eSStephen Hemminger static const struct seq_operations raw_seq_ops = {
10401da177e4SLinus Torvalds 	.start = raw_seq_start,
10411da177e4SLinus Torvalds 	.next  = raw_seq_next,
10421da177e4SLinus Torvalds 	.stop  = raw_seq_stop,
10431da177e4SLinus Torvalds 	.show  = raw_seq_show,
10441da177e4SLinus Torvalds };
10451da177e4SLinus Torvalds 
10463046d767SDenis V. Lunev int raw_seq_open(struct inode *ino, struct file *file,
10473046d767SDenis V. Lunev 		 struct raw_hashinfo *h, const struct seq_operations *ops)
10481da177e4SLinus Torvalds {
1049f51d599fSPavel Emelyanov 	int err;
105042a73808SPavel Emelyanov 	struct raw_iter_state *i;
105142a73808SPavel Emelyanov 
10523046d767SDenis V. Lunev 	err = seq_open_net(ino, file, ops, sizeof(struct raw_iter_state));
1053f51d599fSPavel Emelyanov 	if (err < 0)
1054f51d599fSPavel Emelyanov 		return err;
105542a73808SPavel Emelyanov 
1056f51d599fSPavel Emelyanov 	i = raw_seq_private((struct seq_file *)file->private_data);
105742a73808SPavel Emelyanov 	i->h = h;
105842a73808SPavel Emelyanov 	return 0;
105942a73808SPavel Emelyanov }
106042a73808SPavel Emelyanov EXPORT_SYMBOL_GPL(raw_seq_open);
106142a73808SPavel Emelyanov 
106242a73808SPavel Emelyanov static int raw_v4_seq_open(struct inode *inode, struct file *file)
106342a73808SPavel Emelyanov {
10643046d767SDenis V. Lunev 	return raw_seq_open(inode, file, &raw_v4_hashinfo, &raw_seq_ops);
10651da177e4SLinus Torvalds }
10661da177e4SLinus Torvalds 
10679a32144eSArjan van de Ven static const struct file_operations raw_seq_fops = {
10681da177e4SLinus Torvalds 	.owner	 = THIS_MODULE,
106942a73808SPavel Emelyanov 	.open	 = raw_v4_seq_open,
10701da177e4SLinus Torvalds 	.read	 = seq_read,
10711da177e4SLinus Torvalds 	.llseek	 = seq_lseek,
1072f51d599fSPavel Emelyanov 	.release = seq_release_net,
10731da177e4SLinus Torvalds };
10741da177e4SLinus Torvalds 
1075a308da16SPavel Emelyanov static __net_init int raw_init_net(struct net *net)
1076a308da16SPavel Emelyanov {
1077d4beaa66SGao feng 	if (!proc_create("raw", S_IRUGO, net->proc_net, &raw_seq_fops))
1078a308da16SPavel Emelyanov 		return -ENOMEM;
1079a308da16SPavel Emelyanov 
1080a308da16SPavel Emelyanov 	return 0;
1081a308da16SPavel Emelyanov }
1082a308da16SPavel Emelyanov 
1083a308da16SPavel Emelyanov static __net_exit void raw_exit_net(struct net *net)
1084a308da16SPavel Emelyanov {
1085ece31ffdSGao feng 	remove_proc_entry("raw", net->proc_net);
1086a308da16SPavel Emelyanov }
1087a308da16SPavel Emelyanov 
1088a308da16SPavel Emelyanov static __net_initdata struct pernet_operations raw_net_ops = {
1089a308da16SPavel Emelyanov 	.init = raw_init_net,
1090a308da16SPavel Emelyanov 	.exit = raw_exit_net,
1091a308da16SPavel Emelyanov };
1092a308da16SPavel Emelyanov 
10931da177e4SLinus Torvalds int __init raw_proc_init(void)
10941da177e4SLinus Torvalds {
1095a308da16SPavel Emelyanov 	return register_pernet_subsys(&raw_net_ops);
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds void __init raw_proc_exit(void)
10991da177e4SLinus Torvalds {
1100a308da16SPavel Emelyanov 	unregister_pernet_subsys(&raw_net_ops);
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
1103