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