blob: 6ba7d35c808c8c1fea68126ee6a6725ee83bb886 [file] [log] [blame]
Brenden Blanco2cea0cd2015-06-16 13:50:16 -07001// Copyright (c) PLUMgrid, Inc.
2// Licensed under the Apache License, Version 2.0 (the "License")
3
4#include <bcc/proto.h>
5
6struct ipkey {
7 u32 client_ip;
8};
9
10BPF_TABLE("hash", struct ipkey, int, learned_ips, 1024);
11
12// trivial action
13int pass(struct __sk_buff *skb) {
14 return 1;
15}
16
17// Process each wan packet, and determine if the packet is in the IP
18// table or not. Learned IPs are rate-limited and unclassified are not.
19// returns: > 0 when an IP is known
20// = 0 when an IP is not known, or non-IP traffic
21int classify_wan(struct __sk_buff *skb) {
22 BEGIN(ethernet);
23 PROTO(ethernet) {
24 switch (ethernet->type) {
25 case 0x0800: goto ip;
26 }
27 goto EOP;
28 }
29 PROTO(ip) {
30 u32 dip = ip->dst;
31 struct ipkey key = {.client_ip=dip};
32 int *val = learned_ips.lookup(&key);
33 if (val)
34 return *val;
35 goto EOP;
36 }
37EOP:
38 return 0;
39}
40
41// Process each neighbor packet, and store the source IP in the learned table.
42// Mark the inserted entry with a non-zero value to be used by the classify_wan
43// lookup.
44int classify_neighbor(struct __sk_buff *skb) {
45 BEGIN(ethernet);
46 PROTO(ethernet) {
47 switch (ethernet->type) {
48 case 0x0800: goto ip;
49 }
50 goto EOP;
51 }
52 PROTO(ip) {
53 u32 sip = ip->src;
54 struct ipkey key = {.client_ip=sip};
55 int val = 1;
56 learned_ips.lookup_or_init(&key, &val);
57 goto EOP;
58 }
59EOP:
60 return 1;
61}