Skip to content

Instantly share code, notes, and snippets.

@mauriciovasquezbernal
Created March 1, 2017 14:21
Show Gist options
  • Save mauriciovasquezbernal/1c7452fafe6ceac1055c396e7e1821e2 to your computer and use it in GitHub Desktop.
Save mauriciovasquezbernal/1c7452fafe6ceac1055c396e7e1821e2 to your computer and use it in GitHub Desktop.
// Copyright 2017 Politecnico di Torino
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package null
var null_code = `
#include <bcc/proto.h>
#include <bcc/helpers.h>
#include <uapi/linux/bpf.h>
#include <uapi/linux/icmp.h>
#include <uapi/linux/if_ether.h>
#include <uapi/linux/if_packet.h>
#include <uapi/linux/ip.h>
#include <uapi/linux/in.h>
#include <uapi/linux/filter.h>
#include <uapi/linux/pkt_cls.h>
#define ICMP_CSUM_OFFSET (sizeof(struct eth_hdr) + sizeof(struct iphdr) + offsetof(struct icmphdr, checksum))
#define IP (0x0701A8C0) // 192.168.1.7
struct eth_hdr {
__be64 dst:48;
__be64 src:48;
__be16 proto;
} __attribute__((packed));
static int handle_rx(void *skb, struct metadata *md) {
struct __sk_buff *skb2 = (struct __sk_buff *)skb;
void *data = (void *)(long)skb2->data;
void *data_end = (void *)(long)skb2->data_end;
struct eth_hdr *eth = data;
struct iphdr *ip = data + sizeof(*eth);
struct icmphdr *icmp = data + sizeof(*eth) + sizeof(*ip);
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*icmp) > data_end)
goto DROP;
if (eth->proto != htons(ETH_P_IP))
goto DROP;
if (ip->protocol != IPPROTO_ICMP)
goto DROP;
if (icmp->type != ICMP_ECHO)
goto DROP;
if (ip->daddr != IP)
goto DROP;
//__u8 old_type = icmp->type;
//__u8 new_type = (__u8) ICMP_ECHOREPLY;
//bpf_l4_csum_replace(skb2, ICMP_CSUM_OFFSET, old_type, new_type, 2);
int r = bpf_csum_update(skb2, 0xFFFF);
bpf_trace_printk("r is %d\n", r);
bpf_trace_printk("r is %x\n", r);
//icmp->type = new_type;
//__be32 old_src = ip->saddr;
//__be32 old_dst = ip->daddr;
//
//ip->daddr = old_src;
//ip->saddr = old_dst;
//
//__be64 old_src_mac = eth->src;
//__be64 old_dst_mac = eth->dst;
//eth->src = old_dst_mac;
//eth->dst = old_src_mac;
pkt_redirect(skb, md, md->in_ifc);
return RX_REDIRECT;
DROP:
return RX_DROP;
}
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment