| 1 | #!/bin/sh
|
|---|
| 2 | #
|
|---|
| 3 | # This file is part of the FreeWRT project. FreeWRT is copyrighted
|
|---|
| 4 | # material, please see the LICENCE file in the top-level directory
|
|---|
| 5 | # or at http://www.freewrt.org/licence for details.
|
|---|
| 6 | #
|
|---|
| 7 | # Christian Fischer <spaetzle@freewrt.org>
|
|---|
| 8 | #
|
|---|
| 9 |
|
|---|
| 10 | built_in() {
|
|---|
| 11 | local what=$1
|
|---|
| 12 | local alias=$(alias $what)
|
|---|
| 13 | unalias $what
|
|---|
| 14 | alias $(echo $alias|sed 's/'\''//g')
|
|---|
| 15 | shift
|
|---|
| 16 | $what $@
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | config() {
|
|---|
| 20 | [ x"$1" != x"0" ]
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | __ifupdown() {
|
|---|
| 24 | local environ=$(cat $ENVFILE)
|
|---|
| 25 |
|
|---|
| 26 | IFUPDOWN_ENV=$(echo -n $IFUPDOWN_ENV|sed 's/\(MDENT=\)[^ ]*[[:space:]]*//g';\
|
|---|
| 27 | echo " PARENT_IFACE_TYPE=$IFACE_TYPE"; \
|
|---|
| 28 | echo " PARENT_IFACE=$IFACE"; \
|
|---|
| 29 | echo " MDENT=$MDENT") \
|
|---|
| 30 | busybox "$@"
|
|---|
| 31 |
|
|---|
| 32 | retval=$?
|
|---|
| 33 | echo $environ > $ENVFILE
|
|---|
| 34 | return $retval
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | __exit() {
|
|---|
| 38 | [ x"$1" != x"0" ] && echo "RT_ERR=1" >> $ENVFILE
|
|---|
| 39 | exit $1
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | iface_exists() {
|
|---|
| 43 | grep -q "${1:-$IFACE}:" /proc/net/dev
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | is_up() {
|
|---|
| 47 | local iface=${1:-$IFACE}
|
|---|
| 48 |
|
|---|
| 49 | if iface_exists $iface
|
|---|
| 50 | then
|
|---|
| 51 | ip a|grep ${iface}:[[:space:]]|grep -q UP && return
|
|---|
| 52 |
|
|---|
| 53 | ### ifupdown netlink bug workaround ###
|
|---|
| 54 | # suppress "ip: RTNETLINK answers: File exists" message in the case of
|
|---|
| 55 | # iface is down but an ip address is assigned
|
|---|
| 56 | ip a|grep ${iface}$|grep -q inet && ip addr flush dev $iface >/dev/null 2>&1
|
|---|
| 57 | ### end workaround ###
|
|---|
| 58 | fi
|
|---|
| 59 | return 1
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | main_env_update() {
|
|---|
| 63 | local t_mdent lastmatch=
|
|---|
| 64 |
|
|---|
| 65 | # get submode from calling hook script
|
|---|
| 66 | SUBMODE=$(echo ${0%/*} |sed '{ s/\(.*\/\)*//; s/\.d$//}')
|
|---|
| 67 |
|
|---|
| 68 | # create env file
|
|---|
| 69 | if ! [ -f "$ENVFILE" ]
|
|---|
| 70 | then
|
|---|
| 71 | [ -d ${ENVFILE%/*} ] || mkdir -p ${ENVFILE%/*}
|
|---|
| 72 | echo "MDENT=0" >> $ENVFILE
|
|---|
| 73 | echo "LASTLOG=0" >> $ENVFILE
|
|---|
| 74 |
|
|---|
| 75 | is_up && IFACE_STATE="up" || IFACE_STATE="down"
|
|---|
| 76 | echo "IFACE_STATE=$IFACE_STATE" >> $ENVFILE
|
|---|
| 77 |
|
|---|
| 78 | if [ x"$IFACE" != x"lo" ]
|
|---|
| 79 | then
|
|---|
| 80 | # search for existing lib files end evaluate iface type by using the appropriate
|
|---|
| 81 | # function from lib file
|
|---|
| 82 | for lib in ${LIBDIR}/iftypes/*
|
|---|
| 83 | do
|
|---|
| 84 | [ -f $lib ] && (. $lib; iface_type 2>&-) || continue
|
|---|
| 85 | if [ -n "$lastmatch" ]
|
|---|
| 86 | then
|
|---|
| 87 | if [ ${lib##*/} = ${lastmatch##*/} ]
|
|---|
| 88 | then
|
|---|
| 89 | mprint -s "Warning: $lib overlays $lastmatch"
|
|---|
| 90 | else
|
|---|
| 91 | mprint -s "Error: iface type $lib overlays $lastmatch"
|
|---|
| 92 | fi
|
|---|
| 93 | fi
|
|---|
| 94 | lastmatch=$lib
|
|---|
| 95 | done
|
|---|
| 96 | fi
|
|---|
| 97 |
|
|---|
| 98 | # if iface type isn't evaluated type "iface" is default
|
|---|
| 99 | [ -n "$lastmatch" ] && IFACE_TYPE=${lastmatch##*/} || IFACE_TYPE="iface"
|
|---|
| 100 |
|
|---|
| 101 | # update env
|
|---|
| 102 | echo "IFACE_TYPE=${IFACE_TYPE}" >> $ENVFILE
|
|---|
| 103 | fi
|
|---|
| 104 |
|
|---|
| 105 | . $ENVFILE
|
|---|
| 106 | t_mdent=$MDENT
|
|---|
| 107 |
|
|---|
| 108 | # expand $IFUPDOWN_ENV
|
|---|
| 109 | for var in $IFUPDOWN_ENV; do
|
|---|
| 110 | eval $var
|
|---|
| 111 | done
|
|---|
| 112 |
|
|---|
| 113 | [ $t_mdent -gt $MDENT ] && MDENT=$t_mdent
|
|---|
| 114 | main_pos_update
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | main_pos_update() {
|
|---|
| 118 | [ x"$MDENT" = x"0" ] && STARTCOL= || STARTCOL="[$(expr ${MDENT:-0} \* ${TAB})C"
|
|---|
| 119 | sed 's/MDENT=[0-9]*/'MDENT=$MDENT'/' -i $ENVFILE
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | main_preup() {
|
|---|
| 123 | config $RT_PREUP_PRINTING_OFF || mstart
|
|---|
| 124 |
|
|---|
| 125 | if ! config $RT_PREUP_IFUP_CHECK_OFF && is_up
|
|---|
| 126 | then
|
|---|
| 127 | if ! config $RT_PREUP_PRINTING_OFF
|
|---|
| 128 | then
|
|---|
| 129 | mup
|
|---|
| 130 | mstate 1
|
|---|
| 131 | minfo "Interface already up"
|
|---|
| 132 | fi
|
|---|
| 133 | exit 1
|
|---|
| 134 | fi
|
|---|
| 135 |
|
|---|
| 136 | if_preup 2>&-
|
|---|
| 137 | retval=$?
|
|---|
| 138 |
|
|---|
| 139 | [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {
|
|---|
| 140 | fail_preup 2>&- || exit 1
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | config $RT_PREUP_PRINTING_OFF && return
|
|---|
| 144 | mup
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | main_up() {
|
|---|
| 148 | [ x"$METHOD" = x"manual" ] && ip link set up dev $IFACE
|
|---|
| 149 |
|
|---|
| 150 | if ! config $RT_UP_IFUP_CHECK_OFF
|
|---|
| 151 | then
|
|---|
| 152 | is_up
|
|---|
| 153 | retval=$?
|
|---|
| 154 | config $RT_UP_PRINTING_OFF || mstate $retval
|
|---|
| 155 | [ x"$retval" != x"0" ] && exit 1
|
|---|
| 156 | fi
|
|---|
| 157 |
|
|---|
| 158 | if_up 2>&-
|
|---|
| 159 | retval=$?
|
|---|
| 160 |
|
|---|
| 161 | [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {
|
|---|
| 162 | fail_up 2>&- || exit 1
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | if [ -d /tmp/ifupdown/pcode/${IFACE} ]
|
|---|
| 166 | then
|
|---|
| 167 | for dir in /tmp/ifupdown/pcode/${IFACE}/*
|
|---|
| 168 | do
|
|---|
| 169 | [ -f ${dir}/postup ] || continue
|
|---|
| 170 | . ${dir}/postup
|
|---|
| 171 | done
|
|---|
| 172 | fi
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | main_down() {
|
|---|
| 176 | config $RT_DOWN_PRINTING_OFF || mstop
|
|---|
| 177 |
|
|---|
| 178 | if [ -d /tmp/ifupdown/pcode/${IFACE} ]
|
|---|
| 179 | then
|
|---|
| 180 | for dir in /tmp/ifupdown/pcode/${IFACE}/*
|
|---|
| 181 | do
|
|---|
| 182 | [ -f ${dir}/predown ] || continue
|
|---|
| 183 | . ${dir}/predown
|
|---|
| 184 | done
|
|---|
| 185 | fi
|
|---|
| 186 |
|
|---|
| 187 | if_down 2>&-
|
|---|
| 188 | retval=$?
|
|---|
| 189 |
|
|---|
| 190 | [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {
|
|---|
| 191 | fail_down 2>&- || exit 1
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | config $RT_DOWN_PRINTING_OFF && return
|
|---|
| 195 | mdown
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | main_postdown() {
|
|---|
| 199 | [ x"$METHOD" = x"manual" ] && {
|
|---|
| 200 | ip addr flush dev %ifac
|
|---|
| 201 | ip link set down dev $IFACE
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | if ! config $RT_POSTDOWN_IFDOWN_CHECK_OFF
|
|---|
| 205 | then
|
|---|
| 206 | if [ $IFACE_STATE = "down" ]
|
|---|
| 207 | then
|
|---|
| 208 | if ! config $RT_POSTDOWN_PRINTING_OFF
|
|---|
| 209 | then
|
|---|
| 210 | mdown
|
|---|
| 211 | mstate 1
|
|---|
| 212 | minfo "Interface already down"
|
|---|
| 213 | fi
|
|---|
| 214 | else
|
|---|
| 215 | is_up && retval=1 || retval=0
|
|---|
| 216 | config $RT_POSTDOWN_PRINTING_OFF || mstate $retval
|
|---|
| 217 | [ "$retval" != "0" ] && exit 1
|
|---|
| 218 | fi
|
|---|
| 219 | fi
|
|---|
| 220 |
|
|---|
| 221 | if_postdown 2>&-
|
|---|
| 222 | retval=$?
|
|---|
| 223 |
|
|---|
| 224 | [ x"$retval" != x"0" -a x"$retval" != x"127" ] && {
|
|---|
| 225 | fail_postdown 2>&- || exit 1
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | # pcode cleanup
|
|---|
| 229 | rm -rf /tmp/ifupdown/pcode/*/$IFACE 2>&- 1>&-
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | main_exec_dirhooks() {
|
|---|
| 233 | for hook in ${LIBDIR}/${1}/${2}
|
|---|
| 234 | do
|
|---|
| 235 | [ -f $hook ] || continue
|
|---|
| 236 | . $hook
|
|---|
| 237 | eval $3 2>&-
|
|---|
| 238 | retval=$?
|
|---|
| 239 | [ x"$retval" != x"0" -a x"$retval" != x"127" ] && exit 1
|
|---|
| 240 | return
|
|---|
| 241 | done
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | main_exec_plugins() {
|
|---|
| 245 | for plugin in ${LIBDIR}/plugins/*; do
|
|---|
| 246 | [ -f $plugin ] || continue
|
|---|
| 247 | . $plugin
|
|---|
| 248 | eval "${plugin##*/}_${1}" 2>&-
|
|---|
| 249 | retval=$?
|
|---|
| 250 | [ x"$retval" != x"0" -a x"$retval" != x"127" ] && exit 1
|
|---|
| 251 | done
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | main_exec_inlinehooks() {
|
|---|
| 255 | case $SUBMODE in
|
|---|
| 256 | if-pre-up)
|
|---|
| 257 | [ -n "$IF_PRE_UP" ] && eval $IF_PRE_UP
|
|---|
| 258 | ;;
|
|---|
| 259 | if-up)
|
|---|
| 260 | [ -n "$IF_UP" ] && eval $IF_UP
|
|---|
| 261 | ;;
|
|---|
| 262 | if-down)
|
|---|
| 263 | [ -n "$IF_DOWN" ] && eval $IF_DOWN
|
|---|
| 264 | ;;
|
|---|
| 265 | if-post-down)
|
|---|
| 266 | [ -n "$IF_POST_DOWN" ] && eval $IF_POST_DOWN
|
|---|
| 267 | ;;
|
|---|
| 268 | esac
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | ### main entry point ###
|
|---|
| 272 |
|
|---|
| 273 | FWIFUPDOWN_PRINTING_OFF=0
|
|---|
| 274 | FWIFUPDOWN_SYSLOG_OFF=0
|
|---|
| 275 | FWIFUPDOWN_FANCY=0
|
|---|
| 276 | FWIFUPDOWN_DEBUG=0
|
|---|
| 277 |
|
|---|
| 278 | . /etc/rc.conf
|
|---|
| 279 |
|
|---|
| 280 | config $FWIFUPDOWN_DEBUG && set -x
|
|---|
| 281 |
|
|---|
| 282 | RT_PREUP_PRINTING_OFF=0
|
|---|
| 283 | RT_UP_PRINTING_OFF=0
|
|---|
| 284 | RT_DOWN_PRINTING_OFF=0
|
|---|
| 285 | RT_POSTDOWN_PRINTING_OFF=0
|
|---|
| 286 | RT_PREUP_IFUP_CHECK_OFF=0
|
|---|
| 287 | RT_UP_IFUP_CHECK_OFF=0
|
|---|
| 288 | RT_POSTDOWN_IFDOWN_CHECK_OFF=0
|
|---|
| 289 | RT_BB_NOEXEC=0
|
|---|
| 290 | RT_ERR=0
|
|---|
| 291 | readonly ENVFILE="/tmp/ifupdown/env"
|
|---|
| 292 | readonly LIBDIR="/etc/network"
|
|---|
| 293 | IFACE_TYPE="none"
|
|---|
| 294 |
|
|---|
| 295 | alias exit="__exit"
|
|---|
| 296 | alias ifup="__ifupdown ifup"
|
|---|
| 297 | alias ifdown="__ifupdown ifdown"
|
|---|
| 298 | [ -z "$KSH_VERSION" ] || alias which='whence -p'
|
|---|
| 299 |
|
|---|
| 300 | if ! [ -f /etc/network/mfunctions.sh ]
|
|---|
| 301 | then
|
|---|
| 302 | logger -t ifupdown "/etc/network/mfunctions.sh not found"
|
|---|
| 303 | exit 1
|
|---|
| 304 | fi
|
|---|
| 305 | . /etc/network/mfunctions.sh
|
|---|
| 306 |
|
|---|
| 307 | main_env_update
|
|---|
| 308 |
|
|---|
| 309 | config $RT_ERR && built_in exit 1
|
|---|
| 310 |
|
|---|
| 311 | if [ x"$IFACE_TYPE" != x"iface" ]
|
|---|
| 312 | then
|
|---|
| 313 | if [ -f "${LIBDIR}/iftypes/${IFACE_TYPE}" ]
|
|---|
| 314 | then
|
|---|
| 315 | . ${LIBDIR}/iftypes/$IFACE_TYPE
|
|---|
| 316 | else
|
|---|
| 317 | mprint -s "Error: libfile $IFACE_TYPE not found"
|
|---|
| 318 | fi
|
|---|
| 319 | fi
|
|---|
| 320 |
|
|---|
| 321 | case $SUBMODE in
|
|---|
| 322 | if-pre-up)
|
|---|
| 323 | main_preup
|
|---|
| 324 | main_exec_dirhooks methods $METHOD method_preup
|
|---|
| 325 | main_exec_plugins preup
|
|---|
| 326 |
|
|---|
| 327 | # bypass execution of further busybox ifupdown.c code
|
|---|
| 328 | config $RT_BB_NOEXEC && built_in exit 1
|
|---|
| 329 | ;;
|
|---|
| 330 | if-up)
|
|---|
| 331 | main_exec_plugins up
|
|---|
| 332 | main_exec_dirhooks methods $METHOD method_up
|
|---|
| 333 | main_up
|
|---|
| 334 | ;;
|
|---|
| 335 | if-down)
|
|---|
| 336 | main_down
|
|---|
| 337 | main_exec_dirhooks methods $METHOD method_down
|
|---|
| 338 | main_exec_plugins down
|
|---|
| 339 |
|
|---|
| 340 | # bypass execution of further busybox ifupdown.c code
|
|---|
| 341 | config $RT_BB_NOEXEC && built_in exit 1
|
|---|
| 342 | ;;
|
|---|
| 343 | if-post-down)
|
|---|
| 344 | main_exec_plugins postdown
|
|---|
| 345 | main_exec_dirhooks methods $METHOD method_postdown
|
|---|
| 346 | main_postdown
|
|---|
| 347 | ;;
|
|---|
| 348 | esac
|
|---|
| 349 |
|
|---|
| 350 | exit 0
|
|---|
| 351 |
|
|---|
| 352 | # vim:ts=4
|
|---|