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 #define PACK_OP_COMMON(pack, op_common)  do {\
38 	usbip_net_pack_uint16_t(pack, &(op_common)->version);\
39 	usbip_net_pack_uint16_t(pack, &(op_common)->code);\
40 	usbip_net_pack_uint32_t(pack, &(op_common)->status);\
41 } while (0)
42 
43 /* ---------------------------------------------------------------------- */
44 /* Dummy Code */
45 #define OP_UNSPEC	0x00
46 #define OP_REQ_UNSPEC	OP_UNSPEC
47 #define OP_REP_UNSPEC	OP_UNSPEC
48 
49 /* ---------------------------------------------------------------------- */
50 /* Retrieve USB device information. (still not used) */
51 #define OP_DEVINFO	0x02
52 #define OP_REQ_DEVINFO	(OP_REQUEST | OP_DEVINFO)
53 #define OP_REP_DEVINFO	(OP_REPLY   | OP_DEVINFO)
54 
55 struct op_devinfo_request {
56 	char busid[SYSFS_BUS_ID_SIZE];
57 } __attribute__((packed));
58 
59 struct op_devinfo_reply {
60 	struct usbip_usb_device udev;
61 	struct usbip_usb_interface uinf[];
62 } __attribute__((packed));
63 
64 /* ---------------------------------------------------------------------- */
65 /* Import a remote USB device. */
66 #define OP_IMPORT	0x03
67 #define OP_REQ_IMPORT	(OP_REQUEST | OP_IMPORT)
68 #define OP_REP_IMPORT   (OP_REPLY   | OP_IMPORT)
69 
70 struct op_import_request {
71 	char busid[SYSFS_BUS_ID_SIZE];
72 } __attribute__((packed));
73 
74 struct op_import_reply {
75 	struct usbip_usb_device udev;
76 //	struct usbip_usb_interface uinf[];
77 } __attribute__((packed));
78 
79 #define PACK_OP_IMPORT_REQUEST(pack, request)  do {\
80 } while (0)
81 
82 #define PACK_OP_IMPORT_REPLY(pack, reply)  do {\
83 	usbip_net_pack_usb_device(pack, &(reply)->udev);\
84 } while (0)
85 
86 /* ---------------------------------------------------------------------- */
87 /* Export a USB device to a remote host. */
88 #define OP_EXPORT	0x06
89 #define OP_REQ_EXPORT	(OP_REQUEST | OP_EXPORT)
90 #define OP_REP_EXPORT	(OP_REPLY   | OP_EXPORT)
91 
92 struct op_export_request {
93 	struct usbip_usb_device udev;
94 } __attribute__((packed));
95 
96 struct op_export_reply {
97 	int returncode;
98 } __attribute__((packed));
99 
100 
101 #define PACK_OP_EXPORT_REQUEST(pack, request)  do {\
102 	usbip_net_pack_usb_device(pack, &(request)->udev);\
103 } while (0)
104 
105 #define PACK_OP_EXPORT_REPLY(pack, reply)  do {\
106 } while (0)
107 
108 /* ---------------------------------------------------------------------- */
109 /* un-Export a USB device from a remote host. */
110 #define OP_UNEXPORT	0x07
111 #define OP_REQ_UNEXPORT	(OP_REQUEST | OP_UNEXPORT)
112 #define OP_REP_UNEXPORT	(OP_REPLY   | OP_UNEXPORT)
113 
114 struct op_unexport_request {
115 	struct usbip_usb_device udev;
116 } __attribute__((packed));
117 
118 struct op_unexport_reply {
119 	int returncode;
120 } __attribute__((packed));
121 
122 #define PACK_OP_UNEXPORT_REQUEST(pack, request)  do {\
123 	usbip_net_pack_usb_device(pack, &(request)->udev);\
124 } while (0)
125 
126 #define PACK_OP_UNEXPORT_REPLY(pack, reply)  do {\
127 } while (0)
128 
129 /* ---------------------------------------------------------------------- */
130 /* Negotiate IPSec encryption key. (still not used) */
131 #define OP_CRYPKEY	0x04
132 #define OP_REQ_CRYPKEY	(OP_REQUEST | OP_CRYPKEY)
133 #define OP_REP_CRYPKEY	(OP_REPLY   | OP_CRYPKEY)
134 
135 struct op_crypkey_request {
136 	/* 128bit key */
137 	uint32_t key[4];
138 } __attribute__((packed));
139 
140 struct op_crypkey_reply {
141 	uint32_t __reserved;
142 } __attribute__((packed));
143 
144 
145 /* ---------------------------------------------------------------------- */
146 /* Retrieve the list of exported USB devices. */
147 #define OP_DEVLIST	0x05
148 #define OP_REQ_DEVLIST	(OP_REQUEST | OP_DEVLIST)
149 #define OP_REP_DEVLIST	(OP_REPLY   | OP_DEVLIST)
150 
151 struct op_devlist_request {
152 } __attribute__((packed));
153 
154 struct op_devlist_reply {
155 	uint32_t ndev;
156 	/* followed by reply_extra[] */
157 } __attribute__((packed));
158 
159 struct op_devlist_reply_extra {
160 	struct usbip_usb_device    udev;
161 	struct usbip_usb_interface uinf[];
162 } __attribute__((packed));
163 
164 #define PACK_OP_DEVLIST_REQUEST(pack, request)  do {\
165 } while (0)
166 
167 #define PACK_OP_DEVLIST_REPLY(pack, reply)  do {\
168 	usbip_net_pack_uint32_t(pack, &(reply)->ndev);\
169 } while (0)
170 
171 void usbip_net_pack_uint32_t(int pack, uint32_t *num);
172 void usbip_net_pack_uint16_t(int pack, uint16_t *num);
173 void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev);
174 void usbip_net_pack_usb_interface(int pack, struct usbip_usb_interface *uinf);
175 
176 ssize_t usbip_net_recv(int sockfd, void *buff, size_t bufflen);
177 ssize_t usbip_net_send(int sockfd, void *buff, size_t bufflen);
178 int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status);
179 int usbip_net_recv_op_common(int sockfd, uint16_t *code);
180 int usbip_net_set_reuseaddr(int sockfd);
181 int usbip_net_set_nodelay(int sockfd);
182 int usbip_net_set_keepalive(int sockfd);
183 int usbip_net_set_v6only(int sockfd);
184 int usbip_net_tcp_connect(char *hostname, char *port);
185 
186 #endif /* __USBIP_NETWORK_H */
187