raw_ip.c revision 86047
119304Speter/*
219304Speter * Copyright (c) 1982, 1986, 1988, 1993
319304Speter *	The Regents of the University of California.  All rights reserved.
419304Speter *
519304Speter * Redistribution and use in source and binary forms, with or without
619304Speter * modification, are permitted provided that the following conditions
719304Speter * are met:
819304Speter * 1. Redistributions of source code must retain the above copyright
919304Speter *    notice, this list of conditions and the following disclaimer.
1019304Speter * 2. Redistributions in binary form must reproduce the above copyright
1119304Speter *    notice, this list of conditions and the following disclaimer in the
1219304Speter *    documentation and/or other materials provided with the distribution.
13254225Speter * 3. All advertising materials mentioning features or use of this software
1419304Speter *    must display the following acknowledgement:
1519304Speter *	This product includes software developed by the University of
1619304Speter *	California, Berkeley and its contributors.
1719304Speter * 4. Neither the name of the University nor the names of its contributors
18254225Speter *    may be used to endorse or promote products derived from this software
1919304Speter *    without specific prior written permission.
2019304Speter *
2119304Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2219304Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2319304Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2419304Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2519304Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2619304Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2719304Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2819304Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2919304Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3019304Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3119304Speter * SUCH DAMAGE.
32254225Speter *
3319304Speter *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
3419304Speter * $FreeBSD: head/sys/netinet/raw_ip.c 86047 2001-11-04 22:56:25Z luigi $
3519304Speter */
3619304Speter
3719304Speter#include "opt_inet6.h"
3819304Speter#include "opt_ipsec.h"
3919304Speter#include "opt_random_ip_id.h"
4019304Speter
4119304Speter#include <sys/param.h>
4219304Speter#include <sys/systm.h>
4319304Speter#include <sys/kernel.h>
4419304Speter#include <sys/malloc.h>
4519304Speter#include <sys/mbuf.h>
4619304Speter#include <sys/proc.h>
4719304Speter#include <sys/protosw.h>
4819304Speter#include <sys/socket.h>
4919304Speter#include <sys/socketvar.h>
5019304Speter#include <sys/sysctl.h>
5119304Speter
5219304Speter#include <vm/vm_zone.h>
5319304Speter
5419304Speter#include <net/if.h>
5519304Speter#include <net/route.h>
5619304Speter
5719304Speter#define _IP_VHL
5819304Speter#include <netinet/in.h>
5919304Speter#include <netinet/in_systm.h>
6019304Speter#include <netinet/ip.h>
6119304Speter#include <netinet/in_pcb.h>
6219304Speter#include <netinet/in_var.h>
6319304Speter#include <netinet/ip_var.h>
6419304Speter#include <netinet/ip_mroute.h>
6519304Speter
6619304Speter#include <netinet/ip_fw.h>
6719304Speter#include <netinet/ip_dummynet.h>
6819304Speter
69254225Speter#ifdef IPSEC
70254225Speter#include <netinet6/ipsec.h>
71254225Speter#endif /*IPSEC*/
7219304Speter
7319304Speterstruct	inpcbhead ripcb;
7419304Speterstruct	inpcbinfo ripcbinfo;
7519304Speter
7619304Speter/* control hooks for ipfw and dummynet */
7719304Speterip_fw_ctl_t *ip_fw_ctl_ptr;
7819304Speterip_dn_ctl_t *ip_dn_ctl_ptr;
79254225Speter
8019304Speter/*
8119304Speter * Nominal space allocated to a raw ip socket.
8219304Speter */
8319304Speter#define	RIPSNDQ		8192
8419304Speter#define	RIPRCVQ		8192
8519304Speter
8619304Speter/*
8719304Speter * Raw interface to IP protocol.
8819304Speter */
8919304Speter
90254225Speter/*
91254225Speter * Initialize raw connection block q.
92254225Speter */
9319304Spetervoid
9419304Speterrip_init()
9519304Speter{
9619304Speter	LIST_INIT(&ripcb);
9719304Speter	ripcbinfo.listhead = &ripcb;
9819304Speter	/*
9919304Speter	 * XXX We don't use the hash list for raw IP, but it's easier
100254225Speter	 * to allocate a one entry hash list than it is to check all
101254225Speter	 * over the place for hashbase == NULL.
10219304Speter	 */
10319304Speter	ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
10419304Speter	ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
10519304Speter	ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
10619304Speter				   maxsockets, ZONE_INTERRUPT, 0);
10719304Speter}
10819304Speter
10919304Speterstatic struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
11019304Speter/*
11119304Speter * Setup generic address and protocol structures
11219304Speter * for raw_input routine, then pass them along with
11319304Speter * mbuf chain.
114254225Speter */
115254225Spetervoid
116254225Speterrip_input(m, off)
117254225Speter	struct mbuf *m;
118254225Speter	int off;
11919304Speter{
12019304Speter	register struct ip *ip = mtod(m, struct ip *);
12119304Speter	register struct inpcb *inp;
12219304Speter	struct inpcb *last = 0;
12319304Speter	struct mbuf *opts = 0;
12419304Speter	int proto = ip->ip_p;
12519304Speter
12619304Speter	ripsrc.sin_addr = ip->ip_src;
12719304Speter	LIST_FOREACH(inp, &ripcb, inp_list) {
12819304Speter#ifdef INET6
12919304Speter		if ((inp->inp_vflag & INP_IPV4) == 0)
13019304Speter			continue;
13119304Speter#endif
13219304Speter		if (inp->inp_ip_p && inp->inp_ip_p != proto)
13319304Speter			continue;
13419304Speter		if (inp->inp_laddr.s_addr &&
13519304Speter                  inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
13619304Speter			continue;
13719304Speter		if (inp->inp_faddr.s_addr &&
13819304Speter                  inp->inp_faddr.s_addr != ip->ip_src.s_addr)
13919304Speter			continue;
14019304Speter		if (last) {
14119304Speter			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
14219304Speter
14319304Speter#ifdef IPSEC
14419304Speter			/* check AH/ESP integrity. */
14519304Speter			if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
14619304Speter				m_freem(n);
14719304Speter				ipsecstat.in_polvio++;
14819304Speter				/* do not inject data to pcb */
14919304Speter			} else
15019304Speter#endif /*IPSEC*/
15119304Speter			if (n) {
15219304Speter				if (last->inp_flags & INP_CONTROLOPTS ||
15319304Speter				    last->inp_socket->so_options & SO_TIMESTAMP)
15419304Speter				    ip_savecontrol(last, &opts, ip, n);
15519304Speter				if (sbappendaddr(&last->inp_socket->so_rcv,
15619304Speter				    (struct sockaddr *)&ripsrc, n,
15719304Speter				    opts) == 0) {
15819304Speter					/* should notify about lost packet */
159254225Speter					m_freem(n);
160254225Speter					if (opts)
161254225Speter					    m_freem(opts);
162254225Speter				} else
163254225Speter					sorwakeup(last->inp_socket);
16419304Speter				opts = 0;
16519304Speter			}
16619304Speter		}
16719304Speter		last = inp;
16819304Speter	}
16919304Speter#ifdef IPSEC
17019304Speter	/* check AH/ESP integrity. */
17119304Speter	if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
17219304Speter		m_freem(m);
17319304Speter		ipsecstat.in_polvio++;
17419304Speter		ipstat.ips_delivered--;
17519304Speter		/* do not inject data to pcb */
17619304Speter	} else
17719304Speter#endif /*IPSEC*/
17819304Speter	if (last) {
17919304Speter		if (last->inp_flags & INP_CONTROLOPTS ||
180254225Speter		    last->inp_socket->so_options & SO_TIMESTAMP)
18119304Speter			ip_savecontrol(last, &opts, ip, m);
182254225Speter		if (sbappendaddr(&last->inp_socket->so_rcv,
18319304Speter		    (struct sockaddr *)&ripsrc, m, opts) == 0) {
18419304Speter			m_freem(m);
18519304Speter			if (opts)
18619304Speter			    m_freem(opts);
18719304Speter		} else
18819304Speter			sorwakeup(last->inp_socket);
18919304Speter	} else {
19019304Speter		m_freem(m);
19119304Speter		ipstat.ips_noproto++;
19219304Speter		ipstat.ips_delivered--;
19319304Speter	}
19419304Speter}
19519304Speter
19619304Speter/*
19719304Speter * Generate IP header and pass packet to ip_output.
19819304Speter * Tack on options user may have setup with control call.
19919304Speter */
20019304Speterint
201254225Speterrip_output(m, so, dst)
202254225Speter	struct mbuf *m;
203254225Speter	struct socket *so;
20419304Speter	u_long dst;
205254225Speter{
20619304Speter	register struct ip *ip;
20719304Speter	register struct inpcb *inp = sotoinpcb(so);
20819304Speter	int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
20919304Speter
21019304Speter	/*
211254225Speter	 * If the user handed us a complete IP packet, use it.
21219304Speter	 * Otherwise, allocate an mbuf for a header and fill it in.
21319304Speter	 */
214254225Speter	if ((inp->inp_flags & INP_HDRINCL) == 0) {
215254225Speter		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
21619304Speter			m_freem(m);
21719304Speter			return(EMSGSIZE);
21819304Speter		}
21919304Speter		M_PREPEND(m, sizeof(struct ip), M_TRYWAIT);
22019304Speter		ip = mtod(m, struct ip *);
22119304Speter		ip->ip_tos = inp->inp_ip_tos;
22219304Speter		ip->ip_off = 0;
22319304Speter		ip->ip_p = inp->inp_ip_p;
22419304Speter		ip->ip_len = m->m_pkthdr.len;
22519304Speter		ip->ip_src = inp->inp_laddr;
226254225Speter		ip->ip_dst.s_addr = dst;
227254225Speter		ip->ip_ttl = inp->inp_ip_ttl;
228254225Speter	} else {
229254225Speter		if (m->m_pkthdr.len > IP_MAXPACKET) {
23019304Speter			m_freem(m);
23119304Speter			return(EMSGSIZE);
23219304Speter		}
23319304Speter		ip = mtod(m, struct ip *);
23419304Speter		/* don't allow both user specified and setsockopt options,
23519304Speter		   and don't allow packet length sizes that will crash */
23619304Speter		if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
23719304Speter		     && inp->inp_options)
23819304Speter		    || (ip->ip_len > m->m_pkthdr.len)
239254225Speter		    || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
24019304Speter			m_freem(m);
24119304Speter			return EINVAL;
24219304Speter		}
24319304Speter		if (ip->ip_id == 0)
24419304Speter#ifdef RANDOM_IP_ID
24519304Speter			ip->ip_id = ip_randomid();
24619304Speter#else
24719304Speter			ip->ip_id = htons(ip_id++);
24819304Speter#endif
24919304Speter		/* XXX prevent ip_output from overwriting header fields */
25019304Speter		flags |= IP_RAWOUTPUT;
25119304Speter		ipstat.ips_rawout++;
25219304Speter	}
25319304Speter
25419304Speter#ifdef IPSEC
25519304Speter	if (ipsec_setsocket(m, so) != 0) {
25619304Speter		m_freem(m);
25719304Speter		return ENOBUFS;
25819304Speter	}
25919304Speter#endif /*IPSEC*/
26019304Speter
26119304Speter	return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
26219304Speter			  inp->inp_moptions));
26319304Speter}
26419304Speter
26519304Speter/*
26619304Speter * Raw IP socket option processing.
26719304Speter */
26819304Speterint
269254225Speterrip_ctloutput(so, sopt)
27019304Speter	struct socket *so;
27119304Speter	struct sockopt *sopt;
27219304Speter{
27319304Speter	struct	inpcb *inp = sotoinpcb(so);
27419304Speter	int	error, optval;
27519304Speter
27619304Speter	if (sopt->sopt_level != IPPROTO_IP)
27719304Speter		return (EINVAL);
278
279	error = 0;
280
281	switch (sopt->sopt_dir) {
282	case SOPT_GET:
283		switch (sopt->sopt_name) {
284		case IP_HDRINCL:
285			optval = inp->inp_flags & INP_HDRINCL;
286			error = sooptcopyout(sopt, &optval, sizeof optval);
287			break;
288
289		case IP_FW_ADD:	/* ADD actually returns the body... */
290		case IP_FW_GET:
291			if (IPFW_LOADED)
292				error = ip_fw_ctl_ptr(sopt);
293			else
294				error = ENOPROTOOPT;
295			break;
296
297		case IP_DUMMYNET_GET:
298			if (DUMMYNET_LOADED)
299				error = ip_dn_ctl_ptr(sopt);
300			else
301				error = ENOPROTOOPT;
302			break ;
303
304		case MRT_INIT:
305		case MRT_DONE:
306		case MRT_ADD_VIF:
307		case MRT_DEL_VIF:
308		case MRT_ADD_MFC:
309		case MRT_DEL_MFC:
310		case MRT_VERSION:
311		case MRT_ASSERT:
312			error = ip_mrouter_get(so, sopt);
313			break;
314
315		default:
316			error = ip_ctloutput(so, sopt);
317			break;
318		}
319		break;
320
321	case SOPT_SET:
322		switch (sopt->sopt_name) {
323		case IP_HDRINCL:
324			error = sooptcopyin(sopt, &optval, sizeof optval,
325					    sizeof optval);
326			if (error)
327				break;
328			if (optval)
329				inp->inp_flags |= INP_HDRINCL;
330			else
331				inp->inp_flags &= ~INP_HDRINCL;
332			break;
333
334		case IP_FW_DEL:
335		case IP_FW_FLUSH:
336		case IP_FW_ZERO:
337		case IP_FW_RESETLOG:
338			if (IPFW_LOADED)
339				error = ip_fw_ctl_ptr(sopt);
340			else
341				error = ENOPROTOOPT;
342			break;
343
344		case IP_DUMMYNET_CONFIGURE:
345		case IP_DUMMYNET_DEL:
346		case IP_DUMMYNET_FLUSH:
347			if (DUMMYNET_LOADED)
348				error = ip_dn_ctl_ptr(sopt);
349			else
350				error = ENOPROTOOPT ;
351			break ;
352
353		case IP_RSVP_ON:
354			error = ip_rsvp_init(so);
355			break;
356
357		case IP_RSVP_OFF:
358			error = ip_rsvp_done();
359			break;
360
361			/* XXX - should be combined */
362		case IP_RSVP_VIF_ON:
363			error = ip_rsvp_vif_init(so, sopt);
364			break;
365
366		case IP_RSVP_VIF_OFF:
367			error = ip_rsvp_vif_done(so, sopt);
368			break;
369
370		case MRT_INIT:
371		case MRT_DONE:
372		case MRT_ADD_VIF:
373		case MRT_DEL_VIF:
374		case MRT_ADD_MFC:
375		case MRT_DEL_MFC:
376		case MRT_VERSION:
377		case MRT_ASSERT:
378			error = ip_mrouter_set(so, sopt);
379			break;
380
381		default:
382			error = ip_ctloutput(so, sopt);
383			break;
384		}
385		break;
386	}
387
388	return (error);
389}
390
391/*
392 * This function exists solely to receive the PRC_IFDOWN messages which
393 * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
394 * and calls in_ifadown() to remove all routes corresponding to that address.
395 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
396 * interface routes.
397 */
398void
399rip_ctlinput(cmd, sa, vip)
400	int cmd;
401	struct sockaddr *sa;
402	void *vip;
403{
404	struct in_ifaddr *ia;
405	struct ifnet *ifp;
406	int err;
407	int flags;
408
409	switch (cmd) {
410	case PRC_IFDOWN:
411		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
412			if (ia->ia_ifa.ifa_addr == sa
413			    && (ia->ia_flags & IFA_ROUTE)) {
414				/*
415				 * in_ifscrub kills the interface route.
416				 */
417				in_ifscrub(ia->ia_ifp, ia);
418				/*
419				 * in_ifadown gets rid of all the rest of
420				 * the routes.  This is not quite the right
421				 * thing to do, but at least if we are running
422				 * a routing process they will come back.
423				 */
424				in_ifadown(&ia->ia_ifa, 0);
425				break;
426			}
427		}
428		break;
429
430	case PRC_IFUP:
431		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
432			if (ia->ia_ifa.ifa_addr == sa)
433				break;
434		}
435		if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
436			return;
437		flags = RTF_UP;
438		ifp = ia->ia_ifa.ifa_ifp;
439
440		if ((ifp->if_flags & IFF_LOOPBACK)
441		    || (ifp->if_flags & IFF_POINTOPOINT))
442			flags |= RTF_HOST;
443
444		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
445		if (err == 0)
446			ia->ia_flags |= IFA_ROUTE;
447		break;
448	}
449}
450
451u_long	rip_sendspace = RIPSNDQ;
452u_long	rip_recvspace = RIPRCVQ;
453
454SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
455    &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
456SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
457    &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
458
459static int
460rip_attach(struct socket *so, int proto, struct thread *td)
461{
462	struct inpcb *inp;
463	int error, s;
464
465	inp = sotoinpcb(so);
466	if (inp)
467		panic("rip_attach");
468	if (td && (error = suser_td(td)) != 0)
469		return error;
470
471	error = soreserve(so, rip_sendspace, rip_recvspace);
472	if (error)
473		return error;
474	s = splnet();
475	error = in_pcballoc(so, &ripcbinfo, td);
476	splx(s);
477	if (error)
478		return error;
479	inp = (struct inpcb *)so->so_pcb;
480	inp->inp_vflag |= INP_IPV4;
481	inp->inp_ip_p = proto;
482	inp->inp_ip_ttl = ip_defttl;
483	return 0;
484}
485
486static int
487rip_detach(struct socket *so)
488{
489	struct inpcb *inp;
490
491	inp = sotoinpcb(so);
492	if (inp == 0)
493		panic("rip_detach");
494	if (so == ip_mrouter)
495		ip_mrouter_done();
496	ip_rsvp_force_done(so);
497	if (so == ip_rsvpd)
498		ip_rsvp_done();
499	in_pcbdetach(inp);
500	return 0;
501}
502
503static int
504rip_abort(struct socket *so)
505{
506	soisdisconnected(so);
507	return rip_detach(so);
508}
509
510static int
511rip_disconnect(struct socket *so)
512{
513	if ((so->so_state & SS_ISCONNECTED) == 0)
514		return ENOTCONN;
515	return rip_abort(so);
516}
517
518static int
519rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
520{
521	struct inpcb *inp = sotoinpcb(so);
522	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
523
524	if (nam->sa_len != sizeof(*addr))
525		return EINVAL;
526
527	if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
528				    (addr->sin_family != AF_IMPLINK)) ||
529	    (addr->sin_addr.s_addr &&
530	     ifa_ifwithaddr((struct sockaddr *)addr) == 0))
531		return EADDRNOTAVAIL;
532	inp->inp_laddr = addr->sin_addr;
533	return 0;
534}
535
536static int
537rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
538{
539	struct inpcb *inp = sotoinpcb(so);
540	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
541
542	if (nam->sa_len != sizeof(*addr))
543		return EINVAL;
544	if (TAILQ_EMPTY(&ifnet))
545		return EADDRNOTAVAIL;
546	if ((addr->sin_family != AF_INET) &&
547	    (addr->sin_family != AF_IMPLINK))
548		return EAFNOSUPPORT;
549	inp->inp_faddr = addr->sin_addr;
550	soisconnected(so);
551	return 0;
552}
553
554static int
555rip_shutdown(struct socket *so)
556{
557	socantsendmore(so);
558	return 0;
559}
560
561static int
562rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
563	 struct mbuf *control, struct thread *td)
564{
565	struct inpcb *inp = sotoinpcb(so);
566	register u_long dst;
567
568	if (so->so_state & SS_ISCONNECTED) {
569		if (nam) {
570			m_freem(m);
571			return EISCONN;
572		}
573		dst = inp->inp_faddr.s_addr;
574	} else {
575		if (nam == NULL) {
576			m_freem(m);
577			return ENOTCONN;
578		}
579		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
580	}
581	return rip_output(m, so, dst);
582}
583
584static int
585rip_pcblist(SYSCTL_HANDLER_ARGS)
586{
587	int error, i, n, s;
588	struct inpcb *inp, **inp_list;
589	inp_gen_t gencnt;
590	struct xinpgen xig;
591
592	/*
593	 * The process of preparing the TCB list is too time-consuming and
594	 * resource-intensive to repeat twice on every request.
595	 */
596	if (req->oldptr == 0) {
597		n = ripcbinfo.ipi_count;
598		req->oldidx = 2 * (sizeof xig)
599			+ (n + n/8) * sizeof(struct xinpcb);
600		return 0;
601	}
602
603	if (req->newptr != 0)
604		return EPERM;
605
606	/*
607	 * OK, now we're committed to doing something.
608	 */
609	s = splnet();
610	gencnt = ripcbinfo.ipi_gencnt;
611	n = ripcbinfo.ipi_count;
612	splx(s);
613
614	xig.xig_len = sizeof xig;
615	xig.xig_count = n;
616	xig.xig_gen = gencnt;
617	xig.xig_sogen = so_gencnt;
618	error = SYSCTL_OUT(req, &xig, sizeof xig);
619	if (error)
620		return error;
621
622	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
623	if (inp_list == 0)
624		return ENOMEM;
625
626	s = splnet();
627	for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n;
628	     inp = LIST_NEXT(inp, inp_list)) {
629		if (inp->inp_gencnt <= gencnt) {
630			if (cr_cansee(req->p->p_ucred,
631			    inp->inp_socket->so_cred))
632				continue;
633			inp_list[i++] = inp;
634		}
635	}
636	splx(s);
637	n = i;
638
639	error = 0;
640	for (i = 0; i < n; i++) {
641		inp = inp_list[i];
642		if (inp->inp_gencnt <= gencnt) {
643			struct xinpcb xi;
644			xi.xi_len = sizeof xi;
645			/* XXX should avoid extra copy */
646			bcopy(inp, &xi.xi_inp, sizeof *inp);
647			if (inp->inp_socket)
648				sotoxsocket(inp->inp_socket, &xi.xi_socket);
649			error = SYSCTL_OUT(req, &xi, sizeof xi);
650		}
651	}
652	if (!error) {
653		/*
654		 * Give the user an updated idea of our state.
655		 * If the generation differs from what we told
656		 * her before, she knows that something happened
657		 * while we were processing this request, and it
658		 * might be necessary to retry.
659		 */
660		s = splnet();
661		xig.xig_gen = ripcbinfo.ipi_gencnt;
662		xig.xig_sogen = so_gencnt;
663		xig.xig_count = ripcbinfo.ipi_count;
664		splx(s);
665		error = SYSCTL_OUT(req, &xig, sizeof xig);
666	}
667	free(inp_list, M_TEMP);
668	return error;
669}
670
671SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
672	    rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
673
674struct pr_usrreqs rip_usrreqs = {
675	rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
676	pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
677	pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
678	pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
679	in_setsockaddr, sosend, soreceive, sopoll
680};
681