Home | History | Annotate | Line # | Download | only in netcan
can_pcb.c revision 1.1.2.1
      1 /*	$NetBSD: can_pcb.c,v 1.1.2.1 2017/01/15 20:27:33 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003, 2017 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Robert Swindells and Manuel Bouyer
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: can_pcb.c,v 1.1.2.1 2017/01/15 20:27:33 bouyer Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/malloc.h>
     38 #include <sys/mbuf.h>
     39 #include <sys/protosw.h>
     40 #include <sys/socket.h>
     41 #include <sys/socketvar.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/errno.h>
     44 #include <sys/time.h>
     45 #include <sys/pool.h>
     46 #include <sys/proc.h>
     47 
     48 #include <net/if.h>
     49 #include <net/route.h>
     50 
     51 #include <netcan/can.h>
     52 #include <netcan/can_var.h>
     53 #include <netcan/can_pcb.h>
     54 
     55 #define	CANPCBHASH_BIND(table, ifindex) \
     56 	&(table)->canpt_bindhashtbl[ \
     57 	    (ifindex) & (table)->canpt_bindhash]
     58 #define	CANPCBHASH_CONNECT(table, ifindex) \
     59 	&(table)->canpt_connecthashtbl[ \
     60 	    (ifindex) & (table)->canpt_bindhash]
     61 
     62 struct pool canpcb_pool;
     63 
     64 void
     65 can_pcbinit(struct canpcbtable *table, int bindhashsize, int connecthashsize)
     66 {
     67 	static int canpcb_pool_initialized;
     68 
     69 	if (canpcb_pool_initialized == 0) {
     70 		pool_init(&canpcb_pool, sizeof(struct canpcb), 0, 0, 0,
     71 		    "canpcbpl", NULL, IPL_SOFTNET);
     72 		canpcb_pool_initialized = 1;
     73 	}
     74 
     75 	TAILQ_INIT(&table->canpt_queue);
     76 	table->canpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, true,
     77 	    &table->canpt_bindhash);
     78 	table->canpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST,
     79 	    true, &table->canpt_connecthash);
     80 }
     81 
     82 int
     83 can_pcballoc(struct socket *so, void *v)
     84 {
     85 	struct canpcbtable *table = v;
     86 	struct canpcb *canp;
     87 	int s;
     88 
     89 	s = splnet();
     90 	canp = pool_get(&canpcb_pool, PR_NOWAIT);
     91 	splx(s);
     92 	if (canp == NULL)
     93 		return (ENOBUFS);
     94 	memset(canp, 0, sizeof(*canp));
     95 	canp->canp_table = table;
     96 	canp->canp_socket = so;
     97 
     98 	so->so_pcb = canp;
     99 	s = splnet();
    100 	TAILQ_INSERT_HEAD(&table->canpt_queue, canp, canp_queue);
    101 	can_pcbstate(canp, CANP_ATTACHED);
    102 	splx(s);
    103 	return (0);
    104 }
    105 
    106 int
    107 can_pcbbind(void *v, struct sockaddr_can *scan, struct lwp *l)
    108 {
    109 	struct canpcb *canp = v;
    110 
    111 	if (scan->can_family != AF_CAN)
    112 		return (EAFNOSUPPORT);
    113 	if (scan->can_ifindex != 0) {
    114 		canp->canp_ifp = if_byindex(scan->can_ifindex);
    115 		if (canp->canp_ifp == NULL)
    116 			return (EADDRNOTAVAIL);
    117 		soisconnected(canp->canp_socket);
    118 	} else {
    119 		canp->canp_ifp = NULL;
    120 		canp->canp_socket->so_state &= ~SS_ISCONNECTED;	/* XXX */
    121 	}
    122 	can_pcbstate(canp, CANP_BOUND);
    123 	return 0;
    124 }
    125 
    126 /*
    127  * Connect from a socket to a specified address.
    128  */
    129 int
    130 can_pcbconnect(void *v, struct sockaddr_can *scan)
    131 {
    132 #if 0
    133 	struct canpcb *canp = v;
    134 	struct sockaddr_can *ifaddr = NULL;
    135 	int error;
    136 #endif
    137 
    138 	if (scan->can_family != AF_CAN)
    139 		return (EAFNOSUPPORT);
    140 #if 0
    141 	memcpy(&canp->canp_dst, scan, sizeof(struct sockaddr_can));
    142 	can_pcbstate(canp, CANP_CONNECTED);
    143 	return 0;
    144 #endif
    145 	return EOPNOTSUPP;
    146 }
    147 
    148 void
    149 can_pcbdisconnect(void *v)
    150 {
    151 	struct canpcb *canp = v;
    152 
    153 	can_pcbstate(canp, CANP_BOUND);
    154 	if (canp->canp_socket->so_state & SS_NOFDREF)
    155 		can_pcbdetach(canp);
    156 }
    157 
    158 void
    159 can_pcbdetach(void *v)
    160 {
    161 	struct canpcb *canp = v;
    162 	struct socket *so = canp->canp_socket;
    163 	int s;
    164 
    165 	KASSERT(mutex_owned(softnet_lock));
    166 	so->so_pcb = NULL;
    167 	s =  splnet();
    168 	can_pcbstate(canp, CANP_ATTACHED);
    169 	TAILQ_REMOVE(&canp->canp_table->canpt_queue, canp, canp_queue);
    170 	splx(s);
    171 	sofree(so); /* sofree drops the lock */
    172 	pool_put(&canpcb_pool, canp);
    173 	mutex_enter(softnet_lock);
    174 }
    175 
    176 void
    177 can_setsockaddr(struct canpcb *canp, struct sockaddr_can *scan)
    178 {
    179 
    180 	memset(scan, 0, sizeof (*scan));
    181 	scan->can_family = AF_CAN;
    182 	scan->can_len = sizeof(*scan);
    183 	scan->can_ifindex = canp->canp_ifp->if_index;
    184 }
    185 
    186 #if 0
    187 /*
    188  * Pass some notification to all connections of a protocol
    189  * associated with address dst.  The local address and/or port numbers
    190  * may be specified to limit the search.  The "usual action" will be
    191  * taken, depending on the ctlinput cmd.  The caller must filter any
    192  * cmds that are uninteresting (e.g., no error in the map).
    193  * Call the protocol specific routine (if any) to report
    194  * any errors for each matching socket.
    195  *
    196  * Must be called at splsoftnet.
    197  */
    198 int
    199 can_pcbnotify(struct canpcbtable *table, u_int32_t faddr, u_int32_t laddr,
    200     int errno, void (*notify)(struct canpcb *, int))
    201 {
    202 	struct canpcbhead *head;
    203 	struct canpcb *canp, *ncanp;
    204 	int nmatch;
    205 
    206 	if (faddr == 0 || notify == 0)
    207 		return (0);
    208 
    209 	nmatch = 0;
    210 	head = CANPCBHASH_CONNECT(table, faddr, laddr);
    211 	for (canp = LIST_FIRST(head); canp != NULL; canp = ncanp) {
    212 		ncanp = LIST_NEXT(canp, canp_hash);
    213 		if (canp->canp_faddr == faddr &&
    214 		    canp->canp_laddr == laddr) {
    215 			(*notify)(canp, errno);
    216 			nmatch++;
    217 		}
    218 	}
    219 	return (nmatch);
    220 }
    221 
    222 void
    223 can_pcbnotifyall(struct canpcbtable *table, u_int32_t faddr, int errno,
    224     void (*notify)(struct canpcb *, int))
    225 {
    226 	struct canpcb *canp, *ncanp;
    227 
    228 	if (faddr == 0 || notify == 0)
    229 		return;
    230 
    231 	TAILQ_FOREACH_SAFE(canp, &table->canpt_queue, canp_queue, ncanp) {
    232 		if (canp->canp_faddr == faddr)
    233 			(*notify)(canp, errno);
    234 	}
    235 }
    236 #endif
    237 
    238 #if 0
    239 void
    240 can_pcbpurgeif0(struct canpcbtable *table, struct ifnet *ifp)
    241 {
    242 	struct canpcb *canp, *ncanp;
    243 	struct ip_moptions *imo;
    244 	int i, gap;
    245 
    246 }
    247 
    248 void
    249 can_pcbpurgeif(struct canpcbtable *table, struct ifnet *ifp)
    250 {
    251 	struct canpcb *canp, *ncanp;
    252 
    253 	for (canp = CIRCLEQ_FIRST(&table->canpt_queue);
    254 	    canp != (void *)&table->canpt_queue;
    255 	    canp = ncanp) {
    256 		ncanp = CIRCLEQ_NEXT(canp, canp_queue);
    257 	}
    258 }
    259 #endif
    260 
    261 
    262 
    263 void
    264 can_pcbstate(struct canpcb *canp, int state)
    265 {
    266 	int ifindex = canp->canp_ifp ? canp->canp_ifp->if_index : 0;
    267 
    268 	if (canp->canp_state > CANP_ATTACHED)
    269 		LIST_REMOVE(canp, canp_hash);
    270 
    271 	switch (state) {
    272 	case CANP_BOUND:
    273 		LIST_INSERT_HEAD(CANPCBHASH_BIND(canp->canp_table,
    274 		    ifindex), canp, canp_hash);
    275 		break;
    276 	case CANP_CONNECTED:
    277 		LIST_INSERT_HEAD(CANPCBHASH_CONNECT(canp->canp_table,
    278 		    ifindex), canp, canp_hash);
    279 		break;
    280 	}
    281 
    282 	canp->canp_state = state;
    283 }
    284