[backfire] backport r27616
[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 # Skip fake devices (e.g. relayd)
72 grep -qs "^ *$DEVICE:" /proc/net/dev || exit 0
73
74 case "$ACTION" in
75         ifup)
76                 include /lib/network
77                 scan_interfaces
78
79                 # Setup aliases
80                 config_set "$INTERFACE" aliases ""
81                 config_set "$INTERFACE" alias_count 0
82                 config_foreach setup_interface_alias alias "$INTERFACE" "$DEVICE"
83
84                 # Save alias references in state vars
85                 local aliases
86                 config_get aliases "$INTERFACE" aliases
87                 [ -z "$aliases" ] || uci_toggle_state network "$INTERFACE" aliases "$aliases"
88
89                 # Make ip6addr of parent iface the main address again
90                 local ip6addr
91                 config_get ip6addr "$INTERFACE" ip6addr
92                 [ -z "$ip6addr" ] || {
93                         ifconfig "$DEVICE" del "$ip6addr"
94                         ifconfig "$DEVICE" add "$ip6addr"
95                 }
96                 
97                 # Setup sysctls
98                 local proto accept_ra send_rs
99                 
100                 config_get proto "$INTERFACE" proto
101                 if [ "$proto" = dhcp ]; then
102                         accept_ra=1
103                         send_rs=0
104                 else
105                         accept_ra=0
106                         send_rs=1
107                 fi
108
109                 config_get_bool accept_ra "$INTERFACE" accept_ra $accept_ra
110                 [ $accept_ra -eq 0 ] || {
111                         logger -t ifup "Allowing Router Advertisements on $INTERFACE ($DEVICE)"
112                         accept_ra=2
113                 }
114                 do_sysctl "net.ipv6.conf.$DEVICE.accept_ra" $accept_ra
115
116                 config_get_bool send_rs "$INTERFACE" send_rs $send_rs
117                 [ $send_rs -eq 0 ] || {
118                         logger -t ifup "Enabling Router Solicitations on $INTERFACE ($DEVICE)"
119                         send_rs=2
120                 }
121                 do_sysctl "net.ipv6.conf.$DEVICE.forwarding" $send_rs
122
123
124                 # Setup routes
125                 config_foreach "add_route" route
126                 config_foreach "add_route6" route6
127         ;;
128         ifdown)
129                 # Bring down named aliases
130                 local device=$(uci_get_state network "$INTERFACE" device)
131                 local ifn
132                 for ifn in $(ifconfig | sed -ne "s/^\(\($DEVICE${device:+\|$device}\|br-$INTERFACE\):[^[:space:]]\+\).*/\1/p"); do
133                         ifconfig "$ifn" down
134                 done
135         ;;
136 esac
137