blob: 634f19654071a0315154e247dcbb9f183ded28c9 [file] [log] [blame]
Brenden Blanco246b9422015-06-05 11:15:27 -07001// Copyright (c) PLUMgrid, Inc.
2// Licensed under the Apache License, Version 2.0 (the "License")
Brenden Blanco83102912015-06-09 17:43:27 -07003#include <bcc/proto.h>
Brenden Blanco22a419b2015-05-29 11:14:20 -07004struct IPKey {
5 u32 dip;
6 u32 sip;
7};
8struct IPLeaf {
9 u32 xdip;
10 u32 xsip;
Brenden Blancobb7200c2015-06-04 18:01:42 -070011 u64 ip_xlated_pkts;
12 u64 arp_xlated_pkts;
Brenden Blanco22a419b2015-05-29 11:14:20 -070013};
14BPF_TABLE("hash", struct IPKey, struct IPLeaf, xlate, 1024);
15
Brenden Blanco22a419b2015-05-29 11:14:20 -070016int on_packet(struct __sk_buff *skb) {
Brenden Blanco6ec65e42015-07-02 10:02:04 -070017 u8 *cursor = 0;
Brenden Blanco22a419b2015-05-29 11:14:20 -070018
19 u32 orig_dip = 0;
20 u32 orig_sip = 0;
Brenden Blanco35c72252016-11-23 16:26:01 -080021 struct IPLeaf xleaf = {};
Brenden Blanco22a419b2015-05-29 11:14:20 -070022
Brenden Blanco6ec65e42015-07-02 10:02:04 -070023 ethernet: {
24 struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
Brenden Blanco22a419b2015-05-29 11:14:20 -070025 switch (ethernet->type) {
Brenden Blanco6ec65e42015-07-02 10:02:04 -070026 case ETH_P_IP: goto ip;
27 case ETH_P_ARP: goto arp;
28 case ETH_P_8021Q: goto dot1q;
29 default: goto EOP;
Brenden Blanco22a419b2015-05-29 11:14:20 -070030 }
Brenden Blanco22a419b2015-05-29 11:14:20 -070031 }
32
Brenden Blanco6ec65e42015-07-02 10:02:04 -070033 dot1q: {
34 struct dot1q_t *dot1q = cursor_advance(cursor, sizeof(*dot1q));
Brenden Blanco22a419b2015-05-29 11:14:20 -070035 switch (dot1q->type) {
Brenden Blanco6ec65e42015-07-02 10:02:04 -070036 case ETH_P_IP: goto ip;
37 case ETH_P_ARP: goto arp;
38 default: goto EOP;
Brenden Blanco22a419b2015-05-29 11:14:20 -070039 }
Brenden Blanco22a419b2015-05-29 11:14:20 -070040 }
Brenden Blanco6ec65e42015-07-02 10:02:04 -070041
42 arp: {
43 struct arp_t *arp = cursor_advance(cursor, sizeof(*arp));
Brenden Blancobb7200c2015-06-04 18:01:42 -070044 orig_dip = arp->tpa;
45 orig_sip = arp->spa;
46 struct IPKey key = {.dip=orig_dip, .sip=orig_sip};
Brenden Blanco35c72252016-11-23 16:26:01 -080047 struct IPLeaf *xleafp = xlate.lookup(&key);
48 if (xleafp) {
49 xleaf = *xleafp;
50 arp->tpa = xleaf.xdip;
51 arp->spa = xleaf.xsip;
52 lock_xadd(&xleafp->arp_xlated_pkts, 1);
Brenden Blancobb7200c2015-06-04 18:01:42 -070053 }
54 goto EOP;
55 }
Brenden Blanco22a419b2015-05-29 11:14:20 -070056
Brenden Blanco6ec65e42015-07-02 10:02:04 -070057 ip: {
58 struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
Brenden Blanco22a419b2015-05-29 11:14:20 -070059 orig_dip = ip->dst;
60 orig_sip = ip->src;
61 struct IPKey key = {.dip=orig_dip, .sip=orig_sip};
Brenden Blanco35c72252016-11-23 16:26:01 -080062 struct IPLeaf *xleafp = xlate.lookup(&key);
63 if (xleafp) {
64 xleaf = *xleafp;
65 ip->dst = xleaf.xdip;
66 incr_cksum_l3(&ip->hchecksum, orig_dip, xleaf.xdip);
67 ip->src = xleaf.xsip;
68 incr_cksum_l3(&ip->hchecksum, orig_sip, xleaf.xsip);
69 lock_xadd(&xleafp->ip_xlated_pkts, 1);
Brenden Blanco22a419b2015-05-29 11:14:20 -070070 }
71 switch (ip->nextp) {
72 case 6: goto tcp;
73 case 17: goto udp;
Brenden Blanco6ec65e42015-07-02 10:02:04 -070074 default: goto EOP;
Brenden Blanco22a419b2015-05-29 11:14:20 -070075 }
Brenden Blanco22a419b2015-05-29 11:14:20 -070076 }
77
Brenden Blanco6ec65e42015-07-02 10:02:04 -070078 udp: {
79 struct udp_t *udp = cursor_advance(cursor, sizeof(*udp));
Brenden Blanco35c72252016-11-23 16:26:01 -080080 if (xleaf.xdip) {
81 incr_cksum_l4(&udp->crc, orig_dip, xleaf.xdip, 1);
82 incr_cksum_l4(&udp->crc, orig_sip, xleaf.xsip, 1);
Brenden Blanco22a419b2015-05-29 11:14:20 -070083 }
84 goto EOP;
85 }
86
Brenden Blanco6ec65e42015-07-02 10:02:04 -070087 tcp: {
88 struct tcp_t *tcp = cursor_advance(cursor, sizeof(*tcp));
Brenden Blanco35c72252016-11-23 16:26:01 -080089 if (xleaf.xdip) {
90 incr_cksum_l4(&tcp->cksum, orig_dip, xleaf.xdip, 1);
91 incr_cksum_l4(&tcp->cksum, orig_sip, xleaf.xsip, 1);
Brenden Blanco22a419b2015-05-29 11:14:20 -070092 }
93 goto EOP;
94 }
95
96EOP:
Brenden Blancob4111172015-09-11 09:27:45 -070097 return 0;
Brenden Blanco22a419b2015-05-29 11:14:20 -070098}