* better
[mascara-docs.git] / i386 / linux-2.3.21 / net / ipv4 / raw.c
blob83044d2cda0bac06469e5f3e2ed4b3db2d5c3c97
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.43 1999/08/20 11:05:57 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/inet_common.h>
64 #include <net/checksum.h>
66 struct sock *raw_v4_htable[RAWV4_HTABLE_SIZE];
67 rwlock_t raw_v4_lock = RW_LOCK_UNLOCKED;
69 static void raw_v4_hash(struct sock *sk)
71 struct sock **skp = &raw_v4_htable[sk->num & (RAWV4_HTABLE_SIZE - 1)];
73 write_lock_bh(&raw_v4_lock);
74 if ((sk->next = *skp) != NULL)
75 (*skp)->pprev = &sk->next;
76 *skp = sk;
77 sk->pprev = skp;
78 sk->prot->inuse++;
79 if(sk->prot->highestinuse < sk->prot->inuse)
80 sk->prot->highestinuse = sk->prot->inuse;
81 sock_hold(sk);
82 write_unlock_bh(&raw_v4_lock);
85 static void raw_v4_unhash(struct sock *sk)
87 write_lock_bh(&raw_v4_lock);
88 if (sk->pprev) {
89 if (sk->next)
90 sk->next->pprev = sk->pprev;
91 *sk->pprev = sk->next;
92 sk->pprev = NULL;
93 sk->prot->inuse--;
94 __sock_put(sk);
96 write_unlock_bh(&raw_v4_lock);
99 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->daddr && s->daddr != raddr) &&
108 !(s->rcv_saddr && s->rcv_saddr != laddr) &&
109 !(s->bound_dev_if && s->bound_dev_if != dif))
110 break; /* gotcha */
112 return s;
116 * 0 - deliver
117 * 1 - block
119 static __inline__ int icmp_filter(struct sock *sk, struct sk_buff *skb)
121 int type;
123 type = skb->h.icmph->type;
124 if (type < 32)
125 return test_bit(type, &sk->tp_pinfo.tp_raw4.filter);
127 /* Do not block unknown ICMP types */
128 return 0;
131 /* IP input processing comes here for RAW socket delivery.
132 * This is fun as to avoid copies we want to make no surplus
133 * copies.
135 * RFC 1122: SHOULD pass TOS value up to the transport layer.
136 * -> It does. And not only TOS, but all IP header.
138 struct sock *raw_v4_input(struct sk_buff *skb, struct iphdr *iph, int hash)
140 struct sock *sk;
142 read_lock(&raw_v4_lock);
143 if ((sk = raw_v4_htable[hash]) == NULL)
144 goto out;
145 sk = __raw_v4_lookup(sk, iph->protocol,
146 iph->saddr, iph->daddr,
147 skb->dev->ifindex);
149 while(sk != NULL) {
150 struct sock *sknext = __raw_v4_lookup(sk->next, iph->protocol,
151 iph->saddr, iph->daddr,
152 skb->dev->ifindex);
153 if (iph->protocol != IPPROTO_ICMP ||
154 ! icmp_filter(sk, skb)) {
155 struct sk_buff *clone;
157 if(sknext == NULL)
158 break;
159 clone = skb_clone(skb, GFP_ATOMIC);
160 /* Not releasing hash table! */
161 if(clone)
162 raw_rcv(sk, clone);
164 sk = sknext;
166 out:
167 if (sk)
168 sock_hold(sk);
169 read_unlock(&raw_v4_lock);
171 return sk;
174 void raw_err (struct sock *sk, struct sk_buff *skb)
176 int type = skb->h.icmph->type;
177 int code = skb->h.icmph->code;
178 u32 info = 0;
179 int err = 0;
180 int harderr = 0;
182 /* Report error on raw socket, if:
183 1. User requested ip_recverr.
184 2. Socket is connected (otherwise the error indication
185 is useless without ip_recverr and error is hard.
187 if (!sk->protinfo.af_inet.recverr && sk->state != TCP_ESTABLISHED)
188 return;
190 switch (type) {
191 default:
192 case ICMP_TIME_EXCEEDED:
193 err = EHOSTUNREACH;
194 break;
195 case ICMP_SOURCE_QUENCH:
196 return;
197 case ICMP_PARAMETERPROB:
198 err = EPROTO;
199 info = ntohl(skb->h.icmph->un.gateway)>>24;
200 harderr = 1;
201 break;
202 case ICMP_DEST_UNREACH:
203 err = EHOSTUNREACH;
204 if (code > NR_ICMP_UNREACH)
205 break;
206 err = icmp_err_convert[code].errno;
207 harderr = icmp_err_convert[code].fatal;
208 if (code == ICMP_FRAG_NEEDED) {
209 harderr = (sk->protinfo.af_inet.pmtudisc != IP_PMTUDISC_DONT);
210 err = EMSGSIZE;
211 info = ntohs(skb->h.icmph->un.frag.mtu);
215 if (sk->protinfo.af_inet.recverr)
216 ip_icmp_error(sk, skb, err, 0, info, (u8 *)(skb->h.icmph + 1));
218 if (sk->protinfo.af_inet.recverr || harderr) {
219 sk->err = err;
220 sk->error_report(sk);
224 static int raw_rcv_skb(struct sock * sk, struct sk_buff * skb)
226 /* Charge it to the socket. */
228 if (sock_queue_rcv_skb(sk,skb)<0)
230 ip_statistics.IpInDiscards++;
231 kfree_skb(skb);
232 return -1;
235 ip_statistics.IpInDelivers++;
236 return 0;
240 * This should be the easiest of all, all we do is
241 * copy it into a buffer. All demultiplexing is done
242 * in ip.c
245 int raw_rcv(struct sock *sk, struct sk_buff *skb)
247 /* Now we need to copy this into memory. */
248 skb_trim(skb, ntohs(skb->nh.iph->tot_len));
250 skb->h.raw = skb->nh.raw;
252 raw_rcv_skb(sk, skb);
253 return 0;
256 struct rawfakehdr
258 struct iovec *iov;
259 u32 saddr;
263 * Send a RAW IP packet.
267 * Callback support is trivial for SOCK_RAW
270 static int raw_getfrag(const void *p, char *to, unsigned int offset, unsigned int fraglen)
272 struct rawfakehdr *rfh = (struct rawfakehdr *) p;
273 return memcpy_fromiovecend(to, rfh->iov, offset, fraglen);
277 * IPPROTO_RAW needs extra work.
280 static int raw_getrawfrag(const void *p, char *to, unsigned int offset, unsigned int fraglen)
282 struct rawfakehdr *rfh = (struct rawfakehdr *) p;
284 if (memcpy_fromiovecend(to, rfh->iov, offset, fraglen))
285 return -EFAULT;
287 if (offset==0) {
288 struct iphdr *iph = (struct iphdr *)to;
289 if (!iph->saddr)
290 iph->saddr = rfh->saddr;
291 iph->check=0;
292 iph->tot_len=htons(fraglen); /* This is right as you can't frag
293 RAW packets */
295 * Deliberate breach of modularity to keep
296 * ip_build_xmit clean (well less messy).
298 if (!iph->id)
299 iph->id = htons(ip_id_count++);
300 iph->check=ip_fast_csum((unsigned char *)iph, iph->ihl);
302 return 0;
305 static int raw_sendmsg(struct sock *sk, struct msghdr *msg, int len)
307 struct ipcm_cookie ipc;
308 struct rawfakehdr rfh;
309 struct rtable *rt = NULL;
310 int free = 0;
311 u32 daddr;
312 u8 tos;
313 int err;
315 /* This check is ONLY to check for arithmetic overflow
316 on integer(!) len. Not more! Real check will be made
317 in ip_build_xmit --ANK
319 BTW socket.c -> af_*.c -> ... make multiple
320 invalid conversions size_t -> int. We MUST repair it f.e.
321 by replacing all of them with size_t and revise all
322 the places sort of len += sizeof(struct iphdr)
323 If len was ULONG_MAX-10 it would be cathastrophe --ANK
326 if (len < 0 || len > 0xFFFF)
327 return -EMSGSIZE;
330 * Check the flags.
333 if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
334 return -EOPNOTSUPP;
337 * Get and verify the address.
340 if (msg->msg_namelen) {
341 struct sockaddr_in *usin = (struct sockaddr_in*)msg->msg_name;
342 if (msg->msg_namelen < sizeof(*usin))
343 return(-EINVAL);
344 if (usin->sin_family != AF_INET) {
345 static int complained;
346 if (!complained++)
347 printk(KERN_INFO "%s forgot to set AF_INET in raw sendmsg. Fix it!\n", current->comm);
348 if (usin->sin_family)
349 return -EINVAL;
351 daddr = usin->sin_addr.s_addr;
352 /* ANK: I did not forget to get protocol from port field.
353 * I just do not know, who uses this weirdness.
354 * IP_HDRINCL is much more convenient.
356 } else {
357 if (sk->state != TCP_ESTABLISHED)
358 return(-EINVAL);
359 daddr = sk->daddr;
362 ipc.addr = sk->saddr;
363 ipc.opt = NULL;
364 ipc.oif = sk->bound_dev_if;
366 if (msg->msg_controllen) {
367 int tmp = ip_cmsg_send(msg, &ipc);
368 if (tmp)
369 return tmp;
370 if (ipc.opt)
371 free=1;
374 rfh.saddr = ipc.addr;
375 ipc.addr = daddr;
377 if (!ipc.opt)
378 ipc.opt = sk->protinfo.af_inet.opt;
380 if (ipc.opt) {
381 err = -EINVAL;
382 /* Linux does not mangle headers on raw sockets,
383 * so that IP options + IP_HDRINCL is non-sense.
385 if (sk->protinfo.af_inet.hdrincl)
386 goto done;
387 if (ipc.opt->srr) {
388 if (!daddr)
389 goto done;
390 daddr = ipc.opt->faddr;
393 tos = RT_TOS(sk->protinfo.af_inet.tos) | sk->localroute;
394 if (msg->msg_flags&MSG_DONTROUTE)
395 tos |= RTO_ONLINK;
397 if (MULTICAST(daddr)) {
398 if (!ipc.oif)
399 ipc.oif = sk->protinfo.af_inet.mc_index;
400 if (!rfh.saddr)
401 rfh.saddr = sk->protinfo.af_inet.mc_addr;
404 err = ip_route_output(&rt, daddr, rfh.saddr, tos, ipc.oif);
406 if (err)
407 goto done;
409 err = -EACCES;
410 if (rt->rt_flags&RTCF_BROADCAST && !sk->broadcast)
411 goto done;
413 if (msg->msg_flags&MSG_CONFIRM)
414 goto do_confirm;
415 back_from_confirm:
417 rfh.iov = msg->msg_iov;
418 rfh.saddr = rt->rt_src;
419 if (!ipc.addr)
420 ipc.addr = rt->rt_dst;
421 err=ip_build_xmit(sk, sk->protinfo.af_inet.hdrincl ? raw_getrawfrag : raw_getfrag,
422 &rfh, len, &ipc, rt, msg->msg_flags);
424 done:
425 if (free)
426 kfree(ipc.opt);
427 ip_rt_put(rt);
429 return err<0 ? err : len;
431 do_confirm:
432 dst_confirm(&rt->u.dst);
433 if (!(msg->msg_flags&MSG_PROBE) || len)
434 goto back_from_confirm;
435 err = 0;
436 goto done;
439 static void raw_close(struct sock *sk, long timeout)
442 * Raw sockets may have direct kernel refereneces. Kill them.
444 ip_ra_control(sk, 0, NULL);
446 inet_sock_release(sk);
449 /* This gets rid of all the nasties in af_inet. -DaveM */
450 static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
452 struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
453 int chk_addr_ret;
455 if((sk->state != TCP_CLOSE) || (addr_len < sizeof(struct sockaddr_in)))
456 return -EINVAL;
457 chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
458 if(addr->sin_addr.s_addr != 0 && chk_addr_ret != RTN_LOCAL &&
459 chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
460 return -EADDRNOTAVAIL;
461 sk->rcv_saddr = sk->saddr = addr->sin_addr.s_addr;
462 if(chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
463 sk->saddr = 0; /* Use device */
464 sk_dst_reset(sk);
465 return 0;
469 * This should be easy, if there is something there
470 * we return it, otherwise we block.
473 int raw_recvmsg(struct sock *sk, struct msghdr *msg, int len,
474 int noblock, int flags,int *addr_len)
476 int copied=0;
477 struct sk_buff *skb;
478 int err;
479 struct sockaddr_in *sin=(struct sockaddr_in *)msg->msg_name;
481 if (flags & MSG_OOB)
482 return -EOPNOTSUPP;
484 if (addr_len)
485 *addr_len=sizeof(*sin);
487 if (flags & MSG_ERRQUEUE)
488 return ip_recv_error(sk, msg, len);
490 skb=skb_recv_datagram(sk,flags,noblock,&err);
491 if(skb==NULL)
492 return err;
494 copied = skb->len;
495 if (len < copied)
497 msg->msg_flags |= MSG_TRUNC;
498 copied = len;
501 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
502 if (err)
503 goto done;
505 sk->stamp=skb->stamp;
507 /* Copy the address. */
508 if (sin) {
509 sin->sin_family = AF_INET;
510 sin->sin_addr.s_addr = skb->nh.iph->saddr;
512 if (sk->protinfo.af_inet.cmsg_flags)
513 ip_cmsg_recv(msg, skb);
514 done:
515 skb_free_datagram(sk, skb);
516 return (err ? : copied);
519 static int raw_init(struct sock *sk)
521 struct raw_opt *tp = &(sk->tp_pinfo.tp_raw4);
522 if (sk->num == IPPROTO_ICMP)
523 memset(&tp->filter, 0, sizeof(tp->filter));
524 return 0;
527 static int raw_seticmpfilter(struct sock *sk, char *optval, int optlen)
529 if (optlen > sizeof(struct icmp_filter))
530 optlen = sizeof(struct icmp_filter);
531 if (copy_from_user(&sk->tp_pinfo.tp_raw4.filter, optval, optlen))
532 return -EFAULT;
533 return 0;
536 static int raw_geticmpfilter(struct sock *sk, char *optval, int *optlen)
538 int len;
540 if (get_user(len,optlen))
541 return -EFAULT;
542 if (len > sizeof(struct icmp_filter))
543 len = sizeof(struct icmp_filter);
544 if (put_user(len, optlen))
545 return -EFAULT;
546 if (copy_to_user(optval, &sk->tp_pinfo.tp_raw4.filter, len))
547 return -EFAULT;
548 return 0;
551 static int raw_setsockopt(struct sock *sk, int level, int optname,
552 char *optval, int optlen)
554 if (level != SOL_RAW)
555 return ip_setsockopt(sk, level, optname, optval, optlen);
557 switch (optname) {
558 case ICMP_FILTER:
559 if (sk->num != IPPROTO_ICMP)
560 return -EOPNOTSUPP;
561 return raw_seticmpfilter(sk, optval, optlen);
564 return -ENOPROTOOPT;
567 static int raw_getsockopt(struct sock *sk, int level, int optname,
568 char *optval, int *optlen)
570 if (level != SOL_RAW)
571 return ip_getsockopt(sk, level, optname, optval, optlen);
573 switch (optname) {
574 case ICMP_FILTER:
575 if (sk->num != IPPROTO_ICMP)
576 return -EOPNOTSUPP;
577 return raw_geticmpfilter(sk, optval, optlen);
580 return -ENOPROTOOPT;
583 static void get_raw_sock(struct sock *sp, char *tmpbuf, int i)
585 unsigned int dest, src;
586 __u16 destp, srcp;
587 int timer_active;
588 unsigned long timer_expires;
590 dest = sp->daddr;
591 src = sp->rcv_saddr;
592 destp = 0;
593 srcp = sp->num;
594 timer_active = (sp->timer.prev != NULL) ? 2 : 0;
595 timer_expires = (timer_active == 2 ? sp->timer.expires : jiffies);
596 sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
597 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %ld %d %p",
598 i, src, srcp, dest, destp, sp->state,
599 atomic_read(&sp->wmem_alloc), atomic_read(&sp->rmem_alloc),
600 timer_active, timer_expires-jiffies, 0,
601 sp->socket->inode->i_uid, 0,
602 sp->socket ? sp->socket->inode->i_ino : 0,
603 atomic_read(&sp->refcnt), sp);
606 int raw_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
608 int len = 0, num = 0, i;
609 off_t pos = 0;
610 off_t begin;
611 char tmpbuf[129];
613 if (offset < 128)
614 len += sprintf(buffer, "%-127s\n",
615 " sl local_address rem_address st tx_queue "
616 "rx_queue tr tm->when retrnsmt uid timeout inode");
617 pos = 128;
618 read_lock(&raw_v4_lock);
619 for (i = 0; i < RAWV4_HTABLE_SIZE; i++) {
620 struct sock *sk;
622 for (sk = raw_v4_htable[i]; sk; sk = sk->next, num++) {
623 if (sk->family != PF_INET)
624 continue;
625 pos += 128;
626 if (pos < offset)
627 continue;
628 get_raw_sock(sk, tmpbuf, i);
629 len += sprintf(buffer+len, "%-127s\n", tmpbuf);
630 if(len >= length)
631 goto out;
634 out:
635 read_unlock(&raw_v4_lock);
636 begin = len - (pos - offset);
637 *start = buffer + begin;
638 len -= begin;
639 if(len > length)
640 len = length;
641 if (len < 0)
642 len = 0;
643 return len;
646 struct proto raw_prot = {
647 raw_close, /* close */
648 udp_connect, /* connect */
649 udp_disconnect, /* disconnect */
650 NULL, /* accept */
651 NULL, /* retransmit */
652 NULL, /* write_wakeup */
653 NULL, /* read_wakeup */
654 datagram_poll, /* poll */
655 #ifdef CONFIG_IP_MROUTE
656 ipmr_ioctl, /* ioctl */
657 #else
658 NULL, /* ioctl */
659 #endif
660 raw_init, /* init */
661 NULL, /* destroy */
662 NULL, /* shutdown */
663 raw_setsockopt, /* setsockopt */
664 raw_getsockopt, /* getsockopt */
665 raw_sendmsg, /* sendmsg */
666 raw_recvmsg, /* recvmsg */
667 raw_bind, /* bind */
668 raw_rcv_skb, /* backlog_rcv */
669 raw_v4_hash, /* hash */
670 raw_v4_unhash, /* unhash */
671 NULL, /* get_port */
672 128, /* max_header */
673 0, /* retransmits */
674 "RAW", /* name */
675 0, /* inuse */
676 0 /* highestinuse */