summaryrefslogtreecommitdiffstats
path: root/src/network.c
blob: 78be1e2f74bf944d1632f091110c0edc1e9a0fe6 (plain)
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
/*
 * (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"
#include "log.h"
#include "debug.h"

#include <stdlib.h>
#include <time.h>
#include <string.h>

static unsigned int seq_set, cur_seq;

static size_t __do_send(struct mcast_sock *m, void *data, size_t len)
{
	struct nethdr *net = data;

	debug("send sq: %u fl:%u len:%u\n",
		ntohl(net->seq), net->flags, ntohs(net->len));

	return mcast_send(m, net, len);
}

static size_t __do_prepare(struct mcast_sock *m, void *data, size_t len)
{
	struct nethdr *net = data;

	if (!seq_set) {
		seq_set = 1;
		cur_seq = time(NULL);
	}
	net->version = CONNTRACKD_PROTOCOL_VERSION;
	net->len = len;
	net->seq = cur_seq++;
	HDR_HOST2NETWORK(net);

	return len;
}

static size_t __prepare_ctl(struct mcast_sock *m, void *data)
{
	return __do_prepare(m, data, NETHDR_ACK_SIZ);
}

static size_t __prepare_data(struct mcast_sock *m, void *data)
{
	struct nethdr *net = (struct nethdr *) data;
	struct netpld *pld = NETHDR_DATA(net);

	return __do_prepare(m, data, ntohs(pld->len) + NETPLD_SIZ + NETHDR_SIZ);
}

size_t prepare_send_netmsg(struct mcast_sock *m, void *data)
{
	int ret = 0;
	struct nethdr *net = (struct nethdr *) data;

	if (IS_DATA(net))
		ret = __prepare_data(m, data);
	else if (IS_CTL(net))
		ret = __prepare_ctl(m, data);

	return ret;
}

static size_t tx_buflenmax;
static size_t tx_buflen = 0;
static char *tx_buf;

#define HEADERSIZ 28 /* IP header (20 bytes) + UDP header 8 (bytes) */

int mcast_buffered_init(int if_mtu)
{
	int mtu = if_mtu - HEADERSIZ;

	/* default to Ethernet MTU 1500 bytes */
	if (if_mtu == 0)
		mtu = 1500 - HEADERSIZ;

	tx_buf = malloc(mtu);
	if (tx_buf == NULL)
		return -1;

	tx_buflenmax = mtu;

	return 0;
}

void mcast_buffered_destroy(void)
{
	free(tx_buf);
}

/* return 0 if it is not sent, otherwise return 1 */
int mcast_buffered_send_netmsg(struct mcast_sock *m, void *data, size_t len)
{
	int ret = 0;
	struct nethdr *net = data;

retry:
	if (tx_buflen + len < tx_buflenmax) {
		memcpy(tx_buf + tx_buflen, net, len);
		tx_buflen += len;
	} else {
		__do_send(m, tx_buf, tx_buflen);
		ret = 1;
		tx_buflen = 0;
		goto retry;
	}

	return ret;
}

ssize_t mcast_buffered_pending_netmsg(struct mcast_sock *m)
{
	ssize_t ret;

	if (tx_buflen == 0)
		return 0;

	ret = __do_send(m, tx_buf, tx_buflen);
	tx_buflen = 0;

	return ret;
}

int mcast_send_netmsg(struct mcast_sock *m, void *data)
{
	int ret;
	size_t len = prepare_send_netmsg(m, data);

	ret = mcast_buffered_send_netmsg(m, data, len);
	mcast_buffered_pending_netmsg(m);

	return ret;
}

void build_netmsg(struct nf_conntrack *ct, int query, struct nethdr *net)
{
	struct netpld *pld = NETHDR_DATA(net);

	build_netpld(ct, pld, query);
}

static int local_seq_set = 0;

/* this function only tracks, it does not update the last sequence received */
int mcast_track_seq(uint32_t seq, uint32_t *exp_seq)
{
	int ret = SEQ_UNKNOWN;

	/* netlink sequence tracking initialization */
	if (!local_seq_set) {
		ret = SEQ_UNSET;
		goto out;
	}

	/* fast path: we received the correct sequence */
	if (seq == STATE_SYNC(last_seq_recv)+1) {
		ret = SEQ_IN_SYNC;
		goto out;
	}

	/* out of sequence: some messages got lost */
	if (after(seq, STATE_SYNC(last_seq_recv)+1)) {
		STATE_SYNC(packets_lost) += seq-STATE_SYNC(last_seq_recv)+1;
		ret = SEQ_AFTER;
		goto out;
	}

	/* out of sequence: replayed/delayed packet? */
	if (before(seq, STATE_SYNC(last_seq_recv)+1))
		ret = SEQ_BEFORE;

out:
	*exp_seq = STATE_SYNC(last_seq_recv)+1;

	return ret;
}

void mcast_track_update_seq(uint32_t seq)
{
	if (!local_seq_set)
		local_seq_set = 1;

	STATE_SYNC(last_seq_recv) = seq;
}

int mcast_track_is_seq_set()
{
	return local_seq_set;
}