Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 1 | // Copyright (c) PLUMgrid, Inc. |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License") |
| 3 | |
| 4 | #include <bcc/proto.h> |
| 5 | |
| 6 | struct ipkey { |
| 7 | u32 client_ip; |
| 8 | }; |
| 9 | |
| 10 | BPF_TABLE("hash", struct ipkey, int, learned_ips, 1024); |
| 11 | |
| 12 | // trivial action |
| 13 | int 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 |
| 21 | int 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 | } |
| 37 | EOP: |
| 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. |
| 44 | int 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; |
Brenden Blanco | a5f3eda | 2015-06-16 17:26:46 -0700 | [diff] [blame^] | 56 | learned_ips.update(&key, &val); |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 57 | goto EOP; |
| 58 | } |
| 59 | EOP: |
| 60 | return 1; |
| 61 | } |