summaryrefslogtreecommitdiffstats
path: root/tapset/ipmib.stp
blob: fb8cd0199b87b69a91fc1507d91ddf51522abc6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
 *      Copyright (C) 2009 IBM Corp.
 *      This file is part of systemtap, and is free software.  You can
 *      redistribute it and/or modify it under the terms of the GNU General
 *      Public License (GPL); either version 2, or (at your option) any
 *      later version.
 *      
 *      Version 1.0     wilder@us.ibm.com     2009-07-06
 */

%{ /* pure */
#include <net/route.h>
#include <linux/skbuff.h>
#include <linux/icmp.h>
#include <linux/rtnetlink.h>
#include <linux/tcp.h>
%}


/* Commented globals represent statistics that are not supported in this
 * version. 
 */
global InReceives
// global InHdrErrors
// global InTooBigErrors
global InNoRoutes  
global InAddrErrors
global InUnknownProtos
// global InTruncatedPkts
global InDiscards // included counts of InHdrErrors InTruncatedPkts
// global InDelivers
%( kernel_v >= "2.6.24" %?
global ForwDatagrams
%)
global OutRequests
// global OutDiscards
// global OutNoRoutes
global ReasmTimeout
global ReasmReqds
//global ReasmOKs
// global ReasmFails
global FragOKs
global FragFails
// global FragCreates
// global InMcastPkts
// global OutMcastPkts
// global InBcastPkts
// global OutBcastPkts

/**
 * sfunction ipmib_remote_addr - Get the remote ip address.
 *
 * Returns the remote ip address from an sk_buff.
 * @skb: Pointer to a struct sk_buff.
 *
 */
function ipmib_remote_addr:long (skb:long, SourceIsLocal:long)
{
	iphdr = __get_skb_iphdr(skb);
	if ( SourceIsLocal )
		return ntohl(__ip_skb_daddr(iphdr));
	return ntohl(__ip_skb_saddr(iphdr));
}

/**
 * sfunction ipmib_local_addr - Get the local ip address.
 *
 * Returns the local ip address from an sk_buff.
 * @skb: Pointer to a struct sk_buff.
 *
 */
function ipmib_local_addr:long (skb:long, SourceIsLocal:long)
{
	iphdr = __get_skb_iphdr(skb);
	if ( SourceIsLocal )
		return ntohl(__ip_skb_saddr(iphdr));
	return ntohl(__ip_skb_daddr(iphdr));
}

/**
 * sfunction ipmib_tcp_remote_port - Get the remote tcp port.
 *
 * Returns the remote tcp port from an sk_buff.
 * @skb: Pointer to a struct sk_buff.
 *
 */
function ipmib_tcp_remote_port:long (skb:long, SourceIsLocal:long)
{
	th = _plunge_into_tcphdr(skb);
	if ( SourceIsLocal )
		return _tcphdr_get_dport(th);
	return _tcphdr_get_sport(th);
}

/**
 * sfunction ipmib_tcp_local_port - Get the local tcp port.
 *
 * Returns the local tcp port from an sk_buff.
 * @skb: Pointer to a struct sk_buff.
 *
 */
function ipmib_tcp_local_port:long (skb:long, SourceIsLocal:long)
{
	th = _plunge_into_tcphdr(skb);
	if ( SourceIsLocal )
		return _tcphdr_get_sport(th);
	return _tcphdr_get_dport(th);
}

/**
 * sfunction ipmib_get_proto - Get the protocol value
 *
 * Returns the protocol value from ip header.
 * @skb: Pointer to a struct sk_buff.
 *
 */
function ipmib_get_proto:long (skb:long)
{
	iphdr = __get_skb_iphdr(skb);
	return __ip_skb_proto(iphdr);
}

/* Internal functions */

/*
 * We can't assume the pointer to the sk_buff->transport_header 
 * has been updated, so we must calculate the value from the network_header.
 * The caller must verify the skb is for a tcpip packet.
 */
function _plunge_into_tcphdr:long (skb:long)
%{
	struct sk_buff *skb = (struct sk_buff *)(long)THIS->skb;
	/* as done by skb_network_header() */
	#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,21)
		THIS->__retvalue=(long)kread(&(skb->nh.raw)) +
			(long)sizeof(struct tcphdr);
	#else
	#ifdef NET_SKBUFF_DATA_USES_OFFSET
		THIS->__retvalue=(long)kread(&(skb->network_header)) +
		(long)(kread(&(skb->head))) +
		(long)sizeof(struct tcphdr);
	#else
		THIS->__retvalue=(long)kread(&(skb->network_header)) +
		(long)sizeof(struct tcphdr);
	#endif
	#endif
	CATCH_DEREF_FAULT();
%}

function _tcphdr_get_sport:long (th:long)
%{ /* pure */
	struct tcphdr *th = (struct tcphdr *) (long) THIS->th;
	THIS->__retvalue = ntohs(kread(&(th->source)));
	CATCH_DEREF_FAULT();
%}

function _tcphdr_get_dport:long (th:long)
%{ /* pure */
	struct tcphdr *th = (struct tcphdr *) (long) THIS->th;
	THIS->__retvalue = ntohs(kread(&(th->dest)));
	CATCH_DEREF_FAULT();
%}

function _ehostunreach:long ()
%{
	THIS->__retvalue = EHOSTUNREACH;
%}

function _enetunreach:long ()
%{
	THIS->__retvalue = ENETUNREACH;
%}

function _icmp_dest_unreach:long ()
%{
	THIS->__retvalue = ICMP_DEST_UNREACH;
%}

function _icmp_prot_unreach:long ()
%{
	THIS->__retvalue = ICMP_PROT_UNREACH;
%}

function _net_rx_drop:long ()
%{
	THIS->__retvalue = NET_RX_DROP;
%}

function _icmp_time_exceeded:long ()
%{
	THIS->__retvalue = ICMP_TIME_EXCEEDED;
%}

function _icmp_exc_fragtime:long ()
%{
	THIS->__retvalue = ICMP_EXC_FRAGTIME;
%}

function _rtn_multicast:long ()
%{
	THIS->__retvalue = RTN_MULTICAST;
%}

function _rtn_broadcast:long ()
%{
	THIS->__retvalue = RTN_BROADCAST;
%}

/**
 * probe tcpmib.InReceives - Count an arriving packet.
 * @skb: Pointer to the struct sk_buff being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: InReceives
 * MIB: IPSTATS_MIB_INRECEIVES
 *
 */
probe ipmib.InReceives=kernel.function("ip_rcv")
{
	skb = $skb;
	op = 1;
	SourceIsLocal = 0;
	key = ipmib_filter_key(skb,op,SourceIsLocal);
	if ( key ) InReceives[key] += op;
}

/**
 * probe tcpmib.InNoRoutes - Count an arriving packet with no matching socket.
 * @skb: Pointer to the struct sk_buff being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: InNoRoutes
 * MIB: IPSTATS_MIB_INNOROUTES
 *
 */
probe ipmib.InNoRoutes=kernel.function("ip_route_input").return
{
	skb = $skb;
	op = 1;
	SourceIsLocal = 0;
	if ( $return != -_enetunreach()) next;
	key = ipmib_filter_key(skb,op,SourceIsLocal);
	if ( key ) InNoRoutes[key] += op;
}

/**
 * probe tcpmib.InAddrErrors - Count arriving packets with an incorrect address.
 * @skb: Pointer to the struct sk_buff being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: InAddrErrors
 * MIB: IPSTATS_MIB_INADDRERRORS
 *
 */
probe ipmib.InAddrErrors=kernel.function("ip_route_input").return
{
	skb = $skb;
	op = 1;
	SourceIsLocal = 0;
	if ( $return != -_ehostunreach()) next;
	key = ipmib_filter_key(skb,op,SourceIsLocal);
	if ( key ) InAddrErrors[key] += op;
}

/**
 * probe tcpmib.InUnknownProtos - Count arriving packets with an unbound proto. 
 * @skb: Pointer to the struct sk_buff being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: InUnknownProtos
 * MIB: IPSTATS_MIB_INUNKNOWNPROTOS
 *
 */

/* icmp_send() is called by ip_local_deliver_finish() */
probe ipmib.InUnknownProtos=kernel.function("icmp_send")
{
	skb = $skb_in;
	op = 1;
	SourceIsLocal = 0;
	if (($type==_icmp_dest_unreach())&&($code==_icmp_prot_unreach())){ 
		key = ipmib_filter_key(skb,op,SourceIsLocal);
		if ( key ) InUnknownProtos[key] += op;
	}
}

/**
 * probe tcpmib.InDiscards - Count discarded inbound packets.
 * @skb: Pointer to the struct sk_buff being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: InDiscards
 * MIB: STATS_MIB_INDISCARDS
 *
 */

/*
   This stat combines all instances of IPSTATS_MIB_INHDRERRORS,
   IPSTATS_MIB_INTRUNCATEDPKTS, and STATS_MIB_INDISCARDS.
*/
probe ipmib.InDiscards=kernel.function("ip_rcv").return
{
	skb = $skb;
	op = 1;
	SourceIsLocal = 0;
	if ( $return != _net_rx_drop() ) next;
	key = ipmib_filter_key(skb,op,SourceIsLocal);
	if ( key ) InDiscards[key] += op;
}

/**
 * probe tcpmib.ForwDatagrams
 * @skb: Pointer to the struct sk_buff being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: ForwDatagrams
 * MIB: IPSTATS_MIB_OUTFORWDATAGRAMS
 *
 */
%( kernel_v >= "2.6.24" %?
probe ipmib.ForwDatagrams=kernel.function("ip_forward_finish")
{
	skb = $skb;
	op = 1;
	SourceIsLocal = 0;
	key = ipmib_filter_key(skb,op,SourceIsLocal);
	if ( key ) ForwDatagrams[key] += op;
}
%)

/**
 * probe tcpmib.OutRequests - Count a request to send a packet.
 * @skb: Pointer to the struct sk_buff being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: OutRequests
 * MIB: IPSTATS_MIB_OUTREQUESTS
 *
 */
probe ipmib.OutRequests=kernel.function("ip_output"),
			kernel.function("ip_mc_output")
{
	skb = $skb;
	op = 1;
	SourceIsLocal = 1;
	key = ipmib_filter_key(skb,op,SourceIsLocal);
	if ( key ) OutRequests[key] += op;
}

/**
 * probe tcpmib.ReasmTimeout
 * @skb: Pointer to the struct sk_buff being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: ReasmTimeout
 * MIB: IPSTATS_MIB_REASMTIMEOUT
 *
 */
probe ipmib.ReasmTimeout=kernel.function("icmp_send")
{
	skb = $skb_in;
	op = 0;
	SourceIsLocal = 1;
	if(($type==_icmp_time_exceeded())&&($code==_icmp_exc_fragtime())){
		key = ipmib_filter_key(skb,op,SourceIsLocal);
		if ( key ) ReasmTimeout[key] += op;
	}
}

/**
 * probe tcpmib.ReasmReqds
 * @skb: Pointer to the struct sk_buff being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: ReasmReqds
 * MIB: IPSTATS_MIB_REASMREQDS
 *
 */
probe ipmib.ReasmReqds=kernel.function("ip_defrag")
{
	skb = $skb;
	op = 0;
	SourceIsLocal = 1;
	key = ipmib_filter_key(skb,op,SourceIsLocal);
	if ( key ) ReasmReqds[key] += op;
}

/**
 * probe tcpmib.FragOKs
 * @skb: Pointer to the struct sk_buff being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: FragOKs
 * MIB: IPSTATS_MIB_FRAGOKS
 *
 */
probe ipmib.FragOKs=kernel.function("ip_fragment").return
{
	skb = $skb;
	op = 1;
	SourceIsLocal = 1;
	if ( $return ) next
	key = ipmib_filter_key(skb,op,SourceIsLocal);
	if ( key ) FragOKs [key] += op;
}

/**
 * probe tcpmib.FragFails
 * @skb: Pointer to the struct sk_buff being acted on.
 * @op: Value to be added to the counter (Operation). 
 *
 * Counter Name: FragFails
 * MIB: IPSTATS_MIB_FRAGFAILS
 *
 */
probe ipmib.FragFails=kernel.function("ip_fragment").return
{
	skb = $skb;
	op = 1;
	SourceIsLocal = 1;
	if (!$return ) next
	key = ipmib_filter_key(skb,op,SourceIsLocal);
	if ( key ) FragFails [key] += op;
}

function _input_route_type:long (skb:long)
{
%( kernel_v < "2.6.31" %?
	return __input_route_type_old(skb)
%:
	return __input_route_type_new(skb)
%)
}

function __input_route_type_old:long (skb:long)
%{ /* pure */
	struct rtable *rt;
	struct sk_buff *skb = (struct sk_buff *)(long)THIS->skb;
	rt = (struct rtable *)kread(&(skb->rtable));
	if  ( rt )
		THIS->__retvalue = kread(&(rt->rt_type));
	else
		THIS->__retvalue = RTN_UNSPEC;
	CATCH_DEREF_FAULT();
%}

function __input_route_type_new:long (skb:long)
%{ /* pure */
	struct rtable *rt;
	struct sk_buff *skb = (struct sk_buff *)(long)THIS->skb;
	rt = (struct rtable *)kread(&(skb->_skb_dst));
	if  ( rt )
		THIS->__retvalue = kread(&(rt->rt_type));
	else
		THIS->__retvalue = RTN_UNSPEC;
	CATCH_DEREF_FAULT();
%}

function _output_route_type:long (skb:long)
{
%( kernel_v < "2.6.31" %?
	return __output_route_type_old(skb)
%:
	return __output_route_type_new(skb)
%)
}

function __output_route_type_old:long (skb:long)
%{ /* pure */
	struct rtable *rt;
	struct dst_entry *dst;
	struct sk_buff *skb = (struct sk_buff *)(long)THIS->skb;
	dst = (struct dst_entry *)kread(&(skb->dst));
	rt = (struct rtable *)dst;
	if ( rt )
		THIS->__retvalue = kread(&(rt->rt_type));
	else
		THIS->__retvalue = RTN_UNSPEC;
	CATCH_DEREF_FAULT();
%}

function __output_route_type_new:long (skb:long)
%{ /* pure */
	struct rtable *rt;
	struct sk_buff *skb = (struct sk_buff *)(long)THIS->skb;
	rt = (struct rtable *)kread(&(skb->_skb_dst));
	if ( rt )
		THIS->__retvalue = kread(&(rt->rt_type));
	else
		THIS->__retvalue = RTN_UNSPEC;
	CATCH_DEREF_FAULT();
%}