-
Notifications
You must be signed in to change notification settings - Fork 17
/
freebsd.c
311 lines (260 loc) · 5.77 KB
/
freebsd.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
/*
* Copyright (c) 2005, Stacey Son <sson (at) verio (dot) net>
* All rights reserved.
*/
// freebsd.c: low level access routines for FreeBSD
#include "config.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <net/ethernet.h>
#include <net/bpf.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_dl.h>
#include <net/route.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <net/if.h>
#include <sys/stat.h>
#include <sys/disk.h>
#include <sys/select.h>
#include <sys/sysctl.h>
#include <fcntl.h>
#include <errno.h>
#include "dat.h"
#include "fns.h"
#define BPF_DEV "/dev/bpf0"
/* Packet buffer for getpkt() */
static uchar *pktbuf = NULL;
static int pktbufsz = 0;
int
dial(char *eth, int bufcnt)
{
char m;
int fd = -1;
struct bpf_version bv;
u_int v;
unsigned bufsize, linktype;
char device[sizeof BPF_DEV];
struct ifreq ifr;
struct bpf_program *bpf_program = create_bpf_program(shelf, slot);
strncpy(device, BPF_DEV, sizeof BPF_DEV);
/* find a bpf device we can use, check /dev/bpf[0-9] */
for (m = '0'; m <= '9'; m++) {
device[sizeof(BPF_DEV)-2] = m;
if ((fd = open(device, O_RDWR)) > 0)
break;
}
if (fd < 0) {
perror("open");
return -1;
}
if (ioctl(fd, BIOCVERSION, &bv) < 0) {
perror("BIOCVERSION");
goto bad;
}
if (bv.bv_major != BPF_MAJOR_VERSION ||
bv.bv_minor < BPF_MINOR_VERSION) {
fprintf(stderr,
"kernel bpf filter out of date\n");
goto bad;
}
/*
* Try finding a good size for the buffer; 65536 may be too
* big, so keep cutting it in half until we find a size
* that works, or run out of sizes to try.
*
*/
for (v = 65536; v != 0; v >>= 1) {
(void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
(void)strncpy(ifr.ifr_name, eth,
sizeof(ifr.ifr_name));
if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
break; /* that size worked; we're done */
if (errno != ENOBUFS) {
fprintf(stderr, "BIOCSETIF: %s: %s\n",
eth, strerror(errno));
goto bad;
}
}
if (v == 0) {
fprintf(stderr,
"BIOCSBLEN: %s: No buffer size worked\n", eth);
goto bad;
}
/* Allocate memory for the packet buffer */
pktbufsz = v;
if ((pktbuf = malloc(pktbufsz)) == NULL) {
perror("malloc");
goto bad;
}
/* Don't wait for buffer to be full or timeout */
v = 1;
if (ioctl(fd, BIOCIMMEDIATE, &v) < 0) {
perror("BIOCIMMEDIATE");
goto bad;
}
/* Only read incoming packets */
v = 0;
if (ioctl(fd, BIOCSSEESENT, &v) < 0) {
perror("BIOCSSEESENT");
goto bad;
}
/* Don't complete ethernet hdr */
v = 1;
if (ioctl(fd, BIOCSHDRCMPLT, &v) < 0) {
perror("BIOCSHDRCMPLT");
goto bad;
}
/* Get the data link layer type. */
if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
perror("BIOCGDLT");
goto bad;
}
linktype = v;
/* Get the filter buf size */
if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
perror("BIOCGBLEN");
goto bad;
}
bufsize = v;
if (ioctl(fd, BIOCSETF, (caddr_t)bpf_program) < 0) {
perror("BIOSETF");
goto bad;
}
free_bpf_program(bpf_program);
return(fd);
bad:
free_bpf_program(bpf_program);
close(fd);
return(-1);
}
int
getea(int s, char *eth, uchar *ea)
{
int mib[6];
size_t len;
char *buf, *next, *end;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
mib[0] = CTL_NET; mib[1] = AF_ROUTE;
mib[2] = 0; mib[3] = AF_LINK;
mib[4] = NET_RT_IFLIST; mib[5] = 0;
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
return (-1);
}
if (!(buf = (char *) malloc(len))) {
return (-1);
}
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
free(buf);
return (-1);
}
end = buf + len;
for (next = buf; next < end; next += ifm->ifm_msglen) {
ifm = (struct if_msghdr *)next;
if (ifm->ifm_type == RTM_IFINFO) {
sdl = (struct sockaddr_dl *)(ifm + 1);
if (strncmp(&sdl->sdl_data[0], eth,
sdl->sdl_nlen) == 0) {
memcpy(ea, LLADDR(sdl), ETHER_ADDR_LEN);
break;
}
}
}
free(buf);
return(0);
}
int
getsec(int fd, uchar *place, vlong lba, int nsec)
{
return pread(fd, place, nsec * 512, lba * 512);
}
int
putsec(int fd, uchar *place, vlong lba, int nsec)
{
return pwrite(fd, place, nsec * 512, lba * 512);
}
static int pktn = 0;
static uchar *pktbp = NULL;
int
getpkt(int fd, uchar *buf, int sz)
{
register struct bpf_hdr *bh;
register int pktlen, retlen;
if (pktn <= 0) {
if ((pktn = read(fd, pktbuf, pktbufsz)) < 0) {
perror("read");
exit(1);
}
pktbp = pktbuf;
}
bh = (struct bpf_hdr *) pktbp;
retlen = (int) bh->bh_caplen;
/* This memcpy() is currently needed */
memcpy(buf, (void *)(pktbp + bh->bh_hdrlen),
retlen > sz ? sz : retlen);
pktlen = bh->bh_hdrlen + bh->bh_caplen;
pktbp = pktbp + BPF_WORDALIGN(pktlen);
pktn -= (int) BPF_WORDALIGN(pktlen);
return retlen;
}
int
putpkt(int fd, uchar *buf, int sz)
{
return write(fd, buf, sz);
}
int
getmtu(int fd, char *name)
{
struct ifreq xx;
int s, n, p;
s = socket(AF_INET, SOCK_RAW, 0);
if (s == -1) {
perror("Can't get mtu");
return 1500;
}
xx.ifr_addr.sa_family = AF_INET;
snprintf(xx.ifr_name, sizeof xx.ifr_name, "%s", name);
n = ioctl(s, SIOCGIFMTU, &xx);
if (n == -1) {
perror("Can't get mtu");
return 1500;
}
close(s);
// FreeBSD bpf writes are capped at one PAGESIZE'd mbuf. As such we must
// limit our sector count. See FreeBSD PR 205164, OpenAoE/vblade #7.
p = getpagesize();
if (xx.ifr_mtu > p) {
return p;
}
return xx.ifr_mtu;
}
vlong
getsize(int fd)
{
off_t media_size;
vlong size;
struct stat s;
int n;
// Try getting disklabel from block dev
if ((n = ioctl(fd, DIOCGMEDIASIZE, &media_size)) != -1) {
size = media_size;
} else {
// must not be a block special dev
if (fstat(fd, &s) == -1) {
perror("getsize");
exit(1);
}
size = s.st_size;
}
printf("ioctl returned %d\n", n);
printf("%lld bytes\n", size);
return size;
}