Lines Matching refs:so
167 static u32 isotp_bc_flags(struct isotp_sock *so)
169 return so->opt.flags & ISOTP_ALL_BC_FLAGS;
172 static bool isotp_register_rxid(struct isotp_sock *so)
175 return (isotp_bc_flags(so) == 0);
180 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
182 struct sock *sk = &so->sk;
184 if (so->rx.state == ISOTP_WAIT_DATA) {
193 so->rx.state = ISOTP_IDLE;
204 struct isotp_sock *so = isotp_sk(sk);
207 nskb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), gfp_any());
211 dev = dev_get_by_index(sock_net(sk), so->ifindex);
224 skb_put_zero(nskb, so->ll.mtu);
227 ncf->can_id = so->txid;
229 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
230 memset(ncf->data, so->opt.txpad_content, CAN_MAX_DLEN);
237 ncf->data[ae + 1] = so->rxfc.bs;
238 ncf->data[ae + 2] = so->rxfc.stmin;
241 ncf->data[0] = so->opt.ext_address;
243 ncf->flags = so->ll.tx_flags;
253 so->rx.bs = 0;
256 so->lastrxcf_tstamp = ktime_set(0, 0);
259 hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
318 static int check_pad(struct isotp_sock *so, struct canfd_frame *cf,
324 if (!(so->opt.flags & CAN_ISOTP_RX_PADDING)) {
325 if (so->opt.flags & CAN_ISOTP_CHK_PAD_LEN)
333 if ((so->opt.flags & CAN_ISOTP_CHK_PAD_LEN) &&
338 if (so->opt.flags & CAN_ISOTP_CHK_PAD_DATA) {
346 static void isotp_send_cframe(struct isotp_sock *so);
348 static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
350 struct sock *sk = &so->sk;
352 if (so->tx.state != ISOTP_WAIT_FC &&
353 so->tx.state != ISOTP_WAIT_FIRST_FC)
356 hrtimer_cancel(&so->txtimer);
359 ((so->opt.flags & ISOTP_CHECK_PADDING) &&
360 check_pad(so, cf, ae + FC_CONTENT_SZ, so->opt.rxpad_content))) {
366 so->tx.state = ISOTP_IDLE;
367 wake_up_interruptible(&so->wait);
372 if (so->tx.state == ISOTP_WAIT_FIRST_FC) {
373 so->txfc.bs = cf->data[ae + 1];
374 so->txfc.stmin = cf->data[ae + 2];
377 if (so->txfc.stmin > 0x7F &&
378 (so->txfc.stmin < 0xF1 || so->txfc.stmin > 0xF9))
379 so->txfc.stmin = 0x7F;
381 so->tx_gap = ktime_set(0, 0);
383 so->tx_gap = ktime_add_ns(so->tx_gap, so->frame_txtime);
385 if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
386 so->tx_gap = ktime_add_ns(so->tx_gap,
387 so->force_tx_stmin);
388 else if (so->txfc.stmin < 0x80)
389 so->tx_gap = ktime_add_ns(so->tx_gap,
390 so->txfc.stmin * 1000000);
392 so->tx_gap = ktime_add_ns(so->tx_gap,
393 (so->txfc.stmin - 0xF0)
395 so->tx.state = ISOTP_WAIT_FC;
400 so->tx.bs = 0;
401 so->tx.state = ISOTP_SENDING;
403 hrtimer_start(&so->txtimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
405 isotp_send_cframe(so);
410 hrtimer_start(&so->txtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
423 so->tx.state = ISOTP_IDLE;
424 wake_up_interruptible(&so->wait);
432 struct isotp_sock *so = isotp_sk(sk);
435 hrtimer_cancel(&so->rxtimer);
436 so->rx.state = ISOTP_IDLE;
441 if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
442 check_pad(so, cf, pcilen + len, so->opt.rxpad_content)) {
464 struct isotp_sock *so = isotp_sk(sk);
469 hrtimer_cancel(&so->rxtimer);
470 so->rx.state = ISOTP_IDLE;
473 so->rx.ll_dl = padlen(cf->len);
476 if (cf->len != so->rx.ll_dl)
480 so->rx.len = (cf->data[ae] & 0x0F) << 8;
481 so->rx.len += cf->data[ae + 1];
484 if (so->rx.len) {
488 so->rx.len = cf->data[ae + 2] << 24;
489 so->rx.len += cf->data[ae + 3] << 16;
490 so->rx.len += cf->data[ae + 4] << 8;
491 so->rx.len += cf->data[ae + 5];
496 off = (so->rx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
498 if (so->rx.len + ae + off + ff_pci_sz < so->rx.ll_dl)
501 if (so->rx.len > MAX_MSG_LENGTH) {
508 so->rx.idx = 0;
509 for (i = ae + ff_pci_sz; i < so->rx.ll_dl; i++)
510 so->rx.buf[so->rx.idx++] = cf->data[i];
513 so->rx.sn = 1;
514 so->rx.state = ISOTP_WAIT_DATA;
517 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
528 struct isotp_sock *so = isotp_sk(sk);
532 if (so->rx.state != ISOTP_WAIT_DATA)
536 if (so->opt.flags & CAN_ISOTP_FORCE_RXSTMIN) {
537 if (ktime_to_ns(ktime_sub(skb->tstamp, so->lastrxcf_tstamp)) <
538 so->force_rx_stmin)
541 so->lastrxcf_tstamp = skb->tstamp;
544 hrtimer_cancel(&so->rxtimer);
547 if (cf->len > so->rx.ll_dl)
551 if (cf->len < so->rx.ll_dl) {
553 if (so->rx.len - so->rx.idx > so->rx.ll_dl - ae - N_PCI_SZ)
557 if ((cf->data[ae] & 0x0F) != so->rx.sn) {
564 so->rx.state = ISOTP_IDLE;
567 so->rx.sn++;
568 so->rx.sn %= 16;
571 so->rx.buf[so->rx.idx++] = cf->data[i];
572 if (so->rx.idx >= so->rx.len)
576 if (so->rx.idx >= so->rx.len) {
578 so->rx.state = ISOTP_IDLE;
580 if ((so->opt.flags & ISOTP_CHECK_PADDING) &&
581 check_pad(so, cf, i + 1, so->opt.rxpad_content)) {
589 nskb = alloc_skb(so->rx.len, gfp_any());
593 memcpy(skb_put(nskb, so->rx.len), so->rx.buf,
594 so->rx.len);
603 if (!so->rxfc.bs || ++so->rx.bs < so->rxfc.bs) {
605 hrtimer_start(&so->rxtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
611 if (so->opt.flags & CAN_ISOTP_LISTEN_MODE)
614 /* we reached the specified blocksize so->rxfc.bs */
622 struct isotp_sock *so = isotp_sk(sk);
624 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
630 if (skb->len != so->ll.mtu)
636 if (ae && cf->data[0] != so->opt.rx_ext_address)
645 spin_lock(&so->rx_lock);
647 if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
649 if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) ||
650 (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC))
657 isotp_rcv_fc(so, cf, ae);
678 * If so take care of the increased SF PCI size
702 spin_unlock(&so->rx_lock);
705 static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,
709 int space = so->tx.ll_dl - pcilen;
710 int num = min_t(int, so->tx.len - so->tx.idx, space);
713 cf->can_id = so->txid;
717 if (so->opt.flags & CAN_ISOTP_TX_PADDING) {
720 memset(cf->data, so->opt.txpad_content, cf->len);
730 cf->data[pcilen + i] = so->tx.buf[so->tx.idx++];
733 cf->data[0] = so->opt.ext_address;
736 static void isotp_send_cframe(struct isotp_sock *so)
738 struct sock *sk = &so->sk;
743 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
745 dev = dev_get_by_index(sock_net(sk), so->ifindex);
749 skb = alloc_skb(so->ll.mtu + sizeof(struct can_skb_priv), GFP_ATOMIC);
760 skb_put_zero(skb, so->ll.mtu);
763 isotp_fill_dataframe(cf, so, ae, 0);
766 cf->data[ae] = N_PCI_CF | so->tx.sn++;
767 so->tx.sn %= 16;
768 so->tx.bs++;
770 cf->flags = so->ll.tx_flags;
776 if (so->cfecho)
777 pr_notice_once("can-isotp: cfecho is %08X != 0\n", so->cfecho);
780 so->cfecho = *(u32 *)cf->data;
793 static void isotp_create_fframe(struct canfd_frame *cf, struct isotp_sock *so,
799 cf->can_id = so->txid;
800 cf->len = so->tx.ll_dl;
802 cf->data[0] = so->opt.ext_address;
805 if (so->tx.len > 4095) {
809 cf->data[ae + 2] = (u8)(so->tx.len >> 24) & 0xFFU;
810 cf->data[ae + 3] = (u8)(so->tx.len >> 16) & 0xFFU;
811 cf->data[ae + 4] = (u8)(so->tx.len >> 8) & 0xFFU;
812 cf->data[ae + 5] = (u8)so->tx.len & 0xFFU;
816 cf->data[ae] = (u8)(so->tx.len >> 8) | N_PCI_FF;
817 cf->data[ae + 1] = (u8)so->tx.len & 0xFFU;
822 for (i = ae + ff_pci_sz; i < so->tx.ll_dl; i++)
823 cf->data[i] = so->tx.buf[so->tx.idx++];
825 so->tx.sn = 1;
831 struct isotp_sock *so = isotp_sk(sk);
835 if (skb->sk != sk || so->cfecho != *(u32 *)cf->data)
839 hrtimer_cancel(&so->txtimer);
842 so->cfecho = 0;
844 if (so->tx.idx >= so->tx.len) {
846 so->tx.state = ISOTP_IDLE;
847 wake_up_interruptible(&so->wait);
851 if (so->txfc.bs && so->tx.bs >= so->txfc.bs) {
853 so->tx.state = ISOTP_WAIT_FC;
854 hrtimer_start(&so->txtimer, ktime_set(ISOTP_FC_TIMEOUT, 0),
860 if (!so->tx_gap) {
862 hrtimer_start(&so->txtimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
864 isotp_send_cframe(so);
869 hrtimer_start(&so->txfrtimer, so->tx_gap, HRTIMER_MODE_REL_SOFT);
874 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
876 struct sock *sk = &so->sk;
879 if (so->tx.state == ISOTP_IDLE || so->tx.state == ISOTP_SHUTDOWN)
890 so->tx.state = ISOTP_IDLE;
891 wake_up_interruptible(&so->wait);
898 struct isotp_sock *so = container_of(hrtimer, struct isotp_sock,
902 hrtimer_start(&so->txtimer, ktime_set(ISOTP_ECHO_TIMEOUT, 0),
906 if (so->tx.state == ISOTP_SENDING && !so->cfecho)
907 isotp_send_cframe(so);
915 struct isotp_sock *so = isotp_sk(sk);
919 int ae = (so->opt.flags & CAN_ISOTP_EXTEND_ADDR) ? 1 : 0;
920 int wait_tx_done = (so->opt.flags & CAN_ISOTP_WAIT_TX_DONE) ? 1 : 0;
925 if (!so->bound || so->tx.state == ISOTP_SHUTDOWN)
928 while (cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SENDING) != ISOTP_IDLE) {
933 if (so->tx.state == ISOTP_SHUTDOWN)
937 err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
948 off = (so->tx.ll_dl > CAN_MAX_DLEN) ? 1 : 0;
951 if ((isotp_bc_flags(so) == CAN_ISOTP_SF_BROADCAST) &&
952 (size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off)) {
957 err = memcpy_from_msg(so->tx.buf, msg, size);
961 dev = dev_get_by_index(sock_net(sk), so->ifindex);
967 skb = sock_alloc_send_skb(sk, so->ll.mtu + sizeof(struct can_skb_priv),
978 so->tx.len = size;
979 so->tx.idx = 0;
982 skb_put_zero(skb, so->ll.mtu);
985 if (so->cfecho)
986 pr_notice_once("can-isotp: uninit cfecho %08X\n", so->cfecho);
989 if (size <= so->tx.ll_dl - SF_PCI_SZ4 - ae - off) {
1002 isotp_fill_dataframe(cf, so, ae, off);
1014 so->cfecho = *(u32 *)cf->data;
1018 isotp_create_fframe(cf, so, ae);
1020 if (isotp_bc_flags(so) == CAN_ISOTP_CF_BROADCAST) {
1022 if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
1023 so->tx_gap = ktime_set(0, so->force_tx_stmin);
1025 so->tx_gap = ktime_set(0, so->frame_txtime);
1028 so->txfc.bs = 0;
1031 so->cfecho = *(u32 *)cf->data;
1034 so->tx.state = ISOTP_WAIT_FIRST_FC;
1040 so->cfecho = 0;
1044 hrtimer_start(&so->txtimer, ktime_set(hrtimer_sec, 0),
1048 cf->flags = so->ll.tx_flags;
1059 hrtimer_cancel(&so->txtimer);
1062 so->cfecho = 0;
1069 err = wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE);
1082 so->tx.state = ISOTP_IDLE;
1083 hrtimer_cancel(&so->txfrtimer);
1084 hrtimer_cancel(&so->txtimer);
1087 so->tx.state = ISOTP_IDLE;
1088 wake_up_interruptible(&so->wait);
1098 struct isotp_sock *so = isotp_sk(sk);
1105 if (!so->bound)
1142 struct isotp_sock *so;
1148 so = isotp_sk(sk);
1152 while (wait_event_interruptible(so->wait, so->tx.state == ISOTP_IDLE) == 0 &&
1153 cmpxchg(&so->tx.state, ISOTP_IDLE, ISOTP_SHUTDOWN) != ISOTP_IDLE)
1157 so->tx.state = ISOTP_SHUTDOWN;
1158 so->rx.state = ISOTP_IDLE;
1161 while (isotp_busy_notifier == so) {
1166 list_del(&so->notifier);
1172 if (so->bound) {
1173 if (so->ifindex) {
1176 dev = dev_get_by_index(net, so->ifindex);
1178 if (isotp_register_rxid(so))
1179 can_rx_unregister(net, dev, so->rxid,
1180 SINGLE_MASK(so->rxid),
1183 can_rx_unregister(net, dev, so->txid,
1184 SINGLE_MASK(so->txid),
1192 hrtimer_cancel(&so->txfrtimer);
1193 hrtimer_cancel(&so->txtimer);
1194 hrtimer_cancel(&so->rxtimer);
1196 so->ifindex = 0;
1197 so->bound = 0;
1212 struct isotp_sock *so = isotp_sk(sk);
1238 if (isotp_register_rxid(so)) {
1254 if (so->bound) {
1260 if (isotp_register_rxid(so) && rx_id == tx_id) {
1275 if (dev->mtu < so->ll.mtu) {
1285 if (isotp_register_rxid(so))
1290 so->cfecho = 0;
1299 so->ifindex = ifindex;
1300 so->rxid = rx_id;
1301 so->txid = tx_id;
1302 so->bound = 1;
1320 struct isotp_sock *so = isotp_sk(sk);
1327 addr->can_ifindex = so->ifindex;
1328 addr->can_addr.tp.rx_id = so->rxid;
1329 addr->can_addr.tp.tx_id = so->txid;
1338 struct isotp_sock *so = isotp_sk(sk);
1341 if (so->bound)
1349 if (copy_from_sockptr(&so->opt, optval, optlen))
1353 if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
1354 so->opt.rx_ext_address = so->opt.ext_address;
1357 if (isotp_bc_flags(so) == ISOTP_ALL_BC_FLAGS) {
1359 so->opt.flags &= ~CAN_ISOTP_CF_BROADCAST;
1366 if (so->opt.frame_txtime) {
1367 if (so->opt.frame_txtime == CAN_ISOTP_FRAME_TXTIME_ZERO)
1368 so->frame_txtime = 0;
1370 so->frame_txtime = so->opt.frame_txtime;
1378 if (copy_from_sockptr(&so->rxfc, optval, optlen))
1386 if (copy_from_sockptr(&so->force_tx_stmin, optval, optlen))
1394 if (copy_from_sockptr(&so->force_rx_stmin, optval, optlen))
1416 memcpy(&so->ll, &ll, sizeof(ll));
1419 so->tx.ll_dl = ll.tx_dl;
1452 struct isotp_sock *so = isotp_sk(sk);
1466 val = &so->opt;
1471 val = &so->rxfc;
1476 val = &so->force_tx_stmin;
1481 val = &so->force_rx_stmin;
1486 val = &so->ll;
1500 static void isotp_notify(struct isotp_sock *so, unsigned long msg,
1503 struct sock *sk = &so->sk;
1508 if (so->ifindex != dev->ifindex)
1515 if (so->bound) {
1516 if (isotp_register_rxid(so))
1517 can_rx_unregister(dev_net(dev), dev, so->rxid,
1518 SINGLE_MASK(so->rxid),
1521 can_rx_unregister(dev_net(dev), dev, so->txid,
1522 SINGLE_MASK(so->txid),
1526 so->ifindex = 0;
1527 so->bound = 0;
1568 struct isotp_sock *so = isotp_sk(sk);
1570 so->ifindex = 0;
1571 so->bound = 0;
1573 so->opt.flags = CAN_ISOTP_DEFAULT_FLAGS;
1574 so->opt.ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1575 so->opt.rx_ext_address = CAN_ISOTP_DEFAULT_EXT_ADDRESS;
1576 so->opt.rxpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
1577 so->opt.txpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
1578 so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1579 so->frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
1580 so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
1581 so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
1582 so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;
1583 so->ll.mtu = CAN_ISOTP_DEFAULT_LL_MTU;
1584 so->ll.tx_dl = CAN_ISOTP_DEFAULT_LL_TX_DL;
1585 so->ll.tx_flags = CAN_ISOTP_DEFAULT_LL_TX_FLAGS;
1588 so->tx.ll_dl = so->ll.tx_dl;
1590 so->rx.state = ISOTP_IDLE;
1591 so->tx.state = ISOTP_IDLE;
1593 hrtimer_init(&so->rxtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1594 so->rxtimer.function = isotp_rx_timer_handler;
1595 hrtimer_init(&so->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1596 so->txtimer.function = isotp_tx_timer_handler;
1597 hrtimer_init(&so->txfrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
1598 so->txfrtimer.function = isotp_txfr_timer_handler;
1600 init_waitqueue_head(&so->wait);
1601 spin_lock_init(&so->rx_lock);
1604 list_add_tail(&so->notifier, &isotp_notifier_list);
1613 struct isotp_sock *so = isotp_sk(sk);
1616 poll_wait(file, &so->wait, wait);
1619 if ((mask & EPOLLWRNORM) && (so->tx.state != ISOTP_IDLE))