Developing lightweight computation at the DSG edge

qmp_functions.sh 30.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/sh
#/etc/rc.common
#    Copyright (C) 2011 Fundacio Privada per a la Xarxa Oberta, Lliure i Neutral guifi.net
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#    The full GNU General Public License is included in this distribution in
#    the file called "COPYING".
#
# Contributors:
#	Simó Albert i Beltran
#

QMP_PATH="/etc/qmp"
SOURCE_FUNCTIONS=1

#######################
# Importing files
######################
. $QMP_PATH/qmp_common.sh
[ -z "$SOURCE_GW" ] && . $QMP_PATH/qmp_gw.sh
[ -z "$SOURCE_NET" ] && . $QMP_PATH/qmp_network.sh

# requires ip ipv6calc awk sed grep

qmp_get_llocal_for_dev() {
  local dev=$1
40
  ip a show dev $dev | awk '/inet6/{print $2}'
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
}

# returns primary device
qmp_get_primary_device() {
  local primary_mesh_device="$(uci get qmp.node.primary_device)"
  [ -z "$primary_mesh_device" ] && 
      {
      if ip link show dev eth0 > /dev/null; then
        primary_mesh_device="eth0"
      else
        primary_mesh_device="$(ip link show | awk '!/lo:/&&/^[0-9]?:/{sub(/:$/,"",$2); print $2; exit}')"
      fi
      [ -z "$primary_mesh_device" ] && echo "CRITICAL: No primary network device found, please define qmp.node.primary_device" 
      }
  echo "$primary_mesh_device"
}

qmp_check_device() {
	ip link show $1 1> /dev/null 2>/dev/null
	return $?
}

qmp_set_vlan() {
  local viface="$1" # lan/wan/meshX
  local vid=$2 # 11/12
  [ -z "$viface" ] || [ -z "$vid" ] && return

  uci set network.${viface}_$vid=interface
  uci set network.${viface}_$vid.proto=none
  uci set network.${viface}_$vid.ifname=@$viface.$vid
  uci commit network
}

qmp_get_virtual_iface() {
  local device="$1"
  local viface=""

78
79
80
81
82
  if [ "$device" == "eth1" ] && qmp_is_routerstationpro ; then
    echo "rsp_e1"
    return
  fi

83
84
85
86
87
88
89
90
91
92
93
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
  # is lan?
  if [ "$device" == "br-lan" ]; then
    viface="lan"
    echo $viface
    return
  fi

  for l in $(qmp_get_devices lan); do 
    if [ "$l" == "$device" ]; then 
      viface="lan"
      echo $viface
      return
    fi
  done

  [ -n "$viface" ] && { echo $viface; return; }

  # id is the first and char and the numbers of the device [e]th[0] [w]lan[1]
  local id_num=$(echo $device | tr -d "[A-z]")
  local id_char=$(echo $device | cut -c 1)

  # is wan
  for w in $(qmp_get_devices wan); do 
   if [ "$w" == "$device" ]; then
     viface="wan_${id_char}${id_num}"
     echo $viface
     return
   fi
  done

  # is mesh
  for w in $(qmp_get_devices mesh); do 
   if [ "$w" == "$device" ]; then
     viface="mesh_${id_char}${id_num}"
     break
   fi
  done
  
  echo "$viface" 
}

# arg1=<mesh|lan|wan>, returns the devices which have to be configured in such mode
qmp_get_devices() {
  local devices=""

  if [ "$1" == "mesh" ]; then 
    local brlan_enabled=0
    for dev in $(uci get qmp.interfaces.mesh_devices 2>/dev/null); do

        # Lookging if device is defined as LAN, in such case dev=br-lan, but only once
133
134
135
136
137
138
139
140
141
142
        # except eth1 for RouterStation Pro
        if ! ( [[ "$dev" == "eth1" ]] && qmp_is_routerstationpro ) ; then
            for landev in $(uci get qmp.interfaces.lan_devices 2>/dev/null); do
                if [ "$landev" == "$dev" ]; then
                    if [ $brlan_enabled -eq 0 ]; then
                        dev="br-lan"
                        brlan_enabled=1
                    else
                        dev=""
                    fi
143
                    break
144
                fi
145
146
            done
        fi
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

      [ -n "$dev" ] && devices="$devices $dev"
    done
  fi

  if [ "$1" == "lan" ]; then
     devices="$(uci get qmp.interfaces.lan_devices 2>/dev/null)"
  fi

  if [ "$1" == "wan" ]; then
     devices="$(uci get qmp.interfaces.wan_devices 2>/dev/null)"
  fi

  echo "$devices"
}


qmp_get_rescue_ip() {
	local device=$1
	[ -z "$device" ] && return 1

	local rprefix=$(uci get qmp.networks.rescue_prefix24 2>/dev/null)
	[ -z "$rprefix" ] && return 0

	# if device is virtual, get the ifname
	if qmp_uci_test network.$device.ifname; then
	  device="$(uci get network.$device.ifname | tr -d @)"
	fi

	local mac=$(ip addr show dev $device | grep -m 1 "link/ether" | awk '{print $2}')
	[ -z "$mac" ] && return 2
	
	#local xoctet=$(printf "%d\n" 0x$(echo $mac | cut -d: -f5))
	local yoctet=$(printf "%d\n" 0x$(echo $mac | cut -d: -f6))
	local rip="$rprefix.$yoctet.1"

	echo "$rip"
}

qmp_attach_device_to_interface() {
	local device=$1
	local conf=$2
	local interface=$3
190
191
	local intype="$(uci -qX get network.$interface.type)"
	
192
193
194
195
196
197
	echo "Attaching device $device to interface $interface"
	local wifi_config="$(uci -qX show wireless | sed -n -e "s/wireless\.\([^\.]\+\)\.device=$device/\1/p")"
	if [ -n "$wifi_config" -a "wifi-iface" = "$(uci -q get wireless.$wifi_config)" ] ; then
		uci set wireless.$wifi_config.network="$interface"
		uci commit wireless
	else
198
199
200
201
202
		if [ "$intype" == "bridge" ]; then
			uci add_list $conf.$interface.ifname="$device"
		else
			uci set $conf.$interface.ifname="$device"
		fi
203
204
205
206
207
208
	fi
	
}

qmp_configure_rescue_ip() {
	local device=$1
209
		
210
211
212
213
	[ -z "$device" ] && return 1
	
	local rip="$(qmp_get_rescue_ip $device)"
	[ -z "$rip" ] && { echo "Cannot get rescue IP for device $device"; return 1; }
214
215

	local viface="${2:-$(qmp_get_virtual_iface $device)}"
216
	
217
	echo "Rescue IP for device $device/$viface is $rip"	
218
219
220
221
222
223
224
225
226
227
228

	local conf="network"
	
	uci set $conf.${viface}="interface"
	#qmp_attach_viface_to_interface $viface $conf ${viface}
	uci set $conf.${viface}.proto="static"
	uci set $conf.${viface}.ipaddr="$rip"
	uci set $conf.${viface}.netmask="255.255.255.248"
	uci commit $conf
}

229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
qmp_is_routerstationpro() {
	cat /proc/cpuinfo | grep -q "^machine[[:space:]]*: Ubiquiti RouterStation Pro$"
}

qmp_configure_routerstationpro_switch() {
	local vids="$@"

	uci set network.eth1="switch"
	uci set network.eth1.enable="1"
	uci set network.eth1.enable_vlan="1"
	uci set network.eth1.reset="1"

	uci set network.mesh_ports_vid1="switch_vlan"
	uci set network.mesh_ports_vid1.vlan="1"
	uci set network.mesh_prots_vid1.vid="1"
	uci set network.mesh_ports_vid1.device="eth1"
	uci set network.mesh_ports_vid1.ports="0t 4"
      
	for vid in $vids
	do
		uci set network.mesh_ports_vid$vid="switch_vlan"
		uci set network.mesh_ports_vid$vid.vlan="$vid"
		uci set network.mesh_ports_vid$vid.vid="$vid"
		uci set network.mesh_ports_vid$vid.device="eth1"
		uci set network.mesh_ports_vid$vid.ports="0t 2t 3t"
	done
255
256
257
258
259
260

	local viface="$(qmp_get_virtual_iface eth1)"
	uci set network.$viface="interface"
	uci set network.$viface.proto="static"
	uci set network.$viface.ifname="eth1"
	uci commit network
261
262
}

263
264
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
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}')"

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

  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

      addr16=$( printf "%X" $(( $(( $ip1 * 0x100 )) + $ip2 )) )

      if [ -z "$ip4" ] ; then
        echo "Invalid 3 IPv6 address $addr_prefix" 1>&2
        return 1
      fi

      addr_in=$( printf "%X" $(( $(( $ip3 * 0x100 )) + $ip4 )) )$addr_in


    fi

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

      if [ "$(( $pos + 16 ))" -le "$mask" ] ; then
	prefix16=$addr16
      else
	prefix16=$( printf "%X" $(( 0x$addr16 & 0xFFFF<<$(( $(( $pos + 16 )) - $mask )) )) )
      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"
}







qmp_get_ip6_fast() {

  if ! [ -x /usr/bin/ipv6calc ] ; then
     qmp_get_ip6_slow $1
     return $?
  fi

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

  if [ -z "$mask" ] ; then
    echo "qmp_get_ip6_fast: ERROR addr_prefix=$addr_prefix addr_long=$addr_long  addr=$fake_long mask=$mask" 1>&2
    return 1
    mask="128"
  fi

419
  local addr_long=$( ipv6calc -q  --in ipv6 $addr --showinfo -m 2>&1 | awk -F'=' '/IPV6=/{print $2}' )
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529

  local fake_prefix16="20a2" # original input is manipulated because ipv6calc complains about reserved ipv6 addresses
  local addr_prefix16="$(echo $addr_long | awk -F':' '{print $1}')"
  local fake_long=$( echo $addr_long | sed -e "s/^$addr_prefix16/$fake_prefix16/g" )
  local fake_out

#  echo "qmp_get_ip6_fast: begin addr_prefix=$addr_prefix addr_long=$addr_long  addr=$fake_long mask=$mask" 1>&2

  if [ "$mask" -ge "0" ] &&  [ "$mask" -le "112" ] && [ "$(( $mask % 16))" = "0" ]; then

    fake_out="$( ipv6calc --in ipv6 $fake_long/$mask -F --printprefix --out ipv6addr 2>/dev/null )::/$mask"

  else

    if [ "$(( $mask % 16))" != "0" ]; then
      echo "ERROR addr_prefix=$1 mask=$mask must be multiple of 16" 1>&2
      return 1
    fi

    fake_out="$( ipv6calc --in ipv6 $fake_long/128 -F --printprefix --out ipv6addr 2>/dev/null )"
  fi

  echo $fake_out | sed -e "s/^$fake_prefix16/$addr_prefix16/g"

#  echo "qmp_get_ip6_fast: return addr_prefix=$addr_prefix addr_long=$addr_long  addr=$fake_long mask=$mask" 1>&2
}

qmp_calculate_ula96() {

  local prefix=$1
  local mac=$2
  local suffix=$3

  local prefix48=$( qmp_get_ip6_fast $prefix/128 )
  local suffix48=$( qmp_get_ip6_fast $suffix/128 )

# echo "qmp_calculate_ula96 suffix48=$suffix48" 1>&2

  local mac1="$( echo $mac | awk -F':' '{print $1}' )"
  local mac2="$( echo $mac | awk -F':' '{print $2}' )"
  local mac3="$( echo $mac | awk -F':' '{print $3}' )"
  local mac4="$( echo $mac | awk -F':' '{print $4}' )"
  local mac5="$( echo $mac | awk -F':' '{print $5}' )"
  local mac6="$( echo $mac | awk -F':' '{print $6}' )"

  local p1="$( echo $prefix48 | awk -F':' '{print $1}' )"
  local p2="$( echo $prefix48 | awk -F':' '{print $2}' )"
  local p3="$( echo $prefix48 | awk -F':' '{print $3}' )"

  local s1="$( echo $suffix48 | awk -F':' '{print $7}' )"
  local s2="$( echo $suffix48 | awk -F':' '{print $8}' )"

  printf "%X:%X:%X:%X:%X:%X:%X:%X\n" 0x$p1 0x$p2 0x$p3  $(( ( 0x$mac1 * 0x100 ) + 0x$mac2 ))  $(( ( 0x$mac3 * 0x100 ) + 0x$mac4 ))  $(( ( 0x$mac5 * 0x100 ) + 0x$mac6 ))  0x$s1 0x$s2

}



qmp_calculate_addr64() {

  local prefix=$1
  local node=$2
  local suffix=$3

  local prefix48=$( qmp_get_ip6_fast $prefix/128 )
  local suffix48=$( qmp_get_ip6_fast $suffix/128 )

  local p1="$( echo $prefix48 | awk -F':' '{print $1}' )"
  local p2="$( echo $prefix48 | awk -F':' '{print $2}' )"
  local p3="$( echo $prefix48 | awk -F':' '{print $3}' )"

  local s5="$( echo $suffix48 | awk -F':' '{print $5}' )"
  local s6="$( echo $suffix48 | awk -F':' '{print $6}' )"
  local s7="$( echo $suffix48 | awk -F':' '{print $7}' )"
  local s8="$( echo $suffix48 | awk -F':' '{print $8}' )"

  printf "%X:%X:%X:%X:%X:%X:%X:%X\n" 0x$p1 0x$p2 0x$p3   0x$node   0x$s5 0x$s6 0x$s7 0x$s8

}

qmp_get_ula96() {

  local prefix=$1
  local dev_mac=$2
  local suffix=$3
  local mask=$4

  local mac=$( qmp_get_mac_for_dev $dev_mac )
  local ula96=$( qmp_calculate_ula96 $prefix $mac $suffix )

  if [ -z "$mask" ] ; then
      echo "$ula96"
  else
      echo "$ula96/$mask"
  fi
}

qmp_get_addr64() {

  local prefix=$1
  local node=$2
  local suffix=$3
  local mask=$4

  local addr64=$( qmp_calculate_addr64 $prefix $node $suffix )

  echo "$addr64/$mask"
}


530
531
532
533
534
535
536
537
538
539
540
541
542
543
qmp_is_in()
{
	local search="$1"
	shift
	local item
	for item in $@
	do
		if [ "$search" == "$item" ]
		then
			return 0
		fi
	done
	return 1
}
544

545
546
547
548
549
qmp_configure_rescue_ip_device()
{
	local dev="$1"
	local conf="$2"
	local viface="$3"
550

551
	# Configuring rescue IPs
552
553
554
555
556
557
558
559
560
	if [ "$dev" == "eth1" ] && qmp_is_routerstationpro
	then
		if qmp_is_in "eth1" $(qmp_get_devices lan)
		then
			return
		fi
		qmp_configure_rescue_ip eth1 ${viface}_1_rescue
		qmp_attach_device_to_interface @$viface.1 $conf ${viface}_1_rescue
	elif qmp_is_in "$dev" $(qmp_get_devices wan) || [ "$dev" == "br-lan" ]
561
	then
562
563
564
		# If it is WAN or LAN
		qmp_configure_rescue_ip $dev ${viface}_rescue
		qmp_attach_device_to_interface $dev $conf ${viface}_rescue
565
566
	elif qmp_is_in "$dev" $(qmp_get_devices mesh) && [ "$dev" != "br-lan" ]
	then
567
568
569
		# If it is only mesh device
		qmp_configure_rescue_ip $dev
		qmp_attach_device_to_interface $dev $conf $viface
570
	fi
571
}
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594

qmp_configure_prepare() {

  local conf=$1

  echo "configuring /etc/config/$conf"

  if ! [ -f /etc/config/$conf.orig ]; then
    echo "saving original config in: /etc/config/$conf.orig"
    cp /etc/config/$conf /etc/config/$conf.orig
  fi

  uci revert $conf
  echo "" > /etc/config/$conf

}

qmp_configure_network() {

  local conf="network"

  qmp_configure_prepare $conf

595
596
597
598
599
600
601
602
603
  if qmp_uci_test qmp.interfaces.mesh_devices && qmp_uci_test qmp.networks.mesh_protocol_vids && qmp_uci_test qmp.networks.mesh_vid_offset; then
    local vids="$(uci -q get qmp.networks.mesh_protocol_vids | awk -F':' -v RS=" " '{print $2 + '$(uci -q get qmp.networks.mesh_vid_offset)'}')"
  fi

  if [[ -n "$vids" ]] && qmp_is_routerstationpro ; then
    
    qmp_configure_routerstationpro_switch "$vids"

  elif qmp_uci_test qmp.interfaces.configure_switch ; then
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624

    local switch_device="$(uci get qmp.interfaces.configure_switch)"

    uci set $conf.$switch_device="switch"
    uci set $conf.$switch_device.enable="1"
    uci set $conf.$switch_device.enable_vlan="1"
    uci set $conf.$switch_device.reset="1"

    uci set $conf.lan_ports="switch_vlan"
    uci set $conf.lan_ports.vlan="1"
    uci set $conf.lan_ports.device="$switch_device"
    uci set $conf.lan_ports.ports="0 1 5t"

    uci set $conf.wan_ports="switch_vlan"
    uci set $conf.wan_ports.vlan="2"
    uci set $conf.wan_ports.device="$switch_device"
    uci set $conf.wan_ports.ports="4 5t"




625
    if [[ -n "$vids" ]] ; then
626

627
       for vid in $vids; do
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650

         local mesh_ports="mesh_ports_vid${vid}"

	  uci set $conf.$mesh_ports="switch_vlan"
	  uci set $conf.$mesh_ports.vlan="$vid"
	  uci set $conf.$mesh_ports.device="$switch_device"
	  uci set $conf.$mesh_ports.ports="2t 3t 5t"

       done
    fi


  fi

  uci set $conf.loopback="interface"
  uci set $conf.loopback.ifname="lo"
  uci set $conf.loopback.proto="static"
  uci set $conf.loopback.ipaddr="127.0.0.1"
  uci set $conf.loopback.netmask="255.0.0.0"

  # WAN devices
  for i in $(qmp_get_devices wan) ; do
    local viface="$(qmp_get_virtual_iface $i)"
651
652
653
654
    if [ "$i" == "eth1" ] && qmp_is_routerstationpro ; then
      i="@$viface.1"
      viface="${viface}_1"
    fi
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
    uci set $conf.$viface="interface"
    qmp_attach_device_to_interface $i $conf $viface
    uci set $conf.$viface.proto="dhcp"
  done
  

  local primary_mesh_device="$(qmp_get_primary_device)"
  local community_node_id
  local LSB_PRIM_MAC="$(qmp_get_mac_for_dev $primary_mesh_device | awk -F':' '{print $6}' )"

  if qmp_uci_test qmp.node.community_node_id; then
    community_node_id="$(uci get qmp.node.community_node_id)"
  elif ! [ -z "$primary_mesh_device" ] ; then
    community_node_id=$LSB_PRIM_MAC
  fi

  if qmp_uci_test qmp.interfaces.lan_devices ; then

    # If it is enabled, apply the non-overlapping DHCP-range preset policy

	LAN_MASK="$(uci get qmp.networks.lan_netmask)"
	LAN_ADDR="$(uci get qmp.networks.lan_address)"
	START=2
	LIMIT=253

    if [ $(uci get qmp.non_overlapping.ignore) -eq 0 ]; then
	LAN_MASK="255.255.0.0"
	# Last byte of lan adress must be "1" to avoid overlappings
683
	LAN_ADDR=$(echo $LAN_ADDR | sed -e 's/\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)\.[0-9]\{1,3\}/\1.1/1')
684
685
686
687
688
689
690
691

	OFFSET=0
	NUM_GRP=256

	UCI_OFFSET="$(uci get qmp.non_overlapping.dhcp_offset)"

	[ $UCI_OFFSET -lt $NUM_GRP ] && OFFSET=$UCI_OFFSET

692
	START=$(( 0x$community_node_id * $NUM_GRP + $OFFSET ))
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
	LIMIT=$(( $NUM_GRP - $OFFSET ))

	uci set dhcp.lan.leasetime="$(uci get qmp.non_overlapping.qmp_leasetime)"
    fi

    uci set dhcp.lan.start=$START
    uci set dhcp.lan.limit=$LIMIT

    local disable_dhcp=0
    if qmp_uci_test qmp.networks.disable_lan_dhcp; then
      disable_dhcp="$(uci get qmp.networks.disable_lan_dhcp)"
    fi

    if [ "$disable_dhcp" != "1" ]; then 
      uci set dhcp.lan.ignore="0"
    else
      uci set dhcp.lan.ignore="1"
    fi

    # LAN device
    uci set $conf.lan="interface"
714
    uci set $conf.lan.type="bridge"
715
716
    local device
    for device in $(qmp_get_devices lan) ; do
717
718
719
      if [ "$device" == "eth1" ] && qmp_is_routerstationpro ; then
        device="@$(qmp_get_virtual_iface eth1).1"
      fi
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
      qmp_attach_device_to_interface $device $conf lan
    done
    uci set $conf.lan.proto="static"
    uci set $conf.lan.ipaddr=$LAN_ADDR
    uci set $conf.lan.netmask=$LAN_MASK
    uci set $conf.lan.dns="$(uci get qmp.networks.dns)"

  fi

  # MESH DEVICES CONFIGURATION
  local counter=1

  if uci get qmp.interfaces.mesh_devices && uci get qmp.networks.mesh_protocol_vids && uci get qmp.networks.mesh_vid_offset; then
    for dev in $(qmp_get_devices mesh); do

        # If dev is empty, nothing to do
        [ -z "$dev" ] || ! qmp_check_device $dev && continue

        # Let's configure the mesh device
	echo "Configuring "$dev" for Meshing"

	# Check if the current device is configured as no-vlan
	local use_vlan=1
	for no_vlan_int in $(uci get qmp.interfaces.no_vlan_devices 2>/dev/null); do
		[ "$no_vlan_int" == "$dev" ] && use_vlan=0
	done

        local protocol_vids="$(uci get qmp.networks.mesh_protocol_vids 2>/dev/null)"
        [ -z "$protocol_vids" ] && protocol_vids="olsr6:1 bmx6:2"

	for protocol_vid in $protocol_vids; do

	 # Calculating the VID offset for VLAN tag
         local protocol_name="$(echo $protocol_vid | awk -F':' '{print $1}')"
         local vid_suffix="$(echo $protocol_vid | awk -F':' '{print $2}')"
         local vid_offset="$(uci get qmp.networks.mesh_vid_offset)"
         local vid="$(( $vid_offset + $vid_suffix ))"
        
	 # virtual interface
         local viface=$(qmp_get_virtual_iface $dev)

         # put typical IPv6 prefix (2002::), otherwise ipv6 calc assumes mapped or embedded ipv4 address
         local ip6_suffix="2002::${counter}${vid_suffix}" 
	
	 # Since all interfaces are defined somewhere (LAN, WAN or with Rescue IP), 
	 # in case of not use vlan tag, device definition is not needed.
	 # However for the moment only bmx6 support not-vlan interfaces
	 if [ "$protocol_name" != "bmx6" ] || [ $use_vlan -eq 1 ]; then
             qmp_set_vlan $viface $vid
         fi
         
	 # Configure IPv6 address only if mesh_prefix48 is defined (bmx6 does not need it)
	 if qmp_uci_test qmp.networks.${protocol_name}_mesh_prefix48; then
             local ip6="$(qmp_get_ula96 $(uci get qmp.networks.${protocol_name}_mesh_prefix48):: $primary_mesh_device $ip6_suffix 128)"
             echo "Configuring $ip6 for $protocol_name"
             uci set $conf.${viface}_$vid.proto=static
             uci set $conf.${viface}_$vid.ip6addr="$ip6"
         else
             uci set network.${viface}_$vid.proto=none
             uci set network.${viface}_$vid.auto=1
	 fi

       done

784
785
       qmp_configure_rescue_ip_device "$dev" "$conf" "$viface"

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
821
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
       counter=$(( $counter + 1 ))
    done
  fi

  uci commit
}



qmp_configure_bmx6() {
#  set -x
  local conf="bmx6"

  qmp_configure_prepare $conf

  uci set $conf.general="bmx6"
#  uci set $conf.general.globalPrefix="$(uci get qmp.networks.bmx6_mesh_prefix48)::/48"
#  uci set $conf.general.udpDataSize=1000

  uci set $conf.bmx6_config_plugin=plugin
  uci set $conf.bmx6_config_plugin.plugin=bmx6_config.so

  uci set $conf.bmx6_json_plugin=plugin
  uci set $conf.bmx6_json_plugin.plugin=bmx6_json.so

  uci set $conf.bmx6_sms_plugin=plugin
  uci set $conf.bmx6_sms_plugin.plugin=bmx6_sms.so

  # chat file must be syncronized using sms
  cfg_sms=$(uci add $conf syncSms)
  uci set $conf.${cfg_sms}.syncSms=chat

  uci set $conf.ipVersion=ipVersion
  uci set $conf.ipVersion.ipVersion="6"
  if value="$(uci get qmp.networks.bmx6_throwRules)" ; then
    uci set $conf.ipVersion.throwRules="$value"
  fi
  if value="$(uci get qmp.networks.bmx6_tablePrefTuns)"; then
    uci set $conf.ipVersion.tablePrefTuns="$value"
  fi
  if value=$(uci get qmp.networks.bmx6_tableTuns); then
    uci set $conf.ipVersion.tableTuns="$value"
  fi


  local primary_mesh_device="$(qmp_get_primary_device)"

  local community_node_id
  if qmp_uci_test qmp.node.community_node_id; then
    community_node_id="$(uci get qmp.node.community_node_id)"
  elif ! [ -z "$primary_mesh_device" ] ; then
    community_node_id="$( qmp_get_mac_for_dev $primary_mesh_device | awk -F':' '{print $6}' )"
  fi

  if qmp_uci_test qmp.interfaces.mesh_devices && qmp_uci_test qmp.networks.mesh_protocol_vids && qmp_uci_test qmp.networks.mesh_vid_offset; then

    local counter=1

    for dev in $(qmp_get_devices mesh); do
       for protocol_vid in $(uci get qmp.networks.mesh_protocol_vids); do

         local protocol_name="$(echo $protocol_vid | awk -F':' '{print $1}')"
	
         if [ "$protocol_name" = "bmx6" ] ; then
	    
	    # Check if the current device is configured as no-vlan
	    local use_vlan=1
	    for no_vlan_int in $(uci get qmp.interfaces.no_vlan_devices); do
		[ "$no_vlan_int" == "$dev" ] && use_vlan=0
	    done
		
	    if [ $use_vlan -eq 1 ]; then
	        # If vlan tagging
                local vid_suffix="$(echo $protocol_vid | awk -F':' '{print $2}')"
                local vid_offset="$(uci get qmp.networks.mesh_vid_offset)"
	        local ifname="$dev.$(( $vid_offset + $vid_suffix ))"
	    else
	        # If not vlan tagging
		local ifname="$dev"
	    fi

	    uci set $conf.mesh_$counter="dev"
	    uci set $conf.mesh_$counter.dev="$ifname"

	    if qmp_uci_test qmp.networks.bmx6_ipv4_address ; then
	      local bmx6_ipv4_netmask="$(echo $(uci get qmp.networks.bmx6_ipv4_address) | cut -s -d / -f2)"
	      local bmx6_ipv4_address="$(echo $(uci get qmp.networks.bmx6_ipv4_address) | cut -d / -f1)"
	      [ -z "$bmx6_ipv4_netmask" ] && bmx6_ipv4_netmask="32"
	      uci set $conf.general.tun4Address="$bmx6_ipv4_address/$bmx6_ipv4_netmask"

	    elif qmp_uci_test qmp.networks.bmx6_ipv4_prefix24 ; then
877
	      local ipv4_suffix24="$(( 0x$community_node_id % 0x100 ))"
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
	      uci set $conf.general.tun4Address="$(uci get qmp.networks.bmx6_ipv4_prefix24).$ipv4_suffix24/32"
	    fi

	    counter=$(( $counter + 1 ))
         fi

       done
    done
  fi


  if qmp_uci_test qmp.networks.bmx6_ripe_prefix48 ; then
    uci set $conf.general.tun6Address="$(uci get qmp.networks.bmx6_ripe_prefix48):$community_node_id:0:0:0:1/64"
  fi


  if qmp_uci_test qmp.tunnels.search_ipv6_tunnel ; then
    uci set $conf.tun6Out="tunOut"
    uci set $conf.tun6Out.tunOut="tun6Out"
    uci set $conf.tun6Out.network="$(uci get qmp.tunnels.search_ipv6_tunnel)"
    uci set $conf.tun6Out.mtu=1360
  fi

  if qmp_uci_test qmp.tunnels.search_ipv4_tunnel ; then
    uci set $conf.tun4Out="tunOut"
    uci set $conf.tun4Out.tunOut="tun4Out"
    uci set $conf.tun4Out.network="$(uci get qmp.tunnels.search_ipv4_tunnel)"
    uci set $conf.tun4Out.mtu=1360

  elif qmp_uci_test qmp.tunnels.offer_ipv4_tunnel ; then
    uci set $conf.tunInRemote="tunInRemote"
    uci set $conf.tunInRemote.tunInRemote="$(qmp_get_ula96 $(uci get qmp.networks.bmx6_mesh_prefix48):: $primary_mesh_device 2002::ffff )"

    uci set $conf.tun4InNet="tunInNet"
    uci set $conf.tun4InNet.tunInNet="$(uci get qmp.tunnels.offer_ipv4_tunnel)"
    uci set $conf.tun4InNet.bandwidth="1000000"
  fi

  #Configuring the tunnel to search 10/8 networks
  uci set $conf.nodes10="tunOut"
  uci set $conf.nodes10.tunOut="nodes10"
  uci set $conf.nodes10.network="10.0.0.0/8"
  uci set $conf.nodes10.mtu=1360

  uci commit $conf
#  /etc/init.d/$conf restart
}

qmp_configure_olsr6() {

  local conf="olsrd"
  local file="/etc/olsrd.conf"

  qmp_configure_prepare $conf

  uci add $conf "olsrd"
  uci set $conf.@olsrd[0].config_file="/etc/olsrd.conf"

  uci commit $conf

cat <<EOF > $file
DebugLevel 1
IpVersion  6
#RtTable        90
#RtTableDefault 91
#LinkQualityFishEye  0

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

EOF

  local primary_mesh_device="$(qmp_get_primary_device)"
  
  local community_node_id
  if qmp_uci_test qmp.node.community_node_id; then
    community_node_id="$(uci get qmp.node.community_node_id)"
  elif ! [ -z "$primary_mesh_device" ] ; then
    community_node_id="$( qmp_get_mac_for_dev $primary_mesh_device | awk -F':' '{print $6}' )"
  fi


  if qmp_uci_test qmp.interfaces.mesh_devices && qmp_uci_test qmp.networks.mesh_protocol_vids && qmp_uci_test qmp.networks.mesh_vid_offset; then

    local counter=1

    for dev in $(qmp_get_devices mesh); do
       for protocol_vid in $(uci get qmp.networks.mesh_protocol_vids); do

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

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

           local vid_suffix="$(echo $protocol_vid | awk -F':' '{print $2}')"
           local vid_offset="$(uci get qmp.networks.mesh_vid_offset)"
	   local ifname="$dev.$(( $vid_offset + $vid_suffix ))"
976
           local mode="$(if echo $dev | grep -v "\(ath\|wlan\)" > /dev/null 2>&1; then echo ether; else echo mesh; fi)"
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
           local mesh="mesh_${protocol_name}_${counter}"
           local ip6_suffix="2002::${counter}${vid_suffix}" #put typical IPv6 prefix (2002::), otherwise ipv6 calc assumes mapped or embedded ipv4 address
           local ip6_addr="$( qmp_get_ip6_fast $(qmp_get_ula96 $(uci get qmp.networks.${protocol_name}_mesh_prefix48):: $primary_mesh_device $ip6_suffix 128) )"

cat <<EOF >> $file

Interface "$ifname"
{
    Mode                "$mode"
    IPv6Multicast       FF0E::1
    IPv6Src             $ip6_addr
}

EOF
	    counter=$(( $counter + 1 ))
         fi
       done
    done
  fi



  if qmp_uci_test qmp.networks.olsr6_ripe_prefix48 ; then
cat <<EOF >> $file
Hna6
{
$(uci get qmp.networks.olsr6_ripe_prefix48):$community_node_id:0:0:0:0 64
}

EOF
  fi

  if qmp_uci_test qmp.networks.niit_prefix96 ; then

    if qmp_uci_test qmp.networks.olsr6_ipv4_address && qmp_uci_test qmp.networks.olsr6_ipv4_netmask && qmp_uci_test qmp.networks.olsr6_6to4_netmask; then
cat <<EOF >> $file
Hna6
{
$( qmp_get_ip6_slow $(uci get qmp.networks.niit_prefix96):$(uci get qmp.networks.olsr6_ipv4_address)/$(uci get qmp.networks.olsr6_6to4_netmask)) $(uci get qmp.networks.olsr6_6to4_netmask)
}

EOF

    elif qmp_uci_test qmp.networks.olsr6_ipv4_prefix24; then
      local prefix24=$(uci get qmp.networks.olsr6_ipv4_prefix24)
cat <<EOF >> $file
Hna6
{
$( qmp_get_ip6_slow $(uci get qmp.networks.niit_prefix96):$(uci get qmp.networks.olsr6_ipv4_prefix24).$(( 0x$community_node_id / 0x100 )).$(( 0x$community_node_id % 0x100 ))/128 ) 128
}

EOF
    fi
  fi
#  /etc/init.d/$conf restart
}


qmp_set_hosts() {
  echo "Configuring /etc/hosts file with qmpadmin entry"

  local ip=$(uci get bmx6.general.tun4Address | cut -d'/' -f1)
  local hn=$(uci get system.@system[0].hostname)

  if [ -z "$ip" -o -z "$hn" ]; then
 	echo "Cannot get IP or HostName"
	return
  fi

1046
  if [ $(cat /etc/hosts | grep -c "^$ip.*qmpadmin") -eq 0 ]; then
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
        cat /etc/hosts | grep -v qmpadmin > /tmp/hosts.tmp
        echo "$ip $hn admin.qmp qmpadmin" >> /tmp/hosts.tmp
        cp /tmp/hosts.tmp /etc/hosts
  fi

  echo "done"
}

qmp_configure_system() {

  local primary_mesh_device="$(qmp_get_primary_device)"

  local community_node_id
  if qmp_uci_test qmp.node.community_node_id; then
    community_node_id="$(uci get qmp.node.community_node_id)"
  else
    community_node_id="$(qmp_get_mac_for_dev $primary_mesh_device | awk -F':' '{print $6}' )"
  fi

  local community_id="$(uci get qmp.node.community_id)"
  [ -z "$community_id" ] && community_id="qmp"

  # set hostname
  uci set system.@system[0].hostname=${community_id}${community_node_id}
  uci commit system
  echo "${community_id}${community_node_id}" > /proc/sys/kernel/hostname

  # enable IPv6 in httpd:
  uci set uhttpd.main.listen_http="80"
  uci set uhttpd.main.listen_https="443"
  uci commit uhttpd
  /etc/init.d/uhttpd restart

  # configuring hosts
  qmp_set_hosts
}


qmp_check_force_internet() {
	[ "$(uci get qmp.networks.force_internet)" == "1" ] && qmp_gw_offer_default
	[ "$(uci get qmp.networks.force_internet)" == "0" ] && qmp_gw_search_default
}

qmp_configure() {
  qmp_check_force_internet
  qmp_configure_network
  qmp_configure_bmx6
  qmp_configure_olsr6
  qmp_configure_lan_v6
  qmp_configure_system
}