#!/bin/sh
# Generates ietf-intefaces + ietf-ip settings for all detected interfaces
# Interfaces must be of type Ethernet (copper/sfp), not be "internal" DSA
# ports, and must have a "parentbus", i.e., be physical ports.
#
# Usage: gen-interfaces [-4bd]
#
#   -b brN  Bridged mode, set all interfaces as ports in brN
#   -4      Generic option for both modes, enables ZeroConf IPv4
#   -d      Generic option for both modes, enables DHCPv4 client
#
# By default this script generates plain interfaces with IPv6 autoconfig
# (EUI64) enabled.  Useful both in a plain router and a Fail Secure mode
# where any switchcore-backed ports should be prevented from switching.
#
# The default mode currently does not support the '-4' or '-d' options
# for IPv4 ZeroConf or DHCPv4 address, beacause they require source
# routing, or similar, to work.  IPv6 does not require that and is
# therefore the recommended access method.  A separate (and optional)
# 'gen-cfg-custom' script is checked for in the bootstrap script which
# can be used to enable IPv4 and DHCP on a single service interface if
# needed.
#
# The '-b brname' option triggers the bridge mode, creating a 'brname'
# bridge interface using all interfaces classified in 'group port' by
# the nameif script.  In the bridge mode port interfaces do not have any
# IP address, the IPv6 autoconfig address is instead set on 'brN'.  If
# the '-4' option is set, ZeroConfig (169.254.x.y) is anabled on 'brN'.
# If the '-d' option is set, a DHCPv4 client is enabled on 'brN'.
#
# A "mixed mode" is also supported, where the system may have multiple
# ports in a switchcore, but some Ethernet interfaces directly connected
# to the SoC.  In this case, if '-b brname' is given, all 'group port'
# interfaces will be placed in the bridge 'brname' and all other
# interfaces will be brought up and given an IPv6 address (like above).
#
set -e

bridge=
ipv4=false
dhcp=

gen_interface()
{
    cat <<EOF
      ,{
        "name": "$1",
        "type": "infix-if-type:ethernet",
        "ietf-ip:ipv6": {
	    "enabled": $2
EOF
    if [ -n "$3" ]; then
	cat <<EOF
	},
	"infix-interfaces:bridge-port": {
	    "bridge": "$3"
EOF
    fi
    cat <<EOF
	}
      }
EOF
}

while [ "$1" != "" ]; do
    case $1 in
	-4)
	    ipv4=true
	    ;;
	-b)
	    bridge="$2"
	    shift
	    ;;
	-d)
	    dhcp=true
	    ;;
	*)
	    break
    esac
    shift
done

phys_ifaces=$(ip -d -j link show | jq -r '
	 .[] |
	 select(.link_type == "ether") |
	 select(.group != "internal")  |
	 select(has("parentbus")) |
	 .ifname' | sort -V)
ports=$(ip -d -j link show group port | jq -r '
	 .[] |
	 select(.link_type == "ether") |
	 select(.group != "internal")  |
	 select(has("parentbus")) |
	 .ifname' | sort -V)
ifaces=""
for phy in $phys_ifaces; do
    found=""
    for port in $ports; do
	if [ $port = $phy ]; then
	   found=true
	   break
	fi
    done
    if [ -z "$found" ]; then
	ifaces="$ifaces $phy"
    fi
done

cat <<EOF
{
  "ietf-interfaces:interfaces": {
    "interface": [
      {
        "name": "lo",
        "type": "infix-if-type:loopback",
        "ietf-ip:ipv4": {
          "address": [{ "ip": "127.0.0.1", "prefix-length": 8 }]
        },
        "ietf-ip:ipv6": {
          "address": [{ "ip": "::1", "prefix-length": 128 }]
        }
EOF
if [ -n "$bridge" ]; then
    cat <<EOF
      },
      {
        "name": "$bridge",
        "type": "infix-if-type:bridge",
        "ietf-ip:ipv4": {
	    "infix-ip:autoconf": {
                "enabled": $ipv4
            }
        },
	"ietf-ip:ipv6": {
	    "enabled": true
        }
EOF
fi

cat <<EOF
      }
$(for iface in $ifaces; do gen_interface $iface true; done)
$(for iface in $ports; do gen_interface $iface false $bridge; done)
    ]
EOF
if [ "$dhcp" = "true" ] && [ -n "$bridge" ]; then
cat <<EOF
  },
  "infix-dhcp-client:dhcp-client": {
    "enabled": true,
    "client-if": [
      {
        "if-name": "$bridge"
      }
    ]
EOF
fi
cat <<EOF
  }
}
EOF
