|
| 1 | +// This file is a part of the IncludeOS unikernel - www.includeos.org |
| 2 | +// |
| 3 | +// Copyright 2017 Oslo and Akershus University College of Applied Sciences |
| 4 | +// and Alfred Bratterud |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +#include <net/nat/napt.hpp> |
| 19 | + |
| 20 | +namespace net { |
| 21 | +namespace nat { |
| 22 | + |
| 23 | +IP4::IP_packet_ptr NAPT::nat(IP4::IP_packet_ptr pkt, const Stack& inet) |
| 24 | +{ |
| 25 | + if(pkt->ip_dst() != inet.ip_addr()) |
| 26 | + { |
| 27 | + pkt = snat(std::move(pkt), inet); |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + pkt = dnat(std::move(pkt), inet); |
| 32 | + } |
| 33 | + |
| 34 | + return pkt; |
| 35 | +} |
| 36 | + |
| 37 | +// Replace source socket with external (this) address and random port |
| 38 | +IP4::IP_packet_ptr NAPT::snat(IP4::IP_packet_ptr pkt, const Stack& inet) |
| 39 | +{ |
| 40 | + // Early return if the packet is for me |
| 41 | + if(UNLIKELY(pkt->ip_dst() == inet.ip_addr())) |
| 42 | + return pkt; |
| 43 | + |
| 44 | + if(pkt->ip_protocol() == Protocol::TCP) |
| 45 | + { |
| 46 | + snat(*static_cast<tcp::Packet*>(pkt.get()), inet.ip_addr()); |
| 47 | + } |
| 48 | + |
| 49 | + return pkt; |
| 50 | +} |
| 51 | + |
| 52 | +// Replace source address with external address from table |
| 53 | +IP4::IP_packet_ptr NAPT::dnat(IP4::IP_packet_ptr pkt, const Stack& inet) |
| 54 | +{ |
| 55 | + // Early return if the packet isn't for me |
| 56 | + if(UNLIKELY(pkt->ip_dst() != inet.ip_addr())) |
| 57 | + return pkt; |
| 58 | + |
| 59 | + if(pkt->ip_protocol() == Protocol::TCP) |
| 60 | + { |
| 61 | + dnat(*static_cast<tcp::Packet*>(pkt.get())); |
| 62 | + } |
| 63 | + |
| 64 | + return pkt; |
| 65 | +} |
| 66 | + |
| 67 | +void NAPT::snat(tcp::Packet& pkt, ip4::Addr src_ip) |
| 68 | +{ |
| 69 | + // Get the Socket |
| 70 | + Socket socket = pkt.source(); |
| 71 | + |
| 72 | + // Is there an entry? |
| 73 | + auto it = std::find_if(tcp_trans.begin(), tcp_trans.end(), |
| 74 | + [socket] (auto& ent) { |
| 75 | + return ent.second == socket; |
| 76 | + }); |
| 77 | + |
| 78 | + // If there already is an entry |
| 79 | + if(it != tcp_trans.end()) |
| 80 | + { |
| 81 | + // Replace the source port with the already translated one |
| 82 | + pkt.set_src_port(it->first); |
| 83 | + } |
| 84 | + // If not |
| 85 | + else |
| 86 | + { |
| 87 | + // Generate a new port |
| 88 | + auto port = tcp_ports.get_next_ephemeral(); |
| 89 | + |
| 90 | + // Replace the source port |
| 91 | + pkt.set_src_port(port); |
| 92 | + |
| 93 | + add_entry(port, socket); |
| 94 | + } |
| 95 | + |
| 96 | + // At last, replace the source address |
| 97 | + pkt.set_ip_src(src_ip); |
| 98 | + |
| 99 | + printf("SNAT %s => %s\n", socket.to_string().c_str(), pkt.source().to_string().c_str()); |
| 100 | + |
| 101 | + // Recalculate checksum |
| 102 | + recalculate_checksum(pkt); |
| 103 | +} |
| 104 | + |
| 105 | +void NAPT::dnat(tcp::Packet& pkt) |
| 106 | +{ |
| 107 | + auto dst_port = pkt.dst_port(); |
| 108 | + |
| 109 | + // Is there an entry? |
| 110 | + auto it = tcp_trans.find(dst_port); |
| 111 | + |
| 112 | + // If there already is an entry |
| 113 | + if(it != tcp_trans.end()) |
| 114 | + { |
| 115 | + // Get the Socket |
| 116 | + auto socket = it->second; |
| 117 | + printf("DNAT %s => %s\n", pkt.destination().to_string().c_str(), socket.to_string().c_str()); |
| 118 | + // Replace the destination port with the original one |
| 119 | + pkt.set_destination(socket); |
| 120 | + |
| 121 | + // Recalculate checksum |
| 122 | + recalculate_checksum(pkt); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +} |
| 127 | +} |
0 commit comments