-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhello.c
190 lines (155 loc) · 4.55 KB
/
hello.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
#include <errno.h>
#include <string.h>
#include <linux/filter.h>
#include <stddef.h>
#include <stdbool.h>
#include <sys/resource.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <lkl.h>
#include <lkl_host.h>
#include <sys/time.h>
#include <dirent.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "../tests/test.h"
#include "bpf.h"
#define NUM_ICMP 10
#define ARRAY_MAP_SIZE 0x1337
char bpf_log_buf[BPF_LOG_BUF_SIZE];
static u_short in_cksum(const u_short *addr, register int len, u_short csum)
{
int nleft = len;
const u_short *w = addr;
u_short answer;
int sum = csum;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
if (nleft == 1)
sum += htons(*(u_char *)w << 8);
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return answer;
}
int lkl_test_icmp(int prog_fd)
{
int sock, ret, i;
struct lkl_iphdr *iph;
struct lkl_icmphdr *icmp;
struct lkl_sockaddr_in saddr;
struct lkl_pollfd pfd;
char buf[32];
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
//saddr.sin_addr.lkl_s_addr =
inet_aton("127.0.0.1",&saddr.sin_addr.lkl_s_addr);
printf("pinging %s\n",
inet_ntoa(*(struct in_addr *)&saddr.sin_addr));
sock = lkl_sys_socket(LKL_AF_INET, LKL_SOCK_RAW, LKL_IPPROTO_ICMP);
if (sock < 0) {
printf("socket error (%s)\n", lkl_strerror(sock));
return TEST_FAILURE;
}
#if 1
if (lkl_sys_setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &prog_fd, sizeof(prog_fd)) < 1) {
printf("ATTACH BPF setsockopt %d\n", errno);
}
#endif
for (i = 0; i < NUM_ICMP; i++) {
icmp = malloc(sizeof(struct lkl_icmphdr *));
icmp->type = LKL_ICMP_ECHO;
icmp->code = 0;
icmp->checksum = 0;
icmp->un.echo.sequence = htons(i);
icmp->un.echo.id = 0;
icmp->checksum = in_cksum((u_short *)icmp, sizeof(*icmp), 0);
ret = lkl_sys_sendto(sock, icmp, sizeof(*icmp), 0,
(struct lkl_sockaddr *)&saddr,
sizeof(saddr));
if (ret < 0) {
printf("sendto error (%s)\n", lkl_strerror(ret));
return TEST_FAILURE;
}
free(icmp);
pfd.fd = sock;
pfd.events = LKL_POLLIN;
pfd.revents = 0;
ret = lkl_sys_poll(&pfd, 1, 1000);
if (ret < 0) {
printf("poll error (%s)\n", lkl_strerror(ret));
return TEST_FAILURE;
}
ret = lkl_sys_recv(sock, buf, sizeof(buf), LKL_MSG_DONTWAIT);
if (ret < 0) {
printf("recv error (%s)\n", lkl_strerror(ret));
return TEST_FAILURE;
}
iph = (struct lkl_iphdr *)buf;
icmp = (struct lkl_icmphdr *)(buf + iph->ihl * 4);
/* DHCP server may issue an ICMP echo request to a dhcp client */
if ((icmp->type != LKL_ICMP_ECHOREPLY || icmp->code != 0) &&
(icmp->type != LKL_ICMP_ECHO)) {
printf("no ICMP echo reply (type=%d, code=%d)\n",
icmp->type, icmp->code);
return TEST_FAILURE;
}
printf("ICMP echo reply (seq=%d)\n", ntohs(icmp->un.echo.sequence));
}
return TEST_SUCCESS;
}
int main(void)
{
/* Start the kernel */
lkl_start_kernel(&lkl_host_ops, "mem=100M");
/*Simple Valid Program*/
struct bpf_insn prog_valid[] = {
BPF_MOV64_IMM(BPF_REG_0, 0), // R0 = 0
BPF_ALU64_IMM(BPF_ADD, BPF_REG_0, 2), // R0 = R0 + 2
BPF_MOV32_IMM(BPF_REG_0, 2), // R0 = 2
BPF_EXIT_INSN() // exit()
};
int insn_cnt = sizeof(prog_valid) / sizeof(struct bpf_insn);
union bpf_attr attr = {
.prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
.insns = ptr_to_u64(prog_valid),
.insn_cnt = insn_cnt,
.license = ptr_to_u64("GPL"),
.log_buf = ptr_to_u64(bpf_log_buf),
.log_size = BPF_LOG_BUF_SIZE,
.log_level = 2, /*2 - detailed register states , 1 - minimum logs*/
.kern_version = LINUX_VERSION_CODE
};
/*Load the BPF program, invokes the verifier*/
int prog_fd;
long p[3] = {BPF_PROG_LOAD, (long)&attr,sizeof(attr)};
prog_fd = lkl_syscall(__lkl__NR_bpf, p);
if(prog_fd < 0){
printf("BPF Verification Failed\n");
return 0;
} else {
printf("BPF Verification Passed\n");
}
printf("===== verifier o/p: begin =====\n %s\n============ end ============= \n",bpf_log_buf);
/* Attach to socket to run the ebpf program on receiving packet */
int sock,ret;
sock = lkl_sys_socket(LKL_AF_INET, LKL_SOCK_STREAM, 0);
struct lkl_ifreq ifr;
strcpy(ifr.lkl_ifr_name, "lo");
ret = lkl_sys_ioctl(sock, LKL_SIOCGIFINDEX, (long)&ifr);
lkl_if_up(1); // BACKEND_NONE
lkl_test_icmp(prog_fd);
sleep(1);
return 0;
}