This is projects/routing pre-requisite.
Add if_requestencap() interface method which is capable of calculating various link headers for given interface. Right now there is support for INET/INET6/ARP llheader calculation (IFENCAP_LL type request).
Other types are planned to support more complex calculation (L2 multipath lagg nexthops, tunnel encap nexthops, etc..).
Reshape 'struct route' to be able to pass additional data (with is length) to prepend to mbuf.
These two changes permits routing code to pass cached nexthop data (like L2 header for route w/gateway) down the stack eliminating the need for other lookups.
It also brings us closer to more complex scenarios like transparently handling MPLS nexthops and tunnel interfaces.
Last, but not least, it removes layering violation introduced by flowtable code (ro_lle) and simplifies handling of existing if_output consumers.
ARP/NDP lookups:
There is free room inside first 64 bytes of struct lle sufficient to store full ethernet/ipoib header. Make arp/ndp stack pre-calculate link header upon installing/updating lle record. Interface link address change are handled by re-calculating headers for all lles based on if_lladdr event. After these changes, arpresolve()/nd6_resolve() returns full pre-calculated header for supported interfaces this simplifying if_output().
Move these lookups to separate (inlined) <ether|ipoib>_resolve_addr() function which ether returs error or fully-prepared link header. Add <arp|nd6_>resolve_addr() compat versions to return link addresses instead of pre-calculated data.
ARP requests:
Arp is control plane protocol, so we don't care about performance that much. Use if_requestencap() to pre-calculate link header for each packet and provide if_output() with full prepend data eliminating the need for AF_ARP case.
BPF:
Raw bpf writes occupied _two_ cases: AF_UNSPEC and pseudo_AF_HDRCMPLT.
Despite the naming, both of there have ther header "complete". The only difference is that interface source mac has to be filled by OS for AF_UNSPEC (controlled via BIOCGHDRCMPLT). This logic has to stay inside BPF and not pollute if_output() routines. After that change, convert BPF to pass prepend data via new 'struct route' mechanism. Note that it does not change for non-optimized families: ro+prepend handling is purely optional.
Side note: hackish pseudo_AF_HDRCMPLT is supported for ethernet and FDDI. It is not needed for ethernet anymore. The only remaining FDDI user is dev/pdq mostly untouched since 2007. DLT_FDDI was eliminated on OpenBSD in 2014: http://openbsd-archive.7691.n7.nabble.com/bpf-4-obsolete-data-link-levels-td246688.html .
Flowtable:
Flowtable violates layering by saving (and not correctly managing) rtes/lles. Instead of passing lle pointer, pass pointer to pre-calculated header data from that lle.