Developing lightweight computation at the DSG edge

qmp_functions.sh 34.8 KB
Newer Older
1
#!/bin/sh /etc/rc.common
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#Variable in upper case
#Tabular insertions according to GSF code standards
#Creation logging messages format
#Renaming variables and functions names according to standard nomenclature about IPv6 adressing scheme
#IPv4 and IPv6 variables naming, under unified criteria:
#IP6_GLOBAL_PREFIX{M1}
#IP6_ULA_PREFIX
#IP{P}_SUBNETID{M2}
#IP{P}_IFACEID{M3}
#P={4,6] Mi=Number of bits => Sum(Mi)=128/32 in IPv6/IPv4
#VID assigment method is more flexible. We admit in UCI variable 'option mesh_protocol_vids' the notation: mesh_devN[:vid], where vid is optional. More convenient in switched environment based in cable
#If none VID is supplied it is assumed 'option mesh_vid_offset' value, with incremental progression through the meshing network interfaces
#Main idea:
#Only one global and ULA prefix for community. Therefore a community is a node network with common global prefix.
#Each node have a community_node_id (12bits). The administrator have to check the unicity of this number inside the community. If none number os supplied, the last 12 bits of main MAC address wirl be used
#Each protocol will have 4bits (from 0 to F)
# [GLOBAL PREFIX 48]:[COMM-NUMBER 12 | PROTOCOL NUMBER 4]:1
# [ULA PREFIX 48]:[MAC-ADDRESS 48]:[COMM-NUMBER 12 | PROTOCOL NUMBER 4]:1

21
22
23
24
25
#START=91


# requires ip ipv6calc awk sed grep

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#Required UCI section names
BASE_CONFIG=network
QMP_CONFIG=qmp
BMX6_CONFIG=bmx6
OLSR6_CONFIG=olsrd
OLSR6_UCI_CONFIG=olsrd_uci

#Canonical names of protocols
BMX6_CNAME=bmx6
OLSR6_CNAME=olsr6

LOGFILE=/tmp/qmp.log

#Set UCI-options function
qmp_set_option(){

	local UCI_ARG=$1
	local VALUE=$2
	local SOURCE=$3

	if [ -z "$SOURCE" ]
	then
		SOURCE="qmp_set_option"
	fi

	if [ -z "$UCI_ARG" ]
	then
		qmp_log_file "$SOURCE" "Setting UCI config parameter" "Nothing to set"
	else

		if uci set $UCI_ARG="$VALUE" &> /dev/null
		then
Victor Oncins's avatar
Victor Oncins committed
58
59
60
			#qmp_log_all "$SOURCE" "Setting UCI '$UCI_ARG'" "Done"
			qmp_log_file "$SOURCE" "Setting UCI '$UCI_ARG'='$VALUE'" "Done" 
		else 
61
62
63
64
			#qmp_log_all "$SOURCE" "Setting UCI '$UCI_ARG'" "Failed"
			qmp_log_file "$SOURCE" "Setting UCI '$UCI_ARG'='$VALUE', returned '$(uci set $UCI_ARG=$VALUE 2>&1)'" "Error"
		fi
	fi
65

66
}
67

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#Get UCI-options function
qmp_get_option(){

	local UCI_ARG=$1
	local SOURCE=$2

	if [ -z "$SOURCE" ]
	then
		SOURCE="qmp_get_option"
	fi

	if [ -z "$UCI_ARG" ]
	then
		#qmp_log_all "$SOURCE" "Getting UCI config parameter" "[Nothing to get]"
		qmp_log_file "$SOURCE" "Getting UCI argument" "Nothing to get"
	else
		if uci get $UCI_ARG &> /dev/null
		then
			uci get $UCI_ARG
			#qmp_log_all "$SOURCE" "Getting UCI '$UCI_ARG'" "Done"
			qmp_log_file "$SOURCE" "Getting UCI '$UCI_ARG', uci get $UCI_ARG='$(uci get $UCI_ARG)'" "Done"
		else
			#qmp_log_all "$SOURCE" "Getting UCI '$UCI_ARG'" "Failed"
			qmp_log_file "$SOURCE" "Getting UCI '$UCI_ARG', returned '$(uci get $UCI_ARG 2>&1)'" "Error"
		fi
	fi
94

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
}

#Add UCI-sections function
qmp_add_option(){

	local UCI_ARG=$1
	local VALUE=$2
	local SOURCE=$3

	if [ -z "$SOURCE" ]
	then
		SOURCE="qmp_add_option"
	fi

	if [ -z "$UCI_ARG" ]
	then
		qmp_log_file "$SOURCE" "Adding UCI config parameter" "Nothing to set"
	else

		if uci add $UCI_ARG=$VALUE &> /dev/null
		then
			#qmp_log_all "$SOURCE" "Adding UCI '$UCI_ARG'" "Done"
			qmp_log_file "$SOURCE" "Adding UCI '$UCI_ARG'='$VALUE'" "Done"
		else
			#qmp_log_all "$SOURCE" "Adding UCI '$UCI_ARG'" "Failed"
			qmp_log_file "$SOURCE" "Adding UCI '$UCI_ARG'='$VALUE', returned '$(uci add $UCI_ARG=$VALUE 2>&1)'" "Error"
		fi
	fi

}

#Backup the original config file and revert its UCI-contents
qmp_config_revert() {

	local CONF=$1
	local SOURCE=$2

	if [ -z "$SOURCE" ]
	then
		SOURCE="qmp_config_revert"
	fi

	#Absolute path of file configuration
	local CONF_PATH=/etc/config

	if [ "$CONF" = "" ]
	then
		qmp_log_file "$SOURCE" "Reverting UCI config" "Nothing to set"
	else

		if [ -f $CONF_PATH/$CONF ]
		then
			cp $CONF_PATH/$CONF $CONF_PATH/$CONF.old
			qmp_log_file "$SOURCE" "Saved previous config in $CONF_PATH/$CONF.old" "Done"
		fi

		if uci revert $CONF &> /dev/null
		then
			qmp_log_file "$SOURCE" "Reverting UCI config '$CONF'" "Done"
		else
			qmp_log_file "$SOURCE" "Reverting UCI config '$CONF', returned '$(uci revert $CONF 2>&1)'" "Error"
		fi 

		echo "" > $CONF_PATH/$CONF
	fi
}

# Execute the commiting command of UCI
qmp_config_commit(){

	local CONF=$1
	local SOURCE=$2

	if [ -z "$SOURCE" ]
	then
		SOURCE="qmp_config_commit"
	fi

	if [ "$CONF" = "" ]
	then
		qmp_log_file "$SOURCE" "Comitting UCI" "Nothing to commit"
	else
		if uci commit $CONF &> /dev/null
		then
			qmp_log_file "$SOURCE" "Comitting UCI '$CONF'" "Done"
		else
			qmp_log_file "$SOURCE" "Comitting UCI '$CONF', returned '$(uci commit $CONF 2>&1)'" "Failed"
		fi

	fi

}

# Text colour control function returns text string colored or styled (ANSI terminals)
#qmp_styled_string() {

#	local STRING=$1
#	local STYLE=$2

#	case $STYLE in
#		"BLUE") printf "\033[1;34;48m%s\033[0m" "$STRING";;
#		"GREEN") printf "\033[1;32;48m%s\033[0m" "$STRING";;
#		"RED") printf "\033[1;31;48m%s\033[0m" "$STRING" ;;
#		"BOLD") printf "\033[1;30;48m%s\033[0m" "$STRING";;
#		"YELLOW") printf "\033[1;33;48m%s\033[0m" "$STRING";;
#		*) printf "$STRING"
#	esac
#}

# Send a formated logging message to standard output
qmp_log_stdout(){

	local SOURCE=$1
	local INSTANCE=$2
	local OUTCOME=$3

	if [ -z "$SOURCE" ]
	then
		SOURCE="qmp_log_stdout"
	fi

	if [ -z "$OUTCOME" ]
	then
218
		printf "%s: %s\n" "$SOURCE" "$INSTANCE"
219
	else
220
		printf "%s: %s -> %s\n" "$SOURCE" "$INSTANCE" "[$OUTCOME]"
221
222
223
224
225

	fi

}

226
# Send a message to syslog
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
qmp_log_file(){

	local SOURCE=$1
	local INSTANCE=$2
	local OUTCOME=$3

	if [ -z "$SOURCE" ]
	then
		SOURCE="qmp_log_file"
	fi

	local NODENAME="$(uname -n)"
	local DAYTIME="$(date +"%Y/%m/%d %T")"

	if [ -z "$OUTCOME" ]
	then
Victor Oncins's avatar
Victor Oncins committed
243
		printf "%s %s %s: %s\n" "$DAYTIME" "$NODENAME" "$SOURCE" "$INSTANCE" >> $LOGFILE
244
	else
Victor Oncins's avatar
Victor Oncins committed
245
		printf "%s %s %s: %s -> %s\n" "$DAYTIME" "$NODENAME" "$SOURCE" "$INSTANCE" "[$OUTCOME]" >> $LOGFILE
246
247
248
249
250

	fi

}

251
#Sends the standard logging message to all streams
252
253
254
255
256
257
258
259
260
261
262
263
264
qmp_log_all() {

	local SOURCE=$1
	local INSTANCE=$2
	local OUTCOME=$3

	if [ -z "$SOURCE" ]
	then
		SOURCE="qmp_log_all"
	fi

	qmp_log_file "$SOURCE" "$INSTANCE" "$OUTCOME"
	qmp_log_stdout "$SOURCE" "$INSTANCE" "$OUTCOME"
265
266
267

}

268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# Check whether the passed argument is defined in UCI config file
qmp_config_check() {

	local OPTION=$1

	if uci get $OPTION &> /dev/null
	then
		return 0
	fi

	return 1
}

#Provide the MAC of a supplied network device. Otherwise a default MAC addres is returned
qmp_get_dev_mac() {

	local DEV=$1

	#A convenient default MAC address
	local DEF_MAC="00:00:00:00:00:00"

	if ip addr show dev $DEV &> /dev/null
	then
		ip addr show dev $DEV | grep -m 1 "link/ether" | awk '{print $2}'
	else
		echo $DEF_MAC
	fi
295
296
297
}

qmp_get_llocal_for_dev() {
298
299

	local dev=$1
300
301
302
303
304
305
306
307
308
309
  ip a show dev $dev | grep inet6 | awk '{print $2}'
}


qmp_get_ip6_slow() {

  local addr_prefix="$1"
  local addr="$(echo $addr_prefix | awk -F'/' '{print $1}')"
  local mask="$(echo $addr_prefix | awk -F'/' '{print $2}')"

310
311
  echo "qmp_get_ip6_slow addr_prefix=$addr_prefix addr=$addr mask=$mask" 1>&2

312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
  if [ -z "$mask" ] ; then
    mask="128"
  fi

  local addr_in=$addr
  local addr_out=""
  local found=0

  while ! [ -z "$addr_in" ] ; do

    addr_in=$( echo $addr_in | sed -e "s/^://g" )

    if echo "$addr_in" | grep "^:"  >/dev/null 2>&1 ; then

      if echo "$addr_in" | grep "::"  >/dev/null 2>&1 ; then
        echo "Invalid 1 IPv6 address $addr_prefix" 1>&2
        return 1
      fi

      addr_in=$( echo $addr_in | sed -e "s/^://g" )

      if [ -z "$addr_in" ] ; then
        addr_out="$addr_out::"
      else
        addr_out="$addr_out:"
      fi

    else

      local addr16="$(echo $addr_in | awk -F':' '{print $1}')"
      addr_in=$( echo $addr_in | sed -e "s/^$addr16//g" )

      if [ -z "$addr_out" ] ; then
	addr_out="$addr16"
      else
	addr_out="$addr_out:$addr16"
      fi

      if echo "$addr16" | grep '\.'  >/dev/null 2>&1 ; then
        found=$(( $found + 2 ))
      else
        found=$(( $found + 1 ))
      fi

    fi

  done

  if echo $addr_out | grep "::" >/dev/null 2>&1 && [ "$found" -lt "8" ] ; then

    local insert="0"
    for n in $( seq $found "6" ) ; do
      insert="$insert:0"
    done

    addr_out=$( echo $addr_out | sed -e "s/^::$/$insert/g" )
    addr_out=$( echo $addr_out | sed -e "s/^::/$insert:/g" )
    addr_out=$( echo $addr_out | sed -e "s/::$/:$insert/g" )
    addr_out=$( echo $addr_out | sed -e "s/::/:$insert:/g" )

  elif echo $addr_out | grep "::"  >/dev/null 2>&1 || [ "$found" != "8" ] ; then
    echo "Invalid 2 IPv6 address $addr_prefix found=$found" 1>&2
    return 1
  fi


#  echo "Correct IPv6 address $addr_prefix addr_out=$addr_out found=$found" 1>&2

  local pos=0
  addr_in=$addr_out
  addr_out=""

  while ! [ -z "$addr_in" ] ; do

    addr_in=$( echo $addr_in | sed -e "s/^://g" )

    local addr16="$( echo $addr_in | awk -F':' '{print $1}' )"
    addr_in=$( echo $addr_in | sed -e "s/^$addr16//g" )

    if echo $addr16 | grep '\.' >/dev/null 2>&1  ; then
      local ip1=$( echo $addr16 | awk -F'.' '{print $1}' )
      local ip2=$( echo $addr16 | awk -F'.' '{print $2}' )
      local ip3=$( echo $addr16 | awk -F'.' '{print $3}' )
      local ip4=$( echo $addr16 | awk -F'.' '{print $4}' )

#      echo "addr16=$addr16 ip1=$ip1 ip2=$ip2 ip3=$ip3 ip4=$ip4" 1>&2

399
      addr16=$( printf "%x" $(( $(( $ip1 * 0x100 )) + $ip2 )) )
400
401
402
403
404
405

      if [ -z "$ip4" ] ; then
        echo "Invalid 3 IPv6 address $addr_prefix" 1>&2
        return 1
      fi
      
406
      addr_in=$( printf "%x" $(( $(( $ip3 * 0x100 )) + $ip4 )) )$addr_in
407
408
409
410
411
412
413
414
415
416


    fi

    local prefix16
    if [ "$pos" -le "$mask" ] ; then

      if [ "$(( $pos + 16 ))" -le "$mask" ] ; then
	prefix16=$addr16
      else
417
	prefix16=$( printf "%x" $(( 0x$addr16 & 0xFFFF<<$(( $(( $pos + 16 )) - $mask )) )) )
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
      fi

    else
      prefix16="0"
    fi


    if [ -z "$addr_out" ] ; then
      addr_out="$prefix16"
    else
      addr_out="$addr_out:$prefix16"
    fi

    pos=$(( $pos + 16 ))

  done

  echo "$addr_out"
}



440
#Get extented IPv6 address, from a given compact notation IPv6 prefix and mask
441
442
qmp_get_ip6_fast() {

443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
	if ! [ -x /usr/bin/ipv6calc ] ; then
		qmp_get_ip6_slow $1
		return $?
	fi

	local SOURCE="qmp_get_ip6_fast"
	local ADDR_PREFIX="$1"
	local ADDR="$(echo $ADDR_PREFIX | awk -F'/' '{print $1}')"
	local MASK="$(echo $ADDR_PREFIX | awk -F'/' '{print $2}')"
	local DEF_ADDR="fdff::"
	local DEF_MASK="128"

	if [ -z "$ADDR" ]
	then
		#A default prefix is defined if none value is supplied
		ADDR=$DEF_ADDR
		qmp_log_file "$SOURCE" "Supplied prefix must have non-void value, prefix='$ADDR'" "Warning"
	fi

	if [ -z "$MASK" ] || [ "$(( $MASK % 16))" != "0" ]
	then
		#A default mask is defined if no value is supplied
		MASK=$DEF_MASK
		qmp_log_file "$SOURCE" "Supplied bitmask has a value that is null or non multiple of 16, new bitmask=$MASK" "Warning"
		#return 1
	fi

	if ipv6calc -q --in ipv6 $ADDR --showinfo -m &> /dev/null
	then
		local EXT_ADDR=$( ipv6calc -q  --in ipv6 $ADDR --showinfo -m | grep IPV6= | awk -F'=' '{print $2}' )
	else
		local EXT_ADDR=$( ipv6calc -q  --in ipv6 $DEF_ADDR --showinfo -m | grep IPV6= | awk -F'=' '{print $2}' )
		qmp_log_file "$SOURCE" "Supplied prefix '$ADDR/$MASK' is not valid" "Error"
		return 1
	fi

	if [ "$MASK" -ge "0" ] &&  [ "$MASK" -le "112" ]
	then
		echo "$( ipv6calc --in ipv6 $ADDR/$MASK -F --printprefix --out ipv6addr )::/$MASK"
	else
		echo "$( ipv6calc --in ipv6 $ADDR/$DEF_MASK -F --printprefix --out ipv6addr )"
	fi
485
486
487

}

488
489
#Calculate a complete ULA address from given components according to 48/48/32 bitscheme
qmp_calculate_ula96() {
490

491
492
493
	local PREFIX=$1 
	local MAC_ADDR=$2
	local SUFFIX=$3
494

495
	local SOURCE="qmp_calculate_ula96"
496

497
498
499
500
501
	#Check whether doubled-colon is present in PREFIX
	if echo $PREFIX |grep "::" &> /dev/null
	then
		PREFIX=$( echo $PREFIX | sed 's/::/:0:/g' )
	fi
502
503


504
505
506
507
508
509
	local MAC_ADDR1="$( echo $MAC_ADDR | awk -F':' '{print $1}' )"
	local MAC_ADDR2="$( echo $MAC_ADDR | awk -F':' '{print $2}' )"
	local MAC_ADDR3="$( echo $MAC_ADDR | awk -F':' '{print $3}' )"
	local MAC_ADDR4="$( echo $MAC_ADDR | awk -F':' '{print $4}' )"
	local MAC_ADDR5="$( echo $MAC_ADDR | awk -F':' '{print $5}' )"
	local MAC_ADDR6="$( echo $MAC_ADDR | awk -F':' '{print $6}' )"
510

511
	local IP6_SUBNETID48=`printf ":%x:%x:%x\n" $(( ( 0x$MAC_ADDR1 * 0x100 ) + 0x$MAC_ADDR2 )) $(( ( 0x$MAC_ADDR3 * 0x100 ) + 0x$MAC_ADDR4 )) $(( ( 0x$MAC_ADDR5 * 0x100 ) + 0x$MAC_ADDR6 )))`
512

513
	qmp_log_file "$SOURCE" "prefix='$PREFIX', subnetid='$IP6_SUBNETID48', suffix='$SUFFIX'" ""
514

515
	local IP6_ULA_ADDR=$( qmp_get_ip6_fast "${PREFIX}${IP6_SUBNETID48}${SUFFIX}/128" )
516

517
518
519
520
	if [ -z "$IP6_ULA_ADDR" ]
	then
		return 1
	fi
521

522
	echo $IP6_ULA_ADDR
523
524
525

}

526
527
#Build node id number
qmp_node_id() {
528

529
	local PRIMARY_DEV=$1
530

531
	local SOURCE="qmp_node_id"
532

533
	local NODE_ID
534

535
536
537
538
539
540
	if qmp_config_check ${QMP_CONFIG}.node.community_node_id
	then
		NODE_ID=$(uci get ${QMP_CONFIG}.node.community_node_id)
	else
		NODE_ID="4096"
	fi
541

542
	local NODE_PRJ
543

544
545
546
	if [ "$NODE_ID" -ge "0" ] && [ "$NODE_ID" -le "4095" ]
	then
		NODE_PRJ=$NODE_ID
547
		qmp_log_file "$SOURCE" "Builded node-id='$NODE_PRJ' from '${QMP_CONFIG}.node.community_node_id'" ""
548
549
550
551
552
	else
		local MAC_ADDR=$(qmp_get_dev_mac $PRIMARY_DEV)
		local MAC_ADDR5="$( echo $MAC_ADDR | awk -F':' '{print $5}' )"
		local MAC_ADDR6="$( echo $MAC_ADDR | awk -F':' '{print $6}' )"
		NODE_PRJ=$(( 0x$MAC_ADDR6 + ( 0x$MAC_ADDR5 & 0xf ) * 0x100  ))
553
		qmp_log_file "$SOURCE" "Builded node-id='$NODE_PRJ' from $PRIMARY_DEV MAC address" ""
554
	fi
555

556
	echo $NODE_PRJ
557
558
559
560

}


561
562
#Calculate the community node id projection onto an IPv6 address byte-group (16 bits = 12 node-id-bits + 4 protocol-id-bits) from the canonical protocol's names
qmp_node_proj() {
563

564
565
	local PRIMARY_DEV=$1
	local PROT_CNAME=$2
566

567
	local SOURCE="qmp_node_proj"
568

569
	local PROTOCOL_ID
570

571
572
573
574
575
576
	case $PROT_CNAME in
		"$OLSR6_CNAME") PROTOCOL_ID="1";;
		"$BMX6_CNAME") PROTOCOL_ID="2";;
		#Add more protocol values between 1 and 15 (4 bits)
		*) PROTOCOL_ID="0";;
	esac
577

578
	local NODE_PRJ=$(( 0x$(printf "%x" $(qmp_node_id $PRIMARY_DEV)) * 0x10 ))
579

580
581
582
583
584
585
586
	if [ "$PROTOCOL_ID" -gt "0" ] && [ "$PROTOCOL_ID" -le "15" ]
	then
		NODE_PRJ=$(( $NODE_PRJ + $PROTOCOL_ID ))
		printf "%x" $NODE_PRJ
	else
		printf "%x" $NODE_PRJ
	fi
587

588
	qmp_log_file "$SOURCE" "Byte-group-projection='$(printf "0x%x" $NODE_PRJ)', protocol='$PROT_CNAME'"
589

590
591
}

592
593
#Calculate a complete global address from given components according to 48/16/64 bitscheme
qmp_calculate_global64() {
594

595
596
597
	local PREFIX=$1 
	local NODE=$2
	local SUFFIX=$3
598

599
	local SOURCE="qmp_calculate_global64"
600

601
	#Only file output stream is allowed in this function, otherwise its output interferes with proper returned value
602

603
604
605
606
607
	#Check whether doubled-colon is present in PREFIX
	if echo $PREFIX |grep "::" &> /dev/null
	then
		PREFIX=$( echo $PREFIX | sed 's/::/:0:/g' )
	fi
608

609
	local IP6_SUBNETID16
610

611
612
613
614
615
616
617
	if printf ":%x\n" 0x$NODE &> /dev/null
	then
		IP6_SUBNETID16=`printf ":%x\n" 0x$NODE`
	else
		qmp_log_file "$SOURCE" "Incorrect node value, printf='`printf ":%x\n" 0x$NODE 2>&1`'" "Error"
		return 1
	fi
618

619
620
	qmp_log_file "$SOURCE" "prefix='$PREFIX', subnetid='$IP6_SUBNETID16', suffix='$SUFFIX'"
	local IP6_GLOB_ADDR=$( qmp_get_ip6_fast "${PREFIX}${IP6_SUBNETID16}${SUFFIX}/128" )
621

622
623
624
625
	if [ -z "$IP6_GLOB_ADDR" ]
	then
		return 1
	fi
626

627
	echo $IP6_GLOB_ADDR
628
629
630

}

631
632
#Compose a complete ULA address from given components: ULA prefix, network device, suffix and mask
qmp_get_ula96() {
633

634
635
636
637
	local PREFIX=$1
	local DEV_MAC=$2
	local SUFFIX=$3
	local MASK=$4
638

639
640
	local MAC_ADDR=$( qmp_get_dev_mac $DEV_MAC )
	local IP6_ULA_ADDR=$( qmp_calculate_ula96 $PREFIX $MAC_ADDR $SUFFIX )
641

642
643
	echo "$IP6_ULA_ADDR/$MASK"
}
644
645
646



647
qmp_get_global64() {
648

649
650
651
652
	local PREFIX=$1
	local NODE=$2
	local SUFFIX=$3
	local MASK=$4
653

654
	local IP6_GLOB_ADDR=$( qmp_calculate_global64 $PREFIX $NODE $SUFFIX )
655

656
657
	echo "$IP6_GLOB_ADDR/$MASK"
}
658
659


660
661
#The first device on the list CONFIG.interfaces.mesh_devices is the primary one
qmp_get_primary_dev() {
662

663
664
	local CONFIG=$1
	local SOURCE="qmp_get_primary_dev"
665

666
667
668
669
670
671
672
	if qmp_config_check $CONFIG.interfaces.mesh_devices
	then
		local PRIMARY_MESH_DEV="$(uci get $CONFIG.interfaces.mesh_devices | awk '{print $1}')"
	else
		#Only file output stream is allowed in this function, otherwise its output interferes with proper returned value
		qmp_log_file "$SOURCE" "No primary mesh iface, $CONFIG.interfaces.mesh_devices = void" "Fail"
	fi
673

674
	echo $PRIMARY_MESH_DEV
675

676
}
677
678


679
qmp_configure_network() {
680

681
	local SOURCE="qmp_configure_network"
682

Victor Oncins's avatar
Victor Oncins committed
683
	qmp_log_all "$SOURCE" "General UCI network configuration started..." ""
684

685
	qmp_config_revert "$BASE_CONFIG" "$SOURCE"
686

687
	#If there is 'configure_switch' field in qmp file, configure it! PLEASE REVIEW IT!!!!!
688

689
690
	if qmp_config_check ${QMP_CONFIG}.interfaces.configure_switch
	then
691

Victor Oncins's avatar
Victor Oncins committed
692
	qmp_log_all "$SOURCE" "Switch configuration" ""
693

694
		local SWITCH_DEVICE="$(uci get ${QMP_CONFIG}.interfaces.configure_switch)"
695

696
697
698
699
		qmp_set_option $BASE_CONFIG.$SWITCH_DEVICE "switch" "$SOURCE"
		qmp_set_option $BASE_CONFIG.$SWITCH_DEVICE.enable "1" "$SOURCE"
		qmp_set_option $BASE_CONFIG.$SWITCH_DEVICE.enable_vlan "1" "$SOURCE"
		qmp_set_option $BASE_CONFIG.$SWITCH_DEVICE.reset "1" "$SOURCE"
700

701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
		qmp_set_option $BASE_CONFIG.lan_ports "switch_vlan" "$SOURCE"
		qmp_set_option $BASE_CONFIG.lan_ports.vlan "1" "$SOURCE"
		qmp_set_option $BASE_CONFIG.lan_ports.device "$switch_device" "$SOURCE"
		qmp_set_option $BASE_CONFIG.lan_ports.ports "0 1 5t" "$SOURCE"

		qmp_set_option $BASE_CONFIG.wan_ports "switch_vlan" "$SOURCE"
		qmp_set_option $BASE_CONFIG.wan_ports.vlan "2" "$SOURCE"
		qmp_set_option $BASE_CONFIG.wan_ports.device "$switch_device" "$SOURCE"
		qmp_set_option $BASE_CONFIG.wan_ports.ports "4 5t" "$SOURCE"

		if qmp_config_check ${QMP_CONFIG}.interfaces.mesh_devices && qmp_config_check ${QMP_CONFIG}.networks.mesh_protocol_vids && qmp_config_check ${QMP_CONFIG}.networks.mesh_vid_offset
		then

			for PROT_DESCR in $(uci get ${QMP_CONFIG}.networks.mesh_protocol_vids)
			do

				local VID_SUFFIX="$(echo $PROT_DESCR | awk -F':' '{print $2}')"
				local VID_OFFSET="$(uci get ${QMP_CONFIG}.networks.mesh_vid_offset)"
				local VID="$(( $VID_OFFSET + $VID_SUFFIX ))"
				local MESH_PORTS="mesh_ports_vid${VID}"
721

722
723
724
725
				qmp_set_option $BASE_CONFIG.$MESH_PORTS "switch_vlan" "$SOURCE"
				qmp_set_option $BASE_CONFIG.$MESH_PORTS.vlan "$vid" "$SOURCE"
				qmp_set_option $BASE_CONFIG.$MESH_PORTS.device "$switch_device" "$SOURCE"
				qmp_set_option $BASE_CONFIG.$MESH_PORTS.ports "2t 3t 5t" "$SOURCE"
726

727
728
729
			done
		fi
	fi
730

Victor Oncins's avatar
Victor Oncins committed
731
	qmp_log_all "$SOURCE" "Loopback interface configuration" ""
732

733
734
735
736
737
	qmp_set_option $BASE_CONFIG.loopback "interface" "$SOURCE"
	qmp_set_option $BASE_CONFIG.loopback.ifname "lo" "$SOURCE"
	qmp_set_option $BASE_CONFIG.loopback.proto "static" "$SOURCE"
	qmp_set_option $BASE_CONFIG.loopback.ipaddr "127.0.0.1" "$SOURCE"
	qmp_set_option $BASE_CONFIG.loopback.netmask "255.0.0.0" "$SOURCE"
738

739
740
741
	#The declared wan device is configured with dhcp protocol
	if qmp_config_check ${QMP_CONFIG}.interfaces.wan_device
	then
Victor Oncins's avatar
Victor Oncins committed
742
		qmp_log_all "$SOURCE" "Wan interface configuration" ""
743
744
745
746
		qmp_set_option $BASE_CONFIG.wan "interface" "$SOURCE"
		qmp_set_option $BASE_CONFIG.wan.ifname "$(uci get ${QMP_CONFIG}.interfaces.wan_device)" "$SOURCE"
		qmp_set_option $BASE_CONFIG.wan.proto "dhcp" "$SOURCE"
	fi
747

748
	#Setting niit4to6 and niit6to4 tunnel interfaces configuration
Victor Oncins's avatar
Victor Oncins committed
749
	qmp_log_all "$SOURCE" "niit6to4 tunnel interface configuration" ""
750
751
752
	qmp_set_option $BASE_CONFIG.niit4to6 "interface" "$SOURCE"
	qmp_set_option $BASE_CONFIG.niit4to6.proto "none" "$SOURCE"
	qmp_set_option $BASE_CONFIG.niit4to6.ifname "niit4to6" "$SOURCE"
753

754
755
756
	qmp_set_option $BASE_CONFIG.niit6to4 "interface" "$SOURCE"
	qmp_set_option $BASE_CONFIG.niit6to4.proto "none" "$SOURCE"
	qmp_set_option $BASE_CONFIG.niit6to4.ifname "niit6to4" "$SOURCE"
757

758
	#Get the primary mesh device
759
	local PRIMARY_MESH_DEV="$(qmp_get_primary_dev $QMP_CONFIG $SOURCE)"
760

761
762
763
	#Configuration of LAN devices defined in ${QMP_CONFIG} with default IPv4 static address
	if qmp_config_check ${QMP_CONFIG}.interfaces.lan_devices
	then
Victor Oncins's avatar
Victor Oncins committed
764
		qmp_log_all "$SOURCE" "Lan interfaces configuration" ""
765
766
767
768
769
		#ONLY ONE DEVICE IS CONSIDERED? Ali2D3 has 3 ethN...
		qmp_set_option $BASE_CONFIG.lan "interface" "$SOURCE"
		qmp_set_option $BASE_CONFIG.lan.ifname "$(uci get ${QMP_CONFIG}.interfaces.lan_devices)" "$SOURCE"
#    qmp_set_option $BASE_CONFIG.lan.type="bridge"
		qmp_set_option $BASE_CONFIG.lan.proto "static" "$SOURCE"
770
		qmp_set_option $BASE_CONFIG.lan.ipaddr "192.168.31.6" "$SOURCE"
771
		qmp_set_option $BASE_CONFIG.lan.netmask "255.255.255.0" "$SOURCE"
772

773
774
775
		#Configure all the devices defined like mesh device and assign them unicast global adresses and IPv4 addreses
		if qmp_config_check ${QMP_CONFIG}.interfaces.mesh_devices && qmp_config_check ${QMP_CONFIG}.networks.mesh_protocol_vids
		then
776

777
			for PROT_DESCR in $(uci get ${QMP_CONFIG}.networks.mesh_protocol_vids); do
778

779
780
			local PROTOCOL_NAME="$(echo $PROT_DESCR | awk -F':' '{print $1}')"
			local NIIT_DEV="niit4to6_${PROTOCOL_NAME}"
781

Victor Oncins's avatar
Victor Oncins committed
782
783
			qmp_log_all "$SOURCE" "Building niit4to6_${PROTOCOL_NAME} interface with unicast global IPv6 address" ""

784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
			qmp_set_option $BASE_CONFIG.$NIIT_DEV "alias" "$SOURCE"
			qmp_set_option $BASE_CONFIG.$NIIT_DEV.interface "niit4to6" "$SOURCE"
			qmp_set_option $BASE_CONFIG.$NIIT_DEV.proto "static" "$SOURCE"

			local NODE_PROJ="$(qmp_node_proj $PRIMARY_MESH_DEV $PROTOCOL_NAME)"

				if qmp_config_check ${QMP_CONFIG}.node.global_prefix48
				then
					local IP6_GLOBAL_PREFIX48="$(uci get ${QMP_CONFIG}.node.global_prefix48)"
					qmp_set_option $BASE_CONFIG.$NIIT_DEV.ip6addr "$(qmp_get_global64 $IP6_GLOBAL_PREFIX48 $NODE_PROJ ::1 64)" "$SOURCE"
				fi

				if qmp_config_check ${QMP_CONFIG}.networks.${PROTOCOL_NAME}_ipv4_address && qmp_config_check ${QMP_CONFIG}.networks.${PROTOCOL_NAME}_ipv4_netmask
				then
					qmp_set_option $BASE_CONFIG.$NIIT_DEV.ipaddr "$(uci get ${QMP_CONFIG}.networks.${PROTOCOL_NAME}_ipv4_address)" "$SOURCE"
					qmp_set_option $BASE_CONFIG.$NIIT_DEV.netmask "$(uci get ${QMP_CONFIG}.networks.${PROTOCOL_NAME}_ipv4_netmask)" "$SOURCE"

				elif qmp_config_check ${QMP_CONFIG}.networks.${PROTOCOL_NAME}_ipv4_prefix24
				then
					local IP4_SUBNETID16="$(( 0x$NODE_PROJ / 0x100 )).$(( 0x$NODE_PROJ % 0x100 ))"
					qmp_set_option $BASE_CONFIG.$NIIT_DEV.ipaddr "$(uci get ${QMP_CONFIG}.networks.${PROTOCOL_NAME}_ipv4_prefix24).$IP4_SUBNETID16" "$SOURCE"
					qmp_set_option $BASE_CONFIG.$NIIT_DEV.netmask "255.255.255.255" "$SOURCE"
				fi

			done
		fi
	fi

	#Create new VLAN interfaces for each present protocol
	local COUNTER=1

	if qmp_config_check ${QMP_CONFIG}.interfaces.mesh_devices && qmp_config_check ${QMP_CONFIG}.networks.mesh_protocol_vids
	then

	local VID
	local BASE_VID

Victor Oncins's avatar
Victor Oncins committed
821
		qmp_log_all "$SOURCE" "VLAN interfaces configuration" ""
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847

		if qmp_config_check ${QMP_CONFIG}.networks.mesh_vid_offset
		then
			BASE_VID="$(uci get ${QMP_CONFIG}.networks.mesh_vid_offset)"
		else
			BASE_VID="1"
		fi

		for MESH_DEV in $(uci get ${QMP_CONFIG}.interfaces.mesh_devices); do

		VID=$BASE_VID

			for PROTOCOL_DESCR in $(uci get ${QMP_CONFIG}.networks.mesh_protocol_vids); do

				local PROTOCOL_NAME="$(echo $PROTOCOL_DESCR | awk -F':' '{print $1}')"
				local VID_DESCR="$(echo $PROTOCOL_DESCR | awk -F':' '{print $2}')"

				#According to IEEE 802.1Q only VID-values between 1 and 4094 are allowed 
				if [ "$VID_DESCR" -ge "1" ] && [ "$VID_DESCR" -le "4094" ]
				then
					VID=$VID_DESCR
				fi

				#Beginning of interfaces configuration
				local MESH="mesh_${PROTOCOL_NAME}_${COUNTER}"
				local IP6_IFACEID32=":0:$(qmp_node_proj $PRIMARY_MESH_DEV $PROTOCOL_NAME)"
848

849
850
				if qmp_config_check ${QMP_CONFIG}.networks.${PROTOCOL_NAME}_ula_prefix48
				then
851

852
853
854
855
856
857
858
859
					qmp_set_option $BASE_CONFIG.$MESH "interface" "$SOURCE"
					qmp_set_option $BASE_CONFIG.$MESH.ifname "$MESH_DEV.$VID" "$SOURCE"
					qmp_set_option $BASE_CONFIG.$MESH.proto "static" "$SOURCE"
					qmp_set_option $BASE_CONFIG.$MESH.ip6addr "$(qmp_get_ula96 $(uci get ${QMP_CONFIG}.networks.${PROTOCOL_NAME}_ula_prefix48) $MESH_DEV $IP6_IFACEID32 128)" "$SOURCE"
				fi
				#End of interfaces configuration
				
				VID=$(( $VID + 1 ))
860

861
			done
862

863
864
865
			COUNTER=$(( $COUNTER + 1 ))
		done
	fi
866

867
	qmp_config_commit "$BASE_CONFIG" "$SOURCE"
868
869
870
871
872
873

}



qmp_configure_bmx6() {
874

875
	local SOURCE="qmp_configure_bmx6"
876

Victor Oncins's avatar
Victor Oncins committed
877
	qmp_log_all "$SOURCE" "$BMX6_CNAME configuration started..." ""
878
	qmp_config_revert "$BMX6_CONFIG" "$SOURCE"
879

880
	qmp_set_option "$BMX6_CONFIG.general" "$BMX6_CNAME" "$SOURCE"
881
882
	#qmp_set_option "$BMX6_CONFIG.general.ula_prefix" "$(uci get ${QMP_CONFIG}.networks.bmx6_ula_prefix48)::/48" "$SOURCE"
	qmp_set_option "$BMX6_CONFIG.general.globalPrefix" "$(uci get ${QMP_CONFIG}.networks.bmx6_ula_prefix48)::/48" "$SOURCE"
883

884
885
886
887
888
889
890
891
	qmp_set_option "$BMX6_CONFIG.bmx6_config_plugin" "plugin" "$SOURCE"
	qmp_set_option "$BMX6_CONFIG.bmx6_config_plugin.plugin" "bmx6_config.so" "$SOURCE"
	qmp_set_option "$BMX6_CONFIG.bmx6_json_plugin" "plugin" "$SOURCE"
	qmp_set_option "$BMX6_CONFIG.bmx6_json_plugin.plugin" "bmx6_json.so" "$SOURCE"

	qmp_set_option "$BMX6_CONFIG.ipVersion" "ipVersion" "$SOURCE"
	qmp_set_option "$BMX6_CONFIG.ipVersion.ipVersion" "6" "$SOURCE"
	qmp_set_option "$BMX6_CONFIG.ipVersion.throwRules" "0" "$SOURCE"
892
893


894
895
	local PRIMARY_MESH_DEV="$(qmp_get_primary_dev $QMP_CONFIG "$SOURCE")"
	local NODE_PROJ="$(qmp_node_proj $PRIMARY_MESH_DEV $BMX6_CNAME)"
Victor Oncins's avatar
Victor Oncins committed
896

897
	qmp_log_all "$SOURCE" "Primary mesh devide='$PRIMARY_MESH_DEV'"
898

Victor Oncins's avatar
Victor Oncins committed
899
	#Declare VLAN interfaces for bmx6
900
	local COUNTER=1
901

902
903
	if qmp_config_check ${QMP_CONFIG}.interfaces.mesh_devices && qmp_config_check ${QMP_CONFIG}.networks.mesh_protocol_vids
	then
904

905
906
	local VID
	local BASE_VID
907

Victor Oncins's avatar
Victor Oncins committed
908
909
		qmp_log_all "$SOURCE" "VLAN interfaces configuration" ""

910
911
912
913
914
915
		if qmp_config_check ${QMP_CONFIG}.networks.mesh_vid_offset
		then
			BASE_VID="$(uci get ${QMP_CONFIG}.networks.mesh_vid_offset)"
		else
			BASE_VID="1"
		fi
916

917
		for MESH_DEV in $(uci get ${QMP_CONFIG}.interfaces.mesh_devices); do
918

919
		VID=$BASE_VID
920

921
			for PROTOCOL_DESCR in $(uci get ${QMP_CONFIG}.networks.mesh_protocol_vids); do
922

923
924
925
926
927
928
				local PROTOCOL_NAME="$(echo $PROTOCOL_DESCR | awk -F':' '{print $1}')"
				
				if [ "$PROTOCOL_NAME" = "$BMX6_CNAME" ]
				then
				
					local VID_DESCR="$(echo $PROTOCOL_DESCR | awk -F':' '{print $2}')"
929

930
931
932
933
934
					#According to IEEE 802.1Q only VID-values between 1 and 4094 are allowed 
					if [ "$VID_DESCR" -ge "1" ] && [ "$VID_DESCR" -le "4094" ]
					then
						VID=$VID_DESCR
					fi
935

936
937
					#Beginning of interfaces configuration
					local IFNAME="$MESH_DEV.$VID"
938

939
940
					qmp_set_option "$BMX6_CONFIG.mesh_$COUNTER" "dev" "$SOURCE"
					qmp_set_option "$BMX6_CONFIG.mesh_$COUNTER.dev" "$IFNAME" "$SOURCE"
941

942
943
					if qmp_config_check ${QMP_CONFIG}.networks.bmx6_ipv4_address
					then
944
						qmp_set_option "$BMX6_CONFIG.general.niitSource" "$(uci get ${QMP_CONFIG}.networks.bmx6_ipv4_address)" "$SOURCE"
945
946
947
948
						
					elif qmp_config_check ${QMP_CONFIG}.networks.bmx6_ipv4_prefix24
					then
						local IP4_SUBNETID16="$(( 0x$NODE_PROJ / 0x100 )).$(( 0x$NODE_PROJ % 0x100 ))"
949
						qmp_set_option "$BMX6_CONFIG.general.niitSource" "$(uci get ${QMP_CONFIG}.networks.bmx6_ipv4_prefix24).$IP4_SUBNETID16" "$SOURCE"
950
951
952
						
					fi
					#End of interfaces configuration
953

954
				fi
955

956
957
				VID=$(( $VID + 1 ))
			done
958

959
960
961
			COUNTER=$(( $COUNTER + 1 ))
		done
	fi
962

963

964
965
	if qmp_config_check ${QMP_CONFIG}.node.global_prefix48
	then
966
967
		qmp_set_option "$BMX6_CONFIG.ripe" "hna" "$SOURCE"
		qmp_set_option "$BMX6_CONFIG.ripe.hna" "$(uci get ${QMP_CONFIG}.node.global_prefix48):$NODE_PROJ:0:0:0:0/64" "$SOURCE"
968

969
	fi
970

971
972
973
974
975
976
977
	if qmp_config_check ${QMP_CONFIG}.networks.niit_prefix96
	then
	
		if qmp_config_check ${QMP_CONFIG}.networks.bmx6_ipv4_address && qmp_config_check ${QMP_CONFIG}.networks.bmx6_ipv4_netmask && qmp_config_check ${QMP_CONFIG}.networks.bmx6_6to4_netmask
		then
			local NIIT6TO4_ADDR="$(qmp_get_ip6_fast $(uci get ${QMP_CONFIG}.networks.niit_prefix96):$(uci get ${QMP_CONFIG}.networks.bmx6_ipv4_address)/$(uci get ${QMP_CONFIG}.networks.bmx6_6to4_netmask))"
			qmp_set_option "$BMX6_CONFIG.niit6to4" "unicast_hna" "$SOURCE"
978
			qmp_set_option "$BMX6_CONFIG.niit6to4.hna" "$NIIT6TO4_ADDR/$(uci get ${QMP_CONFIG}.networks.bmx6_6to4_netmask)" "$SOURCE"
979

980
981
982
983
		elif qmp_config_check ${QMP_CONFIG}.networks.bmx6_ipv4_prefix24
		then
			local NIIT6TO4_ADDR="$(uci get ${QMP_CONFIG}.networks.niit_prefix96):$(uci get ${QMP_CONFIG}.networks.bmx6_ipv4_prefix24).$IP4_SUBNETID16"
			qmp_set_option "$BMX6_CONFIG.niit6to4" "unicast_hna" "$SOURCE"
984
			qmp_set_option "$BMX6_CONFIG.niit6to4.hna" "$NIIT6TO4_ADDR/128" "$SOURCE"
985

986
987
		fi
	fi
988

989
990
991
	qmp_config_commit "$BMX6_CONFIG" "$SOURCE"
#  /etc/init.d/$BMX6_CONFIG restart
}
992

993
qmp_configure_olsr6() {
994

995
	local SOURCE="qmp_configure_olsr6"
996

Victor Oncins's avatar
Victor Oncins committed
997
	qmp_log_all "$SOURCE" "$OLSR6_CNAME configuration started..." ""
998

999
	local CONF_FILE="/etc/olsrd.conf"
1000

Victor Oncins's avatar
Victor Oncins committed
1001
1002
	qmp_log_all "$SOURCE" "Creating $CONF_FILE olsr6 conf-file and setting UCI" ""

1003
	qmp_config_revert "$OLSR6_CONFIG" "$SOURCE"
1004

1005
	qmp_add_option "$OLSR6_CONFIG" "olsrd" "$SOURCE"
Victor Oncins's avatar
Victor Oncins committed
1006
	qmp_set_option "${OLSR6_CONFIG}.@olsrd[0].config_file" "$CONF_FILE" "$SOURCE"
1007

1008
	qmp_config_commit "$OLSR6_CONFIG" "$SOURCE"
1009
1010


1011
cat <<EOF > $CONF_FILE
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
DebugLevel 1
IpVersion  6
#RtTable        90
#RtTableDefault 91
#LinkQualityFishEye  0

LoadPlugin "olsrd_txtinfo.so.0.1"
{
  PlParam     "Accept"   "0::0"
}

EOF

1025
1026
	local PRIMARY_MESH_DEV="$(qmp_get_primary_dev ${QMP_CONFIG} "$SOURCE")"
	local NODE_PROJ="$(qmp_node_proj $PRIMARY_MESH_DEV $OLSR6_CNAME)"
1027

1028
1029
	#Declare VLAN interfaces for olsrd.conf
	local COUNTER=1
1030

1031
1032
	if qmp_config_check ${QMP_CONFIG}.interfaces.mesh_devices && qmp_config_check ${QMP_CONFIG}.networks.mesh_protocol_vids
	then
1033

1034
1035
	local VID
	local BASE_VID
1036

1037
1038
1039
1040
1041
1042
		if qmp_config_check ${QMP_CONFIG}.networks.mesh_vid_offset
		then
			BASE_VID="$(uci get ${QMP_CONFIG}.networks.mesh_vid_offset)"
		else
			BASE_VID="1"
		fi
1043

1044
		for MESH_DEV in $(uci get ${QMP_CONFIG}.interfaces.mesh_devices); do
1045

1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
		VID=$BASE_VID

			for PROTOCOL_DESCR in $(uci get ${QMP_CONFIG}.networks.mesh_protocol_vids); do

				local PROTOCOL_NAME="$(echo $PROTOCOL_DESCR | awk -F':' '{print $1}')"
				
				if [ "$PROTOCOL_NAME" = "$OLSR6_CNAME" ]
				then
				
					local VID_DESCR="$(echo $PROTOCOL_DESCR | awk -F':' '{print $2}')"

					#According to IEEE 802.1Q only VID-values between 1 and 4094 are allowed 
					if [ "$VID_DESCR" -ge "1" ] && [ "$VID_DESCR" -le "4094" ]
					then
						VID=$VID_DESCR
					fi
1062

1063
1064
					#Beginning of interfaces configuration
					local IFNAME="$MESH_DEV.$VID"
1065

1066
1067
					local MODE="$(if echo $MESH_DEV | grep -v ath | grep -v wlan > /dev/null 2>&1; then echo ether; else echo mesh; fi)"
					local MESH="mesh_${PROTOCOL_NAME}_${COUNTER}"
Victor Oncins's avatar
Victor Oncins committed
1068
					qmp_log_all "$SOURCE" "Selected mode '$MODE' for $MESH_DEV"
1069
					local IP6_IFACEID32=":0:$NODE_PROJ"
Victor Oncins's avatar
Victor Oncins committed
1070
					#We don't check whether ${QMP_CONFIG}.networks.${PROTOCOL_NAME}_ula_prefix48 is defined.???
1071
					local IP6_ADDR="$( qmp_get_ip6_fast $(qmp_get_ula96 $(uci get ${QMP_CONFIG}.networks.${PROTOCOL_NAME}_ula_prefix48) $MESH_DEV $IP6_IFACEID32 128) )"
1072

1073
1074
1075
cat <<EOF >> $CONF_FILE

Interface "$IFNAME"
1076
{
1077
    Mode                "$MODE"
Victor Oncins's avatar
Victor Oncins committed
1078
    IPv6Multicast       ff0e::1
1079
    IPv6Src             $IP6_ADDR
1080
1081
1082
}

EOF
1083
1084
					#End of interfaces configuration
				fi
1085

1086
1087
					VID=$(( $VID + 1 ))
			done
1088

1089
1090
1091
			COUNTER=$(( $COUNTER + 1 ))
		done
	fi
1092

1093
1094
1095
1096
1097
1098


	if qmp_config_check ${QMP_CONFIG}.node.global_prefix48
	then

cat <<EOF >> $CONF_FILE
1099
1100
Hna6
{
1101
$(uci get ${QMP_CONFIG}.node.global_prefix48):${NODE_PROJ}:0:0:0:0 64
1102
1103
1104
}

EOF
1105
	fi
1106

1107
1108
	if qmp_config_check ${QMP_CONFIG}.networks.niit_prefix96
	then
1109

1110
1111
1112
1113
1114
1115
		if qmp_config_check ${QMP_CONFIG}.networks.olsr6_ipv4_address && qmp_config_check ${QMP_CONFIG}.networks.olsr6_ipv4_netmask && qmp_config_check ${QMP_CONFIG}.networks.olsr6_6to4_netmask
		then
			local IP4_ADDR=$(uci get ${QMP_CONFIG}.networks.olsr6_ipv4_address)
			#local IP6_IFACEID96="$(echo $(ipv6calc -q --action conv6to4 --in ipv4 $IP4_ADDR) | awk -F':' '{print $2}')"
			#local IP6_IFACEID112="$(echo $(ipv6calc -q --action conv6to4 --in ipv4 $IP4_ADDR) | awk -F':' '{print $3}')"
cat <<EOF >> $CONF_FILE
1116
1117
Hna6
{
1118
$(uci get ${QMP_CONFIG}.networks.niit_prefix96):$IP6_ADDR $(uci get ${QMP_CONFIG}.networks.olsr6_6to4_netmask)
1119
1120
1121
1122
}

EOF

1123
1124
1125
1126
1127
1128
		elif qmp_config_check ${QMP_CONFIG}.networks.olsr6_ipv4_prefix24
		then
			local IP4_SUBNETID16=$(uci get ${QMP_CONFIG}.networks.olsr6_ipv4_prefix24)
			local IP6_IFACEID96="$(echo $(ipv6calc -q --action conv6to4 --in ipv4 ${IP4_SUBNETID16}.$(( 0x$NODE_PROJ / 0x100 )).$(( 0x$NODE_PROJ % 0x100 ))) | awk -F':' '{print $2}'):$(echo $(ipv6calc -q --action conv6to4 --in ipv4 ${IP4_SUBNETID16}.$(( 0x$NODE_PROJ / 0x100 )).$(( 0x$NODE_PROJ % 0x100 ))) | awk -F':' '{print $3}')"

cat <<EOF >> $CONF_FILE
1129
1130
Hna6
{
1131
$(uci get ${QMP_CONFIG}.networks.niit_prefix96):$IP6_IFACEID96 128
1132
1133
1134
}

EOF
1135
1136
1137
		fi
	fi
#  /etc/init.d/$OLSR6_CONFIG restart
1138
1139
1140
}


1141
1142
qmp_configure_olsr6_uci_unused() {

1143
  local OLSR6_UCI_CONFIG="olsrd_uci"
1144

1145
  qmp_config_revert "$OLSR6_UCI_CONFIG" "$SOURCE"
1146
1147


1148
1149
  uci set $OLSR6_UCI_CONFIG.networks="olsrd"
  uci set $OLSR6_UCI_CONFIG.networks.IpVersion="6"
1150

1151
1152
  uci set $OLSR6_UCI_CONFIG.arprefresh="LoadPlugin"
  uci set $OLSR6_UCI_CONFIG.arprefresh.library="olsrd_arprefresh.so.0.1"
1153

1154
1155
1156
1157
  uci set $OLSR6_UCI_CONFIG.httpinfo="LoadPlugin"
  uci set $OLSR6_UCI_CONFIG.httpinfo.library="olsrd_httpinfo.so.0.1"
  uci set $OLSR6_UCI_CONFIG.httpinfo.port="1978"
  uci add_list $OLSR6_UCI_CONFIG.httpinfo.Net="0::/0"
1158

1159
1160
  uci set $OLSR6_UCI_CONFIG.nameservice="LoadPlugin"
  uci set $OLSR6_UCI_CONFIG.nameservice.library="olsrd_nameservice.so.0.3"
1161

1162
1163
1164
  uci set $OLSR6_UCI_CONFIG.txtinfo="LoadPlugin"
  uci set $OLSR6_UCI_CONFIG.txtinfo.library="olsrd_txtinfo.so.0.1"
  uci set $OLSR6_UCI_CONFIG.txtinfo.accept="0::"
1165

1166
  if qmp_config_check ${QMP_CONFIG}.interfaces.mesh_devices && qmp_config_check ${QMP_CONFIG}.networks.mesh_protocol_vids && qmp_config_check ${QMP_CONFIG}.networks.mesh_vid_offset; then
1167

1168
    local COUNTER=1
1169
1170
    local interface_list=""

1171
1172
    for MESH_DEV in $(uci get ${QMP_CONFIG}.interfaces.mesh_devices); do 
       for protocol_vid in $(uci get ${QMP_CONFIG}.networks.mesh_protocol_vids); do
1173
1174
1175

         local protocol_name="$(echo $protocol_vid | awk -F':' '{print $1}')"

1176
         echo "qmp_configure_olsr6 dev=$MESH_DEV protocol_vid=$protocol_vid protocol_name=$protocol_name"
1177
1178
1179
1180

         if [ "$protocol_name" = "olsr6" ] ; then

            local vid_suffix="$(echo $protocol_vid | awk -F':' '{print $2}')"
1181
1182
1183
            local vid_offset="$(uci get ${QMP_CONFIG}.networks.mesh_vid_offset)"
	    local interface="olsr6_${COUNTER}"
	    local ifname="$MESH_DEV.$(( $vid_offset + $vid_suffix ))"
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193

            echo "adding ifname=$ifname interface=$interface"

            interface_list="$interface_list $interface"

         fi

       done
    done
 
1194
1195
    uci set $OLSR6_UCI_CONFIG.interface="Interface"
    uci add_list $OLSR6_UCI_CONFIG.interface.interface="$interface_list"
1196
1197
1198

  fi
  
1199
1200
  uci commit $OLSR6_UCI_CONFIG
#  /etc/init.d/$OLSR6_UCI_CONFIG restart