LXR | KVM | PM | Time | Interrupt | Systems Performance | Bootup Optimization

OpenWRT(13):dnsmasq和odhcpd

dnsmasq 是一个轻量级的DNS和DHCP,它通常用于小型网络。

odhcpd 是OpenWrt中用于IPv6的DHCP和RA(Router Advertisement)服务的守护进程。

1 dnsmasq

dnsmasq是DNS masquerade的意思,

dnsmasq是一个为小型网络提供DNS、DHCP和网络启动服务的轻量级工具。

更多参考《[OpenWrt Wiki] Dnsmasq DHCP 服务器》《[OpenWrt Wiki] Dnsmasq DHCP server》。

1.1 dnsmasq配置

 dnsmasq包含3中配置:DNS/DHCP、DNS/DHCPv6、DNS/DHCP/DHCPv6。

Base system
  dnsmasq--包含DNS和DHCP服务。
  dnsmasq-dhcpv6--包含DNS和DHCPv6服务。
  dnsmasq-full--完整的DNS和DHCP/DHCPv6服务。
    Build with DHCP support.
     Build with DHCPv6 support.
    Build with DNSSEC support.--域名系统安全扩展(DNSSEC)是一种对 DNS 协议的扩展,它提供了 DNS 数据的完整性和可认证性。`dnsmasq` 支持 DNSSEC,可以验证 DNS 响应的数字签名,从而保护用户免受 DNS 劫持和欺骗攻击。
    Build with the facility to act as an authoritative DNS server.
    Build with IPset support.--ipset是Linux内核提供的一种机制,用于定义和存储IP地址或地址范围的集合。
    Build with Conntrack support.--Linux 内核中的conntrack(连接跟踪)机制用于追踪穿过网络接口的连接状态。
    Build with NO_ID. (hide *.bind pseudo domain)
    Build with HAVE_BROKEN_RTC.
    Build with TFTP server support.--dnsmasq内置了一个TFTP服务器,可以用于网络引导或其他文件传输需求。

1.2 dnsmasq使用

Usage: dnsmasq [options]

Valid options are:
-a, --listen-address=<ipaddr>                          Specify local address(es) to listen on.
-A, --address=/<domain>/<ipaddr>                       Return ipaddr for all hosts in specified domains.
-b, --bogus-priv                                       Fake reverse lookups for RFC1918 private address ranges.
-B, --bogus-nxdomain=<ipaddr>                          Treat ipaddr as NXDOMAIN (defeats Verisign wildcard).
-c, --cache-size=<integer>                             Specify the size of the cache in entries (defaults to 150).
-C, --conf-file=<path>                                 Specify configuration file (defaults to /etc/dnsmasq.conf).
-d, --no-daemon                                        Do NOT fork into the background: run in debug mode.
...
-k, --keep-in-foreground                               Do NOT fork into the background, do NOT run in debug mode.
-K, --dhcp-authoritative                               Assume we are the only DHCP server on the local network.
-l, --dhcp-leasefile=<path>                            Specify where to store DHCP leases (defaults to /var/lib/misc/dnsmasq.leases).
...
-w, --help                                             Display this message. Use --help dhcp or --help dhcp6 for known DHCP options.
-x, --pid-file=<path>                                  Specify path of PID file (defaults to /var/run/dnsmasq.pid).
-X, --dhcp-lease-max=<integer>                         Specify maximum number of DHCP leases (defaults to 1000).
-y, --localise-queries                                 Answer DNS queries based on the interface a query was sent to.
...

1.3 dnsmasq启动

通过/etc/rc.d/S19dnsmasq:

  • 根据/etc/config/dhcp生成/var/etc/dnsmasq.conf.cfg01411c配置文件。也可以同时在 /etc/dnsmasq.conf 文件里配置。
  • 启动dnsmasq提供DNS和DHCP服务。
/sbin/ujail -t 5 -n dnsmasq -u -l -r /bin/ubus -r /etc/TZ -r /etc/dnsmasq.conf -r /etc/ethers -r /etc/group -r /etc/hosts -r /etc/passwd -w /tmp/dhcp.leases -r /tmp/dnsmasq.d -r /tmp/hosts -r /tmp/resolv.conf.d -r /usr/bin/jshn -r /us
  /usr/sbin/dnsmasq -C /var/etc/dnsmasq.conf.cfg01411c -k -x /var/run/dnsmasq/dnsmasq.cfg01411c.pid

/etc/config/dhcp中关于dnsmasq配置如下:

config dnsmasq
        option domainneeded '1'
        option boguspriv '1'
        option filterwin2k '0'
        option localise_queries '1'
        option rebind_protection '1'
        option rebind_localhost '1'
        option local '/lan/'
        option domain 'lan'
        option expandhosts '1'
        option nonegcache '0'
        option authoritative '1'
        option readethers '1'
        option leasefile '/tmp/dhcp.leases'
        option resolvfile '/tmp/resolv.conf.d/resolv.conf.auto'
        option nonwildcard '1'
        option localservice '1'
        option ednspacket_max '1232'

生成的配置文件:

# auto-generated config file from /etc/config/dhcp
conf-file=/etc/dnsmasq.conf
dhcp-authoritative
domain-needed
localise-queries
read-ethers
enable-ubus=dnsmasq
expand-hosts
bind-dynamic
local-service
edns-packet-max=1232
domain=lan
local=/lan/
addn-hosts=/tmp/hosts
dhcp-leasefile=/tmp/dhcp.leases
resolv-file=/tmp/resolv.conf.d/resolv.conf.auto
stop-dns-rebind
rebind-localhost-ok
dhcp-broadcast=tag:needs-broadcast
conf-dir=/tmp/dnsmasq.d
user=dnsmasq
group=dnsmasq

dhcp-ignore-names=tag:dhcp_bogus_hostname
conf-file=/usr/share/dnsmasq/dhcpbogushostname.conf

bogus-priv
conf-file=/usr/share/dnsmasq/rfc6761.conf
dhcp-range=set:lan,192.168.1.100,192.168.1.249,255.255.255.0,12h

其他的DNS相关配置还包括:/etc/hosts和/etc/ethers。

2 odhcpd

odhcpd提供DHCP、RA、stateless SLAAC和stateful DHCPv6服务:

  • Router Discovery (RD):是 IPv6 中的一个功能,它允许主机发现网络上的路由器以及网络配置参数。
    • RD Server 模式:在从属接口上作为 RD 服务器,自动检测前缀、委托前缀、默认路由和 MTU,并自动重新宣告任何前缀或路由的变化。
    • RD Relay模式:在主接口和从属接口之间作为 RD 中继,支持重写宣布的 DNS 服务器地址。
  • DHCPv6:是用于 IPv6 网络的动态主机配置协议,它允许自动分配和管理 IPv6 地址以及其他网络配置参数。
    • DHCPv6 服务器模式:
      • Stateless DHCPv6:在无状态DHCPv6中,客户端从服务器获取除了 IP 地址以外的其他配置信息,如 DNS 服务器地址、域名等。客户端使用 SLAAC(无状态地址自动配置)来生成自己的 IPv6 地址,而 DHCPv6 用于获取其他配置参数。
      • Stateful DHCPv6:有状态DHCPv6服务为客户端提供完整的IP地址配置,包括IPv6地址和网络配置参数。这与IPv4网络中的DHCP类似,客户端的IP地址和其他配置都是由DHCP服务器分配和管理的。
      • Prefix Delegation:前缀委托是 IPv6 网络中的一个特性,允许一个网络节点(如 odhcpd 运行的路由器)从上游 ISP 动态获取一个或多个 IPv6 网络前缀,并将这些前缀进一步委托给下游网络。odhcpd 支持前缀委托,使得路由器可以从 ISP 获取更大的地址空间,并将其分配给本地网络。
    • DHCPv6中继模式:作为 DHCPv6 中继,在不同网络段之间转发 DHCPv6 消息。支持重写宣布的 DNS 服务器地址。
  • DHCPv4:支持无状态和有状态DHCPv4服务器模式。
  • RA(Router Advertisement):是IPv6网络中的一个协议,用于路由器向主机通告网络配置信息,如网络前缀、默认网关、网络参数等。odhcpd 可以处理 RA 消息,以便在网络中正确地传播这些信息。
  • Neighbor Discovery Proxy (NDP):代理邻居发现请求和通告消息,支持自动学习路由到本地路由表,支持将接口标记为“外部”,这些接口不会接收任何代理的NDP内容,只服务于重复地址检测(DAD)和流量到路由器本身。

  • Stateless SLAAC(无状态地址自动配置):SLAAC 允许 IPv6 主机在没有服务器的情况下自动配置自己的地址。主机使用网络前缀和自己的接口标识符生成自己的 IPv6 地址。odhcpd支持 SLAAC,可以通告网络前缀,使主机能够进行 SLAAC。
  • Relay of RA, DHCPv6, and NDP:odhcpd 可以在不同网络接口之间中继 RA、DHCPv6 和 NDP 消息。这在没有委托前缀可用的情况下特别有用,因为它允许路由器在不同网络段之间转发这些消息,确保 IPv6 流量能够正确路由。

更多參考《[OpenWrt Wiki] odhcpd》。

2.1 odhcpd配置

odhcpd包括两个配置:一个是DHCP和DHCPv6;另一个仅包含DHCPv6。

Network
  odhcpd--提供DHCP和DHCPv6服务。
  odhcpd-ipv6only--提供DHCPv6服务。

 2.2 odhcpd启动

通过/etc/rc.d/S35odhcpd启动odhcpd,提供DHCP服务。

2.3 odhcpd的配置文件

odhcpd 服务的配置是通过 UCI(Unified Configuration Interface)配置文件来管理的。UCI 提供了一个用于管理配置文件的标准化方法,这些配置文件通常位于 /etc/config/ 目录下。对于 odhcpd,其主要配置文件是 /etc/config/dhcp。

/etc/config/dhcp
...
config dhcp 'lan'--关于dhcp配置详细参考《dhcp section》。
        option interface 'lan'
        option start '100'
        option limit '150'
        option leasetime '12h'
        option dhcpv4 'server'
        option dhcpv6 'server'
        option ra 'server'
        option ra_slaac '1'
        list ra_flags 'managed-config'
        list ra_flags 'other-config'

config dhcp 'wan'
        option interface 'wan'
        option ignore '1'

config odhcpd 'odhcpd'--关于odhcpd配置详细参考《odhcpd section》。
        option maindhcp '0'
        option leasefile '/tmp/hosts/odhcpd'
        option leasetrigger '/usr/sbin/odhcpd-update'
        option loglevel '4'

3 dnsmasq和odhcpd异同点

  • dnsmasq通常用于IPv4环境,而odhcpd更专注于IPv6环境,尽管两者都支持DHCP服务。
  • dnsmasq提供了DNS缓存和TFTP服务,而odhcpd专注于IP管理协议。
  • dnsmasq可以通过安装dnsmasq-full版本来支持IPset,这对于需要基于域名进行流量分流的用户来说是一个重要的特性。
  • odhcpd提供了对Router Discovery和Neighbor Discovery Protocol的支持,这些是IPv6特有的功能。

posted on 2024-08-30 23:59  ArnoldLu  阅读(55)  评论(0编辑  收藏  举报

导航