1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (c) 2010-2011 EIA Electronics, 3 // Kurt Van Dijck <kurt.van.dijck@eia.be> 4 // Copyright (c) 2018 Protonic, 5 // Robin van der Gracht <robin@protonic.nl> 6 // Copyright (c) 2017-2019 Pengutronix, 7 // Marc Kleine-Budde <kernel@pengutronix.de> 8 // Copyright (c) 2017-2019 Pengutronix, 9 // Oleksij Rempel <kernel@pengutronix.de> 10 11 #include <linux/can/skb.h> 12 13 #include "j1939-priv.h" 14 15 #define J1939_XTP_TX_RETRY_LIMIT 100 16 17 #define J1939_ETP_PGN_CTL 0xc800 18 #define J1939_ETP_PGN_DAT 0xc700 19 #define J1939_TP_PGN_CTL 0xec00 20 #define J1939_TP_PGN_DAT 0xeb00 21 22 #define J1939_TP_CMD_RTS 0x10 23 #define J1939_TP_CMD_CTS 0x11 24 #define J1939_TP_CMD_EOMA 0x13 25 #define J1939_TP_CMD_BAM 0x20 26 #define J1939_TP_CMD_ABORT 0xff 27 28 #define J1939_ETP_CMD_RTS 0x14 29 #define J1939_ETP_CMD_CTS 0x15 30 #define J1939_ETP_CMD_DPO 0x16 31 #define J1939_ETP_CMD_EOMA 0x17 32 #define J1939_ETP_CMD_ABORT 0xff 33 34 enum j1939_xtp_abort { 35 J1939_XTP_NO_ABORT = 0, 36 J1939_XTP_ABORT_BUSY = 1, 37 /* Already in one or more connection managed sessions and 38 * cannot support another. 39 * 40 * EALREADY: 41 * Operation already in progress 42 */ 43 44 J1939_XTP_ABORT_RESOURCE = 2, 45 /* System resources were needed for another task so this 46 * connection managed session was terminated. 47 * 48 * EMSGSIZE: 49 * The socket type requires that message be sent atomically, 50 * and the size of the message to be sent made this 51 * impossible. 52 */ 53 54 J1939_XTP_ABORT_TIMEOUT = 3, 55 /* A timeout occurred and this is the connection abort to 56 * close the session. 57 * 58 * EHOSTUNREACH: 59 * The destination host cannot be reached (probably because 60 * the host is down or a remote router cannot reach it). 61 */ 62 63 J1939_XTP_ABORT_GENERIC = 4, 64 /* CTS messages received when data transfer is in progress 65 * 66 * EBADMSG: 67 * Not a data message 68 */ 69 70 J1939_XTP_ABORT_FAULT = 5, 71 /* Maximal retransmit request limit reached 72 * 73 * ENOTRECOVERABLE: 74 * State not recoverable 75 */ 76 77 J1939_XTP_ABORT_UNEXPECTED_DATA = 6, 78 /* Unexpected data transfer packet 79 * 80 * ENOTCONN: 81 * Transport endpoint is not connected 82 */ 83 84 J1939_XTP_ABORT_BAD_SEQ = 7, 85 /* Bad sequence number (and software is not able to recover) 86 * 87 * EILSEQ: 88 * Illegal byte sequence 89 */ 90 91 J1939_XTP_ABORT_DUP_SEQ = 8, 92 /* Duplicate sequence number (and software is not able to 93 * recover) 94 */ 95 96 J1939_XTP_ABORT_EDPO_UNEXPECTED = 9, 97 /* Unexpected EDPO packet (ETP) or Message size > 1785 bytes 98 * (TP) 99 */ 100 101 J1939_XTP_ABORT_BAD_EDPO_PGN = 10, 102 /* Unexpected EDPO PGN (PGN in EDPO is bad) */ 103 104 J1939_XTP_ABORT_EDPO_OUTOF_CTS = 11, 105 /* EDPO number of packets is greater than CTS */ 106 107 J1939_XTP_ABORT_BAD_EDPO_OFFSET = 12, 108 /* Bad EDPO offset */ 109 110 J1939_XTP_ABORT_OTHER_DEPRECATED = 13, 111 /* Deprecated. Use 250 instead (Any other reason) */ 112 113 J1939_XTP_ABORT_ECTS_UNXPECTED_PGN = 14, 114 /* Unexpected ECTS PGN (PGN in ECTS is bad) */ 115 116 J1939_XTP_ABORT_ECTS_TOO_BIG = 15, 117 /* ECTS requested packets exceeds message size */ 118 119 J1939_XTP_ABORT_OTHER = 250, 120 /* Any other reason (if a Connection Abort reason is 121 * identified that is not listed in the table use code 250) 122 */ 123 }; 124 125 static unsigned int j1939_tp_block = 255; 126 static unsigned int j1939_tp_packet_delay; 127 static unsigned int j1939_tp_padding = 1; 128 129 /* helpers */ 130 static const char *j1939_xtp_abort_to_str(enum j1939_xtp_abort abort) 131 { 132 switch (abort) { 133 case J1939_XTP_ABORT_BUSY: 134 return "Already in one or more connection managed sessions and cannot support another."; 135 case J1939_XTP_ABORT_RESOURCE: 136 return "System resources were needed for another task so this connection managed session was terminated."; 137 case J1939_XTP_ABORT_TIMEOUT: 138 return "A timeout occurred and this is the connection abort to close the session."; 139 case J1939_XTP_ABORT_GENERIC: 140 return "CTS messages received when data transfer is in progress"; 141 case J1939_XTP_ABORT_FAULT: 142 return "Maximal retransmit request limit reached"; 143 case J1939_XTP_ABORT_UNEXPECTED_DATA: 144 return "Unexpected data transfer packet"; 145 case J1939_XTP_ABORT_BAD_SEQ: 146 return "Bad sequence number (and software is not able to recover)"; 147 case J1939_XTP_ABORT_DUP_SEQ: 148 return "Duplicate sequence number (and software is not able to recover)"; 149 case J1939_XTP_ABORT_EDPO_UNEXPECTED: 150 return "Unexpected EDPO packet (ETP) or Message size > 1785 bytes (TP)"; 151 case J1939_XTP_ABORT_BAD_EDPO_PGN: 152 return "Unexpected EDPO PGN (PGN in EDPO is bad)"; 153 case J1939_XTP_ABORT_EDPO_OUTOF_CTS: 154 return "EDPO number of packets is greater than CTS"; 155 case J1939_XTP_ABORT_BAD_EDPO_OFFSET: 156 return "Bad EDPO offset"; 157 case J1939_XTP_ABORT_OTHER_DEPRECATED: 158 return "Deprecated. Use 250 instead (Any other reason)"; 159 case J1939_XTP_ABORT_ECTS_UNXPECTED_PGN: 160 return "Unexpected ECTS PGN (PGN in ECTS is bad)"; 161 case J1939_XTP_ABORT_ECTS_TOO_BIG: 162 return "ECTS requested packets exceeds message size"; 163 case J1939_XTP_ABORT_OTHER: 164 return "Any other reason (if a Connection Abort reason is identified that is not listed in the table use code 250)"; 165 default: 166 return "<unknown>"; 167 } 168 } 169 170 static int j1939_xtp_abort_to_errno(struct j1939_priv *priv, 171 enum j1939_xtp_abort abort) 172 { 173 int err; 174 175 switch (abort) { 176 case J1939_XTP_NO_ABORT: 177 WARN_ON_ONCE(abort == J1939_XTP_NO_ABORT); 178 err = 0; 179 break; 180 case J1939_XTP_ABORT_BUSY: 181 err = EALREADY; 182 break; 183 case J1939_XTP_ABORT_RESOURCE: 184 err = EMSGSIZE; 185 break; 186 case J1939_XTP_ABORT_TIMEOUT: 187 err = EHOSTUNREACH; 188 break; 189 case J1939_XTP_ABORT_GENERIC: 190 err = EBADMSG; 191 break; 192 case J1939_XTP_ABORT_FAULT: 193 err = ENOTRECOVERABLE; 194 break; 195 case J1939_XTP_ABORT_UNEXPECTED_DATA: 196 err = ENOTCONN; 197 break; 198 case J1939_XTP_ABORT_BAD_SEQ: 199 err = EILSEQ; 200 break; 201 case J1939_XTP_ABORT_DUP_SEQ: 202 err = EPROTO; 203 break; 204 case J1939_XTP_ABORT_EDPO_UNEXPECTED: 205 err = EPROTO; 206 break; 207 case J1939_XTP_ABORT_BAD_EDPO_PGN: 208 err = EPROTO; 209 break; 210 case J1939_XTP_ABORT_EDPO_OUTOF_CTS: 211 err = EPROTO; 212 break; 213 case J1939_XTP_ABORT_BAD_EDPO_OFFSET: 214 err = EPROTO; 215 break; 216 case J1939_XTP_ABORT_OTHER_DEPRECATED: 217 err = EPROTO; 218 break; 219 case J1939_XTP_ABORT_ECTS_UNXPECTED_PGN: 220 err = EPROTO; 221 break; 222 case J1939_XTP_ABORT_ECTS_TOO_BIG: 223 err = EPROTO; 224 break; 225 case J1939_XTP_ABORT_OTHER: 226 err = EPROTO; 227 break; 228 default: 229 netdev_warn(priv->ndev, "Unknown abort code %i", abort); 230 err = EPROTO; 231 } 232 233 return err; 234 } 235 236 static inline void j1939_session_list_lock(struct j1939_priv *priv) 237 { 238 spin_lock_bh(&priv->active_session_list_lock); 239 } 240 241 static inline void j1939_session_list_unlock(struct j1939_priv *priv) 242 { 243 spin_unlock_bh(&priv->active_session_list_lock); 244 } 245 246 void j1939_session_get(struct j1939_session *session) 247 { 248 kref_get(&session->kref); 249 } 250 251 /* session completion functions */ 252 static void __j1939_session_drop(struct j1939_session *session) 253 { 254 if (!session->transmission) 255 return; 256 257 j1939_sock_pending_del(session->sk); 258 sock_put(session->sk); 259 } 260 261 static void j1939_session_destroy(struct j1939_session *session) 262 { 263 if (session->err) 264 j1939_sk_errqueue(session, J1939_ERRQUEUE_ABORT); 265 else 266 j1939_sk_errqueue(session, J1939_ERRQUEUE_ACK); 267 268 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session); 269 270 WARN_ON_ONCE(!list_empty(&session->sk_session_queue_entry)); 271 WARN_ON_ONCE(!list_empty(&session->active_session_list_entry)); 272 273 skb_queue_purge(&session->skb_queue); 274 __j1939_session_drop(session); 275 j1939_priv_put(session->priv); 276 kfree(session); 277 } 278 279 static void __j1939_session_release(struct kref *kref) 280 { 281 struct j1939_session *session = container_of(kref, struct j1939_session, 282 kref); 283 284 j1939_session_destroy(session); 285 } 286 287 void j1939_session_put(struct j1939_session *session) 288 { 289 kref_put(&session->kref, __j1939_session_release); 290 } 291 292 static void j1939_session_txtimer_cancel(struct j1939_session *session) 293 { 294 if (hrtimer_cancel(&session->txtimer)) 295 j1939_session_put(session); 296 } 297 298 static void j1939_session_rxtimer_cancel(struct j1939_session *session) 299 { 300 if (hrtimer_cancel(&session->rxtimer)) 301 j1939_session_put(session); 302 } 303 304 void j1939_session_timers_cancel(struct j1939_session *session) 305 { 306 j1939_session_txtimer_cancel(session); 307 j1939_session_rxtimer_cancel(session); 308 } 309 310 static inline bool j1939_cb_is_broadcast(const struct j1939_sk_buff_cb *skcb) 311 { 312 return (!skcb->addr.dst_name && (skcb->addr.da == 0xff)); 313 } 314 315 static void j1939_session_skb_drop_old(struct j1939_session *session) 316 { 317 struct sk_buff *do_skb; 318 struct j1939_sk_buff_cb *do_skcb; 319 unsigned int offset_start; 320 unsigned long flags; 321 322 if (skb_queue_len(&session->skb_queue) < 2) 323 return; 324 325 offset_start = session->pkt.tx_acked * 7; 326 327 spin_lock_irqsave(&session->skb_queue.lock, flags); 328 do_skb = skb_peek(&session->skb_queue); 329 do_skcb = j1939_skb_to_cb(do_skb); 330 331 if ((do_skcb->offset + do_skb->len) < offset_start) { 332 __skb_unlink(do_skb, &session->skb_queue); 333 /* drop ref taken in j1939_session_skb_queue() */ 334 skb_unref(do_skb); 335 336 kfree_skb(do_skb); 337 } 338 spin_unlock_irqrestore(&session->skb_queue.lock, flags); 339 } 340 341 void j1939_session_skb_queue(struct j1939_session *session, 342 struct sk_buff *skb) 343 { 344 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb); 345 struct j1939_priv *priv = session->priv; 346 347 j1939_ac_fixup(priv, skb); 348 349 if (j1939_address_is_unicast(skcb->addr.da) && 350 priv->ents[skcb->addr.da].nusers) 351 skcb->flags |= J1939_ECU_LOCAL_DST; 352 353 skcb->flags |= J1939_ECU_LOCAL_SRC; 354 355 skb_get(skb); 356 skb_queue_tail(&session->skb_queue, skb); 357 } 358 359 static struct 360 sk_buff *j1939_session_skb_get_by_offset(struct j1939_session *session, 361 unsigned int offset_start) 362 { 363 struct j1939_priv *priv = session->priv; 364 struct j1939_sk_buff_cb *do_skcb; 365 struct sk_buff *skb = NULL; 366 struct sk_buff *do_skb; 367 unsigned long flags; 368 369 spin_lock_irqsave(&session->skb_queue.lock, flags); 370 skb_queue_walk(&session->skb_queue, do_skb) { 371 do_skcb = j1939_skb_to_cb(do_skb); 372 373 if (offset_start >= do_skcb->offset && 374 offset_start < (do_skcb->offset + do_skb->len)) { 375 skb = do_skb; 376 } 377 } 378 379 if (skb) 380 skb_get(skb); 381 382 spin_unlock_irqrestore(&session->skb_queue.lock, flags); 383 384 if (!skb) 385 netdev_dbg(priv->ndev, "%s: 0x%p: no skb found for start: %i, queue size: %i\n", 386 __func__, session, offset_start, 387 skb_queue_len(&session->skb_queue)); 388 389 return skb; 390 } 391 392 static struct sk_buff *j1939_session_skb_get(struct j1939_session *session) 393 { 394 unsigned int offset_start; 395 396 offset_start = session->pkt.dpo * 7; 397 return j1939_session_skb_get_by_offset(session, offset_start); 398 } 399 400 /* see if we are receiver 401 * returns 0 for broadcasts, although we will receive them 402 */ 403 static inline int j1939_tp_im_receiver(const struct j1939_sk_buff_cb *skcb) 404 { 405 return skcb->flags & J1939_ECU_LOCAL_DST; 406 } 407 408 /* see if we are sender */ 409 static inline int j1939_tp_im_transmitter(const struct j1939_sk_buff_cb *skcb) 410 { 411 return skcb->flags & J1939_ECU_LOCAL_SRC; 412 } 413 414 /* see if we are involved as either receiver or transmitter */ 415 static int j1939_tp_im_involved(const struct j1939_sk_buff_cb *skcb, bool swap) 416 { 417 if (swap) 418 return j1939_tp_im_receiver(skcb); 419 else 420 return j1939_tp_im_transmitter(skcb); 421 } 422 423 static int j1939_tp_im_involved_anydir(struct j1939_sk_buff_cb *skcb) 424 { 425 return skcb->flags & (J1939_ECU_LOCAL_SRC | J1939_ECU_LOCAL_DST); 426 } 427 428 /* extract pgn from flow-ctl message */ 429 static inline pgn_t j1939_xtp_ctl_to_pgn(const u8 *dat) 430 { 431 pgn_t pgn; 432 433 pgn = (dat[7] << 16) | (dat[6] << 8) | (dat[5] << 0); 434 if (j1939_pgn_is_pdu1(pgn)) 435 pgn &= 0xffff00; 436 return pgn; 437 } 438 439 static inline unsigned int j1939_tp_ctl_to_size(const u8 *dat) 440 { 441 return (dat[2] << 8) + (dat[1] << 0); 442 } 443 444 static inline unsigned int j1939_etp_ctl_to_packet(const u8 *dat) 445 { 446 return (dat[4] << 16) | (dat[3] << 8) | (dat[2] << 0); 447 } 448 449 static inline unsigned int j1939_etp_ctl_to_size(const u8 *dat) 450 { 451 return (dat[4] << 24) | (dat[3] << 16) | 452 (dat[2] << 8) | (dat[1] << 0); 453 } 454 455 /* find existing session: 456 * reverse: swap cb's src & dst 457 * there is no problem with matching broadcasts, since 458 * broadcasts (no dst, no da) would never call this 459 * with reverse == true 460 */ 461 static bool j1939_session_match(struct j1939_addr *se_addr, 462 struct j1939_addr *sk_addr, bool reverse) 463 { 464 if (se_addr->type != sk_addr->type) 465 return false; 466 467 if (reverse) { 468 if (se_addr->src_name) { 469 if (se_addr->src_name != sk_addr->dst_name) 470 return false; 471 } else if (se_addr->sa != sk_addr->da) { 472 return false; 473 } 474 475 if (se_addr->dst_name) { 476 if (se_addr->dst_name != sk_addr->src_name) 477 return false; 478 } else if (se_addr->da != sk_addr->sa) { 479 return false; 480 } 481 } else { 482 if (se_addr->src_name) { 483 if (se_addr->src_name != sk_addr->src_name) 484 return false; 485 } else if (se_addr->sa != sk_addr->sa) { 486 return false; 487 } 488 489 if (se_addr->dst_name) { 490 if (se_addr->dst_name != sk_addr->dst_name) 491 return false; 492 } else if (se_addr->da != sk_addr->da) { 493 return false; 494 } 495 } 496 497 return true; 498 } 499 500 static struct 501 j1939_session *j1939_session_get_by_addr_locked(struct j1939_priv *priv, 502 struct list_head *root, 503 struct j1939_addr *addr, 504 bool reverse, bool transmitter) 505 { 506 struct j1939_session *session; 507 508 lockdep_assert_held(&priv->active_session_list_lock); 509 510 list_for_each_entry(session, root, active_session_list_entry) { 511 j1939_session_get(session); 512 if (j1939_session_match(&session->skcb.addr, addr, reverse) && 513 session->transmission == transmitter) 514 return session; 515 j1939_session_put(session); 516 } 517 518 return NULL; 519 } 520 521 static struct 522 j1939_session *j1939_session_get_simple(struct j1939_priv *priv, 523 struct sk_buff *skb) 524 { 525 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb); 526 struct j1939_session *session; 527 528 lockdep_assert_held(&priv->active_session_list_lock); 529 530 list_for_each_entry(session, &priv->active_session_list, 531 active_session_list_entry) { 532 j1939_session_get(session); 533 if (session->skcb.addr.type == J1939_SIMPLE && 534 session->tskey == skcb->tskey && session->sk == skb->sk) 535 return session; 536 j1939_session_put(session); 537 } 538 539 return NULL; 540 } 541 542 static struct 543 j1939_session *j1939_session_get_by_addr(struct j1939_priv *priv, 544 struct j1939_addr *addr, 545 bool reverse, bool transmitter) 546 { 547 struct j1939_session *session; 548 549 j1939_session_list_lock(priv); 550 session = j1939_session_get_by_addr_locked(priv, 551 &priv->active_session_list, 552 addr, reverse, transmitter); 553 j1939_session_list_unlock(priv); 554 555 return session; 556 } 557 558 static void j1939_skbcb_swap(struct j1939_sk_buff_cb *skcb) 559 { 560 u8 tmp = 0; 561 562 swap(skcb->addr.dst_name, skcb->addr.src_name); 563 swap(skcb->addr.da, skcb->addr.sa); 564 565 /* swap SRC and DST flags, leave other untouched */ 566 if (skcb->flags & J1939_ECU_LOCAL_SRC) 567 tmp |= J1939_ECU_LOCAL_DST; 568 if (skcb->flags & J1939_ECU_LOCAL_DST) 569 tmp |= J1939_ECU_LOCAL_SRC; 570 skcb->flags &= ~(J1939_ECU_LOCAL_SRC | J1939_ECU_LOCAL_DST); 571 skcb->flags |= tmp; 572 } 573 574 static struct 575 sk_buff *j1939_tp_tx_dat_new(struct j1939_priv *priv, 576 const struct j1939_sk_buff_cb *re_skcb, 577 bool ctl, 578 bool swap_src_dst) 579 { 580 struct sk_buff *skb; 581 struct j1939_sk_buff_cb *skcb; 582 583 skb = alloc_skb(sizeof(struct can_frame) + sizeof(struct can_skb_priv), 584 GFP_ATOMIC); 585 if (unlikely(!skb)) 586 return ERR_PTR(-ENOMEM); 587 588 skb->dev = priv->ndev; 589 can_skb_reserve(skb); 590 can_skb_prv(skb)->ifindex = priv->ndev->ifindex; 591 can_skb_prv(skb)->skbcnt = 0; 592 /* reserve CAN header */ 593 skb_reserve(skb, offsetof(struct can_frame, data)); 594 595 memcpy(skb->cb, re_skcb, sizeof(skb->cb)); 596 skcb = j1939_skb_to_cb(skb); 597 if (swap_src_dst) 598 j1939_skbcb_swap(skcb); 599 600 if (ctl) { 601 if (skcb->addr.type == J1939_ETP) 602 skcb->addr.pgn = J1939_ETP_PGN_CTL; 603 else 604 skcb->addr.pgn = J1939_TP_PGN_CTL; 605 } else { 606 if (skcb->addr.type == J1939_ETP) 607 skcb->addr.pgn = J1939_ETP_PGN_DAT; 608 else 609 skcb->addr.pgn = J1939_TP_PGN_DAT; 610 } 611 612 return skb; 613 } 614 615 /* TP transmit packet functions */ 616 static int j1939_tp_tx_dat(struct j1939_session *session, 617 const u8 *dat, int len) 618 { 619 struct j1939_priv *priv = session->priv; 620 struct sk_buff *skb; 621 622 skb = j1939_tp_tx_dat_new(priv, &session->skcb, 623 false, false); 624 if (IS_ERR(skb)) 625 return PTR_ERR(skb); 626 627 skb_put_data(skb, dat, len); 628 if (j1939_tp_padding && len < 8) 629 memset(skb_put(skb, 8 - len), 0xff, 8 - len); 630 631 return j1939_send_one(priv, skb); 632 } 633 634 static int j1939_xtp_do_tx_ctl(struct j1939_priv *priv, 635 const struct j1939_sk_buff_cb *re_skcb, 636 bool swap_src_dst, pgn_t pgn, const u8 *dat) 637 { 638 struct sk_buff *skb; 639 u8 *skdat; 640 641 if (!j1939_tp_im_involved(re_skcb, swap_src_dst)) 642 return 0; 643 644 skb = j1939_tp_tx_dat_new(priv, re_skcb, true, swap_src_dst); 645 if (IS_ERR(skb)) 646 return PTR_ERR(skb); 647 648 skdat = skb_put(skb, 8); 649 memcpy(skdat, dat, 5); 650 skdat[5] = (pgn >> 0); 651 skdat[6] = (pgn >> 8); 652 skdat[7] = (pgn >> 16); 653 654 return j1939_send_one(priv, skb); 655 } 656 657 static inline int j1939_tp_tx_ctl(struct j1939_session *session, 658 bool swap_src_dst, const u8 *dat) 659 { 660 struct j1939_priv *priv = session->priv; 661 662 return j1939_xtp_do_tx_ctl(priv, &session->skcb, 663 swap_src_dst, 664 session->skcb.addr.pgn, dat); 665 } 666 667 static int j1939_xtp_tx_abort(struct j1939_priv *priv, 668 const struct j1939_sk_buff_cb *re_skcb, 669 bool swap_src_dst, 670 enum j1939_xtp_abort err, 671 pgn_t pgn) 672 { 673 u8 dat[5]; 674 675 if (!j1939_tp_im_involved(re_skcb, swap_src_dst)) 676 return 0; 677 678 memset(dat, 0xff, sizeof(dat)); 679 dat[0] = J1939_TP_CMD_ABORT; 680 dat[1] = err; 681 return j1939_xtp_do_tx_ctl(priv, re_skcb, swap_src_dst, pgn, dat); 682 } 683 684 void j1939_tp_schedule_txtimer(struct j1939_session *session, int msec) 685 { 686 j1939_session_get(session); 687 hrtimer_start(&session->txtimer, ms_to_ktime(msec), 688 HRTIMER_MODE_REL_SOFT); 689 } 690 691 static inline void j1939_tp_set_rxtimeout(struct j1939_session *session, 692 int msec) 693 { 694 j1939_session_rxtimer_cancel(session); 695 j1939_session_get(session); 696 hrtimer_start(&session->rxtimer, ms_to_ktime(msec), 697 HRTIMER_MODE_REL_SOFT); 698 } 699 700 static int j1939_session_tx_rts(struct j1939_session *session) 701 { 702 u8 dat[8]; 703 int ret; 704 705 memset(dat, 0xff, sizeof(dat)); 706 707 dat[1] = (session->total_message_size >> 0); 708 dat[2] = (session->total_message_size >> 8); 709 dat[3] = session->pkt.total; 710 711 if (session->skcb.addr.type == J1939_ETP) { 712 dat[0] = J1939_ETP_CMD_RTS; 713 dat[1] = (session->total_message_size >> 0); 714 dat[2] = (session->total_message_size >> 8); 715 dat[3] = (session->total_message_size >> 16); 716 dat[4] = (session->total_message_size >> 24); 717 } else if (j1939_cb_is_broadcast(&session->skcb)) { 718 dat[0] = J1939_TP_CMD_BAM; 719 /* fake cts for broadcast */ 720 session->pkt.tx = 0; 721 } else { 722 dat[0] = J1939_TP_CMD_RTS; 723 dat[4] = dat[3]; 724 } 725 726 if (dat[0] == session->last_txcmd) 727 /* done already */ 728 return 0; 729 730 ret = j1939_tp_tx_ctl(session, false, dat); 731 if (ret < 0) 732 return ret; 733 734 session->last_txcmd = dat[0]; 735 if (dat[0] == J1939_TP_CMD_BAM) { 736 j1939_tp_schedule_txtimer(session, 50); 737 j1939_tp_set_rxtimeout(session, 250); 738 } else { 739 j1939_tp_set_rxtimeout(session, 1250); 740 } 741 742 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session); 743 744 return 0; 745 } 746 747 static int j1939_session_tx_dpo(struct j1939_session *session) 748 { 749 unsigned int pkt; 750 u8 dat[8]; 751 int ret; 752 753 memset(dat, 0xff, sizeof(dat)); 754 755 dat[0] = J1939_ETP_CMD_DPO; 756 session->pkt.dpo = session->pkt.tx_acked; 757 pkt = session->pkt.dpo; 758 dat[1] = session->pkt.last - session->pkt.tx_acked; 759 dat[2] = (pkt >> 0); 760 dat[3] = (pkt >> 8); 761 dat[4] = (pkt >> 16); 762 763 ret = j1939_tp_tx_ctl(session, false, dat); 764 if (ret < 0) 765 return ret; 766 767 session->last_txcmd = dat[0]; 768 j1939_tp_set_rxtimeout(session, 1250); 769 session->pkt.tx = session->pkt.tx_acked; 770 771 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session); 772 773 return 0; 774 } 775 776 static int j1939_session_tx_dat(struct j1939_session *session) 777 { 778 struct j1939_priv *priv = session->priv; 779 struct j1939_sk_buff_cb *skcb; 780 int offset, pkt_done, pkt_end; 781 unsigned int len, pdelay; 782 struct sk_buff *se_skb; 783 const u8 *tpdat; 784 int ret = 0; 785 u8 dat[8]; 786 787 se_skb = j1939_session_skb_get_by_offset(session, session->pkt.tx * 7); 788 if (!se_skb) 789 return -ENOBUFS; 790 791 skcb = j1939_skb_to_cb(se_skb); 792 tpdat = se_skb->data; 793 ret = 0; 794 pkt_done = 0; 795 if (session->skcb.addr.type != J1939_ETP && 796 j1939_cb_is_broadcast(&session->skcb)) 797 pkt_end = session->pkt.total; 798 else 799 pkt_end = session->pkt.last; 800 801 while (session->pkt.tx < pkt_end) { 802 dat[0] = session->pkt.tx - session->pkt.dpo + 1; 803 offset = (session->pkt.tx * 7) - skcb->offset; 804 len = se_skb->len - offset; 805 if (len > 7) 806 len = 7; 807 808 if (offset + len > se_skb->len) { 809 netdev_err_once(priv->ndev, 810 "%s: 0x%p: requested data outside of queued buffer: offset %i, len %i, pkt.tx: %i\n", 811 __func__, session, skcb->offset, se_skb->len , session->pkt.tx); 812 ret = -EOVERFLOW; 813 goto out_free; 814 } 815 816 if (!len) { 817 ret = -ENOBUFS; 818 break; 819 } 820 821 memcpy(&dat[1], &tpdat[offset], len); 822 ret = j1939_tp_tx_dat(session, dat, len + 1); 823 if (ret < 0) { 824 /* ENOBUS == CAN interface TX queue is full */ 825 if (ret != -ENOBUFS) 826 netdev_alert(priv->ndev, 827 "%s: 0x%p: queue data error: %i\n", 828 __func__, session, ret); 829 break; 830 } 831 832 session->last_txcmd = 0xff; 833 pkt_done++; 834 session->pkt.tx++; 835 pdelay = j1939_cb_is_broadcast(&session->skcb) ? 50 : 836 j1939_tp_packet_delay; 837 838 if (session->pkt.tx < session->pkt.total && pdelay) { 839 j1939_tp_schedule_txtimer(session, pdelay); 840 break; 841 } 842 } 843 844 if (pkt_done) 845 j1939_tp_set_rxtimeout(session, 250); 846 847 out_free: 848 if (ret) 849 kfree_skb(se_skb); 850 else 851 consume_skb(se_skb); 852 853 return ret; 854 } 855 856 static int j1939_xtp_txnext_transmiter(struct j1939_session *session) 857 { 858 struct j1939_priv *priv = session->priv; 859 int ret = 0; 860 861 if (!j1939_tp_im_transmitter(&session->skcb)) { 862 netdev_alert(priv->ndev, "%s: 0x%p: called by not transmitter!\n", 863 __func__, session); 864 return -EINVAL; 865 } 866 867 switch (session->last_cmd) { 868 case 0: 869 ret = j1939_session_tx_rts(session); 870 break; 871 872 case J1939_ETP_CMD_CTS: 873 if (session->last_txcmd != J1939_ETP_CMD_DPO) { 874 ret = j1939_session_tx_dpo(session); 875 if (ret) 876 return ret; 877 } 878 879 fallthrough; 880 case J1939_TP_CMD_CTS: 881 case 0xff: /* did some data */ 882 case J1939_ETP_CMD_DPO: 883 case J1939_TP_CMD_BAM: 884 ret = j1939_session_tx_dat(session); 885 886 break; 887 default: 888 netdev_alert(priv->ndev, "%s: 0x%p: unexpected last_cmd: %x\n", 889 __func__, session, session->last_cmd); 890 } 891 892 return ret; 893 } 894 895 static int j1939_session_tx_cts(struct j1939_session *session) 896 { 897 struct j1939_priv *priv = session->priv; 898 unsigned int pkt, len; 899 int ret; 900 u8 dat[8]; 901 902 if (!j1939_sk_recv_match(priv, &session->skcb)) 903 return -ENOENT; 904 905 len = session->pkt.total - session->pkt.rx; 906 len = min3(len, session->pkt.block, j1939_tp_block ?: 255); 907 memset(dat, 0xff, sizeof(dat)); 908 909 if (session->skcb.addr.type == J1939_ETP) { 910 pkt = session->pkt.rx + 1; 911 dat[0] = J1939_ETP_CMD_CTS; 912 dat[1] = len; 913 dat[2] = (pkt >> 0); 914 dat[3] = (pkt >> 8); 915 dat[4] = (pkt >> 16); 916 } else { 917 dat[0] = J1939_TP_CMD_CTS; 918 dat[1] = len; 919 dat[2] = session->pkt.rx + 1; 920 } 921 922 if (dat[0] == session->last_txcmd) 923 /* done already */ 924 return 0; 925 926 ret = j1939_tp_tx_ctl(session, true, dat); 927 if (ret < 0) 928 return ret; 929 930 if (len) 931 /* only mark cts done when len is set */ 932 session->last_txcmd = dat[0]; 933 j1939_tp_set_rxtimeout(session, 1250); 934 935 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session); 936 937 return 0; 938 } 939 940 static int j1939_session_tx_eoma(struct j1939_session *session) 941 { 942 struct j1939_priv *priv = session->priv; 943 u8 dat[8]; 944 int ret; 945 946 if (!j1939_sk_recv_match(priv, &session->skcb)) 947 return -ENOENT; 948 949 memset(dat, 0xff, sizeof(dat)); 950 951 if (session->skcb.addr.type == J1939_ETP) { 952 dat[0] = J1939_ETP_CMD_EOMA; 953 dat[1] = session->total_message_size >> 0; 954 dat[2] = session->total_message_size >> 8; 955 dat[3] = session->total_message_size >> 16; 956 dat[4] = session->total_message_size >> 24; 957 } else { 958 dat[0] = J1939_TP_CMD_EOMA; 959 dat[1] = session->total_message_size; 960 dat[2] = session->total_message_size >> 8; 961 dat[3] = session->pkt.total; 962 } 963 964 if (dat[0] == session->last_txcmd) 965 /* done already */ 966 return 0; 967 968 ret = j1939_tp_tx_ctl(session, true, dat); 969 if (ret < 0) 970 return ret; 971 972 session->last_txcmd = dat[0]; 973 974 /* wait for the EOMA packet to come in */ 975 j1939_tp_set_rxtimeout(session, 1250); 976 977 netdev_dbg(session->priv->ndev, "%p: 0x%p\n", __func__, session); 978 979 return 0; 980 } 981 982 static int j1939_xtp_txnext_receiver(struct j1939_session *session) 983 { 984 struct j1939_priv *priv = session->priv; 985 int ret = 0; 986 987 if (!j1939_tp_im_receiver(&session->skcb)) { 988 netdev_alert(priv->ndev, "%s: 0x%p: called by not receiver!\n", 989 __func__, session); 990 return -EINVAL; 991 } 992 993 switch (session->last_cmd) { 994 case J1939_TP_CMD_RTS: 995 case J1939_ETP_CMD_RTS: 996 ret = j1939_session_tx_cts(session); 997 break; 998 999 case J1939_ETP_CMD_CTS: 1000 case J1939_TP_CMD_CTS: 1001 case 0xff: /* did some data */ 1002 case J1939_ETP_CMD_DPO: 1003 if ((session->skcb.addr.type == J1939_TP && 1004 j1939_cb_is_broadcast(&session->skcb))) 1005 break; 1006 1007 if (session->pkt.rx >= session->pkt.total) { 1008 ret = j1939_session_tx_eoma(session); 1009 } else if (session->pkt.rx >= session->pkt.last) { 1010 session->last_txcmd = 0; 1011 ret = j1939_session_tx_cts(session); 1012 } 1013 break; 1014 default: 1015 netdev_alert(priv->ndev, "%s: 0x%p: unexpected last_cmd: %x\n", 1016 __func__, session, session->last_cmd); 1017 } 1018 1019 return ret; 1020 } 1021 1022 static int j1939_simple_txnext(struct j1939_session *session) 1023 { 1024 struct j1939_priv *priv = session->priv; 1025 struct sk_buff *se_skb = j1939_session_skb_get(session); 1026 struct sk_buff *skb; 1027 int ret; 1028 1029 if (!se_skb) 1030 return 0; 1031 1032 skb = skb_clone(se_skb, GFP_ATOMIC); 1033 if (!skb) { 1034 ret = -ENOMEM; 1035 goto out_free; 1036 } 1037 1038 can_skb_set_owner(skb, se_skb->sk); 1039 1040 j1939_tp_set_rxtimeout(session, J1939_SIMPLE_ECHO_TIMEOUT_MS); 1041 1042 ret = j1939_send_one(priv, skb); 1043 if (ret) 1044 goto out_free; 1045 1046 j1939_sk_errqueue(session, J1939_ERRQUEUE_SCHED); 1047 j1939_sk_queue_activate_next(session); 1048 1049 out_free: 1050 if (ret) 1051 kfree_skb(se_skb); 1052 else 1053 consume_skb(se_skb); 1054 1055 return ret; 1056 } 1057 1058 static bool j1939_session_deactivate_locked(struct j1939_session *session) 1059 { 1060 bool active = false; 1061 1062 lockdep_assert_held(&session->priv->active_session_list_lock); 1063 1064 if (session->state >= J1939_SESSION_ACTIVE && 1065 session->state < J1939_SESSION_ACTIVE_MAX) { 1066 active = true; 1067 1068 list_del_init(&session->active_session_list_entry); 1069 session->state = J1939_SESSION_DONE; 1070 j1939_session_put(session); 1071 } 1072 1073 return active; 1074 } 1075 1076 static bool j1939_session_deactivate(struct j1939_session *session) 1077 { 1078 bool active; 1079 1080 j1939_session_list_lock(session->priv); 1081 active = j1939_session_deactivate_locked(session); 1082 j1939_session_list_unlock(session->priv); 1083 1084 return active; 1085 } 1086 1087 static void 1088 j1939_session_deactivate_activate_next(struct j1939_session *session) 1089 { 1090 if (j1939_session_deactivate(session)) 1091 j1939_sk_queue_activate_next(session); 1092 } 1093 1094 static void __j1939_session_cancel(struct j1939_session *session, 1095 enum j1939_xtp_abort err) 1096 { 1097 struct j1939_priv *priv = session->priv; 1098 1099 WARN_ON_ONCE(!err); 1100 lockdep_assert_held(&session->priv->active_session_list_lock); 1101 1102 session->err = j1939_xtp_abort_to_errno(priv, err); 1103 session->state = J1939_SESSION_WAITING_ABORT; 1104 /* do not send aborts on incoming broadcasts */ 1105 if (!j1939_cb_is_broadcast(&session->skcb)) { 1106 j1939_xtp_tx_abort(priv, &session->skcb, 1107 !session->transmission, 1108 err, session->skcb.addr.pgn); 1109 } 1110 1111 if (session->sk) 1112 j1939_sk_send_loop_abort(session->sk, session->err); 1113 } 1114 1115 static void j1939_session_cancel(struct j1939_session *session, 1116 enum j1939_xtp_abort err) 1117 { 1118 j1939_session_list_lock(session->priv); 1119 1120 if (session->state >= J1939_SESSION_ACTIVE && 1121 session->state < J1939_SESSION_WAITING_ABORT) { 1122 j1939_tp_set_rxtimeout(session, J1939_XTP_ABORT_TIMEOUT_MS); 1123 __j1939_session_cancel(session, err); 1124 } 1125 1126 j1939_session_list_unlock(session->priv); 1127 } 1128 1129 static enum hrtimer_restart j1939_tp_txtimer(struct hrtimer *hrtimer) 1130 { 1131 struct j1939_session *session = 1132 container_of(hrtimer, struct j1939_session, txtimer); 1133 struct j1939_priv *priv = session->priv; 1134 int ret = 0; 1135 1136 if (session->skcb.addr.type == J1939_SIMPLE) { 1137 ret = j1939_simple_txnext(session); 1138 } else { 1139 if (session->transmission) 1140 ret = j1939_xtp_txnext_transmiter(session); 1141 else 1142 ret = j1939_xtp_txnext_receiver(session); 1143 } 1144 1145 switch (ret) { 1146 case -ENOBUFS: 1147 /* Retry limit is currently arbitrary chosen */ 1148 if (session->tx_retry < J1939_XTP_TX_RETRY_LIMIT) { 1149 session->tx_retry++; 1150 j1939_tp_schedule_txtimer(session, 1151 10 + prandom_u32_max(16)); 1152 } else { 1153 netdev_alert(priv->ndev, "%s: 0x%p: tx retry count reached\n", 1154 __func__, session); 1155 session->err = -ENETUNREACH; 1156 j1939_session_rxtimer_cancel(session); 1157 j1939_session_deactivate_activate_next(session); 1158 } 1159 break; 1160 case -ENETDOWN: 1161 /* In this case we should get a netdev_event(), all active 1162 * sessions will be cleared by 1163 * j1939_cancel_all_active_sessions(). So handle this as an 1164 * error, but let j1939_cancel_all_active_sessions() do the 1165 * cleanup including propagation of the error to user space. 1166 */ 1167 break; 1168 case -EOVERFLOW: 1169 j1939_session_cancel(session, J1939_XTP_ABORT_ECTS_TOO_BIG); 1170 break; 1171 case 0: 1172 session->tx_retry = 0; 1173 break; 1174 default: 1175 netdev_alert(priv->ndev, "%s: 0x%p: tx aborted with unknown reason: %i\n", 1176 __func__, session, ret); 1177 if (session->skcb.addr.type != J1939_SIMPLE) { 1178 j1939_session_cancel(session, J1939_XTP_ABORT_OTHER); 1179 } else { 1180 session->err = ret; 1181 j1939_session_rxtimer_cancel(session); 1182 j1939_session_deactivate_activate_next(session); 1183 } 1184 } 1185 1186 j1939_session_put(session); 1187 1188 return HRTIMER_NORESTART; 1189 } 1190 1191 static void j1939_session_completed(struct j1939_session *session) 1192 { 1193 struct sk_buff *skb; 1194 1195 if (!session->transmission) { 1196 skb = j1939_session_skb_get(session); 1197 /* distribute among j1939 receivers */ 1198 j1939_sk_recv(session->priv, skb); 1199 consume_skb(skb); 1200 } 1201 1202 j1939_session_deactivate_activate_next(session); 1203 } 1204 1205 static enum hrtimer_restart j1939_tp_rxtimer(struct hrtimer *hrtimer) 1206 { 1207 struct j1939_session *session = container_of(hrtimer, 1208 struct j1939_session, 1209 rxtimer); 1210 struct j1939_priv *priv = session->priv; 1211 1212 if (session->state == J1939_SESSION_WAITING_ABORT) { 1213 netdev_alert(priv->ndev, "%s: 0x%p: abort rx timeout. Force session deactivation\n", 1214 __func__, session); 1215 1216 j1939_session_deactivate_activate_next(session); 1217 1218 } else if (session->skcb.addr.type == J1939_SIMPLE) { 1219 netdev_alert(priv->ndev, "%s: 0x%p: Timeout. Failed to send simple message.\n", 1220 __func__, session); 1221 1222 /* The message is probably stuck in the CAN controller and can 1223 * be send as soon as CAN bus is in working state again. 1224 */ 1225 session->err = -ETIME; 1226 j1939_session_deactivate(session); 1227 } else { 1228 netdev_alert(priv->ndev, "%s: 0x%p: rx timeout, send abort\n", 1229 __func__, session); 1230 1231 j1939_session_list_lock(session->priv); 1232 if (session->state >= J1939_SESSION_ACTIVE && 1233 session->state < J1939_SESSION_ACTIVE_MAX) { 1234 j1939_session_get(session); 1235 hrtimer_start(&session->rxtimer, 1236 ms_to_ktime(J1939_XTP_ABORT_TIMEOUT_MS), 1237 HRTIMER_MODE_REL_SOFT); 1238 __j1939_session_cancel(session, J1939_XTP_ABORT_TIMEOUT); 1239 } 1240 j1939_session_list_unlock(session->priv); 1241 } 1242 1243 j1939_session_put(session); 1244 1245 return HRTIMER_NORESTART; 1246 } 1247 1248 static bool j1939_xtp_rx_cmd_bad_pgn(struct j1939_session *session, 1249 const struct sk_buff *skb) 1250 { 1251 const struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb); 1252 pgn_t pgn = j1939_xtp_ctl_to_pgn(skb->data); 1253 struct j1939_priv *priv = session->priv; 1254 enum j1939_xtp_abort abort = J1939_XTP_NO_ABORT; 1255 u8 cmd = skb->data[0]; 1256 1257 if (session->skcb.addr.pgn == pgn) 1258 return false; 1259 1260 switch (cmd) { 1261 case J1939_TP_CMD_BAM: 1262 abort = J1939_XTP_NO_ABORT; 1263 break; 1264 1265 case J1939_ETP_CMD_RTS: 1266 case J1939_TP_CMD_RTS: /* fall through */ 1267 abort = J1939_XTP_ABORT_BUSY; 1268 break; 1269 1270 case J1939_ETP_CMD_CTS: 1271 case J1939_TP_CMD_CTS: /* fall through */ 1272 abort = J1939_XTP_ABORT_ECTS_UNXPECTED_PGN; 1273 break; 1274 1275 case J1939_ETP_CMD_DPO: 1276 abort = J1939_XTP_ABORT_BAD_EDPO_PGN; 1277 break; 1278 1279 case J1939_ETP_CMD_EOMA: 1280 case J1939_TP_CMD_EOMA: /* fall through */ 1281 abort = J1939_XTP_ABORT_OTHER; 1282 break; 1283 1284 case J1939_ETP_CMD_ABORT: /* && J1939_TP_CMD_ABORT */ 1285 abort = J1939_XTP_NO_ABORT; 1286 break; 1287 1288 default: 1289 WARN_ON_ONCE(1); 1290 break; 1291 } 1292 1293 netdev_warn(priv->ndev, "%s: 0x%p: CMD 0x%02x with PGN 0x%05x for running session with different PGN 0x%05x.\n", 1294 __func__, session, cmd, pgn, session->skcb.addr.pgn); 1295 if (abort != J1939_XTP_NO_ABORT) 1296 j1939_xtp_tx_abort(priv, skcb, true, abort, pgn); 1297 1298 return true; 1299 } 1300 1301 static void j1939_xtp_rx_abort_one(struct j1939_priv *priv, struct sk_buff *skb, 1302 bool reverse, bool transmitter) 1303 { 1304 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb); 1305 struct j1939_session *session; 1306 u8 abort = skb->data[1]; 1307 1308 session = j1939_session_get_by_addr(priv, &skcb->addr, reverse, 1309 transmitter); 1310 if (!session) 1311 return; 1312 1313 if (j1939_xtp_rx_cmd_bad_pgn(session, skb)) 1314 goto abort_put; 1315 1316 netdev_info(priv->ndev, "%s: 0x%p: 0x%05x: (%u) %s\n", __func__, 1317 session, j1939_xtp_ctl_to_pgn(skb->data), abort, 1318 j1939_xtp_abort_to_str(abort)); 1319 1320 j1939_session_timers_cancel(session); 1321 session->err = j1939_xtp_abort_to_errno(priv, abort); 1322 if (session->sk) 1323 j1939_sk_send_loop_abort(session->sk, session->err); 1324 j1939_session_deactivate_activate_next(session); 1325 1326 abort_put: 1327 j1939_session_put(session); 1328 } 1329 1330 /* abort packets may come in 2 directions */ 1331 static void 1332 j1939_xtp_rx_abort(struct j1939_priv *priv, struct sk_buff *skb, 1333 bool transmitter) 1334 { 1335 j1939_xtp_rx_abort_one(priv, skb, false, transmitter); 1336 j1939_xtp_rx_abort_one(priv, skb, true, transmitter); 1337 } 1338 1339 static void 1340 j1939_xtp_rx_eoma_one(struct j1939_session *session, struct sk_buff *skb) 1341 { 1342 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb); 1343 const u8 *dat; 1344 int len; 1345 1346 if (j1939_xtp_rx_cmd_bad_pgn(session, skb)) 1347 return; 1348 1349 dat = skb->data; 1350 1351 if (skcb->addr.type == J1939_ETP) 1352 len = j1939_etp_ctl_to_size(dat); 1353 else 1354 len = j1939_tp_ctl_to_size(dat); 1355 1356 if (session->total_message_size != len) { 1357 netdev_warn_once(session->priv->ndev, 1358 "%s: 0x%p: Incorrect size. Expected: %i; got: %i.\n", 1359 __func__, session, session->total_message_size, 1360 len); 1361 } 1362 1363 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session); 1364 1365 session->pkt.tx_acked = session->pkt.total; 1366 j1939_session_timers_cancel(session); 1367 /* transmitted without problems */ 1368 j1939_session_completed(session); 1369 } 1370 1371 static void 1372 j1939_xtp_rx_eoma(struct j1939_priv *priv, struct sk_buff *skb, 1373 bool transmitter) 1374 { 1375 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb); 1376 struct j1939_session *session; 1377 1378 session = j1939_session_get_by_addr(priv, &skcb->addr, true, 1379 transmitter); 1380 if (!session) 1381 return; 1382 1383 j1939_xtp_rx_eoma_one(session, skb); 1384 j1939_session_put(session); 1385 } 1386 1387 static void 1388 j1939_xtp_rx_cts_one(struct j1939_session *session, struct sk_buff *skb) 1389 { 1390 enum j1939_xtp_abort err = J1939_XTP_ABORT_FAULT; 1391 unsigned int pkt; 1392 const u8 *dat; 1393 1394 dat = skb->data; 1395 1396 if (j1939_xtp_rx_cmd_bad_pgn(session, skb)) 1397 return; 1398 1399 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session); 1400 1401 if (session->last_cmd == dat[0]) { 1402 err = J1939_XTP_ABORT_DUP_SEQ; 1403 goto out_session_cancel; 1404 } 1405 1406 if (session->skcb.addr.type == J1939_ETP) 1407 pkt = j1939_etp_ctl_to_packet(dat); 1408 else 1409 pkt = dat[2]; 1410 1411 if (!pkt) 1412 goto out_session_cancel; 1413 else if (dat[1] > session->pkt.block /* 0xff for etp */) 1414 goto out_session_cancel; 1415 1416 /* set packet counters only when not CTS(0) */ 1417 session->pkt.tx_acked = pkt - 1; 1418 j1939_session_skb_drop_old(session); 1419 session->pkt.last = session->pkt.tx_acked + dat[1]; 1420 if (session->pkt.last > session->pkt.total) 1421 /* safety measure */ 1422 session->pkt.last = session->pkt.total; 1423 /* TODO: do not set tx here, do it in txtimer */ 1424 session->pkt.tx = session->pkt.tx_acked; 1425 1426 session->last_cmd = dat[0]; 1427 if (dat[1]) { 1428 j1939_tp_set_rxtimeout(session, 1250); 1429 if (session->transmission) { 1430 if (session->pkt.tx_acked) 1431 j1939_sk_errqueue(session, 1432 J1939_ERRQUEUE_SCHED); 1433 j1939_session_txtimer_cancel(session); 1434 j1939_tp_schedule_txtimer(session, 0); 1435 } 1436 } else { 1437 /* CTS(0) */ 1438 j1939_tp_set_rxtimeout(session, 550); 1439 } 1440 return; 1441 1442 out_session_cancel: 1443 j1939_session_timers_cancel(session); 1444 j1939_session_cancel(session, err); 1445 } 1446 1447 static void 1448 j1939_xtp_rx_cts(struct j1939_priv *priv, struct sk_buff *skb, bool transmitter) 1449 { 1450 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb); 1451 struct j1939_session *session; 1452 1453 session = j1939_session_get_by_addr(priv, &skcb->addr, true, 1454 transmitter); 1455 if (!session) 1456 return; 1457 j1939_xtp_rx_cts_one(session, skb); 1458 j1939_session_put(session); 1459 } 1460 1461 static struct j1939_session *j1939_session_new(struct j1939_priv *priv, 1462 struct sk_buff *skb, size_t size) 1463 { 1464 struct j1939_session *session; 1465 struct j1939_sk_buff_cb *skcb; 1466 1467 session = kzalloc(sizeof(*session), gfp_any()); 1468 if (!session) 1469 return NULL; 1470 1471 INIT_LIST_HEAD(&session->active_session_list_entry); 1472 INIT_LIST_HEAD(&session->sk_session_queue_entry); 1473 kref_init(&session->kref); 1474 1475 j1939_priv_get(priv); 1476 session->priv = priv; 1477 session->total_message_size = size; 1478 session->state = J1939_SESSION_NEW; 1479 1480 skb_queue_head_init(&session->skb_queue); 1481 skb_queue_tail(&session->skb_queue, skb); 1482 1483 skcb = j1939_skb_to_cb(skb); 1484 memcpy(&session->skcb, skcb, sizeof(session->skcb)); 1485 1486 hrtimer_init(&session->txtimer, CLOCK_MONOTONIC, 1487 HRTIMER_MODE_REL_SOFT); 1488 session->txtimer.function = j1939_tp_txtimer; 1489 hrtimer_init(&session->rxtimer, CLOCK_MONOTONIC, 1490 HRTIMER_MODE_REL_SOFT); 1491 session->rxtimer.function = j1939_tp_rxtimer; 1492 1493 netdev_dbg(priv->ndev, "%s: 0x%p: sa: %02x, da: %02x\n", 1494 __func__, session, skcb->addr.sa, skcb->addr.da); 1495 1496 return session; 1497 } 1498 1499 static struct 1500 j1939_session *j1939_session_fresh_new(struct j1939_priv *priv, 1501 int size, 1502 const struct j1939_sk_buff_cb *rel_skcb) 1503 { 1504 struct sk_buff *skb; 1505 struct j1939_sk_buff_cb *skcb; 1506 struct j1939_session *session; 1507 1508 skb = alloc_skb(size + sizeof(struct can_skb_priv), GFP_ATOMIC); 1509 if (unlikely(!skb)) 1510 return NULL; 1511 1512 skb->dev = priv->ndev; 1513 can_skb_reserve(skb); 1514 can_skb_prv(skb)->ifindex = priv->ndev->ifindex; 1515 can_skb_prv(skb)->skbcnt = 0; 1516 skcb = j1939_skb_to_cb(skb); 1517 memcpy(skcb, rel_skcb, sizeof(*skcb)); 1518 1519 session = j1939_session_new(priv, skb, size); 1520 if (!session) { 1521 kfree_skb(skb); 1522 return NULL; 1523 } 1524 1525 /* alloc data area */ 1526 skb_put(skb, size); 1527 /* skb is recounted in j1939_session_new() */ 1528 return session; 1529 } 1530 1531 int j1939_session_activate(struct j1939_session *session) 1532 { 1533 struct j1939_priv *priv = session->priv; 1534 struct j1939_session *active = NULL; 1535 int ret = 0; 1536 1537 j1939_session_list_lock(priv); 1538 if (session->skcb.addr.type != J1939_SIMPLE) 1539 active = j1939_session_get_by_addr_locked(priv, 1540 &priv->active_session_list, 1541 &session->skcb.addr, false, 1542 session->transmission); 1543 if (active) { 1544 j1939_session_put(active); 1545 ret = -EAGAIN; 1546 } else { 1547 WARN_ON_ONCE(session->state != J1939_SESSION_NEW); 1548 list_add_tail(&session->active_session_list_entry, 1549 &priv->active_session_list); 1550 j1939_session_get(session); 1551 session->state = J1939_SESSION_ACTIVE; 1552 1553 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", 1554 __func__, session); 1555 } 1556 j1939_session_list_unlock(priv); 1557 1558 return ret; 1559 } 1560 1561 static struct 1562 j1939_session *j1939_xtp_rx_rts_session_new(struct j1939_priv *priv, 1563 struct sk_buff *skb) 1564 { 1565 enum j1939_xtp_abort abort = J1939_XTP_NO_ABORT; 1566 struct j1939_sk_buff_cb skcb = *j1939_skb_to_cb(skb); 1567 struct j1939_session *session; 1568 const u8 *dat; 1569 pgn_t pgn; 1570 int len; 1571 1572 netdev_dbg(priv->ndev, "%s\n", __func__); 1573 1574 dat = skb->data; 1575 pgn = j1939_xtp_ctl_to_pgn(dat); 1576 skcb.addr.pgn = pgn; 1577 1578 if (!j1939_sk_recv_match(priv, &skcb)) 1579 return NULL; 1580 1581 if (skcb.addr.type == J1939_ETP) { 1582 len = j1939_etp_ctl_to_size(dat); 1583 if (len > J1939_MAX_ETP_PACKET_SIZE) 1584 abort = J1939_XTP_ABORT_FAULT; 1585 else if (len > priv->tp_max_packet_size) 1586 abort = J1939_XTP_ABORT_RESOURCE; 1587 else if (len <= J1939_MAX_TP_PACKET_SIZE) 1588 abort = J1939_XTP_ABORT_FAULT; 1589 } else { 1590 len = j1939_tp_ctl_to_size(dat); 1591 if (len > J1939_MAX_TP_PACKET_SIZE) 1592 abort = J1939_XTP_ABORT_FAULT; 1593 else if (len > priv->tp_max_packet_size) 1594 abort = J1939_XTP_ABORT_RESOURCE; 1595 } 1596 1597 if (abort != J1939_XTP_NO_ABORT) { 1598 j1939_xtp_tx_abort(priv, &skcb, true, abort, pgn); 1599 return NULL; 1600 } 1601 1602 session = j1939_session_fresh_new(priv, len, &skcb); 1603 if (!session) { 1604 j1939_xtp_tx_abort(priv, &skcb, true, 1605 J1939_XTP_ABORT_RESOURCE, pgn); 1606 return NULL; 1607 } 1608 1609 /* initialize the control buffer: plain copy */ 1610 session->pkt.total = (len + 6) / 7; 1611 session->pkt.block = 0xff; 1612 if (skcb.addr.type != J1939_ETP) { 1613 if (dat[3] != session->pkt.total) 1614 netdev_alert(priv->ndev, "%s: 0x%p: strange total, %u != %u\n", 1615 __func__, session, session->pkt.total, 1616 dat[3]); 1617 session->pkt.total = dat[3]; 1618 session->pkt.block = min(dat[3], dat[4]); 1619 } 1620 1621 session->pkt.rx = 0; 1622 session->pkt.tx = 0; 1623 1624 WARN_ON_ONCE(j1939_session_activate(session)); 1625 1626 return session; 1627 } 1628 1629 static int j1939_xtp_rx_rts_session_active(struct j1939_session *session, 1630 struct sk_buff *skb) 1631 { 1632 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb); 1633 struct j1939_priv *priv = session->priv; 1634 1635 if (!session->transmission) { 1636 if (j1939_xtp_rx_cmd_bad_pgn(session, skb)) 1637 return -EBUSY; 1638 1639 /* RTS on active session */ 1640 j1939_session_timers_cancel(session); 1641 j1939_session_cancel(session, J1939_XTP_ABORT_BUSY); 1642 } 1643 1644 if (session->last_cmd != 0) { 1645 /* we received a second rts on the same connection */ 1646 netdev_alert(priv->ndev, "%s: 0x%p: connection exists (%02x %02x). last cmd: %x\n", 1647 __func__, session, skcb->addr.sa, skcb->addr.da, 1648 session->last_cmd); 1649 1650 j1939_session_timers_cancel(session); 1651 j1939_session_cancel(session, J1939_XTP_ABORT_BUSY); 1652 1653 return -EBUSY; 1654 } 1655 1656 if (session->skcb.addr.sa != skcb->addr.sa || 1657 session->skcb.addr.da != skcb->addr.da) 1658 netdev_warn(priv->ndev, "%s: 0x%p: session->skcb.addr.sa=0x%02x skcb->addr.sa=0x%02x session->skcb.addr.da=0x%02x skcb->addr.da=0x%02x\n", 1659 __func__, session, 1660 session->skcb.addr.sa, skcb->addr.sa, 1661 session->skcb.addr.da, skcb->addr.da); 1662 /* make sure 'sa' & 'da' are correct ! 1663 * They may be 'not filled in yet' for sending 1664 * skb's, since they did not pass the Address Claim ever. 1665 */ 1666 session->skcb.addr.sa = skcb->addr.sa; 1667 session->skcb.addr.da = skcb->addr.da; 1668 1669 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session); 1670 1671 return 0; 1672 } 1673 1674 static void j1939_xtp_rx_rts(struct j1939_priv *priv, struct sk_buff *skb, 1675 bool transmitter) 1676 { 1677 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb); 1678 struct j1939_session *session; 1679 u8 cmd = skb->data[0]; 1680 1681 session = j1939_session_get_by_addr(priv, &skcb->addr, false, 1682 transmitter); 1683 1684 if (!session) { 1685 if (transmitter) { 1686 /* If we're the transmitter and this function is called, 1687 * we received our own RTS. A session has already been 1688 * created. 1689 * 1690 * For some reasons however it might have been destroyed 1691 * already. So don't create a new one here (using 1692 * "j1939_xtp_rx_rts_session_new()") as this will be a 1693 * receiver session. 1694 * 1695 * The reasons the session is already destroyed might 1696 * be: 1697 * - user space closed socket was and the session was 1698 * aborted 1699 * - session was aborted due to external abort message 1700 */ 1701 return; 1702 } 1703 session = j1939_xtp_rx_rts_session_new(priv, skb); 1704 if (!session) { 1705 if (cmd == J1939_TP_CMD_BAM && j1939_sk_recv_match(priv, skcb)) 1706 netdev_info(priv->ndev, "%s: failed to create TP BAM session\n", 1707 __func__); 1708 return; 1709 } 1710 } else { 1711 if (j1939_xtp_rx_rts_session_active(session, skb)) { 1712 j1939_session_put(session); 1713 return; 1714 } 1715 } 1716 session->last_cmd = cmd; 1717 1718 if (cmd == J1939_TP_CMD_BAM) { 1719 if (!session->transmission) 1720 j1939_tp_set_rxtimeout(session, 750); 1721 } else { 1722 if (!session->transmission) { 1723 j1939_session_txtimer_cancel(session); 1724 j1939_tp_schedule_txtimer(session, 0); 1725 } 1726 j1939_tp_set_rxtimeout(session, 1250); 1727 } 1728 1729 j1939_session_put(session); 1730 } 1731 1732 static void j1939_xtp_rx_dpo_one(struct j1939_session *session, 1733 struct sk_buff *skb) 1734 { 1735 const u8 *dat = skb->data; 1736 1737 if (j1939_xtp_rx_cmd_bad_pgn(session, skb)) 1738 return; 1739 1740 netdev_dbg(session->priv->ndev, "%s: 0x%p\n", __func__, session); 1741 1742 /* transmitted without problems */ 1743 session->pkt.dpo = j1939_etp_ctl_to_packet(skb->data); 1744 session->last_cmd = dat[0]; 1745 j1939_tp_set_rxtimeout(session, 750); 1746 } 1747 1748 static void j1939_xtp_rx_dpo(struct j1939_priv *priv, struct sk_buff *skb, 1749 bool transmitter) 1750 { 1751 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb); 1752 struct j1939_session *session; 1753 1754 session = j1939_session_get_by_addr(priv, &skcb->addr, false, 1755 transmitter); 1756 if (!session) { 1757 netdev_info(priv->ndev, 1758 "%s: no connection found\n", __func__); 1759 return; 1760 } 1761 1762 j1939_xtp_rx_dpo_one(session, skb); 1763 j1939_session_put(session); 1764 } 1765 1766 static void j1939_xtp_rx_dat_one(struct j1939_session *session, 1767 struct sk_buff *skb) 1768 { 1769 struct j1939_priv *priv = session->priv; 1770 struct j1939_sk_buff_cb *skcb; 1771 struct sk_buff *se_skb = NULL; 1772 const u8 *dat; 1773 u8 *tpdat; 1774 int offset; 1775 int nbytes; 1776 bool final = false; 1777 bool remain = false; 1778 bool do_cts_eoma = false; 1779 int packet; 1780 1781 skcb = j1939_skb_to_cb(skb); 1782 dat = skb->data; 1783 if (skb->len <= 1) 1784 /* makes no sense */ 1785 goto out_session_cancel; 1786 1787 switch (session->last_cmd) { 1788 case 0xff: 1789 break; 1790 case J1939_ETP_CMD_DPO: 1791 if (skcb->addr.type == J1939_ETP) 1792 break; 1793 fallthrough; 1794 case J1939_TP_CMD_BAM: 1795 case J1939_TP_CMD_CTS: /* fall through */ 1796 if (skcb->addr.type != J1939_ETP) 1797 break; 1798 fallthrough; 1799 default: 1800 netdev_info(priv->ndev, "%s: 0x%p: last %02x\n", __func__, 1801 session, session->last_cmd); 1802 goto out_session_cancel; 1803 } 1804 1805 packet = (dat[0] - 1 + session->pkt.dpo); 1806 if (packet > session->pkt.total || 1807 (session->pkt.rx + 1) > session->pkt.total) { 1808 netdev_info(priv->ndev, "%s: 0x%p: should have been completed\n", 1809 __func__, session); 1810 goto out_session_cancel; 1811 } 1812 1813 se_skb = j1939_session_skb_get_by_offset(session, packet * 7); 1814 if (!se_skb) { 1815 netdev_warn(priv->ndev, "%s: 0x%p: no skb found\n", __func__, 1816 session); 1817 goto out_session_cancel; 1818 } 1819 1820 skcb = j1939_skb_to_cb(se_skb); 1821 offset = packet * 7 - skcb->offset; 1822 nbytes = se_skb->len - offset; 1823 if (nbytes > 7) 1824 nbytes = 7; 1825 if (nbytes <= 0 || (nbytes + 1) > skb->len) { 1826 netdev_info(priv->ndev, "%s: 0x%p: nbytes %i, len %i\n", 1827 __func__, session, nbytes, skb->len); 1828 goto out_session_cancel; 1829 } 1830 1831 tpdat = se_skb->data; 1832 if (!session->transmission) { 1833 memcpy(&tpdat[offset], &dat[1], nbytes); 1834 } else { 1835 int err; 1836 1837 err = memcmp(&tpdat[offset], &dat[1], nbytes); 1838 if (err) 1839 netdev_err_once(priv->ndev, 1840 "%s: 0x%p: Data of RX-looped back packet (%*ph) doesn't match TX data (%*ph)!\n", 1841 __func__, session, 1842 nbytes, &dat[1], 1843 nbytes, &tpdat[offset]); 1844 } 1845 1846 if (packet == session->pkt.rx) 1847 session->pkt.rx++; 1848 1849 if (skcb->addr.type != J1939_ETP && 1850 j1939_cb_is_broadcast(&session->skcb)) { 1851 if (session->pkt.rx >= session->pkt.total) 1852 final = true; 1853 else 1854 remain = true; 1855 } else { 1856 /* never final, an EOMA must follow */ 1857 if (session->pkt.rx >= session->pkt.last) 1858 do_cts_eoma = true; 1859 } 1860 1861 if (final) { 1862 j1939_session_timers_cancel(session); 1863 j1939_session_completed(session); 1864 } else if (remain) { 1865 if (!session->transmission) 1866 j1939_tp_set_rxtimeout(session, 750); 1867 } else if (do_cts_eoma) { 1868 j1939_tp_set_rxtimeout(session, 1250); 1869 if (!session->transmission) 1870 j1939_tp_schedule_txtimer(session, 0); 1871 } else { 1872 j1939_tp_set_rxtimeout(session, 250); 1873 } 1874 session->last_cmd = 0xff; 1875 consume_skb(se_skb); 1876 j1939_session_put(session); 1877 1878 return; 1879 1880 out_session_cancel: 1881 kfree_skb(se_skb); 1882 j1939_session_timers_cancel(session); 1883 j1939_session_cancel(session, J1939_XTP_ABORT_FAULT); 1884 j1939_session_put(session); 1885 } 1886 1887 static void j1939_xtp_rx_dat(struct j1939_priv *priv, struct sk_buff *skb) 1888 { 1889 struct j1939_sk_buff_cb *skcb; 1890 struct j1939_session *session; 1891 1892 skcb = j1939_skb_to_cb(skb); 1893 1894 if (j1939_tp_im_transmitter(skcb)) { 1895 session = j1939_session_get_by_addr(priv, &skcb->addr, false, 1896 true); 1897 if (!session) 1898 netdev_info(priv->ndev, "%s: no tx connection found\n", 1899 __func__); 1900 else 1901 j1939_xtp_rx_dat_one(session, skb); 1902 } 1903 1904 if (j1939_tp_im_receiver(skcb)) { 1905 session = j1939_session_get_by_addr(priv, &skcb->addr, false, 1906 false); 1907 if (!session) 1908 netdev_info(priv->ndev, "%s: no rx connection found\n", 1909 __func__); 1910 else 1911 j1939_xtp_rx_dat_one(session, skb); 1912 } 1913 1914 if (j1939_cb_is_broadcast(skcb)) { 1915 session = j1939_session_get_by_addr(priv, &skcb->addr, false, 1916 false); 1917 if (session) 1918 j1939_xtp_rx_dat_one(session, skb); 1919 } 1920 } 1921 1922 /* j1939 main intf */ 1923 struct j1939_session *j1939_tp_send(struct j1939_priv *priv, 1924 struct sk_buff *skb, size_t size) 1925 { 1926 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb); 1927 struct j1939_session *session; 1928 int ret; 1929 1930 if (skcb->addr.pgn == J1939_TP_PGN_DAT || 1931 skcb->addr.pgn == J1939_TP_PGN_CTL || 1932 skcb->addr.pgn == J1939_ETP_PGN_DAT || 1933 skcb->addr.pgn == J1939_ETP_PGN_CTL) 1934 /* avoid conflict */ 1935 return ERR_PTR(-EDOM); 1936 1937 if (size > priv->tp_max_packet_size) 1938 return ERR_PTR(-EMSGSIZE); 1939 1940 if (size <= 8) 1941 skcb->addr.type = J1939_SIMPLE; 1942 else if (size > J1939_MAX_TP_PACKET_SIZE) 1943 skcb->addr.type = J1939_ETP; 1944 else 1945 skcb->addr.type = J1939_TP; 1946 1947 if (skcb->addr.type == J1939_ETP && 1948 j1939_cb_is_broadcast(skcb)) 1949 return ERR_PTR(-EDESTADDRREQ); 1950 1951 /* fill in addresses from names */ 1952 ret = j1939_ac_fixup(priv, skb); 1953 if (unlikely(ret)) 1954 return ERR_PTR(ret); 1955 1956 /* fix DST flags, it may be used there soon */ 1957 if (j1939_address_is_unicast(skcb->addr.da) && 1958 priv->ents[skcb->addr.da].nusers) 1959 skcb->flags |= J1939_ECU_LOCAL_DST; 1960 1961 /* src is always local, I'm sending ... */ 1962 skcb->flags |= J1939_ECU_LOCAL_SRC; 1963 1964 /* prepare new session */ 1965 session = j1939_session_new(priv, skb, size); 1966 if (!session) 1967 return ERR_PTR(-ENOMEM); 1968 1969 /* skb is recounted in j1939_session_new() */ 1970 sock_hold(skb->sk); 1971 session->sk = skb->sk; 1972 session->transmission = true; 1973 session->pkt.total = (size + 6) / 7; 1974 session->pkt.block = skcb->addr.type == J1939_ETP ? 255 : 1975 min(j1939_tp_block ?: 255, session->pkt.total); 1976 1977 if (j1939_cb_is_broadcast(&session->skcb)) 1978 /* set the end-packet for broadcast */ 1979 session->pkt.last = session->pkt.total; 1980 1981 skcb->tskey = session->sk->sk_tskey++; 1982 session->tskey = skcb->tskey; 1983 1984 return session; 1985 } 1986 1987 static void j1939_tp_cmd_recv(struct j1939_priv *priv, struct sk_buff *skb) 1988 { 1989 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb); 1990 int extd = J1939_TP; 1991 u8 cmd = skb->data[0]; 1992 1993 switch (cmd) { 1994 case J1939_ETP_CMD_RTS: 1995 extd = J1939_ETP; 1996 fallthrough; 1997 case J1939_TP_CMD_BAM: 1998 case J1939_TP_CMD_RTS: /* fall through */ 1999 if (skcb->addr.type != extd) 2000 return; 2001 2002 if (cmd == J1939_TP_CMD_RTS && j1939_cb_is_broadcast(skcb)) { 2003 netdev_alert(priv->ndev, "%s: rts without destination (%02x)\n", 2004 __func__, skcb->addr.sa); 2005 return; 2006 } 2007 2008 if (j1939_tp_im_transmitter(skcb)) 2009 j1939_xtp_rx_rts(priv, skb, true); 2010 2011 if (j1939_tp_im_receiver(skcb) || j1939_cb_is_broadcast(skcb)) 2012 j1939_xtp_rx_rts(priv, skb, false); 2013 2014 break; 2015 2016 case J1939_ETP_CMD_CTS: 2017 extd = J1939_ETP; 2018 fallthrough; 2019 case J1939_TP_CMD_CTS: 2020 if (skcb->addr.type != extd) 2021 return; 2022 2023 if (j1939_tp_im_transmitter(skcb)) 2024 j1939_xtp_rx_cts(priv, skb, false); 2025 2026 if (j1939_tp_im_receiver(skcb)) 2027 j1939_xtp_rx_cts(priv, skb, true); 2028 2029 break; 2030 2031 case J1939_ETP_CMD_DPO: 2032 if (skcb->addr.type != J1939_ETP) 2033 return; 2034 2035 if (j1939_tp_im_transmitter(skcb)) 2036 j1939_xtp_rx_dpo(priv, skb, true); 2037 2038 if (j1939_tp_im_receiver(skcb)) 2039 j1939_xtp_rx_dpo(priv, skb, false); 2040 2041 break; 2042 2043 case J1939_ETP_CMD_EOMA: 2044 extd = J1939_ETP; 2045 fallthrough; 2046 case J1939_TP_CMD_EOMA: 2047 if (skcb->addr.type != extd) 2048 return; 2049 2050 if (j1939_tp_im_transmitter(skcb)) 2051 j1939_xtp_rx_eoma(priv, skb, false); 2052 2053 if (j1939_tp_im_receiver(skcb)) 2054 j1939_xtp_rx_eoma(priv, skb, true); 2055 2056 break; 2057 2058 case J1939_ETP_CMD_ABORT: /* && J1939_TP_CMD_ABORT */ 2059 if (j1939_tp_im_transmitter(skcb)) 2060 j1939_xtp_rx_abort(priv, skb, true); 2061 2062 if (j1939_tp_im_receiver(skcb)) 2063 j1939_xtp_rx_abort(priv, skb, false); 2064 2065 break; 2066 default: 2067 return; 2068 } 2069 } 2070 2071 int j1939_tp_recv(struct j1939_priv *priv, struct sk_buff *skb) 2072 { 2073 struct j1939_sk_buff_cb *skcb = j1939_skb_to_cb(skb); 2074 2075 if (!j1939_tp_im_involved_anydir(skcb) && !j1939_cb_is_broadcast(skcb)) 2076 return 0; 2077 2078 switch (skcb->addr.pgn) { 2079 case J1939_ETP_PGN_DAT: 2080 skcb->addr.type = J1939_ETP; 2081 fallthrough; 2082 case J1939_TP_PGN_DAT: 2083 j1939_xtp_rx_dat(priv, skb); 2084 break; 2085 2086 case J1939_ETP_PGN_CTL: 2087 skcb->addr.type = J1939_ETP; 2088 fallthrough; 2089 case J1939_TP_PGN_CTL: 2090 if (skb->len < 8) 2091 return 0; /* Don't care. Nothing to extract here */ 2092 2093 j1939_tp_cmd_recv(priv, skb); 2094 break; 2095 default: 2096 return 0; /* no problem */ 2097 } 2098 return 1; /* "I processed the message" */ 2099 } 2100 2101 void j1939_simple_recv(struct j1939_priv *priv, struct sk_buff *skb) 2102 { 2103 struct j1939_session *session; 2104 2105 if (!skb->sk) 2106 return; 2107 2108 if (skb->sk->sk_family != AF_CAN || 2109 skb->sk->sk_protocol != CAN_J1939) 2110 return; 2111 2112 j1939_session_list_lock(priv); 2113 session = j1939_session_get_simple(priv, skb); 2114 j1939_session_list_unlock(priv); 2115 if (!session) { 2116 netdev_warn(priv->ndev, 2117 "%s: Received already invalidated message\n", 2118 __func__); 2119 return; 2120 } 2121 2122 j1939_session_timers_cancel(session); 2123 j1939_session_deactivate(session); 2124 j1939_session_put(session); 2125 } 2126 2127 int j1939_cancel_active_session(struct j1939_priv *priv, struct sock *sk) 2128 { 2129 struct j1939_session *session, *saved; 2130 2131 netdev_dbg(priv->ndev, "%s, sk: %p\n", __func__, sk); 2132 j1939_session_list_lock(priv); 2133 list_for_each_entry_safe(session, saved, 2134 &priv->active_session_list, 2135 active_session_list_entry) { 2136 if (!sk || sk == session->sk) { 2137 if (hrtimer_try_to_cancel(&session->txtimer) == 1) 2138 j1939_session_put(session); 2139 if (hrtimer_try_to_cancel(&session->rxtimer) == 1) 2140 j1939_session_put(session); 2141 2142 session->err = ESHUTDOWN; 2143 j1939_session_deactivate_locked(session); 2144 } 2145 } 2146 j1939_session_list_unlock(priv); 2147 return NOTIFY_DONE; 2148 } 2149 2150 void j1939_tp_init(struct j1939_priv *priv) 2151 { 2152 spin_lock_init(&priv->active_session_list_lock); 2153 INIT_LIST_HEAD(&priv->active_session_list); 2154 priv->tp_max_packet_size = J1939_MAX_ETP_PACKET_SIZE; 2155 } 2156