Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dnsmasq: Enable host networking #3955

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sleiner
Copy link

@sleiner sleiner commented Feb 25, 2025

This patch enables host networking mode for the dnsmasq addon.

Why is this helpful?

As per the addon's description, it allows users to assign custom domain names to IP addresses in their network, e.g. home-assistant.example.com.
When starting to use this feature, I have hit a limitation that felt quite frustrating:

I have various other hosts in my network that I would like to assign names, such as nas.example.com.
The dnsmasq addon resolves these without issue, from anywhere outside of Home Assistant OS. From inside HAOS, and notably from Home Assistant itself, I cannot use that domain name (e.g. for adding the NAS's SMB share as a backup location).

If my Home Assistant OS has the IP addresses 192.0.2.23 inside my LAN, I can access the DNS resolver at that IP from my laptop just fine, but not so inside HAOS. If I try, I get outputs like this:

> dig nas.example.com

; <<>> DiG 9.18.33 <<>> nas.example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 37159
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 37b052b040bae689 (echoed)
;; QUESTION SECTION:
;nas.example.com.		IN	A

;; AUTHORITY SECTION:
example.com.		600	IN	SOA	ns.icann.org. noc.dns.icann.org. 2025011553 7200 3600 1209600 3600

;; Query time: 135 msec
;; SERVER: 172.30.32.3#53(172.30.32.3) (UDP)
;; WHEN: Tue Feb 25 23:15:48 CET 2025
;; MSG SIZE  rcvd: 132

Just running dig bypasses the names defined in dnsmasq. On the other hand, if I try to access the server directly via the IP that other devices on my home network use for DNS, I get "connection refused":

> dig @192.0.2.23 nas.example.com
;; communications error to 192.0.2.23#53: connection refused
;; communications error to 192.0.2.23#53: connection refused
;; communications error to 192.0.2.23#53: connection refused

; <<>> DiG 9.18.33 <<>> @192.0.2.23 nas.example.com
; (1 server found)
;; global options: +cmd
;; no servers could be reached

I have found multiple Home Assistant community threads which likely refer to the same issue:

Activating host_network fixes these issues:

> dig nas.example.com

; <<>> DiG 9.18.33 <<>> nas.example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24463
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
; COOKIE: 9df3c10872739d34 (echoed)
;; QUESTION SECTION:
;nas.example.com.		IN	A

;; ANSWER SECTION:
nas.example.com.	5	IN	A	192.0.2.42

;; Query time: 3 msec
;; SERVER: 172.30.32.3#53(172.30.32.3) (UDP)
;; WHEN: Tue Feb 25 23:39:27 CET 2025
;; MSG SIZE  rcvd: 95

Any other references?

Before testing this change to the dnsmasq addon, I tried a similar setup with the AdGuard Home Addon, which does not have this problem. It also has host_network enabled.

Summary by CodeRabbit

  • New Features
    • Introduced an option that allows the DNS service to leverage the host’s network stack, which may influence network interface interactions and routing.

This patch enables host networking mode for the dnsmasq addon.

## Why is this helpful?

As per the addon's description, it allows users to assign
custom domain names to IP addresses in their network, e.g.
`home-assistant.example.com`.
When starting to use this feature, I have hit a limitation
that felt quite frustrating:

I have various other hosts in my network that I would like
to assign names, such as `nas.example.com`.
The dnsmasq addon resolves these without issue, from anywhere
_outside_ of Home Assistant OS. From inside HAOS, and notably
from Home Assistant itself, I cannot use that domain name
(e.g. for adding the NAS's SMB share as a backup location).

If my Home Assistant OS has the IP addresses 192.0.2.23 inside
my LAN, I can access the DNS resolver at that IP from my
laptop just fine, but not so inside HAOS. If I try, I get
outputs like this:

    > dig nas.example.com

    ; <<>> DiG 9.18.33 <<>> nas.example.com
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 37159
    ;; flags: qr rd ra ad; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

    ;; OPT PSEUDOSECTION:
    ; EDNS: version: 0, flags:; udp: 1232
    ; COOKIE: 37b052b040bae689 (echoed)
    ;; QUESTION SECTION:
    ;nas.example.com.		IN	A

    ;; AUTHORITY SECTION:
    example.com.		600	IN	SOA	ns.icann.org. noc.dns.icann.org. 2025011553 7200 3600 1209600 3600

    ;; Query time: 135 msec
    ;; SERVER: 172.30.32.3#53(172.30.32.3) (UDP)
    ;; WHEN: Tue Feb 25 23:15:48 CET 2025
    ;; MSG SIZE  rcvd: 132

Just running dig bypasses the names defined in dnsmasq. On
the other hand, if I try to access the server directly via
the IP that other devices on my home network use for DNS,
I get "connection refused":

    > dig @192.0.2.23 nas.example.com
    ;; communications error to 192.0.2.23#53: connection refused
    ;; communications error to 192.0.2.23#53: connection refused
    ;; communications error to 192.0.2.23#53: connection refused

    ; <<>> DiG 9.18.33 <<>> @192.0.2.23 nas.example.com
    ; (1 server found)
    ;; global options: +cmd
    ;; no servers could be reached

I have found multiple Home Assistant community threads which
likely refer to the same issue:

-   https://community.home-assistant.io/t/can-home-assistant-os-use-itself-for-dns/552718
-   https://community.home-assistant.io/t/dnsmasq-configuraton/396360

Activating `host_network` fixes these issues:

    > dig nas.example.com

    ; <<>> DiG 9.18.33 <<>> nas.example.com
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24463
    ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

    ;; OPT PSEUDOSECTION:
    ; EDNS: version: 0, flags:; udp: 1232
    ; COOKIE: 9df3c10872739d34 (echoed)
    ;; QUESTION SECTION:
    ;nas.example.com.		IN	A

    ;; ANSWER SECTION:
    nas.example.com.	5	IN	A	192.0.2.42

    ;; Query time: 3 msec
    ;; SERVER: 172.30.32.3#53(172.30.32.3) (UDP)
    ;; WHEN: Tue Feb 25 23:39:27 CET 2025
    ;; MSG SIZE  rcvd: 95

## Any other references?

Before testing this change to the `dnsmasq` addon, I tried a
similar setup with the [AdGuard Home Addon], which does not
have this problem. It also has `host_network` enabled.

[AdGuard Home Addon]: https://github.com/hassio-addons/addon-adguard-home
Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

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

Hi @sleiner

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft February 25, 2025 22:47
@sleiner sleiner marked this pull request as ready for review February 25, 2025 22:49
Copy link
Contributor

coderabbitai bot commented Feb 25, 2025

📝 Walkthrough

Walkthrough

This update adds a new configuration option, host_network: true, to the dnsmasq/config.yaml file. This option enables the DNS server to utilize the host's network stack while keeping the overall file structure intact.

Changes

File(s) Change Summary
dnsmasq/config.yaml Added new configuration option host_network: true to enable usage of the host's network stack.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1ca5f12 and cc69481.

📒 Files selected for processing (1)
  • dnsmasq/config.yaml (1 hunks)
🧰 Additional context used
📓 Path-based instructions (6)
`*/**(html|markdown|md)`: - For instructional content in doc...

*/**(html|markdown|md): - For instructional content in documentation, use a direct and authoritative tone. Avoid expressions of politeness such as 'may' or 'please', and ensure the goal of the instruction is fronted.

  • Apply the Microsoft Style Guide to ensure documentation maintains clarity and conciseness.
  • In step-by-step instructions, front the location phrase in the instructional sentence.
  • In step-by-step instructions, front the 'goal' in the instructional sentence.
  • In step-by-step instructions, if in doubt what to front, front the 'goal' before the location phrase in the instructional sentence.
  • do not hyphenate terms like 'top-right' or 'bottom-left' with 'corner'
  • dnsmasq/config.yaml
`*/**(html|markdown|md)`: - Use bold to mark UI strings. - I...

*/**(html|markdown|md): - Use bold to mark UI strings.

  • If "" are used to mark UI strings, replace them by bold.
  • dnsmasq/config.yaml
`*/**(html|markdown|md)`: - Be brief in your replies and don...

*/**(html|markdown|md): - Be brief in your replies and don't add fluff like "thank you for..." and "Please let me know if"

  • dnsmasq/config.yaml
`*/**(html|markdown|md)`: - Use sentence-style capitalizatio...

*/**(html|markdown|md): - Use sentence-style capitalization also in headings.

  • dnsmasq/config.yaml
`*/**(html|markdown|md)`: do not comment on HTML used for ic...

*/**(html|markdown|md): do not comment on HTML used for icons

  • dnsmasq/config.yaml
`*/**(html|markdown|md)`: Avoid flagging inline HTML for emb...

*/**(html|markdown|md): Avoid flagging inline HTML for embedding videos in future reviews for this repository.

  • dnsmasq/config.yaml
🔇 Additional comments (1)
dnsmasq/config.yaml (1)

16-16: Enable host networking configuration

The addition of host_network: true enables the DNS service to utilize the host's network stack, addressing the issue where DNS queries from Home Assistant OS returned NXDOMAIN. Confirm that this configuration aligns with your overall network security and usage policies.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@agners
Copy link
Member

agners commented Feb 26, 2025

So technically, we expose the relevant ports using Docker functionality. I'd expect that it doesn't matter if we connect from network or locally, the requests should end up with dnsmasq.

I've tried to reproduce the problem you are reporting on a native generic-x86-64 HAOS 15.0.dev20250218 installation. And for me it seems to work:

homeassistant:/config# dig @192.168.1.9 home.mydomain.io

; <<>> DiG 9.18.33 <<>> @192.168.1.9 home.mydomain.io
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62986
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;home.mydomain.io.              IN      A

;; ANSWER SECTION:
home.mydomain.io.       0       IN      A       192.168.1.10

;; Query time: 0 msec
;; SERVER: 192.168.1.9#53(192.168.1.9) (UDP)
;; WHEN: Wed Feb 26 15:23:52 CET 2025
;; MSG SIZE  rcvd: 61

What system are you running Home Assistant on? 🤔

@sleiner
Copy link
Author

sleiner commented Feb 26, 2025

Hi @agners,

thanks for the quick reply! 😊 I am running HAOS 14.2 on a HA Yellow with a CM4 inside. I did run dig through the Terminal & SSH addon, which means I am also running it from a container (I think?), not on the host directly.

@agners
Copy link
Member

agners commented Feb 26, 2025

thanks for the quick reply! 😊 I am running HAOS 14.2 on a HA Yellow with a CM4 inside. I did run dig through the Terminal & SSH addon, which means I am also running it from a container (I think?), not on the host directly.

I see, yeah that doesn't work for me either.

It is possible to access the DNS server from the Home Assistant Core container though, since that one is running in host network too.

However, it isn't possible to access from the DNS plug-in, hence it can't work if you set the DNS server using ha dns options --servers dns://<public-ip> 😥

However, what does work is using the hassio bridge's host IP, which is static 172.30.32.1.

Simply change dnsmasq add-on to use host network does seem a bit harsh to me, it kinda removes all network isolation we have thanks to Docker.

@agners
Copy link
Member

agners commented Feb 26, 2025

That said, I am actually not sure if what you try to do is a good idea in first place: If the DNS plug-in (CoreDNS) uses the add-on as primary DNS, then the system can't resolve names until the add-on has been started. But the system itself does use DNS to check things (like if there are updates).

So this puts the system in a chicken-egg situation, especially during startup.

@sleiner
Copy link
Author

sleiner commented Feb 27, 2025

However, it isn't possible to access from the DNS plug-in, hence it can't work if you set the DNS server using ha dns options --servers dns://<public-ip> 😥

However, what does work is using the hassio bridge's host IP, which is static 172.30.32.1.

Interesting! I can confirm that it works in my setup 👍🏻 It's still very unexpected: I set my router to announce the HAOS instance as DNS server via DHCP, which works everywhere but for the HAOS machine 😅

I am actually not sure if what you try to do is a good idea in first place: If the DNS plug-in (CoreDNS) uses the add-on as primary DNS, then the system can't resolve names until the add-on has been started. But the system itself does use DNS to check things (like if there are updates).

I tried restarting HAOS a couple times (both with the Dnsmasq addon as it is and the DNS plugin's upstream server set to the bridge IP and with Dnsmasq in host networking mode): everything came up mostly fine. I assume that the DNS plugin's fallback mechanism is helping here.

HA Core came up without issues, integrations using custom dnsmasq domain names (such as custom_homematic) also worked fine, presumably due to retry logic. The only thing that showed up where I had to manually retrigger a retry was the supervisor's SMB mount for the NAS.

Simply change dnsmasq add-on to use host network does seem a bit harsh to me, it kinda removes all network isolation we have thanks to Docker.

If I understand the Docker docs correctly here, keeping host networking disabled lets Docker act like a firewall layer, right?
I initially assumed that the isolation is not necessary here, since the AdGuard Home addon does the same thing. On the other hand, another layer of security might be worth users requiring a workaround in this (probably very niche) use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants