xref: /titanic_41/usr/src/cmd/cmd-inet/usr.sbin/snoop/snoop.h (revision 8d4e547db823a866b8f73efc0acdc423e2963caf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SNOOP_H
28 #define	_SNOOP_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SunOS	*/
31 
32 #include <rpc/types.h>
33 #include <sys/pfmod.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/bufmod.h>
38 #include <net/if.h>
39 #include <netinet/in.h>
40 #include <netinet/if_ether.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/ip.h>
43 #include <netinet/ip6.h>
44 #include <netinet/ip_icmp.h>
45 #include <netinet/icmp6.h>
46 #include <net/pppoe.h>
47 #include <libdlpi.h>
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 /*
54  * Flags to control packet info display
55  */
56 #define	F_NOW		0x00000001	/* display in realtime */
57 #define	F_SUM		0x00000002	/* display summary line */
58 #define	F_ALLSUM	0x00000004	/* display all summary lines */
59 #define	F_DTAIL		0x00000008	/* display detail lines */
60 #define	F_TIME		0x00000010	/* display time */
61 #define	F_ATIME		0x00000020	/* display absolute time */
62 #define	F_RTIME		0x00000040	/* display relative time */
63 #define	F_DROPS		0x00000080	/* display drops */
64 #define	F_LEN		0x00000100	/* display pkt length */
65 #define	F_NUM		0x00000200	/* display pkt number */
66 #define	F_WHO		0x00000400	/* display src/dst */
67 
68 #define	MAXLINE		(1088)		/* max len of detail line */
69 
70 #define	MAX_HDRTRAILER	(64)		/* max hdr/trailer packet slack */
71 
72 /*
73  * The RPC XID cache structure.
74  * When analyzing RPC protocols we
75  * have to cache the xid of the RPC
76  * request together with the program
77  * number, proc, version etc since this
78  * information is missing in the reply
79  * packet.  Using the xid in the reply
80  * we can lookup this previously stashed
81  * information in the cache.
82  *
83  * For RPCSEC_GSS flavor, some special processing is
84  * needed for the argument interpretation based on its
85  * control procedure and service type.  This information
86  * is stored in the cache table during interpretation of
87  * the rpc header and will be referenced later when the rpc
88  * argument is interpreted.
89  */
90 #define	XID_CACHE_SIZE 256
91 struct cache_struct {
92 	int xid_num;	/* RPC transaction id */
93 	int xid_frame;	/* Packet number */
94 	int xid_prog;	/* RPC program number */
95 	int xid_vers;	/* RPC version number */
96 	int xid_proc;	/* RPC procedure number */
97 	unsigned int xid_gss_proc; /* control procedure */
98 	int xid_gss_service; /* none, integ, priv */
99 } xid_cache[XID_CACHE_SIZE];
100 
101 
102 /*
103  * The following macros advance the pointer passed to them.  They
104  * assume they are given a char *.
105  */
106 #define	GETINT8(v, ptr) { \
107 	(v) = (*(ptr)++); \
108 }
109 
110 #define	GETINT16(v, ptr) { \
111 	(v) = *(ptr)++ << 8; \
112 	(v) |= *(ptr)++; \
113 }
114 
115 #define	GETINT32(v, ptr) { \
116 	(v) = *(ptr)++ << 8; \
117 	(v) |= *(ptr)++; (v) <<= 8; \
118 	(v) |= *(ptr)++; (v) <<= 8; \
119 	(v) |= *(ptr)++; \
120 }
121 
122 /*
123  * Used to print nested protocol layers.  For example, an ip datagram included
124  * in an icmp error, or a PPP packet included in an LCP protocol reject..
125  */
126 extern char *prot_nest_prefix;
127 
128 extern char *get_sum_line(void);
129 extern char *get_detail_line(int, int);
130 extern void set_vlan_id(int);
131 extern struct timeval prev_time;
132 extern void process_pkt(struct sb_hdr *, char *, int, int);
133 extern char *getflag(int, int, char *, char *);
134 extern void show_header(char *, char *, int);
135 extern void xdr_init(char *, int);
136 extern char *get_line(int, int);
137 extern int get_line_remain(void);
138 extern char getxdr_char(void);
139 extern char showxdr_char(char *);
140 extern uchar_t getxdr_u_char(void);
141 extern uchar_t showxdr_u_char(char *);
142 extern short getxdr_short(void);
143 extern short showxdr_short(char *);
144 extern ushort_t getxdr_u_short(void);
145 extern ushort_t showxdr_u_short(char *);
146 extern long getxdr_long(void);
147 extern long showxdr_long(char *);
148 extern ulong_t getxdr_u_long(void);
149 extern ulong_t showxdr_u_long(char *);
150 extern longlong_t getxdr_longlong(void);
151 extern longlong_t showxdr_longlong(char *);
152 extern u_longlong_t getxdr_u_longlong(void);
153 extern u_longlong_t showxdr_u_longlong(char *);
154 extern char *getxdr_opaque(char *, int);
155 extern char *getxdr_string(char *, int);
156 extern char *showxdr_string(int, char *);
157 extern char *getxdr_bytes(uint_t *);
158 extern void xdr_skip(int);
159 extern int getxdr_pos(void);
160 extern void setxdr_pos(int);
161 extern char *getxdr_context(char *, int);
162 extern char *showxdr_context(char *);
163 extern enum_t getxdr_enum(void);
164 extern void show_space(void);
165 extern void show_trailer(void);
166 extern char *getxdr_date(void);
167 extern char *showxdr_date(char *);
168 extern char *getxdr_date_ns(void);
169 char *format_time(int64_t sec, uint32_t nsec);
170 extern char *showxdr_date_ns(char *);
171 extern char *getxdr_hex(int);
172 extern char *showxdr_hex(int, char *);
173 extern bool_t getxdr_bool(void);
174 extern bool_t showxdr_bool(char *);
175 extern char *concat_args(char **, int);
176 extern int pf_compile(char *, int);
177 extern void compile(char *, int);
178 extern void load_names(char *);
179 extern void cap_write(struct sb_hdr *, char *, int, int);
180 extern void cap_open_read(const char *);
181 extern void cap_open_write(const char *);
182 extern void cap_read(int, int, int, void (*)(), int);
183 extern void cap_close(void);
184 extern boolean_t check_device(dlpi_handle_t *, char **);
185 extern void initdevice(dlpi_handle_t, ulong_t, ulong_t, struct timeval *,
186     struct Pf_ext_packetfilt *);
187 extern void net_read(dlpi_handle_t, size_t, int, void (*)(), int);
188 extern void click(int);
189 extern void show_pktinfo(int, int, char *, char *, struct timeval *,
190 		struct timeval *, int, int);
191 extern void show_line(char *);
192 extern char *getxdr_time(void);
193 extern char *showxdr_time(char *);
194 extern char *addrtoname(int, const void *);
195 extern char *show_string(const char *, int, int);
196 extern void pr_err(const char *, ...);
197 extern void pr_errdlpi(dlpi_handle_t, const char *, int);
198 extern void check_retransmit(char *, ulong_t);
199 extern char *nameof_prog(int);
200 extern char *getproto(int);
201 extern uint8_t print_ipv6_extensions(int, uint8_t **, uint8_t *, int *, int *);
202 extern void protoprint(int, int, ulong_t, int, int, int, char *, int);
203 extern char *getportname(int, in_port_t);
204 
205 extern void interpret_arp(int, struct arphdr *, int);
206 extern void interpret_bparam(int, int, int, int, int, char *, int);
207 extern void interpret_dns(int, int, const uchar_t *, int);
208 extern void interpret_mount(int, int, int, int, int, char *, int);
209 extern void interpret_nfs(int, int, int, int, int, char *, int);
210 extern void interpret_nfs3(int, int, int, int, int, char *, int);
211 extern void interpret_nfs4(int, int, int, int, int, char *, int);
212 extern void interpret_nfs4_cb(int, int, int, int, int, char *, int);
213 extern void interpret_nfs_acl(int, int, int, int, int, char *, int);
214 extern void interpret_nis(int, int, int, int, int, char *, int);
215 extern void interpret_nisbind(int, int, int, int, int, char *, int);
216 extern void interpret_nisp_cb(int, int, int, int, int, char *, int);
217 extern void interpret_nisplus(int, int, int, int, int, char *, int);
218 extern void interpret_nlm(int, int, int, int, int, char *, int);
219 extern void interpret_pmap(int, int, int, int, int, char *, int);
220 extern int interpret_reserved(int, int, in_port_t, in_port_t, char *, int);
221 extern void interpret_rquota(int, int, int, int, int, char *, int);
222 extern void interpret_rstat(int, int, int, int, int, char *, int);
223 extern void interpret_solarnet_fw(int, int, int, int, int, char *, int);
224 extern void interpret_ldap(int, char *, int, int, int);
225 extern void interpret_icmp(int, struct icmp *, int, int);
226 extern void interpret_icmpv6(int, icmp6_t *, int, int);
227 extern int interpret_ip(int, const struct ip *, int);
228 extern int interpret_ipv6(int, const ip6_t *, int);
229 extern int interpret_ppp(int, uchar_t *, int);
230 extern int interpret_pppoe(int, poep_t *, int);
231 struct tcphdr;
232 extern int interpret_tcp(int, struct tcphdr *, int, int);
233 struct udphdr;
234 extern int interpret_udp(int, struct udphdr *, int, int);
235 extern int interpret_esp(int, uint8_t *, int, int);
236 extern int interpret_ah(int, uint8_t *, int, int);
237 struct sctp_hdr;
238 extern void interpret_sctp(int, struct sctp_hdr *, int, int);
239 extern void interpret_mip_cntrlmsg(int, uchar_t *, int);
240 struct dhcp;
241 extern int interpret_dhcp(int, struct dhcp *, int);
242 extern int interpret_dhcpv6(int, const uint8_t *, int);
243 struct tftphdr;
244 extern int interpret_tftp(int, struct tftphdr *, int);
245 extern int interpret_http(int, char *, int);
246 struct ntpdata;
247 extern int interpret_ntp(int, struct ntpdata *, int);
248 extern void interpret_netbios_ns(int, uchar_t *, int);
249 extern void interpret_netbios_datagram(int, uchar_t *, int);
250 extern void interpret_netbios_ses(int, uchar_t *, int);
251 extern void interpret_slp(int, char *, int);
252 struct rip;
253 extern int interpret_rip(int, struct rip *, int);
254 struct rip6;
255 extern int interpret_rip6(int, struct rip6 *, int);
256 extern int interpret_socks_call(int, char *, int);
257 extern int interpret_socks_reply(int, char *, int);
258 extern void init_ldap(void);
259 extern boolean_t arp_for_ether(char *, struct ether_addr *);
260 extern char *ether_ouiname(uint32_t);
261 extern char *tohex(char *p, int len);
262 extern char *printether(struct ether_addr *);
263 extern char *print_ethertype(int);
264 extern const char *arp_htype(int);
265 
266 /*
267  * Describes characteristics of the Media Access Layer.
268  * The mac_type is one of the supported DLPI media
269  * types (see <sys/dlpi.h>).
270  * The mtu_size is the size of the largest frame.
271  * network_type_offset is where the network type
272  * is located in the link layer header.
273  * The header length is returned by a function to
274  * allow for variable header size - for ethernet it's
275  * just a constant 14 octets.
276  * The interpreter is the function that "knows" how
277  * to interpret the frame.
278  * try_kernel_filter tells snoop to first try a kernel
279  * filter (because the header size is fixed, or if it could
280  * be of variable size where the variable size is easy for a kernel
281  * filter to handle, for example, Ethernet and VLAN tags)
282  * and only use a user space filter if the filter expression
283  * cannot be expressed in kernel space.
284  */
285 typedef struct interface {
286 	uint_t	mac_type;
287 	uint_t	mtu_size;
288 	uint_t  network_type_offset;
289 	uint_t	(*header_len)(char *);
290 	uint_t 	(*interpreter)(int, char *, int, int);
291 	uint_t	try_kernel_filter;
292 } interface_t;
293 
294 extern interface_t INTERFACES[], *interface;
295 extern char *device;
296 
297 extern char *dlc_header;
298 
299 extern char *src_name, *dst_name;
300 
301 extern char *prot_prefix;
302 extern char *prot_nest_prefix;
303 extern char *prot_title;
304 
305 /* Keep track of how many nested IP headers we have. */
306 extern unsigned int encap_levels, total_encap_levels;
307 
308 extern int quitting;
309 
310 /*
311  * Global error recovery routine: used to reset snoop variables after
312  * catastrophic failure.
313  */
314 void snoop_recover(void);
315 
316 /*
317  * Global alarm handler structure for managing multiple alarms within
318  * snoop.
319  */
320 typedef struct snoop_handler {
321 	struct snoop_handler *s_next;		/* next alarm handler */
322 	time_t s_time;				/* time to fire */
323 	void (*s_handler)();			/* alarm handler */
324 } snoop_handler_t;
325 
326 #define	SNOOP_MAXRECOVER	20	/* maxium number of recoveries */
327 #define	SNOOP_ALARM_GRAN	3	/* alarm() timeout multiplier */
328 
329 /*
330  * Global alarm handler management routine.
331  */
332 extern int snoop_alarm(int s_sec, void (*s_handler)());
333 
334 /*
335  * The next two definitions do not take into account the length
336  * of the underlying link header.  In order to use them, you must
337  * add link_header_len to them.  The reason it is not done here is
338  * that later these macros are used to initialize a table.
339  */
340 #define	IPV4_TYPE_HEADER_OFFSET 9
341 #define	IPV6_TYPE_HEADER_OFFSET 6
342 
343 #ifdef __cplusplus
344 }
345 #endif
346 
347 #endif	/* _SNOOP_H */
348