change 'ifnames' to 'ifname' in network config, fix #697
[openwrt.git] / package / base-files / default / lib / network / config.sh
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3
4 # DEBUG="echo"
5
6 find_config() {
7         local iftype iface ifn
8         for ifn in $interfaces; do
9                 config_get iftype "$ifn" type
10                 config_get iface "$ifn" ifname
11                 config_get device "$ifn" device
12                 for ifc in ${device:-$iface}; do
13                         [ "$ifc" = "$1" ] && {
14                                 echo "$ifn"
15                                 return 0
16                         }
17                 done
18         done
19
20         return 1;
21 }
22
23 scan_interfaces() {
24         local mode iftype iface
25         interfaces=
26         config_cb() {
27                 config_get iftype "$CONFIG_SECTION" TYPE
28                 case "$iftype" in
29                         interface)
30                                 config_get proto "$CONFIG_SECTION" proto
31                                 append interfaces "$CONFIG_SECTION"
32                                 ( type "scan_$proto" ) >/dev/null 2>/dev/null && eval "scan_$proto '$CONFIG_SECTION'"
33                         ;;
34                 esac
35         }
36         config_load network
37 }
38
39 add_vlan() {
40         local vif="${1%\.*}"
41         
42         [ "$1" = "$vif" ] || ifconfig "$1" >/dev/null 2>/dev/null || {
43                 ifconfig "$vif" up 2>/dev/null >/dev/null || add_vlan "$vif"
44                 $DEBUG vconfig add "$vif" "${1##*\.}"
45         }
46 }
47
48 setup_interface() {
49         local iface="$1"
50         local config="$2"
51         local proto="$3"
52
53         [ -n "$config" ] || {
54                 config=$(find_config "$iface")
55                 [ "$?" = 0 ] || return 1
56         }
57
58         [ -n "$proto" ] || {
59                 config_get proto "$config" proto
60         }
61
62         config_get iftype "$config" type
63         
64         # Setup VLAN interfaces
65         add_vlan "$iface"
66
67         # Setup bridging
68         case "$iftype" in
69                 bridge)
70                         ifconfig "$iface" up 2>/dev/null >/dev/null
71                         ifconfig "br-$config" 2>/dev/null >/dev/null && {
72                                 $DEBUG brctl addif "br-$config" "$iface"
73                                 return 0
74                         } || {
75                                 $DEBUG brctl addbr "br-$config"
76                                 $DEBUG brctl setfd "br-$config" 0
77                                 $DEBUG brctl addif "br-$config" "$iface"
78                                 iface="br-$config"
79                         }
80                 ;;
81         esac
82         
83         # Interface settings
84         config_get mtu "$config" mtu
85         $DEBUG ifconfig "$iface" ${mtu:+mtu $mtu} up
86
87         pidfile="/var/run/$iface.pid"
88         case "$proto" in
89                 static)
90                         config_get ipaddr "$config" ipaddr
91                         config_get netmask "$config" netmask
92                         [ -z "$ipaddr" -o -z "$netmask" ] && return 1
93                         
94                         config_get ip6addr "$config" ip6addr
95                         config_get gateway "$config" gateway
96                         config_get dns "$config" dns
97                         
98                         $DEBUG ifconfig "$iface" "$ipaddr" netmask "$netmask"
99                         [ -z "$gateway" ] || route add default gw "$gateway"
100                         [ -z "$dns" -o -f /tmp/resolv.conf ] || {
101                                 for ns in $dns; do
102                                         echo "nameserver $ns" >> /tmp/resolv.conf
103                                 done
104                         }
105
106                         env -i ACTION="ifup" INTERFACE="config" DEVICE="$iface" PROTO=static /sbin/hotplug "iface" &
107                 ;;
108                 dhcp)
109                         pid="$(cat "$pidfile" 2>/dev/null)"
110                         [ -n "$pid" -a -d "/proc/$pid" ] && kill -9 "$pid"
111
112                         config_get ipaddr "$config" ipaddr
113                         config_get netmask "$config" netmask
114                         config_get hostname "$config" hostname
115                         config_get proto1 "$config" proto
116
117                         [ -z "$ipaddr" ] || \
118                                 $DEBUG ifconfig "$iface" "$ipaddr" ${netmask:+netmask "$netmask"}
119
120                         # don't stay running in background if dhcp is not the main proto on the interface (e.g. when using pptp)
121                         [ "$proto1" != "$proto" ] && dhcpopts="-n -q"
122                         $DEBUG udhcpc -i "$iface" ${ipaddr:+-r $ipaddr} ${hostname:+-H $hostname} -b -p "$pidfile" ${dhcpopts:- -R &}
123                 ;;
124                 *)
125                         if ( eval "type setup_interface_$proto" ) >/dev/null 2>/dev/null; then
126                                 eval "setup_interface_$proto '$iface' '$config' '$proto'" 
127                         else
128                                 echo "Interface type $proto not supported."
129                                 return 1
130                         fi
131                 ;;
132         esac
133 }
134