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