Home | History | Annotate | Line # | Download | only in netcan
if_canloop.c revision 1.1.2.1
      1 /*	$NetBSD: if_canloop.c,v 1.1.2.1 2017/01/15 20:27: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.1 2017/01/15 20:27: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 int	canloop_output(struct ifnet *,
     73 	struct mbuf *, const struct sockaddr *, const struct rtentry *);
     74 
     75 static int	canloop_count;
     76 
     77 static struct if_clone canloop_cloner =
     78     IF_CLONE_INITIALIZER("canlo", canloop_clone_create, canloop_clone_destroy);
     79 
     80 void
     81 canloopattach(int n)
     82 {
     83 
     84 	/*
     85 	 * Nothing to do here, initialization is handled by the
     86 	 * module initialization code in canloopnit() below).
     87 	 */
     88 }
     89 
     90 void
     91 canloopinit(void)
     92 {
     93 
     94 	canloop_count = 0;
     95 	if_clone_attach(&canloop_cloner);
     96 }
     97 
     98 static int
     99 canloopdetach(void)
    100 {
    101 	if (canloop_count > 0)
    102 		return EBUSY;
    103 	if_clone_detach(&canloop_cloner);
    104 	return 0;
    105 }
    106 
    107 static int
    108 canloop_clone_create(struct if_clone *ifc, int unit)
    109 {
    110 	struct ifnet *ifp;
    111 
    112 	ifp = if_alloc(IFT_OTHER);
    113 
    114 	if_initname(ifp, ifc->ifc_name, unit);
    115 
    116 	ifp->if_mtu = sizeof(struct can_frame);
    117 	ifp->if_flags = IFF_LOOPBACK | IFF_RUNNING;
    118 	ifp->if_extflags = IFEF_OUTPUT_MPSAFE;
    119 	ifp->if_ioctl = canloop_ioctl;
    120 	ifp->if_output = canloop_output;
    121 	ifp->if_type = IFT_OTHER;
    122 	ifp->if_hdrlen = 0;
    123 	ifp->if_addrlen = 0;
    124 	ifp->if_dlt = DLT_CAN_SOCKETCAN;
    125 	IFQ_SET_READY(&ifp->if_snd);
    126 	if_attach(ifp);
    127 	if_alloc_sadl(ifp);
    128 	bpf_attach(ifp, DLT_CAN_SOCKETCAN, sizeof(u_int));
    129 #ifdef MBUFTRACE
    130 	ifp->if_mowner = malloc(sizeof(struct mowner), M_DEVBUF,
    131 	    M_WAITOK | M_ZERO);
    132 	strlcpy(ifp->if_mowner->mo_name, ifp->if_xname,
    133 	    sizeof(ifp->if_mowner->mo_name));
    134 	MOWNER_ATTACH(ifp->if_mowner);
    135 #endif
    136 	canloop_count++;
    137 
    138 	return (0);
    139 }
    140 
    141 static int
    142 canloop_clone_destroy(struct ifnet *ifp)
    143 {
    144 
    145 #ifdef MBUFTRACE
    146 	MOWNER_DETACH(ifp->if_mowner);
    147 	free(ifp->if_mowner, M_DEVBUF);
    148 #endif
    149 
    150 	bpf_detach(ifp);
    151 	if_detach(ifp);
    152 
    153 	if_free(ifp);
    154 	canloop_count--;
    155 	KASSERT(canloop_count >= 0);
    156 	return (0);
    157 }
    158 
    159 static int
    160 canloop_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    161     const struct rtentry *rt)
    162 {
    163 	int error = 0;
    164 	size_t pktlen;
    165 
    166 	MCLAIM(m, ifp->if_mowner);
    167 
    168 	KERNEL_LOCK(1, NULL);
    169 
    170 	if ((m->m_flags & M_PKTHDR) == 0)
    171 		panic("canloop_output: no header mbuf");
    172 	if (ifp->if_flags & IFF_LOOPBACK)
    173 		bpf_mtap_af(ifp, AF_CAN, m);
    174 	m_set_rcvif(m, ifp);
    175 
    176 	pktlen = m->m_pkthdr.len;
    177 	ifp->if_opackets++;
    178 	ifp->if_obytes += pktlen;
    179 
    180 	m_tag_delete_nonpersistent(m);
    181 
    182 #ifdef CAN
    183 	can_input(ifp, m);
    184 #else
    185 	printf("%s: can't handle CAN packet\n", ifp->if_xname);
    186 	m_freem(m);
    187 	error = EAFNOSUPPORT;
    188 #endif
    189 
    190 	KERNEL_UNLOCK_ONE(NULL);
    191 	return error;
    192 }
    193 
    194 
    195 /*
    196  * Process an ioctl request.
    197  */
    198 /* ARGSUSED */
    199 static int
    200 canloop_ioctl(struct ifnet *ifp, u_long cmd, void *data)
    201 {
    202 	struct ifreq *ifr = data;
    203 	int error = 0;
    204 
    205 	switch (cmd) {
    206 
    207 	case SIOCINITIFADDR:
    208 		error = EAFNOSUPPORT;
    209 		break;
    210 
    211 	case SIOCSIFMTU:
    212 		if ((unsigned)ifr->ifr_mtu != sizeof(struct can_frame))
    213 			error = EINVAL;
    214 		break;
    215 
    216 	case SIOCADDMULTI:
    217 	case SIOCDELMULTI:
    218 		error = EAFNOSUPPORT;
    219 		break;
    220 
    221 	default:
    222 		error = ifioctl_common(ifp, cmd, data);
    223 	}
    224 	return (error);
    225 }
    226 
    227 /*
    228  * Module infrastructure
    229  */
    230 #include "../net/if_module.h"
    231 
    232 IF_MODULE(MODULE_CLASS_DRIVER, canloop, "")
    233