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