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