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.
5 *
6 * RAW - implementation of IP "raw" sockets.
7 *
8 * Version: $Id: raw.c,v 1.56 2000/11/28 13:38:38 davem Exp $
9 *
10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 *
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.
34 *
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.
39 */
40
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>
65
66 struct sock *raw_v4_htable[RAWV4_HTABLE_SIZE];
67 rwlock_t raw_v4_lock = RW_LOCK_UNLOCKED;
68
69 static void raw_v4_hash(struct sock *sk)
70 {
71 struct sock **skp = &raw_v4_htable[sk->num & (RAWV4_HTABLE_SIZE - 1)];
72
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 sock_prot_inc_use(sk->prot);
79 sock_hold(sk);
80 write_unlock_bh(&raw_v4_lock);
81 }
82
83 static void raw_v4_unhash(struct sock *sk)
84 {
85 write_lock_bh(&raw_v4_lock);
86 if (sk->pprev) {
87 if (sk->next)
88 sk->next->pprev = sk->pprev;
89 *sk->pprev = sk->next;
90 sk->pprev = NULL;
91 sock_prot_dec_use(sk->prot);
92 __sock_put(sk);
93 }
94 write_unlock_bh(&raw_v4_lock);
95 }
96
97 struct sock *__raw_v4_lookup(struct sock *sk, unsigned short num,
98 unsigned long raddr, unsigned long laddr,
99 int dif)
100 {
101 struct sock *s = sk;
102
103 for(s = sk; s; s = s->next) {
104 if((s->num == num) &&
105 !(s->daddr && s->daddr != raddr) &&
106 !(s->rcv_saddr && s->rcv_saddr != laddr) &&
107 !(s->bound_dev_if && s->bound_dev_if != dif))
108 break; /* gotcha */
109 }
110 return s;
111 }
112
113 /*
114 * 0 - deliver
115 * 1 - block
116 */
117 static __inline__ int icmp_filter(struct sock *sk, struct sk_buff *skb)
118 {
119 int type;
120
121 type = skb->h.icmph->type;
122 if (type < 32)
123 return test_bit(type, &sk->tp_pinfo.tp_raw4.filter);
124
125 /* Do not block unknown ICMP types */
126 return 0;
127 }
128
129 /* IP input processing comes here for RAW socket delivery.
130 * This is fun as to avoid copies we want to make no surplus
131 * copies.
132 *
133 * RFC 1122: SHOULD pass TOS value up to the transport layer.
134 * -> It does. And not only TOS, but all IP header.
135 */
136 struct sock *raw_v4_input(struct sk_buff *skb, struct iphdr *iph, int hash)
137 {
138 struct sock *sk;
139
140 read_lock(&raw_v4_lock);
141 if ((sk = raw_v4_htable[hash]) == NULL)
142 goto out;
143 sk = __raw_v4_lookup(sk, iph->protocol,
144 iph->saddr, iph->daddr,
145 skb->dev->ifindex);
146
147 while(sk != NULL) {
148 struct sock *sknext = __raw_v4_lookup(sk->next, iph->protocol,
149 iph->saddr, iph->daddr,
150 skb->dev->ifindex);
151 if (iph->protocol != IPPROTO_ICMP ||
152 ! icmp_filter(sk, skb)) {
153 struct sk_buff *clone;
154
155 if(sknext == NULL)
156 break;
157 clone = skb_clone(skb, GFP_ATOMIC);
158 /* Not releasing hash table! */
159 if(clone)
160 raw_rcv(sk, clone);
161 }
162 sk = sknext;
163 }
164 out:
165 if (sk)
166 sock_hold(sk);
167 read_unlock(&raw_v4_lock);
168
169 return sk;
170 }
171
172 void raw_err (struct sock *sk, struct sk_buff *skb)
173 {
174 int type = skb->h.icmph->type;
175 int code = skb->h.icmph->code;
176 u32 info = 0;
177 int err = 0;
178 int harderr = 0;
179
180 /* Report error on raw socket, if:
181 1. User requested ip_recverr.
182 2. Socket is connected (otherwise the error indication
183 is useless without ip_recverr and error is hard.
184 */
185 if (!sk->protinfo.af_inet.recverr && sk->state != TCP_ESTABLISHED)
186 return;
187
188 switch (type) {
189 default:
190 case ICMP_TIME_EXCEEDED:
191 err = EHOSTUNREACH;
192 break;
193 case ICMP_SOURCE_QUENCH:
194 return;
195 case ICMP_PARAMETERPROB:
196 err = EPROTO;
197 info = ntohl(skb->h.icmph->un.gateway)>>24;
198 harderr = 1;
199 break;
200 case ICMP_DEST_UNREACH:
201 err = EHOSTUNREACH;
202 if (code > NR_ICMP_UNREACH)
203 break;
204 err = icmp_err_convert[code].errno;
205 harderr = icmp_err_convert[code].fatal;
206 if (code == ICMP_FRAG_NEEDED) {
207 harderr = (sk->protinfo.af_inet.pmtudisc != IP_PMTUDISC_DONT);
208 err = EMSGSIZE;
209 info = ntohs(skb->h.icmph->un.frag.mtu);
210 }
211 }
212
213 if (sk->protinfo.af_inet.recverr)
214 ip_icmp_error(sk, skb, err, 0, info, (u8 *)(skb->h.icmph + 1));
215
216 if (sk->protinfo.af_inet.recverr || harderr) {
217 sk->err = err;
218 sk->error_report(sk);
219 }
220 }
221
222 static int raw_rcv_skb(struct sock * sk, struct sk_buff * skb)
223 {
224 /* Charge it to the socket. */
225
226 if (sock_queue_rcv_skb(sk,skb)<0)
227 {
228 IP_INC_STATS(IpInDiscards);
229 kfree_skb(skb);
230 return NET_RX_DROP;
231 }
232
233 IP_INC_STATS(IpInDelivers);
234 return NET_RX_SUCCESS;
235 }
236
237 /*
238 * This should be the easiest of all, all we do is
239 * copy it into a buffer. All demultiplexing is done
240 * in ip.c
241 */
242
243 int raw_rcv(struct sock *sk, struct sk_buff *skb)
244 {
245 /* Now we need to copy this into memory. */
246 skb_trim(skb, ntohs(skb->nh.iph->tot_len));
247
248 skb->h.raw = skb->nh.raw;
249
250 raw_rcv_skb(sk, skb);
251 return 0;
252 }
253
254 struct rawfakehdr
255 {
256 struct iovec *iov;
257 u32 saddr;
258 struct dst_entry *dst;
259 };
260
261 /*
262 * Send a RAW IP packet.
263 */
264
265 /*
266 * Callback support is trivial for SOCK_RAW
267 */
268
269 static int raw_getfrag(const void *p, char *to, unsigned int offset, unsigned int fraglen)
270 {
271 struct rawfakehdr *rfh = (struct rawfakehdr *) p;
272 return memcpy_fromiovecend(to, rfh->iov, offset, fraglen);
273 }
274
275 /*
276 * IPPROTO_RAW needs extra work.
277 */
278
279 static int raw_getrawfrag(const void *p, char *to, unsigned int offset, unsigned int fraglen)
280 {
281 struct rawfakehdr *rfh = (struct rawfakehdr *) p;
282
283 if (memcpy_fromiovecend(to, rfh->iov, offset, fraglen))
284 return -EFAULT;
285
286 if (offset==0) {
287 struct iphdr *iph = (struct iphdr *)to;
288 if (!iph->saddr)
289 iph->saddr = rfh->saddr;
290 iph->check=0;
291 iph->tot_len=htons(fraglen); /* This is right as you can't frag
292 RAW packets */
293 /*
294 * Deliberate breach of modularity to keep
295 * ip_build_xmit clean (well less messy).
296 */
297 if (!iph->id)
298 ip_select_ident(iph, rfh->dst);
299 iph->check=ip_fast_csum((unsigned char *)iph, iph->ihl);
300 }
301 return 0;
302 }
303
304 static int raw_sendmsg(struct sock *sk, struct msghdr *msg, int len)
305 {
306 struct ipcm_cookie ipc;
307 struct rawfakehdr rfh;
308 struct rtable *rt = NULL;
309 int free = 0;
310 u32 daddr;
311 u8 tos;
312 int err;
313
314 /* This check is ONLY to check for arithmetic overflow
315 on integer(!) len. Not more! Real check will be made
316 in ip_build_xmit --ANK
317
318 BTW socket.c -> af_*.c -> ... make multiple
319 invalid conversions size_t -> int. We MUST repair it f.e.
320 by replacing all of them with size_t and revise all
321 the places sort of len += sizeof(struct iphdr)
322 If len was ULONG_MAX-10 it would be cathastrophe --ANK
323 */
324
325 if (len < 0 || len > 0xFFFF)
326 return -EMSGSIZE;
327
328 /*
329 * Check the flags.
330 */
331
332 if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
333 return -EOPNOTSUPP;
334
335 /*
336 * Get and verify the address.
337 */
338
339 if (msg->msg_namelen) {
340 struct sockaddr_in *usin = (struct sockaddr_in*)msg->msg_name;
341 if (msg->msg_namelen < sizeof(*usin))
342 return(-EINVAL);
343 if (usin->sin_family != AF_INET) {
344 static int complained;
345 if (!complained++)
346 printk(KERN_INFO "%s forgot to set AF_INET in raw sendmsg. Fix it!\n", current->comm);
347 if (usin->sin_family)
348 return -EINVAL;
349 }
350 daddr = usin->sin_addr.s_addr;
351 /* ANK: I did not forget to get protocol from port field.
352 * I just do not know, who uses this weirdness.
353 * IP_HDRINCL is much more convenient.
354 */
355 } else {
356 if (sk->state != TCP_ESTABLISHED)
357 return(-EINVAL);
358 daddr = sk->daddr;
359 }
360
361 ipc.addr = sk->saddr;
362 ipc.opt = NULL;
363 ipc.oif = sk->bound_dev_if;
364
365 if (msg->msg_controllen) {
366 int tmp = ip_cmsg_send(msg, &ipc);
367 if (tmp)
368 return tmp;
369 if (ipc.opt)
370 free=1;
371 }
372
373 rfh.saddr = ipc.addr;
374 ipc.addr = daddr;
375
376 if (!ipc.opt)
377 ipc.opt = sk->protinfo.af_inet.opt;
378
379 if (ipc.opt) {
380 err = -EINVAL;
381 /* Linux does not mangle headers on raw sockets,
382 * so that IP options + IP_HDRINCL is non-sense.
383 */
384 if (sk->protinfo.af_inet.hdrincl)
385 goto done;
386 if (ipc.opt->srr) {
387 if (!daddr)
388 goto done;
389 daddr = ipc.opt->faddr;
390 }
391 }
392 tos = RT_TOS(sk->protinfo.af_inet.tos) | sk->localroute;
393 if (msg->msg_flags&MSG_DONTROUTE)
394 tos |= RTO_ONLINK;
395
396 if (MULTICAST(daddr)) {
397 if (!ipc.oif)
398 ipc.oif = sk->protinfo.af_inet.mc_index;
399 if (!rfh.saddr)
400 rfh.saddr = sk->protinfo.af_inet.mc_addr;
401 }
402
403 err = ip_route_output(&rt, daddr, rfh.saddr, tos, ipc.oif);
404
405 if (err)
406 goto done;
407
408 err = -EACCES;
409 if (rt->rt_flags&RTCF_BROADCAST && !sk->broadcast)
410 goto done;
411
412 if (msg->msg_flags&MSG_CONFIRM)
413 goto do_confirm;
414 back_from_confirm:
415
416 rfh.iov = msg->msg_iov;
417 rfh.saddr = rt->rt_src;
418 rfh.dst = &rt->u.dst;
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);
423
424 done:
425 if (free)
426 kfree(ipc.opt);
427 ip_rt_put(rt);
428
429 return err<0 ? err : len;
430
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;
437 }
438
439 static void raw_close(struct sock *sk, long timeout)
440 {
441 /*
442 * Raw sockets may have direct kernel refereneces. Kill them.
443 */
444 ip_ra_control(sk, 0, NULL);
445
446 inet_sock_release(sk);
447 }
448
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)
451 {
452 struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
453 int chk_addr_ret;
454
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;
466 }
467
468 /*
469 * This should be easy, if there is something there
470 * we return it, otherwise we block.
471 */
472
473 int raw_recvmsg(struct sock *sk, struct msghdr *msg, int len,
474 int noblock, int flags,int *addr_len)
475 {
476 int copied=0;
477 struct sk_buff *skb;
478 int err;
479 struct sockaddr_in *sin=(struct sockaddr_in *)msg->msg_name;
480
481 if (flags & MSG_OOB)
482 return -EOPNOTSUPP;
483
484 if (addr_len)
485 *addr_len=sizeof(*sin);
486
487 if (flags & MSG_ERRQUEUE)
488 return ip_recv_error(sk, msg, len);
489
490 skb=skb_recv_datagram(sk,flags,noblock,&err);
491 if(skb==NULL)
492 return err;
493
494 copied = skb->len;
495 if (len < copied)
496 {
497 msg->msg_flags |= MSG_TRUNC;
498 copied = len;
499 }
500
501 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
502 if (err)
503 goto done;
504
505 sock_recv_timestamp(msg, sk, skb);
506
507 /* Copy the address. */
508 if (sin) {
509 sin->sin_family = AF_INET;
510 sin->sin_addr.s_addr = skb->nh.iph->saddr;
511 }
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);
517 }
518
519 static int raw_init(struct sock *sk)
520 {
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;
525 }
526
527 static int raw_seticmpfilter(struct sock *sk, char *optval, int optlen)
528 {
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;
534 }
535
536 static int raw_geticmpfilter(struct sock *sk, char *optval, int *optlen)
537 {
538 int len;
539
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;
549 }
550
551 static int raw_setsockopt(struct sock *sk, int level, int optname,
552 char *optval, int optlen)
553 {
554 if (level != SOL_RAW)
555 return ip_setsockopt(sk, level, optname, optval, optlen);
556
557 switch (optname) {
558 case ICMP_FILTER:
559 if (sk->num != IPPROTO_ICMP)
560 return -EOPNOTSUPP;
561 return raw_seticmpfilter(sk, optval, optlen);
562 };
563
564 return -ENOPROTOOPT;
565 }
566
567 static int raw_getsockopt(struct sock *sk, int level, int optname,
568 char *optval, int *optlen)
569 {
570 if (level != SOL_RAW)
571 return ip_getsockopt(sk, level, optname, optval, optlen);
572
573 switch (optname) {
574 case ICMP_FILTER:
575 if (sk->num != IPPROTO_ICMP)
576 return -EOPNOTSUPP;
577 return raw_geticmpfilter(sk, optval, optlen);
578 };
579
580 return -ENOPROTOOPT;
581 }
582
583 static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
584 {
585 switch(cmd) {
586 case SIOCOUTQ:
587 {
588 int amount = atomic_read(&sk->wmem_alloc);
589 return put_user(amount, (int *)arg);
590 }
591 case SIOCINQ:
592 {
593 struct sk_buff *skb;
594 int amount = 0;
595
596 spin_lock_irq(&sk->receive_queue.lock);
597 skb = skb_peek(&sk->receive_queue);
598 if (skb != NULL)
599 amount = skb->len;
600 spin_unlock_irq(&sk->receive_queue.lock);
601 return put_user(amount, (int *)arg);
602 }
603
604 default:
605 #ifdef CONFIG_IP_MROUTE
606 return ipmr_ioctl(sk, cmd, arg);
607 #else
608 return -ENOIOCTLCMD;
609 #endif
610 }
611 }
612
613 static void get_raw_sock(struct sock *sp, char *tmpbuf, int i)
614 {
615 unsigned int dest, src;
616 __u16 destp, srcp;
617
618 dest = sp->daddr;
619 src = sp->rcv_saddr;
620 destp = 0;
621 srcp = sp->num;
622 sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
623 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %ld %d %p",
624 i, src, srcp, dest, destp, sp->state,
625 atomic_read(&sp->wmem_alloc), atomic_read(&sp->rmem_alloc),
626 0, 0L, 0,
627 sock_i_uid(sp), 0,
628 sock_i_ino(sp),
629 atomic_read(&sp->refcnt), sp);
630 }
631
632 int raw_get_info(char *buffer, char **start, off_t offset, int length)
633 {
634 int len = 0, num = 0, i;
635 off_t pos = 0;
636 off_t begin;
637 char tmpbuf[129];
638
639 if (offset < 128)
640 len += sprintf(buffer, "%-127s\n",
641 " sl local_address rem_address st tx_queue "
642 "rx_queue tr tm->when retrnsmt uid timeout inode");
643 pos = 128;
644 read_lock(&raw_v4_lock);
645 for (i = 0; i < RAWV4_HTABLE_SIZE; i++) {
646 struct sock *sk;
647
648 for (sk = raw_v4_htable[i]; sk; sk = sk->next, num++) {
649 if (sk->family != PF_INET)
650 continue;
651 pos += 128;
652 if (pos <= offset)
653 continue;
654 get_raw_sock(sk, tmpbuf, i);
655 len += sprintf(buffer+len, "%-127s\n", tmpbuf);
656 if(len >= length)
657 goto out;
658 }
659 }
660 out:
661 read_unlock(&raw_v4_lock);
662 begin = len - (pos - offset);
663 *start = buffer + begin;
664 len -= begin;
665 if(len > length)
666 len = length;
667 if (len < 0)
668 len = 0;
669 return len;
670 }
671
672 struct proto raw_prot = {
673 name: "RAW",
674 close: raw_close,
675 connect: udp_connect,
676 disconnect: udp_disconnect,
677 ioctl: raw_ioctl,
678 init: raw_init,
679 setsockopt: raw_setsockopt,
680 getsockopt: raw_getsockopt,
681 sendmsg: raw_sendmsg,
682 recvmsg: raw_recvmsg,
683 bind: raw_bind,
684 backlog_rcv: raw_rcv_skb,
685 hash: raw_v4_hash,
686 unhash: raw_v4_unhash,
687 };
688
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.