Commit 97fdaa38 authored by Lionel Gauthier's avatar Lionel Gauthier

git-svn-id: http://svn.eurecom.fr/openair4G/trunk@6239 818b1a75-f10b-46b9-bf7c-635c3b92a50f
parent dc906ca8
...@@ -19,9 +19,9 @@ ...@@ -19,9 +19,9 @@
#include <linux/route.h> #include <linux/route.h>
#include <linux/time.h> #include <linux/time.h>
#include <net/checksum.h> #include <net/checksum.h>
#include <net/ip.h>
#include <net/udp.h> #include <net/udp.h>
#include <net/inet_sock.h> #include <net/inet_sock.h>
#include <net/ip.h>
#include <net/route.h> #include <net/route.h>
#include <linux/netfilter/x_tables.h> #include <linux/netfilter/x_tables.h>
#include <linux/netfilter_ipv4/ip_tables.h> #include <linux/netfilter_ipv4/ip_tables.h>
...@@ -39,7 +39,6 @@ MODULE_LICENSE("GPL"); ...@@ -39,7 +39,6 @@ MODULE_LICENSE("GPL");
MODULE_AUTHOR("Pradip Biswas <pradip_biswas@polarisnetworks.net>"); MODULE_AUTHOR("Pradip Biswas <pradip_biswas@polarisnetworks.net>");
MODULE_DESCRIPTION("GTPu Data Path extension on netfilter"); MODULE_DESCRIPTION("GTPu Data Path extension on netfilter");
struct gtpuhdr struct gtpuhdr
{ {
char flags; char flags;
...@@ -62,11 +61,11 @@ struct gtpuhdr ...@@ -62,11 +61,11 @@ struct gtpuhdr
#define IP_MORE_FRAGMENTS 0x2000 #define IP_MORE_FRAGMENTS 0x2000
static bool _gtpuah_route_packet(struct sk_buff *skb, const struct xt_gtpuah_target_info *info) static bool _gtpuah_route_packet(struct sk_buff *skb_pP, const struct xt_gtpuah_target_info *info_pP, char *in_dev_pP)
{ {
int err = 0; int err = 0;
struct rtable *rt = NULL; struct rtable *rt = NULL;
struct iphdr *iph = ip_hdr(skb); struct iphdr *iph = ip_hdr(skb_pP);
int daddr = iph->daddr; int daddr = iph->daddr;
struct flowi fl = { struct flowi fl = {
.u = { .u = {
...@@ -78,7 +77,7 @@ static bool _gtpuah_route_packet(struct sk_buff *skb, const struct xt_gtpuah_tar ...@@ -78,7 +77,7 @@ static bool _gtpuah_route_packet(struct sk_buff *skb, const struct xt_gtpuah_tar
} }
}; };
#if 0 #if 1
int flags, offset; int flags, offset;
offset = ntohs(iph->frag_off); offset = ntohs(iph->frag_off);
...@@ -87,7 +86,7 @@ static bool _gtpuah_route_packet(struct sk_buff *skb, const struct xt_gtpuah_tar ...@@ -87,7 +86,7 @@ static bool _gtpuah_route_packet(struct sk_buff *skb, const struct xt_gtpuah_tar
offset <<= 3; offset <<= 3;
pr_info("GTPUAH(%d): Routing %s packet: %d.%d.%d.%d --> %d.%d.%d.%d Proto: %d, Len: %05u, Id %04X, Offset %u, Flags %04X, Mark: %u\n", pr_info("GTPUAH(%d): Routing %s packet: %d.%d.%d.%d --> %d.%d.%d.%d Proto: %d, Len: %05u, Id %04X, Offset %u, Flags %04X, Mark: %u\n",
info->action, info_pP->action,
(ip_is_fragment(iph) == 0) ? "":"fragmented", (ip_is_fragment(iph) == 0) ? "":"fragmented",
iph->saddr & 0xFF, iph->saddr & 0xFF,
(iph->saddr & 0x0000FF00) >> 8, (iph->saddr & 0x0000FF00) >> 8,
...@@ -102,38 +101,44 @@ static bool _gtpuah_route_packet(struct sk_buff *skb, const struct xt_gtpuah_tar ...@@ -102,38 +101,44 @@ static bool _gtpuah_route_packet(struct sk_buff *skb, const struct xt_gtpuah_tar
ntohs(iph->id), ntohs(iph->id),
offset, offset,
flags, flags,
skb->mark); skb_pP->mark);
#endif #endif
rt = ip_route_output_key(&init_net, &fl.u.ip4); if (in_dev_pP) {
if (err != 0) if (strcasecmp(in_dev_pP, "lo") == 0) {
{ struct inet_sock *inet = inet_sk(skb_pP->sk);
pr_info("GTPU: Failed to route packet to dst 0x%x. Error: (%d)", fl.u.ip4.daddr, err); ip_queue_xmit(&inet->sk, skb_pP, &fl);
return GTPU_FAILURE; } else {
} rt = ip_route_output_key(&init_net, &fl.u.ip4);
if (err != 0)
{
pr_info("GTPU: Failed to route packet to dst 0x%x. Error: (%d)", fl.u.ip4.daddr, err);
return GTPU_FAILURE;
}
#if 0 #if 1
if (rt->dst.dev) if (rt->dst.dev)
{ {
pr_info("GTPU: dst dev name %s\n", rt->dst.dev->name); pr_info("GTPU: dst dev name %s\n", rt->dst.dev->name);
} }
else else
{ {
pr_info("GTPU: dst dev NULL\n"); pr_info("GTPU: dst dev NULL\n");
} }
#endif #endif
//if (info->action == PARAM_GTPUAH_ACTION_ADD) //LG was commented //if (info_pP->action == PARAM_GTPUAH_ACTION_ADD) //LG was commented
{ {
skb_dst_drop(skb); skb_dst_drop(skb_pP);
skb_dst_set(skb, &rt->dst); skb_dst_set(skb_pP, &rt->dst);
skb->dev = skb_dst(skb)->dev; skb_pP->dev = skb_dst(skb_pP)->dev;
} }
skb_pP->protocol = htons(ETH_P_IP);
skb->protocol = htons(ETH_P_IP); /* Send the GTPu message out...gggH */
err = dst_output(skb_pP); }
}
/* Send the GTPu message out...gggH */
err = dst_output(skb);
if (err == 0) if (err == 0)
{ {
...@@ -146,9 +151,9 @@ static bool _gtpuah_route_packet(struct sk_buff *skb, const struct xt_gtpuah_tar ...@@ -146,9 +151,9 @@ static bool _gtpuah_route_packet(struct sk_buff *skb, const struct xt_gtpuah_tar
} }
static unsigned int static unsigned int
_gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) _gtpuah_target_add(struct sk_buff *skb_pP, const struct xt_gtpuah_target_info *tgi_pP, char *in_dev_pP)
{ {
struct iphdr *iph = ip_hdr(skb); struct iphdr *iph = ip_hdr(skb_pP);
struct iphdr *iph2 = NULL; struct iphdr *iph2 = NULL;
struct udphdr *udph = NULL; struct udphdr *udph = NULL;
struct gtpuhdr *gtpuh = NULL; struct gtpuhdr *gtpuh = NULL;
...@@ -160,9 +165,9 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) ...@@ -160,9 +165,9 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi)
payload_to_send = 0, bytes_to_remove = 0, bytes_to_send = 0; payload_to_send = 0, bytes_to_remove = 0, bytes_to_send = 0;
int flags = 0, offset = 0, offset2 = 0; int flags = 0, offset = 0, offset2 = 0;
if (skb->mark == 0) { if (skb_pP->mark == 0) {
pr_info("GTPUAH: _gtpuah_target_add force skb mark %u to tgi mark %u", skb->mark, tgi->rtun); pr_info("GTPUAH: _gtpuah_target_add force info_pP mark %u to skb_pP mark %u", skb_pP->mark, tgi_pP->rtun);
skb->mark = tgi->rtun; skb_pP->mark = tgi_pP->rtun;
} }
/* Keep the length of the source IP packet */ /* Keep the length of the source IP packet */
orig_iplen = ntohs(iph->tot_len); orig_iplen = ntohs(iph->tot_len);
...@@ -173,12 +178,12 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) ...@@ -173,12 +178,12 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi)
/* Create a new copy of the original skb...can't avoid :-( */ /* Create a new copy of the original skb...can't avoid :-( */
if (((orig_iplen + headroom_reqd) <= MTU) || (flags & IP_DF)) { if (((orig_iplen + headroom_reqd) <= MTU) || (flags & IP_DF)) {
#if 0 #if 1
if (flags & IP_DF) { if (flags & IP_DF) {
pr_info("GTPUAH: DONT FRAGMENT id %04X", ntohs(iph->id)); pr_info("GTPUAH: DONT FRAGMENT id %04X", ntohs(iph->id));
} }
#endif #endif
new_skb = skb_copy_expand(skb, headroom_reqd + skb_headroom(skb), skb_tailroom(skb), GFP_ATOMIC); new_skb = skb_copy_expand(skb_pP, headroom_reqd + skb_headroom(skb_pP), skb_tailroom(skb_pP), GFP_ATOMIC);
if (new_skb == NULL) if (new_skb == NULL)
{ {
return NF_ACCEPT; return NF_ACCEPT;
...@@ -188,7 +193,7 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) ...@@ -188,7 +193,7 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi)
gtpuh->flags = 0x30; /* v1 and Protocol-type=GTP */ gtpuh->flags = 0x30; /* v1 and Protocol-type=GTP */
gtpuh->msgtype = 0xff; /* T-PDU */ gtpuh->msgtype = 0xff; /* T-PDU */
gtpuh->length = htons(orig_iplen); gtpuh->length = htons(orig_iplen);
gtpuh->tunid = htonl(skb->mark); gtpuh->tunid = htonl(skb_pP->mark);
/* Add UDP header */ /* Add UDP header */
udp_len = sizeof(struct udphdr) + sizeof(struct gtpuhdr) + orig_iplen; udp_len = sizeof(struct udphdr) + sizeof(struct gtpuhdr) + orig_iplen;
...@@ -197,8 +202,8 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) ...@@ -197,8 +202,8 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi)
udph->dest = htons(GTPU_PORT); udph->dest = htons(GTPU_PORT);
udph->len = htons(udp_len); udph->len = htons(udp_len);
udph->check = 0; udph->check = 0;
udph->check = csum_tcpudp_magic(tgi->laddr, udph->check = csum_tcpudp_magic(tgi_pP->laddr,
tgi->raddr, tgi_pP->raddr,
udp_len, udp_len,
IPPROTO_UDP, IPPROTO_UDP,
csum_partial((char*)udph, udp_len, 0)); csum_partial((char*)udph, udp_len, 0));
...@@ -215,14 +220,14 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) ...@@ -215,14 +220,14 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi)
iph->frag_off = 0; iph->frag_off = 0;
iph->ttl = 64; iph->ttl = 64;
iph->protocol = IPPROTO_UDP; iph->protocol = IPPROTO_UDP;
iph->saddr = (tgi->laddr); iph->saddr = (tgi_pP->laddr);
iph->daddr = (tgi->raddr); iph->daddr = (tgi_pP->raddr);
iph->check = 0; iph->check = 0;
iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
skb_set_network_header(new_skb, 0); skb_set_network_header(new_skb, 0);
/* Route the packet */ /* Route the packet */
if (_gtpuah_route_packet(new_skb, tgi) == GTPU_SUCCESS) if (_gtpuah_route_packet(new_skb, tgi_pP, in_dev_pP) == GTPU_SUCCESS)
{ {
/* Succeeded. Drop the original packet */ /* Succeeded. Drop the original packet */
return NF_DROP; return NF_DROP;
...@@ -233,7 +238,7 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) ...@@ -233,7 +238,7 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi)
return NF_ACCEPT; /* What should we do here ??? ACCEPT seems to be the best option */ return NF_ACCEPT; /* What should we do here ??? ACCEPT seems to be the best option */
} }
} else { } else {
new_skb = skb_copy_expand(skb, headroom_reqd + skb_headroom(skb), 0, GFP_ATOMIC); new_skb = skb_copy_expand(skb_pP, headroom_reqd + skb_headroom(skb_pP), 0, GFP_ATOMIC);
if (new_skb == NULL) if (new_skb == NULL)
{ {
return NF_ACCEPT; return NF_ACCEPT;
...@@ -250,7 +255,7 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) ...@@ -250,7 +255,7 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi)
gtpuh->flags = 0x30; /* v1 and Protocol-type=GTP */ gtpuh->flags = 0x30; /* v1 and Protocol-type=GTP */
gtpuh->msgtype = 0xff; /* T-PDU */ gtpuh->msgtype = 0xff; /* T-PDU */
gtpuh->length = htons(orig_iplen); gtpuh->length = htons(orig_iplen);
gtpuh->tunid = htonl(skb->mark); gtpuh->tunid = htonl(skb_pP->mark);
/* Add UDP header */ /* Add UDP header */
udp_len = sizeof(struct udphdr) + sizeof(struct gtpuhdr) + orig_iplen; udp_len = sizeof(struct udphdr) + sizeof(struct gtpuhdr) + orig_iplen;
...@@ -259,8 +264,8 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) ...@@ -259,8 +264,8 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi)
udph->dest = htons(GTPU_PORT); udph->dest = htons(GTPU_PORT);
udph->len = htons(udp_len); udph->len = htons(udp_len);
udph->check = 0; udph->check = 0;
udph->check = csum_tcpudp_magic(tgi->laddr, udph->check = csum_tcpudp_magic(tgi_pP->laddr,
tgi->raddr, tgi_pP->raddr,
udp_len, udp_len,
IPPROTO_UDP, IPPROTO_UDP,
csum_partial((char*)udph, udp_len, 0)); csum_partial((char*)udph, udp_len, 0));
...@@ -277,8 +282,8 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) ...@@ -277,8 +282,8 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi)
iph->frag_off = htons(IP_MORE_FRAGMENTS); iph->frag_off = htons(IP_MORE_FRAGMENTS);
iph->ttl = 64; iph->ttl = 64;
iph->protocol = IPPROTO_UDP; iph->protocol = IPPROTO_UDP;
iph->saddr = (tgi->laddr); iph->saddr = (tgi_pP->laddr);
iph->daddr = (tgi->raddr); iph->daddr = (tgi_pP->raddr);
iph->check = 0; iph->check = 0;
iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
skb_set_network_header(new_skb, 0); skb_set_network_header(new_skb, 0);
...@@ -286,7 +291,7 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) ...@@ -286,7 +291,7 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi)
skb_trim(new_skb, MTU - headroom_reqd); skb_trim(new_skb, MTU - headroom_reqd);
/* Route the packet */ /* Route the packet */
if (_gtpuah_route_packet(new_skb, tgi) == GTPU_SUCCESS) if (_gtpuah_route_packet(new_skb, tgi_pP, in_dev_pP) == GTPU_SUCCESS)
{ {
remaining_payload_to_send = orig_iplen - MTU + headroom_reqd; remaining_payload_to_send = orig_iplen - MTU + headroom_reqd;
remaining_bytes_to_send = remaining_payload_to_send + sizeof(struct iphdr); remaining_bytes_to_send = remaining_payload_to_send + sizeof(struct iphdr);
...@@ -308,7 +313,7 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) ...@@ -308,7 +313,7 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi)
payload_to_send, payload_to_send,
bytes_to_send); bytes_to_send);
// Not optimal, TO OPTIMIZE copies // Not optimal, TO OPTIMIZE copies
new_skb2 = skb_copy(skb, GFP_ATOMIC); new_skb2 = skb_copy(skb_pP, GFP_ATOMIC);
skb_pull(new_skb2, bytes_to_remove); skb_pull(new_skb2, bytes_to_remove);
/* Add IP header */ /* Add IP header */
iph2 = (struct iphdr*)skb_push(new_skb2, sizeof(struct iphdr)); iph2 = (struct iphdr*)skb_push(new_skb2, sizeof(struct iphdr));
...@@ -320,14 +325,14 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) ...@@ -320,14 +325,14 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi)
iph2->frag_off = htons(offset2 / 8); iph2->frag_off = htons(offset2 / 8);
iph2->ttl = 64; iph2->ttl = 64;
iph2->protocol = IPPROTO_UDP; iph2->protocol = IPPROTO_UDP;
iph2->saddr = (tgi->laddr); iph2->saddr = (tgi_pP->laddr);
iph2->daddr = (tgi->raddr); iph2->daddr = (tgi_pP->raddr);
iph2->check = 0; iph2->check = 0;
iph2->check = ip_fast_csum((unsigned char *)iph2, iph2->ihl); iph2->check = ip_fast_csum((unsigned char *)iph2, iph2->ihl);
skb_set_network_header(new_skb2, 0); skb_set_network_header(new_skb2, 0);
skb_trim(new_skb2, MTU); skb_trim(new_skb2, MTU);
_gtpuah_route_packet(new_skb2, tgi); _gtpuah_route_packet(new_skb2, tgi_pP, in_dev_pP);
remaining_payload_to_send = remaining_payload_to_send - payload_to_send; remaining_payload_to_send = remaining_payload_to_send - payload_to_send;
bytes_to_remove = bytes_to_remove + payload_to_send; bytes_to_remove = bytes_to_remove + payload_to_send;
...@@ -346,19 +351,19 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi) ...@@ -346,19 +351,19 @@ _gtpuah_target_add(struct sk_buff *skb, const struct xt_gtpuah_target_info *tgi)
static unsigned int static unsigned int
xt_gtpuah_target(struct sk_buff *skb, const struct xt_action_param *par) xt_gtpuah_target(struct sk_buff *skb_pP, const struct xt_action_param *par_pP)
{ {
const struct xt_gtpuah_target_info *tgi = par->targinfo; const struct xt_gtpuah_target_info *tgi_p = par_pP->targinfo;
int result = NF_ACCEPT; int result = NF_ACCEPT;
if (tgi == NULL) if (tgi_p == NULL)
{ {
return result; return result;
} }
if (tgi->action == PARAM_GTPUAH_ACTION_ADD) if (tgi_p->action == PARAM_GTPUAH_ACTION_ADD)
{ {
result = _gtpuah_target_add(skb, tgi); result = _gtpuah_target_add(skb_pP, tgi_p, par_pP->in->name);
} }
return result; return result;
} }
...@@ -368,8 +373,9 @@ static struct xt_target xt_gtpuah_reg __read_mostly = ...@@ -368,8 +373,9 @@ static struct xt_target xt_gtpuah_reg __read_mostly =
.name = "GTPUAH", .name = "GTPUAH",
.revision = 0, .revision = 0,
.family = AF_INET, .family = AF_INET,
.hooks = (1 << NF_INET_FORWARD) | .hooks = (1 << NF_INET_FORWARD) |
(1 << NF_INET_POST_ROUTING), (1 << NF_INET_POST_ROUTING) |
(1 << NF_INET_LOCAL_OUT),
.table = "mangle", .table = "mangle",
.target = xt_gtpuah_target, .target = xt_gtpuah_target,
.targetsize = sizeof(struct xt_gtpuah_target_info), .targetsize = sizeof(struct xt_gtpuah_target_info),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment