#!/bin/bash
#

# Copyright (C) 2011, 2012, 2013 Google Inc.
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

# Allow overriding for tests
readonly LOCALSTATEDIR=${LOCALSTATEDIR:-${GANETI_ROOTDIR:-}/var}
readonly SYSCONFDIR=${SYSCONFDIR:-${GANETI_ROOTDIR:-}/etc}

readonly PKGLIBDIR=/usr/lib/ganeti
readonly LOG_DIR="$LOCALSTATEDIR/log/ganeti"
readonly RUN_DIR="$LOCALSTATEDIR/run/ganeti"
readonly DATA_DIR="$LOCALSTATEDIR/lib/ganeti"
readonly CONF_DIR="$SYSCONFDIR/ganeti"

function check {

  if [ -z "$INTERFACE" ]; then
    echo "No network interface specified"
    exit 1
  fi

  if [ -z "$MODE" ]; then
    echo "MODE not specified"
    exit 1
  fi

}

function fix_mac {

  # Fix the autogenerated MAC to have the first octet set to "fe"
  # to discourage the bridge from using the TAP dev's MAC
  FIXED_MAC=$(ip link show $INTERFACE | \
    awk '{if ($1 == "link/ether") printf("fe%s",substr($2,3,15))}')
  # in case of a vif (xen_netback device) this action is not allowed
  ip link set $INTERFACE address $FIXED_MAC || true

}

function setup_bridge {

  if [ "$MODE" = "bridged" ]; then
    fix_mac
    ip link set $INTERFACE up
    ip link set $INTERFACE mtu $(</sys/class/net/${LINK}/mtu)

    # Connect the interface to the bridge
    brctl addif $LINK $INTERFACE
  fi

}

function setup_ovs {
  if [ "$MODE" = "openvswitch" ]; then
    ovs-vsctl add-port ${LINK} $INTERFACE
  fi
}

function setup_route {
  if [ "$MODE" = "openvswitch" ]; then
    ip link set $INTERFACE up

    if [ -z "$IP" ]; then
      echo "Routed NIC but no IP address specified"
      exit 1
    fi

    # Route traffic targeted at the IP to the interface
    if [ -n "$LINK" ]; then
      while ip rule del dev $INTERFACE; do :; done
      ip rule add dev $INTERFACE table $LINK
      ip route replace $IP table $LINK proto static dev $INTERFACE

    else
      ip route replace $IP proto static dev $INTERFACE
    fi

    # Allow routing and arp proxying, or ndp proxying (IPv6)
    if [ -d "/proc/sys/net/ipv4/conf/$INTERFACE" ]; then
      echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/proxy_arp
      echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE/forwarding
    fi

    if [ -d "/proc/sys/net/ipv6/conf/$INTERFACE" ]; then
      echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/proxy_ndp
      echo 1 > /proc/sys/net/ipv6/conf/$INTERFACE/forwarding
    fi
  fi
}
