Import 2.3.10pre5
[davej-history.git] / net / ipv4 / raw.c
blob584fe81fc7abf13e4ce9c95740b32809eb65bea1
1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * RAW - implementation of IP "raw" sockets.
8 * Version: $Id: raw.c,v 1.42 1999/07/02 11:26:26 davem Exp $
10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
13 * Fixes:
14 * Alan Cox : verify_area() fixed up
15 * Alan Cox : ICMP error handling
16 * Alan Cox : EMSGSIZE if you send too big a packet
17 * Alan Cox : Now uses generic datagrams and shared skbuff
18 * library. No more peek crashes, no more backlogs
19 * Alan Cox : Checks sk->broadcast.
20 * Alan Cox : Uses skb_free_datagram/skb_copy_datagram
21 * Alan Cox : Raw passes ip options too
22 * Alan Cox : Setsocketopt added
23 * Alan Cox : Fixed error return for broadcasts
24 * Alan Cox : Removed wake_up calls
25 * Alan Cox : Use ttl/tos
26 * Alan Cox : Cleaned up old debugging
27 * Alan Cox : Use new kernel side addresses
28 * Arnt Gulbrandsen : Fixed MSG_DONTROUTE in raw sockets.
29 * Alan Cox : BSD style RAW socket demultiplexing.
30 * Alan Cox : Beginnings of mrouted support.
31 * Alan Cox : Added IP_HDRINCL option.
32 * Alan Cox : Skip broadcast check if BSDism set.
33 * David S. Miller : New socket lookup architecture.
35 * This program is free software; you can redistribute it and/or
36 * modify it under the terms of the GNU General Public License
37 * as published by the Free Software Foundation; either version
38 * 2 of the License, or (at your option) any later version.
41 #include <linux/config.h>
42 #include <asm/system.h>
43 #include <asm/uaccess.h>
44 #include <linux/types.h>
45 #include <linux/sched.h>
46 #include <linux/errno.h>
47 #include <linux/timer.h>
48 #include <linux/mm.h>
49 #include <linux/kernel.h>
50 #include <linux/fcntl.h>
51 #include <linux/socket.h>
52 #include <linux/in.h>
53 #include <linux/inet.h>
54 #include <linux/netdevice.h>
55 #include <linux/mroute.h>
56 #include <net/ip.h>
57 #include <net/protocol.h>
58 #include <linux/skbuff.h>
59 #include <net/sock.h>
60 #include <net/icmp.h>
61 #include <net/udp.h>
62 #include <net/raw.h>
63 #include <net/checksum.h>
65 #ifdef CONFIG_IP_MROUTE
66 struct sock *mroute_socket=NULL;
67 #endif
69 struct sock *raw_v4_htable[RAWV4_HTABLE_SIZE];
71 static void raw_v4_hash(struct sock *sk)
73 struct sock **skp = &raw_v4_htable[sk->num & (RAWV4_HTABLE_SIZE - 1)];
75 SOCKHASH_LOCK_WRITE();
76 if ((sk->next = *skp) != NULL)
77 (*skp)->pprev = &sk->next;
78 *skp = sk;
79 sk->pprev = skp;
80 sk->prot->inuse++;
81 if(sk->prot->highestinuse < sk->prot->inuse)
82 sk->prot->highestinuse = sk->prot->inuse;
83 SOCKHASH_UNLOCK_WRITE();
86 static void raw_v4_unhash(struct sock *sk)
88 SOCKHASH_LOCK_WRITE();
89 if (sk->pprev) {
90 if (sk->next)
91 sk->next->pprev = sk->pprev;
92 *sk->pprev = sk->next;
93 sk->pprev = NULL;
94 sk->prot->inuse--;
96 SOCKHASH_UNLOCK_WRITE();
99 static __inline__ struct sock *__raw_v4_lookup(struct sock *sk, unsigned short num,
100 unsigned long raddr, unsigned long laddr,
101 int dif)
103 struct sock *s = sk;
105 for(s = sk; s; s = s->next) {
106 if((s->num == num) &&
107 !(s->dead && (s->state == TCP_CLOSE)) &&
108 !(s->daddr && s->daddr != raddr) &&
109 !(s->rcv_saddr && s->rcv_saddr != laddr) &&
110 !(s->bound_dev_if && s->bound_dev_if != dif))
111 break; /* gotcha */
113 return s;
116 struct sock *raw_v4_lookup(struct sock *sk, unsigned short num,
117 unsigned long raddr, unsigned long laddr,
118 int dif)
120 SOCKHASH_LOCK_READ();
121 sk = __raw_v4_lookup(sk, num, raddr, laddr, dif);
122 SOCKHASH_UNLOCK_READ();
124 return sk;
128 * 0 - deliver
129 * 1 - block
131 static __inline__ int icmp_filter(struct sock *sk, struct sk_buff *skb)
133 int type;
135 type = skb->h.icmph->type;
136 if (type < 32)
137 return test_bit(type, &sk->tp_pinfo.tp_raw4.filter);
139 /* Do not block unknown ICMP types */
140 return 0;
143 /* IP input processing comes here for RAW socket delivery.
144 * This is fun as to avoid copies we want to make no surplus
145 * copies.
147 * RFC 1122: SHOULD pass TOS value up to the transport layer.
148 * -> It does. And not only TOS, but all IP header.
150 struct sock *raw_v4_input(struct sk_buff *skb, struct iphdr *iph, int hash)
152 struct sock *sk;
154 SOCKHASH_LOCK_READ_BH();
155 if ((sk = raw_v4_htable[hash]) == NULL)
156 goto out;
157 sk = __raw_v4_lookup(sk, iph->protocol,
158 iph->saddr, iph->daddr,
159 skb->dev->ifindex);
160 while(sk != NULL) {
161 struct sock *sknext = __raw_v4_lookup(sk->next, iph->protocol,
162 iph->saddr, iph->daddr,
163 skb->dev->ifindex);
165 if (iph->protocol != IPPROTO_ICMP ||
166 ! icmp_filter(sk, skb)) {
167 struct sk_buff *clone;
169 if(sknext == NULL)
170 break;
171 clone = skb_clone(skb, GFP_ATOMIC);
172 if(clone) {
173 SOCKHASH_UNLOCK_READ_BH();
174 raw_rcv(sk, clone);
175 SOCKHASH_LOCK_READ_BH();
178 sk = sknext;
180 out:
181 SOCKHASH_UNLOCK_READ_BH();
183 return sk;
186 void raw_err (struct sock *sk, struct sk_buff *skb)
188 int type = skb->h.icmph->type;
189 int code = skb->h.icmph->code;
190 u32 info = 0;
191 int err = 0;
192 int harderr = 0;
194 /* Report error on raw socket, if:
195 1. User requested ip_recverr.
196 2. Socket is connected (otherwise the error indication
197 is useless without ip_recverr and error is hard.
199 if (!sk->ip_recverr && sk->state != TCP_ESTABLISHED)
200 return;
202 switch (type) {
203 default:
204 case ICMP_TIME_EXCEEDED:
205 err = EHOSTUNREACH;
206 break;
207 case ICMP_SOURCE_QUENCH:
208 return;
209 case ICMP_PARAMETERPROB:
210 err = EPROTO;
211 info = ntohl(skb->h.icmph->un.gateway)>>24;
212 harderr = 1;
213 break;
214 case ICMP_DEST_UNREACH:
215 err = EHOSTUNREACH;
216 if (code > NR_ICMP_UNREACH)
217 break;
218 err = icmp_err_convert[code].errno;
219 harderr = icmp_err_convert[code].fatal;
220 if (code == ICMP_FRAG_NEEDED) {
221 harderr = (sk->ip_pmtudisc != IP_PMTUDISC_DONT);
222 err = EMSGSIZE;
223 info = ntohs(skb->h.icmph->un.frag.mtu);
227 if (sk->ip_recverr)
228 ip_icmp_error(sk, skb, err, 0, info, (u8 *)(skb->h.icmph + 1));
230 if (sk->ip_recverr || harderr) {
231 sk->err = err;
232 sk->error_report(sk);
236 static int raw_rcv_skb(struct sock * sk, struct sk_buff * skb)
238 /* Charge it to the socket. */
240 if (sock_queue_rcv_skb(sk,skb)<0)
242 ip_statistics.IpInDiscards++;
243 kfree_skb(skb);
244 return -1;
247 ip_statistics.IpInDelivers++;
248 return 0;
252 * This should be the easiest of all, all we do is
253 * copy it into a buffer. All demultiplexing is done
254 * in ip.c
257 int raw_rcv(struct sock *sk, struct sk_buff *skb)
259 /* Now we need to copy this into memory. */
260 skb_trim(skb, ntohs(skb->nh.iph->tot_len));
262 skb->h.raw = skb->nh.raw;
264 raw_rcv_skb(sk, skb);
265 return 0;
268 struct rawfakehdr
270 struct iovec *iov;
271 u32 saddr;
275 * Send a RAW IP packet.
279 * Callback support is trivial for SOCK_RAW
282 static int raw_getfrag(const void *p, char *to, unsigned int offset, unsigned int fraglen)
284 struct rawfakehdr *rfh = (struct rawfakehdr *) p;
285 return memcpy_fromiovecend(to, rfh->iov, offset, fraglen);
289 * IPPROTO_RAW needs extra work.
292 static int raw_getrawfrag(const void *p, char *to, unsigned int offset, unsigned int fraglen)
294 struct rawfakehdr *rfh = (struct rawfakehdr *) p;
296 if (memcpy_fromiovecend(to, rfh->iov, offset, fraglen))
297 return -EFAULT;
299 if (offset==0) {
300 struct iphdr *iph = (struct iphdr *)to;
301 if (!iph->saddr)
302 iph->saddr = rfh->saddr;
303 iph->check=0;
304 iph->tot_len=htons(fraglen); /* This is right as you can't frag
305 RAW packets */
307 * Deliberate breach of modularity to keep
308 * ip_build_xmit clean (well less messy).
310 if (!iph->id)
311 iph->id = htons(ip_id_count++);
312 iph->check=ip_fast_csum((unsigned char *)iph, iph->ihl);
314 return 0;
317 static int raw_sendmsg(struct sock *sk, struct msghdr *msg, int len)
319 struct ipcm_cookie ipc;
320 struct rawfakehdr rfh;
321 struct rtable *rt = NULL;
322 int free = 0;
323 u32 daddr;
324 u8 tos;
325 int err;
327 /* This check is ONLY to check for arithmetic overflow
328 on integer(!) len. Not more! Real check will be made
329 in ip_build_xmit --ANK
331 BTW socket.c -> af_*.c -> ... make multiple
332 invalid conversions size_t -> int. We MUST repair it f.e.
333 by replacing all of them with size_t and revise all
334 the places sort of len += sizeof(struct iphdr)
335 If len was ULONG_MAX-10 it would be cathastrophe --ANK
338 if (len < 0 || len > 0xFFFF)
339 return -EMSGSIZE;
342 * Check the flags.
345 if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
346 return -EOPNOTSUPP;
348 if (msg->msg_flags & ~(MSG_DONTROUTE|MSG_DONTWAIT))
349 return(-EINVAL);
352 * Get and verify the address.
355 if (msg->msg_namelen) {
356 struct sockaddr_in *usin = (struct sockaddr_in*)msg->msg_name;
357 if (msg->msg_namelen < sizeof(*usin))
358 return(-EINVAL);
359 if (usin->sin_family != AF_INET) {
360 static int complained;
361 if (!complained++)
362 printk(KERN_INFO "%s forgot to set AF_INET in raw sendmsg. Fix it!\n", current->comm);
363 if (usin->sin_family)
364 return -EINVAL;
366 daddr = usin->sin_addr.s_addr;
367 /* ANK: I did not forget to get protocol from port field.
368 * I just do not know, who uses this weirdness.
369 * IP_HDRINCL is much more convenient.
371 } else {
372 if (sk->state != TCP_ESTABLISHED)
373 return(-EINVAL);
374 daddr = sk->daddr;
377 ipc.addr = sk->saddr;
378 ipc.opt = NULL;
379 ipc.oif = sk->bound_dev_if;
381 if (msg->msg_controllen) {
382 int tmp = ip_cmsg_send(msg, &ipc);
383 if (tmp)
384 return tmp;
385 if (ipc.opt)
386 free=1;
389 rfh.saddr = ipc.addr;
390 ipc.addr = daddr;
392 if (!ipc.opt)
393 ipc.opt = sk->opt;
395 if (ipc.opt) {
396 err = -EINVAL;
397 /* Linux does not mangle headers on raw sockets,
398 * so that IP options + IP_HDRINCL is non-sense.
400 if (sk->ip_hdrincl)
401 goto done;
402 if (ipc.opt->srr) {
403 if (!daddr)
404 goto done;
405 daddr = ipc.opt->faddr;
408 tos = RT_TOS(sk->ip_tos) | sk->localroute;
409 if (msg->msg_flags&MSG_DONTROUTE)
410 tos |= RTO_ONLINK;
412 if (MULTICAST(daddr)) {
413 if (!ipc.oif)
414 ipc.oif = sk->ip_mc_index;
415 if (!rfh.saddr)
416 rfh.saddr = sk->ip_mc_addr;
419 err = ip_route_output(&rt, daddr, rfh.saddr, tos, ipc.oif);
421 if (err)
422 goto done;
424 err = -EACCES;
425 if (rt->rt_flags&RTCF_BROADCAST && !sk->broadcast)
426 goto done;
428 rfh.iov = msg->msg_iov;
429 rfh.saddr = rt->rt_src;
430 if (!ipc.addr)
431 ipc.addr = rt->rt_dst;
432 err=ip_build_xmit(sk, sk->ip_hdrincl ? raw_getrawfrag : raw_getfrag,
433 &rfh, len, &ipc, rt, msg->msg_flags);
435 done:
436 if (free)
437 kfree(ipc.opt);
438 ip_rt_put(rt);
440 return err<0 ? err : len;
443 static void raw_close(struct sock *sk, long timeout)
445 bh_lock_sock(sk);
447 /* Observation: when raw_close is called, processes have
448 no access to socket anymore. But net still has.
449 Step one, detach it from networking:
451 A. Remove from hash tables.
453 sk->state = TCP_CLOSE;
454 raw_v4_unhash(sk);
456 B. Raw sockets may have direct kernel refereneces. Kill them.
458 ip_ra_control(sk, 0, NULL);
460 /* In this point socket cannot receive new packets anymore */
463 /* But we still have packets pending on receive
464 queue and probably, our own packets waiting in device queues.
465 sock_destroy will drain receive queue, but transmitted
466 packets will delay socket destruction.
467 Set sk->dead=1 in order to prevent wakeups, when these
468 packet will be freed.
470 sk->dead=1;
471 destroy_sock(sk);
473 /* That's all. No races here. */
476 /* This gets rid of all the nasties in af_inet. -DaveM */
477 static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
479 struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
480 int chk_addr_ret;
482 if((sk->state != TCP_CLOSE) || (addr_len < sizeof(struct sockaddr_in)))
483 return -EINVAL;
484 chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
485 if(addr->sin_addr.s_addr != 0 && chk_addr_ret != RTN_LOCAL &&
486 chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST) {
487 #ifdef CONFIG_IP_TRANSPARENT_PROXY
488 /* Superuser may bind to any address to allow transparent proxying. */
489 if(chk_addr_ret != RTN_UNICAST || !capable(CAP_NET_ADMIN))
490 #endif
491 return -EADDRNOTAVAIL;
493 sk->rcv_saddr = sk->saddr = addr->sin_addr.s_addr;
494 if(chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
495 sk->saddr = 0; /* Use device */
496 dst_release(xchg(&sk->dst_cache, NULL));
497 return 0;
501 * This should be easy, if there is something there
502 * we return it, otherwise we block.
505 int raw_recvmsg(struct sock *sk, struct msghdr *msg, int len,
506 int noblock, int flags,int *addr_len)
508 int copied=0;
509 struct sk_buff *skb;
510 int err;
511 struct sockaddr_in *sin=(struct sockaddr_in *)msg->msg_name;
513 if (flags & MSG_OOB)
514 return -EOPNOTSUPP;
516 if (addr_len)
517 *addr_len=sizeof(*sin);
519 if (flags & MSG_ERRQUEUE)
520 return ip_recv_error(sk, msg, len);
522 skb=skb_recv_datagram(sk,flags,noblock,&err);
523 if(skb==NULL)
524 return err;
526 copied = skb->len;
527 if (len < copied)
529 msg->msg_flags |= MSG_TRUNC;
530 copied = len;
533 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
534 if (err)
535 goto done;
537 sk->stamp=skb->stamp;
539 /* Copy the address. */
540 if (sin) {
541 sin->sin_family = AF_INET;
542 sin->sin_addr.s_addr = skb->nh.iph->saddr;
544 if (sk->ip_cmsg_flags)
545 ip_cmsg_recv(msg, skb);
546 done:
547 skb_free_datagram(sk, skb);
548 return (err ? : copied);
551 static int raw_init(struct sock *sk)
553 struct raw_opt *tp = &(sk->tp_pinfo.tp_raw4);
554 if (sk->num == IPPROTO_ICMP)
555 memset(&tp->filter, 0, sizeof(tp->filter));
556 return 0;
559 static int raw_seticmpfilter(struct sock *sk, char *optval, int optlen)
561 if (optlen > sizeof(struct icmp_filter))
562 optlen = sizeof(struct icmp_filter);
563 if (copy_from_user(&sk->tp_pinfo.tp_raw4.filter, optval, optlen))
564 return -EFAULT;
565 return 0;
568 static int raw_geticmpfilter(struct sock *sk, char *optval, int *optlen)
570 int len;
572 if (get_user(len,optlen))
573 return -EFAULT;
574 if (len > sizeof(struct icmp_filter))
575 len = sizeof(struct icmp_filter);
576 if (put_user(len, optlen))
577 return -EFAULT;
578 if (copy_to_user(optval, &sk->tp_pinfo.tp_raw4.filter, len))
579 return -EFAULT;
580 return 0;
583 static int raw_setsockopt(struct sock *sk, int level, int optname,
584 char *optval, int optlen)
586 if (level != SOL_RAW)
587 return ip_setsockopt(sk, level, optname, optval, optlen);
589 switch (optname) {
590 case ICMP_FILTER:
591 if (sk->num != IPPROTO_ICMP)
592 return -EOPNOTSUPP;
593 return raw_seticmpfilter(sk, optval, optlen);
596 return -ENOPROTOOPT;
599 static int raw_getsockopt(struct sock *sk, int level, int optname,
600 char *optval, int *optlen)
602 if (level != SOL_RAW)
603 return ip_getsockopt(sk, level, optname, optval, optlen);
605 switch (optname) {
606 case ICMP_FILTER:
607 if (sk->num != IPPROTO_ICMP)
608 return -EOPNOTSUPP;
609 return raw_geticmpfilter(sk, optval, optlen);
612 return -ENOPROTOOPT;
615 static void get_raw_sock(struct sock *sp, char *tmpbuf, int i)
617 unsigned int dest, src;
618 __u16 destp, srcp;
619 int timer_active;
620 unsigned long timer_expires;
622 dest = sp->daddr;
623 src = sp->rcv_saddr;
624 destp = ntohs(sp->dport);
625 srcp = ntohs(sp->sport);
626 timer_active = (sp->timer.prev != NULL) ? 2 : 0;
627 timer_expires = (timer_active == 2 ? sp->timer.expires : jiffies);
628 sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
629 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %ld",
630 i, src, srcp, dest, destp, sp->state,
631 atomic_read(&sp->wmem_alloc), atomic_read(&sp->rmem_alloc),
632 timer_active, timer_expires-jiffies, 0,
633 sp->socket->inode->i_uid, timer_active ? sp->timeout : 0,
634 sp->socket ? sp->socket->inode->i_ino : 0);
637 int raw_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
639 int len = 0, num = 0, i;
640 off_t pos = 0;
641 off_t begin;
642 char tmpbuf[129];
644 if (offset < 128)
645 len += sprintf(buffer, "%-127s\n",
646 " sl local_address rem_address st tx_queue "
647 "rx_queue tr tm->when retrnsmt uid timeout inode");
648 pos = 128;
649 SOCKHASH_LOCK_READ();
650 for (i = 0; i < RAWV4_HTABLE_SIZE; i++) {
651 struct sock *sk;
653 for (sk = raw_v4_htable[i]; sk; sk = sk->next, num++) {
654 if (sk->family != PF_INET)
655 continue;
656 pos += 128;
657 if (pos < offset)
658 continue;
659 get_raw_sock(sk, tmpbuf, i);
660 len += sprintf(buffer+len, "%-127s\n", tmpbuf);
661 if(len >= length)
662 goto out;
665 out:
666 SOCKHASH_UNLOCK_READ();
667 begin = len - (pos - offset);
668 *start = buffer + begin;
669 len -= begin;
670 if(len > length)
671 len = length;
672 if (len < 0)
673 len = 0;
674 return len;
677 struct proto raw_prot = {
678 raw_close, /* close */
679 udp_connect, /* connect */
680 NULL, /* accept */
681 NULL, /* retransmit */
682 NULL, /* write_wakeup */
683 NULL, /* read_wakeup */
684 datagram_poll, /* poll */
685 #ifdef CONFIG_IP_MROUTE
686 ipmr_ioctl, /* ioctl */
687 #else
688 NULL, /* ioctl */
689 #endif
690 raw_init, /* init */
691 NULL, /* destroy */
692 NULL, /* shutdown */
693 raw_setsockopt, /* setsockopt */
694 raw_getsockopt, /* getsockopt */
695 raw_sendmsg, /* sendmsg */
696 raw_recvmsg, /* recvmsg */
697 raw_bind, /* bind */
698 raw_rcv_skb, /* backlog_rcv */
699 raw_v4_hash, /* hash */
700 raw_v4_unhash, /* unhash */
701 NULL, /* get_port */
702 128, /* max_header */
703 0, /* retransmits */
704 "RAW", /* name */
705 0, /* inuse */
706 0 /* highestinuse */