trafgen: eth: Add setting next protocol id
[netsniff-ng.git] / trafgen_l2.c
blobc0e92a316ed351ee889500e5f71e4e08e277ced2
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 "die.h"
10 #include "built_in.h"
11 #include "trafgen_l2.h"
12 #include "trafgen_proto.h"
14 static struct proto_field eth_fields[] = {
15 { .id = ETH_DST_ADDR, .len = 6, },
16 { .id = ETH_SRC_ADDR, .len = 6, .offset = 6 },
17 { .id = ETH_TYPE, .len = 2, .offset = 12 },
20 static void eth_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
22 uint16_t eth_type;
24 switch(pid) {
25 case PROTO_ARP:
26 eth_type = ETH_P_ARP;
27 break;
28 case PROTO_IP4:
29 eth_type = ETH_P_IP;
30 break;
31 case PROTO_IP6:
32 eth_type = ETH_P_IPV6;
33 break;
34 default:
35 panic("eth: Not supported protocol id %u\n", pid);
38 proto_field_set_default_be16(hdr, ETH_TYPE, eth_type);
41 static void eth_header_init(struct proto_hdr *hdr)
43 proto_header_fields_add(hdr, eth_fields, array_size(eth_fields));
45 proto_field_set_default_dev_mac(hdr, ETH_SRC_ADDR);
48 static struct proto_hdr eth_hdr = {
49 .id = PROTO_ETH,
50 .layer = PROTO_L2,
51 .header_init = eth_header_init,
52 .set_next_proto = eth_set_next_proto,
55 static struct proto_field arp_fields[] = {
56 { .id = ARP_HTYPE, .len = 2 },
57 { .id = ARP_PTYPE, .len = 2, .offset = 2 },
58 { .id = ARP_HLEN, .len = 1, .offset = 4 },
59 { .id = ARP_PLEN, .len = 1, .offset = 5 },
60 { .id = ARP_OPER, .len = 2, .offset = 6 },
61 { .id = ARP_SHA, .len = 6, .offset = 8 },
62 { .id = ARP_SPA, .len = 4, .offset = 14 },
63 { .id = ARP_THA, .len = 6, .offset = 18 },
64 { .id = ARP_TPA, .len = 4, .offset = 24 },
67 static void arp_header_init(struct proto_hdr *hdr)
69 struct proto_hdr *lower;
71 lower = proto_lower_default_add(hdr, PROTO_ETH);
73 if (lower->id == PROTO_ETH) {
74 uint8_t bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
76 proto_field_set_default_bytes(lower, ETH_DST_ADDR, bcast);
79 proto_header_fields_add(hdr, arp_fields, array_size(arp_fields));
81 /* Generate Announce request by default */
82 proto_field_set_default_be16(hdr, ARP_HTYPE, ARPHRD_ETHER);
83 proto_field_set_default_be16(hdr, ARP_PTYPE, ETH_P_IP);
84 proto_field_set_default_u8(hdr, ARP_HLEN, 6);
85 proto_field_set_default_u8(hdr, ARP_PLEN, 4);
86 proto_field_set_default_be16(hdr, ARP_OPER, ARPOP_REQUEST);
87 proto_field_set_default_dev_mac(hdr, ARP_SHA);
88 proto_field_set_default_dev_ipv4(hdr, ARP_SPA);
89 proto_field_set_default_dev_ipv4(hdr, ARP_TPA);
92 static struct proto_hdr arp_hdr = {
93 .id = PROTO_ARP,
94 .layer = PROTO_L2,
95 .header_init = arp_header_init,
98 void protos_l2_init(void)
100 proto_header_register(&eth_hdr);
101 proto_header_register(&arp_hdr);