Skip to content

Commit 4bf7d94

Browse files
authored
Merge pull request #238 from DavideAG/packetcapture_service
Fixed NAT service and modified utils function
2 parents bd521f4 + a318faa commit 4bf7d94

5 files changed

Lines changed: 36 additions & 22 deletions

File tree

src/libs/polycube/include/polycube/services/utils.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ namespace polycube {
2828
namespace service {
2929
namespace utils {
3030

31-
/* ip (a.b.c.d) to string and viceversa
31+
/* IP string (a.b.c.d) or IP prefix (a.b.c.d/m) to nbo uint. If (a.b.c.d/m) only IP will be processed
3232
* Number is in network byte order (nbo), i.e., big endian */
3333
uint32_t ip_string_to_nbo_uint(const std::string &ip);
34+
35+
/* IP (a.b.c.d) to string
36+
* Number is in network byte order (nbo), i.e., big endian */
3437
std::string nbo_uint_to_ip_string(uint32_t ip);
3538

3639
/* mac (aa:bb:cc:dd:ee:ff) to string and vicersa
@@ -54,7 +57,7 @@ uint64_t hex_string_to_uint(const std::string &str);
5457
std::string get_random_mac();
5558

5659
/* Take in ingress a string like 192.168.0.1/24 and return only the ip
57-
* 192.168.0.1 */
60+
* 192.168.0.1 . If no prefix it will return the same input string*/
5861
std::string get_ip_from_string(const std::string &ipv_net);
5962

6063
/* Take in ingress a string like 192.168.0.1/24 and return only the "prefix

src/libs/polycube/src/utils.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ namespace service {
3434
namespace utils {
3535

3636
// new set of functions
37+
3738
uint32_t ip_string_to_nbo_uint(const std::string &ip) {
3839
unsigned char a[4];
3940
int last = -1;
40-
int rc = std::sscanf(ip.c_str(), "%hhu.%hhu.%hhu.%hhu%n", a + 0, a + 1, a + 2,
41+
std::string IP_address = get_ip_from_string(ip);
42+
43+
int rc = std::sscanf(IP_address.c_str(), "%hhu.%hhu.%hhu.%hhu%n", a + 0, a + 1, a + 2,
4144
a + 3, &last);
42-
if (rc != 4 || ip.size() != last)
45+
if (rc != 4 || IP_address.size() != last)
4346
throw std::runtime_error("Not an ipv4 address " + ip);
4447

4548
return uint32_t(a[3]) << 24 | uint32_t(a[2]) << 16 | uint32_t(a[1]) << 8 |
@@ -230,7 +233,7 @@ uint64_t hex_string_to_uint(const std::string &str) {
230233
std::string get_ip_from_string(const std::string &ipv_net) {
231234
size_t pos = ipv_net.find("/");
232235
if (pos == std::string::npos) {
233-
return std::string(); // throw?
236+
return ipv_net;
234237
}
235238
return ipv_net.substr(0, pos);
236239
}

src/services/pcn-nat/src/Nat.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ Nat::Nat(const std::string name, const NatJsonObject &conf)
3434
//addNattingTableList(conf.getNattingTable());
3535

3636
ParameterEventCallback cb = [&](const std::string &parameter, const std::string &value) {
37-
logger()->debug("parent IP has been updated to {}", value);
38-
external_ip_ = value;
37+
external_ip_ = utils::get_ip_from_string(value);
38+
logger()->debug("parent IP has been updated to {}", external_ip_);
3939
if (rule_->getMasquerade()->getEnabled()) {
4040
rule_->getMasquerade()->inject(utils::ip_string_to_nbo_uint(external_ip_));
4141
}

src/services/pcn-nat/src/Nat_dp.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
1010
void *data_end = (void *)(long)ctx->data_end;
1111

1212
struct eth_hdr *eth = data;
13-
if (data + sizeof(*eth) > data_end)
13+
if ( (void *)eth + sizeof(*eth) > data_end )
1414
goto DROP;
1515

1616
pcn_log(
@@ -49,7 +49,7 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
4949
uint8_t update_session_table = 1;
5050

5151
struct iphdr *ip = data + sizeof(*eth);
52-
if (data + sizeof(*eth) + sizeof(*ip) > data_end)
52+
if ( (void *)ip + sizeof(*ip) > data_end )
5353
goto DROP;
5454

5555
pcn_log(ctx, LOG_TRACE, "Processing IP packet: src %I, dst: %I", ip->saddr,
@@ -61,8 +61,9 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
6161

6262
switch (ip->protocol) {
6363
case IPPROTO_TCP: {
64-
struct tcphdr *tcp = data + sizeof(*eth) + sizeof(*ip);
65-
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*tcp) > data_end)
64+
uint8_t header_len = 4 * ip->ihl;
65+
struct tcphdr *tcp = data + sizeof(*eth) + header_len;
66+
if ( (void *)tcp + sizeof(*tcp) > data_end )
6667
goto DROP;
6768

6869
pcn_log(ctx, LOG_TRACE, "Packet is TCP: src_port %P, dst_port %P",
@@ -72,8 +73,9 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
7273
break;
7374
}
7475
case IPPROTO_UDP: {
75-
struct udphdr *udp = data + sizeof(*eth) + sizeof(*ip);
76-
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*udp) > data_end)
76+
uint8_t header_len = 4 * ip->ihl;
77+
struct udphdr *udp = data + sizeof(*eth) + header_len;
78+
if ( (void *)udp + sizeof(*udp) > data_end )
7779
goto DROP;
7880
pcn_log(ctx, LOG_TRACE, "Packet is UDP: src_port %P, dst_port %P",
7981
udp->source, udp->dest);
@@ -82,8 +84,9 @@ static int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {
8284
break;
8385
}
8486
case IPPROTO_ICMP: {
85-
struct icmphdr *icmp = data + sizeof(*eth) + sizeof(*ip);
86-
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*icmp) > data_end)
87+
uint8_t header_len = 4 * ip->ihl;
88+
struct icmphdr *icmp = data + sizeof(*eth) + header_len;
89+
if ( (void *)icmp + sizeof(*icmp) > data_end )
8790
goto DROP;
8891
pcn_log(ctx, LOG_TRACE, "Packet is ICMP: type %d, id %d", icmp->type,
8992
icmp->un.echo.id);
@@ -302,8 +305,9 @@ apply_nat:;
302305
uint32_t l4sum = pcn_csum_diff(&old_port, 4, &new_port, 4, 0);
303306
switch (proto) {
304307
case IPPROTO_TCP: {
305-
struct tcphdr *tcp = data + sizeof(*eth) + sizeof(*ip);
306-
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*tcp) > data_end)
308+
uint8_t header_len = 4 * ip->ihl;
309+
struct tcphdr *tcp = data + sizeof(*eth) + header_len;
310+
if ( (void *)tcp + sizeof(*tcp) > data_end )
307311
goto DROP;
308312

309313
if (rule_type == NAT_SRC || rule_type == NAT_MSQ) {
@@ -326,8 +330,9 @@ apply_nat:;
326330
goto proceed;
327331
}
328332
case IPPROTO_UDP: {
329-
struct udphdr *udp = data + sizeof(*eth) + sizeof(*ip);
330-
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*udp) > data_end)
333+
uint8_t header_len = 4 * ip->ihl;
334+
struct udphdr *udp = data + sizeof(*eth) + header_len;
335+
if ( (void *)udp + sizeof(*udp) > data_end )
331336
goto DROP;
332337
if (rule_type == NAT_SRC || rule_type == NAT_MSQ) {
333338
ip->saddr = new_ip;
@@ -349,8 +354,9 @@ apply_nat:;
349354
goto proceed;
350355
}
351356
case IPPROTO_ICMP: {
352-
struct icmphdr *icmp = data + sizeof(*eth) + sizeof(*ip);
353-
if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*icmp) > data_end)
357+
uint8_t header_len = 4 * ip->ihl;
358+
struct icmphdr *icmp = data + sizeof(*eth) + header_len;
359+
if ( (void *)icmp + sizeof(*icmp) > data_end )
354360
goto DROP;
355361
if (rule_type == NAT_SRC || rule_type == NAT_MSQ) {
356362
ip->saddr = new_ip;

src/services/pcn-nat/test/test_tcp_masq.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
source "${BASH_SOURCE%/*}/helpers.bash"
1616

1717
function test_tcp {
18-
sudo ip netns exec ns2 netcat -l -w 5 $tcp_port&
18+
sudo ip netns exec ns2 netcat -l -w 5 $tcp_port&
19+
sleep 2
20+
sudo ip netns exec ns1 ping $veth2_ip -c 1
1921
sleep 2
2022
sudo ip netns exec ns1 netcat -w 5 -nvz $veth2_ip $tcp_port
2123
sleep 4

0 commit comments

Comments
 (0)