1 /* Copyright (c) 2016 VMware
2 * Copyright (c) 2016 Facebook
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 #include <uapi/linux/bpf.h>
9 #include <uapi/linux/if_ether.h>
10 #include <uapi/linux/if_packet.h>
11 #include <uapi/linux/ip.h>
12 #include <uapi/linux/in.h>
13 #include <uapi/linux/tcp.h>
14 #include <uapi/linux/filter.h>
15 #include <uapi/linux/pkt_cls.h>
16 #include "bpf_helpers.h"
18 #define ERROR(ret) do {\
19 char fmt[] = "ERROR line:%d ret:%d\n";\
20 bpf_trace_printk(fmt, sizeof(fmt), __LINE__, ret); \
30 u8 opt_data[8]; /* hard-coded to 8 byte */
33 struct vxlan_metadata {
38 int _gre_set_tunnel(struct __sk_buff *skb)
41 struct bpf_tunnel_key key;
43 __builtin_memset(&key, 0x0, sizeof(key));
44 key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
49 ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_ZERO_CSUM_TX);
59 int _gre_get_tunnel(struct __sk_buff *skb)
62 struct bpf_tunnel_key key;
63 char fmt[] = "key %d remote ip 0x%x\n";
65 ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
71 bpf_trace_printk(fmt, sizeof(fmt), key.tunnel_id, key.remote_ipv4);
75 SEC("vxlan_set_tunnel")
76 int _vxlan_set_tunnel(struct __sk_buff *skb)
79 struct bpf_tunnel_key key;
80 struct vxlan_metadata md;
82 __builtin_memset(&key, 0x0, sizeof(key));
83 key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
88 ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_ZERO_CSUM_TX);
94 md.gbp = 0x800FF; /* Set VXLAN Group Policy extension */
95 ret = bpf_skb_set_tunnel_opt(skb, &md, sizeof(md));
104 SEC("vxlan_get_tunnel")
105 int _vxlan_get_tunnel(struct __sk_buff *skb)
108 struct bpf_tunnel_key key;
109 struct vxlan_metadata md;
110 char fmt[] = "key %d remote ip 0x%x vxlan gbp 0x%x\n";
112 ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
118 ret = bpf_skb_get_tunnel_opt(skb, &md, sizeof(md));
124 bpf_trace_printk(fmt, sizeof(fmt),
125 key.tunnel_id, key.remote_ipv4, md.gbp);
130 SEC("geneve_set_tunnel")
131 int _geneve_set_tunnel(struct __sk_buff *skb)
134 struct bpf_tunnel_key key;
135 struct geneve_opt gopt;
137 __builtin_memset(&key, 0x0, sizeof(key));
138 key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
143 __builtin_memset(&gopt, 0x0, sizeof(gopt));
144 gopt.opt_class = 0x102; /* Open Virtual Networking (OVN) */
149 gopt.length = 2; /* 4-byte multiple */
150 *(int *) &gopt.opt_data = 0xdeadbeef;
152 ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), BPF_F_ZERO_CSUM_TX);
158 ret = bpf_skb_set_tunnel_opt(skb, &gopt, sizeof(gopt));
167 SEC("geneve_get_tunnel")
168 int _geneve_get_tunnel(struct __sk_buff *skb)
171 struct bpf_tunnel_key key;
172 struct geneve_opt gopt;
173 char fmt[] = "key %d remote ip 0x%x geneve class 0x%x\n";
175 ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
181 ret = bpf_skb_get_tunnel_opt(skb, &gopt, sizeof(gopt));
187 bpf_trace_printk(fmt, sizeof(fmt),
188 key.tunnel_id, key.remote_ipv4, gopt.opt_class);
192 SEC("ipip_set_tunnel")
193 int _ipip_set_tunnel(struct __sk_buff *skb)
195 struct bpf_tunnel_key key = {};
196 void *data = (void *)(long)skb->data;
197 struct iphdr *iph = data;
198 struct tcphdr *tcp = data + sizeof(*iph);
199 void *data_end = (void *)(long)skb->data_end;
202 /* single length check */
203 if (data + sizeof(*iph) + sizeof(*tcp) > data_end) {
209 if (iph->protocol == IPPROTO_ICMP) {
210 key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
212 if (iph->protocol != IPPROTO_TCP || iph->ihl != 5)
215 if (tcp->dest == htons(5200))
216 key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */
217 else if (tcp->dest == htons(5201))
218 key.remote_ipv4 = 0xac100165; /* 172.16.1.101 */
223 ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), 0);
232 SEC("ipip_get_tunnel")
233 int _ipip_get_tunnel(struct __sk_buff *skb)
236 struct bpf_tunnel_key key;
237 char fmt[] = "remote ip 0x%x\n";
239 ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
245 bpf_trace_printk(fmt, sizeof(fmt), key.remote_ipv4);
249 char _license[] SEC("license") = "GPL";