DOMAIN(9)

πŸ’– DOMAIN(9) πŸ’–

okay bestie, welcome to DOMAIN(9) from the Kernel Developer's Manual β€” yes it's a section 9 kernel developer page, yes it's lowkey a lot, but you're gonna eat this up, i promise 🫢


✨ NAME ✨

domain_add, pfctlinput, pfctlinput2, pffinddomain, pffindproto, pffindtype, DOMAIN_SET β€” network domain management πŸ¦‹


✨ SYNOPSIS ✨

like literally just the receipts, no cap:

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/protosw.h>
#include <sys/domain.h>

void
domain_add(void *data);

void
pfctlinput(int cmd, struct sockaddr *sa);

void
pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam);

struct domain *
pffinddomain(int family);

struct protosw *
pffindproto(int family, int protocol, int type);

struct protosw *
pffindtype(int family, int type);

void
DOMAIN_SET(name);

✨ DESCRIPTION ✨

so the network protocols that live inside the system? they're all kept inside little containers called domains. like inetdomain and localdomain are the examples, iykyk. it's giving organized. it's giving structural. πŸ’…

here's the whole struct domain so you can see the entire vibe:

struct domain {
        int     dom_family;             /* AF_xxx */
        char    *dom_name;
        void    (*dom_init)             /* initialize domain data structures */
                (void);
        void    (*dom_destroy)          /* cleanup structures / state */
                (void);
        int     (*dom_externalize)      /* externalize access rights */
                (struct mbuf *, struct mbuf **);
        void    (*dom_dispose)          /* dispose of internalized rights */
                (struct mbuf *);
        struct  protosw *dom_protosw, *dom_protoswNPROTOSW;
        struct  domain *dom_next;
        int     (*dom_rtattach)         /* initialize routing table */
                (void **, int);
        int     (*dom_rtdetach)         /* clean up routing table */
                (void **, int);
        void    *(*dom_ifattach)(struct ifnet *);
        void    (*dom_ifdetach)(struct ifnet *, void *);
        int     (*dom_ifmtu)(struct ifnet *);
                                        /* af-dependent data on ifnet */
};

every single domain holds an array of protocol switch structures (struct protosw *), one for each socket type it supports. one per type. periodt. 🌸

and here's the struct protosw β€” the main character in its own right:

struct protosw {
        short   pr_type;                /* socket type used for */
        struct  domain *pr_domain;      /* domain protocol a member of */
        short   pr_protocol;            /* protocol number */
        short   pr_flags;               /* see below */
/* protocol-protocol hooks */
        pr_input_t *pr_input;           /* input to protocol (from below) */
        pr_output_t *pr_output;         /* output to protocol (from above) */
        pr_ctlinput_t *pr_ctlinput;     /* control input (from below) */
        pr_ctloutput_t *pr_ctloutput;   /* control output (from above) */
/* utility hooks */
        pr_init_t *pr_init;
        pr_fasttimo_t *pr_fasttimo;     /* fast timeout (200ms) */
        pr_slowtimo_t *pr_slowtimo;     /* slow timeout (500ms) */
        pr_drain_t *pr_drain;           /* flush any excess space possible */

        struct  pr_usrreqs *pr_usrreqs; /* user-protocol hook */
};

ok so the functions below handle: registering a new domain, looking up specific protocols and protocol types inside those domains, and dealing with control messages coming from the system. the whole trio. iconic. ✨

pfctlinput() gets called by the system whenever something happens that could affect every domain. like what? routing table changes, interface shutdowns, certain ICMP message types β€” the "affects literally everyone" kind of events. when it's called, pfctlinput() calls the protocol-specific pr_ctlinput() function for each protocol in that domain that has defined one, in every domain. all of them. no skips. πŸ’€

pfctlinput2() does the same thing as pfctlinput() but with a few extra checks and a new void * argument that gets passed directly to the protocol's pr_ctlinput() function. and unlike pfctlinput(), pfctlinput2() verifies that sa is not NULL, and it only calls pr_ctlinput() on the protocol families that are the same as sa. so like, it's the picky one. we love a discerning queen. πŸ‘‘

domain_add() adds a new protocol domain to the system. the argument data gets cast directly to struct domain * inside the function, but it's declared void * so the compiler doesn't yell at you when new domains get registered with SYSINIT(). in most cases you don't call domain_add() directly β€” you use DOMAIN_SET() instead. (do less, bestie, use the macro.)

if the new domain has defined an initialization routine, domain_add() calls it. and also, every protocol inside that domain that defined an initialization routine gets theirs called too. everyone eats. πŸ“

ok important: once a domain is added it cannot be unloaded. not ever. this is because there's no reference counting system in place to figure out if any sockets within that domain still have active references to it. so like, it's permanent. irreversible. main character commitment. 😭

pffinddomain() finds a domain by family. if the domain can't be found, NULL is returned. that's it, that's the deal. πŸ™ˆ

pffindtype() and pffindproto() look up a protocol by its number or by its type. in most cases if the protocol or type can't be found, NULL is returned β€” but pffindproto() may return the default if the requested type is SOCK_RAW, a protocol switch type of SOCK_RAW is found, and the domain has a default raw protocol. (that's the one little plot twist, ngl.)

both of those functions get called by socreate() to resolve the protocol for the socket currently being created. teamwork fr fr. 🫢

DOMAIN_SET() is a macro that simplifies registering a domain via SYSINIT(). the code that comes out of the macro expects there to be a domain structure named β€œnamedomain” β€” where name is the argument to DOMAIN_SET():

struct domain localdomain =
{ AF_LOCAL, "local", unp_init, unp_externalize, unp_dispose,
  localsw, &localsw[sizeof(localsw)/sizeof(localsw[0])] };

DOMAIN_SET(local);

✨ RETURN VALUES ✨

both pffindtype() and pffindproto() return a struct protosw * for the protocol you asked for. if the protocol or socket type isn't found, NULL is returned. and in the case of pffindproto(), the default protocol may be returned for SOCK_RAW types if the domain has a default raw protocol. same energy as above, just restating the receipts. πŸ’œ


✨ SEE ALSO ✨


✨ HISTORY ✨

the functions domain_add(), pfctlinput(), pfctlinput2(), pffinddomain(), pffindproto(), pffindtype() and DOMAIN_SET() all first appeared in FreeBSD 4.4. so they've been serving looks since 4.4. ⭐


✨ AUTHORS ✨

this manual page was written by Chad David <davidc@acns.ab.ca>. πŸŽ€


and that's the whole page bestie, you're officially domain-literate πŸ’…

this gem came from the Debian build, dated April 29, 2020 β€” so yeah, if you're citing it, that's the version, no cap. now go register a domain and slay πŸ¦‹βœ¨

πŸ“¦ where this page comes from