olsrd: splitting IPv6/4 configuration and start scripts
[feed/routing.git] / olsrd / files / olsrd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2013 OpenWrt.org
3
4 START=65
5
6 SERVICE_DAEMONIZE=1
7 SERVICE_WRITE_PID=1
8
9 OLSRD_OLSRD_SCHEMA='ignore:internal config_file:internal DebugLevel=0 AllowNoInt=yes'
10 OLSRD_IPCCONNECT_SCHEMA='ignore:internal Host:list Net:list2'
11 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'
12 OLSRD_INTERFACE_SCHEMA='ignore:internal interface:internal AutoDetectChanges:bool LinkQualityMult:list2'
13 OLSRD_INTERFACE_DEFAULTS_SCHEMA='AutoDetectChanges:bool'
14
15 T=' '
16 N='
17 '
18
19 log() {
20 logger -t olsrd -p daemon.info -s "${initscript}: $@"
21 }
22
23 error() {
24 log "ERROR: $@"
25 }
26
27 validate_varname() {
28 local varname="$1"
29 [ -z "$varname" -o "$varname" != "${varname%%[!A-Za-z0-9_]*}" ] && return 1
30 return 0
31 }
32
33 validate_olsrd_option() {
34 local str="$1"
35 [ -z "$str" -o "$str" != "${str%%[! 0-9A-Za-z./|:_-]*}" ] && return 1
36 return 0
37 }
38
39 system_config() {
40 local cfg="$1"
41 local cfgt hostname latlon oldIFS
42
43 config_get cfgt "$cfg" TYPE
44
45 if [ "$cfgt" = "system" ]; then
46 config_get hostname "$cfg" hostname
47 hostname="${hostname:-OpenWrt}"
48 SYSTEM_HOSTNAME="$hostname"
49 fi
50
51 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
52 config_get latlon "$cfg" latlon
53 oldIFS="$IFS"; IFS=" ${T}${N},"; set -- $latlon; IFS="$oldIFS"
54 SYSTEM_LAT="$1"
55 SYSTEM_LON="$2"
56 fi
57
58 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
59 config_get latlon "$cfg" latitude
60 SYSTEM_LAT="$latlon"
61 config_get latlon "$cfg" longitude
62 SYSTEM_LON="$latlon"
63 fi
64 }
65
66 olsrd_find_config_file() {
67 local cfg="$1"
68 validate_varname "$cfg" || return 0
69
70 config_get_bool ignore "$cfg" ignore 0
71 [ "$ignore" -ne 0 ] && return 0
72 config_get OLSRD_CONFIG_FILE "$cfg" config_file
73
74 return 0
75 }
76
77 warning_invalid_value() {
78 local funcname="warning_invalid_value"
79 local package="$1"
80 validate_varname "$package" || package=
81 local config="$2"
82 validate_varname "$config" || config=
83 local option="$3"
84 validate_varname "$option" || option=
85
86 if [ -n "$package" -a -n "$config" ]; then
87 log "$funcname() in option '$package.$config${option:+.}$option', skipped"
88 else
89 log "$funcname() skipped"
90 fi
91
92 return 0
93 }
94
95 olsrd_write_option() {
96 local param="$1"
97 local cfg="$2"
98 validate_varname "$cfg" || return 1
99 local option="$3"
100 validate_varname "$option" || return 1
101 local value="$4"
102 local option_type="$5"
103
104 if [ "$option_type" = bool ]; then
105 case "$value" in
106 1|on|true|enabled|yes) value=yes;;
107 0|off|false|disabled|no) value=no;;
108 *) warning_invalid_value olsrd "$cfg" "$option"; return 1;;
109 esac
110 fi
111
112 if ! validate_olsrd_option "$value"; then
113 warning_invalid_value olsrd "$cfg" "$option"
114 return 1
115 fi
116
117 if [ "$value" != "${value%%[G-Zg-z_-]*}" ]; then
118 if [ "$option" != "Ip6AddrType" -a "$option" != "LinkQualityMult" -a "$value" != "yes" -a "$value" != "no" ]; then
119 value="\"$value\""
120 fi
121 fi
122
123 echo -n "${N}$param$option $value"
124
125 return 0
126 }
127
128 olsrd_write_plparam() {
129 local funcname="olsrd_write_plparam"
130 local param="$1"
131 local cfg="$2"
132 validate_varname "$cfg" || return 1
133 local option="$3"
134 validate_varname "$option" || return 1
135 local value="$4"
136 local option_type="$5"
137 local _option oldIFS
138
139 if [ "$option_type" = bool ]; then
140 case "$value" in
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;;
144 esac
145 fi
146
147 if ! validate_olsrd_option "$value"; then
148 warning_invalid_value olsrd "$cfg" "$option"
149 return 1
150 fi
151
152 oldIFS="$IFS"
153 IFS='-_'
154 set -- $option
155 option="$*"
156 IFS="$oldIFS"
157 _option="$option"
158
159 if [ "$option" = 'hosts' ]; then
160 set -- $value
161 option="$1"
162 shift
163 value="$*"
164 fi
165
166 if [ "$option" = 'NonOlsrIf' ]; then
167 if validate_varname "$value"; then
168 if network_get_device ifname "$value"; then
169 log "$funcname() Info: mdns Interface '$value' ifname '$ifname' found"
170 else
171 log "$funcname() Warning: mdns Interface '$value' not found, skipped"
172 fi
173 else
174 warning_invalid_value olsrd "$cfg" "NonOlsrIf"
175 fi
176 [ -z "$ifname" ] || value=$ifname
177 fi
178
179 echo -n "${N}${param}PlParam \"$option\" \"$value\""
180
181 return 0
182 }
183
184 config_update_schema() {
185 local schema_varname="$1"
186 validate_varname "$schema_varname" || return 1
187 local command="$2"
188 validate_varname "$command" || return 1
189 local option="$3"
190 validate_varname "$option" || return 1
191 local value="$4"
192 local schema
193 local cur_option
194
195 case "$varname" in
196 *_LENGTH) return 0;;
197 *_ITEM*) return 0;;
198 esac
199
200 eval "export -n -- \"schema=\${$schema_varname}\""
201
202 for cur_option in $schema; do
203 [ "${cur_option%%[:=]*}" = "$option" ] && return 0
204 done
205
206 if [ "$command" = list ]; then
207 set -- $value
208 if [ "$#" -ge "3" ]; then
209 schema_entry="$option:list3"
210 elif [ "$#" -ge "2" ]; then
211 schema_entry="$option:list2"
212 else
213 schema_entry="$option:list"
214 fi
215 else
216 schema_entry="$option"
217 fi
218
219 append "$schema_varname" "$schema_entry"
220
221 return 0
222 }
223
224 config_write_options() {
225 local funcname="config_write_options"
226 local schema="$1"
227 local cfg="$2"
228 validate_varname "$cfg" || return 1
229 local write_func="$3"
230 [ -z "$write_func" ] && output_func=echo
231 local write_param="$4"
232
233 local schema_entry option option_length option_type default value list_size list_item list_value i position speed oldIFS
234 local list_speed_vars="HelloInterval HelloValidityTime TcInterval TcValidityTime MidInterval MidValidityTime HnaInterval HnaValidityTime"
235
236 get_value_for_entry()
237 {
238 local schema_entry="$1"
239
240 default="${schema_entry#*[=]}"
241 [ "$default" = "$schema_entry" ] && default=
242 option="${schema_entry%%[=]*}"
243
244 oldIFS="$IFS"; IFS=':'; set -- $option; IFS="$oldIFS"
245 option="$1"
246 option_type="$2"
247
248 validate_varname "$option" || return 1
249 [ -z "$option_type" ] || validate_varname "$option_type" || return 1
250 [ "$option_type" = internal ] && return 1
251
252 config_get value "$cfg" "$option"
253 [ "$option" = "speed" ] && return 1
254
255 return 0
256 }
257
258 already_in_schema()
259 {
260 case " $schema " in
261 *" $1 "*)
262 return 0
263 ;;
264 *)
265 return 1
266 ;;
267 esac
268 }
269
270 already_in_schema "speed" && {
271 get_value_for_entry "speed"
272
273 if [ 2>/dev/null $value -gt 0 -a $value -le 20 ]; then
274 speed="$value"
275 else
276 log "$funcname() Warning: invalid speed-value: '$value' - allowed integers: 1...20, fallback to 6"
277 speed=6
278 fi
279
280 for schema_entry in $list_speed_vars; do {
281 already_in_schema "$schema_entry" || schema="$schema $schema_entry"
282 } done
283 }
284
285 for schema_entry in $schema; do
286 if [ -n "$speed" ]; then # like sven-ola freifunk firmware fff-1.7.4
287 case "$schema_entry" in
288 HelloInterval)
289 value="$(( $speed / 2 + 1 )).0"
290 ;;
291 HelloValidityTime)
292 value="$(( $speed * 25 )).0"
293 ;;
294 TcInterval) # todo: not fisheye? -> $(( $speed * 2 ))
295 value=$(( $speed / 2 ))
296 [ $value -eq 0 ] && value=1
297 value="$value.0"
298 ;;
299 TcValidityTime)
300 value="$(( $speed * 100 )).0"
301 ;;
302 MidInterval)
303 value="$(( $speed * 5 )).0"
304 ;;
305 MidValidityTime)
306 value="$(( $speed * 100 )).0"
307 ;;
308 HnaInterval)
309 value="$(( $speed * 2 )).0"
310 ;;
311 HnaValidityTime)
312 value="$(( $speed * 25 )).0"
313 ;;
314 *)
315 get_value_for_entry "$schema_entry" || continue
316 ;;
317 esac
318
319 is_speed_var()
320 {
321 case " $list_speed_vars " in
322 *" $1 "*)
323 return 0
324 ;;
325 *)
326 return 1
327 ;;
328 esac
329 }
330
331 is_speed_var "$schema_entry" && option="$schema_entry"
332 else
333 get_value_for_entry "$schema_entry" || continue
334 fi
335
336 if [ -z "$value" ]; then
337 oldIFS="$IFS"; IFS='+'; set -- $default; IFS="$oldIFS"
338 value=$*
339 elif [ "$value" = '-' -a -n "$default" ]; then
340 continue
341 fi
342
343 [ -z "$value" ] && continue
344
345 case "$option_type" in
346 list) list_size=1;;
347 list2) list_size=2;;
348 list3) list_size=3;;
349 *) list_size=0;;
350 esac
351
352 if [ "$list_size" -gt 0 ]; then
353 config_get option_length "$cfg" "${option}_LENGTH"
354 if [ -n "$option_length" ]; then
355 i=1
356 while [ "$i" -le "$option_length" ]; do
357 config_get list_value "$cfg" "${option}_ITEM$i"
358 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" || break
359 i=$((i + 1))
360 done
361 else
362 list_value=
363 i=0
364 for list_item in $value; do
365 append "list_value" "$list_item"
366 i=$((i + 1))
367 position=$((i % list_size))
368 if [ "$position" -eq 0 ]; then
369 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" || break
370 list_value=
371 fi
372 done
373 [ "$position" -ne 0 ] && "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type"
374 fi
375 else
376 "$write_func" "$write_param" "$cfg" "$option" "$value" "$option_type"
377 fi
378 done
379
380 return 0
381 }
382
383 olsrd_write_olsrd() {
384 local cfg="$1"
385 validate_varname "$cfg" || return 0
386 local ignore
387
388 config_get_bool ignore "$cfg" ignore 0
389 [ "$ignore" -ne 0 ] && return 0
390
391 [ "$OLSRD_COUNT" -gt 0 ] && return 0
392
393 config_get ipversion "$cfg" IpVersion
394 if [ "$ipversion" = "6and4" ]; then
395 OLSRD_IPVERSION_6AND4=1
396 config_set "$cfg" IpVersion '6'
397 fi
398 config_get smartgateway "$cfg" SmartGateway
399 config_get smartgatewayuplink "$cfg" SmartGatewayUplink
400
401 config_write_options "$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option
402 echo
403 OLSRD_COUNT=$((OLSRD_COUNT + 1))
404 return 0
405 }
406
407 olsrd_write_ipcconnect() {
408 local cfg="$1"
409 validate_varname "$cfg" || return 0
410 local ignore
411
412 config_get_bool ignore "$cfg" ignore 0
413 [ "$ignore" -ne 0 ] && return 0
414
415 [ "$IPCCONNECT_COUNT" -gt 0 ] && return 0
416
417 echo -n "${N}IpcConnect${N}{"
418 config_write_options "$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option "${T}"
419 echo "${N}}"
420 IPCCONNECT_COUNT=$((IPCCONNECT_COUNT + 1))
421
422 return 0
423 }
424
425 olsrd_write_hna4() {
426 local cfg="$1"
427 validate_varname "$cfg" || return 0
428 local ignore
429
430 config_get_bool ignore "$cfg" ignore 0
431 [ "$ignore" -ne 0 ] && return 0
432
433 config_get netaddr "$cfg" netaddr
434 if ! validate_olsrd_option "$netaddr"; then
435 warning_invalid_value olsrd "$cfg" "netaddr"
436 return 0
437 fi
438
439 config_get netmask "$cfg" netmask
440 if ! validate_olsrd_option "$netmask"; then
441 warning_invalid_value olsrd "$cfg" "netmask"
442 return 0
443 fi
444
445 [ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{"
446 echo -n "${N}${T}${T}$netaddr $netmask"
447 HNA4_COUNT=$((HNA4_COUNT + 1))
448
449 return 0
450 }
451
452 olsrd_write_hna6() {
453 local cfg="$1"
454 validate_varname "$cfg" || return 0
455 local ignore
456
457 config_get_bool ignore "$cfg" ignore 0
458 [ "$ignore" -ne 0 ] && return 0
459
460 config_get netaddr "$cfg" netaddr
461 if ! validate_olsrd_option "$netaddr"; then
462 warning_invalid_value olsrd "$cfg" "netaddr"
463 return 0
464 fi
465
466 config_get prefix "$cfg" prefix
467 if ! validate_olsrd_option "$prefix"; then
468 warning_invalid_value olsrd "$cfg" "prefix"
469 return 0
470 fi
471
472 [ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{"
473 echo -n "${N}${T}${T}$netaddr $prefix"
474 HNA6_COUNT=$((HNA6_COUNT + 1))
475
476 return 0
477 }
478
479 olsrd_write_loadplugin() {
480 local funcname="olsrd_write_loadplugin"
481 local cfg="$1"
482 validate_varname "$cfg" || return 0
483 local ignore
484 local name
485 local suffix
486 local lat
487 local lon
488 local latlon_infile
489
490 config_get_bool ignore "$cfg" ignore 0
491 [ "$ignore" -ne 0 ] && return 0
492
493 config_get library "$cfg" library
494 if ! validate_olsrd_option "$library"; then
495 warning_invalid_value olsrd "$cfg" "library"
496 return 0
497 fi
498 if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then
499 log "$funcname() Warning: Plugin library '$library' not found, skipped"
500 return 0
501 fi
502
503 case "$library" in
504 olsrd_nameservice.*)
505 config_get name "$cfg" name
506 [ -z "$name" ] && config_set "$cfg" name $SYSTEM_HOSTNAME
507
508 config_get suffix "$cfg" suffix
509 [ -z "$suffix" ] && config_set "$cfg" suffix '.olsr'
510
511 config_get lat "$cfg" lat
512 config_get lon "$cfg" lon
513 config_get latlon_infile "$cfg" latlon_infile
514 if [ \( -z "$lat" -o -z "$lat" \) -a -z "$latlon_infile" ]; then
515 if [ -f '/var/run/latlon.txt' ]; then
516 config_set "$cfg" lat ''
517 config_set "$cfg" lon ''
518 config_set "$cfg" latlon_infile '/var/run/latlon.txt'
519 else
520 config_set "$cfg" lat "$SYSTEM_LAT"
521 config_set "$cfg" lon "$SYSTEM_LON"
522 fi
523 fi
524
525 for f in latlon_file hosts_file services_file resolv_file macs_file; do
526 config_get $f "$cfg" $f
527 done
528
529 [ -z "$latlon_file" ] && config_set "$cfg" latlon_file '/var/run/latlon.js'
530 ;;
531 olsrd_watchdog.*)
532 config_get wd_file "$cfg" file
533 ;;
534 esac
535
536 echo -n "${N}LoadPlugin \"$library\"${N}{"
537 config_write_options "$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam "${T}"
538 echo "${N}}"
539
540 return 0
541 }
542
543 olsrd_write_interface() {
544 local funcname="olsrd_write_interface"
545 local cfg="$1"
546 validate_varname "$cfg" || return 0
547 local ignore
548 local interfaces
549 local interface
550 local ifnames
551
552 config_get_bool ignore "$cfg" ignore 0
553 [ "$ignore" -ne 0 ] && return 0
554
555 ifnames=
556 config_get interfaces "$cfg" interface
557
558 for interface in $interfaces; do
559 if validate_varname "$interface"; then
560 if network_get_device IFNAME "$interface"; then
561 ifnames="$ifnames \"$IFNAME\""
562 ifsglobal="$ifsglobal $IFNAME"
563 elif network_get_physdev IFNAME "$interface"; then
564 local proto="$(uci -q get network.${interface}.proto)"
565 if [ "$proto" = "static" -o "$proto" = "none" ]; then
566 ifnames="$ifnames \"$IFNAME\""
567 ifsglobal="$ifsglobal $IFNAME"
568 fi
569 else
570 log "$funcname() Warning: Interface '$interface' not found, skipped"
571 fi
572 else
573 warning_invalid_value olsrd "$cfg" "interface"
574 fi
575 done
576
577 [ -z "$ifnames" ] && return 0
578
579 echo -n "${N}Interface$ifnames${N}{"
580 config_write_options "$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option "${T}"
581 echo "${N}}"
582 INTERFACES_COUNT=$((INTERFACES_COUNT + 1))
583
584 return 0
585 }
586
587 olsrd_write_interface_defaults() {
588 local cfg="$1"
589 validate_varname "$cfg" || return 0
590
591 echo -n "${N}InterfaceDefaults$ifnames${N}{"
592 config_write_options "$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option "${T}"
593 echo "${N}}"
594
595 return 1
596 }
597
598 olsrd_update_schema() {
599 local command="$1"
600 validate_varname "$command" || return 0
601 local varname="$2"
602 validate_varname "$varname" || return 0
603 local value="$3"
604 local cfg="$CONFIG_SECTION"
605 local cfgt
606 local cur_varname
607
608 config_get cfgt "$cfg" TYPE
609 case "$cfgt" in
610 olsrd) config_update_schema OLSRD_OLSRD_SCHEMA "$command" "$varname" "$value";;
611 IpcConnect) config_update_schema OLSRD_IPCCONNECT_SCHEMA "$command" "$varname" "$value";;
612 LoadPlugin) config_update_schema OLSRD_LOADPLUGIN_SCHEMA "$command" "$varname" "$value";;
613 Interface) config_update_schema OLSRD_INTERFACE_SCHEMA "$command" "$varname" "$value";;
614 InterfaceDefaults) config_update_schema OLSRD_INTERFACE_DEFAULTS_SCHEMA "$command" "$varname" "$value";;
615 esac
616
617 return 0
618 }
619
620 olsrd_write_config() {
621 OLSRD_IPVERSION_6AND4=0
622 OLSRD_COUNT=0
623 config_foreach olsrd_write_olsrd olsrd
624 IPCCONNECT_COUNT=0
625 config_foreach olsrd_write_ipcconnect IpcConnect
626 HNA4_COUNT=0
627 config_foreach olsrd_write_hna4 Hna4
628 [ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
629 HNA6_COUNT=0
630 config_foreach olsrd_write_hna6 Hna6
631 [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
632 config_foreach olsrd_write_loadplugin LoadPlugin
633 INTERFACES_COUNT=0
634 config_foreach olsrd_write_interface_defaults InterfaceDefaults
635 config_foreach olsrd_write_interface Interface
636 echo
637
638 return 0
639 }
640
641 get_wan_ifnames()
642 {
643 local wanifnames word catch_next
644
645 which ip >/dev/null || return 1
646
647 set -- $( ip route list exact 0.0.0.0/0 table all )
648 for word in $*; do
649 case "$word" in
650 dev)
651 catch_next="true"
652 ;;
653 *)
654 [ -n "$catch_next" ] && {
655 case "$wanifnames" in
656 *" $word "*)
657 ;;
658 *)
659 wanifnames="$wanifnames $word "
660 ;;
661 esac
662
663 catch_next=
664 }
665 ;;
666 esac
667 done
668
669 echo "$wanifnames"
670 }
671
672 olsrd_setup_smartgw_rules() {
673 local funcname="olsrd_setup_smartgw_rules"
674 # Check if ipip is installed
675 [ -e /etc/modules.d/[0-9]*-ipip ] || {
676 log "$funcname() Warning: kmod-ipip is missing. SmartGateway will not work until you install it."
677 return 1
678 }
679
680 local wanifnames="$( get_wan_ifnames )"
681
682 if [ -z "$wanifnames" ]; then
683 nowan=1
684 else
685 nowan=0
686 fi
687
688 IP4T=$(which iptables)
689 IP6T=$(which ip6tables)
690
691 # Delete smartgw firewall rules first
692 for IPT in $IP4T $IP6T; do
693 while $IPT -D forwarding_rule -o tnl_+ -j ACCEPT 2> /dev/null; do :;done
694 for IFACE in $wanifnames; do
695 while $IPT -D forwarding_rule -i tunl0 -o $IFACE -j ACCEPT 2> /dev/null; do :; done
696 done
697 for IFACE in $ifsglobal; do
698 while $IPT -D input_rule -i $IFACE -p 4 -j ACCEPT 2> /dev/null; do :; done
699 done
700 done
701 while $IP4T -t nat -D postrouting_rule -o tnl_+ -j MASQUERADE 2> /dev/null; do :;done
702
703 if [ "$smartgateway" == "yes" ]; then
704 log "$funcname() Notice: Inserting firewall rules for SmartGateway"
705 if [ ! "$smartgatewayuplink" == "none" ]; then
706 if [ "$smartgatewayuplink" == "ipv4" ]; then
707 # Allow everything to be forwarded to tnl_+ and use NAT for it
708 $IP4T -I forwarding_rule -o tnl_+ -j ACCEPT
709 $IP4T -t nat -I postrouting_rule -o tnl_+ -j MASQUERADE
710 # Allow forwarding from tunl0 to (all) wan-interfaces
711 if [ "$nowan"="0" ]; then
712 for IFACE in $wanifnames; do
713 $IP4T -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
714 done
715 fi
716 # Allow incoming ipip on all olsr-interfaces
717 for IFACE in $ifsglobal; do
718 $IP4T -I input_rule -i $IFACE -p 4 -j ACCEPT
719 done
720 elif [ "$smartgatewayuplink" == "ipv6" ]; then
721 $IP6T -I forwarding_rule -o tnl_+ -j ACCEPT
722 if [ "$nowan"="0" ]; then
723 for IFACE in $wanifnames; do
724 $IP6T -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
725 done
726 fi
727 for IFACE in $ifsglobal; do
728 $IP6T -I input_rule -i $IFACE -p 4 -j ACCEPT
729 done
730 else
731 $IP4T -t nat -I postrouting_rule -o tnl_+ -j MASQUERADE
732 for IPT in $IP4T $IP6T; do
733 $IPT -I forwarding_rule -o tnl_+ -j ACCEPT
734 if [ "$nowan"="0" ]; then
735 for IFACE in $wanifnames; do
736 $IPT -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
737 done
738 fi
739 for IFACE in $ifsglobal; do
740 $IPT -I input_rule -i $IFACE -p 4 -j ACCEPT
741 done
742 done
743 fi
744 fi
745 fi
746 }
747
748 start() {
749 SYSTEM_HOSTNAME=
750 SYSTEM_LAT=
751 SYSTEM_LON=
752 config_load system
753 config_foreach system_config system
754
755 option_cb() {
756 olsrd_update_schema "option" "$@"
757 }
758
759 list_cb() {
760 olsrd_update_schema "list" "$@"
761 }
762
763 . /lib/functions/network.sh
764
765 config_load $UCI_CONF_NAME
766 reset_cb
767
768 OLSRD_CONFIG_FILE=
769 config_foreach olsrd_find_config_file olsrd
770
771 if [ -z "$OLSRD_CONFIG_FILE" ]; then
772 mkdir -p -- /var/etc/
773 olsrd_write_config > /var/etc/$UCI_CONF_NAME.conf
774 if [ "$INTERFACES_COUNT" -gt 0 -a "$OLSRD_COUNT" -gt 0 ]; then
775 OLSRD_CONFIG_FILE=/var/etc/$UCI_CONF_NAME.conf
776 fi
777 fi
778
779 [ -z "$OLSRD_CONFIG_FILE" ] && return 1
780
781 local bindv6only='0'
782 if [ "$OLSRD_IPVERSION_6AND4" -ne 0 ]; then
783 bindv6only="$(sysctl -n net.ipv6.bindv6only)"
784 sysctl -w net.ipv6.bindv6only=1 > /dev/null
785 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
786 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
787 rm $OLSRD_CONFIG_FILE
788
789 # some filenames should get the suffix .ipv6
790 for file in $latlon_file $hosts_file $services_file $resolv_file $macs_file $wd_file;do
791 f=$(echo $file|sed 's/\//\\\//g')
792 sed -i "s/$f/$f.ipv6/g" /var/etc/olsrd.conf.ipv6
793 done
794
795 SERVICE_PID_FILE="$PID6"
796 if service_check /usr/sbin/olsrd; then
797 error "there is already an IPv6 instance of olsrd running (pid: '$(cat $PID6)'), not starting."
798 else
799 service_start /usr/sbin/olsrd -f /var/etc/olsrd.conf.ipv6 -nofork
800 fi
801
802 SERVICE_PID_FILE="$PID"
803 if service_check /usr/sbin/olsrd; then
804 error "there is already an IPv4 instance of olsrd running (pid: '$(cat $PID)'), not starting."
805 else
806 service_start /usr/sbin/olsrd -f /var/etc/olsrd.conf.ipv4 -nofork
807 fi
808
809 sleep 3
810 sysctl -w net.ipv6.bindv6only="$bindv6only" > /dev/null
811
812 else
813
814 if [ "$ipversion" = "6" ]; then
815 sed -i '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' "$OLSRD_CONFIG_FILE"
816 fi
817
818 SERVICE_PID_FILE="$PID"
819 if service_check /usr/sbin/olsrd; then
820 error "there is already an IPv4 instance of olsrd running (pid: '$(cat $PID)'), not starting."
821 return 1
822 else
823 service_start /usr/sbin/olsrd -f "$OLSRD_CONFIG_FILE" -nofork
824 service_check /usr/sbin/olsrd || {
825 log "startup-error: check via: '/usr/sbin/olsrd -f \"$OLSRD_CONFIG_FILE\" -nofork'"
826 }
827 fi
828 fi
829
830 olsrd_setup_smartgw_rules
831 }
832
833 stop() {
834 SERVICE_PID_FILE="$PID"
835 service_stop /usr/sbin/olsrd
836 }