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 int af = AF_INET; 57 58 if (argc != 0 && argc != 3 && argc != 1) { 59 errno = EINVAL; 60 return -1; 61 } 62 63 if (argc == 3) { 64 va_list ap; 65 va_start(ap, argc); 66 offset = va_arg(ap, size_t); 67 cpy_mem = va_arg(ap, void *); 68 clen = va_arg(ap, size_t); 69 va_end(ap); 70 } else if (argc == 1) { 71 va_list ap; 72 va_start(ap, argc); 73 af = va_arg(ap, int); 74 va_end(ap); 75 } 76 77 if (com > 0xffffffff) { 78 printf("WARNING: ioctl sign-extension ioctl %lx\n", com); 79 com &= 0xffffffff; 80 } 81 82 size = IOCPARM_LEN(com); 83 if ((size > IOCPARM_MAX) || 84 ((com & (IOC_IN | IOC_OUT)) == 0) || 85 (size == 0) || 86 (com & IOC_VOID)) 87 return (ENOTTY); 88 89 msg = ff_ipc_msg_alloc(); 90 if (msg == NULL) { 91 errno = ENOMEM; 92 return -1; 93 } 94 95 if (size > msg->buf_len) { 96 errno = ENOMEM; 97 ff_ipc_msg_free(msg); 98 return -1; 99 } 100 101 #ifdef INET6 102 if (af == AF_INET6) { 103 msg->msg_type = FF_IOCTL6; 104 } else 105 #endif 106 if (af == AF_INET) 107 msg->msg_type = FF_IOCTL; 108 else { 109 errno = EINVAL; 110 ff_ipc_msg_free(msg); 111 return -1; 112 } 113 114 msg->ioctl.cmd = com; 115 msg->ioctl.data = msg->buf_addr; 116 memcpy(msg->ioctl.data, data, size); 117 msg->buf_addr += size; 118 119 if (argc == 3) { 120 if (size + clen > msg->buf_len) { 121 errno = ENOMEM; 122 ff_ipc_msg_free(msg); 123 return -1; 124 } 125 char *ptr = (char *)(msg->ioctl.data) + offset; 126 char *buf_addr = msg->buf_addr; 127 memcpy(ptr, &buf_addr, sizeof(char *)); 128 memcpy(buf_addr, cpy_mem, clen); 129 } 130 131 int ret = ff_ipc_send(msg); 132 if (ret < 0) { 133 errno = EPIPE; 134 ff_ipc_msg_free(msg); 135 return -1; 136 } 137 138 do { 139 if (retmsg != NULL) { 140 ff_ipc_msg_free(retmsg); 141 } 142 ret = ff_ipc_recv(&retmsg); 143 if (ret < 0) { 144 errno = EPIPE; 145 ff_ipc_msg_free(msg); 146 return -1; 147 } 148 } while (msg != retmsg); 149 150 if (retmsg->result == 0) { 151 ret = 0; 152 153 if (com & IOC_OUT) { 154 memcpy(data, retmsg->ioctl.data, size); 155 if (argc == 3) { 156 memcpy(cpy_mem, retmsg->buf_addr, clen); 157 char *ptr = (char *)data + offset; 158 memcpy(ptr, &cpy_mem, sizeof(void *)); 159 } 160 } 161 } else { 162 ret = -1; 163 errno = retmsg->result; 164 } 165 166 ff_ipc_msg_free(msg); 167 168 return ret; 169 } 170 171