]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/testing/selftests/bpf/test_pkt_access.c
bpf: Move endianness BPF helpers out of bpf_util.h
[mirror_ubuntu-artful-kernel.git] / tools / testing / selftests / bpf / test_pkt_access.c
CommitLineData
6882804c
AS
1/* Copyright (c) 2017 Facebook
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#include <stddef.h>
8#include <linux/bpf.h>
9#include <linux/if_ether.h>
10#include <linux/if_packet.h>
11#include <linux/ip.h>
12#include <linux/ipv6.h>
13#include <linux/in.h>
14#include <linux/tcp.h>
15#include <linux/pkt_cls.h>
16#include "bpf_helpers.h"
bc1bafbb 17#include "bpf_endian.h"
6882804c 18
6882804c
AS
19#define barrier() __asm__ __volatile__("": : :"memory")
20int _version SEC("version") = 1;
21
22SEC("test1")
23int process(struct __sk_buff *skb)
24{
25 void *data_end = (void *)(long)skb->data_end;
26 void *data = (void *)(long)skb->data;
27 struct ethhdr *eth = (struct ethhdr *)(data);
28 struct tcphdr *tcp = NULL;
29 __u8 proto = 255;
30 __u64 ihl_len;
31
32 if (eth + 1 > data_end)
33 return TC_ACT_SHOT;
34
43bcf707 35 if (eth->h_proto == bpf_htons(ETH_P_IP)) {
6882804c
AS
36 struct iphdr *iph = (struct iphdr *)(eth + 1);
37
38 if (iph + 1 > data_end)
39 return TC_ACT_SHOT;
40 ihl_len = iph->ihl * 4;
41 proto = iph->protocol;
42 tcp = (struct tcphdr *)((void *)(iph) + ihl_len);
43bcf707 43 } else if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
6882804c
AS
44 struct ipv6hdr *ip6h = (struct ipv6hdr *)(eth + 1);
45
46 if (ip6h + 1 > data_end)
47 return TC_ACT_SHOT;
48 ihl_len = sizeof(*ip6h);
49 proto = ip6h->nexthdr;
50 tcp = (struct tcphdr *)((void *)(ip6h) + ihl_len);
51 }
52
53 if (tcp) {
54 if (((void *)(tcp) + 20) > data_end || proto != 6)
55 return TC_ACT_SHOT;
56 barrier(); /* to force ordering of checks */
57 if (((void *)(tcp) + 18) > data_end)
58 return TC_ACT_SHOT;
59 if (tcp->urg_ptr == 123)
60 return TC_ACT_OK;
61 }
62
63 return TC_ACT_UNSPEC;
64}