Netifd network add_dynamic for static if configuration

Hi,

I'm trying to add and configure network interfaces at runtime, this works perfect for dhcp:

json_init
json_add_string name "${interface}"
json_add_string ifname "${interface}"
json_add_string proto "dhcp"
json_add_string zone "wan"
json_add_int metric "${metric}"
ubus call network add_dynamic "$(json_dump)"

but I'm having problems with interfaces that shall be statically configured. I'm trying this:

json_init
json_add_string name "${interface}"
json_add_string ifname "${interface}"
json_add_string proto "static"
json_add_string zone "wan"
json_add_string ipaddr "${address}"
json_add_string netmask "${mask}"
json_add_string gateway "${gateway}"
json_add_int metric "${metric}"
ubus call network add_dynamic "$(json_dump)"

But the interface is not configured, no network is added to netifd, and no error is returned by ubus.
Is there a (better?) way to do this dynamically?

In the shell you do it like this:

  • Interface section name affects firewall, so better make it predictable such as wan.
  • Clean the current settings to avoid collision:
uci -q delete network.wan
  • Configure the interface:
uci set network.wan="interface"
...
  • Restart/reload the service to apply the new settings.
/etc/init.d/network restart

I don't want to do this in the settings, actually I want to add interfaces ad-hoc from a script while runtime, without touching the configuration in /etc/config.
Pretty much like the mbim protocol handler does here, but with static configuration: https://github.com/openwrt/openwrt/blob/master/package/network/utils/umbim/files/lib/netifd/proto/mbim.sh#L130-L136

You do not need to modify persistent configuration.
All the changes are runtime until you invoke

uci commit

as far as I can tell this really doesn't work for 'static', I ended up implementing a custom protocol handler that also does a bit more and even has custom options, works fine with add_dynamic.

Hi, it is possible to make static dynamic interface.
json_init
json_add_string name "lan2"
json_add_string ifname "@lan"
json_add_string proto "static"
json_add_array ipaddr
json_add_string "" "192.168.2.1/24"
json_close_array
json_close_object
ubus call network add_dynamic "$(json_dump)"

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.