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