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.63.2.1 2002/03/05 12:47:34 davem Exp $
10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
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
18 * skbuff library. No more peek crashes,
20 * Alan Cox : Checks sk->broadcast.
21 * Alan Cox : Uses skb_free_datagram/skb_copy_datagram
22 * Alan Cox : Raw passes ip options too
23 * Alan Cox : Setsocketopt added
24 * Alan Cox : Fixed error return for broadcasts
25 * Alan Cox : Removed wake_up calls
26 * Alan Cox : Use ttl/tos
27 * Alan Cox : Cleaned up old debugging
28 * Alan Cox : Use new kernel side addresses
29 * Arnt Gulbrandsen : Fixed MSG_DONTROUTE in raw sockets.
30 * Alan Cox : BSD style RAW socket demultiplexing.
31 * Alan Cox : Beginnings of mrouted support.
32 * Alan Cox : Added IP_HDRINCL option.
33 * Alan Cox : Skip broadcast check if BSDism set.
34 * David S. Miller : New socket lookup architecture.
36 * This program is free software; you can redistribute it and/or
37 * modify it under the terms of the GNU General Public License
38 * as published by the Free Software Foundation; either version
39 * 2 of the License, or (at your option) any later version.
42 #include <linux/config.h>
43 #include <asm/system.h>
44 #include <asm/uaccess.h>
45 #include <asm/ioctls.h>
46 #include <linux/types.h>
47 #include <linux/sched.h>
48 #include <linux/errno.h>
49 #include <linux/timer.h>
51 #include <linux/kernel.h>
52 #include <linux/fcntl.h>
53 #include <linux/socket.h>
55 #include <linux/inet.h>
56 #include <linux/netdevice.h>
57 #include <linux/mroute.h>
59 #include <net/protocol.h>
60 #include <linux/skbuff.h>
65 #include <net/inet_common.h>
66 #include <net/checksum.h>
68 struct sock *raw_v4_htable[RAWV4_HTABLE_SIZE];
69 rwlock_t raw_v4_lock = RW_LOCK_UNLOCKED;
71 static void raw_v4_hash(struct sock *sk)
73 struct sock **skp = &raw_v4_htable[sk->num & (RAWV4_HTABLE_SIZE - 1)];
75 write_lock_bh(&raw_v4_lock);
76 if ((sk->next = *skp) != NULL)
77 (*skp)->pprev = &sk->next;
80 sock_prot_inc_use(sk->prot);
82 write_unlock_bh(&raw_v4_lock);
85 static void raw_v4_unhash(struct sock *sk)
87 write_lock_bh(&raw_v4_lock);
90 sk->next->pprev = sk->pprev;
91 *sk->pprev = sk->next;
93 sock_prot_dec_use(sk->prot);
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,
105 for (s = sk; s; s = s->next) {
107 !(s->daddr && s->daddr != raddr) &&
108 !(s->rcv_saddr && s->rcv_saddr != laddr) &&
109 !(s->bound_dev_if && s->bound_dev_if != dif))
119 static __inline__ int icmp_filter(struct sock *sk, struct sk_buff *skb)
123 type = skb->h.icmph->type;
125 __u32 data = sk->tp_pinfo.tp_raw4.filter.data;
127 return ((1 << type) & data) != 0;
130 /* Do not block unknown ICMP types */
134 /* IP input processing comes here for RAW socket delivery.
135 * This is fun as to avoid copies we want to make no surplus
138 * RFC 1122: SHOULD pass TOS value up to the transport layer.
139 * -> It does. And not only TOS, but all IP header.
141 struct sock *raw_v4_input(struct sk_buff *skb, struct iphdr *iph, int hash)
145 read_lock(&raw_v4_lock);
146 if ((sk = raw_v4_htable[hash]) == NULL)
148 sk = __raw_v4_lookup(sk, iph->protocol,
149 iph->saddr, iph->daddr,
153 struct sock *sknext = __raw_v4_lookup(sk->next, iph->protocol,
154 iph->saddr, iph->daddr,
156 if (iph->protocol != IPPROTO_ICMP ||
157 !icmp_filter(sk, skb)) {
158 struct sk_buff *clone;
162 clone = skb_clone(skb, GFP_ATOMIC);
163 /* Not releasing hash table! */
172 read_unlock(&raw_v4_lock);
177 void raw_err (struct sock *sk, struct sk_buff *skb, u32 info)
179 int type = skb->h.icmph->type;
180 int code = skb->h.icmph->code;
184 /* Report error on raw socket, if:
185 1. User requested ip_recverr.
186 2. Socket is connected (otherwise the error indication
187 is useless without ip_recverr and error is hard.
189 if (!sk->protinfo.af_inet.recverr && sk->state != TCP_ESTABLISHED)
194 case ICMP_TIME_EXCEEDED:
197 case ICMP_SOURCE_QUENCH:
199 case ICMP_PARAMETERPROB:
203 case ICMP_DEST_UNREACH:
205 if (code > NR_ICMP_UNREACH)
207 err = icmp_err_convert[code].errno;
208 harderr = icmp_err_convert[code].fatal;
209 if (code == ICMP_FRAG_NEEDED) {
210 harderr = sk->protinfo.af_inet.pmtudisc !=
216 if (sk->protinfo.af_inet.recverr) {
217 struct iphdr *iph = (struct iphdr*)skb->data;
218 u8 *payload = skb->data + (iph->ihl << 2);
220 if (sk->protinfo.af_inet.hdrincl)
222 ip_icmp_error(sk, skb, err, 0, info, payload);
225 if (sk->protinfo.af_inet.recverr || harderr) {
227 sk->error_report(sk);
231 static int raw_rcv_skb(struct sock * sk, struct sk_buff * skb)
233 /* Charge it to the socket. */
235 if (sock_queue_rcv_skb(sk, skb) < 0) {
236 IP_INC_STATS(IpInDiscards);
241 IP_INC_STATS(IpInDelivers);
242 return NET_RX_SUCCESS;
245 int raw_rcv(struct sock *sk, struct sk_buff *skb)
247 skb_push(skb, skb->data - skb->nh.raw);
249 raw_rcv_skb(sk, skb);
257 struct dst_entry *dst;
261 * Send a RAW IP packet.
265 * Callback support is trivial for SOCK_RAW
268 static int raw_getfrag(const void *p, char *to, unsigned int offset,
269 unsigned int fraglen, struct sk_buff *skb)
271 struct rawfakehdr *rfh = (struct rawfakehdr *) p;
272 return memcpy_fromiovecend(to, rfh->iov, offset, fraglen);
276 * IPPROTO_RAW needs extra work.
279 static int raw_getrawfrag(const void *p, char *to, unsigned int offset,
280 unsigned int fraglen, struct sk_buff *skb)
282 struct rawfakehdr *rfh = (struct rawfakehdr *) p;
284 if (memcpy_fromiovecend(to, rfh->iov, offset, fraglen))
288 struct iphdr *iph = (struct iphdr *)to;
290 iph->saddr = rfh->saddr;
292 iph->tot_len = htons(fraglen); /* This is right as you can't
295 * Deliberate breach of modularity to keep
296 * ip_build_xmit clean (well less messy).
299 ip_select_ident(iph, rfh->dst, NULL);
300 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
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;
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
327 if (len < 0 || len > 0xFFFF)
335 if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message */
336 goto out; /* compatibility */
339 * Get and verify the address.
342 if (msg->msg_namelen) {
343 struct sockaddr_in *usin = (struct sockaddr_in*)msg->msg_name;
345 if (msg->msg_namelen < sizeof(*usin))
347 if (usin->sin_family != AF_INET) {
348 static int complained;
350 printk(KERN_INFO "%s forgot to set AF_INET in "
351 "raw sendmsg. Fix it!\n",
354 if (usin->sin_family)
357 daddr = usin->sin_addr.s_addr;
358 /* ANK: I did not forget to get protocol from port field.
359 * I just do not know, who uses this weirdness.
360 * IP_HDRINCL is much more convenient.
364 if (sk->state != TCP_ESTABLISHED)
369 ipc.addr = sk->saddr;
371 ipc.oif = sk->bound_dev_if;
373 if (msg->msg_controllen) {
374 err = ip_cmsg_send(msg, &ipc);
381 rfh.saddr = ipc.addr;
385 ipc.opt = sk->protinfo.af_inet.opt;
389 /* Linux does not mangle headers on raw sockets,
390 * so that IP options + IP_HDRINCL is non-sense.
392 if (sk->protinfo.af_inet.hdrincl)
397 daddr = ipc.opt->faddr;
400 tos = RT_TOS(sk->protinfo.af_inet.tos) | sk->localroute;
401 if (msg->msg_flags & MSG_DONTROUTE)
404 if (MULTICAST(daddr)) {
406 ipc.oif = sk->protinfo.af_inet.mc_index;
408 rfh.saddr = sk->protinfo.af_inet.mc_addr;
411 err = ip_route_output(&rt, daddr, rfh.saddr, tos, ipc.oif);
417 if (rt->rt_flags & RTCF_BROADCAST && !sk->broadcast)
420 if (msg->msg_flags & MSG_CONFIRM)
424 rfh.iov = msg->msg_iov;
425 rfh.saddr = rt->rt_src;
426 rfh.dst = &rt->u.dst;
428 ipc.addr = rt->rt_dst;
429 err = ip_build_xmit(sk, sk->protinfo.af_inet.hdrincl ? raw_getrawfrag :
430 raw_getfrag, &rfh, len, &ipc, rt, msg->msg_flags);
437 out: return err < 0 ? err : len;
440 dst_confirm(&rt->u.dst);
441 if (!(msg->msg_flags & MSG_PROBE) || len)
442 goto back_from_confirm;
447 static void raw_close(struct sock *sk, long timeout)
450 * Raw sockets may have direct kernel refereneces. Kill them.
452 ip_ra_control(sk, 0, NULL);
454 inet_sock_release(sk);
457 /* This gets rid of all the nasties in af_inet. -DaveM */
458 static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
460 struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
464 if (sk->state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
466 chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
467 ret = -EADDRNOTAVAIL;
468 if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
469 chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
471 sk->rcv_saddr = sk->saddr = addr->sin_addr.s_addr;
472 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
473 sk->saddr = 0; /* Use device */
480 * This should be easy, if there is something there
481 * we return it, otherwise we block.
484 int raw_recvmsg(struct sock *sk, struct msghdr *msg, int len,
485 int noblock, int flags, int *addr_len)
488 int err = -EOPNOTSUPP;
489 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
496 *addr_len = sizeof(*sin);
498 if (flags & MSG_ERRQUEUE) {
499 err = ip_recv_error(sk, msg, len);
503 skb = skb_recv_datagram(sk, flags, noblock, &err);
509 msg->msg_flags |= MSG_TRUNC;
513 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
517 sock_recv_timestamp(msg, sk, skb);
519 /* Copy the address. */
521 sin->sin_family = AF_INET;
522 sin->sin_addr.s_addr = skb->nh.iph->saddr;
523 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
525 if (sk->protinfo.af_inet.cmsg_flags)
526 ip_cmsg_recv(msg, skb);
527 if (flags & MSG_TRUNC)
530 skb_free_datagram(sk, skb);
531 out: return err ? : copied;
534 static int raw_init(struct sock *sk)
536 struct raw_opt *tp = &(sk->tp_pinfo.tp_raw4);
537 if (sk->num == IPPROTO_ICMP)
538 memset(&tp->filter, 0, sizeof(tp->filter));
542 static int raw_seticmpfilter(struct sock *sk, char *optval, int optlen)
544 if (optlen > sizeof(struct icmp_filter))
545 optlen = sizeof(struct icmp_filter);
546 if (copy_from_user(&sk->tp_pinfo.tp_raw4.filter, optval, optlen))
551 static int raw_geticmpfilter(struct sock *sk, char *optval, int *optlen)
553 int len, ret = -EFAULT;
555 if (get_user(len, optlen))
560 if (len > sizeof(struct icmp_filter))
561 len = sizeof(struct icmp_filter);
563 if (put_user(len, optlen) ||
564 copy_to_user(optval, &sk->tp_pinfo.tp_raw4.filter, len))
570 static int raw_setsockopt(struct sock *sk, int level, int optname,
571 char *optval, int optlen)
573 if (level != SOL_RAW)
574 return ip_setsockopt(sk, level, optname, optval, optlen);
576 if (optname == ICMP_FILTER) {
577 if (sk->num != IPPROTO_ICMP)
580 return raw_seticmpfilter(sk, optval, optlen);
585 static int raw_getsockopt(struct sock *sk, int level, int optname,
586 char *optval, int *optlen)
588 if (level != SOL_RAW)
589 return ip_getsockopt(sk, level, optname, optval, optlen);
591 if (optname == ICMP_FILTER) {
592 if (sk->num != IPPROTO_ICMP)
595 return raw_geticmpfilter(sk, optval, optlen);
600 static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
604 int amount = atomic_read(&sk->wmem_alloc);
605 return put_user(amount, (int *)arg);
611 spin_lock_irq(&sk->receive_queue.lock);
612 skb = skb_peek(&sk->receive_queue);
615 spin_unlock_irq(&sk->receive_queue.lock);
616 return put_user(amount, (int *)arg);
620 #ifdef CONFIG_IP_MROUTE
621 return ipmr_ioctl(sk, cmd, arg);
628 static void get_raw_sock(struct sock *sp, char *tmpbuf, int i)
630 unsigned int dest = sp->daddr,
635 sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
636 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
637 i, src, srcp, dest, destp, sp->state,
638 atomic_read(&sp->wmem_alloc), atomic_read(&sp->rmem_alloc),
642 atomic_read(&sp->refcnt), sp);
645 int raw_get_info(char *buffer, char **start, off_t offset, int length)
647 int len = 0, num = 0, i;
653 len += sprintf(buffer, "%-127s\n",
654 " sl local_address rem_address st tx_queue "
655 "rx_queue tr tm->when retrnsmt uid timeout "
657 read_lock(&raw_v4_lock);
658 for (i = 0; i < RAWV4_HTABLE_SIZE; i++) {
661 for (sk = raw_v4_htable[i]; sk; sk = sk->next, num++) {
662 if (sk->family != PF_INET)
667 get_raw_sock(sk, tmpbuf, i);
668 len += sprintf(buffer + len, "%-127s\n", tmpbuf);
674 read_unlock(&raw_v4_lock);
675 begin = len - (pos - offset);
676 *start = buffer + begin;
685 struct proto raw_prot = {
688 connect: udp_connect,
689 disconnect: udp_disconnect,
692 setsockopt: raw_setsockopt,
693 getsockopt: raw_getsockopt,
694 sendmsg: raw_sendmsg,
695 recvmsg: raw_recvmsg,
697 backlog_rcv: raw_rcv_skb,
699 unhash: raw_v4_unhash,