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