1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (C) 2005-2007 Takahiro Hirofuchi 4 */ 5 6 #ifndef __USBIP_NETWORK_H 7 #define __USBIP_NETWORK_H 8 9 #ifdef HAVE_CONFIG_H 10 #include "../config.h" 11 #endif 12 13 #include <sys/types.h> 14 15 #include <stdint.h> 16 17 extern int usbip_port; 18 extern char *usbip_port_string; 19 void usbip_setup_port_number(char *arg); 20 21 /* ---------------------------------------------------------------------- */ 22 /* Common header for all the kinds of PDUs. */ 23 struct op_common { 24 uint16_t version; 25 26 #define OP_REQUEST (0x80 << 8) 27 #define OP_REPLY (0x00 << 8) 28 uint16_t code; 29 30 /* add more error code */ 31 #define ST_OK 0x00 32 #define ST_NA 0x01 33 uint32_t status; /* op_code status (for reply) */ 34 35 } __attribute__((packed)); 36 37 /* ---------------------------------------------------------------------- */ 38 /* Dummy Code */ 39 #define OP_UNSPEC 0x00 40 #define OP_REQ_UNSPEC OP_UNSPEC 41 #define OP_REP_UNSPEC OP_UNSPEC 42 43 /* ---------------------------------------------------------------------- */ 44 /* Retrieve USB device information. (still not used) */ 45 #define OP_DEVINFO 0x02 46 #define OP_REQ_DEVINFO (OP_REQUEST | OP_DEVINFO) 47 #define OP_REP_DEVINFO (OP_REPLY | OP_DEVINFO) 48 49 struct op_devinfo_request { 50 char busid[SYSFS_BUS_ID_SIZE]; 51 } __attribute__((packed)); 52 53 struct op_devinfo_reply { 54 struct usbip_usb_device udev; 55 struct usbip_usb_interface uinf[]; 56 } __attribute__((packed)); 57 58 /* ---------------------------------------------------------------------- */ 59 /* Import a remote USB device. */ 60 #define OP_IMPORT 0x03 61 #define OP_REQ_IMPORT (OP_REQUEST | OP_IMPORT) 62 #define OP_REP_IMPORT (OP_REPLY | OP_IMPORT) 63 64 struct op_import_request { 65 char busid[SYSFS_BUS_ID_SIZE]; 66 } __attribute__((packed)); 67 68 struct op_import_reply { 69 struct usbip_usb_device udev; 70 // struct usbip_usb_interface uinf[]; 71 } __attribute__((packed)); 72 73 #define PACK_OP_IMPORT_REQUEST(pack, request) do {\ 74 } while (0) 75 76 #define PACK_OP_IMPORT_REPLY(pack, reply) do {\ 77 usbip_net_pack_usb_device(pack, &(reply)->udev);\ 78 } while (0) 79 80 /* ---------------------------------------------------------------------- */ 81 /* Export a USB device to a remote host. */ 82 #define OP_EXPORT 0x06 83 #define OP_REQ_EXPORT (OP_REQUEST | OP_EXPORT) 84 #define OP_REP_EXPORT (OP_REPLY | OP_EXPORT) 85 86 struct op_export_request { 87 struct usbip_usb_device udev; 88 } __attribute__((packed)); 89 90 struct op_export_reply { 91 int returncode; 92 } __attribute__((packed)); 93 94 95 #define PACK_OP_EXPORT_REQUEST(pack, request) do {\ 96 usbip_net_pack_usb_device(pack, &(request)->udev);\ 97 } while (0) 98 99 #define PACK_OP_EXPORT_REPLY(pack, reply) do {\ 100 } while (0) 101 102 /* ---------------------------------------------------------------------- */ 103 /* un-Export a USB device from a remote host. */ 104 #define OP_UNEXPORT 0x07 105 #define OP_REQ_UNEXPORT (OP_REQUEST | OP_UNEXPORT) 106 #define OP_REP_UNEXPORT (OP_REPLY | OP_UNEXPORT) 107 108 struct op_unexport_request { 109 struct usbip_usb_device udev; 110 } __attribute__((packed)); 111 112 struct op_unexport_reply { 113 int returncode; 114 } __attribute__((packed)); 115 116 #define PACK_OP_UNEXPORT_REQUEST(pack, request) do {\ 117 usbip_net_pack_usb_device(pack, &(request)->udev);\ 118 } while (0) 119 120 #define PACK_OP_UNEXPORT_REPLY(pack, reply) do {\ 121 } while (0) 122 123 /* ---------------------------------------------------------------------- */ 124 /* Negotiate IPSec encryption key. (still not used) */ 125 #define OP_CRYPKEY 0x04 126 #define OP_REQ_CRYPKEY (OP_REQUEST | OP_CRYPKEY) 127 #define OP_REP_CRYPKEY (OP_REPLY | OP_CRYPKEY) 128 129 struct op_crypkey_request { 130 /* 128bit key */ 131 uint32_t key[4]; 132 } __attribute__((packed)); 133 134 struct op_crypkey_reply { 135 uint32_t __reserved; 136 } __attribute__((packed)); 137 138 139 /* ---------------------------------------------------------------------- */ 140 /* Retrieve the list of exported USB devices. */ 141 #define OP_DEVLIST 0x05 142 #define OP_REQ_DEVLIST (OP_REQUEST | OP_DEVLIST) 143 #define OP_REP_DEVLIST (OP_REPLY | OP_DEVLIST) 144 145 struct op_devlist_request { 146 } __attribute__((packed)); 147 148 struct op_devlist_reply { 149 uint32_t ndev; 150 /* followed by reply_extra[] */ 151 } __attribute__((packed)); 152 153 struct op_devlist_reply_extra { 154 struct usbip_usb_device udev; 155 struct usbip_usb_interface uinf[]; 156 } __attribute__((packed)); 157 158 #define PACK_OP_DEVLIST_REQUEST(pack, request) do {\ 159 } while (0) 160 161 #define PACK_OP_DEVLIST_REPLY(pack, reply) do {\ 162 (reply)->ndev = usbip_net_pack_uint32_t(pack, (reply)->ndev);\ 163 } while (0) 164 165 uint32_t usbip_net_pack_uint32_t(int pack, uint32_t num); 166 uint16_t usbip_net_pack_uint16_t(int pack, uint16_t num); 167 void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev); 168 void usbip_net_pack_usb_interface(int pack, struct usbip_usb_interface *uinf); 169 170 ssize_t usbip_net_recv(int sockfd, void *buff, size_t bufflen); 171 ssize_t usbip_net_send(int sockfd, void *buff, size_t bufflen); 172 int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status); 173 int usbip_net_recv_op_common(int sockfd, uint16_t *code); 174 int usbip_net_set_reuseaddr(int sockfd); 175 int usbip_net_set_nodelay(int sockfd); 176 int usbip_net_set_keepalive(int sockfd); 177 int usbip_net_set_v6only(int sockfd); 178 int usbip_net_tcp_connect(char *hostname, char *port); 179 180 #endif /* __USBIP_NETWORK_H */ 181