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