Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
docs:guide-developer:network-scripting [2021/02/10 03:24] – add network functions examples vgaeteradocs:guide-developer:network-scripting [2023/07/19 09:34] (current) – [Get WAN gateway for unmanaged default route] add vgaetera
Line 33: Line 33:
 <code bash> <code bash>
 proto_protocolname_init_config() { proto_protocolname_init_config() {
-    proto_config_add_string "stringparam" + proto_config_add_string "stringparam" 
-    proto_config_add_int "intparam" + proto_config_add_int "intparam" 
-    proto_config_add_boolean "boolparam" + proto_config_add_boolean "boolparam" 
-    ...+ ...
 } }
 </code> </code>
Line 50: Line 50:
 <code bash> <code bash>
 proto_protocolname_setup() { proto_protocolname_setup() {
-    local config="$1" + local config="$1" 
-    # set up the interface+ # set up the interface
 } }
 </code> </code>
Line 87: Line 87:
 <code bash> <code bash>
 proto_protocolname_teardown() { proto_protocolname_teardown() {
-    local config="$1" + local config="$1" 
-    local iface="$2" + local iface="$2" 
-    # tear down the interface+ # tear down the interface
 } }
 </code> </code>
Line 101: Line 101:
 <code bash> <code bash>
 proto_protocolname_init_config() { proto_protocolname_init_config() {
-    no_device=1 + no_device=1 
-    available=1 + available=1 
-    ...+ ...
 } }
 </code> </code>
Line 112: Line 112:
   * There is a pending patch: https://patchwork.ozlabs.org/project/openwrt/patch/20201229233319.164640-1-me@irrelefant.net/.   * There is a pending patch: https://patchwork.ozlabs.org/project/openwrt/patch/20201229233319.164640-1-me@irrelefant.net/.
  
-==== Available Proto Flags ==== +==== Available proto flags ==== 
-Flags can be added to a proto handler in ''proto_protoname_init_config'', by setting their value to ''1''. The information about all loaded protocols can be obtained by calling //ubus call network get_proto_handlers//.+Flags can be added to a proto handler in ''proto_protoname_init_config'', by setting their value to ''1''. 
 +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// ^ Meaning ^ ^ Name ^ Name in //ubus call network get_proto_handlers// ^ Meaning ^
 | no_device | no_device | The interface does not have a lower device. <color #ff7f27>TBD: Explain implications.</color>| | no_device | no_device | The interface does not have a lower device. <color #ff7f27>TBD: Explain implications.</color>|
- | immediate | <color #ff7f27>TBD: I only saw this for the proto static. Maybe this is impossible to set from shell protos?</color> |+| | immediate | <color #ff7f27>TBD: I only saw this for the proto static. Maybe this is impossible to set from shell protos?</color> |
 | available | init_available | Initialize the device directly as available, without the device specified by ifname being present <color #ff7f27>TBD: Is that correct?</color>| | available | init_available | Initialize the device directly as available, without the device specified by ifname being present <color #ff7f27>TBD: Is that correct?</color>|
 | renew_handler | renew_available | Has a renew handler, which can be called. <color #ff7f27>TBD: How can it be called? Is it called automatically by sth.?</color> | | renew_handler | renew_available | Has a renew handler, which can be called. <color #ff7f27>TBD: How can it be called? Is it called automatically by sth.?</color> |
Line 124: Line 125:
 | no_proto_task | no_task | <color #ff7f27>TBD</color> | | no_proto_task | no_task | <color #ff7f27>TBD</color> |
  
-==== Error Codes ==== +==== Error codes ==== 
-If errors for interfaces occur, the json object in //ifstatus interfacename// or in //ubus call network.interface dump// have an attribute //"error"//. If there are no errors, this attribute is not existing.+If errors for interfaces occur, the json object in //ifstatus interfacename// or in //ubus call network.interface dump// have an attribute //"error"//. 
 +If there are no errors, this attribute is not existing.
  
 ^ CODE ^ Meaning ^ ^ CODE ^ Meaning ^
Line 182: Line 184:
  * ''proto_add_host_dependency''  * ''proto_add_host_dependency''
  
-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:
   * ''proto_add_host_dependency "$config" "$host" "$ifname"''   * ''proto_add_host_dependency "$config" "$host" "$ifname"''
   * ''proto_add_host_dependency "$config" \'\' "$ifname"'' (only wait for an iface)   * ''proto_add_host_dependency "$config" \'\' "$ifname"'' (only wait for an iface)
Line 204: Line 207:
  
 ===== Examples ===== ===== Examples =====
 +[[https://github.com/openwrt/openwrt/blob/master/package/base-files/files/lib/functions/network.sh|Network functions]] rely on runtime configuration and can return unexpected result if you are using MWAN or VPN.
 +Replace automatic WAN detection with explicit interface definition if necessary.
 +
 +==== Get LAN address ====
 +<code bash>
 +# Runtime configuration
 +NET_IF="lan"
 +. /lib/functions/network.sh
 +network_flush_cache
 +network_get_ipaddr NET_ADDR "${NET_IF}"
 +network_get_ipaddr6 NET_ADDR6 "${NET_IF}"
 +echo "${NET_ADDR}"
 +echo "${NET_ADDR6}"
 +</code>
 +
 ==== Get WAN interface ==== ==== Get WAN interface ====
 <code bash> <code bash>
Line 211: Line 229:
 network_find_wan NET_IF network_find_wan NET_IF
 network_find_wan6 NET_IF6 network_find_wan6 NET_IF6
-network_get_device NET_DEV "${NET_IF}" +echo "${NET_IF}" 
-network_get_device NET_DEV6 "${NET_IF6}" +echo "${NET_IF6}" 
-echo "${NET_DEV}" +</code> 
-echo "${NET_DEV6}"+ 
 +==== Get WAN L2 device ==== 
 +<code bash> 
 +# Runtime configuration 
 +. /lib/functions/network.sh 
 +network_flush_cache 
 +network_find_wan NET_IF 
 +network_find_wan6 NET_IF6 
 +network_get_physdev NET_L2D "${NET_IF}" 
 +network_get_physdev NET_L2D6 "${NET_IF6}" 
 +echo "${NET_L2D}" 
 +echo "${NET_L2D6}" 
 +</code> 
 + 
 +==== Get WAN L3 device ==== 
 +<code bash> 
 +# Runtime configuration 
 +. /lib/functions/network.sh 
 +network_flush_cache 
 +network_find_wan NET_IF 
 +network_find_wan6 NET_IF6 
 +network_get_device NET_L3D "${NET_IF}" 
 +network_get_device NET_L3D6 "${NET_IF6}" 
 +echo "${NET_L3D}" 
 +echo "${NET_L3D6}"
  
 # Persistent configuration # Persistent configuration
-uci get network.wan.ifname +uci get network.wan.device 
-uci get network.wan6.ifname+uci get network.wan6.device
 </code> </code>
  
-==== Get WAN IP address ====+==== Get WAN address ====
 <code bash> <code bash>
 # Runtime configuration # Runtime configuration
Line 253: Line 295:
 uci get network.wan.gateway uci get network.wan.gateway
 uci get network.wan6.ip6gw uci get network.wan6.ip6gw
 +</code>
 +
 +==== Get WAN prefix ====
 +<code bash>
 +# Runtime configuration
 +. /lib/functions/network.sh
 +network_flush_cache
 +network_find_wan6 NET_IF6
 +network_get_prefix6 NET_PFX6 "${NET_IF6}"
 +echo "${NET_PFX6}"
 +
 +# Persistent static configuration
 +uci get network.wan6.ip6prefix
 +</code>
 +
 +==== Get WAN gateway for unmanaged default route ====
 +<code bash>
 +# Runtime configuration
 +ubus call network.interface dump \
 +| jsonfilter -e "$['interface'][*]['inactive']
 +['route'][*]['target'='0.0.0.0','mask'='0','nexthop']"
 </code> </code>
  
  • Last modified: 2021/02/10 03:24
  • by vgaetera