diff options
author | Filip Varga <fivarga@cisco.com> | 2019-10-21 18:18:00 +0200 |
---|---|---|
committer | Ole Trøan <otroan@employees.org> | 2019-10-28 10:56:49 +0000 |
commit | 0d75f783644a24b219ed79d9f9c17387783f67ca (patch) | |
tree | 1614401fb2cce079ef4d2441dd611322f531c8f1 /src/plugins/nat | |
parent | 4aacc01f2ec95ff0a6bbc04c3c573ab5936c9489 (diff) |
nat: respect udp checksum
Type: fix
Change-Id: I73895fa0101bd50483160c8dc6faac2c67513077
Signed-off-by: Filip Varga <fivarga@cisco.com>
Diffstat (limited to 'src/plugins/nat')
-rwxr-xr-x | src/plugins/nat/in2out.c | 110 | ||||
-rw-r--r-- | src/plugins/nat/in2out_ed.c | 120 | ||||
-rw-r--r-- | src/plugins/nat/nat_det_in2out.c | 69 | ||||
-rw-r--r-- | src/plugins/nat/nat_det_out2in.c | 60 | ||||
-rwxr-xr-x | src/plugins/nat/out2in.c | 108 | ||||
-rw-r--r-- | src/plugins/nat/out2in_ed.c | 127 |
6 files changed, 428 insertions, 166 deletions
diff --git a/src/plugins/nat/in2out.c b/src/plugins/nat/in2out.c index c69bc61236b..802202d7939 100755 --- a/src/plugins/nat/in2out.c +++ b/src/plugins/nat/in2out.c @@ -1075,12 +1075,11 @@ snat_in2out_node_fn_inline (vlib_main_t * vm, src_address /* changed member */ ); ip0->checksum = ip_csum_fold (sum0); + old_port0 = udp0->src_port; + new_port0 = udp0->src_port = s0->out2in.port; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { - old_port0 = tcp0->src_port; - tcp0->src_port = s0->out2in.port; - new_port0 = tcp0->src_port; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, @@ -1094,9 +1093,17 @@ snat_in2out_node_fn_inline (vlib_main_t * vm, } else { - old_port0 = udp0->src_port; - udp0->src_port = s0->out2in.port; - udp0->checksum = 0; + if (PREDICT_FALSE (udp0->checksum)) + { + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, + ip4_header_t, + dst_address /* changed member */ ); + sum0 = ip_csum_update (sum0, old_port0, new_port0, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); + } udp_packets++; } @@ -1264,12 +1271,11 @@ snat_in2out_node_fn_inline (vlib_main_t * vm, src_address /* changed member */ ); ip1->checksum = ip_csum_fold (sum1); + old_port1 = udp1->src_port; + new_port1 = udp1->src_port = s1->out2in.port; + if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP)) { - old_port1 = tcp1->src_port; - tcp1->src_port = s1->out2in.port; - new_port1 = tcp1->src_port; - sum1 = tcp1->checksum; sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, @@ -1283,9 +1289,17 @@ snat_in2out_node_fn_inline (vlib_main_t * vm, } else { - old_port1 = udp1->src_port; - udp1->src_port = s1->out2in.port; - udp1->checksum = 0; + if (PREDICT_FALSE (udp1->checksum)) + { + sum1 = udp1->checksum; + sum1 = ip_csum_update (sum1, old_addr1, new_addr1, + ip4_header_t, + dst_address /* changed member */ ); + sum1 = ip_csum_update (sum1, old_port1, new_port1, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp1->checksum = ip_csum_fold (sum1); + } udp_packets++; } @@ -1488,12 +1502,11 @@ snat_in2out_node_fn_inline (vlib_main_t * vm, src_address /* changed member */ ); ip0->checksum = ip_csum_fold (sum0); + old_port0 = udp0->src_port; + new_port0 = udp0->src_port = s0->out2in.port; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { - old_port0 = tcp0->src_port; - tcp0->src_port = s0->out2in.port; - new_port0 = tcp0->src_port; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, @@ -1507,9 +1520,17 @@ snat_in2out_node_fn_inline (vlib_main_t * vm, } else { - old_port0 = udp0->src_port; - udp0->src_port = s0->out2in.port; - udp0->checksum = 0; + if (PREDICT_FALSE (udp0->checksum)) + { + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, + ip4_header_t, + dst_address /* changed member */ ); + sum0 = ip_csum_update (sum0, old_port0, new_port0, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); + } udp_packets++; } @@ -1869,12 +1890,11 @@ VLIB_NODE_FN (nat44_in2out_reass_node) (vlib_main_t * vm, if (PREDICT_FALSE (ip4_is_first_fragment (ip0))) { + old_port0 = udp0->src_port; + new_port0 = udp0->src_port = s0->out2in.port; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { - old_port0 = tcp0->src_port; - tcp0->src_port = s0->out2in.port; - new_port0 = tcp0->src_port; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, @@ -1884,11 +1904,16 @@ VLIB_NODE_FN (nat44_in2out_reass_node) (vlib_main_t * vm, length /* changed member */ ); tcp0->checksum = ip_csum_fold (sum0); } - else + else if (PREDICT_FALSE (udp0->checksum)) { - old_port0 = udp0->src_port; - udp0->src_port = s0->out2in.port; - udp0->checksum = 0; + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, + ip4_header_t, + dst_address /* changed member */ ); + sum0 = ip_csum_update (sum0, old_port0, new_port0, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); } } @@ -2099,11 +2124,11 @@ VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm, if (PREDICT_FALSE (new_port0 != udp0->dst_port)) { + old_port0 = udp0->src_port; + udp0->src_port = new_port0; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { - old_port0 = tcp0->src_port; - tcp0->src_port = new_port0; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, @@ -2114,11 +2139,16 @@ VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm, mss_clamping (sm, tcp0, &sum0); tcp0->checksum = ip_csum_fold (sum0); } - else + else if (udp0->checksum) { - old_port0 = udp0->src_port; - udp0->src_port = new_port0; - udp0->checksum = 0; + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, + ip4_header_t, + dst_address /* changed member */ ); + sum0 = ip_csum_update (sum0, old_port0, new_port0, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); } } else @@ -2132,6 +2162,14 @@ VLIB_NODE_FN (snat_in2out_fast_node) (vlib_main_t * vm, mss_clamping (sm, tcp0, &sum0); tcp0->checksum = ip_csum_fold (sum0); } + else if (udp0->checksum) + { + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, + ip4_header_t, + dst_address /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); + } } /* Hairpinning */ diff --git a/src/plugins/nat/in2out_ed.c b/src/plugins/nat/in2out_ed.c index de00c6e7b13..c878dddd13e 100644 --- a/src/plugins/nat/in2out_ed.c +++ b/src/plugins/nat/in2out_ed.c @@ -1045,11 +1045,11 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm, dst_address); ip0->checksum = ip_csum_fold (sum0); + old_port0 = udp0->src_port; + new_port0 = udp0->src_port = s0->out2in.port; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { - old_port0 = tcp0->src_port; - new_port0 = tcp0->src_port = s0->out2in.port; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address); @@ -1073,10 +1073,29 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm, (sm, s0, tcp0, thread_index)) goto trace00; } + else if (udp0->checksum) + { + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, + dst_address); + sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t, + length); + if (PREDICT_FALSE (is_twice_nat_session (s0))) + { + sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32, + s0->ext_host_addr.as_u32, + ip4_header_t, dst_address); + sum0 = ip_csum_update (sum0, tcp0->dst_port, + s0->ext_host_port, ip4_header_t, + length); + udp0->dst_port = s0->ext_host_port; + ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32; + } + udp0->checksum = ip_csum_fold (sum0); + udp_packets++; + } else { - udp0->src_port = s0->out2in.port; - udp0->checksum = 0; if (PREDICT_FALSE (is_twice_nat_session (s0))) { udp0->dst_port = s0->ext_host_port; @@ -1255,11 +1274,11 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm, dst_address); ip1->checksum = ip_csum_fold (sum1); + old_port1 = udp1->src_port; + new_port1 = udp1->src_port = s1->out2in.port; + if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP)) { - old_port1 = tcp1->src_port; - new_port1 = tcp1->src_port = s1->out2in.port; - sum1 = tcp1->checksum; sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address); @@ -1283,10 +1302,30 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm, (sm, s1, tcp1, thread_index)) goto trace01; } + else if (udp1->checksum) + { + sum1 = udp1->checksum; + sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, + dst_address); + sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t, + length); + + if (PREDICT_FALSE (is_twice_nat_session (s1))) + { + sum1 = ip_csum_update (sum1, ip1->dst_address.as_u32, + s1->ext_host_addr.as_u32, + ip4_header_t, dst_address); + sum1 = ip_csum_update (sum1, tcp1->dst_port, + s1->ext_host_port, ip4_header_t, + length); + udp1->dst_port = s1->ext_host_port; + ip1->dst_address.as_u32 = s1->ext_host_addr.as_u32; + } + udp1->checksum = ip_csum_fold (sum1); + udp_packets++; + } else { - udp1->src_port = s1->out2in.port; - udp1->checksum = 0; if (PREDICT_FALSE (is_twice_nat_session (s1))) { udp1->dst_port = s1->ext_host_port; @@ -1493,11 +1532,11 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm, dst_address); ip0->checksum = ip_csum_fold (sum0); + old_port0 = udp0->src_port; + new_port0 = udp0->src_port = s0->out2in.port; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { - old_port0 = tcp0->src_port; - new_port0 = tcp0->src_port = s0->out2in.port; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address); @@ -1521,10 +1560,29 @@ nat44_ed_in2out_node_fn_inline (vlib_main_t * vm, (sm, s0, tcp0, thread_index)) goto trace0; } + else if (udp0->checksum) + { + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, + dst_address); + sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t, + length); + if (PREDICT_FALSE (is_twice_nat_session (s0))) + { + sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32, + s0->ext_host_addr.as_u32, + ip4_header_t, dst_address); + sum0 = ip_csum_update (sum0, tcp0->dst_port, + s0->ext_host_port, ip4_header_t, + length); + udp0->dst_port = s0->ext_host_port; + ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32; + } + udp0->checksum = ip_csum_fold (sum0); + udp_packets++; + } else { - udp0->src_port = s0->out2in.port; - udp0->checksum = 0; if (PREDICT_FALSE (is_twice_nat_session (s0))) { udp0->dst_port = s0->ext_host_port; @@ -1915,12 +1973,11 @@ nat44_ed_in2out_reass_node_fn_inline (vlib_main_t * vm, if (PREDICT_FALSE (ip4_is_first_fragment (ip0))) { + old_port0 = udp0->src_port; + new_port0 = udp0->src_port = s0->out2in.port; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { - old_port0 = tcp0->src_port; - tcp0->src_port = s0->out2in.port; - new_port0 = tcp0->src_port; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, @@ -1941,11 +1998,30 @@ nat44_ed_in2out_reass_node_fn_inline (vlib_main_t * vm, } tcp0->checksum = ip_csum_fold (sum0); } + else if (udp0->checksum) + { + sum0 = udp0->checksum; + sum0 = + ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, + dst_address); + sum0 = + ip_csum_update (sum0, old_port0, new_port0, ip4_header_t, + length); + if (PREDICT_FALSE (is_twice_nat_session (s0))) + { + sum0 = ip_csum_update (sum0, ip0->dst_address.as_u32, + s0->ext_host_addr.as_u32, + ip4_header_t, dst_address); + sum0 = ip_csum_update (sum0, tcp0->dst_port, + s0->ext_host_port, ip4_header_t, + length); + udp0->dst_port = s0->ext_host_port; + ip0->dst_address.as_u32 = s0->ext_host_addr.as_u32; + } + udp0->checksum = ip_csum_fold (sum0); + } else { - old_port0 = udp0->src_port; - udp0->src_port = s0->out2in.port; - udp0->checksum = 0; if (PREDICT_FALSE (is_twice_nat_session (s0))) { udp0->dst_port = s0->ext_host_port; diff --git a/src/plugins/nat/nat_det_in2out.c b/src/plugins/nat/nat_det_in2out.c index 918e1f5f9c0..832a2bae947 100644 --- a/src/plugins/nat/nat_det_in2out.c +++ b/src/plugins/nat/nat_det_in2out.c @@ -392,7 +392,8 @@ VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm, } } - new_port0 = ses0->out.out_port; + old_port0 = udp0->src_port; + udp0->src_port = new_port0 = ses0->out.out_port; old_addr0.as_u32 = ip0->src_address.as_u32; ip0->src_address.as_u32 = new_addr0.as_u32; @@ -424,9 +425,6 @@ VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm, && ses0->state == SNAT_SESSION_UNKNOWN) ses0->state = SNAT_SESSION_TCP_ESTABLISHED; - old_port0 = tcp0->src; - tcp0->src = new_port0; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32, ip4_header_t, @@ -440,9 +438,20 @@ VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm, else { ses0->state = SNAT_SESSION_UDP_ACTIVE; - old_port0 = udp0->src_port; - udp0->src_port = new_port0; - udp0->checksum = 0; + + if (PREDICT_FALSE (udp0->checksum)) + { + sum0 = udp0->checksum; + sum0 = + ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32, + ip4_header_t, + dst_address /* changed member */ ); + sum0 = + ip_csum_update (sum0, old_port0, new_port0, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); + } } switch (ses0->state) @@ -556,7 +565,8 @@ VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm, } } - new_port1 = ses1->out.out_port; + old_port1 = udp1->src_port; + udp1->src_port = new_port1 = ses1->out.out_port; old_addr1.as_u32 = ip1->src_address.as_u32; ip1->src_address.as_u32 = new_addr1.as_u32; @@ -588,9 +598,6 @@ VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm, && ses1->state == SNAT_SESSION_UNKNOWN) ses1->state = SNAT_SESSION_TCP_ESTABLISHED; - old_port1 = tcp1->src; - tcp1->src = new_port1; - sum1 = tcp1->checksum; sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32, ip4_header_t, @@ -604,9 +611,20 @@ VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm, else { ses1->state = SNAT_SESSION_UDP_ACTIVE; - old_port1 = udp1->src_port; - udp1->src_port = new_port1; - udp1->checksum = 0; + + if (PREDICT_FALSE (udp1->checksum)) + { + sum1 = udp1->checksum; + sum1 = + ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32, + ip4_header_t, + dst_address /* changed member */ ); + sum1 = + ip_csum_update (sum1, old_port1, new_port1, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp1->checksum = ip_csum_fold (sum1); + } } switch (ses1->state) @@ -756,7 +774,8 @@ VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm, } } - new_port0 = ses0->out.out_port; + old_port0 = udp0->src_port; + udp0->src_port = new_port0 = ses0->out.out_port; old_addr0.as_u32 = ip0->src_address.as_u32; ip0->src_address.as_u32 = new_addr0.as_u32; @@ -788,9 +807,6 @@ VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm, && ses0->state == SNAT_SESSION_UNKNOWN) ses0->state = SNAT_SESSION_TCP_ESTABLISHED; - old_port0 = tcp0->src; - tcp0->src = new_port0; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32, ip4_header_t, @@ -804,9 +820,20 @@ VLIB_NODE_FN (snat_det_in2out_node) (vlib_main_t * vm, else { ses0->state = SNAT_SESSION_UDP_ACTIVE; - old_port0 = udp0->src_port; - udp0->src_port = new_port0; - udp0->checksum = 0; + + if (PREDICT_FALSE (udp0->checksum)) + { + sum0 = udp0->checksum; + sum0 = + ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32, + ip4_header_t, + dst_address /* changed member */ ); + sum0 = + ip_csum_update (sum0, old_port0, new_port0, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); + } } switch (ses0->state) diff --git a/src/plugins/nat/nat_det_out2in.c b/src/plugins/nat/nat_det_out2in.c index 45fba18b248..c4bd096deb1 100644 --- a/src/plugins/nat/nat_det_out2in.c +++ b/src/plugins/nat/nat_det_out2in.c @@ -343,7 +343,8 @@ VLIB_NODE_FN (snat_det_out2in_node) (vlib_main_t * vm, b0->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION]; goto trace0; } - new_port0 = ses0->in_port; + old_port0 = udp0->dst_port; + udp0->dst_port = new_port0 = ses0->in_port; old_addr0 = ip0->dst_address; ip0->dst_address = new_addr0; @@ -364,24 +365,25 @@ VLIB_NODE_FN (snat_det_out2in_node) (vlib_main_t * vm, && ses0->state == SNAT_SESSION_TCP_LAST_ACK) snat_det_ses_close (dm0, ses0); - old_port0 = tcp0->dst; - tcp0->dst = new_port0; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32, ip4_header_t, dst_address /* changed member */ ); - sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t /* cheat */ , length /* changed member */ ); tcp0->checksum = ip_csum_fold (sum0); } - else + else if (udp0->checksum) { - old_port0 = udp0->dst_port; - udp0->dst_port = new_port0; - udp0->checksum = 0; + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32, + ip4_header_t, + dst_address /* changed member */ ); + sum0 = ip_csum_update (sum0, old_port0, new_port0, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); } trace0: @@ -462,7 +464,8 @@ VLIB_NODE_FN (snat_det_out2in_node) (vlib_main_t * vm, b1->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION]; goto trace1; } - new_port1 = ses1->in_port; + old_port1 = udp1->dst_port; + udp1->dst_port = new_port1 = ses1->in_port; old_addr1 = ip1->dst_address; ip1->dst_address = new_addr1; @@ -483,24 +486,25 @@ VLIB_NODE_FN (snat_det_out2in_node) (vlib_main_t * vm, && ses1->state == SNAT_SESSION_TCP_LAST_ACK) snat_det_ses_close (dm1, ses1); - old_port1 = tcp1->dst; - tcp1->dst = new_port1; - sum1 = tcp1->checksum; sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32, ip4_header_t, dst_address /* changed member */ ); - sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t /* cheat */ , length /* changed member */ ); tcp1->checksum = ip_csum_fold (sum1); } - else + else if (udp1->checksum) { - old_port1 = udp1->dst_port; - udp1->dst_port = new_port1; - udp1->checksum = 0; + sum1 = udp1->checksum; + sum1 = ip_csum_update (sum1, old_addr1.as_u32, new_addr1.as_u32, + ip4_header_t, + dst_address /* changed member */ ); + sum1 = ip_csum_update (sum1, old_port1, new_port1, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp1->checksum = ip_csum_fold (sum1); } trace1: @@ -614,7 +618,8 @@ VLIB_NODE_FN (snat_det_out2in_node) (vlib_main_t * vm, b0->error = node->errors[NAT_DET_OUT2IN_ERROR_NO_TRANSLATION]; goto trace00; } - new_port0 = ses0->in_port; + old_port0 = udp0->dst_port; + udp0->dst_port = new_port0 = ses0->in_port; old_addr0 = ip0->dst_address; ip0->dst_address = new_addr0; @@ -635,24 +640,25 @@ VLIB_NODE_FN (snat_det_out2in_node) (vlib_main_t * vm, && ses0->state == SNAT_SESSION_TCP_LAST_ACK) snat_det_ses_close (dm0, ses0); - old_port0 = tcp0->dst; - tcp0->dst = new_port0; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32, ip4_header_t, dst_address /* changed member */ ); - sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t /* cheat */ , length /* changed member */ ); tcp0->checksum = ip_csum_fold (sum0); } - else + else if (udp0->checksum) { - old_port0 = udp0->dst_port; - udp0->dst_port = new_port0; - udp0->checksum = 0; + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0.as_u32, new_addr0.as_u32, + ip4_header_t, + dst_address /* changed member */ ); + sum0 = ip_csum_update (sum0, old_port0, new_port0, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); } trace00: diff --git a/src/plugins/nat/out2in.c b/src/plugins/nat/out2in.c index 8c6cb482703..83c099d1bd2 100755 --- a/src/plugins/nat/out2in.c +++ b/src/plugins/nat/out2in.c @@ -893,6 +893,9 @@ VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm, dst_address /* changed member */ ); ip0->checksum = ip_csum_fold (sum0); + old_port0 = udp0->dst_port; + new_port0 = udp0->dst_port = s0->in2out.port; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { old_port0 = tcp0->dst_port; @@ -912,9 +915,17 @@ VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm, } else { - old_port0 = udp0->dst_port; - udp0->dst_port = s0->in2out.port; - udp0->checksum = 0; + if (PREDICT_FALSE (udp0->checksum)) + { + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, + ip4_header_t, + dst_address /* changed member */ ); + sum0 = ip_csum_update (sum0, old_port0, new_port0, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); + } udp_packets++; } @@ -1059,12 +1070,11 @@ VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm, dst_address /* changed member */ ); ip1->checksum = ip_csum_fold (sum1); + old_port1 = udp1->dst_port; + new_port1 = udp1->dst_port = s1->in2out.port; + if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP)) { - old_port1 = tcp1->dst_port; - tcp1->dst_port = s1->in2out.port; - new_port1 = tcp1->dst_port; - sum1 = tcp1->checksum; sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, @@ -1078,9 +1088,17 @@ VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm, } else { - old_port1 = udp1->dst_port; - udp1->dst_port = s1->in2out.port; - udp1->checksum = 0; + if (PREDICT_FALSE (udp1->checksum)) + { + sum1 = udp1->checksum; + sum1 = ip_csum_update (sum1, old_addr1, new_addr1, + ip4_header_t, + dst_address /* changed member */ ); + sum1 = ip_csum_update (sum1, old_port1, new_port1, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp1->checksum = ip_csum_fold (sum1); + } udp_packets++; } @@ -1262,12 +1280,11 @@ VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm, dst_address /* changed member */ ); ip0->checksum = ip_csum_fold (sum0); + old_port0 = udp0->dst_port; + new_port0 = udp0->dst_port = s0->in2out.port; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { - old_port0 = tcp0->dst_port; - tcp0->dst_port = s0->in2out.port; - new_port0 = tcp0->dst_port; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, @@ -1281,9 +1298,17 @@ VLIB_NODE_FN (snat_out2in_node) (vlib_main_t * vm, } else { - old_port0 = udp0->dst_port; - udp0->dst_port = s0->in2out.port; - udp0->checksum = 0; + if (PREDICT_FALSE (udp0->checksum)) + { + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, + ip4_header_t, + dst_address /* changed member */ ); + sum0 = ip_csum_update (sum0, old_port0, new_port0, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); + } udp_packets++; } @@ -1573,12 +1598,11 @@ VLIB_NODE_FN (nat44_out2in_reass_node) (vlib_main_t * vm, if (PREDICT_FALSE (ip4_is_first_fragment (ip0))) { + old_port0 = udp0->dst_port; + new_port0 = udp0->dst_port = s0->in2out.port; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { - old_port0 = tcp0->dst_port; - tcp0->dst_port = s0->in2out.port; - new_port0 = tcp0->dst_port; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, @@ -1589,11 +1613,16 @@ VLIB_NODE_FN (nat44_out2in_reass_node) (vlib_main_t * vm, length /* changed member */ ); tcp0->checksum = ip_csum_fold (sum0); } - else + else if (udp0->checksum) { - old_port0 = udp0->dst_port; - udp0->dst_port = s0->in2out.port; - udp0->checksum = 0; + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, + ip4_header_t, + dst_address /* changed member */ ); + sum0 = ip_csum_update (sum0, old_port0, new_port0, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); } } @@ -1797,26 +1826,30 @@ VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm, if (PREDICT_FALSE (new_port0 != udp0->dst_port)) { + old_port0 = udp0->dst_port; + udp0->dst_port = new_port0; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { - old_port0 = tcp0->dst_port; - tcp0->dst_port = new_port0; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */ ); - sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t /* cheat */ , length /* changed member */ ); tcp0->checksum = ip_csum_fold (sum0); } - else + else if (udp0->checksum) { - old_port0 = udp0->dst_port; - udp0->dst_port = new_port0; - udp0->checksum = 0; + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, + ip4_header_t, + dst_address /* changed member */ ); + sum0 = ip_csum_update (sum0, old_port0, new_port0, + ip4_header_t /* cheat */ , + length /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); } } else @@ -1827,9 +1860,16 @@ VLIB_NODE_FN (snat_out2in_fast_node) (vlib_main_t * vm, sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address /* changed member */ ); - tcp0->checksum = ip_csum_fold (sum0); } + else if (udp0->checksum) + { + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, + ip4_header_t, + dst_address /* changed member */ ); + udp0->checksum = ip_csum_fold (sum0); + } } trace00: diff --git a/src/plugins/nat/out2in_ed.c b/src/plugins/nat/out2in_ed.c index 50abebd9cfb..2067c512b92 100644 --- a/src/plugins/nat/out2in_ed.c +++ b/src/plugins/nat/out2in_ed.c @@ -919,11 +919,11 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm, src_address); ip0->checksum = ip_csum_fold (sum0); + old_port0 = udp0->dst_port; + new_port0 = udp0->dst_port = s0->in2out.port; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { - old_port0 = tcp0->dst_port; - new_port0 = tcp0->dst_port = s0->in2out.port; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address); @@ -946,15 +946,34 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm, (sm, s0, tcp0, thread_index)) goto trace00; } + else if (udp0->checksum) + { + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, + dst_address); + sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t, + length); + if (PREDICT_FALSE (is_twice_nat_session (s0))) + { + sum0 = ip_csum_update (sum0, ip0->src_address.as_u32, + s0->ext_host_nat_addr.as_u32, + ip4_header_t, dst_address); + sum0 = ip_csum_update (sum0, udp0->src_port, + s0->ext_host_nat_port, ip4_header_t, + length); + udp0->src_port = s0->ext_host_nat_port; + ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32; + } + udp0->checksum = ip_csum_fold (sum0); + udp_packets++; + } else { - udp0->dst_port = s0->in2out.port; - if (is_twice_nat_session (s0)) + if (PREDICT_FALSE (is_twice_nat_session (s0))) { udp0->src_port = s0->ext_host_nat_port; ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32; } - udp0->checksum = 0; udp_packets++; } @@ -1157,11 +1176,11 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm, src_address); ip1->checksum = ip_csum_fold (sum1); + old_port1 = udp1->dst_port; + new_port1 = udp1->dst_port = s1->in2out.port; + if (PREDICT_TRUE (proto1 == SNAT_PROTOCOL_TCP)) { - old_port1 = tcp1->dst_port; - new_port1 = tcp1->dst_port = s1->in2out.port; - sum1 = tcp1->checksum; sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, dst_address); @@ -1184,15 +1203,34 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm, (sm, s1, tcp1, thread_index)) goto trace01; } + else if (udp1->checksum) + { + sum1 = udp1->checksum; + sum1 = ip_csum_update (sum1, old_addr1, new_addr1, ip4_header_t, + dst_address); + sum1 = ip_csum_update (sum1, old_port1, new_port1, ip4_header_t, + length); + if (PREDICT_FALSE (is_twice_nat_session (s1))) + { + sum1 = ip_csum_update (sum1, ip1->src_address.as_u32, + s1->ext_host_nat_addr.as_u32, + ip4_header_t, dst_address); + sum1 = ip_csum_update (sum1, udp1->src_port, + s1->ext_host_nat_port, ip4_header_t, + length); + udp1->src_port = s1->ext_host_nat_port; + ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32; + } + udp1->checksum = ip_csum_fold (sum1); + udp_packets++; + } else { - udp1->dst_port = s1->in2out.port; - if (is_twice_nat_session (s1)) + if (PREDICT_FALSE (is_twice_nat_session (s1))) { udp1->src_port = s1->ext_host_nat_port; ip1->src_address.as_u32 = s1->ext_host_nat_addr.as_u32; } - udp1->checksum = 0; udp_packets++; } @@ -1429,11 +1467,11 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm, src_address); ip0->checksum = ip_csum_fold (sum0); + old_port0 = udp0->dst_port; + new_port0 = udp0->dst_port = s0->in2out.port; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { - old_port0 = tcp0->dst_port; - new_port0 = tcp0->dst_port = s0->in2out.port; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, dst_address); @@ -1456,15 +1494,34 @@ nat44_ed_out2in_node_fn_inline (vlib_main_t * vm, (sm, s0, tcp0, thread_index)) goto trace0; } + else if (udp0->checksum) + { + sum0 = udp0->checksum; + sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, + dst_address); + sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t, + length); + if (PREDICT_FALSE (is_twice_nat_session (s0))) + { + sum0 = ip_csum_update (sum0, ip0->src_address.as_u32, + s0->ext_host_nat_addr.as_u32, + ip4_header_t, dst_address); + sum0 = ip_csum_update (sum0, udp0->src_port, + s0->ext_host_nat_port, ip4_header_t, + length); + udp0->src_port = s0->ext_host_nat_port; + ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32; + } + udp0->checksum = ip_csum_fold (sum0); + udp_packets++; + } else { - udp0->dst_port = s0->in2out.port; - if (is_twice_nat_session (s0)) + if (PREDICT_FALSE (is_twice_nat_session (s0))) { udp0->src_port = s0->ext_host_nat_port; ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32; } - udp0->checksum = 0; udp_packets++; } @@ -1818,12 +1875,11 @@ VLIB_NODE_FN (nat44_ed_out2in_reass_node) (vlib_main_t * vm, if (PREDICT_FALSE (ip4_is_first_fragment (ip0))) { + old_port0 = udp0->dst_port; + new_port0 = udp0->dst_port = s0->in2out.port; + if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP)) { - old_port0 = tcp0->dst_port; - tcp0->dst_port = s0->in2out.port; - new_port0 = tcp0->dst_port; - sum0 = tcp0->checksum; sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, @@ -1845,16 +1901,35 @@ VLIB_NODE_FN (nat44_ed_out2in_reass_node) (vlib_main_t * vm, } tcp0->checksum = ip_csum_fold (sum0); } + else if (udp0->checksum) + { + sum0 = udp0->checksum; + sum0 = + ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t, + dst_address); + sum0 = + ip_csum_update (sum0, old_port0, new_port0, ip4_header_t, + length); + if (PREDICT_FALSE (is_twice_nat_session (s0))) + { + sum0 = ip_csum_update (sum0, ip0->src_address.as_u32, + s0->ext_host_nat_addr.as_u32, + ip4_header_t, dst_address); + sum0 = ip_csum_update (sum0, udp0->src_port, + s0->ext_host_nat_port, + ip4_header_t, length); + udp0->src_port = s0->ext_host_nat_port; + ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32; + } + udp0->checksum = ip_csum_fold (sum0); + } else { - old_port0 = udp0->dst_port; - udp0->dst_port = s0->in2out.port; - if (is_twice_nat_session (s0)) + if (PREDICT_FALSE (is_twice_nat_session (s0))) { udp0->src_port = s0->ext_host_nat_port; ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32; } - udp0->checksum = 0; } } |