netifd: scan l3 devices on find_config()
[openwrt/svn-archive/archive.git] / package / netifd / files / lib / network / config.sh
1 #!/bin/sh
2 # Copyright (C) 2011 OpenWrt.org
3
4 . /usr/share/libubox/jshn.sh
5
6 find_config() {
7 local device="$1"
8 for ifobj in `ubus list network.interface.\*`; do
9 interface="${ifobj##network.interface.}"
10 (
11 json_load "$(ifstatus $interface)"
12 json_get_var ifdev device
13 json_get_var ifl3dev l3_device
14 if [[ "$device" = "$ifdev" ]] || [[ "$device" = "$ifl3dev" ]]; then
15 echo "$interface"
16 exit 0
17 else
18 exit 1
19 fi
20 ) && return
21 done
22 }
23
24 unbridge() {
25 return
26 }
27
28 ubus_call() {
29 json_init
30 local _data="$(ubus call "$1" "$2")"
31 [ $? -ne 0 ] && return "$?"
32 json_load "$_data"
33 return 0
34 }
35
36
37 fixup_interface() {
38 local config="$1"
39 local ifname
40
41 config_get type "$config" type
42 config_get ifname "$config" ifname
43 config_get device "$config" device "$ifname"
44 [ "bridge" = "$type" ] && ifname="br-$config"
45 config_set "$config" device "$ifname"
46 ubus_call "network.interface.$config" status
47 json_get_var l3dev l3_device
48 [ -n "$l3dev" ] && ifname="$l3dev"
49 json_init
50 config_set "$config" ifname "$ifname"
51 config_set "$config" device "$device"
52 }
53
54 scan_interfaces() {
55 config_load network
56 config_foreach fixup_interface interface
57 }
58
59 prepare_interface_bridge() {
60 local config="$1"
61
62 [ -n "$config" ] || return 0
63 ubus call network.interface."$config" prepare
64 }
65
66 setup_interface() {
67 local iface="$1"
68 local config="$2"
69
70 [ -n "$config" ] || return 0
71 ubus call network.interface."$config" add_device "{ \"name\": \"$iface\" }"
72 }
73
74 do_sysctl() {
75 [ -n "$2" ] && \
76 sysctl -n -e -w "$1=$2" >/dev/null || \
77 sysctl -n -e "$1"
78 }