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