Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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_array(msg_head->nframes, |
| 927 | op->cfsiz, |
| 928 | GFP_KERNEL); |
| 929 | if (!op->frames) { |
| 930 | kfree(op); |
| 931 | return -ENOMEM; |
| 932 | } |
| 933 | } else |
| 934 | op->frames = &op->sframe; |
| 935 | |
| 936 | for (i = 0; i < msg_head->nframes; i++) { |
| 937 | |
| 938 | cf = op->frames + op->cfsiz * i; |
| 939 | err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz); |
| 940 | |
| 941 | if (op->flags & CAN_FD_FRAME) { |
| 942 | if (cf->len > 64) |
| 943 | err = -EINVAL; |
| 944 | } else { |
| 945 | if (cf->len > 8) |
| 946 | err = -EINVAL; |
| 947 | } |
| 948 | |
| 949 | if (err < 0) { |
| 950 | if (op->frames != &op->sframe) |
| 951 | kfree(op->frames); |
| 952 | kfree(op); |
| 953 | return err; |
| 954 | } |
| 955 | |
| 956 | if (msg_head->flags & TX_CP_CAN_ID) { |
| 957 | /* copy can_id into frame */ |
| 958 | cf->can_id = msg_head->can_id; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | /* tx_ops never compare with previous received messages */ |
| 963 | op->last_frames = NULL; |
| 964 | |
| 965 | /* bcm_can_tx / bcm_tx_timeout_handler needs this */ |
| 966 | op->sk = sk; |
| 967 | op->ifindex = ifindex; |
| 968 | |
| 969 | /* initialize uninitialized (kzalloc) structure */ |
| 970 | hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 971 | op->timer.function = bcm_tx_timeout_handler; |
| 972 | |
| 973 | /* initialize tasklet for tx countevent notification */ |
| 974 | tasklet_init(&op->tsklet, bcm_tx_timeout_tsklet, |
| 975 | (unsigned long) op); |
| 976 | |
| 977 | /* currently unused in tx_ops */ |
| 978 | hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 979 | |
| 980 | /* add this bcm_op to the list of the tx_ops */ |
| 981 | list_add(&op->list, &bo->tx_ops); |
| 982 | |
| 983 | } /* if ((op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex))) */ |
| 984 | |
| 985 | if (op->nframes != msg_head->nframes) { |
| 986 | op->nframes = msg_head->nframes; |
| 987 | /* start multiple frame transmission with index 0 */ |
| 988 | op->currframe = 0; |
| 989 | } |
| 990 | |
| 991 | /* check flags */ |
| 992 | |
| 993 | if (op->flags & TX_RESET_MULTI_IDX) { |
| 994 | /* start multiple frame transmission with index 0 */ |
| 995 | op->currframe = 0; |
| 996 | } |
| 997 | |
| 998 | if (op->flags & SETTIMER) { |
| 999 | /* set timer values */ |
| 1000 | op->count = msg_head->count; |
| 1001 | op->ival1 = msg_head->ival1; |
| 1002 | op->ival2 = msg_head->ival2; |
| 1003 | op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1); |
| 1004 | op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2); |
| 1005 | |
| 1006 | /* disable an active timer due to zero values? */ |
| 1007 | if (!op->kt_ival1 && !op->kt_ival2) |
| 1008 | hrtimer_cancel(&op->timer); |
| 1009 | } |
| 1010 | |
| 1011 | if (op->flags & STARTTIMER) { |
| 1012 | hrtimer_cancel(&op->timer); |
| 1013 | /* spec: send CAN frame when starting timer */ |
| 1014 | op->flags |= TX_ANNOUNCE; |
| 1015 | } |
| 1016 | |
| 1017 | if (op->flags & TX_ANNOUNCE) { |
| 1018 | bcm_can_tx(op); |
| 1019 | if (op->count) |
| 1020 | op->count--; |
| 1021 | } |
| 1022 | |
| 1023 | if (op->flags & STARTTIMER) |
| 1024 | bcm_tx_start_timer(op); |
| 1025 | |
| 1026 | return msg_head->nframes * op->cfsiz + MHSIZ; |
| 1027 | } |
| 1028 | |
| 1029 | /* |
| 1030 | * bcm_rx_setup - create or update a bcm rx op (for bcm_sendmsg) |
| 1031 | */ |
| 1032 | static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg, |
| 1033 | int ifindex, struct sock *sk) |
| 1034 | { |
| 1035 | struct bcm_sock *bo = bcm_sk(sk); |
| 1036 | struct bcm_op *op; |
| 1037 | int do_rx_register; |
| 1038 | int err = 0; |
| 1039 | |
| 1040 | if ((msg_head->flags & RX_FILTER_ID) || (!(msg_head->nframes))) { |
| 1041 | /* be robust against wrong usage ... */ |
| 1042 | msg_head->flags |= RX_FILTER_ID; |
| 1043 | /* ignore trailing garbage */ |
| 1044 | msg_head->nframes = 0; |
| 1045 | } |
| 1046 | |
| 1047 | /* the first element contains the mux-mask => MAX_NFRAMES + 1 */ |
| 1048 | if (msg_head->nframes > MAX_NFRAMES + 1) |
| 1049 | return -EINVAL; |
| 1050 | |
| 1051 | if ((msg_head->flags & RX_RTR_FRAME) && |
| 1052 | ((msg_head->nframes != 1) || |
| 1053 | (!(msg_head->can_id & CAN_RTR_FLAG)))) |
| 1054 | return -EINVAL; |
| 1055 | |
| 1056 | /* check the given can_id */ |
| 1057 | op = bcm_find_op(&bo->rx_ops, msg_head, ifindex); |
| 1058 | if (op) { |
| 1059 | /* update existing BCM operation */ |
| 1060 | |
| 1061 | /* |
| 1062 | * Do we need more space for the CAN frames than currently |
| 1063 | * allocated? -> This is a _really_ unusual use-case and |
| 1064 | * therefore (complexity / locking) it is not supported. |
| 1065 | */ |
| 1066 | if (msg_head->nframes > op->nframes) |
| 1067 | return -E2BIG; |
| 1068 | |
| 1069 | if (msg_head->nframes) { |
| 1070 | /* update CAN frames content */ |
| 1071 | err = memcpy_from_msg(op->frames, msg, |
| 1072 | msg_head->nframes * op->cfsiz); |
| 1073 | if (err < 0) |
| 1074 | return err; |
| 1075 | |
| 1076 | /* clear last_frames to indicate 'nothing received' */ |
| 1077 | memset(op->last_frames, 0, msg_head->nframes * op->cfsiz); |
| 1078 | } |
| 1079 | |
| 1080 | op->nframes = msg_head->nframes; |
| 1081 | op->flags = msg_head->flags; |
| 1082 | |
| 1083 | /* Only an update -> do not call can_rx_register() */ |
| 1084 | do_rx_register = 0; |
| 1085 | |
| 1086 | } else { |
| 1087 | /* insert new BCM operation for the given can_id */ |
| 1088 | op = kzalloc(OPSIZ, GFP_KERNEL); |
| 1089 | if (!op) |
| 1090 | return -ENOMEM; |
| 1091 | |
| 1092 | op->can_id = msg_head->can_id; |
| 1093 | op->nframes = msg_head->nframes; |
| 1094 | op->cfsiz = CFSIZ(msg_head->flags); |
| 1095 | op->flags = msg_head->flags; |
| 1096 | |
| 1097 | if (msg_head->nframes > 1) { |
| 1098 | /* create array for CAN frames and copy the data */ |
| 1099 | op->frames = kmalloc_array(msg_head->nframes, |
| 1100 | op->cfsiz, |
| 1101 | GFP_KERNEL); |
| 1102 | if (!op->frames) { |
| 1103 | kfree(op); |
| 1104 | return -ENOMEM; |
| 1105 | } |
| 1106 | |
| 1107 | /* create and init array for received CAN frames */ |
| 1108 | op->last_frames = kcalloc(msg_head->nframes, |
| 1109 | op->cfsiz, |
| 1110 | GFP_KERNEL); |
| 1111 | if (!op->last_frames) { |
| 1112 | kfree(op->frames); |
| 1113 | kfree(op); |
| 1114 | return -ENOMEM; |
| 1115 | } |
| 1116 | |
| 1117 | } else { |
| 1118 | op->frames = &op->sframe; |
| 1119 | op->last_frames = &op->last_sframe; |
| 1120 | } |
| 1121 | |
| 1122 | if (msg_head->nframes) { |
| 1123 | err = memcpy_from_msg(op->frames, msg, |
| 1124 | msg_head->nframes * op->cfsiz); |
| 1125 | if (err < 0) { |
| 1126 | if (op->frames != &op->sframe) |
| 1127 | kfree(op->frames); |
| 1128 | if (op->last_frames != &op->last_sframe) |
| 1129 | kfree(op->last_frames); |
| 1130 | kfree(op); |
| 1131 | return err; |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | /* bcm_can_tx / bcm_tx_timeout_handler needs this */ |
| 1136 | op->sk = sk; |
| 1137 | op->ifindex = ifindex; |
| 1138 | |
| 1139 | /* ifindex for timeout events w/o previous frame reception */ |
| 1140 | op->rx_ifindex = ifindex; |
| 1141 | |
| 1142 | /* initialize uninitialized (kzalloc) structure */ |
| 1143 | hrtimer_init(&op->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1144 | op->timer.function = bcm_rx_timeout_handler; |
| 1145 | |
| 1146 | /* initialize tasklet for rx timeout notification */ |
| 1147 | tasklet_init(&op->tsklet, bcm_rx_timeout_tsklet, |
| 1148 | (unsigned long) op); |
| 1149 | |
| 1150 | hrtimer_init(&op->thrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1151 | op->thrtimer.function = bcm_rx_thr_handler; |
| 1152 | |
| 1153 | /* initialize tasklet for rx throttle handling */ |
| 1154 | tasklet_init(&op->thrtsklet, bcm_rx_thr_tsklet, |
| 1155 | (unsigned long) op); |
| 1156 | |
| 1157 | /* add this bcm_op to the list of the rx_ops */ |
| 1158 | list_add(&op->list, &bo->rx_ops); |
| 1159 | |
| 1160 | /* call can_rx_register() */ |
| 1161 | do_rx_register = 1; |
| 1162 | |
| 1163 | } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */ |
| 1164 | |
| 1165 | /* check flags */ |
| 1166 | |
| 1167 | if (op->flags & RX_RTR_FRAME) { |
| 1168 | struct canfd_frame *frame0 = op->frames; |
| 1169 | |
| 1170 | /* no timers in RTR-mode */ |
| 1171 | hrtimer_cancel(&op->thrtimer); |
| 1172 | hrtimer_cancel(&op->timer); |
| 1173 | |
| 1174 | /* |
| 1175 | * funny feature in RX(!)_SETUP only for RTR-mode: |
| 1176 | * copy can_id into frame BUT without RTR-flag to |
| 1177 | * prevent a full-load-loopback-test ... ;-] |
| 1178 | */ |
| 1179 | if ((op->flags & TX_CP_CAN_ID) || |
| 1180 | (frame0->can_id == op->can_id)) |
| 1181 | frame0->can_id = op->can_id & ~CAN_RTR_FLAG; |
| 1182 | |
| 1183 | } else { |
| 1184 | if (op->flags & SETTIMER) { |
| 1185 | |
| 1186 | /* set timer value */ |
| 1187 | op->ival1 = msg_head->ival1; |
| 1188 | op->ival2 = msg_head->ival2; |
| 1189 | op->kt_ival1 = bcm_timeval_to_ktime(msg_head->ival1); |
| 1190 | op->kt_ival2 = bcm_timeval_to_ktime(msg_head->ival2); |
| 1191 | |
| 1192 | /* disable an active timer due to zero value? */ |
| 1193 | if (!op->kt_ival1) |
| 1194 | hrtimer_cancel(&op->timer); |
| 1195 | |
| 1196 | /* |
| 1197 | * In any case cancel the throttle timer, flush |
| 1198 | * potentially blocked msgs and reset throttle handling |
| 1199 | */ |
| 1200 | op->kt_lastmsg = 0; |
| 1201 | hrtimer_cancel(&op->thrtimer); |
| 1202 | bcm_rx_thr_flush(op, 1); |
| 1203 | } |
| 1204 | |
| 1205 | if ((op->flags & STARTTIMER) && op->kt_ival1) |
| 1206 | hrtimer_start(&op->timer, op->kt_ival1, |
| 1207 | HRTIMER_MODE_REL); |
| 1208 | } |
| 1209 | |
| 1210 | /* now we can register for can_ids, if we added a new bcm_op */ |
| 1211 | if (do_rx_register) { |
| 1212 | if (ifindex) { |
| 1213 | struct net_device *dev; |
| 1214 | |
| 1215 | dev = dev_get_by_index(sock_net(sk), ifindex); |
| 1216 | if (dev) { |
| 1217 | err = can_rx_register(sock_net(sk), dev, |
| 1218 | op->can_id, |
| 1219 | REGMASK(op->can_id), |
| 1220 | bcm_rx_handler, op, |
| 1221 | "bcm", sk); |
| 1222 | |
| 1223 | op->rx_reg_dev = dev; |
| 1224 | dev_put(dev); |
| 1225 | } |
| 1226 | |
| 1227 | } else |
| 1228 | err = can_rx_register(sock_net(sk), NULL, op->can_id, |
| 1229 | REGMASK(op->can_id), |
| 1230 | bcm_rx_handler, op, "bcm", sk); |
| 1231 | if (err) { |
| 1232 | /* this bcm rx op is broken -> remove it */ |
| 1233 | list_del(&op->list); |
| 1234 | bcm_remove_op(op); |
| 1235 | return err; |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | return msg_head->nframes * op->cfsiz + MHSIZ; |
| 1240 | } |
| 1241 | |
| 1242 | /* |
| 1243 | * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg) |
| 1244 | */ |
| 1245 | static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk, |
| 1246 | int cfsiz) |
| 1247 | { |
| 1248 | struct sk_buff *skb; |
| 1249 | struct net_device *dev; |
| 1250 | int err; |
| 1251 | |
| 1252 | /* we need a real device to send frames */ |
| 1253 | if (!ifindex) |
| 1254 | return -ENODEV; |
| 1255 | |
| 1256 | skb = alloc_skb(cfsiz + sizeof(struct can_skb_priv), GFP_KERNEL); |
| 1257 | if (!skb) |
| 1258 | return -ENOMEM; |
| 1259 | |
| 1260 | can_skb_reserve(skb); |
| 1261 | |
| 1262 | err = memcpy_from_msg(skb_put(skb, cfsiz), msg, cfsiz); |
| 1263 | if (err < 0) { |
| 1264 | kfree_skb(skb); |
| 1265 | return err; |
| 1266 | } |
| 1267 | |
| 1268 | dev = dev_get_by_index(sock_net(sk), ifindex); |
| 1269 | if (!dev) { |
| 1270 | kfree_skb(skb); |
| 1271 | return -ENODEV; |
| 1272 | } |
| 1273 | |
| 1274 | can_skb_prv(skb)->ifindex = dev->ifindex; |
| 1275 | can_skb_prv(skb)->skbcnt = 0; |
| 1276 | skb->dev = dev; |
| 1277 | can_skb_set_owner(skb, sk); |
| 1278 | err = can_send(skb, 1); /* send with loopback */ |
| 1279 | dev_put(dev); |
| 1280 | |
| 1281 | if (err) |
| 1282 | return err; |
| 1283 | |
| 1284 | return cfsiz + MHSIZ; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | * bcm_sendmsg - process BCM commands (opcodes) from the userspace |
| 1289 | */ |
| 1290 | static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) |
| 1291 | { |
| 1292 | struct sock *sk = sock->sk; |
| 1293 | struct bcm_sock *bo = bcm_sk(sk); |
| 1294 | int ifindex = bo->ifindex; /* default ifindex for this bcm_op */ |
| 1295 | struct bcm_msg_head msg_head; |
| 1296 | int cfsiz; |
| 1297 | int ret; /* read bytes or error codes as return value */ |
| 1298 | |
| 1299 | if (!bo->bound) |
| 1300 | return -ENOTCONN; |
| 1301 | |
| 1302 | /* check for valid message length from userspace */ |
| 1303 | if (size < MHSIZ) |
| 1304 | return -EINVAL; |
| 1305 | |
| 1306 | /* read message head information */ |
| 1307 | ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ); |
| 1308 | if (ret < 0) |
| 1309 | return ret; |
| 1310 | |
| 1311 | cfsiz = CFSIZ(msg_head.flags); |
| 1312 | if ((size - MHSIZ) % cfsiz) |
| 1313 | return -EINVAL; |
| 1314 | |
| 1315 | /* check for alternative ifindex for this bcm_op */ |
| 1316 | |
| 1317 | if (!ifindex && msg->msg_name) { |
| 1318 | /* no bound device as default => check msg_name */ |
| 1319 | DECLARE_SOCKADDR(struct sockaddr_can *, addr, msg->msg_name); |
| 1320 | |
| 1321 | if (msg->msg_namelen < sizeof(*addr)) |
| 1322 | return -EINVAL; |
| 1323 | |
| 1324 | if (addr->can_family != AF_CAN) |
| 1325 | return -EINVAL; |
| 1326 | |
| 1327 | /* ifindex from sendto() */ |
| 1328 | ifindex = addr->can_ifindex; |
| 1329 | |
| 1330 | if (ifindex) { |
| 1331 | struct net_device *dev; |
| 1332 | |
| 1333 | dev = dev_get_by_index(sock_net(sk), ifindex); |
| 1334 | if (!dev) |
| 1335 | return -ENODEV; |
| 1336 | |
| 1337 | if (dev->type != ARPHRD_CAN) { |
| 1338 | dev_put(dev); |
| 1339 | return -ENODEV; |
| 1340 | } |
| 1341 | |
| 1342 | dev_put(dev); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | lock_sock(sk); |
| 1347 | |
| 1348 | switch (msg_head.opcode) { |
| 1349 | |
| 1350 | case TX_SETUP: |
| 1351 | ret = bcm_tx_setup(&msg_head, msg, ifindex, sk); |
| 1352 | break; |
| 1353 | |
| 1354 | case RX_SETUP: |
| 1355 | ret = bcm_rx_setup(&msg_head, msg, ifindex, sk); |
| 1356 | break; |
| 1357 | |
| 1358 | case TX_DELETE: |
| 1359 | if (bcm_delete_tx_op(&bo->tx_ops, &msg_head, ifindex)) |
| 1360 | ret = MHSIZ; |
| 1361 | else |
| 1362 | ret = -EINVAL; |
| 1363 | break; |
| 1364 | |
| 1365 | case RX_DELETE: |
| 1366 | if (bcm_delete_rx_op(&bo->rx_ops, &msg_head, ifindex)) |
| 1367 | ret = MHSIZ; |
| 1368 | else |
| 1369 | ret = -EINVAL; |
| 1370 | break; |
| 1371 | |
| 1372 | case TX_READ: |
| 1373 | /* reuse msg_head for the reply to TX_READ */ |
| 1374 | msg_head.opcode = TX_STATUS; |
| 1375 | ret = bcm_read_op(&bo->tx_ops, &msg_head, ifindex); |
| 1376 | break; |
| 1377 | |
| 1378 | case RX_READ: |
| 1379 | /* reuse msg_head for the reply to RX_READ */ |
| 1380 | msg_head.opcode = RX_STATUS; |
| 1381 | ret = bcm_read_op(&bo->rx_ops, &msg_head, ifindex); |
| 1382 | break; |
| 1383 | |
| 1384 | case TX_SEND: |
| 1385 | /* we need exactly one CAN frame behind the msg head */ |
| 1386 | if ((msg_head.nframes != 1) || (size != cfsiz + MHSIZ)) |
| 1387 | ret = -EINVAL; |
| 1388 | else |
| 1389 | ret = bcm_tx_send(msg, ifindex, sk, cfsiz); |
| 1390 | break; |
| 1391 | |
| 1392 | default: |
| 1393 | ret = -EINVAL; |
| 1394 | break; |
| 1395 | } |
| 1396 | |
| 1397 | release_sock(sk); |
| 1398 | |
| 1399 | return ret; |
| 1400 | } |
| 1401 | |
| 1402 | /* |
| 1403 | * notification handler for netdevice status changes |
| 1404 | */ |
| 1405 | static int bcm_notifier(struct notifier_block *nb, unsigned long msg, |
| 1406 | void *ptr) |
| 1407 | { |
| 1408 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 1409 | struct bcm_sock *bo = container_of(nb, struct bcm_sock, notifier); |
| 1410 | struct sock *sk = &bo->sk; |
| 1411 | struct bcm_op *op; |
| 1412 | int notify_enodev = 0; |
| 1413 | |
| 1414 | if (!net_eq(dev_net(dev), sock_net(sk))) |
| 1415 | return NOTIFY_DONE; |
| 1416 | |
| 1417 | if (dev->type != ARPHRD_CAN) |
| 1418 | return NOTIFY_DONE; |
| 1419 | |
| 1420 | switch (msg) { |
| 1421 | |
| 1422 | case NETDEV_UNREGISTER: |
| 1423 | lock_sock(sk); |
| 1424 | |
| 1425 | /* remove device specific receive entries */ |
| 1426 | list_for_each_entry(op, &bo->rx_ops, list) |
| 1427 | if (op->rx_reg_dev == dev) |
| 1428 | bcm_rx_unreg(dev, op); |
| 1429 | |
| 1430 | /* remove device reference, if this is our bound device */ |
| 1431 | if (bo->bound && bo->ifindex == dev->ifindex) { |
| 1432 | bo->bound = 0; |
| 1433 | bo->ifindex = 0; |
| 1434 | notify_enodev = 1; |
| 1435 | } |
| 1436 | |
| 1437 | release_sock(sk); |
| 1438 | |
| 1439 | if (notify_enodev) { |
| 1440 | sk->sk_err = ENODEV; |
| 1441 | if (!sock_flag(sk, SOCK_DEAD)) |
| 1442 | sk->sk_error_report(sk); |
| 1443 | } |
| 1444 | break; |
| 1445 | |
| 1446 | case NETDEV_DOWN: |
| 1447 | if (bo->bound && bo->ifindex == dev->ifindex) { |
| 1448 | sk->sk_err = ENETDOWN; |
| 1449 | if (!sock_flag(sk, SOCK_DEAD)) |
| 1450 | sk->sk_error_report(sk); |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | return NOTIFY_DONE; |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * initial settings for all BCM sockets to be set at socket creation time |
| 1459 | */ |
| 1460 | static int bcm_init(struct sock *sk) |
| 1461 | { |
| 1462 | struct bcm_sock *bo = bcm_sk(sk); |
| 1463 | |
| 1464 | bo->bound = 0; |
| 1465 | bo->ifindex = 0; |
| 1466 | bo->dropped_usr_msgs = 0; |
| 1467 | bo->bcm_proc_read = NULL; |
| 1468 | |
| 1469 | INIT_LIST_HEAD(&bo->tx_ops); |
| 1470 | INIT_LIST_HEAD(&bo->rx_ops); |
| 1471 | |
| 1472 | /* set notifier */ |
| 1473 | bo->notifier.notifier_call = bcm_notifier; |
| 1474 | |
| 1475 | register_netdevice_notifier(&bo->notifier); |
| 1476 | |
| 1477 | return 0; |
| 1478 | } |
| 1479 | |
| 1480 | /* |
| 1481 | * standard socket functions |
| 1482 | */ |
| 1483 | static int bcm_release(struct socket *sock) |
| 1484 | { |
| 1485 | struct sock *sk = sock->sk; |
| 1486 | struct net *net; |
| 1487 | struct bcm_sock *bo; |
| 1488 | struct bcm_op *op, *next; |
| 1489 | |
| 1490 | if (!sk) |
| 1491 | return 0; |
| 1492 | |
| 1493 | net = sock_net(sk); |
| 1494 | bo = bcm_sk(sk); |
| 1495 | |
| 1496 | /* remove bcm_ops, timer, rx_unregister(), etc. */ |
| 1497 | |
| 1498 | unregister_netdevice_notifier(&bo->notifier); |
| 1499 | |
| 1500 | lock_sock(sk); |
| 1501 | |
| 1502 | list_for_each_entry_safe(op, next, &bo->tx_ops, list) |
| 1503 | bcm_remove_op(op); |
| 1504 | |
| 1505 | list_for_each_entry_safe(op, next, &bo->rx_ops, list) { |
| 1506 | /* |
| 1507 | * Don't care if we're bound or not (due to netdev problems) |
| 1508 | * can_rx_unregister() is always a save thing to do here. |
| 1509 | */ |
| 1510 | if (op->ifindex) { |
| 1511 | /* |
| 1512 | * Only remove subscriptions that had not |
| 1513 | * been removed due to NETDEV_UNREGISTER |
| 1514 | * in bcm_notifier() |
| 1515 | */ |
| 1516 | if (op->rx_reg_dev) { |
| 1517 | struct net_device *dev; |
| 1518 | |
| 1519 | dev = dev_get_by_index(net, op->ifindex); |
| 1520 | if (dev) { |
| 1521 | bcm_rx_unreg(dev, op); |
| 1522 | dev_put(dev); |
| 1523 | } |
| 1524 | } |
| 1525 | } else |
| 1526 | can_rx_unregister(net, NULL, op->can_id, |
| 1527 | REGMASK(op->can_id), |
| 1528 | bcm_rx_handler, op); |
| 1529 | |
| 1530 | bcm_remove_op(op); |
| 1531 | } |
| 1532 | |
| 1533 | #if IS_ENABLED(CONFIG_PROC_FS) |
| 1534 | /* remove procfs entry */ |
| 1535 | if (net->can.bcmproc_dir && bo->bcm_proc_read) |
| 1536 | remove_proc_entry(bo->procname, net->can.bcmproc_dir); |
| 1537 | #endif /* CONFIG_PROC_FS */ |
| 1538 | |
| 1539 | /* remove device reference */ |
| 1540 | if (bo->bound) { |
| 1541 | bo->bound = 0; |
| 1542 | bo->ifindex = 0; |
| 1543 | } |
| 1544 | |
| 1545 | sock_orphan(sk); |
| 1546 | sock->sk = NULL; |
| 1547 | |
| 1548 | release_sock(sk); |
| 1549 | sock_put(sk); |
| 1550 | |
| 1551 | return 0; |
| 1552 | } |
| 1553 | |
| 1554 | static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len, |
| 1555 | int flags) |
| 1556 | { |
| 1557 | struct sockaddr_can *addr = (struct sockaddr_can *)uaddr; |
| 1558 | struct sock *sk = sock->sk; |
| 1559 | struct bcm_sock *bo = bcm_sk(sk); |
| 1560 | struct net *net = sock_net(sk); |
| 1561 | int ret = 0; |
| 1562 | |
| 1563 | if (len < sizeof(*addr)) |
| 1564 | return -EINVAL; |
| 1565 | |
| 1566 | lock_sock(sk); |
| 1567 | |
| 1568 | if (bo->bound) { |
| 1569 | ret = -EISCONN; |
| 1570 | goto fail; |
| 1571 | } |
| 1572 | |
| 1573 | /* bind a device to this socket */ |
| 1574 | if (addr->can_ifindex) { |
| 1575 | struct net_device *dev; |
| 1576 | |
| 1577 | dev = dev_get_by_index(net, addr->can_ifindex); |
| 1578 | if (!dev) { |
| 1579 | ret = -ENODEV; |
| 1580 | goto fail; |
| 1581 | } |
| 1582 | if (dev->type != ARPHRD_CAN) { |
| 1583 | dev_put(dev); |
| 1584 | ret = -ENODEV; |
| 1585 | goto fail; |
| 1586 | } |
| 1587 | |
| 1588 | bo->ifindex = dev->ifindex; |
| 1589 | dev_put(dev); |
| 1590 | |
| 1591 | } else { |
| 1592 | /* no interface reference for ifindex = 0 ('any' CAN device) */ |
| 1593 | bo->ifindex = 0; |
| 1594 | } |
| 1595 | |
| 1596 | #if IS_ENABLED(CONFIG_PROC_FS) |
| 1597 | if (net->can.bcmproc_dir) { |
| 1598 | /* unique socket address as filename */ |
| 1599 | sprintf(bo->procname, "%lu", sock_i_ino(sk)); |
| 1600 | bo->bcm_proc_read = proc_create_net_single(bo->procname, 0644, |
| 1601 | net->can.bcmproc_dir, |
| 1602 | bcm_proc_show, sk); |
| 1603 | if (!bo->bcm_proc_read) { |
| 1604 | ret = -ENOMEM; |
| 1605 | goto fail; |
| 1606 | } |
| 1607 | } |
| 1608 | #endif /* CONFIG_PROC_FS */ |
| 1609 | |
| 1610 | bo->bound = 1; |
| 1611 | |
| 1612 | fail: |
| 1613 | release_sock(sk); |
| 1614 | |
| 1615 | return ret; |
| 1616 | } |
| 1617 | |
| 1618 | static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, |
| 1619 | int flags) |
| 1620 | { |
| 1621 | struct sock *sk = sock->sk; |
| 1622 | struct sk_buff *skb; |
| 1623 | int error = 0; |
| 1624 | int noblock; |
| 1625 | int err; |
| 1626 | |
| 1627 | noblock = flags & MSG_DONTWAIT; |
| 1628 | flags &= ~MSG_DONTWAIT; |
| 1629 | skb = skb_recv_datagram(sk, flags, noblock, &error); |
| 1630 | if (!skb) |
| 1631 | return error; |
| 1632 | |
| 1633 | if (skb->len < size) |
| 1634 | size = skb->len; |
| 1635 | |
| 1636 | err = memcpy_to_msg(msg, skb->data, size); |
| 1637 | if (err < 0) { |
| 1638 | skb_free_datagram(sk, skb); |
| 1639 | return err; |
| 1640 | } |
| 1641 | |
| 1642 | sock_recv_ts_and_drops(msg, sk, skb); |
| 1643 | |
| 1644 | if (msg->msg_name) { |
| 1645 | __sockaddr_check_size(sizeof(struct sockaddr_can)); |
| 1646 | msg->msg_namelen = sizeof(struct sockaddr_can); |
| 1647 | memcpy(msg->msg_name, skb->cb, msg->msg_namelen); |
| 1648 | } |
| 1649 | |
| 1650 | skb_free_datagram(sk, skb); |
| 1651 | |
| 1652 | return size; |
| 1653 | } |
| 1654 | |
| 1655 | static const struct proto_ops bcm_ops = { |
| 1656 | .family = PF_CAN, |
| 1657 | .release = bcm_release, |
| 1658 | .bind = sock_no_bind, |
| 1659 | .connect = bcm_connect, |
| 1660 | .socketpair = sock_no_socketpair, |
| 1661 | .accept = sock_no_accept, |
| 1662 | .getname = sock_no_getname, |
| 1663 | .poll = datagram_poll, |
| 1664 | .ioctl = can_ioctl, /* use can_ioctl() from af_can.c */ |
| 1665 | .listen = sock_no_listen, |
| 1666 | .shutdown = sock_no_shutdown, |
| 1667 | .setsockopt = sock_no_setsockopt, |
| 1668 | .getsockopt = sock_no_getsockopt, |
| 1669 | .sendmsg = bcm_sendmsg, |
| 1670 | .recvmsg = bcm_recvmsg, |
| 1671 | .mmap = sock_no_mmap, |
| 1672 | .sendpage = sock_no_sendpage, |
| 1673 | }; |
| 1674 | |
| 1675 | static struct proto bcm_proto __read_mostly = { |
| 1676 | .name = "CAN_BCM", |
| 1677 | .owner = THIS_MODULE, |
| 1678 | .obj_size = sizeof(struct bcm_sock), |
| 1679 | .init = bcm_init, |
| 1680 | }; |
| 1681 | |
| 1682 | static const struct can_proto bcm_can_proto = { |
| 1683 | .type = SOCK_DGRAM, |
| 1684 | .protocol = CAN_BCM, |
| 1685 | .ops = &bcm_ops, |
| 1686 | .prot = &bcm_proto, |
| 1687 | }; |
| 1688 | |
| 1689 | static int canbcm_pernet_init(struct net *net) |
| 1690 | { |
| 1691 | #if IS_ENABLED(CONFIG_PROC_FS) |
| 1692 | /* create /proc/net/can-bcm directory */ |
| 1693 | net->can.bcmproc_dir = proc_net_mkdir(net, "can-bcm", net->proc_net); |
| 1694 | #endif /* CONFIG_PROC_FS */ |
| 1695 | |
| 1696 | return 0; |
| 1697 | } |
| 1698 | |
| 1699 | static void canbcm_pernet_exit(struct net *net) |
| 1700 | { |
| 1701 | #if IS_ENABLED(CONFIG_PROC_FS) |
| 1702 | /* remove /proc/net/can-bcm directory */ |
| 1703 | if (net->can.bcmproc_dir) |
| 1704 | remove_proc_entry("can-bcm", net->proc_net); |
| 1705 | #endif /* CONFIG_PROC_FS */ |
| 1706 | } |
| 1707 | |
| 1708 | static struct pernet_operations canbcm_pernet_ops __read_mostly = { |
| 1709 | .init = canbcm_pernet_init, |
| 1710 | .exit = canbcm_pernet_exit, |
| 1711 | }; |
| 1712 | |
| 1713 | static int __init bcm_module_init(void) |
| 1714 | { |
| 1715 | int err; |
| 1716 | |
| 1717 | pr_info("can: broadcast manager protocol (rev " CAN_BCM_VERSION " t)\n"); |
| 1718 | |
| 1719 | err = can_proto_register(&bcm_can_proto); |
| 1720 | if (err < 0) { |
| 1721 | printk(KERN_ERR "can: registration of bcm protocol failed\n"); |
| 1722 | return err; |
| 1723 | } |
| 1724 | |
| 1725 | register_pernet_subsys(&canbcm_pernet_ops); |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
| 1729 | static void __exit bcm_module_exit(void) |
| 1730 | { |
| 1731 | can_proto_unregister(&bcm_can_proto); |
| 1732 | unregister_pernet_subsys(&canbcm_pernet_ops); |
| 1733 | } |
| 1734 | |
| 1735 | module_init(bcm_module_init); |
| 1736 | module_exit(bcm_module_exit); |