ACCEPT_FILTER(9)

πŸ’– ACCEPT_FILTER(9) β€” the kernel dev manual, but make it cute πŸ’–

okay so bestie, welcome to ACCEPT_FILTER(9), aka the page where the kernel lets apps pre-chew incoming connections before they even hit userspace. it's giving bouncer-at-the-club energy, fr fr. 🫢


✨ NAME ✨

accept_filter, accept_filt_add, accept_filt_del, accept_filt_generic_mod_event, accept_filt_get β€” filter incoming connections πŸŽ€


🌸 SYNOPSIS 🌸

#include <sys/types.h>
#include <sys/module.h>
#include <sys/socket.h>
#define ACCEPT_FILTER_MOD
#include <sys/socketvar.h>

int
accept_filt_add(struct accept_filter *filt);

int
accept_filt_del(char *name);

int
accept_filt_generic_mod_event(module_t mod, int event, void *data);

struct accept_filter *
accept_filt_get(char *name);

like yes, that's the whole cast of characters, and yes they all showed up dressed correctly πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

So the whole vibe here: accept filters let an application ask the kernel to pre-process incoming connections for it. no cap. the app requests one through the setsockopt(2) system call, and it passes in an optname of SO_ACCEPTFILTER. that's the secret handshake. 🀝


πŸ‘‘ IMPLEMENTATION NOTES πŸ‘‘

okay so if a module wants to BE an accept filter (main character behavior), it has to hand the system a struct accept_filter:

struct accept_filter {
        char    accf_name[16];
        void    (*accf_callback)(struct socket *so, void *arg, int waitflag);
        void *  (*accf_create)(struct socket *so, char *arg);
        void    (*accf_destroy)(struct socket *so);
        SLIST_ENTRY(accept_filter) accf_next;   /* next on the list */
};

then the module registers it with the accept_filt_add() function, passing a pointer to a struct accept_filter that was allocated with malloc(9). (yes, you gotta malloc(9) it. the kernel is not your mom, it will not allocate for you. 😭)

and here's the field-by-field breakdown, because every single one matters:

  • accf_name β€” the name of the filter; this is literally how it gets accessed from userland. so pick something cute but, like, legal.
  • accf_callback β€” the callback the kernel does once the connection is established. it's the same thing as a socket upcall, and it gets called when the connection is established and whenever new data arrives on the socket β€” unless the callback modifies the socket's flags. so, like, don't go changing flags and then act surprised. πŸ™ˆ
  • accf_create β€” called whenever setsockopt(2) installs the filter onto a listening socket.
  • accf_destroy β€” called whenever the user removes the accept filter on the socket. bye bestie πŸ‘‹

Now, the accept_filt_del() function: you pass it the same string that you used in accept_filter.accf_name during registration with accept_filt_add(), and then the kernel disallows any further userland use of the filter. periodt. that filter is done. πŸ’€

The accept_filt_get() function is used internally to locate which accept filter to use via the setsockopt(2) system call. it's the matchmaker. iykyk.

And the accept_filt_generic_mod_event() function gives you a simple way to avoid duplicating code for accept filters that don't use the argument field to load and unload themselves. you can use this function in the moduledata_t struct for the DECLARE_MODULE(9) macro. efficiency? slay. ✨


πŸ”₯ SEE ALSO πŸ”₯

setsockopt(2), accf_data(9), accf_dns(9), accf_http(9), malloc(9)

go read those too, bestie, they're all in the friend group πŸ’œ


🌈 HISTORY 🌈

The accept filter mechanism was introduced in FreeBSD 4.0. an icon, a legend, and the moment is still iconic. ⭐


πŸŽ€ AUTHORS πŸŽ€

This manual page was written by Alfred Perlstein, Sheldon Hearn and Jeroen Ruigrok van der Werven. absolute queens of documentation. πŸ‘‘

The accept filter concept was pioneered by David Filo at Yahoo! and refined to be a loadable module system by Alfred Perlstein. two icons, one legend of a feature. 🧚


and that's the whole page!! mwah πŸ’‹

this one came to you from the Debian edition of the page, dated June 25, 2000 β€” so yeah, it's vintage, but it ATE and left no crumbs. πŸ“ take care of your accept filters and they'll take care of you. byeeee πŸ¦‹βœ¨

πŸ“¦ where this page comes from