raw_ip.c revision 178888
1214501Srpaulo/*-
2214501Srpaulo * Copyright (c) 1982, 1986, 1988, 1993
3252726Srpaulo *	The Regents of the University of California.  All rights reserved.
4214501Srpaulo *
5252726Srpaulo * Redistribution and use in source and binary forms, with or without
6252726Srpaulo * modification, are permitted provided that the following conditions
7214501Srpaulo * are met:
8214501Srpaulo * 1. Redistributions of source code must retain the above copyright
9214501Srpaulo *    notice, this list of conditions and the following disclaimer.
10214501Srpaulo * 2. Redistributions in binary form must reproduce the above copyright
11214501Srpaulo *    notice, this list of conditions and the following disclaimer in the
12214501Srpaulo *    documentation and/or other materials provided with the distribution.
13214501Srpaulo * 4. Neither the name of the University nor the names of its contributors
14214501Srpaulo *    may be used to endorse or promote products derived from this software
15214501Srpaulo *    without specific prior written permission.
16214501Srpaulo *
17214501Srpaulo * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18214501Srpaulo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19214501Srpaulo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20214501Srpaulo * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21214501Srpaulo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22214501Srpaulo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23214501Srpaulo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24214501Srpaulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25214501Srpaulo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26214501Srpaulo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27214501Srpaulo * SUCH DAMAGE.
28214501Srpaulo *
29214501Srpaulo *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
30214501Srpaulo */
31214501Srpaulo
32214501Srpaulo#include <sys/cdefs.h>
33214501Srpaulo__FBSDID("$FreeBSD: head/sys/netinet/raw_ip.c 178888 2008-05-09 23:03:00Z julian $");
34214501Srpaulo
35214501Srpaulo#include "opt_inet6.h"
36214501Srpaulo#include "opt_ipsec.h"
37214501Srpaulo#include "opt_mac.h"
38214501Srpaulo
39214501Srpaulo#include <sys/param.h>
40214501Srpaulo#include <sys/jail.h>
41214501Srpaulo#include <sys/kernel.h>
42214501Srpaulo#include <sys/lock.h>
43252726Srpaulo#include <sys/malloc.h>
44214501Srpaulo#include <sys/mbuf.h>
45214501Srpaulo#include <sys/priv.h>
46214501Srpaulo#include <sys/proc.h>
47214501Srpaulo#include <sys/protosw.h>
48214501Srpaulo#include <sys/signalvar.h>
49214501Srpaulo#include <sys/socket.h>
50214501Srpaulo#include <sys/socketvar.h>
51214501Srpaulo#include <sys/sx.h>
52214501Srpaulo#include <sys/sysctl.h>
53214501Srpaulo#include <sys/systm.h>
54214501Srpaulo
55214501Srpaulo#include <vm/uma.h>
56214501Srpaulo
57214501Srpaulo#include <net/if.h>
58214501Srpaulo#include <net/route.h>
59214501Srpaulo
60214501Srpaulo#include <netinet/in.h>
61214501Srpaulo#include <netinet/in_systm.h>
62214501Srpaulo#include <netinet/in_pcb.h>
63214501Srpaulo#include <netinet/in_var.h>
64214501Srpaulo#include <netinet/ip.h>
65214501Srpaulo#include <netinet/ip_var.h>
66214501Srpaulo#include <netinet/ip_mroute.h>
67214501Srpaulo
68214501Srpaulo#include <netinet/ip_fw.h>
69214501Srpaulo#include <netinet/ip_dummynet.h>
70214501Srpaulo
71214501Srpaulo#ifdef IPSEC
72214501Srpaulo#include <netipsec/ipsec.h>
73214501Srpaulo#endif /*IPSEC*/
74214501Srpaulo
75214501Srpaulo#include <security/mac/mac_framework.h>
76214501Srpaulo
77214501Srpaulostruct	inpcbhead ripcb;
78214501Srpaulostruct	inpcbinfo ripcbinfo;
79214501Srpaulo
80214501Srpaulo/* control hooks for ipfw and dummynet */
81214501Srpauloip_fw_ctl_t *ip_fw_ctl_ptr = NULL;
82214501Srpauloip_dn_ctl_t *ip_dn_ctl_ptr = NULL;
83214501Srpaulo
84214501Srpaulo/*
85214501Srpaulo * hooks for multicast routing. They all default to NULL,
86214501Srpaulo * so leave them not initialized and rely on BSS being set to 0.
87214501Srpaulo */
88214501Srpaulo
89214501Srpaulo/* The socket used to communicate with the multicast routing daemon.  */
90214501Srpaulostruct socket  *ip_mrouter;
91214501Srpaulo
92214501Srpaulo/* The various mrouter and rsvp functions */
93214501Srpauloint (*ip_mrouter_set)(struct socket *, struct sockopt *);
94214501Srpauloint (*ip_mrouter_get)(struct socket *, struct sockopt *);
95214501Srpauloint (*ip_mrouter_done)(void);
96214501Srpauloint (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
97214501Srpaulo		   struct ip_moptions *);
98214501Srpauloint (*mrt_ioctl)(int, caddr_t, int);
99252726Srpauloint (*legal_vif_num)(int);
100252726Srpaulou_long (*ip_mcast_src)(int);
101214501Srpaulo
102214501Srpaulovoid (*rsvp_input_p)(struct mbuf *m, int off);
103214501Srpauloint (*ip_rsvp_vif)(struct socket *, struct sockopt *);
104214501Srpaulovoid (*ip_rsvp_force_done)(struct socket *);
105214501Srpaulo
106214501Srpaulo/*
107214501Srpaulo * Raw interface to IP protocol.
108214501Srpaulo */
109214501Srpaulo
110214501Srpaulo/*
111214501Srpaulo * Initialize raw connection block q.
112214501Srpaulo */
113214501Srpaulostatic void
114214501Srpaulorip_zone_change(void *tag)
115214501Srpaulo{
116214501Srpaulo
117214501Srpaulo	uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
118214501Srpaulo}
119214501Srpaulo
120214501Srpaulostatic int
121214501Srpaulorip_inpcb_init(void *mem, int size, int flags)
122214501Srpaulo{
123214501Srpaulo	struct inpcb *inp = mem;
124214501Srpaulo
125214501Srpaulo	INP_LOCK_INIT(inp, "inp", "rawinp");
126214501Srpaulo	return (0);
127214501Srpaulo}
128214501Srpaulo
129214501Srpaulovoid
130214501Srpaulorip_init(void)
131214501Srpaulo{
132214501Srpaulo
133214501Srpaulo	INP_INFO_LOCK_INIT(&ripcbinfo, "rip");
134214501Srpaulo	LIST_INIT(&ripcb);
135214501Srpaulo	ripcbinfo.ipi_listhead = &ripcb;
136214501Srpaulo	/*
137214501Srpaulo	 * XXX We don't use the hash list for raw IP, but it's easier
138252726Srpaulo	 * to allocate a one entry hash list than it is to check all
139252726Srpaulo	 * over the place for hashbase == NULL.
140252726Srpaulo	 */
141214501Srpaulo	ripcbinfo.ipi_hashbase = hashinit(1, M_PCB, &ripcbinfo.ipi_hashmask);
142214501Srpaulo	ripcbinfo.ipi_porthashbase = hashinit(1, M_PCB,
143214501Srpaulo	    &ripcbinfo.ipi_porthashmask);
144214501Srpaulo	ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
145214501Srpaulo	    NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
146214501Srpaulo	uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
147214501Srpaulo	EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change,
148214501Srpaulo		NULL, EVENTHANDLER_PRI_ANY);
149214501Srpaulo}
150214501Srpaulo
151214501Srpaulostatic struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
152214501Srpaulo
153214501Srpaulostatic int
154214501Srpauloraw_append(struct inpcb *last, struct ip *ip, struct mbuf *n)
155214501Srpaulo{
156214501Srpaulo	int policyfail = 0;
157214501Srpaulo
158214501Srpaulo	INP_RLOCK_ASSERT(last);
159214501Srpaulo
160214501Srpaulo#ifdef IPSEC
161214501Srpaulo	/* check AH/ESP integrity. */
162214501Srpaulo	if (ipsec4_in_reject(n, last)) {
163214501Srpaulo		policyfail = 1;
164214501Srpaulo	}
165214501Srpaulo#endif /* IPSEC */
166214501Srpaulo#ifdef MAC
167214501Srpaulo	if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
168252726Srpaulo		policyfail = 1;
169252726Srpaulo#endif
170252726Srpaulo	/* Check the minimum TTL for socket. */
171252726Srpaulo	if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
172252726Srpaulo		policyfail = 1;
173214501Srpaulo	if (!policyfail) {
174214501Srpaulo		struct mbuf *opts = NULL;
175214501Srpaulo		struct socket *so;
176214501Srpaulo
177214501Srpaulo		so = last->inp_socket;
178214501Srpaulo		if ((last->inp_flags & INP_CONTROLOPTS) ||
179214501Srpaulo		    (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
180214501Srpaulo			ip_savecontrol(last, &opts, ip, n);
181214501Srpaulo		SOCKBUF_LOCK(&so->so_rcv);
182214501Srpaulo		if (sbappendaddr_locked(&so->so_rcv,
183214501Srpaulo		    (struct sockaddr *)&ripsrc, n, opts) == 0) {
184214501Srpaulo			/* should notify about lost packet */
185214501Srpaulo			m_freem(n);
186214501Srpaulo			if (opts)
187214501Srpaulo				m_freem(opts);
188214501Srpaulo			SOCKBUF_UNLOCK(&so->so_rcv);
189214501Srpaulo		} else
190214501Srpaulo			sorwakeup_locked(so);
191214501Srpaulo	} else
192214501Srpaulo		m_freem(n);
193214501Srpaulo	return policyfail;
194214501Srpaulo}
195214501Srpaulo
196214501Srpaulo/*
197214501Srpaulo * Setup generic address and protocol structures
198214501Srpaulo * for raw_input routine, then pass them along with
199214501Srpaulo * mbuf chain.
200214501Srpaulo */
201214501Srpaulovoid
202214501Srpaulorip_input(struct mbuf *m, int off)
203214501Srpaulo{
204214501Srpaulo	struct ip *ip = mtod(m, struct ip *);
205214501Srpaulo	int proto = ip->ip_p;
206214501Srpaulo	struct inpcb *inp, *last;
207214501Srpaulo
208214501Srpaulo	INP_INFO_RLOCK(&ripcbinfo);
209214501Srpaulo	ripsrc.sin_addr = ip->ip_src;
210214501Srpaulo	last = NULL;
211214501Srpaulo	LIST_FOREACH(inp, &ripcb, inp_list) {
212252726Srpaulo		INP_RLOCK(inp);
213252726Srpaulo		if (inp->inp_ip_p && inp->inp_ip_p != proto) {
214214501Srpaulo	docontinue:
215214501Srpaulo			INP_RUNLOCK(inp);
216214501Srpaulo			continue;
217214501Srpaulo		}
218214501Srpaulo#ifdef INET6
219214501Srpaulo		if ((inp->inp_vflag & INP_IPV4) == 0)
220214501Srpaulo			goto docontinue;
221214501Srpaulo#endif
222214501Srpaulo		if (inp->inp_laddr.s_addr &&
223214501Srpaulo		    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
224214501Srpaulo			goto docontinue;
225214501Srpaulo		if (inp->inp_faddr.s_addr &&
226214501Srpaulo		    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
227214501Srpaulo			goto docontinue;
228214501Srpaulo		if (jailed(inp->inp_socket->so_cred))
229214501Srpaulo			if (htonl(prison_getip(inp->inp_socket->so_cred)) !=
230214501Srpaulo			    ip->ip_dst.s_addr)
231214501Srpaulo				goto docontinue;
232214501Srpaulo		if (last) {
233214501Srpaulo			struct mbuf *n;
234214501Srpaulo
235214501Srpaulo			n = m_copy(m, 0, (int)M_COPYALL);
236214501Srpaulo			if (n != NULL)
237214501Srpaulo				(void) raw_append(last, ip, n);
238214501Srpaulo			/* XXX count dropped packet */
239214501Srpaulo			INP_RUNLOCK(last);
240214501Srpaulo		}
241214501Srpaulo		last = inp;
242214501Srpaulo	}
243214501Srpaulo	if (last != NULL) {
244214501Srpaulo		if (raw_append(last, ip, m) != 0)
245214501Srpaulo			ipstat.ips_delivered--;
246214501Srpaulo		INP_RUNLOCK(last);
247214501Srpaulo	} else {
248214501Srpaulo		m_freem(m);
249214501Srpaulo		ipstat.ips_noproto++;
250214501Srpaulo		ipstat.ips_delivered--;
251214501Srpaulo	}
252214501Srpaulo	INP_INFO_RUNLOCK(&ripcbinfo);
253214501Srpaulo}
254214501Srpaulo
255214501Srpaulo/*
256214501Srpaulo * Generate IP header and pass packet to ip_output.
257214501Srpaulo * Tack on options user may have setup with control call.
258214501Srpaulo */
259214501Srpauloint
260214501Srpaulorip_output(struct mbuf *m, struct socket *so, u_long dst)
261214501Srpaulo{
262214501Srpaulo	struct ip *ip;
263214501Srpaulo	int error;
264214501Srpaulo	struct inpcb *inp = sotoinpcb(so);
265214501Srpaulo	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
266214501Srpaulo	    IP_ALLOWBROADCAST;
267214501Srpaulo
268214501Srpaulo	/*
269214501Srpaulo	 * If the user handed us a complete IP packet, use it.
270214501Srpaulo	 * Otherwise, allocate an mbuf for a header and fill it in.
271214501Srpaulo	 */
272214501Srpaulo	if ((inp->inp_flags & INP_HDRINCL) == 0) {
273214501Srpaulo		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
274214501Srpaulo			m_freem(m);
275214501Srpaulo			return(EMSGSIZE);
276214501Srpaulo		}
277214501Srpaulo		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
278214501Srpaulo		if (m == NULL)
279214501Srpaulo			return(ENOBUFS);
280214501Srpaulo
281214501Srpaulo		INP_RLOCK(inp);
282214501Srpaulo		ip = mtod(m, struct ip *);
283214501Srpaulo		ip->ip_tos = inp->inp_ip_tos;
284214501Srpaulo		if (inp->inp_flags & INP_DONTFRAG)
285214501Srpaulo			ip->ip_off = IP_DF;
286214501Srpaulo		else
287214501Srpaulo			ip->ip_off = 0;
288214501Srpaulo		ip->ip_p = inp->inp_ip_p;
289214501Srpaulo		ip->ip_len = m->m_pkthdr.len;
290214501Srpaulo		if (jailed(inp->inp_socket->so_cred))
291214501Srpaulo			ip->ip_src.s_addr =
292214501Srpaulo			    htonl(prison_getip(inp->inp_socket->so_cred));
293214501Srpaulo		else
294214501Srpaulo			ip->ip_src = inp->inp_laddr;
295214501Srpaulo		ip->ip_dst.s_addr = dst;
296214501Srpaulo		ip->ip_ttl = inp->inp_ip_ttl;
297214501Srpaulo	} else {
298214501Srpaulo		if (m->m_pkthdr.len > IP_MAXPACKET) {
299214501Srpaulo			m_freem(m);
300214501Srpaulo			return(EMSGSIZE);
301214501Srpaulo		}
302214501Srpaulo		INP_RLOCK(inp);
303214501Srpaulo		ip = mtod(m, struct ip *);
304214501Srpaulo		if (jailed(inp->inp_socket->so_cred)) {
305214501Srpaulo			if (ip->ip_src.s_addr !=
306214501Srpaulo			    htonl(prison_getip(inp->inp_socket->so_cred))) {
307214501Srpaulo				INP_RUNLOCK(inp);
308214501Srpaulo				m_freem(m);
309214501Srpaulo				return (EPERM);
310214501Srpaulo			}
311252726Srpaulo		}
312252726Srpaulo		/* don't allow both user specified and setsockopt options,
313214501Srpaulo		   and don't allow packet length sizes that will crash */
314214501Srpaulo		if (((ip->ip_hl != (sizeof (*ip) >> 2))
315214501Srpaulo		     && inp->inp_options)
316214501Srpaulo		    || (ip->ip_len > m->m_pkthdr.len)
317214501Srpaulo		    || (ip->ip_len < (ip->ip_hl << 2))) {
318214501Srpaulo			INP_RUNLOCK(inp);
319214501Srpaulo			m_freem(m);
320214501Srpaulo			return EINVAL;
321214501Srpaulo		}
322214501Srpaulo		if (ip->ip_id == 0)
323214501Srpaulo			ip->ip_id = ip_newid();
324214501Srpaulo		/* XXX prevent ip_output from overwriting header fields */
325214501Srpaulo		flags |= IP_RAWOUTPUT;
326214501Srpaulo		ipstat.ips_rawout++;
327214501Srpaulo	}
328214501Srpaulo
329214501Srpaulo	if (inp->inp_flags & INP_ONESBCAST)
330214501Srpaulo		flags |= IP_SENDONES;
331214501Srpaulo
332214501Srpaulo#ifdef MAC
333214501Srpaulo	mac_inpcb_create_mbuf(inp, m);
334214501Srpaulo#endif
335214501Srpaulo
336214501Srpaulo	error = ip_output(m, inp->inp_options, NULL, flags,
337214501Srpaulo	    inp->inp_moptions, inp);
338214501Srpaulo	INP_RUNLOCK(inp);
339214501Srpaulo	return error;
340214501Srpaulo}
341214501Srpaulo
342214501Srpaulo/*
343214501Srpaulo * Raw IP socket option processing.
344214501Srpaulo *
345214501Srpaulo * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
346214501Srpaulo * only be created by a privileged process, and as such, socket option
347214501Srpaulo * operations to manage system properties on any raw socket were allowed to
348214501Srpaulo * take place without explicit additional access control checks.  However,
349214501Srpaulo * raw sockets can now also be created in jail(), and therefore explicit
350214501Srpaulo * checks are now required.  Likewise, raw sockets can be used by a process
351214501Srpaulo * after it gives up privilege, so some caution is required.  For options
352214501Srpaulo * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
353214501Srpaulo * performed in ip_ctloutput() and therefore no check occurs here.
354214501Srpaulo * Unilaterally checking priv_check() here breaks normal IP socket option
355214501Srpaulo * operations on raw sockets.
356214501Srpaulo *
357214501Srpaulo * When adding new socket options here, make sure to add access control
358214501Srpaulo * checks here as necessary.
359214501Srpaulo */
360214501Srpauloint
361214501Srpaulorip_ctloutput(struct socket *so, struct sockopt *sopt)
362214501Srpaulo{
363214501Srpaulo	struct	inpcb *inp = sotoinpcb(so);
364214501Srpaulo	int	error, optval;
365214501Srpaulo
366214501Srpaulo	if (sopt->sopt_level != IPPROTO_IP)
367214501Srpaulo		return (EINVAL);
368214501Srpaulo
369214501Srpaulo	error = 0;
370214501Srpaulo	switch (sopt->sopt_dir) {
371214501Srpaulo	case SOPT_GET:
372214501Srpaulo		switch (sopt->sopt_name) {
373214501Srpaulo		case IP_HDRINCL:
374214501Srpaulo			optval = inp->inp_flags & INP_HDRINCL;
375214501Srpaulo			error = sooptcopyout(sopt, &optval, sizeof optval);
376214501Srpaulo			break;
377214501Srpaulo
378214501Srpaulo		case IP_FW_ADD:	/* ADD actually returns the body... */
379214501Srpaulo		case IP_FW_GET:
380214501Srpaulo		case IP_FW_TABLE_GETSIZE:
381214501Srpaulo		case IP_FW_TABLE_LIST:
382214501Srpaulo		case IP_FW_NAT_GET_CONFIG:
383214501Srpaulo		case IP_FW_NAT_GET_LOG:
384214501Srpaulo			/*
385214501Srpaulo			 * XXXRW: Isn't this checked one layer down?  Yes, it
386214501Srpaulo			 * is.
387214501Srpaulo			 */
388214501Srpaulo			error = priv_check(curthread, PRIV_NETINET_IPFW);
389214501Srpaulo			if (error != 0)
390214501Srpaulo				return (error);
391214501Srpaulo			if (ip_fw_ctl_ptr != NULL)
392214501Srpaulo				error = ip_fw_ctl_ptr(sopt);
393214501Srpaulo			else
394214501Srpaulo				error = ENOPROTOOPT;
395214501Srpaulo			break;
396214501Srpaulo
397214501Srpaulo		case IP_DUMMYNET_GET:
398214501Srpaulo			error = priv_check(curthread, PRIV_NETINET_DUMMYNET);
399214501Srpaulo			if (error != 0)
400214501Srpaulo				return (error);
401214501Srpaulo			if (ip_dn_ctl_ptr != NULL)
402214501Srpaulo				error = ip_dn_ctl_ptr(sopt);
403214501Srpaulo			else
404214501Srpaulo				error = ENOPROTOOPT;
405214501Srpaulo			break ;
406214501Srpaulo
407214501Srpaulo		case MRT_INIT:
408214501Srpaulo		case MRT_DONE:
409214501Srpaulo		case MRT_ADD_VIF:
410214501Srpaulo		case MRT_DEL_VIF:
411214501Srpaulo		case MRT_ADD_MFC:
412214501Srpaulo		case MRT_DEL_MFC:
413214501Srpaulo		case MRT_VERSION:
414214501Srpaulo		case MRT_ASSERT:
415214501Srpaulo		case MRT_API_SUPPORT:
416214501Srpaulo		case MRT_API_CONFIG:
417214501Srpaulo		case MRT_ADD_BW_UPCALL:
418214501Srpaulo		case MRT_DEL_BW_UPCALL:
419214501Srpaulo			error = priv_check(curthread, PRIV_NETINET_MROUTE);
420214501Srpaulo			if (error != 0)
421214501Srpaulo				return (error);
422214501Srpaulo			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
423214501Srpaulo				EOPNOTSUPP;
424214501Srpaulo			break;
425214501Srpaulo
426214501Srpaulo		default:
427214501Srpaulo			error = ip_ctloutput(so, sopt);
428214501Srpaulo			break;
429214501Srpaulo		}
430214501Srpaulo		break;
431
432	case SOPT_SET:
433		switch (sopt->sopt_name) {
434		case IP_HDRINCL:
435			error = sooptcopyin(sopt, &optval, sizeof optval,
436					    sizeof optval);
437			if (error)
438				break;
439			if (optval)
440				inp->inp_flags |= INP_HDRINCL;
441			else
442				inp->inp_flags &= ~INP_HDRINCL;
443			break;
444
445		case IP_FW_ADD:
446		case IP_FW_DEL:
447		case IP_FW_FLUSH:
448		case IP_FW_ZERO:
449		case IP_FW_RESETLOG:
450		case IP_FW_TABLE_ADD:
451		case IP_FW_TABLE_DEL:
452		case IP_FW_TABLE_FLUSH:
453		case IP_FW_NAT_CFG:
454		case IP_FW_NAT_DEL:
455			/*
456			 * XXXRW: Isn't this checked one layer down?
457			 */
458			error = priv_check(curthread, PRIV_NETINET_IPFW);
459			if (error != 0)
460				return (error);
461			if (ip_fw_ctl_ptr != NULL)
462				error = ip_fw_ctl_ptr(sopt);
463			else
464				error = ENOPROTOOPT;
465			break;
466
467		case IP_DUMMYNET_CONFIGURE:
468		case IP_DUMMYNET_DEL:
469		case IP_DUMMYNET_FLUSH:
470			error = priv_check(curthread, PRIV_NETINET_DUMMYNET);
471			if (error != 0)
472				return (error);
473			if (ip_dn_ctl_ptr != NULL)
474				error = ip_dn_ctl_ptr(sopt);
475			else
476				error = ENOPROTOOPT ;
477			break ;
478
479		case IP_RSVP_ON:
480			error = priv_check(curthread, PRIV_NETINET_MROUTE);
481			if (error != 0)
482				return (error);
483			error = ip_rsvp_init(so);
484			break;
485
486		case IP_RSVP_OFF:
487			error = priv_check(curthread, PRIV_NETINET_MROUTE);
488			if (error != 0)
489				return (error);
490			error = ip_rsvp_done();
491			break;
492
493		case IP_RSVP_VIF_ON:
494		case IP_RSVP_VIF_OFF:
495			error = priv_check(curthread, PRIV_NETINET_MROUTE);
496			if (error != 0)
497				return (error);
498			error = ip_rsvp_vif ?
499				ip_rsvp_vif(so, sopt) : EINVAL;
500			break;
501
502		case MRT_INIT:
503		case MRT_DONE:
504		case MRT_ADD_VIF:
505		case MRT_DEL_VIF:
506		case MRT_ADD_MFC:
507		case MRT_DEL_MFC:
508		case MRT_VERSION:
509		case MRT_ASSERT:
510		case MRT_API_SUPPORT:
511		case MRT_API_CONFIG:
512		case MRT_ADD_BW_UPCALL:
513		case MRT_DEL_BW_UPCALL:
514			error = priv_check(curthread, PRIV_NETINET_MROUTE);
515			if (error != 0)
516				return (error);
517			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
518					EOPNOTSUPP;
519			break;
520
521		default:
522			error = ip_ctloutput(so, sopt);
523			break;
524		}
525		break;
526	}
527
528	return (error);
529}
530
531/*
532 * This function exists solely to receive the PRC_IFDOWN messages which
533 * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
534 * and calls in_ifadown() to remove all routes corresponding to that address.
535 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
536 * interface routes.
537 */
538void
539rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
540{
541	struct in_ifaddr *ia;
542	struct ifnet *ifp;
543	int err;
544	int flags;
545
546	switch (cmd) {
547	case PRC_IFDOWN:
548		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
549			if (ia->ia_ifa.ifa_addr == sa
550			    && (ia->ia_flags & IFA_ROUTE)) {
551				/*
552				 * in_ifscrub kills the interface route.
553				 */
554				in_ifscrub(ia->ia_ifp, ia);
555				/*
556				 * in_ifadown gets rid of all the rest of
557				 * the routes.  This is not quite the right
558				 * thing to do, but at least if we are running
559				 * a routing process they will come back.
560				 */
561				in_ifadown(&ia->ia_ifa, 0);
562				break;
563			}
564		}
565		break;
566
567	case PRC_IFUP:
568		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
569			if (ia->ia_ifa.ifa_addr == sa)
570				break;
571		}
572		if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
573			return;
574		flags = RTF_UP;
575		ifp = ia->ia_ifa.ifa_ifp;
576
577		if ((ifp->if_flags & IFF_LOOPBACK)
578		    || (ifp->if_flags & IFF_POINTOPOINT))
579			flags |= RTF_HOST;
580
581		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
582		if (err == 0)
583			ia->ia_flags |= IFA_ROUTE;
584		break;
585	}
586}
587
588u_long	rip_sendspace = 9216;
589u_long	rip_recvspace = 9216;
590
591SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
592    &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
593SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
594    &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
595
596static int
597rip_attach(struct socket *so, int proto, struct thread *td)
598{
599	struct inpcb *inp;
600	int error;
601
602	inp = sotoinpcb(so);
603	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
604
605	error = priv_check(td, PRIV_NETINET_RAW);
606	if (error)
607		return error;
608	if (proto >= IPPROTO_MAX || proto < 0)
609		return EPROTONOSUPPORT;
610	error = soreserve(so, rip_sendspace, rip_recvspace);
611	if (error)
612		return error;
613	INP_INFO_WLOCK(&ripcbinfo);
614	error = in_pcballoc(so, &ripcbinfo);
615	if (error) {
616		INP_INFO_WUNLOCK(&ripcbinfo);
617		return error;
618	}
619	inp = (struct inpcb *)so->so_pcb;
620	INP_INFO_WUNLOCK(&ripcbinfo);
621	inp->inp_vflag |= INP_IPV4;
622	inp->inp_ip_p = proto;
623	inp->inp_ip_ttl = ip_defttl;
624	INP_WUNLOCK(inp);
625	return 0;
626}
627
628static void
629rip_detach(struct socket *so)
630{
631	struct inpcb *inp;
632
633	inp = sotoinpcb(so);
634	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
635	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
636	    ("rip_detach: not closed"));
637
638	INP_INFO_WLOCK(&ripcbinfo);
639	INP_WLOCK(inp);
640	if (so == ip_mrouter && ip_mrouter_done)
641		ip_mrouter_done();
642	if (ip_rsvp_force_done)
643		ip_rsvp_force_done(so);
644	if (so == ip_rsvpd)
645		ip_rsvp_done();
646	in_pcbdetach(inp);
647	in_pcbfree(inp);
648	INP_INFO_WUNLOCK(&ripcbinfo);
649}
650
651static void
652rip_dodisconnect(struct socket *so, struct inpcb *inp)
653{
654
655	INP_WLOCK_ASSERT(inp);
656
657	inp->inp_faddr.s_addr = INADDR_ANY;
658	SOCK_LOCK(so);
659	so->so_state &= ~SS_ISCONNECTED;
660	SOCK_UNLOCK(so);
661}
662
663static void
664rip_abort(struct socket *so)
665{
666	struct inpcb *inp;
667
668	inp = sotoinpcb(so);
669	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
670
671	INP_INFO_WLOCK(&ripcbinfo);
672	INP_WLOCK(inp);
673	rip_dodisconnect(so, inp);
674	INP_WUNLOCK(inp);
675	INP_INFO_WUNLOCK(&ripcbinfo);
676}
677
678static void
679rip_close(struct socket *so)
680{
681	struct inpcb *inp;
682
683	inp = sotoinpcb(so);
684	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
685
686	INP_INFO_WLOCK(&ripcbinfo);
687	INP_WLOCK(inp);
688	rip_dodisconnect(so, inp);
689	INP_WUNLOCK(inp);
690	INP_INFO_WUNLOCK(&ripcbinfo);
691}
692
693static int
694rip_disconnect(struct socket *so)
695{
696	struct inpcb *inp;
697
698	if ((so->so_state & SS_ISCONNECTED) == 0)
699		return ENOTCONN;
700
701	inp = sotoinpcb(so);
702	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
703	INP_INFO_WLOCK(&ripcbinfo);
704	INP_WLOCK(inp);
705	rip_dodisconnect(so, inp);
706	INP_WUNLOCK(inp);
707	INP_INFO_WUNLOCK(&ripcbinfo);
708	return (0);
709}
710
711static int
712rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
713{
714	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
715	struct inpcb *inp;
716
717	if (nam->sa_len != sizeof(*addr))
718		return EINVAL;
719
720	if (jailed(td->td_ucred)) {
721		if (addr->sin_addr.s_addr == INADDR_ANY)
722			addr->sin_addr.s_addr =
723			    htonl(prison_getip(td->td_ucred));
724		if (htonl(prison_getip(td->td_ucred)) != addr->sin_addr.s_addr)
725			return (EADDRNOTAVAIL);
726	}
727
728	if (TAILQ_EMPTY(&ifnet) ||
729	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
730	    (addr->sin_addr.s_addr &&
731	     ifa_ifwithaddr((struct sockaddr *)addr) == 0))
732		return EADDRNOTAVAIL;
733
734	inp = sotoinpcb(so);
735	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
736	INP_INFO_WLOCK(&ripcbinfo);
737	INP_WLOCK(inp);
738	inp->inp_laddr = addr->sin_addr;
739	INP_WUNLOCK(inp);
740	INP_INFO_WUNLOCK(&ripcbinfo);
741	return 0;
742}
743
744static int
745rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
746{
747	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
748	struct inpcb *inp;
749
750	if (nam->sa_len != sizeof(*addr))
751		return EINVAL;
752	if (TAILQ_EMPTY(&ifnet))
753		return EADDRNOTAVAIL;
754	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
755		return EAFNOSUPPORT;
756
757	inp = sotoinpcb(so);
758	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
759	INP_INFO_WLOCK(&ripcbinfo);
760	INP_WLOCK(inp);
761	inp->inp_faddr = addr->sin_addr;
762	soisconnected(so);
763	INP_WUNLOCK(inp);
764	INP_INFO_WUNLOCK(&ripcbinfo);
765	return 0;
766}
767
768static int
769rip_shutdown(struct socket *so)
770{
771	struct inpcb *inp;
772
773	inp = sotoinpcb(so);
774	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
775	INP_WLOCK(inp);
776	socantsendmore(so);
777	INP_WUNLOCK(inp);
778	return 0;
779}
780
781static int
782rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
783    struct mbuf *control, struct thread *td)
784{
785	struct inpcb *inp;
786	u_long dst;
787
788	inp = sotoinpcb(so);
789	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
790	/*
791	 * Note: 'dst' reads below are unlocked.
792	 */
793	if (so->so_state & SS_ISCONNECTED) {
794		if (nam) {
795			m_freem(m);
796			return EISCONN;
797		}
798		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
799	} else {
800		if (nam == NULL) {
801			m_freem(m);
802			return ENOTCONN;
803		}
804		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
805	}
806	return rip_output(m, so, dst);
807}
808
809static int
810rip_pcblist(SYSCTL_HANDLER_ARGS)
811{
812	int error, i, n;
813	struct inpcb *inp, **inp_list;
814	inp_gen_t gencnt;
815	struct xinpgen xig;
816
817	/*
818	 * The process of preparing the TCB list is too time-consuming and
819	 * resource-intensive to repeat twice on every request.
820	 */
821	if (req->oldptr == 0) {
822		n = ripcbinfo.ipi_count;
823		req->oldidx = 2 * (sizeof xig)
824			+ (n + n/8) * sizeof(struct xinpcb);
825		return 0;
826	}
827
828	if (req->newptr != 0)
829		return EPERM;
830
831	/*
832	 * OK, now we're committed to doing something.
833	 */
834	INP_INFO_RLOCK(&ripcbinfo);
835	gencnt = ripcbinfo.ipi_gencnt;
836	n = ripcbinfo.ipi_count;
837	INP_INFO_RUNLOCK(&ripcbinfo);
838
839	xig.xig_len = sizeof xig;
840	xig.xig_count = n;
841	xig.xig_gen = gencnt;
842	xig.xig_sogen = so_gencnt;
843	error = SYSCTL_OUT(req, &xig, sizeof xig);
844	if (error)
845		return error;
846
847	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
848	if (inp_list == 0)
849		return ENOMEM;
850
851	INP_INFO_RLOCK(&ripcbinfo);
852	for (inp = LIST_FIRST(ripcbinfo.ipi_listhead), i = 0; inp && i < n;
853	     inp = LIST_NEXT(inp, inp_list)) {
854		INP_RLOCK(inp);
855		if (inp->inp_gencnt <= gencnt &&
856		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) {
857			/* XXX held references? */
858			inp_list[i++] = inp;
859		}
860		INP_RUNLOCK(inp);
861	}
862	INP_INFO_RUNLOCK(&ripcbinfo);
863	n = i;
864
865	error = 0;
866	for (i = 0; i < n; i++) {
867		inp = inp_list[i];
868		INP_RLOCK(inp);
869		if (inp->inp_gencnt <= gencnt) {
870			struct xinpcb xi;
871			bzero(&xi, sizeof(xi));
872			xi.xi_len = sizeof xi;
873			/* XXX should avoid extra copy */
874			bcopy(inp, &xi.xi_inp, sizeof *inp);
875			if (inp->inp_socket)
876				sotoxsocket(inp->inp_socket, &xi.xi_socket);
877			INP_RUNLOCK(inp);
878			error = SYSCTL_OUT(req, &xi, sizeof xi);
879		} else
880			INP_RUNLOCK(inp);
881	}
882	if (!error) {
883		/*
884		 * Give the user an updated idea of our state.
885		 * If the generation differs from what we told
886		 * her before, she knows that something happened
887		 * while we were processing this request, and it
888		 * might be necessary to retry.
889		 */
890		INP_INFO_RLOCK(&ripcbinfo);
891		xig.xig_gen = ripcbinfo.ipi_gencnt;
892		xig.xig_sogen = so_gencnt;
893		xig.xig_count = ripcbinfo.ipi_count;
894		INP_INFO_RUNLOCK(&ripcbinfo);
895		error = SYSCTL_OUT(req, &xig, sizeof xig);
896	}
897	free(inp_list, M_TEMP);
898	return error;
899}
900
901SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
902	    rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
903
904struct pr_usrreqs rip_usrreqs = {
905	.pru_abort =		rip_abort,
906	.pru_attach =		rip_attach,
907	.pru_bind =		rip_bind,
908	.pru_connect =		rip_connect,
909	.pru_control =		in_control,
910	.pru_detach =		rip_detach,
911	.pru_disconnect =	rip_disconnect,
912	.pru_peeraddr =		in_getpeeraddr,
913	.pru_send =		rip_send,
914	.pru_shutdown =		rip_shutdown,
915	.pru_sockaddr =		in_getsockaddr,
916	.pru_sosetlabel =	in_pcbsosetlabel,
917	.pru_close =		rip_close,
918};
919