3 * tcpsnoop_snv.d - snoop TCP network packets by process.
4 * Written using DTrace (Solaris Nevada)
6 * This analyses TCP network packets and prints the responsible PID and UID,
7 * plus standard details such as IP address and port. This captures traffic
8 * of newly created TCP connections that were established while this program
9 * was running. It can help identify which processes is causing TCP traffic.
11 * WARNING: This script may only work on Solaris Nevada and OpenSolaris
12 * of the late 2007 vintage, since it uses the fbt provider to trace the raw
13 * operation of a specific version of the kernel. In the future, a 'stable'
14 * network provider should exist which will allow this to be written for that
15 * and subsequent versions of the kernel. In the meantime, check for other
16 * versions of this script in the /Net directory, and read the
17 * Notes/ALLfbt_notes.txt for more background on fbt.
19 * $Id: tcpsnoop_snv.d 69 2007-10-04 13:40:00Z brendan $
27 * LADDR local IP address
28 * RADDR remote IP address
29 * LPORT local port number
30 * RPORT remote port number
32 * SIZE packet size, bytes
36 * COPYRIGHT: Copyright (c) 2005, 2006 Brendan Gregg.
40 * The contents of this file are subject to the terms of the
41 * Common Development and Distribution License, Version 1.0 only
42 * (the "License"). You may not use this file except in compliance
45 * You can obtain a copy of the license at Docs/cddl1.txt
46 * or http://www.opensolaris.org/os/licensing.
47 * See the License for the specific language governing permissions
48 * and limitations under the License.
52 * Author: Brendan Gregg [Sydney, Australia]
56 * 09-Jul-2004 Brendan Gregg Created this.
57 * 12-Mar-2005 " " Changed probes, size info now printed.
58 * 02-Jul-2005 " " Many more probes. Renamed "tcpsnoop.d".
59 * 03-Dec-2005 " " Fixed tcp_accept_finish bug, now 100% correct
60 * execname. Thanks Kias Belgaied for expertise.
61 * 20-Apr-2006 " " Fixed SS_TCP_FAST_ACCEPT bug in build 31+.
62 * 20-Apr-2006 " " Last update.
63 * 30-Sep-2007 " " Bumped this for recent OpenSolaris/Nevada.
66 #pragma D option quiet
67 #pragma D option switchrate=10hz
70 #include <inet/common.h>
71 #include <sys/byteorder.h>
78 /* print main headers */
79 printf("%5s %6s %-15s %5s %2s %-15s %5s %5s %s\n",
80 "UID", "PID", "LADDR", "LPORT", "DR", "RADDR", "RPORT",
85 * TCP Process inbound connections
87 * 0x00200000 has been hardcoded. It was SS_TCP_FAST_ACCEPT, but was
88 * renamed to SS_DIRECT around build 31.
90 fbt:sockfs:sotpi_accept:entry
91 /(arg1 & FREAD) && (arg1 & FWRITE) && (args[0]->so_state & 0x00200000)/
96 fbt:sockfs:sotpi_create:return
99 self->nsop = (struct sonode *)arg1;
102 fbt:sockfs:sotpi_accept:return
105 this->tcpp = (tcp_t *)self->nsop->so_priv;
106 self->connp = (conn_t *)this->tcpp->tcp_connp;
107 tname[(int)self->connp] = execname;
108 tpid[(int)self->connp] = pid;
109 tuid[(int)self->connp] = uid;
112 fbt:sockfs:sotpi_accept:return
119 * TCP Process outbound connections
121 fbt:ip:tcp_connect:entry
123 this->tcpp = (tcp_t *)arg0;
124 self->connp = (conn_t *)this->tcpp->tcp_connp;
125 tname[(int)self->connp] = execname;
126 tpid[(int)self->connp] = pid;
127 tuid[(int)self->connp] = uid;
131 * TCP Data translations
133 fbt:sockfs:sotpi_accept:return,
134 fbt:ip:tcp_connect:return
138 #if defined(_BIG_ENDIAN)
139 self->lport = self->connp->u_port.tcpu_ports.tcpu_lport;
140 self->fport = self->connp->u_port.tcpu_ports.tcpu_fport;
142 self->lport = BSWAP_16(self->connp->u_port.tcpu_ports.tcpu_lport);
143 self->fport = BSWAP_16(self->connp->u_port.tcpu_ports.tcpu_fport);
146 /* fetch IPv4 addresses */
148 (int)self->connp->connua_v6addr.connua_faddr._S6_un._S6_u8[12];
150 (int)self->connp->connua_v6addr.connua_faddr._S6_un._S6_u8[13];
152 (int)self->connp->connua_v6addr.connua_faddr._S6_un._S6_u8[14];
154 (int)self->connp->connua_v6addr.connua_faddr._S6_un._S6_u8[15];
156 (int)self->connp->connua_v6addr.connua_laddr._S6_un._S6_u8[12];
158 (int)self->connp->connua_v6addr.connua_laddr._S6_un._S6_u8[13];
160 (int)self->connp->connua_v6addr.connua_laddr._S6_un._S6_u8[14];
162 (int)self->connp->connua_v6addr.connua_laddr._S6_un._S6_u8[15];
164 /* convert type for use with lltostr() */
165 this->fad12 = this->fad12 < 0 ? 256 + this->fad12 : this->fad12;
166 this->fad13 = this->fad13 < 0 ? 256 + this->fad13 : this->fad13;
167 this->fad14 = this->fad14 < 0 ? 256 + this->fad14 : this->fad14;
168 this->fad15 = this->fad15 < 0 ? 256 + this->fad15 : this->fad15;
169 this->lad12 = this->lad12 < 0 ? 256 + this->lad12 : this->lad12;
170 this->lad13 = this->lad13 < 0 ? 256 + this->lad13 : this->lad13;
171 this->lad14 = this->lad14 < 0 ? 256 + this->lad14 : this->lad14;
172 this->lad15 = this->lad15 < 0 ? 256 + this->lad15 : this->lad15;
174 /* stringify addresses */
175 self->faddr = strjoin(lltostr(this->fad12), ".");
176 self->faddr = strjoin(self->faddr, strjoin(lltostr(this->fad13), "."));
177 self->faddr = strjoin(self->faddr, strjoin(lltostr(this->fad14), "."));
178 self->faddr = strjoin(self->faddr, lltostr(this->fad15 + 0));
179 self->laddr = strjoin(lltostr(this->lad12), ".");
180 self->laddr = strjoin(self->laddr, strjoin(lltostr(this->lad13), "."));
181 self->laddr = strjoin(self->laddr, strjoin(lltostr(this->lad14), "."));
182 self->laddr = strjoin(self->laddr, lltostr(this->lad15 + 0));
184 /* fix direction and save values */
185 tladdr[(int)self->connp] = self->laddr;
186 tfaddr[(int)self->connp] = self->faddr;
187 tlport[(int)self->connp] = self->lport;
188 tfport[(int)self->connp] = self->fport;
191 tok[(int)self->connp] = 1;
197 fbt:ip:tcp_get_conn:return
200 this->connp = (conn_t *)arg1;
201 tok[(int)this->connp] = 0;
202 tpid[(int)this->connp] = 0;
203 tuid[(int)this->connp] = 0;
204 tname[(int)this->connp] = 0;
208 * TCP Process "port closed"
210 fbt:ip:tcp_xmit_early_reset:entry
212 this->queuep = args[7]->tcps_g_q;
213 this->connp = (conn_t *)this->queuep->q_ptr;
214 this->tcpp = (tcp_t *)this->connp->conn_tcp;
216 /* split addresses */
217 this->ipha = (ipha_t *)args[1]->b_rptr;
218 this->fad15 = (this->ipha->ipha_src & 0xff000000) >> 24;
219 this->fad14 = (this->ipha->ipha_src & 0x00ff0000) >> 16;
220 this->fad13 = (this->ipha->ipha_src & 0x0000ff00) >> 8;
221 this->fad12 = (this->ipha->ipha_src & 0x000000ff);
222 this->lad15 = (this->ipha->ipha_dst & 0xff000000) >> 24;
223 this->lad14 = (this->ipha->ipha_dst & 0x00ff0000) >> 16;
224 this->lad13 = (this->ipha->ipha_dst & 0x0000ff00) >> 8;
225 this->lad12 = (this->ipha->ipha_dst & 0x000000ff);
227 /* stringify addresses */
228 self->faddr = strjoin(lltostr(this->fad12), ".");
229 self->faddr = strjoin(self->faddr, strjoin(lltostr(this->fad13), "."));
230 self->faddr = strjoin(self->faddr, strjoin(lltostr(this->fad14), "."));
231 self->faddr = strjoin(self->faddr, lltostr(this->fad15 + 0));
232 self->laddr = strjoin(lltostr(this->lad12), ".");
233 self->laddr = strjoin(self->laddr, strjoin(lltostr(this->lad13), "."));
234 self->laddr = strjoin(self->laddr, strjoin(lltostr(this->lad14), "."));
235 self->laddr = strjoin(self->laddr, lltostr(this->lad15 + 0));
241 * TCP Fetch "port closed" ports
243 fbt:ip:tcp_xchg:entry
246 #if defined(_BIG_ENDIAN)
247 self->lport = (uint16_t)arg0;
248 self->fport = (uint16_t)arg1;
250 self->lport = BSWAP_16((uint16_t)arg0);
251 self->fport = BSWAP_16((uint16_t)arg1);
253 self->lport = BE16_TO_U16(arg0);
254 self->fport = BE16_TO_U16(arg1);
258 * TCP Print "port closed"
260 fbt:ip:tcp_xmit_early_reset:return
262 self->name = "<closed>";
265 self->size = 54; /* should check trailers */
267 printf("%5d %6d %-15s %5d %2s %-15s %5d %5d %s\n",
268 self->uid, self->pid, self->laddr, self->lport, self->dir,
269 self->faddr, self->fport, self->size, self->name);
271 printf("%5d %6d %-15s %5d %2s %-15s %5d %5d %s\n",
272 self->uid, self->pid, self->laddr, self->lport, self->dir,
273 self->faddr, self->fport, self->size, self->name);
282 fbt:ip:tcp_send_data:entry
284 self->conn_p = (conn_t *)args[0]->tcp_connp;
287 fbt:ip:tcp_send_data:entry
288 /tok[(int)self->conn_p]/
291 self->size = msgdsize(args[2]) + 14; /* should check trailers */
292 self->uid = tuid[(int)self->conn_p];
293 self->laddr = tladdr[(int)self->conn_p];
294 self->faddr = tfaddr[(int)self->conn_p];
295 self->lport = tlport[(int)self->conn_p];
296 self->fport = tfport[(int)self->conn_p];
299 /* follow inetd -> in.* transitions */
300 self->name = pid && (tname[(int)self->conn_p] == "inetd") ?
301 execname : tname[(int)self->conn_p];
302 self->pid = pid && (tname[(int)self->conn_p] == "inetd") ?
303 pid : tpid[(int)self->conn_p];
304 tname[(int)self->conn_p] = self->name;
305 tpid[(int)self->conn_p] = self->pid;
311 fbt:ip:tcp_rput_data:entry
313 self->conn_p = (conn_t *)arg0;
314 self->size = msgdsize(args[1]) + 14; /* should check trailers */
317 fbt:ip:tcp_rput_data:entry
318 /tok[(int)self->conn_p]/
321 self->uid = tuid[(int)self->conn_p];
322 self->laddr = tladdr[(int)self->conn_p];
323 self->faddr = tfaddr[(int)self->conn_p];
324 self->lport = tlport[(int)self->conn_p];
325 self->fport = tfport[(int)self->conn_p];
328 /* follow inetd -> in.* transitions */
329 self->name = pid && (tname[(int)self->conn_p] == "inetd") ?
330 execname : tname[(int)self->conn_p];
331 self->pid = pid && (tname[(int)self->conn_p] == "inetd") ?
332 pid : tpid[(int)self->conn_p];
333 tname[(int)self->conn_p] = self->name;
334 tpid[(int)self->conn_p] = self->pid;
338 * TCP Complete printing outbound handshake
340 fbt:ip:tcp_connect:return
343 self->name = tname[(int)self->connp];
344 self->pid = tpid[(int)self->connp];
345 self->uid = tuid[(int)self->connp];
346 self->size = 54; /* should check trailers */
348 /* this packet occured before connp was fully established */
349 printf("%5d %6d %-15s %5d %2s %-15s %5d %5d %s\n",
350 self->uid, self->pid, self->laddr, self->lport, self->dir,
351 self->faddr, self->fport, self->size, self->name);
355 * TCP Complete printing inbound handshake
357 fbt:sockfs:sotpi_accept:return
360 self->name = tname[(int)self->connp];
361 self->pid = tpid[(int)self->connp];
362 self->uid = tuid[(int)self->connp];
363 self->size = 54; /* should check trailers */
364 /* these packets occured before connp was fully established */
366 printf("%5d %6d %-15s %5d %2s %-15s %5d %5d %s\n",
367 self->uid, self->pid, self->laddr, self->lport, self->dir,
368 self->faddr, self->fport, self->size, self->name);
370 printf("%5d %6d %-15s %5d %2s %-15s %5d %5d %s\n",
371 self->uid, self->pid, self->laddr, self->lport, self->dir,
372 self->faddr, self->fport, self->size, self->name);
374 printf("%5d %6d %-15s %5d %2s %-15s %5d %5d %s\n",
375 self->uid, self->pid, self->laddr, self->lport, self->dir,
376 self->faddr, self->fport, self->size, self->name);
382 fbt:ip:tcp_send_data:entry,
383 fbt:ip:tcp_rput_data:entry
386 /* print output line */
387 printf("%5d %6d %-15s %5d %2s %-15s %5d %5d %s\n",
388 self->uid, self->pid, self->laddr, self->lport, self->dir,
389 self->faddr, self->fport, self->size, self->name);
393 * TCP Clear connect variables
395 fbt:sockfs:sotpi_accept:return,
396 fbt:ip:tcp_connect:return
410 * TCP Clear r/w variables
412 fbt:ip:tcp_send_data:entry,
413 fbt:ip:tcp_rput_data:entry