• Home
  • Raw
  • Download

Lines Matching +full:wait +full:- +full:state

1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 /* isotp.c - ISO 15765-2 CAN transport protocol for protocol family CAN
4 * This implementation does not provide ISO-TP specific return values to the
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
61 #include <linux/wait.h>
76 MODULE_DESCRIPTION("PF_CAN isotp 15765-2:2016 protocol");
79 MODULE_ALIAS("can-proto-6");
87 /* ISO 15765-2:2016 supports more than 4095 byte per ISO PDU as the FF_DL can
94 /* N_PCI type values in bits 7-4 of N_PCI bytes */
111 #define ISOTP_FC_WT 1 /* wait */
125 u32 state; member
149 wait_queue_head_t wait; member
150 spinlock_t rx_lock; /* protect single thread state machine */
166 struct sock *sk = &so->sk; in isotp_rx_timer_handler()
168 if (so->rx.state == ISOTP_WAIT_DATA) { in isotp_rx_timer_handler()
172 sk->sk_err = ETIMEDOUT; in isotp_rx_timer_handler()
174 sk->sk_error_report(sk); in isotp_rx_timer_handler()
176 /* reset rx state */ in isotp_rx_timer_handler()
177 so->rx.state = ISOTP_IDLE; in isotp_rx_timer_handler()
191 nskb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), gfp_any()); in isotp_send_fc()
195 dev = dev_get_by_index(sock_net(sk), so->ifindex); in isotp_send_fc()
202 can_skb_prv(nskb)->ifindex = dev->ifindex; in isotp_send_fc()
203 can_skb_prv(nskb)->skbcnt = 0; in isotp_send_fc()
205 nskb->dev = dev; in isotp_send_fc()
207 ncf = (struct canfd_frame *)nskb->data; in isotp_send_fc()
208 skb_put_zero(nskb, so->ll.mtu); in isotp_send_fc()
211 ncf->can_id = so->txid; in isotp_send_fc()
213 if (so->opt.flags & CAN_ISOTP_TX_PADDING) { in isotp_send_fc()
214 memset(ncf->data, so->opt.txpad_content, CAN_MAX_DLEN); in isotp_send_fc()
215 ncf->len = CAN_MAX_DLEN; in isotp_send_fc()
217 ncf->len = ae + FC_CONTENT_SZ; in isotp_send_fc()
220 ncf->data[ae] = N_PCI_FC | flowstatus; in isotp_send_fc()
221 ncf->data[ae + 1] = so->rxfc.bs; in isotp_send_fc()
222 ncf->data[ae + 2] = so->rxfc.stmin; in isotp_send_fc()
225 ncf->data[0] = so->opt.ext_address; in isotp_send_fc()
227 ncf->flags = so->ll.tx_flags; in isotp_send_fc()
231 pr_notice_once("can-isotp: %s: can_send_ret %d\n", in isotp_send_fc()
237 so->rx.bs = 0; in isotp_send_fc()
240 so->lastrxcf_tstamp = ktime_set(0, 0); in isotp_send_fc()
243 hrtimer_start(&so->rxtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT); in isotp_send_fc()
249 struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb; in isotp_rcv_skb()
251 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can)); in isotp_rcv_skb()
254 addr->can_family = AF_CAN; in isotp_rcv_skb()
255 addr->can_ifindex = skb->dev->ifindex; in isotp_rcv_skb()
264 8, 8, 8, 8, 8, 8, 8, 8, 8, /* 0 - 8 */ in padlen()
265 12, 12, 12, 12, /* 9 - 12 */ in padlen()
266 16, 16, 16, 16, /* 13 - 16 */ in padlen()
267 20, 20, 20, 20, /* 17 - 20 */ in padlen()
268 24, 24, 24, 24, /* 21 - 24 */ in padlen()
269 32, 32, 32, 32, 32, 32, 32, 32, /* 25 - 32 */ in padlen()
270 48, 48, 48, 48, 48, 48, 48, 48, /* 33 - 40 */ in padlen()
271 48, 48, 48, 48, 48, 48, 48, 48 /* 41 - 48 */ in padlen()
285 * start at cf.data[7] cf->len has to be 7 to be optimal. in check_optimized()
288 if (cf->len <= CAN_MAX_DLEN) in check_optimized()
289 return (cf->len != start_index); in check_optimized()
291 /* This relation is also valid in the non-linear DLC range, where in check_optimized()
293 * The correct check would be (padlen(cf->len) != padlen(start_index)). in check_optimized()
294 * But as cf->len can only take discrete values from 12, .., 64 at this in check_optimized()
295 * point the padlen(cf->len) is always equal to cf->len. in check_optimized()
297 return (cf->len != padlen(start_index)); in check_optimized()
307 if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) { in check_pad()
308 if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) in check_pad()
316 if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) && in check_pad()
317 cf->len != padlen(cf->len)) in check_pad()
321 if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) { in check_pad()
322 for (i = start_index; i < cf->len; i++) in check_pad()
323 if (cf->data[i] != content) in check_pad()
331 struct sock *sk = &so->sk; in isotp_rcv_fc()
333 if (so->tx.state != ISOTP_WAIT_FC && in isotp_rcv_fc()
334 so->tx.state != ISOTP_WAIT_FIRST_FC) in isotp_rcv_fc()
337 hrtimer_cancel(&so->txtimer); in isotp_rcv_fc()
339 if ((cf->len < ae + FC_CONTENT_SZ) || in isotp_rcv_fc()
340 ((so->opt.flags & ISOTP_CHECK_PADDING) && in isotp_rcv_fc()
341 check_pad(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) { in isotp_rcv_fc()
342 /* malformed PDU - report 'not a data message' */ in isotp_rcv_fc()
343 sk->sk_err = EBADMSG; in isotp_rcv_fc()
345 sk->sk_error_report(sk); in isotp_rcv_fc()
347 so->tx.state = ISOTP_IDLE; in isotp_rcv_fc()
348 wake_up_interruptible(&so->wait); in isotp_rcv_fc()
353 if (so->tx.state == ISOTP_WAIT_FIRST_FC) { in isotp_rcv_fc()
354 so->txfc.bs = cf->data[ae + 1]; in isotp_rcv_fc()
355 so->txfc.stmin = cf->data[ae + 2]; in isotp_rcv_fc()
358 if (so->txfc.stmin > 0x7F && in isotp_rcv_fc()
359 (so->txfc.stmin < 0xF1 || so->txfc.stmin > 0xF9)) in isotp_rcv_fc()
360 so->txfc.stmin = 0x7F; in isotp_rcv_fc()
362 so->tx_gap = ktime_set(0, 0); in isotp_rcv_fc()
364 so->tx_gap = ktime_add_ns(so->tx_gap, so->frame_txtime); in isotp_rcv_fc()
366 if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN) in isotp_rcv_fc()
367 so->tx_gap = ktime_add_ns(so->tx_gap, in isotp_rcv_fc()
368 so->force_tx_stmin); in isotp_rcv_fc()
369 else if (so->txfc.stmin < 0x80) in isotp_rcv_fc()
370 so->tx_gap = ktime_add_ns(so->tx_gap, in isotp_rcv_fc()
371 so->txfc.stmin * 1000000); in isotp_rcv_fc()
373 so->tx_gap = ktime_add_ns(so->tx_gap, in isotp_rcv_fc()
374 (so->txfc.stmin - 0xF0) in isotp_rcv_fc()
376 so->tx.state = ISOTP_WAIT_FC; in isotp_rcv_fc()
379 switch (cf->data[ae] & 0x0F) { in isotp_rcv_fc()
381 so->tx.bs = 0; in isotp_rcv_fc()
382 so->tx.state = ISOTP_SENDING; in isotp_rcv_fc()
384 hrtimer_start(&so->txtimer, so->tx_gap, in isotp_rcv_fc()
389 /* start timer to wait for next FC frame */ in isotp_rcv_fc()
390 hrtimer_start(&so->txtimer, ktime_set(1, 0), in isotp_rcv_fc()
395 /* overflow on receiver side - report 'message too long' */ in isotp_rcv_fc()
396 sk->sk_err = EMSGSIZE; in isotp_rcv_fc()
398 sk->sk_error_report(sk); in isotp_rcv_fc()
403 so->tx.state = ISOTP_IDLE; in isotp_rcv_fc()
404 wake_up_interruptible(&so->wait); in isotp_rcv_fc()
415 hrtimer_cancel(&so->rxtimer); in isotp_rcv_sf()
416 so->rx.state = ISOTP_IDLE; in isotp_rcv_sf()
418 if (!len || len > cf->len - pcilen) in isotp_rcv_sf()
421 if ((so->opt.flags & ISOTP_CHECK_PADDING) && in isotp_rcv_sf()
422 check_pad(so, cf, pcilen + len, so->opt.rxpad_content)) { in isotp_rcv_sf()
423 /* malformed PDU - report 'not a data message' */ in isotp_rcv_sf()
424 sk->sk_err = EBADMSG; in isotp_rcv_sf()
426 sk->sk_error_report(sk); in isotp_rcv_sf()
434 memcpy(skb_put(nskb, len), &cf->data[pcilen], len); in isotp_rcv_sf()
436 nskb->tstamp = skb->tstamp; in isotp_rcv_sf()
437 nskb->dev = skb->dev; in isotp_rcv_sf()
449 hrtimer_cancel(&so->rxtimer); in isotp_rcv_ff()
450 so->rx.state = ISOTP_IDLE; in isotp_rcv_ff()
453 so->rx.ll_dl = padlen(cf->len); in isotp_rcv_ff()
456 if (cf->len != so->rx.ll_dl) in isotp_rcv_ff()
460 so->rx.len = (cf->data[ae] & 0x0F) << 8; in isotp_rcv_ff()
461 so->rx.len += cf->data[ae + 1]; in isotp_rcv_ff()
464 if (so->rx.len) { in isotp_rcv_ff()
468 so->rx.len = cf->data[ae + 2] << 24; in isotp_rcv_ff()
469 so->rx.len += cf->data[ae + 3] << 16; in isotp_rcv_ff()
470 so->rx.len += cf->data[ae + 4] << 8; in isotp_rcv_ff()
471 so->rx.len += cf->data[ae + 5]; in isotp_rcv_ff()
476 off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0; in isotp_rcv_ff()
478 if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl) in isotp_rcv_ff()
481 if (so->rx.len > MAX_MSG_LENGTH) { in isotp_rcv_ff()
488 so->rx.idx = 0; in isotp_rcv_ff()
489 for (i = ae + ff_pci_sz; i < so->rx.ll_dl; i++) in isotp_rcv_ff()
490 so->rx.buf[so->rx.idx++] = cf->data[i]; in isotp_rcv_ff()
493 so->rx.sn = 1; in isotp_rcv_ff()
494 so->rx.state = ISOTP_WAIT_DATA; in isotp_rcv_ff()
497 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE) in isotp_rcv_ff()
512 if (so->rx.state != ISOTP_WAIT_DATA) in isotp_rcv_cf()
516 if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) { in isotp_rcv_cf()
517 if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) < in isotp_rcv_cf()
518 so->force_rx_stmin) in isotp_rcv_cf()
521 so->lastrxcf_tstamp = skb->tstamp; in isotp_rcv_cf()
524 hrtimer_cancel(&so->rxtimer); in isotp_rcv_cf()
527 if (cf->len > so->rx.ll_dl) in isotp_rcv_cf()
531 if (cf->len < so->rx.ll_dl) { in isotp_rcv_cf()
533 if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ) in isotp_rcv_cf()
537 if ((cf->data[ae] & 0x0F) != so->rx.sn) { in isotp_rcv_cf()
538 /* wrong sn detected - report 'illegal byte sequence' */ in isotp_rcv_cf()
539 sk->sk_err = EILSEQ; in isotp_rcv_cf()
541 sk->sk_error_report(sk); in isotp_rcv_cf()
543 /* reset rx state */ in isotp_rcv_cf()
544 so->rx.state = ISOTP_IDLE; in isotp_rcv_cf()
547 so->rx.sn++; in isotp_rcv_cf()
548 so->rx.sn %= 16; in isotp_rcv_cf()
550 for (i = ae + N_PCI_SZ; i < cf->len; i++) { in isotp_rcv_cf()
551 so->rx.buf[so->rx.idx++] = cf->data[i]; in isotp_rcv_cf()
552 if (so->rx.idx >= so->rx.len) in isotp_rcv_cf()
556 if (so->rx.idx >= so->rx.len) { in isotp_rcv_cf()
558 so->rx.state = ISOTP_IDLE; in isotp_rcv_cf()
560 if ((so->opt.flags & ISOTP_CHECK_PADDING) && in isotp_rcv_cf()
561 check_pad(so, cf, i + 1, so->opt.rxpad_content)) { in isotp_rcv_cf()
562 /* malformed PDU - report 'not a data message' */ in isotp_rcv_cf()
563 sk->sk_err = EBADMSG; in isotp_rcv_cf()
565 sk->sk_error_report(sk); in isotp_rcv_cf()
569 nskb = alloc_skb(so->rx.len, gfp_any()); in isotp_rcv_cf()
573 memcpy(skb_put(nskb, so->rx.len), so->rx.buf, in isotp_rcv_cf()
574 so->rx.len); in isotp_rcv_cf()
576 nskb->tstamp = skb->tstamp; in isotp_rcv_cf()
577 nskb->dev = skb->dev; in isotp_rcv_cf()
583 if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) { in isotp_rcv_cf()
585 hrtimer_start(&so->rxtimer, ktime_set(1, 0), in isotp_rcv_cf()
591 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE) in isotp_rcv_cf()
594 /* we reached the specified blocksize so->rxfc.bs */ in isotp_rcv_cf()
604 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0; in isotp_rcv()
610 if (skb->len != so->ll.mtu) in isotp_rcv()
613 cf = (struct canfd_frame *)skb->data; in isotp_rcv()
616 if (ae && cf->data[0] != so->opt.rx_ext_address) in isotp_rcv()
619 n_pci_type = cf->data[ae] & 0xF0; in isotp_rcv()
621 /* Make sure the state changes and data structures stay consistent at in isotp_rcv()
625 spin_lock(&so->rx_lock); in isotp_rcv()
627 if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) { in isotp_rcv()
629 if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) || in isotp_rcv()
630 (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC)) in isotp_rcv()
645 * requirements - no matter if it's CAN 2.0 or CAN FD in isotp_rcv()
649 sf_dl = cf->data[ae] & 0x0F; in isotp_rcv()
651 if (cf->len <= CAN_MAX_DLEN) { in isotp_rcv()
654 if (skb->len == CANFD_MTU) { in isotp_rcv()
665 cf->data[SF_PCI_SZ4 + ae]); in isotp_rcv()
682 spin_unlock(&so->rx_lock); in isotp_rcv()
689 int space = so->tx.ll_dl - pcilen; in isotp_fill_dataframe()
690 int num = min_t(int, so->tx.len - so->tx.idx, space); in isotp_fill_dataframe()
693 cf->can_id = so->txid; in isotp_fill_dataframe()
694 cf->len = num + pcilen; in isotp_fill_dataframe()
697 if (so->opt.flags & CAN_ISOTP_TX_PADDING) { in isotp_fill_dataframe()
699 cf->len = padlen(cf->len); in isotp_fill_dataframe()
700 memset(cf->data, so->opt.txpad_content, cf->len); in isotp_fill_dataframe()
701 } else if (cf->len > CAN_MAX_DLEN) { in isotp_fill_dataframe()
703 cf->len = padlen(cf->len); in isotp_fill_dataframe()
704 memset(cf->data, CAN_ISOTP_DEFAULT_PAD_CONTENT, in isotp_fill_dataframe()
705 cf->len); in isotp_fill_dataframe()
710 cf->data[pcilen + i] = so->tx.buf[so->tx.idx++]; in isotp_fill_dataframe()
713 cf->data[0] = so->opt.ext_address; in isotp_fill_dataframe()
722 cf->can_id = so->txid; in isotp_create_fframe()
723 cf->len = so->tx.ll_dl; in isotp_create_fframe()
725 cf->data[0] = so->opt.ext_address; in isotp_create_fframe()
728 if (so->tx.len > 4095) { in isotp_create_fframe()
730 cf->data[ae] = N_PCI_FF; in isotp_create_fframe()
731 cf->data[ae + 1] = 0; in isotp_create_fframe()
732 cf->data[ae + 2] = (u8)(so->tx.len >> 24) & 0xFFU; in isotp_create_fframe()
733 cf->data[ae + 3] = (u8)(so->tx.len >> 16) & 0xFFU; in isotp_create_fframe()
734 cf->data[ae + 4] = (u8)(so->tx.len >> 8) & 0xFFU; in isotp_create_fframe()
735 cf->data[ae + 5] = (u8)so->tx.len & 0xFFU; in isotp_create_fframe()
739 cf->data[ae] = (u8)(so->tx.len >> 8) | N_PCI_FF; in isotp_create_fframe()
740 cf->data[ae + 1] = (u8)so->tx.len & 0xFFU; in isotp_create_fframe()
745 for (i = ae + ff_pci_sz; i < so->tx.ll_dl; i++) in isotp_create_fframe()
746 cf->data[i] = so->tx.buf[so->tx.idx++]; in isotp_create_fframe()
748 so->tx.sn = 1; in isotp_create_fframe()
749 so->tx.state = ISOTP_WAIT_FIRST_FC; in isotp_create_fframe()
756 struct sock *sk = &so->sk; in isotp_tx_timer_handler()
762 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0; in isotp_tx_timer_handler()
764 switch (so->tx.state) { in isotp_tx_timer_handler()
771 sk->sk_err = ECOMM; in isotp_tx_timer_handler()
773 sk->sk_error_report(sk); in isotp_tx_timer_handler()
775 /* reset tx state */ in isotp_tx_timer_handler()
776 so->tx.state = ISOTP_IDLE; in isotp_tx_timer_handler()
777 wake_up_interruptible(&so->wait); in isotp_tx_timer_handler()
783 dev = dev_get_by_index(sock_net(sk), so->ifindex); in isotp_tx_timer_handler()
788 skb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), in isotp_tx_timer_handler()
796 can_skb_prv(skb)->ifindex = dev->ifindex; in isotp_tx_timer_handler()
797 can_skb_prv(skb)->skbcnt = 0; in isotp_tx_timer_handler()
799 cf = (struct canfd_frame *)skb->data; in isotp_tx_timer_handler()
800 skb_put_zero(skb, so->ll.mtu); in isotp_tx_timer_handler()
806 cf->data[ae] = N_PCI_CF | so->tx.sn++; in isotp_tx_timer_handler()
807 so->tx.sn %= 16; in isotp_tx_timer_handler()
808 so->tx.bs++; in isotp_tx_timer_handler()
810 cf->flags = so->ll.tx_flags; in isotp_tx_timer_handler()
812 skb->dev = dev; in isotp_tx_timer_handler()
817 pr_notice_once("can-isotp: %s: can_send_ret %d\n", in isotp_tx_timer_handler()
820 if (so->tx.idx >= so->tx.len) { in isotp_tx_timer_handler()
822 so->tx.state = ISOTP_IDLE; in isotp_tx_timer_handler()
824 wake_up_interruptible(&so->wait); in isotp_tx_timer_handler()
828 if (so->txfc.bs && so->tx.bs >= so->txfc.bs) { in isotp_tx_timer_handler()
829 /* stop and wait for FC */ in isotp_tx_timer_handler()
830 so->tx.state = ISOTP_WAIT_FC; in isotp_tx_timer_handler()
832 hrtimer_set_expires(&so->txtimer, in isotp_tx_timer_handler()
840 if (!so->tx_gap) in isotp_tx_timer_handler()
845 hrtimer_set_expires(&so->txtimer, in isotp_tx_timer_handler()
846 ktime_add(ktime_get(), so->tx_gap)); in isotp_tx_timer_handler()
859 struct sock *sk = sock->sk; in isotp_sendmsg()
861 u32 old_state = so->tx.state; in isotp_sendmsg()
865 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0; in isotp_sendmsg()
866 int wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0; in isotp_sendmsg()
871 if (!so->bound) in isotp_sendmsg()
872 return -EADDRNOTAVAIL; in isotp_sendmsg()
874 /* we do not support multiple buffers - for now */ in isotp_sendmsg()
875 if (cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SENDING) != ISOTP_IDLE || in isotp_sendmsg()
876 wq_has_sleeper(&so->wait)) { in isotp_sendmsg()
877 if (msg->msg_flags & MSG_DONTWAIT) { in isotp_sendmsg()
878 err = -EAGAIN; in isotp_sendmsg()
882 /* wait for complete transmission of current pdu */ in isotp_sendmsg()
883 err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE); in isotp_sendmsg()
889 err = -EINVAL; in isotp_sendmsg()
894 off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0; in isotp_sendmsg()
897 if ((so->opt.flags & CAN_ISOTP_SF_BROADCAST) && in isotp_sendmsg()
898 (size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off)) { in isotp_sendmsg()
899 err = -EINVAL; in isotp_sendmsg()
903 err = memcpy_from_msg(so->tx.buf, msg, size); in isotp_sendmsg()
907 dev = dev_get_by_index(sock_net(sk), so->ifindex); in isotp_sendmsg()
909 err = -ENXIO; in isotp_sendmsg()
913 skb = sock_alloc_send_skb(sk, so->ll.mtu + sizeof(struct can_skb_priv), in isotp_sendmsg()
914 msg->msg_flags & MSG_DONTWAIT, &err); in isotp_sendmsg()
921 can_skb_prv(skb)->ifindex = dev->ifindex; in isotp_sendmsg()
922 can_skb_prv(skb)->skbcnt = 0; in isotp_sendmsg()
924 so->tx.len = size; in isotp_sendmsg()
925 so->tx.idx = 0; in isotp_sendmsg()
927 cf = (struct canfd_frame *)skb->data; in isotp_sendmsg()
928 skb_put_zero(skb, so->ll.mtu); in isotp_sendmsg()
931 if (size <= so->tx.ll_dl - SF_PCI_SZ4 - ae - off) { in isotp_sendmsg()
932 /* The message size generally fits into a SingleFrame - good. in isotp_sendmsg()
941 if (size <= CAN_MAX_DLEN - SF_PCI_SZ4 - ae) in isotp_sendmsg()
947 cf->data[ae] = N_PCI_SF; in isotp_sendmsg()
951 cf->data[SF_PCI_SZ4 + ae] = size; in isotp_sendmsg()
953 cf->data[ae] |= size; in isotp_sendmsg()
955 so->tx.state = ISOTP_IDLE; in isotp_sendmsg()
956 wake_up_interruptible(&so->wait); in isotp_sendmsg()
958 /* don't enable wait queue for a single frame transmission */ in isotp_sendmsg()
961 /* send first frame and wait for FC */ in isotp_sendmsg()
967 hrtimer_start(&so->txtimer, ktime_set(hrtimer_sec, 0), in isotp_sendmsg()
972 cf->flags = so->ll.tx_flags; in isotp_sendmsg()
974 skb->dev = dev; in isotp_sendmsg()
975 skb->sk = sk; in isotp_sendmsg()
979 pr_notice_once("can-isotp: %s: can_send_ret %d\n", in isotp_sendmsg()
982 /* no transmission -> no timeout monitoring */ in isotp_sendmsg()
984 hrtimer_cancel(&so->txtimer); in isotp_sendmsg()
990 /* wait for complete transmission of current pdu */ in isotp_sendmsg()
991 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE); in isotp_sendmsg()
993 if (sk->sk_err) in isotp_sendmsg()
994 return -sk->sk_err; in isotp_sendmsg()
1000 /* drop this PDU and unlock a potential wait queue */ in isotp_sendmsg()
1003 so->tx.state = old_state; in isotp_sendmsg()
1004 if (so->tx.state == ISOTP_IDLE) in isotp_sendmsg()
1005 wake_up_interruptible(&so->wait); in isotp_sendmsg()
1013 struct sock *sk = sock->sk; in isotp_recvmsg()
1020 return -EINVAL; in isotp_recvmsg()
1022 if (!so->bound) in isotp_recvmsg()
1023 return -EADDRNOTAVAIL; in isotp_recvmsg()
1030 if (size < skb->len) in isotp_recvmsg()
1031 msg->msg_flags |= MSG_TRUNC; in isotp_recvmsg()
1033 size = skb->len; in isotp_recvmsg()
1035 ret = memcpy_to_msg(msg, skb->data, size); in isotp_recvmsg()
1041 if (msg->msg_name) { in isotp_recvmsg()
1043 msg->msg_namelen = ISOTP_MIN_NAMELEN; in isotp_recvmsg()
1044 memcpy(msg->msg_name, skb->cb, msg->msg_namelen); in isotp_recvmsg()
1048 ret = (flags & MSG_TRUNC) ? skb->len : size; in isotp_recvmsg()
1058 struct sock *sk = sock->sk; in isotp_release()
1068 /* wait for complete transmission of current pdu */ in isotp_release()
1069 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE); in isotp_release()
1077 list_del(&so->notifier); in isotp_release()
1083 if (so->bound && (!(so->opt.flags & CAN_ISOTP_SF_BROADCAST))) { in isotp_release()
1084 if (so->ifindex) { in isotp_release()
1087 dev = dev_get_by_index(net, so->ifindex); in isotp_release()
1089 can_rx_unregister(net, dev, so->rxid, in isotp_release()
1090 SINGLE_MASK(so->rxid), in isotp_release()
1098 hrtimer_cancel(&so->txtimer); in isotp_release()
1099 hrtimer_cancel(&so->rxtimer); in isotp_release()
1101 so->ifindex = 0; in isotp_release()
1102 so->bound = 0; in isotp_release()
1105 sock->sk = NULL; in isotp_release()
1116 struct sock *sk = sock->sk; in isotp_bind()
1127 return -EINVAL; in isotp_bind()
1130 tx_id = addr->can_addr.tp.tx_id; in isotp_bind()
1136 rx_id = addr->can_addr.tp.rx_id; in isotp_bind()
1142 if (!addr->can_ifindex) in isotp_bind()
1143 return -ENODEV; in isotp_bind()
1147 if (so->bound) { in isotp_bind()
1148 err = -EINVAL; in isotp_bind()
1153 if (so->opt.flags & CAN_ISOTP_SF_BROADCAST) in isotp_bind()
1158 err = -EADDRNOTAVAIL; in isotp_bind()
1162 dev = dev_get_by_index(net, addr->can_ifindex); in isotp_bind()
1164 err = -ENODEV; in isotp_bind()
1167 if (dev->type != ARPHRD_CAN) { in isotp_bind()
1169 err = -ENODEV; in isotp_bind()
1172 if (dev->mtu < so->ll.mtu) { in isotp_bind()
1174 err = -EINVAL; in isotp_bind()
1177 if (!(dev->flags & IFF_UP)) in isotp_bind()
1180 ifindex = dev->ifindex; in isotp_bind()
1189 so->ifindex = ifindex; in isotp_bind()
1190 so->rxid = rx_id; in isotp_bind()
1191 so->txid = tx_id; in isotp_bind()
1192 so->bound = 1; in isotp_bind()
1198 sk->sk_err = ENETDOWN; in isotp_bind()
1200 sk->sk_error_report(sk); in isotp_bind()
1209 struct sock *sk = sock->sk; in isotp_getname()
1213 return -EOPNOTSUPP; in isotp_getname()
1216 addr->can_family = AF_CAN; in isotp_getname()
1217 addr->can_ifindex = so->ifindex; in isotp_getname()
1218 addr->can_addr.tp.rx_id = so->rxid; in isotp_getname()
1219 addr->can_addr.tp.tx_id = so->txid; in isotp_getname()
1227 struct sock *sk = sock->sk; in isotp_setsockopt_locked()
1231 if (so->bound) in isotp_setsockopt_locked()
1232 return -EISCONN; in isotp_setsockopt_locked()
1237 return -EINVAL; in isotp_setsockopt_locked()
1239 if (copy_from_sockptr(&so->opt, optval, optlen)) in isotp_setsockopt_locked()
1240 return -EFAULT; in isotp_setsockopt_locked()
1243 if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR)) in isotp_setsockopt_locked()
1244 so->opt.rx_ext_address = so->opt.ext_address; in isotp_setsockopt_locked()
1247 if (so->opt.frame_txtime) { in isotp_setsockopt_locked()
1248 if (so->opt.frame_txtime == CAN_ISOTP_FRAME_TXTIME_ZERO) in isotp_setsockopt_locked()
1249 so->frame_txtime = 0; in isotp_setsockopt_locked()
1251 so->frame_txtime = so->opt.frame_txtime; in isotp_setsockopt_locked()
1257 return -EINVAL; in isotp_setsockopt_locked()
1259 if (copy_from_sockptr(&so->rxfc, optval, optlen)) in isotp_setsockopt_locked()
1260 return -EFAULT; in isotp_setsockopt_locked()
1265 return -EINVAL; in isotp_setsockopt_locked()
1267 if (copy_from_sockptr(&so->force_tx_stmin, optval, optlen)) in isotp_setsockopt_locked()
1268 return -EFAULT; in isotp_setsockopt_locked()
1273 return -EINVAL; in isotp_setsockopt_locked()
1275 if (copy_from_sockptr(&so->force_rx_stmin, optval, optlen)) in isotp_setsockopt_locked()
1276 return -EFAULT; in isotp_setsockopt_locked()
1284 return -EFAULT; in isotp_setsockopt_locked()
1286 /* check for correct ISO 11898-1 DLC data length */ in isotp_setsockopt_locked()
1288 return -EINVAL; in isotp_setsockopt_locked()
1291 return -EINVAL; in isotp_setsockopt_locked()
1295 return -EINVAL; in isotp_setsockopt_locked()
1297 memcpy(&so->ll, &ll, sizeof(ll)); in isotp_setsockopt_locked()
1300 so->tx.ll_dl = ll.tx_dl; in isotp_setsockopt_locked()
1302 return -EINVAL; in isotp_setsockopt_locked()
1307 ret = -ENOPROTOOPT; in isotp_setsockopt_locked()
1317 struct sock *sk = sock->sk; in isotp_setsockopt()
1321 return -EINVAL; in isotp_setsockopt()
1332 struct sock *sk = sock->sk; in isotp_getsockopt()
1338 return -EINVAL; in isotp_getsockopt()
1340 return -EFAULT; in isotp_getsockopt()
1342 return -EINVAL; in isotp_getsockopt()
1347 val = &so->opt; in isotp_getsockopt()
1352 val = &so->rxfc; in isotp_getsockopt()
1357 val = &so->force_tx_stmin; in isotp_getsockopt()
1362 val = &so->force_rx_stmin; in isotp_getsockopt()
1367 val = &so->ll; in isotp_getsockopt()
1371 return -ENOPROTOOPT; in isotp_getsockopt()
1375 return -EFAULT; in isotp_getsockopt()
1377 return -EFAULT; in isotp_getsockopt()
1384 struct sock *sk = &so->sk; in isotp_notify()
1389 if (so->ifindex != dev->ifindex) in isotp_notify()
1396 if (so->bound && (!(so->opt.flags & CAN_ISOTP_SF_BROADCAST))) in isotp_notify()
1397 can_rx_unregister(dev_net(dev), dev, so->rxid, in isotp_notify()
1398 SINGLE_MASK(so->rxid), in isotp_notify()
1401 so->ifindex = 0; in isotp_notify()
1402 so->bound = 0; in isotp_notify()
1405 sk->sk_err = ENODEV; in isotp_notify()
1407 sk->sk_error_report(sk); in isotp_notify()
1411 sk->sk_err = ENETDOWN; in isotp_notify()
1413 sk->sk_error_report(sk); in isotp_notify()
1423 if (dev->type != ARPHRD_CAN) in isotp_notifier()
1445 so->ifindex = 0; in isotp_init()
1446 so->bound = 0; in isotp_init()
1448 so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS; in isotp_init()
1449 so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS; in isotp_init()
1450 so->opt.rx_ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS; in isotp_init()
1451 so->opt.rxpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT; in isotp_init()
1452 so->opt.txpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT; in isotp_init()
1453 so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME; in isotp_init()
1454 so->frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME; in isotp_init()
1455 so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS; in isotp_init()
1456 so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN; in isotp_init()
1457 so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX; in isotp_init()
1458 so->ll.mtu = CAN_ISOTP_DEFAULT_LL_MTU; in isotp_init()
1459 so->ll.tx_dl = CAN_ISOTP_DEFAULT_LL_TX_DL; in isotp_init()
1460 so->ll.tx_flags = CAN_ISOTP_DEFAULT_LL_TX_FLAGS; in isotp_init()
1463 so->tx.ll_dl = so->ll.tx_dl; in isotp_init()
1465 so->rx.state = ISOTP_IDLE; in isotp_init()
1466 so->tx.state = ISOTP_IDLE; in isotp_init()
1468 hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT); in isotp_init()
1469 so->rxtimer.function = isotp_rx_timer_handler; in isotp_init()
1470 hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT); in isotp_init()
1471 so->txtimer.function = isotp_tx_timer_handler; in isotp_init()
1473 init_waitqueue_head(&so->wait); in isotp_init()
1474 spin_lock_init(&so->rx_lock); in isotp_init()
1477 list_add_tail(&so->notifier, &isotp_notifier_list); in isotp_init()
1483 static __poll_t isotp_poll(struct file *file, struct socket *sock, poll_table *wait) in isotp_poll() argument
1485 struct sock *sk = sock->sk; in isotp_poll()
1488 __poll_t mask = datagram_poll(file, sock, wait); in isotp_poll()
1489 poll_wait(file, &so->wait, wait); in isotp_poll()
1491 /* Check for false positives due to TX state */ in isotp_poll()
1492 if ((mask & EPOLLWRNORM) && (so->tx.state != ISOTP_IDLE)) in isotp_poll()
1501 /* no ioctls for socket layer -> hand it down to NIC layer */ in isotp_sock_no_ioctlcmd()
1502 return -ENOIOCTLCMD; in isotp_sock_no_ioctlcmd()