From df4e0684ca5ee8a41ce646888b1b07847f82624a Mon Sep 17 00:00:00 2001
From: Oluf Lorenzen
Date: Fri, 10 Dec 2010 22:18:44 +0100
Subject: [PATCH] enable configuration of VLANs
raw hacked commit, untested
---
examples/ethernet-iproute-vlan | 10 +++
src/connections/ethernet-iproute-vlan | 134 +++++++++++++++++++++++++++++++++
src/network | 17 ++++-
3 files changed, 158 insertions(+), 3 deletions(-)
create mode 100644 examples/ethernet-iproute-vlan
create mode 100644 src/connections/ethernet-iproute-vlan
diff --git a/examples/ethernet-iproute-vlan b/examples/ethernet-iproute-vlan
new file mode 100644
index 0000000..47778a2
--- /dev/null
+++ b/examples/ethernet-iproute-vlan
@@ -0,0 +1,10 @@
+CONNECTION="ethernet-iproute-vlan"
+DESCRIPTION="A more versatile static ethernet connection using iproute and VLAN"
+PHYS_INTERFACE="eth0"
+VLAN="7"
+FLAG="false"
+IP="static"
+# Any valid iproute command can be placed in this array
+IPCFG=("addr add dev eth0.7 192.168.1.23/24 brd +" \
+ "route replace default via 192.168.1.1")
+DNS=("192.168.1.1")
diff --git a/src/connections/ethernet-iproute-vlan b/src/connections/ethernet-iproute-vlan
new file mode 100644
index 0000000..61649ee
--- /dev/null
+++ b/src/connections/ethernet-iproute-vlan
@@ -0,0 +1,134 @@
+#! /bin/bash
+. /usr/lib/network/network
+
+INTERFACE=$PHYS_INTERFACE.$VLAN
+
+error()
+{
+ err_append "$*"
+ ip addr flush $INTERFACE &>/dev/null
+ quirk "nodown" || ip link set $INTERFACE down &>/dev/null
+ exit 1
+}
+
+ethernet_up() {
+ load_profile $1
+
+ if [[ ! -e /sys/class/net/$PHYS_INTERFACE ]]; then
+ if ! echo "$PHYS_INTERFACE"|grep ":"; then
+ error "Physical interface $PHYS_INTERFACE does not exist"
+ fi
+ fi
+ ip link set $PHYS_INTERFACE up
+
+ if [[ ! -e /sys/class/net/$INTERFACE ]]; then
+ if ! modprobe 8021q; then
+ echo "Kernel-Module 8021q could not be loaded"
+ fi
+ if [[! -e /usr/sbin/vconfig ]]; then
+ error "vconfig is not executable"
+ fi
+ if ! vconfig add $PHYS_INTERFACE $VLAN; then
+ error "VLAN-Interface $INTERFACE could not be created"
+ fi
+ if ! "$FLAG" == "true" ; then
+ vconfig set_flag $INTERFACE 1
+ fi
+ fi
+
+ ip link set $INTERFACE up
+ sleep 1
+
+ if ip link show $PHYS_INTERFACE|grep -q "NO-CARRIER"; then
+ err_append "No connection"
+ return 1
+ fi
+
+ if checkyesno ${AUTH8021X:-no}; then
+ . ${SUBR_DIR}/8021x
+ [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf"
+ [[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwired"
+
+ start_wpa "$INTERFACE" "$WPA_CONF" "$WPA_OPTS"
+ if ! wpa_check "$INTERFACE"; then
+ ip link set $INTERFACE down
+ return 1
+ fi
+ fi
+
+ case $IP in
+ dhcp)
+ # Clear remaining pid files.
+ rm -f /var/run/dhcpcd-${INTERFACE}.{pid,cache} >/dev/null 2>&1
+
+ # If using own dns, tell dhcpcd to NOT replace resolv.conf
+ [[ -n "$DNS" ]] && DHCP_OPTIONS="-C resolv.conf $DHCP_OPTIONS"
+
+ if ! dhcpcd -qL -t ${DHCP_TIMEOUT:-10} $DHCP_OPTIONS $INTERFACE; then
+ error "DHCP IP lease attempt failed"
+ fi
+ ;;
+ static)
+ if [[ -n "$ADDR" ]]; then
+ if ! ip addr add ${ADDR}/24 brd + dev $INTERFACE; then
+ error "Could not configure interface"
+ fi
+ fi
+ if [[ -n "$GATEWAY" ]]; then
+ if ! ip route add default via $GATEWAY; then
+ error "Adding gateway failed"
+ fi
+ fi
+ ;;
+ *)
+ error "Profile error: IP must be either 'dhcp' or 'static'"
+ ;;
+ esac
+
+ if [[ -n "$IPCFG" ]]; then
+ for line in "${IPCFG[@]}"; do
+ if ! ip $line; then
+ error "Could not configure interface"
+ fi
+ done
+ fi
+
+ # Set hostname
+ if [[ -n "$HOSTNAME" ]]; then
+ if ! hostname $HOSTNAME; then
+ error "Cannot set hostname"
+ fi
+ fi
+
+ # Generate a new resolv.conf
+ if [[ -n "$DNS" ]]; then
+ : >/etc/resolv.conf
+ [[ -n "$DOMAIN" ]] && echo "domain $DOMAIN" >>/etc/resolv.conf
+ [[ -n "$SEARCH" ]] && echo "search $SEARCH" >>/etc/resolv.conf
+
+ if [[ -n "$DNS" ]]; then
+ for dns in ${DNS[@]}; do
+ echo "nameserver $dns" >>/etc/resolv.conf
+ done
+ fi
+ fi
+ return 0
+}
+
+ethernet_down() {
+ load_profile $1
+
+ if [[ "$IP" == "dhcp" ]]; then
+ if [[ -f /var/run/dhcpcd-${INTERFACE}.pid ]]; then
+ dhcpcd -qx $INTERFACE
+ fi
+ fi
+
+ ip addr flush $INTERFACE
+ quirk "nodown" || vconfig rem $INTERFACE
+
+}
+
+ethernet_$1 $2
+exit $?
+# vim: set ts=4 et sw=4:
diff --git a/src/network b/src/network
index 5bb4182..faf00ba 100644
--- a/src/network
+++ b/src/network
@@ -16,9 +16,20 @@ load_profile()
report_debug "Loading profile $1"
INTERFACE=$(. "$PROFILE_DIR/$1"; echo "$INTERFACE")
report_debug "Configuring interface $INTERFACE"
- if [[ -z "$INTERFACE" ]]; then
- report_err "Profile missing an interface to configure"
- return 1
+ if [[ ! $CONNECTION == "ethernet-iproute-vlan" ]] ; then
+ if [[ -z "$INTERFACE" ]]; then
+ report_err "Profile missing an interface to configure"
+ return 1
+ fi
+ else
+ if [[ -z "$PHYS_INTERFACE" ]]; then
+ report_err "Profile missing an physical interface to configure"
+ return 1
+ fi
+ if [[ -z "$VLAN" ]]; then
+ report_err "Profile missing an VLAN to configure"
+ return 1
+ fi
fi
if [[ -f "$IFACE_DIR/$INTERFACE" ]]; then
report_debug "Interface level configuration enabled: $IFACE_DIR/$INTERFACE"
--
1.6.1