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 | |
Teng Qin | 7a3e5bc | 2017-03-29 13:39:17 -0700 | [diff] [blame] | 10 | BPF_HASH(learned_ips, struct ipkey, int, 1024); |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 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) { |
Brenden Blanco | 6ec65e4 | 2015-07-02 10:02:04 -0700 | [diff] [blame] | 22 | u8 *cursor = 0; |
| 23 | ethernet: { |
| 24 | struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet)); |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 25 | switch (ethernet->type) { |
Brenden Blanco | 6ec65e4 | 2015-07-02 10:02:04 -0700 | [diff] [blame] | 26 | case ETH_P_IP: goto ip; |
| 27 | default: goto EOP; |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 28 | } |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 29 | } |
Brenden Blanco | 6ec65e4 | 2015-07-02 10:02:04 -0700 | [diff] [blame] | 30 | ip: { |
| 31 | struct ip_t *ip = cursor_advance(cursor, sizeof(*ip)); |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 32 | u32 dip = ip->dst; |
| 33 | struct ipkey key = {.client_ip=dip}; |
| 34 | int *val = learned_ips.lookup(&key); |
| 35 | if (val) |
| 36 | return *val; |
| 37 | goto EOP; |
| 38 | } |
| 39 | EOP: |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | // Process each neighbor packet, and store the source IP in the learned table. |
| 44 | // Mark the inserted entry with a non-zero value to be used by the classify_wan |
| 45 | // lookup. |
| 46 | int classify_neighbor(struct __sk_buff *skb) { |
Brenden Blanco | 6ec65e4 | 2015-07-02 10:02:04 -0700 | [diff] [blame] | 47 | u8 *cursor = 0; |
| 48 | ethernet: { |
| 49 | struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet)); |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 50 | switch (ethernet->type) { |
Brenden Blanco | 6ec65e4 | 2015-07-02 10:02:04 -0700 | [diff] [blame] | 51 | case ETH_P_IP: goto ip; |
| 52 | default: goto EOP; |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 53 | } |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 54 | } |
Brenden Blanco | 6ec65e4 | 2015-07-02 10:02:04 -0700 | [diff] [blame] | 55 | ip: { |
| 56 | struct ip_t *ip = cursor_advance(cursor, sizeof(*ip)); |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 57 | u32 sip = ip->src; |
| 58 | struct ipkey key = {.client_ip=sip}; |
| 59 | int val = 1; |
Brenden Blanco | a5f3eda | 2015-06-16 17:26:46 -0700 | [diff] [blame] | 60 | learned_ips.update(&key, &val); |
Brenden Blanco | 2cea0cd | 2015-06-16 13:50:16 -0700 | [diff] [blame] | 61 | goto EOP; |
| 62 | } |
| 63 | EOP: |
| 64 | return 1; |
| 65 | } |