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 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 #include <sys/syscallsubr.h> 31 #include <sys/sysproto.h> 32 #include <sys/pcpu.h> 33 #include <sys/proc.h> 34 #include <netgraph/ng_message.h> 35 #include <netgraph/ng_socket.h> 36 37 #include "ff_api.h" 38 #include "ff_host_interface.h" 39 40 static int 41 ngctl_socket(struct socket_args *uap) 42 { 43 int error = sys_socket(curthread, uap); 44 if (error) { 45 ff_os_errno(error); 46 return -1; 47 } 48 return curthread->td_retval[0]; 49 } 50 51 static int 52 ngctl_connect(struct connect_args *uap) 53 { 54 int error = sys_connect(curthread, uap); 55 if (error) { 56 ff_os_errno(error); 57 return (-1); 58 } 59 60 return (error); 61 } 62 63 static int 64 ngctl_bind(struct bind_args *uap) 65 { 66 int error = sys_bind(curthread, uap); 67 if (error) { 68 ff_os_errno(error); 69 return (-1); 70 } 71 72 return (error); 73 } 74 75 static int 76 ngctl_recvfrom(struct recvfrom_args *uap) 77 { 78 int error = sys_recvfrom(curthread, uap); 79 if (error) { 80 ff_os_errno(error); 81 return (-1); 82 } 83 return curthread->td_retval[0]; 84 } 85 86 static int 87 ngctl_sendto(struct sendto_args *uap) 88 { 89 int error = sys_sendto(curthread, uap); 90 if (error) { 91 ff_os_errno(error); 92 return (-1); 93 } 94 return curthread->td_retval[0]; 95 } 96 97 static int 98 ngctl_close(int sockfd) 99 { 100 int error = kern_close(curthread, sockfd); 101 if (error) { 102 ff_os_errno(error); 103 return (-1); 104 } 105 return (error); 106 } 107 108 int 109 ff_ngctl(int cmd, void *data) 110 { 111 switch(cmd) { 112 case NGCTL_SOCKET: 113 return ngctl_socket((struct socket_args *)data); 114 case NGCTL_CONNECT: 115 return ngctl_connect((struct connect_args *)data); 116 case NGCTL_BIND: 117 return ngctl_bind((struct bind_args *)data); 118 case NGCTL_SEND: 119 return ngctl_sendto((struct sendto_args *)data); 120 case NGCTL_RECV: 121 return ngctl_recvfrom((struct recvfrom_args *)data); 122 case NGCTL_CLOSE: 123 return ngctl_close(*(int *)data); 124 default: 125 break; 126 } 127 128 ff_os_errno(EINVAL); 129 return (-1); 130 } 131 132