trafgen: proto: Add set_next_proto callback to struct proto_hdr
[netsniff-ng-new.git] / trafgen_l2.c
blob529dc363e0418bdac4e6b502ea11393235799a6d
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Subject to the GPL, version 2.
4 */
6 #include <net/if_arp.h>
7 #include <linux/if_ether.h>
9 #include "built_in.h"
10 #include "trafgen_l2.h"
11 #include "trafgen_proto.h"
13 static struct proto_field eth_fields[] = {
14 { .id = ETH_DST_ADDR, .len = 6, },
15 { .id = ETH_SRC_ADDR, .len = 6, .offset = 6 },
16 { .id = ETH_TYPE, .len = 2, .offset = 12 },
19 static void eth_header_init(struct proto_hdr *hdr)
21 proto_header_fields_add(hdr, eth_fields, array_size(eth_fields));
23 proto_field_set_default_dev_mac(hdr, ETH_SRC_ADDR);
26 static struct proto_hdr eth_hdr = {
27 .id = PROTO_ETH,
28 .layer = PROTO_L2,
29 .header_init = eth_header_init,
32 static struct proto_field arp_fields[] = {
33 { .id = ARP_HTYPE, .len = 2 },
34 { .id = ARP_PTYPE, .len = 2, .offset = 2 },
35 { .id = ARP_HLEN, .len = 1, .offset = 4 },
36 { .id = ARP_PLEN, .len = 1, .offset = 5 },
37 { .id = ARP_OPER, .len = 2, .offset = 6 },
38 { .id = ARP_SHA, .len = 6, .offset = 8 },
39 { .id = ARP_SPA, .len = 4, .offset = 14 },
40 { .id = ARP_THA, .len = 6, .offset = 18 },
41 { .id = ARP_TPA, .len = 4, .offset = 24 },
44 static void arp_header_init(struct proto_hdr *hdr)
46 struct proto_hdr *lower;
48 lower = proto_lower_default_add(hdr, PROTO_ETH);
50 if (lower->id == PROTO_ETH) {
51 uint8_t bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
53 proto_field_set_default_bytes(lower, ETH_DST_ADDR, bcast);
54 proto_field_set_default_be16(lower, ETH_TYPE, ETH_P_ARP);
57 proto_header_fields_add(hdr, arp_fields, array_size(arp_fields));
59 /* Generate Announce request by default */
60 proto_field_set_default_be16(hdr, ARP_HTYPE, ARPHRD_ETHER);
61 proto_field_set_default_be16(hdr, ARP_PTYPE, ETH_P_IP);
62 proto_field_set_default_u8(hdr, ARP_HLEN, 6);
63 proto_field_set_default_u8(hdr, ARP_PLEN, 4);
64 proto_field_set_default_be16(hdr, ARP_OPER, ARPOP_REQUEST);
65 proto_field_set_default_dev_mac(hdr, ARP_SHA);
66 proto_field_set_default_dev_ipv4(hdr, ARP_SPA);
67 proto_field_set_default_dev_ipv4(hdr, ARP_TPA);
70 static struct proto_hdr arp_hdr = {
71 .id = PROTO_ARP,
72 .layer = PROTO_L2,
73 .header_init = arp_header_init,
76 void protos_l2_init(void)
78 proto_header_register(&eth_hdr);
79 proto_header_register(&arp_hdr);