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