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