1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) 2 /* 3 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content 4 * 5 * Copyright (c) 2002-2017 Volkswagen Group Electronic Research 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of Volkswagen nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * Alternatively, provided that this notice is retained in full, this 21 * software may be distributed under the terms of the GNU General 22 * Public License ("GPL") version 2, in which case the provisions of the 23 * GPL apply INSTEAD OF those given above. 24 * 25 * The provided data structures and external interfaces from this code 26 * are not restricted to be used by modules with a GPL compatible license. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 39 * DAMAGE. 40 * 41 */ 42 43 #include <linux/module.h> 44 #include <linux/init.h> 45 #include <linux/interrupt.h> 46 #include <linux/hrtimer.h> 47 #include <linux/list.h> 48 #include <linux/proc_fs.h> 49 #include <linux/seq_file.h> 50 #include <linux/uio.h> 51 #include <linux/net.h> 52 #include <linux/netdevice.h> 53 #include <linux/socket.h> 54 #include <linux/if_arp.h> 55 #include <linux/skbuff.h> 56 #include <linux/can.h> 57 #include <linux/can/can-ml.h> 58 #include <linux/can/core.h> 59 #include <linux/can/skb.h> 60 #include <linux/can/bcm.h> 61 #include <linux/slab.h> 62 #include <linux/workqueue.h> 63 #include <linux/spinlock.h> 64 #include <net/can.h> 65 #include <net/sock.h> 66 #include <net/net_namespace.h> 67 68 /* 69 * To send multiple CAN frame content within TX_SETUP or to filter 70 * CAN messages with multiplex index within RX_SETUP, the number of 71 * different filters is limited to 256 due to the one byte index value. 72 */ 73 #define MAX_NFRAMES 256 74 75 /* limit timers to 400 days for sending/timeouts */ 76 #define BCM_TIMER_SEC_MAX (400 * 24 * 60 * 60) 77 78 /* use of last_frames[index].flags */ 79 #define RX_LOCAL 0x10 /* frame was created on the local host */ 80 #define RX_OWN 0x20 /* frame was sent via the socket it was received on */ 81 #define RX_RECV 0x40 /* received data for this element */ 82 #define RX_THR 0x80 /* element not been sent due to throttle feature */ 83 #define BCM_CAN_FLAGS_MASK 0x0F /* to clean private flags after usage */ 84 85 /* get best masking value for can_rx_register() for a given single can_id */ 86 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \ 87 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \ 88 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG)) 89 90 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol"); 91 MODULE_LICENSE("Dual BSD/GPL"); 92 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>"); 93 MODULE_ALIAS("can-proto-2"); 94 95 #define BCM_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_ifindex) 96 97 static struct workqueue_struct *bcm_wq; 98 99 /* 100 * easy access to the first 64 bit of can(fd)_frame payload. cp->data is 101 * 64 bit aligned so the offset has to be multiples of 8 which is ensured 102 * by the only callers in bcm_rx_cmp_to_index() bcm_rx_handler(). 103 */ 104 static inline u64 get_u64(const struct canfd_frame *cp, int offset) 105 { 106 return *(u64 *)(cp->data + offset); 107 } 108 109 struct bcm_op { 110 struct list_head list; 111 struct rcu_head rcu; 112 struct work_struct work; 113 int ifindex; 114 canid_t can_id; 115 u32 flags; 116 atomic_long_t frames_abs, frames_filtered; 117 struct bcm_timeval ival1, ival2; 118 struct hrtimer timer, thrtimer; 119 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg; 120 int rx_ifindex; 121 int if_detected; /* first received ifindex in ANYDEV rx_op mode */ 122 int cfsiz; 123 u32 count; 124 u32 nframes; 125 u32 currframe; 126 /* void pointers to arrays of struct can[fd]_frame */ 127 void *frames; 128 void *last_frames; 129 struct canfd_frame sframe; 130 struct canfd_frame last_sframe; 131 struct sock *sk; 132 struct net_device *rx_reg_dev; 133 netdevice_tracker rx_reg_dev_tracker; 134 spinlock_t bcm_tx_lock; /* protect tx data and timer updates */ 135 spinlock_t bcm_rx_update_lock; /* protect filter/timer data updates */ 136 }; 137 138 struct bcm_sock { 139 struct sock sk; 140 int bound; 141 int ifindex; 142 struct list_head notifier; 143 struct list_head rx_ops; 144 struct list_head tx_ops; 145 unsigned long dropped_usr_msgs; 146 struct proc_dir_entry *bcm_proc_read; 147 char procname [32]; /* inode number in decimal with \0 */ 148 struct mutex conf_lock; /* protects bind and the bcm_op lists */ 149 }; 150 151 static LIST_HEAD(bcm_notifier_list); 152 static DEFINE_SPINLOCK(bcm_notifier_lock); 153 static struct bcm_sock *bcm_busy_notifier; 154 155 /* Return pointer to store the extra msg flags for bcm_recvmsg(). 156 * We use the space of one unsigned int beyond the 'struct sockaddr_can' 157 * in skb->cb. 158 */ 159 static inline unsigned int *bcm_flags(struct sk_buff *skb) 160 { 161 /* return pointer after struct sockaddr_can */ 162 return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]); 163 } 164 165 static inline struct bcm_sock *bcm_sk(const struct sock *sk) 166 { 167 return (struct bcm_sock *)sk; 168 } 169 170 static inline ktime_t bcm_timeval_to_ktime(struct bcm_timeval tv) 171 { 172 return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC); 173 } 174 175 /* check limitations for timeval provided by user */ 176 static bool bcm_is_invalid_tv(struct bcm_msg_head *msg_head) 177 { 178 if ((msg_head->ival1.tv_sec < 0) || 179 (msg_head->ival1.tv_sec > BCM_TIMER_SEC_MAX) || 180 (msg_head->ival1.tv_usec < 0) || 181 (msg_head->ival1.tv_usec >= USEC_PER_SEC) || 182 (msg_head->ival2.tv_sec < 0) || 183 (msg_head->ival2.tv_sec > BCM_TIMER_SEC_MAX) || 184 (msg_head->ival2.tv_usec < 0) || 185 (msg_head->ival2.tv_usec >= USEC_PER_SEC)) 186 return true; 187 188 return false; 189 } 190 191 #define CFSIZ(flags) ((flags & CAN_FD_FRAME) ? CANFD_MTU : CAN_MTU) 192 #define OPSIZ sizeof(struct bcm_op) 193 #define MHSIZ sizeof(struct bcm_msg_head) 194 195 /* 196 * procfs functions 197 */ 198 #if IS_ENABLED(CONFIG_PROC_FS) 199 static char *bcm_proc_getifname(struct net *net, char *result, int ifindex) 200 { 201 struct net_device *dev; 202 203 if (!ifindex) 204 return "any"; 205 206 rcu_read_lock(); 207 dev = dev_get_by_index_rcu(net, ifindex); 208 if (dev) 209 strcpy(result, dev->name); 210 else 211 strcpy(result, "???"); 212 rcu_read_unlock(); 213 214 return result; 215 } 216 217 static int bcm_proc_show(struct seq_file *m, void *v) 218 { 219 char ifname[IFNAMSIZ]; 220 struct net *net = m->private; 221 struct sock *sk = (struct sock *)pde_data(m->file->f_inode); 222 struct bcm_sock *bo = bcm_sk(sk); 223 int ifindex = READ_ONCE(bo->ifindex); 224 struct bcm_op *op; 225 226 seq_printf(m, ">>> sock inode %s", bo->procname); 227 seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs); 228 if (READ_ONCE(bo->bound)) 229 seq_printf(m, " / bound %s", 230 bcm_proc_getifname(net, ifname, ifindex)); 231 else 232 seq_puts(m, " / unbound"); 233 seq_printf(m, " <<<\n"); 234 235 rcu_read_lock(); 236 237 list_for_each_entry_rcu(op, &bo->rx_ops, list) { 238 239 long reduction, frames_filtered, frames_abs; 240 241 frames_filtered = atomic_long_read(&op->frames_filtered); 242 frames_abs = atomic_long_read(&op->frames_abs); 243 244 /* print only active entries & prevent division by zero */ 245 if (!frames_abs) 246 continue; 247 248 seq_printf(m, "rx_op: %03X %-5s ", op->can_id, 249 bcm_proc_getifname(net, ifname, op->ifindex)); 250 251 if (op->flags & CAN_FD_FRAME) 252 seq_printf(m, "(%u)", op->nframes); 253 else 254 seq_printf(m, "[%u]", op->nframes); 255 256 seq_printf(m, "%c ", (op->flags & RX_CHECK_DLC) ? 'd' : ' '); 257 258 if (op->kt_ival1) 259 seq_printf(m, "timeo=%lld ", 260 (long long)ktime_to_us(op->kt_ival1)); 261 262 if (op->kt_ival2) 263 seq_printf(m, "thr=%lld ", 264 (long long)ktime_to_us(op->kt_ival2)); 265 266 seq_printf(m, "# recv %ld (%ld) => reduction: ", 267 frames_filtered, frames_abs); 268 269 reduction = 100 - (frames_filtered * 100) / frames_abs; 270 271 seq_printf(m, "%s%ld%%\n", 272 (reduction == 100) ? "near " : "", reduction); 273 } 274 275 list_for_each_entry_rcu(op, &bo->tx_ops, list) { 276 277 seq_printf(m, "tx_op: %03X %s ", op->can_id, 278 bcm_proc_getifname(net, ifname, op->ifindex)); 279 280 if (op->flags & CAN_FD_FRAME) 281 seq_printf(m, "(%u) ", op->nframes); 282 else 283 seq_printf(m, "[%u] ", op->nframes); 284 285 if (op->kt_ival1) 286 seq_printf(m, "t1=%lld ", 287 (long long)ktime_to_us(op->kt_ival1)); 288 289 if (op->kt_ival2) 290 seq_printf(m, "t2=%lld ", 291 (long long)ktime_to_us(op->kt_ival2)); 292 293 seq_printf(m, "# sent %ld\n", 294 atomic_long_read(&op->frames_abs)); 295 } 296 seq_putc(m, '\n'); 297 298 rcu_read_unlock(); 299 300 return 0; 301 } 302 #endif /* CONFIG_PROC_FS */ 303 304 static void bcm_update_rx_stats(struct bcm_op *op) 305 { 306 /* prevent overflow of the reduction% calculation in bcm_proc_show() */ 307 if (atomic_long_inc_return(&op->frames_abs) > LONG_MAX / 100) { 308 atomic_long_set(&op->frames_filtered, 0); 309 atomic_long_set(&op->frames_abs, 0); 310 } 311 } 312 313 static void bcm_update_tx_stats(struct bcm_op *op) 314 { 315 /* tx_op has no reduction% calculation - use the full range and 316 * just keep the displayed counter non-negative on overflow 317 */ 318 if (atomic_long_inc_return(&op->frames_abs) == LONG_MAX) 319 atomic_long_set(&op->frames_abs, 0); 320 } 321 322 /* 323 * bcm_can_tx - send the (next) CAN frame to the appropriate CAN interface 324 * of the given bcm tx op 325 */ 326 static void bcm_can_tx(struct bcm_op *op, struct canfd_frame *cf) 327 { 328 struct sk_buff *skb; 329 struct can_skb_ext *csx; 330 struct net_device *dev; 331 struct canfd_frame cframe; 332 bool cyclic = !cf; 333 unsigned int idx = 0; 334 int err; 335 336 /* no target device? => exit */ 337 if (!op->ifindex) 338 return; 339 340 if (cyclic) { 341 /* read currframe under lock protection */ 342 spin_lock_bh(&op->bcm_tx_lock); 343 idx = op->currframe; 344 memcpy(&cframe, op->frames + op->cfsiz * idx, op->cfsiz); 345 cf = &cframe; 346 spin_unlock_bh(&op->bcm_tx_lock); 347 } 348 349 dev = dev_get_by_index(sock_net(op->sk), op->ifindex); 350 if (!dev) { 351 /* RFC: should this bcm_op remove itself here? */ 352 return; 353 } 354 355 skb = alloc_skb(op->cfsiz, gfp_any()); 356 if (!skb) 357 goto out; 358 359 csx = can_skb_ext_add(skb); 360 if (!csx) { 361 kfree_skb(skb); 362 goto out; 363 } 364 365 csx->can_iif = dev->ifindex; 366 367 skb_put_data(skb, cf, op->cfsiz); 368 369 /* send with loopback */ 370 skb->dev = dev; 371 can_skb_set_owner(skb, op->sk); 372 err = can_send(skb, 1); 373 374 /* update currframe and count under lock protection */ 375 spin_lock_bh(&op->bcm_tx_lock); 376 377 if (!err) 378 bcm_update_tx_stats(op); 379 380 /* only advance the cyclic sequence if nothing reset currframe while 381 * we were sending - a concurrent TX_RESET_MULTI_IDX means this 382 * frame's bookkeeping belongs to a sequence that no longer exists 383 */ 384 if (!cyclic || op->currframe == idx) { 385 op->currframe++; 386 387 /* reached last frame? */ 388 if (op->currframe >= op->nframes) 389 op->currframe = 0; 390 391 if (op->count > 0) 392 op->count--; 393 } 394 395 spin_unlock_bh(&op->bcm_tx_lock); 396 out: 397 dev_put(dev); 398 } 399 400 /* 401 * bcm_send_to_user - send a BCM message to the userspace 402 * (consisting of bcm_msg_head + x CAN frames) 403 */ 404 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head, 405 struct canfd_frame *frames, int has_timestamp) 406 { 407 struct sk_buff *skb; 408 struct canfd_frame *firstframe; 409 struct sockaddr_can *addr; 410 struct sock *sk = op->sk; 411 unsigned int datalen = head->nframes * op->cfsiz; 412 unsigned int *pflags; 413 enum skb_drop_reason reason; 414 415 skb = alloc_skb(sizeof(*head) + datalen, gfp_any()); 416 if (!skb) 417 return; 418 419 skb_put_data(skb, head, sizeof(*head)); 420 421 /* ensure space for sockaddr_can and msg flags */ 422 sock_skb_cb_check_size(sizeof(struct sockaddr_can) + 423 sizeof(unsigned int)); 424 425 /* initialize msg flags */ 426 pflags = bcm_flags(skb); 427 *pflags = 0; 428 429 if (head->nframes) { 430 /* CAN frames starting here */ 431 firstframe = (struct canfd_frame *)skb_tail_pointer(skb); 432 433 skb_put_data(skb, frames, datalen); 434 435 /* 436 * the BCM uses the flags-element of the canfd_frame 437 * structure for internal purposes. This is only 438 * relevant for updates that are generated by the 439 * BCM, where nframes is 1 440 */ 441 if (head->nframes == 1) { 442 if (firstframe->flags & RX_LOCAL) 443 *pflags |= MSG_DONTROUTE; 444 if (firstframe->flags & RX_OWN) 445 *pflags |= MSG_CONFIRM; 446 447 firstframe->flags &= BCM_CAN_FLAGS_MASK; 448 } 449 } 450 451 if (has_timestamp) { 452 /* restore rx timestamp */ 453 skb->tstamp = op->rx_stamp; 454 } 455 456 /* 457 * Put the datagram to the queue so that bcm_recvmsg() can 458 * get it from there. We need to pass the interface index to 459 * bcm_recvmsg(). We pass a whole struct sockaddr_can in skb->cb 460 * containing the interface index. 461 */ 462 463 addr = (struct sockaddr_can *)skb->cb; 464 memset(addr, 0, sizeof(*addr)); 465 addr->can_family = AF_CAN; 466 addr->can_ifindex = op->rx_ifindex; 467 468 reason = sock_queue_rcv_skb_reason(sk, skb); 469 if (reason) { 470 struct bcm_sock *bo = bcm_sk(sk); 471 472 sk_skb_reason_drop(sk, skb, reason); 473 /* don't care about overflows in this statistic */ 474 bo->dropped_usr_msgs++; 475 } 476 } 477 478 static bool bcm_tx_set_expiry(struct bcm_op *op, struct hrtimer *hrt) 479 { 480 ktime_t ival; 481 482 spin_lock_bh(&op->bcm_tx_lock); 483 484 if (op->kt_ival1 && op->count) 485 ival = op->kt_ival1; 486 else if (op->kt_ival2) { 487 ival = op->kt_ival2; 488 } else { 489 spin_unlock_bh(&op->bcm_tx_lock); 490 return false; 491 } 492 493 spin_unlock_bh(&op->bcm_tx_lock); 494 495 hrtimer_set_expires(hrt, ktime_add(ktime_get(), ival)); 496 return true; 497 } 498 499 static void bcm_tx_start_timer(struct bcm_op *op) 500 { 501 if (bcm_tx_set_expiry(op, &op->timer)) 502 hrtimer_start_expires(&op->timer, HRTIMER_MODE_ABS_SOFT); 503 } 504 505 /* bcm_tx_timeout_handler - performs cyclic CAN frame transmissions */ 506 static enum hrtimer_restart bcm_tx_timeout_handler(struct hrtimer *hrtimer) 507 { 508 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer); 509 struct bcm_msg_head msg_head; 510 bool tx_ival1, tx_ival2; 511 512 /* snapshot kt_ival1/kt_ival2/count under lock to avoid torn 513 * ktime_t reads racing with concurrent bcm_tx_setup() updates 514 */ 515 spin_lock_bh(&op->bcm_tx_lock); 516 tx_ival1 = op->kt_ival1 && (op->count > 0); 517 tx_ival2 = !!op->kt_ival2; 518 spin_unlock_bh(&op->bcm_tx_lock); 519 520 if (tx_ival1) { 521 u32 flags, count; 522 struct bcm_timeval ival1, ival2; 523 524 bcm_can_tx(op, NULL); 525 526 /* snapshot variables under lock to avoid torn reads racing 527 * with concurrent bcm_tx_setup() updates 528 */ 529 spin_lock_bh(&op->bcm_tx_lock); 530 flags = op->flags; 531 count = op->count; 532 ival1 = op->ival1; 533 ival2 = op->ival2; 534 spin_unlock_bh(&op->bcm_tx_lock); 535 536 if (!count && (flags & TX_COUNTEVT)) { 537 /* create notification to user */ 538 memset(&msg_head, 0, sizeof(msg_head)); 539 msg_head.opcode = TX_EXPIRED; 540 msg_head.flags = flags; 541 msg_head.count = count; 542 msg_head.ival1 = ival1; 543 msg_head.ival2 = ival2; 544 msg_head.can_id = op->can_id; 545 msg_head.nframes = 0; 546 547 bcm_send_to_user(op, &msg_head, NULL, 0); 548 } 549 550 } else if (tx_ival2) { 551 bcm_can_tx(op, NULL); 552 } 553 554 return bcm_tx_set_expiry(op, &op->timer) ? 555 HRTIMER_RESTART : HRTIMER_NORESTART; 556 } 557 558 /* 559 * bcm_rx_changed - create a RX_CHANGED notification due to changed content 560 */ 561 static void bcm_rx_changed(struct bcm_op *op, struct canfd_frame *data) 562 { 563 struct bcm_msg_head head; 564 565 /* update statistics (frames_filtered <= frames_abs) */ 566 if (atomic_long_read(&op->frames_abs)) 567 atomic_long_inc(&op->frames_filtered); 568 569 /* this element is not throttled anymore */ 570 data->flags &= ~RX_THR; 571 572 memset(&head, 0, sizeof(head)); 573 head.opcode = RX_CHANGED; 574 head.flags = op->flags; 575 head.count = op->count; 576 head.ival1 = op->ival1; 577 head.ival2 = op->ival2; 578 head.can_id = op->can_id; 579 head.nframes = 1; 580 581 bcm_send_to_user(op, &head, data, 1); 582 } 583 584 /* 585 * bcm_rx_update_and_send - process a detected relevant receive content change 586 * 1. update the last received data 587 * 2. send a notification to the user (if possible) 588 */ 589 static void bcm_rx_update_and_send(struct bcm_op *op, 590 struct canfd_frame *lastdata, 591 const struct canfd_frame *rxdata, 592 unsigned char traffic_flags) 593 { 594 memcpy(lastdata, rxdata, op->cfsiz); 595 596 /* mark as used and throttled by default */ 597 lastdata->flags |= (RX_RECV|RX_THR); 598 599 /* add own/local/remote traffic flags */ 600 lastdata->flags |= traffic_flags; 601 602 /* throttling mode inactive ? */ 603 if (!op->kt_ival2) { 604 /* send RX_CHANGED to the user immediately */ 605 bcm_rx_changed(op, lastdata); 606 return; 607 } 608 609 /* with active throttling timer we are just done here */ 610 if (hrtimer_active(&op->thrtimer)) 611 return; 612 613 /* first reception with enabled throttling mode */ 614 if (!op->kt_lastmsg) 615 goto rx_changed_settime; 616 617 /* got a second frame inside a potential throttle period? */ 618 if (ktime_us_delta(ktime_get(), op->kt_lastmsg) < 619 ktime_to_us(op->kt_ival2)) { 620 /* do not send the saved data - only start throttle timer */ 621 hrtimer_start(&op->thrtimer, 622 ktime_add(op->kt_lastmsg, op->kt_ival2), 623 HRTIMER_MODE_ABS_SOFT); 624 return; 625 } 626 627 /* the gap was that big, that throttling was not needed here */ 628 rx_changed_settime: 629 bcm_rx_changed(op, lastdata); 630 op->kt_lastmsg = ktime_get(); 631 } 632 633 /* 634 * bcm_rx_cmp_to_index - (bit)compares the currently received data to formerly 635 * received data stored in op->last_frames[] 636 */ 637 static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index, 638 const struct canfd_frame *rxdata, 639 unsigned char traffic_flags) 640 { 641 struct canfd_frame *cf = op->frames + op->cfsiz * index; 642 struct canfd_frame *lcf = op->last_frames + op->cfsiz * index; 643 int i; 644 645 /* 646 * no one uses the MSBs of flags for comparison, 647 * so we use it here to detect the first time of reception 648 */ 649 650 if (!(lcf->flags & RX_RECV)) { 651 /* received data for the first time => send update to user */ 652 bcm_rx_update_and_send(op, lcf, rxdata, traffic_flags); 653 return; 654 } 655 656 /* do a real check in CAN frame data section */ 657 for (i = 0; i < rxdata->len; i += 8) { 658 if ((get_u64(cf, i) & get_u64(rxdata, i)) != 659 (get_u64(cf, i) & get_u64(lcf, i))) { 660 bcm_rx_update_and_send(op, lcf, rxdata, traffic_flags); 661 return; 662 } 663 } 664 665 if (op->flags & RX_CHECK_DLC) { 666 /* do a real check in CAN frame length */ 667 if (rxdata->len != lcf->len) { 668 bcm_rx_update_and_send(op, lcf, rxdata, traffic_flags); 669 return; 670 } 671 } 672 } 673 674 /* 675 * bcm_rx_starttimer - enable timeout monitoring for CAN frame reception 676 */ 677 static void bcm_rx_starttimer(struct bcm_op *op) 678 { 679 if (op->flags & RX_NO_AUTOTIMER) 680 return; 681 682 if (op->kt_ival1) 683 hrtimer_start(&op->timer, op->kt_ival1, HRTIMER_MODE_REL_SOFT); 684 } 685 686 /* bcm_rx_timeout_handler - when the (cyclic) CAN frame reception timed out */ 687 static enum hrtimer_restart bcm_rx_timeout_handler(struct hrtimer *hrtimer) 688 { 689 struct bcm_op *op = container_of(hrtimer, struct bcm_op, timer); 690 struct bcm_msg_head msg_head; 691 692 spin_lock_bh(&op->bcm_rx_update_lock); 693 694 /* if user wants to be informed, when cyclic CAN-Messages come back */ 695 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) { 696 /* clear received CAN frames to indicate 'nothing received' */ 697 memset(op->last_frames, 0, op->nframes * op->cfsiz); 698 } 699 700 /* create notification to user */ 701 memset(&msg_head, 0, sizeof(msg_head)); 702 msg_head.opcode = RX_TIMEOUT; 703 msg_head.flags = op->flags; 704 msg_head.count = op->count; 705 msg_head.ival1 = op->ival1; 706 msg_head.ival2 = op->ival2; 707 msg_head.can_id = op->can_id; 708 msg_head.nframes = 0; 709 710 spin_unlock_bh(&op->bcm_rx_update_lock); 711 712 bcm_send_to_user(op, &msg_head, NULL, 0); 713 714 return HRTIMER_NORESTART; 715 } 716 717 /* 718 * bcm_rx_do_flush - helper for bcm_rx_thr_flush 719 */ 720 static inline int bcm_rx_do_flush(struct bcm_op *op, unsigned int index) 721 { 722 struct canfd_frame *lcf = op->last_frames + op->cfsiz * index; 723 724 if ((op->last_frames) && (lcf->flags & RX_THR)) { 725 bcm_rx_changed(op, lcf); 726 return 1; 727 } 728 return 0; 729 } 730 731 /* 732 * bcm_rx_thr_flush - Check for throttled data and send it to the userspace 733 */ 734 static int bcm_rx_thr_flush(struct bcm_op *op) 735 { 736 int updated = 0; 737 738 if (op->nframes > 1) { 739 unsigned int i; 740 741 /* for MUX filter we start at index 1 */ 742 for (i = 1; i < op->nframes; i++) 743 updated += bcm_rx_do_flush(op, i); 744 745 } else { 746 /* for RX_FILTER_ID and simple filter */ 747 updated += bcm_rx_do_flush(op, 0); 748 } 749 750 return updated; 751 } 752 753 /* 754 * bcm_rx_thr_handler - the time for blocked content updates is over now: 755 * Check for throttled data and send it to the userspace 756 */ 757 static enum hrtimer_restart bcm_rx_thr_handler(struct hrtimer *hrtimer) 758 { 759 struct bcm_op *op = container_of(hrtimer, struct bcm_op, thrtimer); 760 enum hrtimer_restart ret; 761 762 spin_lock_bh(&op->bcm_rx_update_lock); 763 764 /* kt_ival2 may have been concurrently cleared by bcm_rx_setup() 765 * before it cancels this timer - never forward with a zero 766 * interval in that case. 767 */ 768 if (bcm_rx_thr_flush(op) && op->kt_ival2) { 769 hrtimer_forward_now(hrtimer, op->kt_ival2); 770 ret = HRTIMER_RESTART; 771 } else { 772 /* rearm throttle handling */ 773 op->kt_lastmsg = 0; 774 ret = HRTIMER_NORESTART; 775 } 776 777 spin_unlock_bh(&op->bcm_rx_update_lock); 778 779 return ret; 780 } 781 782 /* 783 * bcm_rx_handler - handle a CAN frame reception 784 */ 785 static void bcm_rx_handler(struct sk_buff *skb, void *data) 786 { 787 struct bcm_op *op = (struct bcm_op *)data; 788 const struct canfd_frame *rxframe = (struct canfd_frame *)skb->data; 789 struct canfd_frame rtrframe; 790 unsigned int i; 791 unsigned char traffic_flags; 792 bool rtr_frame; 793 794 if (op->can_id != rxframe->can_id) 795 return; 796 797 /* make sure to handle the correct frame type (CAN / CAN FD) */ 798 if (op->flags & CAN_FD_FRAME) { 799 if (!can_is_canfd_skb(skb)) 800 return; 801 } else { 802 if (!can_is_can_skb(skb)) 803 return; 804 } 805 806 /* An ANYDEV op with an active RX timeout and/or throttle timer 807 * tracks a single source interface: claim the first interface that 808 * delivers a matching frame and reject frames from any other one, 809 * before hrtimer_cancel() below can touch op->timer - this avoids 810 * racing bcm_rx_timeout_handler() across concurrent interfaces. 811 * RX_RTR_FRAME ops are excluded, as kt_ival1/kt_ival2 may briefly 812 * hold a stale value from an earlier non-RTR configuration. 813 */ 814 if (!op->ifindex) { 815 spin_lock_bh(&op->bcm_rx_update_lock); 816 817 if (!(op->flags & RX_RTR_FRAME) && 818 (op->kt_ival1 || op->kt_ival2)) { 819 /* don't claim to vanishing interface */ 820 if (!op->if_detected && 821 READ_ONCE(skb->dev->reg_state) == NETREG_REGISTERED) 822 op->if_detected = skb->dev->ifindex; 823 824 if (op->if_detected != skb->dev->ifindex) { 825 spin_unlock_bh(&op->bcm_rx_update_lock); 826 return; 827 } 828 } 829 830 spin_unlock_bh(&op->bcm_rx_update_lock); 831 } 832 833 /* disable timeout */ 834 hrtimer_cancel(&op->timer); 835 836 /* op->flags/op->frames may be updated concurrently by bcm_rx_setup() */ 837 spin_lock_bh(&op->bcm_rx_update_lock); 838 839 rtr_frame = op->flags & RX_RTR_FRAME; 840 if (rtr_frame) { 841 bcm_update_rx_stats(op); 842 /* snapshot RTR content under lock */ 843 memcpy(&rtrframe, op->frames, op->cfsiz); 844 spin_unlock_bh(&op->bcm_rx_update_lock); 845 846 /* send reply for RTR-request (placed in op->frames[0]) */ 847 bcm_can_tx(op, &rtrframe); 848 return; 849 } 850 851 /* update statistics in the same critical section as bcm_rx_changed() 852 * below: frames_filtered must never be checked/incremented against a 853 * frames_abs snapshot from a concurrent bcm_rx_handler() call on 854 * another CPU for the same (wildcard) op, or frames_filtered can end 855 * up larger than frames_abs. 856 */ 857 bcm_update_rx_stats(op); 858 859 /* compute flags to distinguish between own/local/remote CAN traffic */ 860 traffic_flags = 0; 861 if (skb->sk) { 862 traffic_flags |= RX_LOCAL; 863 if (skb->sk == op->sk) 864 traffic_flags |= RX_OWN; 865 } 866 867 /* save rx timestamp and originator for recvfrom() under lock: an 868 * ANYDEV op without an active timer can still run concurrently on 869 * different CPUs, so content and meta data must be bundled here. 870 */ 871 op->rx_stamp = skb->tstamp; 872 op->rx_ifindex = skb->dev->ifindex; 873 874 if (op->flags & RX_FILTER_ID) { 875 /* the easiest case */ 876 bcm_rx_update_and_send(op, op->last_frames, rxframe, 877 traffic_flags); 878 goto rx_starttimer; 879 } 880 881 if (op->nframes == 1) { 882 /* simple compare with index 0 */ 883 bcm_rx_cmp_to_index(op, 0, rxframe, traffic_flags); 884 goto rx_starttimer; 885 } 886 887 if (op->nframes > 1) { 888 /* 889 * multiplex compare 890 * 891 * find the first multiplex mask that fits. 892 * Remark: The MUX-mask is stored in index 0 - but only the 893 * first 64 bits of the frame data[] are relevant (CAN FD) 894 */ 895 896 for (i = 1; i < op->nframes; i++) { 897 if ((get_u64(op->frames, 0) & get_u64(rxframe, 0)) == 898 (get_u64(op->frames, 0) & 899 get_u64(op->frames + op->cfsiz * i, 0))) { 900 bcm_rx_cmp_to_index(op, i, rxframe, 901 traffic_flags); 902 break; 903 } 904 } 905 } 906 907 rx_starttimer: 908 bcm_rx_starttimer(op); 909 910 spin_unlock_bh(&op->bcm_rx_update_lock); 911 } 912 913 /* 914 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements 915 */ 916 static struct bcm_op *bcm_find_op(struct list_head *ops, 917 struct bcm_msg_head *mh, int ifindex) 918 { 919 struct bcm_op *op; 920 921 list_for_each_entry(op, ops, list) { 922 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) && 923 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) 924 return op; 925 } 926 927 return NULL; 928 } 929 930 static void bcm_free_op_work(struct work_struct *work) 931 { 932 struct bcm_op *op = container_of(work, struct bcm_op, work); 933 934 hrtimer_cancel(&op->timer); 935 hrtimer_cancel(&op->thrtimer); 936 937 if ((op->frames) && (op->frames != &op->sframe)) 938 kfree(op->frames); 939 940 if ((op->last_frames) && (op->last_frames != &op->last_sframe)) 941 kfree(op->last_frames); 942 943 /* the last possible access to op->timer/op->thrtimer has now 944 * happened above via hrtimer_cancel() - op->sk is no longer 945 * needed by any pending timer callback, so drop our reference 946 */ 947 sock_put(op->sk); 948 949 kfree(op); 950 } 951 952 static void bcm_free_op_rcu(struct rcu_head *rcu_head) 953 { 954 struct bcm_op *op = container_of(rcu_head, struct bcm_op, rcu); 955 956 INIT_WORK(&op->work, bcm_free_op_work); 957 queue_work(bcm_wq, &op->work); 958 } 959 960 static void bcm_remove_op(struct bcm_op *op) 961 { 962 hrtimer_cancel(&op->timer); 963 hrtimer_cancel(&op->thrtimer); 964 965 call_rcu(&op->rcu, bcm_free_op_rcu); 966 } 967 968 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op) 969 { 970 if (op->rx_reg_dev == dev) { 971 can_rx_unregister(sock_net(op->sk), dev, op->can_id, 972 REGMASK(op->can_id), bcm_rx_handler, op); 973 974 /* mark as removed subscription */ 975 op->rx_reg_dev = NULL; 976 netdev_put(dev, &op->rx_reg_dev_tracker); 977 } else 978 printk(KERN_ERR "can-bcm: bcm_rx_unreg: registered device " 979 "mismatch %p %p\n", op->rx_reg_dev, dev); 980 } 981 982 /* 983 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops) 984 */ 985 static int bcm_delete_rx_op(struct list_head *ops, struct bcm_msg_head *mh, 986 int ifindex) 987 { 988 struct bcm_op *op, *n; 989 990 list_for_each_entry_safe(op, n, ops, list) { 991 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) && 992 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) { 993 994 /* disable automatic timer on frame reception */ 995 op->flags |= RX_NO_AUTOTIMER; 996 997 if (op->ifindex) { 998 /* Only remove subscriptions that had not been 999 * removed in bcm_notify(). op->rx_reg_dev keeps 1000 * the device's receive lists accessible. 1001 */ 1002 if (op->rx_reg_dev) 1003 bcm_rx_unreg(op->rx_reg_dev, op); 1004 } else 1005 can_rx_unregister(sock_net(op->sk), NULL, 1006 op->can_id, 1007 REGMASK(op->can_id), 1008 bcm_rx_handler, op); 1009 1010 list_del_rcu(&op->list); 1011 bcm_remove_op(op); 1012 return 1; /* done */ 1013 } 1014 } 1015 1016 return 0; /* not found */ 1017 } 1018 1019 /* 1020 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops) 1021 */ 1022 static int bcm_delete_tx_op(struct list_head *ops, struct bcm_msg_head *mh, 1023 int ifindex) 1024 { 1025 struct bcm_op *op, *n; 1026 1027 list_for_each_entry_safe(op, n, ops, list) { 1028 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) && 1029 (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) { 1030 list_del_rcu(&op->list); 1031 bcm_remove_op(op); 1032 return 1; /* done */ 1033 } 1034 } 1035 1036 return 0; /* not found */ 1037 } 1038 1039 /* 1040 * bcm_read_op - read out a bcm_op and send it to the user (for bcm_sendmsg) 1041 */ 1042 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head, 1043 int ifindex) 1044 { 1045 struct bcm_op *op = bcm_find_op(ops, msg_head, ifindex); 1046 1047 if (!op) 1048 return -EINVAL; 1049 1050 /* put current values into msg_head */ 1051 msg_head->flags = op->flags; 1052 msg_head->count = op->count; 1053 msg_head->ival1 = op->ival1; 1054 msg_head->ival2 = op->ival2; 1055 msg_head->nframes = op->nframes; 1056 1057 bcm_send_to_user(op, msg_head, op->frames, 0); 1058 1059 return MHSIZ; 1060 } 1061 1062 /* 1063 * bcm_tx_setup - create or update a bcm tx op (for bcm_sendmsg) 1064 */ 1065 static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg, 1066 int ifindex, struct sock *sk) 1067 { 1068 struct bcm_sock *bo = bcm_sk(sk); 1069 struct bcm_op *op; 1070 struct canfd_frame *cf; 1071 bool add_op_to_list = false; 1072 unsigned int i; 1073 int err; 1074 1075 /* we need a real device to send frames */ 1076 if (!ifindex) 1077 return -ENODEV; 1078 1079 /* check nframes boundaries - we need at least one CAN frame */ 1080 if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES) 1081 return -EINVAL; 1082 1083 /* check timeval limitations */ 1084 if ((msg_head->flags & SETTIMER) && bcm_is_invalid_tv(msg_head)) 1085 return -EINVAL; 1086 1087 /* check the given can_id */ 1088 op = bcm_find_op(&bo->tx_ops, msg_head, ifindex); 1089 if (op) { 1090 void *new_frames; 1091 1092 /* update existing BCM operation */ 1093 1094 /* 1095 * Do we need more space for the CAN frames than currently 1096 * allocated? -> This is a _really_ unusual use-case and 1097 * therefore (complexity / locking) it is not supported. 1098 */ 1099 if (msg_head->nframes > op->nframes) 1100 return -E2BIG; 1101 1102 /* get new CAN frames content into a staging buffer before 1103 * locking: validate and normalize the frames there so that 1104 * bcm_can_tx() / bcm_tx_timeout_handler() never observe a 1105 * partially updated or unvalidated frame in op->frames 1106 */ 1107 new_frames = kmalloc(msg_head->nframes * op->cfsiz, GFP_KERNEL); 1108 if (!new_frames) 1109 return -ENOMEM; 1110 1111 for (i = 0; i < msg_head->nframes; i++) { 1112 1113 cf = new_frames + op->cfsiz * i; 1114 err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz); 1115 if (err < 0) { 1116 kfree(new_frames); 1117 return err; 1118 } 1119 1120 if (op->flags & CAN_FD_FRAME) { 1121 if (cf->len > 64) 1122 err = -EINVAL; 1123 } else { 1124 if (cf->len > 8) 1125 err = -EINVAL; 1126 } 1127 1128 if (err < 0) { 1129 kfree(new_frames); 1130 return err; 1131 } 1132 1133 if (msg_head->flags & TX_CP_CAN_ID) { 1134 /* copy can_id into frame */ 1135 cf->can_id = msg_head->can_id; 1136 } 1137 } 1138 1139 spin_lock_bh(&op->bcm_tx_lock); 1140 1141 /* update CAN frames content */ 1142 memcpy(op->frames, new_frames, msg_head->nframes * op->cfsiz); 1143 1144 op->flags = msg_head->flags; 1145 1146 if (op->nframes != msg_head->nframes || 1147 op->flags & TX_RESET_MULTI_IDX) { 1148 /* potentially update changed nframes */ 1149 op->nframes = msg_head->nframes; 1150 /* restart multiple frame transmission */ 1151 op->currframe = 0; 1152 } 1153 1154 if (op->flags & SETTIMER) 1155 op->count = msg_head->count; 1156 1157 spin_unlock_bh(&op->bcm_tx_lock); 1158 1159 kfree(new_frames); 1160 1161 } else { 1162 /* insert new BCM operation for the given can_id */ 1163 1164 op = kzalloc(OPSIZ, GFP_KERNEL); 1165 if (!op) 1166 return -ENOMEM; 1167 1168 spin_lock_init(&op->bcm_tx_lock); 1169 op->can_id = msg_head->can_id; 1170 op->cfsiz = CFSIZ(msg_head->flags); 1171 op->flags = msg_head->flags; 1172 op->nframes = msg_head->nframes; 1173 1174 if (op->flags & SETTIMER) 1175 op->count = msg_head->count; 1176 1177 /* create array for CAN frames and copy the data */ 1178 if (msg_head->nframes > 1) { 1179 op->frames = kmalloc_array(msg_head->nframes, 1180 op->cfsiz, 1181 GFP_KERNEL); 1182 if (!op->frames) { 1183 kfree(op); 1184 return -ENOMEM; 1185 } 1186 } else 1187 op->frames = &op->sframe; 1188 1189 for (i = 0; i < msg_head->nframes; i++) { 1190 1191 cf = op->frames + op->cfsiz * i; 1192 err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz); 1193 if (err < 0) 1194 goto free_op; 1195 1196 if (op->flags & CAN_FD_FRAME) { 1197 if (cf->len > 64) 1198 err = -EINVAL; 1199 } else { 1200 if (cf->len > 8) 1201 err = -EINVAL; 1202 } 1203 1204 if (err < 0) 1205 goto free_op; 1206 1207 if (msg_head->flags & TX_CP_CAN_ID) { 1208 /* copy can_id into frame */ 1209 cf->can_id = msg_head->can_id; 1210 } 1211 } 1212 1213 /* tx_ops never compare with previous received messages */ 1214 op->last_frames = NULL; 1215 1216 /* bcm_can_tx / bcm_tx_timeout_handler needs this */ 1217 op->sk = sk; 1218 sock_hold(sk); 1219 op->ifindex = ifindex; 1220 1221 /* initialize uninitialized (kzalloc) structure */ 1222 hrtimer_setup(&op->timer, bcm_tx_timeout_handler, CLOCK_MONOTONIC, 1223 HRTIMER_MODE_REL_SOFT); 1224 1225 /* currently unused in tx_ops */ 1226 hrtimer_setup(&op->thrtimer, hrtimer_dummy_timeout, CLOCK_MONOTONIC, 1227 HRTIMER_MODE_REL_SOFT); 1228 1229 add_op_to_list = true; 1230 1231 } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */ 1232 1233 if (op->flags & SETTIMER) { 1234 /* set timer values */ 1235 spin_lock_bh(&op->bcm_tx_lock); 1236 op->ival1 = msg_head->ival1; 1237 op->ival2 = msg_head->ival2; 1238 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1); 1239 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2); 1240 spin_unlock_bh(&op->bcm_tx_lock); 1241 1242 /* disable an active timer due to zero values? */ 1243 if (!op->kt_ival1 && !op->kt_ival2) 1244 hrtimer_cancel(&op->timer); 1245 } 1246 1247 if (op->flags & STARTTIMER) { 1248 hrtimer_cancel(&op->timer); 1249 /* spec: send CAN frame when starting timer */ 1250 op->flags |= TX_ANNOUNCE; 1251 } 1252 1253 /* add this bcm_op to the list of the tx_ops? */ 1254 if (add_op_to_list) 1255 list_add_rcu(&op->list, &bo->tx_ops); 1256 1257 if (op->flags & TX_ANNOUNCE) 1258 bcm_can_tx(op, NULL); 1259 1260 if (op->flags & STARTTIMER) 1261 bcm_tx_start_timer(op); 1262 1263 return msg_head->nframes * op->cfsiz + MHSIZ; 1264 1265 free_op: 1266 if (op->frames != &op->sframe) 1267 kfree(op->frames); 1268 kfree(op); 1269 return err; 1270 } 1271 1272 static int bcm_rx_setup_rtr_check(struct bcm_msg_head *msg_head, 1273 struct bcm_op *op, void *new_frames) 1274 { 1275 struct canfd_frame *frame0 = new_frames; 1276 1277 if (!(msg_head->flags & RX_RTR_FRAME)) 1278 return 0; 1279 1280 /* this frame is sent out as-is by bcm_can_tx() whenever a matching 1281 * remote request is received, so validate its length the same way 1282 * bcm_tx_setup() validates TX_SETUP frames before installing it 1283 */ 1284 if (msg_head->flags & CAN_FD_FRAME) { 1285 if (frame0->len > 64) 1286 return -EINVAL; 1287 } else { 1288 if (frame0->len > 8) 1289 return -EINVAL; 1290 } 1291 1292 /* funny feature in RX(!)_SETUP only for RTR-mode: 1293 * copy can_id into frame BUT without RTR-flag to 1294 * prevent a full-load-loopback-test ... ;-] 1295 * normalize this on the staged buffer, before it is 1296 * ever installed into op->frames. 1297 */ 1298 if ((msg_head->flags & TX_CP_CAN_ID) || 1299 frame0->can_id == op->can_id) 1300 frame0->can_id = op->can_id & ~CAN_RTR_FLAG; 1301 1302 return 0; 1303 } 1304 1305 /* 1306 * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg) 1307 */ 1308 static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg, 1309 int ifindex, struct sock *sk) 1310 { 1311 struct bcm_sock *bo = bcm_sk(sk); 1312 struct bcm_op *op; 1313 int do_rx_register; 1314 int new_op = 0; 1315 int err = 0; 1316 1317 if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) { 1318 /* be robust against wrong usage ... */ 1319 msg_head->flags |= RX_FILTER_ID; 1320 /* ignore trailing garbage */ 1321 msg_head->nframes = 0; 1322 } 1323 1324 /* the first element contains the mux-mask => MAX_NFRAMES + 1 */ 1325 if (msg_head->nframes > MAX_NFRAMES + 1) 1326 return -EINVAL; 1327 1328 if ((msg_head->flags & RX_RTR_FRAME) && 1329 ((msg_head->nframes != 1) || 1330 (!(msg_head->can_id & CAN_RTR_FLAG)))) 1331 return -EINVAL; 1332 1333 /* check timeval limitations */ 1334 if ((msg_head->flags & SETTIMER) && bcm_is_invalid_tv(msg_head)) 1335 return -EINVAL; 1336 1337 /* check the given can_id */ 1338 op = bcm_find_op(&bo->rx_ops, msg_head, ifindex); 1339 if (op) { 1340 void *new_frames = NULL; 1341 1342 /* update existing BCM operation */ 1343 1344 /* 1345 * Do we need more space for the CAN frames than currently 1346 * allocated? -> This is a _really_ unusual use-case and 1347 * therefore (complexity / locking) it is not supported. 1348 */ 1349 if (msg_head->nframes > op->nframes) 1350 return -E2BIG; 1351 1352 if (msg_head->nframes) { 1353 /* get new CAN frames content before locking */ 1354 new_frames = kmalloc(msg_head->nframes * op->cfsiz, 1355 GFP_KERNEL); 1356 if (!new_frames) 1357 return -ENOMEM; 1358 1359 err = memcpy_from_msg(new_frames, msg, 1360 msg_head->nframes * op->cfsiz); 1361 if (err < 0) { 1362 kfree(new_frames); 1363 return err; 1364 } 1365 1366 err = bcm_rx_setup_rtr_check(msg_head, op, new_frames); 1367 if (err < 0) { 1368 kfree(new_frames); 1369 return err; 1370 } 1371 } 1372 1373 spin_lock_bh(&op->bcm_rx_update_lock); 1374 op->nframes = msg_head->nframes; 1375 op->flags = msg_head->flags; 1376 1377 if (msg_head->nframes) { 1378 /* update CAN frames content */ 1379 memcpy(op->frames, new_frames, 1380 msg_head->nframes * op->cfsiz); 1381 1382 /* clear last_frames to indicate 'nothing received' */ 1383 memset(op->last_frames, 0, 1384 msg_head->nframes * op->cfsiz); 1385 } 1386 1387 if (msg_head->flags & SETTIMER) { 1388 op->ival1 = msg_head->ival1; 1389 op->ival2 = msg_head->ival2; 1390 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1); 1391 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2); 1392 op->kt_lastmsg = 0; 1393 op->if_detected = 0; /* reclaim ifindex in ANYDEV mode */ 1394 } 1395 spin_unlock_bh(&op->bcm_rx_update_lock); 1396 1397 /* free temporary frames / kfree(NULL) is safe */ 1398 kfree(new_frames); 1399 1400 /* Don't register a new CAN filter for the rx_op update unless 1401 * a concurrent NETDEV_UNREGISTER notifier already tore down 1402 * the previous registration. In this case the receiver needs 1403 * to be re-registered here so that this update doesn't 1404 * silently stop delivering frames for the given ifindex. 1405 * Ops with ifindex = 0 (all CAN interfaces) never carry a 1406 * tracked rx_reg_dev and stay registered as-is. 1407 */ 1408 do_rx_register = (ifindex && !op->rx_reg_dev) ? 1 : 0; 1409 1410 } else { 1411 /* insert new BCM operation for the given can_id */ 1412 op = kzalloc(OPSIZ, GFP_KERNEL); 1413 if (!op) 1414 return -ENOMEM; 1415 1416 spin_lock_init(&op->bcm_tx_lock); 1417 spin_lock_init(&op->bcm_rx_update_lock); 1418 op->can_id = msg_head->can_id; 1419 op->nframes = msg_head->nframes; 1420 op->cfsiz = CFSIZ(msg_head->flags); 1421 op->flags = msg_head->flags; 1422 1423 if (msg_head->nframes > 1) { 1424 /* create array for CAN frames and copy the data */ 1425 op->frames = kmalloc_array(msg_head->nframes, 1426 op->cfsiz, 1427 GFP_KERNEL); 1428 if (!op->frames) { 1429 kfree(op); 1430 return -ENOMEM; 1431 } 1432 1433 /* create and init array for received CAN frames */ 1434 op->last_frames = kcalloc(msg_head->nframes, 1435 op->cfsiz, 1436 GFP_KERNEL); 1437 if (!op->last_frames) { 1438 kfree(op->frames); 1439 kfree(op); 1440 return -ENOMEM; 1441 } 1442 1443 } else { 1444 op->frames = &op->sframe; 1445 op->last_frames = &op->last_sframe; 1446 } 1447 1448 if (msg_head->nframes) { 1449 err = memcpy_from_msg(op->frames, msg, 1450 msg_head->nframes * op->cfsiz); 1451 if (err < 0) 1452 goto free_op; 1453 1454 err = bcm_rx_setup_rtr_check(msg_head, op, op->frames); 1455 if (err < 0) 1456 goto free_op; 1457 } 1458 1459 /* bcm_can_tx / bcm_tx_timeout_handler needs this */ 1460 op->sk = sk; 1461 sock_hold(sk); 1462 op->ifindex = ifindex; 1463 1464 /* ifindex for timeout events w/o previous frame reception */ 1465 op->rx_ifindex = ifindex; 1466 1467 /* initialize uninitialized (kzalloc) structure */ 1468 hrtimer_setup(&op->timer, bcm_rx_timeout_handler, CLOCK_MONOTONIC, 1469 HRTIMER_MODE_REL_SOFT); 1470 hrtimer_setup(&op->thrtimer, bcm_rx_thr_handler, CLOCK_MONOTONIC, 1471 HRTIMER_MODE_REL_SOFT); 1472 1473 /* call can_rx_register() */ 1474 do_rx_register = 1; 1475 new_op = 1; 1476 1477 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */ 1478 1479 /* check flags */ 1480 1481 if (op->flags & RX_RTR_FRAME) { 1482 /* no timers in RTR-mode */ 1483 hrtimer_cancel(&op->thrtimer); 1484 hrtimer_cancel(&op->timer); 1485 } else { 1486 if (op->flags & SETTIMER) { 1487 1488 /* set timers (locked) for newly created op */ 1489 if (new_op) { 1490 spin_lock_bh(&op->bcm_rx_update_lock); 1491 op->ival1 = msg_head->ival1; 1492 op->ival2 = msg_head->ival2; 1493 op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1); 1494 op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2); 1495 op->kt_lastmsg = 0; 1496 spin_unlock_bh(&op->bcm_rx_update_lock); 1497 } 1498 1499 /* disable an active timer due to zero value? */ 1500 if (!op->kt_ival1) 1501 hrtimer_cancel(&op->timer); 1502 1503 /* 1504 * In any case cancel the throttle timer, flush 1505 * potentially blocked msgs and reset throttle handling 1506 */ 1507 hrtimer_cancel(&op->thrtimer); 1508 1509 spin_lock_bh(&op->bcm_rx_update_lock); 1510 bcm_rx_thr_flush(op); 1511 spin_unlock_bh(&op->bcm_rx_update_lock); 1512 } 1513 1514 if ((op->flags & STARTTIMER) && op->kt_ival1) 1515 hrtimer_start(&op->timer, op->kt_ival1, 1516 HRTIMER_MODE_REL_SOFT); 1517 } 1518 1519 /* now we can register for can_ids, if we added a new bcm_op 1520 * or need to re-register after a NETDEV_UNREGISTER tore down 1521 * the previous registration of an existing op 1522 */ 1523 if (do_rx_register) { 1524 if (ifindex) { 1525 struct net_device *dev; 1526 1527 dev = dev_get_by_index(sock_net(sk), ifindex); 1528 if (dev) { 1529 err = can_rx_register(sock_net(sk), dev, 1530 op->can_id, 1531 REGMASK(op->can_id), 1532 bcm_rx_handler, op, 1533 sock_i_ino(sk), sk); 1534 1535 /* keep a tracked reference so that a later 1536 * unregister can safely reach the device even 1537 * if a concurrent NETDEV_UNREGISTER has 1538 * already unlisted it by ifindex 1539 */ 1540 if (!err) { 1541 op->rx_reg_dev = dev; 1542 netdev_hold(dev, 1543 &op->rx_reg_dev_tracker, 1544 GFP_KERNEL); 1545 } 1546 dev_put(dev); 1547 } else { 1548 /* the requested device is gone - do not 1549 * silently succeed without registering 1550 */ 1551 err = -ENODEV; 1552 } 1553 1554 } else { 1555 err = can_rx_register(sock_net(sk), NULL, op->can_id, 1556 REGMASK(op->can_id), 1557 bcm_rx_handler, op, 1558 sock_i_ino(sk), sk); 1559 } 1560 1561 if (err) { 1562 /* newly created bcm rx op is broken -> remove it */ 1563 if (new_op) { 1564 bcm_remove_op(op); 1565 return err; 1566 } 1567 1568 /* an existing op just stays unregistered. 1569 * Cancel op->timer and (defensively) op->thrtimer. 1570 * Other settings can't be reached until the next 1571 * successful RX_SETUP. 1572 */ 1573 hrtimer_cancel(&op->timer); 1574 hrtimer_cancel(&op->thrtimer); 1575 return err; 1576 } 1577 1578 /* add a new bcm_op to the list of the rx_ops */ 1579 if (new_op) 1580 list_add_rcu(&op->list, &bo->rx_ops); 1581 } 1582 1583 return msg_head->nframes * op->cfsiz + MHSIZ; 1584 1585 free_op: 1586 if (op->frames != &op->sframe) 1587 kfree(op->frames); 1588 if (op->last_frames != &op->last_sframe) 1589 kfree(op->last_frames); 1590 kfree(op); 1591 return err; 1592 } 1593 1594 /* 1595 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg) 1596 */ 1597 static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk, 1598 int cfsiz) 1599 { 1600 struct sk_buff *skb; 1601 struct can_skb_ext *csx; 1602 struct net_device *dev; 1603 int err; 1604 1605 /* we need a real device to send frames */ 1606 if (!ifindex) 1607 return -ENODEV; 1608 1609 skb = alloc_skb(cfsiz, GFP_KERNEL); 1610 if (!skb) 1611 return -ENOMEM; 1612 1613 csx = can_skb_ext_add(skb); 1614 if (!csx) { 1615 kfree_skb(skb); 1616 return -ENOMEM; 1617 } 1618 1619 err = memcpy_from_msg(skb_put(skb, cfsiz), msg, cfsiz); 1620 if (err < 0) { 1621 kfree_skb(skb); 1622 return err; 1623 } 1624 1625 dev = dev_get_by_index(sock_net(sk), ifindex); 1626 if (!dev) { 1627 kfree_skb(skb); 1628 return -ENODEV; 1629 } 1630 1631 csx->can_iif = dev->ifindex; 1632 skb->dev = dev; 1633 can_skb_set_owner(skb, sk); 1634 err = can_send(skb, 1); /* send with loopback */ 1635 dev_put(dev); 1636 1637 if (err) 1638 return err; 1639 1640 return cfsiz + MHSIZ; 1641 } 1642 1643 /* 1644 * bcm_sendmsg - process BCM commands (opcodes) from the userspace 1645 */ 1646 static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) 1647 { 1648 struct sock *sk = sock->sk; 1649 struct bcm_sock *bo = bcm_sk(sk); 1650 int ifindex; 1651 struct bcm_msg_head msg_head; 1652 struct msghdr fmsg = {}; /* CAN frames copied to kernel memory */ 1653 struct kvec kv; 1654 struct canfd_frame sframe; /* stack buffer for up to CANFD_MTU bytes */ 1655 void *frames = &sframe; 1656 size_t flen = 0; 1657 int cfsiz; 1658 int ret; /* read bytes or error codes as return value */ 1659 1660 /* Lockless fast-path check for bound socket */ 1661 if (!READ_ONCE(bo->bound)) 1662 return -ENOTCONN; 1663 1664 /* check for valid message length from userspace */ 1665 if (size < MHSIZ) 1666 return -EINVAL; 1667 1668 /* read message head information */ 1669 ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ); 1670 if (ret < 0) 1671 return ret; 1672 1673 cfsiz = CFSIZ(msg_head.flags); 1674 if ((size - MHSIZ) % cfsiz) 1675 return -EINVAL; 1676 1677 /* Copy the CAN frames from user space before taking bo->conf_lock, so a 1678 * stalled copy can not block bcm_notify() waiting for bo->conf_lock 1679 * under RTNL. Only the frames the opcode handler reads are copied. 1680 * The handlers keep their checks and read the frames from fmsg. 1681 */ 1682 switch (msg_head.opcode) { 1683 case TX_SETUP: 1684 if (msg_head.nframes >= 1 && msg_head.nframes <= MAX_NFRAMES) 1685 flen = msg_head.nframes * cfsiz; 1686 break; 1687 1688 case RX_SETUP: 1689 if (!(msg_head.flags & RX_FILTER_ID) && 1690 msg_head.nframes <= MAX_NFRAMES + 1) 1691 flen = msg_head.nframes * cfsiz; 1692 break; 1693 1694 case TX_SEND: 1695 if (msg_head.nframes == 1 && size == cfsiz + MHSIZ) 1696 flen = cfsiz; 1697 break; 1698 } 1699 1700 /* a message with missing frames still fails in the handler */ 1701 flen = min(flen, msg_data_left(msg)); 1702 1703 if (flen > sizeof(sframe)) { 1704 frames = kvmalloc(flen, GFP_KERNEL); 1705 if (!frames) 1706 return -ENOMEM; 1707 } 1708 1709 if (!copy_from_iter_full(frames, flen, &msg->msg_iter)) { 1710 ret = -EFAULT; 1711 goto out_free; 1712 } 1713 1714 kv.iov_base = frames; 1715 kv.iov_len = flen; 1716 iov_iter_kvec(&fmsg.msg_iter, ITER_SOURCE, &kv, 1, flen); 1717 1718 mutex_lock(&bo->conf_lock); 1719 1720 /* Re-validate under bo->conf_lock: a concurrent bcm_notify() 1721 * may have unbound this socket (device removal) after the 1722 * lockless fast-path check above. bo->ifindex is only ever 1723 * mutated under bo->conf_lock, so reading it here - instead of 1724 * before taking the lock - guarantees it can't be observed 1725 * torn against bo->bound. 1726 */ 1727 if (!bo->bound) { 1728 ret = -ENOTCONN; 1729 goto out_release; 1730 } 1731 1732 /* default ifindex for this bcm_op */ 1733 ifindex = bo->ifindex; 1734 1735 /* check for alternative ifindex for this bcm_op */ 1736 1737 if (!ifindex && msg->msg_name) { 1738 /* no bound device as default => check msg_name */ 1739 DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name); 1740 1741 if (msg->msg_namelen < BCM_MIN_NAMELEN) { 1742 ret = -EINVAL; 1743 goto out_release; 1744 } 1745 1746 if (addr->can_family != AF_CAN) { 1747 ret = -EINVAL; 1748 goto out_release; 1749 } 1750 1751 /* ifindex from sendto() */ 1752 ifindex = addr->can_ifindex; 1753 1754 if (ifindex) { 1755 struct net_device *dev; 1756 1757 dev = dev_get_by_index(sock_net(sk), ifindex); 1758 if (!dev) { 1759 ret = -ENODEV; 1760 goto out_release; 1761 } 1762 1763 if (!can_get_ml_priv(dev)) { 1764 dev_put(dev); 1765 ret = -ENODEV; 1766 goto out_release; 1767 } 1768 1769 dev_put(dev); 1770 } 1771 } 1772 1773 switch (msg_head.opcode) { 1774 1775 case TX_SETUP: 1776 ret = bcm_tx_setup(&msg_head, &fmsg, ifindex, sk); 1777 break; 1778 1779 case RX_SETUP: 1780 ret = bcm_rx_setup(&msg_head, &fmsg, ifindex, sk); 1781 break; 1782 1783 case TX_DELETE: 1784 if (bcm_delete_tx_op(&bo->tx_ops, &msg_head, ifindex)) 1785 ret = MHSIZ; 1786 else 1787 ret = -EINVAL; 1788 break; 1789 1790 case RX_DELETE: 1791 if (bcm_delete_rx_op(&bo->rx_ops, &msg_head, ifindex)) 1792 ret = MHSIZ; 1793 else 1794 ret = -EINVAL; 1795 break; 1796 1797 case TX_READ: 1798 /* reuse msg_head for the reply to TX_READ */ 1799 msg_head.opcode = TX_STATUS; 1800 ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex); 1801 break; 1802 1803 case RX_READ: 1804 /* reuse msg_head for the reply to RX_READ */ 1805 msg_head.opcode = RX_STATUS; 1806 ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex); 1807 break; 1808 1809 case TX_SEND: 1810 /* we need exactly one CAN frame behind the msg head */ 1811 if ((msg_head.nframes != 1) || (size != cfsiz + MHSIZ)) 1812 ret = -EINVAL; 1813 else 1814 ret = bcm_tx_send(&fmsg, ifindex, sk, cfsiz); 1815 break; 1816 1817 default: 1818 ret = -EINVAL; 1819 break; 1820 } 1821 1822 out_release: 1823 mutex_unlock(&bo->conf_lock); 1824 out_free: 1825 if (frames != &sframe) 1826 kvfree(frames); 1827 1828 return ret; 1829 } 1830 1831 /* 1832 * notification handler for netdevice status changes 1833 */ 1834 static void bcm_notify(struct bcm_sock *bo, unsigned long msg, 1835 struct net_device *dev) 1836 { 1837 struct sock *sk = &bo->sk; 1838 struct bcm_op *op; 1839 int notify_enodev = 0; 1840 1841 if (!net_eq(dev_net(dev), sock_net(sk))) 1842 return; 1843 1844 switch (msg) { 1845 1846 case NETDEV_UNREGISTER: 1847 mutex_lock(&bo->conf_lock); 1848 1849 /* rx_ops: remove device specific receive entries */ 1850 list_for_each_entry(op, &bo->rx_ops, list) { 1851 if (op->rx_reg_dev == dev) 1852 bcm_rx_unreg(dev, op); 1853 1854 /* release an ANYDEV op's claim (see bcm_rx_handler()) 1855 * on this now confirmed-gone interface. 1856 */ 1857 if (!op->ifindex) { 1858 spin_lock_bh(&op->bcm_rx_update_lock); 1859 if (op->if_detected == dev->ifindex) 1860 op->if_detected = 0; 1861 spin_unlock_bh(&op->bcm_rx_update_lock); 1862 } 1863 } 1864 1865 /* tx_ops: stop device specific cyclic transmissions on the 1866 * vanishing ifindex. Cancelling the timer is enough to stop 1867 * cyclic bcm_can_tx() calls as there is no re-arming. 1868 */ 1869 list_for_each_entry(op, &bo->tx_ops, list) 1870 if (op->ifindex == dev->ifindex) 1871 hrtimer_cancel(&op->timer); 1872 1873 /* remove device reference, if this is our bound device */ 1874 if (bo->bound && bo->ifindex == dev->ifindex) { 1875 /* pairs with the lockless check in bcm_sendmsg() */ 1876 WRITE_ONCE(bo->bound, 0); 1877 WRITE_ONCE(bo->ifindex, 0); 1878 notify_enodev = 1; 1879 } 1880 1881 mutex_unlock(&bo->conf_lock); 1882 1883 if (notify_enodev) { 1884 sk->sk_err = ENODEV; 1885 if (!sock_flag(sk, SOCK_DEAD)) 1886 sk_error_report(sk); 1887 } 1888 break; 1889 1890 case NETDEV_DOWN: 1891 if (READ_ONCE(bo->bound) && 1892 READ_ONCE(bo->ifindex) == dev->ifindex) { 1893 sk->sk_err = ENETDOWN; 1894 if (!sock_flag(sk, SOCK_DEAD)) 1895 sk_error_report(sk); 1896 } 1897 } 1898 } 1899 1900 static int bcm_notifier(struct notifier_block *nb, unsigned long msg, 1901 void *ptr) 1902 { 1903 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1904 1905 if (!can_get_ml_priv(dev)) 1906 return NOTIFY_DONE; 1907 if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN) 1908 return NOTIFY_DONE; 1909 if (unlikely(bcm_busy_notifier)) /* Check for reentrant bug. */ 1910 return NOTIFY_DONE; 1911 1912 spin_lock(&bcm_notifier_lock); 1913 list_for_each_entry(bcm_busy_notifier, &bcm_notifier_list, notifier) { 1914 spin_unlock(&bcm_notifier_lock); 1915 bcm_notify(bcm_busy_notifier, msg, dev); 1916 spin_lock(&bcm_notifier_lock); 1917 } 1918 bcm_busy_notifier = NULL; 1919 spin_unlock(&bcm_notifier_lock); 1920 return NOTIFY_DONE; 1921 } 1922 1923 /* 1924 * initial settings for all BCM sockets to be set at socket creation time 1925 */ 1926 static int bcm_init(struct sock *sk) 1927 { 1928 struct bcm_sock *bo = bcm_sk(sk); 1929 1930 bo->bound = 0; 1931 bo->ifindex = 0; 1932 bo->dropped_usr_msgs = 0; 1933 bo->bcm_proc_read = NULL; 1934 mutex_init(&bo->conf_lock); 1935 1936 INIT_LIST_HEAD(&bo->tx_ops); 1937 INIT_LIST_HEAD(&bo->rx_ops); 1938 1939 /* set notifier */ 1940 spin_lock(&bcm_notifier_lock); 1941 list_add_tail(&bo->notifier, &bcm_notifier_list); 1942 spin_unlock(&bcm_notifier_lock); 1943 1944 return 0; 1945 } 1946 1947 /* 1948 * standard socket functions 1949 */ 1950 static int bcm_release(struct socket *sock) 1951 { 1952 struct sock *sk = sock->sk; 1953 struct net *net; 1954 struct bcm_sock *bo; 1955 struct bcm_op *op, *next; 1956 1957 if (!sk) 1958 return 0; 1959 1960 net = sock_net(sk); 1961 bo = bcm_sk(sk); 1962 1963 /* remove bcm_ops, timer, rx_unregister(), etc. */ 1964 1965 spin_lock(&bcm_notifier_lock); 1966 while (bcm_busy_notifier == bo) { 1967 spin_unlock(&bcm_notifier_lock); 1968 schedule_timeout_uninterruptible(1); 1969 spin_lock(&bcm_notifier_lock); 1970 } 1971 list_del(&bo->notifier); 1972 spin_unlock(&bcm_notifier_lock); 1973 1974 #if IS_ENABLED(CONFIG_PROC_FS) 1975 /* remove procfs entry */ 1976 if (net->can.bcmproc_dir && bo->bcm_proc_read) 1977 remove_proc_entry(bo->procname, net->can.bcmproc_dir); 1978 #endif /* CONFIG_PROC_FS */ 1979 1980 mutex_lock(&bo->conf_lock); 1981 1982 list_for_each_entry_safe(op, next, &bo->tx_ops, list) { 1983 list_del_rcu(&op->list); 1984 bcm_remove_op(op); 1985 } 1986 1987 list_for_each_entry(op, &bo->rx_ops, list) { 1988 if (op->ifindex) { 1989 /* Only remove subscriptions that had not been removed 1990 * in bcm_notify(). op->rx_reg_dev keeps the device's 1991 * receive lists accessible. 1992 */ 1993 if (op->rx_reg_dev) 1994 bcm_rx_unreg(op->rx_reg_dev, op); 1995 } else { 1996 can_rx_unregister(net, NULL, op->can_id, 1997 REGMASK(op->can_id), 1998 bcm_rx_handler, op); 1999 } 2000 cond_resched(); 2001 } 2002 2003 synchronize_rcu(); 2004 2005 list_for_each_entry_safe(op, next, &bo->rx_ops, list) { 2006 list_del_rcu(&op->list); 2007 bcm_remove_op(op); 2008 } 2009 2010 /* remove device reference */ 2011 if (bo->bound) { 2012 WRITE_ONCE(bo->bound, 0); 2013 WRITE_ONCE(bo->ifindex, 0); 2014 } 2015 2016 mutex_unlock(&bo->conf_lock); 2017 2018 sock_orphan(sk); 2019 sock->sk = NULL; 2020 2021 sock_prot_inuse_add(net, sk->sk_prot, -1); 2022 sock_put(sk); 2023 2024 return 0; 2025 } 2026 2027 static int bcm_connect(struct socket *sock, struct sockaddr_unsized *uaddr, int len, 2028 int flags) 2029 { 2030 struct sockaddr_can *addr = (struct sockaddr_can *)uaddr; 2031 struct sock *sk = sock->sk; 2032 struct bcm_sock *bo = bcm_sk(sk); 2033 struct net *net = sock_net(sk); 2034 int ret = 0; 2035 2036 if (len < BCM_MIN_NAMELEN) 2037 return -EINVAL; 2038 2039 mutex_lock(&bo->conf_lock); 2040 2041 if (bo->bound) { 2042 ret = -EISCONN; 2043 goto fail; 2044 } 2045 2046 /* bind a device to this socket */ 2047 if (addr->can_ifindex) { 2048 struct net_device *dev; 2049 2050 dev = dev_get_by_index(net, addr->can_ifindex); 2051 if (!dev) { 2052 ret = -ENODEV; 2053 goto fail; 2054 } 2055 if (!can_get_ml_priv(dev)) { 2056 dev_put(dev); 2057 ret = -ENODEV; 2058 goto fail; 2059 } 2060 2061 WRITE_ONCE(bo->ifindex, dev->ifindex); 2062 dev_put(dev); 2063 2064 } else { 2065 /* no interface reference for ifindex = 0 ('any' CAN device) */ 2066 WRITE_ONCE(bo->ifindex, 0); 2067 } 2068 2069 #if IS_ENABLED(CONFIG_PROC_FS) 2070 /* create the procfs entry once, it is removed in bcm_release() */ 2071 if (net->can.bcmproc_dir && !bo->bcm_proc_read) { 2072 /* use unique socket inode number as filename */ 2073 snprintf(bo->procname, sizeof(bo->procname), 2074 "%llu", sock_i_ino(sk)); 2075 bo->bcm_proc_read = proc_create_net_single(bo->procname, 0644, 2076 net->can.bcmproc_dir, 2077 bcm_proc_show, sk); 2078 if (!bo->bcm_proc_read) { 2079 ret = -ENOMEM; 2080 goto fail; 2081 } 2082 } 2083 #endif /* CONFIG_PROC_FS */ 2084 2085 /* bo->ifindex above is fully assigned before this point; pairs 2086 * with the lockless fast-path check in bcm_sendmsg() 2087 */ 2088 WRITE_ONCE(bo->bound, 1); 2089 2090 fail: 2091 mutex_unlock(&bo->conf_lock); 2092 2093 return ret; 2094 } 2095 2096 static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 2097 int flags) 2098 { 2099 struct sock *sk = sock->sk; 2100 struct sk_buff *skb; 2101 int error = 0; 2102 int err; 2103 2104 skb = skb_recv_datagram(sk, flags, &error); 2105 if (!skb) 2106 return error; 2107 2108 if (skb->len < size) 2109 size = skb->len; 2110 2111 err = memcpy_to_msg(msg, skb->data, size); 2112 if (err < 0) { 2113 skb_free_datagram(sk, skb); 2114 return err; 2115 } 2116 2117 sock_recv_cmsgs(msg, sk, skb); 2118 2119 if (msg->msg_name) { 2120 __sockaddr_check_size(BCM_MIN_NAMELEN); 2121 msg->msg_namelen = BCM_MIN_NAMELEN; 2122 memcpy(msg->msg_name, skb->cb, msg->msg_namelen); 2123 } 2124 2125 /* assign the flags that have been recorded in bcm_send_to_user() */ 2126 msg->msg_flags |= *(bcm_flags(skb)); 2127 2128 skb_free_datagram(sk, skb); 2129 2130 return size; 2131 } 2132 2133 static int bcm_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd, 2134 unsigned long arg) 2135 { 2136 /* no ioctls for socket layer -> hand it down to NIC layer */ 2137 return -ENOIOCTLCMD; 2138 } 2139 2140 static const struct proto_ops bcm_ops = { 2141 .family = PF_CAN, 2142 .release = bcm_release, 2143 .bind = sock_no_bind, 2144 .connect = bcm_connect, 2145 .socketpair = sock_no_socketpair, 2146 .accept = sock_no_accept, 2147 .getname = sock_no_getname, 2148 .poll = datagram_poll, 2149 .ioctl = bcm_sock_no_ioctlcmd, 2150 .gettstamp = sock_gettstamp, 2151 .listen = sock_no_listen, 2152 .shutdown = sock_no_shutdown, 2153 .sendmsg = bcm_sendmsg, 2154 .recvmsg = bcm_recvmsg, 2155 .mmap = sock_no_mmap, 2156 }; 2157 2158 static struct proto bcm_proto __read_mostly = { 2159 .name = "CAN_BCM", 2160 .owner = THIS_MODULE, 2161 .obj_size = sizeof(struct bcm_sock), 2162 .init = bcm_init, 2163 }; 2164 2165 static const struct can_proto bcm_can_proto = { 2166 .type = SOCK_DGRAM, 2167 .protocol = CAN_BCM, 2168 .ops = &bcm_ops, 2169 .prot = &bcm_proto, 2170 }; 2171 2172 static int canbcm_pernet_init(struct net *net) 2173 { 2174 #if IS_ENABLED(CONFIG_PROC_FS) 2175 /* create /proc/net/can-bcm directory */ 2176 net->can.bcmproc_dir = proc_net_mkdir(net, "can-bcm", net->proc_net); 2177 #endif /* CONFIG_PROC_FS */ 2178 2179 return 0; 2180 } 2181 2182 static void canbcm_pernet_exit(struct net *net) 2183 { 2184 #if IS_ENABLED(CONFIG_PROC_FS) 2185 /* remove /proc/net/can-bcm directory */ 2186 if (net->can.bcmproc_dir) 2187 remove_proc_entry("can-bcm", net->proc_net); 2188 #endif /* CONFIG_PROC_FS */ 2189 } 2190 2191 static struct pernet_operations canbcm_pernet_ops __read_mostly = { 2192 .init = canbcm_pernet_init, 2193 .exit = canbcm_pernet_exit, 2194 }; 2195 2196 static struct notifier_block canbcm_notifier = { 2197 .notifier_call = bcm_notifier 2198 }; 2199 2200 static int __init bcm_module_init(void) 2201 { 2202 int err; 2203 2204 bcm_wq = alloc_workqueue("can-bcm-wq", WQ_UNBOUND, 0); 2205 if (!bcm_wq) 2206 return -ENOMEM; 2207 2208 pr_info("can: broadcast manager protocol\n"); 2209 2210 err = register_pernet_subsys(&canbcm_pernet_ops); 2211 if (err) 2212 goto register_pernet_failed; 2213 2214 err = register_netdevice_notifier(&canbcm_notifier); 2215 if (err) 2216 goto register_notifier_failed; 2217 2218 err = can_proto_register(&bcm_can_proto); 2219 if (err < 0) { 2220 printk(KERN_ERR "can: registration of bcm protocol failed\n"); 2221 goto register_proto_failed; 2222 } 2223 2224 return 0; 2225 2226 register_proto_failed: 2227 unregister_netdevice_notifier(&canbcm_notifier); 2228 register_notifier_failed: 2229 unregister_pernet_subsys(&canbcm_pernet_ops); 2230 register_pernet_failed: 2231 destroy_workqueue(bcm_wq); 2232 return err; 2233 } 2234 2235 static void __exit bcm_module_exit(void) 2236 { 2237 can_proto_unregister(&bcm_can_proto); 2238 unregister_netdevice_notifier(&canbcm_notifier); 2239 unregister_pernet_subsys(&canbcm_pernet_ops); 2240 rcu_barrier(); 2241 destroy_workqueue(bcm_wq); 2242 } 2243 2244 module_init(bcm_module_init); 2245 module_exit(bcm_module_exit); 2246