1 // SPDX-License-Identifier: GPL-2.0 2 #include <asm/ioctls.h> 3 #include <linux/io_uring/net.h> 4 #include <linux/errqueue.h> 5 #include <net/sock.h> 6 7 #include "uring_cmd.h" 8 #include "io_uring.h" 9 10 static int io_uring_cmd_get_sock_ioctl(struct socket *sock, int op) 11 { 12 struct sock *sk = sock->sk; 13 struct proto *prot = READ_ONCE(sk->sk_prot); 14 int ret, arg = 0; 15 16 if (!prot || !prot->ioctl) 17 return -EOPNOTSUPP; 18 19 ret = prot->ioctl(sk, op, &arg); 20 if (ret) 21 return ret; 22 return arg; 23 } 24 25 static inline int io_uring_cmd_getsockopt(struct socket *sock, 26 struct io_uring_cmd *cmd, 27 unsigned int issue_flags) 28 { 29 const struct io_uring_sqe *sqe = cmd->sqe; 30 bool compat = !!(issue_flags & IO_URING_F_COMPAT); 31 int optlen, optname, level, err; 32 void __user *optval; 33 34 level = READ_ONCE(sqe->level); 35 if (level != SOL_SOCKET) 36 return -EOPNOTSUPP; 37 38 optval = u64_to_user_ptr(READ_ONCE(sqe->optval)); 39 optname = READ_ONCE(sqe->optname); 40 optlen = READ_ONCE(sqe->optlen); 41 42 err = do_sock_getsockopt(sock, compat, level, optname, 43 USER_SOCKPTR(optval), 44 KERNEL_SOCKPTR(&optlen)); 45 if (err) 46 return err; 47 48 /* On success, return optlen */ 49 return optlen; 50 } 51 52 static inline int io_uring_cmd_setsockopt(struct socket *sock, 53 struct io_uring_cmd *cmd, 54 unsigned int issue_flags) 55 { 56 const struct io_uring_sqe *sqe = cmd->sqe; 57 bool compat = !!(issue_flags & IO_URING_F_COMPAT); 58 int optname, optlen, level; 59 void __user *optval; 60 sockptr_t optval_s; 61 62 optval = u64_to_user_ptr(READ_ONCE(sqe->optval)); 63 optname = READ_ONCE(sqe->optname); 64 optlen = READ_ONCE(sqe->optlen); 65 level = READ_ONCE(sqe->level); 66 optval_s = USER_SOCKPTR(optval); 67 68 return do_sock_setsockopt(sock, compat, level, optname, optval_s, 69 optlen); 70 } 71 72 static int io_process_timestamp_skb(struct io_uring_cmd *cmd, struct sock *sk, 73 struct sk_buff *skb, unsigned int issue_flags) 74 { 75 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 76 struct io_uring_cqe cqe[2]; 77 struct io_timespec *iots; 78 struct timespec64 ts; 79 u32 tstype, tskey; 80 int ret; 81 82 BUILD_BUG_ON(sizeof(struct io_uring_cqe) != sizeof(struct io_timespec)); 83 84 ret = skb_get_tx_timestamp(skb, sk, &ts); 85 if (ret < 0) 86 return ret; 87 88 tskey = serr->ee.ee_data; 89 tstype = serr->ee.ee_info; 90 91 cqe->user_data = 0; 92 cqe->res = tskey; 93 cqe->flags = IORING_CQE_F_MORE | ctx_cqe32_flags(cmd_to_io_kiocb(cmd)->ctx); 94 cqe->flags |= tstype << IORING_TIMESTAMP_TYPE_SHIFT; 95 if (ret == SOF_TIMESTAMPING_TX_HARDWARE) 96 cqe->flags |= IORING_CQE_F_TSTAMP_HW; 97 98 iots = (struct io_timespec *)&cqe[1]; 99 iots->tv_sec = ts.tv_sec; 100 iots->tv_nsec = ts.tv_nsec; 101 if (!io_uring_cmd_post_mshot_cqe32(cmd, issue_flags, cqe)) 102 return -ENOBUFS; 103 return 0; 104 } 105 106 static int io_uring_cmd_timestamp(struct socket *sock, 107 struct io_uring_cmd *cmd, 108 unsigned int issue_flags) 109 { 110 struct sock *sk = sock->sk; 111 struct sk_buff_head *q = &sk->sk_error_queue; 112 struct sk_buff *skb, *tmp; 113 struct sk_buff_head list; 114 int ret; 115 116 if (!(issue_flags & IO_URING_F_CQE32)) 117 return -EINVAL; 118 ret = io_cmd_poll_multishot(cmd, issue_flags, EPOLLERR); 119 if (unlikely(ret)) 120 return ret; 121 122 if (skb_queue_empty_lockless(q)) 123 return -EAGAIN; 124 __skb_queue_head_init(&list); 125 126 scoped_guard(spinlock_irq, &q->lock) { 127 skb_queue_walk_safe(q, skb, tmp) { 128 /* don't support skbs with payload */ 129 if (!skb_has_tx_timestamp(skb, sk) || skb->len) 130 continue; 131 __skb_unlink(skb, q); 132 __skb_queue_tail(&list, skb); 133 } 134 } 135 136 while (1) { 137 skb = skb_peek(&list); 138 if (!skb) 139 break; 140 ret = io_process_timestamp_skb(cmd, sk, skb, issue_flags); 141 if (ret) 142 break; 143 __skb_dequeue(&list); 144 consume_skb(skb); 145 } 146 147 if (!unlikely(skb_queue_empty(&list))) { 148 scoped_guard(spinlock_irqsave, &q->lock) 149 skb_queue_splice(&list, q); 150 } 151 /* 152 * Aux CQEs cannot overflow and the poll is edge triggered, so nothing 153 * re-runs the command once the CQ drains. End the multishot instead. 154 */ 155 if (ret == -ENOBUFS) 156 return -ENOBUFS; 157 return -EAGAIN; 158 } 159 160 static int io_uring_cmd_getsockname(struct socket *sock, 161 struct io_uring_cmd *cmd, 162 unsigned int issue_flags) 163 { 164 const struct io_uring_sqe *sqe = cmd->sqe; 165 struct sockaddr __user *uaddr; 166 unsigned int peer; 167 int __user *ulen; 168 169 if (sqe->ioprio || sqe->__pad1 || sqe->len || sqe->rw_flags) 170 return -EINVAL; 171 172 uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr)); 173 ulen = u64_to_user_ptr(READ_ONCE(sqe->addr3)); 174 peer = READ_ONCE(sqe->optlen); 175 if (peer > 1) 176 return -EINVAL; 177 return do_getsockname(sock, peer, uaddr, ulen); 178 } 179 180 int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags) 181 { 182 struct socket *sock = cmd->file->private_data; 183 184 switch (cmd->cmd_op) { 185 case SOCKET_URING_OP_SIOCINQ: 186 return io_uring_cmd_get_sock_ioctl(sock, SIOCINQ); 187 case SOCKET_URING_OP_SIOCOUTQ: 188 return io_uring_cmd_get_sock_ioctl(sock, SIOCOUTQ); 189 case SOCKET_URING_OP_GETSOCKOPT: 190 return io_uring_cmd_getsockopt(sock, cmd, issue_flags); 191 case SOCKET_URING_OP_SETSOCKOPT: 192 return io_uring_cmd_setsockopt(sock, cmd, issue_flags); 193 case SOCKET_URING_OP_TX_TIMESTAMP: 194 return io_uring_cmd_timestamp(sock, cmd, issue_flags); 195 case SOCKET_URING_OP_GETSOCKNAME: 196 return io_uring_cmd_getsockname(sock, cmd, issue_flags); 197 default: 198 return -EOPNOTSUPP; 199 } 200 } 201 EXPORT_SYMBOL_GPL(io_uring_cmd_sock); 202