[package] base-file: move alias setup to route hotplug, this fixes alias sections...
[openwrt.git] / package / base-files / files / etc / hotplug.d / iface / 10-routes
1 add_route() {
2         local config="$1"
3
4         # is this route intended for the
5         # $INTERFACE of this hotplug event
6         config_get interface "$config" interface
7         [ "$interface" != "$INTERFACE" ] && return 0
8         
9         # get the real interface name from network config
10         config_get dev "$interface" ifname
11
12         config_get target "$config" target
13         config_get netmask "$config" netmask
14         config_get gateway "$config" gateway
15         config_get metric "$config" metric
16
17         # make sure there is a gateway and a target
18         [ -n "$target" ] || {
19                 echo "Missing target in route section $config"
20                 return 1
21         }
22         [ -n "$gateway" ] || {
23                 config_get gateway "$interface" gateway
24         }
25
26         # handle "0.0.0.0" as "no gateway given" to allow
27         # defining gateway-less routes while still keeping
28         # the possibility to have static routes with a
29         # proper gateway on interfaces with dynamic ips 
30         [ "$gateway" = "0.0.0.0" ] && gateway=""
31
32         dest="${netmask:+-net "$target" netmask "$netmask"}"
33         dest="${dest:--host "$target"}"
34         
35         /sbin/route add $dest ${gateway:+gw "$gateway"} \
36                 ${dev:+dev "$dev"} ${metric:+ metric "$metric"}
37 }
38
39 add_route6() {
40         local config="$1"
41
42         # is this route intended for the
43         # $INTERFACE of this hotplug event
44         config_get interface "$config" interface
45         [ "$interface" != "$INTERFACE" ] && return 0
46         
47         # get the real interface name from network config
48         config_get dev "$interface" ifname
49
50         config_get target "$config" target
51         config_get gateway "$config" gateway
52         config_get metric "$config" metric
53
54         # make sure there is a gateway and a target
55         [ -n "$target" ] || {
56                 echo "Missing target in route section $config"
57                 return 1
58         }
59         [ -n "$gateway" ] || {
60                 config_get gateway "$interface" gateway
61         }
62
63         /sbin/route -A inet6 add $target ${gateway:+gw "$gateway"} \
64                 ${dev:+dev "$dev"} ${metric:+ metric "$metric"}
65 }
66
67 case "$ACTION" in
68         ifup)
69                 include /lib/network
70                 scan_interfaces
71
72                 # Setup aliases
73                 config_set "$INTERFACE" aliases ""
74                 config_set "$INTERFACE" alias_count 0
75                 config_foreach setup_interface_alias alias "$INTERFACE" "$DEVICE"
76
77                 # Save alias references in state vars
78                 local aliases
79                 config_get aliases "$INTERFACE" aliases
80                 [ -z "$aliases" ] || uci_set_state network "$INTERFACE" aliases "$aliases"
81
82                 # Make ip6addr of parent iface the main address again
83                 local ip6addr
84                 config_get ip6addr "$INTERFACE" ip6addr
85                 [ -z "$ip6addr" ] || {
86                         ifconfig "$DEVICE" del "$ip6addr"
87                         ifconfig "$DEVICE" add "$ip6addr"
88                 }
89
90                 # Setup routes
91                 config_foreach "add_route" route
92                 config_foreach "add_route6" route6
93         ;;
94         ifdown)
95                 # Bring down named aliases
96                 local ifn
97                 for ifn in $(ifconfig | sed -ne "s/^\($DEVICE:[^[:space:]]\+\).*/\1/p"); do
98                         ifconfig "$ifn" down
99                 done
100         ;;
101 esac
102