1 /* Copyright (c) 2015 PLUMgrid, http://plumgrid.com
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.
7 #include <uapi/linux/bpf.h>
8 #include "bpf_helpers.h"
9 #include <uapi/linux/in.h>
10 #include <uapi/linux/if.h>
11 #include <uapi/linux/if_ether.h>
12 #include <uapi/linux/ip.h>
13 #include <uapi/linux/ipv6.h>
14 #include <uapi/linux/if_tunnel.h>
15 #include <uapi/linux/mpls.h>
17 #define IP_OFFSET 0x1FFF
19 #define PROG(F) SEC("socket/"__stringify(F)) int bpf_func_##F
21 struct bpf_map_def
SEC("maps") jmp_table
= {
22 .type
= BPF_MAP_TYPE_PROG_ARRAY
,
23 .key_size
= sizeof(u32
),
24 .value_size
= sizeof(u32
),
33 /* protocol dispatch routine.
34 * It tail-calls next BPF program depending on eth proto
35 * Note, we could have used:
36 * bpf_tail_call(skb, &jmp_table, proto);
37 * but it would need large prog_array
39 static inline void parse_eth_proto(struct __sk_buff
*skb
, u32 proto
)
44 bpf_tail_call(skb
, &jmp_table
, PARSE_VLAN
);
48 bpf_tail_call(skb
, &jmp_table
, PARSE_MPLS
);
51 bpf_tail_call(skb
, &jmp_table
, PARSE_IP
);
54 bpf_tail_call(skb
, &jmp_table
, PARSE_IPV6
);
61 __be16 h_vlan_encapsulated_proto
;
64 struct bpf_flow_keys
{
74 static inline int ip_is_fragment(struct __sk_buff
*ctx
, __u64 nhoff
)
76 return load_half(ctx
, nhoff
+ offsetof(struct iphdr
, frag_off
))
77 & (IP_MF
| IP_OFFSET
);
80 static inline __u32
ipv6_addr_hash(struct __sk_buff
*ctx
, __u64 off
)
82 __u64 w0
= load_word(ctx
, off
);
83 __u64 w1
= load_word(ctx
, off
+ 4);
84 __u64 w2
= load_word(ctx
, off
+ 8);
85 __u64 w3
= load_word(ctx
, off
+ 12);
87 return (__u32
)(w0
^ w1
^ w2
^ w3
);
91 struct bpf_flow_keys flow
;
94 struct bpf_map_def
SEC("maps") percpu_map
= {
95 .type
= BPF_MAP_TYPE_ARRAY
,
96 .key_size
= sizeof(__u32
),
97 .value_size
= sizeof(struct globals
),
101 /* user poor man's per_cpu until native support is ready */
102 static struct globals
*this_cpu_globals(void)
104 u32 key
= bpf_get_smp_processor_id();
106 return bpf_map_lookup_elem(&percpu_map
, &key
);
109 /* some simple stats for user space consumption */
115 struct bpf_map_def
SEC("maps") hash_map
= {
116 .type
= BPF_MAP_TYPE_HASH
,
117 .key_size
= sizeof(struct bpf_flow_keys
),
118 .value_size
= sizeof(struct pair
),
122 static void update_stats(struct __sk_buff
*skb
, struct globals
*g
)
124 struct bpf_flow_keys key
= g
->flow
;
127 value
= bpf_map_lookup_elem(&hash_map
, &key
);
129 __sync_fetch_and_add(&value
->packets
, 1);
130 __sync_fetch_and_add(&value
->bytes
, skb
->len
);
132 struct pair val
= {1, skb
->len
};
134 bpf_map_update_elem(&hash_map
, &key
, &val
, BPF_ANY
);
138 static __always_inline
void parse_ip_proto(struct __sk_buff
*skb
,
139 struct globals
*g
, __u32 ip_proto
)
141 __u32 nhoff
= skb
->cb
[0];
151 __u32 gre_flags
= load_half(skb
,
152 nhoff
+ offsetof(struct gre_hdr
, flags
));
153 __u32 gre_proto
= load_half(skb
,
154 nhoff
+ offsetof(struct gre_hdr
, proto
));
156 if (gre_flags
& (GRE_VERSION
|GRE_ROUTING
))
160 if (gre_flags
& GRE_CSUM
)
162 if (gre_flags
& GRE_KEY
)
164 if (gre_flags
& GRE_SEQ
)
168 parse_eth_proto(skb
, gre_proto
);
172 parse_eth_proto(skb
, ETH_P_IP
);
175 parse_eth_proto(skb
, ETH_P_IPV6
);
179 g
->flow
.ports
= load_word(skb
, nhoff
);
181 g
->flow
.ip_proto
= ip_proto
;
182 update_stats(skb
, g
);
189 PROG(PARSE_IP
)(struct __sk_buff
*skb
)
191 struct globals
*g
= this_cpu_globals();
192 __u32 nhoff
, verlen
, ip_proto
;
199 if (unlikely(ip_is_fragment(skb
, nhoff
)))
202 ip_proto
= load_byte(skb
, nhoff
+ offsetof(struct iphdr
, protocol
));
204 if (ip_proto
!= IPPROTO_GRE
) {
205 g
->flow
.src
= load_word(skb
, nhoff
+ offsetof(struct iphdr
, saddr
));
206 g
->flow
.dst
= load_word(skb
, nhoff
+ offsetof(struct iphdr
, daddr
));
209 verlen
= load_byte(skb
, nhoff
+ 0/*offsetof(struct iphdr, ihl)*/);
210 nhoff
+= (verlen
& 0xF) << 2;
213 parse_ip_proto(skb
, g
, ip_proto
);
217 PROG(PARSE_IPV6
)(struct __sk_buff
*skb
)
219 struct globals
*g
= this_cpu_globals();
220 __u32 nhoff
, ip_proto
;
227 ip_proto
= load_byte(skb
,
228 nhoff
+ offsetof(struct ipv6hdr
, nexthdr
));
229 g
->flow
.src
= ipv6_addr_hash(skb
,
230 nhoff
+ offsetof(struct ipv6hdr
, saddr
));
231 g
->flow
.dst
= ipv6_addr_hash(skb
,
232 nhoff
+ offsetof(struct ipv6hdr
, daddr
));
233 nhoff
+= sizeof(struct ipv6hdr
);
236 parse_ip_proto(skb
, g
, ip_proto
);
240 PROG(PARSE_VLAN
)(struct __sk_buff
*skb
)
246 proto
= load_half(skb
, nhoff
+ offsetof(struct vlan_hdr
,
247 h_vlan_encapsulated_proto
));
248 nhoff
+= sizeof(struct vlan_hdr
);
251 parse_eth_proto(skb
, proto
);
256 PROG(PARSE_MPLS
)(struct __sk_buff
*skb
)
262 label
= load_word(skb
, nhoff
);
263 nhoff
+= sizeof(struct mpls_label
);
266 if (label
& MPLS_LS_S_MASK
) {
267 __u8 verlen
= load_byte(skb
, nhoff
);
268 if ((verlen
& 0xF0) == 4)
269 parse_eth_proto(skb
, ETH_P_IP
);
271 parse_eth_proto(skb
, ETH_P_IPV6
);
273 parse_eth_proto(skb
, ETH_P_MPLS_UC
);
280 int main_prog(struct __sk_buff
*skb
)
282 __u32 nhoff
= ETH_HLEN
;
283 __u32 proto
= load_half(skb
, 12);
286 parse_eth_proto(skb
, proto
);
290 char _license
[] SEC("license") = "GPL";