Home | History | Annotate | Line # | Download | only in netcan
if_canloop.c revision 1.1.2.6
      1 /*	$NetBSD: if_canloop.c,v 1.1.2.6 2017/04/24 13:38:33 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2017 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by 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 
     33 /*
     34  * Loopback interface driver for the CAN protocol
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: if_canloop.c,v 1.1.2.6 2017/04/24 13:38:33 bouyer Exp $");
     39 
     40 #ifdef _KERNEL_OPT
     41 #include "opt_can.h"
     42 #include "opt_net_mpsafe.h"
     43 #endif
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/socket.h>
     50 #include <sys/errno.h>
     51 #include <sys/ioctl.h>
     52 #include <sys/time.h>
     53 #include <sys/device.h>
     54 #include <sys/module.h>
     55 
     56 #include <sys/cpu.h>
     57 
     58 #include <net/if.h>
     59 #include <net/if_types.h>
     60 #include <net/netisr.h>
     61 
     62 #ifdef	CAN
     63 #include <netcan/can.h>
     64 #endif
     65 #include <net/bpf.h>
     66 
     67 void canloopattach(int);
     68 void canloopinit(void);
     69 static int	canloop_clone_create(struct if_clone *, int);
     70 static int	canloop_clone_destroy(struct ifnet *);
     71 static int	canloop_ioctl(struct ifnet *, u_long, void *);
     72 static void	canloop_ifstart(struct ifnet *);
     73 
     74 static int	canloop_count;
     75 
     76 static struct if_clone canloop_cloner =
     77     IF_CLONE_INITIALIZER("canlo", canloop_clone_create, canloop_clone_destroy);
     78 
     79 void
     80 canloopattach(int n)
     81 {
     82 
     83 	/*
     84 	 * Nothing to do here, initialization is handled by the
     85 	 * module initialization code in canloopinit() below).
     86 	 */
     87 }
     88 
     89 void
     90 canloopinit(void)
     91 {
     92 
     93 	canloop_count = 0;
     94 	if_clone_attach(&canloop_cloner);
     95 }
     96 
     97 static int
     98 canloopdetach(void)
     99 {
    100 	if (canloop_count > 0)
    101 		return EBUSY;
    102 	if_clone_detach(&canloop_cloner);
    103 	return 0;
    104 }
    105 
    106 static int
    107 canloop_clone_create(struct if_clone *ifc, int unit)
    108 {
    109 	struct ifnet *ifp;
    110 
    111 	ifp = if_alloc(IFT_OTHER);
    112 
    113 	if_initname(ifp, ifc->ifc_name, unit);
    114 
    115 	ifp->if_flags = IFF_LOOPBACK | IFF_RUNNING;
    116 	ifp->if_extflags = IFEF_OUTPUT_MPSAFE;
    117 	ifp->if_ioctl = canloop_ioctl;
    118 	ifp->if_start = canloop_ifstart;
    119 	if_attach(ifp);
    120 	can_ifattach(ifp);
    121 	bpf_attach(ifp, DLT_CAN_SOCKETCAN, 0);
    122 #ifdef MBUFTRACE
    123 	ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
    124 	    M_WAITOK | M_ZERO);
    125 	strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
    126 	    sizeof(ifp->if_mowner->mo_name));
    127 	MOWNER_ATTACH(ifp->if_mowner);
    128 #endif
    129 	canloop_count++;
    130 
    131 	return (0);
    132 }
    133 
    134 static int
    135 canloop_clone_destroy(struct ifnet *ifp)
    136 {
    137 
    138 #ifdef MBUFTRACE
    139 	MOWNER_DETACH(ifp->if_mowner);
    140 	free(ifp->if_mowner, M_DEVBUF);
    141 #endif
    142 
    143 	bpf_detach(ifp);
    144 	if_detach(ifp);
    145 
    146 	if_free(ifp);
    147 	canloop_count--;
    148 	KASSERT(canloop_count >= 0);
    149 	return (0);
    150 }
    151 
    152 static void
    153 canloop_ifstart(struct ifnet *ifp)
    154 {
    155 	size_t pktlen;
    156 	struct mbuf *m;
    157 
    158 	KERNEL_LOCK(1, NULL);
    159 	while (true) {
    160 		IF_DEQUEUE(&ifp->if_snd, m);
    161 		if (m == NULL)
    162 			break;
    163 		MCLAIM(m, ifp->if_mowner);
    164 
    165 		if ((m->m_flags & M_PKTHDR) == 0)
    166 			panic("canloop_output: no header mbuf");
    167 		m_set_rcvif(m, ifp);
    168 		if (ifp->if_flags & IFF_LOOPBACK)
    169 			bpf_mtap(ifp, m);
    170 
    171 		pktlen = m->m_pkthdr.len;
    172 		ifp->if_opackets++;
    173 		ifp->if_obytes += pktlen;
    174 
    175 #ifdef CAN
    176 		can_mbuf_tag_clean(m);
    177 		can_input(ifp, m);
    178 #else
    179 		printf("%s: can't handle CAN packet\n", ifp->if_xname);
    180 		m_freem(m);
    181 #endif
    182 	}
    183 
    184 	KERNEL_UNLOCK_ONE(NULL);
    185 }
    186 
    187 /*
    188  * Process an ioctl request.
    189  */
    190 /* ARGSUSED */
    191 static int
    192 canloop_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    193 {
    194 	struct ifreq *ifr = data;
    195 	int error = 0;
    196 
    197 	switch (cmd) {
    198 
    199 	case SIOCINITIFADDR:
    200 		error = EAFNOSUPPORT;
    201 		break;
    202 
    203 	case SIOCSIFMTU:
    204 		if ((unsigned)ifr->ifr_mtu != sizeof(struct can_frame))
    205 			error = EINVAL;
    206 		break;
    207 
    208 	case SIOCADDMULTI:
    209 	case SIOCDELMULTI:
    210 		error = EAFNOSUPPORT;
    211 		break;
    212 
    213 	default:
    214 		error = ifioctl_common(ifp, cmd, data);
    215 	}
    216 	return (error);
    217 }
    218 
    219 /*
    220  * Module infrastructure
    221  */
    222 #include "../net/if_module.h"
    223 
    224 IF_MODULE(MODULE_CLASS_DRIVER, canloop, "")
    225