ia64/xen-unstable

changeset 2494:6279ded05b71

bitkeeper revision 1.1159.1.161 (4149eda74Qj-LHfB3E2bfFqo4OxI5g)

The icmp_filter for raw ip sockets can't handle the skbs we build. It wants
to look at the ICMP header and expects it to be located at skb->data past the
IP header. Our skbs have all the data past the IP header in fragment buffers.

Pull the icmphdr into skb->data before looking at the icmp header.
author cl349@freefall.cl.cam.ac.uk
date Thu Sep 16 19:46:47 2004 +0000 (2004-09-16)
parents 5a20652b7d87
children 879d7c8f0d19 b239a7e0631e
files .rootkeys linux-2.6.8.1-xen-sparse/net/ipv4/raw.c
line diff
     1.1 --- a/.rootkeys	Thu Sep 16 15:20:58 2004 +0000
     1.2 +++ b/.rootkeys	Thu Sep 16 19:46:47 2004 +0000
     1.3 @@ -258,6 +258,7 @@ 4124f66f4NaKNa0xPiGGykn9QaZk3w linux-2.6
     1.4  40f56a0ddHCSs3501MY4hRf22tctOw linux-2.6.8.1-xen-sparse/mkbuildtree
     1.5  412f46c0LJuKAgSPGoC0Z1DEkLfuLA linux-2.6.8.1-xen-sparse/mm/memory.c
     1.6  410a94a4KT6I6X0LVc7djB39tRDp4g linux-2.6.8.1-xen-sparse/mm/page_alloc.c
     1.7 +4149ec79wMpIHdvbntxqVGLRZZjPxw linux-2.6.8.1-xen-sparse/net/ipv4/raw.c
     1.8  413cb1e4zst25MDYjg63Y-NGC5_pLg netbsd-2.0-xen-sparse/Makefile
     1.9  413cb1e5c_Mkxf_X0zimEhTKI_l4DA netbsd-2.0-xen-sparse/mkbuildtree
    1.10  413cb1e5kY_Zil7-b0kI6hvCIxBEYg netbsd-2.0-xen-sparse/nbconfig-xen
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/linux-2.6.8.1-xen-sparse/net/ipv4/raw.c	Thu Sep 16 19:46:47 2004 +0000
     2.3 @@ -0,0 +1,837 @@
     2.4 +/*
     2.5 + * INET		An implementation of the TCP/IP protocol suite for the LINUX
     2.6 + *		operating system.  INET is implemented using the  BSD Socket
     2.7 + *		interface as the means of communication with the user level.
     2.8 + *
     2.9 + *		RAW - implementation of IP "raw" sockets.
    2.10 + *
    2.11 + * Version:	$Id: raw.c,v 1.64 2002/02/01 22:01:04 davem Exp $
    2.12 + *
    2.13 + * Authors:	Ross Biro, <bir7@leland.Stanford.Edu>
    2.14 + *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
    2.15 + *
    2.16 + * Fixes:
    2.17 + *		Alan Cox	:	verify_area() fixed up
    2.18 + *		Alan Cox	:	ICMP error handling
    2.19 + *		Alan Cox	:	EMSGSIZE if you send too big a packet
    2.20 + *		Alan Cox	: 	Now uses generic datagrams and shared
    2.21 + *					skbuff library. No more peek crashes,
    2.22 + *					no more backlogs
    2.23 + *		Alan Cox	:	Checks sk->broadcast.
    2.24 + *		Alan Cox	:	Uses skb_free_datagram/skb_copy_datagram
    2.25 + *		Alan Cox	:	Raw passes ip options too
    2.26 + *		Alan Cox	:	Setsocketopt added
    2.27 + *		Alan Cox	:	Fixed error return for broadcasts
    2.28 + *		Alan Cox	:	Removed wake_up calls
    2.29 + *		Alan Cox	:	Use ttl/tos
    2.30 + *		Alan Cox	:	Cleaned up old debugging
    2.31 + *		Alan Cox	:	Use new kernel side addresses
    2.32 + *	Arnt Gulbrandsen	:	Fixed MSG_DONTROUTE in raw sockets.
    2.33 + *		Alan Cox	:	BSD style RAW socket demultiplexing.
    2.34 + *		Alan Cox	:	Beginnings of mrouted support.
    2.35 + *		Alan Cox	:	Added IP_HDRINCL option.
    2.36 + *		Alan Cox	:	Skip broadcast check if BSDism set.
    2.37 + *		David S. Miller	:	New socket lookup architecture.
    2.38 + *
    2.39 + *		This program is free software; you can redistribute it and/or
    2.40 + *		modify it under the terms of the GNU General Public License
    2.41 + *		as published by the Free Software Foundation; either version
    2.42 + *		2 of the License, or (at your option) any later version.
    2.43 + */
    2.44 + 
    2.45 +#include <linux/config.h> 
    2.46 +#include <asm/atomic.h>
    2.47 +#include <asm/byteorder.h>
    2.48 +#include <asm/current.h>
    2.49 +#include <asm/uaccess.h>
    2.50 +#include <asm/ioctls.h>
    2.51 +#include <linux/types.h>
    2.52 +#include <linux/stddef.h>
    2.53 +#include <linux/slab.h>
    2.54 +#include <linux/errno.h>
    2.55 +#include <linux/aio.h>
    2.56 +#include <linux/kernel.h>
    2.57 +#include <linux/spinlock.h>
    2.58 +#include <linux/sockios.h>
    2.59 +#include <linux/socket.h>
    2.60 +#include <linux/in.h>
    2.61 +#include <linux/mroute.h>
    2.62 +#include <linux/netdevice.h>
    2.63 +#include <linux/in_route.h>
    2.64 +#include <linux/route.h>
    2.65 +#include <linux/tcp.h>
    2.66 +#include <linux/skbuff.h>
    2.67 +#include <net/dst.h>
    2.68 +#include <net/sock.h>
    2.69 +#include <linux/gfp.h>
    2.70 +#include <linux/ip.h>
    2.71 +#include <linux/net.h>
    2.72 +#include <net/ip.h>
    2.73 +#include <net/icmp.h>
    2.74 +#include <net/udp.h>
    2.75 +#include <net/raw.h>
    2.76 +#include <net/snmp.h>
    2.77 +#include <net/inet_common.h>
    2.78 +#include <net/checksum.h>
    2.79 +#include <net/xfrm.h>
    2.80 +#include <linux/rtnetlink.h>
    2.81 +#include <linux/proc_fs.h>
    2.82 +#include <linux/seq_file.h>
    2.83 +#include <linux/netfilter.h>
    2.84 +#include <linux/netfilter_ipv4.h>
    2.85 +
    2.86 +struct hlist_head raw_v4_htable[RAWV4_HTABLE_SIZE];
    2.87 +rwlock_t raw_v4_lock = RW_LOCK_UNLOCKED;
    2.88 +
    2.89 +static void raw_v4_hash(struct sock *sk)
    2.90 +{
    2.91 +	struct hlist_head *head = &raw_v4_htable[inet_sk(sk)->num &
    2.92 +						 (RAWV4_HTABLE_SIZE - 1)];
    2.93 +
    2.94 +	write_lock_bh(&raw_v4_lock);
    2.95 +	sk_add_node(sk, head);
    2.96 +	sock_prot_inc_use(sk->sk_prot);
    2.97 +	write_unlock_bh(&raw_v4_lock);
    2.98 +}
    2.99 +
   2.100 +static void raw_v4_unhash(struct sock *sk)
   2.101 +{
   2.102 + 	write_lock_bh(&raw_v4_lock);
   2.103 +	if (sk_del_node_init(sk))
   2.104 +		sock_prot_dec_use(sk->sk_prot);
   2.105 +	write_unlock_bh(&raw_v4_lock);
   2.106 +}
   2.107 +
   2.108 +struct sock *__raw_v4_lookup(struct sock *sk, unsigned short num,
   2.109 +			     unsigned long raddr, unsigned long laddr,
   2.110 +			     int dif)
   2.111 +{
   2.112 +	struct hlist_node *node;
   2.113 +
   2.114 +	sk_for_each_from(sk, node) {
   2.115 +		struct inet_opt *inet = inet_sk(sk);
   2.116 +
   2.117 +		if (inet->num == num 					&&
   2.118 +		    !(inet->daddr && inet->daddr != raddr) 		&&
   2.119 +		    !(inet->rcv_saddr && inet->rcv_saddr != laddr)	&&
   2.120 +		    !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
   2.121 +			goto found; /* gotcha */
   2.122 +	}
   2.123 +	sk = NULL;
   2.124 +found:
   2.125 +	return sk;
   2.126 +}
   2.127 +
   2.128 +/*
   2.129 + *	0 - deliver
   2.130 + *	1 - block
   2.131 + */
   2.132 +static __inline__ int icmp_filter(struct sock *sk, struct sk_buff *skb)
   2.133 +{
   2.134 +	int type;
   2.135 +
   2.136 +	if (!pskb_may_pull(skb, sizeof(struct icmphdr)))
   2.137 +		return 1;
   2.138 +
   2.139 +	type = skb->h.icmph->type;
   2.140 +	if (type < 32) {
   2.141 +		__u32 data = raw4_sk(sk)->filter.data;
   2.142 +
   2.143 +		return ((1 << type) & data) != 0;
   2.144 +	}
   2.145 +
   2.146 +	/* Do not block unknown ICMP types */
   2.147 +	return 0;
   2.148 +}
   2.149 +
   2.150 +/* IP input processing comes here for RAW socket delivery.
   2.151 + * Caller owns SKB, so we must make clones.
   2.152 + *
   2.153 + * RFC 1122: SHOULD pass TOS value up to the transport layer.
   2.154 + * -> It does. And not only TOS, but all IP header.
   2.155 + */
   2.156 +void raw_v4_input(struct sk_buff *skb, struct iphdr *iph, int hash)
   2.157 +{
   2.158 +	struct sock *sk;
   2.159 +	struct hlist_head *head;
   2.160 +
   2.161 +	read_lock(&raw_v4_lock);
   2.162 +	head = &raw_v4_htable[hash];
   2.163 +	if (hlist_empty(head))
   2.164 +		goto out;
   2.165 +	sk = __raw_v4_lookup(__sk_head(head), iph->protocol,
   2.166 +			     iph->saddr, iph->daddr,
   2.167 +			     skb->dev->ifindex);
   2.168 +
   2.169 +	while (sk) {
   2.170 +		if (iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) {
   2.171 +			struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
   2.172 +
   2.173 +			/* Not releasing hash table! */
   2.174 +			if (clone)
   2.175 +				raw_rcv(sk, clone);
   2.176 +		}
   2.177 +		sk = __raw_v4_lookup(sk_next(sk), iph->protocol,
   2.178 +				     iph->saddr, iph->daddr,
   2.179 +				     skb->dev->ifindex);
   2.180 +	}
   2.181 +out:
   2.182 +	read_unlock(&raw_v4_lock);
   2.183 +}
   2.184 +
   2.185 +void raw_err (struct sock *sk, struct sk_buff *skb, u32 info)
   2.186 +{
   2.187 +	struct inet_opt *inet = inet_sk(sk);
   2.188 +	int type = skb->h.icmph->type;
   2.189 +	int code = skb->h.icmph->code;
   2.190 +	int err = 0;
   2.191 +	int harderr = 0;
   2.192 +
   2.193 +	/* Report error on raw socket, if:
   2.194 +	   1. User requested ip_recverr.
   2.195 +	   2. Socket is connected (otherwise the error indication
   2.196 +	      is useless without ip_recverr and error is hard.
   2.197 +	 */
   2.198 +	if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED)
   2.199 +		return;
   2.200 +
   2.201 +	switch (type) {
   2.202 +	default:
   2.203 +	case ICMP_TIME_EXCEEDED:
   2.204 +		err = EHOSTUNREACH;
   2.205 +		break;
   2.206 +	case ICMP_SOURCE_QUENCH:
   2.207 +		return;
   2.208 +	case ICMP_PARAMETERPROB:
   2.209 +		err = EPROTO;
   2.210 +		harderr = 1;
   2.211 +		break;
   2.212 +	case ICMP_DEST_UNREACH:
   2.213 +		err = EHOSTUNREACH;
   2.214 +		if (code > NR_ICMP_UNREACH)
   2.215 +			break;
   2.216 +		err = icmp_err_convert[code].errno;
   2.217 +		harderr = icmp_err_convert[code].fatal;
   2.218 +		if (code == ICMP_FRAG_NEEDED) {
   2.219 +			harderr = inet->pmtudisc != IP_PMTUDISC_DONT;
   2.220 +			err = EMSGSIZE;
   2.221 +		}
   2.222 +	}
   2.223 +
   2.224 +	if (inet->recverr) {
   2.225 +		struct iphdr *iph = (struct iphdr*)skb->data;
   2.226 +		u8 *payload = skb->data + (iph->ihl << 2);
   2.227 +
   2.228 +		if (inet->hdrincl)
   2.229 +			payload = skb->data;
   2.230 +		ip_icmp_error(sk, skb, err, 0, info, payload);
   2.231 +	}
   2.232 +
   2.233 +	if (inet->recverr || harderr) {
   2.234 +		sk->sk_err = err;
   2.235 +		sk->sk_error_report(sk);
   2.236 +	}
   2.237 +}
   2.238 +
   2.239 +static int raw_rcv_skb(struct sock * sk, struct sk_buff * skb)
   2.240 +{
   2.241 +	/* Charge it to the socket. */
   2.242 +	
   2.243 +	if (sock_queue_rcv_skb(sk, skb) < 0) {
   2.244 +		/* FIXME: increment a raw drops counter here */
   2.245 +		kfree_skb(skb);
   2.246 +		return NET_RX_DROP;
   2.247 +	}
   2.248 +
   2.249 +	return NET_RX_SUCCESS;
   2.250 +}
   2.251 +
   2.252 +int raw_rcv(struct sock *sk, struct sk_buff *skb)
   2.253 +{
   2.254 +	if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
   2.255 +		kfree_skb(skb);
   2.256 +		return NET_RX_DROP;
   2.257 +	}
   2.258 +
   2.259 +	skb_push(skb, skb->data - skb->nh.raw);
   2.260 +
   2.261 +	raw_rcv_skb(sk, skb);
   2.262 +	return 0;
   2.263 +}
   2.264 +
   2.265 +static int raw_send_hdrinc(struct sock *sk, void *from, int length,
   2.266 +			struct rtable *rt, 
   2.267 +			unsigned int flags)
   2.268 +{
   2.269 +	struct inet_opt *inet = inet_sk(sk);
   2.270 +	int hh_len;
   2.271 +	struct iphdr *iph;
   2.272 +	struct sk_buff *skb;
   2.273 +	int err;
   2.274 +
   2.275 +	if (length > rt->u.dst.dev->mtu) {
   2.276 +		ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->dport,
   2.277 +			       rt->u.dst.dev->mtu);
   2.278 +		return -EMSGSIZE;
   2.279 +	}
   2.280 +	if (flags&MSG_PROBE)
   2.281 +		goto out;
   2.282 +
   2.283 +	hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
   2.284 +
   2.285 +	skb = sock_alloc_send_skb(sk, length+hh_len+15,
   2.286 +				  flags&MSG_DONTWAIT, &err);
   2.287 +	if (skb == NULL)
   2.288 +		goto error; 
   2.289 +	skb_reserve(skb, hh_len);
   2.290 +
   2.291 +	skb->priority = sk->sk_priority;
   2.292 +	skb->dst = dst_clone(&rt->u.dst);
   2.293 +
   2.294 +	skb->nh.iph = iph = (struct iphdr *)skb_put(skb, length);
   2.295 +
   2.296 +	skb->ip_summed = CHECKSUM_NONE;
   2.297 +
   2.298 +	skb->h.raw = skb->nh.raw;
   2.299 +	err = memcpy_fromiovecend((void *)iph, from, 0, length);
   2.300 +	if (err)
   2.301 +		goto error_fault;
   2.302 +
   2.303 +	/* We don't modify invalid header */
   2.304 +	if (length >= sizeof(*iph) && iph->ihl * 4 <= length) {
   2.305 +		if (!iph->saddr)
   2.306 +			iph->saddr = rt->rt_src;
   2.307 +		iph->check   = 0;
   2.308 +		iph->tot_len = htons(length);
   2.309 +		if (!iph->id)
   2.310 +			ip_select_ident(iph, &rt->u.dst, NULL);
   2.311 +
   2.312 +		iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
   2.313 +	}
   2.314 +
   2.315 +	err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
   2.316 +		      dst_output);
   2.317 +	if (err > 0)
   2.318 +		err = inet->recverr ? net_xmit_errno(err) : 0;
   2.319 +	if (err)
   2.320 +		goto error;
   2.321 +out:
   2.322 +	return 0;
   2.323 +
   2.324 +error_fault:
   2.325 +	err = -EFAULT;
   2.326 +	kfree_skb(skb);
   2.327 +error:
   2.328 +	IP_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
   2.329 +	return err; 
   2.330 +}
   2.331 +
   2.332 +static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
   2.333 +		       size_t len)
   2.334 +{
   2.335 +	struct inet_opt *inet = inet_sk(sk);
   2.336 +	struct ipcm_cookie ipc;
   2.337 +	struct rtable *rt = NULL;
   2.338 +	int free = 0;
   2.339 +	u32 daddr;
   2.340 +	u32 saddr;
   2.341 +	u8  tos;
   2.342 +	int err;
   2.343 +
   2.344 +	err = -EMSGSIZE;
   2.345 +	if (len < 0 || len > 0xFFFF)
   2.346 +		goto out;
   2.347 +
   2.348 +	/*
   2.349 +	 *	Check the flags.
   2.350 +	 */
   2.351 +
   2.352 +	err = -EOPNOTSUPP;
   2.353 +	if (msg->msg_flags & MSG_OOB)	/* Mirror BSD error message */
   2.354 +		goto out;               /* compatibility */
   2.355 +			 
   2.356 +	/*
   2.357 +	 *	Get and verify the address. 
   2.358 +	 */
   2.359 +
   2.360 +	if (msg->msg_namelen) {
   2.361 +		struct sockaddr_in *usin = (struct sockaddr_in*)msg->msg_name;
   2.362 +		err = -EINVAL;
   2.363 +		if (msg->msg_namelen < sizeof(*usin))
   2.364 +			goto out;
   2.365 +		if (usin->sin_family != AF_INET) {
   2.366 +			static int complained;
   2.367 +			if (!complained++)
   2.368 +				printk(KERN_INFO "%s forgot to set AF_INET in "
   2.369 +						 "raw sendmsg. Fix it!\n",
   2.370 +						 current->comm);
   2.371 +			err = -EINVAL;
   2.372 +			if (usin->sin_family)
   2.373 +				goto out;
   2.374 +		}
   2.375 +		daddr = usin->sin_addr.s_addr;
   2.376 +		/* ANK: I did not forget to get protocol from port field.
   2.377 +		 * I just do not know, who uses this weirdness.
   2.378 +		 * IP_HDRINCL is much more convenient.
   2.379 +		 */
   2.380 +	} else {
   2.381 +		err = -EDESTADDRREQ;
   2.382 +		if (sk->sk_state != TCP_ESTABLISHED) 
   2.383 +			goto out;
   2.384 +		daddr = inet->daddr;
   2.385 +	}
   2.386 +
   2.387 +	ipc.addr = inet->saddr;
   2.388 +	ipc.opt = NULL;
   2.389 +	ipc.oif = sk->sk_bound_dev_if;
   2.390 +
   2.391 +	if (msg->msg_controllen) {
   2.392 +		err = ip_cmsg_send(msg, &ipc);
   2.393 +		if (err)
   2.394 +			goto out;
   2.395 +		if (ipc.opt)
   2.396 +			free = 1;
   2.397 +	}
   2.398 +
   2.399 +	saddr = ipc.addr;
   2.400 +	ipc.addr = daddr;
   2.401 +
   2.402 +	if (!ipc.opt)
   2.403 +		ipc.opt = inet->opt;
   2.404 +
   2.405 +	if (ipc.opt) {
   2.406 +		err = -EINVAL;
   2.407 +		/* Linux does not mangle headers on raw sockets,
   2.408 +		 * so that IP options + IP_HDRINCL is non-sense.
   2.409 +		 */
   2.410 +		if (inet->hdrincl)
   2.411 +			goto done;
   2.412 +		if (ipc.opt->srr) {
   2.413 +			if (!daddr)
   2.414 +				goto done;
   2.415 +			daddr = ipc.opt->faddr;
   2.416 +		}
   2.417 +	}
   2.418 +	tos = RT_TOS(inet->tos) | sk->sk_localroute;
   2.419 +	if (msg->msg_flags & MSG_DONTROUTE)
   2.420 +		tos |= RTO_ONLINK;
   2.421 +
   2.422 +	if (MULTICAST(daddr)) {
   2.423 +		if (!ipc.oif)
   2.424 +			ipc.oif = inet->mc_index;
   2.425 +		if (!saddr)
   2.426 +			saddr = inet->mc_addr;
   2.427 +	}
   2.428 +
   2.429 +	{
   2.430 +		struct flowi fl = { .oif = ipc.oif,
   2.431 +				    .nl_u = { .ip4_u =
   2.432 +					      { .daddr = daddr,
   2.433 +						.saddr = saddr,
   2.434 +						.tos = tos } },
   2.435 +				    .proto = inet->hdrincl ? IPPROTO_RAW :
   2.436 +					    		     sk->sk_protocol,
   2.437 +				  };
   2.438 +		err = ip_route_output_flow(&rt, &fl, sk, !(msg->msg_flags&MSG_DONTWAIT));
   2.439 +	}
   2.440 +	if (err)
   2.441 +		goto done;
   2.442 +
   2.443 +	err = -EACCES;
   2.444 +	if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
   2.445 +		goto done;
   2.446 +
   2.447 +	if (msg->msg_flags & MSG_CONFIRM)
   2.448 +		goto do_confirm;
   2.449 +back_from_confirm:
   2.450 +
   2.451 +	if (inet->hdrincl)
   2.452 +		err = raw_send_hdrinc(sk, msg->msg_iov, len, 
   2.453 +					rt, msg->msg_flags);
   2.454 +	
   2.455 +	 else {
   2.456 +		if (!ipc.addr)
   2.457 +			ipc.addr = rt->rt_dst;
   2.458 +		lock_sock(sk);
   2.459 +		err = ip_append_data(sk, ip_generic_getfrag, msg->msg_iov, len, 0,
   2.460 +					&ipc, rt, msg->msg_flags);
   2.461 +		if (err)
   2.462 +			ip_flush_pending_frames(sk);
   2.463 +		else if (!(msg->msg_flags & MSG_MORE))
   2.464 +			err = ip_push_pending_frames(sk);
   2.465 +		release_sock(sk);
   2.466 +	}
   2.467 +done:
   2.468 +	if (free)
   2.469 +		kfree(ipc.opt);
   2.470 +	ip_rt_put(rt);
   2.471 +
   2.472 +out:	return err < 0 ? err : len;
   2.473 +
   2.474 +do_confirm:
   2.475 +	dst_confirm(&rt->u.dst);
   2.476 +	if (!(msg->msg_flags & MSG_PROBE) || len)
   2.477 +		goto back_from_confirm;
   2.478 +	err = 0;
   2.479 +	goto done;
   2.480 +}
   2.481 +
   2.482 +static void raw_close(struct sock *sk, long timeout)
   2.483 +{
   2.484 +        /*
   2.485 +	 * Raw sockets may have direct kernel refereneces. Kill them.
   2.486 +	 */
   2.487 +	ip_ra_control(sk, 0, NULL);
   2.488 +
   2.489 +	sk_common_release(sk);
   2.490 +}
   2.491 +
   2.492 +/* This gets rid of all the nasties in af_inet. -DaveM */
   2.493 +static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
   2.494 +{
   2.495 +	struct inet_opt *inet = inet_sk(sk);
   2.496 +	struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
   2.497 +	int ret = -EINVAL;
   2.498 +	int chk_addr_ret;
   2.499 +
   2.500 +	if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
   2.501 +		goto out;
   2.502 +	chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
   2.503 +	ret = -EADDRNOTAVAIL;
   2.504 +	if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
   2.505 +	    chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
   2.506 +		goto out;
   2.507 +	inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr;
   2.508 +	if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
   2.509 +		inet->saddr = 0;  /* Use device */
   2.510 +	sk_dst_reset(sk);
   2.511 +	ret = 0;
   2.512 +out:	return ret;
   2.513 +}
   2.514 +
   2.515 +/*
   2.516 + *	This should be easy, if there is something there
   2.517 + *	we return it, otherwise we block.
   2.518 + */
   2.519 +
   2.520 +int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
   2.521 +		size_t len, int noblock, int flags, int *addr_len)
   2.522 +{
   2.523 +	struct inet_opt *inet = inet_sk(sk);
   2.524 +	size_t copied = 0;
   2.525 +	int err = -EOPNOTSUPP;
   2.526 +	struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
   2.527 +	struct sk_buff *skb;
   2.528 +
   2.529 +	if (flags & MSG_OOB)
   2.530 +		goto out;
   2.531 +
   2.532 +	if (addr_len)
   2.533 +		*addr_len = sizeof(*sin);
   2.534 +
   2.535 +	if (flags & MSG_ERRQUEUE) {
   2.536 +		err = ip_recv_error(sk, msg, len);
   2.537 +		goto out;
   2.538 +	}
   2.539 +
   2.540 +	skb = skb_recv_datagram(sk, flags, noblock, &err);
   2.541 +	if (!skb)
   2.542 +		goto out;
   2.543 +
   2.544 +	copied = skb->len;
   2.545 +	if (len < copied) {
   2.546 +		msg->msg_flags |= MSG_TRUNC;
   2.547 +		copied = len;
   2.548 +	}
   2.549 +
   2.550 +	err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
   2.551 +	if (err)
   2.552 +		goto done;
   2.553 +
   2.554 +	sock_recv_timestamp(msg, sk, skb);
   2.555 +
   2.556 +	/* Copy the address. */
   2.557 +	if (sin) {
   2.558 +		sin->sin_family = AF_INET;
   2.559 +		sin->sin_addr.s_addr = skb->nh.iph->saddr;
   2.560 +		memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
   2.561 +	}
   2.562 +	if (inet->cmsg_flags)
   2.563 +		ip_cmsg_recv(msg, skb);
   2.564 +	if (flags & MSG_TRUNC)
   2.565 +		copied = skb->len;
   2.566 +done:
   2.567 +	skb_free_datagram(sk, skb);
   2.568 +out:	return err ? err : copied;
   2.569 +}
   2.570 +
   2.571 +static int raw_init(struct sock *sk)
   2.572 +{
   2.573 +	struct raw_opt *tp = raw4_sk(sk);
   2.574 +	if (inet_sk(sk)->num == IPPROTO_ICMP)
   2.575 +		memset(&tp->filter, 0, sizeof(tp->filter));
   2.576 +	return 0;
   2.577 +}
   2.578 +
   2.579 +static int raw_seticmpfilter(struct sock *sk, char __user *optval, int optlen)
   2.580 +{
   2.581 +	if (optlen > sizeof(struct icmp_filter))
   2.582 +		optlen = sizeof(struct icmp_filter);
   2.583 +	if (copy_from_user(&raw4_sk(sk)->filter, optval, optlen))
   2.584 +		return -EFAULT;
   2.585 +	return 0;
   2.586 +}
   2.587 +
   2.588 +static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
   2.589 +{
   2.590 +	int len, ret = -EFAULT;
   2.591 +
   2.592 +	if (get_user(len, optlen))
   2.593 +		goto out;
   2.594 +	ret = -EINVAL;
   2.595 +	if (len < 0)
   2.596 +		goto out;
   2.597 +	if (len > sizeof(struct icmp_filter))
   2.598 +		len = sizeof(struct icmp_filter);
   2.599 +	ret = -EFAULT;
   2.600 +	if (put_user(len, optlen) ||
   2.601 +	    copy_to_user(optval, &raw4_sk(sk)->filter, len))
   2.602 +		goto out;
   2.603 +	ret = 0;
   2.604 +out:	return ret;
   2.605 +}
   2.606 +
   2.607 +static int raw_setsockopt(struct sock *sk, int level, int optname, 
   2.608 +			  char __user *optval, int optlen)
   2.609 +{
   2.610 +	if (level != SOL_RAW)
   2.611 +		return ip_setsockopt(sk, level, optname, optval, optlen);
   2.612 +
   2.613 +	if (optname == ICMP_FILTER) {
   2.614 +		if (inet_sk(sk)->num != IPPROTO_ICMP)
   2.615 +			return -EOPNOTSUPP;
   2.616 +		else
   2.617 +			return raw_seticmpfilter(sk, optval, optlen);
   2.618 +	}
   2.619 +	return -ENOPROTOOPT;
   2.620 +}
   2.621 +
   2.622 +static int raw_getsockopt(struct sock *sk, int level, int optname, 
   2.623 +			  char __user *optval, int __user *optlen)
   2.624 +{
   2.625 +	if (level != SOL_RAW)
   2.626 +		return ip_getsockopt(sk, level, optname, optval, optlen);
   2.627 +
   2.628 +	if (optname == ICMP_FILTER) {
   2.629 +		if (inet_sk(sk)->num != IPPROTO_ICMP)
   2.630 +			return -EOPNOTSUPP;
   2.631 +		else
   2.632 +			return raw_geticmpfilter(sk, optval, optlen);
   2.633 +	}
   2.634 +	return -ENOPROTOOPT;
   2.635 +}
   2.636 +
   2.637 +static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
   2.638 +{
   2.639 +	switch (cmd) {
   2.640 +		case SIOCOUTQ: {
   2.641 +			int amount = atomic_read(&sk->sk_wmem_alloc);
   2.642 +			return put_user(amount, (int __user *)arg);
   2.643 +		}
   2.644 +		case SIOCINQ: {
   2.645 +			struct sk_buff *skb;
   2.646 +			int amount = 0;
   2.647 +
   2.648 +			spin_lock_irq(&sk->sk_receive_queue.lock);
   2.649 +			skb = skb_peek(&sk->sk_receive_queue);
   2.650 +			if (skb != NULL)
   2.651 +				amount = skb->len;
   2.652 +			spin_unlock_irq(&sk->sk_receive_queue.lock);
   2.653 +			return put_user(amount, (int __user *)arg);
   2.654 +		}
   2.655 +
   2.656 +		default:
   2.657 +#ifdef CONFIG_IP_MROUTE
   2.658 +			return ipmr_ioctl(sk, cmd, (void __user *)arg);
   2.659 +#else
   2.660 +			return -ENOIOCTLCMD;
   2.661 +#endif
   2.662 +	}
   2.663 +}
   2.664 +
   2.665 +struct proto raw_prot = {
   2.666 +	.name =		"RAW",
   2.667 +	.close =	raw_close,
   2.668 +	.connect =	ip4_datagram_connect,
   2.669 +	.disconnect =	udp_disconnect,
   2.670 +	.ioctl =	raw_ioctl,
   2.671 +	.init =		raw_init,
   2.672 +	.setsockopt =	raw_setsockopt,
   2.673 +	.getsockopt =	raw_getsockopt,
   2.674 +	.sendmsg =	raw_sendmsg,
   2.675 +	.recvmsg =	raw_recvmsg,
   2.676 +	.bind =		raw_bind,
   2.677 +	.backlog_rcv =	raw_rcv_skb,
   2.678 +	.hash =		raw_v4_hash,
   2.679 +	.unhash =	raw_v4_unhash,
   2.680 +};
   2.681 +
   2.682 +#ifdef CONFIG_PROC_FS
   2.683 +struct raw_iter_state {
   2.684 +	int bucket;
   2.685 +};
   2.686 +
   2.687 +#define raw_seq_private(seq) ((struct raw_iter_state *)(seq)->private)
   2.688 +
   2.689 +static struct sock *raw_get_first(struct seq_file *seq)
   2.690 +{
   2.691 +	struct sock *sk;
   2.692 +	struct raw_iter_state* state = raw_seq_private(seq);
   2.693 +
   2.694 +	for (state->bucket = 0; state->bucket < RAWV4_HTABLE_SIZE; ++state->bucket) {
   2.695 +		struct hlist_node *node;
   2.696 +
   2.697 +		sk_for_each(sk, node, &raw_v4_htable[state->bucket])
   2.698 +			if (sk->sk_family == PF_INET)
   2.699 +				goto found;
   2.700 +	}
   2.701 +	sk = NULL;
   2.702 +found:
   2.703 +	return sk;
   2.704 +}
   2.705 +
   2.706 +static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
   2.707 +{
   2.708 +	struct raw_iter_state* state = raw_seq_private(seq);
   2.709 +
   2.710 +	do {
   2.711 +		sk = sk_next(sk);
   2.712 +try_again:
   2.713 +		;
   2.714 +	} while (sk && sk->sk_family != PF_INET);
   2.715 +
   2.716 +	if (!sk && ++state->bucket < RAWV4_HTABLE_SIZE) {
   2.717 +		sk = sk_head(&raw_v4_htable[state->bucket]);
   2.718 +		goto try_again;
   2.719 +	}
   2.720 +	return sk;
   2.721 +}
   2.722 +
   2.723 +static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
   2.724 +{
   2.725 +	struct sock *sk = raw_get_first(seq);
   2.726 +
   2.727 +	if (sk)
   2.728 +		while (pos && (sk = raw_get_next(seq, sk)) != NULL)
   2.729 +			--pos;
   2.730 +	return pos ? NULL : sk;
   2.731 +}
   2.732 +
   2.733 +static void *raw_seq_start(struct seq_file *seq, loff_t *pos)
   2.734 +{
   2.735 +	read_lock(&raw_v4_lock);
   2.736 +	return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
   2.737 +}
   2.738 +
   2.739 +static void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
   2.740 +{
   2.741 +	struct sock *sk;
   2.742 +
   2.743 +	if (v == SEQ_START_TOKEN)
   2.744 +		sk = raw_get_first(seq);
   2.745 +	else
   2.746 +		sk = raw_get_next(seq, v);
   2.747 +	++*pos;
   2.748 +	return sk;
   2.749 +}
   2.750 +
   2.751 +static void raw_seq_stop(struct seq_file *seq, void *v)
   2.752 +{
   2.753 +	read_unlock(&raw_v4_lock);
   2.754 +}
   2.755 +
   2.756 +static __inline__ char *get_raw_sock(struct sock *sp, char *tmpbuf, int i)
   2.757 +{
   2.758 +	struct inet_opt *inet = inet_sk(sp);
   2.759 +	unsigned int dest = inet->daddr,
   2.760 +		     src = inet->rcv_saddr;
   2.761 +	__u16 destp = 0,
   2.762 +	      srcp  = inet->num;
   2.763 +
   2.764 +	sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
   2.765 +		" %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
   2.766 +		i, src, srcp, dest, destp, sp->sk_state, 
   2.767 +		atomic_read(&sp->sk_wmem_alloc),
   2.768 +		atomic_read(&sp->sk_rmem_alloc),
   2.769 +		0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
   2.770 +		atomic_read(&sp->sk_refcnt), sp);
   2.771 +	return tmpbuf;
   2.772 +}
   2.773 +
   2.774 +static int raw_seq_show(struct seq_file *seq, void *v)
   2.775 +{
   2.776 +	char tmpbuf[129];
   2.777 +
   2.778 +	if (v == SEQ_START_TOKEN)
   2.779 +		seq_printf(seq, "%-127s\n",
   2.780 +			       "  sl  local_address rem_address   st tx_queue "
   2.781 +			       "rx_queue tr tm->when retrnsmt   uid  timeout "
   2.782 +			       "inode");
   2.783 +	else {
   2.784 +		struct raw_iter_state *state = raw_seq_private(seq);
   2.785 +
   2.786 +		seq_printf(seq, "%-127s\n",
   2.787 +			   get_raw_sock(v, tmpbuf, state->bucket));
   2.788 +	}
   2.789 +	return 0;
   2.790 +}
   2.791 +
   2.792 +static struct seq_operations raw_seq_ops = {
   2.793 +	.start = raw_seq_start,
   2.794 +	.next  = raw_seq_next,
   2.795 +	.stop  = raw_seq_stop,
   2.796 +	.show  = raw_seq_show,
   2.797 +};
   2.798 +
   2.799 +static int raw_seq_open(struct inode *inode, struct file *file)
   2.800 +{
   2.801 +	struct seq_file *seq;
   2.802 +	int rc = -ENOMEM;
   2.803 +	struct raw_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
   2.804 +
   2.805 +	if (!s)
   2.806 +		goto out;
   2.807 +	rc = seq_open(file, &raw_seq_ops);
   2.808 +	if (rc)
   2.809 +		goto out_kfree;
   2.810 +
   2.811 +	seq = file->private_data;
   2.812 +	seq->private = s;
   2.813 +	memset(s, 0, sizeof(*s));
   2.814 +out:
   2.815 +	return rc;
   2.816 +out_kfree:
   2.817 +	kfree(s);
   2.818 +	goto out;
   2.819 +}
   2.820 +
   2.821 +static struct file_operations raw_seq_fops = {
   2.822 +	.owner	 = THIS_MODULE,
   2.823 +	.open	 = raw_seq_open,
   2.824 +	.read	 = seq_read,
   2.825 +	.llseek	 = seq_lseek,
   2.826 +	.release = seq_release_private,
   2.827 +};
   2.828 +
   2.829 +int __init raw_proc_init(void)
   2.830 +{
   2.831 +	if (!proc_net_fops_create("raw", S_IRUGO, &raw_seq_fops))
   2.832 +		return -ENOMEM;
   2.833 +	return 0;
   2.834 +}
   2.835 +
   2.836 +void __init raw_proc_exit(void)
   2.837 +{
   2.838 +	proc_net_remove("raw");
   2.839 +}
   2.840 +#endif /* CONFIG_PROC_FS */