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