1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#!/bin/sh
: ${FAKE_IP_STATE:=${PWD}/var/fake-ip-state}
not_implemented ()
{
echo "ip stub command: \"$1\" not implemented"
exit 127
}
case "$1" in
link)
case "$2" in
set)
iface="$3"
case "$4" in
up)
rm -f "${FAKE_IP_STATE}/interfaces-down/${iface}"
;;
down)
mkdir -p "${FAKE_IP_STATE}/interfaces-down"
touch "${FAKE_IP_STATE}/interfaces-down/${iface}"
;;
*)
not_implemented "$*"
esac
;;
*)
not_implemented "$1 $2"
esac
;;
addr)
case "$2" in
add)
shift 2
local=""
dev=""
brd=""
while [ -n "$1" ] ; do
case "$1" in
*.*.*.*/*)
local="$1" ; shift
;;
local)
local="$2" ; shift 2
;;
broadcast|brd)
# For now assume this is always '+'.
if [ "$2" != "+" ] ; then
not_implemented "addr add ... brd $2 ..."
fi
shift 2
;;
dev)
dev="$2" ; shift 2
;;
*)
not_implemented "addr add ... $1 ..."
esac
done
if [ -z "$dev" ] ; then
not_implemented "addr add (without dev)"
fi
mkdir -p "${FAKE_IP_STATE}/addresses"
pf="${FAKE_IP_STATE}/addresses/${dev}-primary"
sf="${FAKE_IP_STATE}/addresses/${dev}-secondary"
# We could lock here... but we should be the only ones
# playing around here with these stubs.
if [ ! -f "$pf" ] ; then
echo "$local" >"$pf"
elif grep -Fq "$local" "$pf" ; then
echo "RTNETLINK answers: File exists" >&2
exit 254
elif [ -f "$sf" ] && grep -Fq "$local" "$sf" ; then
echo "RTNETLINK answers: File exists" >&2
exit 254
else
echo "$local" >>"$sf"
fi
;;
delete|del)
shift 2
local=""
dev=""
while [ -n "$1" ] ; do
case "$1" in
*.*.*.*/*)
local="$1" ; shift
;;
local)
local="$2" ; shift 2
;;
dev)
dev="$2" ; shift 2
;;
*)
not_implemented "addr del ... $1 ..."
esac
done
if [ -z "$dev" ] ; then
not_implemented "addr del (without dev)"
fi
mkdir -p "${FAKE_IP_STATE}/addresses"
pf="${FAKE_IP_STATE}/addresses/${dev}-primary"
sf="${FAKE_IP_STATE}/addresses/${dev}-secondary"
# We could lock here... but we should be the only ones
# playing around here with these stubs.
if [ ! -f "$pf" ] ; then
echo "RTNETLINK answers: Cannot assign requested address" >&2
exit 254
elif grep -Fq "$local" "$pf" ; then
# Remove primaries AND SECONDARIES.
rm -f "$pf" "$sf"
elif [ -f "$sf" ] && grep -Fq "$local" "$sf" ; then
grep -Fv "$local" "$sf" >"${sf}.new"
mv "${sf}.new" "$sf"
else
echo "RTNETLINK answers: Cannot assign requested address" >&2
exit 254
fi
;;
show|list)
shift 2
dev=""
primary=true
secondary=true
while [ -n "$1" ] ; do
case "$1" in
dev)
dev="$2" ; shift 2
;;
# Do stupid things and stupid things will happen!
primary)
primary=true ; secondary=false ; shift
;;
secondary)
secondary=true ; primary=false ; shift
;;
*)
# Assume an interface name
dev="$1" ; shift 1
esac
done
devices="$dev"
if [ -z "$devices" ] ; then
# No device specified? Get all the primaries...
devices=$(ls "${FAKE_IP_STATE}/addresses/"*-primary 2>/dev/null | \
sed -e 's@.*/@@' -e 's@-primary$@@')
fi
calc_brd ()
{
case "${local#*/}" in
24)
brd="${local%.*}.255"
;;
*)
not_implemented "list ... fake bits other than 24: ${local#*/}"
esac
}
show_iface()
{
pf="${FAKE_IP_STATE}/addresses/${dev}-primary"
sf="${FAKE_IP_STATE}/addresses/${dev}-secondary"
mac=$(echo $dev | md5sum | sed -r -e 's@(..)(..)(..)(..)(..)(..).*@\1:\2:\3:\4:\5:\6@')
cat <<EOF
${n}: ${dev}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether ${mac} brd ff:ff:ff:ff:ff:ff
EOF
if $primary && [ -r "$pf" ] ; then
read local <"$pf"
calc_brd
cat <<EOF
inet ${local} brd ${brd} scope global ${dev}
EOF
fi
if $secondary && [ -r "$sf" ] ; then
while read local ; do
calc_brd
cat <<EOF
inet ${local} brd ${brd} scope global secondary ${dev}
EOF
done <"$sf"
fi
cat <<EOF
valid_lft forever preferred_lft forever
EOF
}
n=1
for dev in $devices ; do
show_iface
n=$(($n + 1))
done
;;
*)
not_implemented "$1 $2"
esac
;;
*)
not_implemented "$1"
esac
exit 0
|