Is there a simple Win32 API function to get the VLAN ID of a network interface like powershell get-netAdapter or getNetAdapterAdvancedProperty? - winapi

My application uses npcap/winpcap interface to send packets on a network interface.
The application layer wants to apply priorities but no VLAN id, so it prepares the VLAN header with priority but VLAN id 0.
If I send to a VLAN tagging interface, the packets don't get the correct VLAN id but still have 0. I verify this by looking at the receiver side using tcpdump/wireshark.
I want to patch in the correct VLAN id, so I need to know it. For the application VLAN selection shall be implicit through interface selection (via the IP address or MAC address).
In Powershell Get-NetAdapter or Get-NetAdapterAdvancedProperties have a VlanID and I know I can access this information using COM to access the WMI database.
But is there a simpler way like an ioctl request or something similar to GetAdaptersInfo that returns the VLAN ID?

Related

How to get the underlying network interface used by a VPN connection in Windows

The closest thing to a solution I've found is using Get-NetConnectionProfile to return all active interfaces, which works fine when there's only an active physical interface and the VPN itself. However, this would not work if the user's machine has 2 active physical interfaces (e.g Wi-Fi + Ethernet) along with the VPN.
Ideally, I'd like a solution that works similarly to "ifconfig -v" in MacOS, which tells you the effective interface for a virtual interface:
Unfortunately it seems there is no sure-fire way to get the underlying physical adapter for a VPN using a Windows API. Short of involving a packet sniffer such as Wireshark, the best solution I found involves parsing the output of two PowerShell commands: Get-NetAdapter and Get-NetRoute.
With the information from these commands, I can know which interfaces are virtual and which ones are physical, and I can rank the physical interfaces by 3 different criteria (in case of tie, we move on to the next criteria):
Sorting the physical interfaces by the interface metric + the route metric to the default gateway (0.0.0.0).
Wired connections over wireless ones (PhysicalMediaType=802.3).
Prioritizing faster adapters.
With this logic all the VPNs I tested appear to reflect the expected network interface, although some VPNs let you force traffic through a particular physical adapter in which case obviously this all goes out the window.
First,
You can install wireshark or some other traffic monitoring tool and capture the relevant packets (filtering using openvVPN protocol or port etc.)
Second,
As far as I know there is no hard linking between the virtual network interface and the regular (ethernet, WIFI etc.) interface, at least not in OpenVPN (there are diffrent VPN protocols). The openVPN packets will be routed to the remote server using you OS routing table.
This way if your ethernet interface is your primary default gateway, and it gets unplugged, your VPN service will be able to recover, since it will have a route to your remote VPN-server address using your WIFI interface.

Routing specific TCP/UDP packets from VLAN's to proxy server

I need to set up a solution which is for routing specific TCP/UDP packets between the devices in two separate VLANs through a proxy server. For eg: all my computers are connected to one VLAN(1) and my printers are one another VLAN(2). I can reroute all the packets from VLAN(1) to VLAN(2). My objective is to only reroute packets which generated for printers in VLAN (2) from computers in VLAN (1).
Is there any way in Layer 3 routing for achieving this?
Normal routing is based on the destination IP address. If you need to select routes based on source IP address or source/destination port numbers you require policy-based routing.
What you're describing though is simple, destination-based routing. Just add a route to VLAN 2 on your default gateway (or connect it directly) and you're set. (Obviously, the printers in VLAN 2 need to have a route back as well.)
If you want to limit the routed protocols to the printing specific ones or filter by source address you need to use a firewall or switch/router ACLs.

How to assign an SNMPv2c inform request from another manager to the proper agent?

The SNMPv2c specification states that "An InformRequest-PDU is generated and transmitted at the request of an application in a SNMPv2 entity acting in a manager role..." [RFC 1905, section 4.2.7]
If a manager receives an inform request from another manager, I wonder how it assigns the data to the proper agent.
When the inform request is received from an agent - like a trap - the IP address can be used to assign the data to the agent.
This case also seems to make more sense to me. Are there any real world applications where an inform is sent by a manager?
You can not change the IP address in INFORM PDU. This type of SNMP PDU does not have such field. So the receiver always uses src_ip of UDP datagram. The Agent Address is part of TrapV1 PDU only.

Is there a single snmp command to get list of snmp enabled devices in some ip address range

In my network, I want to scan for snmp enabled devices. So is there any single line snmp command, to get list of snmp enabled devices in specific range of IP Address like 172.26.1.1 to 172.26.1.255.
I found a link which says it is possible but it does not mention the command.
https://support.panorama9.com/hc/en-us/articles/203568188-Test-if-SNMP-devices-are-responding-correctly-to-SNMP-queries
Is there any generic OID to achieve this or single snmp command ?
Well, you can try to use the subnet broadcast address but from the SNMP FAQ most devices have this turned off. So this probably won't work very well.
http://www.snmp.com/FAQs/snmp-faq-part2.txt
2.60.12
SUBJECT: How should an agent respond to a broadcast request?
When an SNMP request is sent with a broadcast address, Who is
supposed to respond? Ideally every host in the subnet should respond.
But I notice that it doesn't happen. What is the expected behaviour when
a directed broadcast is done?
Depending on how the agent is written, it may respond, or
just drop the message. Many agents just drop the message.
I believe that is what the SNMP agent in cisco product do.
Another alternative is to try to use nmap to scan for hosts that respond to SNMP as well as attempt to see what community strings they respond to.
https://www.vanstechelman.eu/networking/scanning_for_snmp_services_with_default_community_strings
also
https://nmap.org/nsedoc/scripts/snmp-brute.html

snmp, how to retrieve ip connected to the router with MIB-II

I want to create a program that acts as manager and that queries the router (or sets a trap) to obtain the list of ip connected to it. My router has these functionalities: SNMP v1, v2c, built-in MIB-I, MIB-II agent.
Is it possible to retrieve these informations quering the MIB-II agent of the router in a standard way (not vendor dependent)?
Bye
The Address Translation table (OID .1.3.6.1.2.1.3.1) contains arp table of the device.
This will show you list of all devices from the network, which are (or have been recently) sending some traffic via router.

Resources