1 /* 2 * Copyright (C) 2017 THL A29 Limited, a Tencent company. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 */ 26 27 #include <string.h> 28 #include <stdarg.h> 29 #include <sys/socket.h> 30 #include <sys/ioctl.h> 31 #include "ff_ipc.h" 32 33 /* 34 * In general, we always call like this: ioctl(fd, com, data), 35 * but if there is a pointer in the data and the pointer points to 36 * a memory area, for example, data is struct ifreq, and it uses 37 * ifreq.ifr_ifru.ifru_data, we must copy the memory to msg->buf_addr, 38 * after this, it can be used to communicate with F-Stack process. 39 * Otherwise, an unknown error will occur. 40 * 41 * Two cases: 42 * 1.Normal, there is no need to copy memory: ioctl_va(fd, com, data, 0). 43 * 2.There is a memory need to be copied: ioctl_va(fd, com, data, 3, offset, cpy_mem, clen). 44 * offset: the offset of cpy_mem relative to data struct. 45 * cpy_mem: the memory address that need to be copied. 46 * clen: the size of memory that the cpy_mem pointed to. 47 * 48 */ 49 int 50 ioctl_va(int fd, unsigned long com, void *data, int argc, ...) 51 { 52 struct ff_msg *msg, *retmsg = NULL; 53 unsigned size; 54 void *cpy_mem; 55 size_t offset, clen; 56 57 if (argc != 0 && argc != 3) { 58 errno = EINVAL; 59 return -1; 60 } 61 62 if (argc == 3) { 63 va_list ap; 64 va_start(ap, argc); 65 offset = va_arg(ap, size_t); 66 cpy_mem = va_arg(ap, void *); 67 clen = va_arg(ap, size_t); 68 va_end(ap); 69 } 70 71 if (com > 0xffffffff) { 72 printf("WARNING: ioctl sign-extension ioctl %lx\n", com); 73 com &= 0xffffffff; 74 } 75 76 size = IOCPARM_LEN(com); 77 if ((size > IOCPARM_MAX) || 78 ((com & (IOC_IN | IOC_OUT)) == 0) || 79 (size == 0) || 80 (com & IOC_VOID)) 81 return (ENOTTY); 82 83 msg = ff_ipc_msg_alloc(); 84 if (msg == NULL) { 85 errno = ENOMEM; 86 return -1; 87 } 88 89 if (size > msg->buf_len) { 90 errno = ENOMEM; 91 ff_ipc_msg_free(msg); 92 return -1; 93 } 94 95 msg->msg_type = FF_IOCTL; 96 msg->ioctl.cmd = com; 97 msg->ioctl.data = msg->buf_addr; 98 memcpy(msg->ioctl.data, data, size); 99 msg->buf_addr += size; 100 101 if (argc == 3) { 102 if (size + clen > msg->buf_len) { 103 errno = ENOMEM; 104 ff_ipc_msg_free(msg); 105 return -1; 106 } 107 char *ptr = (char *)(msg->ioctl.data) + offset; 108 char *buf_addr = msg->buf_addr; 109 memcpy(ptr, &buf_addr, sizeof(char *)); 110 memcpy(buf_addr, cpy_mem, clen); 111 } 112 113 int ret = ff_ipc_send(msg); 114 if (ret < 0) { 115 errno = EPIPE; 116 ff_ipc_msg_free(msg); 117 return -1; 118 } 119 120 do { 121 if (retmsg != NULL) { 122 ff_ipc_msg_free(retmsg); 123 } 124 ret = ff_ipc_recv(&retmsg); 125 if (ret < 0) { 126 errno = EPIPE; 127 ff_ipc_msg_free(msg); 128 return -1; 129 } 130 } while (msg != retmsg); 131 132 if (retmsg->result == 0) { 133 ret = 0; 134 135 if (com & IOC_OUT) { 136 memcpy(data, retmsg->ioctl.data, size); 137 if (argc == 3) { 138 memcpy(cpy_mem, retmsg->buf_addr, clen); 139 char *ptr = (char *)data + offset; 140 memcpy(ptr, &cpy_mem, sizeof(void *)); 141 } 142 } 143 } else { 144 ret = -1; 145 errno = retmsg->result; 146 } 147 148 ff_ipc_msg_free(msg); 149 150 return ret; 151 } 152 153