Developing lightweight computation at the DSG edge

qmp_functions.sh 21.8 KB
Newer Older
1
#!/bin/sh
2
# requires ip ipv6calc awk sed grep
3
4
5
6
7
8
QMP_PATH="/etc/qmp"
SOURCE_FUNCTIONS=1

#######################
# Importing files
######################
9
10
11
12
13
if [ -z "$SOURCE_OPENWRT_FUNCTIONS" ]
then
	. /lib/functions.sh
	SOURCE_OPENWRT_FUNCTIONS=1
fi
14
15
16
. $QMP_PATH/qmp_common.sh
[ -z "$SOURCE_GW" ] && . $QMP_PATH/qmp_gw.sh
[ -z "$SOURCE_NET" ] && . $QMP_PATH/qmp_network.sh
17
[ -z "$SOURCE_SYS" ] && . $QMP_PATH/qmp_system.sh
18
[ -z "$SOURCE_WIRELESS" ] && . $QMP_PATH/qmp_wireless.sh
19
[ -z "$SOURCE_COMMON" ] && . $QMP_PATH/qmp_common.sh
20
21
22

qmp_get_llocal_for_dev() {
  local dev=$1
23
  ip a show dev $dev | awk '/inet6/{print $2}'
24
25
26
27
28
}

# returns primary device
qmp_get_primary_device() {
  local primary_mesh_device="$(uci get qmp.node.primary_device)"
29
  [ -z "$primary_mesh_device" ] &&
30
31
32
33
34
35
      {
      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
36
      [ -z "$primary_mesh_device" ] && echo "CRITICAL: No primary network device found, please define qmp.node.primary_device"
37
38
39
40
      }
  echo "$primary_mesh_device"
}

41
# check if a device exists
42
43
44
45
46
47
48
qmp_check_device() {
	ip link show $1 1> /dev/null 2>/dev/null
	return $?
}

qmp_set_vlan() {
  local viface="$1" # lan/wan/meshX
49
  local vid=$2
50
51
52
53
54
55
56
57
58
59
60
61
  [ -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=""

62
63
64
65
66
67
68
69
70
  if qmp_is_routerstationpro; then
    if [ "$device" == "eth1" ]; then
      echo "rsp_e1"
      return
    fi
    if [ "$device" == "eth1.1" ]; then
      echo "rsp_e1_1"
      return
    fi
71
72
  fi

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
	# is lan?
	if [ "$device" == "br-lan" ]; then
		viface="lan"
		if [ ! -e "/sys/class/net/$device/phy80211" ]; then
			echo $viface
			return
		fi
  	fi

	for l in $(qmp_get_devices lan); do
		if [ "$l" == "$device" ]; then
			viface="lan"
			if [ ! -e "/sys/class/net/$device/phy80211" ]; then
				echo $viface
				return
			fi
		fi
	done
91

92
	[ ! -e "/sys/class/net/$device/phy80211" ] && [ -n "$viface" ] && { echo $viface; return; }
93

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

98
99
100
101
102
103
104
105
	# 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
106

107
108
	qmp_log "LOG: 5"
		qmp_log "Viface: $viface"
109

110
111
112
113
114
115
116
	# is mesh?
	for w in $(qmp_get_devices mesh); do
		if [ "$w" == "$device" ]; then
			viface="mesh_${id_char}${id_num}"
			break
		fi
	done
117

118
	echo "$viface"
119
120
121
122
123
124
}

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

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

129
        # Looking if device is defined as LAN, in such case dev=br-lan, but only once
130
131
132
        # 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
133
                if [ "$landev" == "$dev" ] && [ ! -e "/sys/class/net/$dev/phy80211" ] ; then
134
135
136
137
138
139
                    if [ $brlan_enabled -eq 0 ]; then
                        dev="br-lan"
                        brlan_enabled=1
                    else
                        dev=""
                    fi
140
                    break
141
                fi
142
143
            done
        fi
144
145
146
147
148
149
150
151
152
153
154
155
156

      [ -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

157
158
159
160
  if qmp_is_routerstationpro && [ "$1" == "wan" -o "$1" == "lan" ]; then
     devices="$(echo $devices | sed -e "s/\beth1\b/eth1.1/g")"
  fi

161
162
163
164
  echo "$devices"
}


165
# Scan and configure the network devices (lan, mesh and wan)
166
# if $1 is set to "force", it rescan all devices
167
168
169
170
qmp_configure_smart_network() {
	echo "---------------------------------------"
	echo "Starting smart networking configuration"
	echo "---------------------------------------"
171
	local force=$1
172
173
174
175
176
	local mesh=""
	local wan=""
	local lan=""
	local dev=""
	local phydevs=""
177
	local ignore_devs=""
178

179
180
181
	[ "$force" != "force" ] && {
		ignore_devs="$(qmp_uci_get interfaces.ignore_devices)"
	}
182

183
	for dev in $(ls /sys/class/net/); do
184
		[ -e /sys/class/net/$dev/device ] || [ dev == "eth0" ] && {
185
			local id
186
			local ignore=0
187

188
			# Check if device is in the ignore list
189
190
191
192
				for id in $ignore_devs; do
					[ "$id" == "$dev" ] && ignore=1
				done

193
			[ $ignore -eq 0 ] && phydevs="$phydevs $dev\n"
194
		}
195
196
197
	done

	phydevs="$(echo -e "$phydevs" | grep -v -e ".*ap$" | grep -v "\\." | sort -u | tr -d ' ' \t)"
198

199
200
201
202
203
204
	# if force is not enabled, we are not changing the existing lan/wan/mesh (only adding new ones)
	[ "$force" != "force" ] && {
		lan="$(qmp_uci_get interfaces.lan_devices)"
		wan="$(qmp_uci_get interfaces.wan_devices)"
		mesh="$(qmp_uci_get interfaces.mesh_devices)"
	}
205

206
207
	local j=0
	local mode=""
208
209
	local cnt
	local cdev
210

211
	for dev in $phydevs; do
212
		# If force is enabled, do not check if the device is already configured
213
214
215
		[ "$force" != "force" ] && {
			cnt=0
			# If it is already configured, doing nothing
216
			for cdev in $lan; do
217
				[ "$cdev" == "$dev" ] && cnt=1
218
			done
219
			for cdev in $mesh; do
220
				[ "$cdev" == "$dev" ] && cnt=1
221
			done
222
			for cdev in $wan; do
223
				[ "$cdev" == "$dev" ] && cnt=1
224
225
226
			done
			[ $cnt -eq 1 ] && continue
		}
227

228
		# If not found before...
229
230
231
232
233
234
235
236
		[ "$dev" == "eth0" ] && {
			lan="$lan eth0"
			mesh="$mesh eth0"
			continue
		}

		## if there is not yet a LAN device, configuring as lan and mesh
		##[ -z "$lan" ] && { lan="$dev"; mesh="$dev" && continue
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251

		# if it is a wifi device
		[ -e "/sys/class/net/$dev/phy80211" ] && {
			j=0
			while qmp_uci_test qmp.@wireless[$j]; do
				[ "$(qmp_uci_get @wireless[$j].device)" == "$dev" ] && {
					mode="$(qmp_uci_get @wireless[$j].mode)"
					[ "$mode" == "ap" ] && lan="$dev $lan" || mesh="$dev $mesh"
					break
				}
				j=$(($j+1))
			done
		} && continue

		# if there is already LAN device and it is not wifi, use as WAN
252
253
254
255
256
		[ -z "$wan" ] && wan="$dev" || {
			# else use as LAN and MESH
			lan="$dev $lan"
			mesh="$dev $mesh"
		}
257
	done
258

259
	echo "Network devices found:"
260
261
262
263
	echo "- LAN $lan"
	echo "- MESH $mesh"
	echo "- WAN $wan"

264
	# Writes the devices to the config
265
266
267
	qmp_uci_set interfaces.lan_devices "$(echo $lan | sed -e s/"^ "//g -e s/" $"//g)"
	qmp_uci_set interfaces.mesh_devices "$(echo $mesh | sed -e s/"^ "//g -e s/" $"//g)"
	qmp_uci_set interfaces.wan_devices "$(echo $wan | sed -e s/"^ "//g -e s/" $"//g)"
268
	qmp_uci_set interfaces.ignore_devices "$ignore_devs"
269
270
}

271
272
qmp_attach_device_to_interface() {
	local device=$1
273
	local interface=$2
274
275
	local intype="$(qmp_uci_get_raw network.$interface.type)"

276
	echo "Attaching device $device to interface $interface"
277
278

	# is it a wifi device?
279
	if qmp_uci_test wireless.$device; then
280
		qmp_uci_set_raw wireless.$device.network=$interface
281
		echo " -> $device wireless attached to $interface"
282
283

	# if it is not
284
	else
285
			if [ "$intype" == "bridge" ]; then
286
				qmp_uci_add_list_raw network.$interface.ifname=$device
287
288
				echo " -> $device attached to $interface bridge"
			else
289
				qmp_uci_set_raw network.$interface.ifname=$device
290
291
				echo " -> $device attached to $interface"
			fi
292
293
294
	fi
}

295
296
297
298
299
300
301
302
303
304
305
306
307
308
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"
309
	uci set network.mesh_ports_vid1.vid="1"
310
311
	uci set network.mesh_ports_vid1.device="eth1"
	uci set network.mesh_ports_vid1.ports="0t 4"
312

313
314
315
316
317
318
319
320
	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
321
322
323
324
325
326

	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
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
419
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
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

478
  local addr_long=$( ipv6calc -q  --in ipv6 $addr --showinfo -m 2>&1 | awk -F'=' '/IPV6=/{print $2}' )
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584

  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"
}

qmp_configure_prepare() {
  local conf=$1
p4u's avatar
p4u committed
585
   if ! [ -f /etc/config/$conf.orig ]; then
586
587
588
589
590
591
592
593
594
595
596
597
    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"

p4u's avatar
p4u committed
598
599
600
601
  echo "-----------------------"
  echo "Configuring networking"
  echo "-----------------------"

602
  qmp_configure_prepare_network $conf
603

604
  # LoopBack device
605
606
607
608
609
610
611
  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
612
613
  qmp_configure_wan
  # LAN devices
614
  qmp_configure_lan
615
616
  # MESH devices
  qmp_configure_mesh
617

618
619
620
621
  uci commit
}


622
qmp_remove_qmp_bmx6_tunnels()
623
{
624
	if echo "$1" | grep -q "^qmp_"
625
626
627
	then
		uci delete bmx6.$1
	fi
628
	uci commit bmx6
629
630
631
632
633
634
}

qmp_unconfigure_bmx6_gateways()
{
	config_load bmx6
	config_foreach qmp_remove_qmp_bmx6_tunnels tunInNet
635
636
	config_foreach qmp_remove_qmp_bmx6_tunnels tunDev
	config_foreach qmp_remove_qmp_bmx6_tunnels tunIn
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
	config_foreach qmp_remove_qmp_bmx6_tunnels tunOut
}

qmp_translate_configuration()
{
	orig_config=$1
	orig_section=$2
	orig_option=$3

	dest_config=$4
	dest_section=$5
	dest_option=${6:-$orig_option}

	value="$(uci -q get $orig_config.$orig_section.$orig_option)"
	if [ -n "$value" ]
	then
		uci set $dest_config.$dest_section.$dest_option="$value"
	fi
}

qmp_add_qmp_bmx6_tunnels()
{
	local section=$1
660
	local name="$section"
661
662
	local config=bmx6
	local ignore
663
	local t
664
	config_get ignore "$section" ignore
665

666
	[ "$ignore" = "1" ] && return
667

668
669
	local type="$(qmp_uci_get_raw gateways.$name.type)"
	qmp_log Configuring gateway $name of type $type
670
	[ -z "$name" ] && name="qmp_$gateway" || name="qmp_$name"
671
672

	if [ "$type" == "offer" ]
673
	then
674
		bmx6_type=tunIn
675
		uci set $config.$name="$bmx6_type"
676
		uci set $config.$name.$bmx6_type="$name"
677
678
679
		for t in \
			network \
			bandwidth
680
		do
681
682
			qmp_translate_configuration gateways $section $t $config $name
		done
683
684
685
	else
		bmx6_type=tunOut
		uci set $config.$name="$bmx6_type"
686
		uci set $config.$name.$bmx6_type="$section"
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
		for t in \
			network \
			srcNet \
			gwName \
			minPrefixLen \
			maxPrefixLen \
			hysteresis \
			rating \
			minBandwidth \
			tableRule \
			kernel \
			boot \
			static \
			zebra \
			system \
			connect \
			rip \
			ripng \
			ospf \
			ospf6 \
			isis \
			bgp \
			babel \
			olsr \
			exportDistance \
			srcType \
			gwId \
			ipMetric
715
		do
716
			qmp_translate_configuration gateways $section $t $config $name
717
		done
718
	fi
719

720
721
722
723
724
725
	gateway="$(($gateway + 1))"
}

qmp_configure_bmx6_gateways()
{
	qmp_unconfigure_bmx6_gateways
726
	config_load gateways
727
728
	gateway=0
	config_foreach qmp_add_qmp_bmx6_tunnels gateway
729
	uci commit bmx6
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

qmp_configure_bmx6() {
  local conf="bmx6"

  qmp_configure_prepare $conf
  uci set $conf.general="bmx6"
  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"

  local primary_mesh_device="$(qmp_get_primary_device)"

756
  local community_node_id=$(qmp_get_id)
757

758
  if qmp_uci_test qmp.interfaces.mesh_devices &&
759
  qmp_uci_test qmp.networks.mesh_protocol_vids
760

761
    then
762
763
    local counter=1

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

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

769
	if [ "$protocol_name" = "bmx6" ] ; then
770

771
772
773
	# Check if the current device is configured as no-vlan
	local use_vlan=1
	for no_vlan_int in $(qmp_uci_get interfaces.no_vlan_devices); do
774
		[ "$no_vlan_int" == "$dev" ] && use_vlan=0
775
	done
776
777
778
779
	
	# Check if the protocol has VLAN tag configured
	local vid="$(echo $protocol_vid | awk -F':' '{print $2}')"
	[ -z "$vid" -o $vid -lt 1 ] && use_vlan=0
780

781
782
783
	# Check if the protocol has VLAN tag configured
	local vid="$(echo $protocol_vid | awk -F':' '{print $2}')"
	[ -z "$vid" -o $vid -lt 1 ] && use_vlan=0
784

785
	# If vlan tagging
786
	    if [ $use_vlan -eq 1 ]; then
787
		local ifname="$dev.$vid"
788

789
	# If not vlan tagging
790
	    else
791
		local ifname="$dev"
792
793
794
795
796
797
798
799
800
801
	    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"
802
803
804
	      uci set $conf.tmain=tunDev
	      uci set $conf.tmain.tunDev=tmain
	      uci set $conf.tmain.tun4Address="$bmx6_ipv4_address/$bmx6_ipv4_netmask"
805

806
	    else
p4u's avatar
p4u committed
807
	      local ipv4_suffix24="$(qmp_get_id 8bit)"
808
	      local ipv4_prefix24="$(qmp_uci_get networks.bmx6_ipv4_prefix24)"
809
810
811
812
	      if [ $(echo -n "$ipv4_prefix24" | tr -d [0-9] | wc -c) -lt 2 ]; then
	      	ipv4_prefix24="${ipv4_prefix24}.0"
	      fi
	      uci set $conf.general.tun4Address="$ipv4_prefix24.$ipv4_suffix24/32"
813
814
815
	      uci set $conf.tmain=tunDev
	      uci set $conf.tmain.tunDev=tmain
	      uci set $conf.tmain.tun4Address="$ipv4_prefix24.$ipv4_suffix24/32"
816

817
818
819
820
821
822
823
824
825
826
827
	    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"
828
829
830
    uci set $conf.tmain=tunDev
    uci set $conf.tmain.tunDev=tmain
    uci set $conf.tmain.tun6Address="$(qmp_uci_get networks.bmx6_ripe_prefix48):$community_node_id:0:0:0:1/64"
831
832
  fi

833
  qmp_configure_bmx6_gateways
834
835
836
837
838

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

p4u's avatar
p4u committed
839
qmp_restart_firewall() {
840
841
842
	iptables -F
	iptables -F -t nat
	sh /etc/firewall.user
p4u's avatar
p4u committed
843
}
844
845

qmp_check_force_internet() {
846
847
	[ "$(qmp_uci_get networks.force_internet)" == "1" ] && qmp_gw_offer_default
	[ "$(qmp_uci_get networks.force_internet)" == "0" ] && qmp_gw_search_default
848
849
}

850
qmp_configure_initial() {
851
	qmp_hooks_exec firstboot
852
853
854
855
	qmp_configure_wifi_initial
	qmp_configure_wifi
	/etc/init.d/network reload
	sleep 1
856
857
858
	qmp_configure_smart_network
}

859
qmp_configure() {
860
  qmp_configure_system
861
  qmp_set_services
862
  qmp_hooks_exec preconf
863
864
865
866
  qmp_check_force_internet
  qmp_configure_network
  qmp_configure_bmx6
  qmp_configure_lan_v6
867
  qmp_hooks_exec postconf
868
869
}