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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
/* $NetBSD: can_pcb.c,v 1.8 2019/07/20 15:34:41 bouyer Exp $ */
/*-
* Copyright (c) 2003, 2017 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Robert Swindells and 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.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: can_pcb.c,v 1.8 2019/07/20 15:34:41 bouyer Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/kmem.h>
#include <sys/mbuf.h>
#include <sys/protosw.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/pool.h>
#include <sys/proc.h>
#include <net/if.h>
#include <net/route.h>
#include <netcan/can.h>
#include <netcan/can_var.h>
#include <netcan/can_pcb.h>
#define CANPCBHASH_BIND(table, ifindex) \
&(table)->canpt_bindhashtbl[ \
(ifindex) & (table)->canpt_bindhash]
#define CANPCBHASH_CONNECT(table, ifindex) \
&(table)->canpt_connecthashtbl[ \
(ifindex) & (table)->canpt_bindhash]
struct pool canpcb_pool;
void
can_pcbinit(struct canpcbtable *table, int bindhashsize, int connecthashsize)
{
static int canpcb_pool_initialized;
if (canpcb_pool_initialized == 0) {
pool_init(&canpcb_pool, sizeof(struct canpcb), 0, 0, 0,
"canpcbpl", NULL, IPL_SOFTNET);
canpcb_pool_initialized = 1;
}
TAILQ_INIT(&table->canpt_queue);
table->canpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, true,
&table->canpt_bindhash);
table->canpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST,
true, &table->canpt_connecthash);
}
int
can_pcballoc(struct socket *so, void *v)
{
struct canpcbtable *table = v;
struct canpcb *canp;
struct can_filter *can_init_filter;
int s;
can_init_filter = kmem_alloc(sizeof(struct can_filter), KM_NOSLEEP);
if (can_init_filter == NULL)
return (ENOBUFS);
can_init_filter->can_id = 0;
can_init_filter->can_mask = 0; /* accept all by default */
s = splnet();
canp = pool_get(&canpcb_pool, PR_NOWAIT);
splx(s);
if (canp == NULL) {
kmem_free(can_init_filter, sizeof(struct can_filter));
return (ENOBUFS);
}
memset(canp, 0, sizeof(*canp));
canp->canp_table = table;
canp->canp_socket = so;
canp->canp_filters = can_init_filter;
canp->canp_nfilters = 1;
mutex_init(&canp->canp_mtx, MUTEX_DEFAULT, IPL_NET);
canp->canp_refcount = 1;
so->so_pcb = canp;
mutex_enter(&canp->canp_mtx);
TAILQ_INSERT_HEAD(&table->canpt_queue, canp, canp_queue);
can_pcbstate(canp, CANP_ATTACHED);
mutex_exit(&canp->canp_mtx);
return (0);
}
int
can_pcbbind(void *v, struct sockaddr_can *scan, struct lwp *l)
{
struct canpcb *canp = v;
if (scan->can_family != AF_CAN)
return (EAFNOSUPPORT);
if (scan->can_len != sizeof(*scan))
return EINVAL;
mutex_enter(&canp->canp_mtx);
if (scan->can_ifindex != 0) {
canp->canp_ifp = if_byindex(scan->can_ifindex);
if (canp->canp_ifp == NULL ||
canp->canp_ifp->if_dlt != DLT_CAN_SOCKETCAN) {
canp->canp_ifp = NULL;
mutex_exit(&canp->canp_mtx);
return (EADDRNOTAVAIL);
}
soisconnected(canp->canp_socket);
} else {
canp->canp_ifp = NULL;
canp->canp_socket->so_state &= ~SS_ISCONNECTED; /* XXX */
}
can_pcbstate(canp, CANP_BOUND);
mutex_exit(&canp->canp_mtx);
return 0;
}
/*
* Connect from a socket to a specified address.
*/
int
can_pcbconnect(void *v, struct sockaddr_can *scan)
{
#if 0
struct canpcb *canp = v;
struct sockaddr_can *ifaddr = NULL;
int error;
#endif
if (scan->can_family != AF_CAN)
return (EAFNOSUPPORT);
if (scan->can_len != sizeof(*scan))
return EINVAL;
#if 0
mutex_enter(&canp->canp_mtx);
memcpy(&canp->canp_dst, scan, sizeof(struct sockaddr_can));
can_pcbstate(canp, CANP_CONNECTED);
mutex_exit(&canp->canp_mtx);
return 0;
#endif
return EOPNOTSUPP;
}
void
can_pcbdisconnect(void *v)
{
struct canpcb *canp = v;
mutex_enter(&canp->canp_mtx);
can_pcbstate(canp, CANP_DETACHED);
mutex_exit(&canp->canp_mtx);
if (canp->canp_socket->so_state & SS_NOFDREF)
can_pcbdetach(canp);
}
void
can_pcbdetach(void *v)
{
struct canpcb *canp = v;
struct socket *so = canp->canp_socket;
KASSERT(mutex_owned(softnet_lock));
so->so_pcb = NULL;
mutex_enter(&canp->canp_mtx);
can_pcbstate(canp, CANP_DETACHED);
mutex_exit(&canp->canp_mtx);
can_pcbsetfilter(canp, NULL, 0);
TAILQ_REMOVE(&canp->canp_table->canpt_queue, canp, canp_queue);
sofree(so); /* sofree drops the softnet_lock */
canp_unref(canp);
mutex_enter(softnet_lock);
}
void
canp_ref(struct canpcb *canp)
{
KASSERT(mutex_owned(&canp->canp_mtx));
canp->canp_refcount++;
}
void
canp_unref(struct canpcb *canp)
{
mutex_enter(&canp->canp_mtx);
canp->canp_refcount--;
KASSERT(canp->canp_refcount >= 0);
if (canp->canp_refcount > 0) {
mutex_exit(&canp->canp_mtx);
return;
}
mutex_exit(&canp->canp_mtx);
mutex_destroy(&canp->canp_mtx);
pool_put(&canpcb_pool, canp);
}
void
can_setsockaddr(struct canpcb *canp, struct sockaddr_can *scan)
{
mutex_enter(&canp->canp_mtx);
memset(scan, 0, sizeof (*scan));
scan->can_family = AF_CAN;
scan->can_len = sizeof(*scan);
if (canp->canp_ifp)
scan->can_ifindex = canp->canp_ifp->if_index;
else
scan->can_ifindex = 0;
mutex_exit(&canp->canp_mtx);
}
int
can_pcbsetfilter(struct canpcb *canp, struct can_filter *fp, int nfilters)
{
struct can_filter *newf;
struct can_filter *oldf;
int oldnf;
int error = 0;
if (nfilters > 0) {
newf =
kmem_alloc(sizeof(struct can_filter) * nfilters, KM_SLEEP);
memcpy(newf, fp, sizeof(struct can_filter) * nfilters);
} else {
newf = NULL;
}
mutex_enter(&canp->canp_mtx);
oldf = canp->canp_filters;
oldnf = canp->canp_nfilters;
if (newf != NULL && canp->canp_state == CANP_DETACHED) {
error = ECONNRESET;
} else {
canp->canp_filters = newf;
canp->canp_nfilters = nfilters;
newf = NULL;
}
mutex_exit(&canp->canp_mtx);
if (oldf != NULL) {
kmem_free(oldf, sizeof(struct can_filter) * oldnf);
}
if (newf != NULL) {
kmem_free(newf, sizeof(struct can_filter) * nfilters);
}
return error;
}
#if 0
/*
* Pass some notification to all connections of a protocol
* associated with address dst. The local address and/or port numbers
* may be specified to limit the search. The "usual action" will be
* taken, depending on the ctlinput cmd. The caller must filter any
* cmds that are uninteresting (e.g., no error in the map).
* Call the protocol specific routine (if any) to report
* any errors for each matching socket.
*
* Must be called at splsoftnet.
*/
int
can_pcbnotify(struct canpcbtable *table, u_int32_t faddr, u_int32_t laddr,
int errno, void (*notify)(struct canpcb *, int))
{
struct canpcbhead *head;
struct canpcb *canp, *ncanp;
int nmatch;
if (faddr == 0 || notify == 0)
return (0);
nmatch = 0;
head = CANPCBHASH_CONNECT(table, faddr, laddr);
for (canp = LIST_FIRST(head); canp != NULL; canp = ncanp) {
ncanp = LIST_NEXT(canp, canp_hash);
if (canp->canp_faddr == faddr &&
canp->canp_laddr == laddr) {
(*notify)(canp, errno);
nmatch++;
}
}
return (nmatch);
}
void
can_pcbnotifyall(struct canpcbtable *table, u_int32_t faddr, int errno,
void (*notify)(struct canpcb *, int))
{
struct canpcb *canp, *ncanp;
if (faddr == 0 || notify == 0)
return;
TAILQ_FOREACH_SAFE(canp, &table->canpt_queue, canp_queue, ncanp) {
if (canp->canp_faddr == faddr)
(*notify)(canp, errno);
}
}
#endif
#if 0
void
can_pcbpurgeif0(struct canpcbtable *table, struct ifnet *ifp)
{
struct canpcb *canp, *ncanp;
struct ip_moptions *imo;
int i, gap;
}
void
can_pcbpurgeif(struct canpcbtable *table, struct ifnet *ifp)
{
struct canpcb *canp, *ncanp;
for (canp = CIRCLEQ_FIRST(&table->canpt_queue);
canp != (void *)&table->canpt_queue;
canp = ncanp) {
ncanp = CIRCLEQ_NEXT(canp, canp_queue);
}
}
#endif
void
can_pcbstate(struct canpcb *canp, int state)
{
int ifindex = canp->canp_ifp ? canp->canp_ifp->if_index : 0;
KASSERT(mutex_owned(&canp->canp_mtx));
if (canp->canp_state > CANP_ATTACHED)
LIST_REMOVE(canp, canp_hash);
switch (state) {
case CANP_BOUND:
LIST_INSERT_HEAD(CANPCBHASH_BIND(canp->canp_table,
ifindex), canp, canp_hash);
break;
case CANP_CONNECTED:
LIST_INSERT_HEAD(CANPCBHASH_CONNECT(canp->canp_table,
ifindex), canp, canp_hash);
break;
}
canp->canp_state = state;
}
/*
* check mbuf against socket accept filter.
* returns true if mbuf is accepted, false otherwise
*/
bool
can_pcbfilter(struct canpcb *canp, struct mbuf *m)
{
int i;
struct can_frame *fmp;
struct can_filter *fip;
KASSERT(mutex_owned(&canp->canp_mtx));
KASSERT((m->m_flags & M_PKTHDR) != 0);
KASSERT(m->m_len == m->m_pkthdr.len);
fmp = mtod(m, struct can_frame *);
for (i = 0; i < canp->canp_nfilters; i++) {
fip = &canp->canp_filters[i];
if ((fmp->can_id & fip->can_mask) == fip->can_id)
return true;
}
/* no match */
return false;
}
|