1

I'll start with stating that I know very little about networking and the whole OSI model.

My goal is to create a tiny network(for now my laptop and a raspberry Pi) using an unmanaged network switch. On higher layer transmissions(level 3+) I would simply set the destination IP address for a packet. From what I've read on Wikipedia a network switch operates at the data link layer which means it uses MAC addresses.

How does one send data to a device on a local area network when it's connecting with something that only supports MAC addresses. More importantly, how does one do it from a high level language like Java or C#?

Justas S
  • 584
  • 4
  • 12
  • 34
  • It is still not clear what prohibits you from using TCP/IP protocols stack? DHCP or other high-level features are not required for it. IP addresses can be hardcoded (configured) directly on each network device even if they are just connected by direct cable without any switches at all. – SergGr Mar 29 '17 at 19:33
  • 1
    The unclear part to me is if the switch doesn't have a notion of IP addresses, how can I use TCP/IP if they both require an IP address to deliver the packet? – Justas S Mar 30 '17 at 14:02
  • Bebras, if your switch is really dumb i.e. just broadcast every incoming packet to every other connected device without any filtering, then TCP/IP should work just fine provided you configure IP on each device manually to have a unique IP address within same sub-nest (mask). TCP/IP supports [bus network topology](https://en.wikipedia.org/wiki/Bus_network) because they were widely used at the moment TCP/IP was desgined (for example Ethernet was at that time was often built as bus). – SergGr Mar 30 '17 at 15:28
  • You really need "smart" switches for TCP/IP only if you want to reduce network load (don't send every packet in the network over every cable to every device) or you want to provide some traffic routes separataion (such as LAN vs WAN or you want to have a DMZ, etc). Those are all features of the bigger networks. Luckily today hardware for simple smart switches is cheap enough that almost noone has to buy dumb switches for economical reasons, but this doesn't mean TCP/IP requires smart switches to work in simple conditions. – SergGr Mar 30 '17 at 15:33

2 Answers2

2

TL;DR The the OSI model is about abstraction and programing languages use operating system calls to implement this abstraction. The Rasberry Pi is running a full OS and will send and receive network data addressed to its assigned IP address. You do not need to specify MAC address.

You want to communicate with a Raspberry Pi from your Laptop. To do this you first connect them to the dumb switch and assign both devices an IP address in the same subnet, on physical interfaces connected to the dumb switch. Let say that your laptop's physical ethernet connection is assigned 10.0.0.1/24 and Rasberry Pi's physical ethernet connection is assigned 10.0.0.2/24 (If you do not understand my notation look at CIDR). IP addresses are Layer 3 constructs. Now your application will use an Operating System socket to create a TCP or UDP connection(see UDP java example here) with a layer 4 address (application port). Everything higher than Layer 4 is handled by your application.

Layer 2 and lower is handled by the OS. When your application tries to send data through the socket, the Operating System determines which physical interface to send data from by looking at the destination IP address. This lookup uses the OS Routing Table. Assuming you have a normal routing table, the OS will pick the interface that has ab IP with the same subnet as the destination IP. So if you send data to 10.0.0.2, your OS will send data from 10.0.0.1 because it has the same subnet of 10.0.0. Now the OS has selected an interface, it still does not know what Layer 2 MAC address to send the Layer 3 IP packet to. The main reason the OS does not know this is because IP addresses can change, but Layer 2 MAC addresses should not. Anyhow the OS sends out an ARP request which tries to get the MAC address for an IP address. If the devices are connected properly, the OS gets a MAC address for the desired IP address and begins to send data to that MAC address. The switch (smart or dumb) makes sure the message gets to the desired MAC address. At the receiving end, the OS receives the packet and send the data in the packet to sockets bound to the Layer 4 address (application port).

Side note: it is technically possible to send data to just a MAC address using RAW sockets but it is extremely technical.

Liam Kelly
  • 3,524
  • 1
  • 17
  • 41
  • So the OS at the endpoints takes care of the IP to MAC "conversions". That was the part that was unclear to me. I'll look into the ARP thing but this post clears up a lot. Thank you! – Justas S Mar 30 '17 at 20:32
  • 1
    No problem. One way to see how arp works is to vie your computer's ARP table. On a mac it is `arp -a`, and modern linux it is `ip neigh show`. You should see a table with rows that list 'Destination IP address', 'Destination MAC Address', and the physical interface you computer uses to send the data from. – Liam Kelly Mar 30 '17 at 23:41
2

Liam Kelly's answer provides great insight on abstraction of data sending. I will try to provide complementary information.

Network switch operation

While most switches operate at data level, there are some that can perform some operation at higher levels:

  • layer 3: Within the confines of the Ethernet physical layer, a layer-3 switch can perform some or all of the functions normally performed by a router.

  • layer 4: [...] capability for network address translation, but then adds some type of load distribution based on TCP sessions.

  • layer 7: [...] distribute the load based on uniform resource locators (URLs), or by using some installation-specific technique to recognize application-level transactions.

RAW sockets usage

As already specified, these require fairly advanced programming skills. They are also severely restricted in non-server versions of modern Windows Operating Systems (source) due to security concerns:

  • TCP data cannot be sent over raw sockets.
  • UDP datagrams with an invalid source address cannot be sent over raw sockets. The IP source address for any outgoing UDP datagram must exist on a network interface or the datagram is dropped. This change was made to limit the ability of malicious code to create distributed denial-of-service attacks and limits the ability to send spoofed packets (TCP/IP packets with a forged source IP address).
  • A call to the bind function with a raw socket for the IPPROTO_TCP protocol is not allowed.

Suggestion

If .NET is a viable option for you, I would take Pcap.Net for a spin, as it allows various operations at packet level using high level programming (including LINQ).

Community
  • 1
  • 1
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164