trafgen: proto: Reference to packet from struct proto_hdr
[netsniff-ng.git] / trafgen_l3.c
blobad582704b3934f5c0f1748a4ff6dd4ddecbbc2d7
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Subject to the GPL, version 2.
4 */
6 #include <linux/if_ether.h>
8 #include "die.h"
9 #include "csum.h"
10 #include "built_in.h"
11 #include "trafgen_l2.h"
12 #include "trafgen_l3.h"
13 #include "trafgen_proto.h"
14 #include "trafgen_conf.h"
16 static struct proto_field ipv4_fields[] = {
17 { .id = IP4_VER, .len = 1, .offset = 0, .shift = 4, .mask = 0xf0 },
18 { .id = IP4_IHL, .len = 1, .offset = 0, .shift = 0, .mask = 0x0f },
19 { .id = IP4_DSCP, .len = 1, .offset = 1, .shift = 2, .mask = 0xfc },
20 { .id = IP4_ECN, .len = 1, .offset = 1, .shift = 0, .mask = 0x03 },
21 { .id = IP4_TOS, .len = 1, .offset = 1 },
22 { .id = IP4_LEN, .len = 2, .offset = 2 },
23 { .id = IP4_ID, .len = 2, .offset = 4 },
24 { .id = IP4_FLAGS, .len = 2, .offset = 6, .shift = 13, .mask = 0xe000 },
25 { .id = IP4_MF, .len = 2, .offset = 6, .shift = 13, .mask = 0x2000 },
26 { .id = IP4_DF, .len = 2, .offset = 6, .shift = 14, .mask = 0x4000 },
27 { .id = IP4_FRAG_OFFS, .len = 2, .offset = 6, .shift = 0, .mask = 0x1fff },
28 { .id = IP4_TTL, .len = 1, .offset = 8 },
29 { .id = IP4_PROTO, .len = 1, .offset = 9 },
30 { .id = IP4_CSUM, .len = 2, .offset = 10 },
31 { .id = IP4_SADDR, .len = 4, .offset = 12 },
32 { .id = IP4_DADDR, .len = 4, .offset = 16 },
35 static void ipv4_header_init(struct proto_hdr *hdr)
37 proto_lower_default_add(hdr, PROTO_ETH);
39 proto_header_fields_add(hdr, ipv4_fields, array_size(ipv4_fields));
41 proto_field_set_default_u8(hdr, IP4_VER, 4);
42 proto_field_set_default_u8(hdr, IP4_IHL, 5);
43 proto_field_set_default_dev_ipv4(hdr, IP4_SADDR);
46 static void ipv4_packet_finish(struct proto_hdr *hdr)
48 struct packet *pkt = current_packet();
49 uint16_t total_len;
51 total_len = pkt->len - hdr->pkt_offset;
52 proto_field_set_default_be16(hdr, IP4_LEN, total_len);
54 if (!proto_field_is_set(hdr, IP4_CSUM)) {
55 uint16_t csum;
57 csum = htons(calc_csum(&pkt->payload[hdr->pkt_offset], hdr->len));
58 proto_field_set_u16(hdr, IP4_CSUM, bswap_16(csum));
62 static void ipv4_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
64 uint8_t ip_proto;
66 switch(pid) {
67 case PROTO_IP4:
68 ip_proto = IPPROTO_IPIP;
69 break;
70 case PROTO_IP6:
71 ip_proto = IPPROTO_IPV6;
72 break;
73 case PROTO_ICMP4:
74 ip_proto = IPPROTO_ICMP;
75 break;
76 case PROTO_UDP:
77 ip_proto = IPPROTO_UDP;
78 break;
79 case PROTO_TCP:
80 ip_proto = IPPROTO_TCP;
81 break;
82 default:
83 bug();
86 proto_field_set_default_u8(hdr, IP4_PROTO, ip_proto);
89 static struct proto_hdr ipv4_hdr = {
90 .id = PROTO_IP4,
91 .layer = PROTO_L3,
92 .header_init = ipv4_header_init,
93 .packet_finish = ipv4_packet_finish,
94 .set_next_proto = ipv4_set_next_proto,
97 static struct proto_field ipv6_fields[] = {
98 { .id = IP6_VER, .len = 4, .offset = 0, .shift = 28, .mask = 0xf0000000 },
99 { .id = IP6_CLASS, .len = 4, .offset = 0, .shift = 20, .mask = 0x0ff00000 },
100 { .id = IP6_FLOW_LBL, .len = 4, .offset = 0, .shift = 0, .mask = 0x000fffff },
101 { .id = IP6_LEN, .len = 2, .offset = 4 },
102 { .id = IP6_NEXT_HDR, .len = 1, .offset = 6 },
103 { .id = IP6_HOP_LIMIT, .len = 1, .offset = 7 },
104 { .id = IP6_SADDR, .len = 16, .offset = 8 },
105 { .id = IP6_DADDR, .len = 16, .offset = 24 },
108 static void ipv6_header_init(struct proto_hdr *hdr)
110 proto_lower_default_add(hdr, PROTO_ETH);
112 proto_header_fields_add(hdr, ipv6_fields, array_size(ipv6_fields));
114 proto_field_set_default_be32(hdr, IP6_VER, 6);
115 proto_field_set_default_dev_ipv6(hdr, IP6_SADDR);
118 #define IPV6_HDR_LEN 40
120 static void ipv6_packet_finish(struct proto_hdr *hdr)
122 struct packet *pkt = current_packet();
123 uint16_t total_len = pkt->len - hdr->pkt_offset - IPV6_HDR_LEN;
125 proto_field_set_default_be16(hdr, IP6_LEN, total_len);
128 static void ipv6_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
130 uint8_t ip_proto;
132 switch(pid) {
133 case PROTO_ICMP6:
134 ip_proto = IPPROTO_ICMPV6;
135 break;
136 case PROTO_UDP:
137 ip_proto = IPPROTO_UDP;
138 break;
139 case PROTO_TCP:
140 ip_proto = IPPROTO_TCP;
141 break;
142 default:
143 bug();
146 proto_field_set_default_u8(hdr, IP6_NEXT_HDR, ip_proto);
149 static struct proto_hdr ipv6_hdr = {
150 .id = PROTO_IP6,
151 .layer = PROTO_L3,
152 .header_init = ipv6_header_init,
153 .packet_finish = ipv6_packet_finish,
154 .set_next_proto = ipv6_set_next_proto,
157 void protos_l3_init(void)
159 proto_header_register(&ipv4_hdr);
160 proto_header_register(&ipv6_hdr);