2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
4 * Copyright (c) 2002-2017 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
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.
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
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>
61 #include <net/net_namespace.h>
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.
68 #define MAX_NFRAMES 256
70 /* use of last_frames[index].flags */
71 #define RX_RECV 0x40 /* received data for this element */
72 #define RX_THR 0x80 /* element not been sent due to throttle feature */
73 #define BCM_CAN_FLAGS_MASK 0x3F /* to clean private flags after usage */
75 /* get best masking value for can_rx_register() for a given single can_id */
76 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
77 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
78 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
80 #define CAN_BCM_VERSION "20170425"
82 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
83 MODULE_LICENSE("Dual BSD/GPL");
85 MODULE_ALIAS("can-proto-2");
88 * easy access to the first 64 bit of can(fd)_frame payload. cp->data is
89 * 64 bit aligned so the offset has to be multiples of 8 which is ensured
90 * by the only callers in bcm_rx_cmp_to_index() bcm_rx_handler().
92 static inline u64 get_u64(const struct canfd_frame *cp, int offset)
94 return *(u64 *)(cp->data + offset);
98 struct list_head list;
102 unsigned long frames_abs, frames_filtered;
103 struct bcm_timeval ival1, ival2;
104 struct hrtimer timer, thrtimer;
105 struct tasklet_struct tsklet, thrtsklet;
106 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
112 /* void pointers to arrays of struct can[fd]_frame */
115 struct canfd_frame sframe;
116 struct canfd_frame last_sframe;
118 struct net_device *rx_reg_dev;
125 struct notifier_block notifier;
126 struct list_head rx_ops;
127 struct list_head tx_ops;
128 unsigned long dropped_usr_msgs;
129 struct proc_dir_entry *bcm_proc_read;
130 char procname [32]; /* inode number in decimal with \0 */
133 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
135 return (struct bcm_sock *)sk;
138 static inline ktime_t bcm_timeval_to_ktime(struct bcm_timeval tv)
140 return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC);
143 #define CFSIZ(flags) ((flags & CAN_FD_FRAME) ? CANFD_MTU : CAN_MTU)
144 #define OPSIZ sizeof(struct bcm_op)
145 #define MHSIZ sizeof(struct bcm_msg_head)
150 #if IS_ENABLED(CONFIG_PROC_FS)
151 static char *bcm_proc_getifname(struct net *net, char *result, int ifindex)
153 struct net_device *dev;
159 dev = dev_get_by_index_rcu(net, ifindex);
161 strcpy(result, dev->name);
163 strcpy(result, "???");
169 static int bcm_proc_show(struct seq_file *m, void *v)
171 char ifname[IFNAMSIZ];
172 struct net *net = m->private;
173 struct sock *sk = (struct sock *)PDE_DATA(m->file->f_inode);
174 struct bcm_sock *bo = bcm_sk(sk);
177 seq_printf(m, ">>> socket %pK", sk->sk_socket);
178 seq_printf(m, " / sk %pK", sk);
179 seq_printf(m, " / bo %pK", bo);
180 seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
181 seq_printf(m, " / bound %s", bcm_proc_getifname(net, ifname, bo->ifindex));
182 seq_printf(m, " <<<\n");
184 list_for_each_entry(op, &bo->rx_ops, list) {
186 unsigned long reduction;
188 /* print only active entries & prevent division by zero */
192 seq_printf(m, "rx_op: %03X %-5s ", op->can_id,
193 bcm_proc_getifname(net, ifname, op->ifindex));
195 if (op->flags & CAN_FD_FRAME)
196 seq_printf(m, "(%u)", op->nframes);
198 seq_printf(m, "[%u]", op->nframes);
200 seq_printf(m, "%c ", (op->flags & RX_CHECK_DLC) ? 'd' : ' ');
203 seq_printf(m, "timeo=%lld ",
204 (long long)ktime_to_us(op->kt_ival1));
207 seq_printf(m, "thr=%lld ",
208 (long long)ktime_to_us(op->kt_ival2));
210 seq_printf(m, "# recv %ld (%ld) => reduction: ",
211 op->frames_filtered, op->frames_abs);
213 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
215 seq_printf(m, "%s%ld%%\n",
216 (reduction == 100) ? "near " : "", reduction);
219 list_for_each_entry(op, &bo->tx_ops, list) {
221 seq_printf(m, "tx_op: %03X %s ", op->can_id,
222 bcm_proc_getifname(net, ifname, op->ifindex));
224 if (op->flags & CAN_FD_FRAME)
225 seq_printf(m, "(%u) ", op->nframes);
227 seq_printf(m, "[%u] ", op->nframes);
230 seq_printf(m, "t1=%lld ",
231 (long long)ktime_to_us(op->kt_ival1));
234 seq_printf(m, "t2=%lld ",
235 (long long)ktime_to_us(op->kt_ival2));
237 seq_printf(m, "# sent %ld\n", op->frames_abs);
242 #endif /* CONFIG_PROC_FS */
245 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
246 * of the given bcm tx op
248 static void bcm_can_tx(struct bcm_op *op)
251 struct net_device *dev;
252 struct canfd_frame *cf = op->frames + op->cfsiz * op->currframe;
254 /* no target device? => exit */
258 dev = dev_get_by_index(sock_net(op->sk), op->ifindex);
260 /* RFC: should this bcm_op remove itself here? */
264 skb = alloc_skb(op->cfsiz + sizeof(struct can_skb_priv), gfp_any());
268 can_skb_reserve(skb);
269 can_skb_prv(skb)->ifindex = dev->ifindex;
270 can_skb_prv(skb)->skbcnt = 0;
272 skb_put_data(skb, cf, op->cfsiz);
274 /* send with loopback */
276 can_skb_set_owner(skb, op->sk);
279 /* update statistics */
283 /* reached last frame? */
284 if (op->currframe >= op->nframes)
291 * bcm_send_to_user - send a BCM message to the userspace
292 * (consisting of bcm_msg_head + x CAN frames)
294 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
295 struct canfd_frame *frames, int has_timestamp)
298 struct canfd_frame *firstframe;
299 struct sockaddr_can *addr;
300 struct sock *sk = op->sk;
301 unsigned int datalen = head->nframes * op->cfsiz;
304 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
308 skb_put_data(skb, head, sizeof(*head));
311 /* CAN frames starting here */
312 firstframe = (struct canfd_frame *)skb_tail_pointer(skb);
314 skb_put_data(skb, frames, datalen);
317 * the BCM uses the flags-element of the canfd_frame
318 * structure for internal purposes. This is only
319 * relevant for updates that are generated by the
320 * BCM, where nframes is 1
322 if (head->nframes == 1)
323 firstframe->flags &= BCM_CAN_FLAGS_MASK;
327 /* restore rx timestamp */
328 skb->tstamp = op->rx_stamp;
332 * Put the datagram to the queue so that bcm_recvmsg() can
333 * get it from there. We need to pass the interface index to
334 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
335 * containing the interface index.
338 sock_skb_cb_check_size(sizeof(struct sockaddr_can));
339 addr = (struct sockaddr_can *)skb->cb;
340 memset(addr, 0, sizeof(*addr));
341 addr->can_family = AF_CAN;
342 addr->can_ifindex = op->rx_ifindex;
344 err = sock_queue_rcv_skb(sk, skb);
346 struct bcm_sock *bo = bcm_sk(sk);
349 /* don't care about overflows in this statistic */
350 bo->dropped_usr_msgs++;
354 static void bcm_tx_start_timer(struct bcm_op *op)
356 if (op->kt_ival1 && op->count)
357 hrtimer_start(&op->timer,
358 ktime_add(ktime_get(), op->kt_ival1),
360 else if (op->kt_ival2)
361 hrtimer_start(&op->timer,
362 ktime_add(ktime_get(), op->kt_ival2),
366 static void bcm_tx_timeout_tsklet(unsigned long data)
368 struct bcm_op *op = (struct bcm_op *)data;
369 struct bcm_msg_head msg_head;
371 if (op->kt_ival1 && (op->count > 0)) {
374 if (!op->count && (op->flags & TX_COUNTEVT)) {
376 /* create notification to user */
377 msg_head.opcode = TX_EXPIRED;
378 msg_head.flags = op->flags;
379 msg_head.count = op->count;
380 msg_head.ival1 = op->ival1;
381 msg_head.ival2 = op->ival2;
382 msg_head.can_id = op->can_id;
383 msg_head.nframes = 0;
385 bcm_send_to_user(op, &msg_head, NULL, 0);
389 } else if (op->kt_ival2)
392 bcm_tx_start_timer(op);
396 * bcm_tx_timeout_handler - performs cyclic CAN frame transmissions
398 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
400 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
402 tasklet_schedule(&op->tsklet);
404 return HRTIMER_NORESTART;
408 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
410 static void bcm_rx_changed(struct bcm_op *op, struct canfd_frame *data)
412 struct bcm_msg_head head;
414 /* update statistics */
415 op->frames_filtered++;
417 /* prevent statistics overflow */
418 if (op->frames_filtered > ULONG_MAX/100)
419 op->frames_filtered = op->frames_abs = 0;
421 /* this element is not throttled anymore */
422 data->flags &= (BCM_CAN_FLAGS_MASK|RX_RECV);
424 head.opcode = RX_CHANGED;
425 head.flags = op->flags;
426 head.count = op->count;
427 head.ival1 = op->ival1;
428 head.ival2 = op->ival2;
429 head.can_id = op->can_id;
432 bcm_send_to_user(op, &head, data, 1);
436 * bcm_rx_update_and_send - process a detected relevant receive content change
437 * 1. update the last received data
438 * 2. send a notification to the user (if possible)
440 static void bcm_rx_update_and_send(struct bcm_op *op,
441 struct canfd_frame *lastdata,
442 const struct canfd_frame *rxdata)
444 memcpy(lastdata, rxdata, op->cfsiz);
446 /* mark as used and throttled by default */
447 lastdata->flags |= (RX_RECV|RX_THR);
449 /* throttling mode inactive ? */
451 /* send RX_CHANGED to the user immediately */
452 bcm_rx_changed(op, lastdata);
456 /* with active throttling timer we are just done here */
457 if (hrtimer_active(&op->thrtimer))
460 /* first reception with enabled throttling mode */
462 goto rx_changed_settime;
464 /* got a second frame inside a potential throttle period? */
465 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
466 ktime_to_us(op->kt_ival2)) {
467 /* do not send the saved data - only start throttle timer */
468 hrtimer_start(&op->thrtimer,
469 ktime_add(op->kt_lastmsg, op->kt_ival2),
474 /* the gap was that big, that throttling was not needed here */
476 bcm_rx_changed(op, lastdata);
477 op->kt_lastmsg = ktime_get();
481 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
482 * received data stored in op->last_frames[]
484 static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index,
485 const struct canfd_frame *rxdata)
487 struct canfd_frame *cf = op->frames + op->cfsiz * index;
488 struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
492 * no one uses the MSBs of flags for comparison,
493 * so we use it here to detect the first time of reception
496 if (!(lcf->flags & RX_RECV)) {
497 /* received data for the first time => send update to user */
498 bcm_rx_update_and_send(op, lcf, rxdata);
502 /* do a real check in CAN frame data section */
503 for (i = 0; i < rxdata->len; i += 8) {
504 if ((get_u64(cf, i) & get_u64(rxdata, i)) !=
505 (get_u64(cf, i) & get_u64(lcf, i))) {
506 bcm_rx_update_and_send(op, lcf, rxdata);
511 if (op->flags & RX_CHECK_DLC) {
512 /* do a real check in CAN frame length */
513 if (rxdata->len != lcf->len) {
514 bcm_rx_update_and_send(op, lcf, rxdata);
521 * bcm_rx_starttimer - enable timeout monitoring for CAN frame reception
523 static void bcm_rx_starttimer(struct bcm_op *op)
525 if (op->flags & RX_NO_AUTOTIMER)
529 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
532 static void bcm_rx_timeout_tsklet(unsigned long data)
534 struct bcm_op *op = (struct bcm_op *)data;
535 struct bcm_msg_head msg_head;
537 /* create notification to user */
538 msg_head.opcode = RX_TIMEOUT;
539 msg_head.flags = op->flags;
540 msg_head.count = op->count;
541 msg_head.ival1 = op->ival1;
542 msg_head.ival2 = op->ival2;
543 msg_head.can_id = op->can_id;
544 msg_head.nframes = 0;
546 bcm_send_to_user(op, &msg_head, NULL, 0);
550 * bcm_rx_timeout_handler - when the (cyclic) CAN frame reception timed out
552 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
554 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
556 /* schedule before NET_RX_SOFTIRQ */
557 tasklet_hi_schedule(&op->tsklet);
559 /* no restart of the timer is done here! */
561 /* if user wants to be informed, when cyclic CAN-Messages come back */
562 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
563 /* clear received CAN frames to indicate 'nothing received' */
564 memset(op->last_frames, 0, op->nframes * op->cfsiz);
567 return HRTIMER_NORESTART;
571 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
573 static inline int bcm_rx_do_flush(struct bcm_op *op, int update,
576 struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
578 if ((op->last_frames) && (lcf->flags & RX_THR)) {
580 bcm_rx_changed(op, lcf);
587 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
589 * update == 0 : just check if throttled data is available (any irq context)
590 * update == 1 : check and send throttled data to userspace (soft_irq context)
592 static int bcm_rx_thr_flush(struct bcm_op *op, int update)
596 if (op->nframes > 1) {
599 /* for MUX filter we start at index 1 */
600 for (i = 1; i < op->nframes; i++)
601 updated += bcm_rx_do_flush(op, update, i);
604 /* for RX_FILTER_ID and simple filter */
605 updated += bcm_rx_do_flush(op, update, 0);
611 static void bcm_rx_thr_tsklet(unsigned long data)
613 struct bcm_op *op = (struct bcm_op *)data;
615 /* push the changed data to the userspace */
616 bcm_rx_thr_flush(op, 1);
620 * bcm_rx_thr_handler - the time for blocked content updates is over now:
621 * Check for throttled data and send it to the userspace
623 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
625 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
627 tasklet_schedule(&op->thrtsklet);
629 if (bcm_rx_thr_flush(op, 0)) {
630 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
631 return HRTIMER_RESTART;
633 /* rearm throttle handling */
635 return HRTIMER_NORESTART;
640 * bcm_rx_handler - handle a CAN frame reception
642 static void bcm_rx_handler(struct sk_buff *skb, void *data)
644 struct bcm_op *op = (struct bcm_op *)data;
645 const struct canfd_frame *rxframe = (struct canfd_frame *)skb->data;
648 if (op->can_id != rxframe->can_id)
651 /* make sure to handle the correct frame type (CAN / CAN FD) */
652 if (skb->len != op->cfsiz)
655 /* disable timeout */
656 hrtimer_cancel(&op->timer);
658 /* save rx timestamp */
659 op->rx_stamp = skb->tstamp;
660 /* save originator for recvfrom() */
661 op->rx_ifindex = skb->dev->ifindex;
662 /* update statistics */
665 if (op->flags & RX_RTR_FRAME) {
666 /* send reply for RTR-request (placed in op->frames[0]) */
671 if (op->flags & RX_FILTER_ID) {
672 /* the easiest case */
673 bcm_rx_update_and_send(op, op->last_frames, rxframe);
677 if (op->nframes == 1) {
678 /* simple compare with index 0 */
679 bcm_rx_cmp_to_index(op, 0, rxframe);
683 if (op->nframes > 1) {
687 * find the first multiplex mask that fits.
688 * Remark: The MUX-mask is stored in index 0 - but only the
689 * first 64 bits of the frame data[] are relevant (CAN FD)
692 for (i = 1; i < op->nframes; i++) {
693 if ((get_u64(op->frames, 0) & get_u64(rxframe, 0)) ==
694 (get_u64(op->frames, 0) &
695 get_u64(op->frames + op->cfsiz * i, 0))) {
696 bcm_rx_cmp_to_index(op, i, rxframe);
703 bcm_rx_starttimer(op);
707 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
709 static struct bcm_op *bcm_find_op(struct list_head *ops,
710 struct bcm_msg_head *mh, int ifindex)
714 list_for_each_entry(op, ops, list) {
715 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
716 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME))
723 static void bcm_remove_op(struct bcm_op *op)
725 if (op->tsklet.func) {
726 while (test_bit(TASKLET_STATE_SCHED, &op->tsklet.state) ||
727 test_bit(TASKLET_STATE_RUN, &op->tsklet.state) ||
728 hrtimer_active(&op->timer)) {
729 hrtimer_cancel(&op->timer);
730 tasklet_kill(&op->tsklet);
734 if (op->thrtsklet.func) {
735 while (test_bit(TASKLET_STATE_SCHED, &op->thrtsklet.state) ||
736 test_bit(TASKLET_STATE_RUN, &op->thrtsklet.state) ||
737 hrtimer_active(&op->thrtimer)) {
738 hrtimer_cancel(&op->thrtimer);
739 tasklet_kill(&op->thrtsklet);
743 if ((op->frames) && (op->frames != &op->sframe))
746 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
747 kfree(op->last_frames);
752 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
754 if (op->rx_reg_dev == dev) {
755 can_rx_unregister(dev_net(dev), dev, op->can_id,
756 REGMASK(op->can_id), bcm_rx_handler, op);
758 /* mark as removed subscription */
759 op->rx_reg_dev = NULL;
761 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
762 "mismatch %p %p\n", op->rx_reg_dev, dev);
766 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
768 static int bcm_delete_rx_op(struct list_head *ops, struct bcm_msg_head *mh,
771 struct bcm_op *op, *n;
773 list_for_each_entry_safe(op, n, ops, list) {
774 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
775 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
778 * Don't care if we're bound or not (due to netdev
779 * problems) can_rx_unregister() is always a save
784 * Only remove subscriptions that had not
785 * been removed due to NETDEV_UNREGISTER
788 if (op->rx_reg_dev) {
789 struct net_device *dev;
791 dev = dev_get_by_index(sock_net(op->sk),
794 bcm_rx_unreg(dev, op);
799 can_rx_unregister(sock_net(op->sk), NULL,
810 return 0; /* not found */
814 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
816 static int bcm_delete_tx_op(struct list_head *ops, struct bcm_msg_head *mh,
819 struct bcm_op *op, *n;
821 list_for_each_entry_safe(op, n, ops, list) {
822 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
823 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
830 return 0; /* not found */
834 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
836 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
839 struct bcm_op *op = bcm_find_op(ops, msg_head, ifindex);
844 /* put current values into msg_head */
845 msg_head->flags = op->flags;
846 msg_head->count = op->count;
847 msg_head->ival1 = op->ival1;
848 msg_head->ival2 = op->ival2;
849 msg_head->nframes = op->nframes;
851 bcm_send_to_user(op, msg_head, op->frames, 0);
857 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
859 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
860 int ifindex, struct sock *sk)
862 struct bcm_sock *bo = bcm_sk(sk);
864 struct canfd_frame *cf;
868 /* we need a real device to send frames */
872 /* check nframes boundaries - we need at least one CAN frame */
873 if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES)
876 /* check the given can_id */
877 op = bcm_find_op(&bo->tx_ops, msg_head, ifindex);
879 /* update existing BCM operation */
882 * Do we need more space for the CAN frames than currently
883 * allocated? -> This is a _really_ unusual use-case and
884 * therefore (complexity / locking) it is not supported.
886 if (msg_head->nframes > op->nframes)
889 /* update CAN frames content */
890 for (i = 0; i < msg_head->nframes; i++) {
892 cf = op->frames + op->cfsiz * i;
893 err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz);
895 if (op->flags & CAN_FD_FRAME) {
906 if (msg_head->flags & TX_CP_CAN_ID) {
907 /* copy can_id into frame */
908 cf->can_id = msg_head->can_id;
911 op->flags = msg_head->flags;
914 /* insert new BCM operation for the given can_id */
916 op = kzalloc(OPSIZ, GFP_KERNEL);
920 op->can_id = msg_head->can_id;
921 op->cfsiz = CFSIZ(msg_head->flags);
922 op->flags = msg_head->flags;
924 /* create array for CAN frames and copy the data */
925 if (msg_head->nframes > 1) {
926 op->frames = kmalloc_array(msg_head->nframes,
934 op->frames = &op->sframe;
936 for (i = 0; i < msg_head->nframes; i++) {
938 cf = op->frames + op->cfsiz * i;
939 err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz);
941 if (op->flags & CAN_FD_FRAME) {
950 if (op->frames != &op->sframe)
956 if (msg_head->flags & TX_CP_CAN_ID) {
957 /* copy can_id into frame */
958 cf->can_id = msg_head->can_id;
962 /* tx_ops never compare with previous received messages */
963 op->last_frames = NULL;
965 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
967 op->ifindex = ifindex;
969 /* initialize uninitialized (kzalloc) structure */
970 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
971 op->timer.function = bcm_tx_timeout_handler;
973 /* initialize tasklet for tx countevent notification */
974 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
977 /* currently unused in tx_ops */
978 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
980 /* add this bcm_op to the list of the tx_ops */
981 list_add(&op->list, &bo->tx_ops);
983 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
985 if (op->nframes != msg_head->nframes) {
986 op->nframes = msg_head->nframes;
987 /* start multiple frame transmission with index 0 */
993 if (op->flags & TX_RESET_MULTI_IDX) {
994 /* start multiple frame transmission with index 0 */
998 if (op->flags & SETTIMER) {
999 /* set timer values */
1000 op->count = msg_head->count;
1001 op->ival1 = msg_head->ival1;
1002 op->ival2 = msg_head->ival2;
1003 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
1004 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
1006 /* disable an active timer due to zero values? */
1007 if (!op->kt_ival1 && !op->kt_ival2)
1008 hrtimer_cancel(&op->timer);
1011 if (op->flags & STARTTIMER) {
1012 hrtimer_cancel(&op->timer);
1013 /* spec: send CAN frame when starting timer */
1014 op->flags |= TX_ANNOUNCE;
1017 if (op->flags & TX_ANNOUNCE) {
1023 if (op->flags & STARTTIMER)
1024 bcm_tx_start_timer(op);
1026 return msg_head->nframes * op->cfsiz + MHSIZ;
1030 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1032 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1033 int ifindex, struct sock *sk)
1035 struct bcm_sock *bo = bcm_sk(sk);
1040 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1041 /* be robust against wrong usage ... */
1042 msg_head->flags |= RX_FILTER_ID;
1043 /* ignore trailing garbage */
1044 msg_head->nframes = 0;
1047 /* the first element contains the mux-mask => MAX_NFRAMES + 1 */
1048 if (msg_head->nframes > MAX_NFRAMES + 1)
1051 if ((msg_head->flags & RX_RTR_FRAME) &&
1052 ((msg_head->nframes != 1) ||
1053 (!(msg_head->can_id & CAN_RTR_FLAG))))
1056 /* check the given can_id */
1057 op = bcm_find_op(&bo->rx_ops, msg_head, ifindex);
1059 /* update existing BCM operation */
1062 * Do we need more space for the CAN frames than currently
1063 * allocated? -> This is a _really_ unusual use-case and
1064 * therefore (complexity / locking) it is not supported.
1066 if (msg_head->nframes > op->nframes)
1069 if (msg_head->nframes) {
1070 /* update CAN frames content */
1071 err = memcpy_from_msg(op->frames, msg,
1072 msg_head->nframes * op->cfsiz);
1076 /* clear last_frames to indicate 'nothing received' */
1077 memset(op->last_frames, 0, msg_head->nframes * op->cfsiz);
1080 op->nframes = msg_head->nframes;
1081 op->flags = msg_head->flags;
1083 /* Only an update -> do not call can_rx_register() */
1087 /* insert new BCM operation for the given can_id */
1088 op = kzalloc(OPSIZ, GFP_KERNEL);
1092 op->can_id = msg_head->can_id;
1093 op->nframes = msg_head->nframes;
1094 op->cfsiz = CFSIZ(msg_head->flags);
1095 op->flags = msg_head->flags;
1097 if (msg_head->nframes > 1) {
1098 /* create array for CAN frames and copy the data */
1099 op->frames = kmalloc_array(msg_head->nframes,
1107 /* create and init array for received CAN frames */
1108 op->last_frames = kcalloc(msg_head->nframes,
1111 if (!op->last_frames) {
1118 op->frames = &op->sframe;
1119 op->last_frames = &op->last_sframe;
1122 if (msg_head->nframes) {
1123 err = memcpy_from_msg(op->frames, msg,
1124 msg_head->nframes * op->cfsiz);
1126 if (op->frames != &op->sframe)
1128 if (op->last_frames != &op->last_sframe)
1129 kfree(op->last_frames);
1135 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1137 op->ifindex = ifindex;
1139 /* ifindex for timeout events w/o previous frame reception */
1140 op->rx_ifindex = ifindex;
1142 /* initialize uninitialized (kzalloc) structure */
1143 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1144 op->timer.function = bcm_rx_timeout_handler;
1146 /* initialize tasklet for rx timeout notification */
1147 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1148 (unsigned long) op);
1150 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1151 op->thrtimer.function = bcm_rx_thr_handler;
1153 /* initialize tasklet for rx throttle handling */
1154 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1155 (unsigned long) op);
1157 /* add this bcm_op to the list of the rx_ops */
1158 list_add(&op->list, &bo->rx_ops);
1160 /* call can_rx_register() */
1163 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1167 if (op->flags & RX_RTR_FRAME) {
1168 struct canfd_frame *frame0 = op->frames;
1170 /* no timers in RTR-mode */
1171 hrtimer_cancel(&op->thrtimer);
1172 hrtimer_cancel(&op->timer);
1175 * funny feature in RX(!)_SETUP only for RTR-mode:
1176 * copy can_id into frame BUT without RTR-flag to
1177 * prevent a full-load-loopback-test ... ;-]
1179 if ((op->flags & TX_CP_CAN_ID) ||
1180 (frame0->can_id == op->can_id))
1181 frame0->can_id = op->can_id & ~CAN_RTR_FLAG;
1184 if (op->flags & SETTIMER) {
1186 /* set timer value */
1187 op->ival1 = msg_head->ival1;
1188 op->ival2 = msg_head->ival2;
1189 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
1190 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
1192 /* disable an active timer due to zero value? */
1194 hrtimer_cancel(&op->timer);
1197 * In any case cancel the throttle timer, flush
1198 * potentially blocked msgs and reset throttle handling
1201 hrtimer_cancel(&op->thrtimer);
1202 bcm_rx_thr_flush(op, 1);
1205 if ((op->flags & STARTTIMER) && op->kt_ival1)
1206 hrtimer_start(&op->timer, op->kt_ival1,
1210 /* now we can register for can_ids, if we added a new bcm_op */
1211 if (do_rx_register) {
1213 struct net_device *dev;
1215 dev = dev_get_by_index(sock_net(sk), ifindex);
1217 err = can_rx_register(sock_net(sk), dev,
1219 REGMASK(op->can_id),
1223 op->rx_reg_dev = dev;
1228 err = can_rx_register(sock_net(sk), NULL, op->can_id,
1229 REGMASK(op->can_id),
1230 bcm_rx_handler, op, "bcm", sk);
1232 /* this bcm rx op is broken -> remove it */
1233 list_del(&op->list);
1239 return msg_head->nframes * op->cfsiz + MHSIZ;
1243 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1245 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk,
1248 struct sk_buff *skb;
1249 struct net_device *dev;
1252 /* we need a real device to send frames */
1256 skb = alloc_skb(cfsiz + sizeof(struct can_skb_priv), GFP_KERNEL);
1260 can_skb_reserve(skb);
1262 err = memcpy_from_msg(skb_put(skb, cfsiz), msg, cfsiz);
1268 dev = dev_get_by_index(sock_net(sk), ifindex);
1274 can_skb_prv(skb)->ifindex = dev->ifindex;
1275 can_skb_prv(skb)->skbcnt = 0;
1277 can_skb_set_owner(skb, sk);
1278 err = can_send(skb, 1); /* send with loopback */
1284 return cfsiz + MHSIZ;
1288 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1290 static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
1292 struct sock *sk = sock->sk;
1293 struct bcm_sock *bo = bcm_sk(sk);
1294 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1295 struct bcm_msg_head msg_head;
1297 int ret; /* read bytes or error codes as return value */
1302 /* check for valid message length from userspace */
1306 /* read message head information */
1307 ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ);
1311 cfsiz = CFSIZ(msg_head.flags);
1312 if ((size - MHSIZ) % cfsiz)
1315 /* check for alternative ifindex for this bcm_op */
1317 if (!ifindex && msg->msg_name) {
1318 /* no bound device as default => check msg_name */
1319 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
1321 if (msg->msg_namelen < sizeof(*addr))
1324 if (addr->can_family != AF_CAN)
1327 /* ifindex from sendto() */
1328 ifindex = addr->can_ifindex;
1331 struct net_device *dev;
1333 dev = dev_get_by_index(sock_net(sk), ifindex);
1337 if (dev->type != ARPHRD_CAN) {
1348 switch (msg_head.opcode) {
1351 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1355 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1359 if (bcm_delete_tx_op(&bo->tx_ops, &msg_head, ifindex))
1366 if (bcm_delete_rx_op(&bo->rx_ops, &msg_head, ifindex))
1373 /* reuse msg_head for the reply to TX_READ */
1374 msg_head.opcode = TX_STATUS;
1375 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1379 /* reuse msg_head for the reply to RX_READ */
1380 msg_head.opcode = RX_STATUS;
1381 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1385 /* we need exactly one CAN frame behind the msg head */
1386 if ((msg_head.nframes != 1) || (size != cfsiz + MHSIZ))
1389 ret = bcm_tx_send(msg, ifindex, sk, cfsiz);
1403 * notification handler for netdevice status changes
1405 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1408 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1409 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1410 struct sock *sk = &bo->sk;
1412 int notify_enodev = 0;
1414 if (!net_eq(dev_net(dev), sock_net(sk)))
1417 if (dev->type != ARPHRD_CAN)
1422 case NETDEV_UNREGISTER:
1425 /* remove device specific receive entries */
1426 list_for_each_entry(op, &bo->rx_ops, list)
1427 if (op->rx_reg_dev == dev)
1428 bcm_rx_unreg(dev, op);
1430 /* remove device reference, if this is our bound device */
1431 if (bo->bound && bo->ifindex == dev->ifindex) {
1439 if (notify_enodev) {
1440 sk->sk_err = ENODEV;
1441 if (!sock_flag(sk, SOCK_DEAD))
1442 sk->sk_error_report(sk);
1447 if (bo->bound && bo->ifindex == dev->ifindex) {
1448 sk->sk_err = ENETDOWN;
1449 if (!sock_flag(sk, SOCK_DEAD))
1450 sk->sk_error_report(sk);
1458 * initial settings for all BCM sockets to be set at socket creation time
1460 static int bcm_init(struct sock *sk)
1462 struct bcm_sock *bo = bcm_sk(sk);
1466 bo->dropped_usr_msgs = 0;
1467 bo->bcm_proc_read = NULL;
1469 INIT_LIST_HEAD(&bo->tx_ops);
1470 INIT_LIST_HEAD(&bo->rx_ops);
1473 bo->notifier.notifier_call = bcm_notifier;
1475 register_netdevice_notifier(&bo->notifier);
1481 * standard socket functions
1483 static int bcm_release(struct socket *sock)
1485 struct sock *sk = sock->sk;
1487 struct bcm_sock *bo;
1488 struct bcm_op *op, *next;
1496 /* remove bcm_ops, timer, rx_unregister(), etc. */
1498 unregister_netdevice_notifier(&bo->notifier);
1502 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1505 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1507 * Don't care if we're bound or not (due to netdev problems)
1508 * can_rx_unregister() is always a save thing to do here.
1512 * Only remove subscriptions that had not
1513 * been removed due to NETDEV_UNREGISTER
1516 if (op->rx_reg_dev) {
1517 struct net_device *dev;
1519 dev = dev_get_by_index(net, op->ifindex);
1521 bcm_rx_unreg(dev, op);
1526 can_rx_unregister(net, NULL, op->can_id,
1527 REGMASK(op->can_id),
1528 bcm_rx_handler, op);
1533 #if IS_ENABLED(CONFIG_PROC_FS)
1534 /* remove procfs entry */
1535 if (net->can.bcmproc_dir && bo->bcm_proc_read)
1536 remove_proc_entry(bo->procname, net->can.bcmproc_dir);
1537 #endif /* CONFIG_PROC_FS */
1539 /* remove device reference */
1554 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1557 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1558 struct sock *sk = sock->sk;
1559 struct bcm_sock *bo = bcm_sk(sk);
1560 struct net *net = sock_net(sk);
1563 if (len < sizeof(*addr))
1573 /* bind a device to this socket */
1574 if (addr->can_ifindex) {
1575 struct net_device *dev;
1577 dev = dev_get_by_index(net, addr->can_ifindex);
1582 if (dev->type != ARPHRD_CAN) {
1588 bo->ifindex = dev->ifindex;
1592 /* no interface reference for ifindex = 0 ('any' CAN device) */
1596 #if IS_ENABLED(CONFIG_PROC_FS)
1597 if (net->can.bcmproc_dir) {
1598 /* unique socket address as filename */
1599 sprintf(bo->procname, "%lu", sock_i_ino(sk));
1600 bo->bcm_proc_read = proc_create_net_single(bo->procname, 0644,
1601 net->can.bcmproc_dir,
1603 if (!bo->bcm_proc_read) {
1608 #endif /* CONFIG_PROC_FS */
1618 static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1621 struct sock *sk = sock->sk;
1622 struct sk_buff *skb;
1627 noblock = flags & MSG_DONTWAIT;
1628 flags &= ~MSG_DONTWAIT;
1629 skb = skb_recv_datagram(sk, flags, noblock, &error);
1633 if (skb->len < size)
1636 err = memcpy_to_msg(msg, skb->data, size);
1638 skb_free_datagram(sk, skb);
1642 sock_recv_ts_and_drops(msg, sk, skb);
1644 if (msg->msg_name) {
1645 __sockaddr_check_size(sizeof(struct sockaddr_can));
1646 msg->msg_namelen = sizeof(struct sockaddr_can);
1647 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1650 skb_free_datagram(sk, skb);
1655 static const struct proto_ops bcm_ops = {
1657 .release = bcm_release,
1658 .bind = sock_no_bind,
1659 .connect = bcm_connect,
1660 .socketpair = sock_no_socketpair,
1661 .accept = sock_no_accept,
1662 .getname = sock_no_getname,
1663 .poll = datagram_poll,
1664 .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
1665 .listen = sock_no_listen,
1666 .shutdown = sock_no_shutdown,
1667 .setsockopt = sock_no_setsockopt,
1668 .getsockopt = sock_no_getsockopt,
1669 .sendmsg = bcm_sendmsg,
1670 .recvmsg = bcm_recvmsg,
1671 .mmap = sock_no_mmap,
1672 .sendpage = sock_no_sendpage,
1675 static struct proto bcm_proto __read_mostly = {
1677 .owner = THIS_MODULE,
1678 .obj_size = sizeof(struct bcm_sock),
1682 static const struct can_proto bcm_can_proto = {
1684 .protocol = CAN_BCM,
1689 static int canbcm_pernet_init(struct net *net)
1691 #if IS_ENABLED(CONFIG_PROC_FS)
1692 /* create /proc/net/can-bcm directory */
1693 net->can.bcmproc_dir = proc_net_mkdir(net, "can-bcm", net->proc_net);
1694 #endif /* CONFIG_PROC_FS */
1699 static void canbcm_pernet_exit(struct net *net)
1701 #if IS_ENABLED(CONFIG_PROC_FS)
1702 /* remove /proc/net/can-bcm directory */
1703 if (net->can.bcmproc_dir)
1704 remove_proc_entry("can-bcm", net->proc_net);
1705 #endif /* CONFIG_PROC_FS */
1708 static struct pernet_operations canbcm_pernet_ops __read_mostly = {
1709 .init = canbcm_pernet_init,
1710 .exit = canbcm_pernet_exit,
1713 static int __init bcm_module_init(void)
1717 pr_info("can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n");
1719 err = can_proto_register(&bcm_can_proto);
1721 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1725 register_pernet_subsys(&canbcm_pernet_ops);
1729 static void __exit bcm_module_exit(void)
1731 can_proto_unregister(&bcm_can_proto);
1732 unregister_pernet_subsys(&canbcm_pernet_ops);
1735 module_init(bcm_module_init);
1736 module_exit(bcm_module_exit);