can: isotp: set default value for N_As to 50 micro seconds
[platform/kernel/linux-rpi.git] / net / can / isotp.c
index df6968b..5bce7c6 100644 (file)
@@ -56,6 +56,7 @@
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/interrupt.h>
+#include <linux/spinlock.h>
 #include <linux/hrtimer.h>
 #include <linux/wait.h>
 #include <linux/uio.h>
@@ -119,8 +120,8 @@ enum {
 };
 
 struct tpcon {
-       int idx;
-       int len;
+       unsigned int idx;
+       unsigned int len;
        u32 state;
        u8 bs;
        u8 sn;
@@ -140,11 +141,13 @@ struct isotp_sock {
        struct can_isotp_options opt;
        struct can_isotp_fc_options rxfc, txfc;
        struct can_isotp_ll_options ll;
+       u32 frame_txtime;
        u32 force_tx_stmin;
        u32 force_rx_stmin;
        struct tpcon rx, tx;
        struct list_head notifier;
        wait_queue_head_t wait;
+       spinlock_t rx_lock; /* protect single thread state machine */
 };
 
 static LIST_HEAD(isotp_notifier_list);
@@ -358,7 +361,7 @@ static int isotp_rcv_fc(struct isotp_sock *so, struct canfd_frame *cf, int ae)
 
                so->tx_gap = ktime_set(0, 0);
                /* add transmission time for CAN frame N_As */
-               so->tx_gap = ktime_add_ns(so->tx_gap, so->opt.frame_txtime);
+               so->tx_gap = ktime_add_ns(so->tx_gap, so->frame_txtime);
                /* add waiting time for consecutive frames N_Cs */
                if (so->opt.flags & CAN_ISOTP_FORCE_TXSTMIN)
                        so->tx_gap = ktime_add_ns(so->tx_gap,
@@ -615,11 +618,17 @@ static void isotp_rcv(struct sk_buff *skb, void *data)
 
        n_pci_type = cf->data[ae] & 0xF0;
 
+       /* Make sure the state changes and data structures stay consistent at
+        * CAN frame reception time. This locking is not needed in real world
+        * use cases but the inconsistency can be triggered with syzkaller.
+        */
+       spin_lock(&so->rx_lock);
+
        if (so->opt.flags & CAN_ISOTP_HALF_DUPLEX) {
                /* check rx/tx path half duplex expectations */
                if ((so->tx.state != ISOTP_IDLE && n_pci_type != N_PCI_FC) ||
                    (so->rx.state != ISOTP_IDLE && n_pci_type == N_PCI_FC))
-                       return;
+                       goto out_unlock;
        }
 
        switch (n_pci_type) {
@@ -668,6 +677,9 @@ static void isotp_rcv(struct sk_buff *skb, void *data)
                isotp_rcv_cf(sk, cf, ae, skb);
                break;
        }
+
+out_unlock:
+       spin_unlock(&so->rx_lock);
 }
 
 static void isotp_fill_dataframe(struct canfd_frame *cf, struct isotp_sock *so,
@@ -876,7 +888,7 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 
        if (!size || size > MAX_MSG_LENGTH) {
                err = -EINVAL;
-               goto err_out;
+               goto err_out_drop;
        }
 
        /* take care of a potential SF_DL ESC offset for TX_DL > 8 */
@@ -886,24 +898,24 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
        if ((so->opt.flags & CAN_ISOTP_SF_BROADCAST) &&
            (size > so->tx.ll_dl - SF_PCI_SZ4 - ae - off)) {
                err = -EINVAL;
-               goto err_out;
+               goto err_out_drop;
        }
 
        err = memcpy_from_msg(so->tx.buf, msg, size);
        if (err < 0)
-               goto err_out;
+               goto err_out_drop;
 
        dev = dev_get_by_index(sock_net(sk), so->ifindex);
        if (!dev) {
                err = -ENXIO;
-               goto err_out;
+               goto err_out_drop;
        }
 
        skb = sock_alloc_send_skb(sk, so->ll.mtu + sizeof(struct can_skb_priv),
                                  msg->msg_flags & MSG_DONTWAIT, &err);
        if (!skb) {
                dev_put(dev);
-               goto err_out;
+               goto err_out_drop;
        }
 
        can_skb_reserve(skb);
@@ -965,7 +977,7 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
        if (err) {
                pr_notice_once("can-isotp: %s: can_send_ret %pe\n",
                               __func__, ERR_PTR(err));
-               goto err_out;
+               goto err_out_drop;
        }
 
        if (wait_tx_done) {
@@ -978,6 +990,9 @@ static int isotp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 
        return size;
 
+err_out_drop:
+       /* drop this PDU and unlock a potential wait queue */
+       old_state = ISOTP_IDLE;
 err_out:
        so->tx.state = old_state;
        if (so->tx.state == ISOTP_IDLE)
@@ -991,26 +1006,29 @@ static int isotp_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
 {
        struct sock *sk = sock->sk;
        struct sk_buff *skb;
-       int err = 0;
-       int noblock;
+       struct isotp_sock *so = isotp_sk(sk);
+       int noblock = flags & MSG_DONTWAIT;
+       int ret = 0;
 
-       noblock = flags & MSG_DONTWAIT;
-       flags &= ~MSG_DONTWAIT;
+       if (flags & ~(MSG_DONTWAIT | MSG_TRUNC | MSG_PEEK))
+               return -EINVAL;
 
-       skb = skb_recv_datagram(sk, flags, noblock, &err);
+       if (!so->bound)
+               return -EADDRNOTAVAIL;
+
+       flags &= ~MSG_DONTWAIT;
+       skb = skb_recv_datagram(sk, flags, noblock, &ret);
        if (!skb)
-               return err;
+               return ret;
 
        if (size < skb->len)
                msg->msg_flags |= MSG_TRUNC;
        else
                size = skb->len;
 
-       err = memcpy_to_msg(msg, skb->data, size);
-       if (err < 0) {
-               skb_free_datagram(sk, skb);
-               return err;
-       }
+       ret = memcpy_to_msg(msg, skb->data, size);
+       if (ret < 0)
+               goto out_err;
 
        sock_recv_timestamp(msg, sk, skb);
 
@@ -1020,9 +1038,13 @@ static int isotp_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
                memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
        }
 
+       /* set length of return value */
+       ret = (flags & MSG_TRUNC) ? skb->len : size;
+
+out_err:
        skb_free_datagram(sk, skb);
 
-       return size;
+       return ret;
 }
 
 static int isotp_release(struct socket *sock)
@@ -1090,6 +1112,7 @@ static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
        struct net *net = sock_net(sk);
        int ifindex;
        struct net_device *dev;
+       canid_t tx_id, rx_id;
        int err = 0;
        int notify_enetdown = 0;
        int do_rx_reg = 1;
@@ -1097,8 +1120,18 @@ static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
        if (len < ISOTP_MIN_NAMELEN)
                return -EINVAL;
 
-       if (addr->can_addr.tp.tx_id & (CAN_ERR_FLAG | CAN_RTR_FLAG))
-               return -EADDRNOTAVAIL;
+       /* sanitize tx/rx CAN identifiers */
+       tx_id = addr->can_addr.tp.tx_id;
+       if (tx_id & CAN_EFF_FLAG)
+               tx_id &= (CAN_EFF_FLAG | CAN_EFF_MASK);
+       else
+               tx_id &= CAN_SFF_MASK;
+
+       rx_id = addr->can_addr.tp.rx_id;
+       if (rx_id & CAN_EFF_FLAG)
+               rx_id &= (CAN_EFF_FLAG | CAN_EFF_MASK);
+       else
+               rx_id &= CAN_SFF_MASK;
 
        if (!addr->can_ifindex)
                return -ENODEV;
@@ -1110,21 +1143,13 @@ static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
                do_rx_reg = 0;
 
        /* do not validate rx address for functional addressing */
-       if (do_rx_reg) {
-               if (addr->can_addr.tp.rx_id == addr->can_addr.tp.tx_id) {
-                       err = -EADDRNOTAVAIL;
-                       goto out;
-               }
-
-               if (addr->can_addr.tp.rx_id & (CAN_ERR_FLAG | CAN_RTR_FLAG)) {
-                       err = -EADDRNOTAVAIL;
-                       goto out;
-               }
+       if (do_rx_reg && rx_id == tx_id) {
+               err = -EADDRNOTAVAIL;
+               goto out;
        }
 
        if (so->bound && addr->can_ifindex == so->ifindex &&
-           addr->can_addr.tp.rx_id == so->rxid &&
-           addr->can_addr.tp.tx_id == so->txid)
+           rx_id == so->rxid && tx_id == so->txid)
                goto out;
 
        dev = dev_get_by_index(net, addr->can_ifindex);
@@ -1148,8 +1173,7 @@ static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
        ifindex = dev->ifindex;
 
        if (do_rx_reg)
-               can_rx_register(net, dev, addr->can_addr.tp.rx_id,
-                               SINGLE_MASK(addr->can_addr.tp.rx_id),
+               can_rx_register(net, dev, rx_id, SINGLE_MASK(rx_id),
                                isotp_rcv, sk, "isotp", sk);
 
        dev_put(dev);
@@ -1169,8 +1193,8 @@ static int isotp_bind(struct socket *sock, struct sockaddr *uaddr, int len)
 
        /* switch to new settings */
        so->ifindex = ifindex;
-       so->rxid = addr->can_addr.tp.rx_id;
-       so->txid = addr->can_addr.tp.tx_id;
+       so->rxid = rx_id;
+       so->txid = tx_id;
        so->bound = 1;
 
 out:
@@ -1224,6 +1248,14 @@ static int isotp_setsockopt_locked(struct socket *sock, int level, int optname,
                /* no separate rx_ext_address is given => use ext_address */
                if (!(so->opt.flags & CAN_ISOTP_RX_EXT_ADDR))
                        so->opt.rx_ext_address = so->opt.ext_address;
+
+               /* check for frame_txtime changes (0 => no changes) */
+               if (so->opt.frame_txtime) {
+                       if (so->opt.frame_txtime == CAN_ISOTP_FRAME_TXTIME_ZERO)
+                               so->frame_txtime = 0;
+                       else
+                               so->frame_txtime = so->opt.frame_txtime;
+               }
                break;
 
        case CAN_ISOTP_RECV_FC:
@@ -1425,6 +1457,7 @@ static int isotp_init(struct sock *sk)
        so->opt.rxpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
        so->opt.txpad_content = CAN_ISOTP_DEFAULT_PAD_CONTENT;
        so->opt.frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
+       so->frame_txtime = CAN_ISOTP_DEFAULT_FRAME_TXTIME;
        so->rxfc.bs = CAN_ISOTP_DEFAULT_RECV_BS;
        so->rxfc.stmin = CAN_ISOTP_DEFAULT_RECV_STMIN;
        so->rxfc.wftmax = CAN_ISOTP_DEFAULT_RECV_WFTMAX;
@@ -1444,6 +1477,7 @@ static int isotp_init(struct sock *sk)
        so->txtimer.function = isotp_tx_timer_handler;
 
        init_waitqueue_head(&so->wait);
+       spin_lock_init(&so->rx_lock);
 
        spin_lock(&isotp_notifier_lock);
        list_add_tail(&so->notifier, &isotp_notifier_list);