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