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
|
#include "pdesc.h"
#include "ppkt.h"
#include "psock.h"
#include "putils.h"
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
enum ptype ppkt_type_to_enum(struct ppkt_header const * pheader)
{
enum ptype pt = (enum ptype)pheader->type;
switch (pt) {
case PTYPE_INVALID:
case PTYPE_AUTH_REQUEST:
case PTYPE_AUTH_RESPONSE:
return pt;
}
return PTYPE_INVALID;
}
int ppkt_process_icmp(struct psock * sock)
{
if (sock->current.peer_sockaddr.ss_family == AF_INET) {
sock->current.pkt_buf.icmphdr.checksum = ntohs(sock->current.pkt_buf.icmphdr.checksum);
sock->current.pkt_buf.icmphdr.un.echo.id = ntohs(sock->current.pkt_buf.icmphdr.un.echo.id);
sock->current.pkt_buf.icmphdr.un.echo.sequence = ntohs(sock->current.pkt_buf.icmphdr.un.echo.sequence);
} else {
return -1;
}
return 0;
}
static size_t ppkt_size(enum ptype type)
{
size_t body_siz = 0;
switch (type) {
case PTYPE_INVALID:
return 0;
case PTYPE_AUTH_REQUEST:
body_siz = sizeof(struct ppkt_auth_request);
break;
case PTYPE_AUTH_RESPONSE:
body_siz = sizeof(struct ppkt_auth_response);
break;
}
return sizeof(struct ppkt_header) + body_siz;
}
static size_t ppkt_data_size(union ppkt_body * pbody, enum ptype type)
{
size_t data_siz = 0;
switch (type) {
case PTYPE_INVALID:
return 0;
case PTYPE_AUTH_REQUEST:
data_siz = pbody->auth_request.authdata_siz;
break;
case PTYPE_AUTH_RESPONSE:
data_siz = 0;
break;
}
return data_siz;
}
int ppkt_process_ppkt(struct psock * sock)
{
size_t const min_pkt_siz = sizeof(struct iphdr) + sizeof(sock->current.pkt_buf.icmphdr);
if (sock->current.peer_sockaddr.ss_family != AF_INET) {
return -1;
}
if (sock->current.bytes_read < min_pkt_siz + sizeof(sock->current.pkt_buf.pheader)) {
return -1;
}
sock->current.pkt_buf.pheader.total_size = ntohs(sock->current.pkt_buf.pheader.total_size);
if (sock->current.pkt_buf.pheader.total_size != sock->current.bytes_read - min_pkt_siz) {
return -1;
}
enum ptype packet_type = ppkt_type_to_enum(&sock->current.pkt_buf.pheader);
size_t packet_body_size = ppkt_size(packet_type);
if (packet_body_size == 0 ||
packet_body_size > sock->current.bytes_read - (min_pkt_siz + sizeof(sock->current.pkt_buf.pheader))) {
return -1;
}
switch (packet_type) {
case PTYPE_INVALID:
return -1;
case PTYPE_AUTH_REQUEST:
sock->current.pkt_buf.pbody.auth_request.magic = ntohl(sock->current.pkt_buf.pbody.auth_request.magic);
sock->current.pkt_buf.pbody.auth_request.authdata_siz =
ntohs(sock->current.pkt_buf.pbody.auth_request.authdata_siz);
break;
case PTYPE_AUTH_RESPONSE:
break;
}
size_t packet_data_size = ppkt_data_size(&sock->current.pkt_buf.pbody, packet_type);
if (sock->current.bytes_read != min_pkt_siz + packet_body_size + packet_data_size) {
return -1;
}
return 0;
}
static void ppkt_init_pkt(struct pdesc * desc, struct ppkt_buffer * pkt_buf, enum ptype type, size_t data_siz)
{
pkt_buf->icmphdr.un.echo.id = desc->identifier;
pkt_buf->icmphdr.un.echo.sequence = ++desc->sequence;
switch (type) {
case PTYPE_INVALID:
pkt_buf->icmphdr.type = 3; // Destination Unreachable
break;
case PTYPE_AUTH_REQUEST:
pkt_buf->icmphdr.type = 8; // Echo Request
pkt_buf->pbody.auth_request.authdata_siz = data_siz;
break;
case PTYPE_AUTH_RESPONSE:
pkt_buf->icmphdr.type = 0; // Echo Reply
break;
}
pkt_buf->icmphdr.code = 0;
pkt_buf->pheader.type = type;
pkt_buf->pheader.total_size = ppkt_size(type) + ppkt_data_size(&pkt_buf->pbody, type);
}
static void ppkt_finalize_pkt(struct ppkt_buffer * const pkt_buf)
{
pkt_buf->icmphdr.un.echo.id = htons(pkt_buf->icmphdr.un.echo.id);
pkt_buf->icmphdr.un.echo.sequence = htons(pkt_buf->icmphdr.un.echo.sequence);
switch (pkt_buf->pheader.type) {
case PTYPE_INVALID:
break;
case PTYPE_AUTH_REQUEST:
pkt_buf->pbody.auth_request.magic = htonl(PTUNNEL_MAGIC);
pkt_buf->pbody.auth_request.authdata_siz = htons(pkt_buf->pbody.auth_request.authdata_siz);
break;
case PTYPE_AUTH_RESPONSE:
break;
}
pkt_buf->pheader.total_size = htons(pkt_buf->pheader.total_size);
pkt_buf->icmphdr.checksum = 0;
pkt_buf->icmphdr.checksum = icmp_checksum_iovec(pkt_buf->iovec, pkt_buf->iovec_used);
}
void ppkt_prepare_auth_request(struct pdesc * desc,
struct ppkt_buffer * pkt_buf,
uint8_t * authdata,
size_t authdata_siz)
{
ppkt_init_pkt(desc, pkt_buf, PTYPE_AUTH_REQUEST, authdata_siz);
pkt_buf->iovec[0].iov_base = &pkt_buf->icmphdr;
pkt_buf->iovec[0].iov_len = sizeof(pkt_buf->icmphdr);
pkt_buf->iovec[1].iov_base = &pkt_buf->pheader;
pkt_buf->iovec[1].iov_len = sizeof(pkt_buf->pheader);
pkt_buf->iovec[2].iov_base = &pkt_buf->pbody.auth_request;
pkt_buf->iovec[2].iov_len = sizeof(pkt_buf->pbody.auth_request);
pkt_buf->iovec[3].iov_base = authdata;
pkt_buf->iovec[3].iov_len = authdata_siz;
pkt_buf->iovec_used = 4;
ppkt_finalize_pkt(pkt_buf);
}
|