Hi, Why do you want to initialize `ipa’ variable in ipsp_pending_acquire() and ipsec_get_acquire()? This doesn’t require.
> On 14 Mar 2022, at 13:09, Alexandr Nedvedicky > <[email protected]> wrote: > > Hello, > > </snip> > > changes looks good. just few nits. > > I took a closer look at ipsec_delete_policy(): > > 667 ipsec_delete_policy(struct ipsec_policy *ipo) > > 668 { > 669 struct ipsec_acquire *ipa; > 670 struct radix_node_head *rnh; > 671 struct radix_node *rn = (struct radix_node *)ipo; > > 672 > 673 NET_ASSERT_LOCKED(); > > 674 > 675 if (refcnt_rele(&ipo->ipo_refcnt) == 0) > > 676 return 0; > > 677 > 678 /* Delete from SPD. */ > 679 if ((rnh = spd_table_get(ipo->ipo_rdomain)) == NULL || > 680 rn_delete(&ipo->ipo_addr, &ipo->ipo_mask, rnh, rn) == NULL) > > 681 return (ESRCH); > > the suggestion is not related to your changes, but I think it's worth > to consider as a different diff. I think we should either panic() > at line 681 or we should at least log error. If I understand code > right we delete policy (ipo), which has been fully created so this > error should not happen here. > > more comments in-line > > thanks and > regards > sashan > >> Index: netinet/ip_spd.c >> =================================================================== >> RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/ip_spd.c,v >> retrieving revision 1.114 >> diff -u -p -r1.114 ip_spd.c > </snip> >> @@ -717,13 +720,38 @@ ipsp_delete_acquire_timo(void *v) >> void >> ipsp_delete_acquire(struct ipsec_acquire *ipa) >> { >> - NET_ASSERT_LOCKED(); >> + mtx_enter(&ipsec_acquire_mtx); >> + ipsp_delete_acquire_locked(ipa); >> + mtx_leave(&ipsec_acquire_mtx); >> +} >> + >> +void >> +ipsp_delete_acquire_locked(struct ipsec_acquire *ipa) >> +{ >> + if (timeout_del(&ipa->ipa_timeout) == 1) >> + refcnt_rele(&ipa->ipa_refcnt); > ^^^^^^^^^^^^^^^^^^ > can we also put ASSERT/check into this branch > to verify we are no releasing the last > reference to ipa. I suspect we might be doing > an extra reference drop here. I believe this ASSERT > would be hit if we will compile kernel without IPSEC. > we grab the extra reference iff are adding a timer. > > > I think this whole ipsp_delete_acquire() logic should > be slightly different. We should call ipsp_unref_acquire_locked(), > when timeout_del() returns 1, because function managed to unschedule > the pending timer. If it returns zero, we should leave things as > they are because timer is running already, waiting for > ipsec_acquire_mtx. As soon as timer will grab mutex, it will > drop the reference for ipa. That's my understanding. > if we go this way then we should not need to grab extra > reference for timer. > >> + ipsp_unref_acquire_locked(ipa); >> +} >> + > </snip> > >> @@ -731,19 +759,21 @@ ipsp_delete_acquire(struct ipsec_acquire >> * Find out if there's an ACQUIRE pending. >> * XXX Need a better structure. >> */ >> -struct ipsec_acquire * >> +int >> ipsp_pending_acquire(struct ipsec_policy *ipo, union sockaddr_union *gw) >> { >> struct ipsec_acquire *ipa; > ^^^^^^^^^^^ > this needs to be initialized to NULL >> >> NET_ASSERT_LOCKED(); >> >> - TAILQ_FOREACH (ipa, &ipo->ipo_acquires, ipa_ipo_next) { >> + mtx_enter(&ipsec_acquire_mtx); >> + TAILQ_FOREACH(ipa, &ipo->ipo_acquires, ipa_ipo_next) { >> if (!memcmp(gw, &ipa->ipa_addr, gw->sa.sa_len)) >> - return ipa; >> + break; >> } >> + mtx_leave(&ipsec_acquire_mtx); >> >> - return NULL; >> + return (ipa != NULL); >> } >> > </snip> >> @@ -850,13 +881,14 @@ ipsp_acquire_sa(struct ipsec_policy *ipo >> return 0; >> } >> >> + mtx_enter(&ipsec_acquire_mtx); >> #ifdef IPSEC >> - timeout_add_sec(&ipa->ipa_timeout, ipsec_expire_acquire); >> + if (timeout_add_sec(&ipa->ipa_timeout, ipsec_expire_acquire) == 1) >> refcnt_take(&ipa->ipa_refcnt); > ^^^^^^^^^^^^^^^^^^^^^^^ > line is too long >> #endif >> - >> TAILQ_INSERT_TAIL(&ipsec_acquire_head, ipa, ipa_next); >> TAILQ_INSERT_TAIL(&ipo->ipo_acquires, ipa, ipa_ipo_next); >> ipa->ipa_policy = ipo; >> + mtx_leave(&ipsec_acquire_mtx); >> >> /* PF_KEYv2 notification message. */ >> return pfkeyv2_acquire(ipo, gw, laddr, &ipa->ipa_seq, ddst); >> @@ -908,9 +940,14 @@ ipsec_get_acquire(u_int32_t seq) >> > local variable ipa, needs to be initialized to NULL here as well. >> NET_ASSERT_LOCKED(); >> >> - TAILQ_FOREACH (ipa, &ipsec_acquire_head, ipa_next) >> - if (ipa->ipa_seq == seq) >> - return ipa; >> + mtx_enter(&ipsec_acquire_mtx); >> + TAILQ_FOREACH(ipa, &ipsec_acquire_head, ipa_next) { >> + if (ipa->ipa_seq == seq) { >> + refcnt_take(&ipa->ipa_refcnt); >> + break; >> + } >> + } >> + mtx_leave(&ipsec_acquire_mtx); >> >> - return NULL; >> + return ipa; >> }
