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