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