Home | History | Annotate | Line # | Download | only in netinet
raw_ip.c revision 1.165
      1 /*	$NetBSD: raw_ip.c,v 1.165 2017/07/06 17:08:57 christos 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.165 2017/07/06 17:08:57 christos Exp $");
     69 
     70 #ifdef _KERNEL_OPT
     71 #include "opt_inet.h"
     72 #include "opt_ipsec.h"
     73 #include "opt_mrouting.h"
     74 #include "opt_net_mpsafe.h"
     75 #endif
     76 
     77 #include <sys/param.h>
     78 #include <sys/sysctl.h>
     79 #include <sys/mbuf.h>
     80 #include <sys/socket.h>
     81 #include <sys/protosw.h>
     82 #include <sys/socketvar.h>
     83 #include <sys/errno.h>
     84 #include <sys/systm.h>
     85 #include <sys/proc.h>
     86 #include <sys/kauth.h>
     87 
     88 #include <net/if.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 struct inpcbtable rawcbtable;
    108 
    109 int	 rip_pcbnotify(struct inpcbtable *, struct in_addr,
    110     struct in_addr, int, int, void (*)(struct inpcb *, int));
    111 static int	 rip_connect_pcb(struct inpcb *, struct sockaddr_in *);
    112 static void	 rip_disconnect1(struct inpcb *);
    113 
    114 static void sysctl_net_inet_raw_setup(struct sysctllog **);
    115 
    116 /*
    117  * Nominal space allocated to a raw ip socket.
    118  */
    119 #define	RIPSNDQ		8192
    120 #define	RIPRCVQ		8192
    121 
    122 static u_long		rip_sendspace = RIPSNDQ;
    123 static u_long		rip_recvspace = RIPRCVQ;
    124 
    125 /*
    126  * Raw interface to IP protocol.
    127  */
    128 
    129 /*
    130  * Initialize raw connection block q.
    131  */
    132 void
    133 rip_init(void)
    134 {
    135 
    136 	sysctl_net_inet_raw_setup(NULL);
    137 	in_pcbinit(&rawcbtable, 1, 1);
    138 }
    139 
    140 static void
    141 rip_sbappendaddr(struct inpcb *last, struct ip *ip, const struct sockaddr *sa,
    142     int hlen, struct mbuf *opts, struct mbuf *n)
    143 {
    144 	if (last->inp_flags & INP_NOHEADER)
    145 		m_adj(n, hlen);
    146 	if (last->inp_flags & INP_CONTROLOPTS
    147 	    || SOOPT_TIMESTAMP(last->inp_socket->so_options))
    148 		ip_savecontrol(last, &opts, ip, n);
    149 	if (sbappendaddr(&last->inp_socket->so_rcv, sa, n, opts) == 0) {
    150 		/* should notify about lost packet */
    151 		m_freem(n);
    152 		if (opts)
    153 			m_freem(opts);
    154 	} else
    155 		sorwakeup(last->inp_socket);
    156 }
    157 
    158 /*
    159  * Setup generic address and protocol structures
    160  * for raw_input routine, then pass them along with
    161  * mbuf chain.
    162  */
    163 void
    164 rip_input(struct mbuf *m, ...)
    165 {
    166 	int hlen, proto;
    167 	struct ip *ip = mtod(m, struct ip *);
    168 	struct inpcb_hdr *inph;
    169 	struct inpcb *inp;
    170 	struct inpcb *last = NULL;
    171 	struct mbuf *n, *opts = NULL;
    172 	struct sockaddr_in ripsrc;
    173 	va_list ap;
    174 
    175 	va_start(ap, m);
    176 	(void)va_arg(ap, int);		/* ignore value, advance ap */
    177 	proto = va_arg(ap, int);
    178 	va_end(ap);
    179 
    180 	sockaddr_in_init(&ripsrc, &ip->ip_src, 0);
    181 
    182 	/*
    183 	 * XXX Compatibility: programs using raw IP expect ip_len
    184 	 * XXX to have the header length subtracted, and in host order.
    185 	 * XXX ip_off is also expected to be host order.
    186 	 */
    187 	hlen = ip->ip_hl << 2;
    188 	ip->ip_len = ntohs(ip->ip_len) - hlen;
    189 	NTOHS(ip->ip_off);
    190 
    191 	TAILQ_FOREACH(inph, &rawcbtable.inpt_queue, inph_queue) {
    192 		inp = (struct inpcb *)inph;
    193 		if (inp->inp_af != AF_INET)
    194 			continue;
    195 		if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
    196 			continue;
    197 		if (!in_nullhost(inp->inp_laddr) &&
    198 		    !in_hosteq(inp->inp_laddr, ip->ip_dst))
    199 			continue;
    200 		if (!in_nullhost(inp->inp_faddr) &&
    201 		    !in_hosteq(inp->inp_faddr, ip->ip_src))
    202 			continue;
    203 		if (last == NULL)
    204 			;
    205 #if defined(IPSEC)
    206 		/* check AH/ESP integrity. */
    207 		else if (ipsec_used &&
    208 		    ipsec4_in_reject(m, last)) {
    209 			IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
    210 			/* do not inject data to pcb */
    211 		}
    212 #endif /*IPSEC*/
    213 		else if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
    214 			rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts,
    215 			    n);
    216 			opts = NULL;
    217 		}
    218 		last = inp;
    219 	}
    220 #if defined(IPSEC)
    221 	/* check AH/ESP integrity. */
    222 	if (ipsec_used && last != NULL
    223 	    && ipsec4_in_reject(m, last)) {
    224 		m_freem(m);
    225 		IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
    226 		IP_STATDEC(IP_STAT_DELIVERED);
    227 		/* do not inject data to pcb */
    228 	} else
    229 #endif /*IPSEC*/
    230 	if (last != NULL)
    231 		rip_sbappendaddr(last, ip, sintosa(&ripsrc), hlen, opts, m);
    232 	else if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
    233 		uint64_t *ips;
    234 
    235 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
    236 		    0, 0);
    237 		ips = IP_STAT_GETREF();
    238 		ips[IP_STAT_NOPROTO]++;
    239 		ips[IP_STAT_DELIVERED]--;
    240 		IP_STAT_PUTREF();
    241 	} else
    242 		m_freem(m);
    243 	return;
    244 }
    245 
    246 int
    247 rip_pcbnotify(struct inpcbtable *table,
    248     struct in_addr faddr, struct in_addr laddr, int proto, int errno,
    249     void (*notify)(struct inpcb *, int))
    250 {
    251 	struct inpcb_hdr *inph, *ninph;
    252 	int nmatch;
    253 
    254 	nmatch = 0;
    255 	TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
    256 		struct inpcb *inp = (struct inpcb *)inph;
    257 		if (inp->inp_af != AF_INET)
    258 			continue;
    259 		if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
    260 			continue;
    261 		if (in_hosteq(inp->inp_faddr, faddr) &&
    262 		    in_hosteq(inp->inp_laddr, laddr)) {
    263 			(*notify)(inp, errno);
    264 			nmatch++;
    265 		}
    266 	}
    267 
    268 	return nmatch;
    269 }
    270 
    271 void *
    272 rip_ctlinput(int cmd, const struct sockaddr *sa, void *v)
    273 {
    274 	struct ip *ip = v;
    275 	void (*notify)(struct inpcb *, int) = in_rtchange;
    276 	int errno;
    277 
    278 	if (sa->sa_family != AF_INET ||
    279 	    sa->sa_len != sizeof(struct sockaddr_in))
    280 		return NULL;
    281 	if ((unsigned)cmd >= PRC_NCMDS)
    282 		return NULL;
    283 	errno = inetctlerrmap[cmd];
    284 	if (PRC_IS_REDIRECT(cmd))
    285 		notify = in_rtchange, ip = 0;
    286 	else if (cmd == PRC_HOSTDEAD)
    287 		ip = 0;
    288 	else if (errno == 0)
    289 		return NULL;
    290 	if (ip) {
    291 		rip_pcbnotify(&rawcbtable, satocsin(sa)->sin_addr,
    292 		    ip->ip_src, ip->ip_p, errno, notify);
    293 
    294 		/* XXX mapped address case */
    295 	} else
    296 		in_pcbnotifyall(&rawcbtable, satocsin(sa)->sin_addr, errno,
    297 		    notify);
    298 	return NULL;
    299 }
    300 
    301 /*
    302  * Generate IP header and pass packet to ip_output.
    303  * Tack on options user may have setup with control call.
    304  */
    305 int
    306 rip_output(struct mbuf *m, struct inpcb *inp)
    307 {
    308 	struct ip *ip;
    309 	struct mbuf *opts;
    310 	int flags;
    311 
    312 	flags =
    313 	    (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
    314 	    | IP_RETURNMTU;
    315 
    316 	/*
    317 	 * If the user handed us a complete IP packet, use it.
    318 	 * Otherwise, allocate an mbuf for a header and fill it in.
    319 	 */
    320 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
    321 		if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
    322 			m_freem(m);
    323 			return (EMSGSIZE);
    324 		}
    325 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    326 		if (!m)
    327 			return (ENOBUFS);
    328 		ip = mtod(m, struct ip *);
    329 		ip->ip_tos = 0;
    330 		ip->ip_off = htons(0);
    331 		ip->ip_p = inp->inp_ip.ip_p;
    332 		ip->ip_len = htons(m->m_pkthdr.len);
    333 		ip->ip_src = inp->inp_laddr;
    334 		ip->ip_dst = inp->inp_faddr;
    335 		ip->ip_ttl = MAXTTL;
    336 		opts = inp->inp_options;
    337 	} else {
    338 		if (m->m_pkthdr.len > IP_MAXPACKET) {
    339 			m_freem(m);
    340 			return (EMSGSIZE);
    341 		}
    342 		ip = mtod(m, struct ip *);
    343 
    344 		/*
    345 		 * If the mbuf is read-only, we need to allocate
    346 		 * a new mbuf for the header, since we need to
    347 		 * modify the header.
    348 		 */
    349 		if (M_READONLY(m)) {
    350 			int hlen = ip->ip_hl << 2;
    351 
    352 			m = m_copyup(m, hlen, (max_linkhdr + 3) & ~3);
    353 			if (m == NULL)
    354 				return (ENOMEM);	/* XXX */
    355 			ip = mtod(m, struct ip *);
    356 		}
    357 
    358 		/* XXX userland passes ip_len and ip_off in host order */
    359 		if (m->m_pkthdr.len != ip->ip_len) {
    360 			m_freem(m);
    361 			return (EINVAL);
    362 		}
    363 		HTONS(ip->ip_len);
    364 		HTONS(ip->ip_off);
    365 		if (ip->ip_id != 0 || m->m_pkthdr.len < IP_MINFRAGSIZE)
    366 			flags |= IP_NOIPNEWID;
    367 		opts = NULL;
    368 		/* XXX prevent ip_output from overwriting header fields */
    369 		flags |= IP_RAWOUTPUT;
    370 		IP_STATINC(IP_STAT_RAWOUT);
    371 	}
    372 
    373 	/*
    374 	 * IP output.  Note: if IP_RETURNMTU flag is set, the MTU size
    375 	 * will be stored in inp_errormtu.
    376 	 */
    377 	return ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions,
    378 	     inp);
    379 }
    380 
    381 /*
    382  * Raw IP socket option processing.
    383  */
    384 int
    385 rip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    386 {
    387 	struct inpcb *inp = sotoinpcb(so);
    388 	int error = 0;
    389 	int optval;
    390 
    391 	if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) {
    392 		if (op == PRCO_GETOPT) {
    393 			optval = (inp->inp_flags & INP_NOHEADER) ? 1 : 0;
    394 			error = sockopt_set(sopt, &optval, sizeof(optval));
    395 		} else if (op == PRCO_SETOPT) {
    396 			error = sockopt_getint(sopt, &optval);
    397 			if (error)
    398 				goto out;
    399 			if (optval) {
    400 				inp->inp_flags &= ~INP_HDRINCL;
    401 				inp->inp_flags |= INP_NOHEADER;
    402 			} else
    403 				inp->inp_flags &= ~INP_NOHEADER;
    404 		}
    405 		goto out;
    406 	} else if (sopt->sopt_level != IPPROTO_IP)
    407 		return ip_ctloutput(op, so, sopt);
    408 
    409 	switch (op) {
    410 
    411 	case PRCO_SETOPT:
    412 		switch (sopt->sopt_name) {
    413 		case IP_HDRINCL:
    414 			error = sockopt_getint(sopt, &optval);
    415 			if (error)
    416 				break;
    417 			if (optval)
    418 				inp->inp_flags |= INP_HDRINCL;
    419 			else
    420 				inp->inp_flags &= ~INP_HDRINCL;
    421 			break;
    422 
    423 #ifdef MROUTING
    424 		case MRT_INIT:
    425 		case MRT_DONE:
    426 		case MRT_ADD_VIF:
    427 		case MRT_DEL_VIF:
    428 		case MRT_ADD_MFC:
    429 		case MRT_DEL_MFC:
    430 		case MRT_ASSERT:
    431 		case MRT_API_CONFIG:
    432 		case MRT_ADD_BW_UPCALL:
    433 		case MRT_DEL_BW_UPCALL:
    434 			error = ip_mrouter_set(so, sopt);
    435 			break;
    436 #endif
    437 
    438 		default:
    439 			error = ip_ctloutput(op, so, sopt);
    440 			break;
    441 		}
    442 		break;
    443 
    444 	case PRCO_GETOPT:
    445 		switch (sopt->sopt_name) {
    446 		case IP_HDRINCL:
    447 			optval = inp->inp_flags & INP_HDRINCL;
    448 			error = sockopt_set(sopt, &optval, sizeof(optval));
    449 			break;
    450 
    451 #ifdef MROUTING
    452 		case MRT_VERSION:
    453 		case MRT_ASSERT:
    454 		case MRT_API_SUPPORT:
    455 		case MRT_API_CONFIG:
    456 			error = ip_mrouter_get(so, sopt);
    457 			break;
    458 #endif
    459 
    460 		default:
    461 			error = ip_ctloutput(op, so, sopt);
    462 			break;
    463 		}
    464 		break;
    465 	}
    466  out:
    467 	return error;
    468 }
    469 
    470 int
    471 rip_connect_pcb(struct inpcb *inp, struct sockaddr_in *addr)
    472 {
    473 
    474 	if (IFNET_READER_EMPTY())
    475 		return (EADDRNOTAVAIL);
    476 	if (addr->sin_family != AF_INET)
    477 		return (EAFNOSUPPORT);
    478 	inp->inp_faddr = addr->sin_addr;
    479 	return (0);
    480 }
    481 
    482 static void
    483 rip_disconnect1(struct inpcb *inp)
    484 {
    485 
    486 	inp->inp_faddr = zeroin_addr;
    487 }
    488 
    489 static int
    490 rip_attach(struct socket *so, int proto)
    491 {
    492 	struct inpcb *inp;
    493 	int error;
    494 
    495 	KASSERT(sotoinpcb(so) == NULL);
    496 	sosetlock(so);
    497 
    498 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    499 		error = soreserve(so, rip_sendspace, rip_recvspace);
    500 		if (error) {
    501 			return error;
    502 		}
    503 	}
    504 
    505 	error = in_pcballoc(so, &rawcbtable);
    506 	if (error) {
    507 		return error;
    508 	}
    509 	inp = sotoinpcb(so);
    510 	inp->inp_ip.ip_p = proto;
    511 	KASSERT(solocked(so));
    512 
    513 	return 0;
    514 }
    515 
    516 static void
    517 rip_detach(struct socket *so)
    518 {
    519 	struct inpcb *inp;
    520 
    521 	KASSERT(solocked(so));
    522 	inp = sotoinpcb(so);
    523 	KASSERT(inp != NULL);
    524 
    525 #ifdef MROUTING
    526 	extern struct socket *ip_mrouter;
    527 	if (so == ip_mrouter) {
    528 		ip_mrouter_done();
    529 	}
    530 #endif
    531 	in_pcbdetach(inp);
    532 }
    533 
    534 static int
    535 rip_accept(struct socket *so, struct sockaddr *nam)
    536 {
    537 	KASSERT(solocked(so));
    538 
    539 	panic("rip_accept");
    540 
    541 	return EOPNOTSUPP;
    542 }
    543 
    544 static int
    545 rip_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
    546 {
    547 	struct inpcb *inp = sotoinpcb(so);
    548 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
    549 	int error = 0;
    550 	int s, ss;
    551 	struct ifaddr *ifa;
    552 
    553 	KASSERT(solocked(so));
    554 	KASSERT(inp != NULL);
    555 	KASSERT(nam != NULL);
    556 
    557 	if (addr->sin_len != sizeof(*addr))
    558 		return EINVAL;
    559 
    560 	s = splsoftnet();
    561 	if (IFNET_READER_EMPTY()) {
    562 		error = EADDRNOTAVAIL;
    563 		goto release;
    564 	}
    565 	if (addr->sin_family != AF_INET) {
    566 		error = EAFNOSUPPORT;
    567 		goto release;
    568 	}
    569 	ss = pserialize_read_enter();
    570 	if ((ifa = ifa_ifwithaddr(sintosa(addr))) == NULL &&
    571 	    !in_nullhost(addr->sin_addr))
    572 	{
    573 		pserialize_read_exit(ss);
    574 		error = EADDRNOTAVAIL;
    575 		goto release;
    576 	}
    577         if (ifa && (ifatoia(ifa))->ia4_flags & IN6_IFF_DUPLICATED) {
    578 		pserialize_read_exit(ss);
    579 		error = EADDRNOTAVAIL;
    580 		goto release;
    581 	}
    582 	pserialize_read_exit(ss);
    583 
    584 	inp->inp_laddr = addr->sin_addr;
    585 
    586 release:
    587 	splx(s);
    588 	return error;
    589 }
    590 
    591 static int
    592 rip_listen(struct socket *so, struct lwp *l)
    593 {
    594 	KASSERT(solocked(so));
    595 
    596 	return EOPNOTSUPP;
    597 }
    598 
    599 static int
    600 rip_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
    601 {
    602 	struct inpcb *inp = sotoinpcb(so);
    603 	int error = 0;
    604 	int s;
    605 
    606 	KASSERT(solocked(so));
    607 	KASSERT(inp != NULL);
    608 	KASSERT(nam != NULL);
    609 
    610 	s = splsoftnet();
    611 	error = rip_connect_pcb(inp, (struct sockaddr_in *)nam);
    612 	if (! error)
    613 		soisconnected(so);
    614 	splx(s);
    615 
    616 	return error;
    617 }
    618 
    619 static int
    620 rip_connect2(struct socket *so, struct socket *so2)
    621 {
    622 	KASSERT(solocked(so));
    623 
    624 	return EOPNOTSUPP;
    625 }
    626 
    627 static int
    628 rip_disconnect(struct socket *so)
    629 {
    630 	struct inpcb *inp = sotoinpcb(so);
    631 	int s;
    632 
    633 	KASSERT(solocked(so));
    634 	KASSERT(inp != NULL);
    635 
    636 	s = splsoftnet();
    637 	soisdisconnected(so);
    638 	rip_disconnect1(inp);
    639 	splx(s);
    640 
    641 	return 0;
    642 }
    643 
    644 static int
    645 rip_shutdown(struct socket *so)
    646 {
    647 	int s;
    648 
    649 	KASSERT(solocked(so));
    650 
    651 	/*
    652 	 * Mark the connection as being incapable of further input.
    653 	 */
    654 	s = splsoftnet();
    655 	socantsendmore(so);
    656 	splx(s);
    657 
    658 	return 0;
    659 }
    660 
    661 static int
    662 rip_abort(struct socket *so)
    663 {
    664 	KASSERT(solocked(so));
    665 
    666 	panic("rip_abort");
    667 
    668 	return EOPNOTSUPP;
    669 }
    670 
    671 static int
    672 rip_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
    673 {
    674 	return in_control(so, cmd, nam, ifp);
    675 }
    676 
    677 static int
    678 rip_stat(struct socket *so, struct stat *ub)
    679 {
    680 	KASSERT(solocked(so));
    681 
    682 	/* stat: don't bother with a blocksize. */
    683 	return 0;
    684 }
    685 
    686 static int
    687 rip_peeraddr(struct socket *so, struct sockaddr *nam)
    688 {
    689 	int s;
    690 
    691 	KASSERT(solocked(so));
    692 	KASSERT(sotoinpcb(so) != NULL);
    693 	KASSERT(nam != NULL);
    694 
    695 	s = splsoftnet();
    696 	in_setpeeraddr(sotoinpcb(so), (struct sockaddr_in *)nam);
    697 	splx(s);
    698 
    699 	return 0;
    700 }
    701 
    702 static int
    703 rip_sockaddr(struct socket *so, struct sockaddr *nam)
    704 {
    705 	int s;
    706 
    707 	KASSERT(solocked(so));
    708 	KASSERT(sotoinpcb(so) != NULL);
    709 	KASSERT(nam != NULL);
    710 
    711 	s = splsoftnet();
    712 	in_setsockaddr(sotoinpcb(so), (struct sockaddr_in *)nam);
    713 	splx(s);
    714 
    715 	return 0;
    716 }
    717 
    718 static int
    719 rip_rcvd(struct socket *so, int flags, struct lwp *l)
    720 {
    721 	KASSERT(solocked(so));
    722 
    723 	return EOPNOTSUPP;
    724 }
    725 
    726 static int
    727 rip_recvoob(struct socket *so, struct mbuf *m, int flags)
    728 {
    729 	KASSERT(solocked(so));
    730 
    731 	return EOPNOTSUPP;
    732 }
    733 
    734 static int
    735 rip_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
    736     struct mbuf *control, struct lwp *l)
    737 {
    738 	struct inpcb *inp = sotoinpcb(so);
    739 	int error = 0;
    740 	int s;
    741 
    742 	KASSERT(solocked(so));
    743 	KASSERT(inp != NULL);
    744 	KASSERT(m != NULL);
    745 
    746 	/*
    747 	 * Ship a packet out.  The appropriate raw output
    748 	 * routine handles any massaging necessary.
    749 	 */
    750 	if (control && control->m_len) {
    751 		m_freem(control);
    752 		m_freem(m);
    753 		return EINVAL;
    754 	}
    755 
    756 	s = splsoftnet();
    757 	if (nam) {
    758 		if ((so->so_state & SS_ISCONNECTED) != 0) {
    759 			error = EISCONN;
    760 			goto die;
    761 		}
    762 		error = rip_connect_pcb(inp, (struct sockaddr_in *)nam);
    763 		if (error) {
    764 		die:
    765 			m_freem(m);
    766 			splx(s);
    767 			return error;
    768 		}
    769 	} else {
    770 		if ((so->so_state & SS_ISCONNECTED) == 0) {
    771 			error = ENOTCONN;
    772 			goto die;
    773 		}
    774 	}
    775 	error = rip_output(m, inp);
    776 	if (nam)
    777 		rip_disconnect1(inp);
    778 
    779 	splx(s);
    780 	return error;
    781 }
    782 
    783 static int
    784 rip_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
    785 {
    786 	KASSERT(solocked(so));
    787 
    788 	m_freem(m);
    789 	m_freem(control);
    790 
    791 	return EOPNOTSUPP;
    792 }
    793 
    794 static int
    795 rip_purgeif(struct socket *so, struct ifnet *ifp)
    796 {
    797 	int s;
    798 
    799 	s = splsoftnet();
    800 	mutex_enter(softnet_lock);
    801 	in_pcbpurgeif0(&rawcbtable, ifp);
    802 #ifdef NET_MPSAFE
    803 	mutex_exit(softnet_lock);
    804 #endif
    805 	in_purgeif(ifp);
    806 #ifdef NET_MPSAFE
    807 	mutex_enter(softnet_lock);
    808 #endif
    809 	in_pcbpurgeif(&rawcbtable, ifp);
    810 	mutex_exit(softnet_lock);
    811 	splx(s);
    812 
    813 	return 0;
    814 }
    815 
    816 PR_WRAP_USRREQS(rip)
    817 #define	rip_attach	rip_attach_wrapper
    818 #define	rip_detach	rip_detach_wrapper
    819 #define	rip_accept	rip_accept_wrapper
    820 #define	rip_bind	rip_bind_wrapper
    821 #define	rip_listen	rip_listen_wrapper
    822 #define	rip_connect	rip_connect_wrapper
    823 #define	rip_connect2	rip_connect2_wrapper
    824 #define	rip_disconnect	rip_disconnect_wrapper
    825 #define	rip_shutdown	rip_shutdown_wrapper
    826 #define	rip_abort	rip_abort_wrapper
    827 #define	rip_ioctl	rip_ioctl_wrapper
    828 #define	rip_stat	rip_stat_wrapper
    829 #define	rip_peeraddr	rip_peeraddr_wrapper
    830 #define	rip_sockaddr	rip_sockaddr_wrapper
    831 #define	rip_rcvd	rip_rcvd_wrapper
    832 #define	rip_recvoob	rip_recvoob_wrapper
    833 #define	rip_send	rip_send_wrapper
    834 #define	rip_sendoob	rip_sendoob_wrapper
    835 #define	rip_purgeif	rip_purgeif_wrapper
    836 
    837 const struct pr_usrreqs rip_usrreqs = {
    838 	.pr_attach	= rip_attach,
    839 	.pr_detach	= rip_detach,
    840 	.pr_accept	= rip_accept,
    841 	.pr_bind	= rip_bind,
    842 	.pr_listen	= rip_listen,
    843 	.pr_connect	= rip_connect,
    844 	.pr_connect2	= rip_connect2,
    845 	.pr_disconnect	= rip_disconnect,
    846 	.pr_shutdown	= rip_shutdown,
    847 	.pr_abort	= rip_abort,
    848 	.pr_ioctl	= rip_ioctl,
    849 	.pr_stat	= rip_stat,
    850 	.pr_peeraddr	= rip_peeraddr,
    851 	.pr_sockaddr	= rip_sockaddr,
    852 	.pr_rcvd	= rip_rcvd,
    853 	.pr_recvoob	= rip_recvoob,
    854 	.pr_send	= rip_send,
    855 	.pr_sendoob	= rip_sendoob,
    856 	.pr_purgeif	= rip_purgeif,
    857 };
    858 
    859 static void
    860 sysctl_net_inet_raw_setup(struct sysctllog **clog)
    861 {
    862 
    863 	sysctl_createv(clog, 0, NULL, NULL,
    864 		       CTLFLAG_PERMANENT,
    865 		       CTLTYPE_NODE, "inet", NULL,
    866 		       NULL, 0, NULL, 0,
    867 		       CTL_NET, PF_INET, CTL_EOL);
    868 	sysctl_createv(clog, 0, NULL, NULL,
    869 		       CTLFLAG_PERMANENT,
    870 		       CTLTYPE_NODE, "raw",
    871 		       SYSCTL_DESCR("Raw IPv4 settings"),
    872 		       NULL, 0, NULL, 0,
    873 		       CTL_NET, PF_INET, IPPROTO_RAW, CTL_EOL);
    874 
    875 	sysctl_createv(clog, 0, NULL, NULL,
    876 		       CTLFLAG_PERMANENT,
    877 		       CTLTYPE_STRUCT, "pcblist",
    878 		       SYSCTL_DESCR("Raw IPv4 control block list"),
    879 		       sysctl_inpcblist, 0, &rawcbtable, 0,
    880 		       CTL_NET, PF_INET, IPPROTO_RAW,
    881 		       CTL_CREATE, CTL_EOL);
    882 }
    883