Home | History | Annotate | Line # | Download | only in netcan
if_canloop.c revision 1.4
      1  1.4   ozaki /*	$NetBSD: if_canloop.c,v 1.4 2017/12/06 07:40:16 ozaki-r Exp $	*/
      2  1.2  bouyer 
      3  1.2  bouyer /*-
      4  1.2  bouyer  * Copyright (c) 2017 The NetBSD Foundation, Inc.
      5  1.2  bouyer  * All rights reserved.
      6  1.2  bouyer  *
      7  1.2  bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2  bouyer  * by Manuel Bouyer.
      9  1.2  bouyer  *
     10  1.2  bouyer  * Redistribution and use in source and binary forms, with or without
     11  1.2  bouyer  * modification, are permitted provided that the following conditions
     12  1.2  bouyer  * are met:
     13  1.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     14  1.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     15  1.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     17  1.2  bouyer  *    documentation and/or other materials provided with the distribution.
     18  1.2  bouyer  *
     19  1.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2  bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2  bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2  bouyer  */
     31  1.2  bouyer 
     32  1.2  bouyer 
     33  1.2  bouyer /*
     34  1.2  bouyer  * Loopback interface driver for the CAN protocol
     35  1.2  bouyer  */
     36  1.2  bouyer 
     37  1.2  bouyer #include <sys/cdefs.h>
     38  1.4   ozaki __KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.4 2017/12/06 07:40:16 ozaki-r Exp $");
     39  1.2  bouyer 
     40  1.2  bouyer #ifdef _KERNEL_OPT
     41  1.2  bouyer #include "opt_can.h"
     42  1.2  bouyer #include "opt_net_mpsafe.h"
     43  1.2  bouyer #endif
     44  1.2  bouyer 
     45  1.2  bouyer #include <sys/param.h>
     46  1.2  bouyer #include <sys/systm.h>
     47  1.2  bouyer #include <sys/kernel.h>
     48  1.2  bouyer #include <sys/mbuf.h>
     49  1.2  bouyer #include <sys/socket.h>
     50  1.2  bouyer #include <sys/errno.h>
     51  1.2  bouyer #include <sys/ioctl.h>
     52  1.2  bouyer #include <sys/time.h>
     53  1.2  bouyer #include <sys/device.h>
     54  1.2  bouyer #include <sys/module.h>
     55  1.2  bouyer 
     56  1.2  bouyer #include <sys/cpu.h>
     57  1.2  bouyer 
     58  1.2  bouyer #include <net/if.h>
     59  1.2  bouyer #include <net/if_types.h>
     60  1.2  bouyer #include <net/netisr.h>
     61  1.2  bouyer 
     62  1.2  bouyer #ifdef	CAN
     63  1.2  bouyer #include <netcan/can.h>
     64  1.2  bouyer #endif
     65  1.2  bouyer 
     66  1.2  bouyer void canloopattach(int);
     67  1.2  bouyer void canloopinit(void);
     68  1.2  bouyer static int	canloop_clone_create(struct if_clone *, int);
     69  1.2  bouyer static int	canloop_clone_destroy(struct ifnet *);
     70  1.2  bouyer static int	canloop_ioctl(struct ifnet *, u_long, void *);
     71  1.2  bouyer static void	canloop_ifstart(struct ifnet *);
     72  1.2  bouyer 
     73  1.2  bouyer static int	canloop_count;
     74  1.2  bouyer 
     75  1.2  bouyer static struct if_clone canloop_cloner =
     76  1.2  bouyer     IF_CLONE_INITIALIZER("canlo", canloop_clone_create, canloop_clone_destroy);
     77  1.2  bouyer 
     78  1.2  bouyer void
     79  1.2  bouyer canloopattach(int n)
     80  1.2  bouyer {
     81  1.2  bouyer 
     82  1.2  bouyer 	/*
     83  1.2  bouyer 	 * Nothing to do here, initialization is handled by the
     84  1.2  bouyer 	 * module initialization code in canloopinit() below).
     85  1.2  bouyer 	 */
     86  1.2  bouyer }
     87  1.2  bouyer 
     88  1.2  bouyer void
     89  1.2  bouyer canloopinit(void)
     90  1.2  bouyer {
     91  1.2  bouyer 
     92  1.2  bouyer 	canloop_count = 0;
     93  1.2  bouyer 	if_clone_attach(&canloop_cloner);
     94  1.2  bouyer }
     95  1.2  bouyer 
     96  1.2  bouyer static int
     97  1.2  bouyer canloopdetach(void)
     98  1.2  bouyer {
     99  1.2  bouyer 	if (canloop_count > 0)
    100  1.2  bouyer 		return EBUSY;
    101  1.2  bouyer 	if_clone_detach(&canloop_cloner);
    102  1.2  bouyer 	return 0;
    103  1.2  bouyer }
    104  1.2  bouyer 
    105  1.2  bouyer static int
    106  1.2  bouyer canloop_clone_create(struct if_clone *ifc, int unit)
    107  1.2  bouyer {
    108  1.2  bouyer 	struct ifnet *ifp;
    109  1.2  bouyer 
    110  1.2  bouyer 	ifp = if_alloc(IFT_OTHER);
    111  1.2  bouyer 
    112  1.2  bouyer 	if_initname(ifp, ifc->ifc_name, unit);
    113  1.2  bouyer 
    114  1.4   ozaki 	ifp->if_flags = IFF_LOOPBACK;
    115  1.3   ozaki 	ifp->if_extflags = IFEF_MPSAFE;
    116  1.2  bouyer 	ifp->if_ioctl = canloop_ioctl;
    117  1.2  bouyer 	ifp->if_start = canloop_ifstart;
    118  1.2  bouyer 	can_ifattach(ifp);
    119  1.2  bouyer #ifdef MBUFTRACE
    120  1.2  bouyer 	ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
    121  1.2  bouyer 	    M_WAITOK | M_ZERO);
    122  1.2  bouyer 	strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
    123  1.2  bouyer 	    sizeof(ifp->if_mowner->mo_name));
    124  1.2  bouyer 	MOWNER_ATTACH(ifp->if_mowner);
    125  1.2  bouyer #endif
    126  1.2  bouyer 	canloop_count++;
    127  1.4   ozaki 	ifp->if_flags |= IFF_RUNNING;
    128  1.2  bouyer 
    129  1.2  bouyer 	return (0);
    130  1.2  bouyer }
    131  1.2  bouyer 
    132  1.2  bouyer static int
    133  1.2  bouyer canloop_clone_destroy(struct ifnet *ifp)
    134  1.2  bouyer {
    135  1.2  bouyer 
    136  1.4   ozaki 	ifp->if_flags &= ~IFF_RUNNING;
    137  1.4   ozaki 
    138  1.2  bouyer #ifdef MBUFTRACE
    139  1.2  bouyer 	MOWNER_DETACH(ifp->if_mowner);
    140  1.2  bouyer 	free(ifp->if_mowner, M_DEVBUF);
    141  1.2  bouyer #endif
    142  1.2  bouyer 
    143  1.2  bouyer 	can_ifdetach(ifp);
    144  1.2  bouyer 
    145  1.2  bouyer 	if_free(ifp);
    146  1.2  bouyer 	canloop_count--;
    147  1.2  bouyer 	KASSERT(canloop_count >= 0);
    148  1.2  bouyer 	return (0);
    149  1.2  bouyer }
    150  1.2  bouyer 
    151  1.2  bouyer static void
    152  1.2  bouyer canloop_ifstart(struct ifnet *ifp)
    153  1.2  bouyer {
    154  1.2  bouyer 	size_t pktlen;
    155  1.2  bouyer 	struct mbuf *m;
    156  1.2  bouyer 
    157  1.2  bouyer 	KERNEL_LOCK(1, NULL);
    158  1.2  bouyer 	while (true) {
    159  1.2  bouyer 		IF_DEQUEUE(&ifp->if_snd, m);
    160  1.2  bouyer 		if (m == NULL)
    161  1.2  bouyer 			break;
    162  1.2  bouyer 		MCLAIM(m, ifp->if_mowner);
    163  1.2  bouyer 
    164  1.2  bouyer 		if ((m->m_flags & M_PKTHDR) == 0)
    165  1.2  bouyer 			panic("canloop_output: no header mbuf");
    166  1.2  bouyer 		m_set_rcvif(m, ifp);
    167  1.2  bouyer 		if (ifp->if_flags & IFF_LOOPBACK)
    168  1.2  bouyer 			can_bpf_mtap(ifp, m, 0);
    169  1.2  bouyer 
    170  1.2  bouyer 		pktlen = m->m_pkthdr.len;
    171  1.2  bouyer 		ifp->if_opackets++;
    172  1.2  bouyer 		ifp->if_obytes += pktlen;
    173  1.2  bouyer 
    174  1.2  bouyer #ifdef CAN
    175  1.2  bouyer 		can_mbuf_tag_clean(m);
    176  1.2  bouyer 		can_input(ifp, m);
    177  1.2  bouyer #else
    178  1.2  bouyer 		printf("%s: can't handle CAN packet\n", ifp->if_xname);
    179  1.2  bouyer 		m_freem(m);
    180  1.2  bouyer #endif
    181  1.2  bouyer 	}
    182  1.2  bouyer 
    183  1.2  bouyer 	KERNEL_UNLOCK_ONE(NULL);
    184  1.2  bouyer }
    185  1.2  bouyer 
    186  1.2  bouyer /*
    187  1.2  bouyer  * Process an ioctl request.
    188  1.2  bouyer  */
    189  1.2  bouyer /* ARGSUSED */
    190  1.2  bouyer static int
    191  1.2  bouyer canloop_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    192  1.2  bouyer {
    193  1.2  bouyer 	struct ifreq *ifr = data;
    194  1.2  bouyer 	int error = 0;
    195  1.2  bouyer 
    196  1.2  bouyer 	switch (cmd) {
    197  1.2  bouyer 
    198  1.2  bouyer 	case SIOCINITIFADDR:
    199  1.2  bouyer 		error = EAFNOSUPPORT;
    200  1.2  bouyer 		break;
    201  1.2  bouyer 
    202  1.2  bouyer 	case SIOCSIFMTU:
    203  1.2  bouyer 		if ((unsigned)ifr->ifr_mtu != sizeof(struct can_frame))
    204  1.2  bouyer 			error = EINVAL;
    205  1.2  bouyer 		break;
    206  1.2  bouyer 
    207  1.2  bouyer 	case SIOCADDMULTI:
    208  1.2  bouyer 	case SIOCDELMULTI:
    209  1.2  bouyer 		error = EAFNOSUPPORT;
    210  1.2  bouyer 		break;
    211  1.2  bouyer 
    212  1.2  bouyer 	default:
    213  1.2  bouyer 		error = ifioctl_common(ifp, cmd, data);
    214  1.2  bouyer 	}
    215  1.2  bouyer 	return (error);
    216  1.2  bouyer }
    217  1.2  bouyer 
    218  1.2  bouyer /*
    219  1.2  bouyer  * Module infrastructure
    220  1.2  bouyer  */
    221  1.2  bouyer #include "../net/if_module.h"
    222  1.2  bouyer 
    223  1.2  bouyer IF_MODULE(MODULE_CLASS_DRIVER, canloop, "")
    224