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