ifpps: fix iface stat parsing if uppercase
[netsniff-ng-new.git] / trafgen_l3.c
blob9d0d9306225c39c0b5cecef009c043910e57f9dd
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Subject to the GPL, version 2.
4 */
6 #include <net/if_arp.h>
8 #include "die.h"
9 #include "dev.h"
10 #include "csum.h"
11 #include "built_in.h"
12 #include "trafgen_l2.h"
13 #include "trafgen_l3.h"
14 #include "trafgen_proto.h"
15 #include "trafgen_conf.h"
17 static struct proto_field ipv4_fields[] = {
18 { .id = IP4_VER, .len = 1, .offset = 0, .shift = 4, .mask = 0xf0 },
19 { .id = IP4_IHL, .len = 1, .offset = 0, .shift = 0, .mask = 0x0f },
20 { .id = IP4_DSCP, .len = 1, .offset = 1, .shift = 2, .mask = 0xfc },
21 { .id = IP4_ECN, .len = 1, .offset = 1, .shift = 0, .mask = 0x03 },
22 { .id = IP4_TOS, .len = 1, .offset = 1 },
23 { .id = IP4_LEN, .len = 2, .offset = 2 },
24 { .id = IP4_ID, .len = 2, .offset = 4 },
25 { .id = IP4_FLAGS, .len = 2, .offset = 6, .shift = 13, .mask = 0xe000 },
26 { .id = IP4_MF, .len = 2, .offset = 6, .shift = 13, .mask = 0x2000 },
27 { .id = IP4_DF, .len = 2, .offset = 6, .shift = 14, .mask = 0x4000 },
28 { .id = IP4_FRAG_OFFS, .len = 2, .offset = 6, .shift = 0, .mask = 0x1fff },
29 { .id = IP4_TTL, .len = 1, .offset = 8 },
30 { .id = IP4_PROTO, .len = 1, .offset = 9 },
31 { .id = IP4_CSUM, .len = 2, .offset = 10 },
32 { .id = IP4_SADDR, .len = 4, .offset = 12 },
33 { .id = IP4_DADDR, .len = 4, .offset = 16 },
36 static void ipv4_header_init(struct proto_hdr *hdr)
38 struct dev_io *dev = proto_dev_get();
40 /* In case of tun interface we do not need to create Ethernet header */
41 if (!dev_io_is_netdev(dev) || device_type(dev_io_name_get(dev)) != ARPHRD_NONE)
42 proto_lower_default_add(hdr, PROTO_ETH);
44 proto_header_fields_add(hdr, ipv4_fields, array_size(ipv4_fields));
46 proto_hdr_field_set_default_u8(hdr, IP4_VER, 4);
47 proto_hdr_field_set_default_u8(hdr, IP4_IHL, 5);
48 proto_hdr_field_set_default_dev_ipv4(hdr, IP4_SADDR);
51 static void ipv4_field_changed(struct proto_field *field)
53 field->hdr->is_csum_valid = false;
55 if (field->id == IP4_SADDR || field->id == IP4_DADDR) {
56 struct proto_hdr *upper = proto_upper_header(field->hdr);
58 if (upper && (upper->ops->id == PROTO_UDP || upper->ops->id == PROTO_TCP))
59 upper->is_csum_valid = false;
63 static void ipv4_csum_update(struct proto_hdr *hdr)
65 struct packet *pkt;
66 uint16_t csum;
68 if (hdr->is_csum_valid)
69 return;
70 if (proto_hdr_field_is_set(hdr, IP4_CSUM))
71 return;
73 pkt = packet_get(hdr->pkt_id);
75 proto_hdr_field_set_default_u16(hdr, IP4_CSUM, 0);
76 csum = htons(calc_csum(&pkt->payload[hdr->pkt_offset], hdr->len));
77 proto_hdr_field_set_default_u16(hdr, IP4_CSUM, bswap_16(csum));
79 hdr->is_csum_valid = true;
82 static void ipv4_packet_finish(struct proto_hdr *hdr)
84 struct packet *pkt = proto_hdr_packet(hdr);
85 uint16_t total_len;
87 total_len = pkt->len - hdr->pkt_offset;
88 proto_hdr_field_set_default_be16(hdr, IP4_LEN, total_len);
90 ipv4_csum_update(hdr);
93 static void ipv4_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
95 uint8_t ip_proto;
97 switch (pid) {
98 case PROTO_IP4:
99 ip_proto = IPPROTO_IPIP;
100 break;
101 case PROTO_IP6:
102 ip_proto = IPPROTO_IPV6;
103 break;
104 case PROTO_ICMP4:
105 ip_proto = IPPROTO_ICMP;
106 break;
107 case PROTO_UDP:
108 ip_proto = IPPROTO_UDP;
109 break;
110 case PROTO_TCP:
111 ip_proto = IPPROTO_TCP;
112 break;
113 default:
114 bug();
117 proto_hdr_field_set_default_u8(hdr, IP4_PROTO, ip_proto);
120 static enum proto_id ipv4_get_next_proto(struct proto_hdr *hdr)
122 switch (proto_hdr_field_get_u8(hdr, IP4_PROTO)) {
123 case IPPROTO_IPIP:
124 return PROTO_IP4;
125 case IPPROTO_IPV6:
126 return PROTO_IP6;
127 case IPPROTO_ICMP:
128 return PROTO_ICMP4;
129 case IPPROTO_UDP:
130 return PROTO_UDP;
131 case IPPROTO_TCP:
132 return PROTO_TCP;
133 default:
134 return __PROTO_MAX;
138 static const struct proto_ops ipv4_proto_ops = {
139 .id = PROTO_IP4,
140 .layer = PROTO_L3,
141 .header_init = ipv4_header_init,
142 .packet_update = ipv4_csum_update,
143 .field_changed = ipv4_field_changed,
144 .packet_finish = ipv4_packet_finish,
145 .set_next_proto = ipv4_set_next_proto,
146 .get_next_proto = ipv4_get_next_proto,
149 static struct proto_field ipv6_fields[] = {
150 { .id = IP6_VER, .len = 4, .offset = 0, .shift = 28, .mask = 0xf0000000 },
151 { .id = IP6_CLASS, .len = 4, .offset = 0, .shift = 20, .mask = 0x0ff00000 },
152 { .id = IP6_FLOW_LBL, .len = 4, .offset = 0, .shift = 0, .mask = 0x000fffff },
153 { .id = IP6_LEN, .len = 2, .offset = 4 },
154 { .id = IP6_NEXT_HDR, .len = 1, .offset = 6 },
155 { .id = IP6_HOP_LIMIT, .len = 1, .offset = 7 },
156 { .id = IP6_SADDR, .len = 16, .offset = 8 },
157 { .id = IP6_DADDR, .len = 16, .offset = 24 },
160 static void ipv6_header_init(struct proto_hdr *hdr)
162 struct dev_io *dev = proto_dev_get();
164 /* In case of tun interface we do not need to create Ethernet header */
165 if (dev_io_is_netdev(dev) && device_type(dev_io_name_get(dev)) != ARPHRD_NONE)
166 proto_lower_default_add(hdr, PROTO_ETH);
168 proto_header_fields_add(hdr, ipv6_fields, array_size(ipv6_fields));
170 proto_hdr_field_set_default_be32(hdr, IP6_VER, 6);
171 proto_hdr_field_set_default_dev_ipv6(hdr, IP6_SADDR);
174 static void ipv6_field_changed(struct proto_field *field)
176 if (field->id == IP6_SADDR || field->id == IP6_DADDR) {
177 struct proto_hdr *upper = proto_upper_header(field->hdr);
179 if (upper && (upper->ops->id == PROTO_UDP || upper->ops->id == PROTO_TCP))
180 upper->is_csum_valid = false;
184 #define IPV6_HDR_LEN 40
186 static void ipv6_packet_finish(struct proto_hdr *hdr)
188 struct packet *pkt = proto_hdr_packet(hdr);
189 uint16_t total_len = pkt->len - hdr->pkt_offset - IPV6_HDR_LEN;
191 proto_hdr_field_set_default_be16(hdr, IP6_LEN, total_len);
194 static void ipv6_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
196 uint8_t ip_proto;
198 switch (pid) {
199 case PROTO_ICMP6:
200 ip_proto = IPPROTO_ICMPV6;
201 break;
202 case PROTO_UDP:
203 ip_proto = IPPROTO_UDP;
204 break;
205 case PROTO_TCP:
206 ip_proto = IPPROTO_TCP;
207 break;
208 default:
209 bug();
212 proto_hdr_field_set_default_u8(hdr, IP6_NEXT_HDR, ip_proto);
215 static enum proto_id ipv6_get_next_proto(struct proto_hdr *hdr)
217 switch (proto_hdr_field_get_u8(hdr, IP6_NEXT_HDR)) {
218 case IPPROTO_ICMPV6:
219 return PROTO_IP4;
220 case IPPROTO_UDP:
221 return PROTO_UDP;
222 case IPPROTO_TCP:
223 return PROTO_TCP;
224 default:
225 return __PROTO_MAX;
229 static const struct proto_ops ipv6_proto_ops = {
230 .id = PROTO_IP6,
231 .layer = PROTO_L3,
232 .header_init = ipv6_header_init,
233 .field_changed = ipv6_field_changed,
234 .packet_finish = ipv6_packet_finish,
235 .set_next_proto = ipv6_set_next_proto,
236 .get_next_proto = ipv6_get_next_proto,
239 void protos_l3_init(void)
241 proto_ops_register(&ipv4_proto_ops);
242 proto_ops_register(&ipv6_proto_ops);