Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) |
| 2 | /* isotp.c - ISO 15765-2 CAN transport protocol for protocol family CAN |
| 3 | * |
| 4 | * This implementation does not provide ISO-TP specific return values to the |
| 5 | * userspace. |
| 6 | * |
| 7 | * - RX path timeout of data reception leads to -ETIMEDOUT |
| 8 | * - RX path SN mismatch leads to -EILSEQ |
| 9 | * - RX path data reception with wrong padding leads to -EBADMSG |
| 10 | * - TX path flowcontrol reception timeout leads to -ECOMM |
| 11 | * - TX path flowcontrol reception overflow leads to -EMSGSIZE |
| 12 | * - TX path flowcontrol reception with wrong layout/padding leads to -EBADMSG |
| 13 | * - when a transfer (tx) is on the run the next write() blocks until it's done |
| 14 | * - use CAN_ISOTP_WAIT_TX_DONE flag to block the caller until the PDU is sent |
| 15 | * - as we have static buffers the check whether the PDU fits into the buffer |
| 16 | * is done at FF reception time (no support for sending 'wait frames') |
| 17 | * - take care of the tx-queue-len as traffic shaping is still on the TODO list |
| 18 | * |
| 19 | * Copyright (c) 2020 Volkswagen Group Electronic Research |
| 20 | * All rights reserved. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the above copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. Neither the name of Volkswagen nor the names of its contributors |
| 31 | * may be used to endorse or promote products derived from this software |
| 32 | * without specific prior written permission. |
| 33 | * |
| 34 | * Alternatively, provided that this notice is retained in full, this |
| 35 | * software may be distributed under the terms of the GNU General |
| 36 | * Public License ("GPL") version 2, in which case the provisions of the |
| 37 | * GPL apply INSTEAD OF those given above. |
| 38 | * |
| 39 | * The provided data structures and external interfaces from this code |
| 40 | * are not restricted to be used by modules with a GPL compatible license. |
| 41 | * |
| 42 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 43 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 44 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 45 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 46 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 47 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 48 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 49 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 50 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 51 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 52 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 53 | * DAMAGE. |
| 54 | */ |
| 55 | |
| 56 | #include <linux/module.h> |
| 57 | #include <linux/init.h> |
| 58 | #include <linux/interrupt.h> |
| 59 | #include <linux/spinlock.h> |
| 60 | #include <linux/hrtimer.h> |
| 61 | #include <linux/wait.h> |
| 62 | #include <linux/uio.h> |
| 63 | #include <linux/net.h> |
| 64 | #include <linux/netdevice.h> |
| 65 | #include <linux/socket.h> |
| 66 | #include <linux/if_arp.h> |
| 67 | #include <linux/skbuff.h> |
| 68 | #include <linux/can.h> |
| 69 | #include <linux/can/core.h> |
| 70 | #include <linux/can/skb.h> |
| 71 | #include <linux/can/isotp.h> |
| 72 | #include <linux/slab.h> |
| 73 | #include <net/sock.h> |
| 74 | #include <net/net_namespace.h> |
| 75 | |
| 76 | MODULE_DESCRIPTION("PF_CAN isotp 15765-2:2016 protocol"); |
| 77 | MODULE_LICENSE("Dual BSD/GPL"); |
| 78 | MODULE_AUTHOR("Oliver Hartkopp <socketcan@hartkopp.net>"); |
| 79 | MODULE_ALIAS("can-proto-6"); |
| 80 | |
| 81 | #define ISOTP_MIN_NAMELEN CAN_REQUIRED_SIZE(struct sockaddr_can, can_addr.tp) |
| 82 | |
| 83 | #define SINGLE_MASK(id) (((id) & CAN_EFF_FLAG) ? \ |
| 84 | (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \ |
| 85 | (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG)) |
| 86 | |
| 87 | /* ISO 15765-2:2016 supports more than 4095 byte per ISO PDU as the FF_DL can |
| 88 | * take full 32 bit values (4 Gbyte). We would need some good concept to handle |
| 89 | * this between user space and kernel space. For now increase the static buffer |
| 90 | * to something about 8 kbyte to be able to test this new functionality. |
| 91 | */ |
| 92 | #define MAX_MSG_LENGTH 8200 |
| 93 | |
| 94 | /* N_PCI type values in bits 7-4 of N_PCI bytes */ |
| 95 | #define N_PCI_SF 0x00 /* single frame */ |
| 96 | #define N_PCI_FF 0x10 /* first frame */ |
| 97 | #define N_PCI_CF 0x20 /* consecutive frame */ |
| 98 | #define N_PCI_FC 0x30 /* flow control */ |
| 99 | |
| 100 | #define N_PCI_SZ 1 /* size of the PCI byte #1 */ |
| 101 | #define SF_PCI_SZ4 1 /* size of SingleFrame PCI including 4 bit SF_DL */ |
| 102 | #define SF_PCI_SZ8 2 /* size of SingleFrame PCI including 8 bit SF_DL */ |
| 103 | #define FF_PCI_SZ12 2 /* size of FirstFrame PCI including 12 bit FF_DL */ |
| 104 | #define FF_PCI_SZ32 6 /* size of FirstFrame PCI including 32 bit FF_DL */ |
| 105 | #define FC_CONTENT_SZ 3 /* flow control content size in byte (FS/BS/STmin) */ |
| 106 | |
| 107 | #define ISOTP_CHECK_PADDING (CAN_ISOTP_CHK_PAD_LEN | CAN_ISOTP_CHK_PAD_DATA) |
| 108 | |
| 109 | /* Flow Status given in FC frame */ |
| 110 | #define ISOTP_FC_CTS 0 /* clear to send */ |
| 111 | #define ISOTP_FC_WT 1 /* wait */ |
| 112 | #define ISOTP_FC_OVFLW 2 /* overflow */ |
| 113 | |
| 114 | enum { |
| 115 | ISOTP_IDLE = 0, |
| 116 | ISOTP_WAIT_FIRST_FC, |
| 117 | ISOTP_WAIT_FC, |
| 118 | ISOTP_WAIT_DATA, |
| 119 | ISOTP_SENDING |
| 120 | }; |
| 121 | |
| 122 | struct tpcon { |
| 123 | unsigned int idx; |
| 124 | unsigned int len; |
| 125 | u32 state; |
| 126 | u8 bs; |
| 127 | u8 sn; |
| 128 | u8 ll_dl; |
| 129 | u8 buf[MAX_MSG_LENGTH + 1]; |
| 130 | }; |
| 131 | |
| 132 | struct isotp_sock { |
| 133 | struct sock sk; |
| 134 | int bound; |
| 135 | int ifindex; |
| 136 | canid_t txid; |
| 137 | canid_t rxid; |
| 138 | ktime_t tx_gap; |
| 139 | ktime_t lastrxcf_tstamp; |
| 140 | struct hrtimer rxtimer, txtimer; |
| 141 | struct can_isotp_options opt; |
| 142 | struct can_isotp_fc_options rxfc, txfc; |
| 143 | struct can_isotp_ll_options ll; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 144 | u32 frame_txtime; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 145 | u32 force_tx_stmin; |
| 146 | u32 force_rx_stmin; |
| 147 | struct tpcon rx, tx; |
| 148 | struct list_head notifier; |
| 149 | wait_queue_head_t wait; |
| 150 | spinlock_t rx_lock; /* protect single thread state machine */ |
| 151 | }; |
| 152 | |
| 153 | static LIST_HEAD(isotp_notifier_list); |
| 154 | static DEFINE_SPINLOCK(isotp_notifier_lock); |
| 155 | static struct isotp_sock *isotp_busy_notifier; |
| 156 | |
| 157 | static inline struct isotp_sock *isotp_sk(const struct sock *sk) |
| 158 | { |
| 159 | return (struct isotp_sock *)sk; |
| 160 | } |
| 161 | |
| 162 | static enum hrtimer_restart isotp_rx_timer_handler(struct hrtimer *hrtimer) |
| 163 | { |
| 164 | struct isotp_sock *so = container_of(hrtimer, struct isotp_sock, |
| 165 | rxtimer); |
| 166 | struct sock *sk = &so->sk; |
| 167 | |
| 168 | if (so->rx.state == ISOTP_WAIT_DATA) { |
| 169 | /* we did not get new data frames in time */ |
| 170 | |
| 171 | /* report 'connection timed out' */ |
| 172 | sk->sk_err = ETIMEDOUT; |
| 173 | if (!sock_flag(sk, SOCK_DEAD)) |
| 174 | sk->sk_error_report(sk); |
| 175 | |
| 176 | /* reset rx state */ |
| 177 | so->rx.state = ISOTP_IDLE; |
| 178 | } |
| 179 | |
| 180 | return HRTIMER_NORESTART; |
| 181 | } |
| 182 | |
| 183 | static int isotp_send_fc(struct sock *sk, int ae, u8 flowstatus) |
| 184 | { |
| 185 | struct net_device *dev; |
| 186 | struct sk_buff *nskb; |
| 187 | struct canfd_frame *ncf; |
| 188 | struct isotp_sock *so = isotp_sk(sk); |
| 189 | int can_send_ret; |
| 190 | |
| 191 | nskb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), gfp_any()); |
| 192 | if (!nskb) |
| 193 | return 1; |
| 194 | |
| 195 | dev = dev_get_by_index(sock_net(sk), so->ifindex); |
| 196 | if (!dev) { |
| 197 | kfree_skb(nskb); |
| 198 | return 1; |
| 199 | } |
| 200 | |
| 201 | can_skb_reserve(nskb); |
| 202 | can_skb_prv(nskb)->ifindex = dev->ifindex; |
| 203 | can_skb_prv(nskb)->skbcnt = 0; |
| 204 | |
| 205 | nskb->dev = dev; |
| 206 | can_skb_set_owner(nskb, sk); |
| 207 | ncf = (struct canfd_frame *)nskb->data; |
| 208 | skb_put_zero(nskb, so->ll.mtu); |
| 209 | |
| 210 | /* create & send flow control reply */ |
| 211 | ncf->can_id = so->txid; |
| 212 | |
| 213 | if (so->opt.flags & CAN_ISOTP_TX_PADDING) { |
| 214 | memset(ncf->data, so->opt.txpad_content, CAN_MAX_DLEN); |
| 215 | ncf->len = CAN_MAX_DLEN; |
| 216 | } else { |
| 217 | ncf->len = ae + FC_CONTENT_SZ; |
| 218 | } |
| 219 | |
| 220 | ncf->data[ae] = N_PCI_FC | flowstatus; |
| 221 | ncf->data[ae + 1] = so->rxfc.bs; |
| 222 | ncf->data[ae + 2] = so->rxfc.stmin; |
| 223 | |
| 224 | if (ae) |
| 225 | ncf->data[0] = so->opt.ext_address; |
| 226 | |
| 227 | ncf->flags = so->ll.tx_flags; |
| 228 | |
| 229 | can_send_ret = can_send(nskb, 1); |
| 230 | if (can_send_ret) |
| 231 | pr_notice_once("can-isotp: %s: can_send_ret %d\n", |
| 232 | __func__, can_send_ret); |
| 233 | |
| 234 | dev_put(dev); |
| 235 | |
| 236 | /* reset blocksize counter */ |
| 237 | so->rx.bs = 0; |
| 238 | |
| 239 | /* reset last CF frame rx timestamp for rx stmin enforcement */ |
| 240 | so->lastrxcf_tstamp = ktime_set(0, 0); |
| 241 | |
| 242 | /* start rx timeout watchdog */ |
| 243 | hrtimer_start(&so->rxtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT); |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static void isotp_rcv_skb(struct sk_buff *skb, struct sock *sk) |
| 248 | { |
| 249 | struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb; |
| 250 | |
| 251 | BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can)); |
| 252 | |
| 253 | memset(addr, 0, sizeof(*addr)); |
| 254 | addr->can_family = AF_CAN; |
| 255 | addr->can_ifindex = skb->dev->ifindex; |
| 256 | |
| 257 | if (sock_queue_rcv_skb(sk, skb) < 0) |
| 258 | kfree_skb(skb); |
| 259 | } |
| 260 | |
| 261 | static u8 padlen(u8 datalen) |
| 262 | { |
| 263 | static const u8 plen[] = { |
| 264 | 8, 8, 8, 8, 8, 8, 8, 8, 8, /* 0 - 8 */ |
| 265 | 12, 12, 12, 12, /* 9 - 12 */ |
| 266 | 16, 16, 16, 16, /* 13 - 16 */ |
| 267 | 20, 20, 20, 20, /* 17 - 20 */ |
| 268 | 24, 24, 24, 24, /* 21 - 24 */ |
| 269 | 32, 32, 32, 32, 32, 32, 32, 32, /* 25 - 32 */ |
| 270 | 48, 48, 48, 48, 48, 48, 48, 48, /* 33 - 40 */ |
| 271 | 48, 48, 48, 48, 48, 48, 48, 48 /* 41 - 48 */ |
| 272 | }; |
| 273 | |
| 274 | if (datalen > 48) |
| 275 | return 64; |
| 276 | |
| 277 | return plen[datalen]; |
| 278 | } |
| 279 | |
| 280 | /* check for length optimization and return 1/true when the check fails */ |
| 281 | static int check_optimized(struct canfd_frame *cf, int start_index) |
| 282 | { |
| 283 | /* for CAN_DL <= 8 the start_index is equal to the CAN_DL as the |
| 284 | * padding would start at this point. E.g. if the padding would |
| 285 | * start at cf.data[7] cf->len has to be 7 to be optimal. |
| 286 | * Note: The data[] index starts with zero. |
| 287 | */ |
| 288 | if (cf->len <= CAN_MAX_DLEN) |
| 289 | return (cf->len != start_index); |
| 290 | |
| 291 | /* This relation is also valid in the non-linear DLC range, where |
| 292 | * we need to take care of the minimal next possible CAN_DL. |
| 293 | * The correct check would be (padlen(cf->len) != padlen(start_index)). |
| 294 | * But as cf->len can only take discrete values from 12, .., 64 at this |
| 295 | * point the padlen(cf->len) is always equal to cf->len. |
| 296 | */ |
| 297 | return (cf->len != padlen(start_index)); |
| 298 | } |
| 299 | |
| 300 | /* check padding and return 1/true when the check fails */ |
| 301 | static int check_pad(struct isotp_sock *so, struct canfd_frame *cf, |
| 302 | int start_index, u8 content) |
| 303 | { |
| 304 | int i; |
| 305 | |
| 306 | /* no RX_PADDING value => check length of optimized frame length */ |
| 307 | if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) { |
| 308 | if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) |
| 309 | return check_optimized(cf, start_index); |
| 310 | |
| 311 | /* no valid test against empty value => ignore frame */ |
| 312 | return 1; |
| 313 | } |
| 314 | |
| 315 | /* check datalength of correctly padded CAN frame */ |
| 316 | if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) && |
| 317 | cf->len != padlen(cf->len)) |
| 318 | return 1; |
| 319 | |
| 320 | /* check padding content */ |
| 321 | if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) { |
| 322 | for (i = start_index; i < cf->len; i++) |
| 323 | if (cf->data[i] != content) |
| 324 | return 1; |
| 325 | } |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae) |
| 330 | { |
| 331 | struct sock *sk = &so->sk; |
| 332 | |
| 333 | if (so->tx.state != ISOTP_WAIT_FC && |
| 334 | so->tx.state != ISOTP_WAIT_FIRST_FC) |
| 335 | return 0; |
| 336 | |
| 337 | hrtimer_cancel(&so->txtimer); |
| 338 | |
| 339 | if ((cf->len < ae + FC_CONTENT_SZ) || |
| 340 | ((so->opt.flags & ISOTP_CHECK_PADDING) && |
| 341 | check_pad(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) { |
| 342 | /* malformed PDU - report 'not a data message' */ |
| 343 | sk->sk_err = EBADMSG; |
| 344 | if (!sock_flag(sk, SOCK_DEAD)) |
| 345 | sk->sk_error_report(sk); |
| 346 | |
| 347 | so->tx.state = ISOTP_IDLE; |
| 348 | wake_up_interruptible(&so->wait); |
| 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | /* get communication parameters only from the first FC frame */ |
| 353 | if (so->tx.state == ISOTP_WAIT_FIRST_FC) { |
| 354 | so->txfc.bs = cf->data[ae + 1]; |
| 355 | so->txfc.stmin = cf->data[ae + 2]; |
| 356 | |
| 357 | /* fix wrong STmin values according spec */ |
| 358 | if (so->txfc.stmin > 0x7F && |
| 359 | (so->txfc.stmin < 0xF1 || so->txfc.stmin > 0xF9)) |
| 360 | so->txfc.stmin = 0x7F; |
| 361 | |
| 362 | so->tx_gap = ktime_set(0, 0); |
| 363 | /* add transmission time for CAN frame N_As */ |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 364 | so->tx_gap = ktime_add_ns(so->tx_gap, so->frame_txtime); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 365 | /* add waiting time for consecutive frames N_Cs */ |
| 366 | if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN) |
| 367 | so->tx_gap = ktime_add_ns(so->tx_gap, |
| 368 | so->force_tx_stmin); |
| 369 | else if (so->txfc.stmin < 0x80) |
| 370 | so->tx_gap = ktime_add_ns(so->tx_gap, |
| 371 | so->txfc.stmin * 1000000); |
| 372 | else |
| 373 | so->tx_gap = ktime_add_ns(so->tx_gap, |
| 374 | (so->txfc.stmin - 0xF0) |
| 375 | * 100000); |
| 376 | so->tx.state = ISOTP_WAIT_FC; |
| 377 | } |
| 378 | |
| 379 | switch (cf->data[ae] & 0x0F) { |
| 380 | case ISOTP_FC_CTS: |
| 381 | so->tx.bs = 0; |
| 382 | so->tx.state = ISOTP_SENDING; |
| 383 | /* start cyclic timer for sending CF frame */ |
| 384 | hrtimer_start(&so->txtimer, so->tx_gap, |
| 385 | HRTIMER_MODE_REL_SOFT); |
| 386 | break; |
| 387 | |
| 388 | case ISOTP_FC_WT: |
| 389 | /* start timer to wait for next FC frame */ |
| 390 | hrtimer_start(&so->txtimer, ktime_set(1, 0), |
| 391 | HRTIMER_MODE_REL_SOFT); |
| 392 | break; |
| 393 | |
| 394 | case ISOTP_FC_OVFLW: |
| 395 | /* overflow on receiver side - report 'message too long' */ |
| 396 | sk->sk_err = EMSGSIZE; |
| 397 | if (!sock_flag(sk, SOCK_DEAD)) |
| 398 | sk->sk_error_report(sk); |
| 399 | fallthrough; |
| 400 | |
| 401 | default: |
| 402 | /* stop this tx job */ |
| 403 | so->tx.state = ISOTP_IDLE; |
| 404 | wake_up_interruptible(&so->wait); |
| 405 | } |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | static int isotp_rcv_sf(struct sock *sk, struct canfd_frame *cf, int pcilen, |
| 410 | struct sk_buff *skb, int len) |
| 411 | { |
| 412 | struct isotp_sock *so = isotp_sk(sk); |
| 413 | struct sk_buff *nskb; |
| 414 | |
| 415 | hrtimer_cancel(&so->rxtimer); |
| 416 | so->rx.state = ISOTP_IDLE; |
| 417 | |
| 418 | if (!len || len > cf->len - pcilen) |
| 419 | return 1; |
| 420 | |
| 421 | if ((so->opt.flags & ISOTP_CHECK_PADDING) && |
| 422 | check_pad(so, cf, pcilen + len, so->opt.rxpad_content)) { |
| 423 | /* malformed PDU - report 'not a data message' */ |
| 424 | sk->sk_err = EBADMSG; |
| 425 | if (!sock_flag(sk, SOCK_DEAD)) |
| 426 | sk->sk_error_report(sk); |
| 427 | return 1; |
| 428 | } |
| 429 | |
| 430 | nskb = alloc_skb(len, gfp_any()); |
| 431 | if (!nskb) |
| 432 | return 1; |
| 433 | |
| 434 | memcpy(skb_put(nskb, len), &cf->data[pcilen], len); |
| 435 | |
| 436 | nskb->tstamp = skb->tstamp; |
| 437 | nskb->dev = skb->dev; |
| 438 | isotp_rcv_skb(nskb, sk); |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static int isotp_rcv_ff(struct sock *sk, struct canfd_frame *cf, int ae) |
| 443 | { |
| 444 | struct isotp_sock *so = isotp_sk(sk); |
| 445 | int i; |
| 446 | int off; |
| 447 | int ff_pci_sz; |
| 448 | |
| 449 | hrtimer_cancel(&so->rxtimer); |
| 450 | so->rx.state = ISOTP_IDLE; |
| 451 | |
| 452 | /* get the used sender LL_DL from the (first) CAN frame data length */ |
| 453 | so->rx.ll_dl = padlen(cf->len); |
| 454 | |
| 455 | /* the first frame has to use the entire frame up to LL_DL length */ |
| 456 | if (cf->len != so->rx.ll_dl) |
| 457 | return 1; |
| 458 | |
| 459 | /* get the FF_DL */ |
| 460 | so->rx.len = (cf->data[ae] & 0x0F) << 8; |
| 461 | so->rx.len += cf->data[ae + 1]; |
| 462 | |
| 463 | /* Check for FF_DL escape sequence supporting 32 bit PDU length */ |
| 464 | if (so->rx.len) { |
| 465 | ff_pci_sz = FF_PCI_SZ12; |
| 466 | } else { |
| 467 | /* FF_DL = 0 => get real length from next 4 bytes */ |
| 468 | so->rx.len = cf->data[ae + 2] << 24; |
| 469 | so->rx.len += cf->data[ae + 3] << 16; |
| 470 | so->rx.len += cf->data[ae + 4] << 8; |
| 471 | so->rx.len += cf->data[ae + 5]; |
| 472 | ff_pci_sz = FF_PCI_SZ32; |
| 473 | } |
| 474 | |
| 475 | /* take care of a potential SF_DL ESC offset for TX_DL > 8 */ |
| 476 | off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0; |
| 477 | |
| 478 | if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl) |
| 479 | return 1; |
| 480 | |
| 481 | if (so->rx.len > MAX_MSG_LENGTH) { |
| 482 | /* send FC frame with overflow status */ |
| 483 | isotp_send_fc(sk, ae, ISOTP_FC_OVFLW); |
| 484 | return 1; |
| 485 | } |
| 486 | |
| 487 | /* copy the first received data bytes */ |
| 488 | so->rx.idx = 0; |
| 489 | for (i = ae + ff_pci_sz; i < so->rx.ll_dl; i++) |
| 490 | so->rx.buf[so->rx.idx++] = cf->data[i]; |
| 491 | |
| 492 | /* initial setup for this pdu reception */ |
| 493 | so->rx.sn = 1; |
| 494 | so->rx.state = ISOTP_WAIT_DATA; |
| 495 | |
| 496 | /* no creation of flow control frames */ |
| 497 | if (so->opt.flags & CAN_ISOTP_LISTEN_MODE) |
| 498 | return 0; |
| 499 | |
| 500 | /* send our first FC frame */ |
| 501 | isotp_send_fc(sk, ae, ISOTP_FC_CTS); |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static int isotp_rcv_cf(struct sock *sk, struct canfd_frame *cf, int ae, |
| 506 | struct sk_buff *skb) |
| 507 | { |
| 508 | struct isotp_sock *so = isotp_sk(sk); |
| 509 | struct sk_buff *nskb; |
| 510 | int i; |
| 511 | |
| 512 | if (so->rx.state != ISOTP_WAIT_DATA) |
| 513 | return 0; |
| 514 | |
| 515 | /* drop if timestamp gap is less than force_rx_stmin nano secs */ |
| 516 | if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) { |
| 517 | if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) < |
| 518 | so->force_rx_stmin) |
| 519 | return 0; |
| 520 | |
| 521 | so->lastrxcf_tstamp = skb->tstamp; |
| 522 | } |
| 523 | |
| 524 | hrtimer_cancel(&so->rxtimer); |
| 525 | |
| 526 | /* CFs are never longer than the FF */ |
| 527 | if (cf->len > so->rx.ll_dl) |
| 528 | return 1; |
| 529 | |
| 530 | /* CFs have usually the LL_DL length */ |
| 531 | if (cf->len < so->rx.ll_dl) { |
| 532 | /* this is only allowed for the last CF */ |
| 533 | if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ) |
| 534 | return 1; |
| 535 | } |
| 536 | |
| 537 | if ((cf->data[ae] & 0x0F) != so->rx.sn) { |
| 538 | /* wrong sn detected - report 'illegal byte sequence' */ |
| 539 | sk->sk_err = EILSEQ; |
| 540 | if (!sock_flag(sk, SOCK_DEAD)) |
| 541 | sk->sk_error_report(sk); |
| 542 | |
| 543 | /* reset rx state */ |
| 544 | so->rx.state = ISOTP_IDLE; |
| 545 | return 1; |
| 546 | } |
| 547 | so->rx.sn++; |
| 548 | so->rx.sn %= 16; |
| 549 | |
| 550 | for (i = ae + N_PCI_SZ; i < cf->len; i++) { |
| 551 | so->rx.buf[so->rx.idx++] = cf->data[i]; |
| 552 | if (so->rx.idx >= so->rx.len) |
| 553 | break; |
| 554 | } |
| 555 | |
| 556 | if (so->rx.idx >= so->rx.len) { |
| 557 | /* we are done */ |
| 558 | so->rx.state = ISOTP_IDLE; |
| 559 | |
| 560 | if ((so->opt.flags & ISOTP_CHECK_PADDING) && |
| 561 | check_pad(so, cf, i + 1, so->opt.rxpad_content)) { |
| 562 | /* malformed PDU - report 'not a data message' */ |
| 563 | sk->sk_err = EBADMSG; |
| 564 | if (!sock_flag(sk, SOCK_DEAD)) |
| 565 | sk->sk_error_report(sk); |
| 566 | return 1; |
| 567 | } |
| 568 | |
| 569 | nskb = alloc_skb(so->rx.len, gfp_any()); |
| 570 | if (!nskb) |
| 571 | return 1; |
| 572 | |
| 573 | memcpy(skb_put(nskb, so->rx.len), so->rx.buf, |
| 574 | so->rx.len); |
| 575 | |
| 576 | nskb->tstamp = skb->tstamp; |
| 577 | nskb->dev = skb->dev; |
| 578 | isotp_rcv_skb(nskb, sk); |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | /* perform blocksize handling, if enabled */ |
| 583 | if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) { |
| 584 | /* start rx timeout watchdog */ |
| 585 | hrtimer_start(&so->rxtimer, ktime_set(1, 0), |
| 586 | HRTIMER_MODE_REL_SOFT); |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | /* no creation of flow control frames */ |
| 591 | if (so->opt.flags & CAN_ISOTP_LISTEN_MODE) |
| 592 | return 0; |
| 593 | |
| 594 | /* we reached the specified blocksize so->rxfc.bs */ |
| 595 | isotp_send_fc(sk, ae, ISOTP_FC_CTS); |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static void isotp_rcv(struct sk_buff *skb, void *data) |
| 600 | { |
| 601 | struct sock *sk = (struct sock *)data; |
| 602 | struct isotp_sock *so = isotp_sk(sk); |
| 603 | struct canfd_frame *cf; |
| 604 | int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0; |
| 605 | u8 n_pci_type, sf_dl; |
| 606 | |
| 607 | /* Strictly receive only frames with the configured MTU size |
| 608 | * => clear separation of CAN2.0 / CAN FD transport channels |
| 609 | */ |
| 610 | if (skb->len != so->ll.mtu) |
| 611 | return; |
| 612 | |
| 613 | cf = (struct canfd_frame *)skb->data; |
| 614 | |
| 615 | /* if enabled: check reception of my configured extended address */ |
| 616 | if (ae && cf->data[0] != so->opt.rx_ext_address) |
| 617 | return; |
| 618 | |
| 619 | n_pci_type = cf->data[ae] & 0xF0; |
| 620 | |
| 621 | /* Make sure the state changes and data structures stay consistent at |
| 622 | * CAN frame reception time. This locking is not needed in real world |
| 623 | * use cases but the inconsistency can be triggered with syzkaller. |
| 624 | */ |
| 625 | spin_lock(&so->rx_lock); |
| 626 | |
| 627 | if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) { |
| 628 | /* check rx/tx path half duplex expectations */ |
| 629 | if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) || |
| 630 | (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC)) |
| 631 | goto out_unlock; |
| 632 | } |
| 633 | |
| 634 | switch (n_pci_type) { |
| 635 | case N_PCI_FC: |
| 636 | /* tx path: flow control frame containing the FC parameters */ |
| 637 | isotp_rcv_fc(so, cf, ae); |
| 638 | break; |
| 639 | |
| 640 | case N_PCI_SF: |
| 641 | /* rx path: single frame |
| 642 | * |
| 643 | * As we do not have a rx.ll_dl configuration, we can only test |
| 644 | * if the CAN frames payload length matches the LL_DL == 8 |
| 645 | * requirements - no matter if it's CAN 2.0 or CAN FD |
| 646 | */ |
| 647 | |
| 648 | /* get the SF_DL from the N_PCI byte */ |
| 649 | sf_dl = cf->data[ae] & 0x0F; |
| 650 | |
| 651 | if (cf->len <= CAN_MAX_DLEN) { |
| 652 | isotp_rcv_sf(sk, cf, SF_PCI_SZ4 + ae, skb, sf_dl); |
| 653 | } else { |
| 654 | if (skb->len == CANFD_MTU) { |
| 655 | /* We have a CAN FD frame and CAN_DL is greater than 8: |
| 656 | * Only frames with the SF_DL == 0 ESC value are valid. |
| 657 | * |
| 658 | * If so take care of the increased SF PCI size |
| 659 | * (SF_PCI_SZ8) to point to the message content behind |
| 660 | * the extended SF PCI info and get the real SF_DL |
| 661 | * length value from the formerly first data byte. |
| 662 | */ |
| 663 | if (sf_dl == 0) |
| 664 | isotp_rcv_sf(sk, cf, SF_PCI_SZ8 + ae, skb, |
| 665 | cf->data[SF_PCI_SZ4 + ae]); |
| 666 | } |
| 667 | } |
| 668 | break; |
| 669 | |
| 670 | case N_PCI_FF: |
| 671 | /* rx path: first frame */ |
| 672 | isotp_rcv_ff(sk, cf, ae); |
| 673 | break; |
| 674 | |
| 675 | case N_PCI_CF: |
| 676 | /* rx path: consecutive frame */ |
| 677 | isotp_rcv_cf(sk, cf, ae, skb); |
| 678 | break; |
| 679 | } |
| 680 | |
| 681 | out_unlock: |
| 682 | spin_unlock(&so->rx_lock); |
| 683 | } |
| 684 | |
| 685 | static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so, |
| 686 | int ae, int off) |
| 687 | { |
| 688 | int pcilen = N_PCI_SZ + ae + off; |
| 689 | int space = so->tx.ll_dl - pcilen; |
| 690 | int num = min_t(int, so->tx.len - so->tx.idx, space); |
| 691 | int i; |
| 692 | |
| 693 | cf->can_id = so->txid; |
| 694 | cf->len = num + pcilen; |
| 695 | |
| 696 | if (num < space) { |
| 697 | if (so->opt.flags & CAN_ISOTP_TX_PADDING) { |
| 698 | /* user requested padding */ |
| 699 | cf->len = padlen(cf->len); |
| 700 | memset(cf->data, so->opt.txpad_content, cf->len); |
| 701 | } else if (cf->len > CAN_MAX_DLEN) { |
| 702 | /* mandatory padding for CAN FD frames */ |
| 703 | cf->len = padlen(cf->len); |
| 704 | memset(cf->data, CAN_ISOTP_DEFAULT_PAD_CONTENT, |
| 705 | cf->len); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | for (i = 0; i < num; i++) |
| 710 | cf->data[pcilen + i] = so->tx.buf[so->tx.idx++]; |
| 711 | |
| 712 | if (ae) |
| 713 | cf->data[0] = so->opt.ext_address; |
| 714 | } |
| 715 | |
| 716 | static void isotp_create_fframe(struct canfd_frame *cf, struct isotp_sock *so, |
| 717 | int ae) |
| 718 | { |
| 719 | int i; |
| 720 | int ff_pci_sz; |
| 721 | |
| 722 | cf->can_id = so->txid; |
| 723 | cf->len = so->tx.ll_dl; |
| 724 | if (ae) |
| 725 | cf->data[0] = so->opt.ext_address; |
| 726 | |
| 727 | /* create N_PCI bytes with 12/32 bit FF_DL data length */ |
| 728 | if (so->tx.len > 4095) { |
| 729 | /* use 32 bit FF_DL notation */ |
| 730 | cf->data[ae] = N_PCI_FF; |
| 731 | cf->data[ae + 1] = 0; |
| 732 | cf->data[ae + 2] = (u8)(so->tx.len >> 24) & 0xFFU; |
| 733 | cf->data[ae + 3] = (u8)(so->tx.len >> 16) & 0xFFU; |
| 734 | cf->data[ae + 4] = (u8)(so->tx.len >> 8) & 0xFFU; |
| 735 | cf->data[ae + 5] = (u8)so->tx.len & 0xFFU; |
| 736 | ff_pci_sz = FF_PCI_SZ32; |
| 737 | } else { |
| 738 | /* use 12 bit FF_DL notation */ |
| 739 | cf->data[ae] = (u8)(so->tx.len >> 8) | N_PCI_FF; |
| 740 | cf->data[ae + 1] = (u8)so->tx.len & 0xFFU; |
| 741 | ff_pci_sz = FF_PCI_SZ12; |
| 742 | } |
| 743 | |
| 744 | /* add first data bytes depending on ae */ |
| 745 | for (i = ae + ff_pci_sz; i < so->tx.ll_dl; i++) |
| 746 | cf->data[i] = so->tx.buf[so->tx.idx++]; |
| 747 | |
| 748 | so->tx.sn = 1; |
| 749 | so->tx.state = ISOTP_WAIT_FIRST_FC; |
| 750 | } |
| 751 | |
| 752 | static enum hrtimer_restart isotp_tx_timer_handler(struct hrtimer *hrtimer) |
| 753 | { |
| 754 | struct isotp_sock *so = container_of(hrtimer, struct isotp_sock, |
| 755 | txtimer); |
| 756 | struct sock *sk = &so->sk; |
| 757 | struct sk_buff *skb; |
| 758 | struct net_device *dev; |
| 759 | struct canfd_frame *cf; |
| 760 | enum hrtimer_restart restart = HRTIMER_NORESTART; |
| 761 | int can_send_ret; |
| 762 | int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0; |
| 763 | |
| 764 | switch (so->tx.state) { |
| 765 | case ISOTP_WAIT_FC: |
| 766 | case ISOTP_WAIT_FIRST_FC: |
| 767 | |
| 768 | /* we did not get any flow control frame in time */ |
| 769 | |
| 770 | /* report 'communication error on send' */ |
| 771 | sk->sk_err = ECOMM; |
| 772 | if (!sock_flag(sk, SOCK_DEAD)) |
| 773 | sk->sk_error_report(sk); |
| 774 | |
| 775 | /* reset tx state */ |
| 776 | so->tx.state = ISOTP_IDLE; |
| 777 | wake_up_interruptible(&so->wait); |
| 778 | break; |
| 779 | |
| 780 | case ISOTP_SENDING: |
| 781 | |
| 782 | /* push out the next segmented pdu */ |
| 783 | dev = dev_get_by_index(sock_net(sk), so->ifindex); |
| 784 | if (!dev) |
| 785 | break; |
| 786 | |
| 787 | isotp_tx_burst: |
| 788 | skb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), |
| 789 | GFP_ATOMIC); |
| 790 | if (!skb) { |
| 791 | dev_put(dev); |
| 792 | break; |
| 793 | } |
| 794 | |
| 795 | can_skb_reserve(skb); |
| 796 | can_skb_prv(skb)->ifindex = dev->ifindex; |
| 797 | can_skb_prv(skb)->skbcnt = 0; |
| 798 | |
| 799 | cf = (struct canfd_frame *)skb->data; |
| 800 | skb_put_zero(skb, so->ll.mtu); |
| 801 | |
| 802 | /* create consecutive frame */ |
| 803 | isotp_fill_dataframe(cf, so, ae, 0); |
| 804 | |
| 805 | /* place consecutive frame N_PCI in appropriate index */ |
| 806 | cf->data[ae] = N_PCI_CF | so->tx.sn++; |
| 807 | so->tx.sn %= 16; |
| 808 | so->tx.bs++; |
| 809 | |
| 810 | cf->flags = so->ll.tx_flags; |
| 811 | |
| 812 | skb->dev = dev; |
| 813 | can_skb_set_owner(skb, sk); |
| 814 | |
| 815 | can_send_ret = can_send(skb, 1); |
| 816 | if (can_send_ret) |
| 817 | pr_notice_once("can-isotp: %s: can_send_ret %d\n", |
| 818 | __func__, can_send_ret); |
| 819 | |
| 820 | if (so->tx.idx >= so->tx.len) { |
| 821 | /* we are done */ |
| 822 | so->tx.state = ISOTP_IDLE; |
| 823 | dev_put(dev); |
| 824 | wake_up_interruptible(&so->wait); |
| 825 | break; |
| 826 | } |
| 827 | |
| 828 | if (so->txfc.bs && so->tx.bs >= so->txfc.bs) { |
| 829 | /* stop and wait for FC */ |
| 830 | so->tx.state = ISOTP_WAIT_FC; |
| 831 | dev_put(dev); |
| 832 | hrtimer_set_expires(&so->txtimer, |
| 833 | ktime_add(ktime_get(), |
| 834 | ktime_set(1, 0))); |
| 835 | restart = HRTIMER_RESTART; |
| 836 | break; |
| 837 | } |
| 838 | |
| 839 | /* no gap between data frames needed => use burst mode */ |
| 840 | if (!so->tx_gap) |
| 841 | goto isotp_tx_burst; |
| 842 | |
| 843 | /* start timer to send next data frame with correct delay */ |
| 844 | dev_put(dev); |
| 845 | hrtimer_set_expires(&so->txtimer, |
| 846 | ktime_add(ktime_get(), so->tx_gap)); |
| 847 | restart = HRTIMER_RESTART; |
| 848 | break; |
| 849 | |
| 850 | default: |
| 851 | WARN_ON_ONCE(1); |
| 852 | } |
| 853 | |
| 854 | return restart; |
| 855 | } |
| 856 | |
| 857 | static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) |
| 858 | { |
| 859 | struct sock *sk = sock->sk; |
| 860 | struct isotp_sock *so = isotp_sk(sk); |
| 861 | u32 old_state = so->tx.state; |
| 862 | struct sk_buff *skb; |
| 863 | struct net_device *dev; |
| 864 | struct canfd_frame *cf; |
| 865 | int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0; |
| 866 | int wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 867 | s64 hrtimer_sec = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 868 | int off; |
| 869 | int err; |
| 870 | |
| 871 | if (!so->bound) |
| 872 | return -EADDRNOTAVAIL; |
| 873 | |
| 874 | /* we do not support multiple buffers - for now */ |
| 875 | if (cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SENDING) != ISOTP_IDLE || |
| 876 | wq_has_sleeper(&so->wait)) { |
| 877 | if (msg->msg_flags & MSG_DONTWAIT) { |
| 878 | err = -EAGAIN; |
| 879 | goto err_out; |
| 880 | } |
| 881 | |
| 882 | /* wait for complete transmission of current pdu */ |
| 883 | err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE); |
| 884 | if (err) |
| 885 | goto err_out; |
| 886 | } |
| 887 | |
| 888 | if (!size || size > MAX_MSG_LENGTH) { |
| 889 | err = -EINVAL; |
| 890 | goto err_out_drop; |
| 891 | } |
| 892 | |
| 893 | /* take care of a potential SF_DL ESC offset for TX_DL > 8 */ |
| 894 | off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0; |
| 895 | |
| 896 | /* does the given data fit into a single frame for SF_BROADCAST? */ |
| 897 | if ((so->opt.flags & CAN_ISOTP_SF_BROADCAST) && |
| 898 | (size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off)) { |
| 899 | err = -EINVAL; |
| 900 | goto err_out_drop; |
| 901 | } |
| 902 | |
| 903 | err = memcpy_from_msg(so->tx.buf, msg, size); |
| 904 | if (err < 0) |
| 905 | goto err_out_drop; |
| 906 | |
| 907 | dev = dev_get_by_index(sock_net(sk), so->ifindex); |
| 908 | if (!dev) { |
| 909 | err = -ENXIO; |
| 910 | goto err_out_drop; |
| 911 | } |
| 912 | |
| 913 | skb = sock_alloc_send_skb(sk, so->ll.mtu + sizeof(struct can_skb_priv), |
| 914 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 915 | if (!skb) { |
| 916 | dev_put(dev); |
| 917 | goto err_out_drop; |
| 918 | } |
| 919 | |
| 920 | can_skb_reserve(skb); |
| 921 | can_skb_prv(skb)->ifindex = dev->ifindex; |
| 922 | can_skb_prv(skb)->skbcnt = 0; |
| 923 | |
| 924 | so->tx.len = size; |
| 925 | so->tx.idx = 0; |
| 926 | |
| 927 | cf = (struct canfd_frame *)skb->data; |
| 928 | skb_put_zero(skb, so->ll.mtu); |
| 929 | |
| 930 | /* check for single frame transmission depending on TX_DL */ |
| 931 | if (size <= so->tx.ll_dl - SF_PCI_SZ4 - ae - off) { |
| 932 | /* The message size generally fits into a SingleFrame - good. |
| 933 | * |
| 934 | * SF_DL ESC offset optimization: |
| 935 | * |
| 936 | * When TX_DL is greater 8 but the message would still fit |
| 937 | * into a 8 byte CAN frame, we can omit the offset. |
| 938 | * This prevents a protocol caused length extension from |
| 939 | * CAN_DL = 8 to CAN_DL = 12 due to the SF_SL ESC handling. |
| 940 | */ |
| 941 | if (size <= CAN_MAX_DLEN - SF_PCI_SZ4 - ae) |
| 942 | off = 0; |
| 943 | |
| 944 | isotp_fill_dataframe(cf, so, ae, off); |
| 945 | |
| 946 | /* place single frame N_PCI w/o length in appropriate index */ |
| 947 | cf->data[ae] = N_PCI_SF; |
| 948 | |
| 949 | /* place SF_DL size value depending on the SF_DL ESC offset */ |
| 950 | if (off) |
| 951 | cf->data[SF_PCI_SZ4 + ae] = size; |
| 952 | else |
| 953 | cf->data[ae] |= size; |
| 954 | |
| 955 | so->tx.state = ISOTP_IDLE; |
| 956 | wake_up_interruptible(&so->wait); |
| 957 | |
| 958 | /* don't enable wait queue for a single frame transmission */ |
| 959 | wait_tx_done = 0; |
| 960 | } else { |
| 961 | /* send first frame and wait for FC */ |
| 962 | |
| 963 | isotp_create_fframe(cf, so, ae); |
| 964 | |
| 965 | /* start timeout for FC */ |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 966 | hrtimer_sec = 1; |
| 967 | hrtimer_start(&so->txtimer, ktime_set(hrtimer_sec, 0), |
| 968 | HRTIMER_MODE_REL_SOFT); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /* send the first or only CAN frame */ |
| 972 | cf->flags = so->ll.tx_flags; |
| 973 | |
| 974 | skb->dev = dev; |
| 975 | skb->sk = sk; |
| 976 | err = can_send(skb, 1); |
| 977 | dev_put(dev); |
| 978 | if (err) { |
| 979 | pr_notice_once("can-isotp: %s: can_send_ret %d\n", |
| 980 | __func__, err); |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 981 | |
| 982 | /* no transmission -> no timeout monitoring */ |
| 983 | if (hrtimer_sec) |
| 984 | hrtimer_cancel(&so->txtimer); |
| 985 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 986 | goto err_out_drop; |
| 987 | } |
| 988 | |
| 989 | if (wait_tx_done) { |
| 990 | /* wait for complete transmission of current pdu */ |
| 991 | wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE); |
| 992 | |
| 993 | if (sk->sk_err) |
| 994 | return -sk->sk_err; |
| 995 | } |
| 996 | |
| 997 | return size; |
| 998 | |
| 999 | err_out_drop: |
| 1000 | /* drop this PDU and unlock a potential wait queue */ |
| 1001 | old_state = ISOTP_IDLE; |
| 1002 | err_out: |
| 1003 | so->tx.state = old_state; |
| 1004 | if (so->tx.state == ISOTP_IDLE) |
| 1005 | wake_up_interruptible(&so->wait); |
| 1006 | |
| 1007 | return err; |
| 1008 | } |
| 1009 | |
| 1010 | static int isotp_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, |
| 1011 | int flags) |
| 1012 | { |
| 1013 | struct sock *sk = sock->sk; |
| 1014 | struct sk_buff *skb; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1015 | struct isotp_sock *so = isotp_sk(sk); |
| 1016 | int noblock = flags & MSG_DONTWAIT; |
| 1017 | int ret = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1018 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1019 | if (flags & ~(MSG_DONTWAIT | MSG_TRUNC | MSG_PEEK)) |
| 1020 | return -EINVAL; |
| 1021 | |
| 1022 | if (!so->bound) |
| 1023 | return -EADDRNOTAVAIL; |
| 1024 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1025 | flags &= ~MSG_DONTWAIT; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1026 | skb = skb_recv_datagram(sk, flags, noblock, &ret); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1027 | if (!skb) |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1028 | return ret; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1029 | |
| 1030 | if (size < skb->len) |
| 1031 | msg->msg_flags |= MSG_TRUNC; |
| 1032 | else |
| 1033 | size = skb->len; |
| 1034 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1035 | ret = memcpy_to_msg(msg, skb->data, size); |
| 1036 | if (ret < 0) |
| 1037 | goto out_err; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1038 | |
| 1039 | sock_recv_timestamp(msg, sk, skb); |
| 1040 | |
| 1041 | if (msg->msg_name) { |
| 1042 | __sockaddr_check_size(ISOTP_MIN_NAMELEN); |
| 1043 | msg->msg_namelen = ISOTP_MIN_NAMELEN; |
| 1044 | memcpy(msg->msg_name, skb->cb, msg->msg_namelen); |
| 1045 | } |
| 1046 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1047 | /* set length of return value */ |
| 1048 | ret = (flags & MSG_TRUNC) ? skb->len : size; |
| 1049 | |
| 1050 | out_err: |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1051 | skb_free_datagram(sk, skb); |
| 1052 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1053 | return ret; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | static int isotp_release(struct socket *sock) |
| 1057 | { |
| 1058 | struct sock *sk = sock->sk; |
| 1059 | struct isotp_sock *so; |
| 1060 | struct net *net; |
| 1061 | |
| 1062 | if (!sk) |
| 1063 | return 0; |
| 1064 | |
| 1065 | so = isotp_sk(sk); |
| 1066 | net = sock_net(sk); |
| 1067 | |
| 1068 | /* wait for complete transmission of current pdu */ |
| 1069 | wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE); |
| 1070 | |
| 1071 | spin_lock(&isotp_notifier_lock); |
| 1072 | while (isotp_busy_notifier == so) { |
| 1073 | spin_unlock(&isotp_notifier_lock); |
| 1074 | schedule_timeout_uninterruptible(1); |
| 1075 | spin_lock(&isotp_notifier_lock); |
| 1076 | } |
| 1077 | list_del(&so->notifier); |
| 1078 | spin_unlock(&isotp_notifier_lock); |
| 1079 | |
| 1080 | lock_sock(sk); |
| 1081 | |
| 1082 | /* remove current filters & unregister */ |
| 1083 | if (so->bound && (!(so->opt.flags & CAN_ISOTP_SF_BROADCAST))) { |
| 1084 | if (so->ifindex) { |
| 1085 | struct net_device *dev; |
| 1086 | |
| 1087 | dev = dev_get_by_index(net, so->ifindex); |
| 1088 | if (dev) { |
| 1089 | can_rx_unregister(net, dev, so->rxid, |
| 1090 | SINGLE_MASK(so->rxid), |
| 1091 | isotp_rcv, sk); |
| 1092 | dev_put(dev); |
| 1093 | synchronize_rcu(); |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | hrtimer_cancel(&so->txtimer); |
| 1099 | hrtimer_cancel(&so->rxtimer); |
| 1100 | |
| 1101 | so->ifindex = 0; |
| 1102 | so->bound = 0; |
| 1103 | |
| 1104 | sock_orphan(sk); |
| 1105 | sock->sk = NULL; |
| 1106 | |
| 1107 | release_sock(sk); |
| 1108 | sock_put(sk); |
| 1109 | |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len) |
| 1114 | { |
| 1115 | struct sockaddr_can *addr = (struct sockaddr_can *)uaddr; |
| 1116 | struct sock *sk = sock->sk; |
| 1117 | struct isotp_sock *so = isotp_sk(sk); |
| 1118 | struct net *net = sock_net(sk); |
| 1119 | int ifindex; |
| 1120 | struct net_device *dev; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1121 | canid_t tx_id, rx_id; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1122 | int err = 0; |
| 1123 | int notify_enetdown = 0; |
| 1124 | int do_rx_reg = 1; |
| 1125 | |
| 1126 | if (len < ISOTP_MIN_NAMELEN) |
| 1127 | return -EINVAL; |
| 1128 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1129 | /* sanitize tx/rx CAN identifiers */ |
| 1130 | tx_id = addr->can_addr.tp.tx_id; |
| 1131 | if (tx_id & CAN_EFF_FLAG) |
| 1132 | tx_id &= (CAN_EFF_FLAG | CAN_EFF_MASK); |
| 1133 | else |
| 1134 | tx_id &= CAN_SFF_MASK; |
| 1135 | |
| 1136 | rx_id = addr->can_addr.tp.rx_id; |
| 1137 | if (rx_id & CAN_EFF_FLAG) |
| 1138 | rx_id &= (CAN_EFF_FLAG | CAN_EFF_MASK); |
| 1139 | else |
| 1140 | rx_id &= CAN_SFF_MASK; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1141 | |
| 1142 | if (!addr->can_ifindex) |
| 1143 | return -ENODEV; |
| 1144 | |
| 1145 | lock_sock(sk); |
| 1146 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1147 | if (so->bound) { |
| 1148 | err = -EINVAL; |
| 1149 | goto out; |
| 1150 | } |
| 1151 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1152 | /* do not register frame reception for functional addressing */ |
| 1153 | if (so->opt.flags & CAN_ISOTP_SF_BROADCAST) |
| 1154 | do_rx_reg = 0; |
| 1155 | |
| 1156 | /* do not validate rx address for functional addressing */ |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1157 | if (do_rx_reg && rx_id == tx_id) { |
| 1158 | err = -EADDRNOTAVAIL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1159 | goto out; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1160 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1161 | |
| 1162 | dev = dev_get_by_index(net, addr->can_ifindex); |
| 1163 | if (!dev) { |
| 1164 | err = -ENODEV; |
| 1165 | goto out; |
| 1166 | } |
| 1167 | if (dev->type != ARPHRD_CAN) { |
| 1168 | dev_put(dev); |
| 1169 | err = -ENODEV; |
| 1170 | goto out; |
| 1171 | } |
| 1172 | if (dev->mtu < so->ll.mtu) { |
| 1173 | dev_put(dev); |
| 1174 | err = -EINVAL; |
| 1175 | goto out; |
| 1176 | } |
| 1177 | if (!(dev->flags & IFF_UP)) |
| 1178 | notify_enetdown = 1; |
| 1179 | |
| 1180 | ifindex = dev->ifindex; |
| 1181 | |
| 1182 | if (do_rx_reg) |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1183 | can_rx_register(net, dev, rx_id, SINGLE_MASK(rx_id), |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1184 | isotp_rcv, sk, "isotp", sk); |
| 1185 | |
| 1186 | dev_put(dev); |
| 1187 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1188 | /* switch to new settings */ |
| 1189 | so->ifindex = ifindex; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1190 | so->rxid = rx_id; |
| 1191 | so->txid = tx_id; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1192 | so->bound = 1; |
| 1193 | |
| 1194 | out: |
| 1195 | release_sock(sk); |
| 1196 | |
| 1197 | if (notify_enetdown) { |
| 1198 | sk->sk_err = ENETDOWN; |
| 1199 | if (!sock_flag(sk, SOCK_DEAD)) |
| 1200 | sk->sk_error_report(sk); |
| 1201 | } |
| 1202 | |
| 1203 | return err; |
| 1204 | } |
| 1205 | |
| 1206 | static int isotp_getname(struct socket *sock, struct sockaddr *uaddr, int peer) |
| 1207 | { |
| 1208 | struct sockaddr_can *addr = (struct sockaddr_can *)uaddr; |
| 1209 | struct sock *sk = sock->sk; |
| 1210 | struct isotp_sock *so = isotp_sk(sk); |
| 1211 | |
| 1212 | if (peer) |
| 1213 | return -EOPNOTSUPP; |
| 1214 | |
| 1215 | memset(addr, 0, ISOTP_MIN_NAMELEN); |
| 1216 | addr->can_family = AF_CAN; |
| 1217 | addr->can_ifindex = so->ifindex; |
| 1218 | addr->can_addr.tp.rx_id = so->rxid; |
| 1219 | addr->can_addr.tp.tx_id = so->txid; |
| 1220 | |
| 1221 | return ISOTP_MIN_NAMELEN; |
| 1222 | } |
| 1223 | |
| 1224 | static int isotp_setsockopt_locked(struct socket *sock, int level, int optname, |
| 1225 | sockptr_t optval, unsigned int optlen) |
| 1226 | { |
| 1227 | struct sock *sk = sock->sk; |
| 1228 | struct isotp_sock *so = isotp_sk(sk); |
| 1229 | int ret = 0; |
| 1230 | |
| 1231 | if (so->bound) |
| 1232 | return -EISCONN; |
| 1233 | |
| 1234 | switch (optname) { |
| 1235 | case CAN_ISOTP_OPTS: |
| 1236 | if (optlen != sizeof(struct can_isotp_options)) |
| 1237 | return -EINVAL; |
| 1238 | |
| 1239 | if (copy_from_sockptr(&so->opt, optval, optlen)) |
| 1240 | return -EFAULT; |
| 1241 | |
| 1242 | /* no separate rx_ext_address is given => use ext_address */ |
| 1243 | if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR)) |
| 1244 | so->opt.rx_ext_address = so->opt.ext_address; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1245 | |
| 1246 | /* check for frame_txtime changes (0 => no changes) */ |
| 1247 | if (so->opt.frame_txtime) { |
| 1248 | if (so->opt.frame_txtime == CAN_ISOTP_FRAME_TXTIME_ZERO) |
| 1249 | so->frame_txtime = 0; |
| 1250 | else |
| 1251 | so->frame_txtime = so->opt.frame_txtime; |
| 1252 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1253 | break; |
| 1254 | |
| 1255 | case CAN_ISOTP_RECV_FC: |
| 1256 | if (optlen != sizeof(struct can_isotp_fc_options)) |
| 1257 | return -EINVAL; |
| 1258 | |
| 1259 | if (copy_from_sockptr(&so->rxfc, optval, optlen)) |
| 1260 | return -EFAULT; |
| 1261 | break; |
| 1262 | |
| 1263 | case CAN_ISOTP_TX_STMIN: |
| 1264 | if (optlen != sizeof(u32)) |
| 1265 | return -EINVAL; |
| 1266 | |
| 1267 | if (copy_from_sockptr(&so->force_tx_stmin, optval, optlen)) |
| 1268 | return -EFAULT; |
| 1269 | break; |
| 1270 | |
| 1271 | case CAN_ISOTP_RX_STMIN: |
| 1272 | if (optlen != sizeof(u32)) |
| 1273 | return -EINVAL; |
| 1274 | |
| 1275 | if (copy_from_sockptr(&so->force_rx_stmin, optval, optlen)) |
| 1276 | return -EFAULT; |
| 1277 | break; |
| 1278 | |
| 1279 | case CAN_ISOTP_LL_OPTS: |
| 1280 | if (optlen == sizeof(struct can_isotp_ll_options)) { |
| 1281 | struct can_isotp_ll_options ll; |
| 1282 | |
| 1283 | if (copy_from_sockptr(&ll, optval, optlen)) |
| 1284 | return -EFAULT; |
| 1285 | |
| 1286 | /* check for correct ISO 11898-1 DLC data length */ |
| 1287 | if (ll.tx_dl != padlen(ll.tx_dl)) |
| 1288 | return -EINVAL; |
| 1289 | |
| 1290 | if (ll.mtu != CAN_MTU && ll.mtu != CANFD_MTU) |
| 1291 | return -EINVAL; |
| 1292 | |
| 1293 | if (ll.mtu == CAN_MTU && |
| 1294 | (ll.tx_dl > CAN_MAX_DLEN || ll.tx_flags != 0)) |
| 1295 | return -EINVAL; |
| 1296 | |
| 1297 | memcpy(&so->ll, &ll, sizeof(ll)); |
| 1298 | |
| 1299 | /* set ll_dl for tx path to similar place as for rx */ |
| 1300 | so->tx.ll_dl = ll.tx_dl; |
| 1301 | } else { |
| 1302 | return -EINVAL; |
| 1303 | } |
| 1304 | break; |
| 1305 | |
| 1306 | default: |
| 1307 | ret = -ENOPROTOOPT; |
| 1308 | } |
| 1309 | |
| 1310 | return ret; |
| 1311 | } |
| 1312 | |
| 1313 | static int isotp_setsockopt(struct socket *sock, int level, int optname, |
| 1314 | sockptr_t optval, unsigned int optlen) |
| 1315 | |
| 1316 | { |
| 1317 | struct sock *sk = sock->sk; |
| 1318 | int ret; |
| 1319 | |
| 1320 | if (level != SOL_CAN_ISOTP) |
| 1321 | return -EINVAL; |
| 1322 | |
| 1323 | lock_sock(sk); |
| 1324 | ret = isotp_setsockopt_locked(sock, level, optname, optval, optlen); |
| 1325 | release_sock(sk); |
| 1326 | return ret; |
| 1327 | } |
| 1328 | |
| 1329 | static int isotp_getsockopt(struct socket *sock, int level, int optname, |
| 1330 | char __user *optval, int __user *optlen) |
| 1331 | { |
| 1332 | struct sock *sk = sock->sk; |
| 1333 | struct isotp_sock *so = isotp_sk(sk); |
| 1334 | int len; |
| 1335 | void *val; |
| 1336 | |
| 1337 | if (level != SOL_CAN_ISOTP) |
| 1338 | return -EINVAL; |
| 1339 | if (get_user(len, optlen)) |
| 1340 | return -EFAULT; |
| 1341 | if (len < 0) |
| 1342 | return -EINVAL; |
| 1343 | |
| 1344 | switch (optname) { |
| 1345 | case CAN_ISOTP_OPTS: |
| 1346 | len = min_t(int, len, sizeof(struct can_isotp_options)); |
| 1347 | val = &so->opt; |
| 1348 | break; |
| 1349 | |
| 1350 | case CAN_ISOTP_RECV_FC: |
| 1351 | len = min_t(int, len, sizeof(struct can_isotp_fc_options)); |
| 1352 | val = &so->rxfc; |
| 1353 | break; |
| 1354 | |
| 1355 | case CAN_ISOTP_TX_STMIN: |
| 1356 | len = min_t(int, len, sizeof(u32)); |
| 1357 | val = &so->force_tx_stmin; |
| 1358 | break; |
| 1359 | |
| 1360 | case CAN_ISOTP_RX_STMIN: |
| 1361 | len = min_t(int, len, sizeof(u32)); |
| 1362 | val = &so->force_rx_stmin; |
| 1363 | break; |
| 1364 | |
| 1365 | case CAN_ISOTP_LL_OPTS: |
| 1366 | len = min_t(int, len, sizeof(struct can_isotp_ll_options)); |
| 1367 | val = &so->ll; |
| 1368 | break; |
| 1369 | |
| 1370 | default: |
| 1371 | return -ENOPROTOOPT; |
| 1372 | } |
| 1373 | |
| 1374 | if (put_user(len, optlen)) |
| 1375 | return -EFAULT; |
| 1376 | if (copy_to_user(optval, val, len)) |
| 1377 | return -EFAULT; |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | static void isotp_notify(struct isotp_sock *so, unsigned long msg, |
| 1382 | struct net_device *dev) |
| 1383 | { |
| 1384 | struct sock *sk = &so->sk; |
| 1385 | |
| 1386 | if (!net_eq(dev_net(dev), sock_net(sk))) |
| 1387 | return; |
| 1388 | |
| 1389 | if (so->ifindex != dev->ifindex) |
| 1390 | return; |
| 1391 | |
| 1392 | switch (msg) { |
| 1393 | case NETDEV_UNREGISTER: |
| 1394 | lock_sock(sk); |
| 1395 | /* remove current filters & unregister */ |
| 1396 | if (so->bound && (!(so->opt.flags & CAN_ISOTP_SF_BROADCAST))) |
| 1397 | can_rx_unregister(dev_net(dev), dev, so->rxid, |
| 1398 | SINGLE_MASK(so->rxid), |
| 1399 | isotp_rcv, sk); |
| 1400 | |
| 1401 | so->ifindex = 0; |
| 1402 | so->bound = 0; |
| 1403 | release_sock(sk); |
| 1404 | |
| 1405 | sk->sk_err = ENODEV; |
| 1406 | if (!sock_flag(sk, SOCK_DEAD)) |
| 1407 | sk->sk_error_report(sk); |
| 1408 | break; |
| 1409 | |
| 1410 | case NETDEV_DOWN: |
| 1411 | sk->sk_err = ENETDOWN; |
| 1412 | if (!sock_flag(sk, SOCK_DEAD)) |
| 1413 | sk->sk_error_report(sk); |
| 1414 | break; |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | static int isotp_notifier(struct notifier_block *nb, unsigned long msg, |
| 1419 | void *ptr) |
| 1420 | { |
| 1421 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 1422 | |
| 1423 | if (dev->type != ARPHRD_CAN) |
| 1424 | return NOTIFY_DONE; |
| 1425 | if (msg != NETDEV_UNREGISTER && msg != NETDEV_DOWN) |
| 1426 | return NOTIFY_DONE; |
| 1427 | if (unlikely(isotp_busy_notifier)) /* Check for reentrant bug. */ |
| 1428 | return NOTIFY_DONE; |
| 1429 | |
| 1430 | spin_lock(&isotp_notifier_lock); |
| 1431 | list_for_each_entry(isotp_busy_notifier, &isotp_notifier_list, notifier) { |
| 1432 | spin_unlock(&isotp_notifier_lock); |
| 1433 | isotp_notify(isotp_busy_notifier, msg, dev); |
| 1434 | spin_lock(&isotp_notifier_lock); |
| 1435 | } |
| 1436 | isotp_busy_notifier = NULL; |
| 1437 | spin_unlock(&isotp_notifier_lock); |
| 1438 | return NOTIFY_DONE; |
| 1439 | } |
| 1440 | |
| 1441 | static int isotp_init(struct sock *sk) |
| 1442 | { |
| 1443 | struct isotp_sock *so = isotp_sk(sk); |
| 1444 | |
| 1445 | so->ifindex = 0; |
| 1446 | so->bound = 0; |
| 1447 | |
| 1448 | so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS; |
| 1449 | so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS; |
| 1450 | so->opt.rx_ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS; |
| 1451 | so->opt.rxpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT; |
| 1452 | so->opt.txpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT; |
| 1453 | so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 1454 | so->frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 1455 | so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS; |
| 1456 | so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN; |
| 1457 | so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX; |
| 1458 | so->ll.mtu = CAN_ISOTP_DEFAULT_LL_MTU; |
| 1459 | so->ll.tx_dl = CAN_ISOTP_DEFAULT_LL_TX_DL; |
| 1460 | so->ll.tx_flags = CAN_ISOTP_DEFAULT_LL_TX_FLAGS; |
| 1461 | |
| 1462 | /* set ll_dl for tx path to similar place as for rx */ |
| 1463 | so->tx.ll_dl = so->ll.tx_dl; |
| 1464 | |
| 1465 | so->rx.state = ISOTP_IDLE; |
| 1466 | so->tx.state = ISOTP_IDLE; |
| 1467 | |
| 1468 | hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT); |
| 1469 | so->rxtimer.function = isotp_rx_timer_handler; |
| 1470 | hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT); |
| 1471 | so->txtimer.function = isotp_tx_timer_handler; |
| 1472 | |
| 1473 | init_waitqueue_head(&so->wait); |
| 1474 | spin_lock_init(&so->rx_lock); |
| 1475 | |
| 1476 | spin_lock(&isotp_notifier_lock); |
| 1477 | list_add_tail(&so->notifier, &isotp_notifier_list); |
| 1478 | spin_unlock(&isotp_notifier_lock); |
| 1479 | |
| 1480 | return 0; |
| 1481 | } |
| 1482 | |
| 1483 | static int isotp_sock_no_ioctlcmd(struct socket *sock, unsigned int cmd, |
| 1484 | unsigned long arg) |
| 1485 | { |
| 1486 | /* no ioctls for socket layer -> hand it down to NIC layer */ |
| 1487 | return -ENOIOCTLCMD; |
| 1488 | } |
| 1489 | |
| 1490 | static const struct proto_ops isotp_ops = { |
| 1491 | .family = PF_CAN, |
| 1492 | .release = isotp_release, |
| 1493 | .bind = isotp_bind, |
| 1494 | .connect = sock_no_connect, |
| 1495 | .socketpair = sock_no_socketpair, |
| 1496 | .accept = sock_no_accept, |
| 1497 | .getname = isotp_getname, |
| 1498 | .poll = datagram_poll, |
| 1499 | .ioctl = isotp_sock_no_ioctlcmd, |
| 1500 | .gettstamp = sock_gettstamp, |
| 1501 | .listen = sock_no_listen, |
| 1502 | .shutdown = sock_no_shutdown, |
| 1503 | .setsockopt = isotp_setsockopt, |
| 1504 | .getsockopt = isotp_getsockopt, |
| 1505 | .sendmsg = isotp_sendmsg, |
| 1506 | .recvmsg = isotp_recvmsg, |
| 1507 | .mmap = sock_no_mmap, |
| 1508 | .sendpage = sock_no_sendpage, |
| 1509 | }; |
| 1510 | |
| 1511 | static struct proto isotp_proto __read_mostly = { |
| 1512 | .name = "CAN_ISOTP", |
| 1513 | .owner = THIS_MODULE, |
| 1514 | .obj_size = sizeof(struct isotp_sock), |
| 1515 | .init = isotp_init, |
| 1516 | }; |
| 1517 | |
| 1518 | static const struct can_proto isotp_can_proto = { |
| 1519 | .type = SOCK_DGRAM, |
| 1520 | .protocol = CAN_ISOTP, |
| 1521 | .ops = &isotp_ops, |
| 1522 | .prot = &isotp_proto, |
| 1523 | }; |
| 1524 | |
| 1525 | static struct notifier_block canisotp_notifier = { |
| 1526 | .notifier_call = isotp_notifier |
| 1527 | }; |
| 1528 | |
| 1529 | static __init int isotp_module_init(void) |
| 1530 | { |
| 1531 | int err; |
| 1532 | |
| 1533 | pr_info("can: isotp protocol\n"); |
| 1534 | |
| 1535 | err = can_proto_register(&isotp_can_proto); |
| 1536 | if (err < 0) |
| 1537 | pr_err("can: registration of isotp protocol failed\n"); |
| 1538 | else |
| 1539 | register_netdevice_notifier(&canisotp_notifier); |
| 1540 | |
| 1541 | return err; |
| 1542 | } |
| 1543 | |
| 1544 | static __exit void isotp_module_exit(void) |
| 1545 | { |
| 1546 | can_proto_unregister(&isotp_can_proto); |
| 1547 | unregister_netdevice_notifier(&canisotp_notifier); |
| 1548 | } |
| 1549 | |
| 1550 | module_init(isotp_module_init); |
| 1551 | module_exit(isotp_module_exit); |