if_canloop.c revision 1.1.2.2 1 /* $NetBSD: if_canloop.c,v 1.1.2.2 2017/01/16 18:03:38 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.2 2017/01/16 18:03:38 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 struct m_tag *sotag;
166
167 MCLAIM(m, ifp->if_mowner);
168
169 KERNEL_LOCK(1, NULL);
170
171 if ((m->m_flags & M_PKTHDR) == 0)
172 panic("canloop_output: no header mbuf");
173 if (ifp->if_flags & IFF_LOOPBACK)
174 bpf_mtap_af(ifp, AF_CAN, m);
175 m_set_rcvif(m, ifp);
176
177 pktlen = m->m_pkthdr.len;
178 ifp->if_opackets++;
179 ifp->if_obytes += pktlen;
180
181 /* we have to preserve the socket tag */
182 sotag = m_tag_find(m, PACKET_TAG_SO, NULL);
183 m_tag_delete_nonpersistent(m);
184 if (sotag)
185 m_tag_prepend(m, sotag);
186 #ifdef CAN
187 can_input(ifp, m);
188 #else
189 printf("%s: can't handle CAN packet\n", ifp->if_xname);
190 m_freem(m);
191 error = EAFNOSUPPORT;
192 #endif
193
194 KERNEL_UNLOCK_ONE(NULL);
195 return error;
196 }
197
198
199 /*
200 * Process an ioctl request.
201 */
202 /* ARGSUSED */
203 static int
204 canloop_ioctl(struct ifnet *ifp, u_long cmd, void *data)
205 {
206 struct ifreq *ifr = data;
207 int error = 0;
208
209 switch (cmd) {
210
211 case SIOCINITIFADDR:
212 error = EAFNOSUPPORT;
213 break;
214
215 case SIOCSIFMTU:
216 if ((unsigned)ifr->ifr_mtu != sizeof(struct can_frame))
217 error = EINVAL;
218 break;
219
220 case SIOCADDMULTI:
221 case SIOCDELMULTI:
222 error = EAFNOSUPPORT;
223 break;
224
225 default:
226 error = ifioctl_common(ifp, cmd, data);
227 }
228 return (error);
229 }
230
231 /*
232 * Module infrastructure
233 */
234 #include "../net/if_module.h"
235
236 IF_MODULE(MODULE_CLASS_DRIVER, canloop, "")
237