1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2011 OpenWrt.org
6 CONF
=/var
/etc
/olsrd.conf
8 PID6
=/var
/run
/olsrd.ipv6.pid
10 OLSRD_OLSRD_SCHEMA
='ignore:internal config_file:internal DebugLevel=0 AllowNoInt=yes'
11 OLSRD_IPCCONNECT_SCHEMA
='ignore:internal Host:list Net:list2'
12 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'
13 OLSRD_INTERFACE_SCHEMA
='ignore:internal interface:internal AutoDetectChanges:bool'
14 OLSRD_INTERFACE_DEFAULTS_SCHEMA
='AutoDetectChanges:bool'
22 [ -z "$varname" -o "$varname" != "${varname%%[!A-Za-z0-9_]*}" ] && return 1
28 [ -z "$ifname" -o "$ifname" != "${ifname%%[!A-Za-z0-9.:_-]*}" ] && return 1
32 validate_olsrd_option
() {
34 [ -z "$str" -o "$str" != "${str%%[! 0-9A-Za-z./|:_-]*}" ] && return 1
41 validate_varname
"$interface" ||
return 1
44 config_get ifname
"$interface" ifname
45 validate_ifname
"$ifname" ||
return 1
56 config_get cfgt
"$cfg" TYPE
58 if [ "$cfgt" = "system" ]; then
59 config_get hostname
"$cfg" hostname
60 hostname
="${hostname:-OpenWrt}"
61 SYSTEM_HOSTNAME
="$hostname"
64 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
65 config_get latlon
"$cfg" latlon
73 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
74 config_get latlon
"$cfg" latitude
76 config_get latlon
"$cfg" longitude
81 olsrd_find_config_file
() {
83 validate_varname
"$cfg" ||
return 0
85 config_get_bool ignore
"$cfg" ignore
0
86 [ "$ignore" -ne 0 ] && return 0
87 config_get OLSRD_CONFIG_FILE
"$cfg" config_file
92 warning_invalid_value
() {
94 validate_varname
"$package" || package
=
96 validate_varname
"$config" || config
=
98 validate_varname
"$option" || option
=
100 echo -n "Warning: Invalid value" 1>&2
102 if [ -n "$package" -a -n "$config" ]; then
103 echo -n " in option '$package.$config${option:+.}$option'" 1>&2
106 echo ", skipped" 1>&2
111 olsrd_write_option
() {
114 validate_varname
"$cfg" ||
return 1
116 validate_varname
"$option" ||
return 1
118 local option_type
="$5"
120 if [ "$option_type" = bool
]; then
122 1|on|true|enabled|
yes) value
=yes;;
123 0|off|false|disabled|no
) value
=no
;;
124 *) warning_invalid_value olsrd
"$cfg" "$option"; return 1;;
128 if ! validate_olsrd_option
"$value"; then
129 warning_invalid_value olsrd
"$cfg" "$option"
133 if [ "$value" != "${value%%[G-Zg-z_-]*}" ]; then
134 if [ "$option" != "Ip6AddrType" -a "$option" != "LinkQualityMult" -a "$value" != "yes" -a "$value" != "no" ]; then
139 echo -n "${N}$param$option $value"
144 olsrd_write_plparam
() {
147 validate_varname
"$cfg" ||
return 1
149 validate_varname
"$option" ||
return 1
151 local option_type
="$5"
154 if [ "$option_type" = bool
]; then
156 1|on|true|enabled|
yes) value
=yes;;
157 0|off|false|disabled|no
) value
=no
;;
158 *) warning_invalid_value olsrd
"$cfg" "$option"; return 1;;
162 if ! validate_olsrd_option
"$value"; then
163 warning_invalid_value olsrd
"$cfg" "$option"
172 if [ "$option" = 'hosts' ]; then
178 if [ "$option" = 'NonOlsrIf' ]; then
179 if validate_varname
"$value"; then
180 if get_ifname
"$value"; then
182 echo "Info: mdns Interface '$value' ifname '$ifname' found" 1>&2
184 echo "Warning: mdns Interface '$value' not found, skipped" 1>&2
187 warning_invalid_value olsrd
"$cfg" "NonOlsrIf"
189 [ -z "$ifname" ] || value
=$ifname
192 echo -n "${N}${param}PlParam \"$option\" \"$value\""
197 config_update_schema
() {
199 local schema_varname
="$1"
200 validate_varname
"$schema_varname" ||
return 1
202 validate_varname
"$command" ||
return 1
204 validate_varname
"$option" ||
return 1
214 eval "export -n -- \"schema=\${$schema_varname}\""
216 for cur_option
in $schema; do
217 [ "${cur_option%%[:=]*}" = "$option" ] && return 0
220 if [ "$command" = list
]; then
222 if [ "$#" -ge "3" ]; then
223 schema_entry
="$option:list3"
224 elif [ "$#" -ge "2" ]; then
225 schema_entry
="$option:list2"
227 schema_entry
="$option:list"
230 schema_entry
="$option"
233 append
"$schema_varname" "$schema_entry"
238 config_write_options
() {
242 validate_varname
"$cfg" ||
return 1
243 local write_func
="$3"
244 [ -z "$write_func" ] && output_func
=echo
245 local write_param
="$4"
258 for schema_entry
in $schema; do
259 default
="${schema_entry#*[=]}"
260 [ "$default" = "$schema_entry" ] && default
=
261 option
="${schema_entry%%[=]*}"
267 validate_varname
"$option" ||
continue
268 [ -z "$option_type" ] || validate_varname
"$option_type" ||
continue
269 [ "$option_type" = internal
] && continue
270 config_get value
"$cfg" "$option"
272 if [ -z "$value" ]; then
277 elif [ "$value" = '-' -a -n "$default" ]; then
281 [ -z "$value" ] && continue
283 case "$option_type" in
290 if [ "$list_size" -gt 0 ]; then
291 config_get option_length
"$cfg" "${option}_LENGTH"
292 if [ -n "$option_length" ]; then
294 while [ "$i" -le "$option_length" ]; do
295 config_get list_value
"$cfg" "${option}_ITEM$i"
296 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" ||
break
302 for list_item
in $value; do
303 append
"list_value" "$list_item"
305 position
=$
((i
% list_size
))
306 if [ "$position" -eq 0 ]; then
307 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" ||
break
311 [ "$position" -ne 0 ] && "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type"
314 "$write_func" "$write_param" "$cfg" "$option" "$value" "$option_type"
321 olsrd_write_olsrd
() {
323 validate_varname
"$cfg" ||
return 0
326 config_get_bool ignore
"$cfg" ignore
0
327 [ "$ignore" -ne 0 ] && return 0
329 [ "$OLSRD_COUNT" -gt 0 ] && return 0
331 config_get ipversion
"$cfg" IpVersion
332 if [ "$ipversion" = "6and4" ]; then
333 OLSRD_IPVERSION_6AND4
=1
334 config_set
"$cfg" IpVersion
'6'
336 config_get smartgateway
"$cfg" SmartGateway
337 config_get smartgatewayuplink
"$cfg" SmartGatewayUplink
339 config_write_options
"$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option
341 OLSRD_COUNT
=$
((OLSRD_COUNT
+ 1))
345 olsrd_write_ipcconnect
() {
347 validate_varname
"$cfg" ||
return 0
350 config_get_bool ignore
"$cfg" ignore
0
351 [ "$ignore" -ne 0 ] && return 0
353 [ "$IPCCONNECT_COUNT" -gt 0 ] && return 0
355 echo -n "${N}IpcConnect${N}{"
356 config_write_options
"$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option
"${T}"
358 IPCCONNECT_COUNT
=$
((IPCCONNECT_COUNT
+ 1))
365 validate_varname
"$cfg" ||
return 0
368 config_get_bool ignore
"$cfg" ignore
0
369 [ "$ignore" -ne 0 ] && return 0
371 config_get netaddr
"$cfg" netaddr
372 if ! validate_olsrd_option
"$netaddr"; then
373 warning_invalid_value olsrd
"$cfg" "netaddr"
377 config_get netmask
"$cfg" netmask
378 if ! validate_olsrd_option
"$netmask"; then
379 warning_invalid_value olsrd
"$cfg" "netmask"
383 [ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{"
384 echo -n "${N}${T}${T}$netaddr $netmask"
385 HNA4_COUNT
=$
((HNA4_COUNT
+ 1))
392 validate_varname
"$cfg" ||
return 0
395 config_get_bool ignore
"$cfg" ignore
0
396 [ "$ignore" -ne 0 ] && return 0
398 config_get netaddr
"$cfg" netaddr
399 if ! validate_olsrd_option
"$netaddr"; then
400 warning_invalid_value olsrd
"$cfg" "netaddr"
404 config_get prefix
"$cfg" prefix
405 if ! validate_olsrd_option
"$prefix"; then
406 warning_invalid_value olsrd
"$cfg" "prefix"
410 [ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{"
411 echo -n "${N}${T}${T}$netaddr $prefix"
412 HNA6_COUNT
=$
((HNA6_COUNT
+ 1))
417 olsrd_write_loadplugin
() {
419 validate_varname
"$cfg" ||
return 0
427 config_get_bool ignore
"$cfg" ignore
0
428 [ "$ignore" -ne 0 ] && return 0
430 config_get library
"$cfg" library
431 if ! validate_olsrd_option
"$library"; then
432 warning_invalid_value olsrd
"$cfg" "library"
435 if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then
436 echo "Warning: Plugin library '$library' not found, skipped" 1>&2
442 config_get name
"$cfg" name
443 [ -z "$name" ] && config_set
"$cfg" name
$SYSTEM_HOSTNAME
445 config_get suffix
"$cfg" suffix
446 [ -z "$suffix" ] && config_set
"$cfg" suffix
'.olsr'
448 config_get lat
"$cfg" lat
449 config_get lon
"$cfg" lon
450 config_get latlon_infile
"$cfg" latlon_infile
451 if [ \
( -z "$lat" -o -z "$lat" \
) -a -z "$latlon_infile" ]; then
452 if [ -f '/var/run/latlon.txt' ]; then
453 config_set
"$cfg" lat
''
454 config_set
"$cfg" lon
''
455 config_set
"$cfg" latlon_infile
'/var/run/latlon.txt'
457 config_set
"$cfg" lat
"$SYSTEM_LAT"
458 config_set
"$cfg" lon
"$SYSTEM_LON"
462 for f
in latlon_file hosts_file services_file resolv_file macs_file
; do
463 config_get
$f "$cfg" $f
466 [ -z "$latlon_file" ] && config_set
"$cfg" latlon_file
'/var/run/latlon.js'
469 config_get wd_file
"$cfg" file
473 echo -n "${N}LoadPlugin \"$library\"${N}{"
474 config_write_options
"$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam
"${T}"
480 olsrd_write_interface
() {
482 validate_varname
"$cfg" ||
return 0
488 config_get_bool ignore
"$cfg" ignore
0
489 [ "$ignore" -ne 0 ] && return 0
492 config_get interfaces
"$cfg" interface
493 for interface
in $interfaces; do
494 if validate_varname
"$interface"; then
495 if get_ifname
"$interface"; then
496 ifnames
="$ifnames \"$IFNAME\""
497 ifsglobal
="$ifsglobal $IFNAME"
499 echo "Warning: Interface '$interface' not found, skipped" 1>&2
502 warning_invalid_value olsrd
"$cfg" "interface"
506 [ -z "$ifnames" ] && return 0
508 echo -n "${N}Interface$ifnames${N}{"
509 config_write_options
"$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option
"${T}"
511 INTERFACES_COUNT
=$
((INTERFACES_COUNT
+ 1))
516 olsrd_write_interface_defaults
() {
518 validate_varname
"$cfg" ||
return 0
520 echo -n "${N}InterfaceDefaults$ifnames${N}{"
521 config_write_options
"$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option
"${T}"
527 olsrd_update_schema
() {
529 validate_varname
"$command" ||
return 0
531 validate_varname
"$varname" ||
return 0
533 local cfg
="$CONFIG_SECTION"
537 config_get cfgt
"$cfg" TYPE
539 olsrd
) config_update_schema OLSRD_OLSRD_SCHEMA
"$command" "$varname" "$value";;
540 IpcConnect
) config_update_schema OLSRD_IPCCONNECT_SCHEMA
"$command" "$varname" "$value";;
541 LoadPlugin
) config_update_schema OLSRD_LOADPLUGIN_SCHEMA
"$command" "$varname" "$value";;
542 Interface
) config_update_schema OLSRD_INTERFACE_SCHEMA
"$command" "$varname" "$value";;
543 InterfaceDefaults
) config_update_schema OLSRD_INTERFACE_DEFAULTS_SCHEMA
"$command" "$varname" "$value";;
549 olsrd_write_config
() {
550 OLSRD_IPVERSION_6AND4
=0
552 config_foreach olsrd_write_olsrd olsrd
554 config_foreach olsrd_write_ipcconnect IpcConnect
556 config_foreach olsrd_write_hna4 Hna4
557 [ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
559 config_foreach olsrd_write_hna6 Hna6
560 [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
561 config_foreach olsrd_write_loadplugin LoadPlugin
563 config_foreach olsrd_write_interface_defaults InterfaceDefaults
564 config_foreach olsrd_write_interface Interface
570 olsrd_setup_smartgw_rules
() {
571 # Check if ipip is installed
572 [ ! -e /etc
/modules.d
/[0-9]*-ipip ] && echo "Warning: kmod-ipip is missing. SmartGateway will not work until you install it."
574 wanifnames
=$
(ip r l e
0/0 t all |
sed -e 's/^.* dev //' |cut
-d " " -f 1 |
sort |
uniq)
576 if [ -z "$wanifnames" ]; then
580 IP4T
=$
(which iptables
)
581 IP6T
=$
(which ip6tables
)
583 # Delete smartgw firewall rules first
584 for IPT
in $IP4T $IP6T; do
585 while $IPT -D forwarding_rule
-o tnl_
+ -j ACCEPT
2> /dev
/null
; do :;done
586 for IFACE
in $wanifnames; do
587 while $IPT -D forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
2> /dev
/null
; do :; done
589 for IFACE
in $ifsglobal; do
590 while $IPT -D input_rule
-i $IFACE -p 4 -j ACCEPT
2> /dev
/null
; do :; done
593 while $IP4T -t nat
-D postrouting_rule
-o tnl_
+ -j MASQUERADE
2> /dev
/null
; do :;done
595 if [ "$smartgateway" == "yes" ]; then
596 echo "Notice: Inserting firewall rules for SmartGateway"
597 if [ ! "$smartgatewayuplink" == "none" ]; then
598 if [ "$smartgatewayuplink" == "ipv4" ]; then
599 # Allow everything to be forwarded to tnl_+ and use NAT for it
600 $IP4T -I forwarding_rule
-o tnl_
+ -j ACCEPT
601 $IP4T -t nat
-I postrouting_rule
-o tnl_
+ -j MASQUERADE
602 # Allow forwarding from tunl0 to (all) wan-interfaces
603 if [ "$nowan"="0" ]; then
604 for IFACE
in $wanifnames; do
605 $IP4T -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
608 # Allow incoming ipip on all olsr-interfaces
609 for IFACE
in $ifsglobal; do
610 $IP4T -I input_rule
-i $IFACE -p 4 -j ACCEPT
612 elif [ "$smartgatewayuplink" == "ipv6" ]; then
613 $IP6T -I forwarding_rule
-o tnl_
+ -j ACCEPT
614 if [ "$nowan"="0" ]; then
615 for IFACE
in $wanifnames; do
616 $IP6T -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
619 for IFACE
in $ifsglobal; do
620 $IP6T -I input_rule
-i $IFACE -p 4 -j ACCEPT
623 for IPT
in $IP4T $IP6T; do
624 $IPT -I forwarding_rule
-o tnl_
+ -j ACCEPT
625 $IPT -t nat
-I postrouting_rule
-o tnl_
+ -j MASQUERADE
626 if [ "$nowan"="0" ]; then
627 for IFACE
in $wanifnames; do
628 $IPT -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
631 for IFACE
in $ifsglobal; do
632 $IPT -I input_rule
-i $IFACE -p 4 -j ACCEPT
645 config_foreach system_config system
648 olsrd_update_schema
"option" "$@"
652 olsrd_update_schema
"list" "$@"
661 config_foreach olsrd_find_config_file olsrd
663 if [ -z "$OLSRD_CONFIG_FILE" ]; then
664 mkdir
-p -- /var
/etc
/
665 olsrd_write_config
> /var
/etc
/olsrd.conf
666 if [ "$INTERFACES_COUNT" -gt 0 -a "$OLSRD_COUNT" -gt 0 ]; then
667 OLSRD_CONFIG_FILE
=/var
/etc
/olsrd.conf
671 [ -z "$OLSRD_CONFIG_FILE" ] && return 1
674 if [ "$OLSRD_IPVERSION_6AND4" -ne 0 ]; then
675 bindv6only
="$(sysctl -n net.ipv6.bindv6only)"
676 sysctl
-w net.ipv6.bindv6only
=1 > /dev
/null
677 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
678 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
679 rm $OLSRD_CONFIG_FILE
681 # some filenames should get the suffix .ipv6
682 for file in $latlon_file $hosts_file $services_file $resolv_file $macs_file $wd_file;do
683 f
=$
(echo $file|
sed 's/\//\\\//g')
684 sed -i "s/$f/$f.ipv6/g" /var
/etc
/olsrd.conf.ipv6
687 if ([ -s $PID6 ] && kill -0 $
(cat $PID6) 2>&-); then
688 echo "Error: There already is a IPv6 instance of olsrd running ($(cat $PID6)), not starting."
690 start-stop-daemon
-q -b -m -p $PID6 -x $BIN -S -- -f /var
/etc
/olsrd.conf.ipv6
-nofork
693 if ([ -s $PID ] && kill -0 $
(cat $PID) 2>&-); then
694 echo "Error: There already is a IPv4 instance of olsrd running ($(cat $PID)), not starting."
696 start-stop-daemon
-q -b -m -p $PID -x $BIN -S -- -f /var
/etc
/olsrd.conf.ipv4
-nofork
700 sysctl
-w net.ipv6.bindv6only
="$bindv6only" > /dev
/null
704 if [ "$ipversion" = "6" ]; then
705 sed -i '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' "$OLSRD_CONFIG_FILE"
708 ### check for running instance
709 if ([ -s $PID ] && kill -0 $
(cat $PID) 2>&-); then
710 echo "Error: There already is a instance of olsrd running ($(cat $PID)), exiting now."
713 start-stop-daemon
-q -b -m -p $PID -x $BIN -S -- -f "$OLSRD_CONFIG_FILE" -nofork
716 olsrd_setup_smartgw_rules
720 start-stop-daemon
-q -p $PID -x $BIN -K
721 start-stop-daemon
-q -p $PID6 -x $BIN -K
722 test -s $PID && rm $PID
723 test -s $PID6 && rm $PID6