1 /* 2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content 3 * 4 * Copyright (c) 2002-2017 Volkswagen Group Electronic Research 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 * 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 * 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 70 /* use of last_frames[index].flags */ 71 #define RX_RECV 0x40 /* received data for this element */ 72 #define RX_THR 0x80 /* element not been sent due to throttle feature */ 73 #define BCM_CAN_FLAGS_MASK 0x3F /* to clean private flags after usage */ 74 75 /* get best masking value for can_rx_register() for a given single can_id */ 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 82 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol"); 83 MODULE_LICENSE("Dual BSD/GPL"); 84 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>"); 85 MODULE_ALIAS("can-proto-2"); 86 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) 93 { 94 return *(u64 *)(cp->data + offset); 95 } 96 97 struct 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 /* void pointers to arrays of struct can[fd]_frame */ 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 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; 130 char procname [32]; /* inode number in decimal with \0 */ 131 }; 132 133 static inline struct bcm_sock *bcm_sk(const struct sock *sk) 134 { 135 return (struct bcm_sock *)sk; 136 } 137 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 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 * procfs functions 149 */ 150 #if IS_ENABLED(CONFIG_PROC_FS) 151 static 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 169 static 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 /* print only active entries & prevent division by zero */ 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 /* CONFIG_PROC_FS */ 243 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; 252 struct canfd_frame *cf = op->frames + op->cfsiz * op->currframe; 253 254 /* no target device? => exit */ 255 if (!op->ifindex) 256 return; 257 258 dev = dev_get_by_index(sock_net(op->sk), op->ifindex); 259 if (!dev) { 260 /* RFC: should this bcm_op remove itself here? */ 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 /* send with loopback */ 275 skb->dev = dev; 276 can_skb_set_owner(skb, op->sk); 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; 286 out: 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, 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 /* CAN frames starting here */ 312 firstframe = (struct canfd_frame *)skb_tail_pointer(skb); 313 314 skb_put_data(skb, frames, datalen); 315 316 /* 317 * the BCM uses the flags-element of the canfd_frame 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) 323 firstframe->flags &= BCM_CAN_FLAGS_MASK; 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 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 /* don't care about overflows in this statistic */ 350 bo->dropped_usr_msgs++; 351 } 352 } 353 354 static 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 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 371 if (op->kt_ival1 && (op->count > 0)) { 372 373 op->count--; 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 } 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 * bcm_tx_timeout_handler - performs cyclic CAN frame transmissions 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); 403 404 return HRTIMER_NORESTART; 405 } 406 407 /* 408 * bcm_rx_changed - create a RX_CHANGED notification due to changed content 409 */ 410 static void bcm_rx_changed(struct bcm_op *op, struct canfd_frame *data) 411 { 412 struct bcm_msg_head head; 413 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 421 /* this element is not throttled anymore */ 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 * 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, 441 struct canfd_frame *lastdata, 442 const struct canfd_frame *rxdata) 443 { 444 memcpy(lastdata, rxdata, op->cfsiz); 445 446 /* mark as used and throttled by default */ 447 lastdata->flags |= (RX_RECV|RX_THR); 448 449 /* throttling mode inactive ? */ 450 if (!op->kt_ival2) { 451 /* send RX_CHANGED to the user immediately */ 452 bcm_rx_changed(op, lastdata); 453 return; 454 } 455 456 /* with active throttling timer we are just done here */ 457 if (hrtimer_active(&op->thrtimer)) 458 return; 459 460 /* first reception with enabled throttling mode */ 461 if (!op->kt_lastmsg) 462 goto rx_changed_settime; 463 464 /* got a second frame inside a potential throttle period? */ 465 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) < 466 ktime_to_us(op->kt_ival2)) { 467 /* do not send the saved data - only start throttle timer */ 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 */ 475 rx_changed_settime: 476 bcm_rx_changed(op, lastdata); 477 op->kt_lastmsg = ktime_get(); 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 */ 484 static 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 * no one uses the MSBs of flags for comparison, 493 * so we use it here to detect the first time of reception 494 */ 495 496 if (!(lcf->flags & RX_RECV)) { 497 /* received data for the first time => send update to user */ 498 bcm_rx_update_and_send(op, lcf, rxdata); 499 return; 500 } 501 502 /* do a real check in CAN frame data section */ 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 /* do a real check in CAN frame length */ 513 if (rxdata->len != lcf->len) { 514 bcm_rx_update_and_send(op, lcf, rxdata); 515 return; 516 } 517 } 518 } 519 520 /* 521 * bcm_rx_starttimer - enable timeout monitoring for CAN frame reception 522 */ 523 static 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 532 static 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 /* create notification to user */ 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 * bcm_rx_timeout_handler - when the (cyclic) CAN frame reception timed out 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); 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) { 563 /* clear received CAN frames to indicate 'nothing received' */ 564 memset(op->last_frames, 0, op->nframes * op->cfsiz); 565 } 566 567 return HRTIMER_NORESTART; 568 } 569 570 /* 571 * bcm_rx_do_flush - helper for bcm_rx_thr_flush 572 */ 573 static 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 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace 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) 591 */ 592 static 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 /* for MUX filter we start at index 1 */ 600 for (i = 1; i < op->nframes; i++) 601 updated += bcm_rx_do_flush(op, update, i); 602 603 } else { 604 /* for RX_FILTER_ID and simple filter */ 605 updated += bcm_rx_do_flush(op, update, 0); 606 } 607 608 return updated; 609 } 610 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 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 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 /* rearm throttle handling */ 634 op->kt_lastmsg = 0; 635 return HRTIMER_NORESTART; 636 } 637 } 638 639 /* 640 * bcm_rx_handler - handle a CAN frame reception 641 */ 642 static 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 /* 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 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++; 664 665 if (op->flags & RX_RTR_FRAME) { 666 /* send reply for RTR-request (placed in op->frames[0]) */ 667 bcm_can_tx(op); 668 return; 669 } 670 671 if (op->flags & RX_FILTER_ID) { 672 /* the easiest case */ 673 bcm_rx_update_and_send(op, op->last_frames, rxframe); 674 goto rx_starttimer; 675 } 676 677 if (op->nframes == 1) { 678 /* simple compare with index 0 */ 679 bcm_rx_cmp_to_index(op, 0, rxframe); 680 goto rx_starttimer; 681 } 682 683 if (op->nframes > 1) { 684 /* 685 * multiplex compare 686 * 687 * find the first multiplex mask that fits. 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) 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 702 rx_starttimer: 703 bcm_rx_starttimer(op); 704 } 705 706 /* 707 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements 708 */ 709 static 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 723 static 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 752 static 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 /* 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 */ 768 static 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 * 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 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; /* 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 */ 816 static 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; /* 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 { 839 struct bcm_op *op = bcm_find_op(ops, msg_head, ifindex); 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; 864 struct canfd_frame *cf; 865 unsigned int i; 866 int err; 867 868 /* we need a real device to send frames */ 869 if (!ifindex) 870 return -ENODEV; 871 872 /* check nframes boundaries - we need at least one CAN frame */ 873 if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES) 874 return -EINVAL; 875 876 /* check the given can_id */ 877 op = bcm_find_op(&bo->tx_ops, msg_head, ifindex); 878 if (op) { 879 /* update existing BCM operation */ 880 881 /* 882 * Do we need more space for the CAN frames than currently 883 * allocated? -> This is a _really_ unusual use-case and 884 * therefore (complexity / locking) it is not supported. 885 */ 886 if (msg_head->nframes > op->nframes) 887 return -E2BIG; 888 889 /* update CAN frames content */ 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 /* copy can_id into frame */ 908 cf->can_id = msg_head->can_id; 909 } 910 } 911 op->flags = msg_head->flags; 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 920 op->can_id = msg_head->can_id; 921 op->cfsiz = CFSIZ(msg_head->flags); 922 op->flags = msg_head->flags; 923 924 /* create array for CAN frames and copy the data */ 925 if (msg_head->nframes > 1) { 926 op->frames = kmalloc(msg_head->nframes * op->cfsiz, 927 GFP_KERNEL); 928 if (!op->frames) { 929 kfree(op); 930 return -ENOMEM; 931 } 932 } else 933 op->frames = &op->sframe; 934 935 for (i = 0; i < msg_head->nframes; i++) { 936 937 cf = op->frames + op->cfsiz * i; 938 err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz); 939 940 if (op->flags & CAN_FD_FRAME) { 941 if (cf->len > 64) 942 err = -EINVAL; 943 } else { 944 if (cf->len > 8) 945 err = -EINVAL; 946 } 947 948 if (err < 0) { 949 if (op->frames != &op->sframe) 950 kfree(op->frames); 951 kfree(op); 952 return err; 953 } 954 955 if (msg_head->flags & TX_CP_CAN_ID) { 956 /* copy can_id into frame */ 957 cf->can_id = msg_head->can_id; 958 } 959 } 960 961 /* tx_ops never compare with previous received messages */ 962 op->last_frames = NULL; 963 964 /* bcm_can_tx / bcm_tx_timeout_handler needs this */ 965 op->sk = sk; 966 op->ifindex = ifindex; 967 968 /* initialize uninitialized (kzalloc) structure */ 969 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 970 op->timer.function = bcm_tx_timeout_handler; 971 972 /* initialize tasklet for tx countevent notification */ 973 tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet, 974 (unsigned long) op); 975 976 /* currently unused in tx_ops */ 977 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 978 979 /* add this bcm_op to the list of the tx_ops */ 980 list_add(&op->list, &bo->tx_ops); 981 982 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */ 983 984 if (op->nframes != msg_head->nframes) { 985 op->nframes = msg_head->nframes; 986 /* start multiple frame transmission with index 0 */ 987 op->currframe = 0; 988 } 989 990 /* check flags */ 991 992 if (op->flags & TX_RESET_MULTI_IDX) { 993 /* start multiple frame transmission with index 0 */ 994 op->currframe = 0; 995 } 996 997 if (op->flags & SETTIMER) { 998 /* set timer values */ 999 op->count = msg_head->count; 1000 op->ival1 = msg_head->ival1; 1001 op->ival2 = msg_head->ival2; 1002 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1); 1003 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2); 1004 1005 /* disable an active timer due to zero values? */ 1006 if (!op->kt_ival1 && !op->kt_ival2) 1007 hrtimer_cancel(&op->timer); 1008 } 1009 1010 if (op->flags & STARTTIMER) { 1011 hrtimer_cancel(&op->timer); 1012 /* spec: send CAN frame when starting timer */ 1013 op->flags |= TX_ANNOUNCE; 1014 } 1015 1016 if (op->flags & TX_ANNOUNCE) { 1017 bcm_can_tx(op); 1018 if (op->count) 1019 op->count--; 1020 } 1021 1022 if (op->flags & STARTTIMER) 1023 bcm_tx_start_timer(op); 1024 1025 return msg_head->nframes * op->cfsiz + MHSIZ; 1026 } 1027 1028 /* 1029 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg) 1030 */ 1031 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg, 1032 int ifindex, struct sock *sk) 1033 { 1034 struct bcm_sock *bo = bcm_sk(sk); 1035 struct bcm_op *op; 1036 int do_rx_register; 1037 int err = 0; 1038 1039 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) { 1040 /* be robust against wrong usage ... */ 1041 msg_head->flags |= RX_FILTER_ID; 1042 /* ignore trailing garbage */ 1043 msg_head->nframes = 0; 1044 } 1045 1046 /* the first element contains the mux-mask => MAX_NFRAMES + 1 */ 1047 if (msg_head->nframes > MAX_NFRAMES + 1) 1048 return -EINVAL; 1049 1050 if ((msg_head->flags & RX_RTR_FRAME) && 1051 ((msg_head->nframes != 1) || 1052 (!(msg_head->can_id & CAN_RTR_FLAG)))) 1053 return -EINVAL; 1054 1055 /* check the given can_id */ 1056 op = bcm_find_op(&bo->rx_ops, msg_head, ifindex); 1057 if (op) { 1058 /* update existing BCM operation */ 1059 1060 /* 1061 * Do we need more space for the CAN frames than currently 1062 * allocated? -> This is a _really_ unusual use-case and 1063 * therefore (complexity / locking) it is not supported. 1064 */ 1065 if (msg_head->nframes > op->nframes) 1066 return -E2BIG; 1067 1068 if (msg_head->nframes) { 1069 /* update CAN frames content */ 1070 err = memcpy_from_msg(op->frames, msg, 1071 msg_head->nframes * op->cfsiz); 1072 if (err < 0) 1073 return err; 1074 1075 /* clear last_frames to indicate 'nothing received' */ 1076 memset(op->last_frames, 0, msg_head->nframes * op->cfsiz); 1077 } 1078 1079 op->nframes = msg_head->nframes; 1080 op->flags = msg_head->flags; 1081 1082 /* Only an update -> do not call can_rx_register() */ 1083 do_rx_register = 0; 1084 1085 } else { 1086 /* insert new BCM operation for the given can_id */ 1087 op = kzalloc(OPSIZ, GFP_KERNEL); 1088 if (!op) 1089 return -ENOMEM; 1090 1091 op->can_id = msg_head->can_id; 1092 op->nframes = msg_head->nframes; 1093 op->cfsiz = CFSIZ(msg_head->flags); 1094 op->flags = msg_head->flags; 1095 1096 if (msg_head->nframes > 1) { 1097 /* create array for CAN frames and copy the data */ 1098 op->frames = kmalloc(msg_head->nframes * op->cfsiz, 1099 GFP_KERNEL); 1100 if (!op->frames) { 1101 kfree(op); 1102 return -ENOMEM; 1103 } 1104 1105 /* create and init array for received CAN frames */ 1106 op->last_frames = kzalloc(msg_head->nframes * op->cfsiz, 1107 GFP_KERNEL); 1108 if (!op->last_frames) { 1109 kfree(op->frames); 1110 kfree(op); 1111 return -ENOMEM; 1112 } 1113 1114 } else { 1115 op->frames = &op->sframe; 1116 op->last_frames = &op->last_sframe; 1117 } 1118 1119 if (msg_head->nframes) { 1120 err = memcpy_from_msg(op->frames, msg, 1121 msg_head->nframes * op->cfsiz); 1122 if (err < 0) { 1123 if (op->frames != &op->sframe) 1124 kfree(op->frames); 1125 if (op->last_frames != &op->last_sframe) 1126 kfree(op->last_frames); 1127 kfree(op); 1128 return err; 1129 } 1130 } 1131 1132 /* bcm_can_tx / bcm_tx_timeout_handler needs this */ 1133 op->sk = sk; 1134 op->ifindex = ifindex; 1135 1136 /* ifindex for timeout events w/o previous frame reception */ 1137 op->rx_ifindex = ifindex; 1138 1139 /* initialize uninitialized (kzalloc) structure */ 1140 hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1141 op->timer.function = bcm_rx_timeout_handler; 1142 1143 /* initialize tasklet for rx timeout notification */ 1144 tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet, 1145 (unsigned long) op); 1146 1147 hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1148 op->thrtimer.function = bcm_rx_thr_handler; 1149 1150 /* initialize tasklet for rx throttle handling */ 1151 tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet, 1152 (unsigned long) op); 1153 1154 /* add this bcm_op to the list of the rx_ops */ 1155 list_add(&op->list, &bo->rx_ops); 1156 1157 /* call can_rx_register() */ 1158 do_rx_register = 1; 1159 1160 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */ 1161 1162 /* check flags */ 1163 1164 if (op->flags & RX_RTR_FRAME) { 1165 struct canfd_frame *frame0 = op->frames; 1166 1167 /* no timers in RTR-mode */ 1168 hrtimer_cancel(&op->thrtimer); 1169 hrtimer_cancel(&op->timer); 1170 1171 /* 1172 * funny feature in RX(!)_SETUP only for RTR-mode: 1173 * copy can_id into frame BUT without RTR-flag to 1174 * prevent a full-load-loopback-test ... ;-] 1175 */ 1176 if ((op->flags & TX_CP_CAN_ID) || 1177 (frame0->can_id == op->can_id)) 1178 frame0->can_id = op->can_id & ~CAN_RTR_FLAG; 1179 1180 } else { 1181 if (op->flags & SETTIMER) { 1182 1183 /* set timer value */ 1184 op->ival1 = msg_head->ival1; 1185 op->ival2 = msg_head->ival2; 1186 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1); 1187 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2); 1188 1189 /* disable an active timer due to zero value? */ 1190 if (!op->kt_ival1) 1191 hrtimer_cancel(&op->timer); 1192 1193 /* 1194 * In any case cancel the throttle timer, flush 1195 * potentially blocked msgs and reset throttle handling 1196 */ 1197 op->kt_lastmsg = 0; 1198 hrtimer_cancel(&op->thrtimer); 1199 bcm_rx_thr_flush(op, 1); 1200 } 1201 1202 if ((op->flags & STARTTIMER) && op->kt_ival1) 1203 hrtimer_start(&op->timer, op->kt_ival1, 1204 HRTIMER_MODE_REL); 1205 } 1206 1207 /* now we can register for can_ids, if we added a new bcm_op */ 1208 if (do_rx_register) { 1209 if (ifindex) { 1210 struct net_device *dev; 1211 1212 dev = dev_get_by_index(sock_net(sk), ifindex); 1213 if (dev) { 1214 err = can_rx_register(sock_net(sk), dev, 1215 op->can_id, 1216 REGMASK(op->can_id), 1217 bcm_rx_handler, op, 1218 "bcm", sk); 1219 1220 op->rx_reg_dev = dev; 1221 dev_put(dev); 1222 } 1223 1224 } else 1225 err = can_rx_register(sock_net(sk), NULL, op->can_id, 1226 REGMASK(op->can_id), 1227 bcm_rx_handler, op, "bcm", sk); 1228 if (err) { 1229 /* this bcm rx op is broken -> remove it */ 1230 list_del(&op->list); 1231 bcm_remove_op(op); 1232 return err; 1233 } 1234 } 1235 1236 return msg_head->nframes * op->cfsiz + MHSIZ; 1237 } 1238 1239 /* 1240 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg) 1241 */ 1242 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk, 1243 int cfsiz) 1244 { 1245 struct sk_buff *skb; 1246 struct net_device *dev; 1247 int err; 1248 1249 /* we need a real device to send frames */ 1250 if (!ifindex) 1251 return -ENODEV; 1252 1253 skb = alloc_skb(cfsiz + sizeof(struct can_skb_priv), GFP_KERNEL); 1254 if (!skb) 1255 return -ENOMEM; 1256 1257 can_skb_reserve(skb); 1258 1259 err = memcpy_from_msg(skb_put(skb, cfsiz), msg, cfsiz); 1260 if (err < 0) { 1261 kfree_skb(skb); 1262 return err; 1263 } 1264 1265 dev = dev_get_by_index(sock_net(sk), ifindex); 1266 if (!dev) { 1267 kfree_skb(skb); 1268 return -ENODEV; 1269 } 1270 1271 can_skb_prv(skb)->ifindex = dev->ifindex; 1272 can_skb_prv(skb)->skbcnt = 0; 1273 skb->dev = dev; 1274 can_skb_set_owner(skb, sk); 1275 err = can_send(skb, 1); /* send with loopback */ 1276 dev_put(dev); 1277 1278 if (err) 1279 return err; 1280 1281 return cfsiz + MHSIZ; 1282 } 1283 1284 /* 1285 * bcm_sendmsg - process BCM commands (opcodes) from the userspace 1286 */ 1287 static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) 1288 { 1289 struct sock *sk = sock->sk; 1290 struct bcm_sock *bo = bcm_sk(sk); 1291 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */ 1292 struct bcm_msg_head msg_head; 1293 int cfsiz; 1294 int ret; /* read bytes or error codes as return value */ 1295 1296 if (!bo->bound) 1297 return -ENOTCONN; 1298 1299 /* check for valid message length from userspace */ 1300 if (size < MHSIZ) 1301 return -EINVAL; 1302 1303 /* read message head information */ 1304 ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ); 1305 if (ret < 0) 1306 return ret; 1307 1308 cfsiz = CFSIZ(msg_head.flags); 1309 if ((size - MHSIZ) % cfsiz) 1310 return -EINVAL; 1311 1312 /* check for alternative ifindex for this bcm_op */ 1313 1314 if (!ifindex && msg->msg_name) { 1315 /* no bound device as default => check msg_name */ 1316 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name); 1317 1318 if (msg->msg_namelen < sizeof(*addr)) 1319 return -EINVAL; 1320 1321 if (addr->can_family != AF_CAN) 1322 return -EINVAL; 1323 1324 /* ifindex from sendto() */ 1325 ifindex = addr->can_ifindex; 1326 1327 if (ifindex) { 1328 struct net_device *dev; 1329 1330 dev = dev_get_by_index(sock_net(sk), ifindex); 1331 if (!dev) 1332 return -ENODEV; 1333 1334 if (dev->type != ARPHRD_CAN) { 1335 dev_put(dev); 1336 return -ENODEV; 1337 } 1338 1339 dev_put(dev); 1340 } 1341 } 1342 1343 lock_sock(sk); 1344 1345 switch (msg_head.opcode) { 1346 1347 case TX_SETUP: 1348 ret = bcm_tx_setup(&msg_head, msg, ifindex, sk); 1349 break; 1350 1351 case RX_SETUP: 1352 ret = bcm_rx_setup(&msg_head, msg, ifindex, sk); 1353 break; 1354 1355 case TX_DELETE: 1356 if (bcm_delete_tx_op(&bo->tx_ops, &msg_head, ifindex)) 1357 ret = MHSIZ; 1358 else 1359 ret = -EINVAL; 1360 break; 1361 1362 case RX_DELETE: 1363 if (bcm_delete_rx_op(&bo->rx_ops, &msg_head, ifindex)) 1364 ret = MHSIZ; 1365 else 1366 ret = -EINVAL; 1367 break; 1368 1369 case TX_READ: 1370 /* reuse msg_head for the reply to TX_READ */ 1371 msg_head.opcode = TX_STATUS; 1372 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex); 1373 break; 1374 1375 case RX_READ: 1376 /* reuse msg_head for the reply to RX_READ */ 1377 msg_head.opcode = RX_STATUS; 1378 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex); 1379 break; 1380 1381 case TX_SEND: 1382 /* we need exactly one CAN frame behind the msg head */ 1383 if ((msg_head.nframes != 1) || (size != cfsiz + MHSIZ)) 1384 ret = -EINVAL; 1385 else 1386 ret = bcm_tx_send(msg, ifindex, sk, cfsiz); 1387 break; 1388 1389 default: 1390 ret = -EINVAL; 1391 break; 1392 } 1393 1394 release_sock(sk); 1395 1396 return ret; 1397 } 1398 1399 /* 1400 * notification handler for netdevice status changes 1401 */ 1402 static int bcm_notifier(struct notifier_block *nb, unsigned long msg, 1403 void *ptr) 1404 { 1405 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1406 struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier); 1407 struct sock *sk = &bo->sk; 1408 struct bcm_op *op; 1409 int notify_enodev = 0; 1410 1411 if (!net_eq(dev_net(dev), sock_net(sk))) 1412 return NOTIFY_DONE; 1413 1414 if (dev->type != ARPHRD_CAN) 1415 return NOTIFY_DONE; 1416 1417 switch (msg) { 1418 1419 case NETDEV_UNREGISTER: 1420 lock_sock(sk); 1421 1422 /* remove device specific receive entries */ 1423 list_for_each_entry(op, &bo->rx_ops, list) 1424 if (op->rx_reg_dev == dev) 1425 bcm_rx_unreg(dev, op); 1426 1427 /* remove device reference, if this is our bound device */ 1428 if (bo->bound && bo->ifindex == dev->ifindex) { 1429 bo->bound = 0; 1430 bo->ifindex = 0; 1431 notify_enodev = 1; 1432 } 1433 1434 release_sock(sk); 1435 1436 if (notify_enodev) { 1437 sk->sk_err = ENODEV; 1438 if (!sock_flag(sk, SOCK_DEAD)) 1439 sk->sk_error_report(sk); 1440 } 1441 break; 1442 1443 case NETDEV_DOWN: 1444 if (bo->bound && bo->ifindex == dev->ifindex) { 1445 sk->sk_err = ENETDOWN; 1446 if (!sock_flag(sk, SOCK_DEAD)) 1447 sk->sk_error_report(sk); 1448 } 1449 } 1450 1451 return NOTIFY_DONE; 1452 } 1453 1454 /* 1455 * initial settings for all BCM sockets to be set at socket creation time 1456 */ 1457 static int bcm_init(struct sock *sk) 1458 { 1459 struct bcm_sock *bo = bcm_sk(sk); 1460 1461 bo->bound = 0; 1462 bo->ifindex = 0; 1463 bo->dropped_usr_msgs = 0; 1464 bo->bcm_proc_read = NULL; 1465 1466 INIT_LIST_HEAD(&bo->tx_ops); 1467 INIT_LIST_HEAD(&bo->rx_ops); 1468 1469 /* set notifier */ 1470 bo->notifier.notifier_call = bcm_notifier; 1471 1472 register_netdevice_notifier(&bo->notifier); 1473 1474 return 0; 1475 } 1476 1477 /* 1478 * standard socket functions 1479 */ 1480 static int bcm_release(struct socket *sock) 1481 { 1482 struct sock *sk = sock->sk; 1483 struct net *net; 1484 struct bcm_sock *bo; 1485 struct bcm_op *op, *next; 1486 1487 if (!sk) 1488 return 0; 1489 1490 net = sock_net(sk); 1491 bo = bcm_sk(sk); 1492 1493 /* remove bcm_ops, timer, rx_unregister(), etc. */ 1494 1495 unregister_netdevice_notifier(&bo->notifier); 1496 1497 lock_sock(sk); 1498 1499 list_for_each_entry_safe(op, next, &bo->tx_ops, list) 1500 bcm_remove_op(op); 1501 1502 list_for_each_entry_safe(op, next, &bo->rx_ops, list) { 1503 /* 1504 * Don't care if we're bound or not (due to netdev problems) 1505 * can_rx_unregister() is always a save thing to do here. 1506 */ 1507 if (op->ifindex) { 1508 /* 1509 * Only remove subscriptions that had not 1510 * been removed due to NETDEV_UNREGISTER 1511 * in bcm_notifier() 1512 */ 1513 if (op->rx_reg_dev) { 1514 struct net_device *dev; 1515 1516 dev = dev_get_by_index(net, op->ifindex); 1517 if (dev) { 1518 bcm_rx_unreg(dev, op); 1519 dev_put(dev); 1520 } 1521 } 1522 } else 1523 can_rx_unregister(net, NULL, op->can_id, 1524 REGMASK(op->can_id), 1525 bcm_rx_handler, op); 1526 1527 bcm_remove_op(op); 1528 } 1529 1530 #if IS_ENABLED(CONFIG_PROC_FS) 1531 /* remove procfs entry */ 1532 if (net->can.bcmproc_dir && bo->bcm_proc_read) 1533 remove_proc_entry(bo->procname, net->can.bcmproc_dir); 1534 #endif /* CONFIG_PROC_FS */ 1535 1536 /* remove device reference */ 1537 if (bo->bound) { 1538 bo->bound = 0; 1539 bo->ifindex = 0; 1540 } 1541 1542 sock_orphan(sk); 1543 sock->sk = NULL; 1544 1545 release_sock(sk); 1546 sock_put(sk); 1547 1548 return 0; 1549 } 1550 1551 static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len, 1552 int flags) 1553 { 1554 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr; 1555 struct sock *sk = sock->sk; 1556 struct bcm_sock *bo = bcm_sk(sk); 1557 struct net *net = sock_net(sk); 1558 int ret = 0; 1559 1560 if (len < sizeof(*addr)) 1561 return -EINVAL; 1562 1563 lock_sock(sk); 1564 1565 if (bo->bound) { 1566 ret = -EISCONN; 1567 goto fail; 1568 } 1569 1570 /* bind a device to this socket */ 1571 if (addr->can_ifindex) { 1572 struct net_device *dev; 1573 1574 dev = dev_get_by_index(net, addr->can_ifindex); 1575 if (!dev) { 1576 ret = -ENODEV; 1577 goto fail; 1578 } 1579 if (dev->type != ARPHRD_CAN) { 1580 dev_put(dev); 1581 ret = -ENODEV; 1582 goto fail; 1583 } 1584 1585 bo->ifindex = dev->ifindex; 1586 dev_put(dev); 1587 1588 } else { 1589 /* no interface reference for ifindex = 0 ('any' CAN device) */ 1590 bo->ifindex = 0; 1591 } 1592 1593 #if IS_ENABLED(CONFIG_PROC_FS) 1594 if (net->can.bcmproc_dir) { 1595 /* unique socket address as filename */ 1596 sprintf(bo->procname, "%lu", sock_i_ino(sk)); 1597 bo->bcm_proc_read = proc_create_net_single(bo->procname, 0644, 1598 net->can.bcmproc_dir, 1599 bcm_proc_show, sk); 1600 if (!bo->bcm_proc_read) { 1601 ret = -ENOMEM; 1602 goto fail; 1603 } 1604 } 1605 #endif /* CONFIG_PROC_FS */ 1606 1607 bo->bound = 1; 1608 1609 fail: 1610 release_sock(sk); 1611 1612 return ret; 1613 } 1614 1615 static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 1616 int flags) 1617 { 1618 struct sock *sk = sock->sk; 1619 struct sk_buff *skb; 1620 int error = 0; 1621 int noblock; 1622 int err; 1623 1624 noblock = flags & MSG_DONTWAIT; 1625 flags &= ~MSG_DONTWAIT; 1626 skb = skb_recv_datagram(sk, flags, noblock, &error); 1627 if (!skb) 1628 return error; 1629 1630 if (skb->len < size) 1631 size = skb->len; 1632 1633 err = memcpy_to_msg(msg, skb->data, size); 1634 if (err < 0) { 1635 skb_free_datagram(sk, skb); 1636 return err; 1637 } 1638 1639 sock_recv_ts_and_drops(msg, sk, skb); 1640 1641 if (msg->msg_name) { 1642 __sockaddr_check_size(sizeof(struct sockaddr_can)); 1643 msg->msg_namelen = sizeof(struct sockaddr_can); 1644 memcpy(msg->msg_name, skb->cb, msg->msg_namelen); 1645 } 1646 1647 skb_free_datagram(sk, skb); 1648 1649 return size; 1650 } 1651 1652 static const struct proto_ops bcm_ops = { 1653 .family = PF_CAN, 1654 .release = bcm_release, 1655 .bind = sock_no_bind, 1656 .connect = bcm_connect, 1657 .socketpair = sock_no_socketpair, 1658 .accept = sock_no_accept, 1659 .getname = sock_no_getname, 1660 .poll = datagram_poll, 1661 .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */ 1662 .listen = sock_no_listen, 1663 .shutdown = sock_no_shutdown, 1664 .setsockopt = sock_no_setsockopt, 1665 .getsockopt = sock_no_getsockopt, 1666 .sendmsg = bcm_sendmsg, 1667 .recvmsg = bcm_recvmsg, 1668 .mmap = sock_no_mmap, 1669 .sendpage = sock_no_sendpage, 1670 }; 1671 1672 static struct proto bcm_proto __read_mostly = { 1673 .name = "CAN_BCM", 1674 .owner = THIS_MODULE, 1675 .obj_size = sizeof(struct bcm_sock), 1676 .init = bcm_init, 1677 }; 1678 1679 static const struct can_proto bcm_can_proto = { 1680 .type = SOCK_DGRAM, 1681 .protocol = CAN_BCM, 1682 .ops = &bcm_ops, 1683 .prot = &bcm_proto, 1684 }; 1685 1686 static int canbcm_pernet_init(struct net *net) 1687 { 1688 #if IS_ENABLED(CONFIG_PROC_FS) 1689 /* create /proc/net/can-bcm directory */ 1690 net->can.bcmproc_dir = proc_net_mkdir(net, "can-bcm", net->proc_net); 1691 #endif /* CONFIG_PROC_FS */ 1692 1693 return 0; 1694 } 1695 1696 static void canbcm_pernet_exit(struct net *net) 1697 { 1698 #if IS_ENABLED(CONFIG_PROC_FS) 1699 /* remove /proc/net/can-bcm directory */ 1700 if (net->can.bcmproc_dir) 1701 remove_proc_entry("can-bcm", net->proc_net); 1702 #endif /* CONFIG_PROC_FS */ 1703 } 1704 1705 static struct pernet_operations canbcm_pernet_ops __read_mostly = { 1706 .init = canbcm_pernet_init, 1707 .exit = canbcm_pernet_exit, 1708 }; 1709 1710 static int __init bcm_module_init(void) 1711 { 1712 int err; 1713 1714 pr_info("can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n"); 1715 1716 err = can_proto_register(&bcm_can_proto); 1717 if (err < 0) { 1718 printk(KERN_ERR "can: registration of bcm protocol failed\n"); 1719 return err; 1720 } 1721 1722 register_pernet_subsys(&canbcm_pernet_ops); 1723 return 0; 1724 } 1725 1726 static void __exit bcm_module_exit(void) 1727 { 1728 can_proto_unregister(&bcm_can_proto); 1729 unregister_pernet_subsys(&canbcm_pernet_ops); 1730 } 1731 1732 module_init(bcm_module_init); 1733 module_exit(bcm_module_exit); 1734