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