#!/bin/sh
#
# Starts networking
#
# Things have become a bit complex:
# a) we may have two usb interfaces, one for the host port and
#    one for the device port. The first one is used for the
#    master-slave communication where the slave communicates
#    via its host port with the device port of the master.
#    The other one on the device port is (except on masters)
#    for logging in via an UB cable.
# b) We need two interfaces on 'br-usb-device', one for RNDIS (to be able to
#    talk with Windows 10) and CDC_Ether. The gadgets are created in
#    S39prepare_networking and the bridge is creatted here and
#    the gadgets added to it. Their MAC adresses are derived
#    fron that of the bridge by replacing just the first octet
#    (42 for the bridge) by '02', '12', '22' or '32'.

. /lib/ebee_funcs
. /lib/net_funcs

# Interface names of bridge and gadgets

br_name="br-usb-device"
rndis_name="usb-rndis"
cdc_ether_name="usb-cdc-ether"

bridge_up()
{
    # Get MAC address of bridge. We rely on it not to start with
    # '[0123]2' as addresses starting that way are used for the
    # interfaces in the bridge. If it does fix it by setting the
    # first octet to '42'.

    br_mac=$(get_mac.sh "${br_name}")
    if (echo "${br_mac}" | grep -q '^[0123]2')
    then
        echo "Fixing bridge MAC address '${br_mac}'"
        br_mac="42$(echo -n ${br_mac} | sed 's/[^:]*//')"
    fi

    # Create the bridge and set its MAC address

    brctl addbr "${br_name}"
    ip link set "${br_name}" address "${br_mac}"

    # Add USB gadgets to the bridge.
    # The MAC of the RNDIS and CDC_Ether interfaces are the
    # same as that of the brudge, just differing only in
    # the first octet. Add those 2 interfaces to the bridge
    # after checking that they exist and renaming them.

    mac="$(echo -n ${br_mac} | sed 's/[^:]*//')"

	rndis_mac="02${mac}"                  # as set by S39prepare_networking
    old_name="$(find_net_interface_by_mac ${rndis_mac})"
    if [ -z "${old_name}" ]
    then
        echo "USB RNDIS gadget not working"
    else
        ip link set "${old_name}" name "${rndis_name}"
        ifconfig "${rndis_name}" 0.0.0.0
        brctl addif "${br_name}" "${rndis_name}"
    fi

    cdc_ether_mac="22${mac}"                  # as set by S39prepare_networking
    old_name="$(find_net_interface_by_mac ${cdc_ether_mac})"
    if [ -z "${old_name}" ]
    then
        echo "USB CDC-Ether gadget not working"
    else
        ip link set "${old_name}" name "${cdc_ether_name}"
        ifconfig "${cdc_ether_name}" 0.0.0.0
        brctl addif "${br_name}" "${cdc_ether_name}"
    fi
}


bridge_down()
{
    # Tear down the bridge as well as the interfaces it was controlling,

    ifconfig "${cdc_ether_name}" down
    ifconfig "${rndis_name}" down
    ifconfig "${br_name}" down

    brctl delif "${br_name}" "${cdc_ether_name}"
    brctl delif "${br_name}" "${rndis_name}"
    brctl delbr "${br_name}"
}


start_network()
{
    echo -n "Starting network: "

    # Put the two interfaces, one for RNDIS and one for CDC_Ether
    # and started by S39prepare_networing, into a bridge named
    # 'br-usb-device'.

    bridge_up

    # Now bring up all interfaces

    ifup -a

    echo "done"
}


stop_network()
{
    echo -n "Stopping network: "

    ifdown -a
    bridge_down

    echo "done"
}


case "$1" in
    start)
        start_network
        ;;

    stop)
        stop_network
        ;;

    restart|reload)
        "$0" stop
        "$0" start
        ;;

    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac


# Local variables:
# tab-width: 4
# indent-tabs-mode: nil
# End:
