Home | History | Annotate | Line # | Download | only in netinet
raw_ip.c revision 1.112.6.1
      1 /*	$NetBSD: raw_ip.c,v 1.112.6.1 2012/02/18 07:35:39 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1988, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.112.6.1 2012/02/18 07:35:39 mrg Exp $");
     65 
     66 #include "opt_inet.h"
     67 #include "opt_compat_netbsd.h"
     68 #include "opt_ipsec.h"
     69 #include "opt_mrouting.h"
     70 
     71 #include <sys/param.h>
     72 #include <sys/sysctl.h>
     73 #include <sys/malloc.h>
     74 #include <sys/mbuf.h>
     75 #include <sys/socket.h>
     76 #include <sys/protosw.h>
     77 #include <sys/socketvar.h>
     78 #include <sys/errno.h>
     79 #include <sys/systm.h>
     80 #include <sys/proc.h>
     81 #include <sys/kauth.h>
     82 
     83 #include <net/if.h>
     84 #include <net/route.h>
     85 
     86 #include <netinet/in.h>
     87 #include <netinet/in_systm.h>
     88 #include <netinet/ip.h>
     89 #include <netinet/ip_var.h>
     90 #include <netinet/ip_private.h>
     91 #include <netinet/ip_mroute.h>
     92 #include <netinet/ip_icmp.h>
     93 #include <netinet/in_pcb.h>
     94 #include <netinet/in_proto.h>
     95 #include <netinet/in_var.h>
     96 
     97 #ifdef KAME_IPSEC
     98 #include <netinet6/ipsec.h>
     99 #include <netinet6/ipsec_private.h>
    100 #endif /* KAME_IPSEC */
    101 
    102 #ifdef FAST_IPSEC
    103 #include <netipsec/ipsec.h>
    104 #include <netipsec/ipsec_var.h>
    105 #include <netipsec/ipsec_private.h>
    106 #endif	/* FAST_IPSEC */
    107 
    108 #ifdef COMPAT_50
    109 #include <compat/sys/socket.h>
    110 #endif
    111 
    112 struct inpcbtable rawcbtable;
    113 
    114 int	 rip_pcbnotify(struct inpcbtable *, struct in_addr,
    115     struct in_addr, int, int, void (*)(struct inpcb *, int));
    116 int	 rip_bind(struct inpcb *, struct mbuf *);
    117 int	 rip_connect(struct inpcb *, struct mbuf *);
    118 void	 rip_disconnect(struct inpcb *);
    119 
    120 static void sysctl_net_inet_raw_setup(struct sysctllog **);
    121 
    122 /*
    123  * Nominal space allocated to a raw ip socket.
    124  */
    125 #define	RIPSNDQ		8192
    126 #define	RIPRCVQ		8192
    127 
    128 /*
    129  * Raw interface to IP protocol.
    130  */
    131 
    132 /*
    133  * Initialize raw connection block q.
    134  */
    135 void
    136 rip_init(void)
    137 {
    138 
    139 	sysctl_net_inet_raw_setup(NULL);
    140 	in_pcbinit(&rawcbtable, 1, 1);
    141 }
    142 
    143 static void
    144 rip_sbappendaddr(struct inpcb *last, struct ip *ip, const struct sockaddr *sa,
    145     int hlen, struct mbuf *opts, struct mbuf *n)
    146 {
    147 	if (last->inp_flags & INP_NOHEADER)
    148 		m_adj(n, hlen);
    149 	if (last->inp_flags & INP_CONTROLOPTS
    150 #ifdef SO_OTIMESTAMP
    151 	    || last->inp_socket->so_options & SO_OTIMESTAMP
    152 #endif
    153 	    || last->inp_socket->so_options & SO_TIMESTAMP)
    154 		ip_savecontrol(last, &opts, ip, n);
    155 	if (sbappendaddr(&last->inp_socket->so_rcv, sa, n, opts) == 0) {
    156 		/* should notify about lost packet */
    157 		m_freem(n);
    158 		if (opts)
    159 			m_freem(opts);
    160 	} else
    161 		sorwakeup(last->inp_socket);
    162 }
    163 
    164 /*
    165  * Setup generic address and protocol structures
    166  * for raw_input routine, then pass them along with
    167  * mbuf chain.
    168  */
    169 void
    170 rip_input(struct mbuf *m, ...)
    171 {
    172 	int hlen, proto;
    173 	struct ip *ip = mtod(m, struct ip *);
    174 	struct inpcb_hdr *inph;
    175 	struct inpcb *inp;
    176 	struct inpcb *last = NULL;
    177 	struct mbuf *n, *opts = NULL;
    178 	struct sockaddr_in ripsrc;
    179 	va_list ap;
    180 
    181 	va_start(ap, m);
    182 	(void)va_arg(ap, int);		/* ignore value, advance ap */
    183 	proto = va_arg(ap, int);
    184 	va_end(ap);
    185 
    186 	sockaddr_in_init(&ripsrc, &ip->ip_src, 0);
    187 
    188 	/*
    189 	 * XXX Compatibility: programs using raw IP expect ip_len
    190 	 * XXX to have the header length subtracted, and in host order.
    191 	 * XXX ip_off is also expected to be host order.
    192 	 */
    193 	hlen = ip->ip_hl << 2;
    194 	ip->ip_len = ntohs(ip->ip_len) - hlen;
    195 	NTOHS(ip->ip_off);
    196 
    197 	CIRCLEQ_FOREACH(inph, &rawcbtable.inpt_queue, inph_queue) {
    198 		inp = (struct inpcb *)inph;
    199 		if (inp->inp_af != AF_INET)
    200 			continue;
    201 		if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
    202 			continue;
    203 		if (!in_nullhost(inp->inp_laddr) &&
    204 		    !in_hosteq(inp->inp_laddr, ip->ip_dst))
    205 			continue;
    206 		if (!in_nullhost(inp->inp_faddr) &&
    207 		    !in_hosteq(inp->inp_faddr, ip->ip_src))
    208 			continue;
    209 		if (last == NULL)
    210 			;
    211 #if defined(KAME_IPSEC) || defined(FAST_IPSEC)
    212 		/* check AH/ESP integrity. */
    213 		else if (ipsec4_in_reject_so(m, last->inp_socket)) {
    214 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
    215 			/* do not inject data to pcb */
    216 		}
    217 #endif /*IPSEC*/
    218 		else if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
    219 			rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts,
    220 			    n);
    221 			opts = NULL;
    222 		}
    223 		last = inp;
    224 	}
    225 #if defined(KAME_IPSEC) || defined(FAST_IPSEC)
    226 	/* check AH/ESP integrity. */
    227 	if (last != NULL && ipsec4_in_reject_so(m, last->inp_socket)) {
    228 		m_freem(m);
    229 		IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
    230 		IP_STATDEC(IP_STAT_DELIVERED);
    231 		/* do not inject data to pcb */
    232 	} else
    233 #endif /*IPSEC*/
    234 	if (last != NULL)
    235 		rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts, m);
    236 	else if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
    237 		uint64_t *ips;
    238 
    239 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
    240 		    0, 0);
    241 		ips = IP_STAT_GETREF();
    242 		ips[IP_STAT_NOPROTO]++;
    243 		ips[IP_STAT_DELIVERED]--;
    244 		IP_STAT_PUTREF();
    245 	} else
    246 		m_freem(m);
    247 	return;
    248 }
    249 
    250 int
    251 rip_pcbnotify(struct inpcbtable *table,
    252     struct in_addr faddr, struct in_addr laddr, int proto, int errno,
    253     void (*notify)(struct inpcb *, int))
    254 {
    255 	struct inpcb *inp, *ninp;
    256 	int nmatch;
    257 
    258 	nmatch = 0;
    259 	for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
    260 	    inp != (struct inpcb *)&table->inpt_queue;
    261 	    inp = ninp) {
    262 		ninp = (struct inpcb *)inp->inp_queue.cqe_next;
    263 		if (inp->inp_af != AF_INET)
    264 			continue;
    265 		if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
    266 			continue;
    267 		if (in_hosteq(inp->inp_faddr, faddr) &&
    268 		    in_hosteq(inp->inp_laddr, laddr)) {
    269 			(*notify)(inp, errno);
    270 			nmatch++;
    271 		}
    272 	}
    273 
    274 	return nmatch;
    275 }
    276 
    277 void *
    278 rip_ctlinput(int cmd, const struct sockaddr *sa, void *v)
    279 {
    280 	struct ip *ip = v;
    281 	void (*notify)(struct inpcb *, int) = in_rtchange;
    282 	int errno;
    283 
    284 	if (sa->sa_family != AF_INET ||
    285 	    sa->sa_len != sizeof(struct sockaddr_in))
    286 		return NULL;
    287 	if ((unsigned)cmd >= PRC_NCMDS)
    288 		return NULL;
    289 	errno = inetctlerrmap[cmd];
    290 	if (PRC_IS_REDIRECT(cmd))
    291 		notify = in_rtchange, ip = 0;
    292 	else if (cmd == PRC_HOSTDEAD)
    293 		ip = 0;
    294 	else if (errno == 0)
    295 		return NULL;
    296 	if (ip) {
    297 		rip_pcbnotify(&rawcbtable, satocsin(sa)->sin_addr,
    298 		    ip->ip_src, ip->ip_p, errno, notify);
    299 
    300 		/* XXX mapped address case */
    301 	} else
    302 		in_pcbnotifyall(&rawcbtable, satocsin(sa)->sin_addr, errno,
    303 		    notify);
    304 	return NULL;
    305 }
    306 
    307 /*
    308  * Generate IP header and pass packet to ip_output.
    309  * Tack on options user may have setup with control call.
    310  */
    311 int
    312 rip_output(struct mbuf *m, ...)
    313 {
    314 	struct inpcb *inp;
    315 	struct ip *ip;
    316 	struct mbuf *opts;
    317 	int flags;
    318 	va_list ap;
    319 
    320 	va_start(ap, m);
    321 	inp = va_arg(ap, struct inpcb *);
    322 	va_end(ap);
    323 
    324 	flags =
    325 	    (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
    326 	    | IP_RETURNMTU;
    327 
    328 	/*
    329 	 * If the user handed us a complete IP packet, use it.
    330 	 * Otherwise, allocate an mbuf for a header and fill it in.
    331 	 */
    332 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
    333 		if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
    334 			m_freem(m);
    335 			return (EMSGSIZE);
    336 		}
    337 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    338 		if (!m)
    339 			return (ENOBUFS);
    340 		ip = mtod(m, struct ip *);
    341 		ip->ip_tos = 0;
    342 		ip->ip_off = htons(0);
    343 		ip->ip_p = inp->inp_ip.ip_p;
    344 		ip->ip_len = htons(m->m_pkthdr.len);
    345 		ip->ip_src = inp->inp_laddr;
    346 		ip->ip_dst = inp->inp_faddr;
    347 		ip->ip_ttl = MAXTTL;
    348 		opts = inp->inp_options;
    349 	} else {
    350 		if (m->m_pkthdr.len > IP_MAXPACKET) {
    351 			m_freem(m);
    352 			return (EMSGSIZE);
    353 		}
    354 		ip = mtod(m, struct ip *);
    355 
    356 		/*
    357 		 * If the mbuf is read-only, we need to allocate
    358 		 * a new mbuf for the header, since we need to
    359 		 * modify the header.
    360 		 */
    361 		if (M_READONLY(m)) {
    362 			int hlen = ip->ip_hl << 2;
    363 
    364 			m = m_copyup(m, hlen, (max_linkhdr + 3) & ~3);
    365 			if (m == NULL)
    366 				return (ENOMEM);	/* XXX */
    367 			ip = mtod(m, struct ip *);
    368 		}
    369 
    370 		/* XXX userland passes ip_len and ip_off in host order */
    371 		if (m->m_pkthdr.len != ip->ip_len) {
    372 			m_freem(m);
    373 			return (EINVAL);
    374 		}
    375 		HTONS(ip->ip_len);
    376 		HTONS(ip->ip_off);
    377 		if (ip->ip_id != 0 || m->m_pkthdr.len < IP_MINFRAGSIZE)
    378 			flags |= IP_NOIPNEWID;
    379 		opts = NULL;
    380 		/* XXX prevent ip_output from overwriting header fields */
    381 		flags |= IP_RAWOUTPUT;
    382 		IP_STATINC(IP_STAT_RAWOUT);
    383 	}
    384 	return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions,
    385 	     inp->inp_socket, &inp->inp_errormtu));
    386 }
    387 
    388 /*
    389  * Raw IP socket option processing.
    390  */
    391 int
    392 rip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    393 {
    394 	struct inpcb *inp = sotoinpcb(so);
    395 	int error = 0;
    396 	int optval;
    397 
    398 	if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) {
    399 		if (op == PRCO_GETOPT) {
    400 			optval = (inp->inp_flags & INP_NOHEADER) ? 1 : 0;
    401 			error = sockopt_set(sopt, &optval, sizeof(optval));
    402 		} else if (op == PRCO_SETOPT) {
    403 			error = sockopt_getint(sopt, &optval);
    404 			if (error)
    405 				goto out;
    406 			if (optval) {
    407 				inp->inp_flags &= ~INP_HDRINCL;
    408 				inp->inp_flags |= INP_NOHEADER;
    409 			} else
    410 				inp->inp_flags &= ~INP_NOHEADER;
    411 		}
    412 		goto out;
    413 	} else if (sopt->sopt_level != IPPROTO_IP)
    414 		return ip_ctloutput(op, so, sopt);
    415 
    416 	switch (op) {
    417 
    418 	case PRCO_SETOPT:
    419 		switch (sopt->sopt_name) {
    420 		case IP_HDRINCL:
    421 			error = sockopt_getint(sopt, &optval);
    422 			if (error)
    423 				break;
    424 			if (optval)
    425 				inp->inp_flags |= INP_HDRINCL;
    426 			else
    427 				inp->inp_flags &= ~INP_HDRINCL;
    428 			break;
    429 
    430 #ifdef MROUTING
    431 		case MRT_INIT:
    432 		case MRT_DONE:
    433 		case MRT_ADD_VIF:
    434 		case MRT_DEL_VIF:
    435 		case MRT_ADD_MFC:
    436 		case MRT_DEL_MFC:
    437 		case MRT_ASSERT:
    438 		case MRT_API_CONFIG:
    439 		case MRT_ADD_BW_UPCALL:
    440 		case MRT_DEL_BW_UPCALL:
    441 			error = ip_mrouter_set(so, sopt);
    442 			break;
    443 #endif
    444 
    445 		default:
    446 			error = ip_ctloutput(op, so, sopt);
    447 			break;
    448 		}
    449 		break;
    450 
    451 	case PRCO_GETOPT:
    452 		switch (sopt->sopt_name) {
    453 		case IP_HDRINCL:
    454 			optval = inp->inp_flags & INP_HDRINCL;
    455 			error = sockopt_set(sopt, &optval, sizeof(optval));
    456 			break;
    457 
    458 #ifdef MROUTING
    459 		case MRT_VERSION:
    460 		case MRT_ASSERT:
    461 		case MRT_API_SUPPORT:
    462 		case MRT_API_CONFIG:
    463 			error = ip_mrouter_get(so, sopt);
    464 			break;
    465 #endif
    466 
    467 		default:
    468 			error = ip_ctloutput(op, so, sopt);
    469 			break;
    470 		}
    471 		break;
    472 	}
    473  out:
    474 	return error;
    475 }
    476 
    477 int
    478 rip_bind(struct inpcb *inp, struct mbuf *nam)
    479 {
    480 	struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
    481 
    482 	if (nam->m_len != sizeof(*addr))
    483 		return (EINVAL);
    484 	if (TAILQ_FIRST(&ifnet) == 0)
    485 		return (EADDRNOTAVAIL);
    486 	if (addr->sin_family != AF_INET &&
    487 	    addr->sin_family != AF_IMPLINK)
    488 		return (EAFNOSUPPORT);
    489 	if (!in_nullhost(addr->sin_addr) &&
    490 	    ifa_ifwithaddr(sintosa(addr)) == 0)
    491 		return (EADDRNOTAVAIL);
    492 	inp->inp_laddr = addr->sin_addr;
    493 	return (0);
    494 }
    495 
    496 int
    497 rip_connect(struct inpcb *inp, struct mbuf *nam)
    498 {
    499 	struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
    500 
    501 	if (nam->m_len != sizeof(*addr))
    502 		return (EINVAL);
    503 	if (TAILQ_FIRST(&ifnet) == 0)
    504 		return (EADDRNOTAVAIL);
    505 	if (addr->sin_family != AF_INET &&
    506 	    addr->sin_family != AF_IMPLINK)
    507 		return (EAFNOSUPPORT);
    508 	inp->inp_faddr = addr->sin_addr;
    509 	return (0);
    510 }
    511 
    512 void
    513 rip_disconnect(struct inpcb *inp)
    514 {
    515 
    516 	inp->inp_faddr = zeroin_addr;
    517 }
    518 
    519 u_long	rip_sendspace = RIPSNDQ;
    520 u_long	rip_recvspace = RIPRCVQ;
    521 
    522 /*ARGSUSED*/
    523 int
    524 rip_usrreq(struct socket *so, int req,
    525     struct mbuf *m, struct mbuf *nam, struct mbuf *control, struct lwp *l)
    526 {
    527 	struct inpcb *inp;
    528 	int s;
    529 	int error = 0;
    530 #ifdef MROUTING
    531 	extern struct socket *ip_mrouter;
    532 #endif
    533 
    534 	if (req == PRU_CONTROL)
    535 		return in_control(so, (long)m, nam, (struct ifnet *)control, l);
    536 
    537 	s = splsoftnet();
    538 
    539 	if (req == PRU_PURGEIF) {
    540 		mutex_enter(softnet_lock);
    541 		in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
    542 		in_purgeif((struct ifnet *)control);
    543 		in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
    544 		mutex_exit(softnet_lock);
    545 		splx(s);
    546 		return (0);
    547 	}
    548 
    549 	inp = sotoinpcb(so);
    550 #ifdef DIAGNOSTIC
    551 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    552 		panic("rip_usrreq: unexpected control mbuf");
    553 #endif
    554 	if (inp == NULL && req != PRU_ATTACH) {
    555 		error = EINVAL;
    556 		goto release;
    557 	}
    558 
    559 	switch (req) {
    560 
    561 	case PRU_ATTACH:
    562 		sosetlock(so);
    563 		if (inp != 0) {
    564 			error = EISCONN;
    565 			break;
    566 		}
    567 
    568 		if (l == NULL) {
    569 			error = EACCES;
    570 			break;
    571 		}
    572 
    573 		/* XXX: raw socket permissions are checked in socreate() */
    574 
    575 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    576 			error = soreserve(so, rip_sendspace, rip_recvspace);
    577 			if (error)
    578 				break;
    579 		}
    580 		error = in_pcballoc(so, &rawcbtable);
    581 		if (error)
    582 			break;
    583 		inp = sotoinpcb(so);
    584 		inp->inp_ip.ip_p = (long)nam;
    585 		break;
    586 
    587 	case PRU_DETACH:
    588 #ifdef MROUTING
    589 		if (so == ip_mrouter)
    590 			ip_mrouter_done();
    591 #endif
    592 		in_pcbdetach(inp);
    593 		break;
    594 
    595 	case PRU_BIND:
    596 		error = rip_bind(inp, nam);
    597 		break;
    598 
    599 	case PRU_LISTEN:
    600 		error = EOPNOTSUPP;
    601 		break;
    602 
    603 	case PRU_CONNECT:
    604 		error = rip_connect(inp, nam);
    605 		if (error)
    606 			break;
    607 		soisconnected(so);
    608 		break;
    609 
    610 	case PRU_CONNECT2:
    611 		error = EOPNOTSUPP;
    612 		break;
    613 
    614 	case PRU_DISCONNECT:
    615 		soisdisconnected(so);
    616 		rip_disconnect(inp);
    617 		break;
    618 
    619 	/*
    620 	 * Mark the connection as being incapable of further input.
    621 	 */
    622 	case PRU_SHUTDOWN:
    623 		socantsendmore(so);
    624 		break;
    625 
    626 	case PRU_RCVD:
    627 		error = EOPNOTSUPP;
    628 		break;
    629 
    630 	/*
    631 	 * Ship a packet out.  The appropriate raw output
    632 	 * routine handles any massaging necessary.
    633 	 */
    634 	case PRU_SEND:
    635 		if (control && control->m_len) {
    636 			m_freem(control);
    637 			m_freem(m);
    638 			error = EINVAL;
    639 			break;
    640 		}
    641 	{
    642 		if (nam) {
    643 			if ((so->so_state & SS_ISCONNECTED) != 0) {
    644 				error = EISCONN;
    645 				goto die;
    646 			}
    647 			error = rip_connect(inp, nam);
    648 			if (error) {
    649 			die:
    650 				m_freem(m);
    651 				break;
    652 			}
    653 		} else {
    654 			if ((so->so_state & SS_ISCONNECTED) == 0) {
    655 				error = ENOTCONN;
    656 				goto die;
    657 			}
    658 		}
    659 		error = rip_output(m, inp);
    660 		if (nam)
    661 			rip_disconnect(inp);
    662 	}
    663 		break;
    664 
    665 	case PRU_SENSE:
    666 		/*
    667 		 * stat: don't bother with a blocksize.
    668 		 */
    669 		splx(s);
    670 		return (0);
    671 
    672 	case PRU_RCVOOB:
    673 		error = EOPNOTSUPP;
    674 		break;
    675 
    676 	case PRU_SENDOOB:
    677 		m_freem(control);
    678 		m_freem(m);
    679 		error = EOPNOTSUPP;
    680 		break;
    681 
    682 	case PRU_SOCKADDR:
    683 		in_setsockaddr(inp, nam);
    684 		break;
    685 
    686 	case PRU_PEERADDR:
    687 		in_setpeeraddr(inp, nam);
    688 		break;
    689 
    690 	default:
    691 		panic("rip_usrreq");
    692 	}
    693 
    694 release:
    695 	splx(s);
    696 	return (error);
    697 }
    698 
    699 static void
    700 sysctl_net_inet_raw_setup(struct sysctllog **clog)
    701 {
    702 
    703 	sysctl_createv(clog, 0, NULL, NULL,
    704 		       CTLFLAG_PERMANENT,
    705 		       CTLTYPE_NODE, "net", NULL,
    706 		       NULL, 0, NULL, 0,
    707 		       CTL_NET, CTL_EOL);
    708 	sysctl_createv(clog, 0, NULL, NULL,
    709 		       CTLFLAG_PERMANENT,
    710 		       CTLTYPE_NODE, "inet", NULL,
    711 		       NULL, 0, NULL, 0,
    712 		       CTL_NET, PF_INET, CTL_EOL);
    713 	sysctl_createv(clog, 0, NULL, NULL,
    714 		       CTLFLAG_PERMANENT,
    715 		       CTLTYPE_NODE, "raw",
    716 		       SYSCTL_DESCR("Raw IPv4 settings"),
    717 		       NULL, 0, NULL, 0,
    718 		       CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
    719 
    720 	sysctl_createv(clog, 0, NULL, NULL,
    721 		       CTLFLAG_PERMANENT,
    722 		       CTLTYPE_STRUCT, "pcblist",
    723 		       SYSCTL_DESCR("Raw IPv4 control block list"),
    724 		       sysctl_inpcblist, 0, &rawcbtable, 0,
    725 		       CTL_NET, PF_INET, IPPROTO_RAW,
    726 		       CTL_CREATE, CTL_EOL);
    727 }
    728