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