18c2ecf20Sopenharmony_ci/* Copyright (c) 2016 Facebook
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or
48c2ecf20Sopenharmony_ci * modify it under the terms of version 2 of the GNU General Public
58c2ecf20Sopenharmony_ci * License as published by the Free Software Foundation.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#define KBUILD_MODNAME "foo"
88c2ecf20Sopenharmony_ci#include <linux/ip.h>
98c2ecf20Sopenharmony_ci#include <linux/ipv6.h>
108c2ecf20Sopenharmony_ci#include <linux/in.h>
118c2ecf20Sopenharmony_ci#include <linux/tcp.h>
128c2ecf20Sopenharmony_ci#include <linux/udp.h>
138c2ecf20Sopenharmony_ci#include <uapi/linux/bpf.h>
148c2ecf20Sopenharmony_ci#include <net/ip.h>
158c2ecf20Sopenharmony_ci#include <bpf/bpf_helpers.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define DEFAULT_PKTGEN_UDP_PORT 9
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* copy of 'struct ethhdr' without __packed */
208c2ecf20Sopenharmony_cistruct eth_hdr {
218c2ecf20Sopenharmony_ci	unsigned char   h_dest[ETH_ALEN];
228c2ecf20Sopenharmony_ci	unsigned char   h_source[ETH_ALEN];
238c2ecf20Sopenharmony_ci	unsigned short  h_proto;
248c2ecf20Sopenharmony_ci};
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ciSEC("simple")
278c2ecf20Sopenharmony_ciint handle_ingress(struct __sk_buff *skb)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	void *data = (void *)(long)skb->data;
308c2ecf20Sopenharmony_ci	struct eth_hdr *eth = data;
318c2ecf20Sopenharmony_ci	struct iphdr *iph = data + sizeof(*eth);
328c2ecf20Sopenharmony_ci	struct udphdr *udp = data + sizeof(*eth) + sizeof(*iph);
338c2ecf20Sopenharmony_ci	void *data_end = (void *)(long)skb->data_end;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	/* single length check */
368c2ecf20Sopenharmony_ci	if (data + sizeof(*eth) + sizeof(*iph) + sizeof(*udp) > data_end)
378c2ecf20Sopenharmony_ci		return 0;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	if (eth->h_proto != htons(ETH_P_IP))
408c2ecf20Sopenharmony_ci		return 0;
418c2ecf20Sopenharmony_ci	if (iph->protocol != IPPROTO_UDP || iph->ihl != 5)
428c2ecf20Sopenharmony_ci		return 0;
438c2ecf20Sopenharmony_ci	if (ip_is_fragment(iph))
448c2ecf20Sopenharmony_ci		return 0;
458c2ecf20Sopenharmony_ci	if (udp->dest == htons(DEFAULT_PKTGEN_UDP_PORT))
468c2ecf20Sopenharmony_ci		return TC_ACT_SHOT;
478c2ecf20Sopenharmony_ci	return 0;
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_cichar _license[] SEC("license") = "GPL";
50