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