Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| docs:guide-developer:network-scripting [2021/02/10 03:13] – update vgaetera | docs:guide-developer:network-scripting [2023/07/19 09:34] (current) – [Get WAN gateway for unmanaged default route] add vgaetera | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Network scripts ====== | ====== Network scripts ====== | ||
| - | netifd can (probably) bring up a wired, static ip configuration without shell scripts. For everything else (PPPoE or 3G) it needs //protocol handlers// implemented as sets of shell functions. | + | netifd can (probably) bring up a wired, static ip configuration without shell scripts. |
| + | For everything else (PPPoE or 3G) it needs //protocol handlers// implemented as sets of shell functions. | ||
| ===== Writing protocol handlers ===== | ===== Writing protocol handlers ===== | ||
| - | Each protocol handler is a shell script in ''/ | + | Each protocol handler is a shell script in ''/ |
| + | It is run (or maybe sourced) when netifd daemon starts. | ||
| + | Changes made to the scripts do not take effect until netifd restarts. | ||
| + | The name of the file usually matches '' | ||
| To be able to access the network functions, the script needs to include the necessary shell scripts at the beginning: | To be able to access the network functions, the script needs to include the necessary shell scripts at the beginning: | ||
| Line 24: | Line 28: | ||
| ==== init_config ==== | ==== init_config ==== | ||
| - | The main purpose of this function is to let netifd know which parameters does this protocol have. These parameters then can be stored in ''/ | + | The main purpose of this function is to let netifd know which parameters does this protocol have. |
| + | These parameters then can be stored in ''/ | ||
| <code bash> | <code bash> | ||
| proto_protocolname_init_config() { | proto_protocolname_init_config() { | ||
| - | | + | proto_config_add_string " |
| - | proto_config_add_int " | + | proto_config_add_int " |
| - | proto_config_add_boolean " | + | proto_config_add_boolean " |
| - | ... | + | ... |
| } | } | ||
| </ | </ | ||
| Line 45: | Line 50: | ||
| <code bash> | <code bash> | ||
| proto_protocolname_setup() { | proto_protocolname_setup() { | ||
| - | | + | local config=" |
| - | # set up the interface | + | # set up the interface |
| } | } | ||
| </ | </ | ||
| Line 52: | Line 57: | ||
| This function must be implemented by any protocol backend. | This function must be implemented by any protocol backend. | ||
| - | There' | + | There' |
| + | If '' | ||
| + | It waits for '' | ||
| If '' | If '' | ||
| Line 65: | Line 72: | ||
| </ | </ | ||
| - | However, this only works once. If someone called '' | + | However, this only works once. |
| + | If someone called '' | ||
| ==== Teardown ==== | ==== Teardown ==== | ||
| - | Protocols that need special shutdown handling, for example to kill associated daemons, may implement | + | Protocols that need special shutdown handling, for example to kill associated daemons, may implement a stop procedure. |
| - | a stop procedure. This procedure is called when an interface is brought down before the associated UCI | + | This procedure is called when an interface is brought down before the associated UCI state is cleared. |
| - | state is cleared. | + | |
| The function is called when we do '' | The function is called when we do '' | ||
| Line 80: | Line 87: | ||
| <code bash> | <code bash> | ||
| proto_protocolname_teardown() { | proto_protocolname_teardown() { | ||
| - | | + | local config=" |
| - | local iface=" | + | local iface=" |
| - | # tear down the interface | + | # tear down the interface |
| } | } | ||
| </ | </ | ||
| Line 94: | Line 101: | ||
| <code bash> | <code bash> | ||
| proto_protocolname_init_config() { | proto_protocolname_init_config() { | ||
| - | | + | no_device=1 |
| - | available=1 | + | available=1 |
| - | ... | + | ... |
| } | } | ||
| </ | </ | ||
| Line 105: | Line 112: | ||
| * There is a pending patch: https:// | * There is a pending patch: https:// | ||
| - | ==== Available | + | ==== Available |
| - | Flags can be added to a proto handler in '' | + | Flags can be added to a proto handler in '' |
| + | The information about all loaded protocols can be obtained by calling //ubus call network get_proto_handlers// | ||
| ^ Name ^ Name in //ubus call network get_proto_handlers// | ^ Name ^ Name in //ubus call network get_proto_handlers// | ||
| | no_device | no_device | The interface does not have a lower device. <color # | | no_device | no_device | The interface does not have a lower device. <color # | ||
| - | | | immediate | <color # | + | | | immediate | <color # |
| | available | init_available | Initialize the device directly as available, without the device specified by ifname being present <color # | | available | init_available | Initialize the device directly as available, without the device specified by ifname being present <color # | ||
| | renew_handler | renew_available | Has a renew handler, which can be called. <color # | | renew_handler | renew_available | Has a renew handler, which can be called. <color # | ||
| Line 117: | Line 125: | ||
| | no_proto_task | no_task | <color # | | no_proto_task | no_task | <color # | ||
| - | ==== Error Codes ==== | + | ==== Error codes ==== |
| - | If errors for interfaces occur, the json object in //ifstatus interfacename// | + | If errors for interfaces occur, the json object in //ifstatus interfacename// |
| + | If there are no errors, this attribute is not existing. | ||
| ^ CODE ^ Meaning ^ | ^ CODE ^ Meaning ^ | ||
| Line 125: | Line 134: | ||
| | SETUP_FAILED | <color # | | SETUP_FAILED | <color # | ||
| - | Custom error codes can be thrown from the proto scripts aswell. This is done via '' | + | Custom error codes can be thrown from the proto scripts aswell. |
| + | This is done via '' | ||
| ==== How does it work internally? ==== | ==== How does it work internally? ==== | ||
| Line 158: | Line 168: | ||
| === notification functions === | === notification functions === | ||
| - | Note: some of these function exit immediately. These functions are dispatched to ubus and arrive | + | Note: some of these function exit immediately. |
| + | These functions are dispatched to ubus and arrive [[commit>? | ||
| * '' | * '' | ||
| Line 166: | Line 177: | ||
| * '' | * '' | ||
| - | We can start a daemon that maintains the connection asynchronously by calling '' | + | We can start a daemon that maintains the connection asynchronously by calling '' |
| + | Netifd remembers its pid. | ||
| + | It can be killed manually by calling '' | ||
| + | Netifd can automatically kill it, when the profile stopped. | ||
| * '' | * '' | ||
| - | It seems to avoid race conditions in protos. We can register the following type of dependencies by calling: | + | It seems to avoid race conditions in protos. |
| + | We can register the following type of dependencies by calling: | ||
| * '' | * '' | ||
| * '' | * '' | ||
| * (maybe more?) | * (maybe more?) | ||
| - | Only if '' | + | Only if '' |
| + | So we need another proto to be completed, we can use this here. | ||
| - | From some observations I think it looks like technically the '' | + | From some observations I think it looks like technically the '' |
| + | However, currently I do not know where to find the source code, so this is only a vague idea of what happens. | ||
| * '' | * '' | ||
| Line 188: | Line 205: | ||
| * '' | * '' | ||
| * '' | * '' | ||
| + | |||
| + | ===== Examples ===== | ||
| + | [[https:// | ||
| + | Replace automatic WAN detection with explicit interface definition if necessary. | ||
| + | |||
| + | ==== Get LAN address ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | NET_IF=" | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_get_ipaddr NET_ADDR " | ||
| + | network_get_ipaddr6 NET_ADDR6 " | ||
| + | echo " | ||
| + | echo " | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN interface ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_find_wan NET_IF | ||
| + | network_find_wan6 NET_IF6 | ||
| + | echo " | ||
| + | echo " | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN L2 device ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_find_wan NET_IF | ||
| + | network_find_wan6 NET_IF6 | ||
| + | network_get_physdev NET_L2D " | ||
| + | network_get_physdev NET_L2D6 " | ||
| + | echo " | ||
| + | echo " | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN L3 device ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_find_wan NET_IF | ||
| + | network_find_wan6 NET_IF6 | ||
| + | network_get_device NET_L3D " | ||
| + | network_get_device NET_L3D6 " | ||
| + | echo " | ||
| + | echo " | ||
| + | |||
| + | # Persistent configuration | ||
| + | uci get network.wan.device | ||
| + | uci get network.wan6.device | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN address ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_find_wan NET_IF | ||
| + | network_find_wan6 NET_IF6 | ||
| + | network_get_ipaddr NET_ADDR " | ||
| + | network_get_ipaddr6 NET_ADDR6 " | ||
| + | echo " | ||
| + | echo " | ||
| + | |||
| + | # Persistent static configuration | ||
| + | uci get network.wan.ipaddr | ||
| + | uci get network.wan6.ip6addr | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN gateway ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_find_wan NET_IF | ||
| + | network_find_wan6 NET_IF6 | ||
| + | network_get_gateway NET_GW " | ||
| + | network_get_gateway6 NET_GW6 " | ||
| + | echo " | ||
| + | echo " | ||
| + | |||
| + | # Persistent static configuration | ||
| + | uci get network.wan.gateway | ||
| + | uci get network.wan6.ip6gw | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN prefix ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | . / | ||
| + | network_flush_cache | ||
| + | network_find_wan6 NET_IF6 | ||
| + | network_get_prefix6 NET_PFX6 " | ||
| + | echo " | ||
| + | |||
| + | # Persistent static configuration | ||
| + | uci get network.wan6.ip6prefix | ||
| + | </ | ||
| + | |||
| + | ==== Get WAN gateway for unmanaged default route ==== | ||
| + | <code bash> | ||
| + | # Runtime configuration | ||
| + | ubus call network.interface dump \ | ||
| + | | jsonfilter -e " | ||
| + | [' | ||
| + | </ | ||