Home | History | Annotate | Line # | Download | only in netinet6
dccp6_usrreq.c revision 1.1
      1 /*	$KAME: dccp6_usrreq.c,v 1.13 2005/07/27 08:42:56 nishida Exp $	*/
      2 /*	$NetBSD: dccp6_usrreq.c,v 1.1 2015/02/10 19:11:52 rjs Exp $ */
      3 
      4 /*
      5  * Copyright (C) 2003 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: dccp6_usrreq.c,v 1.1 2015/02/10 19:11:52 rjs Exp $");
     35 
     36 #include "opt_inet.h"
     37 #include "opt_dccp.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/domain.h>
     42 #include <sys/kernel.h>
     43 #include <sys/pool.h>
     44 #include <sys/lock.h>
     45 #include <sys/malloc.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/proc.h>
     48 #include <sys/protosw.h>
     49 #include <sys/signalvar.h>
     50 #include <sys/socket.h>
     51 #include <sys/socketvar.h>
     52 #include <sys/mutex.h>
     53 #include <sys/sysctl.h>
     54 #include <sys/syslog.h>
     55 #include <sys/queue.h>
     56 
     57 #include <net/if.h>
     58 #include <net/route.h>
     59 
     60 #include <netinet/in.h>
     61 #include <netinet/in_systm.h>
     62 #include <netinet/ip.h>
     63 #include <netinet/in_pcb.h>
     64 #include <netinet/in_var.h>
     65 #include <netinet/ip6.h>
     66 #include <netinet/ip_icmp.h>
     67 #include <netinet/icmp_var.h>
     68 #include <netinet/ip_var.h>
     69 #include <netinet6/in6_pcb.h>
     70 #include <netinet6/ip6_var.h>
     71 #include <netinet6/nd6.h>
     72 #include <netinet/dccp.h>
     73 #include <netinet/dccp_var.h>
     74 #include <netinet6/dccp6_var.h>
     75 #include <netinet/dccp_cc_sw.h>
     76 
     77 #if !defined(__FreeBSD__) || __FreeBSD_version < 500000
     78 #define	INP_INFO_LOCK_INIT(x,y)
     79 #define	INP_INFO_WLOCK(x)
     80 #define INP_INFO_WUNLOCK(x)
     81 #define	INP_INFO_RLOCK(x)
     82 #define INP_INFO_RUNLOCK(x)
     83 #define	INP_LOCK(x)
     84 #define INP_UNLOCK(x)
     85 #endif
     86 
     87 #define PULLDOWN_TEST
     88 
     89 int
     90 dccp6_input(struct mbuf **mp, int *offp, int proto)
     91 {
     92 	struct mbuf *m = *mp;
     93 	DCCP_DEBUG((LOG_INFO, "In dccp6_input!\n"));
     94 #ifndef PULLDOWN_TEST
     95 	IP6_EXTHDR_CHECK(m, *offp, sizeof(struct dccphdr), IPPROTO_DONE);
     96 #endif
     97 
     98 	dccp_input(m, *offp);
     99 	return IPPROTO_DONE;
    100 }
    101 
    102 void *
    103 dccp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
    104 {
    105 	if (sa->sa_family != AF_INET6 ||
    106 	    sa->sa_len != sizeof(struct sockaddr_in6))
    107 		return NULL;
    108 
    109 	/* FIX LATER */
    110 	return NULL;
    111 }
    112 
    113 int
    114 dccp6_bind(struct socket *so, struct mbuf *m, struct lwp *td)
    115 {
    116 	struct in6pcb *in6p;
    117 	struct sockaddr *nam;
    118 	int error;
    119 	struct sockaddr_in6 *sin6p;
    120 
    121 	DCCP_DEBUG((LOG_INFO, "Entering dccp6_bind!\n"));
    122 	INP_INFO_WLOCK(&dccpbinfo);
    123 	in6p = sotoin6pcb(so);
    124 	if (in6p == 0) {
    125 		INP_INFO_WUNLOCK(&dccpbinfo);
    126 		DCCP_DEBUG((LOG_INFO, "dccp6_bind: in6p == 0!\n"));
    127 		return EINVAL;
    128 	}
    129 	/* Do not bind to multicast addresses! */
    130 	nam = mtod(m, struct sockaddr *);
    131 	sin6p = (struct sockaddr_in6 *)nam;
    132 	if (sin6p->sin6_family == AF_INET6 &&
    133 	    IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
    134 		INP_INFO_WUNLOCK(&dccpbinfo);
    135 		return EAFNOSUPPORT;
    136 	}
    137 	INP_LOCK(inp);
    138 
    139 	in6todccpcb(in6p)->inp_vflag &= ~INP_IPV4;
    140 	in6todccpcb(in6p)->inp_vflag |= INP_IPV6;
    141 
    142 	error = in6_pcbbind(in6p, m, td);
    143 	INP_UNLOCK(inp);
    144 	INP_INFO_WUNLOCK(&dccpbinfo);
    145 	return error;
    146 }
    147 
    148 int
    149 dccp6_connect(struct socket *so, struct mbuf *m, struct lwp *l)
    150 {
    151 	struct in6pcb *in6p;
    152 	struct dccpcb *dp;
    153 	int error;
    154 	struct sockaddr_in6 *sin6;
    155 	struct sockaddr *nam;
    156 	char test[2];
    157 
    158 	DCCP_DEBUG((LOG_INFO, "Entering dccp6_connect!\n"));
    159 
    160 #ifndef __NetBSD__
    161 	INP_INFO_WLOCK(&dccpbinfo);
    162 	inp = sotoinpcb(so);
    163 	if (inp == 0) {
    164 		INP_INFO_WUNLOCK(&dccpbinfo);
    165 		return EINVAL;
    166 	}
    167 	INP_LOCK(inp);
    168 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
    169 		INP_UNLOCK(inp);
    170 		INP_INFO_WUNLOCK(&dccpbinfo);
    171 		return EISCONN;
    172 	}
    173 
    174 	dp = (struct dccpcb *)inp->inp_ppcb;
    175 #else
    176 	in6p = sotoin6pcb(so);
    177 	if (in6p == 0) {
    178 		return EINVAL;
    179 	}
    180 	if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    181 		return EISCONN;
    182 	}
    183 
    184 	dp = (struct dccpcb *)in6p->in6p_ppcb;
    185 #endif
    186 	if (dp->state == DCCPS_ESTAB) {
    187 		DCCP_DEBUG((LOG_INFO, "Why are we in connect when we already have a established connection?\n"));
    188 	}
    189 
    190 	dp->who = DCCP_CLIENT;
    191 	dp->seq_snd = (((u_int64_t)random() << 32) | random()) % 281474976710656LL;
    192 
    193 	nam = mtod(m, struct sockaddr *);
    194 	sin6 = (struct sockaddr_in6 *)nam;
    195 	if (sin6->sin6_family == AF_INET6
    196 	    && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
    197 		error = EAFNOSUPPORT;
    198 		goto bad;
    199 	}
    200 
    201 	dp->inp_vflag &= ~INP_IPV4;
    202 	dp->inp_vflag |= INP_IPV6;
    203 
    204 	error = dccp_doconnect(so, m, l, 1);
    205 
    206 	if (error != 0)
    207 		goto bad;
    208 
    209 	callout_reset(&dp->retrans_timer, dp->retrans, dccp_retrans_t, dp);
    210 	callout_reset(&dp->connect_timer, DCCP_CONNECT_TIMER, dccp_connect_t, dp);
    211 
    212 	test[0] = dp->pref_cc;
    213 #if 0
    214 	/* FIX THIS LATER */
    215 	if (dp->pref_cc == 2) {
    216 		test[1] = 3;
    217 	} else {
    218 		test[1] = 2;
    219 	}
    220 	dccp_add_feature(dp, DCCP_OPT_CHANGE, DCCP_FEATURE_CC, test, 2);
    221 	dccp_add_feature(dp, DCCP_OPT_PREFER, DCCP_FEATURE_CC, test, 2);
    222 #else
    223 	 /* we only support CCID2 at this moment */
    224 	dccp_add_feature(dp, DCCP_OPT_CHANGE_R, DCCP_FEATURE_CC, test, 1);
    225 #endif
    226 
    227 	error = dccp_output(dp, 0);
    228 
    229 bad:
    230 	INP_UNLOCK(inp);
    231 	INP_INFO_WUNLOCK(&dccpbinfo);
    232 	return error;
    233 }
    234 
    235 
    236 int
    237 dccp6_listen(struct socket *so, struct lwp *l)
    238 {
    239 	struct in6pcb *in6p;
    240 	struct dccpcb *dp;
    241 	int error = 0;
    242 
    243 	DCCP_DEBUG((LOG_INFO, "Entering dccp6_listen!\n"));
    244 
    245 	INP_INFO_RLOCK(&dccpbinfo);
    246 	in6p = sotoin6pcb(so);
    247 	if (in6p == 0) {
    248 		INP_INFO_RUNLOCK(&dccpbinfo);
    249 		return EINVAL;
    250 	}
    251 	INP_LOCK(inp);
    252 	INP_INFO_RUNLOCK(&dccpbinfo);
    253 	dp = in6todccpcb(in6p);
    254 	DCCP_DEBUG((LOG_INFO, "Checking in6p->in6p_lport!\n"));
    255 	if (in6p->in6p_lport == 0) {
    256 		error = in6_pcbbind(in6p, (struct mbuf *)0, l);
    257 	}
    258 	if (error == 0) {
    259 		dp->state = DCCPS_LISTEN;
    260 		dp->who = DCCP_LISTENER;
    261 		dp->seq_snd = 512;
    262 	}
    263 	INP_UNLOCK(inp);
    264 	return error;
    265 }
    266 
    267 int
    268 dccp6_accept(struct socket *so, struct mbuf *m)
    269 {
    270 	struct in6pcb *in6p = NULL;
    271 	int error = 0;
    272 
    273 	DCCP_DEBUG((LOG_INFO, "Entering dccp6_accept!\n"));
    274 	if (m == NULL) {
    275 		return EINVAL;
    276 	}
    277 	if (so->so_state & SS_ISDISCONNECTED) {
    278 		DCCP_DEBUG((LOG_INFO, "so_state && SS_ISDISCONNECTED!, so->state = %i\n", so->so_state));
    279 		return ECONNABORTED;
    280 	}
    281 
    282 	INP_INFO_RLOCK(&dccpbinfo);
    283 	in6p = sotoin6pcb(so);
    284 	if (in6p == 0) {
    285 		INP_INFO_RUNLOCK(&dccpbinfo);
    286 		return EINVAL;
    287 	}
    288 	INP_LOCK(inp);
    289 	INP_INFO_RUNLOCK(&dccpbinfo);
    290 	in6_setpeeraddr(in6p, m);
    291 
    292 	INP_UNLOCK(inp);
    293 	return error;
    294 }
    295 
    296 static int
    297 dccp6_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
    298 {
    299 	int error = 0;
    300 	int family;
    301 
    302 	family = so->so_proto->pr_domain->dom_family;
    303 	switch (family) {
    304 	case PF_INET6:
    305 		error = in6_control(so, cmd, nam, ifp);
    306 		break;
    307 	default:
    308 		error =	 EAFNOSUPPORT;
    309 	}
    310 	return (error);
    311 }
    312 
    313 static int
    314 dccp6_stat(struct socket *so, struct stat *ub)
    315 {
    316 	return 0;
    317 }
    318 
    319 static int
    320 dccp6_purgeif(struct socket *so, struct ifnet *ifp)
    321 {
    322 	int s;
    323 
    324 	s = splsoftnet();
    325 	mutex_enter(softnet_lock);
    326 	in6_pcbpurgeif0(&dccpbtable, ifp);
    327 	in6_purgeif(ifp);
    328 	in6_pcbpurgeif(&dccpbtable, ifp);
    329 	mutex_exit(softnet_lock);
    330 	splx(s);
    331 
    332 	return 0;
    333 }
    334 
    335 int
    336 dccp6_usrreq(struct socket *so, int req, struct mbuf *m,
    337 	     struct mbuf *nam, struct mbuf *control, struct lwp *l)
    338 {
    339 	int error = 0;
    340 
    341 	KASSERT(req != PRU_ATTACH);
    342 	KASSERT(req != PRU_DETACH);
    343 	KASSERT(req != PRU_ACCEPT);
    344 	KASSERT(req != PRU_BIND);
    345 	KASSERT(req != PRU_LISTEN);
    346 	KASSERT(req != PRU_CONNECT);
    347 	KASSERT(req != PRU_CONNECT2);
    348 	KASSERT(req != PRU_DISCONNECT);
    349 	KASSERT(req != PRU_SHUTDOWN);
    350 	KASSERT(req != PRU_ABORT);
    351 	KASSERT(req != PRU_CONTROL);
    352 	KASSERT(req != PRU_SENSE);
    353 	KASSERT(req != PRU_PEERADDR);
    354 	KASSERT(req != PRU_SOCKADDR);
    355 	KASSERT(req != PRU_RCVD);
    356 	KASSERT(req != PRU_RCVOOB);
    357 	KASSERT(req != PRU_SEND);
    358 	KASSERT(req != PRU_SENDOOB);
    359 	KASSERT(req != PRU_PURGEIF);
    360 
    361 	if (sotoin6pcb(so) == NULL) {
    362 		error = EINVAL;
    363 		goto release;
    364 	}
    365 
    366 	switch (req) {
    367 	case PRU_FASTTIMO:
    368 	case PRU_SLOWTIMO:
    369 	case PRU_PROTORCV:
    370 	case PRU_PROTOSEND:
    371 		error = EOPNOTSUPP;
    372 		break;
    373 
    374 	default:
    375 		panic("dccp6_usrreq");
    376 	}
    377 
    378 release:
    379 	if (control != NULL)
    380 		m_freem(control);
    381 	if (m != NULL)
    382 		m_freem(m);
    383 	return error;
    384 }
    385 
    386 static int
    387 dccp6_attach(struct socket *so, int proto)
    388 {
    389 	return dccp_attach(so, proto);
    390 }
    391 
    392 static int
    393 dccp6_detach(struct socket *so)
    394 {
    395 	return dccp_detach(so);
    396 }
    397 
    398 static int
    399 dccp6_connect2(struct socket *so, struct socket *so2)
    400 {
    401 	KASSERT(solocked(so));
    402 
    403 	return EOPNOTSUPP;
    404 }
    405 
    406 static int
    407 dccp6_disconnect(struct socket *so)
    408 {
    409 	return dccp_disconnect(so);
    410 }
    411 
    412 static int
    413 dccp6_shutdown(struct socket *so)
    414 {
    415 	return dccp_shutdown(so);
    416 }
    417 
    418 static int
    419 dccp6_abort(struct socket *so)
    420 {
    421 	return dccp_abort(so);
    422 }
    423 
    424 
    425 static int
    426 dccp6_peeraddr(struct socket *so, struct mbuf *nam)
    427 {
    428 	KASSERT(solocked(so));
    429 	KASSERT(sotoinpcb(so) != NULL);
    430 	KASSERT(nam != NULL);
    431 
    432 	in6_setpeeraddr(sotoin6pcb(so), nam);
    433 	return 0;
    434 }
    435 
    436 static int
    437 dccp6_sockaddr(struct socket *so, struct mbuf *nam)
    438 {
    439 	KASSERT(solocked(so));
    440 	KASSERT(sotoinpcb(so) != NULL);
    441 	KASSERT(nam != NULL);
    442 
    443 	in6_setsockaddr(sotoin6pcb(so), nam);
    444 	return 0;
    445 }
    446 
    447 static int
    448 dccp6_recvoob(struct socket *so, struct mbuf *m, int flags)
    449 {
    450 	KASSERT(solocked(so));
    451 
    452 	return EOPNOTSUPP;
    453 }
    454 
    455 static int
    456 dccp6_rcvd(struct socket *so, int flags, struct lwp *l)
    457 {
    458 	KASSERT(solocked(so));
    459 
    460 	return EOPNOTSUPP;
    461 }
    462 
    463 static int
    464 dccp6_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
    465     struct mbuf *control, struct lwp *l)
    466 {
    467 	return dccp_send(so, m, nam, control, l);
    468 }
    469 
    470 static int
    471 dccp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
    472 {
    473 	KASSERT(solocked(so));
    474 
    475 	m_freem(m);
    476 	m_freem(control);
    477 
    478 	return EOPNOTSUPP;
    479 }
    480 
    481 
    482 PR_WRAP_USRREQS(dccp6)
    483 #define	dccp6_attach		dccp6_attach_wrapper
    484 #define	dccp6_detach		dccp6_detach_wrapper
    485 #define dccp6_accept		dccp6_accept_wrapper
    486 #define	dccp6_bind		dccp6_bind_wrapper
    487 #define	dccp6_listen		dccp6_listen_wrapper
    488 #define	dccp6_connect		dccp6_connect_wrapper
    489 #define	dccp6_connect2		dccp6_connect2_wrapper
    490 #define	dccp6_disconnect	dccp6_disconnect_wrapper
    491 #define	dccp6_shutdown		dccp6_shutdown_wrapper
    492 #define	dccp6_abort		dccp6_abort_wrapper
    493 #define	dccp6_ioctl		dccp6_ioctl_wrapper
    494 #define	dccp6_stat		dccp6_stat_wrapper
    495 #define	dccp6_peeraddr		dccp6_peeraddr_wrapper
    496 #define	dccp6_sockaddr		dccp6_sockaddr_wrapper
    497 #define	dccp6_rcvd		dccp6_rcvd_wrapper
    498 #define	dccp6_recvoob		dccp6_recvoob_wrapper
    499 #define	dccp6_send		dccp6_send_wrapper
    500 #define	dccp6_sendoob		dccp6_sendoob_wrapper
    501 #define	dccp6_purgeif		dccp6_purgeif_wrapper
    502 #define	dccp6_usrreq		dccp6_usrreq_wrapper
    503 
    504 const struct pr_usrreqs dccp6_usrreqs = {
    505 	.pr_attach	= dccp6_attach,
    506 	.pr_detach	= dccp6_detach,
    507 	.pr_accept	= dccp6_accept,
    508 	.pr_bind	= dccp6_bind,
    509 	.pr_listen	= dccp6_listen,
    510 	.pr_connect	= dccp6_connect,
    511 	.pr_connect2	= dccp6_connect2,
    512 	.pr_disconnect	= dccp6_disconnect,
    513 	.pr_shutdown	= dccp6_shutdown,
    514 	.pr_abort	= dccp6_abort,
    515 	.pr_ioctl	= dccp6_ioctl,
    516 	.pr_stat	= dccp6_stat,
    517 	.pr_peeraddr	= dccp6_peeraddr,
    518 	.pr_sockaddr	= dccp6_sockaddr,
    519 	.pr_rcvd	= dccp6_rcvd,
    520 	.pr_recvoob	= dccp6_recvoob,
    521 	.pr_send	= dccp6_send,
    522 	.pr_sendoob	= dccp6_sendoob,
    523 	.pr_purgeif	= dccp6_purgeif,
    524 	.pr_generic	= dccp6_usrreq,
    525 };
    526