2 * netsniff-ng - the packet sniffing beast
3 * Subject to the GPL, version 2.
6 #include <net/if_arp.h>
7 #include <linux/if_ether.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
)
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 const struct proto_ops eth_proto_ops
= {
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
= proto_lower_default_add(hdr
, PROTO_ETH
);
71 uint16_t lower_etype
= 0;
73 proto_header_fields_add(hdr
, vlan_fields
, array_size(vlan_fields
));
75 if (lower
->ops
->id
== PROTO_ETH
)
76 lower_etype
= proto_field_get_u16(lower
, ETH_TYPE
);
77 else if (lower
->ops
->id
== PROTO_VLAN
)
78 lower_etype
= proto_field_get_u16(lower
, VLAN_ETYPE
);
80 proto_field_set_be16(hdr
, VLAN_ETYPE
, lower_etype
);
81 proto_field_set_default_be16(hdr
, VLAN_TPID
, pid_to_eth(hdr
->ops
->id
));
84 static void vlan_set_next_proto(struct proto_hdr
*hdr
, enum proto_id pid
)
86 if (pid
!= PROTO_VLAN
)
87 proto_field_set_be16(hdr
, VLAN_ETYPE
, pid_to_eth(pid
));
90 static const struct proto_ops vlan_proto_ops
= {
93 .header_init
= vlan_header_init
,
94 .set_next_proto
= vlan_set_next_proto
,
97 static struct proto_field arp_fields
[] = {
98 { .id
= ARP_HTYPE
, .len
= 2 },
99 { .id
= ARP_PTYPE
, .len
= 2, .offset
= 2 },
100 { .id
= ARP_HLEN
, .len
= 1, .offset
= 4 },
101 { .id
= ARP_PLEN
, .len
= 1, .offset
= 5 },
102 { .id
= ARP_OPER
, .len
= 2, .offset
= 6 },
103 { .id
= ARP_SHA
, .len
= 6, .offset
= 8 },
104 { .id
= ARP_SPA
, .len
= 4, .offset
= 14 },
105 { .id
= ARP_THA
, .len
= 6, .offset
= 18 },
106 { .id
= ARP_TPA
, .len
= 4, .offset
= 24 },
109 static void arp_header_init(struct proto_hdr
*hdr
)
111 struct proto_hdr
*lower
= proto_lower_default_add(hdr
, PROTO_ETH
);
113 if (lower
->ops
->id
== PROTO_ETH
) {
114 const uint8_t bcast
[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
116 proto_field_set_default_bytes(lower
, ETH_DST_ADDR
, bcast
);
119 proto_header_fields_add(hdr
, arp_fields
, array_size(arp_fields
));
121 /* Generate Announce request by default */
122 proto_field_set_default_be16(hdr
, ARP_HTYPE
, ARPHRD_ETHER
);
123 proto_field_set_default_be16(hdr
, ARP_PTYPE
, ETH_P_IP
);
124 proto_field_set_default_u8(hdr
, ARP_HLEN
, 6);
125 proto_field_set_default_u8(hdr
, ARP_PLEN
, 4);
126 proto_field_set_default_be16(hdr
, ARP_OPER
, ARPOP_REQUEST
);
127 proto_field_set_default_dev_mac(hdr
, ARP_SHA
);
128 proto_field_set_default_dev_ipv4(hdr
, ARP_SPA
);
129 proto_field_set_default_dev_ipv4(hdr
, ARP_TPA
);
132 static const struct proto_ops arp_proto_ops
= {
135 .header_init
= arp_header_init
,
138 static struct proto_field mpls_fields
[] = {
139 { .id
= MPLS_LABEL
, .len
= 4, .shift
= 12, .mask
= 0xfffff000 },
140 { .id
= MPLS_TC
, .len
= 4, .shift
= 9, .mask
= 0xe00 },
141 { .id
= MPLS_LAST
, .len
= 4, .shift
= 8, .mask
= 0x100 },
142 { .id
= MPLS_TTL
, .len
= 4, .shift
= 0, .mask
= 0xff },
145 static void mpls_header_init(struct proto_hdr
*hdr
)
147 proto_lower_default_add(hdr
, PROTO_ETH
);
149 proto_header_fields_add(hdr
, mpls_fields
, array_size(mpls_fields
));
151 proto_field_set_default_be32(hdr
, MPLS_LAST
, 1);
154 static void mpls_set_next_proto(struct proto_hdr
*hdr
, enum proto_id pid
)
156 if (pid
== PROTO_MPLS
)
157 proto_field_set_default_be32(hdr
, MPLS_LAST
, 0);
160 static const struct proto_ops mpls_proto_ops
= {
163 .header_init
= mpls_header_init
,
164 .set_next_proto
= mpls_set_next_proto
,
167 void protos_l2_init(void)
169 proto_ops_register(ð_proto_ops
);
170 proto_ops_register(&vlan_proto_ops
);
171 proto_ops_register(&arp_proto_ops
);
172 proto_ops_register(&mpls_proto_ops
);