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