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