[package] relayd: add uci integration
[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 gateway
52 config_get gateway "$cfg" gateway
53 [ -n "$gateway" ] && append args "-G $gateway"
54
55 local expiry # = 30
56 config_get expiry "$cfg" expiry
57 [ -n "$expiry" ] && append args "-t $expiry"
58
59 local retry # = 5
60 config_get retry "$cfg" retry
61 [ -n "$retry" ] && append args "-p $retry"
62
63 local table # = 16800
64 config_get table "$cfg" table
65 [ -n "$table" ] && append args "-T $table"
66
67 local fwd_bcast # = 1
68 config_get_bool fwd_bcast "$cfg" forward_bcast 1
69 [ $fwd_bcast -eq 1 ] && append args "-B"
70
71 local fwd_dhcp # = 1
72 config_get_bool fwd_dhcp "$cfg" forward_dhcp 1
73 [ $fwd_dhcp -eq 1 ] && append args "-D"
74
75 start-stop-daemon -b -S -m -p /var/run/$link.pid \
76 -x /usr/sbin/relayd -- $args
77
78 uci_set_state network "$cfg" device "$ifaces"
79
80 env -i ACTION="ifup" DEVICE="$link" INTERFACE="$cfg" PROTO="relay" \
81 /sbin/hotplug-call iface
82 }
83
84 stop_interface_relay() {
85 local cfg="$1"
86 local link="relay-$cfg"
87
88 env -i ACTION="ifdown" DEVICE="$link" INTERFACE="$cfg" PROTO="relay" \
89 /sbin/hotplug-call iface
90
91 service_kill relayd "/var/run/$link.pid"
92 }
93