blob: 12d851c4604dc6494ce8ba986e146d62ebcaffc3 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
3 *
4 * Copyright (c) 2002-2017 Volkswagen Group Electronic Research
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
23 *
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38 * DAMAGE.
39 *
40 */
41
42#include <linux/module.h>
43#include <linux/init.h>
44#include <linux/interrupt.h>
45#include <linux/hrtimer.h>
46#include <linux/list.h>
47#include <linux/proc_fs.h>
48#include <linux/seq_file.h>
49#include <linux/uio.h>
50#include <linux/net.h>
51#include <linux/netdevice.h>
52#include <linux/socket.h>
53#include <linux/if_arp.h>
54#include <linux/skbuff.h>
55#include <linux/can.h>
56#include <linux/can/core.h>
57#include <linux/can/skb.h>
58#include <linux/can/bcm.h>
59#include <linux/slab.h>
60#include <net/sock.h>
61#include <net/net_namespace.h>
62
63/*
64 * To send multiple CAN frame content within TX_SETUP or to filter
65 * CAN messages with multiplex index within RX_SETUP, the number of
66 * different filters is limited to 256 due to the one byte index value.
67 */
68#define MAX_NFRAMES 256
69
70/* limit timers to 400 days for sending/timeouts */
71#define BCM_TIMER_SEC_MAX (400 * 24 * 60 * 60)
72
73/* use of last_frames[index].flags */
74#define RX_RECV 0x40 /* received data for this element */
75#define RX_THR 0x80 /* element not been sent due to throttle feature */
76#define BCM_CAN_FLAGS_MASK 0x3F /* to clean private flags after usage */
77
78/* get best masking value for can_rx_register() for a given single can_id */
79#define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
80 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
81 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
82
83#define CAN_BCM_VERSION "20170425"
84
85MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
86MODULE_LICENSE("Dual BSD/GPL");
87MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
88MODULE_ALIAS("can-proto-2");
89
90/*
91 * easy access to the first 64 bit of can(fd)_frame payload. cp->data is
92 * 64 bit aligned so the offset has to be multiples of 8 which is ensured
93 * by the only callers in bcm_rx_cmp_to_index() bcm_rx_handler().
94 */
95static inline u64 get_u64(const struct canfd_frame *cp, int offset)
96{
97 return *(u64 *)(cp->data + offset);
98}
99
100struct bcm_op {
101 struct list_head list;
102 int ifindex;
103 canid_t can_id;
104 u32 flags;
105 unsigned long frames_abs, frames_filtered;
106 struct bcm_timeval ival1, ival2;
107 struct hrtimer timer, thrtimer;
108 struct tasklet_struct tsklet, thrtsklet;
109 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
110 int rx_ifindex;
111 int cfsiz;
112 u32 count;
113 u32 nframes;
114 u32 currframe;
115 /* void pointers to arrays of struct can[fd]_frame */
116 void *frames;
117 void *last_frames;
118 struct canfd_frame sframe;
119 struct canfd_frame last_sframe;
120 struct sock *sk;
121 struct net_device *rx_reg_dev;
122};
123
124struct bcm_sock {
125 struct sock sk;
126 int bound;
127 int ifindex;
128 struct notifier_block notifier;
129 struct list_head rx_ops;
130 struct list_head tx_ops;
131 unsigned long dropped_usr_msgs;
132 struct proc_dir_entry *bcm_proc_read;
133 char procname [32]; /* inode number in decimal with \0 */
134};
135
136static inline struct bcm_sock *bcm_sk(const struct sock *sk)
137{
138 return (struct bcm_sock *)sk;
139}
140
141static inline ktime_t bcm_timeval_to_ktime(struct bcm_timeval tv)
142{
143 return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC);
144}
145
146/* check limitations for timeval provided by user */
147static bool bcm_is_invalid_tv(struct bcm_msg_head *msg_head)
148{
149 if ((msg_head->ival1.tv_sec < 0) ||
150 (msg_head->ival1.tv_sec > BCM_TIMER_SEC_MAX) ||
151 (msg_head->ival1.tv_usec < 0) ||
152 (msg_head->ival1.tv_usec >= USEC_PER_SEC) ||
153 (msg_head->ival2.tv_sec < 0) ||
154 (msg_head->ival2.tv_sec > BCM_TIMER_SEC_MAX) ||
155 (msg_head->ival2.tv_usec < 0) ||
156 (msg_head->ival2.tv_usec >= USEC_PER_SEC))
157 return true;
158
159 return false;
160}
161
162#define CFSIZ(flags) ((flags & CAN_FD_FRAME) ? CANFD_MTU : CAN_MTU)
163#define OPSIZ sizeof(struct bcm_op)
164#define MHSIZ sizeof(struct bcm_msg_head)
165
166/*
167 * procfs functions
168 */
169#if IS_ENABLED(CONFIG_PROC_FS)
170static char *bcm_proc_getifname(struct net *net, char *result, int ifindex)
171{
172 struct net_device *dev;
173
174 if (!ifindex)
175 return "any";
176
177 rcu_read_lock();
178 dev = dev_get_by_index_rcu(net, ifindex);
179 if (dev)
180 strcpy(result, dev->name);
181 else
182 strcpy(result, "???");
183 rcu_read_unlock();
184
185 return result;
186}
187
188static int bcm_proc_show(struct seq_file *m, void *v)
189{
190 char ifname[IFNAMSIZ];
191 struct net *net = m->private;
192 struct sock *sk = (struct sock *)PDE_DATA(m->file->f_inode);
193 struct bcm_sock *bo = bcm_sk(sk);
194 struct bcm_op *op;
195
196 seq_printf(m, ">>> socket %pK", sk->sk_socket);
197 seq_printf(m, " / sk %pK", sk);
198 seq_printf(m, " / bo %pK", bo);
199 seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
200 seq_printf(m, " / bound %s", bcm_proc_getifname(net, ifname, bo->ifindex));
201 seq_printf(m, " <<<\n");
202
203 list_for_each_entry(op, &bo->rx_ops, list) {
204
205 unsigned long reduction;
206
207 /* print only active entries & prevent division by zero */
208 if (!op->frames_abs)
209 continue;
210
211 seq_printf(m, "rx_op: %03X %-5s ", op->can_id,
212 bcm_proc_getifname(net, ifname, op->ifindex));
213
214 if (op->flags & CAN_FD_FRAME)
215 seq_printf(m, "(%u)", op->nframes);
216 else
217 seq_printf(m, "[%u]", op->nframes);
218
219 seq_printf(m, "%c ", (op->flags & RX_CHECK_DLC) ? 'd' : ' ');
220
221 if (op->kt_ival1)
222 seq_printf(m, "timeo=%lld ",
223 (long long)ktime_to_us(op->kt_ival1));
224
225 if (op->kt_ival2)
226 seq_printf(m, "thr=%lld ",
227 (long long)ktime_to_us(op->kt_ival2));
228
229 seq_printf(m, "# recv %ld (%ld) => reduction: ",
230 op->frames_filtered, op->frames_abs);
231
232 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
233
234 seq_printf(m, "%s%ld%%\n",
235 (reduction == 100) ? "near " : "", reduction);
236 }
237
238 list_for_each_entry(op, &bo->tx_ops, list) {
239
240 seq_printf(m, "tx_op: %03X %s ", op->can_id,
241 bcm_proc_getifname(net, ifname, op->ifindex));
242
243 if (op->flags & CAN_FD_FRAME)
244 seq_printf(m, "(%u) ", op->nframes);
245 else
246 seq_printf(m, "[%u] ", op->nframes);
247
248 if (op->kt_ival1)
249 seq_printf(m, "t1=%lld ",
250 (long long)ktime_to_us(op->kt_ival1));
251
252 if (op->kt_ival2)
253 seq_printf(m, "t2=%lld ",
254 (long long)ktime_to_us(op->kt_ival2));
255
256 seq_printf(m, "# sent %ld\n", op->frames_abs);
257 }
258 seq_putc(m, '\n');
259 return 0;
260}
261
262static int bcm_proc_open(struct inode *inode, struct file *file)
263{
264 return single_open_net(inode, file, bcm_proc_show);
265}
266
267static const struct file_operations bcm_proc_fops = {
268 .owner = THIS_MODULE,
269 .open = bcm_proc_open,
270 .read = seq_read,
271 .llseek = seq_lseek,
272 .release = single_release,
273};
274#endif /* CONFIG_PROC_FS */
275
276/*
277 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
278 * of the given bcm tx op
279 */
280static void bcm_can_tx(struct bcm_op *op)
281{
282 struct sk_buff *skb;
283 struct net_device *dev;
284 struct canfd_frame *cf = op->frames + op->cfsiz * op->currframe;
285
286 /* no target device? => exit */
287 if (!op->ifindex)
288 return;
289
290 dev = dev_get_by_index(sock_net(op->sk), op->ifindex);
291 if (!dev) {
292 /* RFC: should this bcm_op remove itself here? */
293 return;
294 }
295
296 skb = alloc_skb(op->cfsiz + sizeof(struct can_skb_priv), gfp_any());
297 if (!skb)
298 goto out;
299
300 can_skb_reserve(skb);
301 can_skb_prv(skb)->ifindex = dev->ifindex;
302 can_skb_prv(skb)->skbcnt = 0;
303
304 skb_put_data(skb, cf, op->cfsiz);
305
306 /* send with loopback */
307 skb->dev = dev;
308 can_skb_set_owner(skb, op->sk);
309 can_send(skb, 1);
310
311 /* update statistics */
312 op->currframe++;
313 op->frames_abs++;
314
315 /* reached last frame? */
316 if (op->currframe >= op->nframes)
317 op->currframe = 0;
318out:
319 dev_put(dev);
320}
321
322/*
323 * bcm_send_to_user - send a BCM message to the userspace
324 * (consisting of bcm_msg_head + x CAN frames)
325 */
326static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
327 struct canfd_frame *frames, int has_timestamp)
328{
329 struct sk_buff *skb;
330 struct canfd_frame *firstframe;
331 struct sockaddr_can *addr;
332 struct sock *sk = op->sk;
333 unsigned int datalen = head->nframes * op->cfsiz;
334 int err;
335
336 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
337 if (!skb)
338 return;
339
340 skb_put_data(skb, head, sizeof(*head));
341
342 if (head->nframes) {
343 /* CAN frames starting here */
344 firstframe = (struct canfd_frame *)skb_tail_pointer(skb);
345
346 skb_put_data(skb, frames, datalen);
347
348 /*
349 * the BCM uses the flags-element of the canfd_frame
350 * structure for internal purposes. This is only
351 * relevant for updates that are generated by the
352 * BCM, where nframes is 1
353 */
354 if (head->nframes == 1)
355 firstframe->flags &= BCM_CAN_FLAGS_MASK;
356 }
357
358 if (has_timestamp) {
359 /* restore rx timestamp */
360 skb->tstamp = op->rx_stamp;
361 }
362
363 /*
364 * Put the datagram to the queue so that bcm_recvmsg() can
365 * get it from there. We need to pass the interface index to
366 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
367 * containing the interface index.
368 */
369
370 sock_skb_cb_check_size(sizeof(struct sockaddr_can));
371 addr = (struct sockaddr_can *)skb->cb;
372 memset(addr, 0, sizeof(*addr));
373 addr->can_family = AF_CAN;
374 addr->can_ifindex = op->rx_ifindex;
375
376 err = sock_queue_rcv_skb(sk, skb);
377 if (err < 0) {
378 struct bcm_sock *bo = bcm_sk(sk);
379
380 kfree_skb(skb);
381 /* don't care about overflows in this statistic */
382 bo->dropped_usr_msgs++;
383 }
384}
385
386static void bcm_tx_start_timer(struct bcm_op *op)
387{
388 if (op->kt_ival1 && op->count)
389 hrtimer_start(&op->timer,
390 ktime_add(ktime_get(), op->kt_ival1),
391 HRTIMER_MODE_ABS);
392 else if (op->kt_ival2)
393 hrtimer_start(&op->timer,
394 ktime_add(ktime_get(), op->kt_ival2),
395 HRTIMER_MODE_ABS);
396}
397
398static void bcm_tx_timeout_tsklet(unsigned long data)
399{
400 struct bcm_op *op = (struct bcm_op *)data;
401 struct bcm_msg_head msg_head;
402
403 if (op->kt_ival1 && (op->count > 0)) {
404
405 op->count--;
406 if (!op->count && (op->flags & TX_COUNTEVT)) {
407
408 /* create notification to user */
409 msg_head.opcode = TX_EXPIRED;
410 msg_head.flags = op->flags;
411 msg_head.count = op->count;
412 msg_head.ival1 = op->ival1;
413 msg_head.ival2 = op->ival2;
414 msg_head.can_id = op->can_id;
415 msg_head.nframes = 0;
416
417 bcm_send_to_user(op, &msg_head, NULL, 0);
418 }
419 bcm_can_tx(op);
420
421 } else if (op->kt_ival2)
422 bcm_can_tx(op);
423
424 bcm_tx_start_timer(op);
425}
426
427/*
428 * bcm_tx_timeout_handler - performs cyclic CAN frame transmissions
429 */
430static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
431{
432 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
433
434 tasklet_schedule(&op->tsklet);
435
436 return HRTIMER_NORESTART;
437}
438
439/*
440 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
441 */
442static void bcm_rx_changed(struct bcm_op *op, struct canfd_frame *data)
443{
444 struct bcm_msg_head head;
445
446 /* update statistics */
447 op->frames_filtered++;
448
449 /* prevent statistics overflow */
450 if (op->frames_filtered > ULONG_MAX/100)
451 op->frames_filtered = op->frames_abs = 0;
452
453 /* this element is not throttled anymore */
454 data->flags &= (BCM_CAN_FLAGS_MASK|RX_RECV);
455
456 head.opcode = RX_CHANGED;
457 head.flags = op->flags;
458 head.count = op->count;
459 head.ival1 = op->ival1;
460 head.ival2 = op->ival2;
461 head.can_id = op->can_id;
462 head.nframes = 1;
463
464 bcm_send_to_user(op, &head, data, 1);
465}
466
467/*
468 * bcm_rx_update_and_send - process a detected relevant receive content change
469 * 1. update the last received data
470 * 2. send a notification to the user (if possible)
471 */
472static void bcm_rx_update_and_send(struct bcm_op *op,
473 struct canfd_frame *lastdata,
474 const struct canfd_frame *rxdata)
475{
476 memcpy(lastdata, rxdata, op->cfsiz);
477
478 /* mark as used and throttled by default */
479 lastdata->flags |= (RX_RECV|RX_THR);
480
481 /* throttling mode inactive ? */
482 if (!op->kt_ival2) {
483 /* send RX_CHANGED to the user immediately */
484 bcm_rx_changed(op, lastdata);
485 return;
486 }
487
488 /* with active throttling timer we are just done here */
489 if (hrtimer_active(&op->thrtimer))
490 return;
491
492 /* first reception with enabled throttling mode */
493 if (!op->kt_lastmsg)
494 goto rx_changed_settime;
495
496 /* got a second frame inside a potential throttle period? */
497 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
498 ktime_to_us(op->kt_ival2)) {
499 /* do not send the saved data - only start throttle timer */
500 hrtimer_start(&op->thrtimer,
501 ktime_add(op->kt_lastmsg, op->kt_ival2),
502 HRTIMER_MODE_ABS);
503 return;
504 }
505
506 /* the gap was that big, that throttling was not needed here */
507rx_changed_settime:
508 bcm_rx_changed(op, lastdata);
509 op->kt_lastmsg = ktime_get();
510}
511
512/*
513 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
514 * received data stored in op->last_frames[]
515 */
516static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index,
517 const struct canfd_frame *rxdata)
518{
519 struct canfd_frame *cf = op->frames + op->cfsiz * index;
520 struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
521 int i;
522
523 /*
524 * no one uses the MSBs of flags for comparison,
525 * so we use it here to detect the first time of reception
526 */
527
528 if (!(lcf->flags & RX_RECV)) {
529 /* received data for the first time => send update to user */
530 bcm_rx_update_and_send(op, lcf, rxdata);
531 return;
532 }
533
534 /* do a real check in CAN frame data section */
535 for (i = 0; i < rxdata->len; i += 8) {
536 if ((get_u64(cf, i) & get_u64(rxdata, i)) !=
537 (get_u64(cf, i) & get_u64(lcf, i))) {
538 bcm_rx_update_and_send(op, lcf, rxdata);
539 return;
540 }
541 }
542
543 if (op->flags & RX_CHECK_DLC) {
544 /* do a real check in CAN frame length */
545 if (rxdata->len != lcf->len) {
546 bcm_rx_update_and_send(op, lcf, rxdata);
547 return;
548 }
549 }
550}
551
552/*
553 * bcm_rx_starttimer - enable timeout monitoring for CAN frame reception
554 */
555static void bcm_rx_starttimer(struct bcm_op *op)
556{
557 if (op->flags & RX_NO_AUTOTIMER)
558 return;
559
560 if (op->kt_ival1)
561 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
562}
563
564static void bcm_rx_timeout_tsklet(unsigned long data)
565{
566 struct bcm_op *op = (struct bcm_op *)data;
567 struct bcm_msg_head msg_head;
568
569 /* create notification to user */
570 msg_head.opcode = RX_TIMEOUT;
571 msg_head.flags = op->flags;
572 msg_head.count = op->count;
573 msg_head.ival1 = op->ival1;
574 msg_head.ival2 = op->ival2;
575 msg_head.can_id = op->can_id;
576 msg_head.nframes = 0;
577
578 bcm_send_to_user(op, &msg_head, NULL, 0);
579}
580
581/*
582 * bcm_rx_timeout_handler - when the (cyclic) CAN frame reception timed out
583 */
584static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
585{
586 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
587
588 /* schedule before NET_RX_SOFTIRQ */
589 tasklet_hi_schedule(&op->tsklet);
590
591 /* no restart of the timer is done here! */
592
593 /* if user wants to be informed, when cyclic CAN-Messages come back */
594 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
595 /* clear received CAN frames to indicate 'nothing received' */
596 memset(op->last_frames, 0, op->nframes * op->cfsiz);
597 }
598
599 return HRTIMER_NORESTART;
600}
601
602/*
603 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
604 */
605static inline int bcm_rx_do_flush(struct bcm_op *op, int update,
606 unsigned int index)
607{
608 struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
609
610 if ((op->last_frames) && (lcf->flags & RX_THR)) {
611 if (update)
612 bcm_rx_changed(op, lcf);
613 return 1;
614 }
615 return 0;
616}
617
618/*
619 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
620 *
621 * update == 0 : just check if throttled data is available (any irq context)
622 * update == 1 : check and send throttled data to userspace (soft_irq context)
623 */
624static int bcm_rx_thr_flush(struct bcm_op *op, int update)
625{
626 int updated = 0;
627
628 if (op->nframes > 1) {
629 unsigned int i;
630
631 /* for MUX filter we start at index 1 */
632 for (i = 1; i < op->nframes; i++)
633 updated += bcm_rx_do_flush(op, update, i);
634
635 } else {
636 /* for RX_FILTER_ID and simple filter */
637 updated += bcm_rx_do_flush(op, update, 0);
638 }
639
640 return updated;
641}
642
643static void bcm_rx_thr_tsklet(unsigned long data)
644{
645 struct bcm_op *op = (struct bcm_op *)data;
646
647 /* push the changed data to the userspace */
648 bcm_rx_thr_flush(op, 1);
649}
650
651/*
652 * bcm_rx_thr_handler - the time for blocked content updates is over now:
653 * Check for throttled data and send it to the userspace
654 */
655static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
656{
657 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
658
659 tasklet_schedule(&op->thrtsklet);
660
661 if (bcm_rx_thr_flush(op, 0)) {
662 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
663 return HRTIMER_RESTART;
664 } else {
665 /* rearm throttle handling */
666 op->kt_lastmsg = 0;
667 return HRTIMER_NORESTART;
668 }
669}
670
671/*
672 * bcm_rx_handler - handle a CAN frame reception
673 */
674static void bcm_rx_handler(struct sk_buff *skb, void *data)
675{
676 struct bcm_op *op = (struct bcm_op *)data;
677 const struct canfd_frame *rxframe = (struct canfd_frame *)skb->data;
678 unsigned int i;
679
680 if (op->can_id != rxframe->can_id)
681 return;
682
683 /* make sure to handle the correct frame type (CAN / CAN FD) */
684 if (skb->len != op->cfsiz)
685 return;
686
687 /* disable timeout */
688 hrtimer_cancel(&op->timer);
689
690 /* save rx timestamp */
691 op->rx_stamp = skb->tstamp;
692 /* save originator for recvfrom() */
693 op->rx_ifindex = skb->dev->ifindex;
694 /* update statistics */
695 op->frames_abs++;
696
697 if (op->flags & RX_RTR_FRAME) {
698 /* send reply for RTR-request (placed in op->frames[0]) */
699 bcm_can_tx(op);
700 return;
701 }
702
703 if (op->flags & RX_FILTER_ID) {
704 /* the easiest case */
705 bcm_rx_update_and_send(op, op->last_frames, rxframe);
706 goto rx_starttimer;
707 }
708
709 if (op->nframes == 1) {
710 /* simple compare with index 0 */
711 bcm_rx_cmp_to_index(op, 0, rxframe);
712 goto rx_starttimer;
713 }
714
715 if (op->nframes > 1) {
716 /*
717 * multiplex compare
718 *
719 * find the first multiplex mask that fits.
720 * Remark: The MUX-mask is stored in index 0 - but only the
721 * first 64 bits of the frame data[] are relevant (CAN FD)
722 */
723
724 for (i = 1; i < op->nframes; i++) {
725 if ((get_u64(op->frames, 0) & get_u64(rxframe, 0)) ==
726 (get_u64(op->frames, 0) &
727 get_u64(op->frames + op->cfsiz * i, 0))) {
728 bcm_rx_cmp_to_index(op, i, rxframe);
729 break;
730 }
731 }
732 }
733
734rx_starttimer:
735 bcm_rx_starttimer(op);
736}
737
738/*
739 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
740 */
741static struct bcm_op *bcm_find_op(struct list_head *ops,
742 struct bcm_msg_head *mh, int ifindex)
743{
744 struct bcm_op *op;
745
746 list_for_each_entry(op, ops, list) {
747 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
748 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME))
749 return op;
750 }
751
752 return NULL;
753}
754
755static void bcm_remove_op(struct bcm_op *op)
756{
757 if (op->tsklet.func) {
758 while (test_bit(TASKLET_STATE_SCHED, &op->tsklet.state) ||
759 test_bit(TASKLET_STATE_RUN, &op->tsklet.state) ||
760 hrtimer_active(&op->timer)) {
761 hrtimer_cancel(&op->timer);
762 tasklet_kill(&op->tsklet);
763 }
764 }
765
766 if (op->thrtsklet.func) {
767 while (test_bit(TASKLET_STATE_SCHED, &op->thrtsklet.state) ||
768 test_bit(TASKLET_STATE_RUN, &op->thrtsklet.state) ||
769 hrtimer_active(&op->thrtimer)) {
770 hrtimer_cancel(&op->thrtimer);
771 tasklet_kill(&op->thrtsklet);
772 }
773 }
774
775 if ((op->frames) && (op->frames != &op->sframe))
776 kfree(op->frames);
777
778 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
779 kfree(op->last_frames);
780
781 kfree(op);
782}
783
784static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
785{
786 if (op->rx_reg_dev == dev) {
787 can_rx_unregister(dev_net(dev), dev, op->can_id,
788 REGMASK(op->can_id), bcm_rx_handler, op);
789
790 /* mark as removed subscription */
791 op->rx_reg_dev = NULL;
792 } else
793 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
794 "mismatch %p %p\n", op->rx_reg_dev, dev);
795}
796
797/*
798 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
799 */
800static int bcm_delete_rx_op(struct list_head *ops, struct bcm_msg_head *mh,
801 int ifindex)
802{
803 struct bcm_op *op, *n;
804
805 list_for_each_entry_safe(op, n, ops, list) {
806 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
807 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
808
809 /*
810 * Don't care if we're bound or not (due to netdev
811 * problems) can_rx_unregister() is always a save
812 * thing to do here.
813 */
814 if (op->ifindex) {
815 /*
816 * Only remove subscriptions that had not
817 * been removed due to NETDEV_UNREGISTER
818 * in bcm_notifier()
819 */
820 if (op->rx_reg_dev) {
821 struct net_device *dev;
822
823 dev = dev_get_by_index(sock_net(op->sk),
824 op->ifindex);
825 if (dev) {
826 bcm_rx_unreg(dev, op);
827 dev_put(dev);
828 }
829 }
830 } else
831 can_rx_unregister(sock_net(op->sk), NULL,
832 op->can_id,
833 REGMASK(op->can_id),
834 bcm_rx_handler, op);
835
836 list_del(&op->list);
837 bcm_remove_op(op);
838 return 1; /* done */
839 }
840 }
841
842 return 0; /* not found */
843}
844
845/*
846 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
847 */
848static int bcm_delete_tx_op(struct list_head *ops, struct bcm_msg_head *mh,
849 int ifindex)
850{
851 struct bcm_op *op, *n;
852
853 list_for_each_entry_safe(op, n, ops, list) {
854 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
855 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
856 list_del(&op->list);
857 bcm_remove_op(op);
858 return 1; /* done */
859 }
860 }
861
862 return 0; /* not found */
863}
864
865/*
866 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
867 */
868static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
869 int ifindex)
870{
871 struct bcm_op *op = bcm_find_op(ops, msg_head, ifindex);
872
873 if (!op)
874 return -EINVAL;
875
876 /* put current values into msg_head */
877 msg_head->flags = op->flags;
878 msg_head->count = op->count;
879 msg_head->ival1 = op->ival1;
880 msg_head->ival2 = op->ival2;
881 msg_head->nframes = op->nframes;
882
883 bcm_send_to_user(op, msg_head, op->frames, 0);
884
885 return MHSIZ;
886}
887
888/*
889 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
890 */
891static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
892 int ifindex, struct sock *sk)
893{
894 struct bcm_sock *bo = bcm_sk(sk);
895 struct bcm_op *op;
896 struct canfd_frame *cf;
897 unsigned int i;
898 int err;
899
900 /* we need a real device to send frames */
901 if (!ifindex)
902 return -ENODEV;
903
904 /* check nframes boundaries - we need at least one CAN frame */
905 if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES)
906 return -EINVAL;
907
908 /* check timeval limitations */
909 if ((msg_head->flags & SETTIMER) && bcm_is_invalid_tv(msg_head))
910 return -EINVAL;
911
912 /* check the given can_id */
913 op = bcm_find_op(&bo->tx_ops, msg_head, ifindex);
914 if (op) {
915 /* update existing BCM operation */
916
917 /*
918 * Do we need more space for the CAN frames than currently
919 * allocated? -> This is a _really_ unusual use-case and
920 * therefore (complexity / locking) it is not supported.
921 */
922 if (msg_head->nframes > op->nframes)
923 return -E2BIG;
924
925 /* update CAN frames content */
926 for (i = 0; i < msg_head->nframes; i++) {
927
928 cf = op->frames + op->cfsiz * i;
929 err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz);
930
931 if (op->flags & CAN_FD_FRAME) {
932 if (cf->len > 64)
933 err = -EINVAL;
934 } else {
935 if (cf->len > 8)
936 err = -EINVAL;
937 }
938
939 if (err < 0)
940 return err;
941
942 if (msg_head->flags & TX_CP_CAN_ID) {
943 /* copy can_id into frame */
944 cf->can_id = msg_head->can_id;
945 }
946 }
947 op->flags = msg_head->flags;
948
949 } else {
950 /* insert new BCM operation for the given can_id */
951
952 op = kzalloc(OPSIZ, GFP_KERNEL);
953 if (!op)
954 return -ENOMEM;
955
956 op->can_id = msg_head->can_id;
957 op->cfsiz = CFSIZ(msg_head->flags);
958 op->flags = msg_head->flags;
959
960 /* create array for CAN frames and copy the data */
961 if (msg_head->nframes > 1) {
962 op->frames = kmalloc(msg_head->nframes * op->cfsiz,
963 GFP_KERNEL);
964 if (!op->frames) {
965 kfree(op);
966 return -ENOMEM;
967 }
968 } else
969 op->frames = &op->sframe;
970
971 for (i = 0; i < msg_head->nframes; i++) {
972
973 cf = op->frames + op->cfsiz * i;
974 err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz);
975
976 if (op->flags & CAN_FD_FRAME) {
977 if (cf->len > 64)
978 err = -EINVAL;
979 } else {
980 if (cf->len > 8)
981 err = -EINVAL;
982 }
983
984 if (err < 0) {
985 if (op->frames != &op->sframe)
986 kfree(op->frames);
987 kfree(op);
988 return err;
989 }
990
991 if (msg_head->flags & TX_CP_CAN_ID) {
992 /* copy can_id into frame */
993 cf->can_id = msg_head->can_id;
994 }
995 }
996
997 /* tx_ops never compare with previous received messages */
998 op->last_frames = NULL;
999
1000 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1001 op->sk = sk;
1002 op->ifindex = ifindex;
1003
1004 /* initialize uninitialized (kzalloc) structure */
1005 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1006 op->timer.function = bcm_tx_timeout_handler;
1007
1008 /* initialize tasklet for tx countevent notification */
1009 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
1010 (unsigned long) op);
1011
1012 /* currently unused in tx_ops */
1013 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1014
1015 /* add this bcm_op to the list of the tx_ops */
1016 list_add(&op->list, &bo->tx_ops);
1017
1018 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1019
1020 if (op->nframes != msg_head->nframes) {
1021 op->nframes = msg_head->nframes;
1022 /* start multiple frame transmission with index 0 */
1023 op->currframe = 0;
1024 }
1025
1026 /* check flags */
1027
1028 if (op->flags & TX_RESET_MULTI_IDX) {
1029 /* start multiple frame transmission with index 0 */
1030 op->currframe = 0;
1031 }
1032
1033 if (op->flags & SETTIMER) {
1034 /* set timer values */
1035 op->count = msg_head->count;
1036 op->ival1 = msg_head->ival1;
1037 op->ival2 = msg_head->ival2;
1038 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
1039 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
1040
1041 /* disable an active timer due to zero values? */
1042 if (!op->kt_ival1 && !op->kt_ival2)
1043 hrtimer_cancel(&op->timer);
1044 }
1045
1046 if (op->flags & STARTTIMER) {
1047 hrtimer_cancel(&op->timer);
1048 /* spec: send CAN frame when starting timer */
1049 op->flags |= TX_ANNOUNCE;
1050 }
1051
1052 if (op->flags & TX_ANNOUNCE) {
1053 bcm_can_tx(op);
1054 if (op->count)
1055 op->count--;
1056 }
1057
1058 if (op->flags & STARTTIMER)
1059 bcm_tx_start_timer(op);
1060
1061 return msg_head->nframes * op->cfsiz + MHSIZ;
1062}
1063
1064/*
1065 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1066 */
1067static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1068 int ifindex, struct sock *sk)
1069{
1070 struct bcm_sock *bo = bcm_sk(sk);
1071 struct bcm_op *op;
1072 int do_rx_register;
1073 int err = 0;
1074
1075 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1076 /* be robust against wrong usage ... */
1077 msg_head->flags |= RX_FILTER_ID;
1078 /* ignore trailing garbage */
1079 msg_head->nframes = 0;
1080 }
1081
1082 /* the first element contains the mux-mask => MAX_NFRAMES + 1 */
1083 if (msg_head->nframes > MAX_NFRAMES + 1)
1084 return -EINVAL;
1085
1086 if ((msg_head->flags & RX_RTR_FRAME) &&
1087 ((msg_head->nframes != 1) ||
1088 (!(msg_head->can_id & CAN_RTR_FLAG))))
1089 return -EINVAL;
1090
1091 /* check timeval limitations */
1092 if ((msg_head->flags & SETTIMER) && bcm_is_invalid_tv(msg_head))
1093 return -EINVAL;
1094
1095 /* check the given can_id */
1096 op = bcm_find_op(&bo->rx_ops, msg_head, ifindex);
1097 if (op) {
1098 /* update existing BCM operation */
1099
1100 /*
1101 * Do we need more space for the CAN frames than currently
1102 * allocated? -> This is a _really_ unusual use-case and
1103 * therefore (complexity / locking) it is not supported.
1104 */
1105 if (msg_head->nframes > op->nframes)
1106 return -E2BIG;
1107
1108 if (msg_head->nframes) {
1109 /* update CAN frames content */
1110 err = memcpy_from_msg(op->frames, msg,
1111 msg_head->nframes * op->cfsiz);
1112 if (err < 0)
1113 return err;
1114
1115 /* clear last_frames to indicate 'nothing received' */
1116 memset(op->last_frames, 0, msg_head->nframes * op->cfsiz);
1117 }
1118
1119 op->nframes = msg_head->nframes;
1120 op->flags = msg_head->flags;
1121
1122 /* Only an update -> do not call can_rx_register() */
1123 do_rx_register = 0;
1124
1125 } else {
1126 /* insert new BCM operation for the given can_id */
1127 op = kzalloc(OPSIZ, GFP_KERNEL);
1128 if (!op)
1129 return -ENOMEM;
1130
1131 op->can_id = msg_head->can_id;
1132 op->nframes = msg_head->nframes;
1133 op->cfsiz = CFSIZ(msg_head->flags);
1134 op->flags = msg_head->flags;
1135
1136 if (msg_head->nframes > 1) {
1137 /* create array for CAN frames and copy the data */
1138 op->frames = kmalloc(msg_head->nframes * op->cfsiz,
1139 GFP_KERNEL);
1140 if (!op->frames) {
1141 kfree(op);
1142 return -ENOMEM;
1143 }
1144
1145 /* create and init array for received CAN frames */
1146 op->last_frames = kzalloc(msg_head->nframes * op->cfsiz,
1147 GFP_KERNEL);
1148 if (!op->last_frames) {
1149 kfree(op->frames);
1150 kfree(op);
1151 return -ENOMEM;
1152 }
1153
1154 } else {
1155 op->frames = &op->sframe;
1156 op->last_frames = &op->last_sframe;
1157 }
1158
1159 if (msg_head->nframes) {
1160 err = memcpy_from_msg(op->frames, msg,
1161 msg_head->nframes * op->cfsiz);
1162 if (err < 0) {
1163 if (op->frames != &op->sframe)
1164 kfree(op->frames);
1165 if (op->last_frames != &op->last_sframe)
1166 kfree(op->last_frames);
1167 kfree(op);
1168 return err;
1169 }
1170 }
1171
1172 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1173 op->sk = sk;
1174 op->ifindex = ifindex;
1175
1176 /* ifindex for timeout events w/o previous frame reception */
1177 op->rx_ifindex = ifindex;
1178
1179 /* initialize uninitialized (kzalloc) structure */
1180 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1181 op->timer.function = bcm_rx_timeout_handler;
1182
1183 /* initialize tasklet for rx timeout notification */
1184 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1185 (unsigned long) op);
1186
1187 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1188 op->thrtimer.function = bcm_rx_thr_handler;
1189
1190 /* initialize tasklet for rx throttle handling */
1191 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1192 (unsigned long) op);
1193
1194 /* add this bcm_op to the list of the rx_ops */
1195 list_add(&op->list, &bo->rx_ops);
1196
1197 /* call can_rx_register() */
1198 do_rx_register = 1;
1199
1200 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1201
1202 /* check flags */
1203
1204 if (op->flags & RX_RTR_FRAME) {
1205 struct canfd_frame *frame0 = op->frames;
1206
1207 /* no timers in RTR-mode */
1208 hrtimer_cancel(&op->thrtimer);
1209 hrtimer_cancel(&op->timer);
1210
1211 /*
1212 * funny feature in RX(!)_SETUP only for RTR-mode:
1213 * copy can_id into frame BUT without RTR-flag to
1214 * prevent a full-load-loopback-test ... ;-]
1215 */
1216 if ((op->flags & TX_CP_CAN_ID) ||
1217 (frame0->can_id == op->can_id))
1218 frame0->can_id = op->can_id & ~CAN_RTR_FLAG;
1219
1220 } else {
1221 if (op->flags & SETTIMER) {
1222
1223 /* set timer value */
1224 op->ival1 = msg_head->ival1;
1225 op->ival2 = msg_head->ival2;
1226 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
1227 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
1228
1229 /* disable an active timer due to zero value? */
1230 if (!op->kt_ival1)
1231 hrtimer_cancel(&op->timer);
1232
1233 /*
1234 * In any case cancel the throttle timer, flush
1235 * potentially blocked msgs and reset throttle handling
1236 */
1237 op->kt_lastmsg = 0;
1238 hrtimer_cancel(&op->thrtimer);
1239 bcm_rx_thr_flush(op, 1);
1240 }
1241
1242 if ((op->flags & STARTTIMER) && op->kt_ival1)
1243 hrtimer_start(&op->timer, op->kt_ival1,
1244 HRTIMER_MODE_REL);
1245 }
1246
1247 /* now we can register for can_ids, if we added a new bcm_op */
1248 if (do_rx_register) {
1249 if (ifindex) {
1250 struct net_device *dev;
1251
1252 dev = dev_get_by_index(sock_net(sk), ifindex);
1253 if (dev) {
1254 err = can_rx_register(sock_net(sk), dev,
1255 op->can_id,
1256 REGMASK(op->can_id),
1257 bcm_rx_handler, op,
1258 "bcm", sk);
1259
1260 op->rx_reg_dev = dev;
1261 dev_put(dev);
1262 }
1263
1264 } else
1265 err = can_rx_register(sock_net(sk), NULL, op->can_id,
1266 REGMASK(op->can_id),
1267 bcm_rx_handler, op, "bcm", sk);
1268 if (err) {
1269 /* this bcm rx op is broken -> remove it */
1270 list_del(&op->list);
1271 bcm_remove_op(op);
1272 return err;
1273 }
1274 }
1275
1276 return msg_head->nframes * op->cfsiz + MHSIZ;
1277}
1278
1279/*
1280 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1281 */
1282static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk,
1283 int cfsiz)
1284{
1285 struct sk_buff *skb;
1286 struct net_device *dev;
1287 int err;
1288
1289 /* we need a real device to send frames */
1290 if (!ifindex)
1291 return -ENODEV;
1292
1293 skb = alloc_skb(cfsiz + sizeof(struct can_skb_priv), GFP_KERNEL);
1294 if (!skb)
1295 return -ENOMEM;
1296
1297 can_skb_reserve(skb);
1298
1299 err = memcpy_from_msg(skb_put(skb, cfsiz), msg, cfsiz);
1300 if (err < 0) {
1301 kfree_skb(skb);
1302 return err;
1303 }
1304
1305 dev = dev_get_by_index(sock_net(sk), ifindex);
1306 if (!dev) {
1307 kfree_skb(skb);
1308 return -ENODEV;
1309 }
1310
1311 can_skb_prv(skb)->ifindex = dev->ifindex;
1312 can_skb_prv(skb)->skbcnt = 0;
1313 skb->dev = dev;
1314 can_skb_set_owner(skb, sk);
1315 err = can_send(skb, 1); /* send with loopback */
1316 dev_put(dev);
1317
1318 if (err)
1319 return err;
1320
1321 return cfsiz + MHSIZ;
1322}
1323
1324/*
1325 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1326 */
1327static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
1328{
1329 struct sock *sk = sock->sk;
1330 struct bcm_sock *bo = bcm_sk(sk);
1331 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1332 struct bcm_msg_head msg_head;
1333 int cfsiz;
1334 int ret; /* read bytes or error codes as return value */
1335
1336 if (!bo->bound)
1337 return -ENOTCONN;
1338
1339 /* check for valid message length from userspace */
1340 if (size < MHSIZ)
1341 return -EINVAL;
1342
1343 /* read message head information */
1344 ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ);
1345 if (ret < 0)
1346 return ret;
1347
1348 cfsiz = CFSIZ(msg_head.flags);
1349 if ((size - MHSIZ) % cfsiz)
1350 return -EINVAL;
1351
1352 /* check for alternative ifindex for this bcm_op */
1353
1354 if (!ifindex && msg->msg_name) {
1355 /* no bound device as default => check msg_name */
1356 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
1357
1358 if (msg->msg_namelen < sizeof(*addr))
1359 return -EINVAL;
1360
1361 if (addr->can_family != AF_CAN)
1362 return -EINVAL;
1363
1364 /* ifindex from sendto() */
1365 ifindex = addr->can_ifindex;
1366
1367 if (ifindex) {
1368 struct net_device *dev;
1369
1370 dev = dev_get_by_index(sock_net(sk), ifindex);
1371 if (!dev)
1372 return -ENODEV;
1373
1374 if (dev->type != ARPHRD_CAN) {
1375 dev_put(dev);
1376 return -ENODEV;
1377 }
1378
1379 dev_put(dev);
1380 }
1381 }
1382
1383 lock_sock(sk);
1384
1385 switch (msg_head.opcode) {
1386
1387 case TX_SETUP:
1388 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1389 break;
1390
1391 case RX_SETUP:
1392 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1393 break;
1394
1395 case TX_DELETE:
1396 if (bcm_delete_tx_op(&bo->tx_ops, &msg_head, ifindex))
1397 ret = MHSIZ;
1398 else
1399 ret = -EINVAL;
1400 break;
1401
1402 case RX_DELETE:
1403 if (bcm_delete_rx_op(&bo->rx_ops, &msg_head, ifindex))
1404 ret = MHSIZ;
1405 else
1406 ret = -EINVAL;
1407 break;
1408
1409 case TX_READ:
1410 /* reuse msg_head for the reply to TX_READ */
1411 msg_head.opcode = TX_STATUS;
1412 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1413 break;
1414
1415 case RX_READ:
1416 /* reuse msg_head for the reply to RX_READ */
1417 msg_head.opcode = RX_STATUS;
1418 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1419 break;
1420
1421 case TX_SEND:
1422 /* we need exactly one CAN frame behind the msg head */
1423 if ((msg_head.nframes != 1) || (size != cfsiz + MHSIZ))
1424 ret = -EINVAL;
1425 else
1426 ret = bcm_tx_send(msg, ifindex, sk, cfsiz);
1427 break;
1428
1429 default:
1430 ret = -EINVAL;
1431 break;
1432 }
1433
1434 release_sock(sk);
1435
1436 return ret;
1437}
1438
1439/*
1440 * notification handler for netdevice status changes
1441 */
1442static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1443 void *ptr)
1444{
1445 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1446 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1447 struct sock *sk = &bo->sk;
1448 struct bcm_op *op;
1449 int notify_enodev = 0;
1450
1451 if (!net_eq(dev_net(dev), sock_net(sk)))
1452 return NOTIFY_DONE;
1453
1454 if (dev->type != ARPHRD_CAN)
1455 return NOTIFY_DONE;
1456
1457 switch (msg) {
1458
1459 case NETDEV_UNREGISTER:
1460 lock_sock(sk);
1461
1462 /* remove device specific receive entries */
1463 list_for_each_entry(op, &bo->rx_ops, list)
1464 if (op->rx_reg_dev == dev)
1465 bcm_rx_unreg(dev, op);
1466
1467 /* remove device reference, if this is our bound device */
1468 if (bo->bound && bo->ifindex == dev->ifindex) {
1469 bo->bound = 0;
1470 bo->ifindex = 0;
1471 notify_enodev = 1;
1472 }
1473
1474 release_sock(sk);
1475
1476 if (notify_enodev) {
1477 sk->sk_err = ENODEV;
1478 if (!sock_flag(sk, SOCK_DEAD))
1479 sk->sk_error_report(sk);
1480 }
1481 break;
1482
1483 case NETDEV_DOWN:
1484 if (bo->bound && bo->ifindex == dev->ifindex) {
1485 sk->sk_err = ENETDOWN;
1486 if (!sock_flag(sk, SOCK_DEAD))
1487 sk->sk_error_report(sk);
1488 }
1489 }
1490
1491 return NOTIFY_DONE;
1492}
1493
1494/*
1495 * initial settings for all BCM sockets to be set at socket creation time
1496 */
1497static int bcm_init(struct sock *sk)
1498{
1499 struct bcm_sock *bo = bcm_sk(sk);
1500
1501 bo->bound = 0;
1502 bo->ifindex = 0;
1503 bo->dropped_usr_msgs = 0;
1504 bo->bcm_proc_read = NULL;
1505
1506 INIT_LIST_HEAD(&bo->tx_ops);
1507 INIT_LIST_HEAD(&bo->rx_ops);
1508
1509 /* set notifier */
1510 bo->notifier.notifier_call = bcm_notifier;
1511
1512 register_netdevice_notifier(&bo->notifier);
1513
1514 return 0;
1515}
1516
1517/*
1518 * standard socket functions
1519 */
1520static int bcm_release(struct socket *sock)
1521{
1522 struct sock *sk = sock->sk;
1523 struct net *net;
1524 struct bcm_sock *bo;
1525 struct bcm_op *op, *next;
1526
1527 if (!sk)
1528 return 0;
1529
1530 net = sock_net(sk);
1531 bo = bcm_sk(sk);
1532
1533 /* remove bcm_ops, timer, rx_unregister(), etc. */
1534
1535 unregister_netdevice_notifier(&bo->notifier);
1536
1537 lock_sock(sk);
1538
1539 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1540 bcm_remove_op(op);
1541
1542 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1543 /*
1544 * Don't care if we're bound or not (due to netdev problems)
1545 * can_rx_unregister() is always a save thing to do here.
1546 */
1547 if (op->ifindex) {
1548 /*
1549 * Only remove subscriptions that had not
1550 * been removed due to NETDEV_UNREGISTER
1551 * in bcm_notifier()
1552 */
1553 if (op->rx_reg_dev) {
1554 struct net_device *dev;
1555
1556 dev = dev_get_by_index(net, op->ifindex);
1557 if (dev) {
1558 bcm_rx_unreg(dev, op);
1559 dev_put(dev);
1560 }
1561 }
1562 } else
1563 can_rx_unregister(net, NULL, op->can_id,
1564 REGMASK(op->can_id),
1565 bcm_rx_handler, op);
1566
1567 bcm_remove_op(op);
1568 }
1569
1570#if IS_ENABLED(CONFIG_PROC_FS)
1571 /* remove procfs entry */
1572 if (net->can.bcmproc_dir && bo->bcm_proc_read)
1573 remove_proc_entry(bo->procname, net->can.bcmproc_dir);
1574#endif /* CONFIG_PROC_FS */
1575
1576 /* remove device reference */
1577 if (bo->bound) {
1578 bo->bound = 0;
1579 bo->ifindex = 0;
1580 }
1581
1582 sock_orphan(sk);
1583 sock->sk = NULL;
1584
1585 release_sock(sk);
1586 sock_put(sk);
1587
1588 return 0;
1589}
1590
1591static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1592 int flags)
1593{
1594 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1595 struct sock *sk = sock->sk;
1596 struct bcm_sock *bo = bcm_sk(sk);
1597 struct net *net = sock_net(sk);
1598 int ret = 0;
1599
1600 if (len < sizeof(*addr))
1601 return -EINVAL;
1602
1603 lock_sock(sk);
1604
1605 if (bo->bound) {
1606 ret = -EISCONN;
1607 goto fail;
1608 }
1609
1610 /* bind a device to this socket */
1611 if (addr->can_ifindex) {
1612 struct net_device *dev;
1613
1614 dev = dev_get_by_index(net, addr->can_ifindex);
1615 if (!dev) {
1616 ret = -ENODEV;
1617 goto fail;
1618 }
1619 if (dev->type != ARPHRD_CAN) {
1620 dev_put(dev);
1621 ret = -ENODEV;
1622 goto fail;
1623 }
1624
1625 bo->ifindex = dev->ifindex;
1626 dev_put(dev);
1627
1628 } else {
1629 /* no interface reference for ifindex = 0 ('any' CAN device) */
1630 bo->ifindex = 0;
1631 }
1632
1633#if IS_ENABLED(CONFIG_PROC_FS)
1634 if (net->can.bcmproc_dir) {
1635 /* unique socket address as filename */
1636 sprintf(bo->procname, "%lu", sock_i_ino(sk));
1637 bo->bcm_proc_read = proc_create_data(bo->procname, 0644,
1638 net->can.bcmproc_dir,
1639 &bcm_proc_fops, sk);
1640 if (!bo->bcm_proc_read) {
1641 ret = -ENOMEM;
1642 goto fail;
1643 }
1644 }
1645#endif /* CONFIG_PROC_FS */
1646
1647 bo->bound = 1;
1648
1649fail:
1650 release_sock(sk);
1651
1652 return ret;
1653}
1654
1655static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1656 int flags)
1657{
1658 struct sock *sk = sock->sk;
1659 struct sk_buff *skb;
1660 int error = 0;
1661 int noblock;
1662 int err;
1663
1664 noblock = flags & MSG_DONTWAIT;
1665 flags &= ~MSG_DONTWAIT;
1666 skb = skb_recv_datagram(sk, flags, noblock, &error);
1667 if (!skb)
1668 return error;
1669
1670 if (skb->len < size)
1671 size = skb->len;
1672
1673 err = memcpy_to_msg(msg, skb->data, size);
1674 if (err < 0) {
1675 skb_free_datagram(sk, skb);
1676 return err;
1677 }
1678
1679 sock_recv_ts_and_drops(msg, sk, skb);
1680
1681 if (msg->msg_name) {
1682 __sockaddr_check_size(sizeof(struct sockaddr_can));
1683 msg->msg_namelen = sizeof(struct sockaddr_can);
1684 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1685 }
1686
1687 skb_free_datagram(sk, skb);
1688
1689 return size;
1690}
1691
1692static const struct proto_ops bcm_ops = {
1693 .family = PF_CAN,
1694 .release = bcm_release,
1695 .bind = sock_no_bind,
1696 .connect = bcm_connect,
1697 .socketpair = sock_no_socketpair,
1698 .accept = sock_no_accept,
1699 .getname = sock_no_getname,
1700 .poll = datagram_poll,
1701 .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
1702 .listen = sock_no_listen,
1703 .shutdown = sock_no_shutdown,
1704 .setsockopt = sock_no_setsockopt,
1705 .getsockopt = sock_no_getsockopt,
1706 .sendmsg = bcm_sendmsg,
1707 .recvmsg = bcm_recvmsg,
1708 .mmap = sock_no_mmap,
1709 .sendpage = sock_no_sendpage,
1710};
1711
1712static struct proto bcm_proto __read_mostly = {
1713 .name = "CAN_BCM",
1714 .owner = THIS_MODULE,
1715 .obj_size = sizeof(struct bcm_sock),
1716 .init = bcm_init,
1717};
1718
1719static const struct can_proto bcm_can_proto = {
1720 .type = SOCK_DGRAM,
1721 .protocol = CAN_BCM,
1722 .ops = &bcm_ops,
1723 .prot = &bcm_proto,
1724};
1725
1726static int canbcm_pernet_init(struct net *net)
1727{
1728#if IS_ENABLED(CONFIG_PROC_FS)
1729 /* create /proc/net/can-bcm directory */
1730 net->can.bcmproc_dir = proc_net_mkdir(net, "can-bcm", net->proc_net);
1731#endif /* CONFIG_PROC_FS */
1732
1733 return 0;
1734}
1735
1736static void canbcm_pernet_exit(struct net *net)
1737{
1738#if IS_ENABLED(CONFIG_PROC_FS)
1739 /* remove /proc/net/can-bcm directory */
1740 if (net->can.bcmproc_dir)
1741 remove_proc_entry("can-bcm", net->proc_net);
1742#endif /* CONFIG_PROC_FS */
1743}
1744
1745static struct pernet_operations canbcm_pernet_ops __read_mostly = {
1746 .init = canbcm_pernet_init,
1747 .exit = canbcm_pernet_exit,
1748};
1749
1750static int __init bcm_module_init(void)
1751{
1752 int err;
1753
1754 pr_info("can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n");
1755
1756 err = can_proto_register(&bcm_can_proto);
1757 if (err < 0) {
1758 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1759 return err;
1760 }
1761
1762 register_pernet_subsys(&canbcm_pernet_ops);
1763 return 0;
1764}
1765
1766static void __exit bcm_module_exit(void)
1767{
1768 can_proto_unregister(&bcm_can_proto);
1769 unregister_pernet_subsys(&canbcm_pernet_ops);
1770}
1771
1772module_init(bcm_module_init);
1773module_exit(bcm_module_exit);