1/* 2 * (C) 2006-2011 by Pablo Neira Ayuso <pablo@netfilter.org> 3 * (C) 2011 by Vyatta Inc. <http://www.vyatta.com> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 18 */ 19 20#include "conntrackd.h" 21#include "network.h" 22#include "log.h" 23 24#include <stdlib.h> 25#include <time.h> 26#include <string.h> 27 28#define NETHDR_ALIGNTO 4 29 30static unsigned int seq_set, cur_seq; 31 32int nethdr_align(int value) 33{ 34 return (value + NETHDR_ALIGNTO - 1) & ~(NETHDR_ALIGNTO - 1); 35} 36 37int nethdr_size(int len) 38{ 39 return NETHDR_SIZ + len; 40} 41 42static inline void __nethdr_set(struct nethdr *net, int len) 43{ 44 if (!seq_set) { 45 seq_set = 1; 46 cur_seq = time(NULL); 47 } 48 net->version = CONNTRACKD_PROTOCOL_VERSION; 49 net->len = len; 50 net->seq = cur_seq++; 51} 52 53void nethdr_set(struct nethdr *net, int type) 54{ 55 __nethdr_set(net, NETHDR_SIZ); 56 net->type = type; 57} 58 59void nethdr_set_ack(struct nethdr *net) 60{ 61 __nethdr_set(net, NETHDR_ACK_SIZ); 62} 63 64void nethdr_set_ctl(struct nethdr *net) 65{ 66 __nethdr_set(net, NETHDR_SIZ); 67} 68 69static int local_seq_set = 0; 70 71/* this function only tracks, it does not update the last sequence received */ 72int nethdr_track_seq(uint32_t seq, uint32_t *exp_seq) 73{ 74 int ret = SEQ_UNKNOWN; 75 76 /* netlink sequence tracking initialization */ 77 if (!local_seq_set) { 78 ret = SEQ_UNSET; 79 goto out; 80 } 81 82 /* fast path: we received the correct sequence */ 83 if (seq == STATE_SYNC(last_seq_recv)+1) { 84 ret = SEQ_IN_SYNC; 85 goto out; 86 } 87 88 /* out of sequence: some messages got lost */ 89 if (after(seq, STATE_SYNC(last_seq_recv)+1)) { 90 STATE_SYNC(error).msg_rcv_lost += 91 seq - STATE_SYNC(last_seq_recv) + 1; 92 ret = SEQ_AFTER; 93 goto out; 94 } 95 96 /* out of sequence: replayed/delayed packet? */ 97 if (before(seq, STATE_SYNC(last_seq_recv)+1)) { 98 STATE_SYNC(error).msg_rcv_before++; 99 ret = SEQ_BEFORE; 100 } 101 102out: 103 *exp_seq = STATE_SYNC(last_seq_recv)+1; 104 105 return ret; 106} 107 108void nethdr_track_update_seq(uint32_t seq) 109{ 110 if (!local_seq_set) 111 local_seq_set = 1; 112 113 STATE_SYNC(last_seq_recv) = seq; 114} 115 116int nethdr_track_is_seq_set() 117{ 118 return local_seq_set; 119} 120 121#include "cache.h" 122 123static int status2type[CACHE_T_MAX][C_OBJ_MAX] = { 124 [CACHE_T_CT] = { 125 [C_OBJ_NEW] = NET_T_STATE_CT_NEW, 126 [C_OBJ_ALIVE] = NET_T_STATE_CT_UPD, 127 [C_OBJ_DEAD] = NET_T_STATE_CT_DEL, 128 }, 129 [CACHE_T_EXP] = { 130 [C_OBJ_NEW] = NET_T_STATE_EXP_NEW, 131 [C_OBJ_ALIVE] = NET_T_STATE_EXP_UPD, 132 [C_OBJ_DEAD] = NET_T_STATE_EXP_DEL, 133 }, 134}; 135 136int object_status_to_network_type(struct cache_object *obj) 137{ 138 return status2type[obj->cache->type][obj->status]; 139} 140