blob: 6594862787fef19f9ac0ffb8c944adf96b57e313 [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
Teng Qin7a3e5bc2017-03-29 13:39:17 -070010BPF_HASH(learned_ips, struct ipkey, int, 1024);
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070011
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) {
Brenden Blanco6ec65e42015-07-02 10:02:04 -070022 u8 *cursor = 0;
23 ethernet: {
24 struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070025 switch (ethernet->type) {
Brenden Blanco6ec65e42015-07-02 10:02:04 -070026 case ETH_P_IP: goto ip;
27 default: goto EOP;
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070028 }
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070029 }
Brenden Blanco6ec65e42015-07-02 10:02:04 -070030 ip: {
31 struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070032 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 }
39EOP:
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.
46int classify_neighbor(struct __sk_buff *skb) {
Brenden Blanco6ec65e42015-07-02 10:02:04 -070047 u8 *cursor = 0;
48 ethernet: {
49 struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070050 switch (ethernet->type) {
Brenden Blanco6ec65e42015-07-02 10:02:04 -070051 case ETH_P_IP: goto ip;
52 default: goto EOP;
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070053 }
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070054 }
Brenden Blanco6ec65e42015-07-02 10:02:04 -070055 ip: {
56 struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070057 u32 sip = ip->src;
58 struct ipkey key = {.client_ip=sip};
59 int val = 1;
Paul Chaignon6ceb3292017-04-01 18:23:58 +020060 learned_ips.insert(&key, &val);
Brenden Blanco2cea0cd2015-06-16 13:50:16 -070061 goto EOP;
62 }
63EOP:
64 return 1;
65}