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