[package] relayd: map "ipaddr" option to local ip (-L)
[openwrt/svn-archive/archive.git] / package / relayd / files / relay.sh
1 # relay.sh - Abstract relayd protocol backend
2 # Copyright (c) 2011 OpenWrt.org
3
4 # Hook into scan_interfaces() to synthesize a .device option
5 # This is needed for /sbin/ifup to properly dispatch control
6 # to setup_interface_relay() even if no .ifname is set in
7 # the configuration.
8 scan_relay() {
9 config_set "$1" device "relay-$1"
10 }
11
12 # No coldplugging needed, relayd will be restarted if one of
13 # the member interfaces goes up or down
14 #coldplug_interface_relay() {
15 # setup_interface_relay "relay-$1" "$1"
16 #}
17
18 setup_interface_relay() {
19 local iface="$1"
20 local cfg="$2"
21 local link="relay-$cfg"
22
23 local args=""
24 local ifaces=""
25
26 resolve_ifname() {
27 grep -qs "^ *$1:" /proc/net/dev && {
28 append args "-I $1"
29 append ifaces "$1"
30 }
31 }
32
33 resolve_network() {
34 local ifn
35 config_get ifn "$1" ifname
36 resolve_ifname "$ifn"
37 }
38
39 local net networks
40 config_get networks "$cfg" network
41 for net in $networks; do
42 resolve_network "$net"
43 done
44
45 local ifn ifnames
46 config_get ifnames "$cfg" ifname
47 for ifn in $ifnames; do
48 resolve_ifname "$ifn"
49 done
50
51 local ipaddr
52 config_get ipaddr "$cfg" ipaddr
53 [ -n "$ipaddr" ] && append args "-L $ipaddr"
54
55 local gateway
56 config_get gateway "$cfg" gateway
57 [ -n "$gateway" ] && append args "-G $gateway"
58
59 local expiry # = 30
60 config_get expiry "$cfg" expiry
61 [ -n "$expiry" ] && append args "-t $expiry"
62
63 local retry # = 5
64 config_get retry "$cfg" retry
65 [ -n "$retry" ] && append args "-p $retry"
66
67 local table # = 16800
68 config_get table "$cfg" table
69 [ -n "$table" ] && append args "-T $table"
70
71 local fwd_bcast # = 1
72 config_get_bool fwd_bcast "$cfg" forward_bcast 1
73 [ $fwd_bcast -eq 1 ] && append args "-B"
74
75 local fwd_dhcp # = 1
76 config_get_bool fwd_dhcp "$cfg" forward_dhcp 1
77 [ $fwd_dhcp -eq 1 ] && append args "-D"
78
79 start-stop-daemon -b -S -m -p /var/run/$link.pid \
80 -x /usr/sbin/relayd -- $args
81
82 uci_set_state network "$cfg" device "$ifaces"
83
84 env -i ACTION="ifup" DEVICE="$link" INTERFACE="$cfg" PROTO="relay" \
85 /sbin/hotplug-call iface
86 }
87
88 stop_interface_relay() {
89 local cfg="$1"
90 local link="relay-$cfg"
91
92 env -i ACTION="ifdown" DEVICE="$link" INTERFACE="$cfg" PROTO="relay" \
93 /sbin/hotplug-call iface
94
95 service_kill relayd "/var/run/$link.pid"
96 }
97