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
60 #include <linux/wait.h>
75 MODULE_DESCRIPTION("PF_CAN isotp 15765-2:2016 protocol");
78 MODULE_ALIAS("can-proto-6");
86 /* ISO 15765-2:2016 supports more than 4095 byte per ISO PDU as the FF_DL can
93 /* N_PCI type values in bits 7-4 of N_PCI bytes */
110 #define ISOTP_FC_WT 1 /* wait */
124 u32 state; member
147 wait_queue_head_t wait; member
163 struct sock *sk = &so->sk; in isotp_rx_timer_handler()
165 if (so->rx.state == ISOTP_WAIT_DATA) { in isotp_rx_timer_handler()
169 sk->sk_err = ETIMEDOUT; in isotp_rx_timer_handler()
171 sk->sk_error_report(sk); in isotp_rx_timer_handler()
173 /* reset rx state */ in isotp_rx_timer_handler()
174 so->rx.state = ISOTP_IDLE; in isotp_rx_timer_handler()
188 nskb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), gfp_any()); in isotp_send_fc()
192 dev = dev_get_by_index(sock_net(sk), so->ifindex); in isotp_send_fc()
199 can_skb_prv(nskb)->ifindex = dev->ifindex; in isotp_send_fc()
200 can_skb_prv(nskb)->skbcnt = 0; in isotp_send_fc()
202 nskb->dev = dev; in isotp_send_fc()
204 ncf = (struct canfd_frame *)nskb->data; in isotp_send_fc()
205 skb_put_zero(nskb, so->ll.mtu); in isotp_send_fc()
208 ncf->can_id = so->txid; in isotp_send_fc()
210 if (so->opt.flags & CAN_ISOTP_TX_PADDING) { in isotp_send_fc()
211 memset(ncf->data, so->opt.txpad_content, CAN_MAX_DLEN); in isotp_send_fc()
212 ncf->len = CAN_MAX_DLEN; in isotp_send_fc()
214 ncf->len = ae + FC_CONTENT_SZ; in isotp_send_fc()
217 ncf->data[ae] = N_PCI_FC | flowstatus; in isotp_send_fc()
218 ncf->data[ae + 1] = so->rxfc.bs; in isotp_send_fc()
219 ncf->data[ae + 2] = so->rxfc.stmin; in isotp_send_fc()
222 ncf->data[0] = so->opt.ext_address; in isotp_send_fc()
224 ncf->flags = so->ll.tx_flags; in isotp_send_fc()
228 pr_notice_once("can-isotp: %s: can_send_ret %d\n", in isotp_send_fc()
234 so->rx.bs = 0; in isotp_send_fc()
237 so->lastrxcf_tstamp = ktime_set(0, 0); in isotp_send_fc()
240 hrtimer_start(&so->rxtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT); in isotp_send_fc()
246 struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb; in isotp_rcv_skb()
248 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can)); in isotp_rcv_skb()
251 addr->can_family = AF_CAN; in isotp_rcv_skb()
252 addr->can_ifindex = skb->dev->ifindex; in isotp_rcv_skb()
261 8, 8, 8, 8, 8, 8, 8, 8, 8, /* 0 - 8 */ in padlen()
262 12, 12, 12, 12, /* 9 - 12 */ in padlen()
263 16, 16, 16, 16, /* 13 - 16 */ in padlen()
264 20, 20, 20, 20, /* 17 - 20 */ in padlen()
265 24, 24, 24, 24, /* 21 - 24 */ in padlen()
266 32, 32, 32, 32, 32, 32, 32, 32, /* 25 - 32 */ in padlen()
267 48, 48, 48, 48, 48, 48, 48, 48, /* 33 - 40 */ in padlen()
268 48, 48, 48, 48, 48, 48, 48, 48 /* 41 - 48 */ in padlen()
282 * start at cf.data[7] cf->len has to be 7 to be optimal. in check_optimized()
285 if (cf->len <= CAN_MAX_DLEN) in check_optimized()
286 return (cf->len != start_index); in check_optimized()
288 /* This relation is also valid in the non-linear DLC range, where in check_optimized()
290 * The correct check would be (padlen(cf->len) != padlen(start_index)). in check_optimized()
291 * But as cf->len can only take discrete values from 12, .., 64 at this in check_optimized()
292 * point the padlen(cf->len) is always equal to cf->len. in check_optimized()
294 return (cf->len != padlen(start_index)); in check_optimized()
304 if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) { in check_pad()
305 if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) in check_pad()
313 if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) && in check_pad()
314 cf->len != padlen(cf->len)) in check_pad()
318 if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) { in check_pad()
319 for (i = start_index; i < cf->len; i++) in check_pad()
320 if (cf->data[i] != content) in check_pad()
328 struct sock *sk = &so->sk; in isotp_rcv_fc()
330 if (so->tx.state != ISOTP_WAIT_FC && in isotp_rcv_fc()
331 so->tx.state != ISOTP_WAIT_FIRST_FC) in isotp_rcv_fc()
334 hrtimer_cancel(&so->txtimer); in isotp_rcv_fc()
336 if ((cf->len < ae + FC_CONTENT_SZ) || in isotp_rcv_fc()
337 ((so->opt.flags & ISOTP_CHECK_PADDING) && in isotp_rcv_fc()
338 check_pad(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) { in isotp_rcv_fc()
339 /* malformed PDU - report 'not a data message' */ in isotp_rcv_fc()
340 sk->sk_err = EBADMSG; in isotp_rcv_fc()
342 sk->sk_error_report(sk); in isotp_rcv_fc()
344 so->tx.state = ISOTP_IDLE; in isotp_rcv_fc()
345 wake_up_interruptible(&so->wait); in isotp_rcv_fc()
350 if (so->tx.state == ISOTP_WAIT_FIRST_FC) { in isotp_rcv_fc()
351 so->txfc.bs = cf->data[ae + 1]; in isotp_rcv_fc()
352 so->txfc.stmin = cf->data[ae + 2]; in isotp_rcv_fc()
355 if (so->txfc.stmin > 0x7F && in isotp_rcv_fc()
356 (so->txfc.stmin < 0xF1 || so->txfc.stmin > 0xF9)) in isotp_rcv_fc()
357 so->txfc.stmin = 0x7F; in isotp_rcv_fc()
359 so->tx_gap = ktime_set(0, 0); in isotp_rcv_fc()
361 so->tx_gap = ktime_add_ns(so->tx_gap, so->opt.frame_txtime); in isotp_rcv_fc()
363 if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN) in isotp_rcv_fc()
364 so->tx_gap = ktime_add_ns(so->tx_gap, in isotp_rcv_fc()
365 so->force_tx_stmin); in isotp_rcv_fc()
366 else if (so->txfc.stmin < 0x80) in isotp_rcv_fc()
367 so->tx_gap = ktime_add_ns(so->tx_gap, in isotp_rcv_fc()
368 so->txfc.stmin * 1000000); in isotp_rcv_fc()
370 so->tx_gap = ktime_add_ns(so->tx_gap, in isotp_rcv_fc()
371 (so->txfc.stmin - 0xF0) in isotp_rcv_fc()
373 so->tx.state = ISOTP_WAIT_FC; in isotp_rcv_fc()
376 switch (cf->data[ae] & 0x0F) { in isotp_rcv_fc()
378 so->tx.bs = 0; in isotp_rcv_fc()
379 so->tx.state = ISOTP_SENDING; in isotp_rcv_fc()
381 hrtimer_start(&so->txtimer, so->tx_gap, in isotp_rcv_fc()
386 /* start timer to wait for next FC frame */ in isotp_rcv_fc()
387 hrtimer_start(&so->txtimer, ktime_set(1, 0), in isotp_rcv_fc()
392 /* overflow on receiver side - report 'message too long' */ in isotp_rcv_fc()
393 sk->sk_err = EMSGSIZE; in isotp_rcv_fc()
395 sk->sk_error_report(sk); in isotp_rcv_fc()
400 so->tx.state = ISOTP_IDLE; in isotp_rcv_fc()
401 wake_up_interruptible(&so->wait); in isotp_rcv_fc()
412 hrtimer_cancel(&so->rxtimer); in isotp_rcv_sf()
413 so->rx.state = ISOTP_IDLE; in isotp_rcv_sf()
415 if (!len || len > cf->len - pcilen) in isotp_rcv_sf()
418 if ((so->opt.flags & ISOTP_CHECK_PADDING) && in isotp_rcv_sf()
419 check_pad(so, cf, pcilen + len, so->opt.rxpad_content)) { in isotp_rcv_sf()
420 /* malformed PDU - report 'not a data message' */ in isotp_rcv_sf()
421 sk->sk_err = EBADMSG; in isotp_rcv_sf()
423 sk->sk_error_report(sk); in isotp_rcv_sf()
431 memcpy(skb_put(nskb, len), &cf->data[pcilen], len); in isotp_rcv_sf()
433 nskb->tstamp = skb->tstamp; in isotp_rcv_sf()
434 nskb->dev = skb->dev; in isotp_rcv_sf()
446 hrtimer_cancel(&so->rxtimer); in isotp_rcv_ff()
447 so->rx.state = ISOTP_IDLE; in isotp_rcv_ff()
450 so->rx.ll_dl = padlen(cf->len); in isotp_rcv_ff()
453 if (cf->len != so->rx.ll_dl) in isotp_rcv_ff()
457 so->rx.len = (cf->data[ae] & 0x0F) << 8; in isotp_rcv_ff()
458 so->rx.len += cf->data[ae + 1]; in isotp_rcv_ff()
461 if (so->rx.len) { in isotp_rcv_ff()
465 so->rx.len = cf->data[ae + 2] << 24; in isotp_rcv_ff()
466 so->rx.len += cf->data[ae + 3] << 16; in isotp_rcv_ff()
467 so->rx.len += cf->data[ae + 4] << 8; in isotp_rcv_ff()
468 so->rx.len += cf->data[ae + 5]; in isotp_rcv_ff()
473 off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0; in isotp_rcv_ff()
475 if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl) in isotp_rcv_ff()
478 if (so->rx.len > MAX_MSG_LENGTH) { in isotp_rcv_ff()
485 so->rx.idx = 0; in isotp_rcv_ff()
486 for (i = ae + ff_pci_sz; i < so->rx.ll_dl; i++) in isotp_rcv_ff()
487 so->rx.buf[so->rx.idx++] = cf->data[i]; in isotp_rcv_ff()
490 so->rx.sn = 1; in isotp_rcv_ff()
491 so->rx.state = ISOTP_WAIT_DATA; in isotp_rcv_ff()
494 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE) in isotp_rcv_ff()
509 if (so->rx.state != ISOTP_WAIT_DATA) in isotp_rcv_cf()
513 if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) { in isotp_rcv_cf()
514 if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) < in isotp_rcv_cf()
515 so->force_rx_stmin) in isotp_rcv_cf()
518 so->lastrxcf_tstamp = skb->tstamp; in isotp_rcv_cf()
521 hrtimer_cancel(&so->rxtimer); in isotp_rcv_cf()
524 if (cf->len > so->rx.ll_dl) in isotp_rcv_cf()
528 if (cf->len < so->rx.ll_dl) { in isotp_rcv_cf()
530 if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ) in isotp_rcv_cf()
534 if ((cf->data[ae] & 0x0F) != so->rx.sn) { in isotp_rcv_cf()
535 /* wrong sn detected - report 'illegal byte sequence' */ in isotp_rcv_cf()
536 sk->sk_err = EILSEQ; in isotp_rcv_cf()
538 sk->sk_error_report(sk); in isotp_rcv_cf()
540 /* reset rx state */ in isotp_rcv_cf()
541 so->rx.state = ISOTP_IDLE; in isotp_rcv_cf()
544 so->rx.sn++; in isotp_rcv_cf()
545 so->rx.sn %= 16; in isotp_rcv_cf()
547 for (i = ae + N_PCI_SZ; i < cf->len; i++) { in isotp_rcv_cf()
548 so->rx.buf[so->rx.idx++] = cf->data[i]; in isotp_rcv_cf()
549 if (so->rx.idx >= so->rx.len) in isotp_rcv_cf()
553 if (so->rx.idx >= so->rx.len) { in isotp_rcv_cf()
555 so->rx.state = ISOTP_IDLE; in isotp_rcv_cf()
557 if ((so->opt.flags & ISOTP_CHECK_PADDING) && in isotp_rcv_cf()
558 check_pad(so, cf, i + 1, so->opt.rxpad_content)) { in isotp_rcv_cf()
559 /* malformed PDU - report 'not a data message' */ in isotp_rcv_cf()
560 sk->sk_err = EBADMSG; in isotp_rcv_cf()
562 sk->sk_error_report(sk); in isotp_rcv_cf()
566 nskb = alloc_skb(so->rx.len, gfp_any()); in isotp_rcv_cf()
570 memcpy(skb_put(nskb, so->rx.len), so->rx.buf, in isotp_rcv_cf()
571 so->rx.len); in isotp_rcv_cf()
573 nskb->tstamp = skb->tstamp; in isotp_rcv_cf()
574 nskb->dev = skb->dev; in isotp_rcv_cf()
580 if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) { in isotp_rcv_cf()
582 hrtimer_start(&so->rxtimer, ktime_set(1, 0), in isotp_rcv_cf()
588 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE) in isotp_rcv_cf()
591 /* we reached the specified blocksize so->rxfc.bs */ in isotp_rcv_cf()
601 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0; in isotp_rcv()
607 if (skb->len != so->ll.mtu) in isotp_rcv()
610 cf = (struct canfd_frame *)skb->data; in isotp_rcv()
613 if (ae && cf->data[0] != so->opt.rx_ext_address) in isotp_rcv()
616 n_pci_type = cf->data[ae] & 0xF0; in isotp_rcv()
618 if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) { in isotp_rcv()
620 if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) || in isotp_rcv()
621 (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC)) in isotp_rcv()
636 * requirements - no matter if it's CAN 2.0 or CAN FD in isotp_rcv()
640 sf_dl = cf->data[ae] & 0x0F; in isotp_rcv()
642 if (cf->len <= CAN_MAX_DLEN) { in isotp_rcv()
645 if (skb->len == CANFD_MTU) { in isotp_rcv()
656 cf->data[SF_PCI_SZ4 + ae]); in isotp_rcv()
677 int space = so->tx.ll_dl - pcilen; in isotp_fill_dataframe()
678 int num = min_t(int, so->tx.len - so->tx.idx, space); in isotp_fill_dataframe()
681 cf->can_id = so->txid; in isotp_fill_dataframe()
682 cf->len = num + pcilen; in isotp_fill_dataframe()
685 if (so->opt.flags & CAN_ISOTP_TX_PADDING) { in isotp_fill_dataframe()
687 cf->len = padlen(cf->len); in isotp_fill_dataframe()
688 memset(cf->data, so->opt.txpad_content, cf->len); in isotp_fill_dataframe()
689 } else if (cf->len > CAN_MAX_DLEN) { in isotp_fill_dataframe()
691 cf->len = padlen(cf->len); in isotp_fill_dataframe()
692 memset(cf->data, CAN_ISOTP_DEFAULT_PAD_CONTENT, in isotp_fill_dataframe()
693 cf->len); in isotp_fill_dataframe()
698 cf->data[pcilen + i] = so->tx.buf[so->tx.idx++]; in isotp_fill_dataframe()
701 cf->data[0] = so->opt.ext_address; in isotp_fill_dataframe()
710 cf->can_id = so->txid; in isotp_create_fframe()
711 cf->len = so->tx.ll_dl; in isotp_create_fframe()
713 cf->data[0] = so->opt.ext_address; in isotp_create_fframe()
716 if (so->tx.len > 4095) { in isotp_create_fframe()
718 cf->data[ae] = N_PCI_FF; in isotp_create_fframe()
719 cf->data[ae + 1] = 0; in isotp_create_fframe()
720 cf->data[ae + 2] = (u8)(so->tx.len >> 24) & 0xFFU; in isotp_create_fframe()
721 cf->data[ae + 3] = (u8)(so->tx.len >> 16) & 0xFFU; in isotp_create_fframe()
722 cf->data[ae + 4] = (u8)(so->tx.len >> 8) & 0xFFU; in isotp_create_fframe()
723 cf->data[ae + 5] = (u8)so->tx.len & 0xFFU; in isotp_create_fframe()
727 cf->data[ae] = (u8)(so->tx.len >> 8) | N_PCI_FF; in isotp_create_fframe()
728 cf->data[ae + 1] = (u8)so->tx.len & 0xFFU; in isotp_create_fframe()
733 for (i = ae + ff_pci_sz; i < so->tx.ll_dl; i++) in isotp_create_fframe()
734 cf->data[i] = so->tx.buf[so->tx.idx++]; in isotp_create_fframe()
736 so->tx.sn = 1; in isotp_create_fframe()
737 so->tx.state = ISOTP_WAIT_FIRST_FC; in isotp_create_fframe()
744 struct sock *sk = &so->sk; in isotp_tx_timer_handler()
750 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0; in isotp_tx_timer_handler()
752 switch (so->tx.state) { in isotp_tx_timer_handler()
759 sk->sk_err = ECOMM; in isotp_tx_timer_handler()
761 sk->sk_error_report(sk); in isotp_tx_timer_handler()
763 /* reset tx state */ in isotp_tx_timer_handler()
764 so->tx.state = ISOTP_IDLE; in isotp_tx_timer_handler()
765 wake_up_interruptible(&so->wait); in isotp_tx_timer_handler()
771 dev = dev_get_by_index(sock_net(sk), so->ifindex); in isotp_tx_timer_handler()
776 skb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), in isotp_tx_timer_handler()
784 can_skb_prv(skb)->ifindex = dev->ifindex; in isotp_tx_timer_handler()
785 can_skb_prv(skb)->skbcnt = 0; in isotp_tx_timer_handler()
787 cf = (struct canfd_frame *)skb->data; in isotp_tx_timer_handler()
788 skb_put_zero(skb, so->ll.mtu); in isotp_tx_timer_handler()
794 cf->data[ae] = N_PCI_CF | so->tx.sn++; in isotp_tx_timer_handler()
795 so->tx.sn %= 16; in isotp_tx_timer_handler()
796 so->tx.bs++; in isotp_tx_timer_handler()
798 cf->flags = so->ll.tx_flags; in isotp_tx_timer_handler()
800 skb->dev = dev; in isotp_tx_timer_handler()
805 pr_notice_once("can-isotp: %s: can_send_ret %d\n", in isotp_tx_timer_handler()
808 if (so->tx.idx >= so->tx.len) { in isotp_tx_timer_handler()
810 so->tx.state = ISOTP_IDLE; in isotp_tx_timer_handler()
812 wake_up_interruptible(&so->wait); in isotp_tx_timer_handler()
816 if (so->txfc.bs && so->tx.bs >= so->txfc.bs) { in isotp_tx_timer_handler()
817 /* stop and wait for FC */ in isotp_tx_timer_handler()
818 so->tx.state = ISOTP_WAIT_FC; in isotp_tx_timer_handler()
820 hrtimer_set_expires(&so->txtimer, in isotp_tx_timer_handler()
828 if (!so->tx_gap) in isotp_tx_timer_handler()
833 hrtimer_set_expires(&so->txtimer, in isotp_tx_timer_handler()
834 ktime_add(ktime_get(), so->tx_gap)); in isotp_tx_timer_handler()
847 struct sock *sk = sock->sk; in isotp_sendmsg()
849 u32 old_state = so->tx.state; in isotp_sendmsg()
853 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0; in isotp_sendmsg()
854 int wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0; in isotp_sendmsg()
858 if (!so->bound) in isotp_sendmsg()
859 return -EADDRNOTAVAIL; in isotp_sendmsg()
861 /* we do not support multiple buffers - for now */ in isotp_sendmsg()
862 if (cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SENDING) != ISOTP_IDLE || in isotp_sendmsg()
863 wq_has_sleeper(&so->wait)) { in isotp_sendmsg()
864 if (msg->msg_flags & MSG_DONTWAIT) { in isotp_sendmsg()
865 err = -EAGAIN; in isotp_sendmsg()
869 /* wait for complete transmission of current pdu */ in isotp_sendmsg()
870 err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE); in isotp_sendmsg()
876 err = -EINVAL; in isotp_sendmsg()
880 err = memcpy_from_msg(so->tx.buf, msg, size); in isotp_sendmsg()
884 dev = dev_get_by_index(sock_net(sk), so->ifindex); in isotp_sendmsg()
886 err = -ENXIO; in isotp_sendmsg()
890 skb = sock_alloc_send_skb(sk, so->ll.mtu + sizeof(struct can_skb_priv), in isotp_sendmsg()
891 msg->msg_flags & MSG_DONTWAIT, &err); in isotp_sendmsg()
898 can_skb_prv(skb)->ifindex = dev->ifindex; in isotp_sendmsg()
899 can_skb_prv(skb)->skbcnt = 0; in isotp_sendmsg()
901 so->tx.len = size; in isotp_sendmsg()
902 so->tx.idx = 0; in isotp_sendmsg()
904 cf = (struct canfd_frame *)skb->data; in isotp_sendmsg()
905 skb_put_zero(skb, so->ll.mtu); in isotp_sendmsg()
908 off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0; in isotp_sendmsg()
911 if (size <= so->tx.ll_dl - SF_PCI_SZ4 - ae - off) { in isotp_sendmsg()
912 /* The message size generally fits into a SingleFrame - good. in isotp_sendmsg()
921 if (size <= CAN_MAX_DLEN - SF_PCI_SZ4 - ae) in isotp_sendmsg()
927 cf->data[ae] = N_PCI_SF; in isotp_sendmsg()
931 cf->data[SF_PCI_SZ4 + ae] = size; in isotp_sendmsg()
933 cf->data[ae] |= size; in isotp_sendmsg()
935 so->tx.state = ISOTP_IDLE; in isotp_sendmsg()
936 wake_up_interruptible(&so->wait); in isotp_sendmsg()
938 /* don't enable wait queue for a single frame transmission */ in isotp_sendmsg()
941 /* send first frame and wait for FC */ in isotp_sendmsg()
946 hrtimer_start(&so->txtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT); in isotp_sendmsg()
950 cf->flags = so->ll.tx_flags; in isotp_sendmsg()
952 skb->dev = dev; in isotp_sendmsg()
953 skb->sk = sk; in isotp_sendmsg()
957 pr_notice_once("can-isotp: %s: can_send_ret %d\n", in isotp_sendmsg()
963 /* wait for complete transmission of current pdu */ in isotp_sendmsg()
964 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE); in isotp_sendmsg()
966 if (sk->sk_err) in isotp_sendmsg()
967 return -sk->sk_err; in isotp_sendmsg()
973 so->tx.state = old_state; in isotp_sendmsg()
974 if (so->tx.state == ISOTP_IDLE) in isotp_sendmsg()
975 wake_up_interruptible(&so->wait); in isotp_sendmsg()
983 struct sock *sk = sock->sk; in isotp_recvmsg()
995 if (size < skb->len) in isotp_recvmsg()
996 msg->msg_flags |= MSG_TRUNC; in isotp_recvmsg()
998 size = skb->len; in isotp_recvmsg()
1000 err = memcpy_to_msg(msg, skb->data, size); in isotp_recvmsg()
1008 if (msg->msg_name) { in isotp_recvmsg()
1010 msg->msg_namelen = ISOTP_MIN_NAMELEN; in isotp_recvmsg()
1011 memcpy(msg->msg_name, skb->cb, msg->msg_namelen); in isotp_recvmsg()
1021 struct sock *sk = sock->sk; in isotp_release()
1031 /* wait for complete transmission of current pdu */ in isotp_release()
1032 wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE); in isotp_release()
1040 list_del(&so->notifier); in isotp_release()
1046 if (so->bound) { in isotp_release()
1047 if (so->ifindex) { in isotp_release()
1050 dev = dev_get_by_index(net, so->ifindex); in isotp_release()
1052 can_rx_unregister(net, dev, so->rxid, in isotp_release()
1053 SINGLE_MASK(so->rxid), in isotp_release()
1061 hrtimer_cancel(&so->txtimer); in isotp_release()
1062 hrtimer_cancel(&so->rxtimer); in isotp_release()
1064 so->ifindex = 0; in isotp_release()
1065 so->bound = 0; in isotp_release()
1068 sock->sk = NULL; in isotp_release()
1079 struct sock *sk = sock->sk; in isotp_bind()
1088 return -EINVAL; in isotp_bind()
1090 if (addr->can_addr.tp.rx_id == addr->can_addr.tp.tx_id) in isotp_bind()
1091 return -EADDRNOTAVAIL; in isotp_bind()
1093 if ((addr->can_addr.tp.rx_id | addr->can_addr.tp.tx_id) & in isotp_bind()
1095 return -EADDRNOTAVAIL; in isotp_bind()
1097 if (!addr->can_ifindex) in isotp_bind()
1098 return -ENODEV; in isotp_bind()
1102 if (so->bound && addr->can_ifindex == so->ifindex && in isotp_bind()
1103 addr->can_addr.tp.rx_id == so->rxid && in isotp_bind()
1104 addr->can_addr.tp.tx_id == so->txid) in isotp_bind()
1107 dev = dev_get_by_index(net, addr->can_ifindex); in isotp_bind()
1109 err = -ENODEV; in isotp_bind()
1112 if (dev->type != ARPHRD_CAN) { in isotp_bind()
1114 err = -ENODEV; in isotp_bind()
1117 if (dev->mtu < so->ll.mtu) { in isotp_bind()
1119 err = -EINVAL; in isotp_bind()
1122 if (!(dev->flags & IFF_UP)) in isotp_bind()
1125 ifindex = dev->ifindex; in isotp_bind()
1127 can_rx_register(net, dev, addr->can_addr.tp.rx_id, in isotp_bind()
1128 SINGLE_MASK(addr->can_addr.tp.rx_id), isotp_rcv, sk, in isotp_bind()
1133 if (so->bound) { in isotp_bind()
1135 if (so->ifindex) { in isotp_bind()
1136 dev = dev_get_by_index(net, so->ifindex); in isotp_bind()
1138 can_rx_unregister(net, dev, so->rxid, in isotp_bind()
1139 SINGLE_MASK(so->rxid), in isotp_bind()
1147 so->ifindex = ifindex; in isotp_bind()
1148 so->rxid = addr->can_addr.tp.rx_id; in isotp_bind()
1149 so->txid = addr->can_addr.tp.tx_id; in isotp_bind()
1150 so->bound = 1; in isotp_bind()
1156 sk->sk_err = ENETDOWN; in isotp_bind()
1158 sk->sk_error_report(sk); in isotp_bind()
1167 struct sock *sk = sock->sk; in isotp_getname()
1171 return -EOPNOTSUPP; in isotp_getname()
1174 addr->can_family = AF_CAN; in isotp_getname()
1175 addr->can_ifindex = so->ifindex; in isotp_getname()
1176 addr->can_addr.tp.rx_id = so->rxid; in isotp_getname()
1177 addr->can_addr.tp.tx_id = so->txid; in isotp_getname()
1185 struct sock *sk = sock->sk; in isotp_setsockopt()
1190 return -EINVAL; in isotp_setsockopt()
1192 if (so->bound) in isotp_setsockopt()
1193 return -EISCONN; in isotp_setsockopt()
1198 return -EINVAL; in isotp_setsockopt()
1200 if (copy_from_sockptr(&so->opt, optval, optlen)) in isotp_setsockopt()
1201 return -EFAULT; in isotp_setsockopt()
1204 if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR)) in isotp_setsockopt()
1205 so->opt.rx_ext_address = so->opt.ext_address; in isotp_setsockopt()
1210 return -EINVAL; in isotp_setsockopt()
1212 if (copy_from_sockptr(&so->rxfc, optval, optlen)) in isotp_setsockopt()
1213 return -EFAULT; in isotp_setsockopt()
1218 return -EINVAL; in isotp_setsockopt()
1220 if (copy_from_sockptr(&so->force_tx_stmin, optval, optlen)) in isotp_setsockopt()
1221 return -EFAULT; in isotp_setsockopt()
1226 return -EINVAL; in isotp_setsockopt()
1228 if (copy_from_sockptr(&so->force_rx_stmin, optval, optlen)) in isotp_setsockopt()
1229 return -EFAULT; in isotp_setsockopt()
1237 return -EFAULT; in isotp_setsockopt()
1239 /* check for correct ISO 11898-1 DLC data length */ in isotp_setsockopt()
1241 return -EINVAL; in isotp_setsockopt()
1244 return -EINVAL; in isotp_setsockopt()
1248 return -EINVAL; in isotp_setsockopt()
1250 memcpy(&so->ll, &ll, sizeof(ll)); in isotp_setsockopt()
1253 so->tx.ll_dl = ll.tx_dl; in isotp_setsockopt()
1255 return -EINVAL; in isotp_setsockopt()
1260 ret = -ENOPROTOOPT; in isotp_setsockopt()
1269 struct sock *sk = sock->sk; in isotp_getsockopt()
1275 return -EINVAL; in isotp_getsockopt()
1277 return -EFAULT; in isotp_getsockopt()
1279 return -EINVAL; in isotp_getsockopt()
1284 val = &so->opt; in isotp_getsockopt()
1289 val = &so->rxfc; in isotp_getsockopt()
1294 val = &so->force_tx_stmin; in isotp_getsockopt()
1299 val = &so->force_rx_stmin; in isotp_getsockopt()
1304 val = &so->ll; in isotp_getsockopt()
1308 return -ENOPROTOOPT; in isotp_getsockopt()
1312 return -EFAULT; in isotp_getsockopt()
1314 return -EFAULT; in isotp_getsockopt()
1321 struct sock *sk = &so->sk; in isotp_notify()
1326 if (so->ifindex != dev->ifindex) in isotp_notify()
1333 if (so->bound) in isotp_notify()
1334 can_rx_unregister(dev_net(dev), dev, so->rxid, in isotp_notify()
1335 SINGLE_MASK(so->rxid), in isotp_notify()
1338 so->ifindex = 0; in isotp_notify()
1339 so->bound = 0; in isotp_notify()
1342 sk->sk_err = ENODEV; in isotp_notify()
1344 sk->sk_error_report(sk); in isotp_notify()
1348 sk->sk_err = ENETDOWN; in isotp_notify()
1350 sk->sk_error_report(sk); in isotp_notify()
1360 if (dev->type != ARPHRD_CAN) in isotp_notifier()
1382 so->ifindex = 0; in isotp_init()
1383 so->bound = 0; in isotp_init()
1385 so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS; in isotp_init()
1386 so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS; in isotp_init()
1387 so->opt.rx_ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS; in isotp_init()
1388 so->opt.rxpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT; in isotp_init()
1389 so->opt.txpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT; in isotp_init()
1390 so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME; in isotp_init()
1391 so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS; in isotp_init()
1392 so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN; in isotp_init()
1393 so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX; in isotp_init()
1394 so->ll.mtu = CAN_ISOTP_DEFAULT_LL_MTU; in isotp_init()
1395 so->ll.tx_dl = CAN_ISOTP_DEFAULT_LL_TX_DL; in isotp_init()
1396 so->ll.tx_flags = CAN_ISOTP_DEFAULT_LL_TX_FLAGS; in isotp_init()
1399 so->tx.ll_dl = so->ll.tx_dl; in isotp_init()
1401 so->rx.state = ISOTP_IDLE; in isotp_init()
1402 so->tx.state = ISOTP_IDLE; in isotp_init()
1404 hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT); in isotp_init()
1405 so->rxtimer.function = isotp_rx_timer_handler; in isotp_init()
1406 hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT); in isotp_init()
1407 so->txtimer.function = isotp_tx_timer_handler; in isotp_init()
1409 init_waitqueue_head(&so->wait); in isotp_init()
1412 list_add_tail(&so->notifier, &isotp_notifier_list); in isotp_init()
1421 /* no ioctls for socket layer -> hand it down to NIC layer */ in isotp_sock_no_ioctlcmd()
1422 return -ENOIOCTLCMD; in isotp_sock_no_ioctlcmd()