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