1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008 Alina Friedrichsen <x-alina@gmx.net>
3 # Special thanks to bittorf wireless ))
7 CONF=/var/etc/olsrd.conf
9 PID6=/var/run/olsrd.ipv6.pid
11 OLSRD_OLSRD_SCHEMA='ignore:internal config_file:internal DebugLevel=0 AllowNoInt=yes'
12 OLSRD_IPCCONNECT_SCHEMA='ignore:internal Host:list Net:list2'
13 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'
14 OLSRD_INTERFACE_SCHEMA='ignore:internal interface:internal AutoDetectChanges:bool'
15 OLSRD_INTERFACE_DEFAULTS_SCHEMA='AutoDetectChanges:bool'
23 [ -z "$varname" -o "$varname" != "${varname%%[!A-Za-z0-9_]*}" ] && return 1
29 [ -z "$ifname" -o "$ifname" != "${ifname%%[!A-Za-z0-9.:_-]*}" ] && return 1
33 validate_olsrd_option() {
35 [ -z "$str" -o "$str" != "${str%%[! 0-9A-Za-z./|:_-]*}" ] && return 1
42 validate_varname "$interface" || return 1
45 config_get ifname "$interface" ifname
46 validate_ifname "$ifname" || return 1
57 config_get cfgt "$cfg" TYPE
59 if [ "$cfgt" = "system" ]; then
60 config_get hostname "$cfg" hostname
61 hostname="${hostname:-OpenWrt}"
62 SYSTEM_HOSTNAME="$hostname"
65 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
66 config_get latlon "$cfg" latlon
74 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
75 config_get latlon "$cfg" latitude
77 config_get latlon "$cfg" longitude
82 olsrd_find_config_file() {
84 validate_varname "$cfg" || return 0
86 config_get_bool ignore "$cfg" ignore 0
87 [ "$ignore" -ne 0 ] && return 0
88 config_get OLSRD_CONFIG_FILE "$cfg" config_file
93 warning_invalid_value() {
95 validate_varname "$package" || package=
97 validate_varname "$config" || config=
99 validate_varname "$option" || option=
101 echo -n "Warning: Invalid value" 1>&2
103 if [ -n "$package" -a -n "$config" ]; then
104 echo -n " in option '$package.$config${option:+.}$option'" 1>&2
107 echo ", skipped" 1>&2
112 olsrd_write_option() {
115 validate_varname "$cfg" || return 1
117 validate_varname "$option" || return 1
119 local option_type="$5"
121 if [ "$option_type" = bool ]; then
123 1|on|true|enabled|yes) value=yes;;
124 0|off|false|disabled|no) value=no;;
125 *) warning_invalid_value olsrd "$cfg" "$option"; return 1;;
129 if ! validate_olsrd_option "$value"; then
130 warning_invalid_value olsrd "$cfg" "$option"
134 if [ "$value" != "${value%%[G-Zg-z_-]*}" ]; then
135 if [ "$option" != "Ip6AddrType" -a "$option" != "LinkQualityMult" -a "$value" != "yes" -a "$value" != "no" ]; then
140 echo -n "${N}$param$option $value"
145 olsrd_write_plparam() {
148 validate_varname "$cfg" || return 1
150 validate_varname "$option" || return 1
152 local option_type="$5"
155 if [ "$option_type" = bool ]; then
157 1|on|true|enabled|yes) value=yes;;
158 0|off|false|disabled|no) value=no;;
159 *) warning_invalid_value olsrd "$cfg" "$option"; return 1;;
163 if ! validate_olsrd_option "$value"; then
164 warning_invalid_value olsrd "$cfg" "$option"
173 if [ "$option" = 'hosts' ]; then
179 if [ "$option" = 'NonOlsrIf' ]; then
180 if validate_varname "$value"; then
181 if get_ifname "$value"; then
183 echo "Info: mdns Interface '$value' ifname '$ifname' found" 1>&2
185 echo "Warning: mdns Interface '$value' not found, skipped" 1>&2
188 warning_invalid_value olsrd "$cfg" "NonOlsrIf"
190 [ -z "$ifname" ] || value=$ifname
193 echo -n "${N}${param}PlParam \"$option\" \"$value\""
198 config_update_schema() {
200 local schema_varname="$1"
201 validate_varname "$schema_varname" || return 1
203 validate_varname "$command" || return 1
205 validate_varname "$option" || return 1
215 eval "export -n -- \"schema=\${$schema_varname}\""
217 for cur_option in $schema; do
218 [ "${cur_option%%[:=]*}" = "$option" ] && return 0
221 if [ "$command" = list ]; then
223 if [ "$#" -ge "3" ]; then
224 schema_entry="$option:list3"
225 elif [ "$#" -ge "2" ]; then
226 schema_entry="$option:list2"
228 schema_entry="$option:list"
231 schema_entry="$option"
234 append "$schema_varname" "$schema_entry"
239 config_write_options() {
243 validate_varname "$cfg" || return 1
244 local write_func="$3"
245 [ -z "$write_func" ] && output_func=echo
246 local write_param="$4"
259 for schema_entry in $schema; do
260 default="${schema_entry#*[=]}"
261 [ "$default" = "$schema_entry" ] && default=
262 option="${schema_entry%%[=]*}"
268 validate_varname "$option" || continue
269 [ -z "$option_type" ] || validate_varname "$option_type" || continue
270 [ "$option_type" = internal ] && continue
271 config_get value "$cfg" "$option"
273 if [ -z "$value" ]; then
278 elif [ "$value" = '-' -a -n "$default" ]; then
282 [ -z "$value" ] && continue
284 case "$option_type" in
291 if [ "$list_size" -gt 0 ]; then
292 config_get option_length "$cfg" "${option}_LENGTH"
293 if [ -n "$option_length" ]; then
295 while [ "$i" -le "$option_length" ]; do
296 config_get list_value "$cfg" "${option}_ITEM$i"
297 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" || break
303 for list_item in $value; do
304 append "list_value" "$list_item"
306 position=$((i % list_size))
307 if [ "$position" -eq 0 ]; then
308 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" || break
312 [ "$position" -ne 0 ] && "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type"
315 "$write_func" "$write_param" "$cfg" "$option" "$value" "$option_type"
322 olsrd_write_olsrd() {
324 validate_varname "$cfg" || return 0
327 config_get_bool ignore "$cfg" ignore 0
328 [ "$ignore" -ne 0 ] && return 0
330 [ "$OLSRD_COUNT" -gt 0 ] && return 0
332 config_get ipversion "$cfg" IpVersion
333 if [ "$ipversion" = "6and4" ]; then
334 OLSRD_IPVERSION_6AND4=1
335 config_set "$cfg" IpVersion '6'
337 config_get smartgateway "$cfg" SmartGateway
338 config_get smartgatewayuplink "$cfg" SmartGatewayUplink
340 config_write_options "$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option
342 OLSRD_COUNT=$((OLSRD_COUNT + 1))
346 olsrd_write_ipcconnect() {
348 validate_varname "$cfg" || return 0
351 config_get_bool ignore "$cfg" ignore 0
352 [ "$ignore" -ne 0 ] && return 0
354 [ "$IPCCONNECT_COUNT" -gt 0 ] && return 0
356 echo -n "${N}IpcConnect${N}{"
357 config_write_options "$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option "${T}"
359 IPCCONNECT_COUNT=$((IPCCONNECT_COUNT + 1))
366 validate_varname "$cfg" || return 0
369 config_get_bool ignore "$cfg" ignore 0
370 [ "$ignore" -ne 0 ] && return 0
372 config_get netaddr "$cfg" netaddr
373 if ! validate_olsrd_option "$netaddr"; then
374 warning_invalid_value olsrd "$cfg" "netaddr"
378 config_get netmask "$cfg" netmask
379 if ! validate_olsrd_option "$netmask"; then
380 warning_invalid_value olsrd "$cfg" "netmask"
384 [ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{"
385 echo -n "${N}${T}${T}$netaddr $netmask"
386 HNA4_COUNT=$((HNA4_COUNT + 1))
393 validate_varname "$cfg" || return 0
396 config_get_bool ignore "$cfg" ignore 0
397 [ "$ignore" -ne 0 ] && return 0
399 config_get netaddr "$cfg" netaddr
400 if ! validate_olsrd_option "$netaddr"; then
401 warning_invalid_value olsrd "$cfg" "netaddr"
405 config_get prefix "$cfg" prefix
406 if ! validate_olsrd_option "$prefix"; then
407 warning_invalid_value olsrd "$cfg" "prefix"
411 [ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{"
412 echo -n "${N}${T}${T}$netaddr $prefix"
413 HNA6_COUNT=$((HNA6_COUNT + 1))
418 olsrd_write_loadplugin() {
420 validate_varname "$cfg" || return 0
428 config_get_bool ignore "$cfg" ignore 0
429 [ "$ignore" -ne 0 ] && return 0
431 config_get library "$cfg" library
432 if ! validate_olsrd_option "$library"; then
433 warning_invalid_value olsrd "$cfg" "library"
436 if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then
437 echo "Warning: Plugin library '$library' not found, skipped" 1>&2
443 config_get name "$cfg" name
444 [ -z "$name" ] && config_set "$cfg" name $SYSTEM_HOSTNAME
446 config_get suffix "$cfg" suffix
447 [ -z "$suffix" ] && config_set "$cfg" suffix '.olsr'
449 config_get lat "$cfg" lat
450 config_get lon "$cfg" lon
451 config_get latlon_infile "$cfg" latlon_infile
452 if [ \( -z "$lat" -o -z "$lat" \) -a -z "$latlon_infile" ]; then
453 if [ -f '/var/run/latlon.txt' ]; then
454 config_set "$cfg" lat ''
455 config_set "$cfg" lon ''
456 config_set "$cfg" latlon_infile '/var/run/latlon.txt'
458 config_set "$cfg" lat "$SYSTEM_LAT"
459 config_set "$cfg" lon "$SYSTEM_LON"
463 for f in latlon_file hosts_file services_file resolv_file macs_file; do
464 config_get $f "$cfg" $f
467 [ -z "$latlon_file" ] && config_set "$cfg" latlon_file '/var/run/latlon.js'
470 config_get wd_file "$cfg" file
474 echo -n "${N}LoadPlugin \"$library\"${N}{"
475 config_write_options "$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam "${T}"
481 olsrd_write_interface() {
483 validate_varname "$cfg" || return 0
489 config_get_bool ignore "$cfg" ignore 0
490 [ "$ignore" -ne 0 ] && return 0
493 config_get interfaces "$cfg" interface
494 for interface in $interfaces; do
495 if validate_varname "$interface"; then
496 if get_ifname "$interface"; then
497 ifnames="$ifnames \"$IFNAME\""
498 ifsglobal="$ifsglobal $IFNAME"
500 echo "Warning: Interface '$interface' not found, skipped" 1>&2
503 warning_invalid_value olsrd "$cfg" "interface"
507 [ -z "$ifnames" ] && return 0
509 echo -n "${N}Interface$ifnames${N}{"
510 config_write_options "$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option "${T}"
512 INTERFACES_COUNT=$((INTERFACES_COUNT + 1))
517 olsrd_write_interface_defaults() {
519 validate_varname "$cfg" || return 0
521 echo -n "${N}InterfaceDefaults$ifnames${N}{"
522 config_write_options "$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option "${T}"
528 olsrd_update_schema() {
530 validate_varname "$command" || return 0
532 validate_varname "$varname" || return 0
534 local cfg="$CONFIG_SECTION"
538 config_get cfgt "$cfg" TYPE
540 olsrd) config_update_schema OLSRD_OLSRD_SCHEMA "$command" "$varname" "$value";;
541 IpcConnect) config_update_schema OLSRD_IPCCONNECT_SCHEMA "$command" "$varname" "$value";;
542 LoadPlugin) config_update_schema OLSRD_LOADPLUGIN_SCHEMA "$command" "$varname" "$value";;
543 Interface) config_update_schema OLSRD_INTERFACE_SCHEMA "$command" "$varname" "$value";;
544 InterfaceDefaults) config_update_schema OLSRD_INTERFACE_DEFAULTS_SCHEMA "$command" "$varname" "$value";;
550 olsrd_write_config() {
551 OLSRD_IPVERSION_6AND4=0
553 config_foreach olsrd_write_olsrd olsrd
555 config_foreach olsrd_write_ipcconnect IpcConnect
557 config_foreach olsrd_write_hna4 Hna4
558 [ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
560 config_foreach olsrd_write_hna6 Hna6
561 [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
562 config_foreach olsrd_write_loadplugin LoadPlugin
564 config_foreach olsrd_write_interface_defaults InterfaceDefaults
565 config_foreach olsrd_write_interface Interface
571 olsrd_setup_smartgw_rules() {
572 # Check if ipip is installed
573 [ ! -e /etc/modules.d/[0-9]*-ipip ] && echo "Warning: kmod-ipip is missing. SmartGateway will not work until you install it."
575 wanifnames=$(ip r l e 0/0 t all | sed -e 's/^.* dev //' |cut -d " " -f 1 | sort | uniq)
577 if [ -z "$wanifnames" ]; then
581 IP4T=$(which iptables)
582 IP6T=$(which ip6tables)
584 # Delete smartgw firewall rules first
585 for IPT in $IP4T $IP6T; do
586 while $IPT -D forwarding_rule -o tnl_+ -j ACCEPT 2> /dev/null; do :;done
587 for IFACE in $wanifnames; do
588 while $IPT -D forwarding_rule -i tunl0 -o $IFACE -j ACCEPT 2> /dev/null; do :; done
590 for IFACE in $ifsglobal; do
591 while $IPT -D input_rule -i $IFACE -p 4 -j ACCEPT 2> /dev/null; do :; done
594 while $IP4T -t nat -D postrouting_rule -o tnl_+ -j MASQUERADE 2> /dev/null; do :;done
596 if [ "$smartgateway" == "yes" ]; then
597 echo "Notice: Inserting firewall rules for SmartGateway"
598 if [ ! "$smartgatewayuplink" == "none" ]; then
599 if [ "$smartgatewayuplink" == "ipv4" ]; then
600 # Allow everything to be forwarded to tnl_+ and use NAT for it
601 $IP4T -I forwarding_rule -o tnl_+ -j ACCEPT
602 $IP4T -t nat -I postrouting_rule -o tnl_+ -j MASQUERADE
603 # Allow forwarding from tunl0 to (all) wan-interfaces
604 if [ "$nowan"="0" ]; then
605 for IFACE in $wanifnames; do
606 $IP4T -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
609 # Allow incoming ipip on all olsr-interfaces
610 for IFACE in $ifsglobal; do
611 $IP4T -I input_rule -i $IFACE -p 4 -j ACCEPT
613 elif [ "$smartgatewayuplink" == "ipv6" ]; then
614 $IP6T -I forwarding_rule -o tnl_+ -j ACCEPT
615 if [ "$nowan"="0" ]; then
616 for IFACE in $wanifnames; do
617 $IP6T -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
620 for IFACE in $ifsglobal; do
621 $IP6T -I input_rule -i $IFACE -p 4 -j ACCEPT
624 for IPT in $IP4T $IP6T; do
625 $IPT -I forwarding_rule -o tnl_+ -j ACCEPT
626 $IPT -t nat -I postrouting_rule -o tnl_+ -j MASQUERADE
627 if [ "$nowan"="0" ]; then
628 for IFACE in $wanifnames; do
629 $IPT -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
632 for IFACE in $ifsglobal; do
633 $IPT -I input_rule -i $IFACE -p 4 -j ACCEPT
646 config_foreach system_config system
649 olsrd_update_schema "option" "$@"
653 olsrd_update_schema "list" "$@"
662 config_foreach olsrd_find_config_file olsrd
664 if [ -z "$OLSRD_CONFIG_FILE" ]; then
665 mkdir -p -- /var/etc/
666 olsrd_write_config > /var/etc/olsrd.conf
667 if [ "$INTERFACES_COUNT" -gt 0 -a "$OLSRD_COUNT" -gt 0 ]; then
668 OLSRD_CONFIG_FILE=/var/etc/olsrd.conf
672 [ -z "$OLSRD_CONFIG_FILE" ] && return 1
675 if [ "$OLSRD_IPVERSION_6AND4" -ne 0 ]; then
676 bindv6only="$(sysctl -n net.ipv6.bindv6only)"
677 sysctl -w net.ipv6.bindv6only=1 > /dev/null
678 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
679 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
680 rm $OLSRD_CONFIG_FILE
682 # some filenames should get the suffix .ipv6
683 for file in $latlon_file $hosts_file $services_file $resolv_file $macs_file $wd_file;do
684 f=$(echo $file|sed 's/\//\\\//g')
685 sed -i "s/$f/$f.ipv6/g" /var/etc/olsrd.conf.ipv6
688 if ([ -s $PID6 ] && kill -0 $(cat $PID6) 2>&-); then
689 echo "Error: There already is a IPv6 instance of olsrd running ($(cat $PID6)), not starting."
691 start-stop-daemon -q -b -m -p $PID6 -x $BIN -S -- -f /var/etc/olsrd.conf.ipv6 -nofork
694 if ([ -s $PID ] && kill -0 $(cat $PID) 2>&-); then
695 echo "Error: There already is a IPv4 instance of olsrd running ($(cat $PID)), not starting."
697 start-stop-daemon -q -b -m -p $PID -x $BIN -S -- -f /var/etc/olsrd.conf.ipv4 -nofork
701 sysctl -w net.ipv6.bindv6only="$bindv6only" > /dev/null
705 if [ "$ipversion" = "6" ]; then
706 sed -i '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' "$OLSRD_CONFIG_FILE"
709 ### check for running instance
710 if ([ -s $PID ] && kill -0 $(cat $PID) 2>&-); then
711 echo "Error: There already is a instance of olsrd running ($(cat $PID)), exiting now."
714 start-stop-daemon -q -b -m -p $PID -x $BIN -S -- -f "$OLSRD_CONFIG_FILE" -nofork
717 olsrd_setup_smartgw_rules
721 start-stop-daemon -q -p $PID -x $BIN -K
722 start-stop-daemon -q -p $PID6 -x $BIN -K
723 test -s $PID && rm $PID
724 test -s $PID6 && rm $PID6