dragonfly.git
6 days agonrelease - fix/improve livecd master
Michael Neumann [Wed, 10 Apr 2024 17:50:32 +0000 (19:50 +0200)]
nrelease - fix/improve livecd

Make building a livecd image with "binpkgs" easier:

    make release gui binpkgs

now works out of the box without having to manually specify the
corresponding packages for some ports (e.g. perl5).

While there, fix .xinitrc (we no longer use pkgsrc) and use a more
recent xorg.conf.

6 days agoSync psm/evdev/atkbd with FreeBSD
Michael Neumann [Sat, 6 Apr 2024 10:40:13 +0000 (12:40 +0200)]
Sync psm/evdev/atkbd with FreeBSD

Partially sync psm, evdev and atkbd drivers with FreeBSD HEAD as of now.

The result should be much better input device support (Touchpad etc.) on
some Laptops.

Tested on following hardware:

* Panasonic CF-SV (myself)
* Tuxedo InfinityBook 14 (myself)
* Lenovo Thinkpad T480 (Antonio)
* Lenovo Thinkpad T490 (Peeter)

As for the Panasonic CF-SV and Tuxedo InfinityBook 14, it works so much
better than before. Without this commit, the touchpad was either not
recognized (missing active AUX port multiplexing) or barely useable.

Credits go as well to the authors of Bug report #3206 (htse, daftaupe)
who did similar work in parallel.

To summarize some of the changes:

* psm, evdev (w/o uinput) and to some degree the atkbd driver is synced
  with FreeBSD.

* As for atkbd, mainly the parts related to support active AUX port
  multiplexer code is synced from FreeBSD. Active AUX port MUX is
  required to detect for example my synaptics touchpad on the Pansonic
  CF-SV, otherwise the touchpad is only recognized as standard PS/2 mouse.
  Other laptops use the same "method" to attach "multiple" mice to the AUX
  port.

* This patch also includes the patch given in Bug report #3206:
  https://bugs.dragonflybsd.org/issues/3206

* Adds kern.features sysctl. Xorg uses e.g. kern.features.evdev
  and kern.features.evdev_support to detect if evdev is available.

* Tested with a Synaptics Touchpad using both moused/sysmouse and
  libinput under X11. Both work reliably.

* This patch enables hw.psm.{synaptics,trackpoint,elantech}_support
  by default. This should give a better user-experience by default.

* Set hw.psm.mux_disabled=1 if your keyboard no longer works due
  to the AUX MUX code (I haven't synced the quirks code yet).

* atkbd - periodic polling is disabled. Can be turned on by setting
  loader tunable hw.atkbd.hz to 1 or 10 (as it was before).

How to use this with Xorg? Set:

    sysctl kern.evdev.rcpt_mask=6

Then, either add moused_enable=YES to /etc/rc.conf. Then you can
use this with the default xorg.conf.

To use this with libinput, which I do, I had to add the following to
/etc/X11/xorg.conf:

    Section "InputClass"
         Identifier "libinput pointer catchall"
         MatchIsPointer "on"
         MatchIsTouchpad "on"
         MatchDevicePath "/dev/input/event*"
         Driver "libinput"
    EndSection

and in $HOME/.xinitrc:

    xinput set-prop 'SynPS/2 Synaptics TouchPad' 'libinput Tapping Enabled' 1

to enable tapping. Not needed if sysmouse is used (requires running
moused). Scrolling feels better with libinput, but that is likely just
a better default setting.

---

In the following, each commit message is listed:

psm - Fix choosing wrong mode for synaptic device + trackpoint

With guest trackpoint present trackpoint probing switched synaptics
device to absolute mode with different protocol instead of keeping it
in relative mode.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/dcc414b888bd855d3ad30bea0e20fb35733097cb

psm - Fix calculation for clickpad softbuttons at the top

On laptops like the ThinkPad X240, ClickPad buttons are located at the
top. The hw.psm.synaptics.softbuttons_y sysctl was supposed to allow this
by setting the value to a negative one (e.g. -1700). However, the
condition was wrong (double negative), and doing that placed the buttons
in an unreachable area.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/f3bae0ea25e2f2b76f5dcd8a6a800344803a900f

psm - Fix triple-finger taps reported as double-finger for Elan hw v.4 touchpads

Wait for all advertised head packets after status packet have been received.
This fixes rare but quite annoying issue in Elan hw v.4 touchpads support
when triple-finger taps are reported as double-finger taps under several
circumstances.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/933ef2d3b285966c604d21848963afc9e9a07a0b

psm - Reduce default tap_min_queue size for Elan touchpads

Elan hw v.4 touchpads often sends touchpad release packet right after
touchpad touch one. Most probably this happens due to PS/2 limited bandwith.
Reducing of tap_min_queue size to 1 makes multifinger tap detection
more reliable in this case.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/51de77dd3d64846b8ed8462b366f83f4cfabc1b7

psm - Adjust Elantech palm width threshold to nearly match synaptics defaults

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/8a98c8c412d0c7171054423dced8874f62cd6082

psm - reduce cursor jumping on palm detection

This is done with discarding pointer movements rather then mouse packets

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/848714bcb443af01596a455cad58beda33caf98b

psm - Enable palm detection on two finger touches for multitouch trackpads

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/eb65854355fe829af8c9b0d5c5a2c324a220ed10

psm - Report 3-rd and 4-th fingers as first finger for Elan hw v.2/v.3

as Linux does. It should not affect gesture processing in current state
as it ignores finger coords on 3-finger tap detection but it should make
evdev reports looking more Linux-alike.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/6c85d7cb3230313a90ae2f5fb4fe943d5ad686f0

psm - Set predefined logical touchpad sizes for several ancient Elan hw v.2 models

This change is based on Linux driver. Determine logical trace size. It
used for calculation of touch sizes in surface units for MT-protocol
type B evdev reports.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/21cb8f6288d49b2a274db0fa531306a25220abd8

psm - Reduce synaptics touch sensitivity

Increase hw.psm.synaptics.min_pressure default value from 16 to 32
to nearly match Linux driver (30-35 hysteresis loop).
This makes libinput tap detection more reliable.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/ae3b1e4a342724a1ac2151f2526cd8b8f69c60b5

psm - Add sanity checks to Synaptics touchpad driver resolution parser

This fixes "Kernel has only x or y resolution, not both" libinput error.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/8352f326c3db80e16d43d5b29a9cf55d914becdd

psm - Skip sync check when `PSM_CONFIG_NOCHECKSYNC` is set

In psmprobe(), we set the initial `syncmask` to the vendor default value
if the `PSM_CONFIG_NOCHECKSYNC` bit is unset. However, we currently only
set it for the Elantech touchpad later in psmattach(), thus `syncmask`
is always configured.

Now, we check `PSM_CONFIG_NOCHECKSYNC` and skip sync check if it is set.
This fixes Elantech touchpad support for units which have `hascrc` set.

To clarify that, when we log the `syncmask` and `syncbits` fields, also
mention if they are actually used.

Finally, when we set `PSM_CONFIG_NOCHECKSYNC`, clear `PSM_NEED_SYNCBITS`
flag.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/9402bd96df8000c2ac5d9c239ff746919af0dd66

psm - Don't try to detect trackpoint packets if the Elantech device has none

This fixes a panic when `EVDEV_SUPPORT` is enabled: if a trackpoint
packet was detected but there was no trackpoint, we still tried to emit an
evdev event even though the associated relative evdev device (`evdev_r`)
was not initialized.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/b9b766ae4708627ab5f62377c30ec070b6dd103d

psm - Log syncmask[1], not syncmask[0] twice

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/90b0eb9b4a7b94c81f4183937210371c02fc3f58

psm - Add support for HP EliteBook 1040 ForcePads.

ForcePads do not have any physical buttons, instead they detect click
based on finger pressure. Forcepads erroneously report button click
if there are 2 or more fingers on the touchpad breaking multifinger
gestures. To workaround this start reporting a click only after
4 consecutive single touch packets has been received. Skip these packets
in case more contacts appear.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/7d1460a4b1a0e5cd9fbed912b7d4480d0f769d4a

psm - Reduce psm watchdog verbosity

Modern touchpads do not issue interrupts on inactivity so "lost interrupt"
message became annoying spam nowadays. This change quiets the message
if debug.psm.loglevel=5 (or less) is set in /boot/loader.conf

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/f451e00544de30a6ce00b15bf056bedb880e882f

psm - Fix panic occuring soon after PS/2 packet has been rejected by synaptics or elantech sanity checker

This patch has already been partially applied in commit 906090f3.

After packet has been rejected contents of packet buffer is not cleared
with setting of inputbytes counter to 0. So when this packet buffer is
filled again being an element of circular queue, new data appends to old
data rather than overwrites it. This leads to packet buffer overflow
after 10 rounds.

Fix it with setting of packet's inputbytes counter to 0 after rejection.

While here add extra logging of rejected packets.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/74a53bd1315341bc33f9dd0dc589d0397080f270

psm - Always initialize used values in debug print

'status' array passed to get_mouse_status() is usually uninitialized by
callers.

Fully populating it with values in get_mouse_status() can fail due to
read_aux_data().

Additionally, nothing in API constrains 'len' to be >= 3.  In practice,
every caller passes three, so perhaps that argument should just be removed.
Refactoring is a larger change, though.

Remove use of potentially uninitialized values by:
1. Only printing 3 debug statuses if the passed array was at least
   'len' >= 3;
2. Populating 'status' array up to first three elements, if read_aux_data()
   failed.

No functional change intended.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/893daee680dcb62886ce0c3d74457834a1c2ead9

psm - Initialize variables before use

dxp/dyp could have been used uninitialized in the subsequent debugging log
invocation.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/015ab098439452cb798dbea5dd2d4fe57c72808c

psm - Add minimal support for active AUX port multiplexers

Active PS/2 multiplexing is a method for attaching up to four PS/2
pointing devices to a computer. Enabling of multiplexed mode allows
commands to be directed to individual devices using routing prefixes.
Multiplexed mode reports input with each byte tagged to identify
its source. This method differs from one currently supported by psm(4)
where so called guest device (trackpoint) is attached to special
interface located on the host device (touchpad) and latter performs
guest protocol conversion to special encapsulation packet format.

At present time active PS/2 multiplexing is used in some models of
HP laptops e.g. EliteBook 8560w, 9470m. Enabling of absolute operation
mode on such touchpads is connected with following problems:
1. Touchpad's port priority is lower than trackpoint's. That blocks
   information queries thus prevents touchpad detection and configuration.
2. Touchpad and trackpoint have different protocol packet sizes and
   sync bytes.

As PS/2 usage is on decline only minimal possible set of changes to
support Synaptics touchpad and generic mouses is implemented.
Active multiplexing mode is enabled only at probe stage to scan through
attached PS/2 devices to query and configure Synaptics touchpad.
After touchpad has been configured, mux is switched back to legacy
(hidden multiplexing) mode to perform normal interrupt-driven input
data processing. Overflow bit values rather than tags are used to
separate packets produced by different devices. Switching back to
legacy mode allows to avoid psm(4) and atkbd(4) rework to support
4 instances of mouse driver.

Note: While in hidden multiplexing mode KBC does some editing of the
packet stream. It remembers the button bits from the last packet
received from each device, and replaces the button bits of every
packet with the logical OR of all devices’ most recent button bits.
This sort of button crosstalk results in spurious button events
which are inhibitted with various tricks. E.g. trackpoint middle
button events are suppressed while trackpad surface is touched and
touchpad left and right button events are suppressed if corresponding
trackpoint buttons are pressed.

Not applied is a section related to EVDEV support.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/80203ccefd77badf5dc58a63047a1da2d303472c

psm - do not process gestures when palm is present

Ignoring of gesture processing when the palm is detected helps to reduce
some of the erratic pointer behavior.

This fixes regression introduced in FreeBSD r317814.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/51319286ed34e70b00a709ac36a1120c26d95a5d

psm - respect tap_disabled configuration with enabled Extended support

This fixes a bug where, even when hw.psm.tap_enabled=0, touchpad taps
were processed.
tap_enabled has three states: unconfigured, disabled, and enabled (-1, 0, 1).
To respect PR kern/139272, taps are ignored only when explicity disabled.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/0c8a908463dd601a860c6ec9e52742684df3e63b

psm - give names to synaptics commands

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/bf33f20d962c1b4443324af205170a35acce63f7

psm - Add natural scrolling support to sysmouse protocol

This change enables natural scrolling with two finger scroll enabled
and when user is using a trackpad (mouse and trackpoint are not affected).
Depending on trackpad model it can be activated with setting of
hw.psm.synaptics.natural_scroll or hw.psm.elantech.natural_scroll sysctl
values to 1.

Evdev protocol is not affected by this change too. Tune userland client
e.g. libinput to enable natural scrolling in that case.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/3b11e3b6e195d08f6ca6e32464f49b7495697045

psm - Fix Elantech trackpoint support.

Sign bits for X and Y motion data were taken from wrong places.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/8fa46200391063bd5c15a25d9f9b06354fcd4147

psm - Add extra sanity checks to Elantech trackpoint packet parser

Not included are parts related to evedev.

Add strict checks for unused bit states in Elantech trackpoint packet
parser to filter out spurious events produces by some hardware which
are detected as trackpoint packets. See comment on r328191 for example.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/6c53fea7d62edeeb7d1e0d2d1da922a3a67c0bce

psm - Enable touchpads and trackpads by default

Enable synaptics and elantech touchpads, as well as IBM/Lenovo TrackPoints
by default, instead of having users find and toggle a loader tunable.
This makes things like two finger scroll and other modern features work out
of the box with X.  By enabling these settings by default, we get a better
desktop experience in X, since xserver and evdev can make use of the more
advanced synaptics and elantech features.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/2dd9a967d299a1c0c1363e82a1e3f0c19b4b9846

psm - Add macOS-like three finger drag trackpad gesture

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/5a037b11974c8c74c7df22378633caca71456a53

psm - properly check for atkbdc_open failure

atkbdc_open can return NULL if the unit's out of bounds or the softc isn't
setup. Check it to be safe.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/6d3c9beedb7f100922a61f1e089254be24e48547

psm - Do not disable trackpoint when hw.psm.elantech.touchpad_off is enabled

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/8137fb2e38f7b88d2cce9c29a2f00cda33a89d1c

psm - Workaround active PS/2 multiplexor hang

which happens on some laptops after returning to legacy multiplexing mode
at initialization stage.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/ec45be6c36dcb373103014e16a3623e0c182a5bb

psm - Always initialize Synaptics touchpad report range with defaults

Otherwise libinput refuses to recoginize some Synaptics touchpads with
"kernel bug: device has min == max on ABS_X" message in Xorg.log.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/2ac1c1927258e649e3ca3269aea40fb4c63e2296

psm - Probe Synaptics touchpad with active multiplexing mode enabled

if it is only multiplexed device. Also enable syncbit checks for them.
This fixes touchpad recognition on Panasonic Toughbook CF-MX4 laptop.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/f5998d20ed80fdc1cb3ba0c245cae5f179e22fe2

psm - Disable KVM switch "jitter" clamping for absolute touchpads

FreeBSD commit r123442 [1] introduced solution for clamping of PS/2
mice jitter when using a KVM. Solution is to buffer mouse packets for
0.050ms if mouse activity has not been seen for more than 0.5 seconds.
Then flush that data to driver if no validation errors found or drop the
entire queue otherwise.

While it works well with relative devices it has issues with absolute ones
Depending on history buffering may results in delaying of the touch front
edge for 0.050ms that affects gesture processing (tap detection).

As absolute touchpads usually are built-in devices we can safely disable
bufferization and KVM jitter clamping to avoid such a delays.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/bedf31ad7e15c7d3e3ff9e5295bfd5454fbb42fa

[1]: https://svnweb.freebsd.org/base?view=revision&revision=123442

atkbd - Reduce polling rate from 10Hz to ~1Hz

In my understanding this is only needed to workaround lost interrupts.
I was thinking to remove it completely, but the comment about edge-
triggered interrupt may be true and needs deeper investigation.  ~1Hz
should be often enough to handle the supposedly rare loss cases, but
rare enough to not appear in top.  Add sysctl hw.atkbd.hz to tune it.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/9e007a88d65ba0d23e73c3c052d474a78260d503

psm - Remove write-only variables

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/9b0eb55048e6294c00db70f8ebecf18c1c3c1dbe

psm - Swap the unit member in the softc for a device_t

This also adds a device_log() function to subr_bus.c.

This entails various changes to make this driver more "modern"
(new-bus vs pre-new-bus) using device_log() and device_printf() rather
than psm%d.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/a6c09f20e8e00bec7d9eb088b4f32dd38bf7f490

psm - Fix typo in comment

s/diable/disable/

Taken-from: FreeBSD

psm - recognize post-IBM trackpoints on Thinkpads

Newer Thinkpads (e.g. X1 Carbon 7th Gen) come with trackpoints from
different vendors.  They are mostly compatible with the original one.
Not sure all features are going to work, but at least this fixes resume
operation.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/2bcef59d428a672a9144c574b4b4f42f5f0e5b2a

psm/atkbd - Sync with FreeBSD (partially)

Drop MOUSE_GETVARS and MOUSE_SETVARS ioctls support

These ioctls are not documented and only stubbed in a few drivers:
psm(4) and syscon's sysmouse(4). The only exception is MOUSE_GETVARS
implemented in psm(4)

Given the fact that they were introduced 20 years ago and implementation
has never been completed, remove any related code.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/67580198b70aa58d572a2602a02d1a70d9971350

evdev/psm - update evdev, add evdev-support to psm(4)

* Partially update evdev from FreeBSD and add evdev-support to psm

* Touches ums(4)

atkbd - Disable periodic polling by default

If this cause keyboard/mouse freezes on some hardware, please set
loader tunable hw.atkbd.hz=1 as workaround and report the issue.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/ce881170088c4c98c036fe561f8ee8413c2e2585

psm - use evdev_register_mtx()

This uses psm_softc->lock for evdev operations

evdev - Hide "kern.evdev.rcpt_mask" sysctl if kernel compiled w/o EVDEV_SUPPORT

Now presence of this sysctl can be used for discovery if evdev support
for hybrid devices is compiled into kernel or not.
Hide "kern.evdev.sysmouse_t_axis" sysctl for the same reason.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/233e10741a791bfa570a69804ae40f9fffcdcd43

evdev - Take driver's lock in cdev write handler if necessary

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/f3f8069493a5a9db77a5f660f4f9e613742fdc42

evdev - Disable value normalization and state filtering for SND events

Some events can take sound pitch as a value so can not be represented
as binary on/off events. Tracking for on/off state is left in place
as it is a part of the evdev API.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/c6e809423fde2f5ce02b9fef5910c34e434e7c36

evdev - Do not start/stop softrepeat callout if no clients attached

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/37ff33454873d8639d10dc5c72d76fb3a1eb540f

evdev - Fix evdev codes for slash and asterisk numpad keys of AT-keyboards

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/b12ac17ef1928858ed236a9a5851c30f465f8d26

evdev - Fix pause key release event in AT keyboard set 1 to evdev xlat-or

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/a0e9218c019b30a68fe137f8540a8c8da947480b

evdev - export event device properties through sysctl interface

A big security advantage of Wayland is not allowing applications to read
input devices all the time. Having /dev/input/* accessible to the user
account subverts this advantage.

libudev-devd was opening the evdev devices to detect their types (mouse,
keyboard, touchpad, etc). This don't work if /dev/input/* is inaccessible.
With the kernel exposing this information as sysctls (kern.evdev.input.*),
we can work w/o /dev/input/* access, preserving the Wayland security model.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/f99e7b1aed7ea65ca0dbe5b182f2b9cbfdfe54db
FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/dd3a67a0781261c1fdd670456f492ecb695b8fe5

evdev - return error rather than zero-length data on blocked read()

if blocked process has been woken up by evdev device destruction.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/006eb44926d6949e0b60b21920f1ea93f03efd2f

evdev - Use proper mutex reference in autorepeat callout initialization

This fixes panic occuring when evdev key autorepeat is enabled by driver
which initializes evdev with external mutex.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/68cc053763eec2f2afef8de8a12f6786805c9e24

evdev - Use LIST_FOREACH_MUTABLE

instead of LIST_FOREACH as we are removing elements in the middle.

This fixes a panic when detaching USB mouse.

Note that LIST_FOREACH_SAFE in FreeBSD corresponds to our
LIST_FOREACH_MUTABLE.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/5396856ee0e158a1505dce733aad4f9b42afafbb

evdev - Add AT translated set1 scancodes for F-unlocked F1-12 keys

"F lock" is a switch between two sets of scancodes for function keys F1-F12
found on some Logitech and Microsoft PS/2 keyboards [1]. When "F lock" is
pressed, then F1-F12 act as function keys and produce usual keyscans for
these keys. When "F lock" is depressed, F1-F12 produced the same keyscans
but prefixed with E0.
Some laptops use [2] E0-prefixed F1-F12 scancodes for non-standard keys.

[1] https://www.win.tue.nl/~aeb/linux/kbd/scancodes-6.html
[2] https://reviews.freebsd.org/D21565

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/8701adbeb0d4e29c9aae68b493317fb581ae520e

evdev - Sync event codes with Linux kernel 5.6

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/d316504953cf0890433ac2cb75d10c6fcf0c0263

evdev - Add AT translated set1 scancodes for 'Eisu' & 'Kana' keys

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/94811094f8357824108a57169d1f5b1e2ffb56ca

evdev - recognize the Chromebook menu key as F13 like Linux does

This is the key on the right side of the function keys, with the
"hamburger menu" icon on it.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/54cca285fc3d32e0be4028d17c055424b49f4f7d

evdev - Implement fuzz

Fuzz is used to filter noise from the event stream.
Upcoming gamepad drivers use it.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/3b2175fdb62c7f872e78ce732952f2ac2ff99c8d

evdev - Linux evdev does not terminate truncated strings with 0

It seems that DragonFly ioctl(2) cannot return the actual length of
copyouted data as only error=0 is treated as "no-error".

Based-on-FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/7a810290b8f6c6885fdb9917cf590d46fa270a61

Revert "evdev - Linux evdev does not terminate truncated strings with 0"

This reverts commit 12ac6b7825fba8f53f9cbfe3a6aaed27983c1848.

I think it's safer to properly 0-terminate the strings. The only reason
why this commit landed in FreeBSD was to support a Linux binary Imprivata,
which we can't run anyways.

evdev - Do not check maximal contact number for MT initialization

This allows singletouch devices which use multitouch protocols to work.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/e40fec4ec9a7a6625b3e332d4f856eb1d05f0def

evdev - Multitouch code style changes

1. Move touch count reporting helpers to utils. They are not multitouch.
2. Use evdev_mt prefix for private multitouch support routines.
3. Use int instead of int32_t where fixed size is not required.
4. Export some internal functions.

This change should be no-op.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/98a7606b85e05132f328a2498dccab78df31cb7e

evdev - Use bitsets to track active touches and slots changed in current report

Obtained from: OpenBSD

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/2dc7188e532f0147b36a44ce5c033d9f59cbd9f4

evdev - Send first active rather than 0-th slot state as ST report

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/fbe17f9017e785dd564ce7fc5553a9136d3a0b03

kern - Add kern.features sysctl and FEATURE macro

Similar to FreeBSD.

xorg-server checks e.g. for kern.features.evdev or
kern.features.evdev_support.

Add evdev_support feature

evdev - force no fuzz for autogenerated single touch compat events

As fuzz has already been applied on multitouch event processing.
This allows to remove existing workaround for double fuzz procesing.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/314913ed7c6e6e1b7c80a1063620f478961419b7

evdev - Normalize width and pressure of single touch compat events

to match Synaptics touchpad reporting range.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/127e54deb6d8899e1dc1bc6251d512e19f21b0f1

evdev - Give short aliases to items of evdev_mt_slot array

with using of unioned anonymous structure.

Access to the same data by using different members of union generally
works despite it is not supported by C specs.

Also add helper function to report entire slot state.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/059360287e3344f48f5a7839e2d6d54016b18b19

evdev - Make MT tracking IDs monotonically increasing sequence

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/66bd52f5e241bd2548015f847f12cdff69176c40

evdev - Import support for touch-tracking

Touch tracking is a process of assignment of unique trackingID to each
initiated contact on the surface.  Keeping the trackingIDs persistent
across multitouch reports requires solving of so called Euclidian
Bipartite Matching problem.

This commit imports EBM-solver implementation based on Dinitz-Kronrod
algorithm to find minimum cost matching between contacts listed in two
consecutive reports.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/4c0a134e32a7f4dec556fea15c8de22f69864492
Obtained-from: OpenBSD

evdev - Add implicit mode for touch tracking

In implicit mode assignment of slot number and tracking id is performed
automatically on each synchronization requested by device driver.

This is done with creation of intermediate buffer for multitouch events.
This buffer holds untracked events until synchronization is requested by
device driver. It is needed as touch assigment requires
knowledges of all touch positions pushed in current and previous reports.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/f76051c7dabe952b75127a8031d87d78b603be20

evdev - Add support for automatic MT protocol type A to type B conversion

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/d056693d7bc6c1b5f2c1612e5b34807f173e21c7

evdev - Do not export multitouch functions which are unused outside evdev

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/35bc295b30ef91f1835a0ee0225e8d957a9ebddd

evdev - Fix a typo in a commit

- s/prefered/preferred/

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/efd8749fe5cccb6c3d6b5f3c3515bf89ad306bcc

evdev - Fix "hancha" and "han/yong" korean keys handling

The Korean keyboard has two keys, the Korean/Chinese and the
Korean/English toggles, that generate scancodes 0xF1 and 0xF2
(respectively) when pressed, and nothing when released. They do not
repeat.
As Hanyong/Hancha keys are generally greater than 0x80, which is
generally considered a release key, add extra preceding press key event
to generate press/release pair.
Swap Hanyong/Hancha key codes to match reality.

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/6a26c99f827ffa9da5e23d466eeb17131fc75238

evdev - Sync event codes with Linux kernel 6.5

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/87a53bef41fe769920f925e74fedddafd5187b93

Fix commit "Import support for touch tracking" (afa7dd1f)

- Fix maybe-uninitialized warning
- use bitcount32
- bring in bit_foreach_at from FreeBSD
- reorder "static"

evdev - Sync our copy of freebsd-bitstring.h with FreeBSD

- Mostly using proper functions returning the result
  instead of assigning it to a pointer.

- Specify bit positions using size_t instead of int.

evdev - Conditionalize kern.features.evdev_support

evdev - Sync with FreeBSD

atkbdc: use proper pointer type for KBDC

FreeBSD-commit: https://github.com/freebsd/freebsd-src/commit/e9305818c1ba2762ec0ac40294cde0a4b96806c7

atkbdc - add quirks from FreeBSD

atkbd - sync with FreeBSD

atkbd - Incoorperate patch from bugreport #3206

https://bugs.dragonflybsd.org/issues/3206

3 weeks agoiwm: Fix S:N reporting in ifconfig(8)
Michael Neumann [Tue, 2 Apr 2024 15:19:53 +0000 (17:19 +0200)]
iwm: Fix S:N reporting in ifconfig(8)

While there, rename ieee80211_rx_stats fields nf and rssi to c_nf and
c_rssi, following FreeBSD and other field names of the struct.

With this commit, S:N is properly reported:

> ifconfig wlan0 scan
SSID/MESH ID    BSSID              CHAN RATE   S:N     INT CAPS
The Bloom R...  00:e0:4c:b7:65:8c    1   54M -33:-96  100 EP   WPA RSN HTCAP WME
0x4e68c3a02...  50:0f:f5:c5:cd:d9    1   54M -82:-96  100 EP   HTCAP MESHCONF RSN WME
Genetica ta...  b0:be:76:df:af:a6    1   54M -83:-96  100 EPS  RSN HTCAP WME ATH WPS
The Bloom R...  00:e0:4c:b7:6a:b0    6   54M -57:-96  100 EP   WPA RSN HTCAP WME
...

Fixes: https://bugs.dragonflybsd.org/issues/3366 (#3366)

3 weeks agoiwm: Sync with FreeBSD minus few commits (as of April 1, 2024)
Michael Neumann [Mon, 1 Apr 2024 09:17:07 +0000 (11:17 +0200)]
iwm: Sync with FreeBSD minus few commits (as of April 1, 2024)

Following commits were applied:

* iwm: Print the command code for any unhandled commands
  https://github.com/freebsd/freebsd-src/commit/6844522a9aa848b40555fe8cc9d9d353e942f1f2

* iwm: improve rfkill handling
  https://github.com/freebsd/freebsd-src/commit/22d1b05c8c37a0c9c7b391492ccd0c60835d9ea1

* iwm: fix ieee80211_media_change() caller
  https://github.com/freebsd/freebsd-src/commit/c6167b4bf510d82035e8c8db3b0b074045b90916

* iwm: fix regression resp ieee80211_media_change()
  https://github.com/freebsd/freebsd-src/commit/aaa1fdb87b7a9219e88f97fe0638f4ab166681e5

* iwm: Add support for Intel Killer(R) Wireless-AC
  https://github.com/freebsd/freebsd-src/commit/90cc8706ccb2da130c0b1a28434a9ec5d4c80d81

* iwm: update if_iwmreg.h to the latest openbsd changes (24 Oct 2021)
  https://github.com/freebsd/freebsd-src/commit/355c15130aef13484821051a655da9b9066e1015

* iwm: Update SCD register accesses
  https://github.com/freebsd/freebsd-src/commit/d524e370c4dbabf607546aec6e4bcc8d64758851

* iwm: plug some of set-but-not-used vars
  https://github.com/freebsd/freebsd-src/commit/35273e2099e629a3494701036184f2d152bcf7d7

* iwm: Fix -Wunused-but-set-variable warnings
  https://github.com/freebsd/freebsd-src/commit/1dc8ed06f37a0d5ff4b78791885d7ba6dd3e407b

  NOTE: pci_release_msi() now gets called on DragonFly in case of error!

* iwm: plug set-but-not-used vars
  https://github.com/freebsd/freebsd-src/commit/f7daf710382afae9ba5824a89ef522800759c3f2

* iwm: Fix various typos and remove "$FreeBSD$"

Following commits were *not* applied:

* net80211/drivers: improve ieee80211_rx_stats for band
  https://github.com/freebsd/freebsd-src/commit/9a6695532b3997e4e2bc3fe57481cc49be5e9e93

  This requires (minor) changes to ieee80211

* iwm: remove unsued devclass arguments to DRIVER_MODULE
  https://github.com/freebsd/freebsd-src/commit/ef463ef3dab650d08a16a08e3d12683130424752

Tested-with: Intel(R) Dual Band Wireless AC 8265

Taken-from: FreeBSD (see above)

4 weeks agofuse - minor cleanup
Matthew Dillon [Thu, 28 Mar 2024 17:55:48 +0000 (10:55 -0700)]
fuse - minor cleanup

* We have fnp, don't need to VTOI() again

4 weeks agofuse - Add back into the module Makefile
Matthew Dillon [Thu, 28 Mar 2024 17:54:03 +0000 (10:54 -0700)]
fuse - Add back into the module Makefile

* Hook fuse back in, it's usable now

4 weeks agosys/conf: remove referenced to non exisitng file
Antonio Huete Jimenez [Thu, 28 Mar 2024 17:48:43 +0000 (18:48 +0100)]
sys/conf: remove referenced to non exisitng file

Reported-by: kworr
4 weeks agorc.d/wg: Match wg ifnames on wg_start
Antonio Huete Jimenez [Wed, 27 Mar 2024 13:30:57 +0000 (14:30 +0100)]
rc.d/wg: Match wg ifnames on wg_start

  Specifying wg_interfaces="wg0" in /etc/rc.conf causes:

    ifconfig: ioctl SIOCSIFNAME (set name): File exists

  The interface name matching in the wg script is not matching the
  'wg' interfaces thus trying to set a custom interface name.

  Apparently expr does not interpret '+' from regex:

    $ expr 'wg0' : 'wg[0-9]+$' && echo matched
    0

  Workaround it by using already working expr usage:
    $ expr 'wg0' : 'wg[0-9][0-9]*$' && echo matched
    3
    matched

  There is no need to anchor the beginning of line with ^ since
  expr already does that.

4 weeks agoWhitespace cleanups
Aaron LI [Sun, 3 Mar 2024 08:24:24 +0000 (16:24 +0800)]
Whitespace cleanups

4 weeks agosys: Minor fixes to some comments
Aaron LI [Sun, 3 Mar 2024 08:29:23 +0000 (16:29 +0800)]
sys: Minor fixes to some comments

4 weeks agosys: Minor code cleanups
Aaron LI [Sun, 3 Mar 2024 08:28:56 +0000 (16:28 +0800)]
sys: Minor code cleanups

No functional changes.

4 weeks agowg: Minor code cleanups
Aaron LI [Sat, 23 Mar 2024 12:02:25 +0000 (20:02 +0800)]
wg: Minor code cleanups

4 weeks agomount_cd9660(8): Simplify and clean up set_charset()
Aaron LI [Sat, 23 Mar 2024 11:44:40 +0000 (19:44 +0800)]
mount_cd9660(8): Simplify and clean up set_charset()

Simplify set_charset(), partly according to mount_ntfs(8).

Meanwhile, clean up the whitespace styles.

4 weeks agocd9660: Add support for mask,dirmask,uid,gid options
Ricardo Branco [Fri, 8 Mar 2024 15:53:20 +0000 (16:53 +0100)]
cd9660: Add support for mask,dirmask,uid,gid options

The updates to mount_cd9660(8) are basically taken from mount_msdos(8).

GitHub PR: https://github.com/DragonFlyBSD/DragonFlyBSD/pull/23
See also:
- https://github.com/freebsd/freebsd-src/pull/982
- https://github.com/NetBSD/src/pull/29

5 weeks agoumount(8): Add -d option to detach vn(4) devices
Ricardo Branco [Sun, 10 Mar 2024 14:38:17 +0000 (15:38 +0100)]
umount(8): Add -d option to detach vn(4) devices

The '-d' option tells umount(8) to detach the underlying vn(4) device if
the filesystem was mounted from it.  Note that vn(4) is a virtual disk
and can provides multiple filesystems, so the vn(4) detaching can only
succeed when all the filesystems are umounted.

For example:
$ vnconfig -c vn dfly.img
vn4
$ mount_msdos /dev/vn4s1 /mnt/dfly/boot
$ mount_ufs /dev/vn4s2a /mnt/dfly/root
$ umount -d /mnt/dfly/boot
umount: VNIOCDETACH: /dev/vn4: Device busy
umount: detach of /dev/vn4s1 failed
$ umount -d /mnt/dfly/root
(now vn4 is detached)

A similar option exists in Linux's and FreeBSD's umount(8).

GitHub PR: https://github.com/DragonFlyBSD/DragonFlyBSD/pull/24
See Also: https://github.com/freebsd/freebsd-src/pull/972

5 weeks agonet: Clean up ifa_ifwithnet() and ifaof_ifpforaddr()
Aaron LI [Wed, 20 Mar 2024 02:41:23 +0000 (10:41 +0800)]
net: Clean up ifa_ifwithnet() and ifaof_ifpforaddr()

- Add the static inline function ifa_match_withmask() to deduplicate the
  address match code in both functions.
- Use NULL instead of 0 for pointers.
- Minor style cleanups.

5 weeks agonet: Improve rt_print() and rt_addrinfo_print() debug functions
Aaron LI [Wed, 20 Mar 2024 00:22:31 +0000 (08:22 +0800)]
net: Improve rt_print() and rt_addrinfo_print() debug functions

- Update rt_print() to print the full interface name (if_xname) instead
  of the underlying device name (if_dname).
- Update rt_addrinfo_print() to print flags, ifp and ifa; similar to
  rt_print().

5 weeks agonet: Fix panic in the rt_addrinfo_print() debug function
Aaron LI [Tue, 19 Mar 2024 10:36:33 +0000 (18:36 +0800)]
net: Fix panic in the rt_addrinfo_print() debug function

The 'rti_addrs' member is a bitmask of RTA_* values, not a count value,
so the rt_addrinfo_print() debug function would just panic.

Use RTAX_MAX instead to fix this bug.  Meanwhile, add brief comments to
'rti_addrs' and 'rti_flags' members.

5 weeks agonet: Clean up sockaddr_print() by removing duplicate switch()
Aaron LI [Tue, 19 Mar 2024 10:33:04 +0000 (18:33 +0800)]
net: Clean up sockaddr_print() by removing duplicate switch()

5 weeks agonetinet: Improve the deletion of loopback route
Aaron LI [Wed, 20 Mar 2024 15:20:26 +0000 (23:20 +0800)]
netinet: Improve the deletion of loopback route

When the target host address is still bound to another interface address
(e.g., an address on another interface added later), the loopback route
should be updated to point to that address instead of simply being
removed.

Given that our rtrequest1() only supports route addition and deletion,
but not route change, we have to first remove the original loopback
route and then re-add a route with the wanted interface address.

See also in_localip_more() and ifa_switch_loopback_route() in FreeBSD.

5 weeks agonetinet: Auto add loopback routes to interface addresses
Aaron LI [Mon, 18 Mar 2024 03:43:41 +0000 (11:43 +0800)]
netinet: Auto add loopback routes to interface addresses

When adding an address to an interface, a loopback route will be created
to route the traffic targeting it through the lo0 interface.  The
loopback route will also be removed when the corresponding interface
address is deleted.

In principle, this can help simplify the network input and output paths
by avoiding special handling of local addresses.  That deserves some
further efforts.

Nonetheless, an immediate benefit goes to wg(4).  Now we can access
(e.g., ping) from localhost a wg(4) interface's IPv4 addresses.
Previously it was broken because wg(4) is a virtual device and doesn't
have/use ARP.  For Ethernet devices, the ether_output() function will
redirect the traffic targeting its own address to the loopback interface
by calling if_simloop().

Derived from FreeBSD; the major commits are:
- This patch adds a host route to an interface address ...
  https://github.com/freebsd/freebsd-src/commit/ebc90701ac6c1f814c5bd6f3e19f0113ebe06156
- Self pointing routes are installed for configured interface addr ...
  https://github.com/freebsd/freebsd-src/commit/9bb7d0f47a3265a1782a64837ae93dde4c550a6a
- Unify loopback route switching
  https://github.com/freebsd/freebsd-src/commit/59c180c35c1ddc1d544a1c73c789d126a8fbac78
- Fix rt_ifa selection during loopback route insertion process
  https://github.com/freebsd/freebsd-src/commit/2ad7ed6e4a84448234e9aaef3c9fa884848f7387

Discussed-with: dillon

5 weeks agonetinet: Simplify the handling of INADDR_ANY in in_ifinit()
Aaron LI [Wed, 20 Mar 2024 01:00:11 +0000 (09:00 +0800)]
netinet: Simplify the handling of INADDR_ANY in in_ifinit()

Just check the interface address whether is INADDR_ANY and ignore the
checks of netmask/dstaddr.

5 weeks agonet: Integrate link_rtrequest() into rt_getifa()
Aaron LI [Mon, 18 Mar 2024 03:17:07 +0000 (11:17 +0800)]
net: Integrate link_rtrequest() into rt_getifa()

When a route was inserted but had an invalid IFA (rt_ifa), the
link_rtrequest() would be called to fix it with a link-level gateway.

Move the link_rtrequest() logic into the pre-insertion rt_getifa() step,
and thus simplifies the IFA selection.  This will also help the
forthcoming mangement of loopback routes for interface addresses.

Obtained-from: FreeBSD
- https://reviews.freebsd.org/D20076
- https://github.com/freebsd/freebsd-src/commit/2ad7ed6e4a84448234e9aaef3c9fa884848f7387

5 weeks agosys/vfs/fuse: Some mmap related fixes, issues with sshfs and FUSE_FORGET
Matthew Dillon [Sat, 16 Mar 2024 23:53:22 +0000 (16:53 -0700)]
sys/vfs/fuse: Some mmap related fixes, issues with sshfs and FUSE_FORGET

* Only issue FUSE_FSYNC when there is a file-handle associated with
  the fuse_node.

* Be sure to flush the VM object on inactive before releasing the
  file handle (fixes issues with mmap()).

* Don't issue FUSE_FORGET for now, it seems to confuse sshfs so either
  I'm doing something wrong or ... don't know.

5 weeks agosys/vfs/fuse: Fix a ton more stuff
Matthew Dillon [Sat, 16 Mar 2024 21:44:21 +0000 (14:44 -0700)]
sys/vfs/fuse: Fix a ton more stuff

* Remove all the fuse_dent code.  It is no longer needed.

* Remove signal mask wrappers.  tsleep() isn't passing PCATCH so
  signals are basically ignored.

* Add missing required FUSE_RELEASE operations (paired with CREATE/OPEN)

* Add missing required FUSE_FORGET operations (paired with LOOKUPs).
  "." and ".." are not recorded by userland and do not need to be
  forgotten (will confuse userland if they are).

* No reply is expected from FUSE_FORGET RPCs, add infrastructure to
  handle this case.

* Index fuse_node's by their inode number, which greatly improves
  hardlink handling.

* Properly store the file-handle for CREATE operations so we can
  release it later (fixes some dangling .fuse_hidden* files).

* Flush the vnode and issue a FUSE_RELEASE when removing a file,
  including in the rename-over-file case, if the file is no longer
  open.  Otherwise cached vnodes that have not yet been reclaimed
  will interfere with the removal and cause the fuse userland to
  create a .fuse_hidden* file.

NOTE: RELEASE operations still need a bit of work.  The v_opencount
      test in the file removal path is not sufficient, but we had
      to close RELEASEs there for the general case to avoid many
      annoying cases where .fuse_hidden* files are created.

5 weeks agosys/vfs/fuse: Fix a ton of stuff and get writes working
Matthew Dillon [Fri, 15 Mar 2024 06:26:40 +0000 (23:26 -0700)]
sys/vfs/fuse: Fix a ton of stuff and get writes working

* Recode all the file I/O.  Use the buffer cache properly, create a
  backend for BIO strategy calls.  mmap() should work properly now.
  write() should also now work properly.

* For the moment issue vfinalize() when the opencount == 0 to force
  the DFly kernel to dispose of inactive vnodes once all references
  (including mmap references) are gone.

* Recode the filehandle (fh) tracking.   File handles are assigned
  in fuse_vop_open() and instead of being released in fuse_vop_close()
  (which completely blows up mmap() operations), we now release
  file handles in fuse_vop_inactive().

  Remove the fh side allocation.  The fh is now positively stored in
  the fuse_node.

* Properly zero data structures allocated via the objcache.

* Implement the new syncer thread / syncer scan API for fsync
  operations.

* Implement asynchronous I/O in the frontend (the backend helper thread
  is currently still synchronous).

* Rejigger how attributes are handled when the file size changes.
  It is still a bit of a mess but it is better than before.  The
  basic problem is that the file attribute info in userland does
  not update the file size field until we flush the BIOs related
  to an append.

* Not yet fixed: hard link related issues (duplicate fuse_node inodes
  are allocated).

  nlink tracking was a mess.  Adjust fuse_node->nlink to only track
  the allocated directory entries on the kernel side.  Do not try
  to update it against attribute nlink counts.  We always report
  thte attribute nlink counts... the fuse_node->nlink only tracks
  local directory entries for the moment.

* Fix a use-after-free situation that can develop with the root vnode
  for the fuse filesystem.

* Properly disconnect fnp->pfnp linkages and move connection and
  disconnection to fuse_dent_attach() and fuse_dent_detach().

* Improve atomicy in fuse_node_vn().

* Implement advisory locks locally on the kernel side.

* Not yet fixed: Multiple fuse_node's can have the same inode because
  we are not yet indexing them by inode number.

6 weeks agosys/vfs/fuse: Re-disable from build
Tomohiro Kusumi [Thu, 14 Mar 2024 02:50:45 +0000 (19:50 -0700)]
sys/vfs/fuse: Re-disable from build

The potential NULL deref fix in df8dfe6e4994f81f7a8a3cf7c5fbdce4451ac41b
is unrelated to enabling FUSE.

See 5812c3cc7f8e910251a2cf4e78242f0b11a5fb4d
and bd699eeae3b09e46383d416a578ffbf2640e0285 for details.

6 weeks agoFix null dereference bug in fuse and re-enable it.
Kyle Butt [Wed, 13 Mar 2024 20:04:32 +0000 (14:04 -0600)]
Fix null dereference bug in fuse and re-enable it.

There was a null dereference bug in fuse if the subtype was null.
subtype is supposed to be optional, as demonstrated by the code, so test
it for null before copying it in from userspace.

Remove the redundant memcpy which appears to have been missed when the
code was changed to memset/copyinstr.

6 weeks agoless(1): Fix the installation name of 'lesspipe.sh'
Aaron LI [Tue, 12 Mar 2024 04:19:40 +0000 (12:19 +0800)]
less(1): Fix the installation name of 'lesspipe.sh'

Don't change the installation name, as the exact name of 'lesspipe.sh'
is used in 'zless.sh'.

6 weeks agocontrib/less: Fix rendering issue with mandoc
Aaron LI [Tue, 12 Mar 2024 04:14:45 +0000 (12:14 +0800)]
contrib/less: Fix rendering issue with mandoc

In addition, fix the markup issue `.less` to be `.B less`.

See also:
- https://github.com/gwsw/less/pull/427
- https://github.com/gwsw/less/pull/490

6 weeks agoless(1): Regenerate defines.h and update Makefiles
Aaron LI [Mon, 11 Mar 2024 15:14:56 +0000 (23:14 +0800)]
less(1): Regenerate defines.h and update Makefiles

- Regenerate 'defines.h' as described in 'README.DRAGONFLY'.
- Update the Makefiles for less(1), lessecho(1) and lesskey(1).
- Install the lessecho.1 man page.

6 weeks agocontrib/less: Fix edit.c for building
Aaron LI [Mon, 11 Mar 2024 15:10:57 +0000 (23:10 +0800)]
contrib/less: Fix edit.c for building

edit.c: In function 'close_pipe':
edit.c:307:14: error: 'SIGPIPE' undeclared (first use in this function); did you mean 'SI_TIMER'?
   if (sig != SIGPIPE || ch_length() != NULL_POSITION)
              ^~~~~~~
              SI_TIMER

6 weeks agocontrib/less: Update READMEs
Aaron LI [Mon, 11 Mar 2024 15:09:26 +0000 (23:09 +0800)]
contrib/less: Update READMEs

6 weeks agoMerge branch 'vendor/LESS'
Aaron LI [Mon, 11 Mar 2024 13:47:16 +0000 (21:47 +0800)]
Merge branch 'vendor/LESS'

6 weeks agovendor/less: upgrade from 608 to 643 vendor/LESS
Aaron LI [Mon, 11 Mar 2024 13:42:27 +0000 (21:42 +0800)]
vendor/less: upgrade from 608 to 643

Security fix: CVE-2022-46663 (version <= 608)
- https://github.com/gwsw/less/commit/a78e1351113cef564d790a730d657a321624d79c
- https://nvd.nist.gov/vuln/detail/CVE-2022-46663
- https://bugs.dragonflybsd.org/issues/3360

See the NEWS file for the list of changes.

7 weeks agosockstat(1): Fix and improve output formatting
Aaron LI [Wed, 6 Mar 2024 06:02:33 +0000 (14:02 +0800)]
sockstat(1): Fix and improve output formatting

The main issue with the old formatting was that the FD column was
limited to 2 characters, so there would be no space between FD and PROTO
columns.

Increase the FD column to 5 characters, and increase the PID column to
6 characters.  In addition, refactor the output formatting code to clean
up the mess.

While there, clean up the code a bit to save some type castings.

7 weeks agoprocfs(5): Update man page for the previous change
Aaron LI [Thu, 7 Mar 2024 14:58:26 +0000 (22:58 +0800)]
procfs(5): Update man page for the previous change

7 weeks agoprocfs(5): Add '/proc/self/exe' symlink support
Ricardo Branco [Tue, 5 Mar 2024 21:44:51 +0000 (22:44 +0100)]
procfs(5): Add '/proc/self/exe' symlink support

* Add the /proc/self symlink that's the same as /proc/curproc.
* Add the /proc/<pid>/exe entry that's the same as /proc/<pid>/file.

The '/proc/self/exe' symlink has been already landed in NetBSD and
FreeBSD [0].  It could simplify some patches to ports that look for this
symlink.

[0] https://github.com/freebsd/freebsd-src/pull/976

GitHub PR: https://github.com/DragonFlyBSD/DragonFlyBSD/pull/22

7 weeks agogzip(1): Set file flags after setting file times
Ricardo Branco [Mon, 4 Mar 2024 16:41:56 +0000 (17:41 +0100)]
gzip(1): Set file flags after setting file times

Only set the source file flags on the target file after the timestamp
has been set; otherwise setting the timestamp will fail if the flags
don't permit it (i.e., uchg).

To reproduce the issue:
$ /usr/bin/touch /tmp/foo
$ /bin/chflags uchg /tmp/foo
$ /usr/bin/gzip -v /tmp/foo
gzip: couldn't utimes: /tmp/foo.gz: Operation not permitted

GitHub PR: https://github.com/DragonFlyBSD/DragonFlyBSD/pull/21
See also: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=120208

7 weeks agohexdump(1): Ignore zero st_size to work on pseudo-filesystems
Ricardo Branco [Sun, 3 Mar 2024 22:11:15 +0000 (23:11 +0100)]
hexdump(1): Ignore zero st_size to work on pseudo-filesystems

hexdump(1) was not able to skip on files residing on pseudo-filesystems,
for example: hexdump -s1 /proc/$$/status

GitHub PR: https://github.com/DragonFlyBSD/DragonFlyBSD/pull/20

7 weeks agowc(1): Ignore zero st_size to work on pseudo-filesystems
Ricardo Branco [Sun, 3 Mar 2024 22:06:00 +0000 (23:06 +0100)]
wc(1): Ignore zero st_size to work on pseudo-filesystems

Without this fix, 'wc -c' was failing on pseudo-filesystems, for example:
wc -c /proc/$$/status

GitHub PR: https://github.com/DragonFlyBSD/DragonFlyBSD/pull/19

7 weeks agomd5(1): Further cleanup the code a bit
Aaron LI [Tue, 5 Mar 2024 14:39:06 +0000 (22:39 +0800)]
md5(1): Further cleanup the code a bit

No functional changes.

7 weeks agomd5(1): Improve the previous commit
Aaron LI [Tue, 5 Mar 2024 14:30:12 +0000 (22:30 +0800)]
md5(1): Improve the previous commit

Improve digestbig() to further check for read() errors and return NULL
in that case.

Add a comment to explain why can't use file size (st_size).

7 weeks agomd5(1): Fix to calculate correct checksum on pseudo filesystems
Ricardo Branco [Sun, 3 Mar 2024 22:00:59 +0000 (23:00 +0100)]
md5(1): Fix to calculate correct checksum on pseudo filesystems

The file size (st_size) on pseudo filesystems can be zero and
meaningless, so ignore it and read until EOF to get the whole contents.
Now md5(1) is able to calculate the correct checksums for files on
pseudo filesystems, like /proc.

7 weeks agomd5(1): Minor code tweaks and style cleanups
Aaron LI [Tue, 5 Mar 2024 13:52:00 +0000 (21:52 +0800)]
md5(1): Minor code tweaks and style cleanups

No functional changes.

7 weeks agoping(8): Add check for negative wait timeout
Aaron LI [Mon, 4 Mar 2024 06:08:10 +0000 (14:08 +0800)]
ping(8): Add check for negative wait timeout

Meanwhile, fix the error message.

7 weeks agoping(8): Relax the interval restriction for normal users
Aaron LI [Mon, 4 Mar 2024 04:08:27 +0000 (12:08 +0800)]
ping(8): Relax the interval restriction for normal users

The ping interval was restricted to be >= 1 second for normal users to
prevent from performing DoS attacks.  This restriction was added by
Matt Dillon in 1998 during the BEST days [0].  It has become less
meaningful now.  Following the Linux behavior, relax the interval to be
>= 2 milliseconds for normal users.

In addition, add a check for negative interval numbers.

[0] https://github.com/freebsd/freebsd-src/commit/526f06b278d9252add168aa18b60242c08771165

Discussed-with: dillon

7 weeks agoping(8): Allow normal users to specify larger packets
Aaron LI [Mon, 4 Mar 2024 03:42:48 +0000 (11:42 +0800)]
ping(8): Allow normal users to specify larger packets

Before this change, only super-user could specify a packet size larger
than the default 56 bytes.  This restriction was added by Matt Dillon in
1998 during the BEST days [0].

Nowadays, it doesn't make much sense to limit the packet size.  In
addition, this restriction doesn't exist in ping6(8) and on NetBSD,
OpenBSD and Linux.

And one more interesting thing, UMS [1] uses this feature to estimate
the client's bandwidth to optimize the streaming experience.

[0] https://github.com/freebsd/freebsd-src/commit/526f06b278d9252add168aa18b60242c08771165
[1] UMS: https://github.com/UniversalMediaServer/UniversalMediaServer

Discussed-with: dillon

7 weeks agowg: Add comment for noise_remote_index() in handling cookie replies
Aaron LI [Wed, 28 Feb 2024 15:57:34 +0000 (23:57 +0800)]
wg: Add comment for noise_remote_index() in handling cookie replies

noise_remote_index() will lookup the index for both remote and keypair
entries.  Add a comment to explain why this double lookup is necessary.

Credit: Jason A. Donenfeld

7 weeks agowg: Disable auto link-local configuration and DAD
Aaron LI [Sun, 3 Mar 2024 08:20:20 +0000 (16:20 +0800)]
wg: Disable auto link-local configuration and DAD

They're useless for this virtual interface.

Actually, this code presents in the FreeBSD version but was removed
during the porting; now add it back.

7 weeks agostf(4): Set ND6 flags to disable auto link-local and DAD
Aaron LI [Sun, 3 Mar 2024 08:18:03 +0000 (16:18 +0800)]
stf(4): Set ND6 flags to disable auto link-local and DAD

Clear the 'ND6_IFF_AUTO_LINKLOCAL' flag and set the 'ND6_IFF_NO_DAD'
flag for the stf(4) interface after if_attach().  In this way, the
quirks in in6_ifattach() is removed.

7 weeks agonetinet6: Improve in6if_do_dad() by checking IFF_MULTICAST
Aaron LI [Sun, 3 Mar 2024 08:11:19 +0000 (16:11 +0800)]
netinet6: Improve in6if_do_dad() by checking IFF_MULTICAST

DAD requires multicast support.

Obtained from FreeBSD.

7 weeks ago<sys/param.h>: Bump __DragonFly_version for the netinet6 changes
Aaron LI [Sun, 3 Mar 2024 08:10:04 +0000 (16:10 +0800)]
<sys/param.h>: Bump __DragonFly_version for the netinet6 changes

7 weeks agonetinet6: Improve flexibility of receiving Router Advertisement
Aaron LI [Sun, 3 Mar 2024 07:57:05 +0000 (15:57 +0800)]
netinet6: Improve flexibility of receiving Router Advertisement

Convert the sysctl 'net.inet6.ip6.accept_rtadv' to be the default value
of the per-interface flag 'ND6_IFF_ACCEPT_RTADV', making it no longer a
global knob.  Now the receiving RA is allowed if ip6_forwarding==0 *and*
'ND6_IFF_ACCEPT_RTADV' is set on that interface.

ndp(8) already supports to toggle the 'ND6_IFF_ACCEPT_RTADV' flag via
the 'accept_rtadv' option.

Derived from FreeBSD.

7 weeks agonetinet6: Add per-interface flag ND6_IFF_NO_DAD flag to disable DAD
Aaron LI [Sat, 2 Mar 2024 16:04:34 +0000 (00:04 +0800)]
netinet6: Add per-interface flag ND6_IFF_NO_DAD flag to disable DAD

An interface that has no link-local address should also have DAD
disabled.  So this flag is generally set for an interface that has the
ND6_IFF_AUTO_LINKLOCAL flag cleared.

Derived from FreeBSD.

7 weeks agonetinet6: Improve handling of auto link-local address configuration
Aaron LI [Sat, 2 Mar 2024 15:59:12 +0000 (23:59 +0800)]
netinet6: Improve handling of auto link-local address configuration

Add a new per-interface flag 'ND6_IFF_AUTO_LINKLOCAL' and convert the
sysctl 'net.inet6.ip6.auto_linklocal' to be the default value of this
new per-interface flag.  This allows to fine control the automatic
link-local address configuration on a per-interface basis.

Update ndp(8) to support to toggle the 'ND6_IFF_AUTO_LINKLOCAL' flag by
adding the 'auto_linklocal' option.

Document the 'IPV6CTL_AUTO_LINKLOCAL' option in the inet6(4) man page.

Derived from FreeBSD.

7 weeks agonetstat(1): Increase column width of interface name
Aaron LI [Sat, 2 Mar 2024 12:33:56 +0000 (20:33 +0800)]
netstat(1): Increase column width of interface name

Given that the interface can be renamed, the interface name may be
generally longer, so increase the column width of interface name by 2
characters (from 6 to 8).

Referred to FreeBSD.

7 weeks agonetinet6: Remove nonexistent IFT_DUMMY from in6if_do_dad()
Aaron LI [Fri, 1 Mar 2024 05:35:38 +0000 (13:35 +0800)]
netinet6: Remove nonexistent IFT_DUMMY from in6if_do_dad()

Meanwhile, fix typos in the comment.

7 weeks ago<sys/mbuf.h>: Remove the obsolete 'm_pkthdr.loop_cnt' field
Aaron LI [Sun, 3 Mar 2024 08:30:56 +0000 (16:30 +0800)]
<sys/mbuf.h>: Remove the obsolete 'm_pkthdr.loop_cnt' field

It was missed from commit b44c913f6ebc2ff5523e1bf83880522a3bded4fe.

7 weeks agousr.sbin/makefs/msdos: Fix broken extra-directory case
Tomohiro Kusumi [Fri, 1 Mar 2024 08:26:47 +0000 (00:26 -0800)]
usr.sbin/makefs/msdos: Fix broken extra-directory case

Same fix as d541bdf518b24b3f6deb5a62fab0ca6492be4470 for HAMMER2.

8 weeks agonet: Reimplement packet loop detection based on mbuf tags
Aaron LI [Wed, 28 Feb 2024 14:50:43 +0000 (22:50 +0800)]
net: Reimplement packet loop detection based on mbuf tags

The original naive implementation based on mbuf 'm_pkthdr.loop_cnt' was
flawed:

* There were likely some code paths that allocated mbufs failed to
  initialize the 'loop_cnt' to be zero.  This caused unwanted packet
  drops in gif(4), as reported by Kyle Butt (iteratee).

* The 'loop_cnt' was system-wide and thus cannot distinguish between the
  nesting of specific drivers.  For example, it would break an actually
  valid setup that makes use of both gif(4) and gre(4).

As a result, follow the FreeBSD's way and reimplement the packet loop
detection based on mbuf tags.  Each driver is allocated a unique mbuf
tag cookie, and thus a unique mbuf tag will be created to track the
nesting level of each driver.

The if_tunnel_check_nesting() was derived from FreeBSD but I changed it
to use only one mbuf tag for each cookie (i.e., driver).  Although it
can no longer directly detect that a packet loops through the same
interface, it would still be prevented as that would lead to infinite
recursions.

Update gif(4), gre(4) and wg(4) to use the new loop detection facility.

Bump __DragonFly_version as well.

Reported-by: Kyle Butt (iteratee)
8 weeks agowg: Update wg_handshake() to increase 'ierrors' on error
Aaron LI [Tue, 20 Feb 2024 17:21:07 +0000 (01:21 +0800)]
wg: Update wg_handshake() to increase 'ierrors' on error

Meanwhile, add a comment for 'pkt->p_mbuf' update after m_pullup().

8 weeks agowg: Improve error handling in wg_output()
Aaron LI [Tue, 20 Feb 2024 14:47:40 +0000 (22:47 +0800)]
wg: Improve error handling in wg_output()

- Don't send ICMP error if the packet looped, avoiding infinite loops.
- Move the packet loop detection upper to optimize the logic a bit.
- Integrate xmit_err() into wg_output() to simplify the error handling.
- Just increase 'oerrors' and don't use 'oqdrops', which doesn't really
  make more sense here.

8 weeks agoawk(1): Raise WARNS to 5 for internal 'maketab' tool
Aaron LI [Wed, 28 Feb 2024 05:00:51 +0000 (13:00 +0800)]
awk(1): Raise WARNS to 5 for internal 'maketab' tool

8 weeks agocontrib/awk: Update README.DELETED
Aaron LI [Wed, 28 Feb 2024 05:00:29 +0000 (13:00 +0800)]
contrib/awk: Update README.DELETED

8 weeks agoMerge branch 'vendor/AWK'
Aaron LI [Wed, 28 Feb 2024 02:47:43 +0000 (10:47 +0800)]
Merge branch 'vendor/AWK'

8 weeks agovendor/awk: upgrade from 20220912 to 20240122 vendor/AWK
Aaron LI [Wed, 28 Feb 2024 01:11:54 +0000 (09:11 +0800)]
vendor/awk: upgrade from 20220912 to 20240122

This version is described in "The Awk Programming Language", Second
Edition, by Al Aho, Brian Kernighan, and Peter Weinberger
(Addison-Wesley, 2024, ISBN-13 978-0138269722, ISBN-10 0138269726).

Features:
- support UTF-8 input
- support CSV (comma-separated values) input

Fixes:
- see FIXES and FIXES.1e
- see commit history at: https://github.com/onetrueawk/awk/commits/master

8 weeks agorc.d: Rewrite motd to clean it up
Aaron LI [Tue, 27 Feb 2024 15:11:18 +0000 (23:11 +0800)]
rc.d: Rewrite motd to clean it up

No functional change.

8 weeks agorc.d: Fix eval bug in routed script
Aaron LI [Tue, 27 Feb 2024 13:59:40 +0000 (21:59 +0800)]
rc.d: Fix eval bug in routed script

The 'eval' command would fail when 'router_flags' had space or other
special character in it.  Fix it.

Meanwhile, update the 'eval' command in route6d script to use the same
format for consistency, although it was correct.

8 weeks agorc.d: Clean up and tweak dependencies
Aaron LI [Tue, 27 Feb 2024 13:56:48 +0000 (21:56 +0800)]
rc.d: Clean up and tweak dependencies

- Don't use real entries in the dummy scripts; instead, use the dummy
  entries in the real rc scripts.  This makes the dummy entries clean.

- Use dummy entries instead of one/multiple real entries (such as 'root')
  wherever appropriate.

- Use 'REQUIRE' instead of 'BEFORE' wherever appropriate, which makes it
  clearer to understand.

- Simplify and clean up various dependencies; remove unnecessary
  requires.

- Tweak dependencies of some scripts to make the startup order more
  sensible.  For example, make 'hostname' and 'ip6addrctl' run a bit
  later, after 'FILESYSTEMS' but before 'NETWORKING'.

Referred to FreeBSD for some changes.

8 weeks agorc.d: Make ldconfig, netoptions and rtsold run earlier
Aaron LI [Tue, 27 Feb 2024 04:04:58 +0000 (12:04 +0800)]
rc.d: Make ldconfig, netoptions and rtsold run earlier

- Make 'ldconfig' before SERVERS
- Make 'netoptions' and 'rtsold' before NETWORKING

The new ordering should make more sense.

Referred to FreeBSD.

8 weeks agorc.d: Various style and whitespace cleanups
Aaron LI [Tue, 27 Feb 2024 01:54:49 +0000 (09:54 +0800)]
rc.d: Various style and whitespace cleanups

No functional changes.

8 weeks agorc.d: Make autofs(5) scripts run earlier
Aaron LI [Mon, 26 Feb 2024 10:07:14 +0000 (18:07 +0800)]
rc.d: Make autofs(5) scripts run earlier

This allows some daemons to access automounted shares.

Obtained-from: FreeBSD

8 weeks agorc.d: Add FILESYSTEMS dummy dependency
Aaron LI [Fri, 23 Feb 2024 05:52:52 +0000 (13:52 +0800)]
rc.d: Add FILESYSTEMS dummy dependency

This dummy dependency ensures that root and other critical local file
systems are mounted.  One of the intentions to add this dummy dependency
is to fix the warnings caused by some ports' rc script wrongly dependent
on this script.

Update the rc.8 man page; meanwhile, tweak the description of other
dummy dependencies a bit.

Updating existing rc scripts to make use of FILESYSTEMS is coming in
another commit.

Obtained-from: FreeBSD

8 weeks agoAdded support for Realtek E2600 (Killer Ethernet Adapter E2600).
Shingy Shabooya [Wed, 21 Feb 2024 14:52:41 +0000 (21:52 +0700)]
Added support for Realtek E2600 (Killer Ethernet Adapter E2600).

2 months agorc.d/wg: Simplify the quote() in the awk script
Aaron LI [Wed, 21 Feb 2024 05:16:17 +0000 (13:16 +0800)]
rc.d/wg: Simplify the quote() in the awk script

2 months agorc.d/wg: Fix issue in parsing a config file of no peers
Aaron LI [Wed, 21 Feb 2024 05:10:22 +0000 (13:10 +0800)]
rc.d/wg: Fix issue in parsing a config file of no peers

A config file may have only the [interface] section but no [peer]
sections.

2 months agorc.d/wg: Fix the mistake of the 'wg_config_dir' variable
Aaron LI [Tue, 20 Feb 2024 15:56:59 +0000 (23:56 +0800)]
rc.d/wg: Fix the mistake of the 'wg_config_dir' variable

Remove the local 'WG_CONFIG_DIR' variable that was used during the
development.  Use the 'wg_config_dir' variable loaded from 'rc.conf'
instead.

2 months agocrypto: Move blake2s_hmac() to its only user wg_noise.c
Aaron LI [Tue, 20 Feb 2024 13:51:24 +0000 (21:51 +0800)]
crypto: Move blake2s_hmac() to its only user wg_noise.c

The blake2s_hmac() is simply an ad-hoc HMAC implementation using the
BLAKE2s hash algorithm.  It's not generic; a proper solution is to
implement the HMAC construction that supports any hash algorithms.
Therefore, it's better to move blake2s_hmac() to wg_noise.c as
noise_hmac().

See also: https://git.zx2c4.com/wireguard-freebsd/commit/?id=5c5832279855722b939a381b9a291dc5ca2ee52e

2 months agoifconfig(8): Minor cleanups to ifwg.c
Aaron LI [Tue, 20 Feb 2024 13:41:10 +0000 (21:41 +0800)]
ifconfig(8): Minor cleanups to ifwg.c

Meanwhile, fix a minor typo in the error message of '-wgpka' command.

2 months agowg: Write rc(8) script to easily manage wg(4) interfaces
Aaron LI [Wed, 14 Feb 2024 15:17:04 +0000 (23:17 +0800)]
wg: Write rc(8) script to easily manage wg(4) interfaces

This "wg" rc(8) script is somewhat similar to the "wg-quick" tool on
Linux/FreeBSD.  It can be used to quickly start/stop the wg(4)
interfaces according to the wg.conf(5) configuration files in the
"/etc/wireguard" directory.

The syntax of wg.conf(5) configuration file is very similar to that
of "wg-quick" but with necessary changes and minor additions.  See
wg.conf(5) for details.

On the one hand, the new "wg_enable" and "wg_interfaces" variables in
"/etc/rc.conf" can be used to auto-configure the wg(4) interfaces during
the system startup.  See rc.conf(5) for more details.

On the other hand, this "wg" script can be manually called from the
command-line to start/stop the wg(4) interfaces.

Thanks to swildner for reviewing the man page.

2 months agorc.conf.5: Reorder "rc_conf_files" to group "rc_*" variables
Aaron LI [Wed, 14 Feb 2024 15:20:23 +0000 (23:20 +0800)]
rc.conf.5: Reorder "rc_conf_files" to group "rc_*" variables

2 months agowg: Change cpu_sfence() to release store + acquire load pair
Aaron LI [Thu, 8 Feb 2024 16:45:09 +0000 (00:45 +0800)]
wg: Change cpu_sfence() to release store + acquire load pair

Although DragonFly is currently x86-only and this is actually
unnecessary, update to use the store+load pairs for better
portability.

2 months agowg: Convert BPF_MTAP_AF() macro to inline function wg_bpf_ptap()
Aaron LI [Thu, 8 Feb 2024 16:25:25 +0000 (00:25 +0800)]
wg: Convert BPF_MTAP_AF() macro to inline function wg_bpf_ptap()

2 months agowg: Add RXCSUM support to avoid unnecessary checksum validation
Aaron LI [Thu, 8 Feb 2024 09:41:16 +0000 (17:41 +0800)]
wg: Add RXCSUM support to avoid unnecessary checksum validation

The packet that is about to be delivered in is authentic as ensured by
the AEAD tag, so we can tell the networking stack that this packet has
valid checksums and thus is unnecessary to check again.

Therefore, implement RXCSUM support for the wg interface, and update the
ioctl() to support to enable/disable this feature.

Meanwhile, move the mbuf flags clearance code just before the delivery,
i.e., netisr_queue() and wg_send().

2 months agowg: Track noise_{local,remote,keypair} allocations to detect leaks
Aaron LI [Thu, 8 Feb 2024 03:19:03 +0000 (11:19 +0800)]
wg: Track noise_{local,remote,keypair} allocations to detect leaks

Use lists to track the allocations of noise_{local,remote,keypair}
structs, and then assert that all of them have been freed upon the
module deinitialization.

Enclose the code within 'INVARIANTS' macro, so that it can be just
ignored when performance is important.

2 months agowg: Some code cleanups, minor improvements and comment updates
Aaron LI [Wed, 7 Feb 2024 14:25:49 +0000 (22:25 +0800)]
wg: Some code cleanups, minor improvements and comment updates

- Clean up some code logics to make the conditional flow and error
  handling more smooth.
- Add and update various comments to make the code more understandable.
  A large fraction of the comments are derived from the WireGuard code
  in Linux/OpenBSD, and from commit messages.

2 months agowg: Minor improvements to wg_ioctl_set()
Aaron LI [Wed, 7 Feb 2024 14:02:40 +0000 (22:02 +0800)]
wg: Minor improvements to wg_ioctl_set()

- Skip allowed IPs removal for a new peer.
- Try and send staged packets if the interface is UP.

Referred to the Linux version of WireGuard.

2 months agowg: Improve noise_keypair_received_with()
Aaron LI [Wed, 7 Feb 2024 13:56:46 +0000 (21:56 +0800)]
wg: Improve noise_keypair_received_with()

- Optimize the check flow by directly returning if the keypair is of an
  initiator.
- Add a brief function description and another comment.

2 months agowg: Refactor noise_keep_key_fresh_{send,recv}() functions
Aaron LI [Wed, 7 Feb 2024 13:06:45 +0000 (21:06 +0800)]
wg: Refactor noise_keep_key_fresh_{send,recv}() functions

These two functions were derived from the Linux version where called
keep_key_fresh() in {send,receive}.c.  However, they behaved differently
from their Linux version; i.e., they only checked whether the keypair
needed a refresh but didn't actually perform the refreshing.  So their
name was actually misleading.

Refactor these two functions and combine them into a single function
called noise_keypair_should_refresh(), with an extra parameter to
distinguish between the sending and receiving cases.

2 months agowg: Refactor cookie functions to make cookie_{checker,maker} opaque
Aaron LI [Mon, 5 Feb 2024 16:13:15 +0000 (00:13 +0800)]
wg: Refactor cookie functions to make cookie_{checker,maker} opaque

- Rename cookie_{checker,maker}_init() to cookie_{checker,maker}_alloc(),
  in symmetry with cookie_{checker,maker}_free().
- Make cookie_{checker,maker} structs opaque, and move them from
  wg_cookie.h to wg_cookie.c.
- Update if_wg.c and selftest code accordingly.

2 months agowg: Refactor and improve determine_af_and_pullup() and xmit_err()
Aaron LI [Sat, 3 Feb 2024 14:33:08 +0000 (22:33 +0800)]
wg: Refactor and improve determine_af_and_pullup() and xmit_err()

Meanwhile, clean up and improve wg_output(); simplifying the error
handling a lot.

2 months agowg: Cleanup static function prototypes in if_wg.c
Aaron LI [Sat, 3 Feb 2024 11:21:32 +0000 (19:21 +0800)]
wg: Cleanup static function prototypes in if_wg.c

- Group the static function prototypes.
- Remove unnecessary prototypes.
- Style cleanups.

2 months agowg: Improve wg_clone_destroy() and wg_down()
Aaron LI [Sat, 3 Feb 2024 06:49:35 +0000 (14:49 +0800)]
wg: Improve wg_clone_destroy() and wg_down()

- Move the cancellation of tasks from wg_clone_destroy() to wg_down(),
  which is actually more appropriate.
- Just call wg_down() in wg_clone_destroy() to reduce duplicate code.
- No need to call if_purgeaddrs_nolink(), as it will be called by
  if_detach().
- Detach and free the interface before destroying the aip radix trees,
  in order to avoid possible panics.

2 months agowg: Simplify socket so_lock scope and init/uninit
Aaron LI [Sat, 3 Feb 2024 05:28:53 +0000 (13:28 +0800)]
wg: Simplify socket so_lock scope and init/uninit

Move the init/uninit of the so_lock to wg_socket_init() and
wg_socket_uninit() respectively, making its scope more clear.

2 months agowg: Clean up noise_keypair_counter_check() a bit
Aaron LI [Tue, 30 Jan 2024 15:49:50 +0000 (23:49 +0800)]
wg: Clean up noise_keypair_counter_check() a bit

Remove '++recv' together with the '+ 1' calculation, so the code nows
becomes more understandable, and the conditional of
'kp->kp_counter_recv >= REJECT_AFTER_MESSAGES' also becomes the same as
the one in noise_keypair_decrypt(); so it reduces the confusion between
them.  The selftest is also passed.  (Referred to OpenBSD)

In addition, add brief comments to describe the 'kp_counter_send' and
'kp_counter_recv'.

2 months agowg: Improve noise_keypair_counter_check() to return different errnos
Aaron LI [Tue, 30 Jan 2024 15:12:58 +0000 (23:12 +0800)]
wg: Improve noise_keypair_counter_check() to return different errnos

Use different errnos (i.e., EINVAL, ESTALE, EEXIST) for different
failure cases in noise_keypair_counter_check().  Meanwhile, update the
selftest code to test this function more vigorously.

In addition, add function description and comments to help understand
it.

2 months agowg: Reorganize wg_packet and wg_queue functions
Aaron LI [Tue, 23 Jan 2024 15:50:18 +0000 (23:50 +0800)]
wg: Reorganize wg_packet and wg_queue functions

- Move the wg_packet and wg_queue functions to the beginning part, which
  seems more appropriate.
- Remove the unnecessary function prototypes.
- Add a brief description about the various queues, especially about the
  parallel and serial queues.
- Add several more comments to help understand the code.

No functional changes.

2 months agowg: Make peer ID start from 1 (instead of 0)
Aaron LI [Tue, 23 Jan 2024 15:45:55 +0000 (23:45 +0800)]
wg: Make peer ID start from 1 (instead of 0)

Use '++peer_counter' instead of 'peer_counter++' to generate the peer
ID, so make it start from 1.

Since 'peer_counter' is only used in wg_peer_create(), so move it into
this function.  In addition, drop the unnecessary 'volatile' qualifier,
because it's accessed with the 'sc_lock' exclusively hold.

2 months agowg: Rename wg_softc_*() functions to wg_*_worker()
Aaron LI [Mon, 15 Jan 2024 14:44:22 +0000 (22:44 +0800)]
wg: Rename wg_softc_*() functions to wg_*_worker()

This makes the code more understandable.

Referred-to: OpenBSD