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