Karthikeyan Ramasubramanian | f7a4b6e | 2013-01-16 09:00:28 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/net.h> |
| 16 | #include <linux/socket.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/poll.h> |
| 20 | #include <linux/fcntl.h> |
| 21 | #include <linux/gfp.h> |
| 22 | #include <linux/msm_ipc.h> |
Zaheerulla Meer | ba10d37 | 2013-02-21 17:56:18 +0530 | [diff] [blame] | 23 | #include <linux/sched.h> |
| 24 | #include <linux/thread_info.h> |
| 25 | #include <linux/qmi_encdec.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 26 | |
| 27 | #include <asm/string.h> |
| 28 | #include <asm/atomic.h> |
| 29 | |
| 30 | #include <net/sock.h> |
| 31 | |
Karthikeyan Ramasubramanian | efc493b | 2012-07-12 10:25:49 -0600 | [diff] [blame] | 32 | #include <mach/msm_ipc_router.h> |
Zaheerulla Meer | ba10d37 | 2013-02-21 17:56:18 +0530 | [diff] [blame] | 33 | #include <mach/msm_ipc_logging.h> |
Karthikeyan Ramasubramanian | efc493b | 2012-07-12 10:25:49 -0600 | [diff] [blame] | 34 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 35 | #include "ipc_router.h" |
Karthikeyan Ramasubramanian | 5b502d364 | 2012-09-23 22:23:36 -0600 | [diff] [blame] | 36 | #include "msm_ipc_router_security.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 37 | |
| 38 | #define msm_ipc_sk(sk) ((struct msm_ipc_sock *)(sk)) |
| 39 | #define msm_ipc_sk_port(sk) ((struct msm_ipc_port *)(msm_ipc_sk(sk)->port)) |
Zaheerulla Meer | ba10d37 | 2013-02-21 17:56:18 +0530 | [diff] [blame] | 40 | #define REQ_RESP_IPC_LOG_PAGES 5 |
| 41 | #define IND_IPC_LOG_PAGES 5 |
| 42 | #define IPC_SEND 1 |
| 43 | #define IPC_RECV 2 |
| 44 | #define IPC_REQ_RESP_LOG(level, buf...) \ |
| 45 | do { \ |
| 46 | if (ipc_req_resp_log_txt) { \ |
| 47 | ipc_log_string(ipc_req_resp_log_txt, buf); \ |
| 48 | } \ |
| 49 | } while (0) \ |
| 50 | |
| 51 | #define IPC_IND_LOG(level, buf...) \ |
| 52 | do { \ |
| 53 | if (ipc_ind_log_txt) { \ |
| 54 | ipc_log_string(ipc_ind_log_txt, buf); \ |
| 55 | } \ |
| 56 | } while (0) \ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 57 | |
Karthikeyan Ramasubramanian | 29dfa70 | 2013-05-31 15:36:38 -0600 | [diff] [blame] | 58 | #ifndef SIZE_MAX |
| 59 | #define SIZE_MAX ((size_t)-1) |
| 60 | #endif |
| 61 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 62 | static int sockets_enabled; |
| 63 | static struct proto msm_ipc_proto; |
| 64 | static const struct proto_ops msm_ipc_proto_ops; |
Zaheerulla Meer | ba10d37 | 2013-02-21 17:56:18 +0530 | [diff] [blame] | 65 | static void *ipc_req_resp_log_txt; |
| 66 | static void *ipc_ind_log_txt; |
| 67 | |
| 68 | /** |
| 69 | * msm_ipc_router_ipc_log() - Pass log data to IPC logging framework |
| 70 | * @tran: Identifies the data to be a receive or send. |
| 71 | * @ipc_buf: Buffer to extract the log data. |
| 72 | * @port_ptr: IPC Router port corresponding to the current log data. |
| 73 | * |
| 74 | * This function builds the data the would be passed on to the IPC logging |
| 75 | * framework. The data that would be passed corresponds to the information |
| 76 | * that is exchanged between the IPC Router and user space modules during |
| 77 | * request/response/indication transactions. |
| 78 | */ |
| 79 | |
| 80 | static void msm_ipc_router_ipc_log(uint8_t tran, |
| 81 | struct sk_buff *ipc_buf, struct msm_ipc_port *port_ptr) |
| 82 | { |
| 83 | struct qmi_header *hdr = (struct qmi_header *)ipc_buf->data; |
| 84 | |
| 85 | /* |
| 86 | * IPC Logging format is as below:- |
| 87 | * <Name>(Name of the User Space Process): |
| 88 | * <PID> (PID of the user space process) : |
| 89 | * <TID> (TID of the user space thread) : |
| 90 | * <User Space Module>(CLNT or SERV) : |
| 91 | * <Opertaion Type> (Transmit) : |
| 92 | * <Control Flag> (Req/Resp/Ind) : |
| 93 | * <Transaction ID> : |
| 94 | * <Message ID> : |
| 95 | * <Message Length> : |
| 96 | */ |
| 97 | if (ipc_req_resp_log_txt && |
| 98 | (((uint8_t) hdr->cntl_flag == QMI_REQUEST_CONTROL_FLAG) || |
| 99 | ((uint8_t) hdr->cntl_flag == QMI_RESPONSE_CONTROL_FLAG)) && |
| 100 | (port_ptr->type == CLIENT_PORT || |
| 101 | port_ptr->type == SERVER_PORT)) { |
| 102 | IPC_REQ_RESP_LOG(KERN_DEBUG, |
| 103 | "%s %d %d %s %s CF:%x TI:%x MI:%x ML:%x", |
| 104 | current->comm, current->tgid, current->pid, |
| 105 | (port_ptr->type == CLIENT_PORT ? "QCCI" : "QCSI"), |
| 106 | (tran == IPC_RECV ? "RX" : |
| 107 | (tran == IPC_SEND ? "TX" : "ERR")), |
| 108 | (uint8_t)hdr->cntl_flag, hdr->txn_id, hdr->msg_id, |
| 109 | hdr->msg_len); |
| 110 | } else if (ipc_ind_log_txt && |
| 111 | ((uint8_t)hdr->cntl_flag == QMI_INDICATION_CONTROL_FLAG) && |
| 112 | (port_ptr->type == CLIENT_PORT || |
| 113 | port_ptr->type == SERVER_PORT)) { |
| 114 | IPC_IND_LOG(KERN_DEBUG, |
| 115 | "%s %d %d %s %s CF:%x TI:%x MI:%x ML:%x", |
| 116 | current->comm, current->tgid, current->pid, |
| 117 | (port_ptr->type == CLIENT_PORT ? "QCCI" : "QCSI"), |
| 118 | (tran == IPC_RECV ? "RX" : |
| 119 | (tran == IPC_SEND ? "TX" : "ERR")), |
| 120 | (uint8_t)hdr->cntl_flag, hdr->txn_id, hdr->msg_id, |
| 121 | hdr->msg_len); |
| 122 | } |
| 123 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 124 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 125 | static struct sk_buff_head *msm_ipc_router_build_msg(unsigned int num_sect, |
| 126 | struct iovec const *msg_sect, |
| 127 | size_t total_len) |
| 128 | { |
| 129 | struct sk_buff_head *msg_head; |
| 130 | struct sk_buff *msg; |
| 131 | int i, copied, first = 1; |
| 132 | int data_size = 0, request_size, offset; |
| 133 | void *data; |
| 134 | |
| 135 | for (i = 0; i < num_sect; i++) |
| 136 | data_size += msg_sect[i].iov_len; |
| 137 | |
| 138 | if (!data_size) |
| 139 | return NULL; |
| 140 | |
| 141 | msg_head = kmalloc(sizeof(struct sk_buff_head), GFP_KERNEL); |
| 142 | if (!msg_head) { |
| 143 | pr_err("%s: cannot allocate skb_head\n", __func__); |
| 144 | return NULL; |
| 145 | } |
| 146 | skb_queue_head_init(msg_head); |
| 147 | |
| 148 | for (copied = 1, i = 0; copied && (i < num_sect); i++) { |
| 149 | data_size = msg_sect[i].iov_len; |
| 150 | offset = 0; |
| 151 | while (offset != msg_sect[i].iov_len) { |
| 152 | request_size = data_size; |
| 153 | if (first) |
| 154 | request_size += IPC_ROUTER_HDR_SIZE; |
| 155 | |
| 156 | msg = alloc_skb(request_size, GFP_KERNEL); |
| 157 | if (!msg) { |
| 158 | if (request_size <= (PAGE_SIZE/2)) { |
| 159 | pr_err("%s: cannot allocated skb\n", |
| 160 | __func__); |
| 161 | goto msg_build_failure; |
| 162 | } |
| 163 | data_size = data_size / 2; |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | if (first) { |
| 168 | skb_reserve(msg, IPC_ROUTER_HDR_SIZE); |
| 169 | first = 0; |
| 170 | } |
| 171 | |
| 172 | data = skb_put(msg, data_size); |
| 173 | copied = !copy_from_user(msg->data, |
| 174 | msg_sect[i].iov_base + offset, |
| 175 | data_size); |
| 176 | if (!copied) { |
| 177 | pr_err("%s: copy_from_user failed\n", |
| 178 | __func__); |
| 179 | kfree_skb(msg); |
| 180 | goto msg_build_failure; |
| 181 | } |
| 182 | skb_queue_tail(msg_head, msg); |
| 183 | offset += data_size; |
| 184 | data_size = msg_sect[i].iov_len - offset; |
| 185 | } |
| 186 | } |
| 187 | return msg_head; |
| 188 | |
| 189 | msg_build_failure: |
| 190 | while (!skb_queue_empty(msg_head)) { |
| 191 | msg = skb_dequeue(msg_head); |
| 192 | kfree_skb(msg); |
| 193 | } |
| 194 | kfree(msg_head); |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | static int msm_ipc_router_extract_msg(struct msghdr *m, |
| 199 | struct sk_buff_head *msg_head) |
| 200 | { |
Karthikeyan Ramasubramanian | 4d1695b | 2013-04-05 11:30:53 -0600 | [diff] [blame] | 201 | struct sockaddr_msm_ipc *addr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 202 | struct rr_header *hdr; |
| 203 | struct sk_buff *temp; |
Zaheerulla Meer | e3f6c3a | 2013-04-17 01:16:47 +0530 | [diff] [blame] | 204 | union rr_control_msg *ctl_msg; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 205 | int offset = 0, data_len = 0, copy_len; |
| 206 | |
| 207 | if (!m || !msg_head) { |
| 208 | pr_err("%s: Invalid pointers passed\n", __func__); |
| 209 | return -EINVAL; |
| 210 | } |
Karthikeyan Ramasubramanian | 4d1695b | 2013-04-05 11:30:53 -0600 | [diff] [blame] | 211 | addr = (struct sockaddr_msm_ipc *)m->msg_name; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 212 | |
| 213 | temp = skb_peek(msg_head); |
| 214 | hdr = (struct rr_header *)(temp->data); |
Zaheerulla Meer | e3f6c3a | 2013-04-17 01:16:47 +0530 | [diff] [blame] | 215 | if (addr && (hdr->type == IPC_ROUTER_CTRL_CMD_RESUME_TX)) { |
| 216 | skb_pull(temp, IPC_ROUTER_HDR_SIZE); |
| 217 | ctl_msg = (union rr_control_msg *)(temp->data); |
| 218 | addr->family = AF_MSM_IPC; |
| 219 | addr->address.addrtype = MSM_IPC_ADDR_ID; |
| 220 | addr->address.addr.port_addr.node_id = ctl_msg->cli.node_id; |
| 221 | addr->address.addr.port_addr.port_id = ctl_msg->cli.port_id; |
| 222 | m->msg_namelen = sizeof(struct sockaddr_msm_ipc); |
| 223 | return offset; |
| 224 | } |
Karthikeyan Ramasubramanian | 4d1695b | 2013-04-05 11:30:53 -0600 | [diff] [blame] | 225 | if (addr && (hdr->src_port_id != IPC_ROUTER_ADDRESS)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 226 | addr->family = AF_MSM_IPC; |
| 227 | addr->address.addrtype = MSM_IPC_ADDR_ID; |
| 228 | addr->address.addr.port_addr.node_id = hdr->src_node_id; |
| 229 | addr->address.addr.port_addr.port_id = hdr->src_port_id; |
| 230 | m->msg_namelen = sizeof(struct sockaddr_msm_ipc); |
| 231 | } |
| 232 | |
| 233 | data_len = hdr->size; |
| 234 | skb_pull(temp, IPC_ROUTER_HDR_SIZE); |
| 235 | skb_queue_walk(msg_head, temp) { |
| 236 | copy_len = data_len < temp->len ? data_len : temp->len; |
| 237 | if (copy_to_user(m->msg_iov->iov_base + offset, temp->data, |
| 238 | copy_len)) { |
| 239 | pr_err("%s: Copy to user failed\n", __func__); |
| 240 | return -EFAULT; |
| 241 | } |
| 242 | offset += copy_len; |
| 243 | data_len -= copy_len; |
| 244 | } |
| 245 | return offset; |
| 246 | } |
| 247 | |
| 248 | static void msm_ipc_router_release_msg(struct sk_buff_head *msg_head) |
| 249 | { |
| 250 | struct sk_buff *temp; |
| 251 | |
| 252 | if (!msg_head) { |
| 253 | pr_err("%s: Invalid msg pointer\n", __func__); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | while (!skb_queue_empty(msg_head)) { |
| 258 | temp = skb_dequeue(msg_head); |
| 259 | kfree_skb(temp); |
| 260 | } |
| 261 | kfree(msg_head); |
| 262 | } |
| 263 | |
| 264 | static int msm_ipc_router_create(struct net *net, |
| 265 | struct socket *sock, |
| 266 | int protocol, |
| 267 | int kern) |
| 268 | { |
| 269 | struct sock *sk; |
| 270 | struct msm_ipc_port *port_ptr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 271 | |
| 272 | if (unlikely(protocol != 0)) { |
| 273 | pr_err("%s: Protocol not supported\n", __func__); |
| 274 | return -EPROTONOSUPPORT; |
| 275 | } |
| 276 | |
| 277 | switch (sock->type) { |
| 278 | case SOCK_DGRAM: |
| 279 | break; |
| 280 | default: |
| 281 | pr_err("%s: Protocol type not supported\n", __func__); |
| 282 | return -EPROTOTYPE; |
| 283 | } |
| 284 | |
| 285 | sk = sk_alloc(net, AF_MSM_IPC, GFP_KERNEL, &msm_ipc_proto); |
| 286 | if (!sk) { |
| 287 | pr_err("%s: sk_alloc failed\n", __func__); |
| 288 | return -ENOMEM; |
| 289 | } |
| 290 | |
| 291 | port_ptr = msm_ipc_router_create_raw_port(sk, NULL, NULL); |
| 292 | if (!port_ptr) { |
| 293 | pr_err("%s: port_ptr alloc failed\n", __func__); |
| 294 | sk_free(sk); |
| 295 | return -ENOMEM; |
| 296 | } |
| 297 | |
Karthikeyan Ramasubramanian | 5b502d364 | 2012-09-23 22:23:36 -0600 | [diff] [blame] | 298 | port_ptr->check_send_permissions = msm_ipc_check_send_permissions; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 299 | sock->ops = &msm_ipc_proto_ops; |
| 300 | sock_init_data(sock, sk); |
| 301 | sk->sk_rcvtimeo = DEFAULT_RCV_TIMEO; |
| 302 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 303 | msm_ipc_sk(sk)->port = port_ptr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | int msm_ipc_router_bind(struct socket *sock, struct sockaddr *uaddr, |
| 309 | int uaddr_len) |
| 310 | { |
| 311 | struct sockaddr_msm_ipc *addr = (struct sockaddr_msm_ipc *)uaddr; |
| 312 | struct sock *sk = sock->sk; |
| 313 | struct msm_ipc_port *port_ptr; |
| 314 | int ret; |
Karthikeyan Ramasubramanian | 88f02e5 | 2013-06-03 12:05:50 -0600 | [diff] [blame] | 315 | void *pil; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 316 | |
| 317 | if (!sk) |
| 318 | return -EINVAL; |
| 319 | |
Karthikeyan Ramasubramanian | 13ef055 | 2013-01-24 11:45:41 -0700 | [diff] [blame] | 320 | if (!check_permissions()) { |
| 321 | pr_err("%s: %s Do not have permissions\n", |
| 322 | __func__, current->comm); |
| 323 | return -EPERM; |
| 324 | } |
| 325 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 326 | if (!uaddr_len) { |
| 327 | pr_err("%s: Invalid address length\n", __func__); |
| 328 | return -EINVAL; |
| 329 | } |
| 330 | |
| 331 | if (addr->family != AF_MSM_IPC) { |
| 332 | pr_err("%s: Address family is incorrect\n", __func__); |
| 333 | return -EAFNOSUPPORT; |
| 334 | } |
| 335 | |
| 336 | if (addr->address.addrtype != MSM_IPC_ADDR_NAME) { |
| 337 | pr_err("%s: Address type is incorrect\n", __func__); |
| 338 | return -EINVAL; |
| 339 | } |
| 340 | |
| 341 | port_ptr = msm_ipc_sk_port(sk); |
| 342 | if (!port_ptr) |
| 343 | return -ENODEV; |
| 344 | |
Karthikeyan Ramasubramanian | 88f02e5 | 2013-06-03 12:05:50 -0600 | [diff] [blame] | 345 | pil = msm_ipc_load_default_node(); |
| 346 | msm_ipc_sk(sk)->default_pil = pil; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 347 | lock_sock(sk); |
| 348 | |
| 349 | ret = msm_ipc_router_register_server(port_ptr, &addr->address); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 350 | |
| 351 | release_sock(sk); |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | static int msm_ipc_router_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 356 | struct msghdr *m, size_t total_len) |
| 357 | { |
| 358 | struct sock *sk = sock->sk; |
| 359 | struct msm_ipc_port *port_ptr = msm_ipc_sk_port(sk); |
| 360 | struct sockaddr_msm_ipc *dest = (struct sockaddr_msm_ipc *)m->msg_name; |
| 361 | struct sk_buff_head *msg; |
Zaheerulla Meer | ba10d37 | 2013-02-21 17:56:18 +0530 | [diff] [blame] | 362 | struct sk_buff *ipc_buf; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 363 | int ret; |
| 364 | |
| 365 | if (!dest) |
| 366 | return -EDESTADDRREQ; |
| 367 | |
| 368 | if (m->msg_namelen < sizeof(*dest) || dest->family != AF_MSM_IPC) |
| 369 | return -EINVAL; |
| 370 | |
| 371 | if (total_len > MAX_IPC_PKT_SIZE) |
| 372 | return -EINVAL; |
| 373 | |
| 374 | lock_sock(sk); |
| 375 | msg = msm_ipc_router_build_msg(m->msg_iovlen, m->msg_iov, total_len); |
| 376 | if (!msg) { |
| 377 | pr_err("%s: Msg build failure\n", __func__); |
| 378 | ret = -ENOMEM; |
| 379 | goto out_sendmsg; |
| 380 | } |
| 381 | |
Karthikeyan Ramasubramanian | f7a4b6e | 2013-01-16 09:00:28 -0700 | [diff] [blame] | 382 | if (port_ptr->type == CLIENT_PORT) |
| 383 | wait_for_irsc_completion(); |
Zaheerulla Meer | ba10d37 | 2013-02-21 17:56:18 +0530 | [diff] [blame] | 384 | ipc_buf = skb_peek(msg); |
Brent Hronik | 1c9a3d4 | 2013-04-17 15:10:40 -0600 | [diff] [blame] | 385 | if (ipc_buf) |
| 386 | msm_ipc_router_ipc_log(IPC_SEND, ipc_buf, port_ptr); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 387 | ret = msm_ipc_router_send_to(port_ptr, msg, &dest->address); |
| 388 | if (ret == (IPC_ROUTER_HDR_SIZE + total_len)) |
| 389 | ret = total_len; |
| 390 | |
| 391 | out_sendmsg: |
| 392 | release_sock(sk); |
| 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | static int msm_ipc_router_recvmsg(struct kiocb *iocb, struct socket *sock, |
| 397 | struct msghdr *m, size_t buf_len, int flags) |
| 398 | { |
| 399 | struct sock *sk = sock->sk; |
| 400 | struct msm_ipc_port *port_ptr = msm_ipc_sk_port(sk); |
| 401 | struct sk_buff_head *msg; |
Zaheerulla Meer | ba10d37 | 2013-02-21 17:56:18 +0530 | [diff] [blame] | 402 | struct sk_buff *ipc_buf; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 403 | long timeout; |
| 404 | int ret; |
| 405 | |
| 406 | if (m->msg_iovlen != 1) |
| 407 | return -EOPNOTSUPP; |
| 408 | |
| 409 | if (!buf_len) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | lock_sock(sk); |
| 413 | timeout = sk->sk_rcvtimeo; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 414 | |
Arun Kumar Neelakantam | 0af9676 | 2013-06-21 17:57:18 +0530 | [diff] [blame^] | 415 | ret = msm_ipc_router_rx_data_wait(port_ptr, timeout); |
| 416 | if (ret) { |
| 417 | release_sock(sk); |
| 418 | if (ret == -ENOMSG) |
Zaheerulla Meer | e3f6c3a | 2013-04-17 01:16:47 +0530 | [diff] [blame] | 419 | m->msg_namelen = 0; |
Arun Kumar Neelakantam | 0af9676 | 2013-06-21 17:57:18 +0530 | [diff] [blame^] | 420 | return ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 421 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 422 | |
| 423 | ret = msm_ipc_router_read(port_ptr, &msg, buf_len); |
| 424 | if (ret <= 0 || !msg) { |
| 425 | release_sock(sk); |
| 426 | return ret; |
| 427 | } |
| 428 | |
| 429 | ret = msm_ipc_router_extract_msg(m, msg); |
Zaheerulla Meer | ba10d37 | 2013-02-21 17:56:18 +0530 | [diff] [blame] | 430 | ipc_buf = skb_peek(msg); |
Brent Hronik | 1c9a3d4 | 2013-04-17 15:10:40 -0600 | [diff] [blame] | 431 | if (ipc_buf) |
| 432 | msm_ipc_router_ipc_log(IPC_RECV, ipc_buf, port_ptr); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 433 | msm_ipc_router_release_msg(msg); |
| 434 | msg = NULL; |
| 435 | release_sock(sk); |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | static int msm_ipc_router_ioctl(struct socket *sock, |
| 440 | unsigned int cmd, unsigned long arg) |
| 441 | { |
| 442 | struct sock *sk = sock->sk; |
| 443 | struct msm_ipc_port *port_ptr; |
| 444 | struct server_lookup_args server_arg; |
Karthikeyan Ramasubramanian | dfde01b | 2012-06-12 14:25:13 -0600 | [diff] [blame] | 445 | struct msm_ipc_server_info *srv_info = NULL; |
Karthikeyan Ramasubramanian | 29dfa70 | 2013-05-31 15:36:38 -0600 | [diff] [blame] | 446 | unsigned int n; |
| 447 | size_t srv_info_sz = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 448 | int ret; |
Karthikeyan Ramasubramanian | 88f02e5 | 2013-06-03 12:05:50 -0600 | [diff] [blame] | 449 | void *pil; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 450 | |
| 451 | if (!sk) |
| 452 | return -EINVAL; |
| 453 | |
| 454 | lock_sock(sk); |
| 455 | port_ptr = msm_ipc_sk_port(sock->sk); |
| 456 | if (!port_ptr) { |
| 457 | release_sock(sk); |
| 458 | return -EINVAL; |
| 459 | } |
| 460 | |
| 461 | switch (cmd) { |
| 462 | case IPC_ROUTER_IOCTL_GET_VERSION: |
| 463 | n = IPC_ROUTER_VERSION; |
| 464 | ret = put_user(n, (unsigned int *)arg); |
| 465 | break; |
| 466 | |
| 467 | case IPC_ROUTER_IOCTL_GET_MTU: |
| 468 | n = (MAX_IPC_PKT_SIZE - IPC_ROUTER_HDR_SIZE); |
| 469 | ret = put_user(n, (unsigned int *)arg); |
| 470 | break; |
| 471 | |
| 472 | case IPC_ROUTER_IOCTL_GET_CURR_PKT_SIZE: |
| 473 | ret = msm_ipc_router_get_curr_pkt_size(port_ptr); |
| 474 | break; |
| 475 | |
| 476 | case IPC_ROUTER_IOCTL_LOOKUP_SERVER: |
Karthikeyan Ramasubramanian | 88f02e5 | 2013-06-03 12:05:50 -0600 | [diff] [blame] | 477 | pil = msm_ipc_load_default_node(); |
| 478 | msm_ipc_sk(sk)->default_pil = pil; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 479 | ret = copy_from_user(&server_arg, (void *)arg, |
| 480 | sizeof(server_arg)); |
| 481 | if (ret) { |
| 482 | ret = -EFAULT; |
| 483 | break; |
| 484 | } |
| 485 | |
| 486 | if (server_arg.num_entries_in_array < 0) { |
| 487 | ret = -EINVAL; |
| 488 | break; |
| 489 | } |
| 490 | if (server_arg.num_entries_in_array) { |
Karthikeyan Ramasubramanian | 29dfa70 | 2013-05-31 15:36:38 -0600 | [diff] [blame] | 491 | if (server_arg.num_entries_in_array > |
| 492 | (SIZE_MAX / sizeof(*srv_info))) { |
Karthikeyan Ramasubramanian | b34b667 | 2013-02-26 16:37:50 -0700 | [diff] [blame] | 493 | pr_err("%s: Integer Overflow %d * %d\n", |
| 494 | __func__, sizeof(*srv_info), |
| 495 | server_arg.num_entries_in_array); |
| 496 | ret = -EINVAL; |
| 497 | break; |
| 498 | } |
Karthikeyan Ramasubramanian | 29dfa70 | 2013-05-31 15:36:38 -0600 | [diff] [blame] | 499 | srv_info_sz = server_arg.num_entries_in_array * |
| 500 | sizeof(*srv_info); |
Karthikeyan Ramasubramanian | dfde01b | 2012-06-12 14:25:13 -0600 | [diff] [blame] | 501 | srv_info = kmalloc(srv_info_sz, GFP_KERNEL); |
| 502 | if (!srv_info) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 503 | ret = -ENOMEM; |
| 504 | break; |
| 505 | } |
| 506 | } |
| 507 | ret = msm_ipc_router_lookup_server_name(&server_arg.port_name, |
Karthikeyan Ramasubramanian | dfde01b | 2012-06-12 14:25:13 -0600 | [diff] [blame] | 508 | srv_info, server_arg.num_entries_in_array, |
Karthikeyan Ramasubramanian | cc450c9 | 2011-07-27 14:38:15 -0600 | [diff] [blame] | 509 | server_arg.lookup_mask); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 510 | if (ret < 0) { |
| 511 | pr_err("%s: Server not found\n", __func__); |
| 512 | ret = -ENODEV; |
Karthikeyan Ramasubramanian | dfde01b | 2012-06-12 14:25:13 -0600 | [diff] [blame] | 513 | kfree(srv_info); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 514 | break; |
| 515 | } |
| 516 | server_arg.num_entries_found = ret; |
| 517 | |
| 518 | ret = copy_to_user((void *)arg, &server_arg, |
| 519 | sizeof(server_arg)); |
Karthikeyan Ramasubramanian | dfde01b | 2012-06-12 14:25:13 -0600 | [diff] [blame] | 520 | if (srv_info_sz) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 521 | ret = copy_to_user((void *)(arg + sizeof(server_arg)), |
Karthikeyan Ramasubramanian | dfde01b | 2012-06-12 14:25:13 -0600 | [diff] [blame] | 522 | srv_info, srv_info_sz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 523 | if (ret) |
| 524 | ret = -EFAULT; |
Karthikeyan Ramasubramanian | dfde01b | 2012-06-12 14:25:13 -0600 | [diff] [blame] | 525 | kfree(srv_info); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 526 | } |
| 527 | break; |
| 528 | |
| 529 | case IPC_ROUTER_IOCTL_BIND_CONTROL_PORT: |
| 530 | ret = msm_ipc_router_bind_control_port(port_ptr); |
| 531 | break; |
| 532 | |
Karthikeyan Ramasubramanian | 5b502d364 | 2012-09-23 22:23:36 -0600 | [diff] [blame] | 533 | case IPC_ROUTER_IOCTL_CONFIG_SEC_RULES: |
| 534 | ret = msm_ipc_config_sec_rules((void *)arg); |
Karthikeyan Ramasubramanian | f7a4b6e | 2013-01-16 09:00:28 -0700 | [diff] [blame] | 535 | if (ret != -EPERM) |
| 536 | port_ptr->type = IRSC_PORT; |
Karthikeyan Ramasubramanian | 5b502d364 | 2012-09-23 22:23:36 -0600 | [diff] [blame] | 537 | break; |
| 538 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 539 | default: |
| 540 | ret = -EINVAL; |
| 541 | } |
| 542 | release_sock(sk); |
| 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | static unsigned int msm_ipc_router_poll(struct file *file, |
| 547 | struct socket *sock, poll_table *wait) |
| 548 | { |
| 549 | struct sock *sk = sock->sk; |
| 550 | struct msm_ipc_port *port_ptr; |
| 551 | uint32_t mask = 0; |
| 552 | |
| 553 | if (!sk) |
| 554 | return -EINVAL; |
| 555 | |
| 556 | port_ptr = msm_ipc_sk_port(sk); |
| 557 | if (!port_ptr) |
| 558 | return -EINVAL; |
| 559 | |
| 560 | poll_wait(file, &port_ptr->port_rx_wait_q, wait); |
| 561 | |
| 562 | if (!list_empty(&port_ptr->port_rx_q)) |
| 563 | mask |= (POLLRDNORM | POLLIN); |
| 564 | |
| 565 | return mask; |
| 566 | } |
| 567 | |
| 568 | static int msm_ipc_router_close(struct socket *sock) |
| 569 | { |
| 570 | struct sock *sk = sock->sk; |
| 571 | struct msm_ipc_port *port_ptr = msm_ipc_sk_port(sk); |
Karthikeyan Ramasubramanian | 88f02e5 | 2013-06-03 12:05:50 -0600 | [diff] [blame] | 572 | void *pil = msm_ipc_sk(sk)->default_pil; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 573 | int ret; |
| 574 | |
| 575 | lock_sock(sk); |
| 576 | ret = msm_ipc_router_close_port(port_ptr); |
Karthikeyan Ramasubramanian | 88f02e5 | 2013-06-03 12:05:50 -0600 | [diff] [blame] | 577 | if (pil) |
| 578 | msm_ipc_unload_default_node(pil); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 579 | release_sock(sk); |
| 580 | sock_put(sk); |
| 581 | sock->sk = NULL; |
| 582 | |
| 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | static const struct net_proto_family msm_ipc_family_ops = { |
| 587 | .owner = THIS_MODULE, |
| 588 | .family = AF_MSM_IPC, |
| 589 | .create = msm_ipc_router_create |
| 590 | }; |
| 591 | |
| 592 | static const struct proto_ops msm_ipc_proto_ops = { |
| 593 | .owner = THIS_MODULE, |
| 594 | .family = AF_MSM_IPC, |
| 595 | .bind = msm_ipc_router_bind, |
| 596 | .connect = sock_no_connect, |
| 597 | .sendmsg = msm_ipc_router_sendmsg, |
| 598 | .recvmsg = msm_ipc_router_recvmsg, |
| 599 | .ioctl = msm_ipc_router_ioctl, |
| 600 | .poll = msm_ipc_router_poll, |
| 601 | .setsockopt = sock_no_setsockopt, |
| 602 | .getsockopt = sock_no_getsockopt, |
| 603 | .release = msm_ipc_router_close, |
| 604 | }; |
| 605 | |
| 606 | static struct proto msm_ipc_proto = { |
| 607 | .name = "MSM_IPC", |
| 608 | .owner = THIS_MODULE, |
| 609 | .obj_size = sizeof(struct msm_ipc_sock), |
| 610 | }; |
| 611 | |
Zaheerulla Meer | ba10d37 | 2013-02-21 17:56:18 +0530 | [diff] [blame] | 612 | /** |
| 613 | * msm_ipc_router_ipc_log_init() - Init function for IPC Logging |
| 614 | * |
| 615 | * Initialize the buffers to be used to provide the log information |
| 616 | * pertaining to the request, response and indication data flow that |
| 617 | * happens between user and kernel spaces. |
| 618 | */ |
| 619 | void msm_ipc_router_ipc_log_init(void) |
| 620 | { |
| 621 | ipc_req_resp_log_txt = |
Zaheerulla Meer | a3cea66 | 2013-03-08 17:31:23 +0530 | [diff] [blame] | 622 | ipc_log_context_create(REQ_RESP_IPC_LOG_PAGES, |
| 623 | "ipc_rtr_req_resp"); |
Zaheerulla Meer | ba10d37 | 2013-02-21 17:56:18 +0530 | [diff] [blame] | 624 | if (!ipc_req_resp_log_txt) { |
| 625 | pr_err("%s: Unable to create IPC logging for Req/Resp", |
| 626 | __func__); |
| 627 | } |
| 628 | ipc_ind_log_txt = |
Zaheerulla Meer | a3cea66 | 2013-03-08 17:31:23 +0530 | [diff] [blame] | 629 | ipc_log_context_create(IND_IPC_LOG_PAGES, "ipc_rtr_ind"); |
Zaheerulla Meer | ba10d37 | 2013-02-21 17:56:18 +0530 | [diff] [blame] | 630 | if (!ipc_ind_log_txt) { |
| 631 | pr_err("%s: Unable to create IPC logging for Indications", |
| 632 | __func__); |
| 633 | } |
| 634 | } |
| 635 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 636 | int msm_ipc_router_init_sockets(void) |
| 637 | { |
| 638 | int ret; |
| 639 | |
| 640 | ret = proto_register(&msm_ipc_proto, 1); |
| 641 | if (ret) { |
| 642 | pr_err("Failed to register MSM_IPC protocol type\n"); |
| 643 | goto out_init_sockets; |
| 644 | } |
| 645 | |
| 646 | ret = sock_register(&msm_ipc_family_ops); |
| 647 | if (ret) { |
| 648 | pr_err("Failed to register MSM_IPC socket type\n"); |
| 649 | proto_unregister(&msm_ipc_proto); |
| 650 | goto out_init_sockets; |
| 651 | } |
| 652 | |
| 653 | sockets_enabled = 1; |
Zaheerulla Meer | ba10d37 | 2013-02-21 17:56:18 +0530 | [diff] [blame] | 654 | msm_ipc_router_ipc_log_init(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 655 | out_init_sockets: |
| 656 | return ret; |
| 657 | } |
| 658 | |
| 659 | void msm_ipc_router_exit_sockets(void) |
| 660 | { |
| 661 | if (!sockets_enabled) |
| 662 | return; |
| 663 | |
| 664 | sockets_enabled = 0; |
| 665 | sock_unregister(msm_ipc_family_ops.family); |
| 666 | proto_unregister(&msm_ipc_proto); |
| 667 | } |