1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42#include <linux/module.h>
43#include <linux/init.h>
44#include <linux/interrupt.h>
45#include <linux/hrtimer.h>
46#include <linux/list.h>
47#include <linux/proc_fs.h>
48#include <linux/seq_file.h>
49#include <linux/uio.h>
50#include <linux/net.h>
51#include <linux/netdevice.h>
52#include <linux/socket.h>
53#include <linux/if_arp.h>
54#include <linux/skbuff.h>
55#include <linux/can.h>
56#include <linux/can/core.h>
57#include <linux/can/skb.h>
58#include <linux/can/bcm.h>
59#include <linux/slab.h>
60#include <net/sock.h>
61#include <net/net_namespace.h>
62
63
64
65
66
67
68#define MAX_NFRAMES 256
69
70
71#define RX_RECV 0x40
72#define RX_THR 0x80
73#define BCM_CAN_FLAGS_MASK 0x3F
74
75
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))
79
80#define CAN_BCM_VERSION "20170425"
81
82MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
83MODULE_LICENSE("Dual BSD/GPL");
84MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>");
85MODULE_ALIAS("can-proto-2");
86
87
88
89
90
91
92static inline u64 get_u64(const struct canfd_frame *cp, int offset)
93{
94 return *(u64 *)(cp->data + offset);
95}
96
97struct bcm_op {
98 struct list_head list;
99 int ifindex;
100 canid_t can_id;
101 u32 flags;
102 unsigned long frames_abs, frames_filtered;
103 struct bcm_timeval ival1, ival2;
104 struct hrtimer timer, thrtimer;
105 struct tasklet_struct tsklet, thrtsklet;
106 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg;
107 int rx_ifindex;
108 int cfsiz;
109 u32 count;
110 u32 nframes;
111 u32 currframe;
112
113 void *frames;
114 void *last_frames;
115 struct canfd_frame sframe;
116 struct canfd_frame last_sframe;
117 struct sock *sk;
118 struct net_device *rx_reg_dev;
119};
120
121struct 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;
130 char procname [32];
131};
132
133static inline struct bcm_sock *bcm_sk(const struct sock *sk)
134{
135 return (struct bcm_sock *)sk;
136}
137
138static 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
143#define CFSIZ(flags) ((flags & CAN_FD_FRAME) ? CANFD_MTU : CAN_MTU)
144#define OPSIZ sizeof(struct bcm_op)
145#define MHSIZ sizeof(struct bcm_msg_head)
146
147
148
149
150#if IS_ENABLED(CONFIG_PROC_FS)
151static char *bcm_proc_getifname(struct net *net, char *result, int ifindex)
152{
153 struct net_device *dev;
154
155 if (!ifindex)
156 return "any";
157
158 rcu_read_lock();
159 dev = dev_get_by_index_rcu(net, ifindex);
160 if (dev)
161 strcpy(result, dev->name);
162 else
163 strcpy(result, "???");
164 rcu_read_unlock();
165
166 return result;
167}
168
169static int bcm_proc_show(struct seq_file *m, void *v)
170{
171 char ifname[IFNAMSIZ];
172 struct net *net = m->private;
173 struct sock *sk = (struct sock *)PDE_DATA(m->file->f_inode);
174 struct bcm_sock *bo = bcm_sk(sk);
175 struct bcm_op *op;
176
177 seq_printf(m, ">>> socket %pK", sk->sk_socket);
178 seq_printf(m, " / sk %pK", sk);
179 seq_printf(m, " / bo %pK", bo);
180 seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
181 seq_printf(m, " / bound %s", bcm_proc_getifname(net, ifname, bo->ifindex));
182 seq_printf(m, " <<<\n");
183
184 list_for_each_entry(op, &bo->rx_ops, list) {
185
186 unsigned long reduction;
187
188
189 if (!op->frames_abs)
190 continue;
191
192 seq_printf(m, "rx_op: %03X %-5s ", op->can_id,
193 bcm_proc_getifname(net, ifname, op->ifindex));
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
202 if (op->kt_ival1)
203 seq_printf(m, "timeo=%lld ",
204 (long long)ktime_to_us(op->kt_ival1));
205
206 if (op->kt_ival2)
207 seq_printf(m, "thr=%lld ",
208 (long long)ktime_to_us(op->kt_ival2));
209
210 seq_printf(m, "# recv %ld (%ld) => reduction: ",
211 op->frames_filtered, op->frames_abs);
212
213 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs;
214
215 seq_printf(m, "%s%ld%%\n",
216 (reduction == 100) ? "near " : "", reduction);
217 }
218
219 list_for_each_entry(op, &bo->tx_ops, list) {
220
221 seq_printf(m, "tx_op: %03X %s ", op->can_id,
222 bcm_proc_getifname(net, ifname, op->ifindex));
223
224 if (op->flags & CAN_FD_FRAME)
225 seq_printf(m, "(%u) ", op->nframes);
226 else
227 seq_printf(m, "[%u] ", op->nframes);
228
229 if (op->kt_ival1)
230 seq_printf(m, "t1=%lld ",
231 (long long)ktime_to_us(op->kt_ival1));
232
233 if (op->kt_ival2)
234 seq_printf(m, "t2=%lld ",
235 (long long)ktime_to_us(op->kt_ival2));
236
237 seq_printf(m, "# sent %ld\n", op->frames_abs);
238 }
239 seq_putc(m, '\n');
240 return 0;
241}
242#endif
243
244
245
246
247
248static void bcm_can_tx(struct bcm_op *op)
249{
250 struct sk_buff *skb;
251 struct net_device *dev;
252 struct canfd_frame *cf = op->frames + op->cfsiz * op->currframe;
253
254
255 if (!op->ifindex)
256 return;
257
258 dev = dev_get_by_index(sock_net(op->sk), op->ifindex);
259 if (!dev) {
260
261 return;
262 }
263
264 skb = alloc_skb(op->cfsiz + sizeof(struct can_skb_priv), gfp_any());
265 if (!skb)
266 goto out;
267
268 can_skb_reserve(skb);
269 can_skb_prv(skb)->ifindex = dev->ifindex;
270 can_skb_prv(skb)->skbcnt = 0;
271
272 skb_put_data(skb, cf, op->cfsiz);
273
274
275 skb->dev = dev;
276 can_skb_set_owner(skb, op->sk);
277 can_send(skb, 1);
278
279
280 op->currframe++;
281 op->frames_abs++;
282
283
284 if (op->currframe >= op->nframes)
285 op->currframe = 0;
286out:
287 dev_put(dev);
288}
289
290
291
292
293
294static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
295 struct canfd_frame *frames, int has_timestamp)
296{
297 struct sk_buff *skb;
298 struct canfd_frame *firstframe;
299 struct sockaddr_can *addr;
300 struct sock *sk = op->sk;
301 unsigned int datalen = head->nframes * op->cfsiz;
302 int err;
303
304 skb = alloc_skb(sizeof(*head) + datalen, gfp_any());
305 if (!skb)
306 return;
307
308 skb_put_data(skb, head, sizeof(*head));
309
310 if (head->nframes) {
311
312 firstframe = (struct canfd_frame *)skb_tail_pointer(skb);
313
314 skb_put_data(skb, frames, datalen);
315
316
317
318
319
320
321
322 if (head->nframes == 1)
323 firstframe->flags &= BCM_CAN_FLAGS_MASK;
324 }
325
326 if (has_timestamp) {
327
328 skb->tstamp = op->rx_stamp;
329 }
330
331
332
333
334
335
336
337
338 sock_skb_cb_check_size(sizeof(struct sockaddr_can));
339 addr = (struct sockaddr_can *)skb->cb;
340 memset(addr, 0, sizeof(*addr));
341 addr->can_family = AF_CAN;
342 addr->can_ifindex = op->rx_ifindex;
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
350 bo->dropped_usr_msgs++;
351 }
352}
353
354static void bcm_tx_start_timer(struct bcm_op *op)
355{
356 if (op->kt_ival1 && op->count)
357 hrtimer_start(&op->timer,
358 ktime_add(ktime_get(), op->kt_ival1),
359 HRTIMER_MODE_ABS);
360 else if (op->kt_ival2)
361 hrtimer_start(&op->timer,
362 ktime_add(ktime_get(), op->kt_ival2),
363 HRTIMER_MODE_ABS);
364}
365
366static 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
371 if (op->kt_ival1 && (op->count > 0)) {
372
373 op->count--;
374 if (!op->count && (op->flags & TX_COUNTEVT)) {
375
376
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 }
387 bcm_can_tx(op);
388
389 } else if (op->kt_ival2)
390 bcm_can_tx(op);
391
392 bcm_tx_start_timer(op);
393}
394
395
396
397
398static 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);
403
404 return HRTIMER_NORESTART;
405}
406
407
408
409
410static void bcm_rx_changed(struct bcm_op *op, struct canfd_frame *data)
411{
412 struct bcm_msg_head head;
413
414
415 op->frames_filtered++;
416
417
418 if (op->frames_filtered > ULONG_MAX/100)
419 op->frames_filtered = op->frames_abs = 0;
420
421
422 data->flags &= (BCM_CAN_FLAGS_MASK|RX_RECV);
423
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
437
438
439
440static void bcm_rx_update_and_send(struct bcm_op *op,
441 struct canfd_frame *lastdata,
442 const struct canfd_frame *rxdata)
443{
444 memcpy(lastdata, rxdata, op->cfsiz);
445
446
447 lastdata->flags |= (RX_RECV|RX_THR);
448
449
450 if (!op->kt_ival2) {
451
452 bcm_rx_changed(op, lastdata);
453 return;
454 }
455
456
457 if (hrtimer_active(&op->thrtimer))
458 return;
459
460
461 if (!op->kt_lastmsg)
462 goto rx_changed_settime;
463
464
465 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) <
466 ktime_to_us(op->kt_ival2)) {
467
468 hrtimer_start(&op->thrtimer,
469 ktime_add(op->kt_lastmsg, op->kt_ival2),
470 HRTIMER_MODE_ABS);
471 return;
472 }
473
474
475rx_changed_settime:
476 bcm_rx_changed(op, lastdata);
477 op->kt_lastmsg = ktime_get();
478}
479
480
481
482
483
484static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index,
485 const struct canfd_frame *rxdata)
486{
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
491
492
493
494
495
496 if (!(lcf->flags & RX_RECV)) {
497
498 bcm_rx_update_and_send(op, lcf, rxdata);
499 return;
500 }
501
502
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 }
509 }
510
511 if (op->flags & RX_CHECK_DLC) {
512
513 if (rxdata->len != lcf->len) {
514 bcm_rx_update_and_send(op, lcf, rxdata);
515 return;
516 }
517 }
518}
519
520
521
522
523static void bcm_rx_starttimer(struct bcm_op *op)
524{
525 if (op->flags & RX_NO_AUTOTIMER)
526 return;
527
528 if (op->kt_ival1)
529 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL);
530}
531
532static void bcm_rx_timeout_tsklet(unsigned long data)
533{
534 struct bcm_op *op = (struct bcm_op *)data;
535 struct bcm_msg_head msg_head;
536
537
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);
547}
548
549
550
551
552static 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
557 tasklet_hi_schedule(&op->tsklet);
558
559
560
561
562 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) {
563
564 memset(op->last_frames, 0, op->nframes * op->cfsiz);
565 }
566
567 return HRTIMER_NORESTART;
568}
569
570
571
572
573static inline int bcm_rx_do_flush(struct bcm_op *op, int update,
574 unsigned int index)
575{
576 struct canfd_frame *lcf = op->last_frames + op->cfsiz * index;
577
578 if ((op->last_frames) && (lcf->flags & RX_THR)) {
579 if (update)
580 bcm_rx_changed(op, lcf);
581 return 1;
582 }
583 return 0;
584}
585
586
587
588
589
590
591
592static int bcm_rx_thr_flush(struct bcm_op *op, int update)
593{
594 int updated = 0;
595
596 if (op->nframes > 1) {
597 unsigned int i;
598
599
600 for (i = 1; i < op->nframes; i++)
601 updated += bcm_rx_do_flush(op, update, i);
602
603 } else {
604
605 updated += bcm_rx_do_flush(op, update, 0);
606 }
607
608 return updated;
609}
610
611static void bcm_rx_thr_tsklet(unsigned long data)
612{
613 struct bcm_op *op = (struct bcm_op *)data;
614
615
616 bcm_rx_thr_flush(op, 1);
617}
618
619
620
621
622
623static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer)
624{
625 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer);
626
627 tasklet_schedule(&op->thrtsklet);
628
629 if (bcm_rx_thr_flush(op, 0)) {
630 hrtimer_forward(hrtimer, ktime_get(), op->kt_ival2);
631 return HRTIMER_RESTART;
632 } else {
633
634 op->kt_lastmsg = 0;
635 return HRTIMER_NORESTART;
636 }
637}
638
639
640
641
642static void bcm_rx_handler(struct sk_buff *skb, void *data)
643{
644 struct bcm_op *op = (struct bcm_op *)data;
645 const struct canfd_frame *rxframe = (struct canfd_frame *)skb->data;
646 unsigned int i;
647
648 if (op->can_id != rxframe->can_id)
649 return;
650
651
652 if (skb->len != op->cfsiz)
653 return;
654
655
656 hrtimer_cancel(&op->timer);
657
658
659 op->rx_stamp = skb->tstamp;
660
661 op->rx_ifindex = skb->dev->ifindex;
662
663 op->frames_abs++;
664
665 if (op->flags & RX_RTR_FRAME) {
666
667 bcm_can_tx(op);
668 return;
669 }
670
671 if (op->flags & RX_FILTER_ID) {
672
673 bcm_rx_update_and_send(op, op->last_frames, rxframe);
674 goto rx_starttimer;
675 }
676
677 if (op->nframes == 1) {
678
679 bcm_rx_cmp_to_index(op, 0, rxframe);
680 goto rx_starttimer;
681 }
682
683 if (op->nframes > 1) {
684
685
686
687
688
689
690
691
692 for (i = 1; i < op->nframes; i++) {
693 if ((get_u64(op->frames, 0) & get_u64(rxframe, 0)) ==
694 (get_u64(op->frames, 0) &
695 get_u64(op->frames + op->cfsiz * i, 0))) {
696 bcm_rx_cmp_to_index(op, i, rxframe);
697 break;
698 }
699 }
700 }
701
702rx_starttimer:
703 bcm_rx_starttimer(op);
704}
705
706
707
708
709static struct bcm_op *bcm_find_op(struct list_head *ops,
710 struct bcm_msg_head *mh, int ifindex)
711{
712 struct bcm_op *op;
713
714 list_for_each_entry(op, ops, list) {
715 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
716 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME))
717 return op;
718 }
719
720 return NULL;
721}
722
723static void bcm_remove_op(struct bcm_op *op)
724{
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 }
733
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 }
742
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);
750}
751
752static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
753{
754 if (op->rx_reg_dev == dev) {
755 can_rx_unregister(dev_net(dev), dev, op->can_id,
756 REGMASK(op->can_id), bcm_rx_handler, op);
757
758
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
767
768static int bcm_delete_rx_op(struct list_head *ops, struct bcm_msg_head *mh,
769 int ifindex)
770{
771 struct bcm_op *op, *n;
772
773 list_for_each_entry_safe(op, n, ops, list) {
774 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
775 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
776
777
778
779
780
781
782 if (op->ifindex) {
783
784
785
786
787
788 if (op->rx_reg_dev) {
789 struct net_device *dev;
790
791 dev = dev_get_by_index(sock_net(op->sk),
792 op->ifindex);
793 if (dev) {
794 bcm_rx_unreg(dev, op);
795 dev_put(dev);
796 }
797 }
798 } else
799 can_rx_unregister(sock_net(op->sk), NULL,
800 op->can_id,
801 REGMASK(op->can_id),
802 bcm_rx_handler, op);
803
804 list_del(&op->list);
805 bcm_remove_op(op);
806 return 1;
807 }
808 }
809
810 return 0;
811}
812
813
814
815
816static int bcm_delete_tx_op(struct list_head *ops, struct bcm_msg_head *mh,
817 int ifindex)
818{
819 struct bcm_op *op, *n;
820
821 list_for_each_entry_safe(op, n, ops, list) {
822 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) &&
823 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) {
824 list_del(&op->list);
825 bcm_remove_op(op);
826 return 1;
827 }
828 }
829
830 return 0;
831}
832
833
834
835
836static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
837 int ifindex)
838{
839 struct bcm_op *op = bcm_find_op(ops, msg_head, ifindex);
840
841 if (!op)
842 return -EINVAL;
843
844
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
858
859static 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;
864 struct canfd_frame *cf;
865 unsigned int i;
866 int err;
867
868
869 if (!ifindex)
870 return -ENODEV;
871
872
873 if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES)
874 return -EINVAL;
875
876
877 op = bcm_find_op(&bo->tx_ops, msg_head, ifindex);
878 if (op) {
879
880
881
882
883
884
885
886 if (msg_head->nframes > op->nframes)
887 return -E2BIG;
888
889
890 for (i = 0; i < msg_head->nframes; i++) {
891
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 }
902
903 if (err < 0)
904 return err;
905
906 if (msg_head->flags & TX_CP_CAN_ID) {
907
908 cf->can_id = msg_head->can_id;
909 }
910 }
911 op->flags = msg_head->flags;
912
913 } else {
914
915
916 op = kzalloc(OPSIZ, GFP_KERNEL);
917 if (!op)
918 return -ENOMEM;
919
920 op->can_id = msg_head->can_id;
921 op->cfsiz = CFSIZ(msg_head->flags);
922 op->flags = msg_head->flags;
923
924
925 if (msg_head->nframes > 1) {
926 op->frames = kmalloc_array(msg_head->nframes,
927 op->cfsiz,
928 GFP_KERNEL);
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++) {
937
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 }
948
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
958 cf->can_id = msg_head->can_id;
959 }
960 }
961
962
963 op->last_frames = NULL;
964
965
966 op->sk = sk;
967 op->ifindex = ifindex;
968
969
970 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
971 op->timer.function = bcm_tx_timeout_handler;
972
973
974 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet,
975 (unsigned long) op);
976
977
978 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
979
980
981 list_add(&op->list, &bo->tx_ops);
982
983 }
984
985 if (op->nframes != msg_head->nframes) {
986 op->nframes = msg_head->nframes;
987
988 op->currframe = 0;
989 }
990
991
992
993 if (op->flags & TX_RESET_MULTI_IDX) {
994
995 op->currframe = 0;
996 }
997
998 if (op->flags & SETTIMER) {
999
1000 op->count = msg_head->count;
1001 op->ival1 = msg_head->ival1;
1002 op->ival2 = msg_head->ival2;
1003 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
1004 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
1005
1006
1007 if (!op->kt_ival1 && !op->kt_ival2)
1008 hrtimer_cancel(&op->timer);
1009 }
1010
1011 if (op->flags & STARTTIMER) {
1012 hrtimer_cancel(&op->timer);
1013
1014 op->flags |= TX_ANNOUNCE;
1015 }
1016
1017 if (op->flags & TX_ANNOUNCE) {
1018 bcm_can_tx(op);
1019 if (op->count)
1020 op->count--;
1021 }
1022
1023 if (op->flags & STARTTIMER)
1024 bcm_tx_start_timer(op);
1025
1026 return msg_head->nframes * op->cfsiz + MHSIZ;
1027}
1028
1029
1030
1031
1032static 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
1042 msg_head->flags |= RX_FILTER_ID;
1043
1044 msg_head->nframes = 0;
1045 }
1046
1047
1048 if (msg_head->nframes > MAX_NFRAMES + 1)
1049 return -EINVAL;
1050
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
1057 op = bcm_find_op(&bo->rx_ops, msg_head, ifindex);
1058 if (op) {
1059
1060
1061
1062
1063
1064
1065
1066 if (msg_head->nframes > op->nframes)
1067 return -E2BIG;
1068
1069 if (msg_head->nframes) {
1070
1071 err = memcpy_from_msg(op->frames, msg,
1072 msg_head->nframes * op->cfsiz);
1073 if (err < 0)
1074 return err;
1075
1076
1077 memset(op->last_frames, 0, msg_head->nframes * op->cfsiz);
1078 }
1079
1080 op->nframes = msg_head->nframes;
1081 op->flags = msg_head->flags;
1082
1083
1084 do_rx_register = 0;
1085
1086 } else {
1087
1088 op = kzalloc(OPSIZ, GFP_KERNEL);
1089 if (!op)
1090 return -ENOMEM;
1091
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;
1096
1097 if (msg_head->nframes > 1) {
1098
1099 op->frames = kmalloc_array(msg_head->nframes,
1100 op->cfsiz,
1101 GFP_KERNEL);
1102 if (!op->frames) {
1103 kfree(op);
1104 return -ENOMEM;
1105 }
1106
1107
1108 op->last_frames = kcalloc(msg_head->nframes,
1109 op->cfsiz,
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) {
1123 err = memcpy_from_msg(op->frames, msg,
1124 msg_head->nframes * op->cfsiz);
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
1136 op->sk = sk;
1137 op->ifindex = ifindex;
1138
1139
1140 op->rx_ifindex = ifindex;
1141
1142
1143 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1144 op->timer.function = bcm_rx_timeout_handler;
1145
1146
1147 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet,
1148 (unsigned long) op);
1149
1150 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1151 op->thrtimer.function = bcm_rx_thr_handler;
1152
1153
1154 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet,
1155 (unsigned long) op);
1156
1157
1158 list_add(&op->list, &bo->rx_ops);
1159
1160
1161 do_rx_register = 1;
1162
1163 }
1164
1165
1166
1167 if (op->flags & RX_RTR_FRAME) {
1168 struct canfd_frame *frame0 = op->frames;
1169
1170
1171 hrtimer_cancel(&op->thrtimer);
1172 hrtimer_cancel(&op->timer);
1173
1174
1175
1176
1177
1178
1179 if ((op->flags & TX_CP_CAN_ID) ||
1180 (frame0->can_id == op->can_id))
1181 frame0->can_id = op->can_id & ~CAN_RTR_FLAG;
1182
1183 } else {
1184 if (op->flags & SETTIMER) {
1185
1186
1187 op->ival1 = msg_head->ival1;
1188 op->ival2 = msg_head->ival2;
1189 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1);
1190 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2);
1191
1192
1193 if (!op->kt_ival1)
1194 hrtimer_cancel(&op->timer);
1195
1196
1197
1198
1199
1200 op->kt_lastmsg = 0;
1201 hrtimer_cancel(&op->thrtimer);
1202 bcm_rx_thr_flush(op, 1);
1203 }
1204
1205 if ((op->flags & STARTTIMER) && op->kt_ival1)
1206 hrtimer_start(&op->timer, op->kt_ival1,
1207 HRTIMER_MODE_REL);
1208 }
1209
1210
1211 if (do_rx_register) {
1212 if (ifindex) {
1213 struct net_device *dev;
1214
1215 dev = dev_get_by_index(sock_net(sk), ifindex);
1216 if (dev) {
1217 err = can_rx_register(sock_net(sk), dev,
1218 op->can_id,
1219 REGMASK(op->can_id),
1220 bcm_rx_handler, op,
1221 "bcm", sk);
1222
1223 op->rx_reg_dev = dev;
1224 dev_put(dev);
1225 }
1226
1227 } else
1228 err = can_rx_register(sock_net(sk), NULL, op->can_id,
1229 REGMASK(op->can_id),
1230 bcm_rx_handler, op, "bcm", sk);
1231 if (err) {
1232
1233 list_del(&op->list);
1234 bcm_remove_op(op);
1235 return err;
1236 }
1237 }
1238
1239 return msg_head->nframes * op->cfsiz + MHSIZ;
1240}
1241
1242
1243
1244
1245static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk,
1246 int cfsiz)
1247{
1248 struct sk_buff *skb;
1249 struct net_device *dev;
1250 int err;
1251
1252
1253 if (!ifindex)
1254 return -ENODEV;
1255
1256 skb = alloc_skb(cfsiz + sizeof(struct can_skb_priv), GFP_KERNEL);
1257 if (!skb)
1258 return -ENOMEM;
1259
1260 can_skb_reserve(skb);
1261
1262 err = memcpy_from_msg(skb_put(skb, cfsiz), msg, cfsiz);
1263 if (err < 0) {
1264 kfree_skb(skb);
1265 return err;
1266 }
1267
1268 dev = dev_get_by_index(sock_net(sk), ifindex);
1269 if (!dev) {
1270 kfree_skb(skb);
1271 return -ENODEV;
1272 }
1273
1274 can_skb_prv(skb)->ifindex = dev->ifindex;
1275 can_skb_prv(skb)->skbcnt = 0;
1276 skb->dev = dev;
1277 can_skb_set_owner(skb, sk);
1278 err = can_send(skb, 1);
1279 dev_put(dev);
1280
1281 if (err)
1282 return err;
1283
1284 return cfsiz + MHSIZ;
1285}
1286
1287
1288
1289
1290static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
1291{
1292 struct sock *sk = sock->sk;
1293 struct bcm_sock *bo = bcm_sk(sk);
1294 int ifindex = bo->ifindex;
1295 struct bcm_msg_head msg_head;
1296 int cfsiz;
1297 int ret;
1298
1299 if (!bo->bound)
1300 return -ENOTCONN;
1301
1302
1303 if (size < MHSIZ)
1304 return -EINVAL;
1305
1306
1307 ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ);
1308 if (ret < 0)
1309 return ret;
1310
1311 cfsiz = CFSIZ(msg_head.flags);
1312 if ((size - MHSIZ) % cfsiz)
1313 return -EINVAL;
1314
1315
1316
1317 if (!ifindex && msg->msg_name) {
1318
1319 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name);
1320
1321 if (msg->msg_namelen < sizeof(*addr))
1322 return -EINVAL;
1323
1324 if (addr->can_family != AF_CAN)
1325 return -EINVAL;
1326
1327
1328 ifindex = addr->can_ifindex;
1329
1330 if (ifindex) {
1331 struct net_device *dev;
1332
1333 dev = dev_get_by_index(sock_net(sk), ifindex);
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
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:
1359 if (bcm_delete_tx_op(&bo->tx_ops, &msg_head, ifindex))
1360 ret = MHSIZ;
1361 else
1362 ret = -EINVAL;
1363 break;
1364
1365 case RX_DELETE:
1366 if (bcm_delete_rx_op(&bo->rx_ops, &msg_head, ifindex))
1367 ret = MHSIZ;
1368 else
1369 ret = -EINVAL;
1370 break;
1371
1372 case TX_READ:
1373
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
1380 msg_head.opcode = RX_STATUS;
1381 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex);
1382 break;
1383
1384 case TX_SEND:
1385
1386 if ((msg_head.nframes != 1) || (size != cfsiz + MHSIZ))
1387 ret = -EINVAL;
1388 else
1389 ret = bcm_tx_send(msg, ifindex, sk, cfsiz);
1390 break;
1391
1392 default:
1393 ret = -EINVAL;
1394 break;
1395 }
1396
1397 release_sock(sk);
1398
1399 return ret;
1400}
1401
1402
1403
1404
1405static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
1406 void *ptr)
1407{
1408 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1409 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier);
1410 struct sock *sk = &bo->sk;
1411 struct bcm_op *op;
1412 int notify_enodev = 0;
1413
1414 if (!net_eq(dev_net(dev), sock_net(sk)))
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
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
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
1459
1460static 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
1473 bo->notifier.notifier_call = bcm_notifier;
1474
1475 register_netdevice_notifier(&bo->notifier);
1476
1477 return 0;
1478}
1479
1480
1481
1482
1483static int bcm_release(struct socket *sock)
1484{
1485 struct sock *sk = sock->sk;
1486 struct net *net;
1487 struct bcm_sock *bo;
1488 struct bcm_op *op, *next;
1489
1490 if (!sk)
1491 return 0;
1492
1493 net = sock_net(sk);
1494 bo = bcm_sk(sk);
1495
1496
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
1508
1509
1510 if (op->ifindex) {
1511
1512
1513
1514
1515
1516 if (op->rx_reg_dev) {
1517 struct net_device *dev;
1518
1519 dev = dev_get_by_index(net, op->ifindex);
1520 if (dev) {
1521 bcm_rx_unreg(dev, op);
1522 dev_put(dev);
1523 }
1524 }
1525 } else
1526 can_rx_unregister(net, NULL, op->can_id,
1527 REGMASK(op->can_id),
1528 bcm_rx_handler, op);
1529
1530 bcm_remove_op(op);
1531 }
1532
1533#if IS_ENABLED(CONFIG_PROC_FS)
1534
1535 if (net->can.bcmproc_dir && bo->bcm_proc_read)
1536 remove_proc_entry(bo->procname, net->can.bcmproc_dir);
1537#endif
1538
1539
1540 if (bo->bound) {
1541 bo->bound = 0;
1542 bo->ifindex = 0;
1543 }
1544
1545 sock_orphan(sk);
1546 sock->sk = NULL;
1547
1548 release_sock(sk);
1549 sock_put(sk);
1550
1551 return 0;
1552}
1553
1554static 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);
1560 struct net *net = sock_net(sk);
1561 int ret = 0;
1562
1563 if (len < sizeof(*addr))
1564 return -EINVAL;
1565
1566 lock_sock(sk);
1567
1568 if (bo->bound) {
1569 ret = -EISCONN;
1570 goto fail;
1571 }
1572
1573
1574 if (addr->can_ifindex) {
1575 struct net_device *dev;
1576
1577 dev = dev_get_by_index(net, addr->can_ifindex);
1578 if (!dev) {
1579 ret = -ENODEV;
1580 goto fail;
1581 }
1582 if (dev->type != ARPHRD_CAN) {
1583 dev_put(dev);
1584 ret = -ENODEV;
1585 goto fail;
1586 }
1587
1588 bo->ifindex = dev->ifindex;
1589 dev_put(dev);
1590
1591 } else {
1592
1593 bo->ifindex = 0;
1594 }
1595
1596#if IS_ENABLED(CONFIG_PROC_FS)
1597 if (net->can.bcmproc_dir) {
1598
1599 sprintf(bo->procname, "%lu", sock_i_ino(sk));
1600 bo->bcm_proc_read = proc_create_net_single(bo->procname, 0644,
1601 net->can.bcmproc_dir,
1602 bcm_proc_show, sk);
1603 if (!bo->bcm_proc_read) {
1604 ret = -ENOMEM;
1605 goto fail;
1606 }
1607 }
1608#endif
1609
1610 bo->bound = 1;
1611
1612fail:
1613 release_sock(sk);
1614
1615 return ret;
1616}
1617
1618static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1619 int flags)
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
1636 err = memcpy_to_msg(msg, skb->data, size);
1637 if (err < 0) {
1638 skb_free_datagram(sk, skb);
1639 return err;
1640 }
1641
1642 sock_recv_ts_and_drops(msg, sk, skb);
1643
1644 if (msg->msg_name) {
1645 __sockaddr_check_size(sizeof(struct sockaddr_can));
1646 msg->msg_namelen = sizeof(struct sockaddr_can);
1647 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1648 }
1649
1650 skb_free_datagram(sk, skb);
1651
1652 return size;
1653}
1654
1655static const struct proto_ops bcm_ops = {
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,
1663 .poll = datagram_poll,
1664 .ioctl = can_ioctl,
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
1675static 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
1682static const struct can_proto bcm_can_proto = {
1683 .type = SOCK_DGRAM,
1684 .protocol = CAN_BCM,
1685 .ops = &bcm_ops,
1686 .prot = &bcm_proto,
1687};
1688
1689static int canbcm_pernet_init(struct net *net)
1690{
1691#if IS_ENABLED(CONFIG_PROC_FS)
1692
1693 net->can.bcmproc_dir = proc_net_mkdir(net, "can-bcm", net->proc_net);
1694#endif
1695
1696 return 0;
1697}
1698
1699static void canbcm_pernet_exit(struct net *net)
1700{
1701#if IS_ENABLED(CONFIG_PROC_FS)
1702
1703 if (net->can.bcmproc_dir)
1704 remove_proc_entry("can-bcm", net->proc_net);
1705#endif
1706}
1707
1708static struct pernet_operations canbcm_pernet_ops __read_mostly = {
1709 .init = canbcm_pernet_init,
1710 .exit = canbcm_pernet_exit,
1711};
1712
1713static int __init bcm_module_init(void)
1714{
1715 int err;
1716
1717 pr_info("can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n");
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
1725 register_pernet_subsys(&canbcm_pernet_ops);
1726 return 0;
1727}
1728
1729static void __exit bcm_module_exit(void)
1730{
1731 can_proto_unregister(&bcm_can_proto);
1732 unregister_pernet_subsys(&canbcm_pernet_ops);
1733}
1734
1735module_init(bcm_module_init);
1736module_exit(bcm_module_exit);
1737