Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * snat.c - simple nat plugin |
| 3 | * |
| 4 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vnet/vnet.h> |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 19 | #include <vnet/ip/ip.h> |
| 20 | #include <vnet/ip/ip4.h> |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 21 | #include <vnet/plugin/plugin.h> |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 22 | #include <nat/nat.h> |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 23 | #include <nat/nat_dpo.h> |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 24 | #include <nat/nat_ipfix_logging.h> |
| 25 | #include <nat/nat_det.h> |
| 26 | #include <nat/nat64.h> |
Matus Fabian | f2a23cc | 2018-01-22 03:41:53 -0800 | [diff] [blame] | 27 | #include <nat/nat66.h> |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 28 | #include <nat/dslite.h> |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 29 | #include <nat/nat_reass.h> |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 30 | #include <nat/nat_inlines.h> |
Matus Fabian | ea5b5be | 2018-09-03 05:02:23 -0700 | [diff] [blame] | 31 | #include <nat/nat_affinity.h> |
Matus Fabian | ad1f3e1 | 2018-11-28 21:26:34 -0800 | [diff] [blame] | 32 | #include <nat/nat_syslog.h> |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 33 | #include <vnet/fib/fib_table.h> |
| 34 | #include <vnet/fib/ip4_fib.h> |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 35 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 36 | #include <vpp/app/version.h> |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 37 | |
| 38 | snat_main_t snat_main; |
| 39 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 40 | /* *INDENT-OFF* */ |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 41 | |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 42 | /* Hook up input features */ |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 43 | VNET_FEATURE_INIT (ip4_snat_in2out, static) = { |
| 44 | .arc_name = "ip4-unicast", |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 45 | .node_name = "nat44-in2out", |
Matus Fabian | 16f0546 | 2018-02-08 05:28:28 -0800 | [diff] [blame] | 46 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"), |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 47 | }; |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 48 | VNET_FEATURE_INIT (ip4_snat_out2in, static) = { |
| 49 | .arc_name = "ip4-unicast", |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 50 | .node_name = "nat44-out2in", |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 51 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa", |
Dave Barach | 525c9d0 | 2018-05-26 10:48:55 -0400 | [diff] [blame] | 52 | "ip4-dhcp-client-detect"), |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 53 | }; |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 54 | VNET_FEATURE_INIT (ip4_nat_classify, static) = { |
| 55 | .arc_name = "ip4-unicast", |
| 56 | .node_name = "nat44-classify", |
Matus Fabian | 16f0546 | 2018-02-08 05:28:28 -0800 | [diff] [blame] | 57 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"), |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 58 | }; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 59 | VNET_FEATURE_INIT (ip4_snat_det_in2out, static) = { |
| 60 | .arc_name = "ip4-unicast", |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 61 | .node_name = "nat44-det-in2out", |
Matus Fabian | 16f0546 | 2018-02-08 05:28:28 -0800 | [diff] [blame] | 62 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"), |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 63 | }; |
| 64 | VNET_FEATURE_INIT (ip4_snat_det_out2in, static) = { |
| 65 | .arc_name = "ip4-unicast", |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 66 | .node_name = "nat44-det-out2in", |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 67 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa", |
| 68 | "ip4-dhcp-client-detect"), |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 69 | }; |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 70 | VNET_FEATURE_INIT (ip4_nat_det_classify, static) = { |
| 71 | .arc_name = "ip4-unicast", |
| 72 | .node_name = "nat44-det-classify", |
Matus Fabian | 16f0546 | 2018-02-08 05:28:28 -0800 | [diff] [blame] | 73 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"), |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 74 | }; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 75 | VNET_FEATURE_INIT (ip4_nat44_ed_in2out, static) = { |
| 76 | .arc_name = "ip4-unicast", |
| 77 | .node_name = "nat44-ed-in2out", |
| 78 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"), |
| 79 | }; |
| 80 | VNET_FEATURE_INIT (ip4_nat44_ed_out2in, static) = { |
| 81 | .arc_name = "ip4-unicast", |
| 82 | .node_name = "nat44-ed-out2in", |
| 83 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa", |
| 84 | "ip4-dhcp-client-detect"), |
| 85 | }; |
| 86 | VNET_FEATURE_INIT (ip4_nat44_ed_classify, static) = { |
| 87 | .arc_name = "ip4-unicast", |
| 88 | .node_name = "nat44-ed-classify", |
| 89 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"), |
| 90 | }; |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 91 | VNET_FEATURE_INIT (ip4_snat_in2out_worker_handoff, static) = { |
| 92 | .arc_name = "ip4-unicast", |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 93 | .node_name = "nat44-in2out-worker-handoff", |
Matus Fabian | 16f0546 | 2018-02-08 05:28:28 -0800 | [diff] [blame] | 94 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"), |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 95 | }; |
| 96 | VNET_FEATURE_INIT (ip4_snat_out2in_worker_handoff, static) = { |
| 97 | .arc_name = "ip4-unicast", |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 98 | .node_name = "nat44-out2in-worker-handoff", |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 99 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa", |
| 100 | "ip4-dhcp-client-detect"), |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 101 | }; |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 102 | VNET_FEATURE_INIT (ip4_nat_handoff_classify, static) = { |
| 103 | .arc_name = "ip4-unicast", |
| 104 | .node_name = "nat44-handoff-classify", |
Matus Fabian | 16f0546 | 2018-02-08 05:28:28 -0800 | [diff] [blame] | 105 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"), |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 106 | }; |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 107 | VNET_FEATURE_INIT (ip4_snat_in2out_fast, static) = { |
| 108 | .arc_name = "ip4-unicast", |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 109 | .node_name = "nat44-in2out-fast", |
Matus Fabian | 16f0546 | 2018-02-08 05:28:28 -0800 | [diff] [blame] | 110 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"), |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 111 | }; |
Damjan Marion | 8b3191e | 2016-11-09 19:54:20 +0100 | [diff] [blame] | 112 | VNET_FEATURE_INIT (ip4_snat_out2in_fast, static) = { |
| 113 | .arc_name = "ip4-unicast", |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 114 | .node_name = "nat44-out2in-fast", |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 115 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa", |
| 116 | "ip4-dhcp-client-detect"), |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 117 | }; |
Matus Fabian | 161c59c | 2017-07-21 03:46:03 -0700 | [diff] [blame] | 118 | VNET_FEATURE_INIT (ip4_snat_hairpin_dst, static) = { |
| 119 | .arc_name = "ip4-unicast", |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 120 | .node_name = "nat44-hairpin-dst", |
Matus Fabian | 16f0546 | 2018-02-08 05:28:28 -0800 | [diff] [blame] | 121 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"), |
Matus Fabian | 161c59c | 2017-07-21 03:46:03 -0700 | [diff] [blame] | 122 | }; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 123 | VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_dst, static) = { |
| 124 | .arc_name = "ip4-unicast", |
| 125 | .node_name = "nat44-ed-hairpin-dst", |
| 126 | .runs_after = VNET_FEATURES ("acl-plugin-in-ip4-fa"), |
| 127 | }; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 128 | |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 129 | /* Hook up output features */ |
| 130 | VNET_FEATURE_INIT (ip4_snat_in2out_output, static) = { |
| 131 | .arc_name = "ip4-output", |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 132 | .node_name = "nat44-in2out-output", |
Matus Fabian | 16f0546 | 2018-02-08 05:28:28 -0800 | [diff] [blame] | 133 | .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"), |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 134 | }; |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 135 | VNET_FEATURE_INIT (ip4_snat_in2out_output_worker_handoff, static) = { |
| 136 | .arc_name = "ip4-output", |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 137 | .node_name = "nat44-in2out-output-worker-handoff", |
Matus Fabian | 16f0546 | 2018-02-08 05:28:28 -0800 | [diff] [blame] | 138 | .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"), |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 139 | }; |
Matus Fabian | 161c59c | 2017-07-21 03:46:03 -0700 | [diff] [blame] | 140 | VNET_FEATURE_INIT (ip4_snat_hairpin_src, static) = { |
| 141 | .arc_name = "ip4-output", |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 142 | .node_name = "nat44-hairpin-src", |
Matus Fabian | 16f0546 | 2018-02-08 05:28:28 -0800 | [diff] [blame] | 143 | .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"), |
Matus Fabian | 161c59c | 2017-07-21 03:46:03 -0700 | [diff] [blame] | 144 | }; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 145 | VNET_FEATURE_INIT (ip4_nat44_ed_in2out_output, static) = { |
| 146 | .arc_name = "ip4-output", |
| 147 | .node_name = "nat44-ed-in2out-output", |
| 148 | .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"), |
| 149 | }; |
| 150 | VNET_FEATURE_INIT (ip4_nat44_ed_hairpin_src, static) = { |
| 151 | .arc_name = "ip4-output", |
| 152 | .node_name = "nat44-ed-hairpin-src", |
| 153 | .runs_after = VNET_FEATURES ("acl-plugin-out-ip4-fa"), |
| 154 | }; |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 155 | |
Matus Fabian | 87da476 | 2017-10-04 08:03:56 -0700 | [diff] [blame] | 156 | /* Hook up ip4-local features */ |
| 157 | VNET_FEATURE_INIT (ip4_nat_hairpinning, static) = |
| 158 | { |
| 159 | .arc_name = "ip4-local", |
| 160 | .node_name = "nat44-hairpinning", |
| 161 | .runs_before = VNET_FEATURES("ip4-local-end-of-arc"), |
| 162 | }; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 163 | VNET_FEATURE_INIT (ip4_nat44_ed_hairpinning, static) = |
| 164 | { |
| 165 | .arc_name = "ip4-local", |
| 166 | .node_name = "nat44-ed-hairpinning", |
| 167 | .runs_before = VNET_FEATURES("ip4-local-end-of-arc"), |
| 168 | }; |
Matus Fabian | 87da476 | 2017-10-04 08:03:56 -0700 | [diff] [blame] | 169 | |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 170 | |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 171 | VLIB_PLUGIN_REGISTER () = { |
| 172 | .version = VPP_BUILD_VER, |
Damjan Marion | 1bfb0dd | 2017-03-22 11:08:39 +0100 | [diff] [blame] | 173 | .description = "Network Address Translation", |
Damjan Marion | 3b46cba | 2017-01-23 21:13:45 +0100 | [diff] [blame] | 174 | }; |
| 175 | /* *INDENT-ON* */ |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 176 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 177 | void |
| 178 | nat_free_session_data (snat_main_t * sm, snat_session_t * s, u32 thread_index) |
| 179 | { |
| 180 | snat_session_key_t key; |
| 181 | clib_bihash_kv_8_8_t kv; |
| 182 | nat_ed_ses_key_t ed_key; |
| 183 | clib_bihash_kv_16_8_t ed_kv; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 184 | snat_main_per_thread_data_t *tsm = |
| 185 | vec_elt_at_index (sm->per_thread_data, thread_index); |
| 186 | |
Matus Fabian | 36ed73a | 2018-04-18 01:39:17 -0700 | [diff] [blame] | 187 | if (is_fwd_bypass_session (s)) |
| 188 | { |
| 189 | ed_key.l_addr = s->in2out.addr; |
| 190 | ed_key.r_addr = s->ext_host_addr; |
| 191 | ed_key.l_port = s->in2out.port; |
| 192 | ed_key.r_port = s->ext_host_port; |
| 193 | ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol); |
| 194 | ed_key.fib_index = 0; |
| 195 | ed_kv.key[0] = ed_key.as_u64[0]; |
| 196 | ed_kv.key[1] = ed_key.as_u64[1]; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 197 | if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 198 | nat_log_warn ("in2out_ed key del failed"); |
Matus Fabian | 36ed73a | 2018-04-18 01:39:17 -0700 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 202 | /* session lookup tables */ |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 203 | if (is_ed_session (s)) |
| 204 | { |
Matus Fabian | ea5b5be | 2018-09-03 05:02:23 -0700 | [diff] [blame] | 205 | if (is_affinity_sessions (s)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 206 | nat_affinity_unlock (s->ext_host_addr, s->out2in.addr, |
| 207 | s->in2out.protocol, s->out2in.port); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 208 | ed_key.l_addr = s->out2in.addr; |
| 209 | ed_key.r_addr = s->ext_host_addr; |
| 210 | ed_key.fib_index = s->out2in.fib_index; |
| 211 | if (snat_is_unk_proto_session (s)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 212 | { |
| 213 | ed_key.proto = s->in2out.port; |
| 214 | ed_key.r_port = 0; |
| 215 | ed_key.l_port = 0; |
| 216 | } |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 217 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 218 | { |
| 219 | ed_key.proto = snat_proto_to_ip_proto (s->in2out.protocol); |
| 220 | ed_key.l_port = s->out2in.port; |
| 221 | ed_key.r_port = s->ext_host_port; |
| 222 | } |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 223 | ed_kv.key[0] = ed_key.as_u64[0]; |
| 224 | ed_kv.key[1] = ed_key.as_u64[1]; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 225 | if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &ed_kv, 0)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 226 | nat_log_warn ("out2in_ed key del failed"); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 227 | ed_key.l_addr = s->in2out.addr; |
| 228 | ed_key.fib_index = s->in2out.fib_index; |
| 229 | if (!snat_is_unk_proto_session (s)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 230 | ed_key.l_port = s->in2out.port; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 231 | if (is_twice_nat_session (s)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 232 | { |
| 233 | ed_key.r_addr = s->ext_host_nat_addr; |
| 234 | ed_key.r_port = s->ext_host_nat_port; |
| 235 | } |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 236 | ed_kv.key[0] = ed_key.as_u64[0]; |
| 237 | ed_kv.key[1] = ed_key.as_u64[1]; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 238 | if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 239 | nat_log_warn ("in2out_ed key del failed"); |
Matus Fabian | ad1f3e1 | 2018-11-28 21:26:34 -0800 | [diff] [blame] | 240 | |
| 241 | nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index, |
| 242 | &s->in2out.addr, s->in2out.port, |
| 243 | &s->ext_host_nat_addr, s->ext_host_nat_port, |
| 244 | &s->out2in.addr, s->out2in.port, |
| 245 | &s->ext_host_addr, s->ext_host_port, |
| 246 | s->in2out.protocol, is_twice_nat_session (s)); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 247 | } |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 248 | else |
| 249 | { |
| 250 | kv.key = s->in2out.as_u64; |
| 251 | if (clib_bihash_add_del_8_8 (&tsm->in2out, &kv, 0)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 252 | nat_log_warn ("in2out key del failed"); |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 253 | kv.key = s->out2in.as_u64; |
| 254 | if (clib_bihash_add_del_8_8 (&tsm->out2in, &kv, 0)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 255 | nat_log_warn ("out2in key del failed"); |
Matus Fabian | ad1f3e1 | 2018-11-28 21:26:34 -0800 | [diff] [blame] | 256 | |
| 257 | nat_syslog_nat44_apmdel (s->user_index, s->in2out.fib_index, |
| 258 | &s->in2out.addr, s->in2out.port, |
| 259 | &s->out2in.addr, s->out2in.port, |
| 260 | s->in2out.protocol); |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 261 | } |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 262 | |
| 263 | if (snat_is_unk_proto_session (s)) |
| 264 | return; |
| 265 | |
| 266 | /* log NAT event */ |
Filip Varga | 8254ab8 | 2019-01-21 00:05:03 -0800 | [diff] [blame] | 267 | snat_ipfix_logging_nat44_ses_delete (thread_index, |
| 268 | s->in2out.addr.as_u32, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 269 | s->out2in.addr.as_u32, |
| 270 | s->in2out.protocol, |
| 271 | s->in2out.port, |
| 272 | s->out2in.port, s->in2out.fib_index); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 273 | |
| 274 | /* Twice NAT address and port for external host */ |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 275 | if (is_twice_nat_session (s)) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 276 | { |
shubing guo | 762a493 | 2018-08-13 17:16:46 +0800 | [diff] [blame] | 277 | key.protocol = s->in2out.protocol; |
| 278 | key.port = s->ext_host_nat_port; |
| 279 | key.addr.as_u32 = s->ext_host_nat_addr.as_u32; |
| 280 | snat_free_outside_address_and_port (sm->twice_nat_addresses, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 281 | thread_index, &key); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 284 | if (snat_is_session_static (s)) |
| 285 | return; |
| 286 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 287 | snat_free_outside_address_and_port (sm->addresses, thread_index, |
| 288 | &s->out2in); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | snat_user_t * |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 292 | nat_user_get_or_create (snat_main_t * sm, ip4_address_t * addr, u32 fib_index, |
| 293 | u32 thread_index) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 294 | { |
| 295 | snat_user_t *u = 0; |
| 296 | snat_user_key_t user_key; |
| 297 | clib_bihash_kv_8_8_t kv, value; |
| 298 | snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index]; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 299 | dlist_elt_t *per_user_list_head_elt; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 300 | |
| 301 | user_key.addr.as_u32 = addr->as_u32; |
| 302 | user_key.fib_index = fib_index; |
| 303 | kv.key = user_key.as_u64; |
| 304 | |
| 305 | /* Ever heard of the "user" = src ip4 address before? */ |
| 306 | if (clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)) |
| 307 | { |
| 308 | /* no, make a new one */ |
| 309 | pool_get (tsm->users, u); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 310 | clib_memset (u, 0, sizeof (*u)); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 311 | u->addr.as_u32 = addr->as_u32; |
| 312 | u->fib_index = fib_index; |
| 313 | |
| 314 | pool_get (tsm->list_pool, per_user_list_head_elt); |
| 315 | |
| 316 | u->sessions_per_user_list_head_index = per_user_list_head_elt - |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 317 | tsm->list_pool; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 318 | |
| 319 | clib_dlist_init (tsm->list_pool, u->sessions_per_user_list_head_index); |
| 320 | |
| 321 | kv.value = u - tsm->users; |
| 322 | |
| 323 | /* add user */ |
| 324 | if (clib_bihash_add_del_8_8 (&tsm->user_hash, &kv, 1)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 325 | nat_log_warn ("user_hash keay add failed"); |
Matus Fabian | fd0d508 | 2018-12-18 01:08:51 -0800 | [diff] [blame] | 326 | |
| 327 | vlib_set_simple_counter (&sm->total_users, thread_index, 0, |
| 328 | pool_elts (tsm->users)); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 329 | } |
| 330 | else |
| 331 | { |
| 332 | u = pool_elt_at_index (tsm->users, value.value); |
| 333 | } |
| 334 | |
| 335 | return u; |
| 336 | } |
| 337 | |
| 338 | snat_session_t * |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 339 | nat_session_alloc_or_recycle (snat_main_t * sm, snat_user_t * u, |
| 340 | u32 thread_index) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 341 | { |
| 342 | snat_session_t *s; |
| 343 | snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index]; |
| 344 | u32 oldest_per_user_translation_list_index, session_index; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 345 | dlist_elt_t *oldest_per_user_translation_list_elt; |
| 346 | dlist_elt_t *per_user_translation_list_elt; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 347 | |
| 348 | /* Over quota? Recycle the least recently used translation */ |
| 349 | if ((u->nsessions + u->nstaticsessions) >= sm->max_translations_per_user) |
| 350 | { |
| 351 | oldest_per_user_translation_list_index = |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 352 | clib_dlist_remove_head (tsm->list_pool, |
| 353 | u->sessions_per_user_list_head_index); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 354 | |
| 355 | ASSERT (oldest_per_user_translation_list_index != ~0); |
| 356 | |
| 357 | /* Add it back to the end of the LRU list */ |
| 358 | clib_dlist_addtail (tsm->list_pool, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 359 | u->sessions_per_user_list_head_index, |
| 360 | oldest_per_user_translation_list_index); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 361 | /* Get the list element */ |
| 362 | oldest_per_user_translation_list_elt = |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 363 | pool_elt_at_index (tsm->list_pool, |
| 364 | oldest_per_user_translation_list_index); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 365 | |
| 366 | /* Get the session index from the list element */ |
| 367 | session_index = oldest_per_user_translation_list_elt->value; |
| 368 | |
| 369 | /* Get the session */ |
| 370 | s = pool_elt_at_index (tsm->sessions, session_index); |
| 371 | nat_free_session_data (sm, s, thread_index); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 372 | if (snat_is_session_static (s)) |
| 373 | u->nstaticsessions--; |
Matus Fabian | 132dc49 | 2018-05-09 04:51:03 -0700 | [diff] [blame] | 374 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 375 | u->nsessions--; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 376 | s->flags = 0; |
| 377 | s->total_bytes = 0; |
| 378 | s->total_pkts = 0; |
Matus Fabian | ebdf190 | 2018-05-04 03:57:42 -0700 | [diff] [blame] | 379 | s->state = 0; |
| 380 | s->ext_host_addr.as_u32 = 0; |
| 381 | s->ext_host_port = 0; |
| 382 | s->ext_host_nat_addr.as_u32 = 0; |
| 383 | s->ext_host_nat_port = 0; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 384 | } |
| 385 | else |
| 386 | { |
| 387 | pool_get (tsm->sessions, s); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 388 | clib_memset (s, 0, sizeof (*s)); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 389 | |
| 390 | /* Create list elts */ |
| 391 | pool_get (tsm->list_pool, per_user_translation_list_elt); |
| 392 | clib_dlist_init (tsm->list_pool, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 393 | per_user_translation_list_elt - tsm->list_pool); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 394 | |
| 395 | per_user_translation_list_elt->value = s - tsm->sessions; |
| 396 | s->per_user_index = per_user_translation_list_elt - tsm->list_pool; |
| 397 | s->per_user_list_head_index = u->sessions_per_user_list_head_index; |
| 398 | |
| 399 | clib_dlist_addtail (tsm->list_pool, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 400 | s->per_user_list_head_index, |
| 401 | per_user_translation_list_elt - tsm->list_pool); |
Matus Fabian | ad1f3e1 | 2018-11-28 21:26:34 -0800 | [diff] [blame] | 402 | |
| 403 | s->user_index = u - tsm->users; |
Matus Fabian | fd0d508 | 2018-12-18 01:08:51 -0800 | [diff] [blame] | 404 | vlib_set_simple_counter (&sm->total_sessions, thread_index, 0, |
| 405 | pool_elts (tsm->sessions)); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | return s; |
| 409 | } |
| 410 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 411 | snat_session_t * |
Matus Fabian | 8fdc015 | 2018-09-24 04:41:28 -0700 | [diff] [blame] | 412 | nat_ed_session_alloc (snat_main_t * sm, snat_user_t * u, u32 thread_index, |
| 413 | f64 now) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 414 | { |
| 415 | snat_session_t *s; |
| 416 | snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index]; |
Matus Fabian | 8fdc015 | 2018-09-24 04:41:28 -0700 | [diff] [blame] | 417 | dlist_elt_t *per_user_translation_list_elt, *oldest_elt; |
| 418 | u32 oldest_index; |
| 419 | u64 sess_timeout_time; |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 420 | |
Matus Fabian | 7f8a8db | 2018-11-22 00:12:15 -0800 | [diff] [blame] | 421 | if (PREDICT_FALSE (!(u->nsessions) && !(u->nstaticsessions))) |
| 422 | goto alloc_new; |
| 423 | |
| 424 | oldest_index = |
| 425 | clib_dlist_remove_head (tsm->list_pool, |
| 426 | u->sessions_per_user_list_head_index); |
| 427 | oldest_elt = pool_elt_at_index (tsm->list_pool, oldest_index); |
| 428 | s = pool_elt_at_index (tsm->sessions, oldest_elt->value); |
| 429 | sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s); |
| 430 | if (now >= sess_timeout_time) |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 431 | { |
Matus Fabian | 7f8a8db | 2018-11-22 00:12:15 -0800 | [diff] [blame] | 432 | clib_dlist_addtail (tsm->list_pool, |
| 433 | u->sessions_per_user_list_head_index, oldest_index); |
| 434 | nat_free_session_data (sm, s, thread_index); |
| 435 | if (snat_is_session_static (s)) |
| 436 | u->nstaticsessions--; |
Matus Fabian | 8fdc015 | 2018-09-24 04:41:28 -0700 | [diff] [blame] | 437 | else |
Matus Fabian | 7f8a8db | 2018-11-22 00:12:15 -0800 | [diff] [blame] | 438 | u->nsessions--; |
| 439 | s->flags = 0; |
| 440 | s->total_bytes = 0; |
| 441 | s->total_pkts = 0; |
| 442 | s->state = 0; |
| 443 | s->ext_host_addr.as_u32 = 0; |
| 444 | s->ext_host_port = 0; |
| 445 | s->ext_host_nat_addr.as_u32 = 0; |
| 446 | s->ext_host_nat_port = 0; |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | clib_dlist_addhead (tsm->list_pool, |
| 451 | u->sessions_per_user_list_head_index, oldest_index); |
| 452 | if ((u->nsessions + u->nstaticsessions) >= |
| 453 | sm->max_translations_per_user) |
Matus Fabian | 8fdc015 | 2018-09-24 04:41:28 -0700 | [diff] [blame] | 454 | { |
Matus Fabian | 8fdc015 | 2018-09-24 04:41:28 -0700 | [diff] [blame] | 455 | nat_log_warn ("max translations per user %U", format_ip4_address, |
| 456 | &u->addr); |
| 457 | snat_ipfix_logging_max_entries_per_user |
Filip Varga | 8254ab8 | 2019-01-21 00:05:03 -0800 | [diff] [blame] | 458 | (thread_index, sm->max_translations_per_user, u->addr.as_u32); |
Matus Fabian | 8fdc015 | 2018-09-24 04:41:28 -0700 | [diff] [blame] | 459 | return 0; |
| 460 | } |
Matus Fabian | 7f8a8db | 2018-11-22 00:12:15 -0800 | [diff] [blame] | 461 | else |
| 462 | { |
| 463 | alloc_new: |
| 464 | pool_get (tsm->sessions, s); |
| 465 | clib_memset (s, 0, sizeof (*s)); |
| 466 | |
| 467 | /* Create list elts */ |
| 468 | pool_get (tsm->list_pool, per_user_translation_list_elt); |
| 469 | clib_dlist_init (tsm->list_pool, |
| 470 | per_user_translation_list_elt - tsm->list_pool); |
| 471 | |
| 472 | per_user_translation_list_elt->value = s - tsm->sessions; |
| 473 | s->per_user_index = per_user_translation_list_elt - tsm->list_pool; |
| 474 | s->per_user_list_head_index = u->sessions_per_user_list_head_index; |
| 475 | |
| 476 | clib_dlist_addtail (tsm->list_pool, |
| 477 | s->per_user_list_head_index, |
| 478 | per_user_translation_list_elt - tsm->list_pool); |
| 479 | } |
Matus Fabian | fd0d508 | 2018-12-18 01:08:51 -0800 | [diff] [blame] | 480 | |
| 481 | vlib_set_simple_counter (&sm->total_sessions, thread_index, 0, |
| 482 | pool_elts (tsm->sessions)); |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 485 | return s; |
| 486 | } |
| 487 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 488 | void |
| 489 | snat_add_del_addr_to_fib (ip4_address_t * addr, u8 p_len, u32 sw_if_index, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 490 | int is_add) |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 491 | { |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 492 | fib_prefix_t prefix = { |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 493 | .fp_len = p_len, |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 494 | .fp_proto = FIB_PROTOCOL_IP4, |
| 495 | .fp_addr = { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 496 | .ip4.as_u32 = addr->as_u32, |
| 497 | }, |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 498 | }; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 499 | u32 fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index); |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 500 | |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 501 | if (is_add) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 502 | fib_table_entry_update_one_path (fib_index, |
| 503 | &prefix, |
| 504 | FIB_SOURCE_PLUGIN_LOW, |
| 505 | (FIB_ENTRY_FLAG_CONNECTED | |
| 506 | FIB_ENTRY_FLAG_LOCAL | |
| 507 | FIB_ENTRY_FLAG_EXCLUSIVE), |
| 508 | DPO_PROTO_IP4, |
| 509 | NULL, |
| 510 | sw_if_index, |
| 511 | ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE); |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 512 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 513 | fib_table_entry_delete (fib_index, &prefix, FIB_SOURCE_PLUGIN_LOW); |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 516 | int |
| 517 | snat_add_address (snat_main_t * sm, ip4_address_t * addr, u32 vrf_id, |
| 518 | u8 twice_nat) |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 519 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 520 | snat_address_t *ap; |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 521 | snat_interface_t *i; |
Matus Fabian | 624b8d9 | 2017-09-12 04:15:30 -0700 | [diff] [blame] | 522 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 523 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 524 | if (twice_nat && !sm->endpoint_dependent) |
| 525 | return VNET_API_ERROR_FEATURE_DISABLED; |
| 526 | |
Matus Fabian | 860dacc | 2016-10-25 04:19:26 -0700 | [diff] [blame] | 527 | /* Check if address already exists */ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 528 | /* *INDENT-OFF* */ |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 529 | vec_foreach (ap, twice_nat ? sm->twice_nat_addresses : sm->addresses) |
Matus Fabian | 860dacc | 2016-10-25 04:19:26 -0700 | [diff] [blame] | 530 | { |
| 531 | if (ap->addr.as_u32 == addr->as_u32) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 532 | return VNET_API_ERROR_VALUE_EXIST; |
Matus Fabian | 860dacc | 2016-10-25 04:19:26 -0700 | [diff] [blame] | 533 | } |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 534 | /* *INDENT-ON* */ |
Matus Fabian | 860dacc | 2016-10-25 04:19:26 -0700 | [diff] [blame] | 535 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 536 | if (twice_nat) |
| 537 | vec_add2 (sm->twice_nat_addresses, ap, 1); |
| 538 | else |
| 539 | vec_add2 (sm->addresses, ap, 1); |
| 540 | |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 541 | ap->addr = *addr; |
Matus Fabian | f8d8490 | 2017-07-23 23:41:03 -0700 | [diff] [blame] | 542 | if (vrf_id != ~0) |
| 543 | ap->fib_index = |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 544 | fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 545 | FIB_SOURCE_PLUGIN_LOW); |
Matus Fabian | f8d8490 | 2017-07-23 23:41:03 -0700 | [diff] [blame] | 546 | else |
| 547 | ap->fib_index = ~0; |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 548 | #define _(N, i, n, s) \ |
Matus Fabian | 624b8d9 | 2017-09-12 04:15:30 -0700 | [diff] [blame] | 549 | clib_bitmap_alloc (ap->busy_##n##_port_bitmap, 65535); \ |
| 550 | ap->busy_##n##_ports = 0; \ |
dongjuan | df86520 | 2018-09-11 11:20:30 +0000 | [diff] [blame] | 551 | ap->busy_##n##_ports_per_thread = 0;\ |
Matus Fabian | 624b8d9 | 2017-09-12 04:15:30 -0700 | [diff] [blame] | 552 | vec_validate_init_empty (ap->busy_##n##_ports_per_thread, tm->n_vlib_mains - 1, 0); |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 553 | foreach_snat_protocol |
| 554 | #undef _ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 555 | if (twice_nat) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 556 | return 0; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 557 | |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 558 | /* Add external address to FIB */ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 559 | /* *INDENT-OFF* */ |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 560 | pool_foreach (i, sm->interfaces, |
| 561 | ({ |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 562 | if (nat_interface_is_inside(i) || sm->out2in_dpo) |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 563 | continue; |
| 564 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 565 | snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1); |
Matus Fabian | dccbee3 | 2017-01-31 22:20:30 -0800 | [diff] [blame] | 566 | break; |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 567 | })); |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 568 | pool_foreach (i, sm->output_feature_interfaces, |
| 569 | ({ |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 570 | if (nat_interface_is_inside(i) || sm->out2in_dpo) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 571 | continue; |
| 572 | |
| 573 | snat_add_del_addr_to_fib(addr, 32, i->sw_if_index, 1); |
| 574 | break; |
| 575 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 576 | /* *INDENT-ON* */ |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 577 | |
| 578 | return 0; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 579 | } |
| 580 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 581 | static int |
| 582 | is_snat_address_used_in_static_mapping (snat_main_t * sm, ip4_address_t addr) |
Matus Fabian | 724b815 | 2016-10-04 03:23:43 -0700 | [diff] [blame] | 583 | { |
| 584 | snat_static_mapping_t *m; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 585 | /* *INDENT-OFF* */ |
Matus Fabian | 724b815 | 2016-10-04 03:23:43 -0700 | [diff] [blame] | 586 | pool_foreach (m, sm->static_mappings, |
| 587 | ({ |
Matus Fabian | c3df1e9 | 2018-11-06 23:17:31 -0800 | [diff] [blame] | 588 | if (is_addr_only_static_mapping (m) || |
| 589 | is_out2in_only_static_mapping (m) || |
| 590 | is_identity_static_mapping (m)) |
| 591 | continue; |
Matus Fabian | 724b815 | 2016-10-04 03:23:43 -0700 | [diff] [blame] | 592 | if (m->external_addr.as_u32 == addr.as_u32) |
| 593 | return 1; |
| 594 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 595 | /* *INDENT-ON* */ |
Matus Fabian | 724b815 | 2016-10-04 03:23:43 -0700 | [diff] [blame] | 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 600 | void |
| 601 | increment_v4_address (ip4_address_t * a) |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 602 | { |
| 603 | u32 v; |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 604 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 605 | v = clib_net_to_host_u32 (a->as_u32) + 1; |
| 606 | a->as_u32 = clib_host_to_net_u32 (v); |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 607 | } |
| 608 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 609 | static void |
| 610 | snat_add_static_mapping_when_resolved (snat_main_t * sm, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 611 | ip4_address_t l_addr, |
| 612 | u16 l_port, |
| 613 | u32 sw_if_index, |
| 614 | u16 e_port, |
| 615 | u32 vrf_id, |
| 616 | snat_protocol_t proto, |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 617 | int addr_only, int is_add, u8 * tag, |
| 618 | int twice_nat, int out2in_only, |
| 619 | int identity_nat) |
Dave Barach | 8b27537 | 2017-01-16 10:54:02 -0500 | [diff] [blame] | 620 | { |
| 621 | snat_static_map_resolve_t *rp; |
| 622 | |
| 623 | vec_add2 (sm->to_resolve, rp, 1); |
| 624 | rp->l_addr.as_u32 = l_addr.as_u32; |
| 625 | rp->l_port = l_port; |
| 626 | rp->sw_if_index = sw_if_index; |
| 627 | rp->e_port = e_port; |
| 628 | rp->vrf_id = vrf_id; |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 629 | rp->proto = proto; |
Dave Barach | 8b27537 | 2017-01-16 10:54:02 -0500 | [diff] [blame] | 630 | rp->addr_only = addr_only; |
| 631 | rp->is_add = is_add; |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 632 | rp->twice_nat = twice_nat; |
| 633 | rp->out2in_only = out2in_only; |
| 634 | rp->identity_nat = identity_nat; |
Matus Fabian | 5f22499 | 2018-01-25 21:59:16 -0800 | [diff] [blame] | 635 | rp->tag = vec_dup (tag); |
Dave Barach | 8b27537 | 2017-01-16 10:54:02 -0500 | [diff] [blame] | 636 | } |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 637 | |
| 638 | static u32 |
| 639 | get_thread_idx_by_port (u16 e_port) |
dongjuan | 58f50f1 | 2018-09-04 17:40:53 +0800 | [diff] [blame] | 640 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 641 | snat_main_t *sm = &snat_main; |
| 642 | u32 thread_idx = sm->num_workers; |
| 643 | if (sm->num_workers > 1) |
dongjuan | 58f50f1 | 2018-09-04 17:40:53 +0800 | [diff] [blame] | 644 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 645 | thread_idx = |
| 646 | sm->first_worker_index + |
| 647 | sm->workers[(e_port - 1024) / sm->port_per_thread]; |
| 648 | } |
| 649 | return thread_idx; |
dongjuan | 58f50f1 | 2018-09-04 17:40:53 +0800 | [diff] [blame] | 650 | } |
Dave Barach | 8b27537 | 2017-01-16 10:54:02 -0500 | [diff] [blame] | 651 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 652 | int |
| 653 | snat_add_static_mapping (ip4_address_t l_addr, ip4_address_t e_addr, |
| 654 | u16 l_port, u16 e_port, u32 vrf_id, int addr_only, |
| 655 | u32 sw_if_index, snat_protocol_t proto, int is_add, |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 656 | twice_nat_type_t twice_nat, u8 out2in_only, u8 * tag, |
| 657 | u8 identity_nat) |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 658 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 659 | snat_main_t *sm = &snat_main; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 660 | snat_static_mapping_t *m; |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 661 | snat_session_key_t m_key; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 662 | clib_bihash_kv_8_8_t kv, value; |
| 663 | snat_address_t *a = 0; |
| 664 | u32 fib_index = ~0; |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 665 | snat_interface_t *interface; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 666 | int i; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 667 | snat_main_per_thread_data_t *tsm; |
Matus Fabian | b793d09 | 2018-01-31 05:50:21 -0800 | [diff] [blame] | 668 | snat_user_key_t u_key; |
| 669 | snat_user_t *u; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 670 | dlist_elt_t *head, *elt; |
Matus Fabian | b793d09 | 2018-01-31 05:50:21 -0800 | [diff] [blame] | 671 | u32 elt_index, head_index; |
| 672 | u32 ses_index; |
| 673 | u64 user_index; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 674 | snat_session_t *s; |
Matus Fabian | f13a878 | 2018-04-06 02:54:40 -0700 | [diff] [blame] | 675 | snat_static_map_resolve_t *rp, *rp_match = 0; |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 676 | nat44_lb_addr_port_t *local; |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 677 | u32 find = ~0; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 678 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 679 | if (!sm->endpoint_dependent) |
| 680 | { |
| 681 | if (twice_nat || out2in_only) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 682 | return VNET_API_ERROR_FEATURE_DISABLED; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Dave Barach | 8b27537 | 2017-01-16 10:54:02 -0500 | [diff] [blame] | 685 | /* If the external address is a specific interface address */ |
| 686 | if (sw_if_index != ~0) |
| 687 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 688 | ip4_address_t *first_int_addr; |
Matus Fabian | ea2600a | 2018-03-28 04:06:26 -0700 | [diff] [blame] | 689 | |
| 690 | for (i = 0; i < vec_len (sm->to_resolve); i++) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 691 | { |
| 692 | rp = sm->to_resolve + i; |
| 693 | if (rp->sw_if_index != sw_if_index || |
| 694 | rp->l_addr.as_u32 != l_addr.as_u32 || |
| 695 | rp->vrf_id != vrf_id || rp->addr_only != addr_only) |
| 696 | continue; |
Matus Fabian | ea2600a | 2018-03-28 04:06:26 -0700 | [diff] [blame] | 697 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 698 | if (!addr_only) |
| 699 | { |
| 700 | if (rp->l_port != l_port || rp->e_port != e_port |
| 701 | || rp->proto != proto) |
| 702 | continue; |
| 703 | } |
Matus Fabian | ea2600a | 2018-03-28 04:06:26 -0700 | [diff] [blame] | 704 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 705 | rp_match = rp; |
| 706 | break; |
| 707 | } |
Dave Barach | 8b27537 | 2017-01-16 10:54:02 -0500 | [diff] [blame] | 708 | |
| 709 | /* Might be already set... */ |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 710 | first_int_addr = ip4_interface_first_address |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 711 | (sm->ip4_main, sw_if_index, 0 /* just want the address */ ); |
Dave Barach | 8b27537 | 2017-01-16 10:54:02 -0500 | [diff] [blame] | 712 | |
Matus Fabian | ea2600a | 2018-03-28 04:06:26 -0700 | [diff] [blame] | 713 | if (is_add) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 714 | { |
| 715 | if (rp_match) |
| 716 | return VNET_API_ERROR_VALUE_EXIST; |
Matus Fabian | ea2600a | 2018-03-28 04:06:26 -0700 | [diff] [blame] | 717 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 718 | snat_add_static_mapping_when_resolved |
| 719 | (sm, l_addr, l_port, sw_if_index, e_port, vrf_id, proto, |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 720 | addr_only, is_add, tag, twice_nat, out2in_only, identity_nat); |
Matus Fabian | 4772e7a | 2018-04-04 00:38:02 -0700 | [diff] [blame] | 721 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 722 | /* DHCP resolution required? */ |
| 723 | if (first_int_addr == 0) |
| 724 | { |
| 725 | return 0; |
| 726 | } |
| 727 | else |
| 728 | { |
| 729 | e_addr.as_u32 = first_int_addr->as_u32; |
| 730 | /* Identity mapping? */ |
| 731 | if (l_addr.as_u32 == 0) |
| 732 | l_addr.as_u32 = e_addr.as_u32; |
| 733 | } |
| 734 | } |
Matus Fabian | ea2600a | 2018-03-28 04:06:26 -0700 | [diff] [blame] | 735 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 736 | { |
| 737 | if (!rp_match) |
| 738 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
Matus Fabian | ea2600a | 2018-03-28 04:06:26 -0700 | [diff] [blame] | 739 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 740 | vec_del1 (sm->to_resolve, i); |
Matus Fabian | ea2600a | 2018-03-28 04:06:26 -0700 | [diff] [blame] | 741 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 742 | if (first_int_addr) |
| 743 | { |
| 744 | e_addr.as_u32 = first_int_addr->as_u32; |
| 745 | /* Identity mapping? */ |
| 746 | if (l_addr.as_u32 == 0) |
| 747 | l_addr.as_u32 = e_addr.as_u32; |
| 748 | } |
| 749 | else |
| 750 | return 0; |
| 751 | } |
Dave Barach | 8b27537 | 2017-01-16 10:54:02 -0500 | [diff] [blame] | 752 | } |
| 753 | |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 754 | m_key.addr = e_addr; |
| 755 | m_key.port = addr_only ? 0 : e_port; |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 756 | m_key.protocol = addr_only ? 0 : proto; |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 757 | m_key.fib_index = 0; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 758 | kv.key = m_key.as_u64; |
| 759 | if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value)) |
| 760 | m = 0; |
| 761 | else |
| 762 | m = pool_elt_at_index (sm->static_mappings, value.value); |
| 763 | |
| 764 | if (is_add) |
| 765 | { |
| 766 | if (m) |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 767 | { |
| 768 | if (is_identity_static_mapping (m)) |
| 769 | { |
| 770 | /* *INDENT-OFF* */ |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 771 | pool_foreach (local, m->locals, |
| 772 | ({ |
| 773 | if (local->vrf_id == vrf_id) |
| 774 | return VNET_API_ERROR_VALUE_EXIST; |
| 775 | })); |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 776 | /* *INDENT-ON* */ |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 777 | pool_get (m->locals, local); |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 778 | local->vrf_id = vrf_id; |
| 779 | local->fib_index = |
| 780 | fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id, |
| 781 | FIB_SOURCE_PLUGIN_LOW); |
| 782 | m_key.addr = m->local_addr; |
| 783 | m_key.port = m->local_port; |
| 784 | m_key.protocol = m->proto; |
| 785 | m_key.fib_index = local->fib_index; |
| 786 | kv.key = m_key.as_u64; |
| 787 | kv.value = m - sm->static_mappings; |
| 788 | clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1); |
| 789 | return 0; |
| 790 | } |
| 791 | else |
| 792 | return VNET_API_ERROR_VALUE_EXIST; |
| 793 | } |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 794 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 795 | if (twice_nat && addr_only) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 796 | return VNET_API_ERROR_UNSUPPORTED; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 797 | |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 798 | /* Convert VRF id to FIB index */ |
| 799 | if (vrf_id != ~0) |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 800 | fib_index = |
| 801 | fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id, |
| 802 | FIB_SOURCE_PLUGIN_LOW); |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 803 | /* If not specified use inside VRF id from SNAT plugin startup config */ |
| 804 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 805 | { |
| 806 | fib_index = sm->inside_fib_index; |
| 807 | vrf_id = sm->inside_vrf_id; |
| 808 | } |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 809 | |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 810 | if (!(out2in_only || identity_nat)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 811 | { |
| 812 | m_key.addr = l_addr; |
| 813 | m_key.port = addr_only ? 0 : l_port; |
| 814 | m_key.protocol = addr_only ? 0 : proto; |
| 815 | m_key.fib_index = fib_index; |
| 816 | kv.key = m_key.as_u64; |
| 817 | if (!clib_bihash_search_8_8 |
| 818 | (&sm->static_mapping_by_local, &kv, &value)) |
| 819 | return VNET_API_ERROR_VALUE_EXIST; |
| 820 | } |
Matus Fabian | 36a6270 | 2018-04-04 03:27:43 -0700 | [diff] [blame] | 821 | |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 822 | /* Find external address in allocated addresses and reserve port for |
| 823 | address and port pair mapping when dynamic translations enabled */ |
Matus Fabian | e82488f | 2018-01-18 03:38:45 -0800 | [diff] [blame] | 824 | if (!(addr_only || sm->static_mapping_only || out2in_only)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 825 | { |
| 826 | for (i = 0; i < vec_len (sm->addresses); i++) |
| 827 | { |
| 828 | if (sm->addresses[i].addr.as_u32 == e_addr.as_u32) |
| 829 | { |
| 830 | a = sm->addresses + i; |
| 831 | /* External port must be unused */ |
| 832 | switch (proto) |
| 833 | { |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 834 | #define _(N, j, n, s) \ |
| 835 | case SNAT_PROTOCOL_##N: \ |
| 836 | if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \ |
| 837 | return VNET_API_ERROR_INVALID_VALUE; \ |
| 838 | clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \ |
| 839 | if (e_port > 1024) \ |
Matus Fabian | 624b8d9 | 2017-09-12 04:15:30 -0700 | [diff] [blame] | 840 | { \ |
| 841 | a->busy_##n##_ports++; \ |
dongjuan | 58f50f1 | 2018-09-04 17:40:53 +0800 | [diff] [blame] | 842 | a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \ |
Matus Fabian | 624b8d9 | 2017-09-12 04:15:30 -0700 | [diff] [blame] | 843 | } \ |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 844 | break; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 845 | foreach_snat_protocol |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 846 | #undef _ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 847 | default: |
| 848 | nat_log_info ("unknown protocol"); |
| 849 | return VNET_API_ERROR_INVALID_VALUE_2; |
| 850 | } |
| 851 | break; |
| 852 | } |
| 853 | } |
| 854 | /* External address must be allocated */ |
| 855 | if (!a && (l_addr.as_u32 != e_addr.as_u32)) |
| 856 | { |
| 857 | if (sw_if_index != ~0) |
| 858 | { |
| 859 | for (i = 0; i < vec_len (sm->to_resolve); i++) |
| 860 | { |
| 861 | rp = sm->to_resolve + i; |
| 862 | if (rp->addr_only) |
| 863 | continue; |
| 864 | if (rp->sw_if_index != sw_if_index && |
| 865 | rp->l_addr.as_u32 != l_addr.as_u32 && |
| 866 | rp->vrf_id != vrf_id && rp->l_port != l_port && |
| 867 | rp->e_port != e_port && rp->proto != proto) |
| 868 | continue; |
Matus Fabian | f13a878 | 2018-04-06 02:54:40 -0700 | [diff] [blame] | 869 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 870 | vec_del1 (sm->to_resolve, i); |
| 871 | break; |
| 872 | } |
| 873 | } |
| 874 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 875 | } |
| 876 | } |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 877 | |
| 878 | pool_get (sm->static_mappings, m); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 879 | clib_memset (m, 0, sizeof (*m)); |
Matus Fabian | 5f22499 | 2018-01-25 21:59:16 -0800 | [diff] [blame] | 880 | m->tag = vec_dup (tag); |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 881 | m->local_addr = l_addr; |
| 882 | m->external_addr = e_addr; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 883 | m->twice_nat = twice_nat; |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 884 | if (out2in_only) |
| 885 | m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY; |
| 886 | if (addr_only) |
| 887 | m->flags |= NAT_STATIC_MAPPING_FLAG_ADDR_ONLY; |
| 888 | if (identity_nat) |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 889 | { |
| 890 | m->flags |= NAT_STATIC_MAPPING_FLAG_IDENTITY_NAT; |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 891 | pool_get (m->locals, local); |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 892 | local->vrf_id = vrf_id; |
| 893 | local->fib_index = fib_index; |
| 894 | } |
| 895 | else |
| 896 | { |
| 897 | m->vrf_id = vrf_id; |
| 898 | m->fib_index = fib_index; |
| 899 | } |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 900 | if (!addr_only) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 901 | { |
| 902 | m->local_port = l_port; |
| 903 | m->external_port = e_port; |
| 904 | m->proto = proto; |
| 905 | } |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 906 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 907 | if (sm->num_workers > 1) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 908 | { |
| 909 | ip4_header_t ip = { |
| 910 | .src_address = m->local_addr, |
| 911 | }; |
| 912 | vec_add1 (m->workers, sm->worker_in2out_cb (&ip, m->fib_index)); |
| 913 | tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]); |
| 914 | } |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 915 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 916 | tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 917 | |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 918 | m_key.addr = m->local_addr; |
| 919 | m_key.port = m->local_port; |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 920 | m_key.protocol = m->proto; |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 921 | m_key.fib_index = fib_index; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 922 | kv.key = m_key.as_u64; |
| 923 | kv.value = m - sm->static_mappings; |
Matus Fabian | e82488f | 2018-01-18 03:38:45 -0800 | [diff] [blame] | 924 | if (!out2in_only) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 925 | clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1); |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 926 | |
| 927 | m_key.addr = m->external_addr; |
| 928 | m_key.port = m->external_port; |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 929 | m_key.fib_index = 0; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 930 | kv.key = m_key.as_u64; |
| 931 | kv.value = m - sm->static_mappings; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 932 | clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 933 | |
Matus Fabian | b793d09 | 2018-01-31 05:50:21 -0800 | [diff] [blame] | 934 | /* Delete dynamic sessions matching local address (+ local port) */ |
| 935 | if (!(sm->static_mapping_only)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 936 | { |
| 937 | u_key.addr = m->local_addr; |
| 938 | u_key.fib_index = m->fib_index; |
| 939 | kv.key = u_key.as_u64; |
| 940 | if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)) |
| 941 | { |
| 942 | user_index = value.value; |
| 943 | u = pool_elt_at_index (tsm->users, user_index); |
| 944 | if (u->nsessions) |
| 945 | { |
| 946 | head_index = u->sessions_per_user_list_head_index; |
| 947 | head = pool_elt_at_index (tsm->list_pool, head_index); |
| 948 | elt_index = head->next; |
| 949 | elt = pool_elt_at_index (tsm->list_pool, elt_index); |
| 950 | ses_index = elt->value; |
| 951 | while (ses_index != ~0) |
| 952 | { |
| 953 | s = pool_elt_at_index (tsm->sessions, ses_index); |
| 954 | elt = pool_elt_at_index (tsm->list_pool, elt->next); |
| 955 | ses_index = elt->value; |
Matus Fabian | b793d09 | 2018-01-31 05:50:21 -0800 | [diff] [blame] | 956 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 957 | if (snat_is_session_static (s)) |
| 958 | continue; |
Matus Fabian | b793d09 | 2018-01-31 05:50:21 -0800 | [diff] [blame] | 959 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 960 | if (!addr_only |
| 961 | && (clib_net_to_host_u16 (s->in2out.port) != |
| 962 | m->local_port)) |
| 963 | continue; |
Matus Fabian | b793d09 | 2018-01-31 05:50:21 -0800 | [diff] [blame] | 964 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 965 | nat_free_session_data (sm, s, |
| 966 | tsm - sm->per_thread_data); |
| 967 | nat44_delete_session (sm, s, tsm - sm->per_thread_data); |
Matus Fabian | b793d09 | 2018-01-31 05:50:21 -0800 | [diff] [blame] | 968 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 969 | if (!addr_only && !sm->endpoint_dependent) |
| 970 | break; |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | } |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 975 | } |
| 976 | else |
| 977 | { |
| 978 | if (!m) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 979 | { |
| 980 | if (sw_if_index != ~0) |
| 981 | return 0; |
| 982 | else |
| 983 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 984 | } |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 985 | |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 986 | if (identity_nat) |
| 987 | { |
Matus Fabian | c3df1e9 | 2018-11-06 23:17:31 -0800 | [diff] [blame] | 988 | if (vrf_id == ~0) |
| 989 | vrf_id = sm->inside_vrf_id; |
| 990 | |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 991 | /* *INDENT-OFF* */ |
| 992 | pool_foreach (local, m->locals, |
| 993 | ({ |
| 994 | if (local->vrf_id == vrf_id) |
| 995 | find = local - m->locals; |
| 996 | })); |
| 997 | /* *INDENT-ON* */ |
| 998 | if (find == ~0) |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 999 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 1000 | |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 1001 | local = pool_elt_at_index (m->locals, find); |
| 1002 | fib_index = local->fib_index; |
| 1003 | pool_put (m->locals, local); |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 1004 | } |
| 1005 | else |
| 1006 | fib_index = m->fib_index; |
| 1007 | |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 1008 | /* Free external address port */ |
Matus Fabian | e82488f | 2018-01-18 03:38:45 -0800 | [diff] [blame] | 1009 | if (!(addr_only || sm->static_mapping_only || out2in_only)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1010 | { |
| 1011 | for (i = 0; i < vec_len (sm->addresses); i++) |
| 1012 | { |
| 1013 | if (sm->addresses[i].addr.as_u32 == e_addr.as_u32) |
| 1014 | { |
| 1015 | a = sm->addresses + i; |
| 1016 | switch (proto) |
| 1017 | { |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 1018 | #define _(N, j, n, s) \ |
| 1019 | case SNAT_PROTOCOL_##N: \ |
| 1020 | clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \ |
| 1021 | if (e_port > 1024) \ |
Matus Fabian | 624b8d9 | 2017-09-12 04:15:30 -0700 | [diff] [blame] | 1022 | { \ |
| 1023 | a->busy_##n##_ports--; \ |
dongjuan | 58f50f1 | 2018-09-04 17:40:53 +0800 | [diff] [blame] | 1024 | a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \ |
Matus Fabian | 624b8d9 | 2017-09-12 04:15:30 -0700 | [diff] [blame] | 1025 | } \ |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 1026 | break; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1027 | foreach_snat_protocol |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 1028 | #undef _ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1029 | default: |
| 1030 | nat_log_info ("unknown protocol"); |
| 1031 | return VNET_API_ERROR_INVALID_VALUE_2; |
| 1032 | } |
| 1033 | break; |
| 1034 | } |
| 1035 | } |
| 1036 | } |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 1037 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1038 | if (sm->num_workers > 1) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1039 | tsm = vec_elt_at_index (sm->per_thread_data, m->workers[0]); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1040 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1041 | tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1042 | |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 1043 | m_key.addr = m->local_addr; |
| 1044 | m_key.port = m->local_port; |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 1045 | m_key.protocol = m->proto; |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 1046 | m_key.fib_index = fib_index; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 1047 | kv.key = m_key.as_u64; |
Matus Fabian | e82488f | 2018-01-18 03:38:45 -0800 | [diff] [blame] | 1048 | if (!out2in_only) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1049 | clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0); |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 1050 | |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 1051 | /* Delete session(s) for static mapping if exist */ |
| 1052 | if (!(sm->static_mapping_only) || |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1053 | (sm->static_mapping_only && sm->static_mapping_connection_tracking)) |
| 1054 | { |
| 1055 | u_key.addr = m->local_addr; |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 1056 | u_key.fib_index = fib_index; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1057 | kv.key = u_key.as_u64; |
| 1058 | if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)) |
| 1059 | { |
| 1060 | user_index = value.value; |
| 1061 | u = pool_elt_at_index (tsm->users, user_index); |
| 1062 | if (u->nstaticsessions) |
| 1063 | { |
| 1064 | head_index = u->sessions_per_user_list_head_index; |
| 1065 | head = pool_elt_at_index (tsm->list_pool, head_index); |
| 1066 | elt_index = head->next; |
| 1067 | elt = pool_elt_at_index (tsm->list_pool, elt_index); |
| 1068 | ses_index = elt->value; |
| 1069 | while (ses_index != ~0) |
| 1070 | { |
| 1071 | s = pool_elt_at_index (tsm->sessions, ses_index); |
| 1072 | elt = pool_elt_at_index (tsm->list_pool, elt->next); |
| 1073 | ses_index = elt->value; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 1074 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1075 | if (!addr_only) |
| 1076 | { |
| 1077 | if ((s->out2in.addr.as_u32 != e_addr.as_u32) || |
| 1078 | (clib_net_to_host_u16 (s->out2in.port) != |
| 1079 | e_port)) |
| 1080 | continue; |
| 1081 | } |
Matus Fabian | 13c0818 | 2018-04-11 00:36:57 -0700 | [diff] [blame] | 1082 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1083 | if (is_lb_session (s)) |
| 1084 | continue; |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 1085 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1086 | if (!snat_is_session_static (s)) |
| 1087 | continue; |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 1088 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1089 | nat_free_session_data (sm, s, |
| 1090 | tsm - sm->per_thread_data); |
| 1091 | nat44_delete_session (sm, s, tsm - sm->per_thread_data); |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 1092 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1093 | if (!addr_only && !sm->endpoint_dependent) |
| 1094 | break; |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | } |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 1099 | |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 1100 | fib_table_unlock (fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW); |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 1101 | if (pool_elts (m->locals)) |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 1102 | return 0; |
| 1103 | |
| 1104 | m_key.addr = m->external_addr; |
| 1105 | m_key.port = m->external_port; |
| 1106 | m_key.fib_index = 0; |
| 1107 | kv.key = m_key.as_u64; |
| 1108 | clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0); |
| 1109 | |
Matus Fabian | 5f22499 | 2018-01-25 21:59:16 -0800 | [diff] [blame] | 1110 | vec_free (m->tag); |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1111 | vec_free (m->workers); |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 1112 | /* Delete static mapping from pool */ |
| 1113 | pool_put (sm->static_mappings, m); |
| 1114 | } |
| 1115 | |
Matus Fabian | ab7a805 | 2017-11-28 04:29:41 -0800 | [diff] [blame] | 1116 | if (!addr_only || (l_addr.as_u32 == e_addr.as_u32)) |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1117 | return 0; |
| 1118 | |
| 1119 | /* Add/delete external address to FIB */ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1120 | /* *INDENT-OFF* */ |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1121 | pool_foreach (interface, sm->interfaces, |
| 1122 | ({ |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 1123 | if (nat_interface_is_inside(interface) || sm->out2in_dpo) |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1124 | continue; |
| 1125 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 1126 | snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add); |
Matus Fabian | dccbee3 | 2017-01-31 22:20:30 -0800 | [diff] [blame] | 1127 | break; |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1128 | })); |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 1129 | pool_foreach (interface, sm->output_feature_interfaces, |
| 1130 | ({ |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 1131 | if (nat_interface_is_inside(interface) || sm->out2in_dpo) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 1132 | continue; |
| 1133 | |
| 1134 | snat_add_del_addr_to_fib(&e_addr, 32, interface->sw_if_index, is_add); |
| 1135 | break; |
| 1136 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1137 | /* *INDENT-ON* */ |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1138 | |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 1139 | return 0; |
| 1140 | } |
| 1141 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1142 | int |
| 1143 | nat44_add_del_lb_static_mapping (ip4_address_t e_addr, u16 e_port, |
| 1144 | snat_protocol_t proto, |
| 1145 | nat44_lb_addr_port_t * locals, u8 is_add, |
| 1146 | twice_nat_type_t twice_nat, u8 out2in_only, |
| 1147 | u8 * tag, u32 affinity) |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1148 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1149 | snat_main_t *sm = &snat_main; |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1150 | snat_static_mapping_t *m; |
| 1151 | snat_session_key_t m_key; |
| 1152 | clib_bihash_kv_8_8_t kv, value; |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1153 | snat_address_t *a = 0; |
| 1154 | int i; |
| 1155 | nat44_lb_addr_port_t *local; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1156 | u32 elt_index, head_index, ses_index; |
Matus Fabian | 092b3cd | 2017-09-19 05:42:38 -0700 | [diff] [blame] | 1157 | snat_main_per_thread_data_t *tsm; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1158 | snat_user_key_t u_key; |
| 1159 | snat_user_t *u; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1160 | snat_session_t *s; |
| 1161 | dlist_elt_t *head, *elt; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1162 | uword *bitmap = 0; |
| 1163 | |
| 1164 | if (!sm->endpoint_dependent) |
| 1165 | return VNET_API_ERROR_FEATURE_DISABLED; |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1166 | |
| 1167 | m_key.addr = e_addr; |
| 1168 | m_key.port = e_port; |
| 1169 | m_key.protocol = proto; |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 1170 | m_key.fib_index = 0; |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1171 | kv.key = m_key.as_u64; |
| 1172 | if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value)) |
| 1173 | m = 0; |
| 1174 | else |
| 1175 | m = pool_elt_at_index (sm->static_mappings, value.value); |
| 1176 | |
| 1177 | if (is_add) |
| 1178 | { |
| 1179 | if (m) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1180 | return VNET_API_ERROR_VALUE_EXIST; |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1181 | |
| 1182 | if (vec_len (locals) < 2) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1183 | return VNET_API_ERROR_INVALID_VALUE; |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1184 | |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1185 | /* Find external address in allocated addresses and reserve port for |
| 1186 | address and port pair mapping when dynamic translations enabled */ |
Matus Fabian | 240b5ef | 2018-01-11 04:09:17 -0800 | [diff] [blame] | 1187 | if (!(sm->static_mapping_only || out2in_only)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1188 | { |
| 1189 | for (i = 0; i < vec_len (sm->addresses); i++) |
| 1190 | { |
| 1191 | if (sm->addresses[i].addr.as_u32 == e_addr.as_u32) |
| 1192 | { |
| 1193 | a = sm->addresses + i; |
| 1194 | /* External port must be unused */ |
| 1195 | switch (proto) |
| 1196 | { |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1197 | #define _(N, j, n, s) \ |
| 1198 | case SNAT_PROTOCOL_##N: \ |
| 1199 | if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, e_port)) \ |
| 1200 | return VNET_API_ERROR_INVALID_VALUE; \ |
| 1201 | clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 1); \ |
| 1202 | if (e_port > 1024) \ |
Matus Fabian | 624b8d9 | 2017-09-12 04:15:30 -0700 | [diff] [blame] | 1203 | { \ |
| 1204 | a->busy_##n##_ports++; \ |
dongjuan | 58f50f1 | 2018-09-04 17:40:53 +0800 | [diff] [blame] | 1205 | a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]++; \ |
Matus Fabian | 624b8d9 | 2017-09-12 04:15:30 -0700 | [diff] [blame] | 1206 | } \ |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1207 | break; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1208 | foreach_snat_protocol |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1209 | #undef _ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1210 | default: |
| 1211 | nat_log_info ("unknown protocol"); |
| 1212 | return VNET_API_ERROR_INVALID_VALUE_2; |
| 1213 | } |
| 1214 | break; |
| 1215 | } |
| 1216 | } |
| 1217 | /* External address must be allocated */ |
| 1218 | if (!a) |
| 1219 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 1220 | } |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1221 | |
| 1222 | pool_get (sm->static_mappings, m); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1223 | clib_memset (m, 0, sizeof (*m)); |
Matus Fabian | 5f22499 | 2018-01-25 21:59:16 -0800 | [diff] [blame] | 1224 | m->tag = vec_dup (tag); |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1225 | m->external_addr = e_addr; |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1226 | m->external_port = e_port; |
| 1227 | m->proto = proto; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1228 | m->twice_nat = twice_nat; |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 1229 | m->flags |= NAT_STATIC_MAPPING_FLAG_LB; |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 1230 | if (out2in_only) |
| 1231 | m->flags |= NAT_STATIC_MAPPING_FLAG_OUT2IN_ONLY; |
Matus Fabian | ea5b5be | 2018-09-03 05:02:23 -0700 | [diff] [blame] | 1232 | m->affinity = affinity; |
| 1233 | |
| 1234 | if (affinity) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1235 | m->affinity_per_service_list_head_index = |
| 1236 | nat_affinity_get_per_service_list_head_index (); |
Matus Fabian | ea5b5be | 2018-09-03 05:02:23 -0700 | [diff] [blame] | 1237 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1238 | m->affinity_per_service_list_head_index = ~0; |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1239 | |
| 1240 | m_key.addr = m->external_addr; |
| 1241 | m_key.port = m->external_port; |
| 1242 | m_key.protocol = m->proto; |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 1243 | m_key.fib_index = 0; |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1244 | kv.key = m_key.as_u64; |
| 1245 | kv.value = m - sm->static_mappings; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1246 | if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 1)) |
| 1247 | { |
| 1248 | nat_log_err ("static_mapping_by_external key add failed"); |
| 1249 | return VNET_API_ERROR_UNSPECIFIED; |
| 1250 | } |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1251 | |
Matus Fabian | 092b3cd | 2017-09-19 05:42:38 -0700 | [diff] [blame] | 1252 | m_key.fib_index = m->fib_index; |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1253 | for (i = 0; i < vec_len (locals); i++) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1254 | { |
| 1255 | locals[i].fib_index = |
| 1256 | fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, |
| 1257 | locals[i].vrf_id, |
| 1258 | FIB_SOURCE_PLUGIN_LOW); |
| 1259 | m_key.addr = locals[i].addr; |
| 1260 | m_key.fib_index = locals[i].fib_index; |
| 1261 | if (!out2in_only) |
| 1262 | { |
| 1263 | m_key.port = locals[i].port; |
| 1264 | kv.key = m_key.as_u64; |
| 1265 | kv.value = m - sm->static_mappings; |
| 1266 | clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1); |
| 1267 | } |
| 1268 | locals[i].prefix = (i == 0) ? locals[i].probability : |
| 1269 | (locals[i - 1].prefix + locals[i].probability); |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 1270 | pool_get (m->locals, local); |
| 1271 | *local = locals[i]; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1272 | if (sm->num_workers > 1) |
| 1273 | { |
| 1274 | ip4_header_t ip = { |
| 1275 | .src_address = locals[i].addr, |
| 1276 | }; |
| 1277 | bitmap = |
| 1278 | clib_bitmap_set (bitmap, |
| 1279 | sm->worker_in2out_cb (&ip, m->fib_index), 1); |
| 1280 | } |
| 1281 | } |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1282 | |
| 1283 | /* Assign workers */ |
| 1284 | if (sm->num_workers > 1) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1285 | { |
| 1286 | /* *INDENT-OFF* */ |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1287 | clib_bitmap_foreach (i, bitmap, |
| 1288 | ({ |
| 1289 | vec_add1(m->workers, i); |
| 1290 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1291 | /* *INDENT-ON* */ |
| 1292 | } |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1293 | } |
| 1294 | else |
| 1295 | { |
| 1296 | if (!m) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1297 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1298 | |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 1299 | if (!is_lb_static_mapping (m)) |
| 1300 | return VNET_API_ERROR_INVALID_VALUE; |
| 1301 | |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1302 | /* Free external address port */ |
Matus Fabian | 240b5ef | 2018-01-11 04:09:17 -0800 | [diff] [blame] | 1303 | if (!(sm->static_mapping_only || out2in_only)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1304 | { |
| 1305 | for (i = 0; i < vec_len (sm->addresses); i++) |
| 1306 | { |
| 1307 | if (sm->addresses[i].addr.as_u32 == e_addr.as_u32) |
| 1308 | { |
| 1309 | a = sm->addresses + i; |
| 1310 | switch (proto) |
| 1311 | { |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1312 | #define _(N, j, n, s) \ |
| 1313 | case SNAT_PROTOCOL_##N: \ |
| 1314 | clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, e_port, 0); \ |
| 1315 | if (e_port > 1024) \ |
Matus Fabian | 624b8d9 | 2017-09-12 04:15:30 -0700 | [diff] [blame] | 1316 | { \ |
| 1317 | a->busy_##n##_ports--; \ |
dongjuan | 58f50f1 | 2018-09-04 17:40:53 +0800 | [diff] [blame] | 1318 | a->busy_##n##_ports_per_thread[get_thread_idx_by_port(e_port)]--; \ |
Matus Fabian | 624b8d9 | 2017-09-12 04:15:30 -0700 | [diff] [blame] | 1319 | } \ |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1320 | break; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1321 | foreach_snat_protocol |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1322 | #undef _ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1323 | default: |
| 1324 | nat_log_info ("unknown protocol"); |
| 1325 | return VNET_API_ERROR_INVALID_VALUE_2; |
| 1326 | } |
| 1327 | break; |
| 1328 | } |
| 1329 | } |
| 1330 | } |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1331 | |
| 1332 | m_key.addr = m->external_addr; |
| 1333 | m_key.port = m->external_port; |
| 1334 | m_key.protocol = m->proto; |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 1335 | m_key.fib_index = 0; |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1336 | kv.key = m_key.as_u64; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1337 | if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_external, &kv, 0)) |
| 1338 | { |
| 1339 | nat_log_err ("static_mapping_by_external key del failed"); |
| 1340 | return VNET_API_ERROR_UNSPECIFIED; |
| 1341 | } |
Matus Fabian | 7865b5c | 2017-09-26 01:23:01 -0700 | [diff] [blame] | 1342 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1343 | /* *INDENT-OFF* */ |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 1344 | pool_foreach (local, m->locals, |
| 1345 | ({ |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 1346 | fib_table_unlock (local->fib_index, FIB_PROTOCOL_IP4, |
| 1347 | FIB_SOURCE_PLUGIN_LOW); |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1348 | m_key.addr = local->addr; |
Matus Fabian | 240b5ef | 2018-01-11 04:09:17 -0800 | [diff] [blame] | 1349 | if (!out2in_only) |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1350 | { |
Matus Fabian | 240b5ef | 2018-01-11 04:09:17 -0800 | [diff] [blame] | 1351 | m_key.port = local->port; |
Matus Fabian | c6c0d2a | 2018-07-19 22:45:25 -0700 | [diff] [blame] | 1352 | m_key.fib_index = local->fib_index; |
Matus Fabian | 240b5ef | 2018-01-11 04:09:17 -0800 | [diff] [blame] | 1353 | kv.key = m_key.as_u64; |
| 1354 | if (clib_bihash_add_del_8_8(&sm->static_mapping_by_local, &kv, 0)) |
| 1355 | { |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 1356 | nat_log_err ("static_mapping_by_local key del failed"); |
Matus Fabian | 240b5ef | 2018-01-11 04:09:17 -0800 | [diff] [blame] | 1357 | return VNET_API_ERROR_UNSPECIFIED; |
| 1358 | } |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1359 | } |
Matus Fabian | 7865b5c | 2017-09-26 01:23:01 -0700 | [diff] [blame] | 1360 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1361 | if (sm->num_workers > 1) |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1362 | { |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1363 | ip4_header_t ip = { |
| 1364 | .src_address = local->addr, |
| 1365 | }; |
| 1366 | tsm = vec_elt_at_index (sm->per_thread_data, |
| 1367 | sm->worker_in2out_cb (&ip, m->fib_index)); |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1368 | } |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1369 | else |
| 1370 | tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers); |
| 1371 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1372 | /* Delete sessions */ |
| 1373 | u_key.addr = local->addr; |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 1374 | u_key.fib_index = local->fib_index; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1375 | kv.key = u_key.as_u64; |
| 1376 | if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)) |
| 1377 | { |
| 1378 | u = pool_elt_at_index (tsm->users, value.value); |
| 1379 | if (u->nstaticsessions) |
| 1380 | { |
| 1381 | head_index = u->sessions_per_user_list_head_index; |
| 1382 | head = pool_elt_at_index (tsm->list_pool, head_index); |
| 1383 | elt_index = head->next; |
| 1384 | elt = pool_elt_at_index (tsm->list_pool, elt_index); |
| 1385 | ses_index = elt->value; |
| 1386 | while (ses_index != ~0) |
| 1387 | { |
| 1388 | s = pool_elt_at_index (tsm->sessions, ses_index); |
| 1389 | elt = pool_elt_at_index (tsm->list_pool, elt->next); |
| 1390 | ses_index = elt->value; |
| 1391 | |
Matus Fabian | 13c0818 | 2018-04-11 00:36:57 -0700 | [diff] [blame] | 1392 | if (!(is_lb_session (s))) |
| 1393 | continue; |
| 1394 | |
shubing guo | 060c3a7 | 2018-08-10 13:59:50 +0800 | [diff] [blame] | 1395 | if ((s->in2out.addr.as_u32 != local->addr.as_u32) || |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1396 | (clib_net_to_host_u16 (s->in2out.port) != local->port)) |
| 1397 | continue; |
| 1398 | |
| 1399 | nat_free_session_data (sm, s, tsm - sm->per_thread_data); |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 1400 | nat44_delete_session (sm, s, tsm - sm->per_thread_data); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1401 | } |
| 1402 | } |
| 1403 | } |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 1404 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1405 | /* *INDENT-ON* */ |
Matus Fabian | ea5b5be | 2018-09-03 05:02:23 -0700 | [diff] [blame] | 1406 | if (m->affinity) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1407 | nat_affinity_flush_service (m->affinity_per_service_list_head_index); |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 1408 | pool_free (m->locals); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1409 | vec_free (m->tag); |
| 1410 | vec_free (m->workers); |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 1411 | |
| 1412 | pool_put (sm->static_mappings, m); |
| 1413 | } |
| 1414 | |
| 1415 | return 0; |
| 1416 | } |
| 1417 | |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1418 | int |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 1419 | nat44_lb_static_mapping_add_del_local (ip4_address_t e_addr, u16 e_port, |
| 1420 | ip4_address_t l_addr, u16 l_port, |
| 1421 | snat_protocol_t proto, u32 vrf_id, |
| 1422 | u8 probability, u8 is_add) |
| 1423 | { |
| 1424 | snat_main_t *sm = &snat_main; |
| 1425 | snat_static_mapping_t *m = 0; |
| 1426 | snat_session_key_t m_key; |
| 1427 | clib_bihash_kv_8_8_t kv, value; |
| 1428 | nat44_lb_addr_port_t *local, *prev_local, *match_local = 0; |
| 1429 | snat_main_per_thread_data_t *tsm; |
| 1430 | snat_user_key_t u_key; |
| 1431 | snat_user_t *u; |
| 1432 | snat_session_t *s; |
| 1433 | dlist_elt_t *head, *elt; |
| 1434 | u32 elt_index, head_index, ses_index, *locals = 0; |
| 1435 | uword *bitmap = 0; |
| 1436 | int i; |
| 1437 | |
| 1438 | if (!sm->endpoint_dependent) |
| 1439 | return VNET_API_ERROR_FEATURE_DISABLED; |
| 1440 | |
| 1441 | m_key.addr = e_addr; |
| 1442 | m_key.port = e_port; |
| 1443 | m_key.protocol = proto; |
| 1444 | m_key.fib_index = 0; |
| 1445 | kv.key = m_key.as_u64; |
| 1446 | if (!clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value)) |
| 1447 | m = pool_elt_at_index (sm->static_mappings, value.value); |
| 1448 | |
| 1449 | if (!m) |
| 1450 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 1451 | |
| 1452 | if (!is_lb_static_mapping (m)) |
| 1453 | return VNET_API_ERROR_INVALID_VALUE; |
| 1454 | |
| 1455 | /* *INDENT-OFF* */ |
| 1456 | pool_foreach (local, m->locals, |
| 1457 | ({ |
| 1458 | if ((local->addr.as_u32 == l_addr.as_u32) && (local->port == l_port) && |
| 1459 | (local->vrf_id == vrf_id)) |
| 1460 | { |
| 1461 | match_local = local; |
| 1462 | break; |
| 1463 | } |
| 1464 | })); |
| 1465 | /* *INDENT-ON* */ |
| 1466 | |
| 1467 | if (is_add) |
| 1468 | { |
| 1469 | if (match_local) |
| 1470 | return VNET_API_ERROR_VALUE_EXIST; |
| 1471 | |
| 1472 | pool_get (m->locals, local); |
| 1473 | clib_memset (local, 0, sizeof (*local)); |
| 1474 | local->addr.as_u32 = l_addr.as_u32; |
| 1475 | local->port = l_port; |
| 1476 | local->probability = probability; |
| 1477 | local->vrf_id = vrf_id; |
| 1478 | local->fib_index = |
| 1479 | fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, vrf_id, |
| 1480 | FIB_SOURCE_PLUGIN_LOW); |
| 1481 | |
| 1482 | if (!is_out2in_only_static_mapping (m)) |
| 1483 | { |
| 1484 | m_key.addr = l_addr; |
| 1485 | m_key.port = l_port; |
| 1486 | m_key.fib_index = local->fib_index; |
| 1487 | kv.key = m_key.as_u64; |
| 1488 | kv.value = m - sm->static_mappings; |
| 1489 | if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 1)) |
| 1490 | nat_log_err ("static_mapping_by_local key add failed"); |
| 1491 | } |
| 1492 | } |
| 1493 | else |
| 1494 | { |
| 1495 | if (!match_local) |
| 1496 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 1497 | |
| 1498 | if (pool_elts (m->locals) < 3) |
| 1499 | return VNET_API_ERROR_UNSPECIFIED; |
| 1500 | |
| 1501 | fib_table_unlock (match_local->fib_index, FIB_PROTOCOL_IP4, |
| 1502 | FIB_SOURCE_PLUGIN_LOW); |
| 1503 | |
| 1504 | if (!is_out2in_only_static_mapping (m)) |
| 1505 | { |
| 1506 | m_key.addr = l_addr; |
| 1507 | m_key.port = l_port; |
| 1508 | m_key.fib_index = match_local->fib_index; |
| 1509 | kv.key = m_key.as_u64; |
| 1510 | if (clib_bihash_add_del_8_8 (&sm->static_mapping_by_local, &kv, 0)) |
| 1511 | nat_log_err ("static_mapping_by_local key del failed"); |
| 1512 | } |
| 1513 | |
| 1514 | if (sm->num_workers > 1) |
| 1515 | { |
| 1516 | ip4_header_t ip = { |
| 1517 | .src_address = local->addr, |
| 1518 | }; |
| 1519 | tsm = vec_elt_at_index (sm->per_thread_data, |
| 1520 | sm->worker_in2out_cb (&ip, m->fib_index)); |
| 1521 | } |
| 1522 | else |
| 1523 | tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers); |
| 1524 | |
| 1525 | /* Delete sessions */ |
| 1526 | u_key.addr = match_local->addr; |
| 1527 | u_key.fib_index = match_local->fib_index; |
| 1528 | kv.key = u_key.as_u64; |
| 1529 | if (!clib_bihash_search_8_8 (&tsm->user_hash, &kv, &value)) |
| 1530 | { |
| 1531 | u = pool_elt_at_index (tsm->users, value.value); |
| 1532 | if (u->nstaticsessions) |
| 1533 | { |
| 1534 | head_index = u->sessions_per_user_list_head_index; |
| 1535 | head = pool_elt_at_index (tsm->list_pool, head_index); |
| 1536 | elt_index = head->next; |
| 1537 | elt = pool_elt_at_index (tsm->list_pool, elt_index); |
| 1538 | ses_index = elt->value; |
| 1539 | while (ses_index != ~0) |
| 1540 | { |
| 1541 | s = pool_elt_at_index (tsm->sessions, ses_index); |
| 1542 | elt = pool_elt_at_index (tsm->list_pool, elt->next); |
| 1543 | ses_index = elt->value; |
| 1544 | |
| 1545 | if (!(is_lb_session (s))) |
| 1546 | continue; |
| 1547 | |
| 1548 | if ((s->in2out.addr.as_u32 != match_local->addr.as_u32) || |
| 1549 | (clib_net_to_host_u16 (s->in2out.port) != |
| 1550 | match_local->port)) |
| 1551 | continue; |
| 1552 | |
| 1553 | nat_free_session_data (sm, s, tsm - sm->per_thread_data); |
| 1554 | nat44_delete_session (sm, s, tsm - sm->per_thread_data); |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | pool_put (m->locals, match_local); |
| 1560 | } |
| 1561 | |
| 1562 | vec_free (m->workers); |
| 1563 | |
| 1564 | /* *INDENT-OFF* */ |
| 1565 | pool_foreach (local, m->locals, |
| 1566 | ({ |
| 1567 | vec_add1 (locals, local - m->locals); |
| 1568 | if (sm->num_workers > 1) |
| 1569 | { |
| 1570 | ip4_header_t ip; |
| 1571 | ip.src_address.as_u32 = local->addr.as_u32, |
| 1572 | bitmap = clib_bitmap_set (bitmap, |
| 1573 | sm->worker_in2out_cb (&ip, local->fib_index), |
| 1574 | 1); |
| 1575 | } |
| 1576 | })); |
| 1577 | /* *INDENT-ON* */ |
| 1578 | |
Matus Fabian | d2bad81 | 2018-12-21 04:01:00 -0800 | [diff] [blame] | 1579 | ASSERT (vec_len (locals) > 1); |
| 1580 | |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 1581 | local = pool_elt_at_index (m->locals, locals[0]); |
| 1582 | local->prefix = local->probability; |
| 1583 | for (i = 1; i < vec_len (locals); i++) |
| 1584 | { |
| 1585 | local = pool_elt_at_index (m->locals, locals[i]); |
| 1586 | prev_local = pool_elt_at_index (m->locals, locals[i - 1]); |
| 1587 | local->prefix = local->probability + prev_local->prefix; |
| 1588 | } |
| 1589 | |
| 1590 | /* Assign workers */ |
| 1591 | if (sm->num_workers > 1) |
| 1592 | { |
| 1593 | /* *INDENT-OFF* */ |
| 1594 | clib_bitmap_foreach (i, bitmap, ({ vec_add1(m->workers, i); })); |
| 1595 | /* *INDENT-ON* */ |
| 1596 | } |
| 1597 | |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
| 1601 | int |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1602 | snat_del_address (snat_main_t * sm, ip4_address_t addr, u8 delete_sm, |
| 1603 | u8 twice_nat) |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1604 | { |
| 1605 | snat_address_t *a = 0; |
| 1606 | snat_session_t *ses; |
| 1607 | u32 *ses_to_be_removed = 0, *ses_index; |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1608 | snat_main_per_thread_data_t *tsm; |
| 1609 | snat_static_mapping_t *m; |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1610 | snat_interface_t *interface; |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1611 | int i; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1612 | snat_address_t *addresses = |
| 1613 | twice_nat ? sm->twice_nat_addresses : sm->addresses; |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1614 | |
| 1615 | /* Find SNAT address */ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1616 | for (i = 0; i < vec_len (addresses); i++) |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1617 | { |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1618 | if (addresses[i].addr.as_u32 == addr.as_u32) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1619 | { |
| 1620 | a = addresses + i; |
| 1621 | break; |
| 1622 | } |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1623 | } |
| 1624 | if (!a) |
| 1625 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 1626 | |
| 1627 | if (delete_sm) |
| 1628 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1629 | /* *INDENT-OFF* */ |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1630 | pool_foreach (m, sm->static_mappings, |
| 1631 | ({ |
| 1632 | if (m->external_addr.as_u32 == addr.as_u32) |
| 1633 | (void) snat_add_static_mapping (m->local_addr, m->external_addr, |
| 1634 | m->local_port, m->external_port, |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 1635 | m->vrf_id, is_addr_only_static_mapping(m), ~0, |
Matus Fabian | e82488f | 2018-01-18 03:38:45 -0800 | [diff] [blame] | 1636 | m->proto, 0, m->twice_nat, |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 1637 | is_out2in_only_static_mapping(m), m->tag, is_identity_static_mapping(m)); |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1638 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1639 | /* *INDENT-ON* */ |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1640 | } |
| 1641 | else |
| 1642 | { |
| 1643 | /* Check if address is used in some static mapping */ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1644 | if (is_snat_address_used_in_static_mapping (sm, addr)) |
| 1645 | { |
| 1646 | nat_log_notice ("address used in static mapping"); |
| 1647 | return VNET_API_ERROR_UNSPECIFIED; |
| 1648 | } |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1649 | } |
| 1650 | |
Matus Fabian | f8d8490 | 2017-07-23 23:41:03 -0700 | [diff] [blame] | 1651 | if (a->fib_index != ~0) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1652 | fib_table_unlock (a->fib_index, FIB_PROTOCOL_IP4, FIB_SOURCE_PLUGIN_LOW); |
Matus Fabian | f8d8490 | 2017-07-23 23:41:03 -0700 | [diff] [blame] | 1653 | |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1654 | /* Delete sessions using address */ |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 1655 | if (a->busy_tcp_ports || a->busy_udp_ports || a->busy_icmp_ports) |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1656 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1657 | /* *INDENT-OFF* */ |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1658 | vec_foreach (tsm, sm->per_thread_data) |
| 1659 | { |
| 1660 | pool_foreach (ses, tsm->sessions, ({ |
| 1661 | if (ses->out2in.addr.as_u32 == addr.as_u32) |
| 1662 | { |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1663 | nat_free_session_data (sm, ses, tsm - sm->per_thread_data); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1664 | vec_add1 (ses_to_be_removed, ses - tsm->sessions); |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1665 | } |
| 1666 | })); |
| 1667 | |
| 1668 | vec_foreach (ses_index, ses_to_be_removed) |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 1669 | { |
| 1670 | ses = pool_elt_at_index (tsm->sessions, ses_index[0]); |
| 1671 | nat44_delete_session (sm, ses, tsm - sm->per_thread_data); |
| 1672 | } |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1673 | |
| 1674 | vec_free (ses_to_be_removed); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1675 | } |
| 1676 | /* *INDENT-ON* */ |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1677 | } |
| 1678 | |
dongjuan | df86520 | 2018-09-11 11:20:30 +0000 | [diff] [blame] | 1679 | #define _(N, i, n, s) \ |
| 1680 | clib_bitmap_free (a->busy_##n##_port_bitmap); \ |
| 1681 | vec_free (a->busy_##n##_ports_per_thread); |
| 1682 | foreach_snat_protocol |
| 1683 | #undef _ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1684 | if (twice_nat) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 1685 | { |
| 1686 | vec_del1 (sm->twice_nat_addresses, i); |
| 1687 | return 0; |
| 1688 | } |
| 1689 | else |
| 1690 | vec_del1 (sm->addresses, i); |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1691 | |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1692 | /* Delete external address from FIB */ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1693 | /* *INDENT-OFF* */ |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1694 | pool_foreach (interface, sm->interfaces, |
| 1695 | ({ |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 1696 | if (nat_interface_is_inside(interface) || sm->out2in_dpo) |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1697 | continue; |
| 1698 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 1699 | snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0); |
Matus Fabian | dccbee3 | 2017-01-31 22:20:30 -0800 | [diff] [blame] | 1700 | break; |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1701 | })); |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 1702 | pool_foreach (interface, sm->output_feature_interfaces, |
| 1703 | ({ |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 1704 | if (nat_interface_is_inside(interface) || sm->out2in_dpo) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 1705 | continue; |
| 1706 | |
| 1707 | snat_add_del_addr_to_fib(&addr, 32, interface->sw_if_index, 0); |
| 1708 | break; |
| 1709 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1710 | /* *INDENT-ON* */ |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1711 | |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 1712 | return 0; |
| 1713 | } |
| 1714 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1715 | int |
| 1716 | snat_interface_add_del (u32 sw_if_index, u8 is_inside, int is_del) |
Matus Fabian | 588144a | 2016-10-24 03:30:00 -0700 | [diff] [blame] | 1717 | { |
| 1718 | snat_main_t *sm = &snat_main; |
| 1719 | snat_interface_t *i; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1720 | const char *feature_name, *del_feature_name; |
| 1721 | snat_address_t *ap; |
| 1722 | snat_static_mapping_t *m; |
| 1723 | snat_det_map_t *dm; |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 1724 | nat_outside_fib_t *outside_fib; |
| 1725 | u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1726 | sw_if_index); |
Matus Fabian | 588144a | 2016-10-24 03:30:00 -0700 | [diff] [blame] | 1727 | |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 1728 | if (sm->out2in_dpo && !is_inside) |
| 1729 | return VNET_API_ERROR_UNSUPPORTED; |
| 1730 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1731 | /* *INDENT-OFF* */ |
Matus Fabian | e4e34c2 | 2018-03-07 03:17:57 -0800 | [diff] [blame] | 1732 | pool_foreach (i, sm->output_feature_interfaces, |
| 1733 | ({ |
| 1734 | if (i->sw_if_index == sw_if_index) |
| 1735 | return VNET_API_ERROR_VALUE_EXIST; |
| 1736 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1737 | /* *INDENT-ON* */ |
Matus Fabian | e4e34c2 | 2018-03-07 03:17:57 -0800 | [diff] [blame] | 1738 | |
Matus Fabian | 588144a | 2016-10-24 03:30:00 -0700 | [diff] [blame] | 1739 | if (sm->static_mapping_only && !(sm->static_mapping_connection_tracking)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1740 | feature_name = is_inside ? "nat44-in2out-fast" : "nat44-out2in-fast"; |
Matus Fabian | 588144a | 2016-10-24 03:30:00 -0700 | [diff] [blame] | 1741 | else |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 1742 | { |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 1743 | if (sm->num_workers > 1 && !sm->deterministic) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1744 | feature_name = |
| 1745 | is_inside ? "nat44-in2out-worker-handoff" : |
| 1746 | "nat44-out2in-worker-handoff"; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 1747 | else if (sm->deterministic) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1748 | feature_name = is_inside ? "nat44-det-in2out" : "nat44-det-out2in"; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1749 | else if (sm->endpoint_dependent) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1750 | feature_name = is_inside ? "nat44-ed-in2out" : "nat44-ed-out2in"; |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 1751 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1752 | feature_name = is_inside ? "nat44-in2out" : "nat44-out2in"; |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 1753 | } |
Matus Fabian | 588144a | 2016-10-24 03:30:00 -0700 | [diff] [blame] | 1754 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 1755 | if (sm->fq_in2out_index == ~0 && !sm->deterministic && sm->num_workers > 1) |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 1756 | sm->fq_in2out_index = vlib_frame_queue_main_init (sm->in2out_node_index, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1757 | NAT_FQ_NELTS); |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 1758 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 1759 | if (sm->fq_out2in_index == ~0 && !sm->deterministic && sm->num_workers > 1) |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 1760 | sm->fq_out2in_index = vlib_frame_queue_main_init (sm->out2in_node_index, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1761 | NAT_FQ_NELTS); |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 1762 | |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 1763 | if (!is_inside) |
| 1764 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1765 | /* *INDENT-OFF* */ |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 1766 | vec_foreach (outside_fib, sm->outside_fibs) |
| 1767 | { |
| 1768 | if (outside_fib->fib_index == fib_index) |
| 1769 | { |
| 1770 | if (is_del) |
| 1771 | { |
Dmitry Vakhrushev | ee3fb62 | 2019-01-22 05:28:53 -0500 | [diff] [blame] | 1772 | outside_fib->refcount--; |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 1773 | if (!outside_fib->refcount) |
| 1774 | vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs); |
| 1775 | } |
| 1776 | else |
| 1777 | outside_fib->refcount++; |
| 1778 | goto feature_set; |
| 1779 | } |
| 1780 | } |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1781 | /* *INDENT-ON* */ |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 1782 | if (!is_del) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1783 | { |
| 1784 | vec_add2 (sm->outside_fibs, outside_fib, 1); |
| 1785 | outside_fib->refcount = 1; |
| 1786 | outside_fib->fib_index = fib_index; |
| 1787 | } |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 1788 | } |
| 1789 | feature_set: |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1790 | /* *INDENT-OFF* */ |
Matus Fabian | 588144a | 2016-10-24 03:30:00 -0700 | [diff] [blame] | 1791 | pool_foreach (i, sm->interfaces, |
| 1792 | ({ |
| 1793 | if (i->sw_if_index == sw_if_index) |
| 1794 | { |
| 1795 | if (is_del) |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1796 | { |
| 1797 | if (nat_interface_is_inside(i) && nat_interface_is_outside(i)) |
| 1798 | { |
| 1799 | if (is_inside) |
| 1800 | i->flags &= ~NAT_INTERFACE_FLAG_IS_INSIDE; |
| 1801 | else |
| 1802 | i->flags &= ~NAT_INTERFACE_FLAG_IS_OUTSIDE; |
| 1803 | |
| 1804 | if (sm->num_workers > 1 && !sm->deterministic) |
Milan Lenco | 2aa2290 | 2018-01-22 14:05:14 +0100 | [diff] [blame] | 1805 | { |
| 1806 | del_feature_name = "nat44-handoff-classify"; |
| 1807 | feature_name = !is_inside ? "nat44-in2out-worker-handoff" : |
| 1808 | "nat44-out2in-worker-handoff"; |
| 1809 | } |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1810 | else if (sm->deterministic) |
Milan Lenco | 2aa2290 | 2018-01-22 14:05:14 +0100 | [diff] [blame] | 1811 | { |
| 1812 | del_feature_name = "nat44-det-classify"; |
| 1813 | feature_name = !is_inside ? "nat44-det-in2out" : |
| 1814 | "nat44-det-out2in"; |
| 1815 | } |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1816 | else if (sm->endpoint_dependent) |
| 1817 | { |
| 1818 | del_feature_name = "nat44-ed-classify"; |
| 1819 | feature_name = !is_inside ? "nat44-ed-in2out" : |
| 1820 | "nat44-ed-out2in"; |
| 1821 | } |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1822 | else |
Milan Lenco | 2aa2290 | 2018-01-22 14:05:14 +0100 | [diff] [blame] | 1823 | { |
| 1824 | del_feature_name = "nat44-classify"; |
| 1825 | feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in"; |
| 1826 | } |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1827 | |
| 1828 | vnet_feature_enable_disable ("ip4-unicast", del_feature_name, |
| 1829 | sw_if_index, 0, 0, 0); |
| 1830 | vnet_feature_enable_disable ("ip4-unicast", feature_name, |
| 1831 | sw_if_index, 1, 0, 0); |
Matus Fabian | 35dfedc | 2018-04-26 02:12:20 -0700 | [diff] [blame] | 1832 | if (!is_inside) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1833 | { |
| 1834 | if (sm->endpoint_dependent) |
| 1835 | vnet_feature_enable_disable ("ip4-local", |
| 1836 | "nat44-ed-hairpinning", |
| 1837 | sw_if_index, 1, 0, 0); |
Matus Fabian | 38bc308 | 2018-08-09 05:15:19 -0700 | [diff] [blame] | 1838 | else if (!sm->deterministic) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1839 | vnet_feature_enable_disable ("ip4-local", |
| 1840 | "nat44-hairpinning", |
| 1841 | sw_if_index, 1, 0, 0); |
| 1842 | } |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1843 | } |
| 1844 | else |
| 1845 | { |
| 1846 | vnet_feature_enable_disable ("ip4-unicast", feature_name, |
| 1847 | sw_if_index, 0, 0, 0); |
| 1848 | pool_put (sm->interfaces, i); |
Matus Fabian | 35dfedc | 2018-04-26 02:12:20 -0700 | [diff] [blame] | 1849 | if (is_inside) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1850 | { |
| 1851 | if (sm->endpoint_dependent) |
| 1852 | vnet_feature_enable_disable ("ip4-local", |
| 1853 | "nat44-ed-hairpinning", |
| 1854 | sw_if_index, 0, 0, 0); |
Matus Fabian | 38bc308 | 2018-08-09 05:15:19 -0700 | [diff] [blame] | 1855 | else if (!sm->deterministic) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1856 | vnet_feature_enable_disable ("ip4-local", |
| 1857 | "nat44-hairpinning", |
| 1858 | sw_if_index, 0, 0, 0); |
| 1859 | } |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1860 | } |
| 1861 | } |
Matus Fabian | 588144a | 2016-10-24 03:30:00 -0700 | [diff] [blame] | 1862 | else |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1863 | { |
| 1864 | if ((nat_interface_is_inside(i) && is_inside) || |
| 1865 | (nat_interface_is_outside(i) && !is_inside)) |
| 1866 | return 0; |
| 1867 | |
| 1868 | if (sm->num_workers > 1 && !sm->deterministic) |
| 1869 | { |
| 1870 | del_feature_name = !is_inside ? "nat44-in2out-worker-handoff" : |
| 1871 | "nat44-out2in-worker-handoff"; |
| 1872 | feature_name = "nat44-handoff-classify"; |
| 1873 | } |
| 1874 | else if (sm->deterministic) |
| 1875 | { |
| 1876 | del_feature_name = !is_inside ? "nat44-det-in2out" : |
| 1877 | "nat44-det-out2in"; |
| 1878 | feature_name = "nat44-det-classify"; |
| 1879 | } |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1880 | else if (sm->endpoint_dependent) |
| 1881 | { |
| 1882 | del_feature_name = !is_inside ? "nat44-ed-in2out" : |
| 1883 | "nat44-ed-out2in"; |
| 1884 | feature_name = "nat44-ed-classify"; |
| 1885 | } |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1886 | else |
| 1887 | { |
| 1888 | del_feature_name = !is_inside ? "nat44-in2out" : "nat44-out2in"; |
| 1889 | feature_name = "nat44-classify"; |
| 1890 | } |
| 1891 | |
| 1892 | vnet_feature_enable_disable ("ip4-unicast", del_feature_name, |
| 1893 | sw_if_index, 0, 0, 0); |
| 1894 | vnet_feature_enable_disable ("ip4-unicast", feature_name, |
| 1895 | sw_if_index, 1, 0, 0); |
Matus Fabian | 35dfedc | 2018-04-26 02:12:20 -0700 | [diff] [blame] | 1896 | if (!is_inside) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1897 | { |
| 1898 | if (sm->endpoint_dependent) |
| 1899 | vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning", |
| 1900 | sw_if_index, 0, 0, 0); |
Matus Fabian | 38bc308 | 2018-08-09 05:15:19 -0700 | [diff] [blame] | 1901 | else if (!sm->deterministic) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1902 | vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning", |
| 1903 | sw_if_index, 0, 0, 0); |
| 1904 | } |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1905 | goto set_flags; |
| 1906 | } |
Matus Fabian | 588144a | 2016-10-24 03:30:00 -0700 | [diff] [blame] | 1907 | |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1908 | goto fib; |
Matus Fabian | 588144a | 2016-10-24 03:30:00 -0700 | [diff] [blame] | 1909 | } |
| 1910 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1911 | /* *INDENT-ON* */ |
Matus Fabian | 588144a | 2016-10-24 03:30:00 -0700 | [diff] [blame] | 1912 | |
| 1913 | if (is_del) |
| 1914 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 1915 | |
| 1916 | pool_get (sm->interfaces, i); |
| 1917 | i->sw_if_index = sw_if_index; |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1918 | i->flags = 0; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1919 | vnet_feature_enable_disable ("ip4-unicast", feature_name, sw_if_index, 1, 0, |
| 1920 | 0); |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1921 | |
Matus Fabian | 35dfedc | 2018-04-26 02:12:20 -0700 | [diff] [blame] | 1922 | if (is_inside && !sm->out2in_dpo) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1923 | { |
| 1924 | if (sm->endpoint_dependent) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1925 | vnet_feature_enable_disable ("ip4-local", "nat44-ed-hairpinning", |
| 1926 | sw_if_index, 1, 0, 0); |
Matus Fabian | 38bc308 | 2018-08-09 05:15:19 -0700 | [diff] [blame] | 1927 | else if (!sm->deterministic) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1928 | vnet_feature_enable_disable ("ip4-local", "nat44-hairpinning", |
| 1929 | sw_if_index, 1, 0, 0); |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 1930 | } |
Matus Fabian | 35dfedc | 2018-04-26 02:12:20 -0700 | [diff] [blame] | 1931 | |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1932 | set_flags: |
| 1933 | if (is_inside) |
Matus Fabian | 35dfedc | 2018-04-26 02:12:20 -0700 | [diff] [blame] | 1934 | { |
| 1935 | i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE; |
| 1936 | return 0; |
| 1937 | } |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 1938 | else |
| 1939 | i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE; |
Matus Fabian | 588144a | 2016-10-24 03:30:00 -0700 | [diff] [blame] | 1940 | |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1941 | /* Add/delete external addresses to FIB */ |
| 1942 | fib: |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1943 | /* *INDENT-OFF* */ |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1944 | vec_foreach (ap, sm->addresses) |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 1945 | snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del); |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1946 | |
| 1947 | pool_foreach (m, sm->static_mappings, |
| 1948 | ({ |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 1949 | if (!(is_addr_only_static_mapping(m)) || (m->local_addr.as_u32 == m->external_addr.as_u32)) |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1950 | continue; |
| 1951 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 1952 | snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del); |
| 1953 | })); |
| 1954 | |
| 1955 | pool_foreach (dm, sm->det_maps, |
| 1956 | ({ |
| 1957 | snat_add_del_addr_to_fib(&dm->out_addr, dm->out_plen, sw_if_index, !is_del); |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1958 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1959 | /* *INDENT-ON* */ |
Matus Fabian | e1ae29a | 2017-01-27 00:47:58 -0800 | [diff] [blame] | 1960 | |
Matus Fabian | 588144a | 2016-10-24 03:30:00 -0700 | [diff] [blame] | 1961 | return 0; |
| 1962 | } |
| 1963 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1964 | int |
| 1965 | snat_interface_add_del_output_feature (u32 sw_if_index, |
| 1966 | u8 is_inside, int is_del) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 1967 | { |
| 1968 | snat_main_t *sm = &snat_main; |
| 1969 | snat_interface_t *i; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1970 | snat_address_t *ap; |
| 1971 | snat_static_mapping_t *m; |
Dmitry Vakhrushev | f564139 | 2019-01-16 09:44:03 -0500 | [diff] [blame] | 1972 | nat_outside_fib_t *outside_fib; |
| 1973 | u32 fib_index = fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, |
| 1974 | sw_if_index); |
| 1975 | |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 1976 | |
| 1977 | if (sm->deterministic || |
| 1978 | (sm->static_mapping_only && !(sm->static_mapping_connection_tracking))) |
| 1979 | return VNET_API_ERROR_UNSUPPORTED; |
| 1980 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1981 | /* *INDENT-OFF* */ |
Matus Fabian | e4e34c2 | 2018-03-07 03:17:57 -0800 | [diff] [blame] | 1982 | pool_foreach (i, sm->interfaces, |
| 1983 | ({ |
| 1984 | if (i->sw_if_index == sw_if_index) |
| 1985 | return VNET_API_ERROR_VALUE_EXIST; |
| 1986 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 1987 | /* *INDENT-ON* */ |
Matus Fabian | e4e34c2 | 2018-03-07 03:17:57 -0800 | [diff] [blame] | 1988 | |
Dmitry Vakhrushev | f564139 | 2019-01-16 09:44:03 -0500 | [diff] [blame] | 1989 | if (!is_inside) |
| 1990 | { |
| 1991 | /* *INDENT-OFF* */ |
| 1992 | vec_foreach (outside_fib, sm->outside_fibs) |
| 1993 | { |
| 1994 | if (outside_fib->fib_index == fib_index) |
| 1995 | { |
| 1996 | if (is_del) |
| 1997 | { |
Dmitry Vakhrushev | ee3fb62 | 2019-01-22 05:28:53 -0500 | [diff] [blame] | 1998 | outside_fib->refcount--; |
Dmitry Vakhrushev | f564139 | 2019-01-16 09:44:03 -0500 | [diff] [blame] | 1999 | if (!outside_fib->refcount) |
| 2000 | vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs); |
| 2001 | } |
| 2002 | else |
| 2003 | outside_fib->refcount++; |
| 2004 | goto feature_set; |
| 2005 | } |
| 2006 | } |
| 2007 | /* *INDENT-ON* */ |
| 2008 | if (!is_del) |
| 2009 | { |
| 2010 | vec_add2 (sm->outside_fibs, outside_fib, 1); |
| 2011 | outside_fib->refcount = 1; |
| 2012 | outside_fib->fib_index = fib_index; |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | feature_set: |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 2017 | if (is_inside) |
Matus Fabian | 161c59c | 2017-07-21 03:46:03 -0700 | [diff] [blame] | 2018 | { |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 2019 | if (sm->endpoint_dependent) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2020 | { |
| 2021 | vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-hairpin-dst", |
| 2022 | sw_if_index, !is_del, 0, 0); |
| 2023 | vnet_feature_enable_disable ("ip4-output", "nat44-ed-hairpin-src", |
| 2024 | sw_if_index, !is_del, 0, 0); |
| 2025 | } |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 2026 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2027 | { |
| 2028 | vnet_feature_enable_disable ("ip4-unicast", "nat44-hairpin-dst", |
| 2029 | sw_if_index, !is_del, 0, 0); |
| 2030 | vnet_feature_enable_disable ("ip4-output", "nat44-hairpin-src", |
| 2031 | sw_if_index, !is_del, 0, 0); |
| 2032 | } |
Matus Fabian | 161c59c | 2017-07-21 03:46:03 -0700 | [diff] [blame] | 2033 | goto fq; |
| 2034 | } |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 2035 | |
| 2036 | if (sm->num_workers > 1) |
| 2037 | { |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 2038 | vnet_feature_enable_disable ("ip4-unicast", |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2039 | "nat44-out2in-worker-handoff", |
| 2040 | sw_if_index, !is_del, 0, 0); |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 2041 | vnet_feature_enable_disable ("ip4-output", |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2042 | "nat44-in2out-output-worker-handoff", |
| 2043 | sw_if_index, !is_del, 0, 0); |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 2044 | } |
| 2045 | else |
| 2046 | { |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 2047 | if (sm->endpoint_dependent) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2048 | { |
| 2049 | vnet_feature_enable_disable ("ip4-unicast", "nat44-ed-out2in", |
| 2050 | sw_if_index, !is_del, 0, 0); |
| 2051 | vnet_feature_enable_disable ("ip4-output", "nat44-ed-in2out-output", |
| 2052 | sw_if_index, !is_del, 0, 0); |
| 2053 | } |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 2054 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2055 | { |
| 2056 | vnet_feature_enable_disable ("ip4-unicast", "nat44-out2in", |
| 2057 | sw_if_index, !is_del, 0, 0); |
| 2058 | vnet_feature_enable_disable ("ip4-output", "nat44-in2out-output", |
| 2059 | sw_if_index, !is_del, 0, 0); |
| 2060 | } |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 2061 | } |
| 2062 | |
Matus Fabian | 161c59c | 2017-07-21 03:46:03 -0700 | [diff] [blame] | 2063 | fq: |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 2064 | if (sm->fq_in2out_output_index == ~0 && sm->num_workers > 1) |
| 2065 | sm->fq_in2out_output_index = |
| 2066 | vlib_frame_queue_main_init (sm->in2out_output_node_index, 0); |
| 2067 | |
| 2068 | if (sm->fq_out2in_index == ~0 && sm->num_workers > 1) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2069 | sm->fq_out2in_index = |
| 2070 | vlib_frame_queue_main_init (sm->out2in_node_index, 0); |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 2071 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2072 | /* *INDENT-OFF* */ |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 2073 | pool_foreach (i, sm->output_feature_interfaces, |
| 2074 | ({ |
| 2075 | if (i->sw_if_index == sw_if_index) |
| 2076 | { |
| 2077 | if (is_del) |
| 2078 | pool_put (sm->output_feature_interfaces, i); |
| 2079 | else |
| 2080 | return VNET_API_ERROR_VALUE_EXIST; |
| 2081 | |
| 2082 | goto fib; |
| 2083 | } |
| 2084 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2085 | /* *INDENT-ON* */ |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 2086 | |
| 2087 | if (is_del) |
| 2088 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 2089 | |
| 2090 | pool_get (sm->output_feature_interfaces, i); |
| 2091 | i->sw_if_index = sw_if_index; |
Matus Fabian | 36ea2d6 | 2017-10-24 04:13:49 -0700 | [diff] [blame] | 2092 | i->flags = 0; |
| 2093 | if (is_inside) |
| 2094 | i->flags |= NAT_INTERFACE_FLAG_IS_INSIDE; |
| 2095 | else |
| 2096 | i->flags |= NAT_INTERFACE_FLAG_IS_OUTSIDE; |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 2097 | |
| 2098 | /* Add/delete external addresses to FIB */ |
| 2099 | fib: |
| 2100 | if (is_inside) |
| 2101 | return 0; |
| 2102 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2103 | /* *INDENT-OFF* */ |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 2104 | vec_foreach (ap, sm->addresses) |
| 2105 | snat_add_del_addr_to_fib(&ap->addr, 32, sw_if_index, !is_del); |
| 2106 | |
| 2107 | pool_foreach (m, sm->static_mappings, |
| 2108 | ({ |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 2109 | if (!((is_addr_only_static_mapping(m))) || (m->local_addr.as_u32 == m->external_addr.as_u32)) |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 2110 | continue; |
| 2111 | |
| 2112 | snat_add_del_addr_to_fib(&m->external_addr, 32, sw_if_index, !is_del); |
| 2113 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2114 | /* *INDENT-ON* */ |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 2115 | |
| 2116 | return 0; |
| 2117 | } |
| 2118 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2119 | int |
| 2120 | snat_set_workers (uword * bitmap) |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2121 | { |
| 2122 | snat_main_t *sm = &snat_main; |
Matus Fabian | 7801ca2 | 2017-08-03 00:58:05 -0700 | [diff] [blame] | 2123 | int i, j = 0; |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2124 | |
| 2125 | if (sm->num_workers < 2) |
| 2126 | return VNET_API_ERROR_FEATURE_DISABLED; |
| 2127 | |
| 2128 | if (clib_bitmap_last_set (bitmap) >= sm->num_workers) |
| 2129 | return VNET_API_ERROR_INVALID_WORKER; |
| 2130 | |
| 2131 | vec_free (sm->workers); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2132 | /* *INDENT-OFF* */ |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2133 | clib_bitmap_foreach (i, bitmap, |
| 2134 | ({ |
| 2135 | vec_add1(sm->workers, i); |
Matus Fabian | 1049139 | 2018-01-05 05:03:35 -0800 | [diff] [blame] | 2136 | sm->per_thread_data[sm->first_worker_index + i].snat_thread_index = j; |
Matus Fabian | 7801ca2 | 2017-08-03 00:58:05 -0700 | [diff] [blame] | 2137 | j++; |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2138 | })); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2139 | /* *INDENT-ON* */ |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2140 | |
Matus Fabian | 7801ca2 | 2017-08-03 00:58:05 -0700 | [diff] [blame] | 2141 | sm->port_per_thread = (0xffff - 1024) / _vec_len (sm->workers); |
| 2142 | sm->num_snat_thread = _vec_len (sm->workers); |
| 2143 | |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2144 | return 0; |
| 2145 | } |
| 2146 | |
Dmitry Vakhrushev | ee3fb62 | 2019-01-22 05:28:53 -0500 | [diff] [blame] | 2147 | static void |
| 2148 | snat_update_outside_fib (u32 sw_if_index, u32 new_fib_index, |
| 2149 | u32 old_fib_index) |
| 2150 | { |
| 2151 | snat_main_t *sm = &snat_main; |
| 2152 | nat_outside_fib_t *outside_fib; |
| 2153 | snat_interface_t *i; |
| 2154 | u8 is_add = 1; |
| 2155 | |
| 2156 | if (new_fib_index == old_fib_index) |
| 2157 | return; |
| 2158 | |
| 2159 | if (!vec_len (sm->outside_fibs)) |
| 2160 | return; |
| 2161 | |
| 2162 | pool_foreach (i, sm->interfaces, ( |
| 2163 | { |
| 2164 | if (i->sw_if_index == sw_if_index) |
| 2165 | { |
| 2166 | if (!(nat_interface_is_outside (i))) |
| 2167 | return;} |
| 2168 | } |
| 2169 | )); |
| 2170 | vec_foreach (outside_fib, sm->outside_fibs) |
| 2171 | { |
| 2172 | if (outside_fib->fib_index == old_fib_index) |
| 2173 | { |
| 2174 | outside_fib->refcount--; |
| 2175 | if (!outside_fib->refcount) |
| 2176 | vec_del1 (sm->outside_fibs, outside_fib - sm->outside_fibs); |
| 2177 | break; |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | vec_foreach (outside_fib, sm->outside_fibs) |
| 2182 | { |
| 2183 | if (outside_fib->fib_index == new_fib_index) |
| 2184 | { |
| 2185 | outside_fib->refcount++; |
| 2186 | is_add = 0; |
| 2187 | break; |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | if (is_add) |
| 2192 | { |
| 2193 | vec_add2 (sm->outside_fibs, outside_fib, 1); |
| 2194 | outside_fib->refcount = 1; |
| 2195 | outside_fib->fib_index = new_fib_index; |
| 2196 | } |
| 2197 | } |
| 2198 | |
| 2199 | static void |
| 2200 | snat_ip4_table_bind (ip4_main_t * im, |
| 2201 | uword opaque, |
| 2202 | u32 sw_if_index, u32 new_fib_index, u32 old_fib_index) |
| 2203 | { |
| 2204 | snat_update_outside_fib (sw_if_index, new_fib_index, old_fib_index); |
| 2205 | } |
Dave Barach | 8b27537 | 2017-01-16 10:54:02 -0500 | [diff] [blame] | 2206 | |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 2207 | static void |
| 2208 | snat_ip4_add_del_interface_address_cb (ip4_main_t * im, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2209 | uword opaque, |
| 2210 | u32 sw_if_index, |
| 2211 | ip4_address_t * address, |
| 2212 | u32 address_length, |
| 2213 | u32 if_address_index, u32 is_delete); |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 2214 | |
Matus Fabian | 4772e7a | 2018-04-04 00:38:02 -0700 | [diff] [blame] | 2215 | static void |
| 2216 | nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2217 | uword opaque, |
| 2218 | u32 sw_if_index, |
| 2219 | ip4_address_t * address, |
| 2220 | u32 address_length, |
| 2221 | u32 if_address_index, u32 is_delete); |
Matus Fabian | 4772e7a | 2018-04-04 00:38:02 -0700 | [diff] [blame] | 2222 | |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 2223 | static int |
| 2224 | nat_alloc_addr_and_port_default (snat_address_t * addresses, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2225 | u32 fib_index, |
| 2226 | u32 thread_index, |
| 2227 | snat_session_key_t * k, |
| 2228 | u16 port_per_thread, u32 snat_thread_index); |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 2229 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2230 | static clib_error_t * |
| 2231 | snat_init (vlib_main_t * vm) |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2232 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2233 | snat_main_t *sm = &snat_main; |
| 2234 | clib_error_t *error = 0; |
| 2235 | ip4_main_t *im = &ip4_main; |
| 2236 | ip_lookup_main_t *lm = &im->lookup_main; |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2237 | uword *p; |
| 2238 | vlib_thread_registration_t *tr; |
| 2239 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
| 2240 | uword *bitmap = 0; |
| 2241 | u32 i; |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 2242 | ip4_add_del_interface_address_callback_t cb4; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2243 | vlib_node_t *error_drop_node; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2244 | |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2245 | sm->vlib_main = vm; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2246 | sm->vnet_main = vnet_get_main (); |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2247 | sm->ip4_main = im; |
| 2248 | sm->ip4_lookup_main = lm; |
| 2249 | sm->api_main = &api_main; |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2250 | sm->first_worker_index = 0; |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2251 | sm->num_workers = 0; |
Matus Fabian | 7801ca2 | 2017-08-03 00:58:05 -0700 | [diff] [blame] | 2252 | sm->num_snat_thread = 1; |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2253 | sm->workers = 0; |
Matus Fabian | 7801ca2 | 2017-08-03 00:58:05 -0700 | [diff] [blame] | 2254 | sm->port_per_thread = 0xffff - 1024; |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2255 | sm->fq_in2out_index = ~0; |
| 2256 | sm->fq_out2in_index = ~0; |
Matus Fabian | 6a0946f | 2017-04-12 03:36:13 -0700 | [diff] [blame] | 2257 | sm->udp_timeout = SNAT_UDP_TIMEOUT; |
| 2258 | sm->tcp_established_timeout = SNAT_TCP_ESTABLISHED_TIMEOUT; |
| 2259 | sm->tcp_transitory_timeout = SNAT_TCP_TRANSITORY_TIMEOUT; |
| 2260 | sm->icmp_timeout = SNAT_ICMP_TIMEOUT; |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 2261 | sm->alloc_addr_and_port = nat_alloc_addr_and_port_default; |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 2262 | sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT; |
Juraj Sloboda | 7b92979 | 2017-11-23 13:20:48 +0100 | [diff] [blame] | 2263 | sm->forwarding_enabled = 0; |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 2264 | sm->log_class = vlib_log_register_class ("nat", 0); |
| 2265 | error_drop_node = vlib_get_node_by_name (vm, (u8 *) "error-drop"); |
| 2266 | sm->error_node_index = error_drop_node->index; |
Matus Fabian | bb4e022 | 2018-09-13 02:36:25 -0700 | [diff] [blame] | 2267 | sm->mss_clamping = 0; |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2268 | |
| 2269 | p = hash_get_mem (tm->thread_registrations_by_name, "workers"); |
| 2270 | if (p) |
| 2271 | { |
| 2272 | tr = (vlib_thread_registration_t *) p[0]; |
| 2273 | if (tr) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2274 | { |
| 2275 | sm->num_workers = tr->count; |
| 2276 | sm->first_worker_index = tr->first_index; |
| 2277 | } |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2278 | } |
| 2279 | |
Matus Fabian | 7801ca2 | 2017-08-03 00:58:05 -0700 | [diff] [blame] | 2280 | vec_validate (sm->per_thread_data, tm->n_vlib_mains - 1); |
| 2281 | |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2282 | /* Use all available workers by default */ |
| 2283 | if (sm->num_workers > 1) |
| 2284 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2285 | for (i = 0; i < sm->num_workers; i++) |
| 2286 | bitmap = clib_bitmap_set (bitmap, i, 1); |
| 2287 | snat_set_workers (bitmap); |
Matus Fabian | 475f055 | 2016-10-19 06:17:52 -0700 | [diff] [blame] | 2288 | clib_bitmap_free (bitmap); |
| 2289 | } |
Matus Fabian | 7801ca2 | 2017-08-03 00:58:05 -0700 | [diff] [blame] | 2290 | else |
| 2291 | { |
| 2292 | sm->per_thread_data[0].snat_thread_index = 0; |
| 2293 | } |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2294 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2295 | error = snat_api_init (vm, sm); |
Matus Fabian | 08ce432 | 2017-06-19 05:28:27 -0700 | [diff] [blame] | 2296 | if (error) |
| 2297 | return error; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2298 | |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 2299 | /* Set up the interface address add/del callback */ |
| 2300 | cb4.function = snat_ip4_add_del_interface_address_cb; |
| 2301 | cb4.function_opaque = 0; |
| 2302 | |
| 2303 | vec_add1 (im->add_del_interface_address_callbacks, cb4); |
| 2304 | |
Matus Fabian | 4772e7a | 2018-04-04 00:38:02 -0700 | [diff] [blame] | 2305 | cb4.function = nat_ip4_add_del_addr_only_sm_cb; |
| 2306 | cb4.function_opaque = 0; |
| 2307 | |
| 2308 | vec_add1 (im->add_del_interface_address_callbacks, cb4); |
| 2309 | |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 2310 | nat_dpo_module_init (); |
| 2311 | |
Matus Fabian | fd0d508 | 2018-12-18 01:08:51 -0800 | [diff] [blame] | 2312 | /* Init counters */ |
| 2313 | sm->total_users.name = "total-users"; |
| 2314 | sm->total_users.stat_segment_name = "/nat44/total-users"; |
| 2315 | vlib_validate_simple_counter (&sm->total_users, 0); |
| 2316 | vlib_zero_simple_counter (&sm->total_users, 0); |
| 2317 | sm->total_sessions.name = "total-sessions"; |
| 2318 | sm->total_sessions.stat_segment_name = "/nat44/total-sessions"; |
| 2319 | vlib_validate_simple_counter (&sm->total_sessions, 0); |
| 2320 | vlib_zero_simple_counter (&sm->total_sessions, 0); |
| 2321 | |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 2322 | /* Init IPFIX logging */ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2323 | snat_ipfix_logging_init (vm); |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 2324 | |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 2325 | /* Init NAT64 */ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2326 | error = nat64_init (vm); |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 2327 | if (error) |
| 2328 | return error; |
Matus Fabian | 06596c5 | 2017-06-06 04:53:28 -0700 | [diff] [blame] | 2329 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2330 | dslite_init (vm); |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 2331 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2332 | nat66_init (); |
Matus Fabian | f2a23cc | 2018-01-22 03:41:53 -0800 | [diff] [blame] | 2333 | |
Dmitry Vakhrushev | ee3fb62 | 2019-01-22 05:28:53 -0500 | [diff] [blame] | 2334 | ip4_table_bind_callback_t cbt4 = { |
| 2335 | .function = snat_ip4_table_bind, |
| 2336 | }; |
| 2337 | vec_add1 (ip4_main.table_bind_callbacks, cbt4); |
| 2338 | |
Matus Fabian | efcd1e9 | 2017-08-15 06:59:19 -0700 | [diff] [blame] | 2339 | /* Init virtual fragmenentation reassembly */ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2340 | return nat_reass_init (vm); |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | VLIB_INIT_FUNCTION (snat_init); |
| 2344 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2345 | void |
| 2346 | snat_free_outside_address_and_port (snat_address_t * addresses, |
| 2347 | u32 thread_index, snat_session_key_t * k) |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2348 | { |
| 2349 | snat_address_t *a; |
shubing guo | 762a493 | 2018-08-13 17:16:46 +0800 | [diff] [blame] | 2350 | u32 address_index; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2351 | u16 port_host_byte_order = clib_net_to_host_u16 (k->port); |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2352 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2353 | for (address_index = 0; address_index < vec_len (addresses); |
| 2354 | address_index++) |
shubing guo | 762a493 | 2018-08-13 17:16:46 +0800 | [diff] [blame] | 2355 | { |
| 2356 | if (addresses[address_index].addr.as_u32 == k->addr.as_u32) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2357 | break; |
shubing guo | 762a493 | 2018-08-13 17:16:46 +0800 | [diff] [blame] | 2358 | } |
| 2359 | |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 2360 | ASSERT (address_index < vec_len (addresses)); |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2361 | |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 2362 | a = addresses + address_index; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2363 | |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 2364 | switch (k->protocol) |
| 2365 | { |
| 2366 | #define _(N, i, n, s) \ |
| 2367 | case SNAT_PROTOCOL_##N: \ |
| 2368 | ASSERT (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, \ |
| 2369 | port_host_byte_order) == 1); \ |
| 2370 | clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, \ |
| 2371 | port_host_byte_order, 0); \ |
| 2372 | a->busy_##n##_ports--; \ |
Matus Fabian | 624b8d9 | 2017-09-12 04:15:30 -0700 | [diff] [blame] | 2373 | a->busy_##n##_ports_per_thread[thread_index]--; \ |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 2374 | break; |
| 2375 | foreach_snat_protocol |
| 2376 | #undef _ |
| 2377 | default: |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 2378 | nat_log_info ("unknown protocol"); |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 2379 | return; |
| 2380 | } |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 2381 | } |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2382 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2383 | int |
| 2384 | snat_static_mapping_match (snat_main_t * sm, |
| 2385 | snat_session_key_t match, |
| 2386 | snat_session_key_t * mapping, |
| 2387 | u8 by_external, |
| 2388 | u8 * is_addr_only, |
| 2389 | twice_nat_type_t * twice_nat, |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 2390 | lb_nat_type_t * lb, ip4_address_t * ext_host_addr, |
| 2391 | u8 * is_identity_nat) |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 2392 | { |
| 2393 | clib_bihash_kv_8_8_t kv, value; |
| 2394 | snat_static_mapping_t *m; |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 2395 | snat_session_key_t m_key; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 2396 | clib_bihash_8_8_t *mapping_hash = &sm->static_mapping_by_local; |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 2397 | u32 rand, lo = 0, hi, mid, *tmp = 0, i; |
Matus Fabian | ea5b5be | 2018-09-03 05:02:23 -0700 | [diff] [blame] | 2398 | u8 backend_index; |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 2399 | nat44_lb_addr_port_t *local; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 2400 | |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 2401 | m_key.fib_index = match.fib_index; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 2402 | if (by_external) |
Matus Fabian | 8008d7c | 2018-07-09 01:34:20 -0700 | [diff] [blame] | 2403 | { |
| 2404 | mapping_hash = &sm->static_mapping_by_external; |
| 2405 | m_key.fib_index = 0; |
| 2406 | } |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 2407 | |
| 2408 | m_key.addr = match.addr; |
| 2409 | m_key.port = clib_net_to_host_u16 (match.port); |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 2410 | m_key.protocol = match.protocol; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 2411 | |
| 2412 | kv.key = m_key.as_u64; |
| 2413 | |
| 2414 | if (clib_bihash_search_8_8 (mapping_hash, &kv, &value)) |
| 2415 | { |
| 2416 | /* Try address only mapping */ |
| 2417 | m_key.port = 0; |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 2418 | m_key.protocol = 0; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 2419 | kv.key = m_key.as_u64; |
| 2420 | if (clib_bihash_search_8_8 (mapping_hash, &kv, &value)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2421 | return 1; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | m = pool_elt_at_index (sm->static_mappings, value.value); |
| 2425 | |
| 2426 | if (by_external) |
| 2427 | { |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 2428 | if (is_lb_static_mapping (m)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2429 | { |
| 2430 | if (PREDICT_FALSE (lb != 0)) |
| 2431 | *lb = m->affinity ? AFFINITY_LB_NAT : LB_NAT; |
| 2432 | if (m->affinity) |
| 2433 | { |
| 2434 | if (nat_affinity_find_and_lock (ext_host_addr[0], match.addr, |
| 2435 | match.protocol, match.port, |
| 2436 | &backend_index)) |
| 2437 | goto get_local; |
Matus Fabian | ea5b5be | 2018-09-03 05:02:23 -0700 | [diff] [blame] | 2438 | |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 2439 | local = pool_elt_at_index (m->locals, backend_index); |
| 2440 | mapping->addr = local->addr; |
| 2441 | mapping->port = clib_host_to_net_u16 (local->port); |
| 2442 | mapping->fib_index = local->fib_index; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2443 | goto end; |
| 2444 | } |
| 2445 | get_local: |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 2446 | /* *INDENT-OFF* */ |
| 2447 | pool_foreach_index (i, m->locals, |
| 2448 | ({ |
| 2449 | vec_add1 (tmp, i); |
| 2450 | })); |
| 2451 | /* *INDENT-ON* */ |
| 2452 | hi = vec_len (tmp) - 1; |
| 2453 | local = pool_elt_at_index (m->locals, tmp[hi]); |
| 2454 | rand = 1 + (random_u32 (&sm->random_seed) % local->prefix); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2455 | while (lo < hi) |
| 2456 | { |
| 2457 | mid = ((hi - lo) >> 1) + lo; |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 2458 | local = pool_elt_at_index (m->locals, tmp[mid]); |
| 2459 | (rand > local->prefix) ? (lo = mid + 1) : (hi = mid); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2460 | } |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 2461 | local = pool_elt_at_index (m->locals, tmp[lo]); |
| 2462 | if (!(local->prefix >= rand)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2463 | return 1; |
| 2464 | if (PREDICT_FALSE (sm->num_workers > 1)) |
| 2465 | { |
| 2466 | ip4_header_t ip = { |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 2467 | .src_address = local->addr, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2468 | }; |
| 2469 | if (sm->worker_in2out_cb (&ip, m->fib_index) != |
| 2470 | vlib_get_thread_index ()) |
| 2471 | goto get_local; |
| 2472 | } |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 2473 | mapping->addr = local->addr; |
| 2474 | mapping->port = clib_host_to_net_u16 (local->port); |
| 2475 | mapping->fib_index = local->fib_index; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2476 | if (m->affinity) |
| 2477 | { |
| 2478 | if (nat_affinity_create_and_lock (ext_host_addr[0], match.addr, |
| 2479 | match.protocol, match.port, |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 2480 | tmp[lo], m->affinity, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2481 | m->affinity_per_service_list_head_index)) |
| 2482 | nat_log_info ("create affinity record failed"); |
| 2483 | } |
Matus Fabian | b686508 | 2018-12-06 03:11:09 -0800 | [diff] [blame] | 2484 | vec_free (tmp); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2485 | } |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 2486 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2487 | { |
| 2488 | if (PREDICT_FALSE (lb != 0)) |
| 2489 | *lb = NO_LB_NAT; |
| 2490 | mapping->fib_index = m->fib_index; |
| 2491 | mapping->addr = m->local_addr; |
| 2492 | /* Address only mapping doesn't change port */ |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 2493 | mapping->port = is_addr_only_static_mapping (m) ? match.port |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2494 | : clib_host_to_net_u16 (m->local_port); |
| 2495 | } |
Matus Fabian | 704018c | 2017-09-04 02:17:18 -0700 | [diff] [blame] | 2496 | mapping->protocol = m->proto; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 2497 | } |
| 2498 | else |
| 2499 | { |
| 2500 | mapping->addr = m->external_addr; |
| 2501 | /* Address only mapping doesn't change port */ |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 2502 | mapping->port = is_addr_only_static_mapping (m) ? match.port |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2503 | : clib_host_to_net_u16 (m->external_port); |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 2504 | mapping->fib_index = sm->outside_fib_index; |
| 2505 | } |
| 2506 | |
Matus Fabian | ea5b5be | 2018-09-03 05:02:23 -0700 | [diff] [blame] | 2507 | end: |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2508 | if (PREDICT_FALSE (is_addr_only != 0)) |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 2509 | *is_addr_only = is_addr_only_static_mapping (m); |
Juraj Sloboda | d367768 | 2017-04-14 03:24:45 +0200 | [diff] [blame] | 2510 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2511 | if (PREDICT_FALSE (twice_nat != 0)) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 2512 | *twice_nat = m->twice_nat; |
| 2513 | |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 2514 | if (PREDICT_FALSE (is_identity_nat != 0)) |
| 2515 | *is_identity_nat = is_identity_static_mapping (m); |
| 2516 | |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 2517 | return 0; |
| 2518 | } |
| 2519 | |
Matus Fabian | 7801ca2 | 2017-08-03 00:58:05 -0700 | [diff] [blame] | 2520 | static_always_inline u16 |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 2521 | snat_random_port (u16 min, u16 max) |
Matus Fabian | 7801ca2 | 2017-08-03 00:58:05 -0700 | [diff] [blame] | 2522 | { |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 2523 | snat_main_t *sm = &snat_main; |
Matus Fabian | 7801ca2 | 2017-08-03 00:58:05 -0700 | [diff] [blame] | 2524 | return min + random_u32 (&sm->random_seed) / |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2525 | (random_u32_max () / (max - min + 1) + 1); |
Matus Fabian | 7801ca2 | 2017-08-03 00:58:05 -0700 | [diff] [blame] | 2526 | } |
| 2527 | |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 2528 | int |
| 2529 | snat_alloc_outside_address_and_port (snat_address_t * addresses, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2530 | u32 fib_index, |
| 2531 | u32 thread_index, |
| 2532 | snat_session_key_t * k, |
| 2533 | u16 port_per_thread, |
| 2534 | u32 snat_thread_index) |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 2535 | { |
| 2536 | snat_main_t *sm = &snat_main; |
| 2537 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2538 | return sm->alloc_addr_and_port (addresses, fib_index, thread_index, k, |
| 2539 | port_per_thread, snat_thread_index); |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | static int |
| 2543 | nat_alloc_addr_and_port_default (snat_address_t * addresses, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2544 | u32 fib_index, |
| 2545 | u32 thread_index, |
| 2546 | snat_session_key_t * k, |
| 2547 | u16 port_per_thread, u32 snat_thread_index) |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2548 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2549 | int i; |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 2550 | snat_address_t *a, *ga = 0; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2551 | u32 portnum; |
| 2552 | |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 2553 | for (i = 0; i < vec_len (addresses); i++) |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2554 | { |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 2555 | a = addresses + i; |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 2556 | switch (k->protocol) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2557 | { |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 2558 | #define _(N, j, n, s) \ |
| 2559 | case SNAT_PROTOCOL_##N: \ |
Matus Fabian | 8ebe625 | 2017-11-06 05:04:53 -0800 | [diff] [blame] | 2560 | if (a->busy_##n##_ports_per_thread[thread_index] < port_per_thread) \ |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 2561 | { \ |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 2562 | if (a->fib_index == fib_index) \ |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 2563 | { \ |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 2564 | while (1) \ |
| 2565 | { \ |
| 2566 | portnum = (port_per_thread * \ |
| 2567 | snat_thread_index) + \ |
| 2568 | snat_random_port(1, port_per_thread) + 1024; \ |
| 2569 | if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \ |
| 2570 | continue; \ |
| 2571 | clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \ |
| 2572 | a->busy_##n##_ports_per_thread[thread_index]++; \ |
| 2573 | a->busy_##n##_ports++; \ |
| 2574 | k->addr = a->addr; \ |
| 2575 | k->port = clib_host_to_net_u16(portnum); \ |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 2576 | return 0; \ |
| 2577 | } \ |
| 2578 | } \ |
| 2579 | else if (a->fib_index == ~0) \ |
| 2580 | { \ |
| 2581 | ga = a; \ |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 2582 | } \ |
| 2583 | } \ |
| 2584 | break; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2585 | foreach_snat_protocol |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 2586 | #undef _ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2587 | default: |
| 2588 | nat_log_info ("unknown protocol"); |
| 2589 | return 1; |
| 2590 | } |
Matus Fabian | 09d96f4 | 2017-02-02 01:43:00 -0800 | [diff] [blame] | 2591 | |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2592 | } |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 2593 | |
| 2594 | if (ga) |
| 2595 | { |
| 2596 | a = ga; |
| 2597 | switch (k->protocol) |
| 2598 | { |
| 2599 | #define _(N, j, n, s) \ |
| 2600 | case SNAT_PROTOCOL_##N: \ |
| 2601 | while (1) \ |
| 2602 | { \ |
| 2603 | portnum = (port_per_thread * \ |
| 2604 | snat_thread_index) + \ |
| 2605 | snat_random_port(1, port_per_thread) + 1024; \ |
| 2606 | if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \ |
| 2607 | continue; \ |
| 2608 | clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \ |
| 2609 | a->busy_##n##_ports_per_thread[thread_index]++; \ |
| 2610 | a->busy_##n##_ports++; \ |
| 2611 | k->addr = a->addr; \ |
| 2612 | k->port = clib_host_to_net_u16(portnum); \ |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 2613 | return 0; \ |
| 2614 | } |
| 2615 | break; |
| 2616 | foreach_snat_protocol |
| 2617 | #undef _ |
| 2618 | default: |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 2619 | nat_log_info ("unknown protocol"); |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 2620 | return 1; |
| 2621 | } |
| 2622 | } |
| 2623 | |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2624 | /* Totally out of translations to use... */ |
Filip Varga | 8254ab8 | 2019-01-21 00:05:03 -0800 | [diff] [blame] | 2625 | snat_ipfix_logging_addresses_exhausted (thread_index, 0); |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2626 | return 1; |
| 2627 | } |
| 2628 | |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 2629 | static int |
| 2630 | nat_alloc_addr_and_port_mape (snat_address_t * addresses, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2631 | u32 fib_index, |
| 2632 | u32 thread_index, |
| 2633 | snat_session_key_t * k, |
| 2634 | u16 port_per_thread, u32 snat_thread_index) |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 2635 | { |
| 2636 | snat_main_t *sm = &snat_main; |
| 2637 | snat_address_t *a = addresses; |
| 2638 | u16 m, ports, portnum, A, j; |
| 2639 | m = 16 - (sm->psid_offset + sm->psid_length); |
| 2640 | ports = (1 << (16 - sm->psid_length)) - (1 << m); |
| 2641 | |
| 2642 | if (!vec_len (addresses)) |
| 2643 | goto exhausted; |
| 2644 | |
| 2645 | switch (k->protocol) |
| 2646 | { |
| 2647 | #define _(N, i, n, s) \ |
| 2648 | case SNAT_PROTOCOL_##N: \ |
| 2649 | if (a->busy_##n##_ports < ports) \ |
| 2650 | { \ |
| 2651 | while (1) \ |
| 2652 | { \ |
| 2653 | A = snat_random_port(1, pow2_mask(sm->psid_offset)); \ |
| 2654 | j = snat_random_port(0, pow2_mask(m)); \ |
| 2655 | portnum = A | (sm->psid << sm->psid_offset) | (j << (16 - m)); \ |
| 2656 | if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \ |
| 2657 | continue; \ |
| 2658 | clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \ |
| 2659 | a->busy_##n##_ports++; \ |
| 2660 | k->addr = a->addr; \ |
| 2661 | k->port = clib_host_to_net_u16 (portnum); \ |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 2662 | return 0; \ |
| 2663 | } \ |
| 2664 | } \ |
| 2665 | break; |
| 2666 | foreach_snat_protocol |
| 2667 | #undef _ |
| 2668 | default: |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 2669 | nat_log_info ("unknown protocol"); |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 2670 | return 1; |
| 2671 | } |
| 2672 | |
| 2673 | exhausted: |
| 2674 | /* Totally out of translations to use... */ |
Filip Varga | 8254ab8 | 2019-01-21 00:05:03 -0800 | [diff] [blame] | 2675 | snat_ipfix_logging_addresses_exhausted (thread_index, 0); |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 2676 | return 1; |
| 2677 | } |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 2678 | |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 2679 | static int |
| 2680 | nat_alloc_addr_and_port_range (snat_address_t * addresses, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2681 | u32 fib_index, |
| 2682 | u32 thread_index, |
| 2683 | snat_session_key_t * k, |
| 2684 | u16 port_per_thread, u32 snat_thread_index) |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 2685 | { |
| 2686 | snat_main_t *sm = &snat_main; |
| 2687 | snat_address_t *a = addresses; |
| 2688 | u16 portnum, ports; |
| 2689 | |
| 2690 | ports = sm->end_port - sm->start_port + 1; |
| 2691 | |
| 2692 | if (!vec_len (addresses)) |
| 2693 | goto exhausted; |
| 2694 | |
| 2695 | switch (k->protocol) |
| 2696 | { |
| 2697 | #define _(N, i, n, s) \ |
| 2698 | case SNAT_PROTOCOL_##N: \ |
| 2699 | if (a->busy_##n##_ports < ports) \ |
| 2700 | { \ |
| 2701 | while (1) \ |
| 2702 | { \ |
| 2703 | portnum = snat_random_port(sm->start_port, sm->end_port); \ |
| 2704 | if (clib_bitmap_get_no_check (a->busy_##n##_port_bitmap, portnum)) \ |
| 2705 | continue; \ |
| 2706 | clib_bitmap_set_no_check (a->busy_##n##_port_bitmap, portnum, 1); \ |
| 2707 | a->busy_##n##_ports++; \ |
| 2708 | k->addr = a->addr; \ |
| 2709 | k->port = clib_host_to_net_u16 (portnum); \ |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 2710 | return 0; \ |
| 2711 | } \ |
| 2712 | } \ |
| 2713 | break; |
| 2714 | foreach_snat_protocol |
| 2715 | #undef _ |
| 2716 | default: |
| 2717 | nat_log_info ("unknown protocol"); |
| 2718 | return 1; |
| 2719 | } |
| 2720 | |
| 2721 | exhausted: |
| 2722 | /* Totally out of translations to use... */ |
Filip Varga | 8254ab8 | 2019-01-21 00:05:03 -0800 | [diff] [blame] | 2723 | snat_ipfix_logging_addresses_exhausted (thread_index, 0); |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 2724 | return 1; |
| 2725 | } |
| 2726 | |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 2727 | void |
| 2728 | nat44_add_del_address_dpo (ip4_address_t addr, u8 is_add) |
| 2729 | { |
| 2730 | dpo_id_t dpo_v4 = DPO_INVALID; |
| 2731 | fib_prefix_t pfx = { |
| 2732 | .fp_proto = FIB_PROTOCOL_IP4, |
| 2733 | .fp_len = 32, |
| 2734 | .fp_addr.ip4.as_u32 = addr.as_u32, |
| 2735 | }; |
| 2736 | |
| 2737 | if (is_add) |
| 2738 | { |
| 2739 | nat_dpo_create (DPO_PROTO_IP4, 0, &dpo_v4); |
| 2740 | fib_table_entry_special_dpo_add (0, &pfx, FIB_SOURCE_PLUGIN_HI, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2741 | FIB_ENTRY_FLAG_EXCLUSIVE, &dpo_v4); |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 2742 | dpo_reset (&dpo_v4); |
| 2743 | } |
| 2744 | else |
| 2745 | { |
| 2746 | fib_table_entry_special_remove (0, &pfx, FIB_SOURCE_PLUGIN_HI); |
| 2747 | } |
| 2748 | } |
| 2749 | |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 2750 | u8 * |
| 2751 | format_session_kvp (u8 * s, va_list * args) |
| 2752 | { |
| 2753 | clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *); |
| 2754 | snat_session_key_t k; |
| 2755 | |
| 2756 | k.as_u64 = v->key; |
| 2757 | |
| 2758 | s = format (s, "%U session-index %llu", format_snat_key, &k, v->value); |
| 2759 | |
| 2760 | return s; |
| 2761 | } |
| 2762 | |
| 2763 | u8 * |
| 2764 | format_static_mapping_kvp (u8 * s, va_list * args) |
| 2765 | { |
| 2766 | clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *); |
| 2767 | snat_session_key_t k; |
| 2768 | |
| 2769 | k.as_u64 = v->key; |
| 2770 | |
Matus Fabian | 878c646 | 2018-08-23 00:33:35 -0700 | [diff] [blame] | 2771 | s = format (s, "%U static-mapping-index %llu", |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2772 | format_static_mapping_key, &k, v->value); |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 2773 | |
| 2774 | return s; |
| 2775 | } |
| 2776 | |
| 2777 | u8 * |
| 2778 | format_user_kvp (u8 * s, va_list * args) |
| 2779 | { |
| 2780 | clib_bihash_kv_8_8_t *v = va_arg (*args, clib_bihash_kv_8_8_t *); |
| 2781 | snat_user_key_t k; |
| 2782 | |
| 2783 | k.as_u64 = v->key; |
| 2784 | |
| 2785 | s = format (s, "%U fib %d user-index %llu", format_ip4_address, &k.addr, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2786 | k.fib_index, v->value); |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 2787 | |
| 2788 | return s; |
| 2789 | } |
| 2790 | |
| 2791 | u8 * |
| 2792 | format_ed_session_kvp (u8 * s, va_list * args) |
| 2793 | { |
| 2794 | clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *); |
| 2795 | nat_ed_ses_key_t k; |
| 2796 | |
| 2797 | k.as_u64[0] = v->key[0]; |
| 2798 | k.as_u64[1] = v->key[1]; |
| 2799 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2800 | s = |
| 2801 | format (s, "local %U:%d remote %U:%d proto %U fib %d session-index %llu", |
| 2802 | format_ip4_address, &k.l_addr, clib_net_to_host_u16 (k.l_port), |
| 2803 | format_ip4_address, &k.r_addr, clib_net_to_host_u16 (k.r_port), |
| 2804 | format_ip_protocol, k.proto, k.fib_index, v->value); |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 2805 | |
| 2806 | return s; |
| 2807 | } |
| 2808 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2809 | static u32 |
| 2810 | snat_get_worker_in2out_cb (ip4_header_t * ip0, u32 rx_fib_index0) |
| 2811 | { |
| 2812 | snat_main_t *sm = &snat_main; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2813 | u32 next_worker_index = 0; |
Matus Fabian | 7865b5c | 2017-09-26 01:23:01 -0700 | [diff] [blame] | 2814 | u32 hash; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2815 | |
Matus Fabian | 7865b5c | 2017-09-26 01:23:01 -0700 | [diff] [blame] | 2816 | next_worker_index = sm->first_worker_index; |
| 2817 | hash = ip0->src_address.as_u32 + (ip0->src_address.as_u32 >> 8) + |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2818 | (ip0->src_address.as_u32 >> 16) + (ip0->src_address.as_u32 >> 24); |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2819 | |
Matus Fabian | 7865b5c | 2017-09-26 01:23:01 -0700 | [diff] [blame] | 2820 | if (PREDICT_TRUE (is_pow2 (_vec_len (sm->workers)))) |
| 2821 | next_worker_index += sm->workers[hash & (_vec_len (sm->workers) - 1)]; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2822 | else |
Matus Fabian | 7865b5c | 2017-09-26 01:23:01 -0700 | [diff] [blame] | 2823 | next_worker_index += sm->workers[hash % _vec_len (sm->workers)]; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2824 | |
| 2825 | return next_worker_index; |
| 2826 | } |
| 2827 | |
| 2828 | static u32 |
| 2829 | snat_get_worker_out2in_cb (ip4_header_t * ip0, u32 rx_fib_index0) |
| 2830 | { |
| 2831 | snat_main_t *sm = &snat_main; |
Matus Fabian | ed3c160 | 2017-09-21 05:07:12 -0700 | [diff] [blame] | 2832 | udp_header_t *udp; |
| 2833 | u16 port; |
| 2834 | snat_session_key_t m_key; |
| 2835 | clib_bihash_kv_8_8_t kv, value; |
| 2836 | snat_static_mapping_t *m; |
Matus Fabian | ed3c160 | 2017-09-21 05:07:12 -0700 | [diff] [blame] | 2837 | u32 proto; |
Matus Fabian | 1049139 | 2018-01-05 05:03:35 -0800 | [diff] [blame] | 2838 | u32 next_worker_index = 0; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2839 | |
Matus Fabian | ed3c160 | 2017-09-21 05:07:12 -0700 | [diff] [blame] | 2840 | /* first try static mappings without port */ |
| 2841 | if (PREDICT_FALSE (pool_elts (sm->static_mappings))) |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2842 | { |
Matus Fabian | ed3c160 | 2017-09-21 05:07:12 -0700 | [diff] [blame] | 2843 | m_key.addr = ip0->dst_address; |
| 2844 | m_key.port = 0; |
| 2845 | m_key.protocol = 0; |
| 2846 | m_key.fib_index = rx_fib_index0; |
| 2847 | kv.key = m_key.as_u64; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2848 | if (!clib_bihash_search_8_8 |
| 2849 | (&sm->static_mapping_by_external, &kv, &value)) |
| 2850 | { |
| 2851 | m = pool_elt_at_index (sm->static_mappings, value.value); |
| 2852 | return m->workers[0]; |
| 2853 | } |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2854 | } |
| 2855 | |
Matus Fabian | ed3c160 | 2017-09-21 05:07:12 -0700 | [diff] [blame] | 2856 | proto = ip_proto_to_snat_proto (ip0->protocol); |
| 2857 | udp = ip4_next_header (ip0); |
| 2858 | port = udp->dst_port; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2859 | |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 2860 | if (PREDICT_FALSE (ip4_is_fragment (ip0))) |
| 2861 | { |
| 2862 | if (PREDICT_FALSE (nat_reass_is_drop_frag (0))) |
| 2863 | return vlib_get_thread_index (); |
| 2864 | |
Matus Fabian | 6845867 | 2019-02-18 01:54:16 -0800 | [diff] [blame^] | 2865 | nat_reass_ip4_t *reass; |
| 2866 | reass = nat_ip4_reass_find (ip0->src_address, ip0->dst_address, |
| 2867 | ip0->fragment_id, ip0->protocol); |
| 2868 | |
| 2869 | if (reass && (reass->thread_index != (u32) ~ 0)) |
| 2870 | return reass->thread_index; |
| 2871 | |
| 2872 | if (ip4_is_first_fragment (ip0)) |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 2873 | { |
Matus Fabian | 6845867 | 2019-02-18 01:54:16 -0800 | [diff] [blame^] | 2874 | reass = |
| 2875 | nat_ip4_reass_create (ip0->src_address, ip0->dst_address, |
| 2876 | ip0->fragment_id, ip0->protocol); |
| 2877 | if (!reass) |
| 2878 | goto no_reass; |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 2879 | |
Matus Fabian | 6845867 | 2019-02-18 01:54:16 -0800 | [diff] [blame^] | 2880 | if (PREDICT_FALSE (pool_elts (sm->static_mappings))) |
| 2881 | { |
| 2882 | m_key.addr = ip0->dst_address; |
| 2883 | m_key.port = clib_net_to_host_u16 (port); |
| 2884 | m_key.protocol = proto; |
| 2885 | m_key.fib_index = rx_fib_index0; |
| 2886 | kv.key = m_key.as_u64; |
| 2887 | if (!clib_bihash_search_8_8 |
| 2888 | (&sm->static_mapping_by_external, &kv, &value)) |
| 2889 | { |
| 2890 | m = pool_elt_at_index (sm->static_mappings, value.value); |
| 2891 | reass->thread_index = m->workers[0]; |
| 2892 | return reass->thread_index; |
| 2893 | } |
| 2894 | } |
| 2895 | reass->thread_index = sm->first_worker_index; |
| 2896 | reass->thread_index += |
| 2897 | sm->workers[(clib_net_to_host_u16 (port) - 1024) / |
| 2898 | sm->port_per_thread]; |
| 2899 | return reass->thread_index; |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 2900 | } |
Matus Fabian | 6845867 | 2019-02-18 01:54:16 -0800 | [diff] [blame^] | 2901 | else |
| 2902 | return vlib_get_thread_index (); |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 2903 | } |
| 2904 | |
Matus Fabian | 6845867 | 2019-02-18 01:54:16 -0800 | [diff] [blame^] | 2905 | no_reass: |
Matus Fabian | ed3c160 | 2017-09-21 05:07:12 -0700 | [diff] [blame] | 2906 | /* unknown protocol */ |
| 2907 | if (PREDICT_FALSE (proto == ~0)) |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2908 | { |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 2909 | /* use current thread */ |
Matus Fabian | ed3c160 | 2017-09-21 05:07:12 -0700 | [diff] [blame] | 2910 | return vlib_get_thread_index (); |
| 2911 | } |
| 2912 | |
| 2913 | if (PREDICT_FALSE (ip0->protocol == IP_PROTOCOL_ICMP)) |
| 2914 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2915 | icmp46_header_t *icmp = (icmp46_header_t *) udp; |
| 2916 | icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1); |
Matus Fabian | ed3c160 | 2017-09-21 05:07:12 -0700 | [diff] [blame] | 2917 | if (!icmp_is_error_message (icmp)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2918 | port = echo->identifier; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2919 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2920 | { |
| 2921 | ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1); |
| 2922 | proto = ip_proto_to_snat_proto (inner_ip->protocol); |
| 2923 | void *l4_header = ip4_next_header (inner_ip); |
| 2924 | switch (proto) |
| 2925 | { |
| 2926 | case SNAT_PROTOCOL_ICMP: |
| 2927 | icmp = (icmp46_header_t *) l4_header; |
| 2928 | echo = (icmp_echo_header_t *) (icmp + 1); |
| 2929 | port = echo->identifier; |
| 2930 | break; |
| 2931 | case SNAT_PROTOCOL_UDP: |
| 2932 | case SNAT_PROTOCOL_TCP: |
| 2933 | port = ((tcp_udp_header_t *) l4_header)->src_port; |
| 2934 | break; |
| 2935 | default: |
| 2936 | return vlib_get_thread_index (); |
| 2937 | } |
| 2938 | } |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2939 | } |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2940 | |
Matus Fabian | ed3c160 | 2017-09-21 05:07:12 -0700 | [diff] [blame] | 2941 | /* try static mappings with port */ |
| 2942 | if (PREDICT_FALSE (pool_elts (sm->static_mappings))) |
| 2943 | { |
| 2944 | m_key.addr = ip0->dst_address; |
| 2945 | m_key.port = clib_net_to_host_u16 (port); |
| 2946 | m_key.protocol = proto; |
| 2947 | m_key.fib_index = rx_fib_index0; |
| 2948 | kv.key = m_key.as_u64; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2949 | if (!clib_bihash_search_8_8 |
| 2950 | (&sm->static_mapping_by_external, &kv, &value)) |
| 2951 | { |
| 2952 | m = pool_elt_at_index (sm->static_mappings, value.value); |
| 2953 | return m->workers[0]; |
| 2954 | } |
Matus Fabian | ed3c160 | 2017-09-21 05:07:12 -0700 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | /* worker by outside port */ |
Matus Fabian | 1049139 | 2018-01-05 05:03:35 -0800 | [diff] [blame] | 2958 | next_worker_index = sm->first_worker_index; |
| 2959 | next_worker_index += |
| 2960 | sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread]; |
| 2961 | return next_worker_index; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 2962 | } |
| 2963 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 2964 | static u32 |
| 2965 | nat44_ed_get_worker_out2in_cb (ip4_header_t * ip, u32 rx_fib_index) |
| 2966 | { |
| 2967 | snat_main_t *sm = &snat_main; |
| 2968 | clib_bihash_kv_8_8_t kv, value; |
| 2969 | u32 proto, next_worker_index = 0; |
| 2970 | udp_header_t *udp; |
| 2971 | u16 port; |
| 2972 | snat_static_mapping_t *m; |
| 2973 | u32 hash; |
| 2974 | |
| 2975 | /* first try static mappings without port */ |
| 2976 | if (PREDICT_FALSE (pool_elts (sm->static_mappings))) |
| 2977 | { |
| 2978 | make_sm_kv (&kv, &ip->dst_address, 0, rx_fib_index, 0); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 2979 | if (!clib_bihash_search_8_8 |
| 2980 | (&sm->static_mapping_by_external, &kv, &value)) |
| 2981 | { |
| 2982 | m = pool_elt_at_index (sm->static_mappings, value.value); |
| 2983 | return m->workers[0]; |
| 2984 | } |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | proto = ip_proto_to_snat_proto (ip->protocol); |
| 2988 | |
| 2989 | /* unknown protocol */ |
| 2990 | if (PREDICT_FALSE (proto == ~0)) |
| 2991 | { |
| 2992 | /* use current thread */ |
| 2993 | return vlib_get_thread_index (); |
| 2994 | } |
| 2995 | |
| 2996 | udp = ip4_next_header (ip); |
| 2997 | port = udp->dst_port; |
| 2998 | |
| 2999 | if (PREDICT_FALSE (ip->protocol == IP_PROTOCOL_ICMP)) |
| 3000 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3001 | icmp46_header_t *icmp = (icmp46_header_t *) udp; |
| 3002 | icmp_echo_header_t *echo = (icmp_echo_header_t *) (icmp + 1); |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3003 | if (!icmp_is_error_message (icmp)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3004 | port = echo->identifier; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3005 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3006 | { |
| 3007 | ip4_header_t *inner_ip = (ip4_header_t *) (echo + 1); |
| 3008 | proto = ip_proto_to_snat_proto (inner_ip->protocol); |
| 3009 | void *l4_header = ip4_next_header (inner_ip); |
| 3010 | switch (proto) |
| 3011 | { |
| 3012 | case SNAT_PROTOCOL_ICMP: |
| 3013 | icmp = (icmp46_header_t *) l4_header; |
| 3014 | echo = (icmp_echo_header_t *) (icmp + 1); |
| 3015 | port = echo->identifier; |
| 3016 | break; |
| 3017 | case SNAT_PROTOCOL_UDP: |
| 3018 | case SNAT_PROTOCOL_TCP: |
| 3019 | port = ((tcp_udp_header_t *) l4_header)->src_port; |
| 3020 | break; |
| 3021 | default: |
| 3022 | return vlib_get_thread_index (); |
| 3023 | } |
| 3024 | } |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | /* try static mappings with port */ |
| 3028 | if (PREDICT_FALSE (pool_elts (sm->static_mappings))) |
| 3029 | { |
| 3030 | make_sm_kv (&kv, &ip->dst_address, proto, rx_fib_index, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3031 | clib_net_to_host_u16 (port)); |
| 3032 | if (!clib_bihash_search_8_8 |
| 3033 | (&sm->static_mapping_by_external, &kv, &value)) |
| 3034 | { |
| 3035 | m = pool_elt_at_index (sm->static_mappings, value.value); |
Matus Fabian | 82b4ceb | 2018-10-11 04:28:48 -0700 | [diff] [blame] | 3036 | if (!is_lb_static_mapping (m)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3037 | return m->workers[0]; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3038 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3039 | hash = ip->src_address.as_u32 + (ip->src_address.as_u32 >> 8) + |
| 3040 | (ip->src_address.as_u32 >> 16) + (ip->src_address.as_u32 >> 24); |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3041 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3042 | if (PREDICT_TRUE (is_pow2 (_vec_len (m->workers)))) |
| 3043 | return m->workers[hash & (_vec_len (m->workers) - 1)]; |
| 3044 | else |
| 3045 | return m->workers[hash % _vec_len (m->workers)]; |
| 3046 | } |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | /* worker by outside port */ |
| 3050 | next_worker_index = sm->first_worker_index; |
| 3051 | next_worker_index += |
| 3052 | sm->workers[(clib_net_to_host_u16 (port) - 1024) / sm->port_per_thread]; |
| 3053 | |
| 3054 | return next_worker_index; |
| 3055 | } |
| 3056 | |
Matus Fabian | eea28d7 | 2017-01-13 04:15:54 -0800 | [diff] [blame] | 3057 | static clib_error_t * |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3058 | snat_config (vlib_main_t * vm, unformat_input_t * input) |
| 3059 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3060 | snat_main_t *sm = &snat_main; |
| 3061 | nat66_main_t *nm = &nat66_main; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3062 | u32 translation_buckets = 1024; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3063 | u32 translation_memory_size = 128 << 20; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3064 | u32 user_buckets = 128; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3065 | u32 user_memory_size = 64 << 20; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3066 | u32 max_translations_per_user = 100; |
| 3067 | u32 outside_vrf_id = 0; |
Juraj Sloboda | 9341e34 | 2018-04-13 12:00:46 +0200 | [diff] [blame] | 3068 | u32 outside_ip6_vrf_id = 0; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 3069 | u32 inside_vrf_id = 0; |
| 3070 | u32 static_mapping_buckets = 1024; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3071 | u32 static_mapping_memory_size = 64 << 20; |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 3072 | u32 nat64_bib_buckets = 1024; |
| 3073 | u32 nat64_bib_memory_size = 128 << 20; |
| 3074 | u32 nat64_st_buckets = 2048; |
| 3075 | u32 nat64_st_memory_size = 256 << 20; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 3076 | u8 static_mapping_only = 0; |
| 3077 | u8 static_mapping_connection_tracking = 0; |
Matus Fabian | 092b3cd | 2017-09-19 05:42:38 -0700 | [diff] [blame] | 3078 | snat_main_per_thread_data_t *tsm; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3079 | dslite_main_t *dm = &dslite_main; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3080 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 3081 | sm->deterministic = 0; |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 3082 | sm->out2in_dpo = 0; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3083 | sm->endpoint_dependent = 0; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 3084 | |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3085 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 3086 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3087 | if (unformat |
| 3088 | (input, "translation hash buckets %d", &translation_buckets)) |
| 3089 | ; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3090 | else if (unformat (input, "translation hash memory %d", |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3091 | &translation_memory_size)); |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3092 | else if (unformat (input, "user hash buckets %d", &user_buckets)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3093 | ; |
| 3094 | else if (unformat (input, "user hash memory %d", &user_memory_size)) |
| 3095 | ; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3096 | else if (unformat (input, "max translations per user %d", |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3097 | &max_translations_per_user)) |
| 3098 | ; |
| 3099 | else if (unformat (input, "outside VRF id %d", &outside_vrf_id)) |
| 3100 | ; |
| 3101 | else if (unformat (input, "outside ip6 VRF id %d", &outside_ip6_vrf_id)) |
| 3102 | ; |
| 3103 | else if (unformat (input, "inside VRF id %d", &inside_vrf_id)) |
| 3104 | ; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 3105 | else if (unformat (input, "static mapping only")) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3106 | { |
| 3107 | static_mapping_only = 1; |
| 3108 | if (unformat (input, "connection tracking")) |
| 3109 | static_mapping_connection_tracking = 1; |
| 3110 | } |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 3111 | else if (unformat (input, "deterministic")) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3112 | sm->deterministic = 1; |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 3113 | else if (unformat (input, "nat64 bib hash buckets %d", |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3114 | &nat64_bib_buckets)) |
| 3115 | ; |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 3116 | else if (unformat (input, "nat64 bib hash memory %d", |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3117 | &nat64_bib_memory_size)) |
| 3118 | ; |
| 3119 | else |
| 3120 | if (unformat (input, "nat64 st hash buckets %d", &nat64_st_buckets)) |
| 3121 | ; |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 3122 | else if (unformat (input, "nat64 st hash memory %d", |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3123 | &nat64_st_memory_size)) |
| 3124 | ; |
Juraj Sloboda | cba6936 | 2017-12-19 02:09:32 +0100 | [diff] [blame] | 3125 | else if (unformat (input, "out2in dpo")) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3126 | sm->out2in_dpo = 1; |
Juraj Sloboda | c5c6a33 | 2018-01-09 16:08:32 +0100 | [diff] [blame] | 3127 | else if (unformat (input, "dslite ce")) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3128 | dslite_set_ce (dm, 1); |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3129 | else if (unformat (input, "endpoint-dependent")) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3130 | sm->endpoint_dependent = 1; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 3131 | else |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 3132 | return clib_error_return (0, "unknown input '%U'", |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3133 | format_unformat_error, input); |
| 3134 | } |
| 3135 | |
Matus Fabian | 69ce30d | 2018-08-22 01:27:10 -0700 | [diff] [blame] | 3136 | if (sm->deterministic && sm->endpoint_dependent) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3137 | return clib_error_return (0, |
| 3138 | "deterministic and endpoint-dependent modes are mutually exclusive"); |
Matus Fabian | 69ce30d | 2018-08-22 01:27:10 -0700 | [diff] [blame] | 3139 | |
| 3140 | if (static_mapping_only && (sm->deterministic || sm->endpoint_dependent)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3141 | return clib_error_return (0, |
| 3142 | "static mapping only mode available only for simple nat"); |
Matus Fabian | 69ce30d | 2018-08-22 01:27:10 -0700 | [diff] [blame] | 3143 | |
| 3144 | if (sm->out2in_dpo && (sm->deterministic || sm->endpoint_dependent)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3145 | return clib_error_return (0, |
| 3146 | "out2in dpo mode available only for simple nat"); |
Matus Fabian | 69ce30d | 2018-08-22 01:27:10 -0700 | [diff] [blame] | 3147 | |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3148 | /* for show commands, etc. */ |
| 3149 | sm->translation_buckets = translation_buckets; |
| 3150 | sm->translation_memory_size = translation_memory_size; |
Matus Fabian | 41fef50 | 2017-09-22 02:43:05 -0700 | [diff] [blame] | 3151 | /* do not exceed load factor 10 */ |
| 3152 | sm->max_translations = 10 * translation_buckets; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3153 | sm->user_buckets = user_buckets; |
| 3154 | sm->user_memory_size = user_memory_size; |
| 3155 | sm->max_translations_per_user = max_translations_per_user; |
| 3156 | sm->outside_vrf_id = outside_vrf_id; |
Matus Fabian | 31c31aa | 2017-02-05 22:45:57 -0800 | [diff] [blame] | 3157 | sm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3158 | outside_vrf_id, |
| 3159 | FIB_SOURCE_PLUGIN_HI); |
Juraj Sloboda | 9341e34 | 2018-04-13 12:00:46 +0200 | [diff] [blame] | 3160 | nm->outside_vrf_id = outside_ip6_vrf_id; |
| 3161 | nm->outside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP6, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3162 | outside_ip6_vrf_id, |
| 3163 | FIB_SOURCE_PLUGIN_HI); |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 3164 | sm->inside_vrf_id = inside_vrf_id; |
Matus Fabian | 31c31aa | 2017-02-05 22:45:57 -0800 | [diff] [blame] | 3165 | sm->inside_fib_index = fib_table_find_or_create_and_lock (FIB_PROTOCOL_IP4, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3166 | inside_vrf_id, |
| 3167 | FIB_SOURCE_PLUGIN_HI); |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 3168 | sm->static_mapping_only = static_mapping_only; |
| 3169 | sm->static_mapping_connection_tracking = static_mapping_connection_tracking; |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3170 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3171 | nat64_set_hash (nat64_bib_buckets, nat64_bib_memory_size, nat64_st_buckets, |
| 3172 | nat64_st_memory_size); |
Matus Fabian | 51e759f | 2017-12-07 23:22:51 -0800 | [diff] [blame] | 3173 | |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 3174 | if (sm->deterministic) |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 3175 | { |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 3176 | sm->in2out_node_index = snat_det_in2out_node.index; |
Matus Fabian | 93d84c9 | 2017-07-19 08:06:01 -0700 | [diff] [blame] | 3177 | sm->in2out_output_node_index = ~0; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 3178 | sm->out2in_node_index = snat_det_out2in_node.index; |
Juraj Sloboda | 7a1bde0 | 2017-04-03 08:43:58 +0200 | [diff] [blame] | 3179 | sm->icmp_match_in2out_cb = icmp_match_in2out_det; |
| 3180 | sm->icmp_match_out2in_cb = icmp_match_out2in_det; |
Matus Fabian | db64988 | 2016-08-26 05:45:27 -0700 | [diff] [blame] | 3181 | } |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 3182 | else |
| 3183 | { |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3184 | if (sm->endpoint_dependent) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3185 | { |
| 3186 | sm->worker_in2out_cb = snat_get_worker_in2out_cb; |
| 3187 | sm->worker_out2in_cb = nat44_ed_get_worker_out2in_cb; |
| 3188 | sm->in2out_node_index = nat44_ed_in2out_node.index; |
| 3189 | sm->in2out_output_node_index = nat44_ed_in2out_output_node.index; |
| 3190 | sm->out2in_node_index = nat44_ed_out2in_node.index; |
| 3191 | sm->icmp_match_in2out_cb = icmp_match_in2out_ed; |
| 3192 | sm->icmp_match_out2in_cb = icmp_match_out2in_ed; |
| 3193 | nat_affinity_init (vm); |
| 3194 | } |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3195 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3196 | { |
| 3197 | sm->worker_in2out_cb = snat_get_worker_in2out_cb; |
| 3198 | sm->worker_out2in_cb = snat_get_worker_out2in_cb; |
| 3199 | sm->in2out_node_index = snat_in2out_node.index; |
| 3200 | sm->in2out_output_node_index = snat_in2out_output_node.index; |
| 3201 | sm->out2in_node_index = snat_out2in_node.index; |
| 3202 | sm->icmp_match_in2out_cb = icmp_match_in2out_slow; |
| 3203 | sm->icmp_match_out2in_cb = icmp_match_out2in_slow; |
| 3204 | } |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 3205 | if (!static_mapping_only || |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3206 | (static_mapping_only && static_mapping_connection_tracking)) |
| 3207 | { |
| 3208 | /* *INDENT-OFF* */ |
Matus Fabian | 092b3cd | 2017-09-19 05:42:38 -0700 | [diff] [blame] | 3209 | vec_foreach (tsm, sm->per_thread_data) |
| 3210 | { |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3211 | if (sm->endpoint_dependent) |
| 3212 | { |
| 3213 | clib_bihash_init_16_8 (&tsm->in2out_ed, "in2out-ed", |
| 3214 | translation_buckets, |
| 3215 | translation_memory_size); |
| 3216 | clib_bihash_set_kvp_format_fn_16_8 (&tsm->in2out_ed, |
| 3217 | format_ed_session_kvp); |
Matus Fabian | 092b3cd | 2017-09-19 05:42:38 -0700 | [diff] [blame] | 3218 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3219 | clib_bihash_init_16_8 (&tsm->out2in_ed, "out2in-ed", |
| 3220 | translation_buckets, |
| 3221 | translation_memory_size); |
| 3222 | clib_bihash_set_kvp_format_fn_16_8 (&tsm->out2in_ed, |
| 3223 | format_ed_session_kvp); |
| 3224 | } |
| 3225 | else |
| 3226 | { |
| 3227 | clib_bihash_init_8_8 (&tsm->in2out, "in2out", |
| 3228 | translation_buckets, |
| 3229 | translation_memory_size); |
| 3230 | clib_bihash_set_kvp_format_fn_8_8 (&tsm->in2out, |
| 3231 | format_session_kvp); |
| 3232 | |
| 3233 | clib_bihash_init_8_8 (&tsm->out2in, "out2in", |
| 3234 | translation_buckets, |
| 3235 | translation_memory_size); |
| 3236 | clib_bihash_set_kvp_format_fn_8_8 (&tsm->out2in, |
| 3237 | format_session_kvp); |
| 3238 | } |
Matus Fabian | 092b3cd | 2017-09-19 05:42:38 -0700 | [diff] [blame] | 3239 | |
| 3240 | clib_bihash_init_8_8 (&tsm->user_hash, "users", user_buckets, |
| 3241 | user_memory_size); |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 3242 | clib_bihash_set_kvp_format_fn_8_8 (&tsm->user_hash, |
| 3243 | format_user_kvp); |
Matus Fabian | 092b3cd | 2017-09-19 05:42:38 -0700 | [diff] [blame] | 3244 | } |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3245 | /* *INDENT-ON* */ |
Matus Fabian | 092b3cd | 2017-09-19 05:42:38 -0700 | [diff] [blame] | 3246 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3247 | } |
Juraj Sloboda | 557a71c | 2017-02-22 05:16:06 -0800 | [diff] [blame] | 3248 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3249 | { |
| 3250 | sm->icmp_match_in2out_cb = icmp_match_in2out_fast; |
| 3251 | sm->icmp_match_out2in_cb = icmp_match_out2in_fast; |
| 3252 | } |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 3253 | clib_bihash_init_8_8 (&sm->static_mapping_by_local, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3254 | "static_mapping_by_local", static_mapping_buckets, |
| 3255 | static_mapping_memory_size); |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 3256 | clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_local, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3257 | format_static_mapping_kvp); |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 3258 | |
| 3259 | clib_bihash_init_8_8 (&sm->static_mapping_by_external, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3260 | "static_mapping_by_external", |
| 3261 | static_mapping_buckets, |
| 3262 | static_mapping_memory_size); |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 3263 | clib_bihash_set_kvp_format_fn_8_8 (&sm->static_mapping_by_external, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3264 | format_static_mapping_kvp); |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 3265 | } |
| 3266 | |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3267 | return 0; |
| 3268 | } |
| 3269 | |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3270 | VLIB_CONFIG_FUNCTION (snat_config, "nat"); |
Dave Barach | 20c02cb | 2016-06-26 10:42:08 -0400 | [diff] [blame] | 3271 | |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3272 | static void |
Matus Fabian | 4772e7a | 2018-04-04 00:38:02 -0700 | [diff] [blame] | 3273 | nat_ip4_add_del_addr_only_sm_cb (ip4_main_t * im, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3274 | uword opaque, |
| 3275 | u32 sw_if_index, |
| 3276 | ip4_address_t * address, |
| 3277 | u32 address_length, |
| 3278 | u32 if_address_index, u32 is_delete) |
Matus Fabian | 4772e7a | 2018-04-04 00:38:02 -0700 | [diff] [blame] | 3279 | { |
| 3280 | snat_main_t *sm = &snat_main; |
| 3281 | snat_static_map_resolve_t *rp; |
| 3282 | snat_static_mapping_t *m; |
| 3283 | snat_session_key_t m_key; |
| 3284 | clib_bihash_kv_8_8_t kv, value; |
| 3285 | int i, rv; |
| 3286 | ip4_address_t l_addr; |
| 3287 | |
| 3288 | for (i = 0; i < vec_len (sm->to_resolve); i++) |
| 3289 | { |
| 3290 | rp = sm->to_resolve + i; |
| 3291 | if (rp->addr_only == 0) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3292 | continue; |
Matus Fabian | 4772e7a | 2018-04-04 00:38:02 -0700 | [diff] [blame] | 3293 | if (rp->sw_if_index == sw_if_index) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3294 | goto match; |
Matus Fabian | 4772e7a | 2018-04-04 00:38:02 -0700 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | return; |
| 3298 | |
| 3299 | match: |
| 3300 | m_key.addr.as_u32 = address->as_u32; |
| 3301 | m_key.port = rp->addr_only ? 0 : rp->e_port; |
| 3302 | m_key.protocol = rp->addr_only ? 0 : rp->proto; |
| 3303 | m_key.fib_index = sm->outside_fib_index; |
| 3304 | kv.key = m_key.as_u64; |
| 3305 | if (clib_bihash_search_8_8 (&sm->static_mapping_by_external, &kv, &value)) |
| 3306 | m = 0; |
| 3307 | else |
| 3308 | m = pool_elt_at_index (sm->static_mappings, value.value); |
| 3309 | |
| 3310 | if (!is_delete) |
| 3311 | { |
| 3312 | /* Don't trip over lease renewal, static config */ |
| 3313 | if (m) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3314 | return; |
Matus Fabian | 4772e7a | 2018-04-04 00:38:02 -0700 | [diff] [blame] | 3315 | } |
| 3316 | else |
| 3317 | { |
| 3318 | if (!m) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3319 | return; |
Matus Fabian | 4772e7a | 2018-04-04 00:38:02 -0700 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | /* Indetity mapping? */ |
| 3323 | if (rp->l_addr.as_u32 == 0) |
| 3324 | l_addr.as_u32 = address[0].as_u32; |
| 3325 | else |
| 3326 | l_addr.as_u32 = rp->l_addr.as_u32; |
| 3327 | /* Add the static mapping */ |
| 3328 | rv = snat_add_static_mapping (l_addr, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3329 | address[0], |
| 3330 | rp->l_port, |
| 3331 | rp->e_port, |
| 3332 | rp->vrf_id, |
| 3333 | rp->addr_only, ~0 /* sw_if_index */ , |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 3334 | rp->proto, !is_delete, rp->twice_nat, |
| 3335 | rp->out2in_only, rp->tag, rp->identity_nat); |
Matus Fabian | 4772e7a | 2018-04-04 00:38:02 -0700 | [diff] [blame] | 3336 | if (rv) |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 3337 | nat_log_notice ("snat_add_static_mapping returned %d", rv); |
Matus Fabian | 4772e7a | 2018-04-04 00:38:02 -0700 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | static void |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3341 | snat_ip4_add_del_interface_address_cb (ip4_main_t * im, |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3342 | uword opaque, |
| 3343 | u32 sw_if_index, |
| 3344 | ip4_address_t * address, |
| 3345 | u32 address_length, |
| 3346 | u32 if_address_index, u32 is_delete) |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3347 | { |
| 3348 | snat_main_t *sm = &snat_main; |
Dave Barach | 8b27537 | 2017-01-16 10:54:02 -0500 | [diff] [blame] | 3349 | snat_static_map_resolve_t *rp; |
Matus Fabian | ab7a805 | 2017-11-28 04:29:41 -0800 | [diff] [blame] | 3350 | ip4_address_t l_addr; |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3351 | int i, j; |
Dave Barach | 8b27537 | 2017-01-16 10:54:02 -0500 | [diff] [blame] | 3352 | int rv; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 3353 | u8 twice_nat = 0; |
| 3354 | snat_address_t *addresses = sm->addresses; |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3355 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3356 | for (i = 0; i < vec_len (sm->auto_add_sw_if_indices); i++) |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3357 | { |
| 3358 | if (sw_if_index == sm->auto_add_sw_if_indices[i]) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3359 | goto match; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 3360 | } |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3361 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3362 | for (i = 0; i < vec_len (sm->auto_add_sw_if_indices_twice_nat); i++) |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 3363 | { |
| 3364 | twice_nat = 1; |
| 3365 | addresses = sm->twice_nat_addresses; |
| 3366 | if (sw_if_index == sm->auto_add_sw_if_indices_twice_nat[i]) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3367 | goto match; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 3368 | } |
| 3369 | |
| 3370 | return; |
| 3371 | |
| 3372 | match: |
| 3373 | if (!is_delete) |
| 3374 | { |
| 3375 | /* Don't trip over lease renewal, static config */ |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3376 | for (j = 0; j < vec_len (addresses); j++) |
| 3377 | if (addresses[j].addr.as_u32 == address->as_u32) |
| 3378 | return; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 3379 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3380 | (void) snat_add_address (sm, address, ~0, twice_nat); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 3381 | /* Scan static map resolution vector */ |
| 3382 | for (j = 0; j < vec_len (sm->to_resolve); j++) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3383 | { |
| 3384 | rp = sm->to_resolve + j; |
| 3385 | if (rp->addr_only) |
| 3386 | continue; |
| 3387 | /* On this interface? */ |
| 3388 | if (rp->sw_if_index == sw_if_index) |
| 3389 | { |
| 3390 | /* Indetity mapping? */ |
| 3391 | if (rp->l_addr.as_u32 == 0) |
| 3392 | l_addr.as_u32 = address[0].as_u32; |
| 3393 | else |
| 3394 | l_addr.as_u32 = rp->l_addr.as_u32; |
| 3395 | /* Add the static mapping */ |
| 3396 | rv = snat_add_static_mapping (l_addr, |
| 3397 | address[0], |
| 3398 | rp->l_port, |
| 3399 | rp->e_port, |
| 3400 | rp->vrf_id, |
| 3401 | rp->addr_only, |
| 3402 | ~0 /* sw_if_index */ , |
| 3403 | rp->proto, |
Matus Fabian | e2f4e2f | 2018-10-07 21:28:23 -0700 | [diff] [blame] | 3404 | rp->is_add, rp->twice_nat, |
| 3405 | rp->out2in_only, rp->tag, |
| 3406 | rp->identity_nat); |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3407 | if (rv) |
| 3408 | nat_log_notice ("snat_add_static_mapping returned %d", rv); |
| 3409 | } |
| 3410 | } |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 3411 | return; |
| 3412 | } |
| 3413 | else |
| 3414 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3415 | (void) snat_del_address (sm, address[0], 1, twice_nat); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 3416 | return; |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3421 | int |
| 3422 | snat_add_interface_address (snat_main_t * sm, u32 sw_if_index, int is_del, |
| 3423 | u8 twice_nat) |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3424 | { |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3425 | ip4_main_t *ip4_main = sm->ip4_main; |
| 3426 | ip4_address_t *first_int_addr; |
Matus Fabian | 36532bd | 2017-01-23 23:42:28 -0800 | [diff] [blame] | 3427 | snat_static_map_resolve_t *rp; |
| 3428 | u32 *indices_to_delete = 0; |
| 3429 | int i, j; |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 3430 | u32 *auto_add_sw_if_indices = |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3431 | twice_nat ? sm-> |
| 3432 | auto_add_sw_if_indices_twice_nat : sm->auto_add_sw_if_indices; |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3433 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3434 | first_int_addr = ip4_interface_first_address (ip4_main, sw_if_index, 0 /* just want the address */ |
| 3435 | ); |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3436 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3437 | for (i = 0; i < vec_len (auto_add_sw_if_indices); i++) |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3438 | { |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 3439 | if (auto_add_sw_if_indices[i] == sw_if_index) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3440 | { |
| 3441 | if (is_del) |
| 3442 | { |
| 3443 | /* if have address remove it */ |
| 3444 | if (first_int_addr) |
| 3445 | (void) snat_del_address (sm, first_int_addr[0], 1, twice_nat); |
| 3446 | else |
| 3447 | { |
| 3448 | for (j = 0; j < vec_len (sm->to_resolve); j++) |
| 3449 | { |
| 3450 | rp = sm->to_resolve + j; |
| 3451 | if (rp->sw_if_index == sw_if_index) |
| 3452 | vec_add1 (indices_to_delete, j); |
| 3453 | } |
| 3454 | if (vec_len (indices_to_delete)) |
| 3455 | { |
| 3456 | for (j = vec_len (indices_to_delete) - 1; j >= 0; j--) |
| 3457 | vec_del1 (sm->to_resolve, j); |
| 3458 | vec_free (indices_to_delete); |
| 3459 | } |
| 3460 | } |
| 3461 | if (twice_nat) |
| 3462 | vec_del1 (sm->auto_add_sw_if_indices_twice_nat, i); |
| 3463 | else |
| 3464 | vec_del1 (sm->auto_add_sw_if_indices, i); |
| 3465 | } |
| 3466 | else |
| 3467 | return VNET_API_ERROR_VALUE_EXIST; |
Matus Fabian | 8bf68e8 | 2017-01-12 04:24:35 -0800 | [diff] [blame] | 3468 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3469 | return 0; |
| 3470 | } |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3471 | } |
Matus Fabian | 2ba92e3 | 2017-08-21 07:05:03 -0700 | [diff] [blame] | 3472 | |
Matus Fabian | 8bf68e8 | 2017-01-12 04:24:35 -0800 | [diff] [blame] | 3473 | if (is_del) |
| 3474 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 3475 | |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3476 | /* add to the auto-address list */ |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 3477 | if (twice_nat) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3478 | vec_add1 (sm->auto_add_sw_if_indices_twice_nat, sw_if_index); |
Matus Fabian | b932d26 | 2017-12-18 05:38:24 -0800 | [diff] [blame] | 3479 | else |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3480 | vec_add1 (sm->auto_add_sw_if_indices, sw_if_index); |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3481 | |
| 3482 | /* If the address is already bound - or static - add it now */ |
| 3483 | if (first_int_addr) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3484 | (void) snat_add_address (sm, first_int_addr, ~0, twice_nat); |
Dave Barach | cab65ec | 2017-01-11 13:01:14 -0500 | [diff] [blame] | 3485 | |
| 3486 | return 0; |
| 3487 | } |
| 3488 | |
Matus Fabian | 5ba86f7 | 2017-10-26 03:37:38 -0700 | [diff] [blame] | 3489 | int |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3490 | nat44_del_session (snat_main_t * sm, ip4_address_t * addr, u16 port, |
| 3491 | snat_protocol_t proto, u32 vrf_id, int is_in) |
Matus Fabian | 5ba86f7 | 2017-10-26 03:37:38 -0700 | [diff] [blame] | 3492 | { |
| 3493 | snat_main_per_thread_data_t *tsm; |
| 3494 | clib_bihash_kv_8_8_t kv, value; |
| 3495 | ip4_header_t ip; |
| 3496 | u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id); |
| 3497 | snat_session_key_t key; |
| 3498 | snat_session_t *s; |
| 3499 | clib_bihash_8_8_t *t; |
Matus Fabian | 5ba86f7 | 2017-10-26 03:37:38 -0700 | [diff] [blame] | 3500 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3501 | if (sm->endpoint_dependent) |
| 3502 | return VNET_API_ERROR_UNSUPPORTED; |
| 3503 | |
Matus Fabian | 5ba86f7 | 2017-10-26 03:37:38 -0700 | [diff] [blame] | 3504 | ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32; |
Matus Fabian | 4888b50 | 2018-03-27 01:07:25 -0700 | [diff] [blame] | 3505 | if (sm->num_workers > 1) |
Matus Fabian | 5ba86f7 | 2017-10-26 03:37:38 -0700 | [diff] [blame] | 3506 | tsm = |
| 3507 | vec_elt_at_index (sm->per_thread_data, |
| 3508 | sm->worker_in2out_cb (&ip, fib_index)); |
| 3509 | else |
| 3510 | tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers); |
| 3511 | |
| 3512 | key.addr.as_u32 = addr->as_u32; |
| 3513 | key.port = clib_host_to_net_u16 (port); |
| 3514 | key.protocol = proto; |
| 3515 | key.fib_index = fib_index; |
| 3516 | kv.key = key.as_u64; |
| 3517 | t = is_in ? &tsm->in2out : &tsm->out2in; |
| 3518 | if (!clib_bihash_search_8_8 (t, &kv, &value)) |
| 3519 | { |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 3520 | if (pool_is_free_index (tsm->sessions, value.value)) |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3521 | return VNET_API_ERROR_UNSPECIFIED; |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 3522 | |
Matus Fabian | 5ba86f7 | 2017-10-26 03:37:38 -0700 | [diff] [blame] | 3523 | s = pool_elt_at_index (tsm->sessions, value.value); |
Matus Fabian | 229c1aa | 2018-05-28 04:09:52 -0700 | [diff] [blame] | 3524 | nat_free_session_data (sm, s, tsm - sm->per_thread_data); |
| 3525 | nat44_delete_session (sm, s, tsm - sm->per_thread_data); |
Matus Fabian | 5ba86f7 | 2017-10-26 03:37:38 -0700 | [diff] [blame] | 3526 | return 0; |
| 3527 | } |
| 3528 | |
| 3529 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 3530 | } |
| 3531 | |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 3532 | int |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3533 | nat44_del_ed_session (snat_main_t * sm, ip4_address_t * addr, u16 port, |
| 3534 | ip4_address_t * eh_addr, u16 eh_port, u8 proto, |
| 3535 | u32 vrf_id, int is_in) |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 3536 | { |
| 3537 | ip4_header_t ip; |
| 3538 | clib_bihash_16_8_t *t; |
| 3539 | nat_ed_ses_key_t key; |
| 3540 | clib_bihash_kv_16_8_t kv, value; |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 3541 | u32 fib_index = fib_table_find (FIB_PROTOCOL_IP4, vrf_id); |
| 3542 | snat_session_t *s; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3543 | snat_main_per_thread_data_t *tsm; |
| 3544 | |
| 3545 | if (!sm->endpoint_dependent) |
| 3546 | return VNET_API_ERROR_FEATURE_DISABLED; |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 3547 | |
| 3548 | ip.dst_address.as_u32 = ip.src_address.as_u32 = addr->as_u32; |
| 3549 | if (sm->num_workers > 1) |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3550 | tsm = |
| 3551 | vec_elt_at_index (sm->per_thread_data, |
| 3552 | sm->worker_in2out_cb (&ip, fib_index)); |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 3553 | else |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3554 | tsm = vec_elt_at_index (sm->per_thread_data, sm->num_workers); |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 3555 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3556 | t = is_in ? &tsm->in2out_ed : &tsm->out2in_ed; |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 3557 | key.l_addr.as_u32 = addr->as_u32; |
| 3558 | key.r_addr.as_u32 = eh_addr->as_u32; |
| 3559 | key.l_port = clib_host_to_net_u16 (port); |
| 3560 | key.r_port = clib_host_to_net_u16 (eh_port); |
| 3561 | key.proto = proto; |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3562 | key.fib_index = fib_index; |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 3563 | kv.key[0] = key.as_u64[0]; |
| 3564 | kv.key[1] = key.as_u64[1]; |
| 3565 | if (clib_bihash_search_16_8 (t, &kv, &value)) |
| 3566 | return VNET_API_ERROR_NO_SUCH_ENTRY; |
| 3567 | |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3568 | if (pool_is_free_index (tsm->sessions, value.value)) |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 3569 | return VNET_API_ERROR_UNSPECIFIED; |
Matus Fabian | a6110b6 | 2018-06-13 05:39:07 -0700 | [diff] [blame] | 3570 | s = pool_elt_at_index (tsm->sessions, value.value); |
| 3571 | nat_free_session_data (sm, s, tsm - sm->per_thread_data); |
| 3572 | nat44_delete_session (sm, s, tsm - sm->per_thread_data); |
Matus Fabian | 70a26ac | 2018-05-14 06:20:28 -0700 | [diff] [blame] | 3573 | return 0; |
| 3574 | } |
| 3575 | |
Matus Fabian | 8211954 | 2018-01-25 01:13:22 -0800 | [diff] [blame] | 3576 | void |
| 3577 | nat_set_alloc_addr_and_port_mape (u16 psid, u16 psid_offset, u16 psid_length) |
Matus Fabian | 5ba86f7 | 2017-10-26 03:37:38 -0700 | [diff] [blame] | 3578 | { |
| 3579 | snat_main_t *sm = &snat_main; |
Matus Fabian | 5ba86f7 | 2017-10-26 03:37:38 -0700 | [diff] [blame] | 3580 | |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 3581 | sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_MAPE; |
Matus Fabian | 8211954 | 2018-01-25 01:13:22 -0800 | [diff] [blame] | 3582 | sm->alloc_addr_and_port = nat_alloc_addr_and_port_mape; |
| 3583 | sm->psid = psid; |
| 3584 | sm->psid_offset = psid_offset; |
| 3585 | sm->psid_length = psid_length; |
Matus Fabian | 5ba86f7 | 2017-10-26 03:37:38 -0700 | [diff] [blame] | 3586 | } |
| 3587 | |
Matus Fabian | 8211954 | 2018-01-25 01:13:22 -0800 | [diff] [blame] | 3588 | void |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 3589 | nat_set_alloc_addr_and_port_range (u16 start_port, u16 end_port) |
| 3590 | { |
| 3591 | snat_main_t *sm = &snat_main; |
| 3592 | |
| 3593 | sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_RANGE; |
| 3594 | sm->alloc_addr_and_port = nat_alloc_addr_and_port_range; |
| 3595 | sm->start_port = start_port; |
| 3596 | sm->end_port = end_port; |
| 3597 | } |
| 3598 | |
| 3599 | void |
Matus Fabian | 8211954 | 2018-01-25 01:13:22 -0800 | [diff] [blame] | 3600 | nat_set_alloc_addr_and_port_default (void) |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 3601 | { |
| 3602 | snat_main_t *sm = &snat_main; |
Matus Fabian | 2769710 | 2017-11-09 01:43:47 -0800 | [diff] [blame] | 3603 | |
Matus Fabian | 5d28c7a | 2018-09-04 03:55:45 -0700 | [diff] [blame] | 3604 | sm->addr_and_port_alloc_alg = NAT_ADDR_AND_PORT_ALLOC_ALG_DEFAULT; |
Matus Fabian | 8211954 | 2018-01-25 01:13:22 -0800 | [diff] [blame] | 3605 | sm->alloc_addr_and_port = nat_alloc_addr_and_port_default; |
Matus Fabian | 066f034 | 2017-02-10 03:48:01 -0800 | [diff] [blame] | 3606 | } |
| 3607 | |
Matus Fabian | ab395ec | 2018-09-20 23:18:41 -0700 | [diff] [blame] | 3608 | /* |
| 3609 | * fd.io coding-style-patch-verification: ON |
| 3610 | * |
| 3611 | * Local Variables: |
| 3612 | * eval: (c-set-style "gnu") |
| 3613 | * End: |
| 3614 | */ |