25 #ifndef SURICATA_DECODE_TCP_H
26 #define SURICATA_DECODE_TCP_H
28 #define TCP_HEADER_LEN 20
29 #define TCP_OPTLENMAX 40
47 #define TCP_OPT_EOL 0x00
48 #define TCP_OPT_NOP 0x01
49 #define TCP_OPT_MSS 0x02
50 #define TCP_OPT_WS 0x03
51 #define TCP_OPT_SACKOK 0x04
52 #define TCP_OPT_SACK 0x05
53 #define TCP_OPT_TS 0x08
54 #define TCP_OPT_TFO 0x22
55 #define TCP_OPT_EXP1 0xfd
56 #define TCP_OPT_EXP2 0xfe
57 #define TCP_OPT_MD5 0x13
58 #define TCP_OPT_AO 0x1d
60 #define TCP_OPT_SACKOK_LEN 2
61 #define TCP_OPT_WS_LEN 3
62 #define TCP_OPT_TS_LEN 10
63 #define TCP_OPT_MSS_LEN 4
64 #define TCP_OPT_SACK_MIN_LEN 10
65 #define TCP_OPT_SACK_MAX_LEN 34
66 #define TCP_OPT_TFO_MIN_LEN 4
67 #define TCP_OPT_TFO_MAX_LEN 18
70 #define TCP_WSCALE_MAX 14
72 #define TCP_GET_RAW_OFFSET(tcph) (((tcph)->th_offx2 & 0xf0) >> 4)
73 #define TCP_GET_RAW_HLEN(tcph) ((uint8_t)(TCP_GET_RAW_OFFSET((tcph)) << 2))
74 #define TCP_GET_RAW_X2(tcph) (unsigned char)((tcph)->th_offx2 & 0x0f)
75 #define TCP_GET_RAW_SRC_PORT(tcph) SCNtohs((tcph)->th_sport)
76 #define TCP_GET_RAW_DST_PORT(tcph) SCNtohs((tcph)->th_dport)
78 #define TCP_SET_RAW_TCP_OFFSET(tcph, value) ((tcph)->th_offx2 = (unsigned char)(((tcph)->th_offx2 & 0x0f) | (value << 4)))
79 #define TCP_SET_RAW_TCP_X2(tcph, value) ((tcph)->th_offx2 = (unsigned char)(((tcph)->th_offx2 & 0xf0) | (value & 0x0f)))
81 #define TCP_GET_RAW_SEQ(tcph) SCNtohl((tcph)->th_seq)
82 #define TCP_GET_RAW_ACK(tcph) SCNtohl((tcph)->th_ack)
84 #define TCP_GET_RAW_WINDOW(tcph) SCNtohs((tcph)->th_win)
85 #define TCP_GET_RAW_URG_POINTER(tcph) SCNtohs((tcph)->th_urp)
86 #define TCP_GET_RAW_SUM(tcph) SCNtohs((tcph)->th_sum)
89 #define TCP_GET_TSVAL(p) ((p)->l4.vars.tcp.ts_val)
92 #define TCP_GET_TSECR(p) ((p)->l4.vars.tcp.ts_ecr)
94 #define TCP_HAS_WSCALE(p) ((p)->l4.vars.tcp.wscale_set)
95 #define TCP_HAS_SACK(p) (p)->l4.vars.tcp.sack_set
96 #define TCP_HAS_TS(p) ((p)->l4.vars.tcp.ts_set)
97 #define TCP_HAS_MSS(p) ((p)->l4.vars.tcp.mss_set)
98 #define TCP_HAS_TFO(p) ((p)->l4.vars.tcp.tfo_set)
101 #define TCP_GET_WSCALE(p) (p)->l4.vars.tcp.wscale
103 #define TCP_GET_SACKOK(p) (p)->l4.vars.tcp.sack_ok
104 #define TCP_GET_SACK_PTR(p, tcph) ((uint8_t *)(tcph)) + (p)->l4.vars.tcp.sack_offset
105 #define TCP_GET_SACK_CNT(p) (p)->l4.vars.tcp.sack_cnt
106 #define TCP_GET_MSS(p) (p)->l4.vars.tcp.mss
108 #define TCP_GET_OFFSET(p) TCP_GET_RAW_OFFSET((p)->tcph)
109 #define TCP_GET_X2(p) TCP_GET_RAW_X2((p)->tcph)
110 #define TCP_GET_HLEN(p) ((uint8_t)(TCP_GET_OFFSET((p)) << 2))
111 #define TCP_GET_SRC_PORT(p) TCP_GET_RAW_SRC_PORT((p)->tcph)
112 #define TCP_GET_DST_PORT(p) TCP_GET_RAW_DST_PORT((p)->tcph)
113 #define TCP_GET_SEQ(p) TCP_GET_RAW_SEQ((p)->tcph)
114 #define TCP_GET_ACK(p) TCP_GET_RAW_ACK((p)->tcph)
115 #define TCP_GET_WINDOW(p) TCP_GET_RAW_WINDOW((p)->tcph)
116 #define TCP_GET_URG_POINTER(p) TCP_GET_RAW_URG_POINTER((p)->tcph)
117 #define TCP_GET_SUM(p) TCP_GET_RAW_SUM((p)->tcph)
118 #define TCP_GET_FLAGS(p) (p)->tcph->th_flags
120 #define TCP_ISSET_FLAG_RAW_FIN(p) ((tcph)->th_flags & TH_FIN)
121 #define TCP_ISSET_FLAG_RAW_SYN(p) ((tcph)->th_flags & TH_SYN)
122 #define TCP_ISSET_FLAG_RAW_RST(p) ((tcph)->th_flags & TH_RST)
123 #define TCP_ISSET_FLAG_RAW_PUSH(p) ((tcph)->th_flags & TH_PUSH)
124 #define TCP_ISSET_FLAG_RAW_ACK(p) ((tcph)->th_flags & TH_ACK)
125 #define TCP_ISSET_FLAG_RAW_URG(p) ((tcph)->th_flags & TH_URG)
126 #define TCP_ISSET_FLAG_RAW_RES2(p) ((tcph)->th_flags & TH_RES2)
127 #define TCP_ISSET_FLAG_RAW_RES1(p) ((tcph)->th_flags & TH_RES1)
129 #define TCP_ISSET_FLAG_FIN(p) ((p)->tcph->th_flags & TH_FIN)
130 #define TCP_ISSET_FLAG_SYN(p) ((p)->tcph->th_flags & TH_SYN)
131 #define TCP_ISSET_FLAG_RST(p) ((p)->tcph->th_flags & TH_RST)
132 #define TCP_ISSET_FLAG_PUSH(p) ((p)->tcph->th_flags & TH_PUSH)
133 #define TCP_ISSET_FLAG_ACK(p) ((p)->tcph->th_flags & TH_ACK)
134 #define TCP_ISSET_FLAG_URG(p) ((p)->tcph->th_flags & TH_URG)
135 #define TCP_ISSET_FLAG_RES2(p) ((p)->tcph->th_flags & TH_RES2)
136 #define TCP_ISSET_FLAG_RES1(p) ((p)->tcph->th_flags & TH_RES1)
198 static inline uint16_t TCPChecksum(
199 const uint16_t *shdr,
const uint16_t *pkt, uint16_t tlen, uint16_t init)
202 uint32_t csum = init;
204 csum += shdr[0] + shdr[1] + shdr[2] + shdr[3] + htons(6) + htons(tlen);
206 csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
213 csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
216 pkt[9] + pkt[10] + pkt[11] + pkt[12] + pkt[13] +
223 csum += pkt[0] + pkt[1] + pkt[2] + pkt[3];
229 csum += pkt[0] + pkt[1];
241 *(uint8_t *)(&
pad) = (*(uint8_t *)pkt);
245 csum = (csum >> 16) + (csum & 0x0000FFFF);
246 csum += (csum >> 16);
248 return (uint16_t)~csum;
263 static inline uint16_t TCPV6Checksum(
264 const uint16_t *shdr,
const uint16_t *pkt, uint16_t tlen, uint16_t init)
267 uint32_t csum = init;
269 csum += shdr[0] + shdr[1] + shdr[2] + shdr[3] + shdr[4] + shdr[5] +
270 shdr[6] + shdr[7] + shdr[8] + shdr[9] + shdr[10] + shdr[11] +
271 shdr[12] + shdr[13] + shdr[14] + shdr[15] + htons(6) + htons(tlen);
273 csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
280 csum += pkt[0] + pkt[1] + pkt[2] + pkt[3] + pkt[4] + pkt[5] + pkt[6] +
281 pkt[7] + pkt[8] + pkt[9] + pkt[10] + pkt[11] + pkt[12] + pkt[13] +
288 csum += pkt[0] + pkt[1] + pkt[2] + pkt[3];
294 csum += pkt[0] + pkt[1];
306 *(uint8_t *)(&
pad) = (*(uint8_t *)pkt);
310 csum = (csum >> 16) + (csum & 0x0000FFFF);
311 csum += (csum >> 16);
313 return (uint16_t)~csum;