// IP tapset
//
// Copyright (C) 2009, IBM Inc.
// Copyright (C) 2010, 2012 Red Hat Inc.
//
// Author : Breno Leitao <leitao@linux.vnet.ibm.com>
//
// This file is free software.  You can redistribute it and/or modify it under
// the terms of the GNU General Public License (GPL), version 2.
//
// Based on previous work done by Arnaldo Carvalho de Melo <acme@redhat.com>

%{
#include <linux/skbuff.h>
#include <linux/socket.h>		// For AF_INET & AF_INET6
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
#include <linux/types.h>
#include <linux/in6.h>
#endif /* CONFIG_IPV6 || CONFIG_IPV6_MODULE */
%}

/**
 * sfunction format_ipaddr - Returns a string representation for an IP address
 *
 * @addr: the IP address
 * @family: the IP address family (either AF_INET or AF_INET6)
 */
function format_ipaddr:string (addr:long, family:long)
%{ /* pure */
	if (STAP_ARG_family == AF_INET) {
		__be32 ip = (__be32)STAP_ARG_addr;
#ifndef NIPQUAD_FMT			// kver >= 2.6.36
		snprintf(STAP_RETVALUE, MAXSTRINGLEN, "%pI4", &ip);
#else
		snprintf(STAP_RETVALUE, MAXSTRINGLEN, NIPQUAD_FMT,
			 NIPQUAD(ip));
#endif
	}
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	else if (STAP_ARG_family == AF_INET6) {
		struct in6_addr *ipv6 = (struct in6_addr *)(uintptr_t)STAP_ARG_addr;
		// We need to derefence the memory safely from the
		// address passed to us that contains the IPv6 address.
		// However, kderef()/kread() only handle data with a
		// size of 1, 2, 4, or 8.  So, we'll use
		// kderef_buffer() which goes byte by byte.
		kderef_buffer(NULL, ipv6, sizeof(struct in6_addr));
#ifndef NIP6_FMT			// kver >= 2.6.36
		snprintf(STAP_RETVALUE, MAXSTRINGLEN, "%pI6", ipv6);
#else
		snprintf(STAP_RETVALUE, MAXSTRINGLEN, NIP6_FMT, NIP6(*ipv6));
#endif
	}
#endif /* CONFIG_IPV6 || CONFIG_IPV6_MODULE */
	else
		strncpy(STAP_RETVALUE, "*unknown address family*",
			MAXSTRINGLEN);
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
	CATCH_DEREF_FAULT();
#endif /* CONFIG_IPV6 || CONFIG_IPV6_MODULE */
%}

/**
 * sfunction ip_ntop - Returns a string representation for an IPv4 address
 *
 * @addr: the IPv4 address represented as an integer
 */
function ip_ntop:string (addr:long)
{
	return format_ipaddr(addr, %{ /* pure */ /* unprivileged */ AF_INET %})
}

/*
 * Return the source IP address for a given sock.
 *
 * Note that the probes that use this function typically get a 'struct
 * sock' pointer, not a 'struct inet_sock' pointer.  This is expected.
 * To use a 'struct sock' pointer, you typically call inet_sk() on the
 * pointer, which just returns the pointer (since the 1st member of
 * 'struct inet_sock' is a 'struct sock').
 */

function __ip_sock_saddr:long (sock:long)
{
    family = @cast(sock, "inet_sock", "kernel<net/ip.h>")->sk->__sk_common->skc_family
    if (family == %{ /* pure */ /* unprivileged */ AF_INET %}) {
	return @choose_defined(@cast(sock, "inet_sock")->inet_saddr, # kernel >= 2.6.33
		@choose_defined(@cast(sock, "inet_sock")->saddr, # kernel >= 2.6.11
		    @cast(sock, "inet_sock", "kernel<net/ip.h>")->inet->saddr))
    }
%( CONFIG_IPV6 == "[ym]" %?
    else if (family == %{ /* pure */ /* unprivileged */ AF_INET6 %}) {
	return (&@cast(sock, "inet_sock", "kernel<net/ip.h>")->pinet6->saddr)
    }
%)
    return 0
}

/* return the destination IP address for a given sock */
function __ip_sock_daddr:long (sock:long)
{
    family = @cast(sock, "inet_sock", "kernel<net/ip.h>")->sk->__sk_common->skc_family
    if (family == %{ /* pure */ /* unprivileged */ AF_INET %}) {
	return @choose_defined(@cast(sock, "inet_sock")->sk->__sk_common->skc_daddr, # kernel >= 2.6.38
		@choose_defined(@cast(sock, "inet_sock")->inet_daddr, # kernel >= 2.6.33
		    @choose_defined(@cast(sock, "inet_sock")->daddr, # kernel >= 2.6.11
			@cast(sock, "inet_sock", "kernel<net/ip.h>")->inet->daddr)))
    }
%( CONFIG_IPV6 == "[ym]" %?
    else if (family == %{ /* pure */ /* unprivileged */ AF_INET6 %}) {
	return (&@cast(sock, "inet_sock", "kernel<net/ip.h>")->pinet6->daddr)
    }
%)
    return 0
}

/* return the IP address family for a given sock */
function __ip_sock_family:long (sock:long)
{
    return (@cast(sock, "inet_sock", "kernel<net/ip.h>")->sk->__sk_common->skc_family)
}

/* Get the IP header from a sk_buff struct */
function __get_skb_iphdr:long(skb:long)
%( kernel_v < "2.6.21" %?
{
	iphdr = @cast(skb, "sk_buff")->nh->raw
	return iphdr
}
%:
%{ /* pure */
	struct sk_buff *skb;
	skb = (struct sk_buff *)(long)STAP_ARG_skb;
	/* as done by skb_network_header() */
	#ifdef NET_SKBUFF_DATA_USES_OFFSET
		STAP_RETVALUE = (long)(kread(&(skb->head)) + kread(&(skb->network_header)));
	#else
		STAP_RETVALUE = (long)kread(&(skb->network_header));
	#endif
	CATCH_DEREF_FAULT();
%}
%)

/* return the source next layer protocol for a given sk_buff structure */
function __ip_skb_proto:long (iphdr)
{
	return @cast(iphdr, "iphdr")->protocol
}

/* return the source IP address for a given sk_buff structure */
function __ip_skb_saddr:long (iphdr)
{
	return @cast(iphdr, "iphdr")->saddr
}

/* return the destination IP address for a given skb */
function __ip_skb_daddr:long (iphdr)
{
	return @cast(iphdr, "iphdr")->daddr
}
