userPing tried to ping dstIP and if it succeeds, injects pingResPkt into the tundev. It's used in userspace/netstack mode when we don't have kernel support or raw socket access. As such, this does the dumbest thing that can work: runs the ping command. It's not super efficient, so it bounds the num
(dstIP netip.Addr, pingResPkt []byte, direction userPingDirection)
| 1357 | // TODO(bradfitz): when we're running on Windows as the system user, use |
| 1358 | // raw socket APIs instead of ping child processes. |
| 1359 | func (ns *Impl) userPing(dstIP netip.Addr, pingResPkt []byte, direction userPingDirection) { |
| 1360 | if !userPingSem.TryAcquire() { |
| 1361 | return |
| 1362 | } |
| 1363 | defer userPingSem.Release() |
| 1364 | |
| 1365 | t0 := time.Now() |
| 1366 | err := ns.sendOutboundUserPing(dstIP, 3*time.Second) |
| 1367 | d := time.Since(t0) |
| 1368 | if err != nil { |
| 1369 | if d < time.Second/2 { |
| 1370 | // If it failed quicker than the 3 second |
| 1371 | // timeout we gave above (500 ms is a |
| 1372 | // reasonable threshold), then assume the ping |
| 1373 | // failed for problems finding/running |
| 1374 | // ping. We don't want to log if the host is |
| 1375 | // just down. |
| 1376 | ns.logf("exec ping of %v failed in %v: %v", dstIP, d, err) |
| 1377 | } |
| 1378 | return |
| 1379 | } |
| 1380 | if debugNetstack() { |
| 1381 | ns.logf("exec pinged %v in %v", dstIP, time.Since(t0)) |
| 1382 | } |
| 1383 | if direction == userPingDirectionOutbound { |
| 1384 | if err := ns.tundev.InjectOutbound(pingResPkt); err != nil { |
| 1385 | ns.logf("InjectOutbound ping response: %v", err) |
| 1386 | } |
| 1387 | } else if direction == userPingDirectionInbound { |
| 1388 | if err := ns.tundev.InjectInboundCopy(pingResPkt); err != nil { |
| 1389 | ns.logf("InjectInboundCopy ping response: %v", err) |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | // injectInbound is installed as a packet hook on the 'inbound' (from a |
| 1395 | // WireGuard peer) path. Returning filter.Accept releases the packet to |
no test coverage detected