Commit | Line | Data |
---|---|---|
0d66548a OH |
1 | /* |
2 | * af_can.c - Protocol family CAN core module | |
3 | * (used by different CAN protocol modules) | |
4 | * | |
5 | * Copyright (c) 2002-2007 Volkswagen Group Electronic Research | |
6 | * All rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. Neither the name of Volkswagen nor the names of its contributors | |
17 | * may be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * Alternatively, provided that this notice is retained in full, this | |
21 | * software may be distributed under the terms of the GNU General | |
22 | * Public License ("GPL") version 2, in which case the provisions of the | |
23 | * GPL apply INSTEAD OF those given above. | |
24 | * | |
25 | * The provided data structures and external interfaces from this code | |
26 | * are not restricted to be used by modules with a GPL compatible license. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
29 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
30 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
31 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
32 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
33 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
34 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
35 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
36 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
37 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
38 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | |
39 | * DAMAGE. | |
40 | * | |
41 | * Send feedback to <socketcan-users@lists.berlios.de> | |
42 | * | |
43 | */ | |
44 | ||
45 | #include <linux/module.h> | |
46 | #include <linux/init.h> | |
47 | #include <linux/kmod.h> | |
48 | #include <linux/slab.h> | |
49 | #include <linux/list.h> | |
50 | #include <linux/spinlock.h> | |
51 | #include <linux/rcupdate.h> | |
52 | #include <linux/uaccess.h> | |
53 | #include <linux/net.h> | |
54 | #include <linux/netdevice.h> | |
55 | #include <linux/socket.h> | |
56 | #include <linux/if_ether.h> | |
57 | #include <linux/if_arp.h> | |
58 | #include <linux/skbuff.h> | |
59 | #include <linux/can.h> | |
60 | #include <linux/can/core.h> | |
61 | #include <net/net_namespace.h> | |
62 | #include <net/sock.h> | |
63 | ||
64 | #include "af_can.h" | |
65 | ||
66 | static __initdata const char banner[] = KERN_INFO | |
67 | "can: controller area network core (" CAN_VERSION_STRING ")\n"; | |
68 | ||
69 | MODULE_DESCRIPTION("Controller Area Network PF_CAN core"); | |
70 | MODULE_LICENSE("Dual BSD/GPL"); | |
71 | MODULE_AUTHOR("Urs Thuermann <urs.thuermann@volkswagen.de>, " | |
72 | "Oliver Hartkopp <oliver.hartkopp@volkswagen.de>"); | |
73 | ||
74 | MODULE_ALIAS_NETPROTO(PF_CAN); | |
75 | ||
76 | static int stats_timer __read_mostly = 1; | |
77 | module_param(stats_timer, int, S_IRUGO); | |
78 | MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)"); | |
79 | ||
20dd3850 OH |
80 | /* receive filters subscribed for 'all' CAN devices */ |
81 | struct dev_rcv_lists can_rx_alldev_list; | |
0d66548a OH |
82 | static DEFINE_SPINLOCK(can_rcvlists_lock); |
83 | ||
84 | static struct kmem_cache *rcv_cache __read_mostly; | |
85 | ||
86 | /* table of registered CAN protocols */ | |
87 | static struct can_proto *proto_tab[CAN_NPROTO] __read_mostly; | |
1ca050d9 | 88 | static DEFINE_MUTEX(proto_tab_lock); |
0d66548a OH |
89 | |
90 | struct timer_list can_stattimer; /* timer for statistics update */ | |
91 | struct s_stats can_stats; /* packet statistics */ | |
92 | struct s_pstats can_pstats; /* receive list statistics */ | |
93 | ||
94 | /* | |
95 | * af_can socket functions | |
96 | */ | |
97 | ||
53914b67 | 98 | int can_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
0d66548a OH |
99 | { |
100 | struct sock *sk = sock->sk; | |
101 | ||
102 | switch (cmd) { | |
103 | ||
104 | case SIOCGSTAMP: | |
105 | return sock_get_timestamp(sk, (struct timeval __user *)arg); | |
106 | ||
107 | default: | |
108 | return -ENOIOCTLCMD; | |
109 | } | |
110 | } | |
53914b67 | 111 | EXPORT_SYMBOL(can_ioctl); |
0d66548a OH |
112 | |
113 | static void can_sock_destruct(struct sock *sk) | |
114 | { | |
115 | skb_queue_purge(&sk->sk_receive_queue); | |
116 | } | |
117 | ||
1ca050d9 OH |
118 | static struct can_proto *can_try_module_get(int protocol) |
119 | { | |
120 | struct can_proto *cp; | |
121 | ||
122 | rcu_read_lock(); | |
123 | cp = rcu_dereference(proto_tab[protocol]); | |
124 | if (cp && !try_module_get(cp->prot->owner)) | |
125 | cp = NULL; | |
126 | rcu_read_unlock(); | |
127 | ||
128 | return cp; | |
129 | } | |
130 | ||
3f378b68 EP |
131 | static int can_create(struct net *net, struct socket *sock, int protocol, |
132 | int kern) | |
0d66548a OH |
133 | { |
134 | struct sock *sk; | |
135 | struct can_proto *cp; | |
0d66548a OH |
136 | int err = 0; |
137 | ||
138 | sock->state = SS_UNCONNECTED; | |
139 | ||
140 | if (protocol < 0 || protocol >= CAN_NPROTO) | |
141 | return -EINVAL; | |
142 | ||
09ad9bc7 | 143 | if (!net_eq(net, &init_net)) |
0d66548a OH |
144 | return -EAFNOSUPPORT; |
145 | ||
1ca050d9 OH |
146 | cp = can_try_module_get(protocol); |
147 | ||
95a5afca | 148 | #ifdef CONFIG_MODULES |
1ca050d9 OH |
149 | if (!cp) { |
150 | /* try to load protocol module if kernel is modular */ | |
151 | ||
5423dd67 | 152 | err = request_module("can-proto-%d", protocol); |
0d66548a OH |
153 | |
154 | /* | |
155 | * In case of error we only print a message but don't | |
156 | * return the error code immediately. Below we will | |
157 | * return -EPROTONOSUPPORT | |
158 | */ | |
5423dd67 UT |
159 | if (err && printk_ratelimit()) |
160 | printk(KERN_ERR "can: request_module " | |
161 | "(can-proto-%d) failed.\n", protocol); | |
1ca050d9 OH |
162 | |
163 | cp = can_try_module_get(protocol); | |
0d66548a | 164 | } |
5423dd67 | 165 | #endif |
0d66548a | 166 | |
0d66548a OH |
167 | /* check for available protocol and correct usage */ |
168 | ||
169 | if (!cp) | |
170 | return -EPROTONOSUPPORT; | |
171 | ||
172 | if (cp->type != sock->type) { | |
1ca050d9 | 173 | err = -EPROTOTYPE; |
0d66548a OH |
174 | goto errout; |
175 | } | |
176 | ||
0d66548a OH |
177 | sock->ops = cp->ops; |
178 | ||
179 | sk = sk_alloc(net, PF_CAN, GFP_KERNEL, cp->prot); | |
180 | if (!sk) { | |
181 | err = -ENOMEM; | |
182 | goto errout; | |
183 | } | |
184 | ||
185 | sock_init_data(sock, sk); | |
186 | sk->sk_destruct = can_sock_destruct; | |
187 | ||
188 | if (sk->sk_prot->init) | |
189 | err = sk->sk_prot->init(sk); | |
190 | ||
191 | if (err) { | |
192 | /* release sk on errors */ | |
193 | sock_orphan(sk); | |
194 | sock_put(sk); | |
195 | } | |
196 | ||
197 | errout: | |
198 | module_put(cp->prot->owner); | |
199 | return err; | |
200 | } | |
201 | ||
202 | /* | |
203 | * af_can tx path | |
204 | */ | |
205 | ||
206 | /** | |
207 | * can_send - transmit a CAN frame (optional with local loopback) | |
208 | * @skb: pointer to socket buffer with CAN frame in data section | |
209 | * @loop: loopback for listeners on local CAN sockets (recommended default!) | |
210 | * | |
481a8199 OH |
211 | * Due to the loopback this routine must not be called from hardirq context. |
212 | * | |
0d66548a OH |
213 | * Return: |
214 | * 0 on success | |
215 | * -ENETDOWN when the selected interface is down | |
216 | * -ENOBUFS on full driver queue (see net_xmit_errno()) | |
217 | * -ENOMEM when local loopback failed at calling skb_clone() | |
218 | * -EPERM when trying to send on a non-CAN interface | |
7f2d38eb | 219 | * -EINVAL when the skb->data does not contain a valid CAN frame |
0d66548a OH |
220 | */ |
221 | int can_send(struct sk_buff *skb, int loop) | |
222 | { | |
c2ab7ac2 | 223 | struct sk_buff *newskb = NULL; |
7f2d38eb | 224 | struct can_frame *cf = (struct can_frame *)skb->data; |
0d66548a OH |
225 | int err; |
226 | ||
7f2d38eb OH |
227 | if (skb->len != sizeof(struct can_frame) || cf->can_dlc > 8) { |
228 | kfree_skb(skb); | |
229 | return -EINVAL; | |
230 | } | |
231 | ||
0d66548a OH |
232 | if (skb->dev->type != ARPHRD_CAN) { |
233 | kfree_skb(skb); | |
234 | return -EPERM; | |
235 | } | |
236 | ||
237 | if (!(skb->dev->flags & IFF_UP)) { | |
238 | kfree_skb(skb); | |
239 | return -ENETDOWN; | |
240 | } | |
241 | ||
242 | skb->protocol = htons(ETH_P_CAN); | |
243 | skb_reset_network_header(skb); | |
244 | skb_reset_transport_header(skb); | |
245 | ||
246 | if (loop) { | |
247 | /* local loopback of sent CAN frames */ | |
248 | ||
249 | /* indication for the CAN driver: do loopback */ | |
250 | skb->pkt_type = PACKET_LOOPBACK; | |
251 | ||
252 | /* | |
253 | * The reference to the originating sock may be required | |
254 | * by the receiving socket to check whether the frame is | |
255 | * its own. Example: can_raw sockopt CAN_RAW_RECV_OWN_MSGS | |
256 | * Therefore we have to ensure that skb->sk remains the | |
257 | * reference to the originating sock by restoring skb->sk | |
258 | * after each skb_clone() or skb_orphan() usage. | |
259 | */ | |
260 | ||
261 | if (!(skb->dev->flags & IFF_ECHO)) { | |
262 | /* | |
263 | * If the interface is not capable to do loopback | |
264 | * itself, we do it here. | |
265 | */ | |
c2ab7ac2 | 266 | newskb = skb_clone(skb, GFP_ATOMIC); |
0d66548a OH |
267 | if (!newskb) { |
268 | kfree_skb(skb); | |
269 | return -ENOMEM; | |
270 | } | |
271 | ||
272 | newskb->sk = skb->sk; | |
273 | newskb->ip_summed = CHECKSUM_UNNECESSARY; | |
274 | newskb->pkt_type = PACKET_BROADCAST; | |
0d66548a OH |
275 | } |
276 | } else { | |
277 | /* indication for the CAN driver: no loopback required */ | |
278 | skb->pkt_type = PACKET_HOST; | |
279 | } | |
280 | ||
281 | /* send to netdevice */ | |
282 | err = dev_queue_xmit(skb); | |
283 | if (err > 0) | |
284 | err = net_xmit_errno(err); | |
285 | ||
c2ab7ac2 | 286 | if (err) { |
ce030edf | 287 | kfree_skb(newskb); |
c2ab7ac2 OH |
288 | return err; |
289 | } | |
290 | ||
291 | if (newskb) | |
481a8199 | 292 | netif_rx_ni(newskb); |
c2ab7ac2 | 293 | |
0d66548a OH |
294 | /* update statistics */ |
295 | can_stats.tx_frames++; | |
296 | can_stats.tx_frames_delta++; | |
297 | ||
c2ab7ac2 | 298 | return 0; |
0d66548a OH |
299 | } |
300 | EXPORT_SYMBOL(can_send); | |
301 | ||
302 | /* | |
303 | * af_can rx path | |
304 | */ | |
305 | ||
306 | static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev) | |
307 | { | |
20dd3850 OH |
308 | if (!dev) |
309 | return &can_rx_alldev_list; | |
310 | else | |
311 | return (struct dev_rcv_lists *)dev->ml_priv; | |
0d66548a OH |
312 | } |
313 | ||
d253eee2 OH |
314 | /** |
315 | * find_rcv_list - determine optimal filterlist inside device filter struct | |
316 | * @can_id: pointer to CAN identifier of a given can_filter | |
317 | * @mask: pointer to CAN mask of a given can_filter | |
318 | * @d: pointer to the device filter struct | |
319 | * | |
320 | * Description: | |
321 | * Returns the optimal filterlist to reduce the filter handling in the | |
322 | * receive path. This function is called by service functions that need | |
323 | * to register or unregister a can_filter in the filter lists. | |
324 | * | |
325 | * A filter matches in general, when | |
326 | * | |
327 | * <received_can_id> & mask == can_id & mask | |
328 | * | |
329 | * so every bit set in the mask (even CAN_EFF_FLAG, CAN_RTR_FLAG) describe | |
330 | * relevant bits for the filter. | |
331 | * | |
332 | * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can | |
333 | * filter for error frames (CAN_ERR_FLAG bit set in mask). For error frames | |
334 | * there is a special filterlist and a special rx path filter handling. | |
335 | * | |
336 | * Return: | |
337 | * Pointer to optimal filterlist for the given can_id/mask pair. | |
338 | * Constistency checked mask. | |
339 | * Reduced can_id to have a preprocessed filter compare value. | |
340 | */ | |
0d66548a OH |
341 | static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask, |
342 | struct dev_rcv_lists *d) | |
343 | { | |
344 | canid_t inv = *can_id & CAN_INV_FILTER; /* save flag before masking */ | |
345 | ||
d253eee2 | 346 | /* filter for error frames in extra filterlist */ |
0d66548a | 347 | if (*mask & CAN_ERR_FLAG) { |
d253eee2 | 348 | /* clear CAN_ERR_FLAG in filter entry */ |
0d66548a OH |
349 | *mask &= CAN_ERR_MASK; |
350 | return &d->rx[RX_ERR]; | |
351 | } | |
352 | ||
d253eee2 OH |
353 | /* with cleared CAN_ERR_FLAG we have a simple mask/value filterpair */ |
354 | ||
355 | #define CAN_EFF_RTR_FLAGS (CAN_EFF_FLAG | CAN_RTR_FLAG) | |
356 | ||
357 | /* ensure valid values in can_mask for 'SFF only' frame filtering */ | |
358 | if ((*mask & CAN_EFF_FLAG) && !(*can_id & CAN_EFF_FLAG)) | |
359 | *mask &= (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS); | |
0d66548a OH |
360 | |
361 | /* reduce condition testing at receive time */ | |
362 | *can_id &= *mask; | |
363 | ||
364 | /* inverse can_id/can_mask filter */ | |
365 | if (inv) | |
366 | return &d->rx[RX_INV]; | |
367 | ||
368 | /* mask == 0 => no condition testing at receive time */ | |
369 | if (!(*mask)) | |
370 | return &d->rx[RX_ALL]; | |
371 | ||
d253eee2 | 372 | /* extra filterlists for the subscription of a single non-RTR can_id */ |
f64f9e71 JP |
373 | if (((*mask & CAN_EFF_RTR_FLAGS) == CAN_EFF_RTR_FLAGS) && |
374 | !(*can_id & CAN_RTR_FLAG)) { | |
d253eee2 OH |
375 | |
376 | if (*can_id & CAN_EFF_FLAG) { | |
377 | if (*mask == (CAN_EFF_MASK | CAN_EFF_RTR_FLAGS)) { | |
378 | /* RFC: a future use-case for hash-tables? */ | |
379 | return &d->rx[RX_EFF]; | |
380 | } | |
381 | } else { | |
382 | if (*mask == (CAN_SFF_MASK | CAN_EFF_RTR_FLAGS)) | |
383 | return &d->rx_sff[*can_id]; | |
0d66548a | 384 | } |
0d66548a OH |
385 | } |
386 | ||
387 | /* default: filter via can_id/can_mask */ | |
388 | return &d->rx[RX_FIL]; | |
389 | } | |
390 | ||
391 | /** | |
392 | * can_rx_register - subscribe CAN frames from a specific interface | |
393 | * @dev: pointer to netdevice (NULL => subcribe from 'all' CAN devices list) | |
394 | * @can_id: CAN identifier (see description) | |
395 | * @mask: CAN mask (see description) | |
396 | * @func: callback function on filter match | |
397 | * @data: returned parameter for callback function | |
398 | * @ident: string for calling module indentification | |
399 | * | |
400 | * Description: | |
401 | * Invokes the callback function with the received sk_buff and the given | |
402 | * parameter 'data' on a matching receive filter. A filter matches, when | |
403 | * | |
404 | * <received_can_id> & mask == can_id & mask | |
405 | * | |
406 | * The filter can be inverted (CAN_INV_FILTER bit set in can_id) or it can | |
407 | * filter for error frames (CAN_ERR_FLAG bit set in mask). | |
408 | * | |
1fa17d4b OH |
409 | * The provided pointer to the sk_buff is guaranteed to be valid as long as |
410 | * the callback function is running. The callback function must *not* free | |
411 | * the given sk_buff while processing it's task. When the given sk_buff is | |
412 | * needed after the end of the callback function it must be cloned inside | |
413 | * the callback function with skb_clone(). | |
414 | * | |
0d66548a OH |
415 | * Return: |
416 | * 0 on success | |
417 | * -ENOMEM on missing cache mem to create subscription entry | |
418 | * -ENODEV unknown device | |
419 | */ | |
420 | int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask, | |
421 | void (*func)(struct sk_buff *, void *), void *data, | |
422 | char *ident) | |
423 | { | |
424 | struct receiver *r; | |
425 | struct hlist_head *rl; | |
426 | struct dev_rcv_lists *d; | |
427 | int err = 0; | |
428 | ||
429 | /* insert new receiver (dev,canid,mask) -> (func,data) */ | |
430 | ||
8b64056d OH |
431 | if (dev && dev->type != ARPHRD_CAN) |
432 | return -ENODEV; | |
433 | ||
0d66548a OH |
434 | r = kmem_cache_alloc(rcv_cache, GFP_KERNEL); |
435 | if (!r) | |
436 | return -ENOMEM; | |
437 | ||
438 | spin_lock(&can_rcvlists_lock); | |
439 | ||
440 | d = find_dev_rcv_lists(dev); | |
441 | if (d) { | |
442 | rl = find_rcv_list(&can_id, &mask, d); | |
443 | ||
444 | r->can_id = can_id; | |
445 | r->mask = mask; | |
446 | r->matches = 0; | |
447 | r->func = func; | |
448 | r->data = data; | |
449 | r->ident = ident; | |
450 | ||
451 | hlist_add_head_rcu(&r->list, rl); | |
452 | d->entries++; | |
453 | ||
454 | can_pstats.rcv_entries++; | |
455 | if (can_pstats.rcv_entries_max < can_pstats.rcv_entries) | |
456 | can_pstats.rcv_entries_max = can_pstats.rcv_entries; | |
457 | } else { | |
458 | kmem_cache_free(rcv_cache, r); | |
459 | err = -ENODEV; | |
460 | } | |
461 | ||
462 | spin_unlock(&can_rcvlists_lock); | |
463 | ||
464 | return err; | |
465 | } | |
466 | EXPORT_SYMBOL(can_rx_register); | |
467 | ||
0d66548a OH |
468 | /* |
469 | * can_rx_delete_receiver - rcu callback for single receiver entry removal | |
470 | */ | |
471 | static void can_rx_delete_receiver(struct rcu_head *rp) | |
472 | { | |
473 | struct receiver *r = container_of(rp, struct receiver, rcu); | |
474 | ||
475 | kmem_cache_free(rcv_cache, r); | |
476 | } | |
477 | ||
478 | /** | |
479 | * can_rx_unregister - unsubscribe CAN frames from a specific interface | |
480 | * @dev: pointer to netdevice (NULL => unsubcribe from 'all' CAN devices list) | |
481 | * @can_id: CAN identifier | |
482 | * @mask: CAN mask | |
483 | * @func: callback function on filter match | |
484 | * @data: returned parameter for callback function | |
485 | * | |
486 | * Description: | |
487 | * Removes subscription entry depending on given (subscription) values. | |
488 | */ | |
489 | void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask, | |
490 | void (*func)(struct sk_buff *, void *), void *data) | |
491 | { | |
492 | struct receiver *r = NULL; | |
493 | struct hlist_head *rl; | |
494 | struct hlist_node *next; | |
495 | struct dev_rcv_lists *d; | |
496 | ||
8b64056d OH |
497 | if (dev && dev->type != ARPHRD_CAN) |
498 | return; | |
499 | ||
0d66548a OH |
500 | spin_lock(&can_rcvlists_lock); |
501 | ||
502 | d = find_dev_rcv_lists(dev); | |
503 | if (!d) { | |
504 | printk(KERN_ERR "BUG: receive list not found for " | |
505 | "dev %s, id %03X, mask %03X\n", | |
506 | DNAME(dev), can_id, mask); | |
507 | goto out; | |
508 | } | |
509 | ||
510 | rl = find_rcv_list(&can_id, &mask, d); | |
511 | ||
512 | /* | |
513 | * Search the receiver list for the item to delete. This should | |
514 | * exist, since no receiver may be unregistered that hasn't | |
515 | * been registered before. | |
516 | */ | |
517 | ||
518 | hlist_for_each_entry_rcu(r, next, rl, list) { | |
f64f9e71 JP |
519 | if (r->can_id == can_id && r->mask == mask && |
520 | r->func == func && r->data == data) | |
0d66548a OH |
521 | break; |
522 | } | |
523 | ||
524 | /* | |
525 | * Check for bugs in CAN protocol implementations: | |
526 | * If no matching list item was found, the list cursor variable next | |
527 | * will be NULL, while r will point to the last item of the list. | |
528 | */ | |
529 | ||
530 | if (!next) { | |
531 | printk(KERN_ERR "BUG: receive list entry not found for " | |
532 | "dev %s, id %03X, mask %03X\n", | |
533 | DNAME(dev), can_id, mask); | |
534 | r = NULL; | |
0d66548a OH |
535 | goto out; |
536 | } | |
537 | ||
538 | hlist_del_rcu(&r->list); | |
539 | d->entries--; | |
540 | ||
541 | if (can_pstats.rcv_entries > 0) | |
542 | can_pstats.rcv_entries--; | |
543 | ||
544 | /* remove device structure requested by NETDEV_UNREGISTER */ | |
20dd3850 OH |
545 | if (d->remove_on_zero_entries && !d->entries) { |
546 | kfree(d); | |
547 | dev->ml_priv = NULL; | |
548 | } | |
0d66548a OH |
549 | |
550 | out: | |
551 | spin_unlock(&can_rcvlists_lock); | |
552 | ||
553 | /* schedule the receiver item for deletion */ | |
554 | if (r) | |
555 | call_rcu(&r->rcu, can_rx_delete_receiver); | |
0d66548a OH |
556 | } |
557 | EXPORT_SYMBOL(can_rx_unregister); | |
558 | ||
559 | static inline void deliver(struct sk_buff *skb, struct receiver *r) | |
560 | { | |
1fa17d4b OH |
561 | r->func(skb, r->data); |
562 | r->matches++; | |
0d66548a OH |
563 | } |
564 | ||
565 | static int can_rcv_filter(struct dev_rcv_lists *d, struct sk_buff *skb) | |
566 | { | |
567 | struct receiver *r; | |
568 | struct hlist_node *n; | |
569 | int matches = 0; | |
570 | struct can_frame *cf = (struct can_frame *)skb->data; | |
571 | canid_t can_id = cf->can_id; | |
572 | ||
573 | if (d->entries == 0) | |
574 | return 0; | |
575 | ||
576 | if (can_id & CAN_ERR_FLAG) { | |
577 | /* check for error frame entries only */ | |
578 | hlist_for_each_entry_rcu(r, n, &d->rx[RX_ERR], list) { | |
579 | if (can_id & r->mask) { | |
580 | deliver(skb, r); | |
581 | matches++; | |
582 | } | |
583 | } | |
584 | return matches; | |
585 | } | |
586 | ||
587 | /* check for unfiltered entries */ | |
588 | hlist_for_each_entry_rcu(r, n, &d->rx[RX_ALL], list) { | |
589 | deliver(skb, r); | |
590 | matches++; | |
591 | } | |
592 | ||
593 | /* check for can_id/mask entries */ | |
594 | hlist_for_each_entry_rcu(r, n, &d->rx[RX_FIL], list) { | |
595 | if ((can_id & r->mask) == r->can_id) { | |
596 | deliver(skb, r); | |
597 | matches++; | |
598 | } | |
599 | } | |
600 | ||
601 | /* check for inverted can_id/mask entries */ | |
602 | hlist_for_each_entry_rcu(r, n, &d->rx[RX_INV], list) { | |
603 | if ((can_id & r->mask) != r->can_id) { | |
604 | deliver(skb, r); | |
605 | matches++; | |
606 | } | |
607 | } | |
608 | ||
f706644d OH |
609 | /* check filterlists for single non-RTR can_ids */ |
610 | if (can_id & CAN_RTR_FLAG) | |
611 | return matches; | |
612 | ||
0d66548a OH |
613 | if (can_id & CAN_EFF_FLAG) { |
614 | hlist_for_each_entry_rcu(r, n, &d->rx[RX_EFF], list) { | |
615 | if (r->can_id == can_id) { | |
616 | deliver(skb, r); | |
617 | matches++; | |
618 | } | |
619 | } | |
620 | } else { | |
621 | can_id &= CAN_SFF_MASK; | |
622 | hlist_for_each_entry_rcu(r, n, &d->rx_sff[can_id], list) { | |
623 | deliver(skb, r); | |
624 | matches++; | |
625 | } | |
626 | } | |
627 | ||
628 | return matches; | |
629 | } | |
630 | ||
631 | static int can_rcv(struct sk_buff *skb, struct net_device *dev, | |
632 | struct packet_type *pt, struct net_device *orig_dev) | |
633 | { | |
634 | struct dev_rcv_lists *d; | |
7f2d38eb | 635 | struct can_frame *cf = (struct can_frame *)skb->data; |
0d66548a OH |
636 | int matches; |
637 | ||
1758c094 OH |
638 | if (!net_eq(dev_net(dev), &init_net)) |
639 | goto drop; | |
0d66548a | 640 | |
1758c094 OH |
641 | if (WARN_ONCE(dev->type != ARPHRD_CAN || |
642 | skb->len != sizeof(struct can_frame) || | |
643 | cf->can_dlc > 8, | |
644 | "PF_CAN: dropped non conform skbuf: " | |
645 | "dev type %d, len %d, can_dlc %d\n", | |
646 | dev->type, skb->len, cf->can_dlc)) | |
647 | goto drop; | |
7f2d38eb | 648 | |
0d66548a OH |
649 | /* update statistics */ |
650 | can_stats.rx_frames++; | |
651 | can_stats.rx_frames_delta++; | |
652 | ||
653 | rcu_read_lock(); | |
654 | ||
655 | /* deliver the packet to sockets listening on all devices */ | |
656 | matches = can_rcv_filter(&can_rx_alldev_list, skb); | |
657 | ||
658 | /* find receive list for this device */ | |
659 | d = find_dev_rcv_lists(dev); | |
660 | if (d) | |
661 | matches += can_rcv_filter(d, skb); | |
662 | ||
663 | rcu_read_unlock(); | |
664 | ||
62bcaa13 OH |
665 | /* consume the skbuff allocated by the netdevice driver */ |
666 | consume_skb(skb); | |
0d66548a OH |
667 | |
668 | if (matches > 0) { | |
669 | can_stats.matches++; | |
670 | can_stats.matches_delta++; | |
671 | } | |
672 | ||
6ca8b990 | 673 | return NET_RX_SUCCESS; |
1758c094 OH |
674 | |
675 | drop: | |
676 | kfree_skb(skb); | |
6ca8b990 | 677 | return NET_RX_DROP; |
0d66548a OH |
678 | } |
679 | ||
680 | /* | |
681 | * af_can protocol functions | |
682 | */ | |
683 | ||
684 | /** | |
685 | * can_proto_register - register CAN transport protocol | |
686 | * @cp: pointer to CAN protocol structure | |
687 | * | |
688 | * Return: | |
689 | * 0 on success | |
690 | * -EINVAL invalid (out of range) protocol number | |
691 | * -EBUSY protocol already in use | |
692 | * -ENOBUF if proto_register() fails | |
693 | */ | |
694 | int can_proto_register(struct can_proto *cp) | |
695 | { | |
696 | int proto = cp->protocol; | |
697 | int err = 0; | |
698 | ||
699 | if (proto < 0 || proto >= CAN_NPROTO) { | |
700 | printk(KERN_ERR "can: protocol number %d out of range\n", | |
701 | proto); | |
702 | return -EINVAL; | |
703 | } | |
704 | ||
a2fea5f1 UT |
705 | err = proto_register(cp->prot, 0); |
706 | if (err < 0) | |
707 | return err; | |
708 | ||
1ca050d9 OH |
709 | mutex_lock(&proto_tab_lock); |
710 | ||
0d66548a OH |
711 | if (proto_tab[proto]) { |
712 | printk(KERN_ERR "can: protocol %d already registered\n", | |
713 | proto); | |
714 | err = -EBUSY; | |
53914b67 | 715 | } else |
1ca050d9 | 716 | rcu_assign_pointer(proto_tab[proto], cp); |
a2fea5f1 | 717 | |
1ca050d9 | 718 | mutex_unlock(&proto_tab_lock); |
0d66548a | 719 | |
0d66548a | 720 | if (err < 0) |
a2fea5f1 | 721 | proto_unregister(cp->prot); |
0d66548a OH |
722 | |
723 | return err; | |
724 | } | |
725 | EXPORT_SYMBOL(can_proto_register); | |
726 | ||
727 | /** | |
728 | * can_proto_unregister - unregister CAN transport protocol | |
729 | * @cp: pointer to CAN protocol structure | |
730 | */ | |
731 | void can_proto_unregister(struct can_proto *cp) | |
732 | { | |
733 | int proto = cp->protocol; | |
734 | ||
1ca050d9 OH |
735 | mutex_lock(&proto_tab_lock); |
736 | BUG_ON(proto_tab[proto] != cp); | |
737 | rcu_assign_pointer(proto_tab[proto], NULL); | |
738 | mutex_unlock(&proto_tab_lock); | |
739 | ||
740 | synchronize_rcu(); | |
a2fea5f1 UT |
741 | |
742 | proto_unregister(cp->prot); | |
0d66548a OH |
743 | } |
744 | EXPORT_SYMBOL(can_proto_unregister); | |
745 | ||
746 | /* | |
747 | * af_can notifier to create/remove CAN netdevice specific structs | |
748 | */ | |
749 | static int can_notifier(struct notifier_block *nb, unsigned long msg, | |
750 | void *data) | |
751 | { | |
752 | struct net_device *dev = (struct net_device *)data; | |
753 | struct dev_rcv_lists *d; | |
754 | ||
721499e8 | 755 | if (!net_eq(dev_net(dev), &init_net)) |
0d66548a OH |
756 | return NOTIFY_DONE; |
757 | ||
758 | if (dev->type != ARPHRD_CAN) | |
759 | return NOTIFY_DONE; | |
760 | ||
761 | switch (msg) { | |
762 | ||
763 | case NETDEV_REGISTER: | |
764 | ||
20dd3850 | 765 | /* create new dev_rcv_lists for this device */ |
0d66548a OH |
766 | d = kzalloc(sizeof(*d), GFP_KERNEL); |
767 | if (!d) { | |
768 | printk(KERN_ERR | |
769 | "can: allocation of receive list failed\n"); | |
770 | return NOTIFY_DONE; | |
771 | } | |
20dd3850 OH |
772 | BUG_ON(dev->ml_priv); |
773 | dev->ml_priv = d; | |
0d66548a OH |
774 | |
775 | break; | |
776 | ||
777 | case NETDEV_UNREGISTER: | |
778 | spin_lock(&can_rcvlists_lock); | |
779 | ||
20dd3850 | 780 | d = dev->ml_priv; |
0d66548a | 781 | if (d) { |
20dd3850 | 782 | if (d->entries) |
0d66548a | 783 | d->remove_on_zero_entries = 1; |
20dd3850 OH |
784 | else { |
785 | kfree(d); | |
786 | dev->ml_priv = NULL; | |
787 | } | |
0d66548a OH |
788 | } else |
789 | printk(KERN_ERR "can: notifier: receive list not " | |
790 | "found for dev %s\n", dev->name); | |
791 | ||
792 | spin_unlock(&can_rcvlists_lock); | |
793 | ||
0d66548a OH |
794 | break; |
795 | } | |
796 | ||
797 | return NOTIFY_DONE; | |
798 | } | |
799 | ||
800 | /* | |
801 | * af_can module init/exit functions | |
802 | */ | |
803 | ||
804 | static struct packet_type can_packet __read_mostly = { | |
09640e63 | 805 | .type = cpu_to_be16(ETH_P_CAN), |
0d66548a OH |
806 | .dev = NULL, |
807 | .func = can_rcv, | |
808 | }; | |
809 | ||
ec1b4cf7 | 810 | static const struct net_proto_family can_family_ops = { |
0d66548a OH |
811 | .family = PF_CAN, |
812 | .create = can_create, | |
813 | .owner = THIS_MODULE, | |
814 | }; | |
815 | ||
816 | /* notifier block for netdevice event */ | |
817 | static struct notifier_block can_netdev_notifier __read_mostly = { | |
818 | .notifier_call = can_notifier, | |
819 | }; | |
820 | ||
821 | static __init int can_init(void) | |
822 | { | |
823 | printk(banner); | |
824 | ||
20dd3850 OH |
825 | memset(&can_rx_alldev_list, 0, sizeof(can_rx_alldev_list)); |
826 | ||
0d66548a OH |
827 | rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver), |
828 | 0, 0, NULL); | |
829 | if (!rcv_cache) | |
830 | return -ENOMEM; | |
831 | ||
0d66548a OH |
832 | if (stats_timer) { |
833 | /* the statistics are updated every second (timer triggered) */ | |
834 | setup_timer(&can_stattimer, can_stat_update, 0); | |
835 | mod_timer(&can_stattimer, round_jiffies(jiffies + HZ)); | |
836 | } else | |
837 | can_stattimer.function = NULL; | |
838 | ||
839 | can_init_proc(); | |
840 | ||
841 | /* protocol register */ | |
842 | sock_register(&can_family_ops); | |
843 | register_netdevice_notifier(&can_netdev_notifier); | |
844 | dev_add_pack(&can_packet); | |
845 | ||
846 | return 0; | |
847 | } | |
848 | ||
849 | static __exit void can_exit(void) | |
850 | { | |
20dd3850 | 851 | struct net_device *dev; |
0d66548a OH |
852 | |
853 | if (stats_timer) | |
854 | del_timer(&can_stattimer); | |
855 | ||
856 | can_remove_proc(); | |
857 | ||
858 | /* protocol unregister */ | |
859 | dev_remove_pack(&can_packet); | |
860 | unregister_netdevice_notifier(&can_netdev_notifier); | |
861 | sock_unregister(PF_CAN); | |
862 | ||
20dd3850 OH |
863 | /* remove created dev_rcv_lists from still registered CAN devices */ |
864 | rcu_read_lock(); | |
865 | for_each_netdev_rcu(&init_net, dev) { | |
866 | if (dev->type == ARPHRD_CAN && dev->ml_priv){ | |
867 | ||
868 | struct dev_rcv_lists *d = dev->ml_priv; | |
869 | ||
870 | BUG_ON(d->entries); | |
871 | kfree(d); | |
872 | dev->ml_priv = NULL; | |
873 | } | |
0d66548a | 874 | } |
20dd3850 | 875 | rcu_read_unlock(); |
0d66548a | 876 | |
382bfeec JDB |
877 | rcu_barrier(); /* Wait for completion of call_rcu()'s */ |
878 | ||
0d66548a OH |
879 | kmem_cache_destroy(rcv_cache); |
880 | } | |
881 | ||
882 | module_init(can_init); | |
883 | module_exit(can_exit); |