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