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