[backfire] backport r25454
[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 sysctls
95                 local proto accept_ra send_rs
96                 
97                 config_get proto "$INTERFACE" proto
98                 if [ "$proto" = dhcp ]; then
99                         accept_ra=1
100                         send_rs=0
101                 else
102                         accept_ra=0
103                         send_rs=1
104                 fi
105
106                 config_get_bool accept_ra "$INTERFACE" accept_ra $accept_ra
107                 [ $accept_ra -eq 0 ] || {
108                         logger -t ifup "Allowing Router Advertisements on $INTERFACE ($DEVICE)"
109                         accept_ra=2
110                 }
111                 do_sysctl "net.ipv6.conf.$INTERFACE.accept_ra" $accept_ra
112
113                 config_get_bool send_rs "$INTERFACE" send_rs $send_rs
114                 [ $send_rs -eq 0 ] || {
115                         logger -t ifup "Enabling Router Solicitations on $INTERFACE ($DEVICE)"
116                         send_rs=2
117                 }
118                 do_sysctl "net.ipv6.conf.$INTERFACE.forwarding" $send_rs
119
120
121                 # Setup routes
122                 config_foreach "add_route" route
123                 config_foreach "add_route6" route6
124         ;;
125         ifdown)
126                 # Bring down named aliases
127                 local device=$(uci_get_state network "$INTERFACE" device)
128                 local ifn
129                 for ifn in $(ifconfig | sed -ne "s/^\(\($DEVICE${device:+\|$device}\|br-$INTERFACE\):[^[:space:]]\+\).*/\1/p"); do
130                         ifconfig "$ifn" down
131                 done
132         ;;
133 esac
134