2 * netsniff-ng - the packet sniffing beast
3 * Subject to the GPL, version 2.
6 #include <linux/if_ether.h>
10 #include "trafgen_l2.h"
11 #include "trafgen_l3.h"
12 #include "trafgen_proto.h"
13 #include "trafgen_conf.h"
15 static struct proto_field ipv4_fields
[] = {
16 { .id
= IP4_VER
, .len
= 1, .offset
= 0, .shift
= 4, .mask
= 0xf0 },
17 { .id
= IP4_IHL
, .len
= 1, .offset
= 0, .shift
= 0, .mask
= 0x0f },
18 { .id
= IP4_DSCP
, .len
= 1, .offset
= 1, .shift
= 2, .mask
= 0xfc },
19 { .id
= IP4_ECN
, .len
= 1, .offset
= 1, .shift
= 0, .mask
= 0x03 },
20 { .id
= IP4_TOS
, .len
= 1, .offset
= 1 },
21 { .id
= IP4_LEN
, .len
= 2, .offset
= 2 },
22 { .id
= IP4_ID
, .len
= 2, .offset
= 4 },
23 { .id
= IP4_FLAGS
, .len
= 2, .offset
= 6, .shift
= 13, .mask
= 0xe000 },
24 { .id
= IP4_MF
, .len
= 2, .offset
= 6, .shift
= 13, .mask
= 0x2000 },
25 { .id
= IP4_DF
, .len
= 2, .offset
= 6, .shift
= 14, .mask
= 0x4000 },
26 { .id
= IP4_FRAG_OFFS
, .len
= 2, .offset
= 6, .shift
= 0, .mask
= 0x1fff },
27 { .id
= IP4_TTL
, .len
= 1, .offset
= 8 },
28 { .id
= IP4_PROTO
, .len
= 1, .offset
= 9 },
29 { .id
= IP4_CSUM
, .len
= 2, .offset
= 10 },
30 { .id
= IP4_SADDR
, .len
= 4, .offset
= 12 },
31 { .id
= IP4_DADDR
, .len
= 4, .offset
= 16 },
34 static void ipv4_header_init(struct proto_hdr
*hdr
)
36 struct proto_hdr
*lower
;
38 proto_lower_default_add(PROTO_ETH
);
40 lower
= proto_current_header();
42 if (lower
->id
== PROTO_ETH
)
43 proto_field_set_default_be16(lower
, ETH_PROTO_ID
, ETH_P_IP
);
44 else if (lower
->id
== PROTO_IP4
)
45 proto_field_set_default_u8(lower
, IP4_PROTO
, IPPROTO_IPIP
);
47 proto_header_fields_add(hdr
, ipv4_fields
, array_size(ipv4_fields
));
49 proto_field_set_default_u8(hdr
, IP4_VER
, 4);
50 proto_field_set_default_u8(hdr
, IP4_IHL
, 5);
51 proto_field_set_default_dev_ipv4(hdr
, IP4_SADDR
);
54 static void ipv4_packet_finish(struct proto_hdr
*hdr
)
56 struct packet
*pkt
= current_packet();
59 total_len
= pkt
->len
- hdr
->pkt_offset
;
60 proto_field_set_default_be16(hdr
, IP4_LEN
, total_len
);
62 if (!proto_field_is_set(hdr
, IP4_CSUM
)) {
66 ihl
= proto_field_get_u8(hdr
, IP4_IHL
);
67 csum
= htons(calc_csum(&pkt
->payload
[hdr
->pkt_offset
], ihl
* 4));
68 proto_field_set_u16(hdr
, IP4_CSUM
, bswap_16(csum
));
72 static struct proto_hdr ipv4_hdr
= {
75 .header_init
= ipv4_header_init
,
76 .packet_finish
= ipv4_packet_finish
,
79 void protos_l3_init(void)
81 proto_header_register(&ipv4_hdr
);