1 /* $Id$ */
2 /****************************************************************************
3 *
4 * Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved.
5 * Copyright (C) 2005-2013 Sourcefire, Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License Version 2 as
9 * published by the Free Software Foundation. You may not use, modify or
10 * distribute this program under any other version of the GNU General
11 * Public License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 *
22 ****************************************************************************/
23
24 // @file encode.h
25 // @author Russ Combs <rcombs@sourcefire.com>
26
27 #ifndef __ENCODE_H__
28 #define __ENCODE_H__
29
30 #include "decode.h"
31
32 extern Packet *encode_pkt;
33 extern uint64_t total_rebuilt_pkts;
34
35 void Encode_Init(void);
36 void Encode_Term(void);
37
38 typedef enum {
39 ENC_TCP_FIN, ENC_TCP_RST,
40 ENC_UNR_NET, ENC_UNR_HOST,
41 ENC_UNR_PORT, ENC_UNR_FW,
42 ENC_TCP_PUSH, ENC_UDP,
43 ENC_MAX
44 } EncodeType;
45
46 #define ENC_FLAG_FWD 0x80000000 // send in forward direction
47 #define ENC_FLAG_SEQ 0x40000000 // VAL bits contain seq adj
48 #define ENC_FLAG_ID 0x20000000 // use randomized IP ID
49 #define ENC_FLAG_NET 0x10000000 // stop after innermost network (ip4/6) layer
50 #define ENC_FLAG_DEF 0x08000000 // stop before innermost ip4 opts or ip6 frag header
51 #define ENC_FLAG_RAW 0x04000000 // don't encode outer eth header (this is raw ip)
52 #define ENC_FLAG_RST_CLNT 0x02000000 // finish with a client RST packet
53 #define ENC_FLAG_RST_SRVR 0x01000000 // finish with a server RST packet
54 #define ENC_FLAG_VAL 0x00FFFFFF // bits for adjusting seq and/or ack
55
56 typedef uint32_t EncodeFlags;
57
58 // 255 is max pseudo-random flush point; eth mtu ensures that maximum flushes
59 // are not trimmed which throws off the tracking total in stream5_paf.c
60 #define MAXIMUM_PAF_MAX (IP_MAXPACKET - ETHERNET_MTU - 255)
61
62 // ICMPv6 Reason codes...ones used by encode are defined here now, ICMP ones
63 // are defind in header files from dnet, but none exist for ICMPv6, if these
64 // become defined in a system header file those should be used and these removed
65 #define ICMP6_UNREACH_NET 0x00
66 #define ICMP6_UNREACH_HOST 0x03
67 #define ICMP6_UNREACH_PORT 0x04
68 #define ICMP6_UNREACH_FILTER_PROHIB 0x01
69
70 // orig must be the current packet from the interface to
71 // ensure proper encoding (not the reassembled packet).
72 // len is number of bytes in the encoded packet upon return
73 // (or 0 if the returned pointer is null).
74 const uint8_t* Encode_Reject(
75 EncodeType, EncodeFlags, const Packet* orig, uint32_t* len);
76
77 const uint8_t* Encode_Response(
78 EncodeType, EncodeFlags, const Packet* orig, uint32_t* len,
79 const uint8_t* payLoad, uint32_t payLen);
80
81 // allocate a Packet for later formatting (cloning)
82 Packet* Encode_New(void);
83
84 // release the allocated Packet
85 void Encode_Delete(Packet*);
86
87
88 // orig is the wire pkt; clone was obtained with New()
89 int Encode_Format(EncodeFlags, const Packet* orig, Packet* clone, PseudoPacketType);
90
91 #ifdef HAVE_DAQ_ADDRESS_SPACE_ID
92 int Encode_Format_With_DAQ_Info (
93 EncodeFlags f, const Packet* p, Packet* c, PseudoPacketType type,
94 const DAQ_PktHdr_t*, uint32_t opaque);
95
96 #elif defined(HAVE_DAQ_ACQUIRE_WITH_META)
97 int Encode_Format_With_DAQ_Info (
98 EncodeFlags f, const Packet* p, Packet* c, PseudoPacketType type,
99 uint32_t opaque);
100 #endif
101
102 // update length and checksum fields in layers and caplen, etc.
103 void Encode_Update(Packet*);
104
105 // Set the destination MAC address
106 void Encode_SetDstMAC(uint8_t* );
107
Encode_SetPkt(Packet * p)108 static inline void Encode_SetPkt(Packet* p)
109 {
110 encode_pkt = p;
111 }
112
Encode_GetPkt(void)113 static inline Packet* Encode_GetPkt(void)
114 {
115 return encode_pkt;
116 }
117
Encode_Reset(void)118 static inline void Encode_Reset(void)
119 {
120 Encode_SetPkt(NULL);
121 }
122
UpdateRebuiltPktCount(void)123 static inline void UpdateRebuiltPktCount(void)
124 {
125 total_rebuilt_pkts++;
126 }
127
GetRebuiltPktCount(void)128 static inline uint64_t GetRebuiltPktCount(void)
129 {
130 return total_rebuilt_pkts;
131 }
132
Encode_GetMaxPayload(Packet * p)133 static inline uint16_t Encode_GetMaxPayload(Packet* p)
134 {
135 Layer l;
136
137 if(!p->next_layer)
138 return 0;
139
140 l = p->layers[p->next_layer - 1];
141 return ETHERNET_MTU - (l.start - p->layers[0].start) - l.length;
142 }
143
144
145 #endif // __ENCODE_H__
146
147