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