Skip to content

resolved: remove entry from cache when goodbye packet received - #22751

Merged
poettering merged 1 commit into
systemd:mainfrom
vchillara:resolved-goodbye-packet-ttl
Mar 5, 2024
Merged

poettering merged 1 commit into
systemd:mainfrom
vchillara:resolved-goodbye-packet-ttl

Conversation

@vchillara

@vchillara vchillara commented Mar 15, 2022 •

Copy link
Copy Markdown
Contributor

ttl of an incoming mdns goodbye packet is updated to 1 as per RFC6762 10.1. However, in dns_cache_put_positive(), we check for min_ttl <= 0 to remove the record from cache.
As a result, the entries for which goodbye packets are received do not get removed from cache. changed the check to min_ttl <= 1 to account for goodbye packets.

@vchillara vchillara changed the title resolved: TTL of 1 is stored for good bye packet as per RFC6762 10.1.… resolved: remove entry from cache when goodbye packet received Mar 15, 2022
Comment thread src/resolve/resolved-dns-cache.c Outdated
if (min_ttl <= 0) {
/* New TTL is 0? Delete this specific entry...
* In RFC6762 10.1 for Good Bye packet TTL is 1 */
if (min_ttl <= 1) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, as I read the RFC we should instead when min_ttl is zero go through all our matching cached entries and set the stored TTL to 1s.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ttl of incoming goodbye packet records is already being set to 1 before "dns_cache_put_positive" is called (line 399, resolved-mdns.c).

However in dns_cache_put_positive() for cache deletion ttl is checked for 0 instead of 1.
Due to this, records with goodbye packets aren't getting removed from cache.

The RFC says to change the TTL of goodbye packet records to 1, then take a look at them again after a second and delete them if no updates were received.
The current implementation seems to be taking care of setting the TTL to 1, but it doesn't account for the deletion.

This PR takes care of deletion, but ideally according to the RFC we should be waiting for 1 second before doing so.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about the following? When min_ttl == 1, find matching existing positive cache entries and update its TTL and cache lifetime DnsCacheItem.until.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to update the ttl/until of existing matching cache entries to 1 second when we get a TTL==0.
However, there also needs to be some sort of callback after one second to check if the entry was updated in the meantime, and remove the entry if no update was received.
Without this, the entry lingers in the cache indefinitely until the cache is pruned the next time.
We stumbled into this behavior while implementing #22532, with the aim of notifying clients when a service expires.

@poettering poettering added the reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks label Mar 17, 2022
@vchillara
vchillara force-pushed the resolved-goodbye-packet-ttl branch from de26d89 to b732975 Compare June 27, 2022 06:04
@yuwata yuwata added please-review and removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks labels Jun 27, 2022
Comment thread src/resolve/resolved-mdns.c Outdated
/* See the section 10.1 of RFC6762 */
rr->ttl = 1;

r = sd_event_add_time_relative(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not necessary to call dns_cache_prune() after 1 second. As dns_cache_prune() is always called before calling dns_cache_lookup() is called. That is, the cache entry should be removed when we search cached entry.

Now I am confused, why do you want to explicitly remove the cache entry from the cache??
What issue do you want to solve??

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dns_cache_lookup() is only called when a packet is sent, which happens indeterminately.
RFC 6762 section 10.1 does mention that the entry should be deleted form the cache 1 second later.
This helps in keeping the cache up-to-date.
Without this, the entry would linger in cache indefinitely until the next packet is sent.
It also helps to notify the client immediately when a service sends a goodbye packet and expires (#22532).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"the entry should be deleted form the cache 1 second later" does not mean we really need to remove the entry after 1 sec. But it is enough to drop the entry when it is requested later than 1sec.

In #22532, please call dns_cache_prune() before dns_cache_lookup() in mdns_subscriber_lookup_cache().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since goodbye packets are unsolicited, and this logic is actually independent of the RFC6762 section 5.2 stuff implemented in #22532, I figured it would make sense to add here.

@yuwata yuwata added reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks and removed please-review labels Jun 27, 2022
@poettering

Copy link
Copy Markdown
Member

hmm, what i don't get: why have a timer to remove these entries from the cache at all? it should suffice to clean this up when we query the cache, and encounter an entry which is now run over. we generally only clean the cache when using it, but not independently by timer. so why should we do so here?

patching the ttl to 1 should mean that if we then query the cache it the record will be used still within that 1s but once the 1s is hit it won't anymore, even if it still exists in the cache.

what am i missing?

@vchillara

Copy link
Copy Markdown
Contributor Author

@poettering thanks for the review. Working on addressing your feedback.
Regarding your question - does #22751 (comment) answer it?

@poettering

Copy link
Copy Markdown
Member

@poettering thanks for the review. Working on addressing your feedback. Regarding your question - does #22751 (comment) answer it?

Not really. Why would it be beneficial to clean up the cache immediately when the timeout runs out? sounds much better to just do so when we deal with the cache anyway. i.e. usually it makes sense to avoid wakeups for nothing, and instead do all work when we are awake anyway...

@vchillara

Copy link
Copy Markdown
Contributor Author

@poettering thanks for the review. Working on addressing your feedback. Regarding your question - does #22751 (comment) answer it?

Not really. Why would it be beneficial to clean up the cache immediately when the timeout runs out? sounds much better to just do so when we deal with the cache anyway. i.e. usually it makes sense to avoid wakeups for nothing, and instead do all work when we are awake anyway...

I see your point about avoiding unnecessary wakeups, but in the context of browsing services, we would like to keep the client up to date. Since we notify them on addition of services, we also want to notify them when a service expires.
Besides, the logic of removing cache entries with a timer would only be active while there is an active subscriber.
What do you think?

@poettering

Copy link
Copy Markdown
Member

ah, hmm, yeah in the contetx of browsing it indeed makes sense to pro-actively clean up the cache when TTL runs out. But please add a comment about that clarifying this.

@vchillara
vchillara force-pushed the resolved-goodbye-packet-ttl branch from b732975 to a6efbdc Compare July 10, 2023 07:36
@github-actions github-actions Bot added please-review PR is ready for (re-)review by a maintainer and removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks labels Jul 10, 2023
@github-actions

Copy link
Copy Markdown

An -rc1 tag has been created and a release is being prepared, so please note that PRs introducing new features and APIs will be held back until the new version has been released.

@vchillara

Copy link
Copy Markdown
Contributor Author

@poettering Added a comment clarifying the need for this.
One thing - in some of the earlier comments on this PR, there was a request to move this functionality to #22532, which we did.
So the question is do we now want it here or in the other PR?

Comment thread src/resolve/resolved-mdns.c Outdated
/* Look at the cache 1 second later and remove stale entries.
* This is particularly useful to keep service browsers updated on service removal,
* as there are no other reliable triggers to propogate that info. */
r = sd_event_add_time_relative(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this will create one event source for each incoming mdns packet with ttl set to 0. that is a chance for a DoS, as clients can make us allocate basically unbounded memory, if they send us a ton of these packets within 1s.

Thus the event source object should be stored somewhere (in the scope object?), and if it already exists no new event source should be allocated. morevoer, the handler should check if there are RRs in the cache that should be flushed within less than 1s, and if so should immediately requeue a new event source to cover for that.

That way there's only one event source enqueued at max.

Comment thread src/resolve/resolved-mdns.c Outdated
USEC_PER_SEC,
0,
mdns_goodbye_callback,
scope);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will result in memory corruption: let's say a scope goes away (because a network interface goes away), but the event source is still queued. it will be called pointing to a scope that doesn't exist anymore. Bad things will then happen.

if you embedd a pointer to the eent source in DnsScope object, then you can also delete the event source whenever the scope goes away.

@poettering poettering added reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks and removed please-review PR is ready for (re-)review by a maintainer labels Nov 7, 2023
@vchillara
vchillara force-pushed the resolved-goodbye-packet-ttl branch from a6efbdc to 46660eb Compare November 17, 2023 07:42
@github-actions github-actions Bot added please-review PR is ready for (re-)review by a maintainer and removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks labels Nov 17, 2023
Comment thread src/resolve/resolved-dns-cache.h Resolved
* This is particularly useful to keep service browsers updated on service removal,
* as there are no other reliable triggers to propogate that info. */
if (!scope->mdns_goodbye_event_source) {
r = sd_event_add_time_relative(

@yuwata yuwata Nov 20, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still I cannot follow why we need to actively clear the outdated cache entries here...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the context of continuous browsing (#22532), it would be useful to actively clear the outdated cache entries and notify the browser client(s).
To accomplish that, we would eventually add a call to mdns_notify_browsers_unsolicited_updates()
from mdns_goodbye_callback().

@yuwata

yuwata commented Nov 20, 2023

Copy link
Copy Markdown
Member

-> #30096 ?

RFC6762 10.1 says that queriers receiving a Multicast DNS response with a TTL
of zero SHOULD record a TTL of 1 and then delete the record one second later.

Added a timer event to trigger a callback to clean-up the cache one second after
a goodbye packet is received. The callback also checks for any cache entries
expiring within the next one second and schedules follow-up cleanup callbacks
accordingly.
@vchillara
vchillara force-pushed the resolved-goodbye-packet-ttl branch from 46660eb to cba136f Compare November 20, 2023 06:34
@poettering
poettering merged commit d08566f into systemd:main Mar 5, 2024
@github-actions github-actions Bot removed the please-review PR is ready for (re-)review by a maintainer label Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

3 participants