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