| 1 |
// IP tapset |
| 2 |
// |
| 3 |
// Copyright (C) 2009, IBM Inc. |
| 4 |
// Copyright (C) 2010-2018 Red Hat Inc. |
| 5 |
// |
| 6 |
// Author : Breno Leitao <[email protected]> |
| 7 |
// |
| 8 |
// This file is free software. You can redistribute it and/or modify it under |
| 9 |
// the terms of the GNU General Public License (GPL), version 2. |
| 10 |
// |
| 11 |
// Based on previous work done by Arnaldo Carvalho de Melo <[email protected]> |
| 12 |
|
| 13 |
%{ |
| 14 |
#include <linux/skbuff.h> |
| 15 |
#include <linux/socket.h> // For AF_INET & AF_INET6 |
| 16 |
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 17 |
#include <linux/types.h> |
| 18 |
#include <linux/in6.h> |
| 19 |
#endif /* CONFIG_IPV6 || CONFIG_IPV6_MODULE */ |
| 20 |
%} |
| 21 |
|
| 22 |
/** |
| 23 |
* sfunction format_ipaddr - Returns a string representation for an IP address |
| 24 |
* |
| 25 |
* @addr: the IP address |
| 26 |
* @family: the IP address family (either AF_INET or AF_INET6) |
| 27 |
*/ |
| 28 |
function format_ipaddr:string (addr:long, family:long) |
| 29 |
%{ /* pure */ |
| 30 |
if (STAP_ARG_family == AF_INET) { |
| 31 |
__be32 ip = (__be32)STAP_ARG_addr; |
| 32 |
#ifndef NIPQUAD_FMT // kver >= 2.6.36 |
| 33 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, "%pI4", &ip); |
| 34 |
#else |
| 35 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, NIPQUAD_FMT, |
| 36 |
NIPQUAD(ip)); |
| 37 |
#endif |
| 38 |
} |
| 39 |
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 40 |
else if (STAP_ARG_family == AF_INET6) { |
| 41 |
struct in6_addr *ipv6 = (struct in6_addr *)(uintptr_t)STAP_ARG_addr; |
| 42 |
// We need to derefence the memory safely from the |
| 43 |
// address passed to us that contains the IPv6 address. |
| 44 |
// However, kderef()/kread() only handle data with a |
| 45 |
// size of 1, 2, 4, or 8. So, we'll use |
| 46 |
// kderef_buffer() which goes byte by byte. |
| 47 |
kderef_buffer(NULL, ipv6, sizeof(struct in6_addr)); |
| 48 |
#ifndef NIP6_FMT // kver >= 2.6.36 |
| 49 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, "%pI6", ipv6); |
| 50 |
#else |
| 51 |
snprintf(STAP_RETVALUE, MAXSTRINGLEN, NIP6_FMT, NIP6(*ipv6)); |
| 52 |
#endif |
| 53 |
} |
| 54 |
#endif /* CONFIG_IPV6 || CONFIG_IPV6_MODULE */ |
| 55 |
else |
| 56 |
strlcpy(STAP_RETVALUE, "*unknown address family*", |
| 57 |
MAXSTRINGLEN); |
| 58 |
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
| 59 |
CATCH_DEREF_FAULT(); |
| 60 |
#endif /* CONFIG_IPV6 || CONFIG_IPV6_MODULE */ |
| 61 |
%} |
| 62 |
|
| 63 |
/** |
| 64 |
* sfunction ip_ntop - Returns a string representation for an IPv4 address |
| 65 |
* |
| 66 |
* @addr: the IPv4 address represented as an integer |
| 67 |
*/ |
| 68 |
function ip_ntop:string (addr:long) |
| 69 |
{ |
| 70 |
return format_ipaddr(addr, @const("AF_INET")) |
| 71 |
} |
| 72 |
|
| 73 |
/* |
| 74 |
* Return the source IP address for a given sock. |
| 75 |
* |
| 76 |
* Note that the probes that use this function typically get a 'struct |
| 77 |
* sock' pointer, not a 'struct inet_sock' pointer. This is expected. |
| 78 |
* To use a 'struct sock' pointer, you typically call inet_sk() on the |
| 79 |
* pointer, which just returns the pointer (since the 1st member of |
| 80 |
* 'struct inet_sock' is a 'struct sock'). |
| 81 |
*/ |
| 82 |
|
| 83 |
function __ip_sock_saddr:long (sock:long) |
| 84 |
{ |
| 85 |
family = @inet_sock_cast(sock)->sk->__sk_common->skc_family |
| 86 |
if (family == @const("AF_INET")) { |
| 87 |
return @choose_defined(@inet_sock_cast(sock)->inet_saddr, # kernel >= 2.6.33 |
| 88 |
@choose_defined(@inet_sock_cast(sock)->saddr, # kernel >= 2.6.11 |
| 89 |
@inet_sock_cast(sock)->inet->saddr)) |
| 90 |
} |
| 91 |
%( CONFIG_IPV6 == "[ym]" %? |
| 92 |
else if (family == @const("AF_INET6")) { |
| 93 |
return (&@inet_sock_cast(sock)->pinet6->saddr) |
| 94 |
} |
| 95 |
%) |
| 96 |
return 0 |
| 97 |
} |
| 98 |
|
| 99 |
/* return the destination IP address for a given sock */ |
| 100 |
function __ip_sock_daddr:long (sock:long) |
| 101 |
{ |
| 102 |
family = @inet_sock_cast(sock)->sk->__sk_common->skc_family |
| 103 |
if (family == @const("AF_INET")) { |
| 104 |
return @choose_defined(@inet_sock_cast(sock)->sk->__sk_common->skc_daddr, # kernel >= 2.6.38 |
| 105 |
@choose_defined(@inet_sock_cast(sock)->inet_daddr, # kernel >= 2.6.33 |
| 106 |
@choose_defined(@inet_sock_cast(sock)->daddr, # kernel >= 2.6.11 |
| 107 |
@inet_sock_cast(sock)->inet->daddr))) |
| 108 |
} |
| 109 |
%( CONFIG_IPV6 == "[ym]" %? |
| 110 |
else if (family == @const("AF_INET6")) { |
| 111 |
return @choose_defined(&@inet_sock_cast(sock)->sk->__sk_common->skc_v6_daddr, |
| 112 |
&@inet_sock_cast(sock)->pinet6->daddr) |
| 113 |
} |
| 114 |
%) |
| 115 |
return 0 |
| 116 |
} |
| 117 |
|
| 118 |
/* return the IP address family for a given sock */ |
| 119 |
function __ip_sock_family:long (sock:long) |
| 120 |
{ |
| 121 |
return (@inet_sock_cast(sock)->sk->__sk_common->skc_family) |
| 122 |
} |
| 123 |
|
| 124 |
/* Get the IP header from a sk_buff struct */ |
| 125 |
function __get_skb_iphdr:long(skb:long) |
| 126 |
%( kernel_v < "2.6.21" %? |
| 127 |
{ |
| 128 |
iphdr = @cast(skb, "sk_buff", "kernel<linux/skbuff.h>")->nh->raw |
| 129 |
return iphdr |
| 130 |
} |
| 131 |
%: |
| 132 |
%{ /* pure */ |
| 133 |
struct sk_buff *skb; |
| 134 |
skb = (struct sk_buff *)(uintptr_t)STAP_ARG_skb; |
| 135 |
|
| 136 |
/* We used to have our own version of skb_network_header() |
| 137 |
* here, but it has changed too much over time to keep up with |
| 138 |
* it. So, instead we'll make sure the entire skb is safe to |
| 139 |
* read, then call the real skb_network_header(). */ |
| 140 |
kderef_buffer(NULL, skb, sizeof(struct sk_buff)); |
| 141 |
STAP_RETVALUE = (long)skb_network_header(skb); |
| 142 |
CATCH_DEREF_FAULT(); |
| 143 |
%} |
| 144 |
%) |
| 145 |
|
| 146 |
@define iphdr_cast(ptr) %( |
| 147 |
@cast(@ptr, "iphdr", "kernel<uapi/linux/ip.h>") |
| 148 |
%) |
| 149 |
|
| 150 |
/* return the source next layer protocol for a given sk_buff structure */ |
| 151 |
function __ip_skb_proto:long (iphdr) |
| 152 |
{ |
| 153 |
return @iphdr_cast(iphdr)->protocol |
| 154 |
} |
| 155 |
|
| 156 |
/* return the source IP address for a given sk_buff structure */ |
| 157 |
function __ip_skb_saddr:long (iphdr) |
| 158 |
{ |
| 159 |
return @iphdr_cast(iphdr)->saddr |
| 160 |
} |
| 161 |
|
| 162 |
/* return the destination IP address for a given skb */ |
| 163 |
function __ip_skb_daddr:long (iphdr) |
| 164 |
{ |
| 165 |
return @iphdr_cast(iphdr)->daddr |
| 166 |
} |