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