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");
91 #define BCM_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_ifindex)
94 * easy access to the first 64 bit of can(fd)_frame payload. cp->data is
95 * 64 bit aligned so the offset has to be multiples of 8 which is ensured
96 * by the only callers in bcm_rx_cmp_to_index() bcm_rx_handler().
98 static inline u64 get_u64(const struct canfd_frame *cp, int offset)
100 return *(u64 *)(cp->data + offset);
104 struct list_head list;
109 unsigned long frames_abs, frames_filtered;
110 struct bcm_timeval ival1, ival2;
111 struct hrtimer timer, thrtimer;
112 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
118 /* void pointers to arrays of struct can[fd]_frame */
121 struct canfd_frame sframe;
122 struct canfd_frame last_sframe;
124 struct net_device *rx_reg_dev;
131 struct list_head notifier;
132 struct list_head rx_ops;
133 struct list_head tx_ops;
134 unsigned long dropped_usr_msgs;
135 struct proc_dir_entry *bcm_proc_read;
136 char procname [32]; /* inode number in decimal with \0 */
139 static LIST_HEAD(bcm_notifier_list);
140 static DEFINE_SPINLOCK(bcm_notifier_lock);
141 static struct bcm_sock *bcm_busy_notifier;
143 static inline struct bcm_sock *bcm_sk(const struct sock *sk)
145 return (struct bcm_sock *)sk;
148 static inline ktime_t bcm_timeval_to_ktime(struct bcm_timeval tv)
150 return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC);
153 /* check limitations for timeval provided by user */
154 static bool bcm_is_invalid_tv(struct bcm_msg_head *msg_head)
156 if ((msg_head->ival1.tv_sec < 0) ||
157 (msg_head->ival1.tv_sec > BCM_TIMER_SEC_MAX) ||
158 (msg_head->ival1.tv_usec < 0) ||
159 (msg_head->ival1.tv_usec >= USEC_PER_SEC) ||
160 (msg_head->ival2.tv_sec < 0) ||
161 (msg_head->ival2.tv_sec > BCM_TIMER_SEC_MAX) ||
162 (msg_head->ival2.tv_usec < 0) ||
163 (msg_head->ival2.tv_usec >= USEC_PER_SEC))
169 #define CFSIZ(flags) ((flags & CAN_FD_FRAME) ? CANFD_MTU : CAN_MTU)
170 #define OPSIZ sizeof(struct bcm_op)
171 #define MHSIZ sizeof(struct bcm_msg_head)
176 #if IS_ENABLED(CONFIG_PROC_FS)
177 static char *bcm_proc_getifname(struct net *net, char *result, int ifindex)
179 struct net_device *dev;
185 dev = dev_get_by_index_rcu(net, ifindex);
187 strcpy(result, dev->name);
189 strcpy(result, "???");
195 static int bcm_proc_show(struct seq_file *m, void *v)
197 char ifname[IFNAMSIZ];
198 struct net *net = m->private;
199 struct sock *sk = (struct sock *)PDE_DATA(m->file->f_inode);
200 struct bcm_sock *bo = bcm_sk(sk);
203 seq_printf(m, ">>> socket %pK", sk->sk_socket);
204 seq_printf(m, " / sk %pK", sk);
205 seq_printf(m, " / bo %pK", bo);
206 seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
207 seq_printf(m, " / bound %s", bcm_proc_getifname(net, ifname, bo->ifindex));
208 seq_printf(m, " <<<\n");
210 list_for_each_entry(op, &bo->rx_ops, list) {
212 unsigned long reduction;
214 /* print only active entries & prevent division by zero */
218 seq_printf(m, "rx_op: %03X %-5s ", op->can_id,
219 bcm_proc_getifname(net, ifname, op->ifindex));
221 if (op->flags & CAN_FD_FRAME)
222 seq_printf(m, "(%u)", op->nframes);
224 seq_printf(m, "[%u]", op->nframes);
226 seq_printf(m, "%c ", (op->flags & RX_CHECK_DLC) ? 'd' : ' ');
229 seq_printf(m, "timeo=%lld ",
230 (long long)ktime_to_us(op->kt_ival1));
233 seq_printf(m, "thr=%lld ",
234 (long long)ktime_to_us(op->kt_ival2));
236 seq_printf(m, "# recv %ld (%ld) => reduction: ",
237 op->frames_filtered, op->frames_abs);
239 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
241 seq_printf(m, "%s%ld%%\n",
242 (reduction == 100) ? "near " : "", reduction);
245 list_for_each_entry(op, &bo->tx_ops, list) {
247 seq_printf(m, "tx_op: %03X %s ", op->can_id,
248 bcm_proc_getifname(net, ifname, op->ifindex));
250 if (op->flags & CAN_FD_FRAME)
251 seq_printf(m, "(%u) ", op->nframes);
253 seq_printf(m, "[%u] ", op->nframes);
256 seq_printf(m, "t1=%lld ",
257 (long long)ktime_to_us(op->kt_ival1));
260 seq_printf(m, "t2=%lld ",
261 (long long)ktime_to_us(op->kt_ival2));
263 seq_printf(m, "# sent %ld\n", op->frames_abs);
268 #endif /* CONFIG_PROC_FS */
271 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
272 * of the given bcm tx op
274 static void bcm_can_tx(struct bcm_op *op)
277 struct net_device *dev;
278 struct canfd_frame *cf = op->frames + op->cfsiz * op->currframe;
280 /* no target device? => exit */
284 dev = dev_get_by_index(sock_net(op->sk), op->ifindex);
286 /* RFC: should this bcm_op remove itself here? */
290 skb = alloc_skb(op->cfsiz + sizeof(struct can_skb_priv), gfp_any());
294 can_skb_reserve(skb);
295 can_skb_prv(skb)->ifindex = dev->ifindex;
296 can_skb_prv(skb)->skbcnt = 0;
298 skb_put_data(skb, cf, op->cfsiz);
300 /* send with loopback */
302 can_skb_set_owner(skb, op->sk);
305 /* update statistics */
309 /* reached last frame? */
310 if (op->currframe >= op->nframes)
317 * bcm_send_to_user - send a BCM message to the userspace
318 * (consisting of bcm_msg_head + x CAN frames)
320 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
321 struct canfd_frame *frames, int has_timestamp)
324 struct canfd_frame *firstframe;
325 struct sockaddr_can *addr;
326 struct sock *sk = op->sk;
327 unsigned int datalen = head->nframes * op->cfsiz;
330 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
334 skb_put_data(skb, head, sizeof(*head));
337 /* CAN frames starting here */
338 firstframe = (struct canfd_frame *)skb_tail_pointer(skb);
340 skb_put_data(skb, frames, datalen);
343 * the BCM uses the flags-element of the canfd_frame
344 * structure for internal purposes. This is only
345 * relevant for updates that are generated by the
346 * BCM, where nframes is 1
348 if (head->nframes == 1)
349 firstframe->flags &= BCM_CAN_FLAGS_MASK;
353 /* restore rx timestamp */
354 skb->tstamp = op->rx_stamp;
358 * Put the datagram to the queue so that bcm_recvmsg() can
359 * get it from there. We need to pass the interface index to
360 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
361 * containing the interface index.
364 sock_skb_cb_check_size(sizeof(struct sockaddr_can));
365 addr = (struct sockaddr_can *)skb->cb;
366 memset(addr, 0, sizeof(*addr));
367 addr->can_family = AF_CAN;
368 addr->can_ifindex = op->rx_ifindex;
370 err = sock_queue_rcv_skb(sk, skb);
372 struct bcm_sock *bo = bcm_sk(sk);
375 /* don't care about overflows in this statistic */
376 bo->dropped_usr_msgs++;
380 static bool bcm_tx_set_expiry(struct bcm_op *op, struct hrtimer *hrt)
384 if (op->kt_ival1 && op->count)
386 else if (op->kt_ival2)
391 hrtimer_set_expires(hrt, ktime_add(ktime_get(), ival));
395 static void bcm_tx_start_timer(struct bcm_op *op)
397 if (bcm_tx_set_expiry(op, &op->timer))
398 hrtimer_start_expires(&op->timer, HRTIMER_MODE_ABS_SOFT);
401 /* bcm_tx_timeout_handler - performs cyclic CAN frame transmissions */
402 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
404 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
405 struct bcm_msg_head msg_head;
407 if (op->kt_ival1 && (op->count > 0)) {
409 if (!op->count && (op->flags & TX_COUNTEVT)) {
411 /* create notification to user */
412 memset(&msg_head, 0, sizeof(msg_head));
413 msg_head.opcode = TX_EXPIRED;
414 msg_head.flags = op->flags;
415 msg_head.count = op->count;
416 msg_head.ival1 = op->ival1;
417 msg_head.ival2 = op->ival2;
418 msg_head.can_id = op->can_id;
419 msg_head.nframes = 0;
421 bcm_send_to_user(op, &msg_head, NULL, 0);
425 } else if (op->kt_ival2) {
429 return bcm_tx_set_expiry(op, &op->timer) ?
430 HRTIMER_RESTART : HRTIMER_NORESTART;
434 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
436 static void bcm_rx_changed(struct bcm_op *op, struct canfd_frame *data)
438 struct bcm_msg_head head;
440 /* update statistics */
441 op->frames_filtered++;
443 /* prevent statistics overflow */
444 if (op->frames_filtered > ULONG_MAX/100)
445 op->frames_filtered = op->frames_abs = 0;
447 /* this element is not throttled anymore */
448 data->flags &= (BCM_CAN_FLAGS_MASK|RX_RECV);
450 memset(&head, 0, sizeof(head));
451 head.opcode = RX_CHANGED;
452 head.flags = op->flags;
453 head.count = op->count;
454 head.ival1 = op->ival1;
455 head.ival2 = op->ival2;
456 head.can_id = op->can_id;
459 bcm_send_to_user(op, &head, data, 1);
463 * bcm_rx_update_and_send - process a detected relevant receive content change
464 * 1. update the last received data
465 * 2. send a notification to the user (if possible)
467 static void bcm_rx_update_and_send(struct bcm_op *op,
468 struct canfd_frame *lastdata,
469 const struct canfd_frame *rxdata)
471 memcpy(lastdata, rxdata, op->cfsiz);
473 /* mark as used and throttled by default */
474 lastdata->flags |= (RX_RECV|RX_THR);
476 /* throttling mode inactive ? */
478 /* send RX_CHANGED to the user immediately */
479 bcm_rx_changed(op, lastdata);
483 /* with active throttling timer we are just done here */
484 if (hrtimer_active(&op->thrtimer))
487 /* first reception with enabled throttling mode */
489 goto rx_changed_settime;
491 /* got a second frame inside a potential throttle period? */
492 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
493 ktime_to_us(op->kt_ival2)) {
494 /* do not send the saved data - only start throttle timer */
495 hrtimer_start(&op->thrtimer,
496 ktime_add(op->kt_lastmsg, op->kt_ival2),
497 HRTIMER_MODE_ABS_SOFT);
501 /* the gap was that big, that throttling was not needed here */
503 bcm_rx_changed(op, lastdata);
504 op->kt_lastmsg = ktime_get();
508 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
509 * received data stored in op->last_frames[]
511 static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index,
512 const struct canfd_frame *rxdata)
514 struct canfd_frame *cf = op->frames + op->cfsiz * index;
515 struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
519 * no one uses the MSBs of flags for comparison,
520 * so we use it here to detect the first time of reception
523 if (!(lcf->flags & RX_RECV)) {
524 /* received data for the first time => send update to user */
525 bcm_rx_update_and_send(op, lcf, rxdata);
529 /* do a real check in CAN frame data section */
530 for (i = 0; i < rxdata->len; i += 8) {
531 if ((get_u64(cf, i) & get_u64(rxdata, i)) !=
532 (get_u64(cf, i) & get_u64(lcf, i))) {
533 bcm_rx_update_and_send(op, lcf, rxdata);
538 if (op->flags & RX_CHECK_DLC) {
539 /* do a real check in CAN frame length */
540 if (rxdata->len != lcf->len) {
541 bcm_rx_update_and_send(op, lcf, rxdata);
548 * bcm_rx_starttimer - enable timeout monitoring for CAN frame reception
550 static void bcm_rx_starttimer(struct bcm_op *op)
552 if (op->flags & RX_NO_AUTOTIMER)
556 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL_SOFT);
559 /* bcm_rx_timeout_handler - when the (cyclic) CAN frame reception timed out */
560 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
562 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
563 struct bcm_msg_head msg_head;
565 /* if user wants to be informed, when cyclic CAN-Messages come back */
566 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
567 /* clear received CAN frames to indicate 'nothing received' */
568 memset(op->last_frames, 0, op->nframes * op->cfsiz);
571 /* create notification to user */
572 memset(&msg_head, 0, sizeof(msg_head));
573 msg_head.opcode = RX_TIMEOUT;
574 msg_head.flags = op->flags;
575 msg_head.count = op->count;
576 msg_head.ival1 = op->ival1;
577 msg_head.ival2 = op->ival2;
578 msg_head.can_id = op->can_id;
579 msg_head.nframes = 0;
581 bcm_send_to_user(op, &msg_head, NULL, 0);
583 return HRTIMER_NORESTART;
587 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
589 static inline int bcm_rx_do_flush(struct bcm_op *op, unsigned int index)
591 struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
593 if ((op->last_frames) && (lcf->flags & RX_THR)) {
594 bcm_rx_changed(op, lcf);
601 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
603 static int bcm_rx_thr_flush(struct bcm_op *op)
607 if (op->nframes > 1) {
610 /* for MUX filter we start at index 1 */
611 for (i = 1; i < op->nframes; i++)
612 updated += bcm_rx_do_flush(op, i);
615 /* for RX_FILTER_ID and simple filter */
616 updated += bcm_rx_do_flush(op, 0);
623 * bcm_rx_thr_handler - the time for blocked content updates is over now:
624 * Check for throttled data and send it to the userspace
626 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
628 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
630 if (bcm_rx_thr_flush(op)) {
631 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
632 return HRTIMER_RESTART;
634 /* rearm throttle handling */
636 return HRTIMER_NORESTART;
641 * bcm_rx_handler - handle a CAN frame reception
643 static void bcm_rx_handler(struct sk_buff *skb, void *data)
645 struct bcm_op *op = (struct bcm_op *)data;
646 const struct canfd_frame *rxframe = (struct canfd_frame *)skb->data;
649 if (op->can_id != rxframe->can_id)
652 /* make sure to handle the correct frame type (CAN / CAN FD) */
653 if (skb->len != op->cfsiz)
656 /* disable timeout */
657 hrtimer_cancel(&op->timer);
659 /* save rx timestamp */
660 op->rx_stamp = skb->tstamp;
661 /* save originator for recvfrom() */
662 op->rx_ifindex = skb->dev->ifindex;
663 /* update statistics */
666 if (op->flags & RX_RTR_FRAME) {
667 /* send reply for RTR-request (placed in op->frames[0]) */
672 if (op->flags & RX_FILTER_ID) {
673 /* the easiest case */
674 bcm_rx_update_and_send(op, op->last_frames, rxframe);
678 if (op->nframes == 1) {
679 /* simple compare with index 0 */
680 bcm_rx_cmp_to_index(op, 0, rxframe);
684 if (op->nframes > 1) {
688 * find the first multiplex mask that fits.
689 * Remark: The MUX-mask is stored in index 0 - but only the
690 * first 64 bits of the frame data[] are relevant (CAN FD)
693 for (i = 1; i < op->nframes; i++) {
694 if ((get_u64(op->frames, 0) & get_u64(rxframe, 0)) ==
695 (get_u64(op->frames, 0) &
696 get_u64(op->frames + op->cfsiz * i, 0))) {
697 bcm_rx_cmp_to_index(op, i, rxframe);
704 bcm_rx_starttimer(op);
708 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
710 static struct bcm_op *bcm_find_op(struct list_head *ops,
711 struct bcm_msg_head *mh, int ifindex)
715 list_for_each_entry(op, ops, list) {
716 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
717 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME))
724 static void bcm_free_op_rcu(struct rcu_head *rcu_head)
726 struct bcm_op *op = container_of(rcu_head, struct bcm_op, rcu);
728 if ((op->frames) && (op->frames != &op->sframe))
731 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
732 kfree(op->last_frames);
737 static void bcm_remove_op(struct bcm_op *op)
739 hrtimer_cancel(&op->timer);
740 hrtimer_cancel(&op->thrtimer);
742 call_rcu(&op->rcu, bcm_free_op_rcu);
745 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
747 if (op->rx_reg_dev == dev) {
748 can_rx_unregister(dev_net(dev), dev, op->can_id,
749 REGMASK(op->can_id), bcm_rx_handler, op);
751 /* mark as removed subscription */
752 op->rx_reg_dev = NULL;
754 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
755 "mismatch %p %p\n", op->rx_reg_dev, dev);
759 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
761 static int bcm_delete_rx_op(struct list_head *ops, struct bcm_msg_head *mh,
764 struct bcm_op *op, *n;
766 list_for_each_entry_safe(op, n, ops, list) {
767 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
768 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
770 /* disable automatic timer on frame reception */
771 op->flags |= RX_NO_AUTOTIMER;
774 * Don't care if we're bound or not (due to netdev
775 * problems) can_rx_unregister() is always a save
780 * Only remove subscriptions that had not
781 * been removed due to NETDEV_UNREGISTER
784 if (op->rx_reg_dev) {
785 struct net_device *dev;
787 dev = dev_get_by_index(sock_net(op->sk),
790 bcm_rx_unreg(dev, op);
795 can_rx_unregister(sock_net(op->sk), NULL,
806 return 0; /* not found */
810 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
812 static int bcm_delete_tx_op(struct list_head *ops, struct bcm_msg_head *mh,
815 struct bcm_op *op, *n;
817 list_for_each_entry_safe(op, n, ops, list) {
818 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
819 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
826 return 0; /* not found */
830 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
832 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
835 struct bcm_op *op = bcm_find_op(ops, msg_head, ifindex);
840 /* put current values into msg_head */
841 msg_head->flags = op->flags;
842 msg_head->count = op->count;
843 msg_head->ival1 = op->ival1;
844 msg_head->ival2 = op->ival2;
845 msg_head->nframes = op->nframes;
847 bcm_send_to_user(op, msg_head, op->frames, 0);
853 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
855 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
856 int ifindex, struct sock *sk)
858 struct bcm_sock *bo = bcm_sk(sk);
860 struct canfd_frame *cf;
864 /* we need a real device to send frames */
868 /* check nframes boundaries - we need at least one CAN frame */
869 if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES)
872 /* check timeval limitations */
873 if ((msg_head->flags & SETTIMER) && bcm_is_invalid_tv(msg_head))
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,
971 HRTIMER_MODE_REL_SOFT);
972 op->timer.function = bcm_tx_timeout_handler;
974 /* currently unused in tx_ops */
975 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC,
976 HRTIMER_MODE_REL_SOFT);
978 /* add this bcm_op to the list of the tx_ops */
979 list_add(&op->list, &bo->tx_ops);
981 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
983 if (op->nframes != msg_head->nframes) {
984 op->nframes = msg_head->nframes;
985 /* start multiple frame transmission with index 0 */
991 if (op->flags & TX_RESET_MULTI_IDX) {
992 /* start multiple frame transmission with index 0 */
996 if (op->flags & SETTIMER) {
997 /* set timer values */
998 op->count = msg_head->count;
999 op->ival1 = msg_head->ival1;
1000 op->ival2 = msg_head->ival2;
1001 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
1002 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
1004 /* disable an active timer due to zero values? */
1005 if (!op->kt_ival1 && !op->kt_ival2)
1006 hrtimer_cancel(&op->timer);
1009 if (op->flags & STARTTIMER) {
1010 hrtimer_cancel(&op->timer);
1011 /* spec: send CAN frame when starting timer */
1012 op->flags |= TX_ANNOUNCE;
1015 if (op->flags & TX_ANNOUNCE) {
1021 if (op->flags & STARTTIMER)
1022 bcm_tx_start_timer(op);
1024 return msg_head->nframes * op->cfsiz + MHSIZ;
1028 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1030 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1031 int ifindex, struct sock *sk)
1033 struct bcm_sock *bo = bcm_sk(sk);
1038 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1039 /* be robust against wrong usage ... */
1040 msg_head->flags |= RX_FILTER_ID;
1041 /* ignore trailing garbage */
1042 msg_head->nframes = 0;
1045 /* the first element contains the mux-mask => MAX_NFRAMES + 1 */
1046 if (msg_head->nframes > MAX_NFRAMES + 1)
1049 if ((msg_head->flags & RX_RTR_FRAME) &&
1050 ((msg_head->nframes != 1) ||
1051 (!(msg_head->can_id & CAN_RTR_FLAG))))
1054 /* check timeval limitations */
1055 if ((msg_head->flags & SETTIMER) && bcm_is_invalid_tv(msg_head))
1058 /* check the given can_id */
1059 op = bcm_find_op(&bo->rx_ops, msg_head, ifindex);
1061 /* update existing BCM operation */
1064 * Do we need more space for the CAN frames than currently
1065 * allocated? -> This is a _really_ unusual use-case and
1066 * therefore (complexity / locking) it is not supported.
1068 if (msg_head->nframes > op->nframes)
1071 if (msg_head->nframes) {
1072 /* update CAN frames content */
1073 err = memcpy_from_msg(op->frames, msg,
1074 msg_head->nframes * op->cfsiz);
1078 /* clear last_frames to indicate 'nothing received' */
1079 memset(op->last_frames, 0, msg_head->nframes * op->cfsiz);
1082 op->nframes = msg_head->nframes;
1083 op->flags = msg_head->flags;
1085 /* Only an update -> do not call can_rx_register() */
1089 /* insert new BCM operation for the given can_id */
1090 op = kzalloc(OPSIZ, GFP_KERNEL);
1094 op->can_id = msg_head->can_id;
1095 op->nframes = msg_head->nframes;
1096 op->cfsiz = CFSIZ(msg_head->flags);
1097 op->flags = msg_head->flags;
1099 if (msg_head->nframes > 1) {
1100 /* create array for CAN frames and copy the data */
1101 op->frames = kmalloc_array(msg_head->nframes,
1109 /* create and init array for received CAN frames */
1110 op->last_frames = kcalloc(msg_head->nframes,
1113 if (!op->last_frames) {
1120 op->frames = &op->sframe;
1121 op->last_frames = &op->last_sframe;
1124 if (msg_head->nframes) {
1125 err = memcpy_from_msg(op->frames, msg,
1126 msg_head->nframes * op->cfsiz);
1128 if (op->frames != &op->sframe)
1130 if (op->last_frames != &op->last_sframe)
1131 kfree(op->last_frames);
1137 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1139 op->ifindex = ifindex;
1141 /* ifindex for timeout events w/o previous frame reception */
1142 op->rx_ifindex = ifindex;
1144 /* initialize uninitialized (kzalloc) structure */
1145 hrtimer_init(&op->timer, CLOCK_MONOTONIC,
1146 HRTIMER_MODE_REL_SOFT);
1147 op->timer.function = bcm_rx_timeout_handler;
1149 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC,
1150 HRTIMER_MODE_REL_SOFT);
1151 op->thrtimer.function = bcm_rx_thr_handler;
1153 /* add this bcm_op to the list of the rx_ops */
1154 list_add(&op->list, &bo->rx_ops);
1156 /* call can_rx_register() */
1159 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1163 if (op->flags & RX_RTR_FRAME) {
1164 struct canfd_frame *frame0 = op->frames;
1166 /* no timers in RTR-mode */
1167 hrtimer_cancel(&op->thrtimer);
1168 hrtimer_cancel(&op->timer);
1171 * funny feature in RX(!)_SETUP only for RTR-mode:
1172 * copy can_id into frame BUT without RTR-flag to
1173 * prevent a full-load-loopback-test ... ;-]
1175 if ((op->flags & TX_CP_CAN_ID) ||
1176 (frame0->can_id == op->can_id))
1177 frame0->can_id = op->can_id & ~CAN_RTR_FLAG;
1180 if (op->flags & SETTIMER) {
1182 /* set timer value */
1183 op->ival1 = msg_head->ival1;
1184 op->ival2 = msg_head->ival2;
1185 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
1186 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
1188 /* disable an active timer due to zero value? */
1190 hrtimer_cancel(&op->timer);
1193 * In any case cancel the throttle timer, flush
1194 * potentially blocked msgs and reset throttle handling
1197 hrtimer_cancel(&op->thrtimer);
1198 bcm_rx_thr_flush(op);
1201 if ((op->flags & STARTTIMER) && op->kt_ival1)
1202 hrtimer_start(&op->timer, op->kt_ival1,
1203 HRTIMER_MODE_REL_SOFT);
1206 /* now we can register for can_ids, if we added a new bcm_op */
1207 if (do_rx_register) {
1209 struct net_device *dev;
1211 dev = dev_get_by_index(sock_net(sk), ifindex);
1213 err = can_rx_register(sock_net(sk), dev,
1215 REGMASK(op->can_id),
1219 op->rx_reg_dev = dev;
1224 err = can_rx_register(sock_net(sk), NULL, op->can_id,
1225 REGMASK(op->can_id),
1226 bcm_rx_handler, op, "bcm", sk);
1228 /* this bcm rx op is broken -> remove it */
1229 list_del(&op->list);
1235 return msg_head->nframes * op->cfsiz + MHSIZ;
1239 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1241 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk,
1244 struct sk_buff *skb;
1245 struct net_device *dev;
1248 /* we need a real device to send frames */
1252 skb = alloc_skb(cfsiz + sizeof(struct can_skb_priv), GFP_KERNEL);
1256 can_skb_reserve(skb);
1258 err = memcpy_from_msg(skb_put(skb, cfsiz), msg, cfsiz);
1264 dev = dev_get_by_index(sock_net(sk), ifindex);
1270 can_skb_prv(skb)->ifindex = dev->ifindex;
1271 can_skb_prv(skb)->skbcnt = 0;
1273 can_skb_set_owner(skb, sk);
1274 err = can_send(skb, 1); /* send with loopback */
1280 return cfsiz + MHSIZ;
1284 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1286 static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
1288 struct sock *sk = sock->sk;
1289 struct bcm_sock *bo = bcm_sk(sk);
1290 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1291 struct bcm_msg_head msg_head;
1293 int ret; /* read bytes or error codes as return value */
1298 /* check for valid message length from userspace */
1302 /* read message head information */
1303 ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ);
1307 cfsiz = CFSIZ(msg_head.flags);
1308 if ((size - MHSIZ) % cfsiz)
1311 /* check for alternative ifindex for this bcm_op */
1313 if (!ifindex && msg->msg_name) {
1314 /* no bound device as default => check msg_name */
1315 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
1317 if (msg->msg_namelen < BCM_MIN_NAMELEN)
1320 if (addr->can_family != AF_CAN)
1323 /* ifindex from sendto() */
1324 ifindex = addr->can_ifindex;
1327 struct net_device *dev;
1329 dev = dev_get_by_index(sock_net(sk), ifindex);
1333 if (dev->type != ARPHRD_CAN) {
1344 switch (msg_head.opcode) {
1347 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1351 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1355 if (bcm_delete_tx_op(&bo->tx_ops, &msg_head, ifindex))
1362 if (bcm_delete_rx_op(&bo->rx_ops, &msg_head, ifindex))
1369 /* reuse msg_head for the reply to TX_READ */
1370 msg_head.opcode = TX_STATUS;
1371 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1375 /* reuse msg_head for the reply to RX_READ */
1376 msg_head.opcode = RX_STATUS;
1377 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1381 /* we need exactly one CAN frame behind the msg head */
1382 if ((msg_head.nframes != 1) || (size != cfsiz + MHSIZ))
1385 ret = bcm_tx_send(msg, ifindex, sk, cfsiz);
1399 * notification handler for netdevice status changes
1401 static void bcm_notify(struct bcm_sock *bo, unsigned long msg,
1402 struct net_device *dev)
1404 struct sock *sk = &bo->sk;
1406 int notify_enodev = 0;
1408 if (!net_eq(dev_net(dev), sock_net(sk)))
1413 case NETDEV_UNREGISTER:
1416 /* remove device specific receive entries */
1417 list_for_each_entry(op, &bo->rx_ops, list)
1418 if (op->rx_reg_dev == dev)
1419 bcm_rx_unreg(dev, op);
1421 /* remove device reference, if this is our bound device */
1422 if (bo->bound && bo->ifindex == dev->ifindex) {
1430 if (notify_enodev) {
1431 sk->sk_err = ENODEV;
1432 if (!sock_flag(sk, SOCK_DEAD))
1433 sk->sk_error_report(sk);
1438 if (bo->bound && bo->ifindex == dev->ifindex) {
1439 sk->sk_err = ENETDOWN;
1440 if (!sock_flag(sk, SOCK_DEAD))
1441 sk->sk_error_report(sk);
1446 static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1449 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1451 if (dev->type != ARPHRD_CAN)
1453 if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN)
1455 if (unlikely(bcm_busy_notifier)) /* Check for reentrant bug. */
1458 spin_lock(&bcm_notifier_lock);
1459 list_for_each_entry(bcm_busy_notifier, &bcm_notifier_list, notifier) {
1460 spin_unlock(&bcm_notifier_lock);
1461 bcm_notify(bcm_busy_notifier, msg, dev);
1462 spin_lock(&bcm_notifier_lock);
1464 bcm_busy_notifier = NULL;
1465 spin_unlock(&bcm_notifier_lock);
1470 * initial settings for all BCM sockets to be set at socket creation time
1472 static int bcm_init(struct sock *sk)
1474 struct bcm_sock *bo = bcm_sk(sk);
1478 bo->dropped_usr_msgs = 0;
1479 bo->bcm_proc_read = NULL;
1481 INIT_LIST_HEAD(&bo->tx_ops);
1482 INIT_LIST_HEAD(&bo->rx_ops);
1485 spin_lock(&bcm_notifier_lock);
1486 list_add_tail(&bo->notifier, &bcm_notifier_list);
1487 spin_unlock(&bcm_notifier_lock);
1493 * standard socket functions
1495 static int bcm_release(struct socket *sock)
1497 struct sock *sk = sock->sk;
1499 struct bcm_sock *bo;
1500 struct bcm_op *op, *next;
1508 /* remove bcm_ops, timer, rx_unregister(), etc. */
1510 spin_lock(&bcm_notifier_lock);
1511 while (bcm_busy_notifier == bo) {
1512 spin_unlock(&bcm_notifier_lock);
1513 schedule_timeout_uninterruptible(1);
1514 spin_lock(&bcm_notifier_lock);
1516 list_del(&bo->notifier);
1517 spin_unlock(&bcm_notifier_lock);
1521 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1524 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1526 * Don't care if we're bound or not (due to netdev problems)
1527 * can_rx_unregister() is always a save thing to do here.
1531 * Only remove subscriptions that had not
1532 * been removed due to NETDEV_UNREGISTER
1535 if (op->rx_reg_dev) {
1536 struct net_device *dev;
1538 dev = dev_get_by_index(net, op->ifindex);
1540 bcm_rx_unreg(dev, op);
1545 can_rx_unregister(net, NULL, op->can_id,
1546 REGMASK(op->can_id),
1547 bcm_rx_handler, op);
1553 list_for_each_entry_safe(op, next, &bo->rx_ops, list)
1556 #if IS_ENABLED(CONFIG_PROC_FS)
1557 /* remove procfs entry */
1558 if (net->can.bcmproc_dir && bo->bcm_proc_read)
1559 remove_proc_entry(bo->procname, net->can.bcmproc_dir);
1560 #endif /* CONFIG_PROC_FS */
1562 /* remove device reference */
1577 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1580 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1581 struct sock *sk = sock->sk;
1582 struct bcm_sock *bo = bcm_sk(sk);
1583 struct net *net = sock_net(sk);
1586 if (len < BCM_MIN_NAMELEN)
1596 /* bind a device to this socket */
1597 if (addr->can_ifindex) {
1598 struct net_device *dev;
1600 dev = dev_get_by_index(net, addr->can_ifindex);
1605 if (dev->type != ARPHRD_CAN) {
1611 bo->ifindex = dev->ifindex;
1615 /* no interface reference for ifindex = 0 ('any' CAN device) */
1619 #if IS_ENABLED(CONFIG_PROC_FS)
1620 if (net->can.bcmproc_dir) {
1621 /* unique socket address as filename */
1622 sprintf(bo->procname, "%lu", sock_i_ino(sk));
1623 bo->bcm_proc_read = proc_create_net_single(bo->procname, 0644,
1624 net->can.bcmproc_dir,
1626 if (!bo->bcm_proc_read) {
1631 #endif /* CONFIG_PROC_FS */
1641 static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1644 struct sock *sk = sock->sk;
1645 struct sk_buff *skb;
1650 noblock = flags & MSG_DONTWAIT;
1651 flags &= ~MSG_DONTWAIT;
1652 skb = skb_recv_datagram(sk, flags, noblock, &error);
1656 if (skb->len < size)
1659 err = memcpy_to_msg(msg, skb->data, size);
1661 skb_free_datagram(sk, skb);
1665 sock_recv_ts_and_drops(msg, sk, skb);
1667 if (msg->msg_name) {
1668 __sockaddr_check_size(BCM_MIN_NAMELEN);
1669 msg->msg_namelen = BCM_MIN_NAMELEN;
1670 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1673 skb_free_datagram(sk, skb);
1678 static int bcm_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd,
1681 /* no ioctls for socket layer -> hand it down to NIC layer */
1682 return -ENOIOCTLCMD;
1685 static const struct proto_ops bcm_ops = {
1687 .release = bcm_release,
1688 .bind = sock_no_bind,
1689 .connect = bcm_connect,
1690 .socketpair = sock_no_socketpair,
1691 .accept = sock_no_accept,
1692 .getname = sock_no_getname,
1693 .poll = datagram_poll,
1694 .ioctl = bcm_sock_no_ioctlcmd,
1695 .gettstamp = sock_gettstamp,
1696 .listen = sock_no_listen,
1697 .shutdown = sock_no_shutdown,
1698 .setsockopt = sock_no_setsockopt,
1699 .getsockopt = sock_no_getsockopt,
1700 .sendmsg = bcm_sendmsg,
1701 .recvmsg = bcm_recvmsg,
1702 .mmap = sock_no_mmap,
1703 .sendpage = sock_no_sendpage,
1706 static struct proto bcm_proto __read_mostly = {
1708 .owner = THIS_MODULE,
1709 .obj_size = sizeof(struct bcm_sock),
1713 static const struct can_proto bcm_can_proto = {
1715 .protocol = CAN_BCM,
1720 static int canbcm_pernet_init(struct net *net)
1722 #if IS_ENABLED(CONFIG_PROC_FS)
1723 /* create /proc/net/can-bcm directory */
1724 net->can.bcmproc_dir = proc_net_mkdir(net, "can-bcm", net->proc_net);
1725 #endif /* CONFIG_PROC_FS */
1730 static void canbcm_pernet_exit(struct net *net)
1732 #if IS_ENABLED(CONFIG_PROC_FS)
1733 /* remove /proc/net/can-bcm directory */
1734 if (net->can.bcmproc_dir)
1735 remove_proc_entry("can-bcm", net->proc_net);
1736 #endif /* CONFIG_PROC_FS */
1739 static struct pernet_operations canbcm_pernet_ops __read_mostly = {
1740 .init = canbcm_pernet_init,
1741 .exit = canbcm_pernet_exit,
1744 static struct notifier_block canbcm_notifier = {
1745 .notifier_call = bcm_notifier
1748 static int __init bcm_module_init(void)
1752 pr_info("can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n");
1754 err = can_proto_register(&bcm_can_proto);
1756 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1760 register_pernet_subsys(&canbcm_pernet_ops);
1761 register_netdevice_notifier(&canbcm_notifier);
1765 static void __exit bcm_module_exit(void)
1767 can_proto_unregister(&bcm_can_proto);
1768 unregister_netdevice_notifier(&canbcm_notifier);
1769 unregister_pernet_subsys(&canbcm_pernet_ops);
1772 module_init(bcm_module_init);
1773 module_exit(bcm_module_exit);