blob: c99e7c75eeee1c59951ed2868c3c79d1fb8b08e6 [file] [log] [blame]
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001/*
2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
3 *
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02004 * Copyright (c) 2002-2016 Volkswagen Group Electronic Research
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08005 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
23 *
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38 * DAMAGE.
39 *
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080040 */
41
42#include <linux/module.h>
43#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000044#include <linux/interrupt.h>
Oliver Hartkopp73e87e02008-04-15 19:29:14 -070045#include <linux/hrtimer.h>
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080046#include <linux/list.h>
47#include <linux/proc_fs.h>
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +000048#include <linux/seq_file.h>
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080049#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>
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +010057#include <linux/can/skb.h>
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080058#include <linux/can/bcm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090059#include <linux/slab.h>
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080060#include <net/sock.h>
61#include <net/net_namespace.h>
62
Oliver Hartkopp5b75c492010-08-11 16:12:35 -070063/*
64 * To send multiple CAN frame content within TX_SETUP or to filter
65 * CAN messages with multiplex index within RX_SETUP, the number of
66 * different filters is limited to 256 due to the one byte index value.
67 */
68#define MAX_NFRAMES 256
69
Oliver Hartkopp30e75172019-01-13 19:31:43 +010070/* limit timers to 400 days for sending/timeouts */
71#define BCM_TIMER_SEC_MAX (400 * 24 * 60 * 60)
72
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +020073/* use of last_frames[index].flags */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080074#define RX_RECV 0x40 /* received data for this element */
75#define RX_THR 0x80 /* element not been sent due to throttle feature */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +020076#define BCM_CAN_FLAGS_MASK 0x3F /* to clean private flags after usage */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080077
78/* get best masking value for can_rx_register() for a given single can_id */
Oliver Hartkoppd253eee2008-12-03 15:52:35 -080079#define REGMASK(id) ((id & CAN_EFF_FLAG) ? \
80 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
81 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080082
Oliver Hartkopp5499a6b2016-11-23 14:33:25 +010083#define CAN_BCM_VERSION "20161123"
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080084
85MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
86MODULE_LICENSE("Dual BSD/GPL");
87MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
Lothar Waßmannb13bb2e2009-07-14 23:12:25 +000088MODULE_ALIAS("can-proto-2");
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080089
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +020090/*
91 * easy access to the first 64 bit of can(fd)_frame payload. cp->data is
92 * 64 bit aligned so the offset has to be multiples of 8 which is ensured
93 * by the only callers in bcm_rx_cmp_to_index() bcm_rx_handler().
94 */
95static inline u64 get_u64(const struct canfd_frame *cp, int offset)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080096{
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +020097 return *(u64 *)(cp->data + offset);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -080098}
99
100struct bcm_op {
101 struct list_head list;
102 int ifindex;
103 canid_t can_id;
Oliver Hartkopp5b75c492010-08-11 16:12:35 -0700104 u32 flags;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800105 unsigned long frames_abs, frames_filtered;
Arnd Bergmannba61a8d2015-09-30 13:26:42 +0200106 struct bcm_timeval ival1, ival2;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700107 struct hrtimer timer, thrtimer;
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800108 struct tasklet_struct tsklet, thrtsklet;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700109 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800110 int rx_ifindex;
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200111 int cfsiz;
Oliver Hartkopp5b75c492010-08-11 16:12:35 -0700112 u32 count;
113 u32 nframes;
114 u32 currframe;
Oliver Hartkopp5499a6b2016-11-23 14:33:25 +0100115 /* void pointers to arrays of struct can[fd]_frame */
116 void *frames;
117 void *last_frames;
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200118 struct canfd_frame sframe;
119 struct canfd_frame last_sframe;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800120 struct sock *sk;
121 struct net_device *rx_reg_dev;
122};
123
124static struct proc_dir_entry *proc_dir;
125
126struct bcm_sock {
127 struct sock sk;
128 int bound;
129 int ifindex;
130 struct notifier_block notifier;
131 struct list_head rx_ops;
132 struct list_head tx_ops;
133 unsigned long dropped_usr_msgs;
134 struct proc_dir_entry *bcm_proc_read;
Dan Rosenberg9f260e02010-12-26 06:54:53 +0000135 char procname [32]; /* inode number in decimal with \0 */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800136};
137
138static inline struct bcm_sock *bcm_sk(const struct sock *sk)
139{
140 return (struct bcm_sock *)sk;
141}
142
Arnd Bergmannba61a8d2015-09-30 13:26:42 +0200143static inline ktime_t bcm_timeval_to_ktime(struct bcm_timeval tv)
144{
145 return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC);
146}
147
Oliver Hartkopp30e75172019-01-13 19:31:43 +0100148/* check limitations for timeval provided by user */
149static bool bcm_is_invalid_tv(struct bcm_msg_head *msg_head)
150{
151 if ((msg_head->ival1.tv_sec < 0) ||
152 (msg_head->ival1.tv_sec > BCM_TIMER_SEC_MAX) ||
153 (msg_head->ival1.tv_usec < 0) ||
154 (msg_head->ival1.tv_usec >= USEC_PER_SEC) ||
155 (msg_head->ival2.tv_sec < 0) ||
156 (msg_head->ival2.tv_sec > BCM_TIMER_SEC_MAX) ||
157 (msg_head->ival2.tv_usec < 0) ||
158 (msg_head->ival2.tv_usec >= USEC_PER_SEC))
159 return true;
160
161 return false;
162}
163
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200164#define CFSIZ(flags) ((flags & CAN_FD_FRAME) ? CANFD_MTU : CAN_MTU)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800165#define OPSIZ sizeof(struct bcm_op)
166#define MHSIZ sizeof(struct bcm_msg_head)
167
168/*
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800169 * procfs functions
170 */
Eric Dumazet6755aeb2009-11-06 00:23:01 +0000171static char *bcm_proc_getifname(char *result, int ifindex)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800172{
173 struct net_device *dev;
174
175 if (!ifindex)
176 return "any";
177
stephen hemmingerff879eb2009-11-10 07:54:56 +0000178 rcu_read_lock();
179 dev = dev_get_by_index_rcu(&init_net, ifindex);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800180 if (dev)
Eric Dumazet6755aeb2009-11-06 00:23:01 +0000181 strcpy(result, dev->name);
182 else
183 strcpy(result, "???");
stephen hemmingerff879eb2009-11-10 07:54:56 +0000184 rcu_read_unlock();
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800185
Eric Dumazet6755aeb2009-11-06 00:23:01 +0000186 return result;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800187}
188
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000189static int bcm_proc_show(struct seq_file *m, void *v)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800190{
Eric Dumazet6755aeb2009-11-06 00:23:01 +0000191 char ifname[IFNAMSIZ];
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000192 struct sock *sk = (struct sock *)m->private;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800193 struct bcm_sock *bo = bcm_sk(sk);
194 struct bcm_op *op;
195
Dan Rosenberg71338aa2011-05-23 12:17:35 +0000196 seq_printf(m, ">>> socket %pK", sk->sk_socket);
197 seq_printf(m, " / sk %pK", sk);
198 seq_printf(m, " / bo %pK", bo);
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000199 seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
Eric Dumazet6755aeb2009-11-06 00:23:01 +0000200 seq_printf(m, " / bound %s", bcm_proc_getifname(ifname, bo->ifindex));
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000201 seq_printf(m, " <<<\n");
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800202
203 list_for_each_entry(op, &bo->rx_ops, list) {
204
205 unsigned long reduction;
206
207 /* print only active entries & prevent division by zero */
208 if (!op->frames_abs)
209 continue;
210
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200211 seq_printf(m, "rx_op: %03X %-5s ", op->can_id,
212 bcm_proc_getifname(ifname, op->ifindex));
213
214 if (op->flags & CAN_FD_FRAME)
215 seq_printf(m, "(%u)", op->nframes);
216 else
217 seq_printf(m, "[%u]", op->nframes);
218
219 seq_printf(m, "%c ", (op->flags & RX_CHECK_DLC) ? 'd' : ' ');
220
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700221 if (op->kt_ival1.tv64)
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000222 seq_printf(m, "timeo=%lld ",
Oliver Hartkopp95acb492016-06-17 15:35:24 +0200223 (long long)ktime_to_us(op->kt_ival1));
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800224
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700225 if (op->kt_ival2.tv64)
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000226 seq_printf(m, "thr=%lld ",
Oliver Hartkopp95acb492016-06-17 15:35:24 +0200227 (long long)ktime_to_us(op->kt_ival2));
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800228
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000229 seq_printf(m, "# recv %ld (%ld) => reduction: ",
Oliver Hartkopp95acb492016-06-17 15:35:24 +0200230 op->frames_filtered, op->frames_abs);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800231
232 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
233
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000234 seq_printf(m, "%s%ld%%\n",
Oliver Hartkopp95acb492016-06-17 15:35:24 +0200235 (reduction == 100) ? "near " : "", reduction);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800236 }
237
238 list_for_each_entry(op, &bo->tx_ops, list) {
239
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200240 seq_printf(m, "tx_op: %03X %s ", op->can_id,
241 bcm_proc_getifname(ifname, op->ifindex));
242
243 if (op->flags & CAN_FD_FRAME)
244 seq_printf(m, "(%u) ", op->nframes);
245 else
246 seq_printf(m, "[%u] ", op->nframes);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800247
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700248 if (op->kt_ival1.tv64)
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000249 seq_printf(m, "t1=%lld ",
Oliver Hartkopp95acb492016-06-17 15:35:24 +0200250 (long long)ktime_to_us(op->kt_ival1));
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700251
252 if (op->kt_ival2.tv64)
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000253 seq_printf(m, "t2=%lld ",
Oliver Hartkopp95acb492016-06-17 15:35:24 +0200254 (long long)ktime_to_us(op->kt_ival2));
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800255
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000256 seq_printf(m, "# sent %ld\n", op->frames_abs);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800257 }
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000258 seq_putc(m, '\n');
259 return 0;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800260}
261
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000262static int bcm_proc_open(struct inode *inode, struct file *file)
263{
Al Virod9dda782013-03-31 18:16:14 -0400264 return single_open(file, bcm_proc_show, PDE_DATA(inode));
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +0000265}
266
267static const struct file_operations bcm_proc_fops = {
268 .owner = THIS_MODULE,
269 .open = bcm_proc_open,
270 .read = seq_read,
271 .llseek = seq_lseek,
272 .release = single_release,
273};
274
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800275/*
276 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface
277 * of the given bcm tx op
278 */
279static void bcm_can_tx(struct bcm_op *op)
280{
281 struct sk_buff *skb;
282 struct net_device *dev;
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200283 struct canfd_frame *cf = op->frames + op->cfsiz * op->currframe;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800284
285 /* no target device? => exit */
286 if (!op->ifindex)
287 return;
288
289 dev = dev_get_by_index(&init_net, op->ifindex);
290 if (!dev) {
291 /* RFC: should this bcm_op remove itself here? */
292 return;
293 }
294
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200295 skb = alloc_skb(op->cfsiz + sizeof(struct can_skb_priv), gfp_any());
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800296 if (!skb)
297 goto out;
298
Oliver Hartkopp2bf34402013-01-28 08:33:33 +0000299 can_skb_reserve(skb);
300 can_skb_prv(skb)->ifindex = dev->ifindex;
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +0200301 can_skb_prv(skb)->skbcnt = 0;
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +0100302
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200303 memcpy(skb_put(skb, op->cfsiz), cf, op->cfsiz);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800304
305 /* send with loopback */
306 skb->dev = dev;
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +0100307 can_skb_set_owner(skb, op->sk);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800308 can_send(skb, 1);
309
310 /* update statistics */
311 op->currframe++;
312 op->frames_abs++;
313
314 /* reached last frame? */
315 if (op->currframe >= op->nframes)
316 op->currframe = 0;
Oliver Hartkopp95acb492016-06-17 15:35:24 +0200317out:
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800318 dev_put(dev);
319}
320
321/*
322 * bcm_send_to_user - send a BCM message to the userspace
323 * (consisting of bcm_msg_head + x CAN frames)
324 */
325static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200326 struct canfd_frame *frames, int has_timestamp)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800327{
328 struct sk_buff *skb;
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200329 struct canfd_frame *firstframe;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800330 struct sockaddr_can *addr;
331 struct sock *sk = op->sk;
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200332 unsigned int datalen = head->nframes * op->cfsiz;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800333 int err;
334
335 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
336 if (!skb)
337 return;
338
339 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head));
340
341 if (head->nframes) {
Oliver Hartkopp72c8a892016-06-17 15:35:25 +0200342 /* CAN frames starting here */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200343 firstframe = (struct canfd_frame *)skb_tail_pointer(skb);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800344
345 memcpy(skb_put(skb, datalen), frames, datalen);
346
347 /*
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200348 * the BCM uses the flags-element of the canfd_frame
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800349 * structure for internal purposes. This is only
350 * relevant for updates that are generated by the
351 * BCM, where nframes is 1
352 */
353 if (head->nframes == 1)
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200354 firstframe->flags &= BCM_CAN_FLAGS_MASK;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800355 }
356
357 if (has_timestamp) {
358 /* restore rx timestamp */
359 skb->tstamp = op->rx_stamp;
360 }
361
362 /*
363 * Put the datagram to the queue so that bcm_recvmsg() can
364 * get it from there. We need to pass the interface index to
365 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb
366 * containing the interface index.
367 */
368
Eyal Birgerb4772ef2015-03-01 14:58:29 +0200369 sock_skb_cb_check_size(sizeof(struct sockaddr_can));
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800370 addr = (struct sockaddr_can *)skb->cb;
371 memset(addr, 0, sizeof(*addr));
372 addr->can_family = AF_CAN;
373 addr->can_ifindex = op->rx_ifindex;
374
375 err = sock_queue_rcv_skb(sk, skb);
376 if (err < 0) {
377 struct bcm_sock *bo = bcm_sk(sk);
378
379 kfree_skb(skb);
380 /* don't care about overflows in this statistic */
381 bo->dropped_usr_msgs++;
382 }
383}
384
Oliver Hartkopp12d0d0d2011-09-29 15:33:47 -0400385static void bcm_tx_start_timer(struct bcm_op *op)
386{
387 if (op->kt_ival1.tv64 && op->count)
388 hrtimer_start(&op->timer,
389 ktime_add(ktime_get(), op->kt_ival1),
390 HRTIMER_MODE_ABS);
391 else if (op->kt_ival2.tv64)
392 hrtimer_start(&op->timer,
393 ktime_add(ktime_get(), op->kt_ival2),
394 HRTIMER_MODE_ABS);
395}
396
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800397static void bcm_tx_timeout_tsklet(unsigned long data)
398{
399 struct bcm_op *op = (struct bcm_op *)data;
400 struct bcm_msg_head msg_head;
401
Oliver Hartkoppc53a6ee2009-01-14 21:06:55 -0800402 if (op->kt_ival1.tv64 && (op->count > 0)) {
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800403
Oliver Hartkoppc53a6ee2009-01-14 21:06:55 -0800404 op->count--;
405 if (!op->count && (op->flags & TX_COUNTEVT)) {
406
407 /* create notification to user */
408 msg_head.opcode = TX_EXPIRED;
409 msg_head.flags = op->flags;
410 msg_head.count = op->count;
411 msg_head.ival1 = op->ival1;
412 msg_head.ival2 = op->ival2;
413 msg_head.can_id = op->can_id;
414 msg_head.nframes = 0;
415
416 bcm_send_to_user(op, &msg_head, NULL, 0);
417 }
Oliver Hartkoppc53a6ee2009-01-14 21:06:55 -0800418 bcm_can_tx(op);
Oliver Hartkoppc53a6ee2009-01-14 21:06:55 -0800419
Oliver Hartkopp12d0d0d2011-09-29 15:33:47 -0400420 } else if (op->kt_ival2.tv64)
421 bcm_can_tx(op);
Oliver Hartkoppc53a6ee2009-01-14 21:06:55 -0800422
Oliver Hartkopp12d0d0d2011-09-29 15:33:47 -0400423 bcm_tx_start_timer(op);
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800424}
425
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800426/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300427 * bcm_tx_timeout_handler - performs cyclic CAN frame transmissions
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800428 */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700429static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800430{
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700431 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800432
Oliver Hartkoppc53a6ee2009-01-14 21:06:55 -0800433 tasklet_schedule(&op->tsklet);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800434
Oliver Hartkoppc53a6ee2009-01-14 21:06:55 -0800435 return HRTIMER_NORESTART;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800436}
437
438/*
439 * bcm_rx_changed - create a RX_CHANGED notification due to changed content
440 */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200441static void bcm_rx_changed(struct bcm_op *op, struct canfd_frame *data)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800442{
443 struct bcm_msg_head head;
444
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800445 /* update statistics */
446 op->frames_filtered++;
447
448 /* prevent statistics overflow */
449 if (op->frames_filtered > ULONG_MAX/100)
450 op->frames_filtered = op->frames_abs = 0;
451
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800452 /* this element is not throttled anymore */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200453 data->flags &= (BCM_CAN_FLAGS_MASK|RX_RECV);
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800454
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800455 head.opcode = RX_CHANGED;
456 head.flags = op->flags;
457 head.count = op->count;
458 head.ival1 = op->ival1;
459 head.ival2 = op->ival2;
460 head.can_id = op->can_id;
461 head.nframes = 1;
462
463 bcm_send_to_user(op, &head, data, 1);
464}
465
466/*
467 * bcm_rx_update_and_send - process a detected relevant receive content change
468 * 1. update the last received data
469 * 2. send a notification to the user (if possible)
470 */
471static void bcm_rx_update_and_send(struct bcm_op *op,
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200472 struct canfd_frame *lastdata,
473 const struct canfd_frame *rxdata)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800474{
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200475 memcpy(lastdata, rxdata, op->cfsiz);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800476
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800477 /* mark as used and throttled by default */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200478 lastdata->flags |= (RX_RECV|RX_THR);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800479
Jeremiah Mahler069f8452014-12-05 09:54:38 -0800480 /* throttling mode inactive ? */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800481 if (!op->kt_ival2.tv64) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800482 /* send RX_CHANGED to the user immediately */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800483 bcm_rx_changed(op, lastdata);
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700484 return;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800485 }
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700486
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800487 /* with active throttling timer we are just done here */
488 if (hrtimer_active(&op->thrtimer))
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700489 return;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700490
Jeremiah Mahler069f8452014-12-05 09:54:38 -0800491 /* first reception with enabled throttling mode */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800492 if (!op->kt_lastmsg.tv64)
493 goto rx_changed_settime;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700494
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800495 /* got a second frame inside a potential throttle period? */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700496 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
497 ktime_to_us(op->kt_ival2)) {
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800498 /* do not send the saved data - only start throttle timer */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700499 hrtimer_start(&op->thrtimer,
500 ktime_add(op->kt_lastmsg, op->kt_ival2),
501 HRTIMER_MODE_ABS);
502 return;
503 }
504
505 /* the gap was that big, that throttling was not needed here */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800506rx_changed_settime:
507 bcm_rx_changed(op, lastdata);
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700508 op->kt_lastmsg = ktime_get();
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800509}
510
511/*
512 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly
513 * received data stored in op->last_frames[]
514 */
Oliver Hartkopp5b75c492010-08-11 16:12:35 -0700515static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index,
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200516 const struct canfd_frame *rxdata)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800517{
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200518 struct canfd_frame *cf = op->frames + op->cfsiz * index;
519 struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
520 int i;
521
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800522 /*
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200523 * no one uses the MSBs of flags for comparison,
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800524 * so we use it here to detect the first time of reception
525 */
526
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200527 if (!(lcf->flags & RX_RECV)) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800528 /* received data for the first time => send update to user */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200529 bcm_rx_update_and_send(op, lcf, rxdata);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800530 return;
531 }
532
Oliver Hartkopp72c8a892016-06-17 15:35:25 +0200533 /* do a real check in CAN frame data section */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200534 for (i = 0; i < rxdata->len; i += 8) {
535 if ((get_u64(cf, i) & get_u64(rxdata, i)) !=
536 (get_u64(cf, i) & get_u64(lcf, i))) {
537 bcm_rx_update_and_send(op, lcf, rxdata);
538 return;
539 }
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800540 }
541
542 if (op->flags & RX_CHECK_DLC) {
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200543 /* do a real check in CAN frame length */
544 if (rxdata->len != lcf->len) {
545 bcm_rx_update_and_send(op, lcf, rxdata);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800546 return;
547 }
548 }
549}
550
551/*
Jeremiah Mahler069f8452014-12-05 09:54:38 -0800552 * bcm_rx_starttimer - enable timeout monitoring for CAN frame reception
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800553 */
554static void bcm_rx_starttimer(struct bcm_op *op)
555{
556 if (op->flags & RX_NO_AUTOTIMER)
557 return;
558
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700559 if (op->kt_ival1.tv64)
560 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800561}
562
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800563static void bcm_rx_timeout_tsklet(unsigned long data)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800564{
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800565 struct bcm_op *op = (struct bcm_op *)data;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800566 struct bcm_msg_head msg_head;
567
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800568 /* create notification to user */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800569 msg_head.opcode = RX_TIMEOUT;
570 msg_head.flags = op->flags;
571 msg_head.count = op->count;
572 msg_head.ival1 = op->ival1;
573 msg_head.ival2 = op->ival2;
574 msg_head.can_id = op->can_id;
575 msg_head.nframes = 0;
576
577 bcm_send_to_user(op, &msg_head, NULL, 0);
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800578}
579
580/*
Jeremiah Mahler069f8452014-12-05 09:54:38 -0800581 * bcm_rx_timeout_handler - when the (cyclic) CAN frame reception timed out
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800582 */
583static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer)
584{
585 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer);
586
587 /* schedule before NET_RX_SOFTIRQ */
588 tasklet_hi_schedule(&op->tsklet);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800589
590 /* no restart of the timer is done here! */
591
592 /* if user wants to be informed, when cyclic CAN-Messages come back */
593 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
Oliver Hartkopp72c8a892016-06-17 15:35:25 +0200594 /* clear received CAN frames to indicate 'nothing received' */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200595 memset(op->last_frames, 0, op->nframes * op->cfsiz);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800596 }
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700597
598 return HRTIMER_NORESTART;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800599}
600
601/*
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800602 * bcm_rx_do_flush - helper for bcm_rx_thr_flush
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800603 */
Oliver Hartkopp5b75c492010-08-11 16:12:35 -0700604static inline int bcm_rx_do_flush(struct bcm_op *op, int update,
605 unsigned int index)
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800606{
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200607 struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
608
609 if ((op->last_frames) && (lcf->flags & RX_THR)) {
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800610 if (update)
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200611 bcm_rx_changed(op, lcf);
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800612 return 1;
613 }
614 return 0;
615}
616
617/*
618 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace
619 *
620 * update == 0 : just check if throttled data is available (any irq context)
621 * update == 1 : check and send throttled data to userspace (soft_irq context)
622 */
623static int bcm_rx_thr_flush(struct bcm_op *op, int update)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800624{
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700625 int updated = 0;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800626
627 if (op->nframes > 1) {
Oliver Hartkopp5b75c492010-08-11 16:12:35 -0700628 unsigned int i;
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700629
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800630 /* for MUX filter we start at index 1 */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800631 for (i = 1; i < op->nframes; i++)
632 updated += bcm_rx_do_flush(op, update, i);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800633
634 } else {
635 /* for RX_FILTER_ID and simple filter */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800636 updated += bcm_rx_do_flush(op, update, 0);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800637 }
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700638
639 return updated;
640}
641
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800642static void bcm_rx_thr_tsklet(unsigned long data)
643{
644 struct bcm_op *op = (struct bcm_op *)data;
645
646 /* push the changed data to the userspace */
647 bcm_rx_thr_flush(op, 1);
648}
649
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700650/*
651 * bcm_rx_thr_handler - the time for blocked content updates is over now:
652 * Check for throttled data and send it to the userspace
653 */
654static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
655{
656 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
657
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800658 tasklet_schedule(&op->thrtsklet);
659
660 if (bcm_rx_thr_flush(op, 0)) {
Oliver Hartkopp73e87e02008-04-15 19:29:14 -0700661 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
662 return HRTIMER_RESTART;
663 } else {
664 /* rearm throttle handling */
665 op->kt_lastmsg = ktime_set(0, 0);
666 return HRTIMER_NORESTART;
667 }
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800668}
669
670/*
Jeremiah Mahler069f8452014-12-05 09:54:38 -0800671 * bcm_rx_handler - handle a CAN frame reception
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800672 */
673static void bcm_rx_handler(struct sk_buff *skb, void *data)
674{
675 struct bcm_op *op = (struct bcm_op *)data;
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200676 const struct canfd_frame *rxframe = (struct canfd_frame *)skb->data;
Oliver Hartkopp5b75c492010-08-11 16:12:35 -0700677 unsigned int i;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800678
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800679 if (op->can_id != rxframe->can_id)
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800680 return;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800681
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200682 /* make sure to handle the correct frame type (CAN / CAN FD) */
683 if (skb->len != op->cfsiz)
684 return;
685
686 /* disable timeout */
687 hrtimer_cancel(&op->timer);
688
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800689 /* save rx timestamp */
690 op->rx_stamp = skb->tstamp;
691 /* save originator for recvfrom() */
692 op->rx_ifindex = skb->dev->ifindex;
693 /* update statistics */
694 op->frames_abs++;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800695
696 if (op->flags & RX_RTR_FRAME) {
697 /* send reply for RTR-request (placed in op->frames[0]) */
698 bcm_can_tx(op);
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800699 return;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800700 }
701
702 if (op->flags & RX_FILTER_ID) {
703 /* the easiest case */
Oliver Hartkopp5499a6b2016-11-23 14:33:25 +0100704 bcm_rx_update_and_send(op, op->last_frames, rxframe);
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800705 goto rx_starttimer;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800706 }
707
708 if (op->nframes == 1) {
709 /* simple compare with index 0 */
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800710 bcm_rx_cmp_to_index(op, 0, rxframe);
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800711 goto rx_starttimer;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800712 }
713
714 if (op->nframes > 1) {
715 /*
716 * multiplex compare
717 *
718 * find the first multiplex mask that fits.
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200719 * Remark: The MUX-mask is stored in index 0 - but only the
720 * first 64 bits of the frame data[] are relevant (CAN FD)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800721 */
722
723 for (i = 1; i < op->nframes; i++) {
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200724 if ((get_u64(op->frames, 0) & get_u64(rxframe, 0)) ==
725 (get_u64(op->frames, 0) &
726 get_u64(op->frames + op->cfsiz * i, 0))) {
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800727 bcm_rx_cmp_to_index(op, i, rxframe);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800728 break;
729 }
730 }
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800731 }
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800732
Oliver Hartkopp1fa17d42009-01-06 11:07:54 -0800733rx_starttimer:
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800734 bcm_rx_starttimer(op);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800735}
736
737/*
738 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
739 */
Oliver Hartkopp2b5f5f52016-06-17 15:35:26 +0200740static struct bcm_op *bcm_find_op(struct list_head *ops,
741 struct bcm_msg_head *mh, int ifindex)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800742{
743 struct bcm_op *op;
744
745 list_for_each_entry(op, ops, list) {
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200746 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
747 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME))
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800748 return op;
749 }
750
751 return NULL;
752}
753
754static void bcm_remove_op(struct bcm_op *op)
755{
Oliver Hartkoppa150e082017-01-18 21:30:51 +0100756 if (op->tsklet.func) {
757 while (test_bit(TASKLET_STATE_SCHED, &op->tsklet.state) ||
758 test_bit(TASKLET_STATE_RUN, &op->tsklet.state) ||
759 hrtimer_active(&op->timer)) {
760 hrtimer_cancel(&op->timer);
761 tasklet_kill(&op->tsklet);
762 }
763 }
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800764
Oliver Hartkoppa150e082017-01-18 21:30:51 +0100765 if (op->thrtsklet.func) {
766 while (test_bit(TASKLET_STATE_SCHED, &op->thrtsklet.state) ||
767 test_bit(TASKLET_STATE_RUN, &op->thrtsklet.state) ||
768 hrtimer_active(&op->thrtimer)) {
769 hrtimer_cancel(&op->thrtimer);
770 tasklet_kill(&op->thrtsklet);
771 }
772 }
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -0800773
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800774 if ((op->frames) && (op->frames != &op->sframe))
775 kfree(op->frames);
776
777 if ((op->last_frames) && (op->last_frames != &op->last_sframe))
778 kfree(op->last_frames);
779
780 kfree(op);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800781}
782
783static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
784{
785 if (op->rx_reg_dev == dev) {
786 can_rx_unregister(dev, op->can_id, REGMASK(op->can_id),
787 bcm_rx_handler, op);
788
789 /* mark as removed subscription */
790 op->rx_reg_dev = NULL;
791 } else
792 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device "
793 "mismatch %p %p\n", op->rx_reg_dev, dev);
794}
795
796/*
797 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
798 */
Oliver Hartkopp2b5f5f52016-06-17 15:35:26 +0200799static int bcm_delete_rx_op(struct list_head *ops, struct bcm_msg_head *mh,
800 int ifindex)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800801{
802 struct bcm_op *op, *n;
803
804 list_for_each_entry_safe(op, n, ops, list) {
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200805 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
806 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800807
808 /*
809 * Don't care if we're bound or not (due to netdev
810 * problems) can_rx_unregister() is always a save
811 * thing to do here.
812 */
813 if (op->ifindex) {
814 /*
815 * Only remove subscriptions that had not
816 * been removed due to NETDEV_UNREGISTER
817 * in bcm_notifier()
818 */
819 if (op->rx_reg_dev) {
820 struct net_device *dev;
821
822 dev = dev_get_by_index(&init_net,
823 op->ifindex);
824 if (dev) {
825 bcm_rx_unreg(dev, op);
826 dev_put(dev);
827 }
828 }
829 } else
830 can_rx_unregister(NULL, op->can_id,
831 REGMASK(op->can_id),
832 bcm_rx_handler, op);
833
834 list_del(&op->list);
835 bcm_remove_op(op);
836 return 1; /* done */
837 }
838 }
839
840 return 0; /* not found */
841}
842
843/*
844 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
845 */
Oliver Hartkopp2b5f5f52016-06-17 15:35:26 +0200846static int bcm_delete_tx_op(struct list_head *ops, struct bcm_msg_head *mh,
847 int ifindex)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800848{
849 struct bcm_op *op, *n;
850
851 list_for_each_entry_safe(op, n, ops, list) {
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200852 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
853 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800854 list_del(&op->list);
855 bcm_remove_op(op);
856 return 1; /* done */
857 }
858 }
859
860 return 0; /* not found */
861}
862
863/*
864 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg)
865 */
866static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
867 int ifindex)
868{
Oliver Hartkopp2b5f5f52016-06-17 15:35:26 +0200869 struct bcm_op *op = bcm_find_op(ops, msg_head, ifindex);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800870
871 if (!op)
872 return -EINVAL;
873
874 /* put current values into msg_head */
875 msg_head->flags = op->flags;
876 msg_head->count = op->count;
877 msg_head->ival1 = op->ival1;
878 msg_head->ival2 = op->ival2;
879 msg_head->nframes = op->nframes;
880
881 bcm_send_to_user(op, msg_head, op->frames, 0);
882
883 return MHSIZ;
884}
885
886/*
887 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg)
888 */
889static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
890 int ifindex, struct sock *sk)
891{
892 struct bcm_sock *bo = bcm_sk(sk);
893 struct bcm_op *op;
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200894 struct canfd_frame *cf;
Oliver Hartkopp5b75c492010-08-11 16:12:35 -0700895 unsigned int i;
896 int err;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800897
898 /* we need a real device to send frames */
899 if (!ifindex)
900 return -ENODEV;
901
Oliver Hartkopp72c8a892016-06-17 15:35:25 +0200902 /* check nframes boundaries - we need at least one CAN frame */
Oliver Hartkopp5b75c492010-08-11 16:12:35 -0700903 if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800904 return -EINVAL;
905
Oliver Hartkopp30e75172019-01-13 19:31:43 +0100906 /* check timeval limitations */
907 if ((msg_head->flags & SETTIMER) && bcm_is_invalid_tv(msg_head))
908 return -EINVAL;
909
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800910 /* check the given can_id */
Oliver Hartkopp2b5f5f52016-06-17 15:35:26 +0200911 op = bcm_find_op(&bo->tx_ops, msg_head, ifindex);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800912 if (op) {
913 /* update existing BCM operation */
914
915 /*
Oliver Hartkopp72c8a892016-06-17 15:35:25 +0200916 * Do we need more space for the CAN frames than currently
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800917 * allocated? -> This is a _really_ unusual use-case and
918 * therefore (complexity / locking) it is not supported.
919 */
920 if (msg_head->nframes > op->nframes)
921 return -E2BIG;
922
Oliver Hartkopp72c8a892016-06-17 15:35:25 +0200923 /* update CAN frames content */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800924 for (i = 0; i < msg_head->nframes; i++) {
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700925
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200926 cf = op->frames + op->cfsiz * i;
927 err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz);
928
929 if (op->flags & CAN_FD_FRAME) {
930 if (cf->len > 64)
931 err = -EINVAL;
932 } else {
933 if (cf->len > 8)
934 err = -EINVAL;
935 }
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700936
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800937 if (err < 0)
938 return err;
939
940 if (msg_head->flags & TX_CP_CAN_ID) {
941 /* copy can_id into frame */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200942 cf->can_id = msg_head->can_id;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800943 }
944 }
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200945 op->flags = msg_head->flags;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800946
947 } else {
948 /* insert new BCM operation for the given can_id */
949
950 op = kzalloc(OPSIZ, GFP_KERNEL);
951 if (!op)
952 return -ENOMEM;
953
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200954 op->can_id = msg_head->can_id;
955 op->cfsiz = CFSIZ(msg_head->flags);
956 op->flags = msg_head->flags;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800957
Oliver Hartkopp72c8a892016-06-17 15:35:25 +0200958 /* create array for CAN frames and copy the data */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800959 if (msg_head->nframes > 1) {
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200960 op->frames = kmalloc(msg_head->nframes * op->cfsiz,
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800961 GFP_KERNEL);
962 if (!op->frames) {
963 kfree(op);
964 return -ENOMEM;
965 }
966 } else
967 op->frames = &op->sframe;
968
969 for (i = 0; i < msg_head->nframes; i++) {
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700970
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200971 cf = op->frames + op->cfsiz * i;
972 err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz);
973
974 if (op->flags & CAN_FD_FRAME) {
975 if (cf->len > 64)
976 err = -EINVAL;
977 } else {
978 if (cf->len > 8)
979 err = -EINVAL;
980 }
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -0700981
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800982 if (err < 0) {
983 if (op->frames != &op->sframe)
984 kfree(op->frames);
985 kfree(op);
986 return err;
987 }
988
989 if (msg_head->flags & TX_CP_CAN_ID) {
990 /* copy can_id into frame */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +0200991 cf->can_id = msg_head->can_id;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -0800992 }
993 }
994
995 /* tx_ops never compare with previous received messages */
996 op->last_frames = NULL;
997
998 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
999 op->sk = sk;
1000 op->ifindex = ifindex;
1001
1002 /* initialize uninitialized (kzalloc) structure */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001003 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1004 op->timer.function = bcm_tx_timeout_handler;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001005
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -08001006 /* initialize tasklet for tx countevent notification */
1007 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
1008 (unsigned long) op);
1009
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001010 /* currently unused in tx_ops */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001011 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001012
1013 /* add this bcm_op to the list of the tx_ops */
1014 list_add(&op->list, &bo->tx_ops);
1015
1016 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */
1017
1018 if (op->nframes != msg_head->nframes) {
1019 op->nframes = msg_head->nframes;
1020 /* start multiple frame transmission with index 0 */
1021 op->currframe = 0;
1022 }
1023
1024 /* check flags */
1025
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001026 if (op->flags & TX_RESET_MULTI_IDX) {
1027 /* start multiple frame transmission with index 0 */
1028 op->currframe = 0;
1029 }
1030
1031 if (op->flags & SETTIMER) {
1032 /* set timer values */
1033 op->count = msg_head->count;
1034 op->ival1 = msg_head->ival1;
1035 op->ival2 = msg_head->ival2;
Arnd Bergmannba61a8d2015-09-30 13:26:42 +02001036 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
1037 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001038
1039 /* disable an active timer due to zero values? */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001040 if (!op->kt_ival1.tv64 && !op->kt_ival2.tv64)
1041 hrtimer_cancel(&op->timer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001042 }
1043
Oliver Hartkopp12d0d0d2011-09-29 15:33:47 -04001044 if (op->flags & STARTTIMER) {
1045 hrtimer_cancel(&op->timer);
Oliver Hartkopp72c8a892016-06-17 15:35:25 +02001046 /* spec: send CAN frame when starting timer */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001047 op->flags |= TX_ANNOUNCE;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001048 }
1049
Oliver Hartkoppaabdcb02011-09-23 08:23:47 +00001050 if (op->flags & TX_ANNOUNCE) {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001051 bcm_can_tx(op);
Oliver Hartkopp12d0d0d2011-09-29 15:33:47 -04001052 if (op->count)
Oliver Hartkoppaabdcb02011-09-23 08:23:47 +00001053 op->count--;
1054 }
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001055
Oliver Hartkopp12d0d0d2011-09-29 15:33:47 -04001056 if (op->flags & STARTTIMER)
1057 bcm_tx_start_timer(op);
1058
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02001059 return msg_head->nframes * op->cfsiz + MHSIZ;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001060}
1061
1062/*
1063 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg)
1064 */
1065static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1066 int ifindex, struct sock *sk)
1067{
1068 struct bcm_sock *bo = bcm_sk(sk);
1069 struct bcm_op *op;
1070 int do_rx_register;
1071 int err = 0;
1072
1073 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) {
1074 /* be robust against wrong usage ... */
1075 msg_head->flags |= RX_FILTER_ID;
1076 /* ignore trailing garbage */
1077 msg_head->nframes = 0;
1078 }
1079
Oliver Hartkopp5b75c492010-08-11 16:12:35 -07001080 /* the first element contains the mux-mask => MAX_NFRAMES + 1 */
1081 if (msg_head->nframes > MAX_NFRAMES + 1)
1082 return -EINVAL;
1083
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001084 if ((msg_head->flags & RX_RTR_FRAME) &&
1085 ((msg_head->nframes != 1) ||
1086 (!(msg_head->can_id & CAN_RTR_FLAG))))
1087 return -EINVAL;
1088
Oliver Hartkopp30e75172019-01-13 19:31:43 +01001089 /* check timeval limitations */
1090 if ((msg_head->flags & SETTIMER) && bcm_is_invalid_tv(msg_head))
1091 return -EINVAL;
1092
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001093 /* check the given can_id */
Oliver Hartkopp2b5f5f52016-06-17 15:35:26 +02001094 op = bcm_find_op(&bo->rx_ops, msg_head, ifindex);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001095 if (op) {
1096 /* update existing BCM operation */
1097
1098 /*
Oliver Hartkopp72c8a892016-06-17 15:35:25 +02001099 * Do we need more space for the CAN frames than currently
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001100 * allocated? -> This is a _really_ unusual use-case and
1101 * therefore (complexity / locking) it is not supported.
1102 */
1103 if (msg_head->nframes > op->nframes)
1104 return -E2BIG;
1105
1106 if (msg_head->nframes) {
Oliver Hartkopp72c8a892016-06-17 15:35:25 +02001107 /* update CAN frames content */
Oliver Hartkopp5499a6b2016-11-23 14:33:25 +01001108 err = memcpy_from_msg(op->frames, msg,
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02001109 msg_head->nframes * op->cfsiz);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001110 if (err < 0)
1111 return err;
1112
1113 /* clear last_frames to indicate 'nothing received' */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02001114 memset(op->last_frames, 0, msg_head->nframes * op->cfsiz);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001115 }
1116
1117 op->nframes = msg_head->nframes;
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02001118 op->flags = msg_head->flags;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001119
1120 /* Only an update -> do not call can_rx_register() */
1121 do_rx_register = 0;
1122
1123 } else {
1124 /* insert new BCM operation for the given can_id */
1125 op = kzalloc(OPSIZ, GFP_KERNEL);
1126 if (!op)
1127 return -ENOMEM;
1128
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02001129 op->can_id = msg_head->can_id;
1130 op->nframes = msg_head->nframes;
1131 op->cfsiz = CFSIZ(msg_head->flags);
1132 op->flags = msg_head->flags;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001133
1134 if (msg_head->nframes > 1) {
Oliver Hartkopp72c8a892016-06-17 15:35:25 +02001135 /* create array for CAN frames and copy the data */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02001136 op->frames = kmalloc(msg_head->nframes * op->cfsiz,
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001137 GFP_KERNEL);
1138 if (!op->frames) {
1139 kfree(op);
1140 return -ENOMEM;
1141 }
1142
Oliver Hartkopp72c8a892016-06-17 15:35:25 +02001143 /* create and init array for received CAN frames */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02001144 op->last_frames = kzalloc(msg_head->nframes * op->cfsiz,
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001145 GFP_KERNEL);
1146 if (!op->last_frames) {
1147 kfree(op->frames);
1148 kfree(op);
1149 return -ENOMEM;
1150 }
1151
1152 } else {
1153 op->frames = &op->sframe;
1154 op->last_frames = &op->last_sframe;
1155 }
1156
1157 if (msg_head->nframes) {
Oliver Hartkopp5499a6b2016-11-23 14:33:25 +01001158 err = memcpy_from_msg(op->frames, msg,
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02001159 msg_head->nframes * op->cfsiz);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001160 if (err < 0) {
1161 if (op->frames != &op->sframe)
1162 kfree(op->frames);
1163 if (op->last_frames != &op->last_sframe)
1164 kfree(op->last_frames);
1165 kfree(op);
1166 return err;
1167 }
1168 }
1169
1170 /* bcm_can_tx / bcm_tx_timeout_handler needs this */
1171 op->sk = sk;
1172 op->ifindex = ifindex;
1173
Oliver Hartkopp81b40112012-11-26 22:24:23 +01001174 /* ifindex for timeout events w/o previous frame reception */
1175 op->rx_ifindex = ifindex;
1176
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001177 /* initialize uninitialized (kzalloc) structure */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001178 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1179 op->timer.function = bcm_rx_timeout_handler;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001180
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -08001181 /* initialize tasklet for rx timeout notification */
1182 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1183 (unsigned long) op);
1184
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001185 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1186 op->thrtimer.function = bcm_rx_thr_handler;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001187
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -08001188 /* initialize tasklet for rx throttle handling */
1189 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1190 (unsigned long) op);
1191
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001192 /* add this bcm_op to the list of the rx_ops */
1193 list_add(&op->list, &bo->rx_ops);
1194
1195 /* call can_rx_register() */
1196 do_rx_register = 1;
1197
1198 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */
1199
1200 /* check flags */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001201
1202 if (op->flags & RX_RTR_FRAME) {
Oliver Hartkopp5499a6b2016-11-23 14:33:25 +01001203 struct canfd_frame *frame0 = op->frames;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001204
1205 /* no timers in RTR-mode */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001206 hrtimer_cancel(&op->thrtimer);
1207 hrtimer_cancel(&op->timer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001208
1209 /*
1210 * funny feature in RX(!)_SETUP only for RTR-mode:
1211 * copy can_id into frame BUT without RTR-flag to
1212 * prevent a full-load-loopback-test ... ;-]
1213 */
1214 if ((op->flags & TX_CP_CAN_ID) ||
Oliver Hartkopp5499a6b2016-11-23 14:33:25 +01001215 (frame0->can_id == op->can_id))
1216 frame0->can_id = op->can_id & ~CAN_RTR_FLAG;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001217
1218 } else {
1219 if (op->flags & SETTIMER) {
1220
1221 /* set timer value */
1222 op->ival1 = msg_head->ival1;
1223 op->ival2 = msg_head->ival2;
Arnd Bergmannba61a8d2015-09-30 13:26:42 +02001224 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
1225 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001226
1227 /* disable an active timer due to zero value? */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001228 if (!op->kt_ival1.tv64)
1229 hrtimer_cancel(&op->timer);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001230
1231 /*
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001232 * In any case cancel the throttle timer, flush
1233 * potentially blocked msgs and reset throttle handling
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001234 */
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001235 op->kt_lastmsg = ktime_set(0, 0);
1236 hrtimer_cancel(&op->thrtimer);
Oliver Hartkopp6e5c1722009-01-04 17:31:18 -08001237 bcm_rx_thr_flush(op, 1);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001238 }
1239
Oliver Hartkopp73e87e02008-04-15 19:29:14 -07001240 if ((op->flags & STARTTIMER) && op->kt_ival1.tv64)
1241 hrtimer_start(&op->timer, op->kt_ival1,
1242 HRTIMER_MODE_REL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001243 }
1244
1245 /* now we can register for can_ids, if we added a new bcm_op */
1246 if (do_rx_register) {
1247 if (ifindex) {
1248 struct net_device *dev;
1249
1250 dev = dev_get_by_index(&init_net, ifindex);
1251 if (dev) {
1252 err = can_rx_register(dev, op->can_id,
1253 REGMASK(op->can_id),
1254 bcm_rx_handler, op,
Eric Dumazetadf86d52017-01-27 08:11:44 -08001255 "bcm", sk);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001256
1257 op->rx_reg_dev = dev;
1258 dev_put(dev);
1259 }
1260
1261 } else
1262 err = can_rx_register(NULL, op->can_id,
1263 REGMASK(op->can_id),
Eric Dumazetadf86d52017-01-27 08:11:44 -08001264 bcm_rx_handler, op, "bcm", sk);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001265 if (err) {
1266 /* this bcm rx op is broken -> remove it */
1267 list_del(&op->list);
1268 bcm_remove_op(op);
1269 return err;
1270 }
1271 }
1272
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02001273 return msg_head->nframes * op->cfsiz + MHSIZ;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001274}
1275
1276/*
1277 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1278 */
Oliver Hartkopp2b5f5f52016-06-17 15:35:26 +02001279static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk,
1280 int cfsiz)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001281{
1282 struct sk_buff *skb;
1283 struct net_device *dev;
1284 int err;
1285
1286 /* we need a real device to send frames */
1287 if (!ifindex)
1288 return -ENODEV;
1289
Oliver Hartkopp2b5f5f52016-06-17 15:35:26 +02001290 skb = alloc_skb(cfsiz + sizeof(struct can_skb_priv), GFP_KERNEL);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001291 if (!skb)
1292 return -ENOMEM;
1293
Oliver Hartkopp2bf34402013-01-28 08:33:33 +00001294 can_skb_reserve(skb);
Oliver Hartkopp156c2bb2013-01-17 18:43:39 +01001295
Oliver Hartkopp2b5f5f52016-06-17 15:35:26 +02001296 err = memcpy_from_msg(skb_put(skb, cfsiz), msg, cfsiz);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001297 if (err < 0) {
1298 kfree_skb(skb);
1299 return err;
1300 }
1301
1302 dev = dev_get_by_index(&init_net, ifindex);
1303 if (!dev) {
1304 kfree_skb(skb);
1305 return -ENODEV;
1306 }
1307
Oliver Hartkopp2bf34402013-01-28 08:33:33 +00001308 can_skb_prv(skb)->ifindex = dev->ifindex;
Oliver Hartkoppd3b58c42015-06-26 11:58:19 +02001309 can_skb_prv(skb)->skbcnt = 0;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001310 skb->dev = dev;
Oliver Hartkopp0ae89be2014-01-30 10:11:28 +01001311 can_skb_set_owner(skb, sk);
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -07001312 err = can_send(skb, 1); /* send with loopback */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001313 dev_put(dev);
1314
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -07001315 if (err)
1316 return err;
1317
Oliver Hartkopp2b5f5f52016-06-17 15:35:26 +02001318 return cfsiz + MHSIZ;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001319}
1320
1321/*
1322 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1323 */
Ying Xue1b784142015-03-02 15:37:48 +08001324static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001325{
1326 struct sock *sk = sock->sk;
1327 struct bcm_sock *bo = bcm_sk(sk);
1328 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */
1329 struct bcm_msg_head msg_head;
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02001330 int cfsiz;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001331 int ret; /* read bytes or error codes as return value */
1332
1333 if (!bo->bound)
1334 return -ENOTCONN;
1335
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -07001336 /* check for valid message length from userspace */
Oliver Hartkopp2b5f5f52016-06-17 15:35:26 +02001337 if (size < MHSIZ)
1338 return -EINVAL;
1339
1340 /* read message head information */
1341 ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ);
1342 if (ret < 0)
1343 return ret;
1344
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02001345 cfsiz = CFSIZ(msg_head.flags);
1346 if ((size - MHSIZ) % cfsiz)
Oliver Hartkopp7f2d38e2008-07-05 23:38:43 -07001347 return -EINVAL;
1348
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001349 /* check for alternative ifindex for this bcm_op */
1350
1351 if (!ifindex && msg->msg_name) {
1352 /* no bound device as default => check msg_name */
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001353 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001354
Kurt Van Dijck5e507322011-01-15 20:56:42 -08001355 if (msg->msg_namelen < sizeof(*addr))
1356 return -EINVAL;
1357
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001358 if (addr->can_family != AF_CAN)
1359 return -EINVAL;
1360
1361 /* ifindex from sendto() */
1362 ifindex = addr->can_ifindex;
1363
1364 if (ifindex) {
1365 struct net_device *dev;
1366
1367 dev = dev_get_by_index(&init_net, ifindex);
1368 if (!dev)
1369 return -ENODEV;
1370
1371 if (dev->type != ARPHRD_CAN) {
1372 dev_put(dev);
1373 return -ENODEV;
1374 }
1375
1376 dev_put(dev);
1377 }
1378 }
1379
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001380 lock_sock(sk);
1381
1382 switch (msg_head.opcode) {
1383
1384 case TX_SETUP:
1385 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk);
1386 break;
1387
1388 case RX_SETUP:
1389 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk);
1390 break;
1391
1392 case TX_DELETE:
Oliver Hartkopp2b5f5f52016-06-17 15:35:26 +02001393 if (bcm_delete_tx_op(&bo->tx_ops, &msg_head, ifindex))
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001394 ret = MHSIZ;
1395 else
1396 ret = -EINVAL;
1397 break;
1398
1399 case RX_DELETE:
Oliver Hartkopp2b5f5f52016-06-17 15:35:26 +02001400 if (bcm_delete_rx_op(&bo->rx_ops, &msg_head, ifindex))
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001401 ret = MHSIZ;
1402 else
1403 ret = -EINVAL;
1404 break;
1405
1406 case TX_READ:
1407 /* reuse msg_head for the reply to TX_READ */
1408 msg_head.opcode = TX_STATUS;
1409 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex);
1410 break;
1411
1412 case RX_READ:
1413 /* reuse msg_head for the reply to RX_READ */
1414 msg_head.opcode = RX_STATUS;
1415 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1416 break;
1417
1418 case TX_SEND:
Oliver Hartkopp72c8a892016-06-17 15:35:25 +02001419 /* we need exactly one CAN frame behind the msg head */
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02001420 if ((msg_head.nframes != 1) || (size != cfsiz + MHSIZ))
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001421 ret = -EINVAL;
1422 else
Oliver Hartkopp6f3b9112016-06-17 15:35:27 +02001423 ret = bcm_tx_send(msg, ifindex, sk, cfsiz);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001424 break;
1425
1426 default:
1427 ret = -EINVAL;
1428 break;
1429 }
1430
1431 release_sock(sk);
1432
1433 return ret;
1434}
1435
1436/*
1437 * notification handler for netdevice status changes
1438 */
1439static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
Jiri Pirko351638e2013-05-28 01:30:21 +00001440 void *ptr)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001441{
Jiri Pirko351638e2013-05-28 01:30:21 +00001442 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001443 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1444 struct sock *sk = &bo->sk;
1445 struct bcm_op *op;
1446 int notify_enodev = 0;
1447
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001448 if (!net_eq(dev_net(dev), &init_net))
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001449 return NOTIFY_DONE;
1450
1451 if (dev->type != ARPHRD_CAN)
1452 return NOTIFY_DONE;
1453
1454 switch (msg) {
1455
1456 case NETDEV_UNREGISTER:
1457 lock_sock(sk);
1458
1459 /* remove device specific receive entries */
1460 list_for_each_entry(op, &bo->rx_ops, list)
1461 if (op->rx_reg_dev == dev)
1462 bcm_rx_unreg(dev, op);
1463
1464 /* remove device reference, if this is our bound device */
1465 if (bo->bound && bo->ifindex == dev->ifindex) {
1466 bo->bound = 0;
1467 bo->ifindex = 0;
1468 notify_enodev = 1;
1469 }
1470
1471 release_sock(sk);
1472
1473 if (notify_enodev) {
1474 sk->sk_err = ENODEV;
1475 if (!sock_flag(sk, SOCK_DEAD))
1476 sk->sk_error_report(sk);
1477 }
1478 break;
1479
1480 case NETDEV_DOWN:
1481 if (bo->bound && bo->ifindex == dev->ifindex) {
1482 sk->sk_err = ENETDOWN;
1483 if (!sock_flag(sk, SOCK_DEAD))
1484 sk->sk_error_report(sk);
1485 }
1486 }
1487
1488 return NOTIFY_DONE;
1489}
1490
1491/*
1492 * initial settings for all BCM sockets to be set at socket creation time
1493 */
1494static int bcm_init(struct sock *sk)
1495{
1496 struct bcm_sock *bo = bcm_sk(sk);
1497
1498 bo->bound = 0;
1499 bo->ifindex = 0;
1500 bo->dropped_usr_msgs = 0;
1501 bo->bcm_proc_read = NULL;
1502
1503 INIT_LIST_HEAD(&bo->tx_ops);
1504 INIT_LIST_HEAD(&bo->rx_ops);
1505
1506 /* set notifier */
1507 bo->notifier.notifier_call = bcm_notifier;
1508
1509 register_netdevice_notifier(&bo->notifier);
1510
1511 return 0;
1512}
1513
1514/*
1515 * standard socket functions
1516 */
1517static int bcm_release(struct socket *sock)
1518{
1519 struct sock *sk = sock->sk;
Dave Jonesc6914a62011-04-19 20:36:59 -07001520 struct bcm_sock *bo;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001521 struct bcm_op *op, *next;
1522
Dave Jonesc6914a62011-04-19 20:36:59 -07001523 if (sk == NULL)
1524 return 0;
1525
1526 bo = bcm_sk(sk);
1527
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001528 /* remove bcm_ops, timer, rx_unregister(), etc. */
1529
1530 unregister_netdevice_notifier(&bo->notifier);
1531
1532 lock_sock(sk);
1533
1534 list_for_each_entry_safe(op, next, &bo->tx_ops, list)
1535 bcm_remove_op(op);
1536
1537 list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
1538 /*
1539 * Don't care if we're bound or not (due to netdev problems)
1540 * can_rx_unregister() is always a save thing to do here.
1541 */
1542 if (op->ifindex) {
1543 /*
1544 * Only remove subscriptions that had not
1545 * been removed due to NETDEV_UNREGISTER
1546 * in bcm_notifier()
1547 */
1548 if (op->rx_reg_dev) {
1549 struct net_device *dev;
1550
1551 dev = dev_get_by_index(&init_net, op->ifindex);
1552 if (dev) {
1553 bcm_rx_unreg(dev, op);
1554 dev_put(dev);
1555 }
1556 }
1557 } else
1558 can_rx_unregister(NULL, op->can_id,
1559 REGMASK(op->can_id),
1560 bcm_rx_handler, op);
1561
1562 bcm_remove_op(op);
1563 }
1564
1565 /* remove procfs entry */
1566 if (proc_dir && bo->bcm_proc_read)
1567 remove_proc_entry(bo->procname, proc_dir);
1568
1569 /* remove device reference */
1570 if (bo->bound) {
1571 bo->bound = 0;
1572 bo->ifindex = 0;
1573 }
1574
Lothar Waßmannf7e5cc02009-07-14 23:10:21 +00001575 sock_orphan(sk);
1576 sock->sk = NULL;
1577
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001578 release_sock(sk);
1579 sock_put(sk);
1580
1581 return 0;
1582}
1583
1584static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1585 int flags)
1586{
1587 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
1588 struct sock *sk = sock->sk;
1589 struct bcm_sock *bo = bcm_sk(sk);
Oliver Hartkoppdeb507f2016-10-24 21:11:26 +02001590 int ret = 0;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001591
Changli Gao6503d962010-03-31 22:58:26 +00001592 if (len < sizeof(*addr))
1593 return -EINVAL;
1594
Oliver Hartkoppdeb507f2016-10-24 21:11:26 +02001595 lock_sock(sk);
1596
1597 if (bo->bound) {
1598 ret = -EISCONN;
1599 goto fail;
1600 }
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001601
1602 /* bind a device to this socket */
1603 if (addr->can_ifindex) {
1604 struct net_device *dev;
1605
1606 dev = dev_get_by_index(&init_net, addr->can_ifindex);
Oliver Hartkoppdeb507f2016-10-24 21:11:26 +02001607 if (!dev) {
1608 ret = -ENODEV;
1609 goto fail;
1610 }
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001611 if (dev->type != ARPHRD_CAN) {
1612 dev_put(dev);
Oliver Hartkoppdeb507f2016-10-24 21:11:26 +02001613 ret = -ENODEV;
1614 goto fail;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001615 }
1616
1617 bo->ifindex = dev->ifindex;
1618 dev_put(dev);
1619
1620 } else {
1621 /* no interface reference for ifindex = 0 ('any' CAN device) */
1622 bo->ifindex = 0;
1623 }
1624
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001625 if (proc_dir) {
1626 /* unique socket address as filename */
Dan Rosenberg9f260e02010-12-26 06:54:53 +00001627 sprintf(bo->procname, "%lu", sock_i_ino(sk));
Alexey Dobriyanea00b8e2009-08-28 09:57:21 +00001628 bo->bcm_proc_read = proc_create_data(bo->procname, 0644,
1629 proc_dir,
1630 &bcm_proc_fops, sk);
Oliver Hartkoppdeb507f2016-10-24 21:11:26 +02001631 if (!bo->bcm_proc_read) {
1632 ret = -ENOMEM;
1633 goto fail;
1634 }
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001635 }
1636
Oliver Hartkoppdeb507f2016-10-24 21:11:26 +02001637 bo->bound = 1;
1638
1639fail:
1640 release_sock(sk);
1641
1642 return ret;
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001643}
1644
Ying Xue1b784142015-03-02 15:37:48 +08001645static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1646 int flags)
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001647{
1648 struct sock *sk = sock->sk;
1649 struct sk_buff *skb;
1650 int error = 0;
1651 int noblock;
1652 int err;
1653
1654 noblock = flags & MSG_DONTWAIT;
1655 flags &= ~MSG_DONTWAIT;
1656 skb = skb_recv_datagram(sk, flags, noblock, &error);
1657 if (!skb)
1658 return error;
1659
1660 if (skb->len < size)
1661 size = skb->len;
1662
Al Viro7eab8d92014-04-06 21:51:23 -04001663 err = memcpy_to_msg(msg, skb->data, size);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001664 if (err < 0) {
1665 skb_free_datagram(sk, skb);
1666 return err;
1667 }
1668
Neil Horman3b885782009-10-12 13:26:31 -07001669 sock_recv_ts_and_drops(msg, sk, skb);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001670
1671 if (msg->msg_name) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001672 __sockaddr_check_size(sizeof(struct sockaddr_can));
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001673 msg->msg_namelen = sizeof(struct sockaddr_can);
1674 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1675 }
1676
1677 skb_free_datagram(sk, skb);
1678
1679 return size;
1680}
1681
Oliver Hartkopp53914b62011-03-22 08:27:25 +00001682static const struct proto_ops bcm_ops = {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001683 .family = PF_CAN,
1684 .release = bcm_release,
1685 .bind = sock_no_bind,
1686 .connect = bcm_connect,
1687 .socketpair = sock_no_socketpair,
1688 .accept = sock_no_accept,
1689 .getname = sock_no_getname,
1690 .poll = datagram_poll,
Oliver Hartkopp53914b62011-03-22 08:27:25 +00001691 .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001692 .listen = sock_no_listen,
1693 .shutdown = sock_no_shutdown,
1694 .setsockopt = sock_no_setsockopt,
1695 .getsockopt = sock_no_getsockopt,
1696 .sendmsg = bcm_sendmsg,
1697 .recvmsg = bcm_recvmsg,
1698 .mmap = sock_no_mmap,
1699 .sendpage = sock_no_sendpage,
1700};
1701
1702static struct proto bcm_proto __read_mostly = {
1703 .name = "CAN_BCM",
1704 .owner = THIS_MODULE,
1705 .obj_size = sizeof(struct bcm_sock),
1706 .init = bcm_init,
1707};
1708
Kurt Van Dijck16506292011-05-03 18:40:57 +00001709static const struct can_proto bcm_can_proto = {
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001710 .type = SOCK_DGRAM,
1711 .protocol = CAN_BCM,
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001712 .ops = &bcm_ops,
1713 .prot = &bcm_proto,
1714};
1715
1716static int __init bcm_module_init(void)
1717{
1718 int err;
1719
Jeremiah Mahlerb111b782014-11-21 23:42:35 -08001720 pr_info("can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n");
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001721
1722 err = can_proto_register(&bcm_can_proto);
1723 if (err < 0) {
1724 printk(KERN_ERR "can: registration of bcm protocol failed\n");
1725 return err;
1726 }
1727
1728 /* create /proc/net/can-bcm directory */
1729 proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001730 return 0;
1731}
1732
1733static void __exit bcm_module_exit(void)
1734{
1735 can_proto_unregister(&bcm_can_proto);
1736
1737 if (proc_dir)
Gao fengece31ff2013-02-18 01:34:56 +00001738 remove_proc_entry("can-bcm", init_net.proc_net);
Oliver Hartkoppffd980f2007-11-16 15:53:52 -08001739}
1740
1741module_init(bcm_module_init);
1742module_exit(bcm_module_exit);