-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipxbox.c
384 lines (330 loc) · 9.85 KB
/
ipxbox.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* ipxbox - Userspace IPX adapter for DOSBox
*
* Copyright (c) 2014 Vitaly Sinilin
*/
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/ip.h>
#include <netpacket/packet.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "ipx.h"
#include "log.h"
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#define DEFAULT_UDP_PORT 213
#define IPX_MTU 576
#define IPX_BUF_SZ (IPX_MTU + 8) /* 8 bytes reserved for LLC and SNAP headers */
#define UDP_BUF_SZ IPX_MTU /* as per rfc1234 */
#define MAC_ADDR_SZ 6
#define FRAME_TYPES_TABLE \
X(FRAME_802_3, "802.3", ETH_P_802_3 ) \
X(FRAME_802_2, "802.2", ETH_P_802_2, 0xE0, 0xE0, 0x03 ) \
X(FRAME_802_2_SNAP, "802.2SNAP", ETH_P_802_2, 0xAA, 0xAA, 0x03, 0, 0, 0, 0x81, 0x37 ) \
X(FRAME_ETHERNET_II, "EthernetII", ETH_P_IPX )
static const struct
{
const char *name;
int proto;
const char *hdr;
size_t hdr_sz;
} g_frame_types[] =
{
#define X(_id, _name, _proto, ...) \
{ .name = _name, \
.proto = _proto, \
.hdr = (const char []){ __VA_ARGS__ }, \
.hdr_sz = sizeof((const char []){ __VA_ARGS__}) },
FRAME_TYPES_TABLE
#undef X
};
enum frame_type_id
{
#define X(id, name, proto, ...) id,
FRAME_TYPES_TABLE
#undef X
};
static int g_ipx_sock;
static int g_udp_sock;
static int g_udp_port = DEFAULT_UDP_PORT;
static unsigned int g_ipx_ifindex;
static int g_is_client_registered;
static int g_frame_type = FRAME_802_2;
static const char *g_extra_hdr;
static size_t g_extra_hdr_sz;
static union ipx_node g_my_node;
static union ipx_node g_client_node;
static struct sockaddr_in g_client_addr;
static void forward_packet_to_udp(const char *buf, size_t sz)
{
if (sz != sendto(g_udp_sock, buf, sz, 0,
(const struct sockaddr *)&g_client_addr,
sizeof(g_client_addr)))
{
warn_errno("Unable to send IPX->UDP packet");
}
}
static void forward_packet_to_ipx(const char *buf, size_t sz)
{
struct sockaddr_ll dst_addr =
{
.sll_family = AF_PACKET,
.sll_ifindex = g_ipx_ifindex,
.sll_protocol = htons(g_frame_types[g_frame_type].proto),
.sll_halen = MAC_ADDR_SZ
};
struct iovec iov[] =
{
{ (void *)g_extra_hdr, g_extra_hdr_sz },
{ (void *)buf, sz },
};
struct msghdr msg =
{
.msg_name = &dst_addr,
.msg_namelen = sizeof(dst_addr),
.msg_iov = iov,
.msg_iovlen = ARRAY_SIZE(iov),
};
struct ipx_hdr *hdr = (struct ipx_hdr *)buf;
memcpy(&dst_addr.sll_addr, &hdr->dst_addr.node, MAC_ADDR_SZ);
if ((sz + g_extra_hdr_sz) != sendmsg(g_ipx_sock, &msg, 0))
{
warn_errno("Unable to send UDP->IPX packet");
}
}
static void recv_ipx_packet(void)
{
struct sockaddr_ll src_addr;
socklen_t src_addr_len = sizeof(src_addr);
char buf[IPX_BUF_SZ];
ssize_t ret = recvfrom(g_ipx_sock, buf, sizeof(buf), 0,
(struct sockaddr *)&src_addr,
&src_addr_len);
if (-1 != ret)
{
if (ret >= g_extra_hdr_sz + sizeof(struct ipx_hdr))
{
if (g_extra_hdr_sz == 0 || !memcmp(buf, g_extra_hdr, g_extra_hdr_sz))
{
struct ipx_hdr *hdr = (struct ipx_hdr *)(buf + g_extra_hdr_sz);
size_t sz = ret - g_extra_hdr_sz;
if (0xFFFF == hdr->chksum)
{
debug("Received IPX packet of size %d from "
"%02X:%02X:%02X:%02X:%02X:%02X",
ret,
src_addr.sll_addr[0],
src_addr.sll_addr[1],
src_addr.sll_addr[2],
src_addr.sll_addr[3],
src_addr.sll_addr[4],
src_addr.sll_addr[5]);
if (g_is_client_registered)
{
forward_packet_to_udp((char *)hdr, sz);
}
else
{
warn("Ignore IPX packet since client is not "
"registered so far");
}
}
}
}
}
else
{
warn_errno("Unable to read from IPX socket");
}
}
static int is_dosbox_registration_request(const struct ipx_hdr *hdr)
{
const char nullnode[IPX_NODE_SZ] = { 0 };
return hdr->transport_control == 0
&& hdr->pkt_len == htons(IPX_HDR_SZ)
&& hdr->src_addr.net == 0
&& hdr->dst_addr.net == 0
&& hdr->src_addr.sock == htons(IPX_SOCK_ECHO)
&& hdr->dst_addr.sock == htons(IPX_SOCK_ECHO)
&& !memcmp(&hdr->src_addr.node, nullnode, sizeof(nullnode))
&& !memcmp(&hdr->dst_addr.node, nullnode, sizeof(nullnode));
}
static void send_dosbox_registration_response(void)
{
struct ipx_hdr hdr =
{
.chksum = 0xFFFF,
.pkt_len = htons(IPX_HDR_SZ),
.transport_control = 0,
.pkt_type = IPX_PKT_TYPE_ECHO,
.dst_addr = { .node = g_client_node, .sock = htons(IPX_SOCK_ECHO) },
.src_addr = { .node = g_my_node, .sock = htons(IPX_SOCK_ECHO) }
};
if (sizeof(hdr) != sendto(g_udp_sock, &hdr, sizeof(hdr), 0,
(struct sockaddr *)&g_client_addr,
sizeof(g_client_addr)))
{
warn_errno("Unable to send registration response");
}
}
static void recv_udp_packet(void)
{
struct sockaddr_in src_addr;
socklen_t src_addr_len = sizeof(src_addr);
char buf[UDP_BUF_SZ];
struct ipx_hdr *hdr = (struct ipx_hdr *)buf;
ssize_t ret = recvfrom(g_udp_sock, buf, sizeof(buf), 0,
(struct sockaddr *)&src_addr, &src_addr_len);
if (-1 != ret)
{
debug("Received UDP packet of size %u from %s:%u",
ret, inet_ntoa(src_addr.sin_addr), src_addr.sin_port);
if (ret >= sizeof(struct ipx_hdr))
{
if (is_dosbox_registration_request(hdr))
{
info("Received registration request from %s:%u",
inet_ntoa(src_addr.sin_addr), src_addr.sin_port);
g_client_addr = src_addr;
g_is_client_registered = 1;
send_dosbox_registration_response();
}
else
{
forward_packet_to_ipx(buf, ret);
}
}
}
else
{
warn_errno("Unable to read from UDP socket");
}
}
static void init_client_node(int fd, const char *ifname)
{
struct ifreq req;
memset(&req, '\0', sizeof(req));
strncpy(req.ifr_name, ifname, sizeof(req.ifr_name) - 1);
if (0 != ioctl(fd, SIOCGIFHWADDR, &req))
{
crit_errno("Unable to obtain MAC address of %s", ifname);
}
memcpy(&g_client_node, req.ifr_hwaddr.sa_data, MAC_ADDR_SZ);
}
static unsigned int get_if_index(const char *ifname)
{
unsigned int ifindex = if_nametoindex(ifname);
if (0 == ifindex)
{
crit_errno("Unable to obtain index of interface %s", ifname);
}
return ifindex;
}
static void setup_frame_type(void)
{
g_extra_hdr = g_frame_types[g_frame_type].hdr;
g_extra_hdr_sz = g_frame_types[g_frame_type].hdr_sz;
}
static enum frame_type_id parse_frame_type(const char *str)
{
size_t i;
for (i = 0; i < ARRAY_SIZE(g_frame_types); i++)
{
if (!strcmp(g_frame_types[i].name, str))
{
return i;
}
}
warn("Unknown frame type %s, supported types are:", str);
for (i = 0; i < ARRAY_SIZE(g_frame_types); i++)
{
warn(" %s", g_frame_types[i].name);
}
exit(EXIT_FAILURE);
return FRAME_802_2;
}
static int get_ipx_udp_port(void)
{
struct servent *se = getservbyname("ipx", "udp");
endservent();
if (se)
{
return ntohs(se->s_port);
}
else
{
warn("Unable to determine IPX over UDP port, use %i", DEFAULT_UDP_PORT);
return DEFAULT_UDP_PORT;
}
}
int main(int argc, char **argv)
{
if (argc != 2 && argc != 3)
{
crit("usage: ipxbox <ipxif> [frame_type]");
}
if (argc == 3)
{
g_frame_type = parse_frame_type(argv[2]);
}
setup_frame_type();
g_ipx_sock = socket(AF_PACKET, SOCK_DGRAM,
htons(g_frame_types[g_frame_type].proto));
if (-1 == g_ipx_sock)
{
crit_errno("Unable to open IPX socket");
}
g_udp_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (-1 == g_udp_sock)
{
crit_errno("Unable to open UDP socket");
}
g_udp_port = get_ipx_udp_port();
struct sockaddr_in udp_addr =
{
.sin_family = AF_INET,
.sin_port = htons(g_udp_port),
.sin_addr = htonl(0x7F000001) /* 127.0.0.1 */
};
if (0 != bind(g_udp_sock, (struct sockaddr *)&udp_addr, sizeof(udp_addr)))
{
crit_errno("Unable to bind UDP socket");
}
g_my_node.map.port = g_udp_port;
g_ipx_ifindex = get_if_index(argv[1]);
init_client_node(g_ipx_sock, argv[1]);
int nfds = MAX(g_ipx_sock, g_udp_sock) + 1;
for (;;)
{
fd_set sock_set;
FD_ZERO(&sock_set);
FD_SET(g_ipx_sock, &sock_set);
FD_SET(g_udp_sock, &sock_set);
int count = select(nfds, &sock_set, NULL, NULL, NULL);
if (count > 0)
{
if (FD_ISSET(g_ipx_sock, &sock_set))
{
recv_ipx_packet();
}
if (FD_ISSET(g_udp_sock, &sock_set))
{
recv_udp_packet();
}
}
else if (-1 == count)
{
warn_errno("select() error");
}
}
exit(EXIT_SUCCESS);
}