relay.init 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (c) 2011-2012 OpenWrt.org
  3. START=80
  4. USE_PROCD=1
  5. PROG=/usr/sbin/relayd
  6. validate_proto_relayd()
  7. {
  8. uci_validate_section network "interface" "${1}" \
  9. 'network:list(string)' \
  10. 'expiry:uinteger:30' \
  11. 'retry:uinteger:5' \
  12. 'table:range(0, 65535):16800' \
  13. 'forward_bcast:bool:1' \
  14. 'forward_dhcp:bool:1'
  15. }
  16. resolve_ifname() {
  17. grep -qs "^ *$1:" /proc/net/dev && {
  18. append resolved_ifnames "$1"
  19. }
  20. }
  21. resolve_network() {
  22. local ifn
  23. fixup_interface "$1"
  24. config_get ifn "$1" ifname
  25. [ -z "$ifn" ] && return 1
  26. resolve_ifname "$ifn"
  27. }
  28. start_relay() {
  29. local cfg="$1"
  30. local proto disabled
  31. config_get proto "$cfg" proto
  32. [ "$proto" = "relay" ] || return 0
  33. config_get_bool disabled "$cfg" disabled 0
  34. [ "$disabled" -gt 0 ] && return 0
  35. local resolved_ifnames
  36. local net networks
  37. config_get networks "$cfg" network
  38. for net in $networks; do
  39. resolve_network "$net" || {
  40. return 1
  41. }
  42. done
  43. local ifn ifnames
  44. config_get ifnames "$cfg" ifname
  45. for ifn in $ifnames; do
  46. resolve_ifname "$ifn" || {
  47. return 1
  48. }
  49. done
  50. procd_open_instance
  51. procd_set_param command "$PROG"
  52. for ifn in $resolved_ifnames; do
  53. procd_append_param command -I "$ifn"
  54. procd_append_param netdev "$ifn"
  55. done
  56. local ipaddr
  57. config_get ipaddr "$cfg" ipaddr
  58. [ -n "$ipaddr" ] && procd_append_param command -L "$ipaddr"
  59. local gateway
  60. config_get gateway "$cfg" gateway
  61. [ -n "$gateway" ] && procd_append_param command -G "$gateway"
  62. local expiry # = 30
  63. config_get expiry "$cfg" expiry
  64. [ -n "$expiry" ] && procd_append_param command -t "$expiry"
  65. local retry # = 5
  66. config_get retry "$cfg" retry
  67. [ -n "$retry" ] && procd_append_param command -p "$retry"
  68. local table # = 16800
  69. config_get table "$cfg" table
  70. [ -n "$table" ] && procd_append_param command -T "$table"
  71. local fwd_bcast # = 1
  72. config_get_bool fwd_bcast "$cfg" forward_bcast 1
  73. [ $fwd_bcast -eq 1 ] && procd_append_param command "-B"
  74. local fwd_dhcp # = 1
  75. config_get_bool fwd_dhcp "$cfg" forward_dhcp 1
  76. [ $fwd_dhcp -eq 1 ] && procd_append_param command "-D"
  77. procd_close_instance
  78. }
  79. service_triggers()
  80. {
  81. procd_add_reload_trigger "network"
  82. procd_add_raw_trigger "interface.*" 2000 /etc/init.d/relayd reload
  83. }
  84. start_service() {
  85. include /lib/network
  86. config_load network
  87. config_foreach start_relay interface
  88. }