]> The Tcpdump Group git mirrors - tcpdump/blob - print-nflog.c
238226e13a384ed1130becbe6bfcb771a2275bca
[tcpdump] / print-nflog.c
1 /*
2 * Copyright (c) 2013, Petar Alilovic,
3 * Faculty of Electrical Engineering and Computing, University of Zagreb
4 * All rights reserved
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25 * DAMAGE.
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <tcpdump-stdinc.h>
33
34 #include <stdio.h>
35 #include <pcap.h>
36
37 #include "netdissect.h"
38 #include "interface.h"
39
40 #include "nflog.h"
41
42 #ifdef DLT_NFLOG
43
44 #define NFULA_PAYLOAD 9
45 #define NFULA_MAX 17
46
47 const struct tok nflog_values[] = {
48 { AF_INET, "IPv4" },
49 { AF_INET6, "IPv6" },
50 { 0, NULL }
51 };
52
53 static inline void
54 nflog_hdr_print(struct netdissect_options *ndo, const u_char *bp, u_int length)
55 {
56 const nflog_hdr_t *hdr;
57 hdr = (const nflog_hdr_t *)bp;
58
59 ND_PRINT((ndo, "version %d, resource ID %d", hdr->nflog_version, ntohs(hdr->nflog_rid)));
60
61 if (!ndo->ndo_qflag) {
62 ND_PRINT((ndo,", family %s (%d)",
63 tok2str(nflog_values, "Unknown",
64 hdr->nflog_family),
65 hdr->nflog_family));
66 } else {
67 ND_PRINT((ndo,", %s",
68 tok2str(nflog_values,
69 "Unknown NFLOG (0x%02x)",
70 hdr->nflog_family)));
71 }
72
73 ND_PRINT((ndo, ", length %u: ", length));
74 }
75
76 static void
77 nflog_print(struct netdissect_options *ndo, const u_char *p, u_int length, u_int caplen)
78 {
79 const nflog_hdr_t *hdr;
80 const nflog_tlv_t *tlv;
81 u_int16_t size;
82
83 if (caplen < (int) sizeof(nflog_hdr_t)) {
84 ND_PRINT((ndo, "[|nflog]"));
85 return;
86 }
87
88 if (ndo->ndo_eflag)
89 nflog_hdr_print(ndo, p, length);
90
91 length -= sizeof(nflog_hdr_t);
92 caplen -= sizeof(nflog_hdr_t);
93 hdr = (const nflog_hdr_t *)p;
94 p += sizeof(nflog_hdr_t);
95
96 do {
97 tlv = (const nflog_tlv_t *) p;
98 size = tlv->tlv_length;
99
100 /* wrong size of the packet */
101 if (size > length )
102 return;
103
104 /* wrong tlv type */
105 if (tlv->tlv_type > NFULA_MAX)
106 return;
107
108 if (size % 4 != 0)
109 size += 4 - size % 4;
110
111 p += size;
112 length = length - size;
113 caplen = caplen - size;
114
115 } while (tlv->tlv_type != NFULA_PAYLOAD);
116
117 /* dont skip payload just tlv length and type */
118 p = p - size + 4;
119 length += size - 4;
120 caplen += size - 4;
121
122 switch (hdr->nflog_family) {
123
124 case AF_INET:
125 ip_print(ndo, p, length);
126 break;
127
128 #ifdef INET6
129 case AF_INET6:
130 ip6_print(ndo, p, length);
131 break;
132 #endif /*INET6*/
133
134 default:
135 if (!ndo->ndo_eflag)
136 nflog_hdr_print(ndo, (u_char *)hdr,
137 length + sizeof(nflog_hdr_t));
138
139 if (!ndo->ndo_suppress_default_print)
140 ndo->ndo_default_print(ndo, p, caplen);
141 break;
142 }
143 }
144
145 u_int
146 nflog_if_print(struct netdissect_options *ndo,
147 const struct pcap_pkthdr *h, const u_char *p)
148 {
149
150 nflog_print(ndo, p, h->len, h->caplen);
151 return (sizeof(nflog_hdr_t));
152 }
153
154 #endif /* DLT_NFLOG */