Commit graph

236 commits

Author SHA1 Message Date
Gleb Smirnoff
57171f6d14 netlink: with debugging log event when a socket is stuck 2025-02-05 10:09:06 -08:00
Gleb Smirnoff
ea03febed5 netlink: fix a panic with verbose debugging printfs
While here improve general debugging of this function.

Fixes:	660bd40a59
2025-02-05 10:09:06 -08:00
Gleb Smirnoff
06cf365165 netlink: provide genl_unregister_group()
Cause generic netlink group IDs are dynamic, we go through all sockets
and unsubscribe from the group that goes away.  Otherwise they could be
surprisingly find themselves subscribed to a group created later.
2025-02-05 10:09:06 -08:00
Gleb Smirnoff
ee507b70f1 netlink: refactor KPI for generic Netlink modules
Now that the family and group are completely private to netlink_generic.c,
provide a simple and robust KPI, that would require very simple guarantees
from both KPI and the module:

* Strings are used only for family and group registration, that return ID:
  uint16_t genl_register_family(const char *name, ...
  uint32_t genl_register_group(uint16_t family, const char *name, ...
* Once created families and groups are guaranteed to not disappear and
  be addressable by their ID.
* All subsequent calls, including deregistration shall use ID.

Reviewed by:		kp
Differential Revision:	https://reviews.freebsd.org/D48845
2025-02-05 10:09:06 -08:00
Gleb Smirnoff
ef3991d70d netlink: don't store an extra pointer to so_cred 2025-02-04 16:54:21 -08:00
Gleb Smirnoff
164dec88e8 netlink: rename functions that manipulate group membership of a pcb
Use nlp_ prefix for them, cause they operate on single pcb.  Use words
"join" and "leave" instead of "add" and "del", cause we aren't creating
or destroying groups, we are just joining or leaving.
Really assert locks instead of just having "locked" suffix.
2025-02-04 16:54:21 -08:00
Gleb Smirnoff
841dcdcd3f netlink: initialize VNET context with VNET_SYSINIT()
With the initial check-in netlink(4) was very conservative with regards to
using memory and intrusiveness to the kernel and network stack. In
particular it would initialize the VNET context only on the first actuall
call to socket(PF_NETLINK), saving on allocation of a struct nl_control of
size 224 bytes.

Now it is clear that netlink(4) is primary citizen of FreeBSD, with a set
of system tools using it.  So resort to normal VNET_SYSINIT() and with
that shave a lot of complexity, since after the change V_nl_ctl is
immutable.
2025-02-04 16:54:21 -08:00
Gleb Smirnoff
56304579c1 netlink: remove _pru_ prefix from protosw method names
This is a relic that doesn't has any value now.
2025-02-04 16:54:20 -08:00
Gleb Smirnoff
753a4acd09 netlink: make struct genl_family and genl_group private 2025-02-04 11:52:35 -08:00
Gleb Smirnoff
cdacb12065 netlink/route: validate family attribute
PR:			283818
2025-01-29 15:48:55 -08:00
Gleb Smirnoff
031fbf8dc9 netlink/route: fix fib number validation in old Linux compat mode
The value passed via old field also needs to be validated.

PR:			283848
Fixes:			f34aca55ad
2025-01-29 15:48:55 -08:00
Gleb Smirnoff
49a6e21341 netlink/route: fix nlattr_get_multipath() to check length
of supplied nexthop sub-attributes.  While here, use unsigned types for
length calculations and improve style(9).

PR:			283860
2025-01-29 15:48:55 -08:00
Gleb Smirnoff
876b88a448 netlink: consistently use unsigned types in the parser
Use uint32_t for anything that is derived from message length, use
uint16_t for anything that represents attribute length and use u_int for
array indices.
2025-01-29 15:48:55 -08:00
Gleb Smirnoff
810c122695 netlink: use u_int as argument for ifnet_byindex() 2025-01-29 15:48:55 -08:00
Gleb Smirnoff
8b094a9801 netlink: style(9) pass over message parsing code
Mostly breaking long lines, few other changes.
2025-01-29 15:48:55 -08:00
Gleb Smirnoff
d521362f8f netlink: fix build
The commit checked in had a dependency on not yet reviewed changes.
Revert them, but the main gist of the commit is not reverted.

Fixes:	f2a4eed3e1
2025-01-20 13:38:02 -08:00
Gleb Smirnoff
ddc7fd6641 netlink: provide snl_clone() to create a secondary snl state
The function will clone an existing snl_state().  The clone points at the
same socket, but has a separate allocator.  Closing a clone frees the
memory, but doesn't close the socket.  Such clones are handy in a
multithreaded process that has a single socket with multiple writers and a
serialized reader.  It can be easily extended to support multiple readers,
too, but now there is no use.

Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D48568
2025-01-20 13:15:39 -08:00
Gleb Smirnoff
f2a4eed3e1 netlink: underscore snl_get_genl_family_info() to discourage its use
This function uses already supposedly opaque struct _getfamily_attrs as
the argument and it fills it with pointers to volatile memory, which makes
it is unsafe for general use.  While here also underscore structures that
hang off the struct _getfamily_attrs.

Small programs like powerd(8) and RPC daemons are converted to use
snl_get_genl_mcast_group() and/or snl_get_genl_family().  The genl(1)
utility was fixed not to mix its own parsers with parsers declared in
netlink_snl_generic.h.

Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D48480
2025-01-20 12:53:37 -08:00
Gleb Smirnoff
fb63082c0c netlink: provide snl(3) API for variable length raw data attribute
Rename supposedly internal _snl_reserve_msg_attr() into an official
snl_reserve_msg_attr_raw(), that would return pointer to a struct
nlattr followed by allocated memory.  Adjust the snl_reserve_msg_attr()
macro to work on top of that function.

Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D48311
2025-01-20 12:53:07 -08:00
Kristof Provost
7c882c69a4 libpfctl: use snl_f_p_empty instead of declaring own empty array
Just as we did in the kernel in e9255dafa1

Suggested by:	glebius
Reviewed by:	glebius, melifaro
Sponsored by:	Rubicon Communications, LLC ("Netgate")
Differential Revision:	https://reviews.freebsd.org/D48460
2025-01-16 10:05:09 +01:00
Gleb Smirnoff
90b1df4f4d netlink: small cleanup of generic snl(3)
- Add const qualifiers for the family/group name pointers.
- Break & tab long lines.

No functional changes.
2025-01-15 23:22:19 -08:00
Gleb Smirnoff
c3df2fa9a7 netlink: snl_create_msg_request() may fail due to ENOMEM 2025-01-15 23:21:58 -08:00
Gleb Smirnoff
6ed3486980 netlink: avoid underflow of groups bitset index
The subtraction is absolutely unnecessary and created an underflow with
926d2eadcb.  I don't see why it was useful before 926d2eadcb and even
before edf5608bfe.  The bitset addresses bits from zero to
NLP_MAX_GROUPS-1.  Note that check of user supplied argument for
NETLINK_ADD_MEMBERSHIP and NETLINK_DROP_MEMBERSHIP socket options is
already correct !(optval >= NLP_MAX_GROUPS).

Fixes:	926d2eadcb
2025-01-13 13:27:53 -08:00
Andrey V. Elsukov
3ce003c8b6 netlink: restore the ability to delete PINNED routes
route(8) had such ability before migration to netlink. To be able
to delete PINNED routes we pass RTM_F_FORCE to rib_del_route_px()
when userland uses RTF_PINNED flag with RTM_DELETE command.
This is hackish way for route(8) tool, that third-party software
usually doesn't use.

PR:		279988
Tested by:	franco at opnsense org
Discussed with:	glebius
MFC after:	10 days
Differential Revision:	https://reviews.freebsd.org/D46301
2025-01-11 11:25:50 +03:00
Gleb Smirnoff
926d2eadcb netlink: some refactoring of NETLINK_GENERIC layer
- Statically initialize control family/group.  This removes extra startup
code and provides a strong guarantee that they reside at the 0 index of
the respective arrays.  Before a genl_register_family() with a higher
SYSINIT order could try to hijack index 0.

- Remove the family_id field completely.  Now the family ID as well as
group ID are array indices and there is basically no place for a mistake.
Previous code had a bug where a KPI user could induce an ID mismatch.

- Merge netlink_generic_kpi.c to netlink_generic.c.  Both files are small
and now there is more dependency between the control family and the family
allocator. Ok'ed by melifaro@.

Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D48316
2025-01-10 20:59:29 -08:00
Gleb Smirnoff
26d1ad5a44 netlink: snl_create_genl_msg_request() may fail due to ENOMEM
Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D48310
2025-01-10 20:58:08 -08:00
Gleb Smirnoff
bbe6559cf9 netlink: fix size comparison
We want to check the size of the header, not a pointer to it.

Reviewed by:		melifaro, markj
Differential Revision:	https://reviews.freebsd.org/D48309
2025-01-10 20:57:55 -08:00
Gleb Smirnoff
8a8d095718 netlink: add snl(3) primitive to obtain group ID
using the family name and the group name as lookup arguments.

Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D48308
2025-01-10 20:55:50 -08:00
Gleb Smirnoff
0fda4ffd69 netlink: augment group writer with priv(9) argument
This will allow to broadcast messages visible only to priveleged
subscribers.

Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D48307
2025-01-10 20:55:39 -08:00
Gleb Popov
33c670c373 netlink.h: Align macro declarations with tabs
Sponsored by:	Future Crew, LLC
Approved by:	melifaro
Differential Revision:	https://reviews.freebsd.org/D47333
2024-12-10 21:12:38 +03:00
Gleb Popov
5143d8c443 netlink: Use __align_up() instead of homegrown roundup2 macro
Sponsored by:	Future Crew, LLC
Approved by:	melifaro
Differential Revision:	https://reviews.freebsd.org/D47333
2024-12-10 21:12:32 +03:00
Gleb Popov
9df901c8f8 netlink: Pop NLMSG_ALIGNTO and NLMSG_ALIGN out of the #ifndef _KERNEL block
Sponsored by:	Future Crew, LLC
Approved by:	melifaro
Differential Revision:	https://reviews.freebsd.org/D47333
2024-12-10 21:12:09 +03:00
Gleb Popov
c7919fb92d netlink: Do not cast to int in NLMSG_HDRLEN and _NLMSG_LEN
Approved by: melifaro
Sponsored by: Future Crew, LLC
Differential Revision: https://reviews.freebsd.org/D47333
2024-12-10 21:11:16 +03:00
Gleb Smirnoff
29f6150256 netlink: use nitems() and roundup(2) from param.h
While here style nested includes (kernel ones go first).

Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D47557
2024-12-03 12:04:39 -08:00
Gleb Smirnoff
fe048349c6 netlink: use proper argument types in genl_register_family()
Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D47553
2024-12-03 12:04:35 -08:00
Gleb Smirnoff
6380058fe5 netlink: use correct uint16_t type for attribute type & length
Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D47552
2024-12-03 12:04:30 -08:00
Gleb Smirnoff
f1c6edba88 netlink: use size_t through the allocation KPI
This fixes some signedness bugs and potential underflows.  The length of
nl_buf is still limited by UINT_MAX and this is asserted now.

Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D47551
2024-12-03 12:04:27 -08:00
Gleb Smirnoff
0601c0f989 netlink: check buffer length fits into u_int
We may increase it to size_t later, KPI allows that already, but
doesn't seem to be needed today.

Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D47550
2024-12-03 12:04:22 -08:00
Gleb Smirnoff
a034c0aecc netlink: refactor writer initialization KPI
o Allow callers to initialize a writer that will malloc(9) with M_WAITOK.
o Use size_t for expected malloc size.
o Use correct types to initialize a group writer.
o Rename functions into nl_writer_ namespace instead of nlmsg_, cause
  they are working on nl_writer, not on nlmsg.
o Make the KPI responsible to sparsely initialize the writer structure.
o Garbage collect chain writer.  Fixes 17083b94a9.

All current consumers are left as is, however some may benefit from
M_WAITOK allocation as well as supplying a correct expected size.

Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D47549
2024-12-03 12:04:18 -08:00
Gleb Smirnoff
edf5608bfe netlink: use bitset(9)
Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D47548
2024-12-03 12:04:14 -08:00
Gleb Smirnoff
ac84ce05c1 netlink: consistently use uint16_t for family id
Reviewed by:		melifaro
Differential Revision:	https://reviews.freebsd.org/D47547
2024-12-03 12:04:09 -08:00
Mark Johnston
0289db3259 netlink/route: Fix the argument list for rtnl_handle_iflink()
This function is registered as a ifnet_link_event and so should have the
corresponding argument list.

PR:		282870
Reported by:	nakayamakenjiro@gmail.com
MFC after:	1 week
2024-11-22 14:03:40 +00:00
Justin Hibbits
b224af946a netlink: Don't directly access ifnet members
Summary:
Remove the final direct access of struct ifnet members from netlink.
Since only the first address is used, create the iterator and then free,
without fully iterating.

Reviewed By:	kp
Sponsored by:	Juniper Networks, Inc.
Differential Revision: https://reviews.freebsd.org/D42972
2024-11-13 14:30:58 -05:00
Konrad Witaszczyk
bc06c51419 netinet: correct SIOCDIFADDR{,_IN6} calls to use {,in6_}ifreq
The SIOCDIFADDR{,_IN6} ioctls take an ifreq structure object, not an
ifaliasreq/in_aliasreq/in6_aliasreq structure object, as their argument.
As opposed to ifaliasreq/in_aliasreq/in6_aliasreq used by
SIOCAIFADDR{,_IN6}, the ifreq/in6_ifreq structures used by the
SIOCDIFADDR{,_IN6} ioctls do not include a separate field for a
broadcast address and other values required to add an address to a
network interface with SIOCAIFADDR{,_IN6}.

Whilst this issue is not specific to CHERI-extended architectures, it
was first observed on CheriBSD running on Arm Morello. For example,
incorrect calls using the in6_aliasreq object result in CHERI capability
violations. A pointer to the ifra_addr field in in6_aliasreq cast to the
ifru_addr union member of in6_ifreq results in bounds being set to the
union's larger size. Such bounds exceed the bounds of of in6_aliasreq
object and the bounds-setting instruction clears a tag of the object's
capability.

Reviewed by:	brooks, kp, oshogbo
Accepted by:	oshogbo (mentor)
Reported by:	CHERI
Obtained from:	CheriBSD
Differential Revision: https://reviews.freebsd.org/D46016
2024-07-22 14:17:21 +00:00
Mark Johnston
e536b197c0 netlink: Wrap long lines
No functional change intended.

MFC after:	1 week
Sponsored by:	Klara, Inc.
2024-07-14 14:29:15 -04:00
Gleb Smirnoff
f34aca55ad netlink/route: provide pre-2.6.19 Linux compat shim
The old Linux used 8-bit rtm_table field of the RTM_NEWROUTE message to
specify routing table id.  Modern netlink uses RTA_TABLE 32-bit attribute.

Unfortunately, there is modern software (namely bird) that would prefer
the old API as long as the routing table id fits into 8-bit.

PR:		279662
2024-06-20 16:10:39 -07:00
Gleb Smirnoff
969cb79f5b netlink/route: reformat comment to fit 80 chars
No functional or content changes.
2024-06-20 16:10:39 -07:00
Kristof Provost
f0829825fb netlink: pass the correct arguments for SIOCDIFADDR and SIOCDIFADDR_IN6
These take struct ifreq and struct in6_ifreq respectively. Passing struct
in_aliasreq or struct in6_aliasreq means we're supplying a shorter object than
expected. While this doesn't actively break things on most architectures other
than CHERI it is still wrong.

Reported by:	CheriBSD
Event:		Kitchener-Waterloo Hackathon 202406
2024-06-06 15:45:30 +02:00
cnbatch
ff92493a4f netlink: Fix C++ compile errors
Allow these files to be included in C++ programs with careful casting to
the proper type, like C++ wants (and in a way that also works for C).

MFC After: 1 week
Reviewed by: imp
Pull Request: https://github.com/freebsd/freebsd-src/pull/1245
2024-05-24 22:31:42 -06:00
Kristof Provost
5824df8d99 pf: convert DIOCGETSTATUS to netlink
Introduce pfctl_get_status_h() because we need the pfctl_handle. In this variant
use netlink to obtain the information.

Sponsored by:	Rubicon Communications, LLC ("Netgate")
2024-04-29 16:32:23 +02:00