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