olsrd: init: updated (c)-year to 2017
[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 olsrd_write_loadplugin() {
479 local funcname="olsrd_write_loadplugin"
480 local cfg="$1"
481 validate_varname "$cfg" || return 0
482 local ignore
483 local name
484 local suffix
485 local lat
486 local lon
487 local latlon_infile
488
489 config_get_bool ignore "$cfg" ignore 0
490 [ "$ignore" -ne 0 ] && return 0
491
492 config_get library "$cfg" library
493 if ! validate_olsrd_option "$library"; then
494 warning_invalid_value olsrd "$cfg" "library"
495 return 0
496 fi
497 if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then
498 log "$funcname() Warning: Plugin library '$library' not found, skipped"
499 return 0
500 fi
501
502 case "$library" in
503 olsrd_nameservice.*)
504 config_get name "$cfg" name
505 [ -z "$name" ] && config_set "$cfg" name $SYSTEM_HOSTNAME
506
507 config_get suffix "$cfg" suffix
508 [ -z "$suffix" ] && config_set "$cfg" suffix '.olsr'
509
510 config_get lat "$cfg" lat
511 config_get lon "$cfg" lon
512 config_get latlon_infile "$cfg" latlon_infile
513 if [ \( -z "$lat" -o -z "$lat" \) -a -z "$latlon_infile" ]; then
514 if [ -f '/var/run/latlon.txt' ]; then
515 config_set "$cfg" lat ''
516 config_set "$cfg" lon ''
517 config_set "$cfg" latlon_infile '/var/run/latlon.txt'
518 else
519 config_set "$cfg" lat "$SYSTEM_LAT"
520 config_set "$cfg" lon "$SYSTEM_LON"
521 fi
522 fi
523
524 for f in latlon_file hosts_file services_file resolv_file macs_file; do
525 config_get $f "$cfg" $f
526 done
527
528 [ -z "$latlon_file" ] && config_set "$cfg" latlon_file '/var/run/latlon.js'
529 ;;
530 olsrd_watchdog.*)
531 config_get wd_file "$cfg" file
532 ;;
533 esac
534
535 echo -n "${N}LoadPlugin \"$library\"${N}{"
536 config_write_options "$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam "${T}"
537 echo "${N}}"
538
539 return 0
540 }
541
542 olsrd_write_interface() {
543 local funcname="olsrd_write_interface"
544 local cfg="$1"
545 validate_varname "$cfg" || return 0
546 local ignore
547 local interfaces
548 local interface
549 local ifnames
550
551 config_get_bool ignore "$cfg" ignore 0
552 [ "$ignore" -ne 0 ] && return 0
553
554 ifnames=
555 config_get interfaces "$cfg" interface
556
557 for interface in $interfaces; do
558 if validate_varname "$interface"; then
559 if network_get_device IFNAME "$interface"; then
560 ifnames="$ifnames \"$IFNAME\""
561 ifsglobal="$ifsglobal $IFNAME"
562 elif network_get_physdev IFNAME "$interface"; then
563 local proto="$(uci -q get network.${interface}.proto)"
564 if [ "$proto" = "static" -o "$proto" = "none" ]; then
565 ifnames="$ifnames \"$IFNAME\""
566 ifsglobal="$ifsglobal $IFNAME"
567 fi
568 else
569 log "$funcname() Warning: Interface '$interface' not found, skipped"
570 fi
571 else
572 warning_invalid_value olsrd "$cfg" "interface"
573 fi
574 done
575
576 [ -z "$ifnames" ] && return 0
577
578 echo -n "${N}Interface$ifnames${N}{"
579 config_write_options "$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option "${T}"
580 echo "${N}}"
581 INTERFACES_COUNT=$((INTERFACES_COUNT + 1))
582
583 return 0
584 }
585
586 olsrd_write_interface_defaults() {
587 local cfg="$1"
588 validate_varname "$cfg" || return 0
589
590 echo -n "${N}InterfaceDefaults$ifnames${N}{"
591 config_write_options "$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option "${T}"
592 echo "${N}}"
593
594 return 1
595 }
596
597 olsrd_update_schema() {
598 local command="$1"
599 validate_varname "$command" || return 0
600 local varname="$2"
601 validate_varname "$varname" || return 0
602 local value="$3"
603 local cfg="$CONFIG_SECTION"
604 local cfgt
605 local cur_varname
606
607 config_get cfgt "$cfg" TYPE
608 case "$cfgt" in
609 olsrd) config_update_schema OLSRD_OLSRD_SCHEMA "$command" "$varname" "$value";;
610 IpcConnect) config_update_schema OLSRD_IPCCONNECT_SCHEMA "$command" "$varname" "$value";;
611 LoadPlugin) config_update_schema OLSRD_LOADPLUGIN_SCHEMA "$command" "$varname" "$value";;
612 Interface) config_update_schema OLSRD_INTERFACE_SCHEMA "$command" "$varname" "$value";;
613 InterfaceDefaults) config_update_schema OLSRD_INTERFACE_DEFAULTS_SCHEMA "$command" "$varname" "$value";;
614 esac
615
616 return 0
617 }
618
619 olsrd_write_config() {
620 OLSRD_COUNT=0
621 config_foreach olsrd_write_olsrd olsrd
622 IPCCONNECT_COUNT=0
623 config_foreach olsrd_write_ipcconnect IpcConnect
624 HNA4_COUNT=0
625 config_foreach olsrd_write_hna4 Hna4
626 [ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
627 HNA6_COUNT=0
628 config_foreach olsrd_write_hna6 Hna6
629 [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
630 config_foreach olsrd_write_loadplugin LoadPlugin
631 INTERFACES_COUNT=0
632 config_foreach olsrd_write_interface_defaults InterfaceDefaults
633 config_foreach olsrd_write_interface Interface
634 echo
635
636 return 0
637 }
638
639 get_wan_ifnames()
640 {
641 local wanifnames word catch_next
642
643 which ip >/dev/null || return 1
644
645 set -- $( ip route list exact 0.0.0.0/0 table all )
646 for word in $*; do
647 case "$word" in
648 dev)
649 catch_next="true"
650 ;;
651 *)
652 [ -n "$catch_next" ] && {
653 case "$wanifnames" in
654 *" $word "*)
655 ;;
656 *)
657 wanifnames="$wanifnames $word "
658 ;;
659 esac
660
661 catch_next=
662 }
663 ;;
664 esac
665 done
666
667 echo "$wanifnames"
668 }
669
670 olsrd_setup_smartgw_rules() {
671 local funcname="olsrd_setup_smartgw_rules"
672 # Check if ipip is installed
673 [ -e /etc/modules.d/[0-9]*-ipip ] || {
674 log "$funcname() Warning: kmod-ipip is missing. SmartGateway will not work until you install it."
675 return 1
676 }
677
678 local wanifnames="$( get_wan_ifnames )"
679
680 if [ -z "$wanifnames" ]; then
681 nowan=1
682 else
683 nowan=0
684 fi
685
686 IP4T=$(which iptables)
687 IP6T=$(which ip6tables)
688
689 # Delete smartgw firewall rules first
690 if [ "$UCI_CONF_NAME" = "olsrd6" ]; then
691 while $IP6T -D forwarding_rule -o tnl_+ -j ACCEPT 2> /dev/null; do :;done
692 for IFACE in $wanifnames; do
693 while $IP6T -D forwarding_rule -i tunl0 -o $IFACE -j ACCEPT 2> /dev/null; do :; done
694 done
695 for IFACE in $ifsglobal; do
696 while $IP6T -D input_rule -i $IFACE -p 4 -j ACCEPT 2> /dev/null; do :; done
697 done
698 else
699 while $IP4T -D forwarding_rule -o tnl_+ -j ACCEPT 2> /dev/null; do :;done
700 for IFACE in $wanifnames; do
701 while $IP4T -D forwarding_rule -i tunl0 -o $IFACE -j ACCEPT 2> /dev/null; do :; done
702 done
703 for IFACE in $ifsglobal; do
704 while $IP4T -D input_rule -i $IFACE -p 4 -j ACCEPT 2> /dev/null; do :; done
705 done
706 while $IP4T -t nat -D postrouting_rule -o tnl_+ -j MASQUERADE 2> /dev/null; do :;done
707 fi
708
709 if [ "$smartgateway" = "yes" ]; then
710 log "$funcname() Notice: Inserting firewall rules for SmartGateway"
711 if [ ! "$smartgatewayuplink" = "none" ]; then
712 if [ "$smartgatewayuplink" = "ipv4" ]; then
713 # Allow everything to be forwarded to tnl_+ and use NAT for it
714 $IP4T -I forwarding_rule -o tnl_+ -j ACCEPT
715 $IP4T -t nat -I postrouting_rule -o tnl_+ -j MASQUERADE
716 # Allow forwarding from tunl0 to (all) wan-interfaces
717 if [ "$nowan"="0" ]; then
718 for IFACE in $wanifnames; do
719 $IP4T -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
720 done
721 fi
722 # Allow incoming ipip on all olsr-interfaces
723 for IFACE in $ifsglobal; do
724 $IP4T -I input_rule -i $IFACE -p 4 -j ACCEPT
725 done
726 elif [ "$smartgatewayuplink" = "ipv6" ]; then
727 $IP6T -I forwarding_rule -o tnl_+ -j ACCEPT
728 if [ "$nowan"="0" ]; then
729 for IFACE in $wanifnames; do
730 $IP6T -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
731 done
732 fi
733 for IFACE in $ifsglobal; do
734 $IP6T -I input_rule -i $IFACE -p 4 -j ACCEPT
735 done
736 else
737 $IP4T -t nat -I postrouting_rule -o tnl_+ -j MASQUERADE
738 for IPT in $IP4T $IP6T; do
739 $IPT -I forwarding_rule -o tnl_+ -j ACCEPT
740 if [ "$nowan"="0" ]; then
741 for IFACE in $wanifnames; do
742 $IPT -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
743 done
744 fi
745 for IFACE in $ifsglobal; do
746 $IPT -I input_rule -i $IFACE -p 4 -j ACCEPT
747 done
748 done
749 fi
750 fi
751 fi
752 }
753
754 start() {
755 SYSTEM_HOSTNAME=
756 SYSTEM_LAT=
757 SYSTEM_LON=
758 config_load system
759 config_foreach system_config system
760
761 option_cb() {
762 olsrd_update_schema "option" "$@"
763 }
764
765 list_cb() {
766 olsrd_update_schema "list" "$@"
767 }
768
769 . /lib/functions/network.sh
770
771 config_load $UCI_CONF_NAME
772 reset_cb
773
774 OLSRD_CONFIG_FILE=
775 config_foreach olsrd_find_config_file olsrd
776
777 if [ -z "$OLSRD_CONFIG_FILE" ]; then
778 mkdir -p -- /var/etc/
779 olsrd_write_config > /var/etc/$UCI_CONF_NAME.conf || return 1
780 if [ "$INTERFACES_COUNT" -gt 0 -a "$OLSRD_COUNT" -gt 0 ]; then
781 OLSRD_CONFIG_FILE=/var/etc/$UCI_CONF_NAME.conf
782 fi
783 fi
784
785 [ -z "$OLSRD_CONFIG_FILE" ] && return 1
786
787 SERVICE_PID_FILE="$PID"
788 if service_check /usr/sbin/olsrd; then
789 error "there is already an instance of $UCI_CONF_NAME running (pid: '$(cat $PID)'), not starting."
790 return 1
791 else
792 service_start /usr/sbin/olsrd -f "$OLSRD_CONFIG_FILE" -nofork
793 sleep 1
794 service_check /usr/sbin/olsrd || {
795 log "startup-error: check via: '/usr/sbin/olsrd -f \"$OLSRD_CONFIG_FILE\" -nofork'"
796 }
797 fi
798
799 olsrd_setup_smartgw_rules
800 }
801
802 stop() {
803 SERVICE_PID_FILE="$PID"
804 service_stop /usr/sbin/olsrd
805 }