summaryrefslogtreecommitdiff
path: root/src/network.c
diff options
context:
space:
mode:
authorAlexander Wirt <formorer@debian.org>2012-06-03 08:49:55 +0200
committerAlexander Wirt <formorer@debian.org>2012-06-03 08:49:55 +0200
commitceecc8855af313c14e8a164f1cd0399716174398 (patch)
tree0d58f5cf7075dea5ff7ddeff6f9a3c89d9eb6352 /src/network.c
parent10f2c00aa6ef875e7998838c200681c6ea5eeebe (diff)
parentea27bb406e3d8fe9466ba274af38e6f540ff5bfc (diff)
downloadconntrack-tools-ceecc8855af313c14e8a164f1cd0399716174398.tar.gz
conntrack-tools-ceecc8855af313c14e8a164f1cd0399716174398.zip
Merge tag 'upstream/1.2.1'
Upstream version 1.2.1
Diffstat (limited to 'src/network.c')
-rw-r--r--src/network.c139
1 files changed, 139 insertions, 0 deletions
diff --git a/src/network.c b/src/network.c
new file mode 100644
index 0000000..13db37c
--- /dev/null
+++ b/src/network.c
@@ -0,0 +1,139 @@
+/*
+ * (C) 2006-2011 by Pablo Neira Ayuso <pablo@netfilter.org>
+ * (C) 2011 by Vyatta Inc. <http://www.vyatta.com>
+ *
+ * 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 <stdlib.h>
+#include <time.h>
+#include <string.h>
+
+#define NETHDR_ALIGNTO 4
+
+static unsigned int seq_set, cur_seq;
+
+int nethdr_align(int value)
+{
+ return (value + NETHDR_ALIGNTO - 1) & ~(NETHDR_ALIGNTO - 1);
+}
+
+int nethdr_size(int len)
+{
+ return NETHDR_SIZ + len;
+}
+
+static inline void __nethdr_set(struct nethdr *net, int len)
+{
+ if (!seq_set) {
+ seq_set = 1;
+ cur_seq = time(NULL);
+ }
+ net->version = CONNTRACKD_PROTOCOL_VERSION;
+ net->len = len;
+ net->seq = cur_seq++;
+}
+
+void nethdr_set(struct nethdr *net, int type)
+{
+ __nethdr_set(net, NETHDR_SIZ);
+ net->type = type;
+}
+
+void nethdr_set_ack(struct nethdr *net)
+{
+ __nethdr_set(net, NETHDR_ACK_SIZ);
+}
+
+void nethdr_set_ctl(struct nethdr *net)
+{
+ __nethdr_set(net, NETHDR_SIZ);
+}
+
+static int local_seq_set = 0;
+
+/* this function only tracks, it does not update the last sequence received */
+int nethdr_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(error).msg_rcv_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)) {
+ STATE_SYNC(error).msg_rcv_before++;
+ ret = SEQ_BEFORE;
+ }
+
+out:
+ *exp_seq = STATE_SYNC(last_seq_recv)+1;
+
+ return ret;
+}
+
+void nethdr_track_update_seq(uint32_t seq)
+{
+ if (!local_seq_set)
+ local_seq_set = 1;
+
+ STATE_SYNC(last_seq_recv) = seq;
+}
+
+int nethdr_track_is_seq_set()
+{
+ return local_seq_set;
+}
+
+#include "cache.h"
+
+static int status2type[CACHE_T_MAX][C_OBJ_MAX] = {
+ [CACHE_T_CT] = {
+ [C_OBJ_NEW] = NET_T_STATE_CT_NEW,
+ [C_OBJ_ALIVE] = NET_T_STATE_CT_UPD,
+ [C_OBJ_DEAD] = NET_T_STATE_CT_DEL,
+ },
+ [CACHE_T_EXP] = {
+ [C_OBJ_NEW] = NET_T_STATE_EXP_NEW,
+ [C_OBJ_ALIVE] = NET_T_STATE_EXP_UPD,
+ [C_OBJ_DEAD] = NET_T_STATE_EXP_DEL,
+ },
+};
+
+int object_status_to_network_type(struct cache_object *obj)
+{
+ return status2type[obj->cache->type][obj->status];
+}