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