Change mbug allocation flags from M_ to MB_ to avoid confusion with malloc
[dragonfly.git] / sys / netinet / raw_ip.c
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)raw_ip.c    8.7 (Berkeley) 5/15/95
34  * $FreeBSD: src/sys/netinet/raw_ip.c,v 1.64.2.16 2003/08/24 08:24:38 hsu Exp $
35  * $DragonFly: src/sys/netinet/raw_ip.c,v 1.13 2004/06/02 14:43:01 eirikn Exp $
36  */
37
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
40 #include "opt_random_ip_id.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/proc.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52
53 #include <vm/vm_zone.h>
54
55 #include <net/if.h>
56 #include <net/route.h>
57
58 #define _IP_VHL
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip_var.h>
65
66 #include <net/ip_mroute/ip_mroute.h>
67 #include <net/ipfw/ip_fw.h>
68 #include <net/dummynet/ip_dummynet.h>
69
70 #ifdef FAST_IPSEC
71 #include <netipsec/ipsec.h>
72 #endif /*FAST_IPSEC*/
73
74 #ifdef IPSEC
75 #include <netinet6/ipsec.h>
76 #endif /*IPSEC*/
77
78 struct  inpcbinfo ripcbinfo;
79
80 /* control hooks for ipfw and dummynet */
81 ip_fw_ctl_t *ip_fw_ctl_ptr;
82 ip_dn_ctl_t *ip_dn_ctl_ptr;
83
84 /*
85  * hooks for multicast routing. They all default to NULL,
86  * so leave them not initialized and rely on BSS being set to 0.
87  */
88
89 /* The socket used to communicate with the multicast routing daemon.  */
90 struct socket  *ip_mrouter;
91
92 /* The various mrouter and rsvp functions */
93 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
94 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
95 int (*ip_mrouter_done)(void);
96 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
97                 struct ip_moptions *);
98 int (*mrt_ioctl)(int, caddr_t);
99 int (*legal_vif_num)(int);
100 u_long (*ip_mcast_src)(int);
101
102 void (*rsvp_input_p)(struct mbuf *m, int off, int proto);
103 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
104 void (*ip_rsvp_force_done)(struct socket *);
105
106 /*
107  * Nominal space allocated to a raw ip socket.
108  */
109 #define RIPSNDQ         8192
110 #define RIPRCVQ         8192
111
112 /*
113  * Raw interface to IP protocol.
114  */
115
116 /*
117  * Initialize raw connection block queue.
118  */
119 void
120 rip_init(void)
121 {
122         LIST_INIT(&ripcbinfo.listhead);
123         /*
124          * XXX We don't use the hash list for raw IP, but it's easier
125          * to allocate a one entry hash list than it is to check all
126          * over the place for hashbase == NULL.
127          */
128         ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
129         ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
130         ripcbinfo.wildcardhashbase = hashinit(1, M_PCB,
131                                               &ripcbinfo.wildcardhashmask);
132         ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
133                                    maxsockets, ZONE_INTERRUPT, 0);
134 }
135
136 /*
137  * XXX ripsrc is modified in rip_input, so we must be fix this
138  * when we want to make this code smp-friendly.
139  */
140 static struct   sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
141
142 /*
143  * Setup generic address and protocol structures
144  * for raw_input routine, then pass them along with
145  * mbuf chain.
146  */
147 void
148 rip_input(struct mbuf *m, int off, int proto)
149 {
150         struct ip *ip = mtod(m, struct ip *);
151         struct inpcb *inp;
152         struct inpcb *last = NULL;
153         struct mbuf *opts = NULL;
154
155         ripsrc.sin_addr = ip->ip_src;
156         LIST_FOREACH(inp, &ripcbinfo.listhead, inp_list) {
157 #ifdef INET6
158                 if ((inp->inp_vflag & INP_IPV4) == 0)
159                         continue;
160 #endif
161                 if (inp->inp_ip_p && inp->inp_ip_p != proto)
162                         continue;
163                 if (inp->inp_laddr.s_addr != INADDR_ANY &&
164                     inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
165                         continue;
166                 if (inp->inp_faddr.s_addr != INADDR_ANY &&
167                     inp->inp_faddr.s_addr != ip->ip_src.s_addr)
168                         continue;
169                 if (last) {
170                         struct mbuf *n = m_copypacket(m, MB_DONTWAIT);
171
172 #ifdef IPSEC
173                         /* check AH/ESP integrity. */
174                         if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
175                                 m_freem(n);
176                                 ipsecstat.in_polvio++;
177                                 /* do not inject data to pcb */
178                         } else
179 #endif /*IPSEC*/
180 #ifdef FAST_IPSEC
181                         /* check AH/ESP integrity. */
182                         if (ipsec4_in_reject(n, last)) {
183                                 m_freem(n);
184                                 /* do not inject data to pcb */
185                         } else
186 #endif /*FAST_IPSEC*/
187                         if (n) {
188                                 if (last->inp_flags & INP_CONTROLOPTS ||
189                                     last->inp_socket->so_options & SO_TIMESTAMP)
190                                     ip_savecontrol(last, &opts, ip, n);
191                                 if (sbappendaddr(&last->inp_socket->so_rcv,
192                                     (struct sockaddr *)&ripsrc, n,
193                                     opts) == 0) {
194                                         /* should notify about lost packet */
195                                         m_freem(n);
196                                         if (opts)
197                                             m_freem(opts);
198                                 } else
199                                         sorwakeup(last->inp_socket);
200                                 opts = 0;
201                         }
202                 }
203                 last = inp;
204         }
205 #ifdef IPSEC
206         /* check AH/ESP integrity. */
207         if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
208                 m_freem(m);
209                 ipsecstat.in_polvio++;
210                 ipstat.ips_delivered--;
211                 /* do not inject data to pcb */
212         } else
213 #endif /*IPSEC*/
214 #ifdef FAST_IPSEC
215         /* check AH/ESP integrity. */
216         if (last && ipsec4_in_reject(m, last)) {
217                 m_freem(m);
218                 ipstat.ips_delivered--;
219                 /* do not inject data to pcb */
220         } else
221 #endif /*FAST_IPSEC*/
222         if (last) {
223                 if (last->inp_flags & INP_CONTROLOPTS ||
224                     last->inp_socket->so_options & SO_TIMESTAMP)
225                         ip_savecontrol(last, &opts, ip, m);
226                 if (sbappendaddr(&last->inp_socket->so_rcv,
227                     (struct sockaddr *)&ripsrc, m, opts) == 0) {
228                         m_freem(m);
229                         if (opts)
230                             m_freem(opts);
231                 } else
232                         sorwakeup(last->inp_socket);
233         } else {
234                 m_freem(m);
235                 ipstat.ips_noproto++;
236                 ipstat.ips_delivered--;
237         }
238 }
239
240 /*
241  * Generate IP header and pass packet to ip_output.
242  * Tack on options user may have setup with control call.
243  */
244 int
245 rip_output(struct mbuf *m, struct socket *so, u_long dst)
246 {
247         struct ip *ip;
248         struct inpcb *inp = sotoinpcb(so);
249         int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
250
251         /*
252          * If the user handed us a complete IP packet, use it.
253          * Otherwise, allocate an mbuf for a header and fill it in.
254          */
255         if ((inp->inp_flags & INP_HDRINCL) == 0) {
256                 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
257                         m_freem(m);
258                         return(EMSGSIZE);
259                 }
260                 M_PREPEND(m, sizeof(struct ip), MB_WAIT);
261                 if (m == NULL)
262                         return(ENOBUFS);
263                 ip = mtod(m, struct ip *);
264                 ip->ip_tos = inp->inp_ip_tos;
265                 ip->ip_off = 0;
266                 ip->ip_p = inp->inp_ip_p;
267                 ip->ip_len = m->m_pkthdr.len;
268                 ip->ip_src = inp->inp_laddr;
269                 ip->ip_dst.s_addr = dst;
270                 ip->ip_ttl = inp->inp_ip_ttl;
271         } else {
272                 if (m->m_pkthdr.len > IP_MAXPACKET) {
273                         m_freem(m);
274                         return(EMSGSIZE);
275                 }
276                 ip = mtod(m, struct ip *);
277                 /* don't allow both user specified and setsockopt options,
278                    and don't allow packet length sizes that will crash */
279                 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
280                      && inp->inp_options)
281                     || (ip->ip_len > m->m_pkthdr.len)
282                     || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
283                         m_freem(m);
284                         return EINVAL;
285                 }
286                 if (ip->ip_id == 0)
287 #ifdef RANDOM_IP_ID
288                         ip->ip_id = ip_randomid();
289 #else
290                         ip->ip_id = htons(ip_id++);
291 #endif
292                 /* XXX prevent ip_output from overwriting header fields */
293                 flags |= IP_RAWOUTPUT;
294                 ipstat.ips_rawout++;
295         }
296
297         return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
298                           inp->inp_moptions, inp));
299 }
300
301 /*
302  * Raw IP socket option processing.
303  */
304 int
305 rip_ctloutput(struct socket *so, struct sockopt *sopt)
306 {
307         struct  inpcb *inp = sotoinpcb(so);
308         int     error, optval;
309
310         if (sopt->sopt_level != IPPROTO_IP)
311                 return (EINVAL);
312
313         error = 0;
314
315         switch (sopt->sopt_dir) {
316         case SOPT_GET:
317                 switch (sopt->sopt_name) {
318                 case IP_HDRINCL:
319                         optval = inp->inp_flags & INP_HDRINCL;
320                         error = sooptcopyout(sopt, &optval, sizeof optval);
321                         break;
322
323                 case IP_FW_ADD: /* ADD actually returns the body... */
324                 case IP_FW_GET:
325                         if (IPFW_LOADED)
326                                 error = ip_fw_ctl_ptr(sopt);
327                         else
328                                 error = ENOPROTOOPT;
329                         break;
330
331                 case IP_DUMMYNET_GET:
332                         if (DUMMYNET_LOADED)
333                                 error = ip_dn_ctl_ptr(sopt);
334                         else
335                                 error = ENOPROTOOPT;
336                         break ;
337
338                 case MRT_INIT:
339                 case MRT_DONE:
340                 case MRT_ADD_VIF:
341                 case MRT_DEL_VIF:
342                 case MRT_ADD_MFC:
343                 case MRT_DEL_MFC:
344                 case MRT_VERSION:
345                 case MRT_ASSERT:
346                 case MRT_API_SUPPORT:
347                 case MRT_API_CONFIG:
348                 case MRT_ADD_BW_UPCALL:
349                 case MRT_DEL_BW_UPCALL:
350                         error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
351                                 EOPNOTSUPP;
352                         break;
353
354                 default:
355                         error = ip_ctloutput(so, sopt);
356                         break;
357                 }
358                 break;
359
360         case SOPT_SET:
361                 switch (sopt->sopt_name) {
362                 case IP_HDRINCL:
363                         error = sooptcopyin(sopt, &optval, sizeof optval,
364                                             sizeof optval);
365                         if (error)
366                                 break;
367                         if (optval)
368                                 inp->inp_flags |= INP_HDRINCL;
369                         else
370                                 inp->inp_flags &= ~INP_HDRINCL;
371                         break;
372
373                 case IP_FW_ADD:
374                 case IP_FW_DEL:
375                 case IP_FW_FLUSH:
376                 case IP_FW_ZERO:
377                 case IP_FW_RESETLOG:
378                         if (IPFW_LOADED)
379                                 error = ip_fw_ctl_ptr(sopt);
380                         else
381                                 error = ENOPROTOOPT;
382                         break;
383
384                 case IP_DUMMYNET_CONFIGURE:
385                 case IP_DUMMYNET_DEL:
386                 case IP_DUMMYNET_FLUSH:
387                         if (DUMMYNET_LOADED)
388                                 error = ip_dn_ctl_ptr(sopt);
389                         else
390                                 error = ENOPROTOOPT ;
391                         break ;
392
393                 case IP_RSVP_ON:
394                         error = ip_rsvp_init(so);
395                         break;
396
397                 case IP_RSVP_OFF:
398                         error = ip_rsvp_done();
399                         break;
400
401                 case IP_RSVP_VIF_ON:
402                 case IP_RSVP_VIF_OFF:
403                         error = ip_rsvp_vif ?
404                                 ip_rsvp_vif(so, sopt) : EINVAL;
405                         break;
406
407                 case MRT_INIT:
408                 case MRT_DONE:
409                 case MRT_ADD_VIF:
410                 case MRT_DEL_VIF:
411                 case MRT_ADD_MFC:
412                 case MRT_DEL_MFC:
413                 case MRT_VERSION:
414                 case MRT_ASSERT:
415                 case MRT_API_SUPPORT:
416                 case MRT_API_CONFIG:
417                 case MRT_ADD_BW_UPCALL:
418                 case MRT_DEL_BW_UPCALL:
419                         error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
420                                         EOPNOTSUPP;
421                         break;
422
423                 default:
424                         error = ip_ctloutput(so, sopt);
425                         break;
426                 }
427                 break;
428         }
429
430         return (error);
431 }
432
433 /*
434  * This function exists solely to receive the PRC_IFDOWN messages which
435  * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
436  * and calls in_ifadown() to remove all routes corresponding to that address.
437  * It also receives the PRC_IFUP messages from if_up() and reinstalls the
438  * interface routes.
439  */
440 void
441 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
442 {
443         struct in_ifaddr *ia;
444         struct ifnet *ifp;
445         int err;
446         int flags;
447
448         switch (cmd) {
449         case PRC_IFDOWN:
450                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
451                         if (ia->ia_ifa.ifa_addr == sa
452                             && (ia->ia_flags & IFA_ROUTE)) {
453                                 /*
454                                  * in_ifscrub kills the interface route.
455                                  */
456                                 in_ifscrub(ia->ia_ifp, ia);
457                                 /*
458                                  * in_ifadown gets rid of all the rest of
459                                  * the routes.  This is not quite the right
460                                  * thing to do, but at least if we are running
461                                  * a routing process they will come back.
462                                  */
463                                 in_ifadown(&ia->ia_ifa, 0);
464                                 break;
465                         }
466                 }
467                 break;
468
469         case PRC_IFUP:
470                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
471                         if (ia->ia_ifa.ifa_addr == sa)
472                                 break;
473                 }
474                 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
475                         return;
476                 flags = RTF_UP;
477                 ifp = ia->ia_ifa.ifa_ifp;
478
479                 if ((ifp->if_flags & IFF_LOOPBACK)
480                     || (ifp->if_flags & IFF_POINTOPOINT))
481                         flags |= RTF_HOST;
482
483                 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
484                 if (err == 0)
485                         ia->ia_flags |= IFA_ROUTE;
486                 break;
487         }
488 }
489
490 u_long  rip_sendspace = RIPSNDQ;
491 u_long  rip_recvspace = RIPRCVQ;
492
493 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
494     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
495 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
496     &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
497
498 static int
499 rip_attach(struct socket *so, int proto, struct pru_attach_info *ai)
500 {
501         struct inpcb *inp;
502         int error, s;
503
504         inp = sotoinpcb(so);
505         if (inp)
506                 panic("rip_attach");
507         if ((error = suser_cred(ai->p_ucred, NULL_CRED_OKAY)) != 0)
508                 return error;
509
510         error = soreserve(so, rip_sendspace, rip_recvspace, ai->sb_rlimit);
511         if (error)
512                 return error;
513         s = splnet();
514         error = in_pcballoc(so, &ripcbinfo);
515         splx(s);
516         if (error)
517                 return error;
518         inp = (struct inpcb *)so->so_pcb;
519         inp->inp_vflag |= INP_IPV4;
520         inp->inp_ip_p = proto;
521         inp->inp_ip_ttl = ip_defttl;
522         return 0;
523 }
524
525 static int
526 rip_detach(struct socket *so)
527 {
528         struct inpcb *inp;
529
530         inp = sotoinpcb(so);
531         if (inp == 0)
532                 panic("rip_detach");
533         if (so == ip_mrouter && ip_mrouter_done)
534                 ip_mrouter_done();
535         if (ip_rsvp_force_done)
536                 ip_rsvp_force_done(so);
537         if (so == ip_rsvpd)
538                 ip_rsvp_done();
539         in_pcbdetach(inp);
540         return 0;
541 }
542
543 static int
544 rip_abort(struct socket *so)
545 {
546         soisdisconnected(so);
547         if (so->so_state & SS_NOFDREF)
548                 return rip_detach(so);
549         return 0;
550 }
551
552 static int
553 rip_disconnect(struct socket *so)
554 {
555         if ((so->so_state & SS_ISCONNECTED) == 0)
556                 return ENOTCONN;
557         return rip_abort(so);
558 }
559
560 static int
561 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
562 {
563         struct inpcb *inp = sotoinpcb(so);
564         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
565
566         if (nam->sa_len != sizeof(*addr))
567                 return EINVAL;
568
569         if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
570                                     (addr->sin_family != AF_IMPLINK)) ||
571             (addr->sin_addr.s_addr != INADDR_ANY &&
572              ifa_ifwithaddr((struct sockaddr *)addr) == 0))
573                 return EADDRNOTAVAIL;
574         inp->inp_laddr = addr->sin_addr;
575         return 0;
576 }
577
578 static int
579 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
580 {
581         struct inpcb *inp = sotoinpcb(so);
582         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
583
584         if (nam->sa_len != sizeof(*addr))
585                 return EINVAL;
586         if (TAILQ_EMPTY(&ifnet))
587                 return EADDRNOTAVAIL;
588         if ((addr->sin_family != AF_INET) &&
589             (addr->sin_family != AF_IMPLINK))
590                 return EAFNOSUPPORT;
591         inp->inp_faddr = addr->sin_addr;
592         soisconnected(so);
593         return 0;
594 }
595
596 static int
597 rip_shutdown(struct socket *so)
598 {
599         socantsendmore(so);
600         return 0;
601 }
602
603 static int
604 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
605          struct mbuf *control, struct thread *td)
606 {
607         struct inpcb *inp = sotoinpcb(so);
608         u_long dst;
609
610         if (so->so_state & SS_ISCONNECTED) {
611                 if (nam) {
612                         m_freem(m);
613                         return EISCONN;
614                 }
615                 dst = inp->inp_faddr.s_addr;
616         } else {
617                 if (nam == NULL) {
618                         m_freem(m);
619                         return ENOTCONN;
620                 }
621                 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
622         }
623         return rip_output(m, so, dst);
624 }
625
626 static int
627 rip_pcblist(SYSCTL_HANDLER_ARGS)
628 {
629         int error, i, n, s;
630         struct inpcb *inp, **inp_list;
631         inp_gen_t gencnt;
632         struct xinpgen xig;
633
634         /*
635          * The process of preparing the TCB list is too time-consuming and
636          * resource-intensive to repeat twice on every request.
637          */
638         if (req->oldptr == 0) {
639                 n = ripcbinfo.ipi_count;
640                 req->oldidx = 2 * (sizeof xig)
641                         + (n + n/8) * sizeof(struct xinpcb);
642                 return 0;
643         }
644
645         if (req->newptr != 0)
646                 return EPERM;
647
648         /*
649          * OK, now we're committed to doing something.
650          */
651         s = splnet();
652         gencnt = ripcbinfo.ipi_gencnt;
653         n = ripcbinfo.ipi_count;
654         splx(s);
655
656         xig.xig_len = sizeof xig;
657         xig.xig_count = n;
658         xig.xig_gen = gencnt;
659         xig.xig_sogen = so_gencnt;
660         error = SYSCTL_OUT(req, &xig, sizeof xig);
661         if (error)
662                 return error;
663
664         inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
665         if (inp_list == 0)
666                 return ENOMEM;
667         
668         s = splnet();
669         for (inp = LIST_FIRST(&ripcbinfo.listhead), i = 0; inp && i < n;
670              inp = LIST_NEXT(inp, inp_list)) {
671                 if (inp->inp_gencnt <= gencnt)
672                         inp_list[i++] = inp;
673         }
674         splx(s);
675         n = i;
676
677         error = 0;
678         for (i = 0; i < n; i++) {
679                 inp = inp_list[i];
680                 if (inp->inp_gencnt <= gencnt) {
681                         struct xinpcb xi;
682                         xi.xi_len = sizeof xi;
683                         /* XXX should avoid extra copy */
684                         bcopy(inp, &xi.xi_inp, sizeof *inp);
685                         if (inp->inp_socket)
686                                 sotoxsocket(inp->inp_socket, &xi.xi_socket);
687                         error = SYSCTL_OUT(req, &xi, sizeof xi);
688                 }
689         }
690         if (!error) {
691                 /*
692                  * Give the user an updated idea of our state.
693                  * If the generation differs from what we told
694                  * her before, she knows that something happened
695                  * while we were processing this request, and it
696                  * might be necessary to retry.
697                  */
698                 s = splnet();
699                 xig.xig_gen = ripcbinfo.ipi_gencnt;
700                 xig.xig_sogen = so_gencnt;
701                 xig.xig_count = ripcbinfo.ipi_count;
702                 splx(s);
703                 error = SYSCTL_OUT(req, &xig, sizeof xig);
704         }
705         free(inp_list, M_TEMP);
706         return error;
707 }
708
709 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
710             rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
711
712 struct pr_usrreqs rip_usrreqs = {
713         rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
714         pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
715         pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
716         pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
717         in_setsockaddr, sosend, soreceive, sopoll
718 };