Home | History | Annotate | Line # | Download | only in netcan
can_pcb.c revision 1.5
      1 /*	$NetBSD: can_pcb.c,v 1.5 2017/06/01 02:45:14 chs 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.5 2017/06/01 02:45:14 chs Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/malloc.h>
     38 #include <sys/kmem.h>
     39 #include <sys/mbuf.h>
     40 #include <sys/protosw.h>
     41 #include <sys/socket.h>
     42 #include <sys/socketvar.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/errno.h>
     45 #include <sys/time.h>
     46 #include <sys/pool.h>
     47 #include <sys/proc.h>
     48 
     49 #include <net/if.h>
     50 #include <net/route.h>
     51 
     52 #include <netcan/can.h>
     53 #include <netcan/can_var.h>
     54 #include <netcan/can_pcb.h>
     55 
     56 #define	CANPCBHASH_BIND(table, ifindex) \
     57 	&(table)->canpt_bindhashtbl[ \
     58 	    (ifindex) & (table)->canpt_bindhash]
     59 #define	CANPCBHASH_CONNECT(table, ifindex) \
     60 	&(table)->canpt_connecthashtbl[ \
     61 	    (ifindex) & (table)->canpt_bindhash]
     62 
     63 struct pool canpcb_pool;
     64 
     65 void
     66 can_pcbinit(struct canpcbtable *table, int bindhashsize, int connecthashsize)
     67 {
     68 	static int canpcb_pool_initialized;
     69 
     70 	if (canpcb_pool_initialized == 0) {
     71 		pool_init(&canpcb_pool, sizeof(struct canpcb), 0, 0, 0,
     72 		    "canpcbpl", NULL, IPL_SOFTNET);
     73 		canpcb_pool_initialized = 1;
     74 	}
     75 
     76 	TAILQ_INIT(&table->canpt_queue);
     77 	table->canpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, true,
     78 	    &table->canpt_bindhash);
     79 	table->canpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST,
     80 	    true, &table->canpt_connecthash);
     81 }
     82 
     83 int
     84 can_pcballoc(struct socket *so, void *v)
     85 {
     86 	struct canpcbtable *table = v;
     87 	struct canpcb *canp;
     88 	struct can_filter *can_init_filter;
     89 	int s;
     90 
     91 	can_init_filter = kmem_alloc(sizeof(struct can_filter), KM_NOSLEEP);
     92 	if (can_init_filter == NULL)
     93 		return (ENOBUFS);
     94 	can_init_filter->can_id = 0;
     95 	can_init_filter->can_mask = 0; /* accept all by default */
     96 
     97 	s = splnet();
     98 	canp = pool_get(&canpcb_pool, PR_NOWAIT);
     99 	splx(s);
    100 	if (canp == NULL) {
    101 		kmem_free(can_init_filter, sizeof(struct can_filter));
    102 		return (ENOBUFS);
    103 	}
    104 	memset(canp, 0, sizeof(*canp));
    105 	canp->canp_table = table;
    106 	canp->canp_socket = so;
    107 	canp->canp_filters = can_init_filter;
    108 	canp->canp_nfilters = 1;
    109 	mutex_init(&canp->canp_mtx, MUTEX_DEFAULT, IPL_NET);
    110 	canp->canp_refcount = 1;
    111 
    112 	so->so_pcb = canp;
    113 	mutex_enter(&canp->canp_mtx);
    114 	TAILQ_INSERT_HEAD(&table->canpt_queue, canp, canp_queue);
    115 	can_pcbstate(canp, CANP_ATTACHED);
    116 	mutex_exit(&canp->canp_mtx);
    117 	return (0);
    118 }
    119 
    120 int
    121 can_pcbbind(void *v, struct sockaddr_can *scan, struct lwp *l)
    122 {
    123 	struct canpcb *canp = v;
    124 
    125 	if (scan->can_family != AF_CAN)
    126 		return (EAFNOSUPPORT);
    127 	mutex_enter(&canp->canp_mtx);
    128 	if (scan->can_ifindex != 0) {
    129 		canp->canp_ifp = if_byindex(scan->can_ifindex);
    130 		if (canp->canp_ifp == NULL)
    131 			return (EADDRNOTAVAIL);
    132 		soisconnected(canp->canp_socket);
    133 	} else {
    134 		canp->canp_ifp = NULL;
    135 		canp->canp_socket->so_state &= ~SS_ISCONNECTED;	/* XXX */
    136 	}
    137 	can_pcbstate(canp, CANP_BOUND);
    138 	mutex_exit(&canp->canp_mtx);
    139 	return 0;
    140 }
    141 
    142 /*
    143  * Connect from a socket to a specified address.
    144  */
    145 int
    146 can_pcbconnect(void *v, struct sockaddr_can *scan)
    147 {
    148 #if 0
    149 	struct canpcb *canp = v;
    150 	struct sockaddr_can *ifaddr = NULL;
    151 	int error;
    152 #endif
    153 
    154 	if (scan->can_family != AF_CAN)
    155 		return (EAFNOSUPPORT);
    156 #if 0
    157 	mutex_enter(&canp->canp_mtx);
    158 	memcpy(&canp->canp_dst, scan, sizeof(struct sockaddr_can));
    159 	can_pcbstate(canp, CANP_CONNECTED);
    160 	mutex_exit(&canp->canp_mtx);
    161 	return 0;
    162 #endif
    163 	return EOPNOTSUPP;
    164 }
    165 
    166 void
    167 can_pcbdisconnect(void *v)
    168 {
    169 	struct canpcb *canp = v;
    170 
    171 	mutex_enter(&canp->canp_mtx);
    172 	can_pcbstate(canp, CANP_DETACHED);
    173 	mutex_exit(&canp->canp_mtx);
    174 	if (canp->canp_socket->so_state & SS_NOFDREF)
    175 		can_pcbdetach(canp);
    176 }
    177 
    178 void
    179 can_pcbdetach(void *v)
    180 {
    181 	struct canpcb *canp = v;
    182 	struct socket *so = canp->canp_socket;
    183 
    184 	KASSERT(mutex_owned(softnet_lock));
    185 	so->so_pcb = NULL;
    186 	mutex_enter(&canp->canp_mtx);
    187 	can_pcbstate(canp, CANP_DETACHED);
    188 	can_pcbsetfilter(canp, NULL, 0);
    189 	mutex_exit(&canp->canp_mtx);
    190 	TAILQ_REMOVE(&canp->canp_table->canpt_queue, canp, canp_queue);
    191 	sofree(so); /* sofree drops the softnet_lock */
    192 	canp_unref(canp);
    193 	mutex_enter(softnet_lock);
    194 }
    195 
    196 void
    197 canp_ref(struct canpcb *canp)
    198 {
    199 	KASSERT(mutex_owned(&canp->canp_mtx));
    200 	canp->canp_refcount++;
    201 }
    202 
    203 void
    204 canp_unref(struct canpcb *canp)
    205 {
    206 	mutex_enter(&canp->canp_mtx);
    207 	canp->canp_refcount--;
    208 	KASSERT(canp->canp_refcount >= 0);
    209 	if (canp->canp_refcount > 0) {
    210 		mutex_exit(&canp->canp_mtx);
    211 		return;
    212 	}
    213 	mutex_exit(&canp->canp_mtx);
    214 	mutex_destroy(&canp->canp_mtx);
    215 	pool_put(&canpcb_pool, canp);
    216 }
    217 
    218 void
    219 can_setsockaddr(struct canpcb *canp, struct sockaddr_can *scan)
    220 {
    221 
    222 	mutex_enter(&canp->canp_mtx);
    223 	memset(scan, 0, sizeof (*scan));
    224 	scan->can_family = AF_CAN;
    225 	scan->can_len = sizeof(*scan);
    226 	if (canp->canp_ifp)
    227 		scan->can_ifindex = canp->canp_ifp->if_index;
    228 	else
    229 		scan->can_ifindex = 0;
    230 	mutex_exit(&canp->canp_mtx);
    231 }
    232 
    233 int
    234 can_pcbsetfilter(struct canpcb *canp, struct can_filter *fp, int nfilters)
    235 {
    236 
    237 	struct can_filter *newf;
    238 	KASSERT(mutex_owned(&canp->canp_mtx));
    239 
    240 	if (nfilters > 0) {
    241 		newf =
    242 		    kmem_alloc(sizeof(struct can_filter) * nfilters, KM_SLEEP);
    243 		memcpy(newf, fp, sizeof(struct can_filter) * nfilters);
    244 	} else {
    245 		newf = NULL;
    246 	}
    247 	if (canp->canp_filters != NULL) {
    248 		kmem_free(canp->canp_filters,
    249 		    sizeof(struct can_filter) * canp->canp_nfilters);
    250 	}
    251 	canp->canp_filters = newf;
    252 	canp->canp_nfilters = nfilters;
    253 	return 0;
    254 }
    255 
    256 
    257 
    258 #if 0
    259 /*
    260  * Pass some notification to all connections of a protocol
    261  * associated with address dst.  The local address and/or port numbers
    262  * may be specified to limit the search.  The "usual action" will be
    263  * taken, depending on the ctlinput cmd.  The caller must filter any
    264  * cmds that are uninteresting (e.g., no error in the map).
    265  * Call the protocol specific routine (if any) to report
    266  * any errors for each matching socket.
    267  *
    268  * Must be called at splsoftnet.
    269  */
    270 int
    271 can_pcbnotify(struct canpcbtable *table, u_int32_t faddr, u_int32_t laddr,
    272     int errno, void (*notify)(struct canpcb *, int))
    273 {
    274 	struct canpcbhead *head;
    275 	struct canpcb *canp, *ncanp;
    276 	int nmatch;
    277 
    278 	if (faddr == 0 || notify == 0)
    279 		return (0);
    280 
    281 	nmatch = 0;
    282 	head = CANPCBHASH_CONNECT(table, faddr, laddr);
    283 	for (canp = LIST_FIRST(head); canp != NULL; canp = ncanp) {
    284 		ncanp = LIST_NEXT(canp, canp_hash);
    285 		if (canp->canp_faddr == faddr &&
    286 		    canp->canp_laddr == laddr) {
    287 			(*notify)(canp, errno);
    288 			nmatch++;
    289 		}
    290 	}
    291 	return (nmatch);
    292 }
    293 
    294 void
    295 can_pcbnotifyall(struct canpcbtable *table, u_int32_t faddr, int errno,
    296     void (*notify)(struct canpcb *, int))
    297 {
    298 	struct canpcb *canp, *ncanp;
    299 
    300 	if (faddr == 0 || notify == 0)
    301 		return;
    302 
    303 	TAILQ_FOREACH_SAFE(canp, &table->canpt_queue, canp_queue, ncanp) {
    304 		if (canp->canp_faddr == faddr)
    305 			(*notify)(canp, errno);
    306 	}
    307 }
    308 #endif
    309 
    310 #if 0
    311 void
    312 can_pcbpurgeif0(struct canpcbtable *table, struct ifnet *ifp)
    313 {
    314 	struct canpcb *canp, *ncanp;
    315 	struct ip_moptions *imo;
    316 	int i, gap;
    317 
    318 }
    319 
    320 void
    321 can_pcbpurgeif(struct canpcbtable *table, struct ifnet *ifp)
    322 {
    323 	struct canpcb *canp, *ncanp;
    324 
    325 	for (canp = CIRCLEQ_FIRST(&table->canpt_queue);
    326 	    canp != (void *)&table->canpt_queue;
    327 	    canp = ncanp) {
    328 		ncanp = CIRCLEQ_NEXT(canp, canp_queue);
    329 	}
    330 }
    331 #endif
    332 
    333 
    334 
    335 void
    336 can_pcbstate(struct canpcb *canp, int state)
    337 {
    338 	int ifindex = canp->canp_ifp ? canp->canp_ifp->if_index : 0;
    339 	KASSERT(mutex_owned(&canp->canp_mtx));
    340 
    341 	if (canp->canp_state > CANP_ATTACHED)
    342 		LIST_REMOVE(canp, canp_hash);
    343 
    344 	switch (state) {
    345 	case CANP_BOUND:
    346 		LIST_INSERT_HEAD(CANPCBHASH_BIND(canp->canp_table,
    347 		    ifindex), canp, canp_hash);
    348 		break;
    349 	case CANP_CONNECTED:
    350 		LIST_INSERT_HEAD(CANPCBHASH_CONNECT(canp->canp_table,
    351 		    ifindex), canp, canp_hash);
    352 		break;
    353 	}
    354 
    355 	canp->canp_state = state;
    356 }
    357 
    358 /*
    359  * check mbuf against socket accept filter.
    360  * returns true if mbuf is accepted, false otherwise
    361  */
    362 bool
    363 can_pcbfilter(struct canpcb *canp, struct mbuf *m)
    364 {
    365 	int i;
    366 	struct can_frame *fmp;
    367 	struct can_filter *fip;
    368 
    369 	KASSERT(mutex_owned(&canp->canp_mtx));
    370 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    371 	KASSERT(m->m_len == m->m_pkthdr.len);
    372 
    373 	fmp = mtod(m, struct can_frame *);
    374 	for (i = 0; i < canp->canp_nfilters; i++) {
    375 		fip = &canp->canp_filters[i];
    376 		if ((fmp->can_id & fip->can_mask) == fip->can_id)
    377 			return true;
    378 	}
    379 	/* no match */
    380 	return false;
    381 }
    382