ipv6_addrs_IF aliases in rc.conf(5)

Kimmo Paasiala kpaasial at gmail.com
Fri Dec 21 03:43:42 UTC 2012


On Thu, Dec 20, 2012 at 3:27 PM, Jilles Tjoelker <jilles at stack.nl> wrote:
> On Thu, Dec 20, 2012 at 01:04:34PM +0200, Kimmo Paasiala wrote:
>> A question related to this for those who have been doing work on the
>> rc(8) scripts. Can I assume that /usr/bin is available when
>> network.subr functions are used? Doing calculations on hexadecimal
>> numbers is going to be very awkward if I can't use for example bc(1).
>
> You cannot assume that /usr/bin is available when setting up the
> network. It may be that /usr is mounted via NFS.
>
> You can use hexadecimal numbers (prefixed with 0x) in $((...))
> expressions. In FreeBSD 9.0 or newer, sh has a printf builtin you can
> use; in older versions you can use hexdigit and hexprint from
> network.subr.
>
> --
> Jilles Tjoelker

Thanks, I've rewitten my patch to support ranges. It is attached in
this message.

Again it's against a very recent 9-STABLE, I still haven't found time
to see if it applies to CURRENT.

It does allow you to do crazy stuff like

ipv6_addrs_re0="2001:db8:1111:2222::1-ffff/64"

However I didn't find anything to limit the number of aliases in the
ipv4 version of the function either.

Please test it :)


Then a question about the PR
(http://www.freebsd.org/cgi/query-pr.cgi?pr=174225) I wrote, how can I
attach this new patch to it? The submit follow up -button fires up my
email client and I'm not so sure how to submit a new patch for the PR
in an email in such a way that it appears properly formatted in the
PR.

Regards,

Kimmo Paasiala
-------------- next part --------------
Index: network.subr
===================================================================
--- network.subr	(revision 244523)
+++ network.subr	(working copy)
@@ -562,6 +562,7 @@
 	fi
 
 	ifalias_up ${_if} inet6 && _ret=0
+	ipv6_addrs_common ${_if} alias && _ret=0
 	ipv6_prefix_hostid_addr_common ${_if} alias && _ret=0
 	ipv6_accept_rtadv_up ${_if} && _ret=0
 
@@ -684,6 +685,49 @@
 	return $_ret
 }
 
+
+ipv6_addrs_common()
+{
+	local _ret _if _action _ip6prefix _ip6prefixes
+	local _ip6addr _prefixlen
+	local _range _ip6net _ip6low _ip6high
+	_ret=1
+	_if=$1
+	_action=$2
+
+# get the prefixes from ipv6_addrs_IF variable
+	_ip6prefixes=`get_if_var $_if ipv6_addrs_IF`
+	for _ip6prefix in ${_ip6prefixes}; do
+		_ip6addr=${_ip6prefix%%/*}
+		_prefixlen=${_ip6prefix##*/}
+		_range=${_ip6addr##*:}
+		_ip6net=${_ip6addr%:*}
+		_ip6low=${_range%-*}
+		_ip6high=${_range#*-}
+
+# If deleting an alias, set _prefixlen to null string.
+		if [ "${_action}" = "-alias" ]; then
+			_prefixlen=""
+		else
+			_prefixlen="prefixlen $_prefixlen"
+		fi
+
+		_ip6high=$(("0x${_ip6high}"))
+		_ip6count=$(("0x${_ip6low}"))
+		while [ "${_ip6count}" -le "${_ip6high}"  ]; do
+            # Re-uses the _ip6addr variable from above
+			_ip6addr=$(printf "%x" "${_ip6count}")
+			eval "ifconfig ${_if} inet6 ${_ip6net}:${_ip6addr} ${_prefixlen} ${_action}"
+			_ip6count=$((${_ip6count}+1))
+			_ret=0
+		done
+	done
+
+	return $_ret
+}
+
+
+
 # ifalias_up if af
 #	Configure aliases for network interface $if.
 #	It returns 0 if at least one alias was configured or


More information about the freebsd-stable mailing list