Lines Matching +full:rx +full:- +full:tx

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
75 MODULE_DESCRIPTION("PF_CAN ISO 15765-2 transport protocol");
78 MODULE_ALIAS("can-proto-6");
86 /* Since ISO 15765-2:2016 the CAN isotp protocol supports more than 4095
94 /* maximum PDU size before ISO 15765-2:2016 extension was 4095 */
105 /* N_PCI type values in bits 7-4 of N_PCI bytes */
166 struct tpcon rx, tx;
183 return so->opt.flags & ISOTP_ALL_BC_FLAGS;
196 struct sock *sk = &so->sk;
198 if (so->rx.state == ISOTP_WAIT_DATA) {
202 sk->sk_err = ETIMEDOUT;
206 /* reset rx state */
207 so->rx.state = ISOTP_IDLE;
221 nskb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), gfp_any());
225 dev = dev_get_by_index(sock_net(sk), so->ifindex);
232 can_skb_prv(nskb)->ifindex = dev->ifindex;
233 can_skb_prv(nskb)->skbcnt = 0;
235 nskb->dev = dev;
237 ncf = (struct canfd_frame *)nskb->data;
238 skb_put_zero(nskb, so->ll.mtu);
241 ncf->can_id = so->txid;
243 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
244 memset(ncf->data, so->opt.txpad_content, CAN_MAX_DLEN);
245 ncf->len = CAN_MAX_DLEN;
247 ncf->len = ae + FC_CONTENT_SZ;
250 ncf->data[ae] = N_PCI_FC | flowstatus;
251 ncf->data[ae + 1] = so->rxfc.bs;
252 ncf->data[ae + 2] = so->rxfc.stmin;
255 ncf->data[0] = so->opt.ext_address;
257 ncf->flags = so->ll.tx_flags;
261 pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
267 so->rx.bs = 0;
269 /* reset last CF frame rx timestamp for rx stmin enforcement */
270 so->lastrxcf_tstamp = ktime_set(0, 0);
272 /* start rx timeout watchdog */
273 hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
280 struct sockaddr_can *addr = (struct sockaddr_can *)skb->cb;
283 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can));
286 addr->can_family = AF_CAN;
287 addr->can_ifindex = skb->dev->ifindex;
296 8, 8, 8, 8, 8, 8, 8, 8, 8, /* 0 - 8 */
297 12, 12, 12, 12, /* 9 - 12 */
298 16, 16, 16, 16, /* 13 - 16 */
299 20, 20, 20, 20, /* 17 - 20 */
300 24, 24, 24, 24, /* 21 - 24 */
301 32, 32, 32, 32, 32, 32, 32, 32, /* 25 - 32 */
302 48, 48, 48, 48, 48, 48, 48, 48, /* 33 - 40 */
303 48, 48, 48, 48, 48, 48, 48, 48 /* 41 - 48 */
317 * start at cf.data[7] cf->len has to be 7 to be optimal.
320 if (cf->len <= CAN_MAX_DLEN)
321 return (cf->len != start_index);
323 /* This relation is also valid in the non-linear DLC range, where
325 * The correct check would be (padlen(cf->len) != padlen(start_index)).
326 * But as cf->len can only take discrete values from 12, .., 64 at this
327 * point the padlen(cf->len) is always equal to cf->len.
329 return (cf->len != padlen(start_index));
339 if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) {
340 if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN)
348 if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) &&
349 cf->len != padlen(cf->len))
353 if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
354 for (i = start_index; i < cf->len; i++)
355 if (cf->data[i] != content)
365 struct sock *sk = &so->sk;
367 if (so->tx.state != ISOTP_WAIT_FC &&
368 so->tx.state != ISOTP_WAIT_FIRST_FC)
371 hrtimer_cancel(&so->txtimer);
373 if ((cf->len < ae + FC_CONTENT_SZ) ||
374 ((so->opt.flags & ISOTP_CHECK_PADDING) &&
375 check_pad(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) {
376 /* malformed PDU - report 'not a data message' */
377 sk->sk_err = EBADMSG;
381 so->tx.state = ISOTP_IDLE;
382 wake_up_interruptible(&so->wait);
387 if (so->tx.state == ISOTP_WAIT_FIRST_FC ||
388 so->opt.flags & CAN_ISOTP_DYN_FC_PARMS) {
389 so->txfc.bs = cf->data[ae + 1];
390 so->txfc.stmin = cf->data[ae + 2];
393 if (so->txfc.stmin > 0x7F &&
394 (so->txfc.stmin < 0xF1 || so->txfc.stmin > 0xF9))
395 so->txfc.stmin = 0x7F;
397 so->tx_gap = ktime_set(0, 0);
399 so->tx_gap = ktime_add_ns(so->tx_gap, so->frame_txtime);
401 if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
402 so->tx_gap = ktime_add_ns(so->tx_gap,
403 so->force_tx_stmin);
404 else if (so->txfc.stmin < 0x80)
405 so->tx_gap = ktime_add_ns(so->tx_gap,
406 so->txfc.stmin * 1000000);
408 so->tx_gap = ktime_add_ns(so->tx_gap,
409 (so->txfc.stmin - 0xF0)
411 so->tx.state = ISOTP_WAIT_FC;
414 switch (cf->data[ae] & 0x0F) {
416 so->tx.bs = 0;
417 so->tx.state = ISOTP_SENDING;
419 hrtimer_start(&so->txtimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
426 hrtimer_start(&so->txtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
431 /* overflow on receiver side - report 'message too long' */
432 sk->sk_err = EMSGSIZE;
438 /* stop this tx job */
439 so->tx.state = ISOTP_IDLE;
440 wake_up_interruptible(&so->wait);
451 hrtimer_cancel(&so->rxtimer);
452 so->rx.state = ISOTP_IDLE;
454 if (!len || len > cf->len - pcilen)
457 if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
458 check_pad(so, cf, pcilen + len, so->opt.rxpad_content)) {
459 /* malformed PDU - report 'not a data message' */
460 sk->sk_err = EBADMSG;
470 memcpy(skb_put(nskb, len), &cf->data[pcilen], len);
472 nskb->tstamp = skb->tstamp;
473 nskb->dev = skb->dev;
485 hrtimer_cancel(&so->rxtimer);
486 so->rx.state = ISOTP_IDLE;
489 so->rx.ll_dl = padlen(cf->len);
492 if (cf->len != so->rx.ll_dl)
496 so->rx.len = (cf->data[ae] & 0x0F) << 8;
497 so->rx.len += cf->data[ae + 1];
500 if (so->rx.len) {
504 so->rx.len = cf->data[ae + 2] << 24;
505 so->rx.len += cf->data[ae + 3] << 16;
506 so->rx.len += cf->data[ae + 4] << 8;
507 so->rx.len += cf->data[ae + 5];
512 off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
514 if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl)
518 if (so->rx.len > so->rx.buflen && so->rx.buflen < max_pdu_size) {
522 so->rx.buf = newbuf;
523 so->rx.buflen = max_pdu_size;
527 if (so->rx.len > so->rx.buflen) {
534 so->rx.idx = 0;
535 for (i = ae + ff_pci_sz; i < so->rx.ll_dl; i++)
536 so->rx.buf[so->rx.idx++] = cf->data[i];
539 so->rx.sn = 1;
540 so->rx.state = ISOTP_WAIT_DATA;
543 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
558 if (so->rx.state != ISOTP_WAIT_DATA)
562 if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) {
563 if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) <
564 so->force_rx_stmin)
567 so->lastrxcf_tstamp = skb->tstamp;
570 hrtimer_cancel(&so->rxtimer);
573 if (cf->len > so->rx.ll_dl)
577 if (cf->len < so->rx.ll_dl) {
579 if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ)
583 if ((cf->data[ae] & 0x0F) != so->rx.sn) {
584 /* wrong sn detected - report 'illegal byte sequence' */
585 sk->sk_err = EILSEQ;
589 /* reset rx state */
590 so->rx.state = ISOTP_IDLE;
593 so->rx.sn++;
594 so->rx.sn %= 16;
596 for (i = ae + N_PCI_SZ; i < cf->len; i++) {
597 so->rx.buf[so->rx.idx++] = cf->data[i];
598 if (so->rx.idx >= so->rx.len)
602 if (so->rx.idx >= so->rx.len) {
604 so->rx.state = ISOTP_IDLE;
606 if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
607 check_pad(so, cf, i + 1, so->opt.rxpad_content)) {
608 /* malformed PDU - report 'not a data message' */
609 sk->sk_err = EBADMSG;
615 nskb = alloc_skb(so->rx.len, gfp_any());
619 memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
620 so->rx.len);
622 nskb->tstamp = skb->tstamp;
623 nskb->dev = skb->dev;
629 if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
630 /* start rx timeout watchdog */
631 hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
637 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
640 /* we reached the specified blocksize so->rxfc.bs */
650 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
656 if (skb->len != so->ll.mtu)
659 cf = (struct canfd_frame *)skb->data;
662 if (ae && cf->data[0] != so->opt.rx_ext_address)
665 n_pci_type = cf->data[ae] & 0xF0;
671 spin_lock(&so->rx_lock);
673 if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
674 /* check rx/tx path half duplex expectations */
675 if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) ||
676 (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC))
682 /* tx path: flow control frame containing the FC parameters */
687 /* rx path: single frame
689 * As we do not have a rx.ll_dl configuration, we can only test
691 * requirements - no matter if it's CAN 2.0 or CAN FD
695 sf_dl = cf->data[ae] & 0x0F;
697 if (cf->len <= CAN_MAX_DLEN) {
711 cf->data[SF_PCI_SZ4 + ae]);
717 /* rx path: first frame */
722 /* rx path: consecutive frame */
728 spin_unlock(&so->rx_lock);
735 int space = so->tx.ll_dl - pcilen;
736 int num = min_t(int, so->tx.len - so->tx.idx, space);
739 cf->can_id = so->txid;
740 cf->len = num + pcilen;
743 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
745 cf->len = padlen(cf->len);
746 memset(cf->data, so->opt.txpad_content, cf->len);
747 } else if (cf->len > CAN_MAX_DLEN) {
749 cf->len = padlen(cf->len);
750 memset(cf->data, CAN_ISOTP_DEFAULT_PAD_CONTENT,
751 cf->len);
756 cf->data[pcilen + i] = so->tx.buf[so->tx.idx++];
759 cf->data[0] = so->opt.ext_address;
764 struct sock *sk = &so->sk;
769 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
771 dev = dev_get_by_index(sock_net(sk), so->ifindex);
775 skb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), GFP_ATOMIC);
782 can_skb_prv(skb)->ifindex = dev->ifindex;
783 can_skb_prv(skb)->skbcnt = 0;
785 cf = (struct canfd_frame *)skb->data;
786 skb_put_zero(skb, so->ll.mtu);
792 cf->data[ae] = N_PCI_CF | so->tx.sn++;
793 so->tx.sn %= 16;
794 so->tx.bs++;
796 cf->flags = so->ll.tx_flags;
798 skb->dev = dev;
802 if (so->cfecho)
803 pr_notice_once("can-isotp: cfecho is %08X != 0\n", so->cfecho);
806 so->cfecho = *(u32 *)cf->data;
811 pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
813 if (can_send_ret == -ENOBUFS)
814 pr_notice_once("can-isotp: tx queue is full\n");
825 cf->can_id = so->txid;
826 cf->len = so->tx.ll_dl;
828 cf->data[0] = so->opt.ext_address;
831 if (so->tx.len > MAX_12BIT_PDU_SIZE) {
833 cf->data[ae] = N_PCI_FF;
834 cf->data[ae + 1] = 0;
835 cf->data[ae + 2] = (u8)(so->tx.len >> 24) & 0xFFU;
836 cf->data[ae + 3] = (u8)(so->tx.len >> 16) & 0xFFU;
837 cf->data[ae + 4] = (u8)(so->tx.len >> 8) & 0xFFU;
838 cf->data[ae + 5] = (u8)so->tx.len & 0xFFU;
842 cf->data[ae] = (u8)(so->tx.len >> 8) | N_PCI_FF;
843 cf->data[ae + 1] = (u8)so->tx.len & 0xFFU;
848 for (i = ae + ff_pci_sz; i < so->tx.ll_dl; i++)
849 cf->data[i] = so->tx.buf[so->tx.idx++];
851 so->tx.sn = 1;
858 struct canfd_frame *cf = (struct canfd_frame *)skb->data;
861 if (skb->sk != sk || so->cfecho != *(u32 *)cf->data)
865 hrtimer_cancel(&so->txtimer);
868 so->cfecho = 0;
870 if (so->tx.idx >= so->tx.len) {
872 so->tx.state = ISOTP_IDLE;
873 wake_up_interruptible(&so->wait);
877 if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
879 so->tx.state = ISOTP_WAIT_FC;
880 hrtimer_start(&so->txtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
886 if (!so->tx_gap) {
888 hrtimer_start(&so->txtimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
895 hrtimer_start(&so->txfrtimer, so->tx_gap, HRTIMER_MODE_REL_SOFT);
902 struct sock *sk = &so->sk;
905 if (so->tx.state == ISOTP_IDLE || so->tx.state == ISOTP_SHUTDOWN)
911 sk->sk_err = ECOMM;
915 /* reset tx state */
916 so->tx.state = ISOTP_IDLE;
917 wake_up_interruptible(&so->wait);
928 hrtimer_start(&so->txtimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
932 if (so->tx.state == ISOTP_SENDING && !so->cfecho)
940 struct sock *sk = sock->sk;
945 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
946 int wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0;
951 if (!so->bound || so->tx.state == ISOTP_SHUTDOWN)
952 return -EADDRNOTAVAIL;
954 while (cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SENDING) != ISOTP_IDLE) {
955 /* we do not support multiple buffers - for now */
956 if (msg->msg_flags & MSG_DONTWAIT)
957 return -EAGAIN;
959 if (so->tx.state == ISOTP_SHUTDOWN)
960 return -EADDRNOTAVAIL;
963 err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
969 if (size > so->tx.buflen && so->tx.buflen < max_pdu_size) {
973 so->tx.buf = newbuf;
974 so->tx.buflen = max_pdu_size;
978 if (!size || size > so->tx.buflen) {
979 err = -EINVAL;
984 off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
988 (size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off)) {
989 err = -EINVAL;
993 err = memcpy_from_msg(so->tx.buf, msg, size);
997 dev = dev_get_by_index(sock_net(sk), so->ifindex);
999 err = -ENXIO;
1003 skb = sock_alloc_send_skb(sk, so->ll.mtu + sizeof(struct can_skb_priv),
1004 msg->msg_flags & MSG_DONTWAIT, &err);
1011 can_skb_prv(skb)->ifindex = dev->ifindex;
1012 can_skb_prv(skb)->skbcnt = 0;
1014 so->tx.len = size;
1015 so->tx.idx = 0;
1017 cf = (struct canfd_frame *)skb->data;
1018 skb_put_zero(skb, so->ll.mtu);
1021 if (so->cfecho)
1022 pr_notice_once("can-isotp: uninit cfecho %08X\n", so->cfecho);
1025 if (size <= so->tx.ll_dl - SF_PCI_SZ4 - ae - off) {
1026 /* The message size generally fits into a SingleFrame - good.
1035 if (size <= CAN_MAX_DLEN - SF_PCI_SZ4 - ae)
1041 cf->data[ae] = N_PCI_SF;
1045 cf->data[SF_PCI_SZ4 + ae] = size;
1047 cf->data[ae] |= size;
1049 /* set CF echo tag for isotp_rcv_echo() (SF-mode) */
1050 so->cfecho = *(u32 *)cf->data;
1057 /* set timer for FC-less operation (STmin = 0) */
1058 if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
1059 so->tx_gap = ktime_set(0, so->force_tx_stmin);
1061 so->tx_gap = ktime_set(0, so->frame_txtime);
1064 so->txfc.bs = 0;
1066 /* set CF echo tag for isotp_rcv_echo() (CF-mode) */
1067 so->cfecho = *(u32 *)cf->data;
1070 so->tx.state = ISOTP_WAIT_FIRST_FC;
1075 /* no CF echo tag for isotp_rcv_echo() (FF-mode) */
1076 so->cfecho = 0;
1080 hrtimer_start(&so->txtimer, ktime_set(hrtimer_sec, 0),
1084 cf->flags = so->ll.tx_flags;
1086 skb->dev = dev;
1087 skb->sk = sk;
1091 pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
1094 /* no transmission -> no timeout monitoring */
1095 hrtimer_cancel(&so->txtimer);
1098 so->cfecho = 0;
1105 err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
1117 /* got signal: force tx state machine to be idle */
1118 so->tx.state = ISOTP_IDLE;
1119 hrtimer_cancel(&so->txfrtimer);
1120 hrtimer_cancel(&so->txtimer);
1123 so->tx.state = ISOTP_IDLE;
1124 wake_up_interruptible(&so->wait);
1132 struct sock *sk = sock->sk;
1138 return -EINVAL;
1140 if (!so->bound)
1141 return -EADDRNOTAVAIL;
1147 if (size < skb->len)
1148 msg->msg_flags |= MSG_TRUNC;
1150 size = skb->len;
1152 ret = memcpy_to_msg(msg, skb->data, size);
1158 if (msg->msg_name) {
1160 msg->msg_namelen = ISOTP_MIN_NAMELEN;
1161 memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1165 ret = (flags & MSG_TRUNC) ? skb->len : size;
1175 struct sock *sk = sock->sk;
1186 while (wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE) == 0 &&
1187 cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SHUTDOWN) != ISOTP_IDLE)
1191 so->tx.state = ISOTP_SHUTDOWN;
1192 so->rx.state = ISOTP_IDLE;
1200 list_del(&so->notifier);
1206 if (so->bound) {
1207 if (so->ifindex) {
1210 dev = dev_get_by_index(net, so->ifindex);
1213 can_rx_unregister(net, dev, so->rxid,
1214 SINGLE_MASK(so->rxid),
1217 can_rx_unregister(net, dev, so->txid,
1218 SINGLE_MASK(so->txid),
1226 hrtimer_cancel(&so->txfrtimer);
1227 hrtimer_cancel(&so->txtimer);
1228 hrtimer_cancel(&so->rxtimer);
1230 so->ifindex = 0;
1231 so->bound = 0;
1233 if (so->rx.buf != so->rx.sbuf)
1234 kfree(so->rx.buf);
1236 if (so->tx.buf != so->tx.sbuf)
1237 kfree(so->tx.buf);
1240 sock->sk = NULL;
1243 sock_prot_inuse_add(net, sk->sk_prot, -1);
1252 struct sock *sk = sock->sk;
1257 canid_t tx_id = addr->can_addr.tp.tx_id;
1258 canid_t rx_id = addr->can_addr.tp.rx_id;
1263 return -EINVAL;
1265 if (addr->can_family != AF_CAN)
1266 return -EINVAL;
1268 /* sanitize tx CAN identifier */
1274 /* give feedback on wrong CAN-ID value */
1275 if (tx_id != addr->can_addr.tp.tx_id)
1276 return -EINVAL;
1278 /* sanitize rx CAN identifier (if needed) */
1285 /* give feedback on wrong CAN-ID value */
1286 if (rx_id != addr->can_addr.tp.rx_id)
1287 return -EINVAL;
1290 if (!addr->can_ifindex)
1291 return -ENODEV;
1295 if (so->bound) {
1296 err = -EINVAL;
1302 err = -EADDRNOTAVAIL;
1306 dev = dev_get_by_index(net, addr->can_ifindex);
1308 err = -ENODEV;
1311 if (dev->type != ARPHRD_CAN) {
1313 err = -ENODEV;
1316 if (READ_ONCE(dev->mtu) < so->ll.mtu) {
1318 err = -EINVAL;
1321 if (!(dev->flags & IFF_UP))
1324 ifindex = dev->ifindex;
1331 so->cfecho = 0;
1340 so->ifindex = ifindex;
1341 so->rxid = rx_id;
1342 so->txid = tx_id;
1343 so->bound = 1;
1349 sk->sk_err = ENETDOWN;
1360 struct sock *sk = sock->sk;
1364 return -EOPNOTSUPP;
1367 addr->can_family = AF_CAN;
1368 addr->can_ifindex = so->ifindex;
1369 addr->can_addr.tp.rx_id = so->rxid;
1370 addr->can_addr.tp.tx_id = so->txid;
1378 struct sock *sk = sock->sk;
1382 if (so->bound)
1383 return -EISCONN;
1388 return -EINVAL;
1390 if (copy_from_sockptr(&so->opt, optval, optlen))
1391 return -EFAULT;
1394 if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
1395 so->opt.rx_ext_address = so->opt.ext_address;
1400 so->opt.flags &= ~CAN_ISOTP_CF_BROADCAST;
1403 ret = -EINVAL;
1407 if (so->opt.frame_txtime) {
1408 if (so->opt.frame_txtime == CAN_ISOTP_FRAME_TXTIME_ZERO)
1409 so->frame_txtime = 0;
1411 so->frame_txtime = so->opt.frame_txtime;
1417 return -EINVAL;
1419 if (copy_from_sockptr(&so->rxfc, optval, optlen))
1420 return -EFAULT;
1425 return -EINVAL;
1427 if (copy_from_sockptr(&so->force_tx_stmin, optval, optlen))
1428 return -EFAULT;
1433 return -EINVAL;
1435 if (copy_from_sockptr(&so->force_rx_stmin, optval, optlen))
1436 return -EFAULT;
1444 return -EFAULT;
1446 /* check for correct ISO 11898-1 DLC data length */
1448 return -EINVAL;
1451 return -EINVAL;
1455 return -EINVAL;
1457 memcpy(&so->ll, &ll, sizeof(ll));
1459 /* set ll_dl for tx path to similar place as for rx */
1460 so->tx.ll_dl = ll.tx_dl;
1462 return -EINVAL;
1467 ret = -ENOPROTOOPT;
1477 struct sock *sk = sock->sk;
1481 return -EINVAL;
1492 struct sock *sk = sock->sk;
1498 return -EINVAL;
1500 return -EFAULT;
1502 return -EINVAL;
1507 val = &so->opt;
1512 val = &so->rxfc;
1517 val = &so->force_tx_stmin;
1522 val = &so->force_rx_stmin;
1527 val = &so->ll;
1531 return -ENOPROTOOPT;
1535 return -EFAULT;
1537 return -EFAULT;
1544 struct sock *sk = &so->sk;
1549 if (so->ifindex != dev->ifindex)
1556 if (so->bound) {
1558 can_rx_unregister(dev_net(dev), dev, so->rxid,
1559 SINGLE_MASK(so->rxid),
1562 can_rx_unregister(dev_net(dev), dev, so->txid,
1563 SINGLE_MASK(so->txid),
1567 so->ifindex = 0;
1568 so->bound = 0;
1571 sk->sk_err = ENODEV;
1577 sk->sk_err = ENETDOWN;
1589 if (dev->type != ARPHRD_CAN)
1611 so->ifindex = 0;
1612 so->bound = 0;
1614 so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
1615 so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1616 so->opt.rx_ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1617 so->opt.rxpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
1618 so->opt.txpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
1619 so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1620 so->frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1621 so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
1622 so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
1623 so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;
1624 so->ll.mtu = CAN_ISOTP_DEFAULT_LL_MTU;
1625 so->ll.tx_dl = CAN_ISOTP_DEFAULT_LL_TX_DL;
1626 so->ll.tx_flags = CAN_ISOTP_DEFAULT_LL_TX_FLAGS;
1628 /* set ll_dl for tx path to similar place as for rx */
1629 so->tx.ll_dl = so->ll.tx_dl;
1631 so->rx.state = ISOTP_IDLE;
1632 so->tx.state = ISOTP_IDLE;
1634 so->rx.buf = so->rx.sbuf;
1635 so->tx.buf = so->tx.sbuf;
1636 so->rx.buflen = ARRAY_SIZE(so->rx.sbuf);
1637 so->tx.buflen = ARRAY_SIZE(so->tx.sbuf);
1639 hrtimer_setup(&so->rxtimer, isotp_rx_timer_handler, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1640 hrtimer_setup(&so->txtimer, isotp_tx_timer_handler, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1641 hrtimer_setup(&so->txfrtimer, isotp_txfr_timer_handler, CLOCK_MONOTONIC,
1644 init_waitqueue_head(&so->wait);
1645 spin_lock_init(&so->rx_lock);
1648 list_add_tail(&so->notifier, &isotp_notifier_list);
1656 struct sock *sk = sock->sk;
1660 poll_wait(file, &so->wait, wait);
1662 /* Check for false positives due to TX state */
1663 if ((mask & EPOLLWRNORM) && (so->tx.state != ISOTP_IDLE))
1672 /* no ioctls for socket layer -> hand it down to NIC layer */
1673 return -ENOIOCTLCMD;