special-case code for OpenBSD - advice needed
Hi,
while working on "make IPv6 payload work on Win32", I found something quite peculiar for OpenBSD in the OpenVPN code.
In "init.c", do_open_tun(), calls two other functions from tun.c for tunnel setup:
open_tun() --> find free /dev/tun<n> device, open() it, thereby
activating a "tun<n>" network interface, store the device name in "struct tuntap", etc.
and
do_ifconfig() --> run "ifconfig tun<n> 1.2.3.4" system commands
(basically consists of a ton of
#if defined(TARGET_nnn) ... #else blocks, containing the system specific variations of "ifconfig" commands.
Now, for all operatings systems *except* Win32 and OpenBSD, the sequence of execution is
open_tun()
do_ifconfig()
and for the named two systems, it's
do_ifconfig()
open_tun()
This is controlled by the inline_function "ifconfig_order()" in tun.h, which returns IFCONFIG_BEFORE_TUN_OPEN or IFCONFIG_AFTER_TUN_OPEN:
---------- quote ----------
/*
* Should ifconfig be called before or after
* tun dev open?
*/
#define IFCONFIG_BEFORE_TUN_OPEN 0
#define IFCONFIG_AFTER_TUN_OPEN 1
#define IFCONFIG_DEFAULT IFCONFIG_AFTER_TUN_OPEN
static inline intifconfig_order(void)
{
#if defined(TARGET_LINUX)
return IFCONFIG_AFTER_TUN_OPEN;
#elif defined(TARGET_SOLARIS)
return IFCONFIG_AFTER_TUN_OPEN;
#elif defined(TARGET_OPENBSD)
return IFCONFIG_BEFORE_TUN_OPEN;
#elif defined(TARGET_DARWIN)
return IFCONFIG_AFTER_TUN_OPEN;
#elif defined(TARGET_NETBSD)
return IFCONFIG_AFTER_TUN_OPEN;
#elif defined(WIN32)
return IFCONFIG_BEFORE_TUN_OPEN;
#else
return IFCONFIG_DEFAULT;
#endif
}
---------- quote ----------
I am ready to accept that Windows is weird, and needs special-casing of stuff, but the OpenBSD case baffles me - it's a BSD after all, and all other BSDs do it the other way round.
Now, reading through the code, OpenBSD, NetBSD and Darwin (MacOS X) are actually very similar - that is, tun devices are persistent, and you need to do "ifconfig tun<n> destroy" before doing the target ifconfig, otherwise you can have "leftover" configuration garbage there. Still, NetBSD and Darwin do "IFCONFIG_AFTER_TUN_OPEN".
So...
Question #1: why is OpenBSD treated differently? Does anyone on this list know why this is so, and whether it needs to be kept that way? (I have no OpenBSD system to test on, right now).
Question #2: (more a style question) - why is this function so convoluted, if all the operating systems default to the default anyway - so why not change it to:
static inline intifconfig_order(void)
{
#if defined(TARGET_OPENBSD)
return IFCONFIG_BEFORE_TUN_OPEN;
#elif defined(WIN32)
return IFCONFIG_BEFORE_TUN_OPEN;
#else
return IFCONFIG_AFTER_TUN_OPEN;
#endif
}
I'm purposely not sending a patch right now, because I don't understand the reason for the code being what it is.
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...Quoted 20 lines · Gert Doering
Hi,
while working on "make IPv6 payload work on Win32", I found something quite peculiar for OpenBSD in the OpenVPN code.
Now, for all operatings systems *except* Win32 and OpenBSD, the sequence
of execution is
open_tun()
do_ifconfig()and for the named two systems, it's
do_ifconfig()
open_tun()
Quoted 5 lines
Question #1: why is OpenBSD treated differently? Does anyone on this list
know why this is so, and whether it needs to be kept that way? (I have no
OpenBSD system to test on, right now).
Random comments....
On OpenBSD 4.6 stable "man 4 tun" says:
A tun interface can be created at runtime using the ifconfig tunN
create
command or by opening the character special device /dev/tunN.
Both layer 3 and layer 2 tunneling is supported. Layer 3
tunneling is
the default mode; to enable layer 2 tunneling mode the link0 flag
needs
to be set with ifconfig(8), or by setting up a hostname.if(5)
configura-
tion file for netstart(8). In layer 2 mode the tun interface is
simulat-
ing an Ethernet network interface.
So, you should not need to do the ifconfig at all unless you're interested in tap functionality or there's other odd frobbing going on.
I can't speak for older releases but all the old man pages are on the OpenBSD.org site.
Karl <[email protected]...>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein
Hi,
Quoted 3 lines · Karl O. Pinc
So, you should not need to do the ifconfig at all unless you're interested in tap functionality or there's other odd frobbing going on.
You need ifconfig to set an IP address :-) - which might be considered "odd frobbing", but people seem to value it somehow ;-)
In addition to that, on OpenBSD and NetBSD, Tunnel interfaces stay around even after /dev/tun is closed, so if you want to be sure that no old configuration lingers around and messes with your tunnel, the "safe" approach is to do "ifconfig tun0 destroy ; ifconfig tun0 create" before IP/IPv6 configuration.
*That* part is easily understood - the interesting question is why OpenVPN does do_ifconfig/open_tun for OpenBSD and open_tun/do_ifconfig for NetBSD.
(As a side note: are you running an OpenBSD system and could test the OpenVPN IPv6 payload stuff there?)
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...Quoted 10 lines · Gert Doering
Hi,
Quoted 3 lines · Karl O. Pinc
So, you should not need to do the ifconfig at all unless you're interested in tap functionality or there's other odd frobbing going on.
You need ifconfig to set an IP address :-) - which might be considered
"odd frobbing", but people seem to value it somehow ;-)
:)
Quoted 7 lines
*That* part is easily understood - the interesting question is why OpenVPN
does do_ifconfig/open_tun for OpenBSD and open_tun/do_ifconfig for NetBSD.(As a side note: are you running an OpenBSD system and could test the OpenVPN IPv6 payload stuff there?)
I am, but there's no IPV6 going on so unless there's a test suite to run (somehow) I couldn't test. It would probably also take me a while to get to it because at the moment all my OpenBSD boxes are in production.
Karl <[email protected]...>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein
Hi Gert,
Quoted 5 lines · On Sunday 28 February 2010 15:50:01 Gert Doering wrote:
Now, for all operatings systems *except* Win32 and OpenBSD, the sequence of execution is
open_tun()
do_ifconfig()
That seems to make sense for OpenBSD. I just had a colleague (kudos Moritz) try to connect with ifconfig_order() changed to
static inline int
ifconfig_order(void)
{
#if defined(WIN32)
return IFCONFIG_BEFORE_TUN_OPEN;
#else
return IFCONFIG_AFTER_TUN_OPEN;
#endif
}
and it broke the tunnel.
Regards
Heiko
Signature · 7 lines
Heiko Hund | Software Engineer | Phone +49-721-25516-237 | Fax -200 Astaro AG | An der RaumFabrik 33a | 76227 Karlsruhe | Germany Executive Board: Markus Hennig, Jan Hichert, Günter Junk, Dr. Frank Nellissen Chairman of the Supervisory Board: Stanley J. Krasnow Headquarter Location: Karlsruhe Commercial Register: Mannheim HRB 108997
Hi,
Quoted 13 lines · Heiko Hund
Quoted 5 lines · On Sunday 28 February 2010 15:50:01 Gert Doering wrote:
Now, for all operatings systems *except* Win32 and OpenBSD, the sequence of execution is
open_tun()
do_ifconfig()That seems to make sense for OpenBSD. I just had a colleague (kudos Moritz) try to connect with
ifconfig_order()changed tostatic inline int
ifconfig_order(void)
{
Quoted 8 lines
#if defined(WIN32) return IFCONFIG_BEFORE_TUN_OPEN; #else return IFCONFIG_AFTER_TUN_OPEN;#endif
}and it broke the tunnel.
OK, this is interesting, and thanks a lot for testing.
What happened exactly? Could you ask your colleague for a log file?
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...Hi Gert,
Quoted 1 line · On Friday 05 March 2010 10:11:51 Gert Doering wrote:
What happened exactly? Could you ask your colleague for a log file?
Well, he couldn't ping any remote host. Nothing special in the log, really. If it isn't misleading it's quite obvious that the ordering is wrong:
TUN/TAP device /dev/tun1 opened
/sbin/ifconfig tun1 destroy
/sbin/ifconfig tun1 create
NOTE: Tried to delete pre-existing tun/tap instance --No Problem if failure /sbin/ifconfig tun1 10.1.1.18 10.1.1.17 mtu 1500 netmask 255.255.255.255 up
Heiko
Signature · 7 lines
Heiko Hund | Software Engineer | Phone +49-721-25516-237 | Fax -200 Astaro AG | An der RaumFabrik 33a | 76227 Karlsruhe | Germany Executive Board: Markus Hennig, Jan Hichert, Günter Junk, Dr. Frank Nellissen Chairman of the Supervisory Board: Stanley J. Krasnow Headquarter Location: Karlsruhe Commercial Register: Mannheim HRB 108997
Hi,
Quoted 11 lines · Heiko Hund
Quoted 1 line · On Friday 05 March 2010 10:11:51 Gert Doering wrote:
What happened exactly? Could you ask your colleague for a log file?
Well, he couldn't ping any remote host. Nothing special in the log, really. If it isn't misleading it's quite obvious that the ordering is wrong:
TUN/TAP device
/dev/tun1opened
/sbin/ifconfigtun1 destroy
/sbin/ifconfigtun1 create
NOTE: Tried to delete pre-existing tun/tap instance --No Problem if failure/sbin/ifconfigtun1 10.1.1.18 10.1.1.17 mtu 1500 netmask 255.255.255.255 up
Well, this is exactly what the current code for NetBSD does... (and how this thread got started). On NetBSD, it works.
The problem is that if you do it the other way round, you have a big fat race condition here - between "I assume that my tun device will be tun1, so I do 'ifconfig tun1 destroy' now" and actually opening /dev/tun1, some other process might grab this tun device.
open() first will exclusively lock it for you...
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...Quoted 31 lines · Gert Doering
Hi,
Quoted 2 lines · Heiko Hund
Quoted 1 line · On Friday 05 March 2010 10:11:51 Gert Doering wrote:
What happened exactly? Could you ask your colleague for a log
file?
Quoted 2 lines
Well, he couldn't ping any remote host. Nothing special in the log,
really. If
Quoted 6 lines
it isn't misleading it's quite obvious that the ordering is wrong:
TUN/TAP device
/dev/tun1opened
/sbin/ifconfigtun1 destroy
/sbin/ifconfigtun1 create
NOTE: Tried to delete pre-existing tun/tap instance --No Problemif failure
Quoted 1 line
/sbin/ifconfigtun1 10.1.1.18 10.1.1.17 mtu 1500 netmask255.255.255.255 up
Well, this is exactly what the current code for NetBSD does... (and how
this thread got started). On NetBSD, it works.The problem is that if you do it the other way round, you have a big fat
race condition here - between "I assume that my tun device will be tun1,
so I do 'ifconfig tun1 destroy' now" and actually opening/dev/tun1, some other process might grab this tun device.
open()first will exclusively lock it for you...
Hi,
Quoted 15 lines · Karl O. Pinc
Quoted 1 line
open()first will exclusively lock it for you...From OpenBSD's tun(4) man page:
Each device has the exclusive open property; it cannot be opened if it is
already open and in use by another process.
...
On the last close of the device, all queued packets are discarded. If the device was created by opening/dev/tunN, it will be
automatically destroyed. Devices created via ifconfig(8) are only marked
as not running and traffic will be dropped returning EHOSTDOWN.
Quoted 8 lines
Which means to me that you want to omit the ifconfig destroy and create, thus:
TUN/TAP device /dev/tun1 opened with open(2) /* iterate increasing tunN number until you get other than * EBUSY (the device was already opened), or suchlike searching */ /sbin/ifconfig tun1 10.1.1.18 10.1.1.17 mtu 1500 netmask \ 255.255.255.255 up
This is what other platforms do, e.g. FreeBSD and Linux (on close, auto-destroy - and if you want persistance, you need to do something special).
Maybe this was changed between older OpenBSD versions and the current versions?
Given this information, I will prepare a patch that will handle OpenBSD the same way as FreeBSD is done - run open_tun() first, do_ifconfig() second, and do away with the "ifconfig destroy" logic. I'll send the patch to the list for testing.
gert
Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
//www.muc.de/~gert/
Gert Doering - Munich, Germany [email protected]...
fax: +49-89-35655025 [email protected]...