#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # # Copyright (C) 2012 IPFire Network Development Team # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # ############################################################################### PROC_NET_VLAN="/proc/net/vlan" PROC_NET_VLAN_CONFIG="${PROC_NET_VLAN}/config" VLAN_PORT_INTERFIX="v" vlan_create() { local device="${1}" shift assert isset device local address local parent local tag # Parse command line arguments while [ $# -gt 0 ]; do case "${1}" in --address=*) address=$(cli_get_val "${1}") ;; --parent=*) parent=$(cli_get_val "${1}") ;; --tag=*) tag=$(cli_get_val "${1}") ;; *) error "Unrecognized argument: ${1}" return ${EXIT_ERROR} ;; esac shift done # Generate a random MAC address if none was passed if ! isset address; then address="$(mac_generate)" fi # Check if address is valid if ! ismac address; then log ERROR "Invalid mac address: ${address}" return ${EXIT_ERROR} fi # Check if a device with the name does already exist if device_exists "${device}"; then log ERROR "Device '${device}' already exists" return ${EXIT_ERROR} fi # Check if the parent device exists if ! device_exists "${parent}"; then log ERROR "Parent device '${parent}' does not exist" return ${EXIT_ERROR} fi # Make the command local command=( ip link add link "${parent}" name "${device}" address "${address}" type vlan id "${tag}" ) # Run the command if ! cmd_quiet "${command[*]}"; then log ERROR "Could not create VLAN device ${device}: $?" return ${EXIT_ERROR} fi log DEBUG "Created VLAN device ${device} (parent = ${parent}, id = ${tag})" return ${EXIT_OK} } vlan_remove() { device_delete "$@" } vlan_get_parent() { local device=${1} assert isset device # Nothing to do, if 8021q module is not loaded. [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK} local dev spacer1 id spacer2 parent while read dev spacer1 id spacer2 parent; do [ "${device}" = "${dev}" ] || continue print "${parent}" return ${EXIT_OK} done < ${PROC_NET_VLAN_CONFIG} return ${EXIT_ERROR} } vlan_get_id() { local device=${1} assert isset device # Nothing to do, if 8021q module is not loaded. [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK} local dev spacer1 id spacer2 parent while read dev spacer1 id spacer2 parent; do [ "${device}" = "${dev}" ] || continue print "${id}" return ${EXIT_OK} done < ${PROC_NET_VLAN_CONFIG} return ${EXIT_ERROR} } vlan_get_by_parent_and_vid() { local parent=${1} assert isset parent local vid=${2} assert isset vid # Nothing to do, if 8021q module is not loaded. [ -r "${PROC_NET_VLAN_CONFIG}" ] || return ${EXIT_OK} local dev spacer1 id spacer2 par while read dev spacer1 id spacer2 par; do [ "${parent}" = "${par}" ] || continue [ "${vid}" = "${id}" ] || continue print "${dev}" return ${EXIT_OK} done < ${PROC_NET_VLAN_CONFIG} return ${EXIT_ERROR} }