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