libmooshika
rmitm.h
Go to the documentation of this file.
1 /* Based on tcpreplay's tcpr.h, itself based from libnet's libnet-headers.h. Thanks. */
2 
3 #include <sys/types.h>
4 #include <netinet/in.h>
5 #include <netinet/in_systm.h>
6 
7 #ifndef _RMITM_H_
8 #define _RMITM_H_
9 
10 #if 0
11 struct in6_addr
12 {
13  union
14  {
15  u_int8_t __u6_addr8[16];
16  u_int16_t __u6_addr16[8];
17  u_int32_t __u6_addr32[4];
18  } __u6_addr; /* 128-bit IP6 address */
19 };
20 #define s6_addr __u6_addr.__u6_addr8
21 #define s6_addr8 __u6_addr.__u6_addr8
22 #define s6_addr16 __u6_addr.__u6_addr16
23 #define s6_addr32 __u6_addr.__u6_addr32
24 #endif
25 
26 /*
27  * IPv6 header
28  * Internet Protocol, version 6
29  * Static header size: 40 bytes
30  */
31 struct ipv6_hdr
32 {
33  u_int8_t ip_flags[4]; /* version, traffic class, flow label */
34  u_int16_t ip_len; /* total length */
35  u_int8_t ip_nh; /* next header */
36  u_int8_t ip_hl; /* hop limit */
37  struct in6_addr ip_src, ip_dst; /* source and dest address */
38 };
39 
40 /*
41  * tcp header structure. This is the minimal header (20 bytes)
42  */
43 
44 typedef struct tcp_hdr {
45  u_int16_t th_sport;
46  u_int16_t th_dport;
47  u_int32_t th_seq_nr;
48  u_int32_t th_ack_nr;
49  u_int8_t th_data_off;
50  u_int8_t th_flags;
51  u_int16_t th_window;
52  u_int16_t th_sum;
53  u_int16_t th_urgptr;
54 } tcp_hdr_t;
55 
56 #define TH_DO_MASK 0xf0
57 
58 #define TH_FLAGS_MASK 0x3f
59 #define THF_FIN 0x1
60 #define THF_SYN 0x2
61 #define THF_RST 0x4
62 #define THF_PSH 0x8
63 #define THF_ACK 0x10
64 #define THF_URG 0x20
65 
66 struct pkt_hdr {
67  struct ipv6_hdr ipv6;
68  struct tcp_hdr tcp;
69  uint8_t data[0];
70 };
71 
72 #define PACKET_HDR_LEN sizeof(struct pkt_hdr)
73 
74 #define CHECKSUM_CARRY(x) do { \
75  x = ((x & 0xffff) + (x >> 16)); \
76  if (x > 0xffff) \
77  x -= 0xffff; \
78 } while (0)
79 
80 static inline uint16_t checksum(u_int16_t *data, int len) {
81  uint32_t sum = 0;
82  union {
83  int16_t s;
84  u_int8_t b[2];
85  } pad;
86 
87  while (len > 1) {
88  sum += *data++;
89  len -= 2;
90  if (sum >= 0x10000)
91  sum -= 0xffff;
92  }
93 
94  if (len == 1) {
95  pad.b[0] = *(u_int8_t *)data;
96  pad.b[1] = 0;
97  sum += pad.s;
98  }
99 
100  CHECKSUM_CARRY(sum);
101  return sum;
102 }
103 
104 static inline void ipv6_tcp_checksum(struct pkt_hdr *hdr) {
105  uint32_t sum;
106  hdr->tcp.th_sum = 0;
107 
108  sum = checksum((uint16_t*)&hdr->ipv6.ip_src, 2*sizeof(struct in6_addr));
109  sum += htons(IPPROTO_TCP + ntohs(hdr->ipv6.ip_len));
110  sum += checksum((uint16_t*)&hdr->tcp, ntohs(hdr->ipv6.ip_len));
111 
112  CHECKSUM_CARRY(sum);
113  hdr->tcp.th_sum = ((~sum) & 0xffff);
114 }
115 
116 static inline int min(int a, int b) {
117  return (a < b) ? a: b;
118 }
119 static inline int max(int a, int b) {
120  return (a < b) ? b: a;
121 }
122 
123 #endif /* _RMITM_H_ */
u_int8_t th_data_off
Definition: rmitm.h:49
uint8_t data[0]
Definition: rmitm.h:69
struct in6_addr ip_src ip_dst
Definition: rmitm.h:37
struct ipv6_hdr ipv6
Definition: rmitm.h:67
u_int16_t th_sport
Definition: rmitm.h:45
u_int8_t ip_flags[4]
Definition: rmitm.h:33
Definition: rmitm.h:66
u_int8_t ip_hl
Definition: rmitm.h:36
Definition: rmitm.h:31
struct tcp_hdr tcp
Definition: rmitm.h:68
u_int16_t th_urgptr
Definition: rmitm.h:53
u_int32_t th_ack_nr
Definition: rmitm.h:48
u_int16_t th_sum
Definition: rmitm.h:52
u_int8_t ip_nh
Definition: rmitm.h:35
u_int16_t ip_len
Definition: rmitm.h:34
u_int16_t th_window
Definition: rmitm.h:51
u_int8_t th_flags
Definition: rmitm.h:50
Definition: rmitm.h:44
#define CHECKSUM_CARRY(x)
Definition: rmitm.h:74
u_int32_t th_seq_nr
Definition: rmitm.h:47
u_int16_t th_dport
Definition: rmitm.h:46
struct tcp_hdr tcp_hdr_t