I'm looking to write kernel-space implementations of a non-IP network protocol and associated network device driver (non-ethernet hardware). I found some resources on developing the protocol and the driver, though I'm having difficulty understanding how to associate sockets using my protocol with my driver.

The aim is to have it working such that a program in userspace would only need to call bind(socket(AF_TERRIBLE_IDEA, ...), ...); and be good to go.

The aim is to get more familiar developing for kernel-space simultaneously, so moving to userspace is not ideal.


when a bind call is made, how does the kernel know which device (and therefore device driver) to associate a socket to?

  • bind(2) doesn't bind to interfaces; it binds to addresses. eg for TCP sockets, it's at the TCP layer, which is above the IP layer, which is above the physical layer. The physical interface isn't relevant to bind(2). – Stephen Harris Sep 23 '18 at 1:53
  • @StephenHarris Where does the association occur then? – Kenneth Hau Sep 23 '18 at 2:05
  • It doesn't. The physical driver knows what packets are addressed to it's MAC address. It passes that up the stack to the protocol driver, which may pass it further up the stack. Finally a match on address/port is made. This is far away from the physical interface. – Stephen Harris Sep 23 '18 at 2:16
  • @StephenHarris How is this done before any packets are received? Say there is a system with 2 NICs and I bind to an address before sending a packet. What dictates which NIC the packet comes out of? – Kenneth Hau Sep 23 '18 at 3:04
  • 1
    @flow2k Correct, if the client binds to something that isn't nor is an address of any interface, it's unlikely it will be able to communicate with another system. Although something like Linux iptables can programatically rewrite and reroute packets, so anything is possible there. – Mark Plotnick Jul 5 '19 at 19:55

Have a look at raw sockets and packet sockets, they are specifically meant to implement non-standard network protocols in user space.

It's entirely up to you how other applications connect to your driver, and the best choice depends on the details of what you are trying to implement.

The bind command does different things in different contexts; usually some variation of "let's provide some default data that is necessary". That can mean a specific source address and/or network interface, especially for the standard protocols (which you are not using), but doesn't have to. So it doesn't work the way you think it works.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.