make: fix build error in curvetun due to empty env var
[netsniff-ng.git] / src / proto_ipv6.c
blobf682fbe44527a804cb669cf19b20bde1a3b1fc80
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Copyright 2010 Emmanuel Roullit.
6 * Subject to the GPL, version 2.
7 */
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <netinet/in.h> /* for ntohs() */
12 #include <arpa/inet.h> /* for inet_ntop() */
13 #include <asm/byteorder.h>
15 #include "proto.h"
16 #include "protos.h"
17 #include "csum.h"
18 #include "dissector_eth.h"
19 #include "pkt_buff.h"
20 #include "built_in.h"
23 * IPv6 fixed header
25 * BEWARE, it is incorrect. The first 4 bits of flow_lbl
26 * are glued to priority now, forming "class".
28 struct ipv6hdr {
29 #if defined(__LITTLE_ENDIAN_BITFIELD)
30 __extension__ uint8_t priority:4,
31 version:4;
32 #elif defined(__BIG_ENDIAN_BITFIELD)
33 __extension__ uint8_t version:4,
34 priority:4;
35 #else
36 # error "Please fix <asm/byteorder.h>"
37 #endif
38 uint8_t flow_lbl[3];
39 uint16_t payload_len;
40 uint8_t nexthdr;
41 uint8_t hop_limit;
42 struct in6_addr saddr;
43 struct in6_addr daddr;
44 } __packed;
46 extern void ipv6(struct pkt_buff *pkt);
47 extern void ipv6_less(struct pkt_buff *pkt);
49 void ipv6(struct pkt_buff *pkt)
51 uint8_t traffic_class;
52 uint32_t flow_label;
53 char src_ip[INET6_ADDRSTRLEN];
54 char dst_ip[INET6_ADDRSTRLEN];
55 struct ipv6hdr *ip = (struct ipv6hdr *) pkt_pull(pkt, sizeof(*ip));
57 if (ip == NULL)
58 return;
60 traffic_class = (ip->priority << 4) |
61 ((ip->flow_lbl[0] & 0xF0) >> 4);
62 flow_label = ((ip->flow_lbl[0] & 0x0F) << 8) |
63 (ip->flow_lbl[1] << 4) | ip->flow_lbl[2];
65 inet_ntop(AF_INET6, &ip->saddr, src_ip, sizeof(src_ip));
66 inet_ntop(AF_INET6, &ip->daddr, dst_ip, sizeof(dst_ip));
68 tprintf(" [ IPv6 ");
69 tprintf("Addr (%s => %s), ", src_ip, dst_ip);
70 tprintf("Version (%u), ", ip->version);
71 tprintf("TrafficClass (%u), ", traffic_class);
72 tprintf("FlowLabel (%u), ", flow_label);
73 tprintf("Len (%u), ", ntohs(ip->payload_len));
74 tprintf("NextHdr (%u), ", ip->nexthdr);
75 tprintf("HopLimit (%u)", ip->hop_limit);
76 tprintf(" ]\n");
78 pkt_set_proto(pkt, &eth_lay3, ip->nexthdr);
81 void ipv6_less(struct pkt_buff *pkt)
83 char src_ip[INET6_ADDRSTRLEN];
84 char dst_ip[INET6_ADDRSTRLEN];
85 struct ipv6hdr *ip = (struct ipv6hdr *) pkt_pull(pkt, sizeof(*ip));
87 if (ip == NULL)
88 return;
90 inet_ntop(AF_INET6, &ip->saddr, src_ip, sizeof(src_ip));
91 inet_ntop(AF_INET6, &ip->daddr, dst_ip, sizeof(dst_ip));
93 tprintf(" %s/%s Len %u", src_ip, dst_ip,
94 ntohs(ip->payload_len));
96 pkt_set_proto(pkt, &eth_lay3, ip->nexthdr);
99 struct protocol ipv6_ops = {
100 .key = 0x86DD,
101 .print_full = ipv6,
102 .print_less = ipv6_less,
105 EXPORT_SYMBOL(ipv6_ops);