1*05b00f60SXin Li /*
2*05b00f60SXin Li * Copyright (c) 1988-1997
3*05b00f60SXin Li * The Regents of the University of California. All rights reserved.
4*05b00f60SXin Li *
5*05b00f60SXin Li * Copyright (c) 1998-2012 Michael Richardson <[email protected]>
6*05b00f60SXin Li * The TCPDUMP project
7*05b00f60SXin Li *
8*05b00f60SXin Li * Redistribution and use in source and binary forms, with or without
9*05b00f60SXin Li * modification, are permitted provided that: (1) source code distributions
10*05b00f60SXin Li * retain the above copyright notice and this paragraph in its entirety, (2)
11*05b00f60SXin Li * distributions including binary code include the above copyright notice and
12*05b00f60SXin Li * this paragraph in its entirety in the documentation or other materials
13*05b00f60SXin Li * provided with the distribution, and (3) all advertising materials mentioning
14*05b00f60SXin Li * features or use of this software display the following acknowledgement:
15*05b00f60SXin Li * ``This product includes software developed by the University of California,
16*05b00f60SXin Li * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
17*05b00f60SXin Li * the University nor the names of its contributors may be used to endorse
18*05b00f60SXin Li * or promote products derived from this software without specific prior
19*05b00f60SXin Li * written permission.
20*05b00f60SXin Li * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
21*05b00f60SXin Li * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22*05b00f60SXin Li * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23*05b00f60SXin Li */
24*05b00f60SXin Li
25*05b00f60SXin Li #ifdef HAVE_CONFIG_H
26*05b00f60SXin Li #include <config.h>
27*05b00f60SXin Li #endif
28*05b00f60SXin Li
29*05b00f60SXin Li #include "netdissect-stdinc.h"
30*05b00f60SXin Li #include "netdissect.h"
31*05b00f60SXin Li #include <string.h>
32*05b00f60SXin Li #include <stdio.h>
33*05b00f60SXin Li #include <stdlib.h>
34*05b00f60SXin Li
35*05b00f60SXin Li #ifdef USE_LIBSMI
36*05b00f60SXin Li #include <smi.h>
37*05b00f60SXin Li #endif
38*05b00f60SXin Li
39*05b00f60SXin Li /*
40*05b00f60SXin Li * Initialize anything that must be initialized before dissecting
41*05b00f60SXin Li * packets.
42*05b00f60SXin Li *
43*05b00f60SXin Li * This should be called at the beginning of the program; it does
44*05b00f60SXin Li * not need to be called, and should not be called, for every
45*05b00f60SXin Li * netdissect_options structure.
46*05b00f60SXin Li */
47*05b00f60SXin Li int
nd_init(char * errbuf,size_t errbuf_size)48*05b00f60SXin Li nd_init(char *errbuf, size_t errbuf_size)
49*05b00f60SXin Li {
50*05b00f60SXin Li #ifdef _WIN32
51*05b00f60SXin Li WORD wVersionRequested;
52*05b00f60SXin Li WSADATA wsaData;
53*05b00f60SXin Li int err;
54*05b00f60SXin Li
55*05b00f60SXin Li /*
56*05b00f60SXin Li * Request Winsock 2.2; we expect Winsock 2.
57*05b00f60SXin Li */
58*05b00f60SXin Li wVersionRequested = MAKEWORD(2, 2);
59*05b00f60SXin Li err = WSAStartup(wVersionRequested, &wsaData);
60*05b00f60SXin Li if (err != 0) {
61*05b00f60SXin Li strlcpy(errbuf, "Attempting to initialize Winsock failed",
62*05b00f60SXin Li errbuf_size);
63*05b00f60SXin Li return (-1);
64*05b00f60SXin Li }
65*05b00f60SXin Li #endif /* _WIN32 */
66*05b00f60SXin Li
67*05b00f60SXin Li #ifdef USE_LIBSMI
68*05b00f60SXin Li /*
69*05b00f60SXin Li * XXX - should we just fail if this fails? Some of the
70*05b00f60SXin Li * libsmi calls may fail.
71*05b00f60SXin Li */
72*05b00f60SXin Li smiInit("tcpdump");
73*05b00f60SXin Li #endif
74*05b00f60SXin Li
75*05b00f60SXin Li /*
76*05b00f60SXin Li * Clears the error buffer, and uses it so we don't get
77*05b00f60SXin Li * "unused argument" warnings at compile time.
78*05b00f60SXin Li */
79*05b00f60SXin Li strlcpy(errbuf, "", errbuf_size);
80*05b00f60SXin Li return (0);
81*05b00f60SXin Li }
82*05b00f60SXin Li
83*05b00f60SXin Li /*
84*05b00f60SXin Li * Clean up anything that ndo_init() did.
85*05b00f60SXin Li */
86*05b00f60SXin Li void
nd_cleanup(void)87*05b00f60SXin Li nd_cleanup(void)
88*05b00f60SXin Li {
89*05b00f60SXin Li #ifdef USE_LIBSMI
90*05b00f60SXin Li /*
91*05b00f60SXin Li * This appears, in libsmi 0.4.8, to do nothing if smiInit()
92*05b00f60SXin Li * wasn't done or failed, so we call it unconditionally.
93*05b00f60SXin Li */
94*05b00f60SXin Li smiExit();
95*05b00f60SXin Li #endif
96*05b00f60SXin Li
97*05b00f60SXin Li #ifdef _WIN32
98*05b00f60SXin Li /*
99*05b00f60SXin Li * Undo the WSAStartup() call above.
100*05b00f60SXin Li */
101*05b00f60SXin Li WSACleanup();
102*05b00f60SXin Li #endif
103*05b00f60SXin Li }
104*05b00f60SXin Li
105*05b00f60SXin Li int
nd_have_smi_support(void)106*05b00f60SXin Li nd_have_smi_support(void)
107*05b00f60SXin Li {
108*05b00f60SXin Li #ifdef USE_LIBSMI
109*05b00f60SXin Li return (1);
110*05b00f60SXin Li #else
111*05b00f60SXin Li return (0);
112*05b00f60SXin Li #endif
113*05b00f60SXin Li }
114*05b00f60SXin Li
115*05b00f60SXin Li /*
116*05b00f60SXin Li * Indicates whether an SMI module has been loaded, so that we can use
117*05b00f60SXin Li * libsmi to translate OIDs.
118*05b00f60SXin Li */
119*05b00f60SXin Li int nd_smi_module_loaded;
120*05b00f60SXin Li
121*05b00f60SXin Li int
nd_load_smi_module(const char * module,char * errbuf,size_t errbuf_size)122*05b00f60SXin Li nd_load_smi_module(const char *module, char *errbuf, size_t errbuf_size)
123*05b00f60SXin Li {
124*05b00f60SXin Li #ifdef USE_LIBSMI
125*05b00f60SXin Li if (smiLoadModule(module) == 0) {
126*05b00f60SXin Li snprintf(errbuf, errbuf_size, "could not load MIB module %s",
127*05b00f60SXin Li module);
128*05b00f60SXin Li return (-1);
129*05b00f60SXin Li }
130*05b00f60SXin Li nd_smi_module_loaded = 1;
131*05b00f60SXin Li return (0);
132*05b00f60SXin Li #else
133*05b00f60SXin Li snprintf(errbuf, errbuf_size, "MIB module %s not loaded: no libsmi support",
134*05b00f60SXin Li module);
135*05b00f60SXin Li return (-1);
136*05b00f60SXin Li #endif
137*05b00f60SXin Li }
138*05b00f60SXin Li
139*05b00f60SXin Li const char *
nd_smi_version_string(void)140*05b00f60SXin Li nd_smi_version_string(void)
141*05b00f60SXin Li {
142*05b00f60SXin Li #ifdef USE_LIBSMI
143*05b00f60SXin Li return (smi_version_string);
144*05b00f60SXin Li #else
145*05b00f60SXin Li return (NULL);
146*05b00f60SXin Li #endif
147*05b00f60SXin Li }
148*05b00f60SXin Li
149*05b00f60SXin Li
150*05b00f60SXin Li int
nd_push_buffer(netdissect_options * ndo,u_char * new_buffer,const u_char * new_packetp,const u_int newlen)151*05b00f60SXin Li nd_push_buffer(netdissect_options *ndo, u_char *new_buffer,
152*05b00f60SXin Li const u_char *new_packetp, const u_int newlen)
153*05b00f60SXin Li {
154*05b00f60SXin Li struct netdissect_saved_packet_info *ndspi;
155*05b00f60SXin Li
156*05b00f60SXin Li ndspi = (struct netdissect_saved_packet_info *)malloc(sizeof(struct netdissect_saved_packet_info));
157*05b00f60SXin Li if (ndspi == NULL)
158*05b00f60SXin Li return (0); /* fail */
159*05b00f60SXin Li ndspi->ndspi_buffer = new_buffer;
160*05b00f60SXin Li ndspi->ndspi_packetp = ndo->ndo_packetp;
161*05b00f60SXin Li ndspi->ndspi_snapend = ndo->ndo_snapend;
162*05b00f60SXin Li ndspi->ndspi_prev = ndo->ndo_packet_info_stack;
163*05b00f60SXin Li
164*05b00f60SXin Li ndo->ndo_packetp = new_packetp;
165*05b00f60SXin Li ndo->ndo_snapend = new_packetp + newlen;
166*05b00f60SXin Li ndo->ndo_packet_info_stack = ndspi;
167*05b00f60SXin Li
168*05b00f60SXin Li return (1); /* success */
169*05b00f60SXin Li }
170*05b00f60SXin Li
171*05b00f60SXin Li
172*05b00f60SXin Li /*
173*05b00f60SXin Li * In a given netdissect_options structure:
174*05b00f60SXin Li *
175*05b00f60SXin Li * push the current packet information onto the packet information
176*05b00f60SXin Li * stack;
177*05b00f60SXin Li *
178*05b00f60SXin Li * given a pointer into the packet and a length past that point in
179*05b00f60SXin Li * the packet, calculate a new snapshot end that's at the lower
180*05b00f60SXin Li * of the current snapshot end and that point in the packet;
181*05b00f60SXin Li *
182*05b00f60SXin Li * set the snapshot end to that new value.
183*05b00f60SXin Li */
184*05b00f60SXin Li int
nd_push_snaplen(netdissect_options * ndo,const u_char * bp,const u_int newlen)185*05b00f60SXin Li nd_push_snaplen(netdissect_options *ndo, const u_char *bp, const u_int newlen)
186*05b00f60SXin Li {
187*05b00f60SXin Li struct netdissect_saved_packet_info *ndspi;
188*05b00f60SXin Li u_int snaplen_remaining;
189*05b00f60SXin Li
190*05b00f60SXin Li ndspi = (struct netdissect_saved_packet_info *)malloc(sizeof(struct netdissect_saved_packet_info));
191*05b00f60SXin Li if (ndspi == NULL)
192*05b00f60SXin Li return (0); /* fail */
193*05b00f60SXin Li ndspi->ndspi_buffer = NULL; /* no new buffer */
194*05b00f60SXin Li ndspi->ndspi_packetp = ndo->ndo_packetp;
195*05b00f60SXin Li ndspi->ndspi_snapend = ndo->ndo_snapend;
196*05b00f60SXin Li ndspi->ndspi_prev = ndo->ndo_packet_info_stack;
197*05b00f60SXin Li
198*05b00f60SXin Li /*
199*05b00f60SXin Li * Push the saved previous data onto the stack.
200*05b00f60SXin Li */
201*05b00f60SXin Li ndo->ndo_packet_info_stack = ndspi;
202*05b00f60SXin Li
203*05b00f60SXin Li /*
204*05b00f60SXin Li * Find out how many bytes remain after the current snapend.
205*05b00f60SXin Li *
206*05b00f60SXin Li * We're restricted to packets with at most UINT_MAX bytes;
207*05b00f60SXin Li * cast the result to u_int, so that we don't get truncation
208*05b00f60SXin Li * warnings on LP64 and LLP64 platforms. (ptrdiff_t is
209*05b00f60SXin Li * signed and we want an unsigned difference; the pointer
210*05b00f60SXin Li * should at most be equal to snapend, and must *never*
211*05b00f60SXin Li * be past snapend.)
212*05b00f60SXin Li */
213*05b00f60SXin Li snaplen_remaining = (u_int)(ndo->ndo_snapend - bp);
214*05b00f60SXin Li
215*05b00f60SXin Li /*
216*05b00f60SXin Li * If the new snapend is smaller than the one calculated
217*05b00f60SXin Li * above, set the snapend to that value, otherwise leave
218*05b00f60SXin Li * it unchanged.
219*05b00f60SXin Li */
220*05b00f60SXin Li if (newlen <= snaplen_remaining) {
221*05b00f60SXin Li /* Snapend isn't past the previous snapend */
222*05b00f60SXin Li ndo->ndo_snapend = bp + newlen;
223*05b00f60SXin Li }
224*05b00f60SXin Li
225*05b00f60SXin Li return (1); /* success */
226*05b00f60SXin Li }
227*05b00f60SXin Li
228*05b00f60SXin Li /*
229*05b00f60SXin Li * In a given netdissect_options structure:
230*05b00f60SXin Li *
231*05b00f60SXin Li * given a pointer into the packet and a length past that point in
232*05b00f60SXin Li * the packet, calculate a new snapshot end that's at the lower
233*05b00f60SXin Li * of the previous snapshot end - or, if there is no previous
234*05b00f60SXin Li * snapshot end, the current snapshot end - and that point in the
235*05b00f60SXin Li * packet;
236*05b00f60SXin Li *
237*05b00f60SXin Li * set the snapshot end to that new value.
238*05b00f60SXin Li *
239*05b00f60SXin Li * This is to change the current snapshot end. This may increase the
240*05b00f60SXin Li * snapshot end, as it may be used, for example, for a Jumbo Payload
241*05b00f60SXin Li * option in IPv6. It must not increase it past the snapshot length
242*05b00f60SXin Li * atop which the current one was pushed, however.
243*05b00f60SXin Li */
244*05b00f60SXin Li void
nd_change_snaplen(netdissect_options * ndo,const u_char * bp,const u_int newlen)245*05b00f60SXin Li nd_change_snaplen(netdissect_options *ndo, const u_char *bp, const u_int newlen)
246*05b00f60SXin Li {
247*05b00f60SXin Li struct netdissect_saved_packet_info *ndspi;
248*05b00f60SXin Li const u_char *previous_snapend;
249*05b00f60SXin Li u_int snaplen_remaining;
250*05b00f60SXin Li
251*05b00f60SXin Li ndspi = ndo->ndo_packet_info_stack;
252*05b00f60SXin Li if (ndspi->ndspi_prev != NULL)
253*05b00f60SXin Li previous_snapend = ndspi->ndspi_prev->ndspi_snapend;
254*05b00f60SXin Li else
255*05b00f60SXin Li previous_snapend = ndo->ndo_snapend;
256*05b00f60SXin Li
257*05b00f60SXin Li /*
258*05b00f60SXin Li * Find out how many bytes remain after the previous
259*05b00f60SXin Li * snapend - or, if there is no previous snapend, after
260*05b00f60SXin Li * the current snapend.
261*05b00f60SXin Li *
262*05b00f60SXin Li * We're restricted to packets with at most UINT_MAX bytes;
263*05b00f60SXin Li * cast the result to u_int, so that we don't get truncation
264*05b00f60SXin Li * warnings on LP64 and LLP64 platforms. (ptrdiff_t is
265*05b00f60SXin Li * signed and we want an unsigned difference; the pointer
266*05b00f60SXin Li * should at most be equal to snapend, and must *never*
267*05b00f60SXin Li * be past snapend.)
268*05b00f60SXin Li */
269*05b00f60SXin Li snaplen_remaining = (u_int)(previous_snapend - bp);
270*05b00f60SXin Li
271*05b00f60SXin Li /*
272*05b00f60SXin Li * If the new snapend is smaller than the one calculated
273*05b00f60SXin Li * above, set the snapend to that value, otherwise leave
274*05b00f60SXin Li * it unchanged.
275*05b00f60SXin Li */
276*05b00f60SXin Li if (newlen <= snaplen_remaining) {
277*05b00f60SXin Li /* Snapend isn't past the previous snapend */
278*05b00f60SXin Li ndo->ndo_snapend = bp + newlen;
279*05b00f60SXin Li }
280*05b00f60SXin Li }
281*05b00f60SXin Li
282*05b00f60SXin Li void
nd_pop_packet_info(netdissect_options * ndo)283*05b00f60SXin Li nd_pop_packet_info(netdissect_options *ndo)
284*05b00f60SXin Li {
285*05b00f60SXin Li struct netdissect_saved_packet_info *ndspi;
286*05b00f60SXin Li
287*05b00f60SXin Li ndspi = ndo->ndo_packet_info_stack;
288*05b00f60SXin Li ndo->ndo_packetp = ndspi->ndspi_packetp;
289*05b00f60SXin Li ndo->ndo_snapend = ndspi->ndspi_snapend;
290*05b00f60SXin Li ndo->ndo_packet_info_stack = ndspi->ndspi_prev;
291*05b00f60SXin Li
292*05b00f60SXin Li free(ndspi->ndspi_buffer);
293*05b00f60SXin Li free(ndspi);
294*05b00f60SXin Li }
295*05b00f60SXin Li
296*05b00f60SXin Li void
nd_pop_all_packet_info(netdissect_options * ndo)297*05b00f60SXin Li nd_pop_all_packet_info(netdissect_options *ndo)
298*05b00f60SXin Li {
299*05b00f60SXin Li while (ndo->ndo_packet_info_stack != NULL)
300*05b00f60SXin Li nd_pop_packet_info(ndo);
301*05b00f60SXin Li }
302