raw_ip.c revision 178285
1130803Smarcel/*-
2130803Smarcel * Copyright (c) 1982, 1986, 1988, 1993
3130803Smarcel *	The Regents of the University of California.  All rights reserved.
4130803Smarcel *
5130803Smarcel * Redistribution and use in source and binary forms, with or without
6130803Smarcel * modification, are permitted provided that the following conditions
7130803Smarcel * are met:
8130803Smarcel * 1. Redistributions of source code must retain the above copyright
9130803Smarcel *    notice, this list of conditions and the following disclaimer.
10130803Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11130803Smarcel *    notice, this list of conditions and the following disclaimer in the
12130803Smarcel *    documentation and/or other materials provided with the distribution.
13130803Smarcel * 4. Neither the name of the University nor the names of its contributors
14130803Smarcel *    may be used to endorse or promote products derived from this software
15130803Smarcel *    without specific prior written permission.
16130803Smarcel *
17130803Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18130803Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19130803Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20130803Smarcel * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21130803Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22130803Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23130803Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24130803Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25130803Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26130803Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27130803Smarcel * SUCH DAMAGE.
28130803Smarcel *
29130803Smarcel *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
30130803Smarcel */
31130803Smarcel
32130803Smarcel#include <sys/cdefs.h>
33130803Smarcel__FBSDID("$FreeBSD: head/sys/netinet/raw_ip.c 178285 2008-04-17 21:38:18Z rwatson $");
34130803Smarcel
35130803Smarcel#include "opt_inet6.h"
36130803Smarcel#include "opt_ipsec.h"
37130803Smarcel#include "opt_mac.h"
38130803Smarcel
39130803Smarcel#include <sys/param.h>
40130803Smarcel#include <sys/jail.h>
41130803Smarcel#include <sys/kernel.h>
42130803Smarcel#include <sys/lock.h>
43130803Smarcel#include <sys/malloc.h>
44130803Smarcel#include <sys/mbuf.h>
45130803Smarcel#include <sys/priv.h>
46130803Smarcel#include <sys/proc.h>
47130803Smarcel#include <sys/protosw.h>
48130803Smarcel#include <sys/signalvar.h>
49130803Smarcel#include <sys/socket.h>
50130803Smarcel#include <sys/socketvar.h>
51130803Smarcel#include <sys/sx.h>
52130803Smarcel#include <sys/sysctl.h>
53130803Smarcel#include <sys/systm.h>
54130803Smarcel
55130803Smarcel#include <vm/uma.h>
56130803Smarcel
57130803Smarcel#include <net/if.h>
58130803Smarcel#include <net/route.h>
59130803Smarcel
60130803Smarcel#include <netinet/in.h>
61130803Smarcel#include <netinet/in_systm.h>
62130803Smarcel#include <netinet/in_pcb.h>
63130803Smarcel#include <netinet/in_var.h>
64130803Smarcel#include <netinet/ip.h>
65130803Smarcel#include <netinet/ip_var.h>
66130803Smarcel#include <netinet/ip_mroute.h>
67130803Smarcel
68130803Smarcel#include <netinet/ip_fw.h>
69130803Smarcel#include <netinet/ip_dummynet.h>
70130803Smarcel
71130803Smarcel#ifdef IPSEC
72130803Smarcel#include <netipsec/ipsec.h>
73130803Smarcel#endif /*IPSEC*/
74130803Smarcel
75130803Smarcel#include <security/mac/mac_framework.h>
76130803Smarcel
77130803Smarcelstruct	inpcbhead ripcb;
78130803Smarcelstruct	inpcbinfo ripcbinfo;
79130803Smarcel
80130803Smarcel/* control hooks for ipfw and dummynet */
81130803Smarcelip_fw_ctl_t *ip_fw_ctl_ptr = NULL;
82130803Smarcelip_dn_ctl_t *ip_dn_ctl_ptr = NULL;
83130803Smarcel
84130803Smarcel/*
85130803Smarcel * hooks for multicast routing. They all default to NULL,
86130803Smarcel * so leave them not initialized and rely on BSS being set to 0.
87130803Smarcel */
88130803Smarcel
89130803Smarcel/* The socket used to communicate with the multicast routing daemon.  */
90130803Smarcelstruct socket  *ip_mrouter;
91130803Smarcel
92130803Smarcel/* The various mrouter and rsvp functions */
93130803Smarcelint (*ip_mrouter_set)(struct socket *, struct sockopt *);
94130803Smarcelint (*ip_mrouter_get)(struct socket *, struct sockopt *);
95130803Smarcelint (*ip_mrouter_done)(void);
96130803Smarcelint (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
97130803Smarcel		   struct ip_moptions *);
98130803Smarcelint (*mrt_ioctl)(int, caddr_t);
99130803Smarcelint (*legal_vif_num)(int);
100130803Smarcelu_long (*ip_mcast_src)(int);
101130803Smarcel
102130803Smarcelvoid (*rsvp_input_p)(struct mbuf *m, int off);
103130803Smarcelint (*ip_rsvp_vif)(struct socket *, struct sockopt *);
104130803Smarcelvoid (*ip_rsvp_force_done)(struct socket *);
105130803Smarcel
106130803Smarcel/*
107130803Smarcel * Raw interface to IP protocol.
108130803Smarcel */
109130803Smarcel
110130803Smarcel/*
111130803Smarcel * Initialize raw connection block q.
112130803Smarcel */
113130803Smarcelstatic void
114130803Smarcelrip_zone_change(void *tag)
115130803Smarcel{
116130803Smarcel
117130803Smarcel	uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
118130803Smarcel}
119130803Smarcel
120130803Smarcelstatic int
121130803Smarcelrip_inpcb_init(void *mem, int size, int flags)
122130803Smarcel{
123130803Smarcel	struct inpcb *inp = mem;
124130803Smarcel
125130803Smarcel	INP_LOCK_INIT(inp, "inp", "rawinp");
126130803Smarcel	return (0);
127130803Smarcel}
128130803Smarcel
129130803Smarcelvoid
130130803Smarcelrip_init(void)
131130803Smarcel{
132130803Smarcel
133130803Smarcel	INP_INFO_LOCK_INIT(&ripcbinfo, "rip");
134130803Smarcel	LIST_INIT(&ripcb);
135130803Smarcel	ripcbinfo.ipi_listhead = &ripcb;
136130803Smarcel	/*
137130803Smarcel	 * XXX We don't use the hash list for raw IP, but it's easier
138130803Smarcel	 * to allocate a one entry hash list than it is to check all
139130803Smarcel	 * over the place for hashbase == NULL.
140130803Smarcel	 */
141130803Smarcel	ripcbinfo.ipi_hashbase = hashinit(1, M_PCB, &ripcbinfo.ipi_hashmask);
142130803Smarcel	ripcbinfo.ipi_porthashbase = hashinit(1, M_PCB,
143130803Smarcel	    &ripcbinfo.ipi_porthashmask);
144130803Smarcel	ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
145130803Smarcel	    NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
146130803Smarcel	uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
147130803Smarcel	EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change,
148130803Smarcel		NULL, EVENTHANDLER_PRI_ANY);
149130803Smarcel}
150130803Smarcel
151130803Smarcelstatic struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
152130803Smarcel
153130803Smarcelstatic int
154130803Smarcelraw_append(struct inpcb *last, struct ip *ip, struct mbuf *n)
155130803Smarcel{
156130803Smarcel	int policyfail = 0;
157130803Smarcel
158130803Smarcel	INP_WLOCK_ASSERT(last);
159130803Smarcel
160130803Smarcel#ifdef IPSEC
161130803Smarcel	/* check AH/ESP integrity. */
162130803Smarcel	if (ipsec4_in_reject(n, last)) {
163130803Smarcel		policyfail = 1;
164130803Smarcel	}
165130803Smarcel#endif /* IPSEC */
166130803Smarcel#ifdef MAC
167130803Smarcel	if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
168130803Smarcel		policyfail = 1;
169130803Smarcel#endif
170130803Smarcel	/* Check the minimum TTL for socket. */
171130803Smarcel	if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
172130803Smarcel		policyfail = 1;
173130803Smarcel	if (!policyfail) {
174130803Smarcel		struct mbuf *opts = NULL;
175130803Smarcel		struct socket *so;
176130803Smarcel
177130803Smarcel		so = last->inp_socket;
178130803Smarcel		if ((last->inp_flags & INP_CONTROLOPTS) ||
179130803Smarcel		    (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
180130803Smarcel			ip_savecontrol(last, &opts, ip, n);
181130803Smarcel		SOCKBUF_LOCK(&so->so_rcv);
182130803Smarcel		if (sbappendaddr_locked(&so->so_rcv,
183130803Smarcel		    (struct sockaddr *)&ripsrc, n, opts) == 0) {
184130803Smarcel			/* should notify about lost packet */
185130803Smarcel			m_freem(n);
186130803Smarcel			if (opts)
187130803Smarcel				m_freem(opts);
188130803Smarcel			SOCKBUF_UNLOCK(&so->so_rcv);
189130803Smarcel		} else
190130803Smarcel			sorwakeup_locked(so);
191130803Smarcel	} else
192130803Smarcel		m_freem(n);
193130803Smarcel	return policyfail;
194130803Smarcel}
195130803Smarcel
196130803Smarcel/*
197130803Smarcel * Setup generic address and protocol structures
198130803Smarcel * for raw_input routine, then pass them along with
199130803Smarcel * mbuf chain.
200130803Smarcel */
201130803Smarcelvoid
202130803Smarcelrip_input(struct mbuf *m, int off)
203130803Smarcel{
204130803Smarcel	struct ip *ip = mtod(m, struct ip *);
205130803Smarcel	int proto = ip->ip_p;
206130803Smarcel	struct inpcb *inp, *last;
207130803Smarcel
208130803Smarcel	INP_INFO_RLOCK(&ripcbinfo);
209130803Smarcel	ripsrc.sin_addr = ip->ip_src;
210130803Smarcel	last = NULL;
211130803Smarcel	LIST_FOREACH(inp, &ripcb, inp_list) {
212130803Smarcel		INP_WLOCK(inp);
213130803Smarcel		if (inp->inp_ip_p && inp->inp_ip_p != proto) {
214130803Smarcel	docontinue:
215130803Smarcel			INP_WUNLOCK(inp);
216130803Smarcel			continue;
217130803Smarcel		}
218130803Smarcel#ifdef INET6
219130803Smarcel		if ((inp->inp_vflag & INP_IPV4) == 0)
220130803Smarcel			goto docontinue;
221130803Smarcel#endif
222130803Smarcel		if (inp->inp_laddr.s_addr &&
223130803Smarcel		    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
224130803Smarcel			goto docontinue;
225130803Smarcel		if (inp->inp_faddr.s_addr &&
226130803Smarcel		    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
227130803Smarcel			goto docontinue;
228130803Smarcel		if (jailed(inp->inp_socket->so_cred))
229130803Smarcel			if (htonl(prison_getip(inp->inp_socket->so_cred)) !=
230130803Smarcel			    ip->ip_dst.s_addr)
231130803Smarcel				goto docontinue;
232130803Smarcel		if (last) {
233130803Smarcel			struct mbuf *n;
234130803Smarcel
235130803Smarcel			n = m_copy(m, 0, (int)M_COPYALL);
236130803Smarcel			if (n != NULL)
237130803Smarcel				(void) raw_append(last, ip, n);
238130803Smarcel			/* XXX count dropped packet */
239130803Smarcel			INP_WUNLOCK(last);
240130803Smarcel		}
241130803Smarcel		last = inp;
242130803Smarcel	}
243130803Smarcel	if (last != NULL) {
244130803Smarcel		if (raw_append(last, ip, m) != 0)
245130803Smarcel			ipstat.ips_delivered--;
246130803Smarcel		INP_WUNLOCK(last);
247130803Smarcel	} else {
248130803Smarcel		m_freem(m);
249130803Smarcel		ipstat.ips_noproto++;
250130803Smarcel		ipstat.ips_delivered--;
251130803Smarcel	}
252130803Smarcel	INP_INFO_RUNLOCK(&ripcbinfo);
253130803Smarcel}
254130803Smarcel
255130803Smarcel/*
256130803Smarcel * Generate IP header and pass packet to ip_output.
257130803Smarcel * Tack on options user may have setup with control call.
258130803Smarcel */
259130803Smarcelint
260130803Smarcelrip_output(struct mbuf *m, struct socket *so, u_long dst)
261130803Smarcel{
262130803Smarcel	struct ip *ip;
263130803Smarcel	int error;
264130803Smarcel	struct inpcb *inp = sotoinpcb(so);
265130803Smarcel	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
266130803Smarcel	    IP_ALLOWBROADCAST;
267130803Smarcel
268130803Smarcel	/*
269130803Smarcel	 * If the user handed us a complete IP packet, use it.
270130803Smarcel	 * Otherwise, allocate an mbuf for a header and fill it in.
271130803Smarcel	 */
272130803Smarcel	if ((inp->inp_flags & INP_HDRINCL) == 0) {
273130803Smarcel		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
274130803Smarcel			m_freem(m);
275130803Smarcel			return(EMSGSIZE);
276130803Smarcel		}
277130803Smarcel		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
278130803Smarcel		if (m == NULL)
279130803Smarcel			return(ENOBUFS);
280130803Smarcel
281130803Smarcel		INP_WLOCK(inp);
282130803Smarcel		ip = mtod(m, struct ip *);
283130803Smarcel		ip->ip_tos = inp->inp_ip_tos;
284130803Smarcel		if (inp->inp_flags & INP_DONTFRAG)
285130803Smarcel			ip->ip_off = IP_DF;
286130803Smarcel		else
287130803Smarcel			ip->ip_off = 0;
288130803Smarcel		ip->ip_p = inp->inp_ip_p;
289130803Smarcel		ip->ip_len = m->m_pkthdr.len;
290130803Smarcel		if (jailed(inp->inp_socket->so_cred))
291130803Smarcel			ip->ip_src.s_addr =
292130803Smarcel			    htonl(prison_getip(inp->inp_socket->so_cred));
293130803Smarcel		else
294130803Smarcel			ip->ip_src = inp->inp_laddr;
295130803Smarcel		ip->ip_dst.s_addr = dst;
296130803Smarcel		ip->ip_ttl = inp->inp_ip_ttl;
297130803Smarcel	} else {
298130803Smarcel		if (m->m_pkthdr.len > IP_MAXPACKET) {
299130803Smarcel			m_freem(m);
300130803Smarcel			return(EMSGSIZE);
301130803Smarcel		}
302130803Smarcel		INP_WLOCK(inp);
303130803Smarcel		ip = mtod(m, struct ip *);
304130803Smarcel		if (jailed(inp->inp_socket->so_cred)) {
305130803Smarcel			if (ip->ip_src.s_addr !=
306130803Smarcel			    htonl(prison_getip(inp->inp_socket->so_cred))) {
307130803Smarcel				INP_WUNLOCK(inp);
308130803Smarcel				m_freem(m);
309130803Smarcel				return (EPERM);
310130803Smarcel			}
311130803Smarcel		}
312130803Smarcel		/* don't allow both user specified and setsockopt options,
313130803Smarcel		   and don't allow packet length sizes that will crash */
314130803Smarcel		if (((ip->ip_hl != (sizeof (*ip) >> 2))
315130803Smarcel		     && inp->inp_options)
316130803Smarcel		    || (ip->ip_len > m->m_pkthdr.len)
317130803Smarcel		    || (ip->ip_len < (ip->ip_hl << 2))) {
318130803Smarcel			INP_WUNLOCK(inp);
319130803Smarcel			m_freem(m);
320130803Smarcel			return EINVAL;
321130803Smarcel		}
322130803Smarcel		if (ip->ip_id == 0)
323130803Smarcel			ip->ip_id = ip_newid();
324130803Smarcel		/* XXX prevent ip_output from overwriting header fields */
325130803Smarcel		flags |= IP_RAWOUTPUT;
326130803Smarcel		ipstat.ips_rawout++;
327130803Smarcel	}
328130803Smarcel
329130803Smarcel	if (inp->inp_flags & INP_ONESBCAST)
330130803Smarcel		flags |= IP_SENDONES;
331130803Smarcel
332130803Smarcel#ifdef MAC
333130803Smarcel	mac_inpcb_create_mbuf(inp, m);
334130803Smarcel#endif
335130803Smarcel
336130803Smarcel	error = ip_output(m, inp->inp_options, NULL, flags,
337130803Smarcel	    inp->inp_moptions, inp);
338130803Smarcel	INP_WUNLOCK(inp);
339130803Smarcel	return error;
340130803Smarcel}
341130803Smarcel
342130803Smarcel/*
343130803Smarcel * Raw IP socket option processing.
344130803Smarcel *
345130803Smarcel * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
346130803Smarcel * only be created by a privileged process, and as such, socket option
347130803Smarcel * operations to manage system properties on any raw socket were allowed to
348130803Smarcel * take place without explicit additional access control checks.  However,
349130803Smarcel * raw sockets can now also be created in jail(), and therefore explicit
350130803Smarcel * checks are now required.  Likewise, raw sockets can be used by a process
351130803Smarcel * after it gives up privilege, so some caution is required.  For options
352 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
353 * performed in ip_ctloutput() and therefore no check occurs here.
354 * Unilaterally checking priv_check() here breaks normal IP socket option
355 * operations on raw sockets.
356 *
357 * When adding new socket options here, make sure to add access control
358 * checks here as necessary.
359 */
360int
361rip_ctloutput(struct socket *so, struct sockopt *sopt)
362{
363	struct	inpcb *inp = sotoinpcb(so);
364	int	error, optval;
365
366	if (sopt->sopt_level != IPPROTO_IP)
367		return (EINVAL);
368
369	error = 0;
370	switch (sopt->sopt_dir) {
371	case SOPT_GET:
372		switch (sopt->sopt_name) {
373		case IP_HDRINCL:
374			optval = inp->inp_flags & INP_HDRINCL;
375			error = sooptcopyout(sopt, &optval, sizeof optval);
376			break;
377
378		case IP_FW_ADD:	/* ADD actually returns the body... */
379		case IP_FW_GET:
380		case IP_FW_TABLE_GETSIZE:
381		case IP_FW_TABLE_LIST:
382		case IP_FW_NAT_GET_CONFIG:
383		case IP_FW_NAT_GET_LOG:
384			/*
385			 * XXXRW: Isn't this checked one layer down?  Yes, it
386			 * is.
387			 */
388			error = priv_check(curthread, PRIV_NETINET_IPFW);
389			if (error != 0)
390				return (error);
391			if (ip_fw_ctl_ptr != NULL)
392				error = ip_fw_ctl_ptr(sopt);
393			else
394				error = ENOPROTOOPT;
395			break;
396
397		case IP_DUMMYNET_GET:
398			error = priv_check(curthread, PRIV_NETINET_DUMMYNET);
399			if (error != 0)
400				return (error);
401			if (ip_dn_ctl_ptr != NULL)
402				error = ip_dn_ctl_ptr(sopt);
403			else
404				error = ENOPROTOOPT;
405			break ;
406
407		case MRT_INIT:
408		case MRT_DONE:
409		case MRT_ADD_VIF:
410		case MRT_DEL_VIF:
411		case MRT_ADD_MFC:
412		case MRT_DEL_MFC:
413		case MRT_VERSION:
414		case MRT_ASSERT:
415		case MRT_API_SUPPORT:
416		case MRT_API_CONFIG:
417		case MRT_ADD_BW_UPCALL:
418		case MRT_DEL_BW_UPCALL:
419			error = priv_check(curthread, PRIV_NETINET_MROUTE);
420			if (error != 0)
421				return (error);
422			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
423				EOPNOTSUPP;
424			break;
425
426		default:
427			error = ip_ctloutput(so, sopt);
428			break;
429		}
430		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_WLOCK(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_WUNLOCK(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_WLOCK(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_WUNLOCK(inp);
878			error = SYSCTL_OUT(req, &xi, sizeof xi);
879		} else
880			INP_WUNLOCK(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