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