-
Notifications
You must be signed in to change notification settings - Fork 17
/
linux.c
164 lines (142 loc) · 3.09 KB
/
linux.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
// linux.c: low level access routines for Linux
#define _GNU_SOURCE
#include "config.h"
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <features.h> /* for the glibc version number */
#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h> /* the L2 protocols */
#else
#include <asm/types.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h> /* The L2 protocols */
#endif
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <net/if.h>
#include <netinet/in.h>
#include <linux/fs.h>
#include <sys/stat.h>
#include "dat.h"
#include "fns.h"
int getindx(int, char *);
int getea(int, char *, uchar *);
int
dial(char *eth, int bufcnt) // get us a raw connection to an interface
{
int i, n, s;
struct sockaddr_ll sa;
enum { aoe_type = 0x88a2 };
memset(&sa, 0, sizeof sa);
s = socket(PF_PACKET, SOCK_RAW, htons(aoe_type));
if (s == -1) {
perror("got bad socket");
return -1;
}
i = getindx(s, eth);
if (i < 0) {
perror(eth);
return -1;
}
sa.sll_family = AF_PACKET;
sa.sll_protocol = htons(0x88a2);
sa.sll_ifindex = i;
n = bind(s, (struct sockaddr *)&sa, sizeof sa);
if (n == -1) {
perror("bind funky");
return -1;
}
struct bpf_program {
ulong bf_len;
void *bf_insns;
} *bpf_program = create_bpf_program(shelf, slot);
setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, bpf_program, sizeof(*bpf_program));
free_bpf_program(bpf_program);
n = bufcnt * getmtu(s, eth);
if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) < 0)
perror("setsockopt SOL_SOCKET, SO_SNDBUF");
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) < 0)
perror("setsockopt SOL_SOCKET, SO_RCVBUF");
return s;
}
int
getindx(int s, char *name) // return the index of device 'name'
{
struct ifreq xx;
int n;
snprintf(xx.ifr_name, sizeof xx.ifr_name, "%s", name);
n = ioctl(s, SIOCGIFINDEX, &xx);
if (n == -1)
return -1;
return xx.ifr_ifindex;
}
int
getea(int s, char *name, uchar *ea)
{
struct ifreq xx;
int n;
snprintf(xx.ifr_name, sizeof xx.ifr_name, "%s", name);
n = ioctl(s, SIOCGIFHWADDR, &xx);
if (n == -1) {
perror("Can't get hw addr");
return 0;
}
memmove(ea, xx.ifr_hwaddr.sa_data, 6);
return 1;
}
int
getmtu(int s, char *name)
{
struct ifreq xx;
int n;
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;
}
return xx.ifr_mtu;
}
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);
}
int
getpkt(int fd, uchar *buf, int sz)
{
return read(fd, buf, sz);
}
int
putpkt(int fd, uchar *buf, int sz)
{
return write(fd, buf, sz);
}
vlong
getsize(int fd)
{
vlong size;
struct stat s;
int n;
n = ioctl(fd, BLKGETSIZE64, &size);
if (n == -1) { // must not be a block special
n = fstat(fd, &s);
if (n == -1) {
perror("getsize");
exit(1);
}
size = s.st_size;
}
return size;
}