summaryrefslogtreecommitdiffstats
path: root/src/plugins/nat/nat_det.c
diff options
context:
space:
mode:
authorKlement Sekera <ksekera@cisco.com>2020-06-24 13:13:46 +0000
committerOle Trøan <otroan@employees.org>2020-06-25 08:58:26 +0000
commitfe77bdc1906cca6a76bd44b1aceffc971f64cec4 (patch)
tree4a10a7321e72cab239a3eacfe2778004b831c1bc /src/plugins/nat/nat_det.c
parent8eca60df745b7656b96db16e75b0deda66bfc515 (diff)
nat: avoid division by zero
Return error instead of dividing by zero. Type: fix Signed-off-by: Klement Sekera <ksekera@cisco.com> Change-Id: I9f6a942e87ab87e8f1921e744ec1add45884e74a
Diffstat (limited to 'src/plugins/nat/nat_det.c')
-rw-r--r--src/plugins/nat/nat_det.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/plugins/nat/nat_det.c b/src/plugins/nat/nat_det.c
index cad0d2ae5af..65a669bceb9 100644
--- a/src/plugins/nat/nat_det.c
+++ b/src/plugins/nat/nat_det.c
@@ -82,13 +82,20 @@ snat_det_add_map (snat_main_t * sm, ip4_address_t * in_addr, u8 in_plen,
num_sessions = num_sessions * 1000 - 1;
}
+ u32 sharing_ratio = (1 << (32 - in_plen)) / (1 << (32 - out_plen));
+ if (!sharing_ratio)
+ {
+ // avoid division by zero
+ return VNET_API_ERROR_INVALID_VALUE;
+ }
+
pool_get (sm->det_maps, det_map);
clib_memset (det_map, 0, sizeof (*det_map));
det_map->in_addr.as_u32 = in_cmp.as_u32;
det_map->in_plen = in_plen;
det_map->out_addr.as_u32 = out_cmp.as_u32;
det_map->out_plen = out_plen;
- det_map->sharing_ratio = (1 << (32 - in_plen)) / (1 << (32 - out_plen));
+ det_map->sharing_ratio = sharing_ratio;
det_map->ports_per_host = (65535 - 1023) / det_map->sharing_ratio;
vec_validate_init_empty (det_map->sessions, num_sessions,