Commit graph

162 commits

Author SHA1 Message Date
Alexander V. Chernikov ce9f95bd83 netlink: fix vlan interface creation
MFC after:	2 weeks
2023-03-03 16:02:05 +00:00
Alexander V. Chernikov 61507ae30b netlink: add u8/u64 attribute fetcher accessors to snl(3).
MFC after:	2 weeks
2023-03-03 14:05:40 +00:00
Alexander V. Chernikov e6b4177c47 netlink: add userland name mappings to all NETLINK_ROUTE messages.
MFC after:	1 day
2023-03-01 15:24:45 +00:00
Alexander V. Chernikov 28a5d88f70 netlink: make the maximum allowed netlink socket buffer runtime tunable.
Dumping large routng tables (>1M paths with multipath) require the socket
 buffer which is larger than the currently defined limit.
Allow the limit to be set in runtime, similar to kern.ipc.maxsockbuf.

Reported by:	Marek Zarychta <zarychtam@plan-b.pwste.edu.pl>
MFC after:	1 day
2023-02-27 10:48:31 +00:00
Alexander V. Chernikov d2deebe21b netlink: fix addition of blackhole/reject routes.
* Make nhop_set_blackhole() set all necessary properties for the
 nexthop
* Make nexthops blackhole/reject based on the rtm_type netlink
 property instead of using rtflags.

Reported by:	Marek Zarychta <zarychtam@plan-b.pwste.edu.pl>
MFC after:	3 days
2023-02-23 17:43:18 +00:00
Alexander V. Chernikov b9b2184322 netlink: clear IPv6 embedded scope when dumping route gateways.
Reported by:	zarychtam@plan-b.pwste.edu.pl
MFC after:	3 days
2023-02-21 12:27:26 +00:00
Alexander V. Chernikov c7c3481621 netlink: fix IPv6 route addition with link-local gateway
Currently kernel assumes that IPv6 gateway address is in "embedded"
 form - that is, for the link-local IPv6 addresses, interface index
 is embedded in bytes 2 and 3 of the address.
Fix address embedding in netlink by wrapping nhop_set_gw() in the
 netlink-specific nl_set_nexthop_gw(), which does such embedding
 automatically.

Reported by:	Marek Zarychta <zarychtam@plan-b.pwste.edu.pl>
MFC after:	3 days
2023-02-20 14:29:48 +00:00
Alexander V. Chernikov 453c7d6803 netlink: do not crash when linux message translation fails.
CID:		1498889
MFC after:	2 weeks
2023-02-18 17:53:06 +00:00
Alexander V. Chernikov 8f7455a926 netlink: fix OOB read in genetlink
CID:		1498863
MFC after:	1 day
2023-02-18 17:53:06 +00:00
Alexander V. Chernikov 4404e840cf netlink: initialise error in nl_autobind_port().
CID:		1498877
MFC after:	2 weeks
2023-02-18 17:53:05 +00:00
Alexander V. Chernikov 3f70fca90f netlink: check result of sooptcopyin().
CID:		1498866
MFC after:	2 weeks
2023-02-18 17:53:05 +00:00
Alexander V. Chernikov 1ea58b4135 netlink: remove redundant check.
MFC after:	2 weeks
CID:		1498867
2023-02-18 17:53:05 +00:00
Alexander V. Chernikov 7cbe771f01 netlink: call genetlink notification handler upon successful family
search.

MFC after:	3 days
2023-02-17 18:16:07 +00:00
Alexander V. Chernikov 45356a1864 netlink: simplify temporary address allocation in rtnl_handle_getlink().
MFC after:	3 days
2023-02-17 18:16:07 +00:00
Alexander V. Chernikov 6694cb1f57 netlink: fix if_allocdescr() malloc options argument.
The current value was based on the initial revision of D37566 and
 was not updated afterwards.

MFC after:	1 day
2023-02-17 18:00:37 +00:00
Alexander V. Chernikov f2f7911c55 netlink: validate rtable value in RTM_<NEW|DEL|GET>ROUTE.
Reported by:	Stefan Grundmann <sg2342@googlemail.com>
MFC after:	1 day
2023-02-17 18:00:37 +00:00
Alexander V. Chernikov 86fd0bdba5 netlink: fix interface dump.
The current code missed interface addition when reallocating
 temporary buffer.
Tweak the code to perform the reallocation first and add
 interface afterwards unconditionally.

Reported by:	Marek Zarychta <zarychtam@plan-b.pwste.edu.pl>
MFC after:	3 days
2023-02-16 13:20:45 +00:00
Alexander V. Chernikov 5dd48f7184 netlink: use ifmedia to provide vlan interface operstate.
Netlink customers rely on admin and operational state when
 working with interfaces. The current implementation retuns
 "unknown" operstate for all interface types except IFT_ETHER
 and IFT_LOOP.

This change updates the code to fetch vlan operstate in the same way
 as for the ether interfaces. For the rest of the interface types,
 operstate is now mapped to the admin state.

Reported by:	Marek Zarychta <zarychtam@plan-b.pwste.edu.pl>
MFC after:	3 days
2023-02-15 20:22:47 +00:00
Alexander V. Chernikov 6d7da7c849 Revert "netlink: make netlink_snl(3) c++ friendly."
Was pushed accidentally.

This reverts commit 629d9219d9.
2023-02-12 12:17:05 +00:00
Alexander V. Chernikov 629d9219d9 netlink: make netlink_snl(3) c++ friendly. 2023-02-12 11:46:38 +00:00
Alexander V. Chernikov 25c2dd2f2c netlink: return optional metadata with the operation result.
Some operations like interface creation may need to return metadata
 - in this case, interface name - back to the caller if the operation
 is successful.
This change implements attaching an `NLMSGERR_ATTR_COOKIE` nla to the
operation reply message via `nlmsg_report_cookie()`.
Additionally, on successful interface creation, interface index and
 interface name are returned in the `IFLA_NEW_IFINDEX` and `IFLA_IFNAME
 TLVs, encapsulated in the `NLMSGERR_ATTR_COOKIE`.

Reviewed By: pauamma
Differential Revision: https://reviews.freebsd.org/D38283
MFC after:	1 week
2023-02-09 15:30:00 +00:00
Alexander V. Chernikov 5a5436eb5d netlink: do not require elevated privileges when reading arp/ndp state.
MFC after:	1 day
2023-02-09 14:12:33 +00:00
Alexander V. Chernikov 7fc9cfa57c netlink: add "netlink" to the list of kernel features
Reduce the amount of debug messages on module init/detach.

MFC after:	1 week
2023-01-30 11:23:53 +00:00
Alexander V. Chernikov 3ebccb20d5 netlink: allow path weight manipulations for single-path routes.
Add support for the scenario when user adds/deletes paths for a single
 prefix one-by-one, all with different weights.
This change adds a new FreeBSD-specific RTA attribute, NL_RTA_WEIGHT.
When dumping non-multipath routes, this attribute is added if the
 route weight is not RT_DEFAULT_WEIGHT.
When adding a new route, this attribute is parsed as a relative path
 weight.

MFC after:	2 weeks
2023-01-29 16:04:10 +00:00
Alexander V. Chernikov 95b47ba629 netlink: export nextop group kernel index when dumping multipath route.
MFC after:	2 weeks
2023-01-28 15:39:25 +00:00
Mark Johnston 7a78ae8865 netlink: Zero-initialize writer structures allocated on the stack
The prevailing pattern seems to be to simply initialize all fields to
zero.  Without this, it's possible to trigger a branch on uninitialized
memory, specifically, when testing nw->ignore_limit in
nlmsg_refill_buffer().

Initialize the writer structure in a couple of functions where this is
necessary.

Reported by:	KMSAN
Reviewed by:	melifaro
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D38213
2023-01-26 10:46:19 -05:00
Alexander V. Chernikov 10f2a38769 netlink: fix OOB write when creating attribute bitmask.
Fix wrong arithmetics by moving to the standard bitset(9) functions.

Reported by:	markj, KASAN
2023-01-21 18:03:47 +00:00
Alexander V. Chernikov 0079d177ab netlink: allow creating sockets with SOCK_DGRAM.
Some existing applications setup Netlink socket with
SOCK_DGRAM instead of SOCK_RAW. Update the manpage to clarify
that the default way of creating the socket should be with
SOCK_RAW. Update the code to support both SOCK_RAW and SOCK_DGRAM.

Reviewed By: pauamma
Differential Revision: https://reviews.freebsd.org/D38075
2023-01-21 14:58:19 +00:00
Mark Johnston 35472cb60a netlink: Fix indentation in netlink_message_writer.c
This file is indented with a mixture of tabs and spaces.  No functional
change intended.

Reviewed by:	melifaro
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D38100
2023-01-17 09:37:33 -05:00
Mark Johnston e262610007 netlink: Make the writers function table static and const
No functional change intended.

Reviewed by:	melifaro
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D38099
2023-01-17 09:37:21 -05:00
Mark Johnston d91be0f121 netlink: Zero-initialize mbuf messages
Some users of nlmsg_reserve_object() and nlmsg_reserve_data() are not
careful to fully initialize pad and reserved fields, allowing
uninitialized bytes to leak to userspace.  For example, dump_nhgrp()
doesn't set nhm->resvd = 0.

Meanwhile, nlmsg_get_ns_buf() and nlmsg_get_ns_lbuf() zero-initialize
the buffer, so nlmsg_get_ns_mbuf() is inconsistent.  Let's just make
them all behave the same here.

Reported by:	KMSAN
Reviewed by:	melifaro
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D38098
2023-01-17 09:36:54 -05:00
Alexander V. Chernikov 228c632ab3 netlink: fix compatibility with older netlink applications.
Some apps try to provide only the non-zero part of the required message
 header instead of the full one. It happens when fetching routes or
 interface addresses, where the first header byte is the family.
This behavior is "illegal" under the "strict" Netlink socket option,
 however there are many applications out there doing things in the
 "old" way.

Support this usecase by copying the provided bytes into the temporary
 zero-filled header and running the parser on this header instead.

Reported by:	Goran Mekić <meka@tilda.center>
2023-01-16 12:21:40 +00:00
Alexander V. Chernikov c1871a3372 netlink: improve RTM_GETADDR handling.
* Allow filtering by ifa_family & ifa_index.
* Add common RTM_<NEW|DEL|GET>ADDR parser
* Add tests verifying RTM_GETADDR filtering behaviour & output
* Factor out common netlink socket test methods into NetlinkTestTemplate
* Add NLMSG_DONE message handler

Reviewed By: pauamma
Differential Revision: https://reviews.freebsd.org/D37970
2023-01-08 15:06:34 +00:00
Alexander V. Chernikov 13214c601a netlink: fix ifaddr reporting.
Output the proper attributes for IPv4/IPvv6 ifaddrs:
* IFA_ADDRESS contains local address in every case except p2p,
   in that case it contains the peer address
* IFA_LOCAL contains local address. It is always present in IPv4,
   or in IPv6/p2p.
* IFA_BROADCAST contains the network broadcast address (if any)

Reported by:	Adam Wood <aswood@gmail.com>
Tested by:	Adam Wood <aswood@gmail.com>
2023-01-06 19:18:54 +00:00
Alexander V. Chernikov c89209c674 netlink: fix test-includes broken by f2c8381fce
Reported by:	many
2022-12-23 17:58:08 +00:00
Alexander V. Chernikov 3e38754131 netlink: make snl(3) headers self-contained. 2022-12-23 15:52:33 +00:00
Alexander V. Chernikov c9313a0bad netlink: allow netlink to be build in the kernel
Differential Revision: https://reviews.freebsd.org/D37781
2022-12-23 15:24:44 +00:00
Alexander V. Chernikov f6f4ba0b05 netlink: fix netlink_snl_route.h header includes 2022-12-23 15:24:44 +00:00
Alexander V. Chernikov f2c8381fce netlink: add snl(3) - simple netlink library
Reviewed by:	bapt, pauamma
Differential Revision: https://reviews.freebsd.org/D37736
2022-12-23 15:03:13 +00:00
Zhenlei Huang ab591c874b netlink: Use NET_EPOCH_[CALL|WAIT] macros
Reviewed by:	melifaro, kp
Approved by:	kp (mentor)
Differential Revision:	https://reviews.freebsd.org/D37730
2022-12-21 09:40:05 +08:00
Alexander V. Chernikov 80f03e63d6 netlink: improve interface handling
* Separate interface creation from interface modification code
* Support setting some interface attributes (ifdescr, mtu, up/down, promisc)
* Improve interaction with the cloners requiring to parse/write custom
 interface attributes
* Add bitmask-based way of checking if the attribute is present in the
message
* Don't use multipart RTM_GETLINK replies when searching for the
specific interface names
* Use ENODEV instead of ENOENT in case of failed RTM_GETLINK search
* Add python netlink test helpers
* Add some netlink interface tests

Differential Revision: https://reviews.freebsd.org/D37668
2022-12-14 19:52:35 +00:00
Alexander V. Chernikov 1bcd230f95 netlink: add interface notification on link status / flags change.
* Add link-state change notifications by subscribing to ifnet_link_event.
 In the Linux netlink model, link state is reported in 2 places: first is
 the IFLA_OPERSTATE, which stores state per RFC2863.
 The second is an IFF_LOWER_UP interface flag. As many applications rely
 on the latter, reserve 1 bit from if_flags, named as IFF_NETLINK_1.
 This flag is mapped to IFF_LOWER_UP in the netlink headers. This is done
 to avoid making applications think this flag is actually
 supported / presented in non-netlink outputs.
* Add flag change notifications, by hooking into rt_ifmsg().
 In the netlink model, notification should include the bitmask for the
 change flags. Update rt_ifmsg() to include such bitmask.

Differential Revision: https://reviews.freebsd.org/D37597
2022-12-09 11:20:07 +00:00
Alexander V. Chernikov 6ab87ec483 netlink: fix non-multipath build 2022-12-02 22:21:00 +00:00
Alexander V. Chernikov cc2be31177 netlink: store user-provided rtm_protocol
Store user-supplied source protocol in the nexthops and nexthop groups.
Protocol specification help routing daemons like bird to quickly
identify self-originated routes after the crash or restart.

Example:
```
10.2.0.0/24 via 10.0.0.2 dev vtnet0 proto bird
10.3.0.0/24 proto bird
	nexthop via 10.0.0.2 dev vtnet0 weight 3
	nexthop via 10.0.0.3 dev vtnet0 weight 4
```
2022-12-02 20:08:47 +00:00
Alexander V. Chernikov f4d3aa7490 netlink: suppress sending NLMSG_ERROR if NLMSG_DONE is already sent
Netlink has a confirmation/error reporting mechanism for the sent
messages. Kernel explicitly acks each messages if requested (NLM_F_ACK)
 or if message processing results in an error.
Similarly, for multipart messages - typically dumps, where each message
 represents a single object like an interface or a route - another
 message, NLMSG_DONE is used to indicate the end of dump and the
 resulting status.
As a result, successfull dump ends with both NLMSG_DONE and NLMSG_ERROR
 messages.
RFC 3549 does not say anything specific about such case.
Linux adopted an optimisation which suppresses NLMSG_ERROR message
 when NLMSG_DONE is already sent. Certain libraries/applications like
 libnl depends on such behavior.

Suppress sending NLMSG_ERROR if NLMSG_DONE is already sent, by
 setting newly-added 'suppress_ack' flag in the writer and checking
 this flag when generating ack.

This change restores libnl compatibility.

Before:
```
~ nl-link-list
Error: Unable to allocate link cache: Message sequence number mismatch
````

After:
```
~ nl-link-list
vtnet0 ether 52:54:00:14:e3:19 <broadcast,multicast,up,running>
lo0 ieee1394 <loopback,multicast,up,running>
```

Reviewed by:	bapt,pauamma
Tested by:	bapt
Differential Revision: https://reviews.freebsd.org/D37565
2022-11-30 13:24:38 +00:00
Baptiste Daroussin 1edc3b6e56 netlink: unregister groups along with family
Differential Revision:	https://reviews.freebsd.org/D37308
Reviewed by:	melifaro
2022-11-08 12:46:58 +01:00
Alexander V. Chernikov 4dfd380e06 netlink: allow more than 64 groups per netlink socket. 2022-11-03 17:05:34 +00:00
Alexander V. Chernikov 669d63eb85 netlink: remove private netlink_var.h header from the non-netlink core files. 2022-11-03 17:04:18 +00:00
Alexander V. Chernikov 728ca8506d netlink: fix CTRL_CMD_GETFAMILY lookup/dumps.
Reported by:	bapt
2022-10-31 17:12:42 +00:00
Alexander V. Chernikov 830352ccca netlink: fix genetlink CTRL_ATTR_MCAST_GRP_MAX define.
Reported by:	harti
2022-10-31 15:03:27 +00:00
Alexander V. Chernikov 03de61aabb netlink: fix NLMSG_SPACE() macro.
Reported by:	bapt
2022-10-31 15:02:12 +00:00
Alexander V. Chernikov fc47afbfaf netlink: fix generic netlink privilege check.
Reported by:	bapt
2022-10-31 13:40:25 +00:00
Ed Maste 43d0c2ddd2 netlink: use (void) for function definitions with no arguments
For some of these Clang produced a warning that "a function declaration
without a prototype is deprecated in all versions of C".  In other cases
the function defintion used () which did not match the header
declaration, which used (void).

Sponsored by:	The FreeBSD Foundation
2022-10-27 11:14:00 -04:00
Gleb Smirnoff b958b862b1 netlink: make it working without INET6 2022-10-04 14:39:49 -07:00
Hans Petter Selasky 1d41a05557 Fix kernel build after fcb3f813f3 .
By adding missing ifdefs for INET and INET6 when building LINT-NOIP .

Differential Revision:	https://reviews.freebsd.org/D36731
Sponsored by:	NVIDIA Networking
2022-10-04 16:08:39 +02:00
Alexander V. Chernikov 356724fc93 netlink: fix non-default builds (no INET, INET6, ROUTE_MPATH). 2022-10-02 13:12:52 +00:00
Jung-uk Kim fc083c3eb2 netlink: Fix build without VIMAGE 2022-10-01 21:41:54 -04:00
Alexander V. Chernikov 03994c24da netlink: fix build without INVARIANTS
Reported by:	cy
2022-10-01 21:20:50 +00:00
Alexander V. Chernikov 8d9f3e0572 netlink: fix format strings on 32-bit platforms 2022-10-01 21:16:31 +00:00
Alexander V. Chernikov c90bff3fa7 netlink: fix debugging on 32-bit platforms 2022-10-01 19:03:35 +00:00
Alexander V. Chernikov dddafa8d25 netlink: make test-includes happy by hiding most of the header
contents under _KERNEL.
2022-10-01 17:01:53 +00:00
Alexander V. Chernikov 7e5bf68495 netlink: add netlink support
Netlinks is a communication protocol currently used in Linux kernel to modify,
 read and subscribe for nearly all networking state. Interfaces, addresses, routes,
 firewall, fibs, vnets, etc are controlled via netlink.
It is async, TLV-based protocol, providing 1-1 and 1-many communications.

The current implementation supports the subset of NETLINK_ROUTE
family. To be more specific, the following is supported:
* Dumps:
 - routes
 - nexthops / nexthop groups
 - interfaces
 - interface addresses
 - neighbors (arp/ndp)
* Notifications:
 - interface arrival/departure
 - interface address arrival/departure
 - route addition/deletion
* Modifications:
 - adding/deleting routes
 - adding/deleting nexthops/nexthops groups
 - adding/deleting neghbors
 - adding/deleting interfaces (basic support only)
* Rtsock interaction
 - route events are bridged both ways

The implementation also supports the NETLINK_GENERIC family framework.

Implementation notes:
Netlink is implemented via loadable/unloadable kernel module,
 not touching many kernel parts.
Each netlink socket uses dedicated taskqueue to support async operations
 that can sleep, such as interface creation. All message processing is
 performed within these taskqueues.

Compatibility:
Most of the Netlink data models specified above maps to FreeBSD concepts
 nicely. Unmodified ip(8) binary correctly works with
interfaces, addresses, routes, nexthops and nexthop groups. Some
software such as net/bird require header-only modifications to compile
and work with FreeBSD netlink.

Reviewed by:	imp
Differential Revision: https://reviews.freebsd.org/D36002
MFC after:	2 months
2022-10-01 14:15:35 +00:00