Home | History | Annotate | Line # | Download | only in netinet
raw_ip.c revision 1.129
      1 /*	$NetBSD: raw_ip.c,v 1.129 2014/07/07 07:09:58 rtr 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.129 2014/07/07 07:09:58 rtr 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 (ipsec_used &&
    216 		    ipsec4_in_reject_so(m, last->inp_socket)) {
    217 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
    218 			/* do not inject data to pcb */
    219 		}
    220 #endif /*IPSEC*/
    221 		else if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
    222 			rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts,
    223 			    n);
    224 			opts = NULL;
    225 		}
    226 		last = inp;
    227 	}
    228 #if defined(IPSEC)
    229 	/* check AH/ESP integrity. */
    230 	if (ipsec_used && last != NULL
    231 	    && ipsec4_in_reject_so(m, last->inp_socket)) {
    232 		m_freem(m);
    233 		IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
    234 		IP_STATDEC(IP_STAT_DELIVERED);
    235 		/* do not inject data to pcb */
    236 	} else
    237 #endif /*IPSEC*/
    238 	if (last != NULL)
    239 		rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts, m);
    240 	else if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
    241 		uint64_t *ips;
    242 
    243 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
    244 		    0, 0);
    245 		ips = IP_STAT_GETREF();
    246 		ips[IP_STAT_NOPROTO]++;
    247 		ips[IP_STAT_DELIVERED]--;
    248 		IP_STAT_PUTREF();
    249 	} else
    250 		m_freem(m);
    251 	return;
    252 }
    253 
    254 int
    255 rip_pcbnotify(struct inpcbtable *table,
    256     struct in_addr faddr, struct in_addr laddr, int proto, int errno,
    257     void (*notify)(struct inpcb *, int))
    258 {
    259 	struct inpcb_hdr *inph, *ninph;
    260 	int nmatch;
    261 
    262 	nmatch = 0;
    263 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
    264 		struct inpcb *inp = (struct inpcb *)inph;
    265 		if (inp->inp_af != AF_INET)
    266 			continue;
    267 		if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
    268 			continue;
    269 		if (in_hosteq(inp->inp_faddr, faddr) &&
    270 		    in_hosteq(inp->inp_laddr, laddr)) {
    271 			(*notify)(inp, errno);
    272 			nmatch++;
    273 		}
    274 	}
    275 
    276 	return nmatch;
    277 }
    278 
    279 void *
    280 rip_ctlinput(int cmd, const struct sockaddr *sa, void *v)
    281 {
    282 	struct ip *ip = v;
    283 	void (*notify)(struct inpcb *, int) = in_rtchange;
    284 	int errno;
    285 
    286 	if (sa->sa_family != AF_INET ||
    287 	    sa->sa_len != sizeof(struct sockaddr_in))
    288 		return NULL;
    289 	if ((unsigned)cmd >= PRC_NCMDS)
    290 		return NULL;
    291 	errno = inetctlerrmap[cmd];
    292 	if (PRC_IS_REDIRECT(cmd))
    293 		notify = in_rtchange, ip = 0;
    294 	else if (cmd == PRC_HOSTDEAD)
    295 		ip = 0;
    296 	else if (errno == 0)
    297 		return NULL;
    298 	if (ip) {
    299 		rip_pcbnotify(&rawcbtable, satocsin(sa)->sin_addr,
    300 		    ip->ip_src, ip->ip_p, errno, notify);
    301 
    302 		/* XXX mapped address case */
    303 	} else
    304 		in_pcbnotifyall(&rawcbtable, satocsin(sa)->sin_addr, errno,
    305 		    notify);
    306 	return NULL;
    307 }
    308 
    309 /*
    310  * Generate IP header and pass packet to ip_output.
    311  * Tack on options user may have setup with control call.
    312  */
    313 int
    314 rip_output(struct mbuf *m, ...)
    315 {
    316 	struct inpcb *inp;
    317 	struct ip *ip;
    318 	struct mbuf *opts;
    319 	int flags;
    320 	va_list ap;
    321 
    322 	va_start(ap, m);
    323 	inp = va_arg(ap, struct inpcb *);
    324 	va_end(ap);
    325 
    326 	flags =
    327 	    (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
    328 	    | IP_RETURNMTU;
    329 
    330 	/*
    331 	 * If the user handed us a complete IP packet, use it.
    332 	 * Otherwise, allocate an mbuf for a header and fill it in.
    333 	 */
    334 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
    335 		if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
    336 			m_freem(m);
    337 			return (EMSGSIZE);
    338 		}
    339 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    340 		if (!m)
    341 			return (ENOBUFS);
    342 		ip = mtod(m, struct ip *);
    343 		ip->ip_tos = 0;
    344 		ip->ip_off = htons(0);
    345 		ip->ip_p = inp->inp_ip.ip_p;
    346 		ip->ip_len = htons(m->m_pkthdr.len);
    347 		ip->ip_src = inp->inp_laddr;
    348 		ip->ip_dst = inp->inp_faddr;
    349 		ip->ip_ttl = MAXTTL;
    350 		opts = inp->inp_options;
    351 	} else {
    352 		if (m->m_pkthdr.len > IP_MAXPACKET) {
    353 			m_freem(m);
    354 			return (EMSGSIZE);
    355 		}
    356 		ip = mtod(m, struct ip *);
    357 
    358 		/*
    359 		 * If the mbuf is read-only, we need to allocate
    360 		 * a new mbuf for the header, since we need to
    361 		 * modify the header.
    362 		 */
    363 		if (M_READONLY(m)) {
    364 			int hlen = ip->ip_hl << 2;
    365 
    366 			m = m_copyup(m, hlen, (max_linkhdr + 3) & ~3);
    367 			if (m == NULL)
    368 				return (ENOMEM);	/* XXX */
    369 			ip = mtod(m, struct ip *);
    370 		}
    371 
    372 		/* XXX userland passes ip_len and ip_off in host order */
    373 		if (m->m_pkthdr.len != ip->ip_len) {
    374 			m_freem(m);
    375 			return (EINVAL);
    376 		}
    377 		HTONS(ip->ip_len);
    378 		HTONS(ip->ip_off);
    379 		if (ip->ip_id != 0 || m->m_pkthdr.len < IP_MINFRAGSIZE)
    380 			flags |= IP_NOIPNEWID;
    381 		opts = NULL;
    382 		/* XXX prevent ip_output from overwriting header fields */
    383 		flags |= IP_RAWOUTPUT;
    384 		IP_STATINC(IP_STAT_RAWOUT);
    385 	}
    386 
    387 	/*
    388 	 * IP output.  Note: if IP_RETURNMTU flag is set, the MTU size
    389 	 * will be stored in inp_errormtu.
    390 	 */
    391 	return ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions,
    392 	     inp->inp_socket);
    393 }
    394 
    395 /*
    396  * Raw IP socket option processing.
    397  */
    398 int
    399 rip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    400 {
    401 	struct inpcb *inp = sotoinpcb(so);
    402 	int error = 0;
    403 	int optval;
    404 
    405 	if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) {
    406 		if (op == PRCO_GETOPT) {
    407 			optval = (inp->inp_flags & INP_NOHEADER) ? 1 : 0;
    408 			error = sockopt_set(sopt, &optval, sizeof(optval));
    409 		} else if (op == PRCO_SETOPT) {
    410 			error = sockopt_getint(sopt, &optval);
    411 			if (error)
    412 				goto out;
    413 			if (optval) {
    414 				inp->inp_flags &= ~INP_HDRINCL;
    415 				inp->inp_flags |= INP_NOHEADER;
    416 			} else
    417 				inp->inp_flags &= ~INP_NOHEADER;
    418 		}
    419 		goto out;
    420 	} else if (sopt->sopt_level != IPPROTO_IP)
    421 		return ip_ctloutput(op, so, sopt);
    422 
    423 	switch (op) {
    424 
    425 	case PRCO_SETOPT:
    426 		switch (sopt->sopt_name) {
    427 		case IP_HDRINCL:
    428 			error = sockopt_getint(sopt, &optval);
    429 			if (error)
    430 				break;
    431 			if (optval)
    432 				inp->inp_flags |= INP_HDRINCL;
    433 			else
    434 				inp->inp_flags &= ~INP_HDRINCL;
    435 			break;
    436 
    437 #ifdef MROUTING
    438 		case MRT_INIT:
    439 		case MRT_DONE:
    440 		case MRT_ADD_VIF:
    441 		case MRT_DEL_VIF:
    442 		case MRT_ADD_MFC:
    443 		case MRT_DEL_MFC:
    444 		case MRT_ASSERT:
    445 		case MRT_API_CONFIG:
    446 		case MRT_ADD_BW_UPCALL:
    447 		case MRT_DEL_BW_UPCALL:
    448 			error = ip_mrouter_set(so, sopt);
    449 			break;
    450 #endif
    451 
    452 		default:
    453 			error = ip_ctloutput(op, so, sopt);
    454 			break;
    455 		}
    456 		break;
    457 
    458 	case PRCO_GETOPT:
    459 		switch (sopt->sopt_name) {
    460 		case IP_HDRINCL:
    461 			optval = inp->inp_flags & INP_HDRINCL;
    462 			error = sockopt_set(sopt, &optval, sizeof(optval));
    463 			break;
    464 
    465 #ifdef MROUTING
    466 		case MRT_VERSION:
    467 		case MRT_ASSERT:
    468 		case MRT_API_SUPPORT:
    469 		case MRT_API_CONFIG:
    470 			error = ip_mrouter_get(so, sopt);
    471 			break;
    472 #endif
    473 
    474 		default:
    475 			error = ip_ctloutput(op, so, sopt);
    476 			break;
    477 		}
    478 		break;
    479 	}
    480  out:
    481 	return error;
    482 }
    483 
    484 int
    485 rip_bind(struct inpcb *inp, struct mbuf *nam)
    486 {
    487 	struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
    488 
    489 	if (nam->m_len != sizeof(*addr))
    490 		return (EINVAL);
    491 	if (!IFNET_FIRST())
    492 		return (EADDRNOTAVAIL);
    493 	if (addr->sin_family != AF_INET)
    494 		return (EAFNOSUPPORT);
    495 	if (!in_nullhost(addr->sin_addr) &&
    496 	    ifa_ifwithaddr(sintosa(addr)) == 0)
    497 		return (EADDRNOTAVAIL);
    498 	inp->inp_laddr = addr->sin_addr;
    499 	return (0);
    500 }
    501 
    502 int
    503 rip_connect(struct inpcb *inp, struct mbuf *nam)
    504 {
    505 	struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
    506 
    507 	if (nam->m_len != sizeof(*addr))
    508 		return (EINVAL);
    509 	if (!IFNET_FIRST())
    510 		return (EADDRNOTAVAIL);
    511 	if (addr->sin_family != AF_INET)
    512 		return (EAFNOSUPPORT);
    513 	inp->inp_faddr = addr->sin_addr;
    514 	return (0);
    515 }
    516 
    517 void
    518 rip_disconnect(struct inpcb *inp)
    519 {
    520 
    521 	inp->inp_faddr = zeroin_addr;
    522 }
    523 
    524 static int
    525 rip_attach(struct socket *so, int proto)
    526 {
    527 	struct inpcb *inp;
    528 	int error;
    529 
    530 	KASSERT(sotoinpcb(so) == NULL);
    531 	sosetlock(so);
    532 
    533 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    534 		error = soreserve(so, rip_sendspace, rip_recvspace);
    535 		if (error) {
    536 			return error;
    537 		}
    538 	}
    539 
    540 	error = in_pcballoc(so, &rawcbtable);
    541 	if (error) {
    542 		return error;
    543 	}
    544 	inp = sotoinpcb(so);
    545 	inp->inp_ip.ip_p = proto;
    546 	KASSERT(solocked(so));
    547 
    548 	return 0;
    549 }
    550 
    551 static void
    552 rip_detach(struct socket *so)
    553 {
    554 	struct inpcb *inp;
    555 
    556 	KASSERT(solocked(so));
    557 	inp = sotoinpcb(so);
    558 	KASSERT(inp != NULL);
    559 
    560 #ifdef MROUTING
    561 	extern struct socket *ip_mrouter;
    562 	if (so == ip_mrouter) {
    563 		ip_mrouter_done();
    564 	}
    565 #endif
    566 	in_pcbdetach(inp);
    567 }
    568 
    569 static int
    570 rip_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
    571 {
    572 	return in_control(so, cmd, nam, ifp);
    573 }
    574 
    575 static int
    576 rip_stat(struct socket *so, struct stat *ub)
    577 {
    578 	return EOPNOTSUPP;
    579 }
    580 
    581 int
    582 rip_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    583     struct mbuf *control, struct lwp *l)
    584 {
    585 	struct inpcb *inp;
    586 	int s, error = 0;
    587 
    588 	KASSERT(req != PRU_ATTACH);
    589 	KASSERT(req != PRU_DETACH);
    590 	KASSERT(req != PRU_CONTROL);
    591 	KASSERT(req != PRU_SENSE);
    592 
    593 	s = splsoftnet();
    594 	if (req == PRU_PURGEIF) {
    595 		mutex_enter(softnet_lock);
    596 		in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
    597 		in_purgeif((struct ifnet *)control);
    598 		in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
    599 		mutex_exit(softnet_lock);
    600 		splx(s);
    601 		return 0;
    602 	}
    603 
    604 	KASSERT(solocked(so));
    605 	inp = sotoinpcb(so);
    606 
    607 	KASSERT(!control || (req == PRU_SEND || req == PRU_SENDOOB));
    608 	if (inp == NULL) {
    609 		splx(s);
    610 		return EINVAL;
    611 	}
    612 
    613 	switch (req) {
    614 
    615 	case PRU_BIND:
    616 		error = rip_bind(inp, nam);
    617 		break;
    618 
    619 	case PRU_LISTEN:
    620 		error = EOPNOTSUPP;
    621 		break;
    622 
    623 	case PRU_CONNECT:
    624 		error = rip_connect(inp, nam);
    625 		if (error)
    626 			break;
    627 		soisconnected(so);
    628 		break;
    629 
    630 	case PRU_CONNECT2:
    631 		error = EOPNOTSUPP;
    632 		break;
    633 
    634 	case PRU_DISCONNECT:
    635 		soisdisconnected(so);
    636 		rip_disconnect(inp);
    637 		break;
    638 
    639 	/*
    640 	 * Mark the connection as being incapable of further input.
    641 	 */
    642 	case PRU_SHUTDOWN:
    643 		socantsendmore(so);
    644 		break;
    645 
    646 	case PRU_RCVD:
    647 		error = EOPNOTSUPP;
    648 		break;
    649 
    650 	/*
    651 	 * Ship a packet out.  The appropriate raw output
    652 	 * routine handles any massaging necessary.
    653 	 */
    654 	case PRU_SEND:
    655 		if (control && control->m_len) {
    656 			m_freem(control);
    657 			m_freem(m);
    658 			error = EINVAL;
    659 			break;
    660 		}
    661 	{
    662 		if (nam) {
    663 			if ((so->so_state & SS_ISCONNECTED) != 0) {
    664 				error = EISCONN;
    665 				goto die;
    666 			}
    667 			error = rip_connect(inp, nam);
    668 			if (error) {
    669 			die:
    670 				m_freem(m);
    671 				break;
    672 			}
    673 		} else {
    674 			if ((so->so_state & SS_ISCONNECTED) == 0) {
    675 				error = ENOTCONN;
    676 				goto die;
    677 			}
    678 		}
    679 		error = rip_output(m, inp);
    680 		if (nam)
    681 			rip_disconnect(inp);
    682 	}
    683 		break;
    684 
    685 	case PRU_RCVOOB:
    686 		error = EOPNOTSUPP;
    687 		break;
    688 
    689 	case PRU_SENDOOB:
    690 		m_freem(control);
    691 		m_freem(m);
    692 		error = EOPNOTSUPP;
    693 		break;
    694 
    695 	case PRU_SOCKADDR:
    696 		in_setsockaddr(inp, nam);
    697 		break;
    698 
    699 	case PRU_PEERADDR:
    700 		in_setpeeraddr(inp, nam);
    701 		break;
    702 
    703 	default:
    704 		panic("rip_usrreq");
    705 	}
    706 	splx(s);
    707 
    708 	return error;
    709 }
    710 
    711 PR_WRAP_USRREQS(rip)
    712 #define	rip_attach	rip_attach_wrapper
    713 #define	rip_detach	rip_detach_wrapper
    714 #define	rip_ioctl	rip_ioctl_wrapper
    715 #define	rip_stat	rip_stat_wrapper
    716 #define	rip_usrreq	rip_usrreq_wrapper
    717 
    718 const struct pr_usrreqs rip_usrreqs = {
    719 	.pr_attach	= rip_attach,
    720 	.pr_detach	= rip_detach,
    721 	.pr_ioctl	= rip_ioctl,
    722 	.pr_stat	= rip_stat,
    723 	.pr_generic	= rip_usrreq,
    724 };
    725 
    726 static void
    727 sysctl_net_inet_raw_setup(struct sysctllog **clog)
    728 {
    729 
    730 	sysctl_createv(clog, 0, NULL, NULL,
    731 		       CTLFLAG_PERMANENT,
    732 		       CTLTYPE_NODE, "inet", NULL,
    733 		       NULL, 0, NULL, 0,
    734 		       CTL_NET, PF_INET, CTL_EOL);
    735 	sysctl_createv(clog, 0, NULL, NULL,
    736 		       CTLFLAG_PERMANENT,
    737 		       CTLTYPE_NODE, "raw",
    738 		       SYSCTL_DESCR("Raw IPv4 settings"),
    739 		       NULL, 0, NULL, 0,
    740 		       CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
    741 
    742 	sysctl_createv(clog, 0, NULL, NULL,
    743 		       CTLFLAG_PERMANENT,
    744 		       CTLTYPE_STRUCT, "pcblist",
    745 		       SYSCTL_DESCR("Raw IPv4 control block list"),
    746 		       sysctl_inpcblist, 0, &rawcbtable, 0,
    747 		       CTL_NET, PF_INET, IPPROTO_RAW,
    748 		       CTL_CREATE, CTL_EOL);
    749 }
    750