Commit 7aa7a496 authored by Vladimir Bashkirtsev's avatar Vladimir Bashkirtsev

Added ability to configure network IP interfaces over CWMP

parent c867dc97
......@@ -16,6 +16,7 @@ all: easycwmp-service device-info easycwmp-config
install -v -Dm755 easycwmp-1.5.3/ext/openwrt/scripts/functions/tr181/root /usr/share/easycwmp/functions/root
install -v -Dm755 easycwmp-1.5.3/ext/openwrt/scripts/functions/tr181/ip /usr/share/easycwmp/functions/ip
install -v -Dm755 easycwmp-1.5.3/ext/openwrt/scripts/functions/tr181/ipping_diagnostic /usr/share/easycwmp/functions/ipping_diagnostic
install -v -Dm755 ip /usr/share/easycwmp/functions/ip
install -v -Dm755 ssh /usr/share/easycwmp/functions/ssh
install -v -Dm755 easycwmp-init-device-info /usr/sbin/easycwmp-init-device-info
@echo "$$DEVICE_INFO" > /etc/device_info
......
#!/bin/sh
# Copyright (C) 2015 PIVA Software <www.pivasoftware.com>
# Author: Anis Ellouze <anis.ellouze@pivasoftware.com>
#common_execute_method_param "$parameter" "$permission" "$get_cmd" "$set_cmd" "xsd:$type" "$forcedinform"
# $forcedinform should be set to 1 if the parameter is included in the inform message otherwise empty
# Default of $type = string
#############################
# Entry point functions #
#############################
prefix_list="$prefix_list $DMROOT.IP."
entry_execute_method_list="$entry_execute_method_list entry_execute_method_root_IP"
entry_execute_method_root_IP() {
case "$1" in ""|"$DMROOT."|"$DMROOT.IP."*)
common_execute_method_obj "$DMROOT.IP." "0"
common_execute_method_obj "$DMROOT.IP.Interface." "0" "" "" "ip_device_browse_instances"
common_execute_method_obj "$DMROOT.IP.Diagnostics." "0"
common_execute_method_obj "$DMROOT.IP.Diagnostics.IPPing." "0" "" "" "entry_execute_method_root_IPPingDiagnostics"
return 0
;;
esac
return $E_INVALID_PARAMETER_NAME;
}
sub_entry_ip_interface() {
local j="$2"
local ip_int="$3"
case_param "$1" belongto "$DMROOT.IP.Interface.$j." && {
common_execute_method_obj "$DMROOT.IP.Interface.$j." "0" "" ""
common_execute_method_param "$DMROOT.IP.Interface.$j.Enable" "0" "ip_iface_get_ip_enable $ip_int" "" "xsd:boolean"
common_execute_method_param "$DMROOT.IP.Interface.$j.Name" "0" "ip_get_iface_name $ip_int"
common_execute_method_param "$DMROOT.IP.Interface.$j.Type" "0" "ip_iface_get_ip_type $ip_int"
common_execute_method_param "$DMROOT.IP.Interface.$j.Dynamic" "1" "ip_iface_get_ip_dynamic $ip_int" "ip_iface_set_ip_dynamic $ip_int" "xsd:boolean"
common_execute_method_param "$DMROOT.IP.Interface.$j.IPv4AddressNumberOfEntries" "0" "ip_iface_get_ipv4_count $ip_int" "" "xsd:unsignedInt"
common_execute_method_obj "$DMROOT.IP.Interface.$j.IPv4Address." "1" "add_ipv4 $ip_int" "" "ipv4_browse_instances $j $ip_int"
common_execute_method_obj "$DMROOT.IP.Interface.$j.Stats." "0" "" "" "ip_browse_stat $j $ip_int"
return 0
}
return $E_INVALID_PARAMETER_NAME;
}
sub_entry_ipv4_interface() {
local i="$1"
local j="$2"
local ip_int="$3"
local address=$(echo $4 | cut -d "/" -f 1)
local mask=$(echo $4 | cut -d "/" -f 2)
if [ `is_ipv4 $address` == "1" ] && [ `is_mask $mask` == "1" ]; then
local enabled=1
else
local enabled=0
fi
if [ "$5" == 1 ]; then
local type="DHCP"
local editable=0
else
local type="Static"
local editable=1
fi
case_param "$1" belongto "$DMROOT.IP.Interface.$i.IPv4Address.$j." && {
common_execute_method_obj "$DMROOT.IP.Interface.$i.IPv4Address.$j." "1" "" "delete_ipv4 $ip_int $j"
common_execute_method_param "$DMROOT.IP.Interface.$i.IPv4Address.$j.IPAddress" "$editable" "echo $address" "ipv4_set_address $ip_int $j"
common_execute_method_param "$DMROOT.IP.Interface.$i.IPv4Address.$j.AddressingType" "0" "echo $type"
common_execute_method_param "$DMROOT.IP.Interface.$i.IPv4Address.$j.Enable" "0" "echo $enabled" "" "xsd:boolean"
common_execute_method_param "$DMROOT.IP.Interface.$i.IPv4Address.$j.SubnetMask" "$editable" "echo $mask" "ipv4_set_mask $ip_int $j"
return 0
}
return $E_INVALID_PARAMETER_NAME;
}
sub_entry_ip_stat() {
local j="$1"
local ip_int="$2"
case_param "$1" belongto "$DMROOT.IP.Interface.$j.Stats." && {
common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.BytesSent" "0" "get_tx_byte_stats $ip_int" "" "xsd:unsignedInt"
common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.BytesReceived" "0" "get_rx_byte_stats $ip_int" "" "xsd:unsignedInt"
common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.PacketsSent" "0" "get_tx_packet_stats $ip_int" "" "xsd:unsignedInt"
common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.PacketsReceived" "0" "get_rx_packet_stats $ip_int" "" "xsd:unsignedInt"
common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.ErrorsSent" "0" "get_tx_error_stats $ip_int" "" "xsd:unsignedInt"
common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.ErrorsReceived" "0" "get_rx_error_stats $ip_int" "" "xsd:unsignedInt"
common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.DiscardPacketsSent" "0" "get_tx_discard_stats $ip_int" "" "xsd:unsignedInt"
common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.DiscardPacketsReceived" "0" "get_rx_discard_stats $ip_int" "" "xsd:unsignedInt"
return 0
}
return $E_INVALID_PARAMETER_NAME;
}
#######################################
# Data model browse instances #
#######################################
ip_get_max_instance() {
local max=`$UCI_SHOW network | grep "ip_int_instance" | cut -d'=' -f2 | sed 's/[^0-9]*//g' | sort -nru | head -1`
echo ${max:-0}
}
ip_device_update_instance() {
local ip_int="$1"
local instance=`$UCI_GET $1.ip_int_instance`
local dynamic=`$UCI_GET $1.dynamic`
if [ -z "$instance" ]; then
instance=`ip_get_max_instance`
$UCI_SET $1=interface
$UCI_SET $1.ip_int_instance=$((++instance))
if [ -z "$dynamic" ]; then
$UCI_SET $1.dynamic="false"
fi
$UCI_COMMIT
fi
echo $instance
}
ip_device_browse_instances() {
touch /etc/config/network
local ip_int ip_ints=`ls -1 /sys/class/net`
for ip_int in $ip_ints; do
local j=`ip_device_update_instance network.$ip_int`
sub_entry_ip_interface "$1" "$j" "network.$ip_int"
done
}
ip_iface_get_ipv4_count() {
local name=`ip_get_iface_name $1`
echo `ip -o -4 addr show dev $name | grep -c .`
}
ipv4_browse_instances() {
get_ips $2
local dhcp=`ip_iface_get_ip_dynamic $2`
local dynamic=()
local name=`ip_get_iface_name $2`
local addresses=`ip -o -4 addr show dev $name`
local i=0
while read line; do
if [ ! -z "$line" ] && [[ "$line" =~ dynamic ]]; then
dynamic[$i]=1
local ip=$(echo $line | cut -d " " -f 4)
ips[$i]="$ip"
((i++))
fi
done <<< $addresses
if [ "$dhcp" == "true" ] && [ "$i" == 0 ]; then
dynamic[$i]=1
ips[$i]="Unset/Unset"
((i++))
fi
while read line; do
if [ ! -z "$line" ] && [[ ! "$line" =~ dynamic ]]; then
dynamic[$i]=0
local ip=$(echo $line | cut -d " " -f 4)
ips[$i]="$ip"
((i++))
fi
done <<< $addresses
$UCI_DELETE network.$name.ipv4
local i=0
for ip in "${ips[@]}"; do
if [ "${dynamic[$i]}" == 1 ]; then
$UCI_SET network.$name.dynamic="true"
$UCI_ADD_LIST network.$name.ipv4="-"
fi
((i++))
done
local i=0
for ip in "${ips[@]}"; do
if [ "${dynamic[$i]}" != 1 ]; then
$UCI_ADD_LIST network.$name.ipv4="$ip"
fi
((i++))
done
$UCI_COMMIT
local i=0
local idx=1
for ip in "${ips[@]}"; do
sub_entry_ipv4_interface $1 $idx $2 $ip ${dynamic[$i]}
((i++))
((idx++))
done
}
ip_browse_stat() {
sub_entry_ip_stat $1 $2
}
is_ipv4() {
if [[ "$1" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
echo 1
else
echo 0
fi
}
is_mask() {
if [ "$1" -ge "8" ] && [ "$1" -le "32" ]; then
echo 1
else
echo 0
fi
}
#######################################
# GET & SET Values #
#######################################
ip_get_iface_name() {
local interface=`echo $1 |cut -d "." -f 2`
echo $interface
}
ip_iface_get_ip_enable() {
local name=`ip_get_iface_name $1`
local carrier=`cat /sys/class/net/$name/carrier`
if [ -z "$carrier" ]; then
carrier=0
fi
echo $carrier
}
ip_iface_get_ip_type() {
local name=`ip_get_iface_name $1`
if [ "$name" == "lo" ]; then
echo "Loopback"
else
echo "Normal"
fi
}
ip_iface_get_ip_dynamic() {
local dynamic=`$UCI_GET $1.dynamic`
echo $dynamic
}
ip_iface_set_ip_dynamic() {
$UCI_SET $1.dynamic=$2
get_ips $1
if [ "$2" == "true" ]; then
if [ "${ips[0]}" != "-" ]; then
ips=("-" "${ips[@]}")
fi
else
if [ "${ips[0]}" == "-" ]; then
unset ips[0]
fi
fi
$UCI_DELETE $1.ipv4
for ip in "${ips[@]}"; do
$UCI_ADD_LIST $1.ipv4="$ip"
done
$UCI_COMMIT
reconfigure_networking
}
get_ips() {
local name=`ip_get_iface_name $1`
ipscount=0
ips=()
list=`$UCI_GET network.$name.ipv4`
if [ ! -z "$list" ]; then
while read word; do
if [ -z "$word" ]; then
ips+=" "
else
ips+=("$word")
fi
((ipscount++))
done <<< $(echo "$list" | xargs -n 1)
fi
}
add_ipv4() {
local name=`ip_get_iface_name $1`
$UCI_ADD_LIST network.$name.ipv4="/"
$UCI_COMMIT
get_ips $1
echo $ipscount
}
delete_ipv4() {
local name=`ip_get_iface_name $1`
get_ips $1
local idx=$(($2 - 1))
if [ "${ips[$idx]}" == "-" ]; then
$UCI_SET network.$name.dynamic="false"
fi
unset ips[$idx]
$UCI_DELETE network.$name.ipv4
for ip in "${ips[@]}"; do
$UCI_ADD_LIST network.$name.ipv4="$ip"
done
$UCI_COMMIT
reconfigure_networking
}
ipv4_set_address() {
local name=`ip_get_iface_name $1`
get_ips $1
local idx=$(($2 - 1))
if [ "${ips[$idx]}" != "-" ]; then
IFS="/" read address mask <<< "${ips[$idx]}"
ips[$idx]="$3/$mask"
$UCI_DELETE network.$name.ipv4
for ip in "${ips[@]}"; do
$UCI_ADD_LIST network.$name.ipv4="$ip"
done
$UCI_COMMIT
reconfigure_networking
fi
}
ipv4_set_mask() {
local name=`ip_get_iface_name $1`
get_ips $1
local idx=$(($2 - 1))
if [ "${ips[$idx]}" != "-" ]; then
IFS="/" read address mask <<< "${ips[$idx]}"
ips[$idx]="$address/$3"
$UCI_DELETE network.$name.ipv4
for ip in "${ips[@]}"; do
$UCI_ADD_LIST network.$name.ipv4="$ip"
done
$UCI_COMMIT
reconfigure_networking
fi
}
reconfigure_networking() {
local ints=`ls -1 /sys/class/net`
local configs=`ls -1 /etc/systemd/network`
for config in $configs; do
local found=0
for int in $ints; do
if [ "$config" -eq "$int.network" ]; then
found=1
fi
done
if [ "$found" -eq "0" ]; then
rm -f /etc/systemd/network/$config
fi
done
for int in $ints; do
if [ "$int" != "lo" ]; then
get_ips network.$int
local count=0
local dynamic=0
for ip in "${ips[@]}"; do
if [ "$ip" == "-" ]; then
((count++))
local dynamic=1
else
local address=$(echo $ip | cut -d "/" -f 1)
local mask=$(echo $ip | cut -d "/" -f 2)
if [ `is_ipv4 $address` == "1" ] && [ `is_mask $mask` == "1" ]; then
((count++))
fi
fi
done
if [ "$count" -ne "0" ]; then
echo "[Match]" > /etc/systemd/network/$int.network
echo "Name=$int" >> /etc/systemd/network/$int.network
echo "" >> /etc/systemd/network/$int.network
echo "[Network]" >> /etc/systemd/network/$int.network
if [ "$dynamic" == "1" ]; then
echo "DHCP=ipv4" >> /etc/systemd/network/$int.network
fi
for ip in "${ips[@]}"; do
if [ "$ip" != "-" ]; then
local address=$(echo $ip | cut -d "/" -f 1)
local mask=$(echo $ip | cut -d "/" -f 2)
if [ `is_ipv4 $address` == "1" ] && [ `is_mask $mask` == "1" ]; then
((count++))
fi
echo "Address=$ip" >> /etc/systemd/network/$int.network
fi
done
if [ "$dynamic" == "1" ]; then
echo "" >> /etc/systemd/network/$int.network
echo "[DHCP]" >> /etc/systemd/network/$int.network
echo "UseDomains=true" >> /etc/systemd/network/$int.network
fi
else
rm -f /etc/systemd/network/$int.network
fi
else
rm -f /etc/systemd/network/$int.network
fi
done
systemctl restart systemd-networkd
}
#######################################
# Stats #
#######################################
get_tx_byte_stats() {
local name=`ip_get_iface_name $1`
tx_bytes=`cat /sys/class/net/$name/statistics/tx_bytes`
echo ${tx_bytes:-0}
}
get_rx_byte_stats() {
local name=`ip_get_iface_name $1`
rx_bytes=`cat /sys/class/net/$name/statistics/rx_bytes`
echo ${rx_bytes:-0}
}
get_tx_packet_stats() {
local name=`ip_get_iface_name $1`
tx_packets=`cat /sys/class/net/$name/statistics/tx_packets`
echo ${tx_packets:-0}
}
get_rx_packet_stats() {
local name=`ip_get_iface_name $1`
rx_packets=`cat /sys/class/net/$name/statistics/rx_packets`
echo ${rx_packets:-0}
}
get_tx_error_stats() {
local name=`ip_get_iface_name $1`
tx_errors=`cat /sys/class/net/$name/statistics/tx_errors`
echo ${tx_errors:-0}
}
get_rx_error_stats() {
local name=`ip_get_iface_name $1`
rx_errors=`cat /sys/class/net/$name/statistics/rx_errors`
echo ${rx_errors:-0}
}
get_tx_discard_stats() {
local name=`ip_get_iface_name $1`
tx_dropped=`cat /sys/class/net/$name/statistics/tx_dropped`
echo ${tx_dropped:-0}
}
get_rx_discard_stats() {
local name=`ip_get_iface_name $1`
rx_dropped=`cat /sys/class/net/$name/statistics/rx_dropped`
echo ${rx_dropped:-0}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment