flowtop: Remove unused parameters from draw_help() and draw_footer()
[netsniff-ng.git] / trafgen_l3.c
blobcbdbe6c018fe60121089960efc2a658c7e29cdaf
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;
56 uint8_t ihl;
58 ihl = proto_field_get_u8(hdr, IP4_IHL);
59 csum = htons(calc_csum(&pkt->payload[hdr->pkt_offset], ihl * 4));
60 proto_field_set_u16(hdr, IP4_CSUM, bswap_16(csum));
64 static void ipv4_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
66 uint8_t ip_proto;
68 switch(pid) {
69 case PROTO_IP4:
70 ip_proto = IPPROTO_IPIP;
71 break;
72 case PROTO_UDP:
73 ip_proto = IPPROTO_UDP;
74 break;
75 case PROTO_TCP:
76 ip_proto = IPPROTO_TCP;
77 break;
78 default:
79 bug();
82 proto_field_set_default_u8(hdr, IP4_PROTO, ip_proto);
85 static struct proto_hdr ipv4_hdr = {
86 .id = PROTO_IP4,
87 .layer = PROTO_L3,
88 .header_init = ipv4_header_init,
89 .packet_finish = ipv4_packet_finish,
90 .set_next_proto = ipv4_set_next_proto,
93 static struct proto_field ipv6_fields[] = {
94 { .id = IP6_VER, .len = 4, .offset = 0, .shift = 28, .mask = 0xf0000000 },
95 { .id = IP6_CLASS, .len = 4, .offset = 0, .shift = 20, .mask = 0x0ff00000 },
96 { .id = IP6_FLOW_LBL, .len = 4, .offset = 0, .shift = 0, .mask = 0x000fffff },
97 { .id = IP6_LEN, .len = 2, .offset = 4 },
98 { .id = IP6_NEXT_HDR, .len = 1, .offset = 6 },
99 { .id = IP6_HOP_LIMIT, .len = 1, .offset = 7 },
100 { .id = IP6_SADDR, .len = 16, .offset = 8 },
101 { .id = IP6_DADDR, .len = 16, .offset = 24 },
104 static void ipv6_header_init(struct proto_hdr *hdr)
106 proto_lower_default_add(hdr, PROTO_ETH);
108 proto_header_fields_add(hdr, ipv6_fields, array_size(ipv6_fields));
110 proto_field_set_default_be32(hdr, IP6_VER, 6);
111 proto_field_set_default_dev_ipv6(hdr, IP6_SADDR);
114 #define IPV6_HDR_LEN 40
116 static void ipv6_packet_finish(struct proto_hdr *hdr)
118 struct packet *pkt = current_packet();
119 uint16_t total_len = pkt->len - hdr->pkt_offset - IPV6_HDR_LEN;
121 proto_field_set_default_be16(hdr, IP6_LEN, total_len);
124 static void ipv6_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
126 uint8_t ip_proto;
128 switch(pid) {
129 case PROTO_ICMP6:
130 ip_proto = IPPROTO_ICMPV6;
131 break;
132 case PROTO_UDP:
133 ip_proto = IPPROTO_UDP;
134 break;
135 case PROTO_TCP:
136 ip_proto = IPPROTO_TCP;
137 break;
138 default:
139 bug();
142 proto_field_set_default_u8(hdr, IP6_NEXT_HDR, ip_proto);
145 static struct proto_hdr ipv6_hdr = {
146 .id = PROTO_IP6,
147 .layer = PROTO_L3,
148 .header_init = ipv6_header_init,
149 .packet_finish = ipv6_packet_finish,
150 .set_next_proto = ipv6_set_next_proto,
153 void protos_l3_init(void)
155 proto_header_register(&ipv4_hdr);
156 proto_header_register(&ipv6_hdr);