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