trafgen: proto: Add IPv6 header generation
[netsniff-ng-new.git] / trafgen_l2.c
blob316d23689e95af05188320d4f7016ef4117ee3c9
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 uint16_t pid_to_eth(enum proto_id pid)
22 switch(pid) {
23 case PROTO_ARP:
24 return ETH_P_ARP;
25 case PROTO_IP4:
26 return ETH_P_IP;
27 case PROTO_IP6:
28 return ETH_P_IPV6;
29 case PROTO_MPLS:
30 return ETH_P_MPLS_UC;
31 case PROTO_VLAN:
32 return ETH_P_8021Q;
33 default:
34 bug();
38 static void eth_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
40 proto_field_set_default_be16(hdr, ETH_TYPE, pid_to_eth(pid));
43 static void eth_header_init(struct proto_hdr *hdr)
45 proto_header_fields_add(hdr, eth_fields, array_size(eth_fields));
47 proto_field_set_default_dev_mac(hdr, ETH_SRC_ADDR);
50 static struct proto_hdr eth_hdr = {
51 .id = PROTO_ETH,
52 .layer = PROTO_L2,
53 .header_init = eth_header_init,
54 .set_next_proto = eth_set_next_proto,
57 static struct proto_field vlan_fields[] = {
58 /* TPID overlaps with Ethernet header and points to ether type */
59 { .id = VLAN_TPID, .len = 2, .offset = -2 },
60 { .id = VLAN_TCI, .len = 2, .offset = 0 },
61 { .id = VLAN_PCP, .len = 2, .offset = 0, .shift = 13, .mask = 0xe000 },
62 { .id = VLAN_DEI, .len = 2, .offset = 0, .shift = 12, .mask = 0x1000 },
63 { .id = VLAN_VID, .len = 2, .offset = 0, .shift = 0, .mask = 0xfff },
64 /* Original ether type is stored after VLAN header */
65 { .id = VLAN_ETYPE, .len = 2, .offset = 2 },
68 static void vlan_header_init(struct proto_hdr *hdr)
70 struct proto_hdr *lower;
71 uint16_t lower_etype = 0;
73 lower = proto_lower_default_add(hdr, PROTO_ETH);
75 proto_header_fields_add(hdr, vlan_fields, array_size(vlan_fields));
77 if (lower->id == PROTO_ETH)
78 lower_etype = proto_field_get_u16(lower, ETH_TYPE);
79 else if (lower->id == PROTO_VLAN)
80 lower_etype = proto_field_get_u16(lower, VLAN_ETYPE);
82 proto_field_set_be16(hdr, VLAN_ETYPE, lower_etype);
83 proto_field_set_default_be16(hdr, VLAN_TPID, pid_to_eth(hdr->id));
86 static void vlan_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
88 if (pid != PROTO_VLAN)
89 proto_field_set_be16(hdr, VLAN_ETYPE, pid_to_eth(pid));
92 static struct proto_hdr vlan_hdr = {
93 .id = PROTO_VLAN,
94 .layer = PROTO_L2,
95 .header_init = vlan_header_init,
96 .set_next_proto = vlan_set_next_proto,
99 static struct proto_field arp_fields[] = {
100 { .id = ARP_HTYPE, .len = 2 },
101 { .id = ARP_PTYPE, .len = 2, .offset = 2 },
102 { .id = ARP_HLEN, .len = 1, .offset = 4 },
103 { .id = ARP_PLEN, .len = 1, .offset = 5 },
104 { .id = ARP_OPER, .len = 2, .offset = 6 },
105 { .id = ARP_SHA, .len = 6, .offset = 8 },
106 { .id = ARP_SPA, .len = 4, .offset = 14 },
107 { .id = ARP_THA, .len = 6, .offset = 18 },
108 { .id = ARP_TPA, .len = 4, .offset = 24 },
111 static void arp_header_init(struct proto_hdr *hdr)
113 struct proto_hdr *lower;
115 lower = proto_lower_default_add(hdr, PROTO_ETH);
117 if (lower->id == PROTO_ETH) {
118 uint8_t bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
120 proto_field_set_default_bytes(lower, ETH_DST_ADDR, bcast);
123 proto_header_fields_add(hdr, arp_fields, array_size(arp_fields));
125 /* Generate Announce request by default */
126 proto_field_set_default_be16(hdr, ARP_HTYPE, ARPHRD_ETHER);
127 proto_field_set_default_be16(hdr, ARP_PTYPE, ETH_P_IP);
128 proto_field_set_default_u8(hdr, ARP_HLEN, 6);
129 proto_field_set_default_u8(hdr, ARP_PLEN, 4);
130 proto_field_set_default_be16(hdr, ARP_OPER, ARPOP_REQUEST);
131 proto_field_set_default_dev_mac(hdr, ARP_SHA);
132 proto_field_set_default_dev_ipv4(hdr, ARP_SPA);
133 proto_field_set_default_dev_ipv4(hdr, ARP_TPA);
136 static struct proto_hdr arp_hdr = {
137 .id = PROTO_ARP,
138 .layer = PROTO_L2,
139 .header_init = arp_header_init,
142 static struct proto_field mpls_fields[] = {
143 { .id = MPLS_LABEL, .len = 4, .shift = 12, .mask = 0xfffff000 },
144 { .id = MPLS_TC, .len = 4, .shift = 9, .mask = 0xe00 },
145 { .id = MPLS_LAST, .len = 4, .shift = 8, .mask = 0x100 },
146 { .id = MPLS_TTL, .len = 4, .shift = 0, .mask = 0xff },
149 static void mpls_header_init(struct proto_hdr *hdr)
151 proto_lower_default_add(hdr, PROTO_ETH);
153 proto_header_fields_add(hdr, mpls_fields, array_size(mpls_fields));
155 proto_field_set_default_be32(hdr, MPLS_LAST, 1);
158 static void mpls_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
160 if (pid == PROTO_MPLS)
161 proto_field_set_default_be32(hdr, MPLS_LAST, 0);
164 static struct proto_hdr mpls_hdr = {
165 .id = PROTO_MPLS,
166 .layer = PROTO_L2,
167 .header_init = mpls_header_init,
168 .set_next_proto = mpls_set_next_proto,
171 void protos_l2_init(void)
173 proto_header_register(&eth_hdr);
174 proto_header_register(&vlan_hdr);
175 proto_header_register(&arp_hdr);
176 proto_header_register(&mpls_hdr);