raw_ip.c revision 118622
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 118622 2003-08-07 18:16:59Z hsu $
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), M_TRYWAIT);
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 *
332 * Note that access to all of the IP administrative functions here is
333 * implicitly protected by suser() as gaining access to a raw socket
334 * requires either that the thread pass a suser() check, or that it be
335 * passed a raw socket by another thread that has passed a suser() check.
336 * If FreeBSD moves to a more fine-grained access control mechanism,
337 * additional checks will need to be placed here if the raw IP attachment
338 * check is not equivilent the the check required for these
339 * administrative operations; in some cases, these checks are already
340 * present.
341 */
342int
343rip_ctloutput(so, sopt)
344	struct socket *so;
345	struct sockopt *sopt;
346{
347	struct	inpcb *inp = sotoinpcb(so);
348	int	error, optval;
349
350	if (sopt->sopt_level != IPPROTO_IP)
351		return (EINVAL);
352
353	error = 0;
354
355	switch (sopt->sopt_dir) {
356	case SOPT_GET:
357		switch (sopt->sopt_name) {
358		case IP_HDRINCL:
359			optval = inp->inp_flags & INP_HDRINCL;
360			error = sooptcopyout(sopt, &optval, sizeof optval);
361			break;
362
363		case IP_FW_ADD:	/* ADD actually returns the body... */
364		case IP_FW_GET:
365			if (IPFW_LOADED)
366				error = ip_fw_ctl_ptr(sopt);
367			else
368				error = ENOPROTOOPT;
369			break;
370
371		case IP_DUMMYNET_GET:
372			if (DUMMYNET_LOADED)
373				error = ip_dn_ctl_ptr(sopt);
374			else
375				error = ENOPROTOOPT;
376			break ;
377
378		case MRT_INIT:
379		case MRT_DONE:
380		case MRT_ADD_VIF:
381		case MRT_DEL_VIF:
382		case MRT_ADD_MFC:
383		case MRT_DEL_MFC:
384		case MRT_VERSION:
385		case MRT_ASSERT:
386		case MRT_API_SUPPORT:
387		case MRT_API_CONFIG:
388		case MRT_ADD_BW_UPCALL:
389		case MRT_DEL_BW_UPCALL:
390			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
391				EOPNOTSUPP;
392			break;
393
394		default:
395			error = ip_ctloutput(so, sopt);
396			break;
397		}
398		break;
399
400	case SOPT_SET:
401		switch (sopt->sopt_name) {
402		case IP_HDRINCL:
403			error = sooptcopyin(sopt, &optval, sizeof optval,
404					    sizeof optval);
405			if (error)
406				break;
407			if (optval)
408				inp->inp_flags |= INP_HDRINCL;
409			else
410				inp->inp_flags &= ~INP_HDRINCL;
411			break;
412
413		case IP_FW_ADD:
414		case IP_FW_DEL:
415		case IP_FW_FLUSH:
416		case IP_FW_ZERO:
417		case IP_FW_RESETLOG:
418			if (IPFW_LOADED)
419				error = ip_fw_ctl_ptr(sopt);
420			else
421				error = ENOPROTOOPT;
422			break;
423
424		case IP_DUMMYNET_CONFIGURE:
425		case IP_DUMMYNET_DEL:
426		case IP_DUMMYNET_FLUSH:
427			if (DUMMYNET_LOADED)
428				error = ip_dn_ctl_ptr(sopt);
429			else
430				error = ENOPROTOOPT ;
431			break ;
432
433		case IP_RSVP_ON:
434			error = ip_rsvp_init(so);
435			break;
436
437		case IP_RSVP_OFF:
438			error = ip_rsvp_done();
439			break;
440
441		case IP_RSVP_VIF_ON:
442		case IP_RSVP_VIF_OFF:
443			error = ip_rsvp_vif ?
444				ip_rsvp_vif(so, sopt) : EINVAL;
445			break;
446
447		case MRT_INIT:
448		case MRT_DONE:
449		case MRT_ADD_VIF:
450		case MRT_DEL_VIF:
451		case MRT_ADD_MFC:
452		case MRT_DEL_MFC:
453		case MRT_VERSION:
454		case MRT_ASSERT:
455		case MRT_API_SUPPORT:
456		case MRT_API_CONFIG:
457		case MRT_ADD_BW_UPCALL:
458		case MRT_DEL_BW_UPCALL:
459			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
460					EOPNOTSUPP;
461			break;
462
463		default:
464			error = ip_ctloutput(so, sopt);
465			break;
466		}
467		break;
468	}
469
470	return (error);
471}
472
473/*
474 * This function exists solely to receive the PRC_IFDOWN messages which
475 * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
476 * and calls in_ifadown() to remove all routes corresponding to that address.
477 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
478 * interface routes.
479 */
480void
481rip_ctlinput(cmd, sa, vip)
482	int cmd;
483	struct sockaddr *sa;
484	void *vip;
485{
486	struct in_ifaddr *ia;
487	struct ifnet *ifp;
488	int err;
489	int flags;
490
491	switch (cmd) {
492	case PRC_IFDOWN:
493		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
494			if (ia->ia_ifa.ifa_addr == sa
495			    && (ia->ia_flags & IFA_ROUTE)) {
496				/*
497				 * in_ifscrub kills the interface route.
498				 */
499				in_ifscrub(ia->ia_ifp, ia);
500				/*
501				 * in_ifadown gets rid of all the rest of
502				 * the routes.  This is not quite the right
503				 * thing to do, but at least if we are running
504				 * a routing process they will come back.
505				 */
506				in_ifadown(&ia->ia_ifa, 0);
507				break;
508			}
509		}
510		break;
511
512	case PRC_IFUP:
513		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
514			if (ia->ia_ifa.ifa_addr == sa)
515				break;
516		}
517		if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
518			return;
519		flags = RTF_UP;
520		ifp = ia->ia_ifa.ifa_ifp;
521
522		if ((ifp->if_flags & IFF_LOOPBACK)
523		    || (ifp->if_flags & IFF_POINTOPOINT))
524			flags |= RTF_HOST;
525
526		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
527		if (err == 0)
528			ia->ia_flags |= IFA_ROUTE;
529		break;
530	}
531}
532
533u_long	rip_sendspace = RIPSNDQ;
534u_long	rip_recvspace = RIPRCVQ;
535int	rip_olddiverterror = 1;
536
537SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
538    &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
539SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
540    &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
541SYSCTL_INT(_net_inet_raw, OID_AUTO, olddiverterror, CTLFLAG_RW,
542    &rip_olddiverterror, 0, "Return an error when creating an 'old' DIVERT socket");
543
544static int
545rip_attach(struct socket *so, int proto, struct thread *td)
546{
547	struct inpcb *inp;
548	int error, s;
549
550	inp = sotoinpcb(so);
551	if (inp)
552		panic("rip_attach");
553	if (td && (error = suser(td)) != 0)
554		return error;
555
556	if (proto >= IPPROTO_MAX || proto < 0)
557		return EPROTONOSUPPORT;
558
559	/* To be removed before 5.2 */
560	if (rip_olddiverterror && proto == IPPROTO_OLD_DIVERT) {
561		printf("Old IPDIVERT program needs to be recompiled, or new IP proto 254 user needs sysctl net.inet.raw.olddiverterror=0\n");
562		return EPROTONOSUPPORT;
563	}
564
565	error = soreserve(so, rip_sendspace, rip_recvspace);
566	if (error)
567		return error;
568	s = splnet();
569	error = in_pcballoc(so, &ripcbinfo, td);
570	splx(s);
571	if (error)
572		return error;
573	inp = (struct inpcb *)so->so_pcb;
574	inp->inp_vflag |= INP_IPV4;
575	inp->inp_ip_p = proto;
576	inp->inp_ip_ttl = ip_defttl;
577	return 0;
578}
579
580static int
581rip_detach(struct socket *so)
582{
583	struct inpcb *inp;
584
585	inp = sotoinpcb(so);
586	if (inp == 0)
587		panic("rip_detach");
588	if (so == ip_mrouter && ip_mrouter_done)
589		ip_mrouter_done();
590	if (ip_rsvp_force_done)
591		ip_rsvp_force_done(so);
592	if (so == ip_rsvpd)
593		ip_rsvp_done();
594	in_pcbdetach(inp);
595	return 0;
596}
597
598static int
599rip_abort(struct socket *so)
600{
601	soisdisconnected(so);
602	if (so->so_state & SS_NOFDREF)
603		return rip_detach(so);
604	return 0;
605}
606
607static int
608rip_disconnect(struct socket *so)
609{
610	if ((so->so_state & SS_ISCONNECTED) == 0)
611		return ENOTCONN;
612	return rip_abort(so);
613}
614
615static int
616rip_bind(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
624	if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
625				    (addr->sin_family != AF_IMPLINK)) ||
626	    (addr->sin_addr.s_addr &&
627	     ifa_ifwithaddr((struct sockaddr *)addr) == 0))
628		return EADDRNOTAVAIL;
629	inp->inp_laddr = addr->sin_addr;
630	return 0;
631}
632
633static int
634rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
635{
636	struct inpcb *inp = sotoinpcb(so);
637	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
638
639	if (nam->sa_len != sizeof(*addr))
640		return EINVAL;
641	if (TAILQ_EMPTY(&ifnet))
642		return EADDRNOTAVAIL;
643	if ((addr->sin_family != AF_INET) &&
644	    (addr->sin_family != AF_IMPLINK))
645		return EAFNOSUPPORT;
646	inp->inp_faddr = addr->sin_addr;
647	soisconnected(so);
648	return 0;
649}
650
651static int
652rip_shutdown(struct socket *so)
653{
654	socantsendmore(so);
655	return 0;
656}
657
658static int
659rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
660	 struct mbuf *control, struct thread *td)
661{
662	struct inpcb *inp = sotoinpcb(so);
663	register u_long dst;
664
665	if (so->so_state & SS_ISCONNECTED) {
666		if (nam) {
667			m_freem(m);
668			return EISCONN;
669		}
670		dst = inp->inp_faddr.s_addr;
671	} else {
672		if (nam == NULL) {
673			m_freem(m);
674			return ENOTCONN;
675		}
676		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
677	}
678	return rip_output(m, so, dst);
679}
680
681static int
682rip_pcblist(SYSCTL_HANDLER_ARGS)
683{
684	int error, i, n, s;
685	struct inpcb *inp, **inp_list;
686	inp_gen_t gencnt;
687	struct xinpgen xig;
688
689	/*
690	 * The process of preparing the TCB list is too time-consuming and
691	 * resource-intensive to repeat twice on every request.
692	 */
693	if (req->oldptr == 0) {
694		n = ripcbinfo.ipi_count;
695		req->oldidx = 2 * (sizeof xig)
696			+ (n + n/8) * sizeof(struct xinpcb);
697		return 0;
698	}
699
700	if (req->newptr != 0)
701		return EPERM;
702
703	/*
704	 * OK, now we're committed to doing something.
705	 */
706	s = splnet();
707	gencnt = ripcbinfo.ipi_gencnt;
708	n = ripcbinfo.ipi_count;
709	splx(s);
710
711	xig.xig_len = sizeof xig;
712	xig.xig_count = n;
713	xig.xig_gen = gencnt;
714	xig.xig_sogen = so_gencnt;
715	error = SYSCTL_OUT(req, &xig, sizeof xig);
716	if (error)
717		return error;
718
719	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
720	if (inp_list == 0)
721		return ENOMEM;
722
723	s = splnet();
724	for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n;
725	     inp = LIST_NEXT(inp, inp_list)) {
726		if (inp->inp_gencnt <= gencnt) {
727			if (cr_canseesocket(req->td->td_ucred,
728			    inp->inp_socket))
729				continue;
730			inp_list[i++] = inp;
731		}
732	}
733	splx(s);
734	n = i;
735
736	error = 0;
737	for (i = 0; i < n; i++) {
738		inp = inp_list[i];
739		if (inp->inp_gencnt <= gencnt) {
740			struct xinpcb xi;
741			xi.xi_len = sizeof xi;
742			/* XXX should avoid extra copy */
743			bcopy(inp, &xi.xi_inp, sizeof *inp);
744			if (inp->inp_socket)
745				sotoxsocket(inp->inp_socket, &xi.xi_socket);
746			error = SYSCTL_OUT(req, &xi, sizeof xi);
747		}
748	}
749	if (!error) {
750		/*
751		 * Give the user an updated idea of our state.
752		 * If the generation differs from what we told
753		 * her before, she knows that something happened
754		 * while we were processing this request, and it
755		 * might be necessary to retry.
756		 */
757		s = splnet();
758		xig.xig_gen = ripcbinfo.ipi_gencnt;
759		xig.xig_sogen = so_gencnt;
760		xig.xig_count = ripcbinfo.ipi_count;
761		splx(s);
762		error = SYSCTL_OUT(req, &xig, sizeof xig);
763	}
764	free(inp_list, M_TEMP);
765	return error;
766}
767
768/*
769 * This is the wrapper function for in_setsockaddr.  We just pass down
770 * the pcbinfo for in_setpeeraddr to lock.
771 */
772static int
773rip_sockaddr(struct socket *so, struct sockaddr **nam)
774{
775	return (in_setsockaddr(so, nam, &ripcbinfo));
776}
777
778/*
779 * This is the wrapper function for in_setpeeraddr.  We just pass down
780 * the pcbinfo for in_setpeeraddr to lock.
781 */
782static int
783rip_peeraddr(struct socket *so, struct sockaddr **nam)
784{
785	return (in_setpeeraddr(so, nam, &ripcbinfo));
786}
787
788
789SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
790	    rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
791
792struct pr_usrreqs rip_usrreqs = {
793	rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
794	pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
795	pru_listen_notsupp, rip_peeraddr, pru_rcvd_notsupp,
796	pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
797	rip_sockaddr, sosend, soreceive, sopoll
798};
799