hostapd vs netplan:启动时未将 wlan0 添加到桥接器

hostapd vs netplan:启动时未将 wlan0 添加到桥接器

和许多人一样,我使用运行 Ubuntu 20.10 arm64 的 Raspberry Pi 4B 作为 LAN/wifi 桥接器,使用 hostapd 和 netplan。一切正常,只是机器重新启动时桥接器配置不正确。 brctl show显示br0仅包含eth0和不是wlan0应有的。但是,在启动后运行netplan apply或添加到桥接器,然后其他一切都正常。systemctl restart systemd-networkdwlan0

/var/log/syslog包括如下信息:

Jan  1 06:09:25 triangle systemd-networkd[1819]: br0: Link UP
Jan  1 06:09:25 triangle systemd-networkd[1819]: wlan0: Could not join netdev: Device does not allow enslaving to a bridge. Operation not supported
Jan  1 06:09:25 triangle systemd-networkd[1819]: wlan0: Failed

然后:

Jan  1 06:09:25 triangle systemd-networkd[1819]: wlan0: Link UP
Jan  1 06:09:25 triangle hostapd[1889]: wlan0: interface state UNINITIALIZED->COUNTRY_UPDATE
Jan  1 06:09:25 triangle systemd-networkd[1819]: wlan0: Gained carrier
Jan  1 06:09:25 triangle kernel: [   21.926114] IPv6: ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready

我认为问题在于,当 netplan 在启动时运行时,wlan0接口处于关闭状态,因此 netplan 无法将其添加到网桥。hostapd配置完成后,接口会启动,但为时已晚。

我已经在 root 的 crontab 中解决了这个问题@reboot sleep 5 ; /usr/bin/systemctl restart systemd-networkd,但是有合适的解决方案吗?

其内容etc/netplan/01-netcfg.yaml为:

network:
  version: 2
  renderer: networkd

  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      wakeonlan: yes

    wlan0:
      optional: true

  bridges:
    br0:
      interfaces: [eth0, wlan0]
      dhcp4: yes
      parameters:
        stp: true
        forward-delay: 4

其内容hostapd.conf为:

interface=wlan0
driver=nl80211
logger_syslog=-1
logger_syslog_level=2
logger_stdout=-1
logger_stdout_level=2
ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
ssid=xxxxxxxxxxxxxxxx
country_code=US
ieee80211d=1
ieee80211h=1
hw_mode=a
channel=161
beacon_int=100
dtim_period=2
max_num_sta=255
rts_threshold=-1
fragm_threshold=-1
macaddr_acl=0
auth_algs=3
ignore_broadcast_ssid=0
wmm_enabled=1
wmm_ac_bk_cwmin=4
wmm_ac_bk_cwmax=10
wmm_ac_bk_aifs=7
wmm_ac_bk_txop_limit=0
wmm_ac_bk_acm=0
wmm_ac_be_aifs=3
wmm_ac_be_cwmin=4
wmm_ac_be_cwmax=10
wmm_ac_be_txop_limit=0
wmm_ac_be_acm=0
wmm_ac_vi_aifs=2
wmm_ac_vi_cwmin=3
wmm_ac_vi_cwmax=4
wmm_ac_vi_txop_limit=94
wmm_ac_vi_acm=0
wmm_ac_vo_aifs=2
wmm_ac_vo_cwmin=2
wmm_ac_vo_cwmax=3
wmm_ac_vo_txop_limit=47
wmm_ac_vo_acm=0
ieee80211n=1
ieee80211ac=1
eapol_key_index_workaround=0
eap_server=0
own_ip_addr=127.0.0.1
wpa=2
wpa_psk_file=/etc/hostapd/hostapd.wpa_psk
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP

答案1

我遇到了同样的问题,启动时将 wlan0 链接设置为客户端模式左右......

查看内核源代码br_if.c#L605这里拒绝桥接功能br_add_if

if (dev->priv_flags & IFF_DONT_BRIDGE) {
        NL_SET_ERR_MSG(extack,
                   "Device does not allow enslaving to a bridge");
        return -EOPNOTSUPP;
}

并且当 WLAN 设备注册无线/核心.c#L1305设置IFF_DONT_BRIDGE标志

    if ((wdev->iftype == NL80211_IFTYPE_STATION ||
         wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
         wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
        wdev->netdev->priv_flags |= IFF_DONT_BRIDGE;

看来 networkdNL80211_IFTYPE_STATION在启动时在模块中注册了 wlan0。

很好!以下是我的决心:

  1. 从 netplan 中删除 wlan0,保留 networkd 未管理的 wlan0 链接。
  2. 然后,您可能会注意到 hostapd.conf 有类似的字段bridge=br0,使用它将 wlan0 桥接到 br0。

就您而言,配置如下:

etc/netplan/01-netcfg.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      wakeonlan: yes
  bridges:
    br0:
      interfaces: [eth0]
      dhcp4: yes
      parameters:
        stp: true
        forward-delay: 4

hostapd.conf

interface=wlan0
driver=nl80211
bridge=br0
logger_syslog=-1
...

相关内容