1#!/bin/mksh
2# $MirOS: src/sbin/route/route_print.sh,v 1.5 2009/10/19 20:43:49 tg Exp $
3#-
4# Copyright (c) 2005, 2009
5#	Thorsten "mirabilos" Glaser <tg@mirbsd.org>
6#
7# Licensee is hereby permitted to deal in this work without restric-
8# tion, including unlimited rights to use, publicly perform, modify,
9# merge, distribute, sell, give away or sublicence, provided all co-
10# pyright notices above, these terms and the disclaimer are retained
11# in all redistributions or reproduced in accompanying documentation
12# or other materials provided with binary redistributions.
13#
14# All advertising materials mentioning features or use of this soft-
15# ware must display the following acknowledgement:
16#	This product includes material provided by Thorsten Glaser.
17#
18# Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
19# express, or implied, to the maximum extent permitted by applicable
20# law, without malicious intent or gross negligence; in no event may
21# licensor, an author or contributor be held liable for any indirect
22# or other damage, or direct damage except proven a consequence of a
23# direct error of said person and intended use of this work, loss or
24# other issues arising in any way out of its use, even if advised of
25# the possibility of such damage or existence of a nontrivial bug.
26#-
27# Display a list of all manually added (static) routes which are not
28# set on the loopback interface, for IPv4 and IPv6.
29
30#-- Functions
31
32function reverse_route
33{
34	local proto=$1
35	if [[ $2 = */* ]]; then
36		local dest=${2%/*}
37		local cidr=${2##*/}
38	else
39		local dest=$2
40		local cidr=-
41	fi
42	local gw=$3
43	local flags=$4
44	local mtu=$5
45	local iface=$6
46
47	local cmd="/sbin/route -n add -$proto"
48	if [[ $flags = *H* ]]; then
49		cmd="$cmd -host"
50	else
51		cmd="$cmd -net"
52	fi
53	cmd="$cmd $dest"
54	[[ $cidr = - ]] || cmd="$cmd -prefixlen $cidr"
55	[[ $flags = *1* ]] && cmd="$cmd -proto1"
56	[[ $flags = *2* ]] && cmd="$cmd -proto3"
57	[[ $flags = *3* ]] && cmd="$cmd -proto3"
58	[[ $flags = *B* ]] && cmd="$cmd -blackhole"
59	[[ $flags = *C* ]] && cmd="$cmd -cloning"
60	[[ $flags = *G* ]] || cmd="$cmd -iface"
61	[[ $flags = *L* ]] && cmd="$cmd -llinfo"
62	[[ $flags = *R* ]] && cmd="$cmd -reject"
63	[[ $flags = *X* ]] && cmd="$cmd -xresolve"
64	[[ $mtu = - ]] || cmd="$cmd -mtu $mtu"
65	[[ $gw = - && $flags != *L* ]] \
66	    && cmd="#error: $cmd"
67	if [[ $flags = *L* ]]; then
68		cmd="$cmd -link ${iface}:"
69	else
70		cmd="$cmd $gw"
71	fi
72	print -- $cmd
73}
74
75#-- Entry Point
76
77tab=$(print \\t)
78disp=h
79imode=n
80q=0
81dynrt=0
82
83while getopts "dho:rq" opt; do
84	case $opt {
85	d)	dynrt=1
86		;;
87	# h = help, *) case
88	o)	imode=o
89		proto=$OPTARG
90		;;
91	r)	disp=r
92		;;
93	q)	q=1
94		;;
95	*)	print "Usage:\tmksh route_print [-d] | column -t"
96		print "\tmksh route_print -[d]r"
97		print "\tnetstat -rnvf inet[6] | fgrep ..." \
98		    "| mksh route_print -[dr]o inet[6]"
99		exit 1
100		;;
101	}
102done
103shift $((OPTIND - 1))
104
105[[ $disp = h && $q = 0 ]] && print Destination Gateway Flags MTU Interface
106case $imode {
107o)	/usr/bin/sed -e "s/^/$proto /"
108	;;
109n)	for proto in inet inet6; do
110		/usr/bin/netstat -rnvf $proto \
111		    | /usr/bin/tail +5 \
112		    | /usr/bin/sed -e "s/^/$proto /"
113	done
114	;;
115} \
116    | /usr/bin/fgrep -v "${tab}expire" \
117    | while read proto dest gw flags refs use mtu iface rn_dupedkey; do
118	if [[ $gw = U* ]]; then
119		# no gw (yet) in routing table
120		rn_dupedkey="$iface $rn_dupedkey"
121		iface=$mtu
122		mtu=$use
123		use=$refs
124		refs=$flags
125		flags=$gw
126		gw=-
127	fi
128	[[ $iface = lo0 ]] && continue
129	[[ $dynrt == 1 || $flags = *S* ]] || continue
130	case $disp {
131	h)	print -- $dest $gw $flags $mtu $iface
132		;;
133	r)	reverse_route $proto $dest $gw $flags $mtu $iface
134		;;
135	}
136done
137exit 0
138