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