1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2011 OpenWrt.org
9 CONF
=/var
/etc
/olsrd.conf
10 PID
=/var
/run
/olsrd.pid
11 PID6
=/var
/run
/olsrd.ipv6.pid
13 OLSRD_OLSRD_SCHEMA
='ignore:internal config_file:internal DebugLevel=0 AllowNoInt=yes'
14 OLSRD_IPCCONNECT_SCHEMA
='ignore:internal Host:list Net:list2'
15 OLSRD_LOADPLUGIN_SCHEMA
='ignore:internal library:internal Host4:list Net4:list2 Host:list Net:list2 Host6:list Net6:list2 Ping:list redistribute:list NonOlsrIf:list name:list lat lon latlon_infile HNA:list2 hosts:list2'
16 OLSRD_INTERFACE_SCHEMA
='ignore:internal interface:internal AutoDetectChanges:bool'
17 OLSRD_INTERFACE_DEFAULTS_SCHEMA
='AutoDetectChanges:bool'
25 [ -z "$varname" -o "$varname" != "${varname%%[!A-Za-z0-9_]*}" ] && return 1
31 [ -z "$ifname" -o "$ifname" != "${ifname%%[!A-Za-z0-9.:_-]*}" ] && return 1
35 validate_olsrd_option
() {
37 [ -z "$str" -o "$str" != "${str%%[! 0-9A-Za-z./|:_-]*}" ] && return 1
44 validate_varname
"$interface" ||
return 1
47 config_get ifname
"$interface" ifname
48 validate_ifname
"$ifname" ||
return 1
59 config_get cfgt
"$cfg" TYPE
61 if [ "$cfgt" = "system" ]; then
62 config_get hostname
"$cfg" hostname
63 hostname
="${hostname:-OpenWrt}"
64 SYSTEM_HOSTNAME
="$hostname"
67 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
68 config_get latlon
"$cfg" latlon
76 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
77 config_get latlon
"$cfg" latitude
79 config_get latlon
"$cfg" longitude
84 olsrd_find_config_file
() {
86 validate_varname
"$cfg" ||
return 0
88 config_get_bool ignore
"$cfg" ignore
0
89 [ "$ignore" -ne 0 ] && return 0
90 config_get OLSRD_CONFIG_FILE
"$cfg" config_file
95 warning_invalid_value
() {
97 validate_varname
"$package" || package
=
99 validate_varname
"$config" || config
=
101 validate_varname
"$option" || option
=
103 echo -n "Warning: Invalid value" 1>&2
105 if [ -n "$package" -a -n "$config" ]; then
106 echo -n " in option '$package.$config${option:+.}$option'" 1>&2
109 echo ", skipped" 1>&2
114 olsrd_write_option
() {
117 validate_varname
"$cfg" ||
return 1
119 validate_varname
"$option" ||
return 1
121 local option_type
="$5"
123 if [ "$option_type" = bool
]; then
125 1|on|true|enabled|
yes) value
=yes;;
126 0|off|false|disabled|no
) value
=no
;;
127 *) warning_invalid_value olsrd
"$cfg" "$option"; return 1;;
131 if ! validate_olsrd_option
"$value"; then
132 warning_invalid_value olsrd
"$cfg" "$option"
136 if [ "$value" != "${value%%[G-Zg-z_-]*}" ]; then
137 if [ "$option" != "Ip6AddrType" -a "$option" != "LinkQualityMult" -a "$value" != "yes" -a "$value" != "no" ]; then
142 echo -n "${N}$param$option $value"
147 olsrd_write_plparam
() {
150 validate_varname
"$cfg" ||
return 1
152 validate_varname
"$option" ||
return 1
154 local option_type
="$5"
157 if [ "$option_type" = bool
]; then
159 1|on|true|enabled|
yes) value
=yes;;
160 0|off|false|disabled|no
) value
=no
;;
161 *) warning_invalid_value olsrd
"$cfg" "$option"; return 1;;
165 if ! validate_olsrd_option
"$value"; then
166 warning_invalid_value olsrd
"$cfg" "$option"
175 if [ "$option" = 'hosts' ]; then
181 if [ "$option" = 'NonOlsrIf' ]; then
182 if validate_varname
"$value"; then
183 if get_ifname
"$value"; then
185 echo "Info: mdns Interface '$value' ifname '$ifname' found" 1>&2
187 echo "Warning: mdns Interface '$value' not found, skipped" 1>&2
190 warning_invalid_value olsrd
"$cfg" "NonOlsrIf"
192 [ -z "$ifname" ] || value
=$ifname
195 echo -n "${N}${param}PlParam \"$option\" \"$value\""
200 config_update_schema
() {
202 local schema_varname
="$1"
203 validate_varname
"$schema_varname" ||
return 1
205 validate_varname
"$command" ||
return 1
207 validate_varname
"$option" ||
return 1
217 eval "export -n -- \"schema=\${$schema_varname}\""
219 for cur_option
in $schema; do
220 [ "${cur_option%%[:=]*}" = "$option" ] && return 0
223 if [ "$command" = list
]; then
225 if [ "$#" -ge "3" ]; then
226 schema_entry
="$option:list3"
227 elif [ "$#" -ge "2" ]; then
228 schema_entry
="$option:list2"
230 schema_entry
="$option:list"
233 schema_entry
="$option"
236 append
"$schema_varname" "$schema_entry"
241 config_write_options
() {
245 validate_varname
"$cfg" ||
return 1
246 local write_func
="$3"
247 [ -z "$write_func" ] && output_func
=echo
248 local write_param
="$4"
261 for schema_entry
in $schema; do
262 default
="${schema_entry#*[=]}"
263 [ "$default" = "$schema_entry" ] && default
=
264 option
="${schema_entry%%[=]*}"
270 validate_varname
"$option" ||
continue
271 [ -z "$option_type" ] || validate_varname
"$option_type" ||
continue
272 [ "$option_type" = internal
] && continue
273 config_get value
"$cfg" "$option"
275 if [ -z "$value" ]; then
280 elif [ "$value" = '-' -a -n "$default" ]; then
284 [ -z "$value" ] && continue
286 case "$option_type" in
293 if [ "$list_size" -gt 0 ]; then
294 config_get option_length
"$cfg" "${option}_LENGTH"
295 if [ -n "$option_length" ]; then
297 while [ "$i" -le "$option_length" ]; do
298 config_get list_value
"$cfg" "${option}_ITEM$i"
299 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" ||
break
305 for list_item
in $value; do
306 append
"list_value" "$list_item"
308 position
=$
((i
% list_size
))
309 if [ "$position" -eq 0 ]; then
310 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" ||
break
314 [ "$position" -ne 0 ] && "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type"
317 "$write_func" "$write_param" "$cfg" "$option" "$value" "$option_type"
324 olsrd_write_olsrd
() {
326 validate_varname
"$cfg" ||
return 0
329 config_get_bool ignore
"$cfg" ignore
0
330 [ "$ignore" -ne 0 ] && return 0
332 [ "$OLSRD_COUNT" -gt 0 ] && return 0
334 config_get ipversion
"$cfg" IpVersion
335 if [ "$ipversion" = "6and4" ]; then
336 OLSRD_IPVERSION_6AND4
=1
337 config_set
"$cfg" IpVersion
'6'
339 config_get smartgateway
"$cfg" SmartGateway
340 config_get smartgatewayuplink
"$cfg" SmartGatewayUplink
342 config_write_options
"$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option
344 OLSRD_COUNT
=$
((OLSRD_COUNT
+ 1))
348 olsrd_write_ipcconnect
() {
350 validate_varname
"$cfg" ||
return 0
353 config_get_bool ignore
"$cfg" ignore
0
354 [ "$ignore" -ne 0 ] && return 0
356 [ "$IPCCONNECT_COUNT" -gt 0 ] && return 0
358 echo -n "${N}IpcConnect${N}{"
359 config_write_options
"$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option
"${T}"
361 IPCCONNECT_COUNT
=$
((IPCCONNECT_COUNT
+ 1))
368 validate_varname
"$cfg" ||
return 0
371 config_get_bool ignore
"$cfg" ignore
0
372 [ "$ignore" -ne 0 ] && return 0
374 config_get netaddr
"$cfg" netaddr
375 if ! validate_olsrd_option
"$netaddr"; then
376 warning_invalid_value olsrd
"$cfg" "netaddr"
380 config_get netmask
"$cfg" netmask
381 if ! validate_olsrd_option
"$netmask"; then
382 warning_invalid_value olsrd
"$cfg" "netmask"
386 [ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{"
387 echo -n "${N}${T}${T}$netaddr $netmask"
388 HNA4_COUNT
=$
((HNA4_COUNT
+ 1))
395 validate_varname
"$cfg" ||
return 0
398 config_get_bool ignore
"$cfg" ignore
0
399 [ "$ignore" -ne 0 ] && return 0
401 config_get netaddr
"$cfg" netaddr
402 if ! validate_olsrd_option
"$netaddr"; then
403 warning_invalid_value olsrd
"$cfg" "netaddr"
407 config_get prefix
"$cfg" prefix
408 if ! validate_olsrd_option
"$prefix"; then
409 warning_invalid_value olsrd
"$cfg" "prefix"
413 [ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{"
414 echo -n "${N}${T}${T}$netaddr $prefix"
415 HNA6_COUNT
=$
((HNA6_COUNT
+ 1))
420 olsrd_write_loadplugin
() {
422 validate_varname
"$cfg" ||
return 0
430 config_get_bool ignore
"$cfg" ignore
0
431 [ "$ignore" -ne 0 ] && return 0
433 config_get library
"$cfg" library
434 if ! validate_olsrd_option
"$library"; then
435 warning_invalid_value olsrd
"$cfg" "library"
438 if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then
439 echo "Warning: Plugin library '$library' not found, skipped" 1>&2
445 config_get name
"$cfg" name
446 [ -z "$name" ] && config_set
"$cfg" name
$SYSTEM_HOSTNAME
448 config_get suffix
"$cfg" suffix
449 [ -z "$suffix" ] && config_set
"$cfg" suffix
'.olsr'
451 config_get lat
"$cfg" lat
452 config_get lon
"$cfg" lon
453 config_get latlon_infile
"$cfg" latlon_infile
454 if [ \
( -z "$lat" -o -z "$lat" \
) -a -z "$latlon_infile" ]; then
455 if [ -f '/var/run/latlon.txt' ]; then
456 config_set
"$cfg" lat
''
457 config_set
"$cfg" lon
''
458 config_set
"$cfg" latlon_infile
'/var/run/latlon.txt'
460 config_set
"$cfg" lat
"$SYSTEM_LAT"
461 config_set
"$cfg" lon
"$SYSTEM_LON"
465 for f
in latlon_file hosts_file services_file resolv_file macs_file
; do
466 config_get
$f "$cfg" $f
469 [ -z "$latlon_file" ] && config_set
"$cfg" latlon_file
'/var/run/latlon.js'
472 config_get wd_file
"$cfg" file
476 echo -n "${N}LoadPlugin \"$library\"${N}{"
477 config_write_options
"$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam
"${T}"
483 olsrd_write_interface
() {
485 validate_varname
"$cfg" ||
return 0
491 config_get_bool ignore
"$cfg" ignore
0
492 [ "$ignore" -ne 0 ] && return 0
495 config_get interfaces
"$cfg" interface
496 for interface
in $interfaces; do
497 if validate_varname
"$interface"; then
498 if get_ifname
"$interface"; then
499 ifnames
="$ifnames \"$IFNAME\""
500 ifsglobal
="$ifsglobal $IFNAME"
502 echo "Warning: Interface '$interface' not found, skipped" 1>&2
505 warning_invalid_value olsrd
"$cfg" "interface"
509 [ -z "$ifnames" ] && return 0
511 echo -n "${N}Interface$ifnames${N}{"
512 config_write_options
"$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option
"${T}"
514 INTERFACES_COUNT
=$
((INTERFACES_COUNT
+ 1))
519 olsrd_write_interface_defaults
() {
521 validate_varname
"$cfg" ||
return 0
523 echo -n "${N}InterfaceDefaults$ifnames${N}{"
524 config_write_options
"$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option
"${T}"
530 olsrd_update_schema
() {
532 validate_varname
"$command" ||
return 0
534 validate_varname
"$varname" ||
return 0
536 local cfg
="$CONFIG_SECTION"
540 config_get cfgt
"$cfg" TYPE
542 olsrd
) config_update_schema OLSRD_OLSRD_SCHEMA
"$command" "$varname" "$value";;
543 IpcConnect
) config_update_schema OLSRD_IPCCONNECT_SCHEMA
"$command" "$varname" "$value";;
544 LoadPlugin
) config_update_schema OLSRD_LOADPLUGIN_SCHEMA
"$command" "$varname" "$value";;
545 Interface
) config_update_schema OLSRD_INTERFACE_SCHEMA
"$command" "$varname" "$value";;
546 InterfaceDefaults
) config_update_schema OLSRD_INTERFACE_DEFAULTS_SCHEMA
"$command" "$varname" "$value";;
552 olsrd_write_config
() {
553 OLSRD_IPVERSION_6AND4
=0
555 config_foreach olsrd_write_olsrd olsrd
557 config_foreach olsrd_write_ipcconnect IpcConnect
559 config_foreach olsrd_write_hna4 Hna4
560 [ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
562 config_foreach olsrd_write_hna6 Hna6
563 [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
564 config_foreach olsrd_write_loadplugin LoadPlugin
566 config_foreach olsrd_write_interface_defaults InterfaceDefaults
567 config_foreach olsrd_write_interface Interface
573 olsrd_setup_smartgw_rules
() {
574 # Check if ipip is installed
575 [ ! -e /etc
/modules.d
/[0-9]*-ipip ] && echo "Warning: kmod-ipip is missing. SmartGateway will not work until you install it."
577 wanifnames
=$
(ip r l e
0/0 t all |
sed -e 's/^.* dev //' |cut
-d " " -f 1 |
sort |
uniq)
579 if [ -z "$wanifnames" ]; then
583 IP4T
=$
(which iptables
)
584 IP6T
=$
(which ip6tables
)
586 # Delete smartgw firewall rules first
587 for IPT
in $IP4T $IP6T; do
588 while $IPT -D forwarding_rule
-o tnl_
+ -j ACCEPT
2> /dev
/null
; do :;done
589 for IFACE
in $wanifnames; do
590 while $IPT -D forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
2> /dev
/null
; do :; done
592 for IFACE
in $ifsglobal; do
593 while $IPT -D input_rule
-i $IFACE -p 4 -j ACCEPT
2> /dev
/null
; do :; done
596 while $IP4T -t nat
-D postrouting_rule
-o tnl_
+ -j MASQUERADE
2> /dev
/null
; do :;done
598 if [ "$smartgateway" == "yes" ]; then
599 echo "Notice: Inserting firewall rules for SmartGateway"
600 if [ ! "$smartgatewayuplink" == "none" ]; then
601 if [ "$smartgatewayuplink" == "ipv4" ]; then
602 # Allow everything to be forwarded to tnl_+ and use NAT for it
603 $IP4T -I forwarding_rule
-o tnl_
+ -j ACCEPT
604 $IP4T -t nat
-I postrouting_rule
-o tnl_
+ -j MASQUERADE
605 # Allow forwarding from tunl0 to (all) wan-interfaces
606 if [ "$nowan"="0" ]; then
607 for IFACE
in $wanifnames; do
608 $IP4T -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
611 # Allow incoming ipip on all olsr-interfaces
612 for IFACE
in $ifsglobal; do
613 $IP4T -I input_rule
-i $IFACE -p 4 -j ACCEPT
615 elif [ "$smartgatewayuplink" == "ipv6" ]; then
616 $IP6T -I forwarding_rule
-o tnl_
+ -j ACCEPT
617 if [ "$nowan"="0" ]; then
618 for IFACE
in $wanifnames; do
619 $IP6T -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
622 for IFACE
in $ifsglobal; do
623 $IP6T -I input_rule
-i $IFACE -p 4 -j ACCEPT
626 for IPT
in $IP4T $IP6T; do
627 $IPT -I forwarding_rule
-o tnl_
+ -j ACCEPT
628 $IPT -t nat
-I postrouting_rule
-o tnl_
+ -j MASQUERADE
629 if [ "$nowan"="0" ]; then
630 for IFACE
in $wanifnames; do
631 $IPT -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
634 for IFACE
in $ifsglobal; do
635 $IPT -I input_rule
-i $IFACE -p 4 -j ACCEPT
644 echo "${initscript}:" "$@" 1>&2
652 config_foreach system_config system
655 olsrd_update_schema
"option" "$@"
659 olsrd_update_schema
"list" "$@"
668 config_foreach olsrd_find_config_file olsrd
670 if [ -z "$OLSRD_CONFIG_FILE" ]; then
671 mkdir
-p -- /var
/etc
/
672 olsrd_write_config
> /var
/etc
/olsrd.conf
673 if [ "$INTERFACES_COUNT" -gt 0 -a "$OLSRD_COUNT" -gt 0 ]; then
674 OLSRD_CONFIG_FILE
=/var
/etc
/olsrd.conf
678 [ -z "$OLSRD_CONFIG_FILE" ] && return 1
681 if [ "$OLSRD_IPVERSION_6AND4" -ne 0 ]; then
682 bindv6only
="$(sysctl -n net.ipv6.bindv6only)"
683 sysctl
-w net.ipv6.bindv6only
=1 > /dev
/null
684 sed -e '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' < "$OLSRD_CONFIG_FILE" > /var
/etc
/olsrd.conf.ipv6
685 sed -e 's/^IpVersion[ ][ ]*6$/IpVersion 4/' -e 's/^\t\t[A-Fa-f0-9.:]*[:][A-Fa-f0-9.:]*[ ][0-9]*$//' < "$OLSRD_CONFIG_FILE" > /var
/etc
/olsrd.conf.ipv4
686 rm $OLSRD_CONFIG_FILE
688 # some filenames should get the suffix .ipv6
689 for file in $latlon_file $hosts_file $services_file $resolv_file $macs_file $wd_file;do
690 f
=$
(echo $file|
sed 's/\//\\\//g')
691 sed -i "s/$f/$f.ipv6/g" /var
/etc
/olsrd.conf.ipv6
694 SERVICE_PID_FILE
="$PID6"
695 if service_check
/usr
/sbin
/olsrd
; then
696 error
"there already is an IPv6 instance of olsrd running ($(cat $PID6)), not starting."
698 service_start
/usr
/sbin
/olsrd
-f /var
/etc
/olsrd.conf.ipv6
-nofork
701 SERVICE_PID_FILE
="$PID"
702 if service_check
/usr
/sbin
/olsrd
; then
703 error
"there already is an IPv4 instance of olsrd running ($(cat $PID)), not starting."
705 service_start
/usr
/sbin
/olsrd
-f /var
/etc
/olsrd.conf.ipv4
-nofork
709 sysctl
-w net.ipv6.bindv6only
="$bindv6only" > /dev
/null
713 if [ "$ipversion" = "6" ]; then
714 sed -i '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' "$OLSRD_CONFIG_FILE"
717 SERVICE_PID_FILE
="$PID"
718 if service_check
/usr
/sbin
/olsrd
; then
719 error
"there already is an IPv4 instance of olsrd running ($(cat $PID)), not starting."
722 service_start
/usr
/sbin
/olsrd
-f "$OLSRD_CONFIG_FILE" -nofork
725 olsrd_setup_smartgw_rules
729 SERVICE_PID_FILE
="$PID"
730 service_stop
/usr
/sbin
/olsrd
732 SERVICE_PID_FILE
="$PID6"
733 service_stop
/usr
/sbin
/olsrd