xref: /freebsd/sys/netinet/raw_ip.c (revision 43faedc1339a9624c7acedb7f3e5624e64da5b99)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *	The Regents of the University of California.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
39 
40 #include <sys/param.h>
41 #include <sys/jail.h>
42 #include <sys/kernel.h>
43 #include <sys/eventhandler.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/priv.h>
48 #include <sys/proc.h>
49 #include <sys/protosw.h>
50 #include <sys/rmlock.h>
51 #include <sys/rwlock.h>
52 #include <sys/signalvar.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/sx.h>
56 #include <sys/sysctl.h>
57 #include <sys/systm.h>
58 
59 #include <vm/uma.h>
60 
61 #include <net/if.h>
62 #include <net/if_var.h>
63 #include <net/route.h>
64 #include <net/vnet.h>
65 
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/in_var.h>
70 #include <netinet/if_ether.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_var.h>
73 #include <netinet/ip_mroute.h>
74 
75 #ifdef IPSEC
76 #include <netipsec/ipsec.h>
77 #endif /*IPSEC*/
78 
79 #include <machine/stdarg.h>
80 #include <security/mac/mac_framework.h>
81 
82 VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
83 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
84     &VNET_NAME(ip_defttl), 0,
85     "Maximum TTL on IP packets");
86 
87 VNET_DEFINE(struct inpcbhead, ripcb);
88 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
89 
90 #define	V_ripcb			VNET(ripcb)
91 #define	V_ripcbinfo		VNET(ripcbinfo)
92 
93 /*
94  * Control and data hooks for ipfw, dummynet, divert and so on.
95  * The data hooks are not used here but it is convenient
96  * to keep them all in one place.
97  */
98 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
99 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
100 
101 int	(*ip_dn_ctl_ptr)(struct sockopt *);
102 int	(*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
103 void	(*ip_divert_ptr)(struct mbuf *, int);
104 int	(*ng_ipfw_input_p)(struct mbuf **, int,
105 			struct ip_fw_args *, int);
106 
107 #ifdef INET
108 /*
109  * Hooks for multicast routing. They all default to NULL, so leave them not
110  * initialized and rely on BSS being set to 0.
111  */
112 
113 /*
114  * The socket used to communicate with the multicast routing daemon.
115  */
116 VNET_DEFINE(struct socket *, ip_mrouter);
117 
118 /*
119  * The various mrouter and rsvp functions.
120  */
121 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
122 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
123 int (*ip_mrouter_done)(void);
124 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
125 		   struct ip_moptions *);
126 int (*mrt_ioctl)(u_long, caddr_t, int);
127 int (*legal_vif_num)(int);
128 u_long (*ip_mcast_src)(int);
129 
130 int (*rsvp_input_p)(struct mbuf **, int *, int);
131 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
132 void (*ip_rsvp_force_done)(struct socket *);
133 #endif /* INET */
134 
135 u_long	rip_sendspace = 9216;
136 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
137     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
138 
139 u_long	rip_recvspace = 9216;
140 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
141     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
142 
143 /*
144  * Hash functions
145  */
146 
147 #define INP_PCBHASH_RAW_SIZE	256
148 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
149         (((proto) + (laddr) + (faddr)) % (mask) + 1)
150 
151 #ifdef INET
152 static void
153 rip_inshash(struct inpcb *inp)
154 {
155 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
156 	struct inpcbhead *pcbhash;
157 	int hash;
158 
159 	INP_INFO_WLOCK_ASSERT(pcbinfo);
160 	INP_WLOCK_ASSERT(inp);
161 
162 	if (inp->inp_ip_p != 0 &&
163 	    inp->inp_laddr.s_addr != INADDR_ANY &&
164 	    inp->inp_faddr.s_addr != INADDR_ANY) {
165 		hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
166 		    inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
167 	} else
168 		hash = 0;
169 	pcbhash = &pcbinfo->ipi_hashbase[hash];
170 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
171 }
172 
173 static void
174 rip_delhash(struct inpcb *inp)
175 {
176 
177 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
178 	INP_WLOCK_ASSERT(inp);
179 
180 	LIST_REMOVE(inp, inp_hash);
181 }
182 #endif /* INET */
183 
184 /*
185  * Raw interface to IP protocol.
186  */
187 
188 /*
189  * Initialize raw connection block q.
190  */
191 static void
192 rip_zone_change(void *tag)
193 {
194 
195 	uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
196 }
197 
198 static int
199 rip_inpcb_init(void *mem, int size, int flags)
200 {
201 	struct inpcb *inp = mem;
202 
203 	INP_LOCK_INIT(inp, "inp", "rawinp");
204 	return (0);
205 }
206 
207 void
208 rip_init(void)
209 {
210 
211 	in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
212 	    1, "ripcb", rip_inpcb_init, NULL, UMA_ZONE_NOFREE,
213 	    IPI_HASHFIELDS_NONE);
214 	EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
215 	    EVENTHANDLER_PRI_ANY);
216 }
217 
218 #ifdef VIMAGE
219 void
220 rip_destroy(void)
221 {
222 
223 	in_pcbinfo_destroy(&V_ripcbinfo);
224 }
225 #endif
226 
227 #ifdef INET
228 static int
229 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
230     struct sockaddr_in *ripsrc)
231 {
232 	int policyfail = 0;
233 
234 	INP_LOCK_ASSERT(last);
235 
236 #ifdef IPSEC
237 	/* check AH/ESP integrity. */
238 	if (ipsec4_in_reject(n, last)) {
239 		policyfail = 1;
240 	}
241 #endif /* IPSEC */
242 #ifdef MAC
243 	if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
244 		policyfail = 1;
245 #endif
246 	/* Check the minimum TTL for socket. */
247 	if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
248 		policyfail = 1;
249 	if (!policyfail) {
250 		struct mbuf *opts = NULL;
251 		struct socket *so;
252 
253 		so = last->inp_socket;
254 		if ((last->inp_flags & INP_CONTROLOPTS) ||
255 		    (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
256 			ip_savecontrol(last, &opts, ip, n);
257 		SOCKBUF_LOCK(&so->so_rcv);
258 		if (sbappendaddr_locked(&so->so_rcv,
259 		    (struct sockaddr *)ripsrc, n, opts) == 0) {
260 			/* should notify about lost packet */
261 			m_freem(n);
262 			if (opts)
263 				m_freem(opts);
264 			SOCKBUF_UNLOCK(&so->so_rcv);
265 		} else
266 			sorwakeup_locked(so);
267 	} else
268 		m_freem(n);
269 	return (policyfail);
270 }
271 
272 /*
273  * Setup generic address and protocol structures for raw_input routine, then
274  * pass them along with mbuf chain.
275  */
276 int
277 rip_input(struct mbuf **mp, int *offp, int proto)
278 {
279 	struct ifnet *ifp;
280 	struct mbuf *m = *mp;
281 	struct ip *ip = mtod(m, struct ip *);
282 	struct inpcb *inp, *last;
283 	struct sockaddr_in ripsrc;
284 	int hash;
285 
286 	*mp = NULL;
287 
288 	bzero(&ripsrc, sizeof(ripsrc));
289 	ripsrc.sin_len = sizeof(ripsrc);
290 	ripsrc.sin_family = AF_INET;
291 	ripsrc.sin_addr = ip->ip_src;
292 	last = NULL;
293 
294 	ifp = m->m_pkthdr.rcvif;
295 
296 	hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
297 	    ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
298 	INP_INFO_RLOCK(&V_ripcbinfo);
299 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
300 		if (inp->inp_ip_p != proto)
301 			continue;
302 #ifdef INET6
303 		/* XXX inp locking */
304 		if ((inp->inp_vflag & INP_IPV4) == 0)
305 			continue;
306 #endif
307 		if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
308 			continue;
309 		if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
310 			continue;
311 		if (jailed_without_vnet(inp->inp_cred)) {
312 			/*
313 			 * XXX: If faddr was bound to multicast group,
314 			 * jailed raw socket will drop datagram.
315 			 */
316 			if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
317 				continue;
318 		}
319 		if (last != NULL) {
320 			struct mbuf *n;
321 
322 			n = m_copy(m, 0, (int)M_COPYALL);
323 			if (n != NULL)
324 		    	    (void) rip_append(last, ip, n, &ripsrc);
325 			/* XXX count dropped packet */
326 			INP_RUNLOCK(last);
327 		}
328 		INP_RLOCK(inp);
329 		last = inp;
330 	}
331 	LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
332 		if (inp->inp_ip_p && inp->inp_ip_p != proto)
333 			continue;
334 #ifdef INET6
335 		/* XXX inp locking */
336 		if ((inp->inp_vflag & INP_IPV4) == 0)
337 			continue;
338 #endif
339 		if (!in_nullhost(inp->inp_laddr) &&
340 		    !in_hosteq(inp->inp_laddr, ip->ip_dst))
341 			continue;
342 		if (!in_nullhost(inp->inp_faddr) &&
343 		    !in_hosteq(inp->inp_faddr, ip->ip_src))
344 			continue;
345 		if (jailed_without_vnet(inp->inp_cred)) {
346 			/*
347 			 * Allow raw socket in jail to receive multicast;
348 			 * assume process had PRIV_NETINET_RAW at attach,
349 			 * and fall through into normal filter path if so.
350 			 */
351 			if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
352 			    prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
353 				continue;
354 		}
355 		/*
356 		 * If this raw socket has multicast state, and we
357 		 * have received a multicast, check if this socket
358 		 * should receive it, as multicast filtering is now
359 		 * the responsibility of the transport layer.
360 		 */
361 		if (inp->inp_moptions != NULL &&
362 		    IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
363 			/*
364 			 * If the incoming datagram is for IGMP, allow it
365 			 * through unconditionally to the raw socket.
366 			 *
367 			 * In the case of IGMPv2, we may not have explicitly
368 			 * joined the group, and may have set IFF_ALLMULTI
369 			 * on the interface. imo_multi_filter() may discard
370 			 * control traffic we actually need to see.
371 			 *
372 			 * Userland multicast routing daemons should continue
373 			 * filter the control traffic appropriately.
374 			 */
375 			int blocked;
376 
377 			blocked = MCAST_PASS;
378 			if (proto != IPPROTO_IGMP) {
379 				struct sockaddr_in group;
380 
381 				bzero(&group, sizeof(struct sockaddr_in));
382 				group.sin_len = sizeof(struct sockaddr_in);
383 				group.sin_family = AF_INET;
384 				group.sin_addr = ip->ip_dst;
385 
386 				blocked = imo_multi_filter(inp->inp_moptions,
387 				    ifp,
388 				    (struct sockaddr *)&group,
389 				    (struct sockaddr *)&ripsrc);
390 			}
391 
392 			if (blocked != MCAST_PASS) {
393 				IPSTAT_INC(ips_notmember);
394 				continue;
395 			}
396 		}
397 		if (last != NULL) {
398 			struct mbuf *n;
399 
400 			n = m_copy(m, 0, (int)M_COPYALL);
401 			if (n != NULL)
402 				(void) rip_append(last, ip, n, &ripsrc);
403 			/* XXX count dropped packet */
404 			INP_RUNLOCK(last);
405 		}
406 		INP_RLOCK(inp);
407 		last = inp;
408 	}
409 	INP_INFO_RUNLOCK(&V_ripcbinfo);
410 	if (last != NULL) {
411 		if (rip_append(last, ip, m, &ripsrc) != 0)
412 			IPSTAT_INC(ips_delivered);
413 		INP_RUNLOCK(last);
414 	} else {
415 		m_freem(m);
416 		IPSTAT_INC(ips_noproto);
417 		IPSTAT_DEC(ips_delivered);
418 	}
419 	return (IPPROTO_DONE);
420 }
421 
422 /*
423  * Generate IP header and pass packet to ip_output.  Tack on options user may
424  * have setup with control call.
425  */
426 int
427 rip_output(struct mbuf *m, struct socket *so, ...)
428 {
429 	struct ip *ip;
430 	int error;
431 	struct inpcb *inp = sotoinpcb(so);
432 	va_list ap;
433 	u_long dst;
434 	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
435 	    IP_ALLOWBROADCAST;
436 
437 	va_start(ap, so);
438 	dst = va_arg(ap, u_long);
439 	va_end(ap);
440 
441 	/*
442 	 * If the user handed us a complete IP packet, use it.  Otherwise,
443 	 * allocate an mbuf for a header and fill it in.
444 	 */
445 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
446 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
447 			m_freem(m);
448 			return(EMSGSIZE);
449 		}
450 		M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
451 		if (m == NULL)
452 			return(ENOBUFS);
453 
454 		INP_RLOCK(inp);
455 		ip = mtod(m, struct ip *);
456 		ip->ip_tos = inp->inp_ip_tos;
457 		if (inp->inp_flags & INP_DONTFRAG)
458 			ip->ip_off = htons(IP_DF);
459 		else
460 			ip->ip_off = htons(0);
461 		ip->ip_p = inp->inp_ip_p;
462 		ip->ip_len = htons(m->m_pkthdr.len);
463 		ip->ip_src = inp->inp_laddr;
464 		ip->ip_dst.s_addr = dst;
465 		if (jailed(inp->inp_cred)) {
466 			/*
467 			 * prison_local_ip4() would be good enough but would
468 			 * let a source of INADDR_ANY pass, which we do not
469 			 * want to see from jails.
470 			 */
471 			if (ip->ip_src.s_addr == INADDR_ANY) {
472 				error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
473 				    inp->inp_cred);
474 			} else {
475 				error = prison_local_ip4(inp->inp_cred,
476 				    &ip->ip_src);
477 			}
478 			if (error != 0) {
479 				INP_RUNLOCK(inp);
480 				m_freem(m);
481 				return (error);
482 			}
483 		}
484 		ip->ip_ttl = inp->inp_ip_ttl;
485 	} else {
486 		if (m->m_pkthdr.len > IP_MAXPACKET) {
487 			m_freem(m);
488 			return(EMSGSIZE);
489 		}
490 		INP_RLOCK(inp);
491 		ip = mtod(m, struct ip *);
492 		error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
493 		if (error != 0) {
494 			INP_RUNLOCK(inp);
495 			m_freem(m);
496 			return (error);
497 		}
498 
499 		/*
500 		 * Don't allow both user specified and setsockopt options,
501 		 * and don't allow packet length sizes that will crash.
502 		 */
503 		if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
504 		    || (ntohs(ip->ip_len) > m->m_pkthdr.len)
505 		    || (ntohs(ip->ip_len) < (ip->ip_hl << 2))) {
506 			INP_RUNLOCK(inp);
507 			m_freem(m);
508 			return (EINVAL);
509 		}
510 		/*
511 		 * This doesn't allow application to specify ID of zero,
512 		 * but we got this limitation from the beginning of history.
513 		 */
514 		if (ip->ip_id == 0)
515 			ip_fillid(ip);
516 
517 		/*
518 		 * XXX prevent ip_output from overwriting header fields.
519 		 */
520 		flags |= IP_RAWOUTPUT;
521 		IPSTAT_INC(ips_rawout);
522 	}
523 
524 	if (inp->inp_flags & INP_ONESBCAST)
525 		flags |= IP_SENDONES;
526 
527 #ifdef MAC
528 	mac_inpcb_create_mbuf(inp, m);
529 #endif
530 
531 	error = ip_output(m, inp->inp_options, NULL, flags,
532 	    inp->inp_moptions, inp);
533 	INP_RUNLOCK(inp);
534 	return (error);
535 }
536 
537 /*
538  * Raw IP socket option processing.
539  *
540  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
541  * only be created by a privileged process, and as such, socket option
542  * operations to manage system properties on any raw socket were allowed to
543  * take place without explicit additional access control checks.  However,
544  * raw sockets can now also be created in jail(), and therefore explicit
545  * checks are now required.  Likewise, raw sockets can be used by a process
546  * after it gives up privilege, so some caution is required.  For options
547  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
548  * performed in ip_ctloutput() and therefore no check occurs here.
549  * Unilaterally checking priv_check() here breaks normal IP socket option
550  * operations on raw sockets.
551  *
552  * When adding new socket options here, make sure to add access control
553  * checks here as necessary.
554  *
555  * XXX-BZ inp locking?
556  */
557 int
558 rip_ctloutput(struct socket *so, struct sockopt *sopt)
559 {
560 	struct	inpcb *inp = sotoinpcb(so);
561 	int	error, optval;
562 
563 	if (sopt->sopt_level != IPPROTO_IP) {
564 		if ((sopt->sopt_level == SOL_SOCKET) &&
565 		    (sopt->sopt_name == SO_SETFIB)) {
566 			inp->inp_inc.inc_fibnum = so->so_fibnum;
567 			return (0);
568 		}
569 		return (EINVAL);
570 	}
571 
572 	error = 0;
573 	switch (sopt->sopt_dir) {
574 	case SOPT_GET:
575 		switch (sopt->sopt_name) {
576 		case IP_HDRINCL:
577 			optval = inp->inp_flags & INP_HDRINCL;
578 			error = sooptcopyout(sopt, &optval, sizeof optval);
579 			break;
580 
581 		case IP_FW3:	/* generic ipfw v.3 functions */
582 		case IP_FW_ADD:	/* ADD actually returns the body... */
583 		case IP_FW_GET:
584 		case IP_FW_TABLE_GETSIZE:
585 		case IP_FW_TABLE_LIST:
586 		case IP_FW_NAT_GET_CONFIG:
587 		case IP_FW_NAT_GET_LOG:
588 			if (V_ip_fw_ctl_ptr != NULL)
589 				error = V_ip_fw_ctl_ptr(sopt);
590 			else
591 				error = ENOPROTOOPT;
592 			break;
593 
594 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
595 		case IP_DUMMYNET_GET:
596 			if (ip_dn_ctl_ptr != NULL)
597 				error = ip_dn_ctl_ptr(sopt);
598 			else
599 				error = ENOPROTOOPT;
600 			break ;
601 
602 		case MRT_INIT:
603 		case MRT_DONE:
604 		case MRT_ADD_VIF:
605 		case MRT_DEL_VIF:
606 		case MRT_ADD_MFC:
607 		case MRT_DEL_MFC:
608 		case MRT_VERSION:
609 		case MRT_ASSERT:
610 		case MRT_API_SUPPORT:
611 		case MRT_API_CONFIG:
612 		case MRT_ADD_BW_UPCALL:
613 		case MRT_DEL_BW_UPCALL:
614 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
615 			if (error != 0)
616 				return (error);
617 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
618 				EOPNOTSUPP;
619 			break;
620 
621 		default:
622 			error = ip_ctloutput(so, sopt);
623 			break;
624 		}
625 		break;
626 
627 	case SOPT_SET:
628 		switch (sopt->sopt_name) {
629 		case IP_HDRINCL:
630 			error = sooptcopyin(sopt, &optval, sizeof optval,
631 					    sizeof optval);
632 			if (error)
633 				break;
634 			if (optval)
635 				inp->inp_flags |= INP_HDRINCL;
636 			else
637 				inp->inp_flags &= ~INP_HDRINCL;
638 			break;
639 
640 		case IP_FW3:	/* generic ipfw v.3 functions */
641 		case IP_FW_ADD:
642 		case IP_FW_DEL:
643 		case IP_FW_FLUSH:
644 		case IP_FW_ZERO:
645 		case IP_FW_RESETLOG:
646 		case IP_FW_TABLE_ADD:
647 		case IP_FW_TABLE_DEL:
648 		case IP_FW_TABLE_FLUSH:
649 		case IP_FW_NAT_CFG:
650 		case IP_FW_NAT_DEL:
651 			if (V_ip_fw_ctl_ptr != NULL)
652 				error = V_ip_fw_ctl_ptr(sopt);
653 			else
654 				error = ENOPROTOOPT;
655 			break;
656 
657 		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
658 		case IP_DUMMYNET_CONFIGURE:
659 		case IP_DUMMYNET_DEL:
660 		case IP_DUMMYNET_FLUSH:
661 			if (ip_dn_ctl_ptr != NULL)
662 				error = ip_dn_ctl_ptr(sopt);
663 			else
664 				error = ENOPROTOOPT ;
665 			break ;
666 
667 		case IP_RSVP_ON:
668 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
669 			if (error != 0)
670 				return (error);
671 			error = ip_rsvp_init(so);
672 			break;
673 
674 		case IP_RSVP_OFF:
675 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
676 			if (error != 0)
677 				return (error);
678 			error = ip_rsvp_done();
679 			break;
680 
681 		case IP_RSVP_VIF_ON:
682 		case IP_RSVP_VIF_OFF:
683 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
684 			if (error != 0)
685 				return (error);
686 			error = ip_rsvp_vif ?
687 				ip_rsvp_vif(so, sopt) : EINVAL;
688 			break;
689 
690 		case MRT_INIT:
691 		case MRT_DONE:
692 		case MRT_ADD_VIF:
693 		case MRT_DEL_VIF:
694 		case MRT_ADD_MFC:
695 		case MRT_DEL_MFC:
696 		case MRT_VERSION:
697 		case MRT_ASSERT:
698 		case MRT_API_SUPPORT:
699 		case MRT_API_CONFIG:
700 		case MRT_ADD_BW_UPCALL:
701 		case MRT_DEL_BW_UPCALL:
702 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
703 			if (error != 0)
704 				return (error);
705 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
706 					EOPNOTSUPP;
707 			break;
708 
709 		default:
710 			error = ip_ctloutput(so, sopt);
711 			break;
712 		}
713 		break;
714 	}
715 
716 	return (error);
717 }
718 
719 /*
720  * This function exists solely to receive the PRC_IFDOWN messages which are
721  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
722  * in_ifadown() to remove all routes corresponding to that address.  It also
723  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
724  * routes.
725  */
726 void
727 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
728 {
729 	struct rm_priotracker in_ifa_tracker;
730 	struct in_ifaddr *ia;
731 	struct ifnet *ifp;
732 	int err;
733 	int flags;
734 
735 	switch (cmd) {
736 	case PRC_IFDOWN:
737 		IN_IFADDR_RLOCK(&in_ifa_tracker);
738 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
739 			if (ia->ia_ifa.ifa_addr == sa
740 			    && (ia->ia_flags & IFA_ROUTE)) {
741 				ifa_ref(&ia->ia_ifa);
742 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
743 				/*
744 				 * in_scrubprefix() kills the interface route.
745 				 */
746 				in_scrubprefix(ia, 0);
747 				/*
748 				 * in_ifadown gets rid of all the rest of the
749 				 * routes.  This is not quite the right thing
750 				 * to do, but at least if we are running a
751 				 * routing process they will come back.
752 				 */
753 				in_ifadown(&ia->ia_ifa, 0);
754 				ifa_free(&ia->ia_ifa);
755 				break;
756 			}
757 		}
758 		if (ia == NULL)		/* If ia matched, already unlocked. */
759 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
760 		break;
761 
762 	case PRC_IFUP:
763 		IN_IFADDR_RLOCK(&in_ifa_tracker);
764 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
765 			if (ia->ia_ifa.ifa_addr == sa)
766 				break;
767 		}
768 		if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
769 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
770 			return;
771 		}
772 		ifa_ref(&ia->ia_ifa);
773 		IN_IFADDR_RUNLOCK(&in_ifa_tracker);
774 		flags = RTF_UP;
775 		ifp = ia->ia_ifa.ifa_ifp;
776 
777 		if ((ifp->if_flags & IFF_LOOPBACK)
778 		    || (ifp->if_flags & IFF_POINTOPOINT))
779 			flags |= RTF_HOST;
780 
781 		err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
782 
783 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
784 		if (err == 0)
785 			ia->ia_flags |= IFA_ROUTE;
786 
787 		err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
788 
789 		ifa_free(&ia->ia_ifa);
790 		break;
791 	}
792 }
793 
794 static int
795 rip_attach(struct socket *so, int proto, struct thread *td)
796 {
797 	struct inpcb *inp;
798 	int error;
799 
800 	inp = sotoinpcb(so);
801 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
802 
803 	error = priv_check(td, PRIV_NETINET_RAW);
804 	if (error)
805 		return (error);
806 	if (proto >= IPPROTO_MAX || proto < 0)
807 		return EPROTONOSUPPORT;
808 	error = soreserve(so, rip_sendspace, rip_recvspace);
809 	if (error)
810 		return (error);
811 	INP_INFO_WLOCK(&V_ripcbinfo);
812 	error = in_pcballoc(so, &V_ripcbinfo);
813 	if (error) {
814 		INP_INFO_WUNLOCK(&V_ripcbinfo);
815 		return (error);
816 	}
817 	inp = (struct inpcb *)so->so_pcb;
818 	inp->inp_vflag |= INP_IPV4;
819 	inp->inp_ip_p = proto;
820 	inp->inp_ip_ttl = V_ip_defttl;
821 	rip_inshash(inp);
822 	INP_INFO_WUNLOCK(&V_ripcbinfo);
823 	INP_WUNLOCK(inp);
824 	return (0);
825 }
826 
827 static void
828 rip_detach(struct socket *so)
829 {
830 	struct inpcb *inp;
831 
832 	inp = sotoinpcb(so);
833 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
834 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
835 	    ("rip_detach: not closed"));
836 
837 	INP_INFO_WLOCK(&V_ripcbinfo);
838 	INP_WLOCK(inp);
839 	rip_delhash(inp);
840 	if (so == V_ip_mrouter && ip_mrouter_done)
841 		ip_mrouter_done();
842 	if (ip_rsvp_force_done)
843 		ip_rsvp_force_done(so);
844 	if (so == V_ip_rsvpd)
845 		ip_rsvp_done();
846 	in_pcbdetach(inp);
847 	in_pcbfree(inp);
848 	INP_INFO_WUNLOCK(&V_ripcbinfo);
849 }
850 
851 static void
852 rip_dodisconnect(struct socket *so, struct inpcb *inp)
853 {
854 	struct inpcbinfo *pcbinfo;
855 
856 	pcbinfo = inp->inp_pcbinfo;
857 	INP_INFO_WLOCK(pcbinfo);
858 	INP_WLOCK(inp);
859 	rip_delhash(inp);
860 	inp->inp_faddr.s_addr = INADDR_ANY;
861 	rip_inshash(inp);
862 	SOCK_LOCK(so);
863 	so->so_state &= ~SS_ISCONNECTED;
864 	SOCK_UNLOCK(so);
865 	INP_WUNLOCK(inp);
866 	INP_INFO_WUNLOCK(pcbinfo);
867 }
868 
869 static void
870 rip_abort(struct socket *so)
871 {
872 	struct inpcb *inp;
873 
874 	inp = sotoinpcb(so);
875 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
876 
877 	rip_dodisconnect(so, inp);
878 }
879 
880 static void
881 rip_close(struct socket *so)
882 {
883 	struct inpcb *inp;
884 
885 	inp = sotoinpcb(so);
886 	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
887 
888 	rip_dodisconnect(so, inp);
889 }
890 
891 static int
892 rip_disconnect(struct socket *so)
893 {
894 	struct inpcb *inp;
895 
896 	if ((so->so_state & SS_ISCONNECTED) == 0)
897 		return (ENOTCONN);
898 
899 	inp = sotoinpcb(so);
900 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
901 
902 	rip_dodisconnect(so, inp);
903 	return (0);
904 }
905 
906 static int
907 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
908 {
909 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
910 	struct inpcb *inp;
911 	int error;
912 
913 	if (nam->sa_len != sizeof(*addr))
914 		return (EINVAL);
915 
916 	error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
917 	if (error != 0)
918 		return (error);
919 
920 	inp = sotoinpcb(so);
921 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
922 
923 	if (TAILQ_EMPTY(&V_ifnet) ||
924 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
925 	    (addr->sin_addr.s_addr &&
926 	     (inp->inp_flags & INP_BINDANY) == 0 &&
927 	     ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
928 		return (EADDRNOTAVAIL);
929 
930 	INP_INFO_WLOCK(&V_ripcbinfo);
931 	INP_WLOCK(inp);
932 	rip_delhash(inp);
933 	inp->inp_laddr = addr->sin_addr;
934 	rip_inshash(inp);
935 	INP_WUNLOCK(inp);
936 	INP_INFO_WUNLOCK(&V_ripcbinfo);
937 	return (0);
938 }
939 
940 static int
941 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
942 {
943 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
944 	struct inpcb *inp;
945 
946 	if (nam->sa_len != sizeof(*addr))
947 		return (EINVAL);
948 	if (TAILQ_EMPTY(&V_ifnet))
949 		return (EADDRNOTAVAIL);
950 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
951 		return (EAFNOSUPPORT);
952 
953 	inp = sotoinpcb(so);
954 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
955 
956 	INP_INFO_WLOCK(&V_ripcbinfo);
957 	INP_WLOCK(inp);
958 	rip_delhash(inp);
959 	inp->inp_faddr = addr->sin_addr;
960 	rip_inshash(inp);
961 	soisconnected(so);
962 	INP_WUNLOCK(inp);
963 	INP_INFO_WUNLOCK(&V_ripcbinfo);
964 	return (0);
965 }
966 
967 static int
968 rip_shutdown(struct socket *so)
969 {
970 	struct inpcb *inp;
971 
972 	inp = sotoinpcb(so);
973 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
974 
975 	INP_WLOCK(inp);
976 	socantsendmore(so);
977 	INP_WUNLOCK(inp);
978 	return (0);
979 }
980 
981 static int
982 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
983     struct mbuf *control, struct thread *td)
984 {
985 	struct inpcb *inp;
986 	u_long dst;
987 
988 	inp = sotoinpcb(so);
989 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
990 
991 	/*
992 	 * Note: 'dst' reads below are unlocked.
993 	 */
994 	if (so->so_state & SS_ISCONNECTED) {
995 		if (nam) {
996 			m_freem(m);
997 			return (EISCONN);
998 		}
999 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
1000 	} else {
1001 		if (nam == NULL) {
1002 			m_freem(m);
1003 			return (ENOTCONN);
1004 		}
1005 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1006 	}
1007 	return (rip_output(m, so, dst));
1008 }
1009 #endif /* INET */
1010 
1011 static int
1012 rip_pcblist(SYSCTL_HANDLER_ARGS)
1013 {
1014 	int error, i, n;
1015 	struct inpcb *inp, **inp_list;
1016 	inp_gen_t gencnt;
1017 	struct xinpgen xig;
1018 
1019 	/*
1020 	 * The process of preparing the TCB list is too time-consuming and
1021 	 * resource-intensive to repeat twice on every request.
1022 	 */
1023 	if (req->oldptr == 0) {
1024 		n = V_ripcbinfo.ipi_count;
1025 		n += imax(n / 8, 10);
1026 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1027 		return (0);
1028 	}
1029 
1030 	if (req->newptr != 0)
1031 		return (EPERM);
1032 
1033 	/*
1034 	 * OK, now we're committed to doing something.
1035 	 */
1036 	INP_INFO_RLOCK(&V_ripcbinfo);
1037 	gencnt = V_ripcbinfo.ipi_gencnt;
1038 	n = V_ripcbinfo.ipi_count;
1039 	INP_INFO_RUNLOCK(&V_ripcbinfo);
1040 
1041 	xig.xig_len = sizeof xig;
1042 	xig.xig_count = n;
1043 	xig.xig_gen = gencnt;
1044 	xig.xig_sogen = so_gencnt;
1045 	error = SYSCTL_OUT(req, &xig, sizeof xig);
1046 	if (error)
1047 		return (error);
1048 
1049 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1050 	if (inp_list == 0)
1051 		return (ENOMEM);
1052 
1053 	INP_INFO_RLOCK(&V_ripcbinfo);
1054 	for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1055 	     inp = LIST_NEXT(inp, inp_list)) {
1056 		INP_WLOCK(inp);
1057 		if (inp->inp_gencnt <= gencnt &&
1058 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1059 			in_pcbref(inp);
1060 			inp_list[i++] = inp;
1061 		}
1062 		INP_WUNLOCK(inp);
1063 	}
1064 	INP_INFO_RUNLOCK(&V_ripcbinfo);
1065 	n = i;
1066 
1067 	error = 0;
1068 	for (i = 0; i < n; i++) {
1069 		inp = inp_list[i];
1070 		INP_RLOCK(inp);
1071 		if (inp->inp_gencnt <= gencnt) {
1072 			struct xinpcb xi;
1073 
1074 			bzero(&xi, sizeof(xi));
1075 			xi.xi_len = sizeof xi;
1076 			/* XXX should avoid extra copy */
1077 			bcopy(inp, &xi.xi_inp, sizeof *inp);
1078 			if (inp->inp_socket)
1079 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
1080 			INP_RUNLOCK(inp);
1081 			error = SYSCTL_OUT(req, &xi, sizeof xi);
1082 		} else
1083 			INP_RUNLOCK(inp);
1084 	}
1085 	INP_INFO_WLOCK(&V_ripcbinfo);
1086 	for (i = 0; i < n; i++) {
1087 		inp = inp_list[i];
1088 		INP_RLOCK(inp);
1089 		if (!in_pcbrele_rlocked(inp))
1090 			INP_RUNLOCK(inp);
1091 	}
1092 	INP_INFO_WUNLOCK(&V_ripcbinfo);
1093 
1094 	if (!error) {
1095 		/*
1096 		 * Give the user an updated idea of our state.  If the
1097 		 * generation differs from what we told her before, she knows
1098 		 * that something happened while we were processing this
1099 		 * request, and it might be necessary to retry.
1100 		 */
1101 		INP_INFO_RLOCK(&V_ripcbinfo);
1102 		xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1103 		xig.xig_sogen = so_gencnt;
1104 		xig.xig_count = V_ripcbinfo.ipi_count;
1105 		INP_INFO_RUNLOCK(&V_ripcbinfo);
1106 		error = SYSCTL_OUT(req, &xig, sizeof xig);
1107 	}
1108 	free(inp_list, M_TEMP);
1109 	return (error);
1110 }
1111 
1112 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1113     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
1114     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1115 
1116 #ifdef INET
1117 struct pr_usrreqs rip_usrreqs = {
1118 	.pru_abort =		rip_abort,
1119 	.pru_attach =		rip_attach,
1120 	.pru_bind =		rip_bind,
1121 	.pru_connect =		rip_connect,
1122 	.pru_control =		in_control,
1123 	.pru_detach =		rip_detach,
1124 	.pru_disconnect =	rip_disconnect,
1125 	.pru_peeraddr =		in_getpeeraddr,
1126 	.pru_send =		rip_send,
1127 	.pru_shutdown =		rip_shutdown,
1128 	.pru_sockaddr =		in_getsockaddr,
1129 	.pru_sosetlabel =	in_pcbsosetlabel,
1130 	.pru_close =		rip_close,
1131 };
1132 #endif /* INET */
1133