Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 637394 | Differences between
and this patch

Collapse All | Expand All

(-)a/net/iproute2.sh (-18 / +95 lines)
Lines 204-209 _add_address() Link Here
204
	return $rc
204
	return $rc
205
}
205
}
206
206
207
_filter_metric()
208
{
209
	# ip route show does not allow to filter by metric
210
	# so we need to filter metric manually
211
212
	local metric_value="$1"
213
	shift
214
215
	if [ "${metric_value}" -eq 0 ]; then
216
		# metric 0 is not displayed
217
		ip -o "$@" | fgrep -vsq " metric "
218
	else
219
		# add trailing space for matching
220
		echo "$(ip -o "$@") " | fgrep -sq " metric ${metric_value} "
221
	fi
222
}
223
224
_is_route_type()
225
{
226
	case "$1" in
227
		unicast|multicast|local|broadcast|throw|unreachable|prohibit|blackhole) true ;;
228
		*) false ;;
229
	esac
230
}
231
207
_add_route()
232
_add_route()
208
{
233
{
209
	local family=
234
	local family=
Lines 219-259 _add_route() Link Here
219
		shift
244
		shift
220
	fi
245
	fi
221
246
222
	if [ $# -eq 3 ]; then
247
	if _is_route_type "$2"; then
248
		:
249
	elif [ $# -eq 3 ]; then
223
		set -- "$1" "$2" via "$3"
250
		set -- "$1" "$2" via "$3"
224
	elif [ "$3" = "gw" ]; then
251
	elif [ "$3" = "gw" ]; then
225
		local one=$1 two=$2
252
		local one="$1" two="$2"
226
		shift; shift; shift
253
		shift; shift; shift
227
		set -- "${one}" "${two}" via "$@"
254
		set -- "${one}" "${two}" via "$@"
228
	fi
255
	fi
229
256
230
	local cmd_add= cmd_show= have_metric=false
257
	local cmd_prefix= cmd_add= cmd_show= metric_value= table_value=
258
        local have_metric=false have_table=false have_prefix=false nodev=false
231
	while [ -n "$1" ]; do
259
	while [ -n "$1" ]; do
232
		case "$1" in
260
		case "$1" in
233
			metric) metric=$2 ; cmd_add="${cmd_add} metric $2" ; shift ; have_metric=true ;;
261
			metric|preference)
234
			netmask) x="/$(_netmask2cidr "$2")" ; cmd_add="${cmd_add}${x}" ; cmd_show="${cmd_show}${x}" ; shift;;
262
				metric="$2"
263
				cmd_prefix="${cmd_prefix} ${1} ${2}"
264
				metric_value="$2"
265
				have_metric=true
266
				shift ;;
267
			netmask)
268
				x="/$(_netmask2cidr "$2")"
269
				cmd_prefix="${cmd_prefix}${x}"
270
				cmd_show="${cmd_show}${x}"
271
				have_prefix=true
272
				shift ;;
273
			tos)
274
				cmd_add="${cmd_add} ${1} ${2}"
275
				cmd_show="${cmd_show} ${1} ${2}"
276
				shift ;;
277
			table)
278
				cmd_add="${cmd_add} ${1} ${2}"
279
				cmd_show="${cmd_show} ${1} ${2}"
280
				have_table=true
281
				shift ;;
282
			unicast|multicast)
283
				cmd_prefix="${cmd_prefix} ${1}"
284
				;;
285
			local|broadcast)
286
				# those are added to table local by default
287
				cmd_prefix="${cmd_prefix} ${1}"
288
				table_value="local"
289
				;;
290
			throw|unreachable|prohibit|blackhole)
291
				cmd_prefix="${cmd_prefix} ${1}"
292
				nodev=true
293
				;;
235
			-host|-net);;
294
			-host|-net);;
236
			*) cmd_add="${cmd_add} ${1}" ; cmd_show="${cmd_show} ${1}" ;;
295
			*)
296
				if ${have_prefix}; then
297
					cmd_add="${cmd_add} ${1}"
298
				else
299
					cmd_prefix="${cmd_prefix} ${1}"
300
					cmd_show="${cmd_show} ${1}"
301
					have_prefix=true
302
				fi ;;
237
		esac
303
		esac
238
		shift
304
		shift
239
	done
305
	done
240
306
241
	# We cannot use a metric if we're using a nexthop
307
	# Add table for special types
242
	if ! ${have_metric} && \
308
	if ! ${have_table} && [ -n "${table_value}" ]; then
243
		[ -n "${metric}" -a \
309
		cmd_show="${cmd_show} table ${table_value}"
244
			"${cmd_add##* nexthop }" = "${cmd_add}" ]
310
	fi
245
	then
311
246
		cmd_add="${cmd_add} metric ${metric}"
312
	# Add interface's metric by default
313
	if ! ${have_metric} && [ -n "${metric}" ]; then
314
		cmd_prefix="${cmd_prefix} metric ${metric}"
315
		metric_value="${metric}"
247
	fi
316
	fi
248
317
249
	# Check for route already existing:
318
	# Check for route already existing:
250
	ip ${family} route show ${cmd_show} dev "${IFACE}" 2>/dev/null | \
319
	_filter_metric "${metric_value}" ${family} route show ${cmd_show} 2>/dev/null
251
		fgrep -sq "${cmd_add%% *}"
252
	route_already_exists=$?
320
	route_already_exists=$?
253
321
254
	veinfo ip ${family} route append ${cmd_add} dev "${IFACE}"
322
	cmd_add="${cmd_prefix}${cmd_add}"
255
	ip ${family} route append ${cmd_add} dev "${IFACE}"
323
256
	rc=$?
324
	if yesno "${nodev}"; then
325
		veinfo ip ${family} route append ${cmd_add}
326
		ip ${family} route append ${cmd_add}
327
		rc=$?
328
	else
329
		veinfo ip ${family} route append ${cmd_add} dev "${IFACE}"
330
		ip ${family} route append ${cmd_add} dev "${IFACE}"
331
		rc=$?
332
	fi
333
257
	# Check return code in some cases
334
	# Check return code in some cases
258
	if [ $rc -ne 0 ]; then
335
	if [ $rc -ne 0 ]; then
259
		# If the route already exists, our default behavior is to WARN but continue.
336
		# If the route already exists, our default behavior is to WARN but continue.
Lines 268-274 _add_route() Link Here
268
				*) msgfunc=eerror rc=1 ; eerror "Unknown error behavior: $eh_behavior" ;;
345
				*) msgfunc=eerror rc=1 ; eerror "Unknown error behavior: $eh_behavior" ;;
269
			esac
346
			esac
270
			eval $msgfunc "Route '$cmd_show' already existed:"
347
			eval $msgfunc "Route '$cmd_show' already existed:"
271
			eval $msgfunc \"$(ip $family route show ${cmd_show} dev "${IFACE}" 2>&1)\"
348
			eval $msgfunc "\"$(_filter_metric "${metric_value}" $family route show ${cmd_show} 2>&1)\""
272
		else
349
		else
273
			: # TODO: Handle other errors
350
			: # TODO: Handle other errors
274
		fi
351
		fi

Return to bug 637394