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