1 // SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
3 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
5 * Copyright (c) 2002-2017 Volkswagen Group Electronic Research
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
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.
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
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/interrupt.h>
46 #include <linux/hrtimer.h>
47 #include <linux/list.h>
48 #include <linux/proc_fs.h>
49 #include <linux/seq_file.h>
50 #include <linux/uio.h>
51 #include <linux/net.h>
52 #include <linux/netdevice.h>
53 #include <linux/socket.h>
54 #include <linux/if_arp.h>
55 #include <linux/skbuff.h>
56 #include <linux/can.h>
57 #include <linux/can/core.h>
58 #include <linux/can/skb.h>
59 #include <linux/can/bcm.h>
60 #include <linux/slab.h>
62 #include <net/net_namespace.h>
65 * To send multiple CAN frame content within TX_SETUP or to filter
66 * CAN messages with multiplex index within RX_SETUP, the number of
67 * different filters is limited to 256 due to the one byte index value.
69 #define MAX_NFRAMES 256
71 /* limit timers to 400 days for sending/timeouts */
72 #define BCM_TIMER_SEC_MAX (400 * 24 * 60 * 60)
74 /* use of last_frames[index].flags */
75 #define RX_RECV 0x40 /* received data for this element */
76 #define RX_THR 0x80 /* element not been sent due to throttle feature */
77 #define BCM_CAN_FLAGS_MASK 0x3F /* to clean private flags after usage */
79 /* get best masking value for can_rx_register() for a given single can_id */
80 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
81 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
82 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
84 #define CAN_BCM_VERSION "20170425"
86 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
87 MODULE_LICENSE("Dual BSD/GPL");
88 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
89 MODULE_ALIAS("can-proto-2");
92 * easy access to the first 64 bit of can(fd)_frame payload. cp->data is
93 * 64 bit aligned so the offset has to be multiples of 8 which is ensured
94 * by the only callers in bcm_rx_cmp_to_index() bcm_rx_handler().
96 static inline u64 get_u64(const struct canfd_frame *cp, int offset)
98 return *(u64 *)(cp->data + offset);
102 struct list_head list;
106 unsigned long frames_abs, frames_filtered;
107 struct bcm_timeval ival1, ival2;
108 struct hrtimer timer, thrtimer;
109 struct tasklet_struct tsklet, thrtsklet;
110 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
116 /* void pointers to arrays of struct can[fd]_frame */
119 struct canfd_frame sframe;
120 struct canfd_frame last_sframe;
122 struct net_device *rx_reg_dev;
129 struct notifier_block notifier;
130 struct list_head rx_ops;
131 struct list_head tx_ops;
132 unsigned long dropped_usr_msgs;
133 struct proc_dir_entry *bcm_proc_read;
134 char procname [32]; /* inode number in decimal with \0 */
137 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
139 return (struct bcm_sock *)sk;
142 static inline ktime_t bcm_timeval_to_ktime(struct bcm_timeval tv)
144 return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC);
147 /* check limitations for timeval provided by user */
148 static bool bcm_is_invalid_tv(struct bcm_msg_head *msg_head)
150 if ((msg_head->ival1.tv_sec < 0) ||
151 (msg_head->ival1.tv_sec > BCM_TIMER_SEC_MAX) ||
152 (msg_head->ival1.tv_usec < 0) ||
153 (msg_head->ival1.tv_usec >= USEC_PER_SEC) ||
154 (msg_head->ival2.tv_sec < 0) ||
155 (msg_head->ival2.tv_sec > BCM_TIMER_SEC_MAX) ||
156 (msg_head->ival2.tv_usec < 0) ||
157 (msg_head->ival2.tv_usec >= USEC_PER_SEC))
163 #define CFSIZ(flags) ((flags & CAN_FD_FRAME) ? CANFD_MTU : CAN_MTU)
164 #define OPSIZ sizeof(struct bcm_op)
165 #define MHSIZ sizeof(struct bcm_msg_head)
170 #if IS_ENABLED(CONFIG_PROC_FS)
171 static char *bcm_proc_getifname(struct net *net, char *result, int ifindex)
173 struct net_device *dev;
179 dev = dev_get_by_index_rcu(net, ifindex);
181 strcpy(result, dev->name);
183 strcpy(result, "???");
189 static int bcm_proc_show(struct seq_file *m, void *v)
191 char ifname[IFNAMSIZ];
192 struct net *net = m->private;
193 struct sock *sk = (struct sock *)PDE_DATA(m->file->f_inode);
194 struct bcm_sock *bo = bcm_sk(sk);
197 seq_printf(m, ">>> socket %pK", sk->sk_socket);
198 seq_printf(m, " / sk %pK", sk);
199 seq_printf(m, " / bo %pK", bo);
200 seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
201 seq_printf(m, " / bound %s", bcm_proc_getifname(net, ifname, bo->ifindex));
202 seq_printf(m, " <<<\n");
204 list_for_each_entry(op, &bo->rx_ops, list) {
206 unsigned long reduction;
208 /* print only active entries & prevent division by zero */
212 seq_printf(m, "rx_op: %03X %-5s ", op->can_id,
213 bcm_proc_getifname(net, ifname, op->ifindex));
215 if (op->flags & CAN_FD_FRAME)
216 seq_printf(m, "(%u)", op->nframes);
218 seq_printf(m, "[%u]", op->nframes);
220 seq_printf(m, "%c ", (op->flags & RX_CHECK_DLC) ? 'd' : ' ');
223 seq_printf(m, "timeo=%lld ",
224 (long long)ktime_to_us(op->kt_ival1));
227 seq_printf(m, "thr=%lld ",
228 (long long)ktime_to_us(op->kt_ival2));
230 seq_printf(m, "# recv %ld (%ld) => reduction: ",
231 op->frames_filtered, op->frames_abs);
233 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
235 seq_printf(m, "%s%ld%%\n",
236 (reduction == 100) ? "near " : "", reduction);
239 list_for_each_entry(op, &bo->tx_ops, list) {
241 seq_printf(m, "tx_op: %03X %s ", op->can_id,
242 bcm_proc_getifname(net, ifname, op->ifindex));
244 if (op->flags & CAN_FD_FRAME)
245 seq_printf(m, "(%u) ", op->nframes);
247 seq_printf(m, "[%u] ", op->nframes);
250 seq_printf(m, "t1=%lld ",
251 (long long)ktime_to_us(op->kt_ival1));
254 seq_printf(m, "t2=%lld ",
255 (long long)ktime_to_us(op->kt_ival2));
257 seq_printf(m, "# sent %ld\n", op->frames_abs);
262 #endif /* CONFIG_PROC_FS */
265 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
266 * of the given bcm tx op
268 static void bcm_can_tx(struct bcm_op *op)
271 struct net_device *dev;
272 struct canfd_frame *cf = op->frames + op->cfsiz * op->currframe;
274 /* no target device? => exit */
278 dev = dev_get_by_index(sock_net(op->sk), op->ifindex);
280 /* RFC: should this bcm_op remove itself here? */
284 skb = alloc_skb(op->cfsiz + sizeof(struct can_skb_priv), gfp_any());
288 can_skb_reserve(skb);
289 can_skb_prv(skb)->ifindex = dev->ifindex;
290 can_skb_prv(skb)->skbcnt = 0;
292 skb_put_data(skb, cf, op->cfsiz);
294 /* send with loopback */
296 can_skb_set_owner(skb, op->sk);
299 /* update statistics */
303 /* reached last frame? */
304 if (op->currframe >= op->nframes)
311 * bcm_send_to_user - send a BCM message to the userspace
312 * (consisting of bcm_msg_head + x CAN frames)
314 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
315 struct canfd_frame *frames, int has_timestamp)
318 struct canfd_frame *firstframe;
319 struct sockaddr_can *addr;
320 struct sock *sk = op->sk;
321 unsigned int datalen = head->nframes * op->cfsiz;
324 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
328 skb_put_data(skb, head, sizeof(*head));
331 /* CAN frames starting here */
332 firstframe = (struct canfd_frame *)skb_tail_pointer(skb);
334 skb_put_data(skb, frames, datalen);
337 * the BCM uses the flags-element of the canfd_frame
338 * structure for internal purposes. This is only
339 * relevant for updates that are generated by the
340 * BCM, where nframes is 1
342 if (head->nframes == 1)
343 firstframe->flags &= BCM_CAN_FLAGS_MASK;
347 /* restore rx timestamp */
348 skb->tstamp = op->rx_stamp;
352 * Put the datagram to the queue so that bcm_recvmsg() can
353 * get it from there. We need to pass the interface index to
354 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
355 * containing the interface index.
358 sock_skb_cb_check_size(sizeof(struct sockaddr_can));
359 addr = (struct sockaddr_can *)skb->cb;
360 memset(addr, 0, sizeof(*addr));
361 addr->can_family = AF_CAN;
362 addr->can_ifindex = op->rx_ifindex;
364 err = sock_queue_rcv_skb(sk, skb);
366 struct bcm_sock *bo = bcm_sk(sk);
369 /* don't care about overflows in this statistic */
370 bo->dropped_usr_msgs++;
374 static void bcm_tx_start_timer(struct bcm_op *op)
376 if (op->kt_ival1 && op->count)
377 hrtimer_start(&op->timer,
378 ktime_add(ktime_get(), op->kt_ival1),
380 else if (op->kt_ival2)
381 hrtimer_start(&op->timer,
382 ktime_add(ktime_get(), op->kt_ival2),
386 static void bcm_tx_timeout_tsklet(unsigned long data)
388 struct bcm_op *op = (struct bcm_op *)data;
389 struct bcm_msg_head msg_head;
391 if (op->kt_ival1 && (op->count > 0)) {
394 if (!op->count && (op->flags & TX_COUNTEVT)) {
396 /* create notification to user */
397 msg_head.opcode = TX_EXPIRED;
398 msg_head.flags = op->flags;
399 msg_head.count = op->count;
400 msg_head.ival1 = op->ival1;
401 msg_head.ival2 = op->ival2;
402 msg_head.can_id = op->can_id;
403 msg_head.nframes = 0;
405 bcm_send_to_user(op, &msg_head, NULL, 0);
409 } else if (op->kt_ival2)
412 bcm_tx_start_timer(op);
416 * bcm_tx_timeout_handler - performs cyclic CAN frame transmissions
418 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
420 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
422 tasklet_schedule(&op->tsklet);
424 return HRTIMER_NORESTART;
428 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
430 static void bcm_rx_changed(struct bcm_op *op, struct canfd_frame *data)
432 struct bcm_msg_head head;
434 /* update statistics */
435 op->frames_filtered++;
437 /* prevent statistics overflow */
438 if (op->frames_filtered > ULONG_MAX/100)
439 op->frames_filtered = op->frames_abs = 0;
441 /* this element is not throttled anymore */
442 data->flags &= (BCM_CAN_FLAGS_MASK|RX_RECV);
444 head.opcode = RX_CHANGED;
445 head.flags = op->flags;
446 head.count = op->count;
447 head.ival1 = op->ival1;
448 head.ival2 = op->ival2;
449 head.can_id = op->can_id;
452 bcm_send_to_user(op, &head, data, 1);
456 * bcm_rx_update_and_send - process a detected relevant receive content change
457 * 1. update the last received data
458 * 2. send a notification to the user (if possible)
460 static void bcm_rx_update_and_send(struct bcm_op *op,
461 struct canfd_frame *lastdata,
462 const struct canfd_frame *rxdata)
464 memcpy(lastdata, rxdata, op->cfsiz);
466 /* mark as used and throttled by default */
467 lastdata->flags |= (RX_RECV|RX_THR);
469 /* throttling mode inactive ? */
471 /* send RX_CHANGED to the user immediately */
472 bcm_rx_changed(op, lastdata);
476 /* with active throttling timer we are just done here */
477 if (hrtimer_active(&op->thrtimer))
480 /* first reception with enabled throttling mode */
482 goto rx_changed_settime;
484 /* got a second frame inside a potential throttle period? */
485 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
486 ktime_to_us(op->kt_ival2)) {
487 /* do not send the saved data - only start throttle timer */
488 hrtimer_start(&op->thrtimer,
489 ktime_add(op->kt_lastmsg, op->kt_ival2),
494 /* the gap was that big, that throttling was not needed here */
496 bcm_rx_changed(op, lastdata);
497 op->kt_lastmsg = ktime_get();
501 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
502 * received data stored in op->last_frames[]
504 static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index,
505 const struct canfd_frame *rxdata)
507 struct canfd_frame *cf = op->frames + op->cfsiz * index;
508 struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
512 * no one uses the MSBs of flags for comparison,
513 * so we use it here to detect the first time of reception
516 if (!(lcf->flags & RX_RECV)) {
517 /* received data for the first time => send update to user */
518 bcm_rx_update_and_send(op, lcf, rxdata);
522 /* do a real check in CAN frame data section */
523 for (i = 0; i < rxdata->len; i += 8) {
524 if ((get_u64(cf, i) & get_u64(rxdata, i)) !=
525 (get_u64(cf, i) & get_u64(lcf, i))) {
526 bcm_rx_update_and_send(op, lcf, rxdata);
531 if (op->flags & RX_CHECK_DLC) {
532 /* do a real check in CAN frame length */
533 if (rxdata->len != lcf->len) {
534 bcm_rx_update_and_send(op, lcf, rxdata);
541 * bcm_rx_starttimer - enable timeout monitoring for CAN frame reception
543 static void bcm_rx_starttimer(struct bcm_op *op)
545 if (op->flags & RX_NO_AUTOTIMER)
549 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
552 static void bcm_rx_timeout_tsklet(unsigned long data)
554 struct bcm_op *op = (struct bcm_op *)data;
555 struct bcm_msg_head msg_head;
557 /* create notification to user */
558 msg_head.opcode = RX_TIMEOUT;
559 msg_head.flags = op->flags;
560 msg_head.count = op->count;
561 msg_head.ival1 = op->ival1;
562 msg_head.ival2 = op->ival2;
563 msg_head.can_id = op->can_id;
564 msg_head.nframes = 0;
566 bcm_send_to_user(op, &msg_head, NULL, 0);
570 * bcm_rx_timeout_handler - when the (cyclic) CAN frame reception timed out
572 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
574 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
576 /* schedule before NET_RX_SOFTIRQ */
577 tasklet_hi_schedule(&op->tsklet);
579 /* no restart of the timer is done here! */
581 /* if user wants to be informed, when cyclic CAN-Messages come back */
582 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
583 /* clear received CAN frames to indicate 'nothing received' */
584 memset(op->last_frames, 0, op->nframes * op->cfsiz);
587 return HRTIMER_NORESTART;
591 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
593 static inline int bcm_rx_do_flush(struct bcm_op *op, int update,
596 struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
598 if ((op->last_frames) && (lcf->flags & RX_THR)) {
600 bcm_rx_changed(op, lcf);
607 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
609 * update == 0 : just check if throttled data is available (any irq context)
610 * update == 1 : check and send throttled data to userspace (soft_irq context)
612 static int bcm_rx_thr_flush(struct bcm_op *op, int update)
616 if (op->nframes > 1) {
619 /* for MUX filter we start at index 1 */
620 for (i = 1; i < op->nframes; i++)
621 updated += bcm_rx_do_flush(op, update, i);
624 /* for RX_FILTER_ID and simple filter */
625 updated += bcm_rx_do_flush(op, update, 0);
631 static void bcm_rx_thr_tsklet(unsigned long data)
633 struct bcm_op *op = (struct bcm_op *)data;
635 /* push the changed data to the userspace */
636 bcm_rx_thr_flush(op, 1);
640 * bcm_rx_thr_handler - the time for blocked content updates is over now:
641 * Check for throttled data and send it to the userspace
643 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
645 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
647 tasklet_schedule(&op->thrtsklet);
649 if (bcm_rx_thr_flush(op, 0)) {
650 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
651 return HRTIMER_RESTART;
653 /* rearm throttle handling */
655 return HRTIMER_NORESTART;
660 * bcm_rx_handler - handle a CAN frame reception
662 static void bcm_rx_handler(struct sk_buff *skb, void *data)
664 struct bcm_op *op = (struct bcm_op *)data;
665 const struct canfd_frame *rxframe = (struct canfd_frame *)skb->data;
668 if (op->can_id != rxframe->can_id)
671 /* make sure to handle the correct frame type (CAN / CAN FD) */
672 if (skb->len != op->cfsiz)
675 /* disable timeout */
676 hrtimer_cancel(&op->timer);
678 /* save rx timestamp */
679 op->rx_stamp = skb->tstamp;
680 /* save originator for recvfrom() */
681 op->rx_ifindex = skb->dev->ifindex;
682 /* update statistics */
685 if (op->flags & RX_RTR_FRAME) {
686 /* send reply for RTR-request (placed in op->frames[0]) */
691 if (op->flags & RX_FILTER_ID) {
692 /* the easiest case */
693 bcm_rx_update_and_send(op, op->last_frames, rxframe);
697 if (op->nframes == 1) {
698 /* simple compare with index 0 */
699 bcm_rx_cmp_to_index(op, 0, rxframe);
703 if (op->nframes > 1) {
707 * find the first multiplex mask that fits.
708 * Remark: The MUX-mask is stored in index 0 - but only the
709 * first 64 bits of the frame data[] are relevant (CAN FD)
712 for (i = 1; i < op->nframes; i++) {
713 if ((get_u64(op->frames, 0) & get_u64(rxframe, 0)) ==
714 (get_u64(op->frames, 0) &
715 get_u64(op->frames + op->cfsiz * i, 0))) {
716 bcm_rx_cmp_to_index(op, i, rxframe);
723 bcm_rx_starttimer(op);
727 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
729 static struct bcm_op *bcm_find_op(struct list_head *ops,
730 struct bcm_msg_head *mh, int ifindex)
734 list_for_each_entry(op, ops, list) {
735 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
736 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME))
743 static void bcm_remove_op(struct bcm_op *op)
745 if (op->tsklet.func) {
746 while (test_bit(TASKLET_STATE_SCHED, &op->tsklet.state) ||
747 test_bit(TASKLET_STATE_RUN, &op->tsklet.state) ||
748 hrtimer_active(&op->timer)) {
749 hrtimer_cancel(&op->timer);
750 tasklet_kill(&op->tsklet);
754 if (op->thrtsklet.func) {
755 while (test_bit(TASKLET_STATE_SCHED, &op->thrtsklet.state) ||
756 test_bit(TASKLET_STATE_RUN, &op->thrtsklet.state) ||
757 hrtimer_active(&op->thrtimer)) {
758 hrtimer_cancel(&op->thrtimer);
759 tasklet_kill(&op->thrtsklet);
763 if ((op->frames) && (op->frames != &op->sframe))
766 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
767 kfree(op->last_frames);
772 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
774 if (op->rx_reg_dev == dev) {
775 can_rx_unregister(dev_net(dev), dev, op->can_id,
776 REGMASK(op->can_id), bcm_rx_handler, op);
778 /* mark as removed subscription */
779 op->rx_reg_dev = NULL;
781 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
782 "mismatch %p %p\n", op->rx_reg_dev, dev);
786 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
788 static int bcm_delete_rx_op(struct list_head *ops, struct bcm_msg_head *mh,
791 struct bcm_op *op, *n;
793 list_for_each_entry_safe(op, n, ops, list) {
794 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
795 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
798 * Don't care if we're bound or not (due to netdev
799 * problems) can_rx_unregister() is always a save
804 * Only remove subscriptions that had not
805 * been removed due to NETDEV_UNREGISTER
808 if (op->rx_reg_dev) {
809 struct net_device *dev;
811 dev = dev_get_by_index(sock_net(op->sk),
814 bcm_rx_unreg(dev, op);
819 can_rx_unregister(sock_net(op->sk), NULL,
830 return 0; /* not found */
834 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
836 static int bcm_delete_tx_op(struct list_head *ops, struct bcm_msg_head *mh,
839 struct bcm_op *op, *n;
841 list_for_each_entry_safe(op, n, ops, list) {
842 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
843 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
850 return 0; /* not found */
854 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
856 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
859 struct bcm_op *op = bcm_find_op(ops, msg_head, ifindex);
864 /* put current values into msg_head */
865 msg_head->flags = op->flags;
866 msg_head->count = op->count;
867 msg_head->ival1 = op->ival1;
868 msg_head->ival2 = op->ival2;
869 msg_head->nframes = op->nframes;
871 bcm_send_to_user(op, msg_head, op->frames, 0);
877 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
879 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
880 int ifindex, struct sock *sk)
882 struct bcm_sock *bo = bcm_sk(sk);
884 struct canfd_frame *cf;
888 /* we need a real device to send frames */
892 /* check nframes boundaries - we need at least one CAN frame */
893 if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES)
896 /* check timeval limitations */
897 if ((msg_head->flags & SETTIMER) && bcm_is_invalid_tv(msg_head))
900 /* check the given can_id */
901 op = bcm_find_op(&bo->tx_ops, msg_head, ifindex);
903 /* update existing BCM operation */
906 * Do we need more space for the CAN frames than currently
907 * allocated? -> This is a _really_ unusual use-case and
908 * therefore (complexity / locking) it is not supported.
910 if (msg_head->nframes > op->nframes)
913 /* update CAN frames content */
914 for (i = 0; i < msg_head->nframes; i++) {
916 cf = op->frames + op->cfsiz * i;
917 err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz);
919 if (op->flags & CAN_FD_FRAME) {
930 if (msg_head->flags & TX_CP_CAN_ID) {
931 /* copy can_id into frame */
932 cf->can_id = msg_head->can_id;
935 op->flags = msg_head->flags;
938 /* insert new BCM operation for the given can_id */
940 op = kzalloc(OPSIZ, GFP_KERNEL);
944 op->can_id = msg_head->can_id;
945 op->cfsiz = CFSIZ(msg_head->flags);
946 op->flags = msg_head->flags;
948 /* create array for CAN frames and copy the data */
949 if (msg_head->nframes > 1) {
950 op->frames = kmalloc_array(msg_head->nframes,
958 op->frames = &op->sframe;
960 for (i = 0; i < msg_head->nframes; i++) {
962 cf = op->frames + op->cfsiz * i;
963 err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz);
965 if (op->flags & CAN_FD_FRAME) {
974 if (op->frames != &op->sframe)
980 if (msg_head->flags & TX_CP_CAN_ID) {
981 /* copy can_id into frame */
982 cf->can_id = msg_head->can_id;
986 /* tx_ops never compare with previous received messages */
987 op->last_frames = NULL;
989 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
991 op->ifindex = ifindex;
993 /* initialize uninitialized (kzalloc) structure */
994 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
995 op->timer.function = bcm_tx_timeout_handler;
997 /* initialize tasklet for tx countevent notification */
998 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
1001 /* currently unused in tx_ops */
1002 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1004 /* add this bcm_op to the list of the tx_ops */
1005 list_add(&op->list, &bo->tx_ops);
1007 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1009 if (op->nframes != msg_head->nframes) {
1010 op->nframes = msg_head->nframes;
1011 /* start multiple frame transmission with index 0 */
1017 if (op->flags & TX_RESET_MULTI_IDX) {
1018 /* start multiple frame transmission with index 0 */
1022 if (op->flags & SETTIMER) {
1023 /* set timer values */
1024 op->count = msg_head->count;
1025 op->ival1 = msg_head->ival1;
1026 op->ival2 = msg_head->ival2;
1027 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
1028 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
1030 /* disable an active timer due to zero values? */
1031 if (!op->kt_ival1 && !op->kt_ival2)
1032 hrtimer_cancel(&op->timer);
1035 if (op->flags & STARTTIMER) {
1036 hrtimer_cancel(&op->timer);
1037 /* spec: send CAN frame when starting timer */
1038 op->flags |= TX_ANNOUNCE;
1041 if (op->flags & TX_ANNOUNCE) {
1047 if (op->flags & STARTTIMER)
1048 bcm_tx_start_timer(op);
1050 return msg_head->nframes * op->cfsiz + MHSIZ;
1054 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1056 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1057 int ifindex, struct sock *sk)
1059 struct bcm_sock *bo = bcm_sk(sk);
1064 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1065 /* be robust against wrong usage ... */
1066 msg_head->flags |= RX_FILTER_ID;
1067 /* ignore trailing garbage */
1068 msg_head->nframes = 0;
1071 /* the first element contains the mux-mask => MAX_NFRAMES + 1 */
1072 if (msg_head->nframes > MAX_NFRAMES + 1)
1075 if ((msg_head->flags & RX_RTR_FRAME) &&
1076 ((msg_head->nframes != 1) ||
1077 (!(msg_head->can_id & CAN_RTR_FLAG))))
1080 /* check timeval limitations */
1081 if ((msg_head->flags & SETTIMER) && bcm_is_invalid_tv(msg_head))
1084 /* check the given can_id */
1085 op = bcm_find_op(&bo->rx_ops, msg_head, ifindex);
1087 /* update existing BCM operation */
1090 * Do we need more space for the CAN frames than currently
1091 * allocated? -> This is a _really_ unusual use-case and
1092 * therefore (complexity / locking) it is not supported.
1094 if (msg_head->nframes > op->nframes)
1097 if (msg_head->nframes) {
1098 /* update CAN frames content */
1099 err = memcpy_from_msg(op->frames, msg,
1100 msg_head->nframes * op->cfsiz);
1104 /* clear last_frames to indicate 'nothing received' */
1105 memset(op->last_frames, 0, msg_head->nframes * op->cfsiz);
1108 op->nframes = msg_head->nframes;
1109 op->flags = msg_head->flags;
1111 /* Only an update -> do not call can_rx_register() */
1115 /* insert new BCM operation for the given can_id */
1116 op = kzalloc(OPSIZ, GFP_KERNEL);
1120 op->can_id = msg_head->can_id;
1121 op->nframes = msg_head->nframes;
1122 op->cfsiz = CFSIZ(msg_head->flags);
1123 op->flags = msg_head->flags;
1125 if (msg_head->nframes > 1) {
1126 /* create array for CAN frames and copy the data */
1127 op->frames = kmalloc_array(msg_head->nframes,
1135 /* create and init array for received CAN frames */
1136 op->last_frames = kcalloc(msg_head->nframes,
1139 if (!op->last_frames) {
1146 op->frames = &op->sframe;
1147 op->last_frames = &op->last_sframe;
1150 if (msg_head->nframes) {
1151 err = memcpy_from_msg(op->frames, msg,
1152 msg_head->nframes * op->cfsiz);
1154 if (op->frames != &op->sframe)
1156 if (op->last_frames != &op->last_sframe)
1157 kfree(op->last_frames);
1163 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1165 op->ifindex = ifindex;
1167 /* ifindex for timeout events w/o previous frame reception */
1168 op->rx_ifindex = ifindex;
1170 /* initialize uninitialized (kzalloc) structure */
1171 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1172 op->timer.function = bcm_rx_timeout_handler;
1174 /* initialize tasklet for rx timeout notification */
1175 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1176 (unsigned long) op);
1178 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1179 op->thrtimer.function = bcm_rx_thr_handler;
1181 /* initialize tasklet for rx throttle handling */
1182 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1183 (unsigned long) op);
1185 /* add this bcm_op to the list of the rx_ops */
1186 list_add(&op->list, &bo->rx_ops);
1188 /* call can_rx_register() */
1191 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1195 if (op->flags & RX_RTR_FRAME) {
1196 struct canfd_frame *frame0 = op->frames;
1198 /* no timers in RTR-mode */
1199 hrtimer_cancel(&op->thrtimer);
1200 hrtimer_cancel(&op->timer);
1203 * funny feature in RX(!)_SETUP only for RTR-mode:
1204 * copy can_id into frame BUT without RTR-flag to
1205 * prevent a full-load-loopback-test ... ;-]
1207 if ((op->flags & TX_CP_CAN_ID) ||
1208 (frame0->can_id == op->can_id))
1209 frame0->can_id = op->can_id & ~CAN_RTR_FLAG;
1212 if (op->flags & SETTIMER) {
1214 /* set timer value */
1215 op->ival1 = msg_head->ival1;
1216 op->ival2 = msg_head->ival2;
1217 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
1218 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
1220 /* disable an active timer due to zero value? */
1222 hrtimer_cancel(&op->timer);
1225 * In any case cancel the throttle timer, flush
1226 * potentially blocked msgs and reset throttle handling
1229 hrtimer_cancel(&op->thrtimer);
1230 bcm_rx_thr_flush(op, 1);
1233 if ((op->flags & STARTTIMER) && op->kt_ival1)
1234 hrtimer_start(&op->timer, op->kt_ival1,
1238 /* now we can register for can_ids, if we added a new bcm_op */
1239 if (do_rx_register) {
1241 struct net_device *dev;
1243 dev = dev_get_by_index(sock_net(sk), ifindex);
1245 err = can_rx_register(sock_net(sk), dev,
1247 REGMASK(op->can_id),
1251 op->rx_reg_dev = dev;
1256 err = can_rx_register(sock_net(sk), NULL, op->can_id,
1257 REGMASK(op->can_id),
1258 bcm_rx_handler, op, "bcm", sk);
1260 /* this bcm rx op is broken -> remove it */
1261 list_del(&op->list);
1267 return msg_head->nframes * op->cfsiz + MHSIZ;
1271 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1273 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk,
1276 struct sk_buff *skb;
1277 struct net_device *dev;
1280 /* we need a real device to send frames */
1284 skb = alloc_skb(cfsiz + sizeof(struct can_skb_priv), GFP_KERNEL);
1288 can_skb_reserve(skb);
1290 err = memcpy_from_msg(skb_put(skb, cfsiz), msg, cfsiz);
1296 dev = dev_get_by_index(sock_net(sk), ifindex);
1302 can_skb_prv(skb)->ifindex = dev->ifindex;
1303 can_skb_prv(skb)->skbcnt = 0;
1305 can_skb_set_owner(skb, sk);
1306 err = can_send(skb, 1); /* send with loopback */
1312 return cfsiz + MHSIZ;
1316 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1318 static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
1320 struct sock *sk = sock->sk;
1321 struct bcm_sock *bo = bcm_sk(sk);
1322 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1323 struct bcm_msg_head msg_head;
1325 int ret; /* read bytes or error codes as return value */
1330 /* check for valid message length from userspace */
1334 /* read message head information */
1335 ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ);
1339 cfsiz = CFSIZ(msg_head.flags);
1340 if ((size - MHSIZ) % cfsiz)
1343 /* check for alternative ifindex for this bcm_op */
1345 if (!ifindex && msg->msg_name) {
1346 /* no bound device as default => check msg_name */
1347 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
1349 if (msg->msg_namelen < sizeof(*addr))
1352 if (addr->can_family != AF_CAN)
1355 /* ifindex from sendto() */
1356 ifindex = addr->can_ifindex;
1359 struct net_device *dev;
1361 dev = dev_get_by_index(sock_net(sk), ifindex);
1365 if (dev->type != ARPHRD_CAN) {
1376 switch (msg_head.opcode) {
1379 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1383 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1387 if (bcm_delete_tx_op(&bo->tx_ops, &msg_head, ifindex))
1394 if (bcm_delete_rx_op(&bo->rx_ops, &msg_head, ifindex))
1401 /* reuse msg_head for the reply to TX_READ */
1402 msg_head.opcode = TX_STATUS;
1403 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1407 /* reuse msg_head for the reply to RX_READ */
1408 msg_head.opcode = RX_STATUS;
1409 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1413 /* we need exactly one CAN frame behind the msg head */
1414 if ((msg_head.nframes != 1) || (size != cfsiz + MHSIZ))
1417 ret = bcm_tx_send(msg, ifindex, sk, cfsiz);
1431 * notification handler for netdevice status changes
1433 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1436 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1437 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1438 struct sock *sk = &bo->sk;
1440 int notify_enodev = 0;
1442 if (!net_eq(dev_net(dev), sock_net(sk)))
1445 if (dev->type != ARPHRD_CAN)
1450 case NETDEV_UNREGISTER:
1453 /* remove device specific receive entries */
1454 list_for_each_entry(op, &bo->rx_ops, list)
1455 if (op->rx_reg_dev == dev)
1456 bcm_rx_unreg(dev, op);
1458 /* remove device reference, if this is our bound device */
1459 if (bo->bound && bo->ifindex == dev->ifindex) {
1467 if (notify_enodev) {
1468 sk->sk_err = ENODEV;
1469 if (!sock_flag(sk, SOCK_DEAD))
1470 sk->sk_error_report(sk);
1475 if (bo->bound && bo->ifindex == dev->ifindex) {
1476 sk->sk_err = ENETDOWN;
1477 if (!sock_flag(sk, SOCK_DEAD))
1478 sk->sk_error_report(sk);
1486 * initial settings for all BCM sockets to be set at socket creation time
1488 static int bcm_init(struct sock *sk)
1490 struct bcm_sock *bo = bcm_sk(sk);
1494 bo->dropped_usr_msgs = 0;
1495 bo->bcm_proc_read = NULL;
1497 INIT_LIST_HEAD(&bo->tx_ops);
1498 INIT_LIST_HEAD(&bo->rx_ops);
1501 bo->notifier.notifier_call = bcm_notifier;
1503 register_netdevice_notifier(&bo->notifier);
1509 * standard socket functions
1511 static int bcm_release(struct socket *sock)
1513 struct sock *sk = sock->sk;
1515 struct bcm_sock *bo;
1516 struct bcm_op *op, *next;
1524 /* remove bcm_ops, timer, rx_unregister(), etc. */
1526 unregister_netdevice_notifier(&bo->notifier);
1530 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1533 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1535 * Don't care if we're bound or not (due to netdev problems)
1536 * can_rx_unregister() is always a save thing to do here.
1540 * Only remove subscriptions that had not
1541 * been removed due to NETDEV_UNREGISTER
1544 if (op->rx_reg_dev) {
1545 struct net_device *dev;
1547 dev = dev_get_by_index(net, op->ifindex);
1549 bcm_rx_unreg(dev, op);
1554 can_rx_unregister(net, NULL, op->can_id,
1555 REGMASK(op->can_id),
1556 bcm_rx_handler, op);
1561 #if IS_ENABLED(CONFIG_PROC_FS)
1562 /* remove procfs entry */
1563 if (net->can.bcmproc_dir && bo->bcm_proc_read)
1564 remove_proc_entry(bo->procname, net->can.bcmproc_dir);
1565 #endif /* CONFIG_PROC_FS */
1567 /* remove device reference */
1582 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1585 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1586 struct sock *sk = sock->sk;
1587 struct bcm_sock *bo = bcm_sk(sk);
1588 struct net *net = sock_net(sk);
1591 if (len < sizeof(*addr))
1601 /* bind a device to this socket */
1602 if (addr->can_ifindex) {
1603 struct net_device *dev;
1605 dev = dev_get_by_index(net, addr->can_ifindex);
1610 if (dev->type != ARPHRD_CAN) {
1616 bo->ifindex = dev->ifindex;
1620 /* no interface reference for ifindex = 0 ('any' CAN device) */
1624 #if IS_ENABLED(CONFIG_PROC_FS)
1625 if (net->can.bcmproc_dir) {
1626 /* unique socket address as filename */
1627 sprintf(bo->procname, "%lu", sock_i_ino(sk));
1628 bo->bcm_proc_read = proc_create_net_single(bo->procname, 0644,
1629 net->can.bcmproc_dir,
1631 if (!bo->bcm_proc_read) {
1636 #endif /* CONFIG_PROC_FS */
1646 static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1649 struct sock *sk = sock->sk;
1650 struct sk_buff *skb;
1655 noblock = flags & MSG_DONTWAIT;
1656 flags &= ~MSG_DONTWAIT;
1657 skb = skb_recv_datagram(sk, flags, noblock, &error);
1661 if (skb->len < size)
1664 err = memcpy_to_msg(msg, skb->data, size);
1666 skb_free_datagram(sk, skb);
1670 sock_recv_ts_and_drops(msg, sk, skb);
1672 if (msg->msg_name) {
1673 __sockaddr_check_size(sizeof(struct sockaddr_can));
1674 msg->msg_namelen = sizeof(struct sockaddr_can);
1675 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1678 skb_free_datagram(sk, skb);
1683 int bcm_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
1686 /* no ioctls for socket layer -> hand it down to NIC layer */
1687 return -ENOIOCTLCMD;
1690 static const struct proto_ops bcm_ops = {
1692 .release = bcm_release,
1693 .bind = sock_no_bind,
1694 .connect = bcm_connect,
1695 .socketpair = sock_no_socketpair,
1696 .accept = sock_no_accept,
1697 .getname = sock_no_getname,
1698 .poll = datagram_poll,
1699 .ioctl = bcm_sock_no_ioctlcmd,
1700 .gettstamp = sock_gettstamp,
1701 .listen = sock_no_listen,
1702 .shutdown = sock_no_shutdown,
1703 .setsockopt = sock_no_setsockopt,
1704 .getsockopt = sock_no_getsockopt,
1705 .sendmsg = bcm_sendmsg,
1706 .recvmsg = bcm_recvmsg,
1707 .mmap = sock_no_mmap,
1708 .sendpage = sock_no_sendpage,
1711 static struct proto bcm_proto __read_mostly = {
1713 .owner = THIS_MODULE,
1714 .obj_size = sizeof(struct bcm_sock),
1718 static const struct can_proto bcm_can_proto = {
1720 .protocol = CAN_BCM,
1725 static int canbcm_pernet_init(struct net *net)
1727 #if IS_ENABLED(CONFIG_PROC_FS)
1728 /* create /proc/net/can-bcm directory */
1729 net->can.bcmproc_dir = proc_net_mkdir(net, "can-bcm", net->proc_net);
1730 #endif /* CONFIG_PROC_FS */
1735 static void canbcm_pernet_exit(struct net *net)
1737 #if IS_ENABLED(CONFIG_PROC_FS)
1738 /* remove /proc/net/can-bcm directory */
1739 if (net->can.bcmproc_dir)
1740 remove_proc_entry("can-bcm", net->proc_net);
1741 #endif /* CONFIG_PROC_FS */
1744 static struct pernet_operations canbcm_pernet_ops __read_mostly = {
1745 .init = canbcm_pernet_init,
1746 .exit = canbcm_pernet_exit,
1749 static int __init bcm_module_init(void)
1753 pr_info("can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n");
1755 err = can_proto_register(&bcm_can_proto);
1757 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1761 register_pernet_subsys(&canbcm_pernet_ops);
1765 static void __exit bcm_module_exit(void)
1767 can_proto_unregister(&bcm_can_proto);
1768 unregister_pernet_subsys(&canbcm_pernet_ops);
1771 module_init(bcm_module_init);
1772 module_exit(bcm_module_exit);