| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
FAKE_IP_STATE="${FAKE_NETWORK_STATE}/ip-state" |
| 4 |
mkdir -p "$FAKE_IP_STATE" |
| 5 |
|
| 6 |
promote_secondaries=true |
| 7 |
|
| 8 |
not_implemented() |
| 9 |
{ |
| 10 |
echo "ip stub command: \"$1\" not implemented" |
| 11 |
exit 127 |
| 12 |
} |
| 13 |
|
| 14 |
###################################################################### |
| 15 |
|
| 16 |
ip_link() |
| 17 |
{ |
| 18 |
case "$1" in |
| 19 |
set) |
| 20 |
shift |
| 21 |
# iface="$1" |
| 22 |
case "$2" in |
| 23 |
up) ip_link_set_up "$1" ;; |
| 24 |
down) ip_link_down_up "$1" ;; |
| 25 |
*) not_implemented "\"$2\" in \"$orig_args\"" ;; |
| 26 |
esac |
| 27 |
;; |
| 28 |
show) |
| 29 |
shift |
| 30 |
ip_link_show "$@" |
| 31 |
;; |
| 32 |
add*) |
| 33 |
shift |
| 34 |
ip_link_add "$@" |
| 35 |
;; |
| 36 |
del*) |
| 37 |
shift |
| 38 |
ip_link_delete "$@" |
| 39 |
;; |
| 40 |
*) not_implemented "$*" ;; |
| 41 |
esac |
| 42 |
} |
| 43 |
|
| 44 |
ip_link_add() |
| 45 |
{ |
| 46 |
_link="" |
| 47 |
_name="" |
| 48 |
_type="" |
| 49 |
|
| 50 |
while [ -n "$1" ]; do |
| 51 |
case "$1" in |
| 52 |
link) |
| 53 |
_link="$2" |
| 54 |
shift 2 |
| 55 |
;; |
| 56 |
name) |
| 57 |
_name="$2" |
| 58 |
shift 2 |
| 59 |
;; |
| 60 |
type) |
| 61 |
if [ "$2" != "vlan" ]; then |
| 62 |
not_implemented "link type $1" |
| 63 |
fi |
| 64 |
_type="$2" |
| 65 |
shift 2 |
| 66 |
;; |
| 67 |
id) shift 2 ;; |
| 68 |
*) not_implemented "$1" ;; |
| 69 |
esac |
| 70 |
done |
| 71 |
|
| 72 |
case "$_type" in |
| 73 |
vlan) |
| 74 |
if [ -z "$_name" ] || [ -z "$_link" ]; then |
| 75 |
not_implemented "ip link add with null name or link" |
| 76 |
fi |
| 77 |
|
| 78 |
mkdir -p "${FAKE_IP_STATE}/interfaces-vlan" |
| 79 |
echo "$_link" >"${FAKE_IP_STATE}/interfaces-vlan/${_name}" |
| 80 |
ip_link_set_down "$_name" |
| 81 |
;; |
| 82 |
esac |
| 83 |
} |
| 84 |
|
| 85 |
ip_link_delete() |
| 86 |
{ |
| 87 |
mkdir -p "${FAKE_IP_STATE}/interfaces-deleted" |
| 88 |
touch "${FAKE_IP_STATE}/interfaces-deleted/$1" |
| 89 |
rm -f "${FAKE_IP_STATE}/interfaces-vlan/$1" |
| 90 |
} |
| 91 |
|
| 92 |
ip_link_set_up() |
| 93 |
{ |
| 94 |
rm -f "${FAKE_IP_STATE}/interfaces-down/$1" |
| 95 |
rm -f "${FAKE_IP_STATE}/interfaces-deleted/$1" |
| 96 |
} |
| 97 |
|
| 98 |
ip_link_set_down() |
| 99 |
{ |
| 100 |
rm -f "${FAKE_IP_STATE}/interfaces-deleted/$1" |
| 101 |
mkdir -p "${FAKE_IP_STATE}/interfaces-down" |
| 102 |
touch "${FAKE_IP_STATE}/interfaces-down/$1" |
| 103 |
} |
| 104 |
|
| 105 |
ip_link_show() |
| 106 |
{ |
| 107 |
dev="$1" |
| 108 |
if [ "$dev" = "dev" ] && [ -n "$2" ]; then |
| 109 |
dev="$2" |
| 110 |
fi |
| 111 |
|
| 112 |
if [ -e "${FAKE_IP_STATE}/interfaces-deleted/$dev" ]; then |
| 113 |
echo "Device \"${dev}\" does not exist." >&2 |
| 114 |
exit 255 |
| 115 |
fi |
| 116 |
|
| 117 |
if [ -r "${FAKE_IP_STATE}/interfaces-vlan/${dev}" ]; then |
| 118 |
read -r _link <"${FAKE_IP_STATE}/interfaces-vlan/${dev}" |
| 119 |
dev="${dev}@${_link}" |
| 120 |
fi |
| 121 |
|
| 122 |
_state="UP" |
| 123 |
_flags=",UP,LOWER_UP" |
| 124 |
if [ -e "${FAKE_IP_STATE}/interfaces-down/$dev" ]; then |
| 125 |
_state="DOWN" |
| 126 |
_flags="" |
| 127 |
fi |
| 128 |
case "$dev" in |
| 129 |
lo) |
| 130 |
_mac="00:00:00:00:00:00" |
| 131 |
_brd="00:00:00:00:00:00" |
| 132 |
_type="loopback" |
| 133 |
_state="UNKNOWN" |
| 134 |
_status="<LOOPBACK${_flags}>" |
| 135 |
_opts="mtu 65536 qdisc noqueue state ${_state}" |
| 136 |
;; |
| 137 |
*) |
| 138 |
_mac=$(echo "$dev" | cksum | sed -r -e 's@(..)(..)(..).*@fe:fe:fe:\1:\2:\3@') |
| 139 |
_brd="ff:ff:ff:ff:ff:ff" |
| 140 |
_type="ether" |
| 141 |
_status="<BROADCAST,MULTICAST${_flags}>" |
| 142 |
_opts="mtu 1500 qdisc pfifo_fast state ${_state} qlen 1000" |
| 143 |
;; |
| 144 |
esac |
| 145 |
|
| 146 |
if $brief; then |
| 147 |
printf '%-16s %-14s %-17s %s\n' \ |
| 148 |
"$dev" "$_status" "$_mac" "$_status" |
| 149 |
else |
| 150 |
echo "${n:-42}: ${dev}: ${_status} ${_opts}" |
| 151 |
echo " link/${_type} ${_mac} brd ${_brd}" |
| 152 |
fi |
| 153 |
} |
| 154 |
|
| 155 |
# This is incomplete because it doesn't actually look up table ids in |
| 156 |
# /etc/iproute2/rt_tables. The rules/routes are actually associated |
| 157 |
# with the name instead of the number. However, we include a variable |
| 158 |
# to fake a bad table id. |
| 159 |
[ -n "$IP_ROUTE_BAD_TABLE_ID" ] || IP_ROUTE_BAD_TABLE_ID=false |
| 160 |
|
| 161 |
ip_check_table() |
| 162 |
{ |
| 163 |
_cmd="$1" |
| 164 |
|
| 165 |
if [ "$_cmd" = "route" ] && [ -z "$_table" ]; then |
| 166 |
_table="main" |
| 167 |
fi |
| 168 |
|
| 169 |
[ -n "$_table" ] || not_implemented "ip rule/route without \"table\"" |
| 170 |
|
| 171 |
# Only allow tables names from 13.per_ip_routing and "main". This |
| 172 |
# is a cheap way of avoiding implementing the default/local |
| 173 |
# tables. |
| 174 |
case "$_table" in |
| 175 |
ctdb.* | main) |
| 176 |
if $IP_ROUTE_BAD_TABLE_ID; then |
| 177 |
# Ouch. Simulate inconsistent errors from ip. :-( |
| 178 |
case "$_cmd" in |
| 179 |
route) |
| 180 |
echo "Error: argument \"${_table}\" is wrong: table id value is invalid" >&2 |
| 181 |
|
| 182 |
;; |
| 183 |
*) |
| 184 |
echo "Error: argument \"${_table}\" is wrong: invalid table ID" >&2 |
| 185 |
;; |
| 186 |
esac |
| 187 |
exit 255 |
| 188 |
fi |
| 189 |
;; |
| 190 |
*) not_implemented "table=${_table} ${orig_args}" ;; |
| 191 |
esac |
| 192 |
} |
| 193 |
|
| 194 |
###################################################################### |
| 195 |
|
| 196 |
ip_addr() |
| 197 |
{ |
| 198 |
case "$1" in |
| 199 |
show | list | "") |
| 200 |
shift |
| 201 |
ip_addr_show "$@" |
| 202 |
;; |
| 203 |
add*) |
| 204 |
shift |
| 205 |
ip_addr_add "$@" |
| 206 |
;; |
| 207 |
del*) |
| 208 |
shift |
| 209 |
ip_addr_del "$@" |
| 210 |
;; |
| 211 |
*) not_implemented "\"$1\" in \"$orig_args\"" ;; |
| 212 |
esac |
| 213 |
} |
| 214 |
|
| 215 |
ip_addr_show() |
| 216 |
{ |
| 217 |
dev="" |
| 218 |
primary=true |
| 219 |
secondary=true |
| 220 |
_to="" |
| 221 |
|
| 222 |
if $brief; then |
| 223 |
not_implemented "ip -br addr show in \"$orig_args\"" |
| 224 |
fi |
| 225 |
|
| 226 |
while [ -n "$1" ]; do |
| 227 |
case "$1" in |
| 228 |
dev) |
| 229 |
dev="$2" |
| 230 |
shift 2 |
| 231 |
;; |
| 232 |
# Do stupid things and stupid things will happen! |
| 233 |
primary) |
| 234 |
primary=true |
| 235 |
secondary=false |
| 236 |
shift |
| 237 |
;; |
| 238 |
secondary) |
| 239 |
secondary=true |
| 240 |
primary=false |
| 241 |
shift |
| 242 |
;; |
| 243 |
to) |
| 244 |
_to="$2" |
| 245 |
shift 2 |
| 246 |
;; |
| 247 |
*) |
| 248 |
# Assume an interface name |
| 249 |
dev="$1" |
| 250 |
shift 1 |
| 251 |
;; |
| 252 |
esac |
| 253 |
done |
| 254 |
devices="$dev" |
| 255 |
if [ -z "$devices" ]; then |
| 256 |
# No device specified? Get all the primaries... |
| 257 |
devices=$(find "${FAKE_IP_STATE}/addresses" -name "*-primary" | |
| 258 |
sed -e 's@.*/@@' -e 's@-.*-primary$@@' | |
| 259 |
sort -u) |
| 260 |
fi |
| 261 |
calc_brd() |
| 262 |
{ |
| 263 |
case "${local#*/}" in |
| 264 |
24) brd="${local%.*}.255" ;; |
| 265 |
32) brd="" ;; |
| 266 |
*) not_implemented "list ... fake bits other than 24/32: ${local#*/}" ;; |
| 267 |
esac |
| 268 |
} |
| 269 |
show_iface() |
| 270 |
{ |
| 271 |
ip_link_show "$dev" |
| 272 |
|
| 273 |
nets=$(find "${FAKE_IP_STATE}/addresses" -name "${dev}-*-primary" | |
| 274 |
sed -e 's@.*/@@' -e "s@${dev}-\(.*\)-primary\$@\1@") |
| 275 |
|
| 276 |
for net in $nets; do |
| 277 |
pf="${FAKE_IP_STATE}/addresses/${dev}-${net}-primary" |
| 278 |
sf="${FAKE_IP_STATE}/addresses/${dev}-${net}-secondary" |
| 279 |
if $primary && [ -r "$pf" ]; then |
| 280 |
read -r local scope <"$pf" |
| 281 |
if [ -z "$_to" ] || [ "${_to%/*}" = "${local%/*}" ]; then |
| 282 |
calc_brd |
| 283 |
echo " inet ${local} ${brd:+brd ${brd} }scope ${scope} ${dev}" |
| 284 |
fi |
| 285 |
fi |
| 286 |
if $secondary && [ -r "$sf" ]; then |
| 287 |
while read -r local scope; do |
| 288 |
if [ -z "$_to" ] || [ "${_to%/*}" = "${local%/*}" ]; then |
| 289 |
calc_brd |
| 290 |
echo " inet ${local} ${brd:+brd }${brd} scope ${scope} secondary ${dev}" |
| 291 |
fi |
| 292 |
done <"$sf" |
| 293 |
fi |
| 294 |
if [ -z "$_to" ]; then |
| 295 |
echo " valid_lft forever preferred_lft forever" |
| 296 |
fi |
| 297 |
done |
| 298 |
} |
| 299 |
n=1 |
| 300 |
for dev in $devices; do |
| 301 |
if [ -z "$_to" ] || |
| 302 |
grep -F "${_to%/*}/" "${FAKE_IP_STATE}/addresses/${dev}-"* >/dev/null; then |
| 303 |
show_iface |
| 304 |
fi |
| 305 |
n=$((n + 1)) |
| 306 |
done |
| 307 |
} |
| 308 |
|
| 309 |
# Copied from 13.per_ip_routing for now... so this is lazy testing :-( |
| 310 |
ipv4_host_addr_to_net() |
| 311 |
{ |
| 312 |
_addr="$1" |
| 313 |
|
| 314 |
_host="${_addr%/*}" |
| 315 |
_maskbits="${_addr#*/}" |
| 316 |
|
| 317 |
# Convert the host address to an unsigned long by splitting out |
| 318 |
# the octets and doing the math. |
| 319 |
_host_ul=0 |
| 320 |
# Want word splitting here |
| 321 |
# shellcheck disable=SC2086 |
| 322 |
for _o in $( |
| 323 |
export IFS="." |
| 324 |
echo $_host |
| 325 |
); do |
| 326 |
_host_ul=$(((_host_ul << 8) + _o)) # work around Emacs color bug |
| 327 |
done |
| 328 |
|
| 329 |
# Calculate the mask and apply it. |
| 330 |
_mask_ul=$((0xffffffff << (32 - _maskbits))) |
| 331 |
_net_ul=$((_host_ul & _mask_ul)) |
| 332 |
|
| 333 |
# Now convert to a network address one byte at a time. |
| 334 |
_net="" |
| 335 |
for _o in $(seq 1 4); do |
| 336 |
_net="$((_net_ul & 255))${_net:+.}${_net}" |
| 337 |
_net_ul=$((_net_ul >> 8)) |
| 338 |
done |
| 339 |
|
| 340 |
echo "${_net}/${_maskbits}" |
| 341 |
} |
| 342 |
|
| 343 |
ip_addr_add() |
| 344 |
{ |
| 345 |
local="" |
| 346 |
dev="" |
| 347 |
brd="" |
| 348 |
scope="global" |
| 349 |
while [ -n "$1" ]; do |
| 350 |
case "$1" in |
| 351 |
*.*.*.*/*) |
| 352 |
local="$1" |
| 353 |
shift |
| 354 |
;; |
| 355 |
local) |
| 356 |
local="$2" |
| 357 |
shift 2 |
| 358 |
;; |
| 359 |
broadcast | brd) |
| 360 |
# For now assume this is always '+'. |
| 361 |
if [ "$2" != "+" ]; then |
| 362 |
not_implemented "addr add ... brd $2 ..." |
| 363 |
fi |
| 364 |
shift 2 |
| 365 |
;; |
| 366 |
dev) |
| 367 |
dev="$2" |
| 368 |
shift 2 |
| 369 |
;; |
| 370 |
scope) |
| 371 |
scope="$2" |
| 372 |
shift 2 |
| 373 |
;; |
| 374 |
*) |
| 375 |
not_implemented "$@" |
| 376 |
;; |
| 377 |
esac |
| 378 |
done |
| 379 |
if [ -z "$dev" ]; then |
| 380 |
not_implemented "addr add (without dev)" |
| 381 |
fi |
| 382 |
mkdir -p "${FAKE_IP_STATE}/addresses" |
| 383 |
net_str=$(ipv4_host_addr_to_net "$local") |
| 384 |
net_str=$(echo "$net_str" | sed -e 's@/@_@') |
| 385 |
pf="${FAKE_IP_STATE}/addresses/${dev}-${net_str}-primary" |
| 386 |
sf="${FAKE_IP_STATE}/addresses/${dev}-${net_str}-secondary" |
| 387 |
# We could lock here... but we should be the only ones playing |
| 388 |
# around here with these stubs. |
| 389 |
if [ ! -f "$pf" ]; then |
| 390 |
echo "$local $scope" >"$pf" |
| 391 |
elif grep -Fq "$local" "$pf"; then |
| 392 |
echo "RTNETLINK answers: File exists" >&2 |
| 393 |
exit 254 |
| 394 |
elif [ -f "$sf" ] && grep -Fq "$local" "$sf"; then |
| 395 |
echo "RTNETLINK answers: File exists" >&2 |
| 396 |
exit 254 |
| 397 |
else |
| 398 |
echo "$local $scope" >>"$sf" |
| 399 |
fi |
| 400 |
} |
| 401 |
|
| 402 |
ip_addr_del() |
| 403 |
{ |
| 404 |
local="" |
| 405 |
dev="" |
| 406 |
while [ -n "$1" ]; do |
| 407 |
case "$1" in |
| 408 |
*.*.*.*/*) |
| 409 |
local="$1" |
| 410 |
shift |
| 411 |
;; |
| 412 |
local) |
| 413 |
local="$2" |
| 414 |
shift 2 |
| 415 |
;; |
| 416 |
dev) |
| 417 |
dev="$2" |
| 418 |
shift 2 |
| 419 |
;; |
| 420 |
*) |
| 421 |
not_implemented "addr del ... $1 ..." |
| 422 |
;; |
| 423 |
esac |
| 424 |
done |
| 425 |
if [ -z "$dev" ]; then |
| 426 |
not_implemented "addr del (without dev)" |
| 427 |
fi |
| 428 |
mkdir -p "${FAKE_IP_STATE}/addresses" |
| 429 |
net_str=$(ipv4_host_addr_to_net "$local") |
| 430 |
net_str=$(echo "$net_str" | sed -e 's@/@_@') |
| 431 |
pf="${FAKE_IP_STATE}/addresses/${dev}-${net_str}-primary" |
| 432 |
sf="${FAKE_IP_STATE}/addresses/${dev}-${net_str}-secondary" |
| 433 |
# We could lock here... but we should be the only ones playing |
| 434 |
# around here with these stubs. |
| 435 |
if [ ! -f "$pf" ]; then |
| 436 |
echo "RTNETLINK answers: Cannot assign requested address" >&2 |
| 437 |
exit 254 |
| 438 |
elif grep -Fq "$local" "$pf"; then |
| 439 |
if $promote_secondaries && [ -s "$sf" ]; then |
| 440 |
head -n 1 "$sf" >"$pf" |
| 441 |
sed -i -e '1d' "$sf" |
| 442 |
else |
| 443 |
# Remove primaries AND SECONDARIES. |
| 444 |
rm -f "$pf" "$sf" |
| 445 |
fi |
| 446 |
elif [ -f "$sf" ] && grep -Fq "$local" "$sf"; then |
| 447 |
grep -Fv "$local" "$sf" >"${sf}.new" |
| 448 |
mv "${sf}.new" "$sf" |
| 449 |
else |
| 450 |
echo "RTNETLINK answers: Cannot assign requested address" >&2 |
| 451 |
exit 254 |
| 452 |
fi |
| 453 |
} |
| 454 |
|
| 455 |
###################################################################### |
| 456 |
|
| 457 |
ip_rule() |
| 458 |
{ |
| 459 |
case "$1" in |
| 460 |
show | list | "") |
| 461 |
shift |
| 462 |
ip_rule_show "$@" |
| 463 |
;; |
| 464 |
add) |
| 465 |
shift |
| 466 |
ip_rule_add "$@" |
| 467 |
;; |
| 468 |
del*) |
| 469 |
shift |
| 470 |
ip_rule_del "$@" |
| 471 |
;; |
| 472 |
*) not_implemented "$1 in \"$orig_args\"" ;; |
| 473 |
esac |
| 474 |
|
| 475 |
} |
| 476 |
|
| 477 |
# All non-default rules are in $FAKE_IP_STATE_RULES/rules. As with |
| 478 |
# the real version, rules can be repeated. Deleting just deletes the |
| 479 |
# 1st match. |
| 480 |
|
| 481 |
ip_rule_show() |
| 482 |
{ |
| 483 |
if $brief; then |
| 484 |
not_implemented "ip -br rule show in \"$orig_args\"" |
| 485 |
fi |
| 486 |
|
| 487 |
ip_rule_show_1() |
| 488 |
{ |
| 489 |
_pre="$1" |
| 490 |
_table="$2" |
| 491 |
_selectors="$3" |
| 492 |
# potentially more options |
| 493 |
|
| 494 |
printf "%d:\t%s lookup %s \n" "$_pre" "$_selectors" "$_table" |
| 495 |
} |
| 496 |
|
| 497 |
ip_rule_show_some() |
| 498 |
{ |
| 499 |
_min="$1" |
| 500 |
_max="$2" |
| 501 |
|
| 502 |
[ -f "${FAKE_IP_STATE}/rules" ] || return |
| 503 |
|
| 504 |
while read -r _pre _table _selectors; do |
| 505 |
# Only print those in range |
| 506 |
if [ "$_min" -le "$_pre" ] && |
| 507 |
[ "$_pre" -le "$_max" ]; then |
| 508 |
ip_rule_show_1 "$_pre" "$_table" "$_selectors" |
| 509 |
fi |
| 510 |
done <"${FAKE_IP_STATE}/rules" |
| 511 |
} |
| 512 |
|
| 513 |
ip_rule_show_1 0 "local" "from all" |
| 514 |
|
| 515 |
ip_rule_show_some 1 32765 |
| 516 |
|
| 517 |
ip_rule_show_1 32766 "main" "from all" |
| 518 |
ip_rule_show_1 32767 "default" "from all" |
| 519 |
|
| 520 |
ip_rule_show_some 32768 2147483648 |
| 521 |
} |
| 522 |
|
| 523 |
ip_rule_common() |
| 524 |
{ |
| 525 |
_from="" |
| 526 |
_pre="" |
| 527 |
_table="" |
| 528 |
while [ -n "$1" ]; do |
| 529 |
case "$1" in |
| 530 |
from) |
| 531 |
_from="$2" |
| 532 |
shift 2 |
| 533 |
;; |
| 534 |
pref) |
| 535 |
_pre="$2" |
| 536 |
shift 2 |
| 537 |
;; |
| 538 |
table) |
| 539 |
_table="$2" |
| 540 |
shift 2 |
| 541 |
;; |
| 542 |
*) not_implemented "$1 in \"$orig_args\"" ;; |
| 543 |
esac |
| 544 |
done |
| 545 |
|
| 546 |
[ -n "$_pre" ] || not_implemented "ip rule without \"pref\"" |
| 547 |
ip_check_table "rule" |
| 548 |
# Relax this if more selectors added later... |
| 549 |
[ -n "$_from" ] || not_implemented "ip rule without \"from\"" |
| 550 |
} |
| 551 |
|
| 552 |
ip_rule_add() |
| 553 |
{ |
| 554 |
ip_rule_common "$@" |
| 555 |
|
| 556 |
_f="${FAKE_IP_STATE}/rules" |
| 557 |
touch "$_f" |
| 558 |
( |
| 559 |
flock 0 |
| 560 |
# Filter order must be consistent with the comparison in ip_rule_del() |
| 561 |
echo "$_pre $_table${_from:+ from }$_from" >>"$_f" |
| 562 |
) <"$_f" |
| 563 |
} |
| 564 |
|
| 565 |
ip_rule_del() |
| 566 |
{ |
| 567 |
ip_rule_common "$@" |
| 568 |
|
| 569 |
_f="${FAKE_IP_STATE}/rules" |
| 570 |
touch "$_f" |
| 571 |
# ShellCheck doesn't understand this flock pattern |
| 572 |
# shellcheck disable=SC2094 |
| 573 |
( |
| 574 |
flock 0 |
| 575 |
_tmp="${_f}.new" |
| 576 |
: >"$_tmp" |
| 577 |
_found=false |
| 578 |
while read -r _p _t _s; do |
| 579 |
if ! $_found && |
| 580 |
[ "$_p" = "$_pre" ] && [ "$_t" = "$_table" ] && |
| 581 |
[ "$_s" = "${_from:+from }$_from" ]; then |
| 582 |
# Found. Skip this one but not future ones. |
| 583 |
_found=true |
| 584 |
else |
| 585 |
echo "$_p $_t $_s" >>"$_tmp" |
| 586 |
fi |
| 587 |
done |
| 588 |
if cmp -s "$_tmp" "$_f"; then |
| 589 |
# No changes, must not have found what we wanted to delete |
| 590 |
echo "RTNETLINK answers: No such file or directory" >&2 |
| 591 |
rm -f "$_tmp" |
| 592 |
exit 2 |
| 593 |
else |
| 594 |
mv "$_tmp" "$_f" |
| 595 |
fi |
| 596 |
) <"$_f" || exit $? |
| 597 |
} |
| 598 |
|
| 599 |
###################################################################### |
| 600 |
|
| 601 |
ip_route() |
| 602 |
{ |
| 603 |
case "$1" in |
| 604 |
show | list) |
| 605 |
shift |
| 606 |
ip_route_show "$@" |
| 607 |
;; |
| 608 |
flush) |
| 609 |
shift |
| 610 |
ip_route_flush "$@" |
| 611 |
;; |
| 612 |
add) |
| 613 |
shift |
| 614 |
ip_route_add "$@" |
| 615 |
;; |
| 616 |
del*) |
| 617 |
shift |
| 618 |
ip_route_del "$@" |
| 619 |
;; |
| 620 |
*) not_implemented "$1 in \"ip route\"" ;; |
| 621 |
esac |
| 622 |
} |
| 623 |
|
| 624 |
ip_route_common() |
| 625 |
{ |
| 626 |
if [ "$1" = table ]; then |
| 627 |
_table="$2" |
| 628 |
shift 2 |
| 629 |
fi |
| 630 |
|
| 631 |
ip_check_table "route" |
| 632 |
} |
| 633 |
|
| 634 |
# Routes are in a file per table in the directory |
| 635 |
# $FAKE_IP_STATE/routes. These routes just use the table ID |
| 636 |
# that is passed and don't do any lookup. This could be "improved" if |
| 637 |
# necessary. |
| 638 |
|
| 639 |
ip_route_show() |
| 640 |
{ |
| 641 |
ip_route_common "$@" |
| 642 |
|
| 643 |
# Missing file is just an empty table |
| 644 |
sort "$FAKE_IP_STATE/routes/${_table}" 2>/dev/null || true |
| 645 |
} |
| 646 |
|
| 647 |
ip_route_flush() |
| 648 |
{ |
| 649 |
ip_route_common "$@" |
| 650 |
|
| 651 |
rm -f "$FAKE_IP_STATE/routes/${_table}" |
| 652 |
} |
| 653 |
|
| 654 |
ip_route_add() |
| 655 |
{ |
| 656 |
_prefix="" |
| 657 |
_dev="" |
| 658 |
_gw="" |
| 659 |
_table="" |
| 660 |
_metric="" |
| 661 |
|
| 662 |
while [ -n "$1" ]; do |
| 663 |
case "$1" in |
| 664 |
*.*.*.*/* | *.*.*.*) |
| 665 |
_prefix="$1" |
| 666 |
shift 1 |
| 667 |
;; |
| 668 |
local) |
| 669 |
_prefix="$2" |
| 670 |
shift 2 |
| 671 |
;; |
| 672 |
dev) |
| 673 |
_dev="$2" |
| 674 |
shift 2 |
| 675 |
;; |
| 676 |
via) |
| 677 |
_gw="$2" |
| 678 |
shift 2 |
| 679 |
;; |
| 680 |
table) |
| 681 |
_table="$2" |
| 682 |
shift 2 |
| 683 |
;; |
| 684 |
metric) |
| 685 |
_metric="$2" |
| 686 |
shift 2 |
| 687 |
;; |
| 688 |
*) not_implemented "$1 in \"$orig_args\"" ;; |
| 689 |
esac |
| 690 |
done |
| 691 |
|
| 692 |
ip_check_table "route" |
| 693 |
[ -n "$_prefix" ] || not_implemented "ip route without inet prefix in \"$orig_args\"" |
| 694 |
# This can't be easily deduced, so print some garbage. |
| 695 |
[ -n "$_dev" ] || _dev="ethXXX" |
| 696 |
|
| 697 |
# Alias or add missing bits |
| 698 |
case "$_prefix" in |
| 699 |
0.0.0.0/0) _prefix="default" ;; |
| 700 |
*/*) : ;; |
| 701 |
*) _prefix="${_prefix}/32" ;; |
| 702 |
esac |
| 703 |
|
| 704 |
_f="$FAKE_IP_STATE/routes/${_table}" |
| 705 |
mkdir -p "$FAKE_IP_STATE/routes" |
| 706 |
touch "$_f" |
| 707 |
|
| 708 |
# Check for duplicate |
| 709 |
_prefix_regexp=$(echo "^${_prefix}" | sed -e 's@\.@\\.@g') |
| 710 |
if [ -n "$_metric" ]; then |
| 711 |
_prefix_regexp="${_prefix_regexp} .*metric ${_metric} " |
| 712 |
fi |
| 713 |
if grep -q "$_prefix_regexp" "$_f"; then |
| 714 |
echo "RTNETLINK answers: File exists" >&2 |
| 715 |
exit 1 |
| 716 |
fi |
| 717 |
|
| 718 |
( |
| 719 |
flock 0 |
| 720 |
|
| 721 |
_out="${_prefix} " |
| 722 |
[ -z "$_gw" ] || _out="${_out}via ${_gw} " |
| 723 |
[ -z "$_dev" ] || _out="${_out}dev ${_dev} " |
| 724 |
[ -n "$_gw" ] || _out="${_out} scope link " |
| 725 |
[ -z "$_metric" ] || _out="${_out} metric ${_metric} " |
| 726 |
echo "$_out" >>"$_f" |
| 727 |
) <"$_f" |
| 728 |
} |
| 729 |
|
| 730 |
ip_route_del() |
| 731 |
{ |
| 732 |
_prefix="" |
| 733 |
_dev="" |
| 734 |
_gw="" |
| 735 |
_table="" |
| 736 |
_metric="" |
| 737 |
|
| 738 |
while [ -n "$1" ]; do |
| 739 |
case "$1" in |
| 740 |
*.*.*.*/* | *.*.*.*) |
| 741 |
_prefix="$1" |
| 742 |
shift 1 |
| 743 |
;; |
| 744 |
local) |
| 745 |
_prefix="$2" |
| 746 |
shift 2 |
| 747 |
;; |
| 748 |
dev) |
| 749 |
_dev="$2" |
| 750 |
shift 2 |
| 751 |
;; |
| 752 |
via) |
| 753 |
_gw="$2" |
| 754 |
shift 2 |
| 755 |
;; |
| 756 |
table) |
| 757 |
_table="$2" |
| 758 |
shift 2 |
| 759 |
;; |
| 760 |
metric) |
| 761 |
_metric="$2" |
| 762 |
shift 2 |
| 763 |
;; |
| 764 |
*) not_implemented "$1 in \"$orig_args\"" ;; |
| 765 |
esac |
| 766 |
done |
| 767 |
|
| 768 |
ip_check_table "route" |
| 769 |
[ -n "$_prefix" ] || not_implemented "ip route without inet prefix in \"$orig_args\"" |
| 770 |
# This can't be easily deduced, so print some garbage. |
| 771 |
[ -n "$_dev" ] || _dev="ethXXX" |
| 772 |
|
| 773 |
# Alias or add missing bits |
| 774 |
case "$_prefix" in |
| 775 |
0.0.0.0/0) _prefix="default" ;; |
| 776 |
*/*) : ;; |
| 777 |
*) _prefix="${_prefix}/32" ;; |
| 778 |
esac |
| 779 |
|
| 780 |
_f="$FAKE_IP_STATE/routes/${_table}" |
| 781 |
mkdir -p "$FAKE_IP_STATE/routes" |
| 782 |
touch "$_f" |
| 783 |
|
| 784 |
# ShellCheck doesn't understand this flock pattern |
| 785 |
# shellcheck disable=SC2094 |
| 786 |
( |
| 787 |
flock 0 |
| 788 |
|
| 789 |
# Escape some dots |
| 790 |
[ -z "$_gw" ] || _gw=$(echo "$_gw" | sed -e 's@\.@\\.@g') |
| 791 |
_prefix=$(echo "$_prefix" | sed -e 's@\.@\\.@g' -e 's@/@\\/@') |
| 792 |
|
| 793 |
_re="^${_prefix}\>.*" |
| 794 |
[ -z "$_gw" ] || _re="${_re}\<via ${_gw}\>.*" |
| 795 |
[ -z "$_dev" ] || _re="${_re}\<dev ${_dev}\>.*" |
| 796 |
[ -z "$_metric" ] || _re="${_re}.*\<metric ${_metric}\>.*" |
| 797 |
sed -i -e "/${_re}/d" "$_f" |
| 798 |
) <"$_f" |
| 799 |
} |
| 800 |
|
| 801 |
###################################################################### |
| 802 |
|
| 803 |
orig_args="$*" |
| 804 |
|
| 805 |
brief=false |
| 806 |
case "$1" in |
| 807 |
-br*) |
| 808 |
brief=true |
| 809 |
shift |
| 810 |
;; |
| 811 |
esac |
| 812 |
|
| 813 |
case "$1" in |
| 814 |
link) |
| 815 |
shift |
| 816 |
ip_link "$@" |
| 817 |
;; |
| 818 |
addr*) |
| 819 |
shift |
| 820 |
ip_addr "$@" |
| 821 |
;; |
| 822 |
rule) |
| 823 |
shift |
| 824 |
ip_rule "$@" |
| 825 |
;; |
| 826 |
route) |
| 827 |
shift |
| 828 |
ip_route "$@" |
| 829 |
;; |
| 830 |
*) not_implemented "$1" ;; |
| 831 |
esac |
| 832 |
|
| 833 |
exit 0 |