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
|
/*
* (C) 2006-2007 by Pablo Neira Ayuso <pablo@netfilter.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "conntrackd.h"
#include "network.h"
#if 0
#define _TEST_DROP
#else
#undef _TEST_DROP
#endif
static int drop = 0; /* debugging purposes */
static unsigned int seq_set, cur_seq;
static int send_netmsg(struct mcast_sock *m, void *data, unsigned int len)
{
struct nlnetwork *net = data;
#ifdef _TEST_DROP
if (++drop > 10) {
drop = 0;
printf("dropping resend (seq=%u)\n", ntohl(net->seq));
return 0;
}
#endif
return mcast_send(m, net, len);
}
int mcast_send_netmsg(struct mcast_sock *m, void *data)
{
struct nlmsghdr *nlh = data + sizeof(struct nlnetwork);
unsigned int len = nlh->nlmsg_len + sizeof(struct nlnetwork);
struct nlnetwork *net = data;
if (!seq_set) {
seq_set = 1;
cur_seq = time(NULL);
net->flags |= NET_HELLO;
}
net->flags = htons(net->flags);
net->seq = htonl(cur_seq++);
if (nlh_host2network(nlh) == -1)
return -1;
net->checksum = 0;
net->checksum = ntohs(do_csum(data, len));
return send_netmsg(m, data, len);
}
int mcast_resend_netmsg(struct mcast_sock *m, void *data)
{
struct nlnetwork *net = data;
struct nlmsghdr *nlh = data + sizeof(struct nlnetwork);
unsigned int len;
net->flags = ntohs(net->flags);
if (!seq_set) {
seq_set = 1;
cur_seq = time(NULL);
net->flags |= NET_HELLO;
}
if (net->flags & NET_NACK || net->flags & NET_ACK)
len = sizeof(struct nlnetwork_ack);
else
len = sizeof(struct nlnetwork) + ntohl(nlh->nlmsg_len);
net->flags = htons(net->flags);
net->seq = htonl(cur_seq++);
net->checksum = 0;
net->checksum = ntohs(do_csum(data, len));
return send_netmsg(m, data, len);
}
int mcast_send_error(struct mcast_sock *m, void *data)
{
struct nlnetwork *net = data;
unsigned int len = sizeof(struct nlnetwork);
if (!seq_set) {
seq_set = 1;
cur_seq = time(NULL);
net->flags |= NET_HELLO;
}
if (net->flags & NET_NACK || net->flags & NET_ACK) {
struct nlnetwork_ack *nack = (struct nlnetwork_ack *) net;
nack->from = htonl(nack->from);
nack->to = htonl(nack->to);
len = sizeof(struct nlnetwork_ack);
}
net->flags = htons(net->flags);
net->seq = htonl(cur_seq++);
net->checksum = 0;
net->checksum = ntohs(do_csum(data, len));
return send_netmsg(m, data, len);
}
static int valid_checksum(void *data, unsigned int len)
{
struct nlnetwork *net = data;
unsigned short checksum, tmp;
checksum = ntohs(net->checksum);
/* no checksum, skip */
if (!checksum)
return 1;
net->checksum = 0;
tmp = do_csum(data, len);
return tmp == checksum;
}
int mcast_recv_netmsg(struct mcast_sock *m, void *data, int len)
{
int ret;
struct nlnetwork *net = data;
struct nlmsghdr *nlh = data + sizeof(struct nlnetwork);
struct nfgenmsg *nfhdr;
ret = mcast_recv(m, net, len);
if (ret <= 0)
return ret;
/* message too small: no room for the header */
if (ret < sizeof(struct nlnetwork))
return -1;
if (ntohs(net->flags) & NET_HELLO)
STATE_SYNC(last_seq_recv) = ntohl(net->seq) - 1;
if (ntohs(net->flags) & NET_NACK || ntohs(net->flags) & NET_ACK) {
struct nlnetwork_ack *nack = (struct nlnetwork_ack *) net;
/* message too small: no room for the header */
if (ret < sizeof(struct nlnetwork_ack))
return -1;
if (!valid_checksum(data, ret))
return -1;
/* host byte order conversion */
net->flags = ntohs(net->flags);
net->seq = ntohl(net->seq);
/* acknowledgement conversion */
nack->from = ntohl(nack->from);
nack->to = ntohl(nack->to);
return ret;
}
if (ntohs(net->flags) & NET_RESYNC) {
if (!valid_checksum(data, ret))
return -1;
/* host byte order conversion */
net->flags = ntohs(net->flags);
net->seq = ntohl(net->seq);
return ret;
}
/* information received is too small */
if (ret < NLMSG_SPACE(sizeof(struct nfgenmsg)))
return -1;
/* information received and message length does not match */
if (ret != ntohl(nlh->nlmsg_len) + sizeof(struct nlnetwork))
return -1;
/* this message does not come from ctnetlink */
if (NFNL_SUBSYS_ID(ntohs(nlh->nlmsg_type)) != NFNL_SUBSYS_CTNETLINK)
return -1;
nfhdr = NLMSG_DATA(nlh);
/* only AF_INET and AF_INET6 are supported */
if (nfhdr->nfgen_family != AF_INET &&
nfhdr->nfgen_family != AF_INET6)
return -1;
/* only process message coming from nfnetlink v0 */
if (nfhdr->version != NFNETLINK_V0)
return -1;
if (!valid_checksum(data, ret))
return -1;
/* host byte order conversion */
net->flags = ntohs(net->flags);
net->seq = ntohl(net->seq);
if (nlh_network2host(nlh) == -1)
return -1;
return ret;
}
int mcast_track_seq(u_int32_t seq, u_int32_t *exp_seq)
{
static int seq_set = 0;
int ret = 1;
/* netlink sequence tracking initialization */
if (!seq_set) {
seq_set = 1;
goto out;
}
/* fast path: we received the correct sequence */
if (seq == STATE_SYNC(last_seq_recv)+1)
goto out;
/* out of sequence: some messages got lost */
if (seq > STATE_SYNC(last_seq_recv)+1) {
STATE_SYNC(packets_lost) += seq-STATE_SYNC(last_seq_recv)+1;
ret = 0;
goto out;
}
/* out of sequence: replayed or sequence wrapped around issues */
if (seq < STATE_SYNC(last_seq_recv)+1) {
/*
* Check if the sequence has wrapped around.
* Perhaps it can be a replayed packet.
*/
if (STATE_SYNC(last_seq_recv)+1-seq > ~0U/2) {
/*
* Indeed, it is a wrapped around
*/
STATE_SYNC(packets_lost) +=
~0U-STATE_SYNC(last_seq_recv)+1+seq;
} else {
/*
* It is a delayed packet
*/
dlog(STATE(log), "delayed packet? exp=%u rcv=%u",
STATE_SYNC(last_seq_recv)+1, seq);
}
ret = 0;
}
out:
*exp_seq = STATE_SYNC(last_seq_recv)+1;
/* update expected sequence */
STATE_SYNC(last_seq_recv) = seq;
return ret;
}
int build_network_msg(const int msg_type,
struct nfnl_subsys_handle *ssh,
struct nf_conntrack *ct,
void *buffer,
unsigned int size)
{
memset(buffer, 0, size);
buffer += sizeof(struct nlnetwork);
return nfct_build_query(ssh, msg_type, ct, buffer, size);
}
unsigned int parse_network_msg(struct nf_conntrack *ct,
const struct nlmsghdr *nlh)
{
/*
* The parsing of netlink messages going through network is
* similar to the one that is done for messages coming from
* kernel, therefore do not replicate more code and use the
* function provided in the libraries.
*
* Yup, this is a hack 8)
*/
return nfct_parse_conntrack(NFCT_T_ALL, nlh, ct);
}
|