Lines Matching refs:so
180 static u32 isotp_bc_flags(struct isotp_sock *so)
182 return so->opt.flags & ISOTP_ALL_BC_FLAGS;
185 static bool isotp_register_rxid(struct isotp_sock *so)
188 return (isotp_bc_flags(so) == 0);
193 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
195 struct sock *sk = &so->sk;
197 if (so->rx.state == ISOTP_WAIT_DATA) {
206 so->rx.state = ISOTP_IDLE;
217 struct isotp_sock *so = isotp_sk(sk);
220 nskb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), gfp_any());
224 dev = dev_get_by_index(sock_net(sk), so->ifindex);
237 skb_put_zero(nskb, so->ll.mtu);
240 ncf->can_id = so->txid;
242 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
243 memset(ncf->data, so->opt.txpad_content, CAN_MAX_DLEN);
250 ncf->data[ae + 1] = so->rxfc.bs;
251 ncf->data[ae + 2] = so->rxfc.stmin;
254 ncf->data[0] = so->opt.ext_address;
256 ncf->flags = so->ll.tx_flags;
266 so->rx.bs = 0;
269 so->lastrxcf_tstamp = ktime_set(0, 0);
272 hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
331 static int check_pad(struct isotp_sock *so, struct canfd_frame *cf,
337 if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) {
338 if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN)
346 if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) &&
351 if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
359 static void isotp_send_cframe(struct isotp_sock *so);
361 static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
363 struct sock *sk = &so->sk;
365 if (so->tx.state != ISOTP_WAIT_FC &&
366 so->tx.state != ISOTP_WAIT_FIRST_FC)
369 hrtimer_cancel(&so->txtimer);
372 ((so->opt.flags & ISOTP_CHECK_PADDING) &&
373 check_pad(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) {
379 so->tx.state = ISOTP_IDLE;
380 wake_up_interruptible(&so->wait);
385 if (so->tx.state == ISOTP_WAIT_FIRST_FC) {
386 so->txfc.bs = cf->data[ae + 1];
387 so->txfc.stmin = cf->data[ae + 2];
390 if (so->txfc.stmin > 0x7F &&
391 (so->txfc.stmin < 0xF1 || so->txfc.stmin > 0xF9))
392 so->txfc.stmin = 0x7F;
394 so->tx_gap = ktime_set(0, 0);
396 so->tx_gap = ktime_add_ns(so->tx_gap, so->frame_txtime);
398 if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
399 so->tx_gap = ktime_add_ns(so->tx_gap,
400 so->force_tx_stmin);
401 else if (so->txfc.stmin < 0x80)
402 so->tx_gap = ktime_add_ns(so->tx_gap,
403 so->txfc.stmin * 1000000);
405 so->tx_gap = ktime_add_ns(so->tx_gap,
406 (so->txfc.stmin - 0xF0)
408 so->tx.state = ISOTP_WAIT_FC;
413 so->tx.bs = 0;
414 so->tx.state = ISOTP_SENDING;
416 hrtimer_start(&so->txtimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
418 isotp_send_cframe(so);
423 hrtimer_start(&so->txtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
436 so->tx.state = ISOTP_IDLE;
437 wake_up_interruptible(&so->wait);
445 struct isotp_sock *so = isotp_sk(sk);
448 hrtimer_cancel(&so->rxtimer);
449 so->rx.state = ISOTP_IDLE;
454 if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
455 check_pad(so, cf, pcilen + len, so->opt.rxpad_content)) {
477 struct isotp_sock *so = isotp_sk(sk);
482 hrtimer_cancel(&so->rxtimer);
483 so->rx.state = ISOTP_IDLE;
486 so->rx.ll_dl = padlen(cf->len);
489 if (cf->len != so->rx.ll_dl)
493 so->rx.len = (cf->data[ae] & 0x0F) << 8;
494 so->rx.len += cf->data[ae + 1];
497 if (so->rx.len) {
501 so->rx.len = cf->data[ae + 2] << 24;
502 so->rx.len += cf->data[ae + 3] << 16;
503 so->rx.len += cf->data[ae + 4] << 8;
504 so->rx.len += cf->data[ae + 5];
509 off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
511 if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl)
515 if (so->rx.len > so->rx.buflen && so->rx.buflen < max_pdu_size) {
519 so->rx.buf = newbuf;
520 so->rx.buflen = max_pdu_size;
524 if (so->rx.len > so->rx.buflen) {
531 so->rx.idx = 0;
532 for (i = ae + ff_pci_sz; i < so->rx.ll_dl; i++)
533 so->rx.buf[so->rx.idx++] = cf->data[i];
536 so->rx.sn = 1;
537 so->rx.state = ISOTP_WAIT_DATA;
540 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
551 struct isotp_sock *so = isotp_sk(sk);
555 if (so->rx.state != ISOTP_WAIT_DATA)
559 if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) {
560 if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) <
561 so->force_rx_stmin)
564 so->lastrxcf_tstamp = skb->tstamp;
567 hrtimer_cancel(&so->rxtimer);
570 if (cf->len > so->rx.ll_dl)
574 if (cf->len < so->rx.ll_dl) {
576 if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ)
580 if ((cf->data[ae] & 0x0F) != so->rx.sn) {
587 so->rx.state = ISOTP_IDLE;
590 so->rx.sn++;
591 so->rx.sn %= 16;
594 so->rx.buf[so->rx.idx++] = cf->data[i];
595 if (so->rx.idx >= so->rx.len)
599 if (so->rx.idx >= so->rx.len) {
601 so->rx.state = ISOTP_IDLE;
603 if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
604 check_pad(so, cf, i + 1, so->opt.rxpad_content)) {
612 nskb = alloc_skb(so->rx.len, gfp_any());
616 memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
617 so->rx.len);
626 if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
628 hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
634 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
637 /* we reached the specified blocksize so->rxfc.bs */
645 struct isotp_sock *so = isotp_sk(sk);
647 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
653 if (skb->len != so->ll.mtu)
659 if (ae && cf->data[0] != so->opt.rx_ext_address)
668 spin_lock(&so->rx_lock);
670 if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
672 if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) ||
673 (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC))
680 isotp_rcv_fc(so, cf, ae);
701 * If so take care of the increased SF PCI size
725 spin_unlock(&so->rx_lock);
728 static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,
732 int space = so->tx.ll_dl - pcilen;
733 int num = min_t(int, so->tx.len - so->tx.idx, space);
736 cf->can_id = so->txid;
740 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
743 memset(cf->data, so->opt.txpad_content, cf->len);
753 cf->data[pcilen + i] = so->tx.buf[so->tx.idx++];
756 cf->data[0] = so->opt.ext_address;
759 static void isotp_send_cframe(struct isotp_sock *so)
761 struct sock *sk = &so->sk;
766 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
768 dev = dev_get_by_index(sock_net(sk), so->ifindex);
772 skb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), GFP_ATOMIC);
783 skb_put_zero(skb, so->ll.mtu);
786 isotp_fill_dataframe(cf, so, ae, 0);
789 cf->data[ae] = N_PCI_CF | so->tx.sn++;
790 so->tx.sn %= 16;
791 so->tx.bs++;
793 cf->flags = so->ll.tx_flags;
799 if (so->cfecho)
800 pr_notice_once("can-isotp: cfecho is %08X != 0\n", so->cfecho);
803 so->cfecho = *(u32 *)cf->data;
816 static void isotp_create_fframe(struct canfd_frame *cf, struct isotp_sock *so,
822 cf->can_id = so->txid;
823 cf->len = so->tx.ll_dl;
825 cf->data[0] = so->opt.ext_address;
828 if (so->tx.len > MAX_12BIT_PDU_SIZE) {
832 cf->data[ae + 2] = (u8)(so->tx.len >> 24) & 0xFFU;
833 cf->data[ae + 3] = (u8)(so->tx.len >> 16) & 0xFFU;
834 cf->data[ae + 4] = (u8)(so->tx.len >> 8) & 0xFFU;
835 cf->data[ae + 5] = (u8)so->tx.len & 0xFFU;
839 cf->data[ae] = (u8)(so->tx.len >> 8) | N_PCI_FF;
840 cf->data[ae + 1] = (u8)so->tx.len & 0xFFU;
845 for (i = ae + ff_pci_sz; i < so->tx.ll_dl; i++)
846 cf->data[i] = so->tx.buf[so->tx.idx++];
848 so->tx.sn = 1;
854 struct isotp_sock *so = isotp_sk(sk);
858 if (skb->sk != sk || so->cfecho != *(u32 *)cf->data)
862 hrtimer_cancel(&so->txtimer);
865 so->cfecho = 0;
867 if (so->tx.idx >= so->tx.len) {
869 so->tx.state = ISOTP_IDLE;
870 wake_up_interruptible(&so->wait);
874 if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
876 so->tx.state = ISOTP_WAIT_FC;
877 hrtimer_start(&so->txtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
883 if (!so->tx_gap) {
885 hrtimer_start(&so->txtimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
887 isotp_send_cframe(so);
892 hrtimer_start(&so->txfrtimer, so->tx_gap, HRTIMER_MODE_REL_SOFT);
897 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
899 struct sock *sk = &so->sk;
902 if (so->tx.state == ISOTP_IDLE || so->tx.state == ISOTP_SHUTDOWN)
913 so->tx.state = ISOTP_IDLE;
914 wake_up_interruptible(&so->wait);
921 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
925 hrtimer_start(&so->txtimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
929 if (so->tx.state == ISOTP_SENDING && !so->cfecho)
930 isotp_send_cframe(so);
938 struct isotp_sock *so = isotp_sk(sk);
942 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
943 int wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0;
948 if (!so->bound || so->tx.state == ISOTP_SHUTDOWN)
951 while (cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SENDING) != ISOTP_IDLE) {
956 if (so->tx.state == ISOTP_SHUTDOWN)
960 err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
966 if (size > so->tx.buflen && so->tx.buflen < max_pdu_size) {
970 so->tx.buf = newbuf;
971 so->tx.buflen = max_pdu_size;
975 if (!size || size > so->tx.buflen) {
981 off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
984 if ((isotp_bc_flags(so) == CAN_ISOTP_SF_BROADCAST) &&
985 (size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off)) {
990 err = memcpy_from_msg(so->tx.buf, msg, size);
994 dev = dev_get_by_index(sock_net(sk), so->ifindex);
1000 skb = sock_alloc_send_skb(sk, so->ll.mtu + sizeof(struct can_skb_priv),
1011 so->tx.len = size;
1012 so->tx.idx = 0;
1015 skb_put_zero(skb, so->ll.mtu);
1018 if (so->cfecho)
1019 pr_notice_once("can-isotp: uninit cfecho %08X\n", so->cfecho);
1022 if (size <= so->tx.ll_dl - SF_PCI_SZ4 - ae - off) {
1035 isotp_fill_dataframe(cf, so, ae, off);
1047 so->cfecho = *(u32 *)cf->data;
1051 isotp_create_fframe(cf, so, ae);
1053 if (isotp_bc_flags(so) == CAN_ISOTP_CF_BROADCAST) {
1055 if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
1056 so->tx_gap = ktime_set(0, so->force_tx_stmin);
1058 so->tx_gap = ktime_set(0, so->frame_txtime);
1061 so->txfc.bs = 0;
1064 so->cfecho = *(u32 *)cf->data;
1067 so->tx.state = ISOTP_WAIT_FIRST_FC;
1073 so->cfecho = 0;
1077 hrtimer_start(&so->txtimer, ktime_set(hrtimer_sec, 0),
1081 cf->flags = so->ll.tx_flags;
1092 hrtimer_cancel(&so->txtimer);
1095 so->cfecho = 0;
1102 err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
1115 so->tx.state = ISOTP_IDLE;
1116 hrtimer_cancel(&so->txfrtimer);
1117 hrtimer_cancel(&so->txtimer);
1120 so->tx.state = ISOTP_IDLE;
1121 wake_up_interruptible(&so->wait);
1131 struct isotp_sock *so = isotp_sk(sk);
1137 if (!so->bound)
1173 struct isotp_sock *so;
1179 so = isotp_sk(sk);
1183 while (wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE) == 0 &&
1184 cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SHUTDOWN) != ISOTP_IDLE)
1188 so->tx.state = ISOTP_SHUTDOWN;
1189 so->rx.state = ISOTP_IDLE;
1192 while (isotp_busy_notifier == so) {
1197 list_del(&so->notifier);
1203 if (so->bound) {
1204 if (so->ifindex) {
1207 dev = dev_get_by_index(net, so->ifindex);
1209 if (isotp_register_rxid(so))
1210 can_rx_unregister(net, dev, so->rxid,
1211 SINGLE_MASK(so->rxid),
1214 can_rx_unregister(net, dev, so->txid,
1215 SINGLE_MASK(so->txid),
1223 hrtimer_cancel(&so->txfrtimer);
1224 hrtimer_cancel(&so->txtimer);
1225 hrtimer_cancel(&so->rxtimer);
1227 so->ifindex = 0;
1228 so->bound = 0;
1230 if (so->rx.buf != so->rx.sbuf)
1231 kfree(so->rx.buf);
1233 if (so->tx.buf != so->tx.sbuf)
1234 kfree(so->tx.buf);
1249 struct isotp_sock *so = isotp_sk(sk);
1275 if (isotp_register_rxid(so)) {
1291 if (so->bound) {
1297 if (isotp_register_rxid(so) && rx_id == tx_id) {
1312 if (dev->mtu < so->ll.mtu) {
1322 if (isotp_register_rxid(so))
1327 so->cfecho = 0;
1336 so->ifindex = ifindex;
1337 so->rxid = rx_id;
1338 so->txid = tx_id;
1339 so->bound = 1;
1357 struct isotp_sock *so = isotp_sk(sk);
1364 addr->can_ifindex = so->ifindex;
1365 addr->can_addr.tp.rx_id = so->rxid;
1366 addr->can_addr.tp.tx_id = so->txid;
1375 struct isotp_sock *so = isotp_sk(sk);
1378 if (so->bound)
1386 if (copy_from_sockptr(&so->opt, optval, optlen))
1390 if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
1391 so->opt.rx_ext_address = so->opt.ext_address;
1394 if (isotp_bc_flags(so) == ISOTP_ALL_BC_FLAGS) {
1396 so->opt.flags &= ~CAN_ISOTP_CF_BROADCAST;
1403 if (so->opt.frame_txtime) {
1404 if (so->opt.frame_txtime == CAN_ISOTP_FRAME_TXTIME_ZERO)
1405 so->frame_txtime = 0;
1407 so->frame_txtime = so->opt.frame_txtime;
1415 if (copy_from_sockptr(&so->rxfc, optval, optlen))
1423 if (copy_from_sockptr(&so->force_tx_stmin, optval, optlen))
1431 if (copy_from_sockptr(&so->force_rx_stmin, optval, optlen))
1453 memcpy(&so->ll, &ll, sizeof(ll));
1456 so->tx.ll_dl = ll.tx_dl;
1489 struct isotp_sock *so = isotp_sk(sk);
1503 val = &so->opt;
1508 val = &so->rxfc;
1513 val = &so->force_tx_stmin;
1518 val = &so->force_rx_stmin;
1523 val = &so->ll;
1537 static void isotp_notify(struct isotp_sock *so, unsigned long msg,
1540 struct sock *sk = &so->sk;
1545 if (so->ifindex != dev->ifindex)
1552 if (so->bound) {
1553 if (isotp_register_rxid(so))
1554 can_rx_unregister(dev_net(dev), dev, so->rxid,
1555 SINGLE_MASK(so->rxid),
1558 can_rx_unregister(dev_net(dev), dev, so->txid,
1559 SINGLE_MASK(so->txid),
1563 so->ifindex = 0;
1564 so->bound = 0;
1605 struct isotp_sock *so = isotp_sk(sk);
1607 so->ifindex = 0;
1608 so->bound = 0;
1610 so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
1611 so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1612 so->opt.rx_ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1613 so->opt.rxpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
1614 so->opt.txpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
1615 so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1616 so->frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1617 so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
1618 so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
1619 so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;
1620 so->ll.mtu = CAN_ISOTP_DEFAULT_LL_MTU;
1621 so->ll.tx_dl = CAN_ISOTP_DEFAULT_LL_TX_DL;
1622 so->ll.tx_flags = CAN_ISOTP_DEFAULT_LL_TX_FLAGS;
1625 so->tx.ll_dl = so->ll.tx_dl;
1627 so->rx.state = ISOTP_IDLE;
1628 so->tx.state = ISOTP_IDLE;
1630 so->rx.buf = so->rx.sbuf;
1631 so->tx.buf = so->tx.sbuf;
1632 so->rx.buflen = ARRAY_SIZE(so->rx.sbuf);
1633 so->tx.buflen = ARRAY_SIZE(so->tx.sbuf);
1635 hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1636 so->rxtimer.function = isotp_rx_timer_handler;
1637 hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1638 so->txtimer.function = isotp_tx_timer_handler;
1639 hrtimer_init(&so->txfrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1640 so->txfrtimer.function = isotp_txfr_timer_handler;
1642 init_waitqueue_head(&so->wait);
1643 spin_lock_init(&so->rx_lock);
1646 list_add_tail(&so->notifier, &isotp_notifier_list);
1655 struct isotp_sock *so = isotp_sk(sk);
1658 poll_wait(file, &so->wait, wait);
1661 if ((mask & EPOLLWRNORM) && (so->tx.state != ISOTP_IDLE))