xref: /freebsd/sys/netinet/raw_ip.c (revision acd3428b7d3e94cef0e1881c868cb4b131d4ff41)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
30  * $FreeBSD$
31  */
32 
33 #include "opt_inet6.h"
34 #include "opt_ipsec.h"
35 #include "opt_mac.h"
36 
37 #include <sys/param.h>
38 #include <sys/jail.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/protosw.h>
46 #include <sys/signalvar.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sx.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
52 
53 #include <vm/uma.h>
54 
55 #include <net/if.h>
56 #include <net/route.h>
57 
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/in_var.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/ip_mroute.h>
65 
66 #include <netinet/ip_fw.h>
67 #include <netinet/ip_dummynet.h>
68 
69 #ifdef FAST_IPSEC
70 #include <netipsec/ipsec.h>
71 #endif /*FAST_IPSEC*/
72 
73 #ifdef IPSEC
74 #include <netinet6/ipsec.h>
75 #endif /*IPSEC*/
76 
77 #include <security/mac/mac_framework.h>
78 
79 struct	inpcbhead ripcb;
80 struct	inpcbinfo ripcbinfo;
81 
82 /* control hooks for ipfw and dummynet */
83 ip_fw_ctl_t *ip_fw_ctl_ptr = NULL;
84 ip_dn_ctl_t *ip_dn_ctl_ptr = NULL;
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, int off);
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 q.
120  */
121 static void
122 rip_zone_change(void *tag)
123 {
124 
125 	uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
126 }
127 
128 static int
129 rip_inpcb_init(void *mem, int size, int flags)
130 {
131 	struct inpcb *inp = (struct inpcb *) mem;
132 	INP_LOCK_INIT(inp, "inp", "rawinp");
133 	return (0);
134 }
135 
136 void
137 rip_init()
138 {
139 	INP_INFO_LOCK_INIT(&ripcbinfo, "rip");
140 	LIST_INIT(&ripcb);
141 	ripcbinfo.listhead = &ripcb;
142 	/*
143 	 * XXX We don't use the hash list for raw IP, but it's easier
144 	 * to allocate a one entry hash list than it is to check all
145 	 * over the place for hashbase == NULL.
146 	 */
147 	ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
148 	ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
149 	ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
150 	    NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
151 	uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
152 	EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change,
153 		NULL, EVENTHANDLER_PRI_ANY);
154 }
155 
156 static struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
157 
158 static int
159 raw_append(struct inpcb *last, struct ip *ip, struct mbuf *n)
160 {
161 	int policyfail = 0;
162 
163 	INP_LOCK_ASSERT(last);
164 
165 #if defined(IPSEC) || defined(FAST_IPSEC)
166 	/* check AH/ESP integrity. */
167 	if (ipsec4_in_reject(n, last)) {
168 		policyfail = 1;
169 #ifdef IPSEC
170 		ipsecstat.in_polvio++;
171 #endif /*IPSEC*/
172 		/* do not inject data to pcb */
173 	}
174 #endif /*IPSEC || FAST_IPSEC*/
175 #ifdef MAC
176 	if (!policyfail && mac_check_inpcb_deliver(last, n) != 0)
177 		policyfail = 1;
178 #endif
179 	/* Check the minimum TTL for socket. */
180 	if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
181 		policyfail = 1;
182 	if (!policyfail) {
183 		struct mbuf *opts = NULL;
184 		struct socket *so;
185 
186 		so = last->inp_socket;
187 		if ((last->inp_flags & INP_CONTROLOPTS) ||
188 		    (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
189 			ip_savecontrol(last, &opts, ip, n);
190 		SOCKBUF_LOCK(&so->so_rcv);
191 		if (sbappendaddr_locked(&so->so_rcv,
192 		    (struct sockaddr *)&ripsrc, n, opts) == 0) {
193 			/* should notify about lost packet */
194 			m_freem(n);
195 			if (opts)
196 				m_freem(opts);
197 			SOCKBUF_UNLOCK(&so->so_rcv);
198 		} else
199 			sorwakeup_locked(so);
200 	} else
201 		m_freem(n);
202 	return policyfail;
203 }
204 
205 /*
206  * Setup generic address and protocol structures
207  * for raw_input routine, then pass them along with
208  * mbuf chain.
209  */
210 void
211 rip_input(struct mbuf *m, int off)
212 {
213 	struct ip *ip = mtod(m, struct ip *);
214 	int proto = ip->ip_p;
215 	struct inpcb *inp, *last;
216 
217 	INP_INFO_RLOCK(&ripcbinfo);
218 	ripsrc.sin_addr = ip->ip_src;
219 	last = NULL;
220 	LIST_FOREACH(inp, &ripcb, inp_list) {
221 		INP_LOCK(inp);
222 		if (inp->inp_ip_p && inp->inp_ip_p != proto) {
223 	docontinue:
224 			INP_UNLOCK(inp);
225 			continue;
226 		}
227 #ifdef INET6
228 		if ((inp->inp_vflag & INP_IPV4) == 0)
229 			goto docontinue;
230 #endif
231 		if (inp->inp_laddr.s_addr &&
232 		    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
233 			goto docontinue;
234 		if (inp->inp_faddr.s_addr &&
235 		    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
236 			goto docontinue;
237 		if (jailed(inp->inp_socket->so_cred))
238 			if (htonl(prison_getip(inp->inp_socket->so_cred)) !=
239 			    ip->ip_dst.s_addr)
240 				goto docontinue;
241 		if (last) {
242 			struct mbuf *n;
243 
244 			n = m_copy(m, 0, (int)M_COPYALL);
245 			if (n != NULL)
246 				(void) raw_append(last, ip, n);
247 			/* XXX count dropped packet */
248 			INP_UNLOCK(last);
249 		}
250 		last = inp;
251 	}
252 	if (last != NULL) {
253 		if (raw_append(last, ip, m) != 0)
254 			ipstat.ips_delivered--;
255 		INP_UNLOCK(last);
256 	} else {
257 		m_freem(m);
258 		ipstat.ips_noproto++;
259 		ipstat.ips_delivered--;
260 	}
261 	INP_INFO_RUNLOCK(&ripcbinfo);
262 }
263 
264 /*
265  * Generate IP header and pass packet to ip_output.
266  * Tack on options user may have setup with control call.
267  */
268 int
269 rip_output(struct mbuf *m, struct socket *so, u_long dst)
270 {
271 	struct ip *ip;
272 	int error;
273 	struct inpcb *inp = sotoinpcb(so);
274 	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
275 	    IP_ALLOWBROADCAST;
276 
277 	/*
278 	 * If the user handed us a complete IP packet, use it.
279 	 * Otherwise, allocate an mbuf for a header and fill it in.
280 	 */
281 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
282 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
283 			m_freem(m);
284 			return(EMSGSIZE);
285 		}
286 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
287 		if (m == NULL)
288 			return(ENOBUFS);
289 
290 		INP_LOCK(inp);
291 		ip = mtod(m, struct ip *);
292 		ip->ip_tos = inp->inp_ip_tos;
293 		if (inp->inp_flags & INP_DONTFRAG)
294 			ip->ip_off = IP_DF;
295 		else
296 			ip->ip_off = 0;
297 		ip->ip_p = inp->inp_ip_p;
298 		ip->ip_len = m->m_pkthdr.len;
299 		if (jailed(inp->inp_socket->so_cred))
300 			ip->ip_src.s_addr =
301 			    htonl(prison_getip(inp->inp_socket->so_cred));
302 		else
303 			ip->ip_src = inp->inp_laddr;
304 		ip->ip_dst.s_addr = dst;
305 		ip->ip_ttl = inp->inp_ip_ttl;
306 	} else {
307 		if (m->m_pkthdr.len > IP_MAXPACKET) {
308 			m_freem(m);
309 			return(EMSGSIZE);
310 		}
311 		INP_LOCK(inp);
312 		ip = mtod(m, struct ip *);
313 		if (jailed(inp->inp_socket->so_cred)) {
314 			if (ip->ip_src.s_addr !=
315 			    htonl(prison_getip(inp->inp_socket->so_cred))) {
316 				INP_UNLOCK(inp);
317 				m_freem(m);
318 				return (EPERM);
319 			}
320 		}
321 		/* don't allow both user specified and setsockopt options,
322 		   and don't allow packet length sizes that will crash */
323 		if (((ip->ip_hl != (sizeof (*ip) >> 2))
324 		     && inp->inp_options)
325 		    || (ip->ip_len > m->m_pkthdr.len)
326 		    || (ip->ip_len < (ip->ip_hl << 2))) {
327 			INP_UNLOCK(inp);
328 			m_freem(m);
329 			return EINVAL;
330 		}
331 		if (ip->ip_id == 0)
332 			ip->ip_id = ip_newid();
333 		/* XXX prevent ip_output from overwriting header fields */
334 		flags |= IP_RAWOUTPUT;
335 		ipstat.ips_rawout++;
336 	}
337 
338 	if (inp->inp_flags & INP_ONESBCAST)
339 		flags |= IP_SENDONES;
340 
341 #ifdef MAC
342 	mac_create_mbuf_from_inpcb(inp, m);
343 #endif
344 
345 	error = ip_output(m, inp->inp_options, NULL, flags,
346 	    inp->inp_moptions, inp);
347 	INP_UNLOCK(inp);
348 	return error;
349 }
350 
351 /*
352  * Raw IP socket option processing.
353  *
354  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
355  * only be created by a privileged process, and as such, socket option
356  * operations to manage system properties on any raw socket were allowed to
357  * take place without explicit additional access control checks.  However,
358  * raw sockets can now also be created in jail(), and therefore explicit
359  * checks are now required.  Likewise, raw sockets can be used by a process
360  * after it gives up privilege, so some caution is required.  For options
361  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
362  * performed in ip_ctloutput() and therefore no check occurs here.
363  * Unilaterally checking suser() here breaks normal IP socket option
364  * operations on raw sockets.
365  *
366  * When adding new socket options here, make sure to add access control
367  * checks here as necessary.
368  */
369 int
370 rip_ctloutput(struct socket *so, struct sockopt *sopt)
371 {
372 	struct	inpcb *inp = sotoinpcb(so);
373 	int	error, optval;
374 
375 	if (sopt->sopt_level != IPPROTO_IP)
376 		return (EINVAL);
377 
378 	error = 0;
379 	switch (sopt->sopt_dir) {
380 	case SOPT_GET:
381 		switch (sopt->sopt_name) {
382 		case IP_HDRINCL:
383 			optval = inp->inp_flags & INP_HDRINCL;
384 			error = sooptcopyout(sopt, &optval, sizeof optval);
385 			break;
386 
387 		case IP_FW_ADD:	/* ADD actually returns the body... */
388 		case IP_FW_GET:
389 		case IP_FW_TABLE_GETSIZE:
390 		case IP_FW_TABLE_LIST:
391 			/*
392 			 * XXXRW: Isn't this checked one layer down?  Yes, it
393 			 * is.
394 			 */
395 			error = priv_check(curthread, PRIV_NETINET_IPFW);
396 			if (error != 0)
397 				return (error);
398 			if (ip_fw_ctl_ptr != NULL)
399 				error = ip_fw_ctl_ptr(sopt);
400 			else
401 				error = ENOPROTOOPT;
402 			break;
403 
404 		case IP_DUMMYNET_GET:
405 			error = priv_check(curthread, PRIV_NETINET_DUMMYNET);
406 			if (error != 0)
407 				return (error);
408 			if (ip_dn_ctl_ptr != NULL)
409 				error = ip_dn_ctl_ptr(sopt);
410 			else
411 				error = ENOPROTOOPT;
412 			break ;
413 
414 		case MRT_INIT:
415 		case MRT_DONE:
416 		case MRT_ADD_VIF:
417 		case MRT_DEL_VIF:
418 		case MRT_ADD_MFC:
419 		case MRT_DEL_MFC:
420 		case MRT_VERSION:
421 		case MRT_ASSERT:
422 		case MRT_API_SUPPORT:
423 		case MRT_API_CONFIG:
424 		case MRT_ADD_BW_UPCALL:
425 		case MRT_DEL_BW_UPCALL:
426 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
427 			if (error != 0)
428 				return (error);
429 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
430 				EOPNOTSUPP;
431 			break;
432 
433 		default:
434 			error = ip_ctloutput(so, sopt);
435 			break;
436 		}
437 		break;
438 
439 	case SOPT_SET:
440 		switch (sopt->sopt_name) {
441 		case IP_HDRINCL:
442 			error = sooptcopyin(sopt, &optval, sizeof optval,
443 					    sizeof optval);
444 			if (error)
445 				break;
446 			if (optval)
447 				inp->inp_flags |= INP_HDRINCL;
448 			else
449 				inp->inp_flags &= ~INP_HDRINCL;
450 			break;
451 
452 		case IP_FW_ADD:
453 		case IP_FW_DEL:
454 		case IP_FW_FLUSH:
455 		case IP_FW_ZERO:
456 		case IP_FW_RESETLOG:
457 		case IP_FW_TABLE_ADD:
458 		case IP_FW_TABLE_DEL:
459 		case IP_FW_TABLE_FLUSH:
460 			/*
461 			 * XXXRW: Isn't this checked one layer down?
462 			 */
463 			error = priv_check(curthread, PRIV_NETINET_IPFW);
464 			if (error != 0)
465 				return (error);
466 			if (ip_fw_ctl_ptr != NULL)
467 				error = ip_fw_ctl_ptr(sopt);
468 			else
469 				error = ENOPROTOOPT;
470 			break;
471 
472 		case IP_DUMMYNET_CONFIGURE:
473 		case IP_DUMMYNET_DEL:
474 		case IP_DUMMYNET_FLUSH:
475 			error = priv_check(curthread, PRIV_NETINET_DUMMYNET);
476 			if (error != 0)
477 				return (error);
478 			if (ip_dn_ctl_ptr != NULL)
479 				error = ip_dn_ctl_ptr(sopt);
480 			else
481 				error = ENOPROTOOPT ;
482 			break ;
483 
484 		case IP_RSVP_ON:
485 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
486 			if (error != 0)
487 				return (error);
488 			error = ip_rsvp_init(so);
489 			break;
490 
491 		case IP_RSVP_OFF:
492 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
493 			if (error != 0)
494 				return (error);
495 			error = ip_rsvp_done();
496 			break;
497 
498 		case IP_RSVP_VIF_ON:
499 		case IP_RSVP_VIF_OFF:
500 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
501 			if (error != 0)
502 				return (error);
503 			error = ip_rsvp_vif ?
504 				ip_rsvp_vif(so, sopt) : EINVAL;
505 			break;
506 
507 		case MRT_INIT:
508 		case MRT_DONE:
509 		case MRT_ADD_VIF:
510 		case MRT_DEL_VIF:
511 		case MRT_ADD_MFC:
512 		case MRT_DEL_MFC:
513 		case MRT_VERSION:
514 		case MRT_ASSERT:
515 		case MRT_API_SUPPORT:
516 		case MRT_API_CONFIG:
517 		case MRT_ADD_BW_UPCALL:
518 		case MRT_DEL_BW_UPCALL:
519 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
520 			if (error != 0)
521 				return (error);
522 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
523 					EOPNOTSUPP;
524 			break;
525 
526 		default:
527 			error = ip_ctloutput(so, sopt);
528 			break;
529 		}
530 		break;
531 	}
532 
533 	return (error);
534 }
535 
536 /*
537  * This function exists solely to receive the PRC_IFDOWN messages which
538  * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
539  * and calls in_ifadown() to remove all routes corresponding to that address.
540  * It also receives the PRC_IFUP messages from if_up() and reinstalls the
541  * interface routes.
542  */
543 void
544 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
545 {
546 	struct in_ifaddr *ia;
547 	struct ifnet *ifp;
548 	int err;
549 	int flags;
550 
551 	switch (cmd) {
552 	case PRC_IFDOWN:
553 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
554 			if (ia->ia_ifa.ifa_addr == sa
555 			    && (ia->ia_flags & IFA_ROUTE)) {
556 				/*
557 				 * in_ifscrub kills the interface route.
558 				 */
559 				in_ifscrub(ia->ia_ifp, ia);
560 				/*
561 				 * in_ifadown gets rid of all the rest of
562 				 * the routes.  This is not quite the right
563 				 * thing to do, but at least if we are running
564 				 * a routing process they will come back.
565 				 */
566 				in_ifadown(&ia->ia_ifa, 0);
567 				break;
568 			}
569 		}
570 		break;
571 
572 	case PRC_IFUP:
573 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
574 			if (ia->ia_ifa.ifa_addr == sa)
575 				break;
576 		}
577 		if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
578 			return;
579 		flags = RTF_UP;
580 		ifp = ia->ia_ifa.ifa_ifp;
581 
582 		if ((ifp->if_flags & IFF_LOOPBACK)
583 		    || (ifp->if_flags & IFF_POINTOPOINT))
584 			flags |= RTF_HOST;
585 
586 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
587 		if (err == 0)
588 			ia->ia_flags |= IFA_ROUTE;
589 		break;
590 	}
591 }
592 
593 u_long	rip_sendspace = RIPSNDQ;
594 u_long	rip_recvspace = RIPRCVQ;
595 
596 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
597     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
598 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
599     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
600 
601 static int
602 rip_attach(struct socket *so, int proto, struct thread *td)
603 {
604 	struct inpcb *inp;
605 	int error;
606 
607 	inp = sotoinpcb(so);
608 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
609 	/*
610 	 * XXXRW: Centralize privilege decision in kern_jail.c.
611 	 */
612 	if (jailed(td->td_ucred) && !jail_allow_raw_sockets)
613 		return (EPERM);
614 	error = priv_check_cred(td->td_ucred, PRIV_NETINET_RAW,
615 	    SUSER_ALLOWJAIL);
616 	if (error)
617 		return error;
618 	if (proto >= IPPROTO_MAX || proto < 0)
619 		return EPROTONOSUPPORT;
620 	error = soreserve(so, rip_sendspace, rip_recvspace);
621 	if (error)
622 		return error;
623 	INP_INFO_WLOCK(&ripcbinfo);
624 	error = in_pcballoc(so, &ripcbinfo);
625 	if (error) {
626 		INP_INFO_WUNLOCK(&ripcbinfo);
627 		return error;
628 	}
629 	inp = (struct inpcb *)so->so_pcb;
630 	INP_INFO_WUNLOCK(&ripcbinfo);
631 	inp->inp_vflag |= INP_IPV4;
632 	inp->inp_ip_p = proto;
633 	inp->inp_ip_ttl = ip_defttl;
634 	INP_UNLOCK(inp);
635 	return 0;
636 }
637 
638 static void
639 rip_detach(struct socket *so)
640 {
641 	struct inpcb *inp;
642 
643 	inp = sotoinpcb(so);
644 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
645 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
646 	    ("rip_detach: not closed"));
647 
648 	INP_INFO_WLOCK(&ripcbinfo);
649 	INP_LOCK(inp);
650 	if (so == ip_mrouter && ip_mrouter_done)
651 		ip_mrouter_done();
652 	if (ip_rsvp_force_done)
653 		ip_rsvp_force_done(so);
654 	if (so == ip_rsvpd)
655 		ip_rsvp_done();
656 	in_pcbdetach(inp);
657 	in_pcbfree(inp);
658 	INP_INFO_WUNLOCK(&ripcbinfo);
659 }
660 
661 static void
662 rip_dodisconnect(struct socket *so, struct inpcb *inp)
663 {
664 
665 	INP_LOCK_ASSERT(inp);
666 
667 	inp->inp_faddr.s_addr = INADDR_ANY;
668 	SOCK_LOCK(so);
669 	so->so_state &= ~SS_ISCONNECTED;
670 	SOCK_UNLOCK(so);
671 }
672 
673 static void
674 rip_abort(struct socket *so)
675 {
676 	struct inpcb *inp;
677 
678 	inp = sotoinpcb(so);
679 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
680 
681 	INP_INFO_WLOCK(&ripcbinfo);
682 	INP_LOCK(inp);
683 	rip_dodisconnect(so, inp);
684 	INP_UNLOCK(inp);
685 	INP_INFO_WUNLOCK(&ripcbinfo);
686 }
687 
688 static void
689 rip_close(struct socket *so)
690 {
691 	struct inpcb *inp;
692 
693 	inp = sotoinpcb(so);
694 	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
695 
696 	INP_INFO_WLOCK(&ripcbinfo);
697 	INP_LOCK(inp);
698 	rip_dodisconnect(so, inp);
699 	INP_UNLOCK(inp);
700 	INP_INFO_WUNLOCK(&ripcbinfo);
701 }
702 
703 static int
704 rip_disconnect(struct socket *so)
705 {
706 	struct inpcb *inp;
707 
708 	if ((so->so_state & SS_ISCONNECTED) == 0)
709 		return ENOTCONN;
710 
711 	inp = sotoinpcb(so);
712 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
713 	INP_INFO_WLOCK(&ripcbinfo);
714 	INP_LOCK(inp);
715 	rip_dodisconnect(so, inp);
716 	INP_UNLOCK(inp);
717 	INP_INFO_WUNLOCK(&ripcbinfo);
718 	return (0);
719 }
720 
721 static int
722 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
723 {
724 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
725 	struct inpcb *inp;
726 
727 	if (nam->sa_len != sizeof(*addr))
728 		return EINVAL;
729 
730 	if (jailed(td->td_ucred)) {
731 		if (addr->sin_addr.s_addr == INADDR_ANY)
732 			addr->sin_addr.s_addr =
733 			    htonl(prison_getip(td->td_ucred));
734 		if (htonl(prison_getip(td->td_ucred)) != addr->sin_addr.s_addr)
735 			return (EADDRNOTAVAIL);
736 	}
737 
738 	if (TAILQ_EMPTY(&ifnet) ||
739 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
740 	    (addr->sin_addr.s_addr &&
741 	     ifa_ifwithaddr((struct sockaddr *)addr) == 0))
742 		return EADDRNOTAVAIL;
743 
744 	inp = sotoinpcb(so);
745 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
746 	INP_INFO_WLOCK(&ripcbinfo);
747 	INP_LOCK(inp);
748 	inp->inp_laddr = addr->sin_addr;
749 	INP_UNLOCK(inp);
750 	INP_INFO_WUNLOCK(&ripcbinfo);
751 	return 0;
752 }
753 
754 static int
755 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
756 {
757 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
758 	struct inpcb *inp;
759 
760 	if (nam->sa_len != sizeof(*addr))
761 		return EINVAL;
762 	if (TAILQ_EMPTY(&ifnet))
763 		return EADDRNOTAVAIL;
764 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
765 		return EAFNOSUPPORT;
766 
767 	inp = sotoinpcb(so);
768 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
769 	INP_INFO_WLOCK(&ripcbinfo);
770 	INP_LOCK(inp);
771 	inp->inp_faddr = addr->sin_addr;
772 	soisconnected(so);
773 	INP_UNLOCK(inp);
774 	INP_INFO_WUNLOCK(&ripcbinfo);
775 	return 0;
776 }
777 
778 static int
779 rip_shutdown(struct socket *so)
780 {
781 	struct inpcb *inp;
782 
783 	inp = sotoinpcb(so);
784 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
785 	INP_LOCK(inp);
786 	socantsendmore(so);
787 	INP_UNLOCK(inp);
788 	return 0;
789 }
790 
791 static int
792 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
793 	 struct mbuf *control, struct thread *td)
794 {
795 	struct inpcb *inp;
796 	u_long dst;
797 
798 	inp = sotoinpcb(so);
799 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
800 	/*
801 	 * Note: 'dst' reads below are unlocked.
802 	 */
803 	if (so->so_state & SS_ISCONNECTED) {
804 		if (nam) {
805 			m_freem(m);
806 			return EISCONN;
807 		}
808 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
809 	} else {
810 		if (nam == NULL) {
811 			m_freem(m);
812 			return ENOTCONN;
813 		}
814 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
815 	}
816 	return rip_output(m, so, dst);
817 }
818 
819 static int
820 rip_pcblist(SYSCTL_HANDLER_ARGS)
821 {
822 	int error, i, n;
823 	struct inpcb *inp, **inp_list;
824 	inp_gen_t gencnt;
825 	struct xinpgen xig;
826 
827 	/*
828 	 * The process of preparing the TCB list is too time-consuming and
829 	 * resource-intensive to repeat twice on every request.
830 	 */
831 	if (req->oldptr == 0) {
832 		n = ripcbinfo.ipi_count;
833 		req->oldidx = 2 * (sizeof xig)
834 			+ (n + n/8) * sizeof(struct xinpcb);
835 		return 0;
836 	}
837 
838 	if (req->newptr != 0)
839 		return EPERM;
840 
841 	/*
842 	 * OK, now we're committed to doing something.
843 	 */
844 	INP_INFO_RLOCK(&ripcbinfo);
845 	gencnt = ripcbinfo.ipi_gencnt;
846 	n = ripcbinfo.ipi_count;
847 	INP_INFO_RUNLOCK(&ripcbinfo);
848 
849 	xig.xig_len = sizeof xig;
850 	xig.xig_count = n;
851 	xig.xig_gen = gencnt;
852 	xig.xig_sogen = so_gencnt;
853 	error = SYSCTL_OUT(req, &xig, sizeof xig);
854 	if (error)
855 		return error;
856 
857 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
858 	if (inp_list == 0)
859 		return ENOMEM;
860 
861 	INP_INFO_RLOCK(&ripcbinfo);
862 	for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n;
863 	     inp = LIST_NEXT(inp, inp_list)) {
864 		INP_LOCK(inp);
865 		if (inp->inp_gencnt <= gencnt &&
866 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) {
867 			/* XXX held references? */
868 			inp_list[i++] = inp;
869 		}
870 		INP_UNLOCK(inp);
871 	}
872 	INP_INFO_RUNLOCK(&ripcbinfo);
873 	n = i;
874 
875 	error = 0;
876 	for (i = 0; i < n; i++) {
877 		inp = inp_list[i];
878 		INP_LOCK(inp);
879 		if (inp->inp_gencnt <= gencnt) {
880 			struct xinpcb xi;
881 			bzero(&xi, sizeof(xi));
882 			xi.xi_len = sizeof xi;
883 			/* XXX should avoid extra copy */
884 			bcopy(inp, &xi.xi_inp, sizeof *inp);
885 			if (inp->inp_socket)
886 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
887 			INP_UNLOCK(inp);
888 			error = SYSCTL_OUT(req, &xi, sizeof xi);
889 		} else
890 			INP_UNLOCK(inp);
891 	}
892 	if (!error) {
893 		/*
894 		 * Give the user an updated idea of our state.
895 		 * If the generation differs from what we told
896 		 * her before, she knows that something happened
897 		 * while we were processing this request, and it
898 		 * might be necessary to retry.
899 		 */
900 		INP_INFO_RLOCK(&ripcbinfo);
901 		xig.xig_gen = ripcbinfo.ipi_gencnt;
902 		xig.xig_sogen = so_gencnt;
903 		xig.xig_count = ripcbinfo.ipi_count;
904 		INP_INFO_RUNLOCK(&ripcbinfo);
905 		error = SYSCTL_OUT(req, &xig, sizeof xig);
906 	}
907 	free(inp_list, M_TEMP);
908 	return error;
909 }
910 
911 /*
912  * This is the wrapper function for in_setsockaddr.  We just pass down
913  * the pcbinfo for in_setpeeraddr to lock.
914  */
915 static int
916 rip_sockaddr(struct socket *so, struct sockaddr **nam)
917 {
918 	return (in_setsockaddr(so, nam, &ripcbinfo));
919 }
920 
921 /*
922  * This is the wrapper function for in_setpeeraddr.  We just pass down
923  * the pcbinfo for in_setpeeraddr to lock.
924  */
925 static int
926 rip_peeraddr(struct socket *so, struct sockaddr **nam)
927 {
928 	return (in_setpeeraddr(so, nam, &ripcbinfo));
929 }
930 
931 
932 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
933 	    rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
934 
935 struct pr_usrreqs rip_usrreqs = {
936 	.pru_abort =		rip_abort,
937 	.pru_attach =		rip_attach,
938 	.pru_bind =		rip_bind,
939 	.pru_connect =		rip_connect,
940 	.pru_control =		in_control,
941 	.pru_detach =		rip_detach,
942 	.pru_disconnect =	rip_disconnect,
943 	.pru_peeraddr =		rip_peeraddr,
944 	.pru_send =		rip_send,
945 	.pru_shutdown =		rip_shutdown,
946 	.pru_sockaddr =		rip_sockaddr,
947 	.pru_sosetlabel =	in_pcbsosetlabel,
948 	.pru_close =		rip_close,
949 };
950