Skip to content

Tracing OpenBSD's `bind`

Ronsor edited this page Nov 23, 2018 · 15 revisions

Step-Through Description

The [regression test] (https://github.com/openbsd/src/blob/5271000b44abe23907b73bbb3aa38ddf4a0bce08/regress/sys/kern/bind/bind.c) creates a socket that will use the UNIX internal domain (AF_INET) and a datagram communication protocol. The test enables port reuse on this socket with the SO_REUSEPORT option passed to setsockopt (see man socket and man setsockopt for details).

We call bind on the socket's file descriptor. In order to trace this call, we go to our syscalls function assignment, which links 104 (bind) to sys_bind. sys_bind, found in uipc_syscalls.c, parses the syscall arguments (standard for all syscalls), calls pledge_socket, and eventually calls sobind on the pointer to our socket, the mbuf carrying address information, and the metadata about the process that called this.

sobind delegates out to the pr_usrreq function attached to the passed-in socket's protocol. Before we go directly to the appropriate pr_usrreq function, it's important we trace how that gets assigned in the first place. This will turn into quite the detour...

When we call socreate, which eventually gets called in sys_socket, we call one of two methods, pffindproto or pffindtype. Both of these methods return a protosw pointer which includes the pr_usrreq function for whatever protocol gets returned.

TODO:

  • Link unlinked formatted functions to code.
  • Explain how pr_usrreq functions get linked to protosw instances.
  • Cover the interface to pr_usrreq.
  • Add details to descriptions.
    • socreate
    • pffindproto
    • pffindtype
  • Consider reformatting this into an outline or Q&A format.
  • Add an actual trace of function calls with sample arguments.
  • Add code blocks for important structs or pieces of code.

Questions (Some Stephen-specific)

  1. What does sw in protosw stand for? Protocol switch table.
  2. What does it mean when a function starts with "/*ARGSUSED*/"? Obsolete hint for lint, looks like.
  3. Why is the only implementation of bind found in uipc_syscalls.c? What does "uipc" stand for? Is it "inter-process communication"?
  4. What does the ... mean at the end of a C function signature? http://man.openbsd.org/OpenBSD-5.8/varargs.3
Clone this wiki locally