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