raw_ip.c revision 200473
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 200473 2009-12-13 13:57:32Z bz $");
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 and dummynet.
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;
89int (*ip_dn_ctl_ptr)(struct sockopt *) = NULL;
90int (*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_without_vnet(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_without_vnet(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_FW3:	/* generic ipfw v.3 functions */
539		case IP_FW_ADD:	/* ADD actually returns the body... */
540		case IP_FW_GET:
541		case IP_FW_TABLE_GETSIZE:
542		case IP_FW_TABLE_LIST:
543		case IP_FW_NAT_GET_CONFIG:
544		case IP_FW_NAT_GET_LOG:
545			if (V_ip_fw_ctl_ptr != NULL)
546				error = V_ip_fw_ctl_ptr(sopt);
547			else
548				error = ENOPROTOOPT;
549			break;
550
551		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
552		case IP_DUMMYNET_GET:
553			if (ip_dn_ctl_ptr != NULL)
554				error = ip_dn_ctl_ptr(sopt);
555			else
556				error = ENOPROTOOPT;
557			break ;
558
559		case MRT_INIT:
560		case MRT_DONE:
561		case MRT_ADD_VIF:
562		case MRT_DEL_VIF:
563		case MRT_ADD_MFC:
564		case MRT_DEL_MFC:
565		case MRT_VERSION:
566		case MRT_ASSERT:
567		case MRT_API_SUPPORT:
568		case MRT_API_CONFIG:
569		case MRT_ADD_BW_UPCALL:
570		case MRT_DEL_BW_UPCALL:
571			error = priv_check(curthread, PRIV_NETINET_MROUTE);
572			if (error != 0)
573				return (error);
574			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
575				EOPNOTSUPP;
576			break;
577
578		default:
579			error = ip_ctloutput(so, sopt);
580			break;
581		}
582		break;
583
584	case SOPT_SET:
585		switch (sopt->sopt_name) {
586		case IP_HDRINCL:
587			error = sooptcopyin(sopt, &optval, sizeof optval,
588					    sizeof optval);
589			if (error)
590				break;
591			if (optval)
592				inp->inp_flags |= INP_HDRINCL;
593			else
594				inp->inp_flags &= ~INP_HDRINCL;
595			break;
596
597		case IP_FW3:	/* generic ipfw v.3 functions */
598		case IP_FW_ADD:
599		case IP_FW_DEL:
600		case IP_FW_FLUSH:
601		case IP_FW_ZERO:
602		case IP_FW_RESETLOG:
603		case IP_FW_TABLE_ADD:
604		case IP_FW_TABLE_DEL:
605		case IP_FW_TABLE_FLUSH:
606		case IP_FW_NAT_CFG:
607		case IP_FW_NAT_DEL:
608			if (V_ip_fw_ctl_ptr != NULL)
609				error = V_ip_fw_ctl_ptr(sopt);
610			else
611				error = ENOPROTOOPT;
612			break;
613
614		case IP_DUMMYNET3:	/* generic dummynet v.3 functions */
615		case IP_DUMMYNET_CONFIGURE:
616		case IP_DUMMYNET_DEL:
617		case IP_DUMMYNET_FLUSH:
618			if (ip_dn_ctl_ptr != NULL)
619				error = ip_dn_ctl_ptr(sopt);
620			else
621				error = ENOPROTOOPT ;
622			break ;
623
624		case IP_RSVP_ON:
625			error = priv_check(curthread, PRIV_NETINET_MROUTE);
626			if (error != 0)
627				return (error);
628			error = ip_rsvp_init(so);
629			break;
630
631		case IP_RSVP_OFF:
632			error = priv_check(curthread, PRIV_NETINET_MROUTE);
633			if (error != 0)
634				return (error);
635			error = ip_rsvp_done();
636			break;
637
638		case IP_RSVP_VIF_ON:
639		case IP_RSVP_VIF_OFF:
640			error = priv_check(curthread, PRIV_NETINET_MROUTE);
641			if (error != 0)
642				return (error);
643			error = ip_rsvp_vif ?
644				ip_rsvp_vif(so, sopt) : EINVAL;
645			break;
646
647		case MRT_INIT:
648		case MRT_DONE:
649		case MRT_ADD_VIF:
650		case MRT_DEL_VIF:
651		case MRT_ADD_MFC:
652		case MRT_DEL_MFC:
653		case MRT_VERSION:
654		case MRT_ASSERT:
655		case MRT_API_SUPPORT:
656		case MRT_API_CONFIG:
657		case MRT_ADD_BW_UPCALL:
658		case MRT_DEL_BW_UPCALL:
659			error = priv_check(curthread, PRIV_NETINET_MROUTE);
660			if (error != 0)
661				return (error);
662			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
663					EOPNOTSUPP;
664			break;
665
666		default:
667			error = ip_ctloutput(so, sopt);
668			break;
669		}
670		break;
671	}
672
673	return (error);
674}
675
676/*
677 * This function exists solely to receive the PRC_IFDOWN messages which are
678 * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
679 * in_ifadown() to remove all routes corresponding to that address.  It also
680 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
681 * routes.
682 */
683void
684rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
685{
686	struct in_ifaddr *ia;
687	struct ifnet *ifp;
688	int err;
689	int flags;
690
691	switch (cmd) {
692	case PRC_IFDOWN:
693		IN_IFADDR_RLOCK();
694		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
695			if (ia->ia_ifa.ifa_addr == sa
696			    && (ia->ia_flags & IFA_ROUTE)) {
697				ifa_ref(&ia->ia_ifa);
698				IN_IFADDR_RUNLOCK();
699				/*
700				 * in_ifscrub kills the interface route.
701				 */
702				in_ifscrub(ia->ia_ifp, ia);
703				/*
704				 * in_ifadown gets rid of all the rest of the
705				 * routes.  This is not quite the right thing
706				 * to do, but at least if we are running a
707				 * routing process they will come back.
708				 */
709				in_ifadown(&ia->ia_ifa, 0);
710				ifa_free(&ia->ia_ifa);
711				break;
712			}
713		}
714		if (ia == NULL)		/* If ia matched, already unlocked. */
715			IN_IFADDR_RUNLOCK();
716		break;
717
718	case PRC_IFUP:
719		IN_IFADDR_RLOCK();
720		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
721			if (ia->ia_ifa.ifa_addr == sa)
722				break;
723		}
724		if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
725			IN_IFADDR_RUNLOCK();
726			return;
727		}
728		ifa_ref(&ia->ia_ifa);
729		IN_IFADDR_RUNLOCK();
730		flags = RTF_UP;
731		ifp = ia->ia_ifa.ifa_ifp;
732
733		if ((ifp->if_flags & IFF_LOOPBACK)
734		    || (ifp->if_flags & IFF_POINTOPOINT))
735			flags |= RTF_HOST;
736
737		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
738		if (err == 0)
739			ia->ia_flags |= IFA_ROUTE;
740		err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
741		ifa_free(&ia->ia_ifa);
742		break;
743	}
744}
745
746u_long	rip_sendspace = 9216;
747u_long	rip_recvspace = 9216;
748
749SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
750    &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
751SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
752    &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
753
754static int
755rip_attach(struct socket *so, int proto, struct thread *td)
756{
757	struct inpcb *inp;
758	int error;
759
760	inp = sotoinpcb(so);
761	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
762
763	error = priv_check(td, PRIV_NETINET_RAW);
764	if (error)
765		return (error);
766	if (proto >= IPPROTO_MAX || proto < 0)
767		return EPROTONOSUPPORT;
768	error = soreserve(so, rip_sendspace, rip_recvspace);
769	if (error)
770		return (error);
771	INP_INFO_WLOCK(&V_ripcbinfo);
772	error = in_pcballoc(so, &V_ripcbinfo);
773	if (error) {
774		INP_INFO_WUNLOCK(&V_ripcbinfo);
775		return (error);
776	}
777	inp = (struct inpcb *)so->so_pcb;
778	inp->inp_vflag |= INP_IPV4;
779	inp->inp_ip_p = proto;
780	inp->inp_ip_ttl = V_ip_defttl;
781	rip_inshash(inp);
782	INP_INFO_WUNLOCK(&V_ripcbinfo);
783	INP_WUNLOCK(inp);
784	return (0);
785}
786
787static void
788rip_detach(struct socket *so)
789{
790	struct inpcb *inp;
791
792	inp = sotoinpcb(so);
793	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
794	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
795	    ("rip_detach: not closed"));
796
797	INP_INFO_WLOCK(&V_ripcbinfo);
798	INP_WLOCK(inp);
799	rip_delhash(inp);
800	if (so == V_ip_mrouter && ip_mrouter_done)
801		ip_mrouter_done();
802	if (ip_rsvp_force_done)
803		ip_rsvp_force_done(so);
804	if (so == V_ip_rsvpd)
805		ip_rsvp_done();
806	in_pcbdetach(inp);
807	in_pcbfree(inp);
808	INP_INFO_WUNLOCK(&V_ripcbinfo);
809}
810
811static void
812rip_dodisconnect(struct socket *so, struct inpcb *inp)
813{
814
815	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
816	INP_WLOCK_ASSERT(inp);
817
818	rip_delhash(inp);
819	inp->inp_faddr.s_addr = INADDR_ANY;
820	rip_inshash(inp);
821	SOCK_LOCK(so);
822	so->so_state &= ~SS_ISCONNECTED;
823	SOCK_UNLOCK(so);
824}
825
826static void
827rip_abort(struct socket *so)
828{
829	struct inpcb *inp;
830
831	inp = sotoinpcb(so);
832	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
833
834	INP_INFO_WLOCK(&V_ripcbinfo);
835	INP_WLOCK(inp);
836	rip_dodisconnect(so, inp);
837	INP_WUNLOCK(inp);
838	INP_INFO_WUNLOCK(&V_ripcbinfo);
839}
840
841static void
842rip_close(struct socket *so)
843{
844	struct inpcb *inp;
845
846	inp = sotoinpcb(so);
847	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
848
849	INP_INFO_WLOCK(&V_ripcbinfo);
850	INP_WLOCK(inp);
851	rip_dodisconnect(so, inp);
852	INP_WUNLOCK(inp);
853	INP_INFO_WUNLOCK(&V_ripcbinfo);
854}
855
856static int
857rip_disconnect(struct socket *so)
858{
859	struct inpcb *inp;
860
861	if ((so->so_state & SS_ISCONNECTED) == 0)
862		return (ENOTCONN);
863
864	inp = sotoinpcb(so);
865	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
866
867	INP_INFO_WLOCK(&V_ripcbinfo);
868	INP_WLOCK(inp);
869	rip_dodisconnect(so, inp);
870	INP_WUNLOCK(inp);
871	INP_INFO_WUNLOCK(&V_ripcbinfo);
872	return (0);
873}
874
875static int
876rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
877{
878	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
879	struct inpcb *inp;
880	int error;
881
882	if (nam->sa_len != sizeof(*addr))
883		return (EINVAL);
884
885	error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
886	if (error != 0)
887		return (error);
888
889	inp = sotoinpcb(so);
890	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
891
892	if (TAILQ_EMPTY(&V_ifnet) ||
893	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
894	    (addr->sin_addr.s_addr &&
895	     (inp->inp_flags & INP_BINDANY) == 0 &&
896	     ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
897		return (EADDRNOTAVAIL);
898
899	INP_INFO_WLOCK(&V_ripcbinfo);
900	INP_WLOCK(inp);
901	rip_delhash(inp);
902	inp->inp_laddr = addr->sin_addr;
903	rip_inshash(inp);
904	INP_WUNLOCK(inp);
905	INP_INFO_WUNLOCK(&V_ripcbinfo);
906	return (0);
907}
908
909static int
910rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
911{
912	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
913	struct inpcb *inp;
914
915	if (nam->sa_len != sizeof(*addr))
916		return (EINVAL);
917	if (TAILQ_EMPTY(&V_ifnet))
918		return (EADDRNOTAVAIL);
919	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
920		return (EAFNOSUPPORT);
921
922	inp = sotoinpcb(so);
923	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
924
925	INP_INFO_WLOCK(&V_ripcbinfo);
926	INP_WLOCK(inp);
927	rip_delhash(inp);
928	inp->inp_faddr = addr->sin_addr;
929	rip_inshash(inp);
930	soisconnected(so);
931	INP_WUNLOCK(inp);
932	INP_INFO_WUNLOCK(&V_ripcbinfo);
933	return (0);
934}
935
936static int
937rip_shutdown(struct socket *so)
938{
939	struct inpcb *inp;
940
941	inp = sotoinpcb(so);
942	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
943
944	INP_WLOCK(inp);
945	socantsendmore(so);
946	INP_WUNLOCK(inp);
947	return (0);
948}
949
950static int
951rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
952    struct mbuf *control, struct thread *td)
953{
954	struct inpcb *inp;
955	u_long dst;
956
957	inp = sotoinpcb(so);
958	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
959
960	/*
961	 * Note: 'dst' reads below are unlocked.
962	 */
963	if (so->so_state & SS_ISCONNECTED) {
964		if (nam) {
965			m_freem(m);
966			return (EISCONN);
967		}
968		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
969	} else {
970		if (nam == NULL) {
971			m_freem(m);
972			return (ENOTCONN);
973		}
974		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
975	}
976	return (rip_output(m, so, dst));
977}
978
979static int
980rip_pcblist(SYSCTL_HANDLER_ARGS)
981{
982	int error, i, n;
983	struct inpcb *inp, **inp_list;
984	inp_gen_t gencnt;
985	struct xinpgen xig;
986
987	/*
988	 * The process of preparing the TCB list is too time-consuming and
989	 * resource-intensive to repeat twice on every request.
990	 */
991	if (req->oldptr == 0) {
992		n = V_ripcbinfo.ipi_count;
993		req->oldidx = 2 * (sizeof xig)
994		    + (n + n/8) * sizeof(struct xinpcb);
995		return (0);
996	}
997
998	if (req->newptr != 0)
999		return (EPERM);
1000
1001	/*
1002	 * OK, now we're committed to doing something.
1003	 */
1004	INP_INFO_RLOCK(&V_ripcbinfo);
1005	gencnt = V_ripcbinfo.ipi_gencnt;
1006	n = V_ripcbinfo.ipi_count;
1007	INP_INFO_RUNLOCK(&V_ripcbinfo);
1008
1009	xig.xig_len = sizeof xig;
1010	xig.xig_count = n;
1011	xig.xig_gen = gencnt;
1012	xig.xig_sogen = so_gencnt;
1013	error = SYSCTL_OUT(req, &xig, sizeof xig);
1014	if (error)
1015		return (error);
1016
1017	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1018	if (inp_list == 0)
1019		return (ENOMEM);
1020
1021	INP_INFO_RLOCK(&V_ripcbinfo);
1022	for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1023	     inp = LIST_NEXT(inp, inp_list)) {
1024		INP_RLOCK(inp);
1025		if (inp->inp_gencnt <= gencnt &&
1026		    cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1027			/* XXX held references? */
1028			inp_list[i++] = inp;
1029		}
1030		INP_RUNLOCK(inp);
1031	}
1032	INP_INFO_RUNLOCK(&V_ripcbinfo);
1033	n = i;
1034
1035	error = 0;
1036	for (i = 0; i < n; i++) {
1037		inp = inp_list[i];
1038		INP_RLOCK(inp);
1039		if (inp->inp_gencnt <= gencnt) {
1040			struct xinpcb xi;
1041
1042			bzero(&xi, sizeof(xi));
1043			xi.xi_len = sizeof xi;
1044			/* XXX should avoid extra copy */
1045			bcopy(inp, &xi.xi_inp, sizeof *inp);
1046			if (inp->inp_socket)
1047				sotoxsocket(inp->inp_socket, &xi.xi_socket);
1048			INP_RUNLOCK(inp);
1049			error = SYSCTL_OUT(req, &xi, sizeof xi);
1050		} else
1051			INP_RUNLOCK(inp);
1052	}
1053	if (!error) {
1054		/*
1055		 * Give the user an updated idea of our state.  If the
1056		 * generation differs from what we told her before, she knows
1057		 * that something happened while we were processing this
1058		 * request, and it might be necessary to retry.
1059		 */
1060		INP_INFO_RLOCK(&V_ripcbinfo);
1061		xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1062		xig.xig_sogen = so_gencnt;
1063		xig.xig_count = V_ripcbinfo.ipi_count;
1064		INP_INFO_RUNLOCK(&V_ripcbinfo);
1065		error = SYSCTL_OUT(req, &xig, sizeof xig);
1066	}
1067	free(inp_list, M_TEMP);
1068	return (error);
1069}
1070
1071SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
1072    rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1073
1074struct pr_usrreqs rip_usrreqs = {
1075	.pru_abort =		rip_abort,
1076	.pru_attach =		rip_attach,
1077	.pru_bind =		rip_bind,
1078	.pru_connect =		rip_connect,
1079	.pru_control =		in_control,
1080	.pru_detach =		rip_detach,
1081	.pru_disconnect =	rip_disconnect,
1082	.pru_peeraddr =		in_getpeeraddr,
1083	.pru_send =		rip_send,
1084	.pru_shutdown =		rip_shutdown,
1085	.pru_sockaddr =		in_getsockaddr,
1086	.pru_sosetlabel =	in_pcbsosetlabel,
1087	.pru_close =		rip_close,
1088};
1089