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