raw_ip.c revision 101934
1160814Ssimon/*
2160814Ssimon * Copyright (c) 1982, 1986, 1988, 1993
3160814Ssimon *	The Regents of the University of California.  All rights reserved.
4160814Ssimon *
5160814Ssimon * Redistribution and use in source and binary forms, with or without
6160814Ssimon * modification, are permitted provided that the following conditions
7160814Ssimon * are met:
8160814Ssimon * 1. Redistributions of source code must retain the above copyright
9160814Ssimon *    notice, this list of conditions and the following disclaimer.
10160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
11160814Ssimon *    notice, this list of conditions and the following disclaimer in the
12160814Ssimon *    documentation and/or other materials provided with the distribution.
13160814Ssimon * 3. All advertising materials mentioning features or use of this software
14160814Ssimon *    must display the following acknowledgement:
15160814Ssimon *	This product includes software developed by the University of
16160814Ssimon *	California, Berkeley and its contributors.
17167612Ssimon * 4. Neither the name of the University nor the names of its contributors
18160814Ssimon *    may be used to endorse or promote products derived from this software
19238405Sjkim *    without specific prior written permission.
20160814Ssimon *
21238405Sjkim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22160814Ssimon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24160814Ssimon * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25160814Ssimon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26238405Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27160814Ssimon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29160814Ssimon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30238405Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31160814Ssimon * SUCH DAMAGE.
32238405Sjkim *
33160814Ssimon *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
34160814Ssimon * $FreeBSD: head/sys/netinet/raw_ip.c 101934 2002-08-15 18:51:27Z rwatson $
35160814Ssimon */
36160814Ssimon
37238405Sjkim#include "opt_inet6.h"
38160814Ssimon#include "opt_ipsec.h"
39160814Ssimon#include "opt_mac.h"
40160814Ssimon#include "opt_random_ip_id.h"
41160814Ssimon
42160814Ssimon#include <sys/param.h>
43160814Ssimon#include <sys/kernel.h>
44160814Ssimon#include <sys/lock.h>
45160814Ssimon#include <sys/mac.h>
46160814Ssimon#include <sys/malloc.h>
47160814Ssimon#include <sys/mbuf.h>
48160814Ssimon#include <sys/proc.h>
49160814Ssimon#include <sys/protosw.h>
50160814Ssimon#include <sys/signalvar.h>
51160814Ssimon#include <sys/socket.h>
52160814Ssimon#include <sys/socketvar.h>
53160814Ssimon#include <sys/sx.h>
54160814Ssimon#include <sys/sysctl.h>
55160814Ssimon#include <sys/systm.h>
56160814Ssimon
57160814Ssimon#include <vm/uma.h>
58160814Ssimon
59160814Ssimon#include <net/if.h>
60160814Ssimon#include <net/route.h>
61160814Ssimon
62160814Ssimon#define _IP_VHL
63160814Ssimon#include <netinet/in.h>
64160814Ssimon#include <netinet/in_systm.h>
65160814Ssimon#include <netinet/in_pcb.h>
66160814Ssimon#include <netinet/in_var.h>
67160814Ssimon#include <netinet/ip.h>
68160814Ssimon#include <netinet/ip_var.h>
69160814Ssimon#include <netinet/ip_mroute.h>
70160814Ssimon
71160814Ssimon#include <netinet/ip_fw.h>
72160814Ssimon#include <netinet/ip_dummynet.h>
73160814Ssimon
74160814Ssimon#ifdef IPSEC
75160814Ssimon#include <netinet6/ipsec.h>
76160814Ssimon#endif /*IPSEC*/
77160814Ssimon
78160814Ssimonstruct	inpcbhead ripcb;
79160814Ssimonstruct	inpcbinfo ripcbinfo;
80160814Ssimon
81160814Ssimon/* control hooks for ipfw and dummynet */
82160814Ssimonip_fw_ctl_t *ip_fw_ctl_ptr;
83160814Ssimonip_dn_ctl_t *ip_dn_ctl_ptr;
84160814Ssimon
85160814Ssimon/*
86160814Ssimon * Nominal space allocated to a raw ip socket.
87238405Sjkim */
88160814Ssimon#define	RIPSNDQ		8192
89160814Ssimon#define	RIPRCVQ		8192
90238405Sjkim
91246772Sjkim/*
92160814Ssimon * Raw interface to IP protocol.
93160814Ssimon */
94238405Sjkim
95246772Sjkim/*
96160814Ssimon * Initialize raw connection block q.
97160814Ssimon */
98160814Ssimonvoid
99160814Ssimonrip_init()
100238405Sjkim{
101246772Sjkim	INP_INFO_LOCK_INIT(&ripcbinfo, "rip");
102160814Ssimon	LIST_INIT(&ripcb);
103160814Ssimon	ripcbinfo.listhead = &ripcb;
104160814Ssimon	/*
105160814Ssimon	 * XXX We don't use the hash list for raw IP, but it's easier
106160814Ssimon	 * to allocate a one entry hash list than it is to check all
107160814Ssimon	 * over the place for hashbase == NULL.
108160814Ssimon	 */
109160814Ssimon	ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
110194206Ssimon	ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
111194206Ssimon	ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
112194206Ssimon	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
113160814Ssimon	uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
114160814Ssimon}
115160814Ssimon
116160814Ssimonstatic struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
117194206Ssimon/*
118160814Ssimon * Setup generic address and protocol structures
119160814Ssimon * for raw_input routine, then pass them along with
120160814Ssimon * mbuf chain.
121160814Ssimon */
122160814Ssimonvoid
123160814Ssimonrip_input(m, off)
124160814Ssimon	struct mbuf *m;
125160814Ssimon	int off;
126160814Ssimon{
127160814Ssimon	register struct ip *ip = mtod(m, struct ip *);
128160814Ssimon	register struct inpcb *inp;
129160814Ssimon	struct inpcb *last = 0;
130160814Ssimon	struct mbuf *opts = 0;
131160814Ssimon	int proto = ip->ip_p;
132160814Ssimon
133160814Ssimon	ripsrc.sin_addr = ip->ip_src;
134160814Ssimon	LIST_FOREACH(inp, &ripcb, inp_list) {
135160814Ssimon#ifdef INET6
136160814Ssimon		if ((inp->inp_vflag & INP_IPV4) == 0)
137160814Ssimon			continue;
138160814Ssimon#endif
139160814Ssimon		if (inp->inp_ip_p && inp->inp_ip_p != proto)
140160814Ssimon			continue;
141160814Ssimon		if (inp->inp_laddr.s_addr &&
142160814Ssimon                  inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
143160814Ssimon			continue;
144160814Ssimon		if (inp->inp_faddr.s_addr &&
145160814Ssimon                  inp->inp_faddr.s_addr != ip->ip_src.s_addr)
146160814Ssimon			continue;
147160814Ssimon		if (last) {
148160814Ssimon			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
149160814Ssimon			int policyfail = 0;
150160814Ssimon
151194206Ssimon			if (n != NULL) {
152160814Ssimon#ifdef IPSSEC
153160814Ssimon				/* check AH/ESP integrity. */
154194206Ssimon				if (ipsec4_in_reject_so(n, last->inp_socket)) {
155160814Ssimon					policyfail = 1;
156160814Ssimon					ipsecstat.in_polvio++;
157160814Ssimon					/* do not inject data to pcb */
158160814Ssimon				}
159160814Ssimon#endif /*IPSEC*/
160160814Ssimon#ifdef MAC
161160814Ssimon				if (policyfail == 0 &&
162160814Ssimon				    mac_check_socket_deliver(last->inp_socket,
163194206Ssimon				    n) != 0)
164160814Ssimon					policyfail = 1;
165194206Ssimon#endif
166194206Ssimon			}
167194206Ssimon			if (policyfail)
168194206Ssimon				m_freem(n);
169194206Ssimon			else if (n) {
170160814Ssimon				if (last->inp_flags & INP_CONTROLOPTS ||
171160814Ssimon				    last->inp_socket->so_options & SO_TIMESTAMP)
172160814Ssimon				    ip_savecontrol(last, &opts, ip, n);
173160814Ssimon				if (sbappendaddr(&last->inp_socket->so_rcv,
174160814Ssimon				    (struct sockaddr *)&ripsrc, n,
175160814Ssimon				    opts) == 0) {
176160814Ssimon					/* should notify about lost packet */
177160814Ssimon					m_freem(n);
178160814Ssimon					if (opts)
179160814Ssimon					    m_freem(opts);
180160814Ssimon				} else
181160814Ssimon					sorwakeup(last->inp_socket);
182160814Ssimon				opts = 0;
183160814Ssimon			}
184160814Ssimon		}
185160814Ssimon		last = inp;
186160814Ssimon	}
187160814Ssimon	if (last) {
188160814Ssimon#ifdef IPSEC
189160814Ssimon		/* check AH/ESP integrity. */
190160814Ssimon		if (ipsec4_in_reject_so(m, last->inp_socket)) {
191160814Ssimon			m_freem(m);
192160814Ssimon			ipsecstat.in_polvio++;
193160814Ssimon			ipstat.ips_delivered--;
194160814Ssimon			/* do not inject data to pcb */
195160814Ssimon			return;
196160814Ssimon		}
197160814Ssimon#endif /*IPSEC*/
198160814Ssimon#ifdef MAC
199160814Ssimon		if (mac_check_socket_deliver(last->inp_socket, m) != 0) {
200160814Ssimon			m_freem(m);
201160814Ssimon			ipstat.ips_delivered--;
202160814Ssimon			return;
203194206Ssimon		}
204160814Ssimon#endif
205160814Ssimon		if (last->inp_flags & INP_CONTROLOPTS ||
206160814Ssimon		    last->inp_socket->so_options & SO_TIMESTAMP)
207160814Ssimon			ip_savecontrol(last, &opts, ip, m);
208160814Ssimon		if (sbappendaddr(&last->inp_socket->so_rcv,
209194206Ssimon		    (struct sockaddr *)&ripsrc, m, opts) == 0) {
210160814Ssimon			m_freem(m);
211160814Ssimon			if (opts)
212160814Ssimon			    m_freem(opts);
213160814Ssimon		} else
214160814Ssimon			sorwakeup(last->inp_socket);
215160814Ssimon	} else {
216194206Ssimon		m_freem(m);
217160814Ssimon		ipstat.ips_noproto++;
218160814Ssimon		ipstat.ips_delivered--;
219160814Ssimon	}
220160814Ssimon}
221160814Ssimon
222160814Ssimon/*
223160814Ssimon * Generate IP header and pass packet to ip_output.
224160814Ssimon * Tack on options user may have setup with control call.
225160814Ssimon */
226160814Ssimonint
227160814Ssimonrip_output(m, so, dst)
228160814Ssimon	struct mbuf *m;
229160814Ssimon	struct socket *so;
230160814Ssimon	u_long dst;
231160814Ssimon{
232160814Ssimon	register struct ip *ip;
233160814Ssimon	register struct inpcb *inp = sotoinpcb(so);
234160814Ssimon	int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
235160814Ssimon
236160814Ssimon#ifdef MAC
237160814Ssimon	mac_create_mbuf_from_socket(so, m);
238160814Ssimon#endif
239160814Ssimon
240160814Ssimon	/*
241160814Ssimon	 * If the user handed us a complete IP packet, use it.
242160814Ssimon	 * Otherwise, allocate an mbuf for a header and fill it in.
243160814Ssimon	 */
244160814Ssimon	if ((inp->inp_flags & INP_HDRINCL) == 0) {
245160814Ssimon		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
246160814Ssimon			m_freem(m);
247160814Ssimon			return(EMSGSIZE);
248160814Ssimon		}
249160814Ssimon		M_PREPEND(m, sizeof(struct ip), M_TRYWAIT);
250160814Ssimon		ip = mtod(m, struct ip *);
251160814Ssimon		ip->ip_tos = inp->inp_ip_tos;
252160814Ssimon		ip->ip_off = 0;
253160814Ssimon		ip->ip_p = inp->inp_ip_p;
254160814Ssimon		ip->ip_len = m->m_pkthdr.len;
255160814Ssimon		ip->ip_src = inp->inp_laddr;
256160814Ssimon		ip->ip_dst.s_addr = dst;
257160814Ssimon		ip->ip_ttl = inp->inp_ip_ttl;
258160814Ssimon	} else {
259160814Ssimon		if (m->m_pkthdr.len > IP_MAXPACKET) {
260160814Ssimon			m_freem(m);
261160814Ssimon			return(EMSGSIZE);
262160814Ssimon		}
263160814Ssimon		ip = mtod(m, struct ip *);
264160814Ssimon		/* don't allow both user specified and setsockopt options,
265160814Ssimon		   and don't allow packet length sizes that will crash */
266160814Ssimon		if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
267160814Ssimon		     && inp->inp_options)
268160814Ssimon		    || (ip->ip_len > m->m_pkthdr.len)
269160814Ssimon		    || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
270160814Ssimon			m_freem(m);
271160814Ssimon			return EINVAL;
272160814Ssimon		}
273160814Ssimon		if (ip->ip_id == 0)
274160814Ssimon#ifdef RANDOM_IP_ID
275160814Ssimon			ip->ip_id = ip_randomid();
276160814Ssimon#else
277160814Ssimon			ip->ip_id = htons(ip_id++);
278160814Ssimon#endif
279160814Ssimon		/* XXX prevent ip_output from overwriting header fields */
280160814Ssimon		flags |= IP_RAWOUTPUT;
281160814Ssimon		ipstat.ips_rawout++;
282160814Ssimon	}
283
284#ifdef IPSEC
285	if (ipsec_setsocket(m, so) != 0) {
286		m_freem(m);
287		return ENOBUFS;
288	}
289#endif /*IPSEC*/
290
291	return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
292			  inp->inp_moptions));
293}
294
295/*
296 * Raw IP socket option processing.
297 */
298int
299rip_ctloutput(so, sopt)
300	struct socket *so;
301	struct sockopt *sopt;
302{
303	struct	inpcb *inp = sotoinpcb(so);
304	int	error, optval;
305
306	if (sopt->sopt_level != IPPROTO_IP)
307		return (EINVAL);
308
309	error = 0;
310
311	switch (sopt->sopt_dir) {
312	case SOPT_GET:
313		switch (sopt->sopt_name) {
314		case IP_HDRINCL:
315			optval = inp->inp_flags & INP_HDRINCL;
316			error = sooptcopyout(sopt, &optval, sizeof optval);
317			break;
318
319		case IP_FW_ADD:	/* ADD actually returns the body... */
320		case IP_FW_GET:
321			if (IPFW_LOADED)
322				error = ip_fw_ctl_ptr(sopt);
323			else
324				error = ENOPROTOOPT;
325			break;
326
327		case IP_DUMMYNET_GET:
328			if (DUMMYNET_LOADED)
329				error = ip_dn_ctl_ptr(sopt);
330			else
331				error = ENOPROTOOPT;
332			break ;
333
334		case MRT_INIT:
335		case MRT_DONE:
336		case MRT_ADD_VIF:
337		case MRT_DEL_VIF:
338		case MRT_ADD_MFC:
339		case MRT_DEL_MFC:
340		case MRT_VERSION:
341		case MRT_ASSERT:
342			error = ip_mrouter_get(so, sopt);
343			break;
344
345		default:
346			error = ip_ctloutput(so, sopt);
347			break;
348		}
349		break;
350
351	case SOPT_SET:
352		switch (sopt->sopt_name) {
353		case IP_HDRINCL:
354			error = sooptcopyin(sopt, &optval, sizeof optval,
355					    sizeof optval);
356			if (error)
357				break;
358			if (optval)
359				inp->inp_flags |= INP_HDRINCL;
360			else
361				inp->inp_flags &= ~INP_HDRINCL;
362			break;
363
364		case IP_FW_ADD:
365		case IP_FW_DEL:
366		case IP_FW_FLUSH:
367		case IP_FW_ZERO:
368		case IP_FW_RESETLOG:
369			if (IPFW_LOADED)
370				error = ip_fw_ctl_ptr(sopt);
371			else
372				error = ENOPROTOOPT;
373			break;
374
375		case IP_DUMMYNET_CONFIGURE:
376		case IP_DUMMYNET_DEL:
377		case IP_DUMMYNET_FLUSH:
378			if (DUMMYNET_LOADED)
379				error = ip_dn_ctl_ptr(sopt);
380			else
381				error = ENOPROTOOPT ;
382			break ;
383
384		case IP_RSVP_ON:
385			error = ip_rsvp_init(so);
386			break;
387
388		case IP_RSVP_OFF:
389			error = ip_rsvp_done();
390			break;
391
392			/* XXX - should be combined */
393		case IP_RSVP_VIF_ON:
394			error = ip_rsvp_vif_init(so, sopt);
395			break;
396
397		case IP_RSVP_VIF_OFF:
398			error = ip_rsvp_vif_done(so, sopt);
399			break;
400
401		case MRT_INIT:
402		case MRT_DONE:
403		case MRT_ADD_VIF:
404		case MRT_DEL_VIF:
405		case MRT_ADD_MFC:
406		case MRT_DEL_MFC:
407		case MRT_VERSION:
408		case MRT_ASSERT:
409			error = ip_mrouter_set(so, sopt);
410			break;
411
412		default:
413			error = ip_ctloutput(so, sopt);
414			break;
415		}
416		break;
417	}
418
419	return (error);
420}
421
422/*
423 * This function exists solely to receive the PRC_IFDOWN messages which
424 * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
425 * and calls in_ifadown() to remove all routes corresponding to that address.
426 * It also receives the PRC_IFUP messages from if_up() and reinstalls the
427 * interface routes.
428 */
429void
430rip_ctlinput(cmd, sa, vip)
431	int cmd;
432	struct sockaddr *sa;
433	void *vip;
434{
435	struct in_ifaddr *ia;
436	struct ifnet *ifp;
437	int err;
438	int flags;
439
440	switch (cmd) {
441	case PRC_IFDOWN:
442		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
443			if (ia->ia_ifa.ifa_addr == sa
444			    && (ia->ia_flags & IFA_ROUTE)) {
445				/*
446				 * in_ifscrub kills the interface route.
447				 */
448				in_ifscrub(ia->ia_ifp, ia);
449				/*
450				 * in_ifadown gets rid of all the rest of
451				 * the routes.  This is not quite the right
452				 * thing to do, but at least if we are running
453				 * a routing process they will come back.
454				 */
455				in_ifadown(&ia->ia_ifa, 0);
456				break;
457			}
458		}
459		break;
460
461	case PRC_IFUP:
462		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
463			if (ia->ia_ifa.ifa_addr == sa)
464				break;
465		}
466		if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
467			return;
468		flags = RTF_UP;
469		ifp = ia->ia_ifa.ifa_ifp;
470
471		if ((ifp->if_flags & IFF_LOOPBACK)
472		    || (ifp->if_flags & IFF_POINTOPOINT))
473			flags |= RTF_HOST;
474
475		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
476		if (err == 0)
477			ia->ia_flags |= IFA_ROUTE;
478		break;
479	}
480}
481
482u_long	rip_sendspace = RIPSNDQ;
483u_long	rip_recvspace = RIPRCVQ;
484
485SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
486    &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
487SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
488    &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
489
490static int
491rip_attach(struct socket *so, int proto, struct thread *td)
492{
493	struct inpcb *inp;
494	int error, s;
495
496	inp = sotoinpcb(so);
497	if (inp)
498		panic("rip_attach");
499	if (td && (error = suser(td)) != 0)
500		return error;
501
502	error = soreserve(so, rip_sendspace, rip_recvspace);
503	if (error)
504		return error;
505	s = splnet();
506	error = in_pcballoc(so, &ripcbinfo, td);
507	splx(s);
508	if (error)
509		return error;
510	inp = (struct inpcb *)so->so_pcb;
511	inp->inp_vflag |= INP_IPV4;
512	inp->inp_ip_p = proto;
513	inp->inp_ip_ttl = ip_defttl;
514	return 0;
515}
516
517static int
518rip_detach(struct socket *so)
519{
520	struct inpcb *inp;
521
522	inp = sotoinpcb(so);
523	if (inp == 0)
524		panic("rip_detach");
525	if (so == ip_mrouter)
526		ip_mrouter_done();
527	ip_rsvp_force_done(so);
528	if (so == ip_rsvpd)
529		ip_rsvp_done();
530	in_pcbdetach(inp);
531	return 0;
532}
533
534static int
535rip_abort(struct socket *so)
536{
537	soisdisconnected(so);
538	return rip_detach(so);
539}
540
541static int
542rip_disconnect(struct socket *so)
543{
544	if ((so->so_state & SS_ISCONNECTED) == 0)
545		return ENOTCONN;
546	return rip_abort(so);
547}
548
549static int
550rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
551{
552	struct inpcb *inp = sotoinpcb(so);
553	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
554
555	if (nam->sa_len != sizeof(*addr))
556		return EINVAL;
557
558	if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
559				    (addr->sin_family != AF_IMPLINK)) ||
560	    (addr->sin_addr.s_addr &&
561	     ifa_ifwithaddr((struct sockaddr *)addr) == 0))
562		return EADDRNOTAVAIL;
563	inp->inp_laddr = addr->sin_addr;
564	return 0;
565}
566
567static int
568rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
569{
570	struct inpcb *inp = sotoinpcb(so);
571	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
572
573	if (nam->sa_len != sizeof(*addr))
574		return EINVAL;
575	if (TAILQ_EMPTY(&ifnet))
576		return EADDRNOTAVAIL;
577	if ((addr->sin_family != AF_INET) &&
578	    (addr->sin_family != AF_IMPLINK))
579		return EAFNOSUPPORT;
580	inp->inp_faddr = addr->sin_addr;
581	soisconnected(so);
582	return 0;
583}
584
585static int
586rip_shutdown(struct socket *so)
587{
588	socantsendmore(so);
589	return 0;
590}
591
592static int
593rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
594	 struct mbuf *control, struct thread *td)
595{
596	struct inpcb *inp = sotoinpcb(so);
597	register u_long dst;
598
599	if (so->so_state & SS_ISCONNECTED) {
600		if (nam) {
601			m_freem(m);
602			return EISCONN;
603		}
604		dst = inp->inp_faddr.s_addr;
605	} else {
606		if (nam == NULL) {
607			m_freem(m);
608			return ENOTCONN;
609		}
610		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
611	}
612	return rip_output(m, so, dst);
613}
614
615static int
616rip_pcblist(SYSCTL_HANDLER_ARGS)
617{
618	int error, i, n, s;
619	struct inpcb *inp, **inp_list;
620	inp_gen_t gencnt;
621	struct xinpgen xig;
622
623	/*
624	 * The process of preparing the TCB list is too time-consuming and
625	 * resource-intensive to repeat twice on every request.
626	 */
627	if (req->oldptr == 0) {
628		n = ripcbinfo.ipi_count;
629		req->oldidx = 2 * (sizeof xig)
630			+ (n + n/8) * sizeof(struct xinpcb);
631		return 0;
632	}
633
634	if (req->newptr != 0)
635		return EPERM;
636
637	/*
638	 * OK, now we're committed to doing something.
639	 */
640	s = splnet();
641	gencnt = ripcbinfo.ipi_gencnt;
642	n = ripcbinfo.ipi_count;
643	splx(s);
644
645	xig.xig_len = sizeof xig;
646	xig.xig_count = n;
647	xig.xig_gen = gencnt;
648	xig.xig_sogen = so_gencnt;
649	error = SYSCTL_OUT(req, &xig, sizeof xig);
650	if (error)
651		return error;
652
653	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
654	if (inp_list == 0)
655		return ENOMEM;
656
657	s = splnet();
658	for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n;
659	     inp = LIST_NEXT(inp, inp_list)) {
660		if (inp->inp_gencnt <= gencnt) {
661			if (cr_canseesocket(req->td->td_ucred,
662			    inp->inp_socket))
663				continue;
664			inp_list[i++] = inp;
665		}
666	}
667	splx(s);
668	n = i;
669
670	error = 0;
671	for (i = 0; i < n; i++) {
672		inp = inp_list[i];
673		if (inp->inp_gencnt <= gencnt) {
674			struct xinpcb xi;
675			xi.xi_len = sizeof xi;
676			/* XXX should avoid extra copy */
677			bcopy(inp, &xi.xi_inp, sizeof *inp);
678			if (inp->inp_socket)
679				sotoxsocket(inp->inp_socket, &xi.xi_socket);
680			error = SYSCTL_OUT(req, &xi, sizeof xi);
681		}
682	}
683	if (!error) {
684		/*
685		 * Give the user an updated idea of our state.
686		 * If the generation differs from what we told
687		 * her before, she knows that something happened
688		 * while we were processing this request, and it
689		 * might be necessary to retry.
690		 */
691		s = splnet();
692		xig.xig_gen = ripcbinfo.ipi_gencnt;
693		xig.xig_sogen = so_gencnt;
694		xig.xig_count = ripcbinfo.ipi_count;
695		splx(s);
696		error = SYSCTL_OUT(req, &xig, sizeof xig);
697	}
698	free(inp_list, M_TEMP);
699	return error;
700}
701
702/*
703 * This is the wrapper function for in_setsockaddr.  We just pass down
704 * the pcbinfo for in_setpeeraddr to lock.
705 */
706static int
707rip_sockaddr(struct socket *so, struct sockaddr **nam)
708{
709	return (in_setsockaddr(so, nam, &ripcbinfo));
710}
711
712/*
713 * This is the wrapper function for in_setpeeraddr.  We just pass down
714 * the pcbinfo for in_setpeeraddr to lock.
715 */
716static int
717rip_peeraddr(struct socket *so, struct sockaddr **nam)
718{
719	return (in_setpeeraddr(so, nam, &ripcbinfo));
720}
721
722
723SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
724	    rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
725
726struct pr_usrreqs rip_usrreqs = {
727	rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
728	pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
729	pru_listen_notsupp, rip_peeraddr, pru_rcvd_notsupp,
730	pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
731	rip_sockaddr, sosend, soreceive, sopoll
732};
733