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