Hello t...@!

Here's a diff for tun to make it keep record and display the
pid of tunnel owner process in ifconfig.
I'm sure it would be a handy feature, hope you'll like it.

It looks like this:
tun1: flags=9843<UP,BROADCAST,RUNNING,SIMPLEX,LINK0,MULTICAST> mtu 1500
        lladdr 00:bd:8e:68:8e:02
        description: vpn
        priority: 0
        tun: opened (pid 26212)
        groups: tun
        media: Ethernet autoselect
        status: active
        inet 10.252.253.2 netmask 0xffffff00 broadcast 10.252.253.255

tun(4) diff:

Index: if_tun.c
===================================================================
RCS file: /cvs/src/sys/net/if_tun.c,v
retrieving revision 1.100
diff -u -p -r1.100 if_tun.c
--- if_tun.c    9 Nov 2009 17:53:39 -0000       1.100
+++ if_tun.c    4 Dec 2009 07:28:44 -0000
@@ -88,16 +88,17 @@
 #include <net/if_tun.h>
 
 struct tun_softc {
-       struct arpcom   arpcom;         /* ethernet common data */
-       struct selinfo  tun_rsel;       /* read select */
-       struct selinfo  tun_wsel;       /* write select (not used) */
+       struct arpcom    arpcom;        /* ethernet common data */
+       struct selinfo   tun_rsel;      /* read select */
+       struct selinfo   tun_wsel;      /* write select (not used) */
        LIST_ENTRY(tun_softc) tun_list; /* all tunnel interfaces */
-       struct ifmedia  tun_media;
-       int             tun_unit;
-       uid_t           tun_siguid;     /* uid for process that set tun_pgid */
-       uid_t           tun_sigeuid;    /* euid for process that set tun_pgid */
-       pid_t           tun_pgid;       /* the process group - if any */
-       u_short         tun_flags;      /* misc flags */
+       struct ifmedia   tun_media;
+       int              tun_unit;
+       uid_t            tun_siguid;    /* uid for process that set tun_pgid */
+       uid_t            tun_sigeuid;   /* euid for process that set tun_pgid */
+       pid_t            tun_pgid;      /* the process group - if any */
+       u_short          tun_flags;     /* misc flags */
+       struct proc     *tun_proc;      /* tunnel owner process */
 #define tun_if arpcom.ac_if
 };
 
@@ -177,6 +178,7 @@ tun_create(struct if_clone *ifc, int uni
 
        tp->tun_unit = unit;
        tp->tun_flags = TUN_INITED|TUN_STAYUP;
+       tp->tun_proc = NULL;
 
        /* generate fake MAC address: 00 bd xx xx xx unit_no */
        tp->arpcom.ac_enaddr[0] = 0x00;
@@ -372,6 +374,7 @@ tunopen(dev_t dev, int flag, int mode, s
        s = splnet();
        ifp->if_flags |= IFF_RUNNING;
        tun_link_state(tp);
+       tp->tun_proc = p;
        if_up(ifp);
        splx(s);
 
@@ -395,6 +398,7 @@ tunclose(dev_t dev, int flag, int mode, 
 
        ifp = &tp->tun_if;
        tp->tun_flags &= ~(TUN_OPEN|TUN_NBIO|TUN_ASYNC);
+       tp->tun_proc = NULL;
 
        /*
         * junk all pending output
@@ -483,6 +487,7 @@ tun_ioctl(struct ifnet *ifp, u_long cmd,
 {
        struct tun_softc        *tp = (struct tun_softc *)(ifp->if_softc);
        struct ifreq            *ifr = (struct ifreq *)data;
+       struct tunreq           *tunr = (struct tunreq *)data;
        int                      error = 0, s;
 
        s = splnet();
@@ -561,6 +566,15 @@ tun_ioctl(struct ifnet *ifp, u_long cmd,
        case SIOCGIFMEDIA:
        case SIOCSIFMEDIA:
                error = ifmedia_ioctl(ifp, ifr, &tp->tun_media, cmd);
+               break;
+
+       case SIOCGTUNPID:
+               if (tp->tun_proc == NULL) {
+                       error = ENOTTY;
+                       break;
+               }
+
+               tunr->tun_pid = tp->tun_proc->p_pid;
                break;
        default:
                if (tp->tun_flags & TUN_LAYER2)
Index: if_tun.h
===================================================================
RCS file: /cvs/src/sys/net/if_tun.h,v
retrieving revision 1.15
diff -u -p -r1.15 if_tun.h
--- if_tun.h    6 Feb 2007 10:49:40 -0000       1.15
+++ if_tun.h    4 Dec 2009 07:28:44 -0000
@@ -30,6 +30,7 @@
 #define _NET_IF_TUN_H_
 
 #include <sys/ioccom.h>
+#include <net/if.h>
 
 #define        TUN_OPEN        0x0001
 #define        TUN_INITED      0x0002
@@ -67,5 +68,14 @@ struct tuninfo {
 /* ioctl's for get/set debug */
 #define        TUNSDEBUG       _IOW('t', 94, int)
 #define        TUNGDEBUG       _IOR('t', 95, int)
+
+/* ioctl request */
+struct tunreq {
+       char     tun_ifname[IFNAMSIZ];
+       pid_t    tun_pid;
+};
+
+/* socket ioctl to get pid of owner process */
+#define        SIOCGTUNPID     _IOWR('i', 140, struct tunreq)
 
 #endif /* _NET_IF_TUN_H_ */


ifconfig(8) diff:

Index: ifconfig.c
===================================================================
RCS file: /cvs/src/sbin/ifconfig/ifconfig.c,v
retrieving revision 1.223
diff -u -p -r1.223 ifconfig.c
--- ifconfig.c  22 Nov 2009 22:00:24 -0000      1.223
+++ ifconfig.c  4 Dec 2009 07:30:48 -0000
@@ -84,6 +84,7 @@
 #include <net/if_trunk.h>
 #include <net/if_sppp.h>
 #include <net/ppp_defs.h>
+#include <net/if_tun.h>
 
 #include <netatalk/at.h>
 
@@ -248,6 +249,8 @@ void        setpflow_sender(const char *, int);
 void   unsetpflow_sender(const char *, int);
 void   setpflow_receiver(const char *, int);
 void   unsetpflow_receiver(const char *, int);
+
+void   tun_status(void);
 #endif
 
 /*
@@ -2714,6 +2717,7 @@ status(int link, struct sockaddr_dl *sdl
        trunk_status();
        mpe_status();
        pflow_status();
+       tun_status();
 #endif
        getifgroups();
 
@@ -4760,5 +4764,19 @@ setinstance(const char *id, int param)
        ifr.ifr_rdomainid = rdomainid;
        if (ioctl(s, SIOCSIFRTABLEID, (caddr_t)&ifr) < 0)
                warn("SIOCSIFRTABLEID");
+}
+#endif
+
+#ifndef SMALL
+void
+tun_status(void)
+{
+       struct tunreq    tr;
+
+       bzero(&tr, sizeof(tr));
+       strlcpy(tr.tun_ifname, name, sizeof(tr.tun_ifname));
+
+       if (ioctl(s, SIOCGTUNPID, &tr) != -1)
+               printf("\ttun: opened (pid %d)\n", tr.tun_pid);
 }
 #endif

Reply via email to