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