diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c --- a/sys/netpfil/pf/pf.c +++ b/sys/netpfil/pf/pf.c @@ -1988,14 +1988,14 @@ (*sk)->proto = pd->proto; (*sk)->af = pd->af; - *nk = pf_state_key_clone(*sk); - if (*nk == NULL) { - uma_zfree(V_pf_state_key_z, *sk); - *sk = NULL; - return (ENOMEM); - } - if (pd->af != pd->naf) { + *nk = pf_state_key_clone(*sk); + if (*nk == NULL) { + uma_zfree(V_pf_state_key_z, *sk); + *sk = NULL; + return (ENOMEM); + } + (*sk)->port[pd->sidx] = pd->osport; (*sk)->port[pd->didx] = pd->odport; @@ -2033,6 +2033,8 @@ default: (*nk)->proto = pd->proto; } + } else { + *nk = *sk; } return (0); @@ -6595,7 +6597,8 @@ } } else { uma_zfree(V_pf_state_key_z, ctx.sk); - uma_zfree(V_pf_state_key_z, ctx.nk); + if (ctx.sk != ctx.nk) + uma_zfree(V_pf_state_key_z, ctx.nk); ctx.sk = ctx.nk = NULL; pf_udp_mapping_release(ctx.udp_mapping); } @@ -6622,7 +6625,8 @@ cleanup: uma_zfree(V_pf_state_key_z, ctx.sk); - uma_zfree(V_pf_state_key_z, ctx.nk); + if (ctx.sk != ctx.nk) + uma_zfree(V_pf_state_key_z, ctx.nk); pf_udp_mapping_release(ctx.udp_mapping); *reason = ctx.reason; @@ -6958,7 +6962,8 @@ csfailed: uma_zfree(V_pf_state_key_z, ctx->sk); - uma_zfree(V_pf_state_key_z, ctx->nk); + if (ctx->sk != ctx->nk) + uma_zfree(V_pf_state_key_z, ctx->nk); for (pf_sn_types_t sn_type=0; sn_typensport, pd->ndport, &ctx->sk, &ctx->nk)) return (PFRES_MEMORY); + if (ctx->sk == ctx->nk) { + ctx->nk = pf_state_key_clone(ctx->sk); + if (ctx->nk == NULL) { + uma_zfree(V_pf_state_key_z, ctx->sk); + ctx->sk = NULL; + return (PFRES_MEMORY); + } + } } switch (nat_action) { @@ -1339,7 +1347,8 @@ reason = PFRES_MAX; notrans: uma_zfree(V_pf_state_key_z, ctx->nk); - uma_zfree(V_pf_state_key_z, ctx->sk); + if (ctx->nk != ctx->sk) + uma_zfree(V_pf_state_key_z, ctx->sk); ctx->sk = ctx->nk = NULL; return (reason);