1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2012 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
29 validate_olsrd_option() {
31 [ -z "$str" -o "$str" != "${str%%[! 0-9A-Za-z./|:_-]*}" ] && return 1
41 config_get cfgt "$cfg" TYPE
43 if [ "$cfgt" = "system" ]; then
44 config_get hostname "$cfg" hostname
45 hostname="${hostname:-OpenWrt}"
46 SYSTEM_HOSTNAME="$hostname"
49 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
50 config_get latlon "$cfg" latlon
58 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
59 config_get latlon "$cfg" latitude
61 config_get latlon "$cfg" longitude
66 olsrd_find_config_file() {
68 validate_varname "$cfg" || return 0
70 config_get_bool ignore "$cfg" ignore 0
71 [ "$ignore" -ne 0 ] && return 0
72 config_get OLSRD_CONFIG_FILE "$cfg" config_file
77 warning_invalid_value() {
79 validate_varname "$package" || package=
81 validate_varname "$config" || config=
83 validate_varname "$option" || option=
85 echo -n "Warning: Invalid value" 1>&2
87 if [ -n "$package" -a -n "$config" ]; then
88 echo -n " in option '$package.$config${option:+.}$option'" 1>&2
96 olsrd_write_option() {
99 validate_varname "$cfg" || return 1
101 validate_varname "$option" || return 1
103 local option_type="$5"
105 if [ "$option_type" = bool ]; then
107 1|on|true|enabled|yes) value=yes;;
108 0|off|false|disabled|no) value=no;;
109 *) warning_invalid_value olsrd "$cfg" "$option"; return 1;;
113 if ! validate_olsrd_option "$value"; then
114 warning_invalid_value olsrd "$cfg" "$option"
118 if [ "$value" != "${value%%[G-Zg-z_-]*}" ]; then
119 if [ "$option" != "Ip6AddrType" -a "$option" != "LinkQualityMult" -a "$value" != "yes" -a "$value" != "no" ]; then
124 echo -n "${N}$param$option $value"
129 olsrd_write_plparam() {
132 validate_varname "$cfg" || return 1
134 validate_varname "$option" || return 1
136 local option_type="$5"
139 if [ "$option_type" = bool ]; then
141 1|on|true|enabled|yes) value=yes;;
142 0|off|false|disabled|no) value=no;;
143 *) warning_invalid_value olsrd "$cfg" "$option"; return 1;;
147 if ! validate_olsrd_option "$value"; then
148 warning_invalid_value olsrd "$cfg" "$option"
157 if [ "$option" = 'hosts' ]; then
163 if [ "$option" = 'NonOlsrIf' ]; then
164 if validate_varname "$value"; then
165 if network_get_device ifname "$value"; then
166 echo "Info: mdns Interface '$value' ifname '$ifname' found" 1>&2
168 echo "Warning: mdns Interface '$value' not found, skipped" 1>&2
171 warning_invalid_value olsrd "$cfg" "NonOlsrIf"
173 [ -z "$ifname" ] || value=$ifname
176 echo -n "${N}${param}PlParam \"$option\" \"$value\""
181 config_update_schema() {
183 local schema_varname="$1"
184 validate_varname "$schema_varname" || return 1
186 validate_varname "$command" || return 1
188 validate_varname "$option" || return 1
198 eval "export -n -- \"schema=\${$schema_varname}\""
200 for cur_option in $schema; do
201 [ "${cur_option%%[:=]*}" = "$option" ] && return 0
204 if [ "$command" = list ]; then
206 if [ "$#" -ge "3" ]; then
207 schema_entry="$option:list3"
208 elif [ "$#" -ge "2" ]; then
209 schema_entry="$option:list2"
211 schema_entry="$option:list"
214 schema_entry="$option"
217 append "$schema_varname" "$schema_entry"
222 config_write_options() {
226 validate_varname "$cfg" || return 1
227 local write_func="$3"
228 [ -z "$write_func" ] && output_func=echo
229 local write_param="$4"
242 for schema_entry in $schema; do
243 default="${schema_entry#*[=]}"
244 [ "$default" = "$schema_entry" ] && default=
245 option="${schema_entry%%[=]*}"
251 validate_varname "$option" || continue
252 [ -z "$option_type" ] || validate_varname "$option_type" || continue
253 [ "$option_type" = internal ] && continue
254 config_get value "$cfg" "$option"
256 if [ -z "$value" ]; then
261 elif [ "$value" = '-' -a -n "$default" ]; then
265 [ -z "$value" ] && continue
267 case "$option_type" in
274 if [ "$list_size" -gt 0 ]; then
275 config_get option_length "$cfg" "${option}_LENGTH"
276 if [ -n "$option_length" ]; then
278 while [ "$i" -le "$option_length" ]; do
279 config_get list_value "$cfg" "${option}_ITEM$i"
280 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" || break
286 for list_item in $value; do
287 append "list_value" "$list_item"
289 position=$((i % list_size))
290 if [ "$position" -eq 0 ]; then
291 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" || break
295 [ "$position" -ne 0 ] && "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type"
298 "$write_func" "$write_param" "$cfg" "$option" "$value" "$option_type"
305 olsrd_write_olsrd() {
307 validate_varname "$cfg" || return 0
310 config_get_bool ignore "$cfg" ignore 0
311 [ "$ignore" -ne 0 ] && return 0
313 [ "$OLSRD_COUNT" -gt 0 ] && return 0
315 config_get ipversion "$cfg" IpVersion
316 if [ "$ipversion" = "6and4" ]; then
317 OLSRD_IPVERSION_6AND4=1
318 config_set "$cfg" IpVersion '6'
320 config_get smartgateway "$cfg" SmartGateway
321 config_get smartgatewayuplink "$cfg" SmartGatewayUplink
323 config_write_options "$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option
325 OLSRD_COUNT=$((OLSRD_COUNT + 1))
329 olsrd_write_ipcconnect() {
331 validate_varname "$cfg" || return 0
334 config_get_bool ignore "$cfg" ignore 0
335 [ "$ignore" -ne 0 ] && return 0
337 [ "$IPCCONNECT_COUNT" -gt 0 ] && return 0
339 echo -n "${N}IpcConnect${N}{"
340 config_write_options "$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option "${T}"
342 IPCCONNECT_COUNT=$((IPCCONNECT_COUNT + 1))
349 validate_varname "$cfg" || return 0
352 config_get_bool ignore "$cfg" ignore 0
353 [ "$ignore" -ne 0 ] && return 0
355 config_get netaddr "$cfg" netaddr
356 if ! validate_olsrd_option "$netaddr"; then
357 warning_invalid_value olsrd "$cfg" "netaddr"
361 config_get netmask "$cfg" netmask
362 if ! validate_olsrd_option "$netmask"; then
363 warning_invalid_value olsrd "$cfg" "netmask"
367 [ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{"
368 echo -n "${N}${T}${T}$netaddr $netmask"
369 HNA4_COUNT=$((HNA4_COUNT + 1))
376 validate_varname "$cfg" || return 0
379 config_get_bool ignore "$cfg" ignore 0
380 [ "$ignore" -ne 0 ] && return 0
382 config_get netaddr "$cfg" netaddr
383 if ! validate_olsrd_option "$netaddr"; then
384 warning_invalid_value olsrd "$cfg" "netaddr"
388 config_get prefix "$cfg" prefix
389 if ! validate_olsrd_option "$prefix"; then
390 warning_invalid_value olsrd "$cfg" "prefix"
394 [ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{"
395 echo -n "${N}${T}${T}$netaddr $prefix"
396 HNA6_COUNT=$((HNA6_COUNT + 1))
401 olsrd_write_loadplugin() {
403 validate_varname "$cfg" || return 0
411 config_get_bool ignore "$cfg" ignore 0
412 [ "$ignore" -ne 0 ] && return 0
414 config_get library "$cfg" library
415 if ! validate_olsrd_option "$library"; then
416 warning_invalid_value olsrd "$cfg" "library"
419 if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then
420 echo "Warning: Plugin library '$library' not found, skipped" 1>&2
426 config_get name "$cfg" name
427 [ -z "$name" ] && config_set "$cfg" name $SYSTEM_HOSTNAME
429 config_get suffix "$cfg" suffix
430 [ -z "$suffix" ] && config_set "$cfg" suffix '.olsr'
432 config_get lat "$cfg" lat
433 config_get lon "$cfg" lon
434 config_get latlon_infile "$cfg" latlon_infile
435 if [ \( -z "$lat" -o -z "$lat" \) -a -z "$latlon_infile" ]; then
436 if [ -f '/var/run/latlon.txt' ]; then
437 config_set "$cfg" lat ''
438 config_set "$cfg" lon ''
439 config_set "$cfg" latlon_infile '/var/run/latlon.txt'
441 config_set "$cfg" lat "$SYSTEM_LAT"
442 config_set "$cfg" lon "$SYSTEM_LON"
446 for f in latlon_file hosts_file services_file resolv_file macs_file; do
447 config_get $f "$cfg" $f
450 [ -z "$latlon_file" ] && config_set "$cfg" latlon_file '/var/run/latlon.js'
453 config_get wd_file "$cfg" file
457 echo -n "${N}LoadPlugin \"$library\"${N}{"
458 config_write_options "$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam "${T}"
464 olsrd_write_interface() {
466 validate_varname "$cfg" || return 0
472 config_get_bool ignore "$cfg" ignore 0
473 [ "$ignore" -ne 0 ] && return 0
476 config_get interfaces "$cfg" interface
477 for interface in $interfaces; do
478 if validate_varname "$interface"; then
479 if network_get_device IFNAME "$interface"; then
480 ifnames="$ifnames \"$IFNAME\""
481 ifsglobal="$ifsglobal $IFNAME"
483 echo "Warning: Interface '$interface' not found, skipped" 1>&2
486 warning_invalid_value olsrd "$cfg" "interface"
490 [ -z "$ifnames" ] && return 0
492 echo -n "${N}Interface$ifnames${N}{"
493 config_write_options "$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option "${T}"
495 INTERFACES_COUNT=$((INTERFACES_COUNT + 1))
500 olsrd_write_interface_defaults() {
502 validate_varname "$cfg" || return 0
504 echo -n "${N}InterfaceDefaults$ifnames${N}{"
505 config_write_options "$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option "${T}"
511 olsrd_update_schema() {
513 validate_varname "$command" || return 0
515 validate_varname "$varname" || return 0
517 local cfg="$CONFIG_SECTION"
521 config_get cfgt "$cfg" TYPE
523 olsrd) config_update_schema OLSRD_OLSRD_SCHEMA "$command" "$varname" "$value";;
524 IpcConnect) config_update_schema OLSRD_IPCCONNECT_SCHEMA "$command" "$varname" "$value";;
525 LoadPlugin) config_update_schema OLSRD_LOADPLUGIN_SCHEMA "$command" "$varname" "$value";;
526 Interface) config_update_schema OLSRD_INTERFACE_SCHEMA "$command" "$varname" "$value";;
527 InterfaceDefaults) config_update_schema OLSRD_INTERFACE_DEFAULTS_SCHEMA "$command" "$varname" "$value";;
533 olsrd_write_config() {
534 OLSRD_IPVERSION_6AND4=0
536 config_foreach olsrd_write_olsrd olsrd
538 config_foreach olsrd_write_ipcconnect IpcConnect
540 config_foreach olsrd_write_hna4 Hna4
541 [ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
543 config_foreach olsrd_write_hna6 Hna6
544 [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
545 config_foreach olsrd_write_loadplugin LoadPlugin
547 config_foreach olsrd_write_interface_defaults InterfaceDefaults
548 config_foreach olsrd_write_interface Interface
554 olsrd_setup_smartgw_rules() {
555 # Check if ipip is installed
556 [ ! -e /etc/modules.d/[0-9]*-ipip ] && echo "Warning: kmod-ipip is missing. SmartGateway will not work until you install it."
558 wanifnames=$(ip r l e 0/0 t all | sed -e 's/^.* dev //' |cut -d " " -f 1 | sort | uniq)
560 if [ -z "$wanifnames" ]; then
564 IP4T=$(which iptables)
565 IP6T=$(which ip6tables)
567 # Delete smartgw firewall rules first
568 for IPT in $IP4T $IP6T; do
569 while $IPT -D forwarding_rule -o tnl_+ -j ACCEPT 2> /dev/null; do :;done
570 for IFACE in $wanifnames; do
571 while $IPT -D forwarding_rule -i tunl0 -o $IFACE -j ACCEPT 2> /dev/null; do :; done
573 for IFACE in $ifsglobal; do
574 while $IPT -D input_rule -i $IFACE -p 4 -j ACCEPT 2> /dev/null; do :; done
577 while $IP4T -t nat -D postrouting_rule -o tnl_+ -j MASQUERADE 2> /dev/null; do :;done
579 if [ "$smartgateway" == "yes" ]; then
580 echo "Notice: Inserting firewall rules for SmartGateway"
581 if [ ! "$smartgatewayuplink" == "none" ]; then
582 if [ "$smartgatewayuplink" == "ipv4" ]; then
583 # Allow everything to be forwarded to tnl_+ and use NAT for it
584 $IP4T -I forwarding_rule -o tnl_+ -j ACCEPT
585 $IP4T -t nat -I postrouting_rule -o tnl_+ -j MASQUERADE
586 # Allow forwarding from tunl0 to (all) wan-interfaces
587 if [ "$nowan"="0" ]; then
588 for IFACE in $wanifnames; do
589 $IP4T -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
592 # Allow incoming ipip on all olsr-interfaces
593 for IFACE in $ifsglobal; do
594 $IP4T -I input_rule -i $IFACE -p 4 -j ACCEPT
596 elif [ "$smartgatewayuplink" == "ipv6" ]; then
597 $IP6T -I forwarding_rule -o tnl_+ -j ACCEPT
598 if [ "$nowan"="0" ]; then
599 for IFACE in $wanifnames; do
600 $IP6T -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
603 for IFACE in $ifsglobal; do
604 $IP6T -I input_rule -i $IFACE -p 4 -j ACCEPT
607 for IPT in $IP4T $IP6T; do
608 $IPT -I forwarding_rule -o tnl_+ -j ACCEPT
609 $IPT -t nat -I postrouting_rule -o tnl_+ -j MASQUERADE
610 if [ "$nowan"="0" ]; then
611 for IFACE in $wanifnames; do
612 $IPT -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
615 for IFACE in $ifsglobal; do
616 $IPT -I input_rule -i $IFACE -p 4 -j ACCEPT
625 echo "${initscript}:" "$@" 1>&2
633 config_foreach system_config system
636 olsrd_update_schema "option" "$@"
640 olsrd_update_schema "list" "$@"
643 . /lib/functions/network.sh
649 config_foreach olsrd_find_config_file olsrd
651 if [ -z "$OLSRD_CONFIG_FILE" ]; then
652 mkdir -p -- /var/etc/
653 olsrd_write_config > /var/etc/olsrd.conf
654 if [ "$INTERFACES_COUNT" -gt 0 -a "$OLSRD_COUNT" -gt 0 ]; then
655 OLSRD_CONFIG_FILE=/var/etc/olsrd.conf
659 [ -z "$OLSRD_CONFIG_FILE" ] && return 1
662 if [ "$OLSRD_IPVERSION_6AND4" -ne 0 ]; then
663 bindv6only="$(sysctl -n net.ipv6.bindv6only)"
664 sysctl -w net.ipv6.bindv6only=1 > /dev/null
665 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
666 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
667 rm $OLSRD_CONFIG_FILE
669 # some filenames should get the suffix .ipv6
670 for file in $latlon_file $hosts_file $services_file $resolv_file $macs_file $wd_file;do
671 f=$(echo $file|sed 's/\//\\\//g')
672 sed -i "s/$f/$f.ipv6/g" /var/etc/olsrd.conf.ipv6
675 SERVICE_PID_FILE="$PID6"
676 if service_check /usr/sbin/olsrd; then
677 error "there already is an IPv6 instance of olsrd running ($(cat $PID6)), not starting."
679 service_start /usr/sbin/olsrd -f /var/etc/olsrd.conf.ipv6 -nofork
682 SERVICE_PID_FILE="$PID"
683 if service_check /usr/sbin/olsrd; then
684 error "there already is an IPv4 instance of olsrd running ($(cat $PID)), not starting."
686 service_start /usr/sbin/olsrd -f /var/etc/olsrd.conf.ipv4 -nofork
690 sysctl -w net.ipv6.bindv6only="$bindv6only" > /dev/null
694 if [ "$ipversion" = "6" ]; then
695 sed -i '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' "$OLSRD_CONFIG_FILE"
698 SERVICE_PID_FILE="$PID"
699 if service_check /usr/sbin/olsrd; then
700 error "there already is an IPv4 instance of olsrd running ($(cat $PID)), not starting."
703 service_start /usr/sbin/olsrd -f "$OLSRD_CONFIG_FILE" -nofork
706 olsrd_setup_smartgw_rules
710 SERVICE_PID_FILE="$PID"
711 service_stop /usr/sbin/olsrd
713 SERVICE_PID_FILE="$PID6"
714 service_stop /usr/sbin/olsrd