[IPv4] RAW: Compact the API for the kernel
[safe/jmp/linux-2.6] / net / ipv4 / raw.c
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.64 2002/02/01 22:01:04 davem Exp $
9  *
10  * Authors:     Ross Biro
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
18  *                                      skbuff library. No more peek crashes,
19  *                                      no more backlogs
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.
35  *
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.
40  */
41
42 #include <linux/types.h>
43 #include <asm/atomic.h>
44 #include <asm/byteorder.h>
45 #include <asm/current.h>
46 #include <asm/uaccess.h>
47 #include <asm/ioctls.h>
48 #include <linux/stddef.h>
49 #include <linux/slab.h>
50 #include <linux/errno.h>
51 #include <linux/aio.h>
52 #include <linux/kernel.h>
53 #include <linux/spinlock.h>
54 #include <linux/sockios.h>
55 #include <linux/socket.h>
56 #include <linux/in.h>
57 #include <linux/mroute.h>
58 #include <linux/netdevice.h>
59 #include <linux/in_route.h>
60 #include <linux/route.h>
61 #include <linux/skbuff.h>
62 #include <net/net_namespace.h>
63 #include <net/dst.h>
64 #include <net/sock.h>
65 #include <linux/gfp.h>
66 #include <linux/ip.h>
67 #include <linux/net.h>
68 #include <net/ip.h>
69 #include <net/icmp.h>
70 #include <net/udp.h>
71 #include <net/raw.h>
72 #include <net/snmp.h>
73 #include <net/tcp_states.h>
74 #include <net/inet_common.h>
75 #include <net/checksum.h>
76 #include <net/xfrm.h>
77 #include <linux/rtnetlink.h>
78 #include <linux/proc_fs.h>
79 #include <linux/seq_file.h>
80 #include <linux/netfilter.h>
81 #include <linux/netfilter_ipv4.h>
82
83 #define RAWV4_HTABLE_SIZE       MAX_INET_PROTOS
84
85 static struct hlist_head raw_v4_htable[RAWV4_HTABLE_SIZE];
86 static DEFINE_RWLOCK(raw_v4_lock);
87
88 static void raw_v4_hash(struct sock *sk)
89 {
90         struct hlist_head *head = &raw_v4_htable[inet_sk(sk)->num &
91                                                  (RAWV4_HTABLE_SIZE - 1)];
92
93         write_lock_bh(&raw_v4_lock);
94         sk_add_node(sk, head);
95         sock_prot_inc_use(sk->sk_prot);
96         write_unlock_bh(&raw_v4_lock);
97 }
98
99 static void raw_v4_unhash(struct sock *sk)
100 {
101         write_lock_bh(&raw_v4_lock);
102         if (sk_del_node_init(sk))
103                 sock_prot_dec_use(sk->sk_prot);
104         write_unlock_bh(&raw_v4_lock);
105 }
106
107 static struct sock *__raw_v4_lookup(struct sock *sk, unsigned short num,
108                              __be32 raddr, __be32 laddr,
109                              int dif)
110 {
111         struct hlist_node *node;
112
113         sk_for_each_from(sk, node) {
114                 struct inet_sock *inet = inet_sk(sk);
115
116                 if (inet->num == num                                    &&
117                     !(inet->daddr && inet->daddr != raddr)              &&
118                     !(inet->rcv_saddr && inet->rcv_saddr != laddr)      &&
119                     !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
120                         goto found; /* gotcha */
121         }
122         sk = NULL;
123 found:
124         return sk;
125 }
126
127 /*
128  *      0 - deliver
129  *      1 - block
130  */
131 static __inline__ int icmp_filter(struct sock *sk, struct sk_buff *skb)
132 {
133         int type;
134
135         if (!pskb_may_pull(skb, sizeof(struct icmphdr)))
136                 return 1;
137
138         type = icmp_hdr(skb)->type;
139         if (type < 32) {
140                 __u32 data = raw_sk(sk)->filter.data;
141
142                 return ((1 << type) & data) != 0;
143         }
144
145         /* Do not block unknown ICMP types */
146         return 0;
147 }
148
149 /* IP input processing comes here for RAW socket delivery.
150  * Caller owns SKB, so we must make clones.
151  *
152  * RFC 1122: SHOULD pass TOS value up to the transport layer.
153  * -> It does. And not only TOS, but all IP header.
154  */
155 static int raw_v4_input(struct sk_buff *skb, struct iphdr *iph, int hash)
156 {
157         struct sock *sk;
158         struct hlist_head *head;
159         int delivered = 0;
160
161         read_lock(&raw_v4_lock);
162         head = &raw_v4_htable[hash];
163         if (hlist_empty(head))
164                 goto out;
165         sk = __raw_v4_lookup(__sk_head(head), iph->protocol,
166                              iph->saddr, iph->daddr,
167                              skb->dev->ifindex);
168
169         while (sk) {
170                 delivered = 1;
171                 if (iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) {
172                         struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
173
174                         /* Not releasing hash table! */
175                         if (clone)
176                                 raw_rcv(sk, clone);
177                 }
178                 sk = __raw_v4_lookup(sk_next(sk), iph->protocol,
179                                      iph->saddr, iph->daddr,
180                                      skb->dev->ifindex);
181         }
182 out:
183         read_unlock(&raw_v4_lock);
184         return delivered;
185 }
186
187 int raw_local_deliver(struct sk_buff *skb, int protocol)
188 {
189         int hash;
190         struct sock *raw_sk;
191
192         hash = protocol & (RAWV4_HTABLE_SIZE - 1);
193         raw_sk = sk_head(&raw_v4_htable[hash]);
194
195         /* If there maybe a raw socket we must check - if not we
196          * don't care less
197          */
198         if (raw_sk && !raw_v4_input(skb, ip_hdr(skb), hash))
199                 raw_sk = NULL;
200
201         return raw_sk != NULL;
202
203 }
204
205 static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)
206 {
207         struct inet_sock *inet = inet_sk(sk);
208         const int type = icmp_hdr(skb)->type;
209         const int code = icmp_hdr(skb)->code;
210         int err = 0;
211         int harderr = 0;
212
213         /* Report error on raw socket, if:
214            1. User requested ip_recverr.
215            2. Socket is connected (otherwise the error indication
216               is useless without ip_recverr and error is hard.
217          */
218         if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED)
219                 return;
220
221         switch (type) {
222         default:
223         case ICMP_TIME_EXCEEDED:
224                 err = EHOSTUNREACH;
225                 break;
226         case ICMP_SOURCE_QUENCH:
227                 return;
228         case ICMP_PARAMETERPROB:
229                 err = EPROTO;
230                 harderr = 1;
231                 break;
232         case ICMP_DEST_UNREACH:
233                 err = EHOSTUNREACH;
234                 if (code > NR_ICMP_UNREACH)
235                         break;
236                 err = icmp_err_convert[code].errno;
237                 harderr = icmp_err_convert[code].fatal;
238                 if (code == ICMP_FRAG_NEEDED) {
239                         harderr = inet->pmtudisc != IP_PMTUDISC_DONT;
240                         err = EMSGSIZE;
241                 }
242         }
243
244         if (inet->recverr) {
245                 struct iphdr *iph = (struct iphdr*)skb->data;
246                 u8 *payload = skb->data + (iph->ihl << 2);
247
248                 if (inet->hdrincl)
249                         payload = skb->data;
250                 ip_icmp_error(sk, skb, err, 0, info, payload);
251         }
252
253         if (inet->recverr || harderr) {
254                 sk->sk_err = err;
255                 sk->sk_error_report(sk);
256         }
257 }
258
259 void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info)
260 {
261         int hash;
262         struct sock *raw_sk;
263         struct iphdr *iph;
264
265         hash = protocol & (RAWV4_HTABLE_SIZE - 1);
266
267         read_lock(&raw_v4_lock);
268         raw_sk = sk_head(&raw_v4_htable[hash]);
269         if (raw_sk != NULL) {
270                 iph = (struct iphdr *)skb->data;
271                 while ((raw_sk = __raw_v4_lookup(raw_sk, protocol, iph->daddr,
272                                                 iph->saddr,
273                                                 skb->dev->ifindex)) != NULL) {
274                         raw_err(raw_sk, skb, info);
275                         raw_sk = sk_next(raw_sk);
276                         iph = (struct iphdr *)skb->data;
277                 }
278         }
279         read_unlock(&raw_v4_lock);
280 }
281
282 static int raw_rcv_skb(struct sock * sk, struct sk_buff * skb)
283 {
284         /* Charge it to the socket. */
285
286         if (sock_queue_rcv_skb(sk, skb) < 0) {
287                 atomic_inc(&sk->sk_drops);
288                 kfree_skb(skb);
289                 return NET_RX_DROP;
290         }
291
292         return NET_RX_SUCCESS;
293 }
294
295 int raw_rcv(struct sock *sk, struct sk_buff *skb)
296 {
297         if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
298                 atomic_inc(&sk->sk_drops);
299                 kfree_skb(skb);
300                 return NET_RX_DROP;
301         }
302         nf_reset(skb);
303
304         skb_push(skb, skb->data - skb_network_header(skb));
305
306         raw_rcv_skb(sk, skb);
307         return 0;
308 }
309
310 static int raw_send_hdrinc(struct sock *sk, void *from, size_t length,
311                         struct rtable *rt,
312                         unsigned int flags)
313 {
314         struct inet_sock *inet = inet_sk(sk);
315         int hh_len;
316         struct iphdr *iph;
317         struct sk_buff *skb;
318         unsigned int iphlen;
319         int err;
320
321         if (length > rt->u.dst.dev->mtu) {
322                 ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->dport,
323                                rt->u.dst.dev->mtu);
324                 return -EMSGSIZE;
325         }
326         if (flags&MSG_PROBE)
327                 goto out;
328
329         hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
330
331         skb = sock_alloc_send_skb(sk, length+hh_len+15,
332                                   flags&MSG_DONTWAIT, &err);
333         if (skb == NULL)
334                 goto error;
335         skb_reserve(skb, hh_len);
336
337         skb->priority = sk->sk_priority;
338         skb->dst = dst_clone(&rt->u.dst);
339
340         skb_reset_network_header(skb);
341         iph = ip_hdr(skb);
342         skb_put(skb, length);
343
344         skb->ip_summed = CHECKSUM_NONE;
345
346         skb->transport_header = skb->network_header;
347         err = memcpy_fromiovecend((void *)iph, from, 0, length);
348         if (err)
349                 goto error_fault;
350
351         /* We don't modify invalid header */
352         iphlen = iph->ihl * 4;
353         if (iphlen >= sizeof(*iph) && iphlen <= length) {
354                 if (!iph->saddr)
355                         iph->saddr = rt->rt_src;
356                 iph->check   = 0;
357                 iph->tot_len = htons(length);
358                 if (!iph->id)
359                         ip_select_ident(iph, &rt->u.dst, NULL);
360
361                 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
362         }
363         if (iph->protocol == IPPROTO_ICMP)
364                 icmp_out_count(((struct icmphdr *)
365                         skb_transport_header(skb))->type);
366
367         err = NF_HOOK(PF_INET, NF_INET_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
368                       dst_output);
369         if (err > 0)
370                 err = inet->recverr ? net_xmit_errno(err) : 0;
371         if (err)
372                 goto error;
373 out:
374         return 0;
375
376 error_fault:
377         err = -EFAULT;
378         kfree_skb(skb);
379 error:
380         IP_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
381         return err;
382 }
383
384 static int raw_probe_proto_opt(struct flowi *fl, struct msghdr *msg)
385 {
386         struct iovec *iov;
387         u8 __user *type = NULL;
388         u8 __user *code = NULL;
389         int probed = 0;
390         unsigned int i;
391
392         if (!msg->msg_iov)
393                 return 0;
394
395         for (i = 0; i < msg->msg_iovlen; i++) {
396                 iov = &msg->msg_iov[i];
397                 if (!iov)
398                         continue;
399
400                 switch (fl->proto) {
401                 case IPPROTO_ICMP:
402                         /* check if one-byte field is readable or not. */
403                         if (iov->iov_base && iov->iov_len < 1)
404                                 break;
405
406                         if (!type) {
407                                 type = iov->iov_base;
408                                 /* check if code field is readable or not. */
409                                 if (iov->iov_len > 1)
410                                         code = type + 1;
411                         } else if (!code)
412                                 code = iov->iov_base;
413
414                         if (type && code) {
415                                 if (get_user(fl->fl_icmp_type, type) ||
416                                     get_user(fl->fl_icmp_code, code))
417                                         return -EFAULT;
418                                 probed = 1;
419                         }
420                         break;
421                 default:
422                         probed = 1;
423                         break;
424                 }
425                 if (probed)
426                         break;
427         }
428         return 0;
429 }
430
431 static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
432                        size_t len)
433 {
434         struct inet_sock *inet = inet_sk(sk);
435         struct ipcm_cookie ipc;
436         struct rtable *rt = NULL;
437         int free = 0;
438         __be32 daddr;
439         __be32 saddr;
440         u8  tos;
441         int err;
442
443         err = -EMSGSIZE;
444         if (len > 0xFFFF)
445                 goto out;
446
447         /*
448          *      Check the flags.
449          */
450
451         err = -EOPNOTSUPP;
452         if (msg->msg_flags & MSG_OOB)   /* Mirror BSD error message */
453                 goto out;               /* compatibility */
454
455         /*
456          *      Get and verify the address.
457          */
458
459         if (msg->msg_namelen) {
460                 struct sockaddr_in *usin = (struct sockaddr_in*)msg->msg_name;
461                 err = -EINVAL;
462                 if (msg->msg_namelen < sizeof(*usin))
463                         goto out;
464                 if (usin->sin_family != AF_INET) {
465                         static int complained;
466                         if (!complained++)
467                                 printk(KERN_INFO "%s forgot to set AF_INET in "
468                                                  "raw sendmsg. Fix it!\n",
469                                                  current->comm);
470                         err = -EAFNOSUPPORT;
471                         if (usin->sin_family)
472                                 goto out;
473                 }
474                 daddr = usin->sin_addr.s_addr;
475                 /* ANK: I did not forget to get protocol from port field.
476                  * I just do not know, who uses this weirdness.
477                  * IP_HDRINCL is much more convenient.
478                  */
479         } else {
480                 err = -EDESTADDRREQ;
481                 if (sk->sk_state != TCP_ESTABLISHED)
482                         goto out;
483                 daddr = inet->daddr;
484         }
485
486         ipc.addr = inet->saddr;
487         ipc.opt = NULL;
488         ipc.oif = sk->sk_bound_dev_if;
489
490         if (msg->msg_controllen) {
491                 err = ip_cmsg_send(msg, &ipc);
492                 if (err)
493                         goto out;
494                 if (ipc.opt)
495                         free = 1;
496         }
497
498         saddr = ipc.addr;
499         ipc.addr = daddr;
500
501         if (!ipc.opt)
502                 ipc.opt = inet->opt;
503
504         if (ipc.opt) {
505                 err = -EINVAL;
506                 /* Linux does not mangle headers on raw sockets,
507                  * so that IP options + IP_HDRINCL is non-sense.
508                  */
509                 if (inet->hdrincl)
510                         goto done;
511                 if (ipc.opt->srr) {
512                         if (!daddr)
513                                 goto done;
514                         daddr = ipc.opt->faddr;
515                 }
516         }
517         tos = RT_CONN_FLAGS(sk);
518         if (msg->msg_flags & MSG_DONTROUTE)
519                 tos |= RTO_ONLINK;
520
521         if (MULTICAST(daddr)) {
522                 if (!ipc.oif)
523                         ipc.oif = inet->mc_index;
524                 if (!saddr)
525                         saddr = inet->mc_addr;
526         }
527
528         {
529                 struct flowi fl = { .oif = ipc.oif,
530                                     .nl_u = { .ip4_u =
531                                               { .daddr = daddr,
532                                                 .saddr = saddr,
533                                                 .tos = tos } },
534                                     .proto = inet->hdrincl ? IPPROTO_RAW :
535                                                              sk->sk_protocol,
536                                   };
537                 if (!inet->hdrincl) {
538                         err = raw_probe_proto_opt(&fl, msg);
539                         if (err)
540                                 goto done;
541                 }
542
543                 security_sk_classify_flow(sk, &fl);
544                 err = ip_route_output_flow(&rt, &fl, sk, 1);
545         }
546         if (err)
547                 goto done;
548
549         err = -EACCES;
550         if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
551                 goto done;
552
553         if (msg->msg_flags & MSG_CONFIRM)
554                 goto do_confirm;
555 back_from_confirm:
556
557         if (inet->hdrincl)
558                 err = raw_send_hdrinc(sk, msg->msg_iov, len,
559                                         rt, msg->msg_flags);
560
561          else {
562                 if (!ipc.addr)
563                         ipc.addr = rt->rt_dst;
564                 lock_sock(sk);
565                 err = ip_append_data(sk, ip_generic_getfrag, msg->msg_iov, len, 0,
566                                         &ipc, rt, msg->msg_flags);
567                 if (err)
568                         ip_flush_pending_frames(sk);
569                 else if (!(msg->msg_flags & MSG_MORE))
570                         err = ip_push_pending_frames(sk);
571                 release_sock(sk);
572         }
573 done:
574         if (free)
575                 kfree(ipc.opt);
576         ip_rt_put(rt);
577
578 out:
579         if (err < 0)
580                 return err;
581         return len;
582
583 do_confirm:
584         dst_confirm(&rt->u.dst);
585         if (!(msg->msg_flags & MSG_PROBE) || len)
586                 goto back_from_confirm;
587         err = 0;
588         goto done;
589 }
590
591 static void raw_close(struct sock *sk, long timeout)
592 {
593         /*
594          * Raw sockets may have direct kernel refereneces. Kill them.
595          */
596         ip_ra_control(sk, 0, NULL);
597
598         sk_common_release(sk);
599 }
600
601 /* This gets rid of all the nasties in af_inet. -DaveM */
602 static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
603 {
604         struct inet_sock *inet = inet_sk(sk);
605         struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
606         int ret = -EINVAL;
607         int chk_addr_ret;
608
609         if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
610                 goto out;
611         chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
612         ret = -EADDRNOTAVAIL;
613         if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
614             chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
615                 goto out;
616         inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr;
617         if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
618                 inet->saddr = 0;  /* Use device */
619         sk_dst_reset(sk);
620         ret = 0;
621 out:    return ret;
622 }
623
624 /*
625  *      This should be easy, if there is something there
626  *      we return it, otherwise we block.
627  */
628
629 static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
630                        size_t len, int noblock, int flags, int *addr_len)
631 {
632         struct inet_sock *inet = inet_sk(sk);
633         size_t copied = 0;
634         int err = -EOPNOTSUPP;
635         struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
636         struct sk_buff *skb;
637
638         if (flags & MSG_OOB)
639                 goto out;
640
641         if (addr_len)
642                 *addr_len = sizeof(*sin);
643
644         if (flags & MSG_ERRQUEUE) {
645                 err = ip_recv_error(sk, msg, len);
646                 goto out;
647         }
648
649         skb = skb_recv_datagram(sk, flags, noblock, &err);
650         if (!skb)
651                 goto out;
652
653         copied = skb->len;
654         if (len < copied) {
655                 msg->msg_flags |= MSG_TRUNC;
656                 copied = len;
657         }
658
659         err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
660         if (err)
661                 goto done;
662
663         sock_recv_timestamp(msg, sk, skb);
664
665         /* Copy the address. */
666         if (sin) {
667                 sin->sin_family = AF_INET;
668                 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
669                 sin->sin_port = 0;
670                 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
671         }
672         if (inet->cmsg_flags)
673                 ip_cmsg_recv(msg, skb);
674         if (flags & MSG_TRUNC)
675                 copied = skb->len;
676 done:
677         skb_free_datagram(sk, skb);
678 out:
679         if (err)
680                 return err;
681         return copied;
682 }
683
684 static int raw_init(struct sock *sk)
685 {
686         struct raw_sock *rp = raw_sk(sk);
687
688         if (inet_sk(sk)->num == IPPROTO_ICMP)
689                 memset(&rp->filter, 0, sizeof(rp->filter));
690         return 0;
691 }
692
693 static int raw_seticmpfilter(struct sock *sk, char __user *optval, int optlen)
694 {
695         if (optlen > sizeof(struct icmp_filter))
696                 optlen = sizeof(struct icmp_filter);
697         if (copy_from_user(&raw_sk(sk)->filter, optval, optlen))
698                 return -EFAULT;
699         return 0;
700 }
701
702 static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
703 {
704         int len, ret = -EFAULT;
705
706         if (get_user(len, optlen))
707                 goto out;
708         ret = -EINVAL;
709         if (len < 0)
710                 goto out;
711         if (len > sizeof(struct icmp_filter))
712                 len = sizeof(struct icmp_filter);
713         ret = -EFAULT;
714         if (put_user(len, optlen) ||
715             copy_to_user(optval, &raw_sk(sk)->filter, len))
716                 goto out;
717         ret = 0;
718 out:    return ret;
719 }
720
721 static int do_raw_setsockopt(struct sock *sk, int level, int optname,
722                           char __user *optval, int optlen)
723 {
724         if (optname == ICMP_FILTER) {
725                 if (inet_sk(sk)->num != IPPROTO_ICMP)
726                         return -EOPNOTSUPP;
727                 else
728                         return raw_seticmpfilter(sk, optval, optlen);
729         }
730         return -ENOPROTOOPT;
731 }
732
733 static int raw_setsockopt(struct sock *sk, int level, int optname,
734                           char __user *optval, int optlen)
735 {
736         if (level != SOL_RAW)
737                 return ip_setsockopt(sk, level, optname, optval, optlen);
738         return do_raw_setsockopt(sk, level, optname, optval, optlen);
739 }
740
741 #ifdef CONFIG_COMPAT
742 static int compat_raw_setsockopt(struct sock *sk, int level, int optname,
743                                  char __user *optval, int optlen)
744 {
745         if (level != SOL_RAW)
746                 return compat_ip_setsockopt(sk, level, optname, optval, optlen);
747         return do_raw_setsockopt(sk, level, optname, optval, optlen);
748 }
749 #endif
750
751 static int do_raw_getsockopt(struct sock *sk, int level, int optname,
752                           char __user *optval, int __user *optlen)
753 {
754         if (optname == ICMP_FILTER) {
755                 if (inet_sk(sk)->num != IPPROTO_ICMP)
756                         return -EOPNOTSUPP;
757                 else
758                         return raw_geticmpfilter(sk, optval, optlen);
759         }
760         return -ENOPROTOOPT;
761 }
762
763 static int raw_getsockopt(struct sock *sk, int level, int optname,
764                           char __user *optval, int __user *optlen)
765 {
766         if (level != SOL_RAW)
767                 return ip_getsockopt(sk, level, optname, optval, optlen);
768         return do_raw_getsockopt(sk, level, optname, optval, optlen);
769 }
770
771 #ifdef CONFIG_COMPAT
772 static int compat_raw_getsockopt(struct sock *sk, int level, int optname,
773                                  char __user *optval, int __user *optlen)
774 {
775         if (level != SOL_RAW)
776                 return compat_ip_getsockopt(sk, level, optname, optval, optlen);
777         return do_raw_getsockopt(sk, level, optname, optval, optlen);
778 }
779 #endif
780
781 static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
782 {
783         switch (cmd) {
784                 case SIOCOUTQ: {
785                         int amount = atomic_read(&sk->sk_wmem_alloc);
786                         return put_user(amount, (int __user *)arg);
787                 }
788                 case SIOCINQ: {
789                         struct sk_buff *skb;
790                         int amount = 0;
791
792                         spin_lock_bh(&sk->sk_receive_queue.lock);
793                         skb = skb_peek(&sk->sk_receive_queue);
794                         if (skb != NULL)
795                                 amount = skb->len;
796                         spin_unlock_bh(&sk->sk_receive_queue.lock);
797                         return put_user(amount, (int __user *)arg);
798                 }
799
800                 default:
801 #ifdef CONFIG_IP_MROUTE
802                         return ipmr_ioctl(sk, cmd, (void __user *)arg);
803 #else
804                         return -ENOIOCTLCMD;
805 #endif
806         }
807 }
808
809 DEFINE_PROTO_INUSE(raw)
810
811 struct proto raw_prot = {
812         .name              = "RAW",
813         .owner             = THIS_MODULE,
814         .close             = raw_close,
815         .connect           = ip4_datagram_connect,
816         .disconnect        = udp_disconnect,
817         .ioctl             = raw_ioctl,
818         .init              = raw_init,
819         .setsockopt        = raw_setsockopt,
820         .getsockopt        = raw_getsockopt,
821         .sendmsg           = raw_sendmsg,
822         .recvmsg           = raw_recvmsg,
823         .bind              = raw_bind,
824         .backlog_rcv       = raw_rcv_skb,
825         .hash              = raw_v4_hash,
826         .unhash            = raw_v4_unhash,
827         .obj_size          = sizeof(struct raw_sock),
828 #ifdef CONFIG_COMPAT
829         .compat_setsockopt = compat_raw_setsockopt,
830         .compat_getsockopt = compat_raw_getsockopt,
831 #endif
832         REF_PROTO_INUSE(raw)
833 };
834
835 #ifdef CONFIG_PROC_FS
836 struct raw_iter_state {
837         int bucket;
838 };
839
840 #define raw_seq_private(seq) ((struct raw_iter_state *)(seq)->private)
841
842 static struct sock *raw_get_first(struct seq_file *seq)
843 {
844         struct sock *sk;
845         struct raw_iter_state* state = raw_seq_private(seq);
846
847         for (state->bucket = 0; state->bucket < RAWV4_HTABLE_SIZE; ++state->bucket) {
848                 struct hlist_node *node;
849
850                 sk_for_each(sk, node, &raw_v4_htable[state->bucket])
851                         if (sk->sk_family == PF_INET)
852                                 goto found;
853         }
854         sk = NULL;
855 found:
856         return sk;
857 }
858
859 static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
860 {
861         struct raw_iter_state* state = raw_seq_private(seq);
862
863         do {
864                 sk = sk_next(sk);
865 try_again:
866                 ;
867         } while (sk && sk->sk_family != PF_INET);
868
869         if (!sk && ++state->bucket < RAWV4_HTABLE_SIZE) {
870                 sk = sk_head(&raw_v4_htable[state->bucket]);
871                 goto try_again;
872         }
873         return sk;
874 }
875
876 static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
877 {
878         struct sock *sk = raw_get_first(seq);
879
880         if (sk)
881                 while (pos && (sk = raw_get_next(seq, sk)) != NULL)
882                         --pos;
883         return pos ? NULL : sk;
884 }
885
886 static void *raw_seq_start(struct seq_file *seq, loff_t *pos)
887 {
888         read_lock(&raw_v4_lock);
889         return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
890 }
891
892 static void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
893 {
894         struct sock *sk;
895
896         if (v == SEQ_START_TOKEN)
897                 sk = raw_get_first(seq);
898         else
899                 sk = raw_get_next(seq, v);
900         ++*pos;
901         return sk;
902 }
903
904 static void raw_seq_stop(struct seq_file *seq, void *v)
905 {
906         read_unlock(&raw_v4_lock);
907 }
908
909 static __inline__ char *get_raw_sock(struct sock *sp, char *tmpbuf, int i)
910 {
911         struct inet_sock *inet = inet_sk(sp);
912         __be32 dest = inet->daddr,
913                src = inet->rcv_saddr;
914         __u16 destp = 0,
915               srcp  = inet->num;
916
917         sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
918                 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d",
919                 i, src, srcp, dest, destp, sp->sk_state,
920                 atomic_read(&sp->sk_wmem_alloc),
921                 atomic_read(&sp->sk_rmem_alloc),
922                 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
923                 atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
924         return tmpbuf;
925 }
926
927 #define TMPSZ 128
928
929 static int raw_seq_show(struct seq_file *seq, void *v)
930 {
931         char tmpbuf[TMPSZ+1];
932
933         if (v == SEQ_START_TOKEN)
934                 seq_printf(seq, "%-*s\n", TMPSZ-1,
935                                "  sl  local_address rem_address   st tx_queue "
936                                "rx_queue tr tm->when retrnsmt   uid  timeout "
937                                "inode  drops");
938         else {
939                 struct raw_iter_state *state = raw_seq_private(seq);
940
941                 seq_printf(seq, "%-*s\n", TMPSZ-1,
942                            get_raw_sock(v, tmpbuf, state->bucket));
943         }
944         return 0;
945 }
946
947 static const struct seq_operations raw_seq_ops = {
948         .start = raw_seq_start,
949         .next  = raw_seq_next,
950         .stop  = raw_seq_stop,
951         .show  = raw_seq_show,
952 };
953
954 static int raw_seq_open(struct inode *inode, struct file *file)
955 {
956         return seq_open_private(file, &raw_seq_ops,
957                         sizeof(struct raw_iter_state));
958 }
959
960 static const struct file_operations raw_seq_fops = {
961         .owner   = THIS_MODULE,
962         .open    = raw_seq_open,
963         .read    = seq_read,
964         .llseek  = seq_lseek,
965         .release = seq_release_private,
966 };
967
968 int __init raw_proc_init(void)
969 {
970         if (!proc_net_fops_create(&init_net, "raw", S_IRUGO, &raw_seq_fops))
971                 return -ENOMEM;
972         return 0;
973 }
974
975 void __init raw_proc_exit(void)
976 {
977         proc_net_remove(&init_net, "raw");
978 }
979 #endif /* CONFIG_PROC_FS */