netifd: implement find_config()
[openwrt.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                         if [[ "$device" = "$ifdev" ]]; then
14                                 echo "$interface"
15                                 exit 0
16                         else
17                                 exit 1
18                         fi
19                 ) && return
20         done
21 }
22
23 unbridge() {
24         return
25 }
26
27 ubus_call() {
28         json_init
29         local _data="$(ubus call "$1" "$2")"
30         [ $? -ne 0 ] && return "$?"
31         json_load "$_data"
32         return 0
33 }
34
35
36 fixup_interface() {
37         local config="$1"
38         local ifname
39
40         config_get type "$config" type
41         config_get ifname "$config" ifname
42         config_get device "$config" device "$ifname"
43         [ "bridge" = "$type" ] && ifname="br-$config"
44         config_set "$config" device "$ifname"
45         ubus_call "network.interface.$config" status
46         json_get_var l3dev l3_device
47         [ -n "$l3dev" ] && ifname="$l3dev"
48         json_init
49         config_set "$config" ifname "$ifname"
50         config_set "$config" device "$device"
51 }
52
53 scan_interfaces() {
54         config_load network
55         config_foreach fixup_interface interface
56 }
57
58 prepare_interface_bridge() {
59         local config="$1"
60
61         [ -n "$config" ] || return 0
62         ubus call network.interface."$config" prepare
63 }
64
65 setup_interface() {
66         local iface="$1"
67         local config="$2"
68
69         [ -n "$config" ] || return 0
70         ubus call network.interface."$config" add_device "{ \"name\": \"$iface\" }"
71 }
72
73 do_sysctl() {
74         [ -n "$2" ] && \
75                 sysctl -n -e -w "$1=$2" >/dev/null || \
76                 sysctl -n -e "$1"
77 }