[package] base-files: (#4928)
[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" and "interface" as "no gateway given"
27         # to allow defining gateway-less routes while still
28         # keeping the possibility to have static routes with a
29         # proper gateway on interfaces with dynamic ips 
30         [ "$gateway" = "0.0.0.0" -o \
31                 "$gateway" = "interface" ] && 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 }
39
40 add_route6() {
41         local config="$1"
42
43         # is this route intended for the
44         # $INTERFACE of this hotplug event
45         config_get interface "$config" interface
46         [ "$interface" != "$INTERFACE" ] && return 0
47         
48         # get the real interface name from network config
49         config_get dev "$interface" ifname
50
51         config_get target "$config" target
52         config_get gateway "$config" gateway
53         config_get metric "$config" metric
54
55         # make sure there is a gateway and a target
56         [ -n "$target" ] || {
57                 echo "Missing target in route section $config"
58                 return 1
59         }
60         [ -n "$gateway" ] || {
61                 config_get gateway "$interface" gateway
62         }
63
64         /sbin/route -A inet6 add $target ${gateway:+gw "$gateway"} \
65                 ${dev:+dev "$dev"} ${metric:+ metric "$metric"}
66 }
67
68 case "$ACTION" in
69         ifup)
70                 include /lib/network
71                 scan_interfaces
72                 config_foreach "add_route" route
73                 config_foreach "add_route6" route6
74         ;;
75 esac