Home | History | Annotate | Line # | Download | only in netinet
raw_ip.c revision 1.123
      1 /*	$NetBSD: raw_ip.c,v 1.123 2014/05/22 23:42:53 rmind 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 /*
     64  * Raw interface to IP protocol.
     65  */
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.123 2014/05/22 23:42:53 rmind Exp $");
     69 
     70 #include "opt_inet.h"
     71 #include "opt_compat_netbsd.h"
     72 #include "opt_ipsec.h"
     73 #include "opt_mrouting.h"
     74 
     75 #include <sys/param.h>
     76 #include <sys/sysctl.h>
     77 #include <sys/malloc.h>
     78 #include <sys/mbuf.h>
     79 #include <sys/socket.h>
     80 #include <sys/protosw.h>
     81 #include <sys/socketvar.h>
     82 #include <sys/errno.h>
     83 #include <sys/systm.h>
     84 #include <sys/proc.h>
     85 #include <sys/kauth.h>
     86 
     87 #include <net/if.h>
     88 #include <net/route.h>
     89 
     90 #include <netinet/in.h>
     91 #include <netinet/in_systm.h>
     92 #include <netinet/ip.h>
     93 #include <netinet/ip_var.h>
     94 #include <netinet/ip_private.h>
     95 #include <netinet/ip_mroute.h>
     96 #include <netinet/ip_icmp.h>
     97 #include <netinet/in_pcb.h>
     98 #include <netinet/in_proto.h>
     99 #include <netinet/in_var.h>
    100 
    101 #ifdef IPSEC
    102 #include <netipsec/ipsec.h>
    103 #include <netipsec/ipsec_var.h>
    104 #include <netipsec/ipsec_private.h>
    105 #endif	/* IPSEC */
    106 
    107 #ifdef COMPAT_50
    108 #include <compat/sys/socket.h>
    109 #endif
    110 
    111 struct inpcbtable rawcbtable;
    112 
    113 int	 rip_pcbnotify(struct inpcbtable *, struct in_addr,
    114     struct in_addr, int, int, void (*)(struct inpcb *, int));
    115 int	 rip_bind(struct inpcb *, struct mbuf *);
    116 int	 rip_connect(struct inpcb *, struct mbuf *);
    117 void	 rip_disconnect(struct inpcb *);
    118 
    119 static void sysctl_net_inet_raw_setup(struct sysctllog **);
    120 
    121 /*
    122  * Nominal space allocated to a raw ip socket.
    123  */
    124 #define	RIPSNDQ		8192
    125 #define	RIPRCVQ		8192
    126 
    127 static u_long		rip_sendspace = RIPSNDQ;
    128 static u_long		rip_recvspace = RIPRCVQ;
    129 
    130 /*
    131  * Raw interface to IP protocol.
    132  */
    133 
    134 /*
    135  * Initialize raw connection block q.
    136  */
    137 void
    138 rip_init(void)
    139 {
    140 
    141 	sysctl_net_inet_raw_setup(NULL);
    142 	in_pcbinit(&rawcbtable, 1, 1);
    143 }
    144 
    145 static void
    146 rip_sbappendaddr(struct inpcb *last, struct ip *ip, const struct sockaddr *sa,
    147     int hlen, struct mbuf *opts, struct mbuf *n)
    148 {
    149 	if (last->inp_flags & INP_NOHEADER)
    150 		m_adj(n, hlen);
    151 	if (last->inp_flags & INP_CONTROLOPTS
    152 #ifdef SO_OTIMESTAMP
    153 	    || last->inp_socket->so_options & SO_OTIMESTAMP
    154 #endif
    155 	    || last->inp_socket->so_options & SO_TIMESTAMP)
    156 		ip_savecontrol(last, &opts, ip, n);
    157 	if (sbappendaddr(&last->inp_socket->so_rcv, sa, n, opts) == 0) {
    158 		/* should notify about lost packet */
    159 		m_freem(n);
    160 		if (opts)
    161 			m_freem(opts);
    162 	} else
    163 		sorwakeup(last->inp_socket);
    164 }
    165 
    166 /*
    167  * Setup generic address and protocol structures
    168  * for raw_input routine, then pass them along with
    169  * mbuf chain.
    170  */
    171 void
    172 rip_input(struct mbuf *m, ...)
    173 {
    174 	int hlen, proto;
    175 	struct ip *ip = mtod(m, struct ip *);
    176 	struct inpcb_hdr *inph;
    177 	struct inpcb *inp;
    178 	struct inpcb *last = NULL;
    179 	struct mbuf *n, *opts = NULL;
    180 	struct sockaddr_in ripsrc;
    181 	va_list ap;
    182 
    183 	va_start(ap, m);
    184 	(void)va_arg(ap, int);		/* ignore value, advance ap */
    185 	proto = va_arg(ap, int);
    186 	va_end(ap);
    187 
    188 	sockaddr_in_init(&ripsrc, &ip->ip_src, 0);
    189 
    190 	/*
    191 	 * XXX Compatibility: programs using raw IP expect ip_len
    192 	 * XXX to have the header length subtracted, and in host order.
    193 	 * XXX ip_off is also expected to be host order.
    194 	 */
    195 	hlen = ip->ip_hl << 2;
    196 	ip->ip_len = ntohs(ip->ip_len) - hlen;
    197 	NTOHS(ip->ip_off);
    198 
    199 	TAILQ_FOREACH(inph, &rawcbtable.inpt_queue, inph_queue) {
    200 		inp = (struct inpcb *)inph;
    201 		if (inp->inp_af != AF_INET)
    202 			continue;
    203 		if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
    204 			continue;
    205 		if (!in_nullhost(inp->inp_laddr) &&
    206 		    !in_hosteq(inp->inp_laddr, ip->ip_dst))
    207 			continue;
    208 		if (!in_nullhost(inp->inp_faddr) &&
    209 		    !in_hosteq(inp->inp_faddr, ip->ip_src))
    210 			continue;
    211 		if (last == NULL)
    212 			;
    213 #if defined(IPSEC)
    214 		/* check AH/ESP integrity. */
    215 		else if (ipsec4_in_reject_so(m, last->inp_socket)) {
    216 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
    217 			/* do not inject data to pcb */
    218 		}
    219 #endif /*IPSEC*/
    220 		else if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
    221 			rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts,
    222 			    n);
    223 			opts = NULL;
    224 		}
    225 		last = inp;
    226 	}
    227 #if defined(IPSEC)
    228 	/* check AH/ESP integrity. */
    229 	if (last != NULL && ipsec4_in_reject_so(m, last->inp_socket)) {
    230 		m_freem(m);
    231 		IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
    232 		IP_STATDEC(IP_STAT_DELIVERED);
    233 		/* do not inject data to pcb */
    234 	} else
    235 #endif /*IPSEC*/
    236 	if (last != NULL)
    237 		rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts, m);
    238 	else if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
    239 		uint64_t *ips;
    240 
    241 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
    242 		    0, 0);
    243 		ips = IP_STAT_GETREF();
    244 		ips[IP_STAT_NOPROTO]++;
    245 		ips[IP_STAT_DELIVERED]--;
    246 		IP_STAT_PUTREF();
    247 	} else
    248 		m_freem(m);
    249 	return;
    250 }
    251 
    252 int
    253 rip_pcbnotify(struct inpcbtable *table,
    254     struct in_addr faddr, struct in_addr laddr, int proto, int errno,
    255     void (*notify)(struct inpcb *, int))
    256 {
    257 	struct inpcb_hdr *inph, *ninph;
    258 	int nmatch;
    259 
    260 	nmatch = 0;
    261 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
    262 		struct inpcb *inp = (struct inpcb *)inph;
    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 
    385 	/*
    386 	 * IP output.  Note: if IP_RETURNMTU flag is set, the MTU size
    387 	 * will be stored in inp_errormtu.
    388 	 */
    389 	return ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions,
    390 	     inp->inp_socket);
    391 }
    392 
    393 /*
    394  * Raw IP socket option processing.
    395  */
    396 int
    397 rip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    398 {
    399 	struct inpcb *inp = sotoinpcb(so);
    400 	int error = 0;
    401 	int optval;
    402 
    403 	if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) {
    404 		if (op == PRCO_GETOPT) {
    405 			optval = (inp->inp_flags & INP_NOHEADER) ? 1 : 0;
    406 			error = sockopt_set(sopt, &optval, sizeof(optval));
    407 		} else if (op == PRCO_SETOPT) {
    408 			error = sockopt_getint(sopt, &optval);
    409 			if (error)
    410 				goto out;
    411 			if (optval) {
    412 				inp->inp_flags &= ~INP_HDRINCL;
    413 				inp->inp_flags |= INP_NOHEADER;
    414 			} else
    415 				inp->inp_flags &= ~INP_NOHEADER;
    416 		}
    417 		goto out;
    418 	} else if (sopt->sopt_level != IPPROTO_IP)
    419 		return ip_ctloutput(op, so, sopt);
    420 
    421 	switch (op) {
    422 
    423 	case PRCO_SETOPT:
    424 		switch (sopt->sopt_name) {
    425 		case IP_HDRINCL:
    426 			error = sockopt_getint(sopt, &optval);
    427 			if (error)
    428 				break;
    429 			if (optval)
    430 				inp->inp_flags |= INP_HDRINCL;
    431 			else
    432 				inp->inp_flags &= ~INP_HDRINCL;
    433 			break;
    434 
    435 #ifdef MROUTING
    436 		case MRT_INIT:
    437 		case MRT_DONE:
    438 		case MRT_ADD_VIF:
    439 		case MRT_DEL_VIF:
    440 		case MRT_ADD_MFC:
    441 		case MRT_DEL_MFC:
    442 		case MRT_ASSERT:
    443 		case MRT_API_CONFIG:
    444 		case MRT_ADD_BW_UPCALL:
    445 		case MRT_DEL_BW_UPCALL:
    446 			error = ip_mrouter_set(so, sopt);
    447 			break;
    448 #endif
    449 
    450 		default:
    451 			error = ip_ctloutput(op, so, sopt);
    452 			break;
    453 		}
    454 		break;
    455 
    456 	case PRCO_GETOPT:
    457 		switch (sopt->sopt_name) {
    458 		case IP_HDRINCL:
    459 			optval = inp->inp_flags & INP_HDRINCL;
    460 			error = sockopt_set(sopt, &optval, sizeof(optval));
    461 			break;
    462 
    463 #ifdef MROUTING
    464 		case MRT_VERSION:
    465 		case MRT_ASSERT:
    466 		case MRT_API_SUPPORT:
    467 		case MRT_API_CONFIG:
    468 			error = ip_mrouter_get(so, sopt);
    469 			break;
    470 #endif
    471 
    472 		default:
    473 			error = ip_ctloutput(op, so, sopt);
    474 			break;
    475 		}
    476 		break;
    477 	}
    478  out:
    479 	return error;
    480 }
    481 
    482 int
    483 rip_bind(struct inpcb *inp, struct mbuf *nam)
    484 {
    485 	struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
    486 
    487 	if (nam->m_len != sizeof(*addr))
    488 		return (EINVAL);
    489 	if (!IFNET_FIRST())
    490 		return (EADDRNOTAVAIL);
    491 	if (addr->sin_family != AF_INET)
    492 		return (EAFNOSUPPORT);
    493 	if (!in_nullhost(addr->sin_addr) &&
    494 	    ifa_ifwithaddr(sintosa(addr)) == 0)
    495 		return (EADDRNOTAVAIL);
    496 	inp->inp_laddr = addr->sin_addr;
    497 	return (0);
    498 }
    499 
    500 int
    501 rip_connect(struct inpcb *inp, struct mbuf *nam)
    502 {
    503 	struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
    504 
    505 	if (nam->m_len != sizeof(*addr))
    506 		return (EINVAL);
    507 	if (!IFNET_FIRST())
    508 		return (EADDRNOTAVAIL);
    509 	if (addr->sin_family != AF_INET)
    510 		return (EAFNOSUPPORT);
    511 	inp->inp_faddr = addr->sin_addr;
    512 	return (0);
    513 }
    514 
    515 void
    516 rip_disconnect(struct inpcb *inp)
    517 {
    518 
    519 	inp->inp_faddr = zeroin_addr;
    520 }
    521 
    522 static int
    523 rip_attach(struct socket *so, int proto)
    524 {
    525 	struct inpcb *inp;
    526 	int error;
    527 
    528 	KASSERT(sotoinpcb(so) == NULL);
    529 	sosetlock(so);
    530 
    531 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    532 		error = soreserve(so, rip_sendspace, rip_recvspace);
    533 		if (error) {
    534 			return error;
    535 		}
    536 	}
    537 
    538 	error = in_pcballoc(so, &rawcbtable);
    539 	if (error) {
    540 		return error;
    541 	}
    542 	inp = sotoinpcb(so);
    543 	inp->inp_ip.ip_p = proto;
    544 	KASSERT(solocked(so));
    545 
    546 	return 0;
    547 }
    548 
    549 static void
    550 rip_detach(struct socket *so)
    551 {
    552 	struct inpcb *inp;
    553 
    554 	KASSERT(solocked(so));
    555 	inp = sotoinpcb(so);
    556 	KASSERT(inp != NULL);
    557 
    558 #ifdef MROUTING
    559 	extern struct socket *ip_mrouter;
    560 	if (so == ip_mrouter) {
    561 		ip_mrouter_done();
    562 	}
    563 #endif
    564 	in_pcbdetach(inp);
    565 }
    566 
    567 int
    568 rip_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    569     struct mbuf *control, struct lwp *l)
    570 {
    571 	struct inpcb *inp;
    572 	int s, error = 0;
    573 
    574 	KASSERT(req != PRU_ATTACH);
    575 	KASSERT(req != PRU_DETACH);
    576 
    577 	if (req == PRU_CONTROL) {
    578 		return in_control(so, (long)m, nam, (ifnet_t *)control, l);
    579 	}
    580 	s = splsoftnet();
    581 	if (req == PRU_PURGEIF) {
    582 		mutex_enter(softnet_lock);
    583 		in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
    584 		in_purgeif((struct ifnet *)control);
    585 		in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
    586 		mutex_exit(softnet_lock);
    587 		splx(s);
    588 		return 0;
    589 	}
    590 
    591 	KASSERT(solocked(so));
    592 	inp = sotoinpcb(so);
    593 
    594 	KASSERT(!control || (req == PRU_SEND || req == PRU_SENDOOB));
    595 	if (inp == NULL) {
    596 		splx(s);
    597 		return EINVAL;
    598 	}
    599 
    600 	switch (req) {
    601 
    602 	case PRU_BIND:
    603 		error = rip_bind(inp, nam);
    604 		break;
    605 
    606 	case PRU_LISTEN:
    607 		error = EOPNOTSUPP;
    608 		break;
    609 
    610 	case PRU_CONNECT:
    611 		error = rip_connect(inp, nam);
    612 		if (error)
    613 			break;
    614 		soisconnected(so);
    615 		break;
    616 
    617 	case PRU_CONNECT2:
    618 		error = EOPNOTSUPP;
    619 		break;
    620 
    621 	case PRU_DISCONNECT:
    622 		soisdisconnected(so);
    623 		rip_disconnect(inp);
    624 		break;
    625 
    626 	/*
    627 	 * Mark the connection as being incapable of further input.
    628 	 */
    629 	case PRU_SHUTDOWN:
    630 		socantsendmore(so);
    631 		break;
    632 
    633 	case PRU_RCVD:
    634 		error = EOPNOTSUPP;
    635 		break;
    636 
    637 	/*
    638 	 * Ship a packet out.  The appropriate raw output
    639 	 * routine handles any massaging necessary.
    640 	 */
    641 	case PRU_SEND:
    642 		if (control && control->m_len) {
    643 			m_freem(control);
    644 			m_freem(m);
    645 			error = EINVAL;
    646 			break;
    647 		}
    648 	{
    649 		if (nam) {
    650 			if ((so->so_state & SS_ISCONNECTED) != 0) {
    651 				error = EISCONN;
    652 				goto die;
    653 			}
    654 			error = rip_connect(inp, nam);
    655 			if (error) {
    656 			die:
    657 				m_freem(m);
    658 				break;
    659 			}
    660 		} else {
    661 			if ((so->so_state & SS_ISCONNECTED) == 0) {
    662 				error = ENOTCONN;
    663 				goto die;
    664 			}
    665 		}
    666 		error = rip_output(m, inp);
    667 		if (nam)
    668 			rip_disconnect(inp);
    669 	}
    670 		break;
    671 
    672 	case PRU_SENSE:
    673 		/*
    674 		 * stat: don't bother with a blocksize.
    675 		 */
    676 		splx(s);
    677 		return (0);
    678 
    679 	case PRU_RCVOOB:
    680 		error = EOPNOTSUPP;
    681 		break;
    682 
    683 	case PRU_SENDOOB:
    684 		m_freem(control);
    685 		m_freem(m);
    686 		error = EOPNOTSUPP;
    687 		break;
    688 
    689 	case PRU_SOCKADDR:
    690 		in_setsockaddr(inp, nam);
    691 		break;
    692 
    693 	case PRU_PEERADDR:
    694 		in_setpeeraddr(inp, nam);
    695 		break;
    696 
    697 	default:
    698 		panic("rip_usrreq");
    699 	}
    700 	splx(s);
    701 
    702 	return error;
    703 }
    704 
    705 PR_WRAP_USRREQS(rip)
    706 #define	rip_attach	rip_attach_wrapper
    707 #define	rip_detach	rip_detach_wrapper
    708 #define	rip_usrreq	rip_usrreq_wrapper
    709 
    710 const struct pr_usrreqs rip_usrreqs = {
    711 	.pr_attach	= rip_attach,
    712 	.pr_detach	= rip_detach,
    713 	.pr_generic	= rip_usrreq,
    714 };
    715 
    716 static void
    717 sysctl_net_inet_raw_setup(struct sysctllog **clog)
    718 {
    719 
    720 	sysctl_createv(clog, 0, NULL, NULL,
    721 		       CTLFLAG_PERMANENT,
    722 		       CTLTYPE_NODE, "inet", NULL,
    723 		       NULL, 0, NULL, 0,
    724 		       CTL_NET, PF_INET, CTL_EOL);
    725 	sysctl_createv(clog, 0, NULL, NULL,
    726 		       CTLFLAG_PERMANENT,
    727 		       CTLTYPE_NODE, "raw",
    728 		       SYSCTL_DESCR("Raw IPv4 settings"),
    729 		       NULL, 0, NULL, 0,
    730 		       CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
    731 
    732 	sysctl_createv(clog, 0, NULL, NULL,
    733 		       CTLFLAG_PERMANENT,
    734 		       CTLTYPE_STRUCT, "pcblist",
    735 		       SYSCTL_DESCR("Raw IPv4 control block list"),
    736 		       sysctl_inpcblist, 0, &rawcbtable, 0,
    737 		       CTL_NET, PF_INET, IPPROTO_RAW,
    738 		       CTL_CREATE, CTL_EOL);
    739 }
    740