automaticaly add "tun" group when opening /dev/tunX device

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

automaticaly add "tun" group when opening /dev/tunX device

Mike Belopuhov
Hello.

There are some cases when creation tun(4) interface doesn't imply
adding created interface to the default interface group ("tun").
It occurs when interface is created via opening /dev/tunX device.
Such programs as ppp(8) operate this way. But tunopen() routine
(in /sys/net/if_tun.c) doesn't call if_groupadd() function to add
a group name. So why not fix it? :)

The patch below actually does two things:

 - adds if_addgroup() call _only_ when creating iface;
 - and a minor KNF "while there" ;)

Best regards.


Index: sys/net/if_tun.c
===================================================================
RCS file: /cvs/src/sys/net/if_tun.c,v
retrieving revision 1.68
diff -u -p -r1.68 if_tun.c
--- sys/net/if_tun.c 8 Jun 2005 06:53:32 -0000 1.68
+++ sys/net/if_tun.c 16 Nov 2005 01:03:09 -0000
@@ -317,10 +317,13 @@ tunopen(dev_t dev, int flag, int mode, s
 
  if ((tp = tun_lookup(minor(dev))) == NULL)
  return (ENXIO);
+
+ if ((error = if_addgroup(&tp->tun_if, "tun")) != 0)
+ return (error);
  }
 
  if (tp->tun_flags & TUN_OPEN)
- return EBUSY;
+ return (EBUSY);
 
  ifp = &tp->tun_if;
  tp->tun_flags |= TUN_OPEN;

Reply | Threaded
Open this post in threaded view
|

Re: automaticaly add "tun" group when opening /dev/tunX device

Henning Brauer-2
* Mike Belopuhov <[hidden email]> [2005-11-16 02:06]:
> There are some cases when creation tun(4) interface doesn't imply
> adding created interface to the default interface group ("tun").
> It occurs when interface is created via opening /dev/tunX device.

wow, just had a look, what a mess.

>  - adds if_addgroup() call _only_ when creating iface;

we actually need one more...

can you test this?

it doesn't seem like these created-upon-device-opening tuns get
destroyed on close, so no special handling needed there

Index: if_tun.c
===================================================================
RCS file: /cvs/src/sys/net/if_tun.c,v
retrieving revision 1.69
diff -u -p -r1.69 if_tun.c
--- if_tun.c 16 Nov 2005 10:45:33 -0000 1.69
+++ if_tun.c 16 Nov 2005 10:54:18 -0000
@@ -317,6 +317,10 @@ tunopen(dev_t dev, int flag, int mode, s
 
  if ((tp = tun_lookup(minor(dev))) == NULL)
  return (ENXIO);
+
+ if ((error = if_addgroup(&tp->tun_if, "tun")) != 0 ||
+    (error = if_addgroup(&tp->tun_if, IFG_ALL)) != 0)
+ return (error);
  }
 
  if (tp->tun_flags & TUN_OPEN)

Reply | Threaded
Open this post in threaded view
|

Re: automaticaly add "tun" group when opening /dev/tunX device

Henning Brauer-2
actually, if the diff below would work, that would be even better,
since it removes the special casing. can you test that instead?

Index: if_tun.c
===================================================================
RCS file: /cvs/src/sys/net/if_tun.c,v
retrieving revision 1.69
diff -u -p -r1.69 if_tun.c
--- if_tun.c 16 Nov 2005 10:45:33 -0000 1.69
+++ if_tun.c 16 Nov 2005 11:03:30 -0000
@@ -313,7 +313,11 @@ tunopen(dev_t dev, int flag, int mode, s
  return (error);
 
  if ((tp = tun_lookup(minor(dev))) == NULL) { /* create on demand */
- tun_clone_create(&tun_cloner, minor(dev));
+ char xname[IFNAMSIZ];
+
+ snprintf(xname, sizeof(xname), "%s%d", "tun", minor(dev));
+ if ((error = if_clone_create(xname)) != 0)
+ return (error);
 
  if ((tp = tun_lookup(minor(dev))) == NULL)
  return (ENXIO);