@@ -76,6 +76,7 @@ type gtunnel struct {
7676 ctx context.Context
7777 stack * stack.Stack // a tcpip stack
7878 ep netstack.SeamlessEndpoint // endpoint for the stack
79+ sid * core.Volatile [int ] // session id (almost always tunnel fd)
7980 hdl netstack.GConnHandler // tcp, udp, and icmp handlers
8081 pcapio * pcapsink // pcap output, if any
8182 closed atomic.Bool // open/close?
@@ -207,6 +208,7 @@ func NewGTunnel(pctx context.Context, fd, mtu int, l3 string, hdl netstack.GConn
207208 ctx : pctx ,
208209 stack : stack ,
209210 ep : ep ,
211+ sid : core .NewVolatile (fd ), // fd is the og tun device
210212 hdl : hdl ,
211213 pcapio : sink ,
212214 closed : atomic.Bool {},
@@ -258,9 +260,17 @@ func (t *gtunnel) Unlink() error {
258260 return t .ep .Dispose ()
259261}
260262
261- func (t * gtunnel ) SetLink (fd int ) error {
263+ func (t * gtunnel ) SetLink (fd int ) ( err error ) {
262264 defer core .Recover (core .Exit11 , "g.SetLink" )
263265
266+ defer func () {
267+ if err != nil {
268+ t .sid .Store (- 1 ) // reset sid
269+ } else {
270+ t .sid .Store (fd ) // set sid to fd
271+ }
272+ }()
273+
264274 dupfd , err := dup (fd ) // tunnel will own dupfd
265275 if err != nil {
266276 log .E ("tun: new link; err %v" , err )
@@ -293,6 +303,7 @@ func (t *gtunnel) Stat() (*x.NetStat, error) {
293303 st .TUNSt .Open = ! t .closed .Load ()
294304 st .TUNSt .Up = t .ep .IsAttached ()
295305 st .TUNSt .Fd = t .ep .Cur ()
306+ st .TUNSt .Sid = t .sid .Load () // session id (tunnel fd)
296307 st .TUNSt .Mtu = int32 (t .ep .MTU ())
297308 st .TUNSt .PcapMode = t .pcapio .mode ()
298309
0 commit comments