xref: /freebsd/sys/netinet/raw_ip.c (revision aa0a1e58f0189b0fde359a8bda032887e72057fa)
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 		if (err == 0)
745 			ia->ia_flags |= IFA_RTSELF;
746 		ifa_free(&ia->ia_ifa);
747 		break;
748 	}
749 }
750 
751 u_long	rip_sendspace = 9216;
752 u_long	rip_recvspace = 9216;
753 
754 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
755     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
756 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
757     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
758 
759 static int
760 rip_attach(struct socket *so, int proto, struct thread *td)
761 {
762 	struct inpcb *inp;
763 	int error;
764 
765 	inp = sotoinpcb(so);
766 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
767 
768 	error = priv_check(td, PRIV_NETINET_RAW);
769 	if (error)
770 		return (error);
771 	if (proto >= IPPROTO_MAX || proto < 0)
772 		return EPROTONOSUPPORT;
773 	error = soreserve(so, rip_sendspace, rip_recvspace);
774 	if (error)
775 		return (error);
776 	INP_INFO_WLOCK(&V_ripcbinfo);
777 	error = in_pcballoc(so, &V_ripcbinfo);
778 	if (error) {
779 		INP_INFO_WUNLOCK(&V_ripcbinfo);
780 		return (error);
781 	}
782 	inp = (struct inpcb *)so->so_pcb;
783 	inp->inp_vflag |= INP_IPV4;
784 	inp->inp_ip_p = proto;
785 	inp->inp_ip_ttl = V_ip_defttl;
786 	rip_inshash(inp);
787 	INP_INFO_WUNLOCK(&V_ripcbinfo);
788 	INP_WUNLOCK(inp);
789 	return (0);
790 }
791 
792 static void
793 rip_detach(struct socket *so)
794 {
795 	struct inpcb *inp;
796 
797 	inp = sotoinpcb(so);
798 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
799 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
800 	    ("rip_detach: not closed"));
801 
802 	INP_INFO_WLOCK(&V_ripcbinfo);
803 	INP_WLOCK(inp);
804 	rip_delhash(inp);
805 	if (so == V_ip_mrouter && ip_mrouter_done)
806 		ip_mrouter_done();
807 	if (ip_rsvp_force_done)
808 		ip_rsvp_force_done(so);
809 	if (so == V_ip_rsvpd)
810 		ip_rsvp_done();
811 	in_pcbdetach(inp);
812 	in_pcbfree(inp);
813 	INP_INFO_WUNLOCK(&V_ripcbinfo);
814 }
815 
816 static void
817 rip_dodisconnect(struct socket *so, struct inpcb *inp)
818 {
819 
820 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
821 	INP_WLOCK_ASSERT(inp);
822 
823 	rip_delhash(inp);
824 	inp->inp_faddr.s_addr = INADDR_ANY;
825 	rip_inshash(inp);
826 	SOCK_LOCK(so);
827 	so->so_state &= ~SS_ISCONNECTED;
828 	SOCK_UNLOCK(so);
829 }
830 
831 static void
832 rip_abort(struct socket *so)
833 {
834 	struct inpcb *inp;
835 
836 	inp = sotoinpcb(so);
837 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
838 
839 	INP_INFO_WLOCK(&V_ripcbinfo);
840 	INP_WLOCK(inp);
841 	rip_dodisconnect(so, inp);
842 	INP_WUNLOCK(inp);
843 	INP_INFO_WUNLOCK(&V_ripcbinfo);
844 }
845 
846 static void
847 rip_close(struct socket *so)
848 {
849 	struct inpcb *inp;
850 
851 	inp = sotoinpcb(so);
852 	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
853 
854 	INP_INFO_WLOCK(&V_ripcbinfo);
855 	INP_WLOCK(inp);
856 	rip_dodisconnect(so, inp);
857 	INP_WUNLOCK(inp);
858 	INP_INFO_WUNLOCK(&V_ripcbinfo);
859 }
860 
861 static int
862 rip_disconnect(struct socket *so)
863 {
864 	struct inpcb *inp;
865 
866 	if ((so->so_state & SS_ISCONNECTED) == 0)
867 		return (ENOTCONN);
868 
869 	inp = sotoinpcb(so);
870 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
871 
872 	INP_INFO_WLOCK(&V_ripcbinfo);
873 	INP_WLOCK(inp);
874 	rip_dodisconnect(so, inp);
875 	INP_WUNLOCK(inp);
876 	INP_INFO_WUNLOCK(&V_ripcbinfo);
877 	return (0);
878 }
879 
880 static int
881 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
882 {
883 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
884 	struct inpcb *inp;
885 	int error;
886 
887 	if (nam->sa_len != sizeof(*addr))
888 		return (EINVAL);
889 
890 	error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
891 	if (error != 0)
892 		return (error);
893 
894 	inp = sotoinpcb(so);
895 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
896 
897 	if (TAILQ_EMPTY(&V_ifnet) ||
898 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
899 	    (addr->sin_addr.s_addr &&
900 	     (inp->inp_flags & INP_BINDANY) == 0 &&
901 	     ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
902 		return (EADDRNOTAVAIL);
903 
904 	INP_INFO_WLOCK(&V_ripcbinfo);
905 	INP_WLOCK(inp);
906 	rip_delhash(inp);
907 	inp->inp_laddr = addr->sin_addr;
908 	rip_inshash(inp);
909 	INP_WUNLOCK(inp);
910 	INP_INFO_WUNLOCK(&V_ripcbinfo);
911 	return (0);
912 }
913 
914 static int
915 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
916 {
917 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
918 	struct inpcb *inp;
919 
920 	if (nam->sa_len != sizeof(*addr))
921 		return (EINVAL);
922 	if (TAILQ_EMPTY(&V_ifnet))
923 		return (EADDRNOTAVAIL);
924 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
925 		return (EAFNOSUPPORT);
926 
927 	inp = sotoinpcb(so);
928 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
929 
930 	INP_INFO_WLOCK(&V_ripcbinfo);
931 	INP_WLOCK(inp);
932 	rip_delhash(inp);
933 	inp->inp_faddr = addr->sin_addr;
934 	rip_inshash(inp);
935 	soisconnected(so);
936 	INP_WUNLOCK(inp);
937 	INP_INFO_WUNLOCK(&V_ripcbinfo);
938 	return (0);
939 }
940 
941 static int
942 rip_shutdown(struct socket *so)
943 {
944 	struct inpcb *inp;
945 
946 	inp = sotoinpcb(so);
947 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
948 
949 	INP_WLOCK(inp);
950 	socantsendmore(so);
951 	INP_WUNLOCK(inp);
952 	return (0);
953 }
954 
955 static int
956 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
957     struct mbuf *control, struct thread *td)
958 {
959 	struct inpcb *inp;
960 	u_long dst;
961 
962 	inp = sotoinpcb(so);
963 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
964 
965 	/*
966 	 * Note: 'dst' reads below are unlocked.
967 	 */
968 	if (so->so_state & SS_ISCONNECTED) {
969 		if (nam) {
970 			m_freem(m);
971 			return (EISCONN);
972 		}
973 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
974 	} else {
975 		if (nam == NULL) {
976 			m_freem(m);
977 			return (ENOTCONN);
978 		}
979 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
980 	}
981 	return (rip_output(m, so, dst));
982 }
983 
984 static int
985 rip_pcblist(SYSCTL_HANDLER_ARGS)
986 {
987 	int error, i, n;
988 	struct inpcb *inp, **inp_list;
989 	inp_gen_t gencnt;
990 	struct xinpgen xig;
991 
992 	/*
993 	 * The process of preparing the TCB list is too time-consuming and
994 	 * resource-intensive to repeat twice on every request.
995 	 */
996 	if (req->oldptr == 0) {
997 		n = V_ripcbinfo.ipi_count;
998 		n += imax(n / 8, 10);
999 		req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1000 		return (0);
1001 	}
1002 
1003 	if (req->newptr != 0)
1004 		return (EPERM);
1005 
1006 	/*
1007 	 * OK, now we're committed to doing something.
1008 	 */
1009 	INP_INFO_RLOCK(&V_ripcbinfo);
1010 	gencnt = V_ripcbinfo.ipi_gencnt;
1011 	n = V_ripcbinfo.ipi_count;
1012 	INP_INFO_RUNLOCK(&V_ripcbinfo);
1013 
1014 	xig.xig_len = sizeof xig;
1015 	xig.xig_count = n;
1016 	xig.xig_gen = gencnt;
1017 	xig.xig_sogen = so_gencnt;
1018 	error = SYSCTL_OUT(req, &xig, sizeof xig);
1019 	if (error)
1020 		return (error);
1021 
1022 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1023 	if (inp_list == 0)
1024 		return (ENOMEM);
1025 
1026 	INP_INFO_RLOCK(&V_ripcbinfo);
1027 	for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1028 	     inp = LIST_NEXT(inp, inp_list)) {
1029 		INP_WLOCK(inp);
1030 		if (inp->inp_gencnt <= gencnt &&
1031 		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1032 			in_pcbref(inp);
1033 			inp_list[i++] = inp;
1034 		}
1035 		INP_WUNLOCK(inp);
1036 	}
1037 	INP_INFO_RUNLOCK(&V_ripcbinfo);
1038 	n = i;
1039 
1040 	error = 0;
1041 	for (i = 0; i < n; i++) {
1042 		inp = inp_list[i];
1043 		INP_RLOCK(inp);
1044 		if (inp->inp_gencnt <= gencnt) {
1045 			struct xinpcb xi;
1046 
1047 			bzero(&xi, sizeof(xi));
1048 			xi.xi_len = sizeof xi;
1049 			/* XXX should avoid extra copy */
1050 			bcopy(inp, &xi.xi_inp, sizeof *inp);
1051 			if (inp->inp_socket)
1052 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
1053 			INP_RUNLOCK(inp);
1054 			error = SYSCTL_OUT(req, &xi, sizeof xi);
1055 		} else
1056 			INP_RUNLOCK(inp);
1057 	}
1058 	INP_INFO_WLOCK(&V_ripcbinfo);
1059 	for (i = 0; i < n; i++) {
1060 		inp = inp_list[i];
1061 		INP_WLOCK(inp);
1062 		if (!in_pcbrele(inp))
1063 			INP_WUNLOCK(inp);
1064 	}
1065 	INP_INFO_WUNLOCK(&V_ripcbinfo);
1066 
1067 	if (!error) {
1068 		/*
1069 		 * Give the user an updated idea of our state.  If the
1070 		 * generation differs from what we told her before, she knows
1071 		 * that something happened while we were processing this
1072 		 * request, and it might be necessary to retry.
1073 		 */
1074 		INP_INFO_RLOCK(&V_ripcbinfo);
1075 		xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1076 		xig.xig_sogen = so_gencnt;
1077 		xig.xig_count = V_ripcbinfo.ipi_count;
1078 		INP_INFO_RUNLOCK(&V_ripcbinfo);
1079 		error = SYSCTL_OUT(req, &xig, sizeof xig);
1080 	}
1081 	free(inp_list, M_TEMP);
1082 	return (error);
1083 }
1084 
1085 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1086     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
1087     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1088 
1089 struct pr_usrreqs rip_usrreqs = {
1090 	.pru_abort =		rip_abort,
1091 	.pru_attach =		rip_attach,
1092 	.pru_bind =		rip_bind,
1093 	.pru_connect =		rip_connect,
1094 	.pru_control =		in_control,
1095 	.pru_detach =		rip_detach,
1096 	.pru_disconnect =	rip_disconnect,
1097 	.pru_peeraddr =		in_getpeeraddr,
1098 	.pru_send =		rip_send,
1099 	.pru_shutdown =		rip_shutdown,
1100 	.pru_sockaddr =		in_getsockaddr,
1101 	.pru_sosetlabel =	in_pcbsosetlabel,
1102 	.pru_close =		rip_close,
1103 };
1104