Why udp port 53 traffic is captured?

Hi there,

I am using NEAppProxyProvider to filter a scope of port traffics. Am I am trying to capture all port traffic (except udp port 53) like below.

There are mainly two problems with below approach:
  1. Sometimes it works that can capture all traffic, but sometimes it cannot.

  2. All the time, udp 53 traffic is always captured in.

Is this a bug?


Code Block
private func buildRules(fullMode: Bool) -> [NENetworkRule] {
var hosts = [("", "")]
var rules: [NENetworkRule] = []
hosts = [("0.0.0.0", "0...52"),
("0.0.0.0", "54...65535"),]
for host in hosts {
let ep = NWHostEndpoint(hostname: host.0, port: host.1)
let rule = NENetworkRule.init(remoteNetwork: ep, remotePrefix: 0, localNetwork: nil, localPrefix: 0, protocol: .any, direction: .outbound)
rules.append(rule)
}
}
...
let settings = NETransparentProxyNetworkSettings.init(tunnelRemoteAddress: "127.0.0.1")
settings.includedNetworkRules = buildRules()
settings.excludedNetworkRules = nil
settings.dnsSettings = NEDNSSettings(servers: ["1.1.1.1", "1.0.0.1", "8.8.8.8", "8.8.4.4"])
setTunnelNetworkSettings(settings) { [unowned self] (error) in
...


Is this a bug?

I would log out the flows that you are capturing on port 53 and then log out a small set of network rules for esting purpose; for example: 0.0.0.0 : 52 and 0.0.0.0 : 54 and if they overlap that you are still getting port 53 traffic, then yes, I would open a bug report. Please send me the Feedback ID so I can also take a look at your logs and extended examples here.



Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Rules with only
Code Block
0.0.0.0 : 52 and 0.0.0.0 : 54

not going wrong, but it overlaps with rules like:
Code Block
("0.0.0.0", "0...52") and ("0.0.0.0", "54...65535")


but it overlaps with rules like:
("0.0.0.0", "0...52") and ("0.0.0.0", "54...65535")

This is odd. What do your logs show if you log out your traffic being claimed when the overlapping takes place? In other words, how do you know it's overlapping and traffic for port 53 is entering handleNewFlow?


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
I have put in place some traced from within handleNewFlow together with the rule setting:
("0.0.0.0", "0...52") and ("0.0.0.0", "54...65535")
And I can get below traces
Code Block
🤪FZ$--traffic handling udp flow to: 8.8.4.4:53 fzmacappproxy 04:16:22.468086+1000
🤪FZ$--traffic handling udp flow to: 1.1.1.1:53 fzmacappproxy 04:16:22.470011+1000
🤪FZ$--traffic handling udp flow to: 10.0.0.10:53 fzmacappproxy 04:16:22.471286+1000
🤪FZ$--traffic handling udp flow to: 10.0.0.10:53 fzmacappproxy 04:16:22.472517+1000
🤪FZ$--traffic handling udp flow to: 10.0.0.10:53 fzmacappproxy 04:16:22.473659+1000
🤪FZ$--traffic handling udp flow to: 10.0.0.10:53 fzmacappproxy 04:16:22.475028+1000


This is one thing.

The second thing is that with that rule set, sometimes it can capture all traffics, some times it seems blocked some where.
That can be verified by using netcat command sending out packets with the same subnet of en0.
If I filter with com.apple.nc on console app, with the command nc 192.168.0.100 8888, in good cases I can get below trace:
Code Block
(2281480580): New flow: NEFlow type = stream, app = com.apple.nc, name = , 192.168.0.183:0 <-> 192.168.0.100:8888, filter_id = , interface = en0 fzmacappproxy 08:16:13.368577+1000
[Extension com.familyzone.macappproxy.proxy]: Calling handleNewFlow with TCP com.apple.nc[{length = 20, bytes = 0xb98252db9f8d8bbe4c383cf2ce6374eb34e3b83a}] remote: 192.168.0.100:8888 interface en0 fzmacappproxy 08:16:17.992887+1000
🤪FZ$--traffic handling tcp flow TCP com.apple.nc[{length = 20, bytes = 0xb98252db9f8d8bbe4c383cf2ce6374eb34e3b83a}] remote: 192.168.0.100:8888 interface en0 fzmacappproxy 08:16:17.992990+1000
[Extension com.familyzone.macappproxy.proxy]: provider accepted new flow TCP com.apple.nc[{length = 20, bytes = 0xb98252db9f8d8bbe4c383cf2ce6374eb34e3b83a}] remote: 192.168.0.100:8888 interface en0 fzmacappproxy 08:16:17.993701+1000

But sometimes it only show the first line below without the rest, which means NE callback is not envoked for some reason.
Code Block
(95809956): New flow: NEFlow type = stream, app = com.apple.nc, name = , 192.168.0.183:0 <-> 192.168.0.100:8888, filter_id = , interface = en0 fzmacappproxy 08:19:05.315187+1000


Is it another bug?


🤪FZ$--traffic handling udp flow to: 8.8.4.4:53 fzmacappproxy 04:16:22.468086+1000
🤪FZ$--traffic handling udp flow to: 1.1.1.1:53 fzmacappproxy 04:16:22.470011+1000
🤪FZ$--traffic handling udp flow to: 10.0.0.10:53 fzmacappproxy 04:16:22.471286+1000

These flows looks like they are pointing to very specific DNS servers, are you also setting NEDNSSettings for domains? That would explain what you are seeing.

Regarding the second item:

But sometimes it only show the first line below without the rest, which means NE callback is not envoked for some reason.

Do you have a guard in the handleNewFlow method checking for a property of variable that is not set or nil?


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
There is a dns setting like below:
Code Block
settings.dnsSettings = NEDNSSettings(servers: ["1.1.1.1", "1.0.0.1", "8.8.8.8", "8.8.4.4"])

Commenting it out still see the port 53 traffic like below:
Code Block
🤪FZ$--traffic handling tcp flow TCP fc-system-service_darwin-amd64[{length = 20, bytes = 0xf2eb944db46831eb67c5114a4da698418011e2ad}] remote: 52.95.132.69:80 interface en0 fzmacappproxy 07:59:41.762307+1000
🤪FZ$--traffic handling udp flow to: 192.168.0.1:53 fzmacappproxy 07:59:41.989191+1000
🤪FZ$--traffic handling udp flow to: 192.168.0.1:53 fzmacappproxy 07:59:42.195246+1000
🤪FZ$--traffic handling udp flow to: 192.168.0.1:53 fzmacappproxy 07:59:42.196681+1000
🤪FZ$--traffic handling udp flow to: 192.168.0.1:53 fzmacappproxy 07:59:42.399203+1000
🤪FZ$--traffic handling udp flow to: fe80::1.53 fzmacappproxy 07:59:42.400754+1000
🤪FZ$--traffic handling udp flow to: fe80::1.53 fzmacappproxy 07:59:42.402079+1000
🤪FZ$--traffic handling udp flow to: 192.168.0.1:53 fzmacappproxy 07:59:42.609971+1000
🤪FZ$--traffic handling udp flow to: 192.168.0.1:53 fzmacappproxy 07:59:42.614274+1000
🤪FZ$--traffic handling udp flow to: 2001:4860:4860::8888.53 fzmacappproxy 07:59:53.037751+1000
🤪FZ$--traffic handling udp flow to: 10.0.0.10:53 fzmacappproxy 07:59:53.046197+1000
🤪FZ$--traffic handling udp flow to: 10.0.0.10:53 fzmacappproxy 07:59:53.048681+1000
🤪FZ$--traffic handling udp flow to: 1.1.1.1:53 fzmacappproxy 07:59:53.760442+1000
🤪FZ$--traffic handling udp flow to: 2001:4860:4860::8888.53 fzmacappproxy 07:59:55.095695+1000
🤪FZ$--traffic handling udp flow to: fe80::1.53 fzmacappproxy 07:59:56.042586+1000


handleNewFlow() is like below:
Code Block
override func handleNewFlow(_ flow: NEAppProxyFlow) -> Bool {
if let TCPFlow = flow as? NEAppProxyTCPFlow {
FZLog(.verbose, "traffic handling tcp flow \(TCPFlow.debugDescription)")
TCPFlow.open(withLocalEndpoint: nil) { [unowned self] (error) in
guard error == nil else {
FZLog(.error, "tcp flow open error: \(error.debugDescription)")
self.closeAppProxyFlow(flow: TCPFlow, error: error)
return
}
self.handleFlow(tcpflow: TCPFlow)
}
} else if let UDPFlow = flow as? NEAppProxyUDPFlow {
FZLog(.fatal, "UDP flow should not be captured \(UDPFlow.debugDescription) if handleNewUDPFlow is overridden")
} else {
FZLog(.fatal, "Should not capture something here \(flow.metaData.debugDescription)")
}
return true
}

checking for a property of variable that is not set or nil?

What property of variable here do you mean? Code example?

The port 53 traffic is odd if you have excluded it from your NENetworkRule's and removed NEDNSSettings.

The extra inconsistent logging that you are also see if odd based on your code snippet.

If you are unable to resolve this, open a TSI and I, or Quinn, can take a deeper look at these two specific things.


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
If I put in excluded rule with port 53, appproxy will keep recycling. Removing NEDNSSettings doesn't help with the two issues.

Raised a TSI case 767665663 only for udp 53 traffic issue. When I try to update with the other issue (handleNewFlow() sometimes not invoked) in the same scenario, couldn't find anywhere to edit my ticket. Do you know how to edit a TSI request?

Raised a TSI case 767665663 only for udp 53 traffic issue.

Thanks. I grabbed it and it's in my queue.

When I try to update with the other issue (handleNewFlow() sometimes not invoked) in the same scenario, couldn't find anywhere to edit my ticket. Do you know how to edit a TSI request?

Typically, if you respond on the TSI with more information, this should update the incident. However, I will remind you that a TSI is to cover one question, so if the port 53 traffic is the most important question, then let's focus on that.


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Ok. Will raise a new one for it (handleNewFlow() sometimes not invoked). Seems need to pay then.

Are you using Catalina or Big Sur for this Network Extension?
I am using BigSur 11.2.3

So if you do not want port 53 UDP traffic this leaves you with a few options; first you could try to >filter only on a specific set of addresses or ranges that are not the default route (0.0.0.0). For >example, here is one that I used for all 17.0.0.0 traffic on port 443:
settings.includedNetworkRules = [
    NENetworkRule.init(remoteNetwork: NWHostEndpoint(hostname: "17.0.0.0", port: "443"),
                                 remotePrefix: 8,
                                 localNetwork: nil,
                                 localPrefix: 0,
                                 protocol: .any,
                                 direction: .outbound)
]
Next, you could proxy this traffic and handle it just like your regular UDP flows.

Thanks for suggesting touching the default route. That might be the way to go.
Can you say something more about usage of remotePrefix?
What I understand is value 8 means taking care of the first eight bits of the ip address which is 17 in this example. Any other examples showing different cases?
Also, I tried below setup, but still see udp port 53 traffic captured.
Thanks in advance for any suggestion.

Code Block
var hosts = [("", "")]
var rules: [NENetworkRule] = []
for seg in 1...254 {
if seg != 127 {
hosts = [("\(seg).0.0.0", "0...52"),
("\(seg).0.0.0", "54...65535"),]
rule = NENetworkRule.init(remoteNetwork: ep, remotePrefix: 8, localNetwork: nil, localPrefix: 0, protocol: .UDP, direction: .outbound)
rules.append(rule)
}
}
}
let settings = NETransparentProxyNetworkSettings.init(tunnelRemoteAddress: "127.0.0.1")
settings.includedNetworkRules = rules
settings.excludedNetworkRules = nil


Any other examples showing different cases?

Sure. It looks like you are adopting UDP based flows for 1...254.0.0.0/8 based traffic. I would recommend testing this and other TCP based NENetworkRule's out so that you are letting the system handle anything coming through on 0.0.0.0 and your proxy then presumably claiming all other traffic.


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Following your suggestions, tested with below code capturing all port traffics:
Two things to mention:
  1. It is better than before: network keeps alive for a couple of hours before it dies.

Attached the log when it crashed.
2. still seen udp 53 traffic.

Code Block
for seg in 1...254 {
if seg != 127 {
hosts = [("\(seg).0.0.0", "0")]
for host in hosts {
let ep = NWHostEndpoint(hostname: host.0, port: host.1)
var rule = NENetworkRule.init(remoteNetwork: ep, remotePrefix: 8, localNetwork: nil, localPrefix: 0, protocol: .TCP, direction: .outbound)
rules.append(rule)
hosts = [("\(seg).0.0.0", "0...52"),
("\(seg).0.0.0", "54...65535"),]
rule = NENetworkRule.init(remoteNetwork: ep, remotePrefix: 8, localNetwork: nil, localPrefix: 0, protocol: .UDP, direction: .outbound)
rules.append(rule)
}
}
}
let settings = NETransparentProxyNetworkSettings.init(tunnelRemoteAddress: "127.0.0.1")
settings.includedNetworkRules = rules
settings.excludedNetworkRules = nil


Code Block
[full mode crash](https://developer.apple.com/forums/content/attachment/88570304-6dc8-4bcb-a54e-5df782fdebca){: .log-attachment}



network keeps alive for a couple of hours before it dies.

I cannot see your crash log. You can attach it to the response or you can attach it to the existing TSI.

2. still seen udp 53 traffic.

If you are still seeing port 53 traffic come through then I suspect it is because of one of two reasons; first, because your NENetworkRule's localNetwork is nil and is matching UDP flows with an arbitrary port that falls in between 54...65535 on the local endpoint because NEAppProxyUDPFlow's only have a localEndpoint property. Then port 53 datagram traffic comes through when the local flow is opened. Second, that you are matching the remote network on outbound TCP traffic for port 53 traffic because you are using a wildcard for TCP traffic.


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Attach the full mode crash log:



You encountered a segmentation fault when you tried to start a new connection.

Code Block
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000020
Exception Note: EXC_CORPSE_NOTIFY
Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [79060]
Thread 15 Crashed:: Dispatch queue: Network.framework
0 libobjc.A.dylib 0x7fff202ec9bf objc_release + 31
1 com.apple.Foundation 0x7fff212c420d NSKVOPendingNotificationRelease + 67
2 com.apple.CoreFoundation 0x7fff2051d512 __CFArrayReleaseValues + 477
3 com.apple.CoreFoundation 0x7fff205e38d7 _CFArrayReplaceValues + 365
4 com.apple.Foundation 0x7fff2136e294 NSKeyValuePopPendingNotificationPerThread + 267
5 com.apple.Foundation 0x7fff2136dd01 NSKeyValueDidChange + 107
6 com.apple.Foundation 0x7fff2140e539 NSKeyValueDidChangeWithPerThreadPendingNotifications + 146
7 com.apple.Network 0x7fff2641cda6 __21-[NWConnection start]_block_invoke.38 + 422 <----- SEG FAULT
8 libdispatch.dylib 0x7fff202b01d5 _dispatch_block_async_invoke2 + 83
9 libdispatch.dylib 0x7fff202a37c7 _dispatch_client_callout + 8
10 libdispatch.dylib 0x7fff202a95fe _dispatch_lane_serial_drain + 606
11 libdispatch.dylib 0x7fff202aa0fe _dispatch_lane_invoke + 426
12 libdispatch.dylib 0x7fff202b3c5d _dispatch_workloop_worker_thread + 819 (
13 libsystem_pthread.dylib 0x7fff2044b499 _pthread_wqthread + 314
14 libsystem_pthread.dylib 0x00007fff2044a467 start_wqthread + 15

Take a look at this part of your code as well as any KVO surrounding this logic.


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
Thanks for the good point.

To rule out whether it is tcp or udp that caused the crash, I would like to have a try with NWConnection rather than NWUDPSession (I am currently in use).

The question is:
If I want to try NWConnection for udp flow handle (with .udp option), how to handle the multi datagrams with connection.send?

Code Block
udpflow.readDatagrams(completionHandler: { [unowned self] (datagrams: [Data]?, endpoints: [NWEndpoint]?, readError) in


Confirmed that only by doing all udp traffic capturing like below will cause network crash.

Would like to try NWConnection .udp rather than NWUDPSession currently in use.
Do you have any sample code for using NWconnection together with NEAppProxyUDPFlow?

Code Block
for seg in 1...254 {
if seg != 127 {
hosts = [("\(seg).0.0.0", "0")]
for host in hosts {
let ep = NWHostEndpoint(hostname: host.0, port: host.1)
let rule = NENetworkRule.init(remoteNetwork: ep, remotePrefix: 8, localNetwork: nil, localPrefix: 0, protocol: .UDP, direction: .outbound)
rules.append(rule)
}
}
}


Sorry for the possible off topic, but I have noticed that the port defined in the rules as "0...52" and "54...65535". Is this range notation legal at all? If so, I was unable to find any reference to it in the documentation. Can
you please confirm?
Thanks.
@richard_wang

If I want to try NWConnection for udp flow handle (with .udp option), how to handle the multi datagrams with connection.send?

I recommend that you create a new managing class for each one of your outbound datagrams and then use NWConnection inside of that.

Do you have any sample code for using NWconnection together with NEAppProxyUDPFlow?

No.

@SerusMac

I have noticed that the port defined in the rules as "0...52" and "54...65535". Is this range notation legal at all?

This is something on my to-do list to research. I've tested it and it does seem to work, but just because it works does not mean it is valid and thus why I have not called it out one way or the other. (Usually I would call it out right away.) I will keep this thread updated as I find out more.


Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
As confirmed that by only capturing all udp traffic can cause the network crash. Tried different approaches around udp, but all failed. That include exclude reserved ip segments for local network, exclude multicast segments, only include 1.0.0.0/8 to 127.0.0.0/8 for udp traffic.

So far, I do believe that if there is any way to exclude udp 53 traffic, should get resolved. But as you see, I am using none zero default route as suggested, but udp 53 packets still comes in. Furthermore, as you side noted that " you cannot filter localNetwork traffic with NEAppProxyProvider either", does it mean that we have no way to eliminate udp 53 traffic with NEAppProvider?

Thanks in advance for any suggestion.

I do believe that if there is any way to exclude udp 53 traffic, should get resolved. But as you see, I am using none zero default route as suggested, but udp 53 packets still comes in.

Just as a clarification here; when I tested your workflow, you were not receiving post 53 UDP flows in handleNewFlow, but when your Network Extension acted upon the UDP flows being proxied your Network Extension was receiving these flows. For example, a UDP flow with the local endpoint of 0.0.0.0: 6244 might be handled in handleNewFlow and then when the flow was proxied the outbound datagrams might contain port 53 traffic, x.x.x.x:53.

does it mean that we have no way to eliminate udp 53 traffic with NEAppProvider?

Building up my above description, you can do a few things here; You can let the system handle all UDP flows. You can attempt to target a small subset of UDP flows and if you receive port 53 flows, you can proxy them. Proxying UDP flows is something that can be done without a crash.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com

You can let the system handle all UDP flows. You can attempt to target a small subset of UDP flows and if you
receive port 53 flows, you can proxy them. Proxying UDP flows is something that can be done without a crash.

That is what I am trying at the moment. But when letting all udp traffic through with filter setting like below, within around three hours, network is gone like below. Crash might not happen though.
Code Block
ping apple.com
...
Request timeout for icmp_seq 14745
ping: sendto: No route to host
Request timeout for icmp_seq 14746
ping: sendto: No route to host

I have tried both udp session and NWConnection for remote connection, same result.
Any suggestion?
Code Block
for seg in 1...254 {
if seg != 127 {
hosts = [("\(seg).0.0.0", "0")]
for host in hosts {
let ep = NWHostEndpoint(hostname: host.0, port: host.1)
let rule = NENetworkRule.init(remoteNetwork: ep, remotePrefix: 8, localNetwork: nil, localPrefix: 0, protocol: .UDP, direction: .outbound)
rules.append(rule)
}
}
}

Also, can you help indicate how to update in the tsi ticket? I would like to go on discussion fromTSI case 767665663, but don't know what is the link for it.
Thanks in advance.