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