This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit c5bf0e4c78e216c8c357e409d3d7c8be2760eb44 Author: zhanghongyu <[email protected]> AuthorDate: Fri Nov 27 16:49:10 2026 +0800 net/netlink: add the Generic Netlink protocol. A kernel subsystem that wants to talk to userspace over netlink had to get its own protocol number in NETLINK_* and its own sendto handler in netlink_sockif.c. Generic Netlink exists so that it does not have to: a subsystem registers a family by name at run time, userspace resolves the name to an id with the GENL_ID_CTRL controller, and every family shares the single NETLINK_GENERIC protocol. Add that framework: - genl_register_family() and genl_unregister_family() take a struct genl_family that names the family and gives its version, its command handler and its multicast groups. Registration assigns the family id and the multicast group offset, and fails with EEXIST if the name is taken, ENOMEM once GENL_MAX_FAMILIES are registered, and ENOSPC once the ids run out: neither space is given back by genl_unregister_family(), the family id stops at GENL_MAX_ID, and a multicast group lives in the 32-bit mask of a connection, so 32 is the last group that can be subscribed to and delivered. - netlink_generic_sendto() refuses a request shorter than a nlmsghdr plus a genlmsghdr, then routes it by nlmsg_type: GENL_ID_CTRL goes to the controller, anything else to the cmd_handler of the family that owns that id, which is left to interpret the command and validate the attributes. - The built-in GENL_ID_CTRL family answers CTRL_CMD_GETFAMILY: a client resolves a family by name, or dumps every family with NLM_F_DUMP, to discover what the board offers. The name is taken from the request only when its attribute really holds a string that fits, because it is then used with strcmp() and printed. Lookup by CTRL_ATTR_FAMILY_ID is not implemented; libnl resolves by name. - The family table is read under g_genl_lock, which the register and unregister paths already took: unregister moves the last entry into the freed slot, so a lookup running beside it could otherwise miss a family or return one that is gone. The dump takes a snapshot under the lock and sends outside it, because sending ends in netlink_add_response(), which takes the netlink lock. netlink_add_broadcast() takes the protocol as its first argument, since a multicast group number is only meaningful within one protocol. All nine callers are updated; the two in the wifi simulation pass NETLINK_ROUTE, which is what they were broadcasting to all along. include/netpacket/netlink.h is split so that a new user can include only the protocol it speaks: the rtnetlink definitions move to netpacket/rtnetlink.h, the netfilter ones to netpacket/nfnetlink.h, and netpacket/genetlink.h is new. The move carries no definition away, and adds only NETLINK_ADD_MEMBERSHIP and NETLINK_DROP_MEMBERSHIP, which the new SOL_NETLINK socket options need. netlink.h includes the three protocol headers at its end, so existing code, in nuttx-apps and in the third-party projects it builds, keeps seeing every definition through the one header it already includes. The include guard breaks the cycle, since each protocol header includes netlink.h for the common definitions. The protocol is behind CONFIG_NETLINK_GENERIC, default n, so a configuration that does not use it builds exactly as before. Assisted-by: Kiro:claude-opus-4 Signed-off-by: daichuan <[email protected]> --- arch/sim/src/sim/sim_wifihost.c | 2 +- drivers/net/wifi_sim.c | 2 +- include/netpacket/genetlink.h | 143 +++++ include/netpacket/netlink.h | 899 ++++++++------------------- include/netpacket/nfnetlink.h | 186 ++++++ include/netpacket/{netlink.h => rtnetlink.h} | 449 +------------ include/nuttx/net/genetlink.h | 171 +++++ include/nuttx/net/netlink.h | 15 +- include/sys/socket.h | 1 + net/nat/ipv4_nat_entry.c | 3 + net/nat/ipv6_nat_entry.c | 3 + net/netlink/CMakeLists.txt | 8 +- net/netlink/Kconfig | 6 + net/netlink/Make.defs | 8 +- net/netlink/netlink.h | 31 +- net/netlink/netlink_attr.c | 5 + net/netlink/netlink_conn.c | 29 +- net/netlink/netlink_generic.c | 760 ++++++++++++++++++++++ net/netlink/netlink_netfilter.c | 5 +- net/netlink/netlink_route.c | 12 +- net/netlink/netlink_sockif.c | 115 +++- 21 files changed, 1757 insertions(+), 1096 deletions(-) diff --git a/arch/sim/src/sim/sim_wifihost.c b/arch/sim/src/sim/sim_wifihost.c index d6a54813206..90bb485f7ba 100644 --- a/arch/sim/src/sim/sim_wifihost.c +++ b/arch/sim/src/sim/sim_wifihost.c @@ -1074,7 +1074,7 @@ static int wifi_send_event(struct net_driver_s *dev, unsigned int cmd, memset(&wev->event.u, 0, sizeof(union iwreq_data)); memcpy(&wev->event.u, ((char *)wrqu), sizeof(union iwreq_data)); - netlink_add_broadcast(RTNLGRP_LINK, + netlink_add_broadcast(NETLINK_ROUTE, RTNLGRP_LINK, (struct netlink_response_s *)alloc); return OK; } diff --git a/drivers/net/wifi_sim.c b/drivers/net/wifi_sim.c index e83ecfac842..385d1bcf105 100644 --- a/drivers/net/wifi_sim.c +++ b/drivers/net/wifi_sim.c @@ -469,7 +469,7 @@ static int wifi_send_event(FAR struct wifi_sim_s *wifidev, memset(&wev->event.u, 0, sizeof(union iwreq_data)); memcpy(&wev->event.u, ((FAR char *) wrqu), sizeof(union iwreq_data)); - netlink_add_broadcast(RTNLGRP_LINK, + netlink_add_broadcast(NETLINK_ROUTE, RTNLGRP_LINK, (FAR struct netlink_response_s *)alloc); return OK; } diff --git a/include/netpacket/genetlink.h b/include/netpacket/genetlink.h new file mode 100644 index 00000000000..53a8ecf4500 --- /dev/null +++ b/include/netpacket/genetlink.h @@ -0,0 +1,143 @@ +/**************************************************************************** + * include/netpacket/genetlink.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NETPACKET_GENETLINK_H +#define __INCLUDE_NETPACKET_GENETLINK_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <stdint.h> +#include <netpacket/netlink.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define GENL_NAMSIZ 16 /* Length of family name */ + +#define GENL_MIN_ID NLMSG_MIN_TYPE +#define GENL_MAX_ID 1023 + +#define GENL_HDRLEN NLMSG_ALIGN(sizeof(struct genlmsghdr)) + +#define GENL_ADMIN_PERM 0x01 +#define GENL_CMD_CAP_DO 0x02 +#define GENL_CMD_CAP_DUMP 0x04 +#define GENL_CMD_CAP_HASPOL 0x08 +#define GENL_UNS_ADMIN_PERM 0x10 + +/* List of reserved static generic netlink identifiers: */ + +#define GENL_ID_CTRL NLMSG_MIN_TYPE +#define GENL_ID_VFS_DQUOT (NLMSG_MIN_TYPE + 1) +#define GENL_ID_PMCRAID (NLMSG_MIN_TYPE + 2) + +/* Must be last reserved + 1 */ + +#define GENL_START_ALLOC (NLMSG_MIN_TYPE + 3) + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +struct genlmsghdr +{ + uint8_t cmd; + uint8_t version; + uint16_t reserved; +}; + +/**************************************************************************** + * Controller + ****************************************************************************/ + +enum +{ + CTRL_CMD_UNSPEC, + CTRL_CMD_NEWFAMILY, + CTRL_CMD_DELFAMILY, + CTRL_CMD_GETFAMILY, + CTRL_CMD_NEWOPS, + CTRL_CMD_DELOPS, + CTRL_CMD_GETOPS, + CTRL_CMD_NEWMCAST_GRP, + CTRL_CMD_DELMCAST_GRP, + CTRL_CMD_GETMCAST_GRP, /* unused */ + CTRL_CMD_GETPOLICY, + __CTRL_CMD_MAX, +}; + +#define CTRL_CMD_MAX (__CTRL_CMD_MAX - 1) + +enum +{ + CTRL_ATTR_UNSPEC, + CTRL_ATTR_FAMILY_ID, + CTRL_ATTR_FAMILY_NAME, + CTRL_ATTR_VERSION, + CTRL_ATTR_HDRSIZE, + CTRL_ATTR_MAXATTR, + CTRL_ATTR_OPS, + CTRL_ATTR_MCAST_GROUPS, + CTRL_ATTR_POLICY, + CTRL_ATTR_OP_POLICY, + CTRL_ATTR_OP, + __CTRL_ATTR_MAX, +}; + +#define CTRL_ATTR_MAX (__CTRL_ATTR_MAX - 1) + +enum +{ + CTRL_ATTR_OP_UNSPEC, + CTRL_ATTR_OP_ID, + CTRL_ATTR_OP_FLAGS, + __CTRL_ATTR_OP_MAX, +}; + +#define CTRL_ATTR_OP_MAX (__CTRL_ATTR_OP_MAX - 1) + +enum +{ + CTRL_ATTR_MCAST_GRP_UNSPEC, + CTRL_ATTR_MCAST_GRP_NAME, + CTRL_ATTR_MCAST_GRP_ID, + __CTRL_ATTR_MCAST_GRP_MAX, +}; + +#define CTRL_ATTR_MCAST_GRP_MAX (__CTRL_ATTR_MCAST_GRP_MAX - 1) + +enum +{ + CTRL_ATTR_POLICY_UNSPEC, + CTRL_ATTR_POLICY_DO, + CTRL_ATTR_POLICY_DUMP, + + __CTRL_ATTR_POLICY_DUMP_MAX, + CTRL_ATTR_POLICY_DUMP_MAX = __CTRL_ATTR_POLICY_DUMP_MAX - 1 +}; + +#define CTRL_ATTR_POLICY_MAX (__CTRL_ATTR_POLICY_DUMP_MAX - 1) + +#endif /* __INCLUDE_NETPACKET_GENETLINK_H */ diff --git a/include/netpacket/netlink.h b/include/netpacket/netlink.h index 9d67b6d1aa7..72b08f323b5 100644 --- a/include/netpacket/netlink.h +++ b/include/netpacket/netlink.h @@ -33,8 +33,6 @@ #include <sys/socket.h> #include <stdint.h> -#include <netpacket/if_addr.h> - /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -137,283 +135,6 @@ #define NLMSG_OVERRUN 4 /* Data lost */ #define NLMSG_MIN_TYPE 16 /* < 16: Reserved control messages */ -/* Attribute definitions for struct rtattr **********************************/ - -/* Macros to handle attribute lists */ - -#define RTA_MASK (sizeof(uint32_t) - 1) -#define RTA_ALIGN(n) (((n) + RTA_MASK) & ~RTA_MASK) -#define RTA_OK(rta, n) ((n) >= (int)sizeof(struct rtattr) && \ - (rta)->rta_len >= sizeof(struct rtattr) && \ - (rta)->rta_len <= (n)) -#define RTA_NEXT(rta, attrlen) ((attrlen) -= RTA_ALIGN((rta)->rta_len), \ - (FAR struct rtattr *)(((FAR char *)(rta)) + \ - RTA_ALIGN((rta)->rta_len))) -#define RTA_LENGTH(n) (sizeof(struct rtattr) + (n)) -#define RTA_SPACE(n) RTA_ALIGN(RTA_LENGTH(n)) -#define RTA_DATA(rta) ((FAR void *)(((FAR char *)(rta)) + RTA_LENGTH(0))) -#define RTA_PAYLOAD(rta) ((int)((rta)->rta_len) - RTA_LENGTH(0)) - -/* NETLINK_ROUTE: Routing table attributes */ - -#define RTA_UNSPEC 0 /* Inored */ -#define RTA_DST 1 /* Argument: Route destination address */ -#define RTA_SRC 2 /* Argument: Route source address */ -#define RTA_IIF 3 /* Argument: Input interface index */ -#define RTA_OIF 4 /* Argument: Output interface index */ -#define RTA_GATEWAY 5 /* Argument: Gateway address of the route */ -#define RTA_GENMASK 6 /* Argument: Network address mask of sub-net */ -#define RTA_PRIORITY 7 /* Argument: Route priority */ -#define RTA_TABLE 8 /* Argument: Route table */ -#define RTA_PREFSRC 9 /* Argument: Preferred source address */ -#define RTA_METRICS 10 /* Argument: Route metric */ -#define RTA_MAX 10 /* MAX type, same as last argument */ - -/* NETLINK_ROUTE protocol message types *************************************/ - -/* Link layer: - * - * RTM_NEWLINK, RTM_DELLINK, RTM_GETLINK - * Create, remove or get information about a specific network interface. - * These messages contain an ifinfomsg structure followed by a series - * of rtattr structures. - */ - -#define RTM_NEWLINK 16 -#define RTM_DELLINK 17 -#define RTM_GETLINK 18 -#define RTM_SETLINK 19 - -/* Address settings: - * - * RTM_NEWADDR, RTM_DELADDR, RTM_GETADDR - * Add, remove or receive information about an IP address associated with - * an interface. These messages contain an ifaddrmsg structure, optionally - * followed by rtattr routing attributes. - */ - -#define RTM_NEWADDR 20 -#define RTM_DELADDR 21 -#define RTM_GETADDR 22 - -/* Routing tables: - * - * RTM_NEWROUTE, RTM_DELROUTE, RTM_GETROUTE - * Create, remove or receive information about a network route. These - * messages contain an rtmsg structure with an optional sequence of - * rtattr structures following. - * - * For RTM_GETROUTE, setting rtm_dst_len and rtm_src_len to 0 means you - * get all entries for the specified routing table. For the other fields, - * except rtm_table and rtm_protocol, 0 is the wildcard. - */ - -#define RTM_NEWROUTE 24 -#define RTM_DELROUTE 25 -#define RTM_GETROUTE 26 - -/* Neighbor cache: - * - * RTM_NEWNEIGH, RTM_DELNEIGH, RTM_GETNEIGH - * Add, remove or receive information about a neighbor table entry (e.g., - * an ARP entry). The message contains an ndmsg structure. - */ - -#define RTM_NEWNEIGH 28 -#define RTM_DELNEIGH 29 -#define RTM_GETNEIGH 30 - -/* Routing rules: - * - * RTM_NEWRULE, RTM_DELRULE, RTM_GETRULE - * Add, delete or retrieve a routing rule. Carries a struct rtmsg - */ - -#define RTM_NEWRULE 32 -#define RTM_DELRULE 33 -#define RTM_GETRULE 34 - -/* Queuing discipline settings: - * - * RTM_NEWQDISC, RTM_DELQDISC, RTM_GETQDISC - * Add, remove or get a queuing discipline. The message contains a - * struct tcmsg and may be followed by a series of attributes. - */ - -#define RTM_NEWQDISC 36 -#define RTM_DELQDISC 37 -#define RTM_GETQDISC 38 - -/* Traffic classes used with queues: - * - * RTM_NEWTCLASS, RTM_DELTCLASS, RTM_GETTCLASS - * Add, remove or get a traffic class. These messages contain a struct - * tcmsg as described above. - */ - -#define RTM_NEWTCLASS 40 -#define RTM_DELTCLASS 41 -#define RTM_GETTCLASS 42 - -/* Traffic filters: - * - * RTM_NEWTFILTER, RTM_DELTFILTER, RTM_GETTFILTER - * Add, remove or receive information about a traffic filter. These - * messages contain a struct tcmsg as described above. - */ - -#define RTM_NEWTFILTER 44 -#define RTM_DELTFILTER 45 -#define RTM_GETTFILTER 46 - -/* Others: */ - -#define RTM_NEWACTION 48 -#define RTM_DELACTION 49 -#define RTM_GETACTION 50 -#define RTM_NEWPREFIX 52 -#define RTM_GETMULTICAST 58 -#define RTM_GETANYCAST 62 -#define RTM_NEWNEIGHTBL 64 -#define RTM_GETNEIGHTBL 66 -#define RTM_SETNEIGHTBL 67 -#define RTM_NEWNDUSEROPT 68 - -#define RTM_BASE 16 -#define RTM_MAX 68 - -/* Definitions for struct ifinfomsg *****************************************/ - -#define IFLA_RTA(r) ((FAR struct rtattr *) \ - (((FAR char *)(r)) + \ - NLMSG_ALIGN(sizeof(struct ifinfomsg)))) -#define IFLA_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg)) - -/* Values for rta_type */ - -#define IFLA_IFNAME 1 -#define IFLA_ADDRESS 2 -#define IFLA_MTU 3 -#define IFLA_WIRELESS 4 -#define IFLA_STATS 5 -#define IFLA_OPERSTATE 6 -#define IFLA_LINKMODE 7 -#define IFLA_BROADCAST 8 -#define IFLA_LINK 9 -#define IFLA_QDISC 10 -#define IFLA_COST 11 -#define IFLA_PRIORITY 12 -#define IFLA_MASTER 13 -#define IFLA_PROTINFO 14 -#define IFLA_TXQLEN 15 -#define IFLA_MAP 16 -#define IFLA_WEIGHT 17 - -/* Definitions for struct rtmsg *********************************************/ - -#define RTM_RTA(r) ((FAR struct rtattr *)\ - (((FAR char *)(r)) + \ - NLMSG_ALIGN(sizeof(struct rtmsg)))) -#define RTM_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct rtmsg)) - -/* rtm_table. Routing table identifiers */ - -#define RT_TABLE_UNSPEC 0 - /* 1-251: User defined values */ -#define RT_TABLE_MAIN 254 -#define RT_TABLE_LOCAL 255 -#define RT_TABLE_MAX 0xffffffff - -/* rtm_type */ - -#define RTN_UNSPEC 0 -#define RTN_UNICAST 1 /* Gateway or direct route */ -#define RTN_LOCAL 2 /* Accept locally */ -#define RTN_BROADCAST 3 /* Accept locally as broadcast; - * send as broadcast */ -#define RTN_ANYCAST 4 /* Accept locally as broadcast - * but send as unicast */ -#define RTN_MULTICAST 5 /* Multicast route */ - -/* rtm_protocol */ - -#define RTPROT_UNSPEC 0 -#define RTPROT_REDIRECT 1 /* Route installed by ICMP redirects */ -#define RTPROT_KERNEL 2 /* Route installed by kernel */ -#define RTPROT_BOOT 3 /* Route installed during boot */ -#define RTPROT_STATIC 4 /* Route installed by administrator */ -#define RTPROT_RA 9 /* RDISC/ND router advertisements */ -#define RTPROT_DHCP 16 /* DHCP client */ - -/* rtm_scope */ - -#define RT_SCOPE_UNIVERSE 0 /* Global route */ - /* 1-199: User defined values */ -#define RT_SCOPE_SITE 200 /* Interior route in local system */ -#define RT_SCOPE_LINK 253 /* Route on this link */ -#define RT_SCOPE_HOST 254 /* Route on local host */ -#define RT_SCOPE_NOWHERE 255 /* Destination does not exist */ - -/* RTnetlink multicast groups (userspace) */ - -#define RTMGRP_LINK 1 -#define RTMGRP_NOTIFY 2 -#define RTMGRP_NEIGH 4 -#define RTMGRP_TC 8 - -#define RTMGRP_IPV4_IFADDR 0x10 -#define RTMGRP_IPV4_MROUTE 0x20 -#define RTMGRP_IPV4_ROUTE 0x40 -#define RTMGRP_IPV4_RULE 0x80 - -#define RTMGRP_IPV6_IFADDR 0x100 -#define RTMGRP_IPV6_MROUTE 0x200 -#define RTMGRP_IPV6_ROUTE 0x400 -#define RTMGRP_IPV6_IFINFO 0x800 - -#define RTMGRP_DECnet_IFADDR 0x1000 -#define RTMGRP_DECnet_ROUTE 0x4000 - -#define RTMGRP_IPV6_PREFIX 0x20000 - -/* RTnetlink multicast groups */ - -#define RTNLGRP_NONE 0 -#define RTNLGRP_LINK 1 -#define RTNLGRP_NOTIFY 2 -#define RTNLGRP_NEIGH 3 -#define RTNLGRP_TC 4 -#define RTNLGRP_IPV4_IFADDR 5 -#define RTNLGRP_IPV4_MROUTE 6 -#define RTNLGRP_IPV4_ROUTE 7 -#define RTNLGRP_IPV4_RULE 8 -#define RTNLGRP_IPV6_IFADDR 9 -#define RTNLGRP_IPV6_MROUTE 10 -#define RTNLGRP_IPV6_ROUTE 11 -#define RTNLGRP_IPV6_IFINFO 12 -#define RTNLGRP_DECnet_IFADDR 13 -#define RTNLGRP_NOP2 14 -#define RTNLGRP_DECnet_ROUTE 15 -#define RTNLGRP_DECnet_RULE 16 -#define RTNLGRP_NOP4 17 -#define RTNLGRP_IPV6_PREFIX 18 -#define RTNLGRP_IPV6_RULE 19 -#define RTNLGRP_ND_USEROPT 20 -#define RTNLGRP_PHONET_IFADDR 21 -#define RTNLGRP_PHONET_ROUTE 22 -#define RTNLGRP_DCB 23 -#define RTNLGRP_IPV4_NETCONF 24 -#define RTNLGRP_IPV6_NETCONF 25 -#define RTNLGRP_MDB 26 -#define RTNLGRP_MPLS_ROUTE 27 -#define RTNLGRP_NSID 28 -#define RTNLGRP_MAX 29 - -#define FRA_UNSPEC 0 -#define FRA_FWMARK 1 /* Mark */ -#define FRA_TABLE 2 /* Extended table id */ -#define FRA_FWMASK 3 /* Mask for netfilter mark */ - /* nla_type (16 bits) * +---+---+-------------------------------+ * | N | O | Attribute Type | @@ -432,162 +153,8 @@ #define NLA_ALIGN(len) (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1)) #define NLA_HDRLEN (NLA_ALIGN(sizeof(struct nlattr))) -/* rtm_flags */ - -#define RTM_F_CLONED 0x200 /* This route is cloned */ - -/* Attribute definitions for struct nfattr **********************************/ - -/* Macros to handle attribute lists */ - -#define NFNL_NFA_NEST 0x8000 -#define NFA_TYPE(attr) ((attr)->nfa_type & 0x7fff) - -#define NFA_MASK (sizeof(uint32_t) - 1) -#define NFA_ALIGN(n) (((n) + NFA_MASK) & ~NFA_MASK) -#define NFA_OK(nfa, n) ((n) >= (int)sizeof(struct nfattr) && \ - (nfa)->nfa_len >= sizeof(struct nfattr) && \ - (nfa)->nfa_len <= (n)) -#define NFA_NEXT(nfa, attrlen) ((attrlen) -= NFA_ALIGN((nfa)->nfa_len), \ - (FAR struct nfattr *)(((FAR char *)(nfa)) + \ - NFA_ALIGN((nfa)->nfa_len))) -#define NFA_LENGTH(len) ((sizeof(struct nfattr)) + (len)) -#define NFA_SPACE(len) NFA_ALIGN(NFA_LENGTH(len)) -#define NFA_DATA(nfa) ((FAR void *)(((FAR char *)(nfa)) + NFA_LENGTH(0))) -#define NFA_PAYLOAD(nfa) ((int)((nfa)->nfa_len) - NFA_LENGTH(0)) - -/* Definitions for struct nfgenmsg ******************************************/ - -#define NFM_NFA(n) ((FAR struct nfattr *) \ - (((FAR char *)(n)) + \ - NLMSG_ALIGN(sizeof(struct nfgenmsg)))) -#define NFM_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct nfgenmsg)) - -/* Definitions for NETLINK_NETFILTER ****************************************/ - -#define NFNETLINK_V0 0 - -/* netfilter netlink message types are split in two pieces: - * 8 bit subsystem, 8bit operation. - */ - -#define NFNL_SUBSYS_ID(x) (((x) & 0xff00) >> 8) -#define NFNL_MSG_TYPE(x) ((x) & 0x00ff) - -/* Subsystems */ - -#define NFNL_SUBSYS_NONE 0 -#define NFNL_SUBSYS_CTNETLINK 1 -#define NFNL_SUBSYS_CTNETLINK_EXP 2 -#define NFNL_SUBSYS_QUEUE 3 -#define NFNL_SUBSYS_ULOG 4 -#define NFNL_SUBSYS_OSF 5 -#define NFNL_SUBSYS_IPSET 6 -#define NFNL_SUBSYS_ACCT 7 -#define NFNL_SUBSYS_CTNETLINK_TIMEOUT 8 -#define NFNL_SUBSYS_CTHELPER 9 -#define NFNL_SUBSYS_NFTABLES 10 -#define NFNL_SUBSYS_NFT_COMPAT 11 -#define NFNL_SUBSYS_HOOK 12 -#define NFNL_SUBSYS_COUNT 13 - -/* NETLINK_NETFILTER: subsystem CTNL (ip conntrack netlink) message types */ - -#define IPCTNL_MSG_CT_NEW 0 -#define IPCTNL_MSG_CT_GET 1 -#define IPCTNL_MSG_CT_DELETE 2 -#define IPCTNL_MSG_CT_GET_CTRZERO 3 -#define IPCTNL_MSG_CT_GET_STATS_CPU 4 -#define IPCTNL_MSG_CT_GET_STATS 5 -#define IPCTNL_MSG_CT_GET_DYING 6 -#define IPCTNL_MSG_CT_GET_UNCONFIRMED 7 -#define IPCTNL_MSG_MAX 8 - -/* NETLINK_NETFILTER: Conntrack attributes */ - -#define CTA_UNSPEC 0 -#define CTA_TUPLE_ORIG 1 -#define CTA_TUPLE_REPLY 2 -#define CTA_STATUS 3 -#define CTA_PROTOINFO 4 -#define CTA_HELP 5 -#define CTA_NAT_SRC 6 -#define CTA_TIMEOUT 7 -#define CTA_MARK 8 -#define CTA_COUNTERS_ORIG 9 -#define CTA_COUNTERS_REPLY 10 -#define CTA_USE 11 -#define CTA_ID 12 -#define CTA_NAT_DST 13 -#define CTA_TUPLE_MASTER 14 -#define CTA_SEQ_ADJ_ORIG 15 -#define CTA_NAT_SEQ_ADJ_ORIG CTA_SEQ_ADJ_ORIG -#define CTA_SEQ_ADJ_REPLY 16 -#define CTA_NAT_SEQ_ADJ_REPLY CTA_SEQ_ADJ_REPLY -#define CTA_ZONE 18 -#define CTA_SECCTX 19 -#define CTA_TIMESTAMP 20 -#define CTA_MARK_MASK 21 -#define CTA_LABELS 22 -#define CTA_LABELS_MASK 23 -#define CTA_SYNPROXY 24 -#define CTA_FILTER 25 -#define CTA_STATUS_MASK 26 -#define CTA_MAX 26 - -/* NETLINK_NETFILTER: Conntrack tuple attributes */ - -#define CTA_TUPLE_UNSPEC 0 -#define CTA_TUPLE_IP 1 -#define CTA_TUPLE_PROTO 2 -#define CTA_TUPLE_ZONE 3 -#define CTA_TUPLE_MAX 3 - -/* NETLINK_NETFILTER: Conntrack IP attributes */ - -#define CTA_IP_UNSPEC 0 -#define CTA_IP_V4_SRC 1 -#define CTA_IP_V4_DST 2 -#define CTA_IP_V6_SRC 3 -#define CTA_IP_V6_DST 4 -#define CTA_IP_MAX 4 - -/* NETLINK_NETFILTER: Conntrack protocol attributes */ - -#define CTA_PROTO_UNSPEC 0 -#define CTA_PROTO_NUM 1 -#define CTA_PROTO_SRC_PORT 2 -#define CTA_PROTO_DST_PORT 3 -#define CTA_PROTO_ICMP_ID 4 -#define CTA_PROTO_ICMP_TYPE 5 -#define CTA_PROTO_ICMP_CODE 6 -#define CTA_PROTO_ICMPV6_ID 7 -#define CTA_PROTO_ICMPV6_TYPE 8 -#define CTA_PROTO_ICMPV6_CODE 9 -#define CTA_PROTO_MAX 9 - -/* NFnetlink multicast groups (userspace) */ - -#define NF_NETLINK_CONNTRACK_NEW 0x00000001 -#define NF_NETLINK_CONNTRACK_UPDATE 0x00000002 -#define NF_NETLINK_CONNTRACK_DESTROY 0x00000004 -#define NF_NETLINK_CONNTRACK_EXP_NEW 0x00000008 -#define NF_NETLINK_CONNTRACK_EXP_UPDATE 0x00000010 -#define NF_NETLINK_CONNTRACK_EXP_DESTROY 0x00000020 - -/* NFnetlink multicast groups */ - -#define NFNLGRP_NONE 0 -#define NFNLGRP_CONNTRACK_NEW 1 -#define NFNLGRP_CONNTRACK_UPDATE 2 -#define NFNLGRP_CONNTRACK_DESTROY 3 -#define NFNLGRP_CONNTRACK_EXP_NEW 4 -#define NFNLGRP_CONNTRACK_EXP_UPDATE 5 -#define NFNLGRP_CONNTRACK_EXP_DESTROY 6 -#define NFNLGRP_NFTABLES 7 -#define NFNLGRP_ACCT_QUOTA 8 -#define NFNLGRP_NFTRACE 9 -#define NFNLGRP_MAX 9 +#define NETLINK_ADD_MEMBERSHIP 1 +#define NETLINK_DROP_MEMBERSHIP 2 /**************************************************************************** * Public Type Definitions @@ -660,100 +227,6 @@ struct nlmsgerr */ }; -/* NETLINK_ROUTE Message Structures *****************************************/ - -/* RTM_NEWLINK, RTM_DELLINK, RTM_GETLINK - * - * Create, remove or get information about a specific network interface. - * These messages contain an ifinfomsg structure followed by a series - * of rtattr structures. - * - * These attributes should be manipulated using only the RTA_* - */ - -struct rtattr -{ - uint16_t rta_len; /* Length of option */ - uint16_t rta_type; /* Type of option */ - /* Data follows */ -}; - -struct ifinfomsg -{ - uint8_t ifi_family; /* AF_UNSPEC */ - uint8_t ifi_pad; - uint16_t ifi_type; /* Device type (ARPHRD) */ - int32_t ifi_index; /* Unique interface index */ - uint32_t ifi_flags; /* Device IFF_* flags */ - uint32_t ifi_change; /* Change mask, must always be 0xffffffff */ -}; - -/* General form of address family dependent message. */ - -struct rtgenmsg -{ - uint8_t rtgen_family; -}; - -/* RTM_NEWNEIGH, RTM_DELNEIGH, RTM_GETNEIGH - * Add, remove or receive information about a neighbor table entry (e.g., - * an ARP entry). The message contains an ndmsg structure. - */ - -struct ndmsg -{ - uint8_t ndm_family; - uint8_t ndm_pad1; - uint16_t ndm_pad2; - int32_t ndm_ifindex; - uint16_t ndm_state; - uint8_t ndm_flags; - uint8_t ndm_type; -}; - -/* Structures used in routing table administration. */ - -struct rtmsg -{ - uint8_t rtm_family; - uint8_t rtm_dst_len; - uint8_t rtm_src_len; - uint8_t rtm_tos; - uint8_t rtm_table; /* Routing table id */ - uint8_t rtm_protocol; /* Routing protocol; See RTPROT_ definitions. */ - uint8_t rtm_scope; /* See RT_SCOPE_* definitions */ - uint8_t rtm_type; /* See RTN_* definitions */ - uint32_t rtm_flags; -}; - -/* Structures used in prefix information. */ - -struct prefixmsg -{ - uint8_t prefix_family; - uint8_t prefix_pad1; - uint16_t prefix_pad2; - int32_t prefix_ifindex; - uint8_t prefix_type; - uint8_t prefix_len; - uint8_t prefix_flags; - uint8_t prefix_pad3; -}; - -enum -{ - PREFIX_UNSPEC, - PREFIX_ADDRESS, - PREFIX_CACHEINFO, - PREFIX_MAX = PREFIX_CACHEINFO -}; - -struct prefix_cacheinfo -{ - uint32_t preferred_time; - uint32_t valid_time; -}; - /* <------- NLA_HDRLEN ------> <-- NLA_ALIGN(payload)--> * +---------------------+- - -+- - - - - - - - - -+- - -+ * | Header | Pad | Payload | Pad | @@ -788,129 +261,291 @@ struct nla_bitfield32 uint32_t selector; }; -/* NETLINK_NETFILTER Message Structures *************************************/ +/**************************************************************************** + * Name: nla_data + * + * Description: + * Return pointer to the payload of a netlink attribute. + * + * Input Parameters: + * nla - The netlink attribute + * + * Returned Value: + * Pointer to attribute payload + * + ****************************************************************************/ -/* These attributes should be manipulated using only the NFA_* */ +static inline FAR void *nla_data(FAR const struct nlattr *nla) +{ + return (FAR void *)(((FAR char *)nla) + NLA_HDRLEN); +} -struct nfattr +/**************************************************************************** + * Name: nla_len + * + * Description: + * Return length of the payload of a netlink attribute. + * + * Input Parameters: + * nla - The netlink attribute + * + * Returned Value: + * Length of attribute payload + * + ****************************************************************************/ + +static inline int nla_len(FAR const struct nlattr *nla) { - uint16_t nfa_len; - uint16_t nfa_type; -}; + return nla->nla_len - NLA_HDRLEN; +} + +/**************************************************************************** + * Name: nla_type + * + * Description: + * Return attribute type (without flags). + * + * Input Parameters: + * nla - The netlink attribute + * + * Returned Value: + * Attribute type + * + ****************************************************************************/ -/* General form of address family dependent message. */ +static inline int nla_type(FAR const struct nlattr *nla) +{ + return nla->nla_type & NLA_TYPE_MASK; +} -struct nfgenmsg +/**************************************************************************** + * Name: nla_get_u8 + * + * Description: + * Return the value of a u8 attribute. + * + * Input Parameters: + * nla - The netlink attribute + * + * Returned Value: + * The u8 value + * + ****************************************************************************/ + +static inline uint8_t nla_get_u8(FAR const struct nlattr *nla) { - uint8_t nfgen_family; /* AF_xxx */ - uint8_t version; /* nfnetlink version */ - uint16_t res_id; /* Resource id */ -}; + return *(FAR uint8_t *)nla_data(nla); +} -/* Neighbor Discovery userland options **************************************/ +/**************************************************************************** + * Name: nla_get_u16 + * + * Description: + * Return the value of a u16 attribute. + * + * Input Parameters: + * nla - The netlink attribute + * + * Returned Value: + * The u16 value + * + ****************************************************************************/ -struct nduseroptmsg +static inline uint16_t nla_get_u16(FAR const struct nlattr *nla) { - uint8_t nduseropt_family; - uint8_t nduseropt_pad1; - uint16_t nduseropt_opts_len; /* Total length of options */ - int32_t nduseropt_ifindex; - uint8_t nduseropt_icmp_type; - uint8_t nduseropt_icmp_code; - uint16_t nduseropt_pad2; - uint32_t nduseropt_pad3; - - /* Followed by one or more ND options */ -}; + return *(FAR uint16_t *)nla_data(nla); +} -enum +/**************************************************************************** + * Name: nla_get_u32 + * + * Description: + * Return the value of a u32 attribute. + * + * Input Parameters: + * nla - The netlink attribute + * + * Returned Value: + * The u32 value + * + ****************************************************************************/ + +static inline uint32_t nla_get_u32(FAR const struct nlattr *nla) { - NDUSEROPT_UNSPEC, - NDUSEROPT_SRCADDR, - NDUSEROPT_MAX = NDUSEROPT_SRCADDR -}; + return *(FAR uint32_t *)nla_data(nla); +} -/* This struct should be in sync with struct rtnl_link_stats64 */ +/**************************************************************************** + * Name: nla_get_u64 + * + * Description: + * Return the value of a u64 attribute. + * + * Input Parameters: + * nla - The netlink attribute + * + * Returned Value: + * The u64 value + * + ****************************************************************************/ -struct rtnl_link_stats +static inline uint64_t nla_get_u64(FAR const struct nlattr *nla) { - uint32_t rx_packets; /* Total packets received */ - uint32_t tx_packets; /* Total packets transmitted */ - uint32_t rx_bytes; /* Total bytes received */ - uint32_t tx_bytes; /* Total bytes transmitted */ - uint32_t rx_errors; /* Bad packets received */ - uint32_t tx_errors; /* Packet transmit problems */ - uint32_t rx_dropped; /* No space in linux buffers */ - uint32_t tx_dropped; /* No space available in linux */ - uint32_t multicast; /* Multicast packets received */ - uint32_t collisions; - - /* Detailed rx_errors: */ - - uint32_t rx_length_errors; - uint32_t rx_over_errors; /* Receiver ring buff overflow */ - uint32_t rx_crc_errors; /* Recved pkt with crc error */ - uint32_t rx_frame_errors; /* Recv'd frame alignment error */ - uint32_t rx_fifo_errors; /* Recv'r fifo overrun */ - uint32_t rx_missed_errors; /* Receiver missed packet */ - - /* Detailed tx_errors */ - - uint32_t tx_aborted_errors; - uint32_t tx_carrier_errors; - uint32_t tx_fifo_errors; - uint32_t tx_heartbeat_errors; - uint32_t tx_window_errors; - - /* For cslip etc */ - - uint32_t rx_compressed; - uint32_t tx_compressed; - - uint32_t rx_nohandler; /* Dropped, no handler found */ -}; + return *(FAR uint64_t *)nla_data(nla); +} -/* The main device statistics structure */ +/**************************************************************************** + * Name: nla_get_s8 + * + * Description: + * Return the value of a s8 attribute. + * + * Input Parameters: + * nla - The netlink attribute + * + * Returned Value: + * The s8 value + * + ****************************************************************************/ -struct rtnl_link_stats64 +static inline int8_t nla_get_s8(FAR const struct nlattr *nla) { - uint64_t rx_packets; /* Total packets received */ - uint64_t tx_packets; /* Total packets transmitted */ - uint64_t rx_bytes; /* Total bytes received */ - uint64_t tx_bytes; /* Total bytes transmitted */ - uint64_t rx_errors; /* Bad packets received */ - uint64_t tx_errors; /* Packet transmit problems */ - uint64_t rx_dropped; /* No space in linux buffers */ - uint64_t tx_dropped; /* No space available in linux */ - uint64_t multicast; /* Multicast packets received */ - uint64_t collisions; - - /* Detailed rx_errors: */ - - uint64_t rx_length_errors; - uint64_t rx_over_errors; /* Receiver ring buff overflow */ - uint64_t rx_crc_errors; /* Recved pkt with crc error */ - uint64_t rx_frame_errors; /* Recv'd frame alignment error */ - uint64_t rx_fifo_errors; /* Recv'r fifo overrun */ - uint64_t rx_missed_errors; /* Receiver missed packet */ - - /* Detailed tx_errors */ - - uint64_t tx_aborted_errors; - uint64_t tx_carrier_errors; - uint64_t tx_fifo_errors; - uint64_t tx_heartbeat_errors; - uint64_t tx_window_errors; - - /* For cslip etc */ - - uint64_t rx_compressed; - uint64_t tx_compressed; - uint64_t rx_nohandler; /* Dropped, no handler found */ -}; + return *(FAR int8_t *)nla_data(nla); +} + +/**************************************************************************** + * Name: nla_get_s16 + * + * Description: + * Return the value of a s16 attribute. + * + * Input Parameters: + * nla - The netlink attribute + * + * Returned Value: + * The s16 value + * + ****************************************************************************/ + +static inline int16_t nla_get_s16(FAR const struct nlattr *nla) +{ + return *(FAR int16_t *)nla_data(nla); +} + +/**************************************************************************** + * Name: nla_get_s32 + * + * Description: + * Return the value of a s32 attribute. + * + * Input Parameters: + * nla - The netlink attribute + * + * Returned Value: + * The s32 value + * + ****************************************************************************/ + +static inline int32_t nla_get_s32(FAR const struct nlattr *nla) +{ + return *(FAR int32_t *)nla_data(nla); +} + +/**************************************************************************** + * Name: nla_get_s64 + * + * Description: + * Return the value of a s64 attribute. + * + * Input Parameters: + * nla - The netlink attribute + * + * Returned Value: + * The s64 value + * + ****************************************************************************/ + +static inline int64_t nla_get_s64(FAR const struct nlattr *nla) +{ + return *(FAR int64_t *)nla_data(nla); +} + +/**************************************************************************** + * Name: nla_get_flag + * + * Description: + * Return the value of a flag attribute. + * + * Input Parameters: + * nla - The netlink attribute + * + * Returned Value: + * 1 if flag is present, 0 otherwise + * + ****************************************************************************/ + +static inline int nla_get_flag(FAR const struct nlattr *nla) +{ + return !!nla; +} + +/**************************************************************************** + * Name: nla_for_each_attr + * + * Description: + * Iterate over a stream of attributes. + * + * Input Parameters: + * pos - Loop counter, set to current attribute + * head - Head of attribute stream + * len - Length of attribute stream + * rem - Initialized to len, holds bytes currently remaining in stream + * + ****************************************************************************/ + +#define nla_for_each_attr(pos, head, len, rem) \ + for (pos = (FAR struct nlattr *)(head), rem = (len); \ + nla_len(pos) >= 0 && rem >= (int)sizeof(struct nlattr) && \ + pos->nla_len >= sizeof(struct nlattr) && \ + pos->nla_len <= rem; \ + rem -= NLA_ALIGN(pos->nla_len), \ + pos = (FAR struct nlattr *)(((FAR char *)pos) + NLA_ALIGN(pos->nla_len))) + +/**************************************************************************** + * Name: nla_for_each_nested + * + * Description: + * Iterate over nested attributes. + * + * Input Parameters: + * pos - Loop counter, set to current attribute + * nla - Attribute containing the nested attributes + * rem - Initialized to nla_len(nla), holds bytes currently remaining + * + ****************************************************************************/ + +#define nla_for_each_nested(pos, nla, rem) \ + nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem) /**************************************************************************** * Public Function Prototypes ****************************************************************************/ +/**************************************************************************** + * Included Files + ****************************************************************************/ + +/* The definitions of the individual protocols used to live in this header. + * They now have one header each, which are included here so that a user + * that includes only <netpacket/netlink.h> still sees everything, as it + * did before. Include the protocol header directly in new code. + * + * These come last: each of them includes this header for the common + * definitions, and the include guard above breaks the cycle. + */ + +#include <netpacket/rtnetlink.h> +#include <netpacket/nfnetlink.h> +#include <netpacket/genetlink.h> + #endif /* __INCLUDE_NETPACKET_NETLINK_H */ diff --git a/include/netpacket/nfnetlink.h b/include/netpacket/nfnetlink.h new file mode 100644 index 00000000000..50bf800b99e --- /dev/null +++ b/include/netpacket/nfnetlink.h @@ -0,0 +1,186 @@ +/**************************************************************************** + * include/netpacket/nfnetlink.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NETPACKET_NFNETLINK_H +#define __INCLUDE_NETPACKET_NFNETLINK_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <netpacket/netlink.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Attribute definitions for struct nfattr **********************************/ + +#define NFNL_NFA_NEST 0x8000 +#define NFA_TYPE(attr) ((attr)->nfa_type & 0x7fff) + +#define NFA_MASK (sizeof(uint32_t) - 1) +#define NFA_ALIGN(n) (((n) + NFA_MASK) & ~NFA_MASK) +#define NFA_OK(nfa, n) \ + ((n) >= (int)sizeof(struct nfattr) && \ + (nfa)->nfa_len >= sizeof(struct nfattr) && (nfa)->nfa_len <= (n)) +#define NFA_NEXT(nfa, attrlen) \ + ((attrlen) -= NFA_ALIGN((nfa)->nfa_len), \ + (FAR struct nfattr *)(((FAR char *)(nfa)) + \ + NFA_ALIGN((nfa)->nfa_len))) +#define NFA_LENGTH(len) (sizeof(struct nfattr) + (len)) +#define NFA_SPACE(len) NFA_ALIGN(NFA_LENGTH(len)) +#define NFA_DATA(nfa) \ + ((FAR void *)(((FAR char *)(nfa)) + NFA_LENGTH(0))) +#define NFA_PAYLOAD(nfa) \ + ((int)((nfa)->nfa_len) - NFA_LENGTH(0)) + +/* Definitions for struct nfgenmsg ******************************************/ + +#define NFM_NFA(n) \ + ((FAR struct nfattr *)(((FAR char *)(n)) + \ + NLMSG_ALIGN(sizeof(struct nfgenmsg)))) +#define NFM_PAYLOAD(n) \ + NLMSG_PAYLOAD(n, sizeof(struct nfgenmsg)) + +#define NFNETLINK_V0 0 + +#define NFNL_SUBSYS_ID(x) (((x) & 0xff00) >> 8) +#define NFNL_MSG_TYPE(x) ((x) & 0x00ff) + +#define NFNL_SUBSYS_NONE 0 +#define NFNL_SUBSYS_CTNETLINK 1 +#define NFNL_SUBSYS_CTNETLINK_EXP 2 +#define NFNL_SUBSYS_QUEUE 3 +#define NFNL_SUBSYS_ULOG 4 +#define NFNL_SUBSYS_OSF 5 +#define NFNL_SUBSYS_IPSET 6 +#define NFNL_SUBSYS_ACCT 7 +#define NFNL_SUBSYS_CTNETLINK_TIMEOUT 8 +#define NFNL_SUBSYS_CTHELPER 9 +#define NFNL_SUBSYS_NFTABLES 10 +#define NFNL_SUBSYS_NFT_COMPAT 11 +#define NFNL_SUBSYS_HOOK 12 +#define NFNL_SUBSYS_COUNT 13 + +#define IPCTNL_MSG_CT_NEW 0 +#define IPCTNL_MSG_CT_GET 1 +#define IPCTNL_MSG_CT_DELETE 2 +#define IPCTNL_MSG_CT_GET_CTRZERO 3 +#define IPCTNL_MSG_CT_GET_STATS_CPU 4 +#define IPCTNL_MSG_CT_GET_STATS 5 +#define IPCTNL_MSG_CT_GET_DYING 6 +#define IPCTNL_MSG_CT_GET_UNCONFIRMED 7 +#define IPCTNL_MSG_MAX 8 + +#define CTA_UNSPEC 0 +#define CTA_TUPLE_ORIG 1 +#define CTA_TUPLE_REPLY 2 +#define CTA_STATUS 3 +#define CTA_PROTOINFO 4 +#define CTA_HELP 5 +#define CTA_NAT_SRC 6 +#define CTA_TIMEOUT 7 +#define CTA_MARK 8 +#define CTA_COUNTERS_ORIG 9 +#define CTA_COUNTERS_REPLY 10 +#define CTA_USE 11 +#define CTA_ID 12 +#define CTA_NAT_DST 13 +#define CTA_TUPLE_MASTER 14 +#define CTA_SEQ_ADJ_ORIG 15 +#define CTA_NAT_SEQ_ADJ_ORIG CTA_SEQ_ADJ_ORIG +#define CTA_SEQ_ADJ_REPLY 16 +#define CTA_NAT_SEQ_ADJ_REPLY CTA_SEQ_ADJ_REPLY +#define CTA_ZONE 18 +#define CTA_SECCTX 19 +#define CTA_TIMESTAMP 20 +#define CTA_MARK_MASK 21 +#define CTA_LABELS 22 +#define CTA_LABELS_MASK 23 +#define CTA_SYNPROXY 24 +#define CTA_FILTER 25 +#define CTA_STATUS_MASK 26 +#define CTA_MAX 26 + +#define CTA_TUPLE_UNSPEC 0 +#define CTA_TUPLE_IP 1 +#define CTA_TUPLE_PROTO 2 +#define CTA_TUPLE_ZONE 3 +#define CTA_TUPLE_MAX 3 + +#define CTA_IP_UNSPEC 0 +#define CTA_IP_V4_SRC 1 +#define CTA_IP_V4_DST 2 +#define CTA_IP_V6_SRC 3 +#define CTA_IP_V6_DST 4 +#define CTA_IP_MAX 4 + +#define CTA_PROTO_UNSPEC 0 +#define CTA_PROTO_NUM 1 +#define CTA_PROTO_SRC_PORT 2 +#define CTA_PROTO_DST_PORT 3 +#define CTA_PROTO_ICMP_ID 4 +#define CTA_PROTO_ICMP_TYPE 5 +#define CTA_PROTO_ICMP_CODE 6 +#define CTA_PROTO_ICMPV6_ID 7 +#define CTA_PROTO_ICMPV6_TYPE 8 +#define CTA_PROTO_ICMPV6_CODE 9 +#define CTA_PROTO_MAX 9 + +#define NF_NETLINK_CONNTRACK_NEW 0x00000001 +#define NF_NETLINK_CONNTRACK_UPDATE 0x00000002 +#define NF_NETLINK_CONNTRACK_DESTROY 0x00000004 +#define NF_NETLINK_CONNTRACK_EXP_NEW 0x00000008 +#define NF_NETLINK_CONNTRACK_EXP_UPDATE 0x00000010 +#define NF_NETLINK_CONNTRACK_EXP_DESTROY 0x00000020 + +#define NFNLGRP_NONE 0 +#define NFNLGRP_CONNTRACK_NEW 1 +#define NFNLGRP_CONNTRACK_UPDATE 2 +#define NFNLGRP_CONNTRACK_DESTROY 3 +#define NFNLGRP_CONNTRACK_EXP_NEW 4 +#define NFNLGRP_CONNTRACK_EXP_UPDATE 5 +#define NFNLGRP_CONNTRACK_EXP_DESTROY 6 +#define NFNLGRP_NFTABLES 7 +#define NFNLGRP_ACCT_QUOTA 8 +#define NFNLGRP_NFTRACE 9 +#define NFNLGRP_MAX 9 + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +struct nfattr +{ + uint16_t nfa_len; + uint16_t nfa_type; +}; + +struct nfgenmsg +{ + uint8_t nfgen_family; + uint8_t version; + uint16_t res_id; +}; + +#endif /* __INCLUDE_NETPACKET_NFNETLINK_H */ diff --git a/include/netpacket/netlink.h b/include/netpacket/rtnetlink.h similarity index 51% copy from include/netpacket/netlink.h copy to include/netpacket/rtnetlink.h index 9d67b6d1aa7..2ba9143d363 100644 --- a/include/netpacket/netlink.h +++ b/include/netpacket/rtnetlink.h @@ -1,5 +1,5 @@ /**************************************************************************** - * include/netpacket/netlink.h + * include/netpacket/rtnetlink.h * * SPDX-License-Identifier: Apache-2.0 * @@ -20,123 +20,20 @@ * ****************************************************************************/ -#ifndef __INCLUDE_NETPACKET_NETLINK_H -#define __INCLUDE_NETPACKET_NETLINK_H +#ifndef __INCLUDE_NETPACKET_RTNETLINK_H +#define __INCLUDE_NETPACKET_RTNETLINK_H /**************************************************************************** * Included Files ****************************************************************************/ -#include <nuttx/config.h> -#include <nuttx/compiler.h> - -#include <sys/socket.h> -#include <stdint.h> - +#include <netpacket/netlink.h> #include <netpacket/if_addr.h> /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ -/* Netlink socket protocols *************************************************/ - -/* The AF_NETLINK family offers multiple protocol subsets. Each interfaces - * to a different kernel component and has a different messaging subset. The - * subset is referenced by the protocol field in the socket call: - * - * int socket(AF_NETLINK, SOCK_DGRAM or SOCK_RAW, protocol) - * - * Ref. Wikipedia.org - * - * Namespace is Linux compatible. - */ - -#define NETLINK_ROUTE 0 /* Routing/device hook for user-space - * routing daemons (default) */ -#define NETLINK_UNUSED 1 /* Unused number */ -#define NETLINK_USERSOCK 2 /* Reserved for user mode socket protocols */ -#define NETLINK_FIREWALL 3 /* Interface to receive packets from - * the firewall */ -#define NETLINK_SOCK_DIAG 4 /* Socket monitoring */ -#define NETLINK_NFLOG 5 /* netfilter/iptables ULOG */ -#define NETLINK_XFRM 6 /* Interface to IPsec security databases - * for key-manager daemons using the - * Internet Key Exchange protocol. */ -#define NETLINK_SELINUX 7 /* SELinux event notifications */ -#define NETLINK_ISCSI 8 /* Open-iSCSI */ -#define NETLINK_AUDIT 9 /* Interface to auditing sub-system */ -#define NETLINK_FIB_LOOKUP 10 -#define NETLINK_CONNECTOR 11 -#define NETLINK_NETFILTER 12 /* netfilter subsystem */ -#define NETLINK_IP6_FW 13 /* Interface to transport packets from - * netfilter to user-space. */ -#define NETLINK_DNRTMSG 14 /* DECnet routing messages */ -#define NETLINK_KOBJECT_UEVENT 15 /* Kernel messages to userspace */ -#define NETLINK_GENERIC 16 /* NETLINK_DM (DM Events) */ -#define NETLINK_SCSITRANSPORT 18 /* SCSI Transports */ -#define NETLINK_ECRYPTFS 19 -#define NETLINK_RDMA 20 -#define NETLINK_CRYPTO 21 /* Crypto layer */ -#define NETLINK_SMC 22 /* SMC monitoring */ - -/* Definitions associated with struct sockaddr_nl ***************************/ - -/* Flags values */ - -#define NLM_F_REQUEST 0x0001 /* It is request message. */ -#define NLM_F_MULTI 0x0002 /* Multipart message, terminated by NLMSG_DONE */ -#define NLM_F_ACK 0x0004 /* Reply with ack, with zero or error code */ -#define NLM_F_ECHO 0x0008 /* Echo this request */ -#define NLM_F_DUMP_INTR 0x0010 /* Dump was inconsistent due to sequence change */ -#define NLM_F_DUMP_FILTERED 0x0020 /* Dump was filtered as requested */ - -/* Modifiers to GET request */ - -#define NLM_F_ROOT 0x0100 /* Specify tree root */ -#define NLM_F_MATCH 0x0200 /* Return all matching */ -#define NLM_F_ATOMIC 0x0400 /* Atomic GET */ -#define NLM_F_DUMP (NLM_F_ROOT | NLM_F_MATCH) - -/* Modifiers to NEW request */ - -#define NLM_F_REPLACE 0x0100 /* Override existing */ -#define NLM_F_EXCL 0x0200 /* Do not touch, if it exists */ -#define NLM_F_CREATE 0x0400 /* Create, if it does not exist */ -#define NLM_F_APPEND 0x0800 /* Add to end of list */ - -/* Modifiers to DELETE request */ - -#define NLM_F_NONREC 0x0100 /* Do not delete recursively */ - -/* Flags for ACK message */ - -#define NLM_F_CAPPED 0x0100 /* Request was capped */ -#define NLM_F_ACK_TLVS 0x0200 /* Extended ACK TVLs were included */ - -/* Definitions for struct nlmsghdr ******************************************/ - -#define NLMSG_ALIGNTO 4 -#define NLMSG_MASK (sizeof(uint32_t) - 1) -#define NLMSG_ALIGN(n) (((n) + NLMSG_MASK) & ~NLMSG_MASK) -#define NLMSG_HDRLEN sizeof(struct nlmsghdr) -#define NLMSG_LENGTH(n) (NLMSG_HDRLEN + (n)) -#define NLMSG_SPACE(len) NLMSG_ALIGN(NLMSG_LENGTH(len)) -#define NLMSG_DATA(hdr) ((FAR void *)(((FAR char *)hdr) + NLMSG_HDRLEN)) -#define NLMSG_NEXT(hdr, n) ((n) -= NLMSG_ALIGN((hdr)->nlmsg_len), \ - (FAR struct nlmsghdr *) \ - (((FAR char *)(hdr)) + NLMSG_ALIGN((hdr)->nlmsg_len))) -#define NLMSG_OK(nlh, len) ((len) >= (int)sizeof(struct nlmsghdr) && \ - (nlh)->nlmsg_len >= sizeof(struct nlmsghdr) && \ - (nlh)->nlmsg_len <= (len)) -#define NLMSG_PAYLOAD(hdr, len) ((hdr)->nlmsg_len - NLMSG_SPACE(len)) - -#define NLMSG_NOOP 1 /* Nothing */ -#define NLMSG_ERROR 2 /* Error */ -#define NLMSG_DONE 3 /* End of a dump */ -#define NLMSG_OVERRUN 4 /* Data lost */ -#define NLMSG_MIN_TYPE 16 /* < 16: Reserved control messages */ - /* Attribute definitions for struct rtattr **********************************/ /* Macros to handle attribute lists */ @@ -291,23 +188,24 @@ /* Values for rta_type */ -#define IFLA_IFNAME 1 -#define IFLA_ADDRESS 2 -#define IFLA_MTU 3 -#define IFLA_WIRELESS 4 -#define IFLA_STATS 5 -#define IFLA_OPERSTATE 6 -#define IFLA_LINKMODE 7 -#define IFLA_BROADCAST 8 -#define IFLA_LINK 9 -#define IFLA_QDISC 10 -#define IFLA_COST 11 -#define IFLA_PRIORITY 12 -#define IFLA_MASTER 13 -#define IFLA_PROTINFO 14 -#define IFLA_TXQLEN 15 -#define IFLA_MAP 16 -#define IFLA_WEIGHT 17 +#define IFLA_UNSPEC 0 +#define IFLA_ADDRESS 1 +#define IFLA_BROADCAST 2 +#define IFLA_IFNAME 3 +#define IFLA_MTU 4 +#define IFLA_LINK 5 +#define IFLA_QDISC 6 +#define IFLA_STATS 7 +#define IFLA_COST 8 +#define IFLA_PRIORITY 9 +#define IFLA_MASTER 10 +#define IFLA_WIRELESS 11 +#define IFLA_PROTINFO 12 +#define IFLA_TXQLEN 13 +#define IFLA_MAP 14 +#define IFLA_WEIGHT 15 +#define IFLA_OPERSTATE 16 +#define IFLA_LINKMODE 17 /* Definitions for struct rtmsg *********************************************/ @@ -318,11 +216,11 @@ /* rtm_table. Routing table identifiers */ -#define RT_TABLE_UNSPEC 0 - /* 1-251: User defined values */ -#define RT_TABLE_MAIN 254 -#define RT_TABLE_LOCAL 255 -#define RT_TABLE_MAX 0xffffffff +#define RT_TABLE_UNSPEC 0 + /* 1-251: User defined values */ +#define RT_TABLE_MAIN 254 +#define RT_TABLE_LOCAL 255 +#define RT_TABLE_MAX 0xffffffff /* rtm_type */ @@ -414,252 +312,14 @@ #define FRA_TABLE 2 /* Extended table id */ #define FRA_FWMASK 3 /* Mask for netfilter mark */ -/* nla_type (16 bits) - * +---+---+-------------------------------+ - * | N | O | Attribute Type | - * +---+---+-------------------------------+ - * N := Carries nested attributes - * O := Payload stored in network byte order - * - * Note: The N and O flag are mutually exclusive. - */ - -#define NLA_F_NET_BYTEORDER (1 << 14) -#define NLA_F_NESTED (1 << 15) -#define NLA_TYPE_MASK ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER) - -#define NLA_ALIGNTO 4 -#define NLA_ALIGN(len) (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1)) -#define NLA_HDRLEN (NLA_ALIGN(sizeof(struct nlattr))) - /* rtm_flags */ #define RTM_F_CLONED 0x200 /* This route is cloned */ -/* Attribute definitions for struct nfattr **********************************/ - -/* Macros to handle attribute lists */ - -#define NFNL_NFA_NEST 0x8000 -#define NFA_TYPE(attr) ((attr)->nfa_type & 0x7fff) - -#define NFA_MASK (sizeof(uint32_t) - 1) -#define NFA_ALIGN(n) (((n) + NFA_MASK) & ~NFA_MASK) -#define NFA_OK(nfa, n) ((n) >= (int)sizeof(struct nfattr) && \ - (nfa)->nfa_len >= sizeof(struct nfattr) && \ - (nfa)->nfa_len <= (n)) -#define NFA_NEXT(nfa, attrlen) ((attrlen) -= NFA_ALIGN((nfa)->nfa_len), \ - (FAR struct nfattr *)(((FAR char *)(nfa)) + \ - NFA_ALIGN((nfa)->nfa_len))) -#define NFA_LENGTH(len) ((sizeof(struct nfattr)) + (len)) -#define NFA_SPACE(len) NFA_ALIGN(NFA_LENGTH(len)) -#define NFA_DATA(nfa) ((FAR void *)(((FAR char *)(nfa)) + NFA_LENGTH(0))) -#define NFA_PAYLOAD(nfa) ((int)((nfa)->nfa_len) - NFA_LENGTH(0)) - -/* Definitions for struct nfgenmsg ******************************************/ - -#define NFM_NFA(n) ((FAR struct nfattr *) \ - (((FAR char *)(n)) + \ - NLMSG_ALIGN(sizeof(struct nfgenmsg)))) -#define NFM_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct nfgenmsg)) - -/* Definitions for NETLINK_NETFILTER ****************************************/ - -#define NFNETLINK_V0 0 - -/* netfilter netlink message types are split in two pieces: - * 8 bit subsystem, 8bit operation. - */ - -#define NFNL_SUBSYS_ID(x) (((x) & 0xff00) >> 8) -#define NFNL_MSG_TYPE(x) ((x) & 0x00ff) - -/* Subsystems */ - -#define NFNL_SUBSYS_NONE 0 -#define NFNL_SUBSYS_CTNETLINK 1 -#define NFNL_SUBSYS_CTNETLINK_EXP 2 -#define NFNL_SUBSYS_QUEUE 3 -#define NFNL_SUBSYS_ULOG 4 -#define NFNL_SUBSYS_OSF 5 -#define NFNL_SUBSYS_IPSET 6 -#define NFNL_SUBSYS_ACCT 7 -#define NFNL_SUBSYS_CTNETLINK_TIMEOUT 8 -#define NFNL_SUBSYS_CTHELPER 9 -#define NFNL_SUBSYS_NFTABLES 10 -#define NFNL_SUBSYS_NFT_COMPAT 11 -#define NFNL_SUBSYS_HOOK 12 -#define NFNL_SUBSYS_COUNT 13 - -/* NETLINK_NETFILTER: subsystem CTNL (ip conntrack netlink) message types */ - -#define IPCTNL_MSG_CT_NEW 0 -#define IPCTNL_MSG_CT_GET 1 -#define IPCTNL_MSG_CT_DELETE 2 -#define IPCTNL_MSG_CT_GET_CTRZERO 3 -#define IPCTNL_MSG_CT_GET_STATS_CPU 4 -#define IPCTNL_MSG_CT_GET_STATS 5 -#define IPCTNL_MSG_CT_GET_DYING 6 -#define IPCTNL_MSG_CT_GET_UNCONFIRMED 7 -#define IPCTNL_MSG_MAX 8 - -/* NETLINK_NETFILTER: Conntrack attributes */ - -#define CTA_UNSPEC 0 -#define CTA_TUPLE_ORIG 1 -#define CTA_TUPLE_REPLY 2 -#define CTA_STATUS 3 -#define CTA_PROTOINFO 4 -#define CTA_HELP 5 -#define CTA_NAT_SRC 6 -#define CTA_TIMEOUT 7 -#define CTA_MARK 8 -#define CTA_COUNTERS_ORIG 9 -#define CTA_COUNTERS_REPLY 10 -#define CTA_USE 11 -#define CTA_ID 12 -#define CTA_NAT_DST 13 -#define CTA_TUPLE_MASTER 14 -#define CTA_SEQ_ADJ_ORIG 15 -#define CTA_NAT_SEQ_ADJ_ORIG CTA_SEQ_ADJ_ORIG -#define CTA_SEQ_ADJ_REPLY 16 -#define CTA_NAT_SEQ_ADJ_REPLY CTA_SEQ_ADJ_REPLY -#define CTA_ZONE 18 -#define CTA_SECCTX 19 -#define CTA_TIMESTAMP 20 -#define CTA_MARK_MASK 21 -#define CTA_LABELS 22 -#define CTA_LABELS_MASK 23 -#define CTA_SYNPROXY 24 -#define CTA_FILTER 25 -#define CTA_STATUS_MASK 26 -#define CTA_MAX 26 - -/* NETLINK_NETFILTER: Conntrack tuple attributes */ - -#define CTA_TUPLE_UNSPEC 0 -#define CTA_TUPLE_IP 1 -#define CTA_TUPLE_PROTO 2 -#define CTA_TUPLE_ZONE 3 -#define CTA_TUPLE_MAX 3 - -/* NETLINK_NETFILTER: Conntrack IP attributes */ - -#define CTA_IP_UNSPEC 0 -#define CTA_IP_V4_SRC 1 -#define CTA_IP_V4_DST 2 -#define CTA_IP_V6_SRC 3 -#define CTA_IP_V6_DST 4 -#define CTA_IP_MAX 4 - -/* NETLINK_NETFILTER: Conntrack protocol attributes */ - -#define CTA_PROTO_UNSPEC 0 -#define CTA_PROTO_NUM 1 -#define CTA_PROTO_SRC_PORT 2 -#define CTA_PROTO_DST_PORT 3 -#define CTA_PROTO_ICMP_ID 4 -#define CTA_PROTO_ICMP_TYPE 5 -#define CTA_PROTO_ICMP_CODE 6 -#define CTA_PROTO_ICMPV6_ID 7 -#define CTA_PROTO_ICMPV6_TYPE 8 -#define CTA_PROTO_ICMPV6_CODE 9 -#define CTA_PROTO_MAX 9 - -/* NFnetlink multicast groups (userspace) */ - -#define NF_NETLINK_CONNTRACK_NEW 0x00000001 -#define NF_NETLINK_CONNTRACK_UPDATE 0x00000002 -#define NF_NETLINK_CONNTRACK_DESTROY 0x00000004 -#define NF_NETLINK_CONNTRACK_EXP_NEW 0x00000008 -#define NF_NETLINK_CONNTRACK_EXP_UPDATE 0x00000010 -#define NF_NETLINK_CONNTRACK_EXP_DESTROY 0x00000020 - -/* NFnetlink multicast groups */ - -#define NFNLGRP_NONE 0 -#define NFNLGRP_CONNTRACK_NEW 1 -#define NFNLGRP_CONNTRACK_UPDATE 2 -#define NFNLGRP_CONNTRACK_DESTROY 3 -#define NFNLGRP_CONNTRACK_EXP_NEW 4 -#define NFNLGRP_CONNTRACK_EXP_UPDATE 5 -#define NFNLGRP_CONNTRACK_EXP_DESTROY 6 -#define NFNLGRP_NFTABLES 7 -#define NFNLGRP_ACCT_QUOTA 8 -#define NFNLGRP_NFTRACE 9 -#define NFNLGRP_MAX 9 - /**************************************************************************** * Public Type Definitions ****************************************************************************/ -/* Standard attribute types to specify validation policy */ - -enum -{ - NLA_UNSPEC, - NLA_U8, - NLA_U16, - NLA_U32, - NLA_U64, - NLA_STRING, - NLA_FLAG, - NLA_MSECS, - NLA_NESTED, - NLA_NESTED_COMPAT, - NLA_NUL_STRING, - NLA_BINARY, - NLA_S8, - NLA_S16, - NLA_S32, - NLA_S64, - NLA_BITFIELD32, - NLA_TYPE_MAX = NLA_BITFIELD32, -}; - -/* Netlink socket address type. */ - -struct sockaddr_nl -{ - sa_family_t nl_family; /* AF_NETLINK */ - uint16_t nl_pad; /* Zero */ - uint32_t nl_pid; /* Port ID */ - uint32_t nl_groups; /* Multicast groups mask */ -}; - -/* Packet structure. The Netlink message header, struct nlmsghdr, must be - * prepared by the caller. The Netlink socket generally works in a SOCK_RAW- - * like mode (even if SOCK_DGRAM was used to create it). - * - * The data portion then contains a subsystem-specific message that may be - * further nested. - */ - -struct nlmsghdr -{ - uint32_t nlmsg_len; /* Length of message including header */ - uint16_t nlmsg_type; /* Message content */ - uint16_t nlmsg_flags; /* Additional flags */ - uint32_t nlmsg_seq; /* Sequence number */ - uint32_t nlmsg_pid; /* Sending process port ID */ - /* Data follows */ -}; - -struct nlmsgerr -{ - int32_t error; - struct nlmsghdr msg; - - /* Followed by the message contents unless NETLINK_CAP_ACK was set - * or the ACK indicates success (error == 0) - * message length is aligned with NLMSG_ALIGN() - */ - - /* Followed by TLVs defined in enum nlmsgerr_attrs - * if NETLINK_EXT_ACK was set - */ -}; - /* NETLINK_ROUTE Message Structures *****************************************/ /* RTM_NEWLINK, RTM_DELLINK, RTM_GETLINK @@ -754,59 +414,6 @@ struct prefix_cacheinfo uint32_t valid_time; }; -/* <------- NLA_HDRLEN ------> <-- NLA_ALIGN(payload)--> - * +---------------------+- - -+- - - - - - - - - -+- - -+ - * | Header | Pad | Payload | Pad | - * | (struct nlattr) | ing | | ing | - * +---------------------+- - -+- - - - - - - - - -+- - -+ - * <-------------- nlattr->nla_len --------------> - */ - -struct nlattr -{ - uint16_t nla_len; - uint16_t nla_type; -}; - -/* Generic 32 bitflags attribute content sent to the kernel. - * - * The value is a bitmap that defines the values being set - * The selector is a bitmask that defines which value is legit - * - * Examples: - * value = 0x0, and selector = 0x1 - * implies we are selecting bit 1 and we want to set its value to 0. - * - * value = 0x2, and selector = 0x2 - * implies we are selecting bit 2 and we want to set its value to 1. - * - */ - -struct nla_bitfield32 -{ - uint32_t value; - uint32_t selector; -}; - -/* NETLINK_NETFILTER Message Structures *************************************/ - -/* These attributes should be manipulated using only the NFA_* */ - -struct nfattr -{ - uint16_t nfa_len; - uint16_t nfa_type; -}; - -/* General form of address family dependent message. */ - -struct nfgenmsg -{ - uint8_t nfgen_family; /* AF_xxx */ - uint8_t version; /* nfnetlink version */ - uint16_t res_id; /* Resource id */ -}; - /* Neighbor Discovery userland options **************************************/ struct nduseroptmsg @@ -913,4 +520,4 @@ struct rtnl_link_stats64 * Public Function Prototypes ****************************************************************************/ -#endif /* __INCLUDE_NETPACKET_NETLINK_H */ +#endif /* __INCLUDE_NETPACKET_RTNETLINK_H */ diff --git a/include/nuttx/net/genetlink.h b/include/nuttx/net/genetlink.h new file mode 100644 index 00000000000..eaac87848c0 --- /dev/null +++ b/include/nuttx/net/genetlink.h @@ -0,0 +1,171 @@ +/**************************************************************************** + * include/nuttx/net/genetlink.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_NET_GENETLINK_H +#define __INCLUDE_NUTTX_NET_GENETLINK_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <stdint.h> +#include <netpacket/genetlink.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define GENL_MAX_FAMILIES 8 +#define GENL_MCAST_GRPS_MAX 8 + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct netlink_conn_s; + +/* Multicast group definition - matches Linux struct genl_multicast_group */ + +struct genl_multicast_group +{ + char name[GENL_NAMSIZ]; +}; + +/* Generic netlink family - simplified version of Linux struct genl_family. + * + * Each family provides: + * - name: Family name (e.g. "nl80211"), used by CTRL_CMD_GETFAMILY + * - version: Protocol version + * - maxattr: Maximum attribute number understood by this family + * - hdrsize: Size of user-specific header in messages + * - cmd_handler: Callback to handle incoming messages for this family + * - mcgrps/n_mcgrps: Multicast group definitions + * + * The 'id' and 'mcgrp_offset' fields are assigned dynamically by + * genl_register_family(). + */ + +struct genl_family +{ + uint16_t id; /* Assigned dynamically; do not set */ + uint16_t mcgrp_offset; /* Assigned dynamically; do not set */ + char name[GENL_NAMSIZ]; + uint8_t version; + uint8_t hdrsize; + uint16_t maxattr; + + /* Command handler - called for every message sent to this family. */ + + int (*cmd_handler)(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *nlmsg); + + /* Multicast groups */ + + FAR const struct genl_multicast_group *mcgrps; + uint8_t n_mcgrps; +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/**************************************************************************** + * Name: genl_register_family + * + * Description: + * Register a generic netlink family. A unique family ID is allocated + * from the GENL_START_ALLOC range and stored in family->id. + * Multicast group IDs are also allocated and stored via + * family->mcgrp_offset. + * + * This mirrors the Linux kernel genl_register_family() API. + * + * Input Parameters: + * family - Pointer to the family structure to register. The 'name', + * 'version', 'maxattr', 'cmd_handler', 'mcgrps', and + * 'n_mcgrps' fields must be filled in by the caller. + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int genl_register_family(FAR struct genl_family *family); + +/**************************************************************************** + * Name: genl_unregister_family + * + * Description: + * Unregister a previously registered generic netlink family. + * + * Input Parameters: + * family - Pointer to the family structure to unregister. + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + * + ****************************************************************************/ + +int genl_unregister_family(FAR const struct genl_family *family); + +/**************************************************************************** + * Name: genl_family_find_byname + * + * Description: + * Look up a registered family by name. + * + * Input Parameters: + * name - The family name to search for. + * + * Returned Value: + * Pointer to the family on success; NULL if not found. + * + ****************************************************************************/ + +FAR const struct genl_family *genl_family_find_byname(FAR const char *name); + +/**************************************************************************** + * Name: genl_family_find_byid + * + * Description: + * Look up a registered family by ID. + * + * Input Parameters: + * id - The family ID to search for. + * + * Returned Value: + * Pointer to the family on success; NULL if not found. + * + ****************************************************************************/ + +FAR const struct genl_family *genl_family_find_byid(uint16_t id); + +#ifdef __cplusplus +} +#endif + +#endif /* __INCLUDE_NUTTX_NET_GENETLINK_H */ diff --git a/include/nuttx/net/netlink.h b/include/nuttx/net/netlink.h index 8ffdd39ee66..a5e47a9e2f4 100644 --- a/include/nuttx/net/netlink.h +++ b/include/nuttx/net/netlink.h @@ -101,20 +101,23 @@ void netlink_add_response(NETLINK_HANDLE handle, * Name: netlink_add_broadcast * * Description: - * Add broadcast data to all interested netlink connections. + * Add broadcast data to interested netlink connections that match both + * the specified protocol and multicast group. * * Note: The network will be momentarily locked to support exclusive * access to the pending response list. * * Input Parameters: - * group - The broadcast group index. - * data - The broadcast data. The memory referenced by 'data' - * must have been allocated via kmm_malloc(). It will be freed - * using kmm_free() after it has been consumed. + * protocol - The netlink protocol to match (NETLINK_ROUTE, etc). + * group - The broadcast group index. + * data - The broadcast data. The memory referenced by 'data' + * must have been allocated via kmm_malloc(). It will be freed + * using kmm_free() after it has been consumed. * ****************************************************************************/ -void netlink_add_broadcast(int group, FAR struct netlink_response_s *data); +void netlink_add_broadcast(int protocol, int group, + FAR struct netlink_response_s *data); #ifdef __cplusplus } diff --git a/include/sys/socket.h b/include/sys/socket.h index 65cd64d0dcc..688ca71d795 100644 --- a/include/sys/socket.h +++ b/include/sys/socket.h @@ -280,6 +280,7 @@ #define SOL_RFCOMM 18 /* See options in include/netpacket/bluetooth.h */ #define SOL_PACKET 19 +#define SOL_NETLINK 20 /* Values for the 'how' argument of shutdown() */ diff --git a/net/nat/ipv4_nat_entry.c b/net/nat/ipv4_nat_entry.c index 189143cc6ee..9fbfe479bf8 100644 --- a/net/nat/ipv4_nat_entry.c +++ b/net/nat/ipv4_nat_entry.c @@ -34,6 +34,8 @@ #include <nuttx/kmalloc.h> #include <nuttx/nuttx.h> +#include <netpacket/nfnetlink.h> + #include "nat/nat.h" #include "netlink/netlink.h" @@ -125,6 +127,7 @@ ipv4_nat_entry_create(uint8_t protocol, in_addr_t peer_ip, uint16_t peer_port) { FAR ipv4_nat_entry_t *entry = kmm_malloc(sizeof(ipv4_nat_entry_t)); + if (entry == NULL) { nwarn("WARNING: Failed to allocate IPv4 NAT entry\n"); diff --git a/net/nat/ipv6_nat_entry.c b/net/nat/ipv6_nat_entry.c index 6f6c6309327..dfc8dd78c77 100644 --- a/net/nat/ipv6_nat_entry.c +++ b/net/nat/ipv6_nat_entry.c @@ -34,6 +34,8 @@ #include <nuttx/kmalloc.h> #include <nuttx/nuttx.h> +#include <netpacket/nfnetlink.h> + #include "inet/inet.h" #include "nat/nat.h" #include "netlink/netlink.h" @@ -113,6 +115,7 @@ ipv6_nat_entry_create(uint8_t protocol, const net_ipv6addr_t external_ip, uint16_t peer_port) { FAR ipv6_nat_entry_t *entry = kmm_malloc(sizeof(ipv6_nat_entry_t)); + if (entry == NULL) { nwarn("WARNING: Failed to allocate IPv6 NAT entry\n"); diff --git a/net/netlink/CMakeLists.txt b/net/netlink/CMakeLists.txt index 7e45e73b21a..9d67d39d886 100644 --- a/net/netlink/CMakeLists.txt +++ b/net/netlink/CMakeLists.txt @@ -23,10 +23,14 @@ # Logic specific to NETLINK if(CONFIG_NET_NETLINK) - set(SRCS netlink_sockif.c netlink_conn.c netlink_notifier.c) + set(SRCS netlink_sockif.c netlink_conn.c netlink_notifier.c netlink_attr.c) if(CONFIG_NETLINK_ROUTE) - list(APPEND SRCS netlink_route.c netlink_attr.c) + list(APPEND SRCS netlink_route.c) + endif() + + if(CONFIG_NETLINK_GENERIC) + list(APPEND SRCS netlink_generic.c) endif() if(CONFIG_NETLINK_NETFILTER) diff --git a/net/netlink/Kconfig b/net/netlink/Kconfig index 9e9c1b4e70f..cebbd2a0305 100644 --- a/net/netlink/Kconfig +++ b/net/netlink/Kconfig @@ -86,6 +86,12 @@ config NETLINK_MAX_CONNS menu "Netlink Protocols" +config NETLINK_GENERIC + bool "Netlink Generic protocol" + default n + ---help--- + Support the NETLINK_GENERIC protocol option. + config NETLINK_ROUTE bool "Netlink Route protocol" default n diff --git a/net/netlink/Make.defs b/net/netlink/Make.defs index b3627f91670..84589103400 100644 --- a/net/netlink/Make.defs +++ b/net/netlink/Make.defs @@ -25,10 +25,14 @@ ifeq ($(CONFIG_NET_NETLINK),y) SOCK_CSRCS += netlink_sockif.c -NET_CSRCS += netlink_conn.c netlink_notifier.c +NET_CSRCS += netlink_conn.c netlink_notifier.c netlink_attr.c ifeq ($(CONFIG_NETLINK_ROUTE),y) -NET_CSRCS += netlink_route.c netlink_attr.c +NET_CSRCS += netlink_route.c +endif + +ifeq ($(CONFIG_NETLINK_GENERIC),y) +NET_CSRCS += netlink_generic.c endif ifeq ($(CONFIG_NETLINK_NETFILTER),y) diff --git a/net/netlink/netlink.h b/net/netlink/netlink.h index e06f5590212..0c4018aab89 100644 --- a/net/netlink/netlink.h +++ b/net/netlink/netlink.h @@ -34,6 +34,7 @@ #include <netpacket/cangw.h> #include <netpacket/netlink.h> +#include <netpacket/rtnetlink.h> #include <nuttx/queue.h> #include <nuttx/net/icmpv6.h> #include <nuttx/net/netlink.h> @@ -57,17 +58,6 @@ #ifdef CONFIG_NET_NETLINK -/* nla_for_each_attr - iterate over a stream of attributes - * pos: loop counter, set to current attribute - * head: head of attribute stream - * len: length of attribute stream - * rem: initialized to len, holds bytes currently remaining in stream - */ - -#define nla_for_each_attr(pos, head, len, rem) \ - for (pos = head, rem = len; nla_ok(pos, rem); \ - pos = nla_next(pos, &(rem))) - /* Always use this macro, this allows later putting the * message into a separate section or such for things * like translation or listing all possible messages. @@ -194,6 +184,7 @@ struct netlink_conn_s uint32_t dst_pid; /* Destination port ID */ uint32_t dst_groups; /* Destination multicast groups mask */ uint8_t crefs; /* Reference counts on this instance */ + uint8_t protocol; /* Netlink protocol (NETLINK_ROUTE, etc) */ /* poll() support */ @@ -560,6 +551,8 @@ void netlink_ipv6_prefix_notify(FAR struct net_driver_s *dev, int type, FAR const struct icmpv6_prefixinfo_s *pinfo); #endif +#endif /* CONFIG_NETLINK_ROUTE */ + /**************************************************************************** * Name: nla_next * @@ -603,7 +596,21 @@ int nla_parse(FAR struct nlattr **tb, int maxtype, int nla_memcpy(FAR void *dest, FAR const struct nlattr *src, int count); -#endif /* CONFIG_NETLINK_ROUTE */ +/**************************************************************************** + * Name: netlink_generic_sendto + * + * Description: + * Perform the sendto() operation for the NETLINK_GENERIC protocol. + * + ****************************************************************************/ + +#ifdef CONFIG_NETLINK_GENERIC +ssize_t netlink_generic_sendto(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *msg, + size_t len, int flags, + FAR const struct sockaddr_nl *to, + socklen_t tolen); +#endif /**************************************************************************** * Name: netlink_netfilter_sendto diff --git a/net/netlink/netlink_attr.c b/net/netlink/netlink_attr.c index 7eeea4e99ae..f6bd7f3a36e 100644 --- a/net/netlink/netlink_attr.c +++ b/net/netlink/netlink_attr.c @@ -124,6 +124,11 @@ static int validate_nla(FAR const struct nlattr *nla, int maxtype, return -EINVAL; } + if (policy == NULL) + { + return 0; + } + pt = &policy[type]; DEBUGASSERT(pt->type <= NLA_TYPE_MAX); diff --git a/net/netlink/netlink_conn.c b/net/netlink/netlink_conn.c index eb2b87ef7f0..935a7f80bf8 100644 --- a/net/netlink/netlink_conn.c +++ b/net/netlink/netlink_conn.c @@ -362,7 +362,7 @@ int netlink_add_terminator(NETLINK_HANDLE handle, if (group > 0) { - netlink_add_broadcast(group, resp); + netlink_add_broadcast(NETLINK_ROUTE, group, resp); } else { @@ -376,31 +376,41 @@ int netlink_add_terminator(NETLINK_HANDLE handle, * Name: netlink_add_broadcast * * Description: - * Add broadcast data to all interested netlink connections. + * Add broadcast data to interested netlink connections that match both + * the specified protocol and multicast group. * * Note: The network will be momentarily locked to support exclusive * access to the pending response list. * * Input Parameters: - * group - The broadcast group index. - * data - The broadcast data. The memory referenced by 'data' - * must have been allocated via kmm_malloc(). It will be freed - * using kmm_free() after it has been consumed. + * protocol - The netlink protocol to match (NETLINK_ROUTE, etc). + * group - The broadcast group index. + * data - The broadcast data. The memory referenced by 'data' + * must have been allocated via kmm_malloc(). It will be freed + * using kmm_free() after it has been consumed. * ****************************************************************************/ -void netlink_add_broadcast(int group, FAR struct netlink_response_s *data) +void netlink_add_broadcast(int protocol, int group, + FAR struct netlink_response_s *data) { FAR struct netlink_conn_s *conn = NULL; int first = 1; DEBUGASSERT(data != NULL); + if (group < 1 || group > 32) + { + kmm_free(data); + return; + } + netlink_lock(); while ((conn = netlink_nextconn(conn)) != NULL) { - if ((conn->groups & (1 << (group - 1))) == 0) + if (conn->protocol != protocol || + (conn->groups & (1u << (group - 1))) == 0) { continue; } @@ -412,7 +422,8 @@ void netlink_add_broadcast(int group, FAR struct netlink_response_s *data) FAR struct netlink_response_s *tmp; size_t len; - len = sizeof(sq_entry_t) + data->msg.nlmsg_len; + len = offsetof(struct netlink_response_s, msg) + + data->msg.nlmsg_len; tmp = kmm_malloc(len); if (tmp == NULL) { diff --git a/net/netlink/netlink_generic.c b/net/netlink/netlink_generic.c new file mode 100644 index 00000000000..0ea36bca843 --- /dev/null +++ b/net/netlink/netlink_generic.c @@ -0,0 +1,760 @@ +/**************************************************************************** + * net/netlink/netlink_generic.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <sys/types.h> +#include <string.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> + +#include <nuttx/net/netlink.h> +#include <nuttx/net/genetlink.h> +#include <nuttx/kmalloc.h> +#include <nuttx/mutex.h> + +#include "netlink/netlink.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Family registry - stores pointers to registered families */ + +static FAR struct genl_family *g_genl_families[GENL_MAX_FAMILIES]; +static mutex_t g_genl_lock = NXMUTEX_INITIALIZER; +static int g_genl_family_count; +static uint16_t g_genl_next_id = GENL_START_ALLOC; +static uint16_t g_genl_next_mcgrp = 1; /* Multicast group IDs start at 1 */ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: add_mcast_grp_attr + * + * Description: + * Helper to serialize one multicast group as nested rtattr. + * + * Input Parameters: + * buf - Buffer to write the attribute data. + * offset - Current offset in the buffer. + * index - Index of the multicast group. + * gname - Name of the multicast group. + * gid - ID of the multicast group. + * + * Returned Value: + * The aligned length of the added attribute. + * + ****************************************************************************/ + +static int add_mcast_grp_attr(FAR uint8_t *buf, int offset, + int index, FAR const char *gname, int gid) +{ + FAR struct rtattr *grp = (FAR struct rtattr *)(buf + offset); + FAR uint8_t *ptr = (FAR uint8_t *)RTA_DATA(grp); + FAR struct rtattr *attr; + int len = 0; + + grp->rta_type = index | NLA_F_NESTED; + + /* Group Name */ + + attr = (FAR struct rtattr *)ptr; + attr->rta_type = CTRL_ATTR_MCAST_GRP_NAME; + attr->rta_len = RTA_LENGTH(strlen(gname) + 1); + strlcpy((FAR char *)RTA_DATA(attr), gname, GENL_NAMSIZ); + len += RTA_ALIGN(attr->rta_len); + + /* Group ID */ + + attr = (FAR struct rtattr *)(ptr + len); + attr->rta_type = CTRL_ATTR_MCAST_GRP_ID; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = gid; + len += RTA_ALIGN(attr->rta_len); + + grp->rta_len = RTA_LENGTH(len); + return RTA_ALIGN(grp->rta_len); +} + +/**************************************************************************** + * Name: send_family_info + * + * Description: + * Build and queue a CTRL_CMD_NEWFAMILY response for one family. + * This is used both for CTRL_CMD_GETFAMILY replies and DUMP mode. + * + * Input Parameters: + * conn - The netlink connection to send the response to. + * req - The request message header. + * family - The generic netlink family to report. + * dump - True if this is part of a dump operation (NLM_F_MULTI). + * + * Returned Value: + * The length of the netlink message on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int send_family_info(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req, + FAR const struct genl_family *family, + bool dump) +{ + FAR struct netlink_response_s *response; + FAR struct genlmsghdr *genl; + FAR struct nlmsghdr *nlh; + FAR struct rtattr *attr; + FAR uint8_t *attr_ptr; + FAR uint8_t *buf; + int attr_len; + int buf_len; + int offset; + + /* Calculate required buffer size: + * base headers + fixed attrs + family name + mcast groups + */ + + buf_len = NLMSG_HDRLEN + sizeof(struct genlmsghdr) + + RTA_SPACE(2) /* FAMILY_ID */ + + RTA_SPACE(GENL_NAMSIZ) /* FAMILY_NAME */ + + RTA_SPACE(4) * 3; /* VERSION+HDRSIZE+MAXATTR */ + + if (family->mcgrps && family->n_mcgrps > 0) + { + int i; + + for (i = 0; i < family->n_mcgrps; i++) + { + buf_len += RTA_SPACE(RTA_SPACE(GENL_NAMSIZ) + RTA_SPACE(4)); + } + + buf_len += RTA_SPACE(0); /* mcast root header */ + } + + response = (FAR struct netlink_response_s *) + kmm_zalloc(sizeof(struct netlink_response_s) + NLMSG_ALIGN(buf_len)); + if (response == NULL) + { + return -ENOMEM; + } + + buf = (FAR uint8_t *)&response->msg; + nlh = (FAR struct nlmsghdr *)buf; + + nlh->nlmsg_type = GENL_ID_CTRL; + nlh->nlmsg_flags = dump ? NLM_F_MULTI : 0; + nlh->nlmsg_seq = req->nlmsg_seq; + nlh->nlmsg_pid = req->nlmsg_pid; + + genl = (FAR struct genlmsghdr *)NLMSG_DATA(nlh); + genl->cmd = CTRL_CMD_NEWFAMILY; + genl->version = 1; + + offset = NLMSG_ALIGN(sizeof(struct genlmsghdr)); + attr_ptr = (FAR uint8_t *)genl + offset; + + /* CTRL_ATTR_FAMILY_ID */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_FAMILY_ID; + attr->rta_len = RTA_LENGTH(2); + *(FAR uint16_t *)RTA_DATA(attr) = family->id; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_FAMILY_NAME */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_FAMILY_NAME; + attr->rta_len = RTA_LENGTH(strlen(family->name) + 1); + strlcpy((FAR char *)RTA_DATA(attr), family->name, GENL_NAMSIZ); + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_VERSION */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_VERSION; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->version; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_HDRSIZE */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_HDRSIZE; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->hdrsize; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_MAXATTR */ + + attr = (FAR struct rtattr *)attr_ptr; + attr->rta_type = CTRL_ATTR_MAXATTR; + attr->rta_len = RTA_LENGTH(4); + *(FAR uint32_t *)RTA_DATA(attr) = family->maxattr; + attr_len = RTA_ALIGN(attr->rta_len); + attr_ptr += attr_len; + offset += attr_len; + + /* CTRL_ATTR_MCAST_GROUPS — only if the family has multicast groups */ + + if (family->mcgrps && family->n_mcgrps > 0) + { + FAR struct rtattr *mcast = (FAR struct rtattr *)attr_ptr; + FAR uint8_t *grp; + int grp_len = 0; + int i; + + mcast->rta_type = CTRL_ATTR_MCAST_GROUPS | NLA_F_NESTED; + grp = (FAR uint8_t *)RTA_DATA(mcast); + + for (i = 0; i < family->n_mcgrps; i++) + { + grp_len += add_mcast_grp_attr(grp, grp_len, + i + 1, + family->mcgrps[i].name, + family->mcgrp_offset + i); + } + + mcast->rta_len = RTA_LENGTH(grp_len); + attr_len = RTA_ALIGN(mcast->rta_len); + attr_ptr += attr_len; + offset += attr_len; + } + + nlh->nlmsg_len = NLMSG_LENGTH(offset); + + netlink_add_response(conn, response); + return nlh->nlmsg_len; +} + +/**************************************************************************** + * Name: send_ack_response + * + * Description: + * Send an ACK (NLMSG_ERROR with error=0) response. + * + * Input Parameters: + * conn - The netlink connection to send the ACK to. + * req - The request message header. + * + * Returned Value: + * Zero (OK) is returned on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int send_ack_response(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req) +{ + FAR struct netlink_response_s *response; + FAR struct nlmsgerr *err; + int payload_len = sizeof(struct nlmsgerr); + + response = (FAR struct netlink_response_s *) + kmm_zalloc(sizeof(struct netlink_response_s) + NLMSG_ALIGN(payload_len)); + if (response == NULL) + { + return -ENOMEM; + } + + response->msg.nlmsg_len = NLMSG_LENGTH(payload_len); + response->msg.nlmsg_type = NLMSG_ERROR; + response->msg.nlmsg_flags = 0; + response->msg.nlmsg_seq = req->nlmsg_seq; + response->msg.nlmsg_pid = req->nlmsg_pid; + + err = (FAR struct nlmsgerr *)NLMSG_DATA(&response->msg); + err->error = 0; + memcpy(&err->msg, req, sizeof(struct nlmsghdr)); + + netlink_add_response(conn, response); + return 0; +} + +/**************************************************************************** + * Name: netlink_ctrl_getfamily + * + * Description: + * Handle a CTRL_CMD_GETFAMILY request. Supports both single-family + * lookup by name and NLM_F_DUMP to enumerate all families. + * + * Input Parameters: + * conn - The netlink connection handling the request. + * req - The request message header. + * + * Returned Value: + * The length of the request message on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +static int netlink_ctrl_getfamily(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *req) +{ + FAR const struct genl_family *fam; + FAR struct genlmsghdr *ghdr; + FAR struct nlattr *na; + FAR char *family_name = NULL; + int rem; + int ret; + int i; + + ghdr = (FAR struct genlmsghdr *)NLMSG_DATA(req); + na = (FAR struct nlattr *)((FAR char *)ghdr + GENL_HDRLEN); + rem = req->nlmsg_len - NLMSG_HDRLEN - GENL_HDRLEN; + + while (sizeof(struct nlattr) <= rem) + { + if (na->nla_len < sizeof(struct nlattr) || na->nla_len > rem) + { + break; + } + + if (nla_type(na) == CTRL_ATTR_FAMILY_NAME) + { + /* The name is used with strcmp() and printed with %s, so take it + * only when the payload really holds a string that fits. + */ + + FAR char *data = nla_data(na); + int len = nla_len(na); + + if (len > 0 && len <= GENL_NAMSIZ && + memchr(data, '\0', len) != NULL) + { + family_name = data; + } + } + + na = nla_next(na, &rem); + } + + if (req->nlmsg_flags & NLM_F_DUMP) + { + FAR const struct genl_family *snapshot[GENL_MAX_FAMILIES]; + FAR struct netlink_response_s *done_resp; + FAR struct nlmsghdr *done_hdr; + struct genl_family ctrl_fam; + int nfam = 0; + + /* Dump the built-in nlctrl family */ + + memset(&ctrl_fam, 0, sizeof(ctrl_fam)); + ctrl_fam.id = GENL_ID_CTRL; + strlcpy(ctrl_fam.name, "nlctrl", GENL_NAMSIZ); + ctrl_fam.version = 1; + ctrl_fam.maxattr = CTRL_ATTR_MAX; + send_family_info(conn, req, &ctrl_fam, true); + + /* Dump all registered families. Take a snapshot of the table under + * the lock, then send outside it: send_family_info() ends in + * netlink_add_response(), which takes the netlink lock, and nesting + * that inside g_genl_lock would fix an order that nothing else + * needs. + * + * A family unregistered after the snapshot still appears in this + * one dump. The pointer stays valid, because the family is owned + * by its caller and genl_unregister_family() does not free it. + */ + + nxmutex_lock(&g_genl_lock); + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i]) + { + snapshot[nfam++] = g_genl_families[i]; + } + } + + nxmutex_unlock(&g_genl_lock); + + for (i = 0; i < nfam; i++) + { + send_family_info(conn, req, snapshot[i], true); + } + + /* Send NLMSG_DONE terminator */ + + done_resp = kmm_zalloc(sizeof(*done_resp)); + if (done_resp == NULL) + { + return -ENOMEM; + } + + done_hdr = &done_resp->msg; + done_hdr->nlmsg_len = NLMSG_LENGTH(0); + done_hdr->nlmsg_type = NLMSG_DONE; + done_hdr->nlmsg_flags = NLM_F_MULTI; + done_hdr->nlmsg_seq = req->nlmsg_seq; + done_hdr->nlmsg_pid = req->nlmsg_pid; + + netlink_add_response(conn, done_resp); + return req->nlmsg_len; + } + + /* Single family lookup by name */ + + if (family_name) + { + /* Check built-in nlctrl */ + + if (strcmp(family_name, "nlctrl") == 0) + { + struct genl_family ctrl_fam; + + memset(&ctrl_fam, 0, sizeof(ctrl_fam)); + ctrl_fam.id = GENL_ID_CTRL; + strlcpy(ctrl_fam.name, "nlctrl", GENL_NAMSIZ); + ctrl_fam.version = 1; + ctrl_fam.maxattr = CTRL_ATTR_MAX; + send_family_info(conn, req, &ctrl_fam, false); + + if (req->nlmsg_flags & NLM_F_ACK) + { + send_ack_response(conn, req); + } + + return req->nlmsg_len; + } + + /* Search registered families */ + + fam = genl_family_find_byname(family_name); + if (fam) + { + ret = send_family_info(conn, req, fam, false); + if (ret < 0) + { + return ret; + } + + if (req->nlmsg_flags & NLM_F_ACK) + { + send_ack_response(conn, req); + } + + return req->nlmsg_len; + } + } + + nerr("netlink_ctrl_getfamily: family '%s' not found\n", + family_name ? family_name : "(null)"); + return -ENOENT; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: genl_register_family + * + * Description: + * Register a generic netlink family with the netlink subsystem. + * Allocates a dynamic family ID and multicast group IDs if needed. + * + * Input Parameters: + * family - Pointer to the generic netlink family structure to register. + * + * Returned Value: + * Zero (OK) is returned on success. + * -EEXIST is returned if a family with the same name already exists. + * -ENOMEM is returned if the maximum number of families is reached. + * + ****************************************************************************/ + +int genl_register_family(FAR struct genl_family *family) +{ + int ret = 0; + int i; + + DEBUGASSERT(family != NULL && family->name[0] != '\0'); + + nxmutex_lock(&g_genl_lock); + + /* Check for duplicate */ + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i] && + strcmp(g_genl_families[i]->name, family->name) == 0) + { + ret = -EEXIST; + goto out; + } + } + + if (g_genl_family_count >= GENL_MAX_FAMILIES) + { + ret = -ENOMEM; + goto out; + } + + /* The ids only ever increase, so refuse the registration once either + * space is used up rather than hand out an id that cannot be addressed. + * A multicast group lives in the 32-bit mask of a connection, so group + * 32 is the last one that NETLINK_ADD_MEMBERSHIP can subscribe to and + * netlink_add_broadcast() can deliver to. + */ + + if (g_genl_next_id > GENL_MAX_ID) + { + ret = -ENOSPC; + goto out; + } + + if (family->mcgrps && family->n_mcgrps > 0 && + g_genl_next_mcgrp + family->n_mcgrps - 1 > 32) + { + ret = -ENOSPC; + goto out; + } + + /* Allocate a dynamic family ID */ + + family->id = g_genl_next_id++; + + /* Allocate multicast group IDs */ + + if (family->mcgrps && family->n_mcgrps > 0) + { + family->mcgrp_offset = g_genl_next_mcgrp; + g_genl_next_mcgrp += family->n_mcgrps; + } + else + { + family->mcgrp_offset = 0; + } + + /* Store in registry */ + + g_genl_families[g_genl_family_count++] = family; + + ninfo("genl: registered family '%s' id=%d mcgrp_offset=%d\n", + family->name, family->id, family->mcgrp_offset); + +out: + nxmutex_unlock(&g_genl_lock); + return ret; +} + +/**************************************************************************** + * Name: genl_unregister_family + * + * Description: + * Unregister a generic netlink family from the netlink subsystem. + * + * Input Parameters: + * family - Pointer to the generic netlink family structure to unregister. + * + * Returned Value: + * Zero (OK) is returned on success. + * -ENOENT is returned if the family was not found in the registry. + * + ****************************************************************************/ + +int genl_unregister_family(FAR const struct genl_family *family) +{ + int ret = -ENOENT; + int i; + + DEBUGASSERT(family != NULL); + + nxmutex_lock(&g_genl_lock); + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i] == family) + { + /* Shift remaining entries down */ + + g_genl_family_count--; + g_genl_families[i] = g_genl_families[g_genl_family_count]; + g_genl_families[g_genl_family_count] = NULL; + ret = 0; + break; + } + } + + nxmutex_unlock(&g_genl_lock); + return ret; +} + +/**************************************************************************** + * Name: genl_family_find_byname + * + * Description: + * Find a registered generic netlink family by name. + * + * Input Parameters: + * name - The name of the family to find. + * + * Returned Value: + * Pointer to the generic netlink family structure if found. + * NULL is returned if the family was not found. + * + ****************************************************************************/ + +FAR const struct genl_family *genl_family_find_byname(FAR const char *name) +{ + FAR const struct genl_family *found = NULL; + int i; + + DEBUGASSERT(name != NULL); + + nxmutex_lock(&g_genl_lock); + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i] && + strcmp(g_genl_families[i]->name, name) == 0) + { + found = g_genl_families[i]; + break; + } + } + + nxmutex_unlock(&g_genl_lock); + return found; +} + +/**************************************************************************** + * Name: genl_family_find_byid + * + * Description: + * Find a registered generic netlink family by ID. + * + * Input Parameters: + * id - The family ID to search for. + * + * Returned Value: + * Pointer to the generic netlink family structure if found. + * NULL is returned if the family was not found. + * + ****************************************************************************/ + +FAR const struct genl_family *genl_family_find_byid(uint16_t id) +{ + FAR const struct genl_family *found = NULL; + int i; + + nxmutex_lock(&g_genl_lock); + + for (i = 0; i < g_genl_family_count; i++) + { + if (g_genl_families[i] && g_genl_families[i]->id == id) + { + found = g_genl_families[i]; + break; + } + } + + nxmutex_unlock(&g_genl_lock); + return found; +} + +/**************************************************************************** + * Name: netlink_generic_sendto + * + * Description: + * Perform the sendto() operation for the NETLINK_GENERIC protocol. + * Dispatches to the controller (GENL_ID_CTRL) or to a registered + * family's cmd_handler based on nlmsg_type. + * + * Input Parameters: + * conn - The netlink connection. + * msg - The netlink message to send. + * len - Length of the message. + * flags - Send flags. + * to - Destination address (sockaddr_nl). + * tolen - Length of the destination address. + * + * Returned Value: + * The length of the request message on success. + * A negated error value is returned if an unexpected error occurred. + * + ****************************************************************************/ + +ssize_t netlink_generic_sendto(FAR struct netlink_conn_s *conn, + FAR const struct nlmsghdr *msg, + size_t len, int flags, + FAR const struct sockaddr_nl *to, + socklen_t tolen) +{ + FAR const struct genl_family *family; + + DEBUGASSERT(conn != NULL && msg != NULL); + + /* netlink_sendmsg() only guarantees a complete nlmsghdr. Every generic + * netlink message carries a genlmsghdr after it, which the dispatch below + * reads, so refuse a request that is too short for one. + */ + + if (msg->nlmsg_len < NLMSG_LENGTH(GENL_HDRLEN)) + { + nerr("genl: message too short: %u\n", (unsigned int)msg->nlmsg_len); + return -EINVAL; + } + + /* Handle controller messages (CTRL_CMD_GETFAMILY etc.) */ + + if (msg->nlmsg_type == GENL_ID_CTRL) + { + FAR struct genlmsghdr *ghdr; + + ghdr = (FAR struct genlmsghdr *)NLMSG_DATA(msg); + if (ghdr->cmd == CTRL_CMD_GETFAMILY) + { + return netlink_ctrl_getfamily(conn, msg); + } + + nerr("genl: unsupported ctrl cmd %d\n", ghdr->cmd); + return -EOPNOTSUPP; + } + + /* Dispatch to registered family by nlmsg_type (== family ID) */ + + family = genl_family_find_byid(msg->nlmsg_type); + if (family && family->cmd_handler) + { + return family->cmd_handler(conn, msg); + } + + nerr("genl: no family for nlmsg_type=%d\n", msg->nlmsg_type); + return -ENOENT; +} diff --git a/net/netlink/netlink_netfilter.c b/net/netlink/netlink_netfilter.c index 0c314678850..31ea86489b9 100644 --- a/net/netlink/netlink_netfilter.c +++ b/net/netlink/netlink_netfilter.c @@ -32,7 +32,7 @@ #include <stddef.h> #include <stdint.h> -#include <netpacket/netlink.h> +#include <netpacket/nfnetlink.h> #include <nuttx/kmalloc.h> #include <nuttx/net/net.h> @@ -561,6 +561,7 @@ static int netlink_list_conntrack(NETLINK_HANDLE handle, { struct nfnl_info_s info; uint8_t type = NFNL_MSG_TYPE(req->hdr.nlmsg_type); + if (type != IPCTNL_MSG_CT_GET) { return -ENOSYS; @@ -698,7 +699,7 @@ void netlink_conntrack_notify(uint8_t type, uint8_t domain, if (resp != NULL) { - netlink_add_broadcast(group, resp); + netlink_add_broadcast(NETLINK_NETFILTER, group, resp); } } diff --git a/net/netlink/netlink_route.c b/net/netlink/netlink_route.c index 46dab7522d0..7cf7a934dc4 100644 --- a/net/netlink/netlink_route.c +++ b/net/netlink/netlink_route.c @@ -35,7 +35,7 @@ #include <arpa/inet.h> #include <net/route.h> -#include <netpacket/netlink.h> +#include <netpacket/rtnetlink.h> #include <netinet/if_ether.h> #include <nuttx/kmalloc.h> @@ -2222,7 +2222,7 @@ void netlink_device_notify(FAR struct net_driver_s *dev) NULL); if (resp != NULL) { - netlink_add_broadcast(RTNLGRP_LINK, resp); + netlink_add_broadcast(NETLINK_ROUTE, RTNLGRP_LINK, resp); netlink_add_terminator(NULL, NULL, RTNLGRP_LINK); } } @@ -2271,7 +2271,7 @@ void netlink_device_notify_ipaddr(FAR struct net_driver_s *dev, return; } - netlink_add_broadcast(group, resp); + netlink_add_broadcast(NETLINK_ROUTE, group, resp); netlink_add_terminator(NULL, NULL, group); } } @@ -2324,7 +2324,7 @@ void netlink_route_notify(FAR const void *route, int type, int domain) if (resp != NULL) { - netlink_add_broadcast(group, resp); + netlink_add_broadcast(NETLINK_ROUTE, group, resp); netlink_add_terminator(NULL, NULL, group); } } @@ -2349,7 +2349,7 @@ void netlink_neigh_notify(FAR const void *neigh, int type, int domain) return; } - netlink_add_broadcast(RTNLGRP_NEIGH, resp); + netlink_add_broadcast(NETLINK_ROUTE, RTNLGRP_NEIGH, resp); netlink_add_terminator(NULL, NULL, RTNLGRP_NEIGH); } #endif @@ -2374,7 +2374,7 @@ void netlink_ipv6_prefix_notify(FAR struct net_driver_s *dev, int type, return; } - netlink_add_broadcast(RTNLGRP_IPV6_PREFIX, resp); + netlink_add_broadcast(NETLINK_ROUTE, RTNLGRP_IPV6_PREFIX, resp); netlink_add_terminator(NULL, NULL, RTNLGRP_IPV6_PREFIX); } #endif diff --git a/net/netlink/netlink_sockif.c b/net/netlink/netlink_sockif.c index a02bccb515f..90346e7b52e 100644 --- a/net/netlink/netlink_sockif.c +++ b/net/netlink/netlink_sockif.c @@ -74,6 +74,11 @@ static ssize_t netlink_sendmsg(FAR struct socket *psock, static ssize_t netlink_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg, int flags); static int netlink_close(FAR struct socket *psock); +#ifdef CONFIG_NET_SOCKOPTS +static int netlink_setsockopt(FAR struct socket *psock, int level, + int option, FAR const void *value, + socklen_t value_len); +#endif /**************************************************************************** * Public Data @@ -93,7 +98,14 @@ const struct sock_intf_s g_netlink_sockif = netlink_poll, /* si_poll */ netlink_sendmsg, /* si_sendmsg */ netlink_recvmsg, /* si_recvmsg */ - netlink_close /* si_close */ + netlink_close, /* si_close */ + NULL, /* si_ioctl */ + NULL, /* si_socketpair */ + NULL, /* si_shutdown */ +#ifdef CONFIG_NET_SOCKOPTS + NULL, /* si_getsockopt */ + netlink_setsockopt /* si_setsockopt */ +#endif }; /**************************************************************************** @@ -135,6 +147,11 @@ static int netlink_setup(FAR struct socket *psock) break; #endif +#ifdef CONFIG_NETLINK_GENERIC + case NETLINK_GENERIC: + break; +#endif + #ifdef CONFIG_NETLINK_NETFILTER case NETLINK_NETFILTER: break; @@ -167,6 +184,7 @@ static int netlink_setup(FAR struct socket *psock) */ conn->crefs = 1; + conn->protocol = proto; /* Attach the connection instance to the socket */ @@ -645,7 +663,10 @@ static ssize_t netlink_sendmsg(FAR struct socket *psock, tolen = sizeof(struct sockaddr_nl); } - DEBUGASSERT(tolen >= sizeof(struct sockaddr_nl)); + if (tolen < sizeof(struct sockaddr_nl)) + { + return -EINVAL; + } /* Netlink message must contain at least a complete nlmsghdr */ @@ -674,6 +695,15 @@ static ssize_t netlink_sendmsg(FAR struct socket *psock, break; #endif +#ifdef CONFIG_NETLINK_GENERIC + case NETLINK_GENERIC: + ret = netlink_generic_sendto(conn, nlmsg, + msg->msg_iov->iov_len, flags, + (FAR const struct sockaddr_nl *)to, + tolen); + break; +#endif + #ifdef CONFIG_NETLINK_NETFILTER case NETLINK_NETFILTER: ret = netlink_netfilter_sendto(conn, nlmsg, @@ -868,4 +898,85 @@ static int netlink_close(FAR struct socket *psock) return OK; } +/**************************************************************************** + * Name: netlink_setsockopt + * + * Description: + * netlink_setsockopt() sets the NetLink-specific option specified by the + * 'option' argument to the value pointed to by the 'value' argument for + * the socket specified by the 'psock' argument. + * + * See <sys/socket.h> a complete list of values for the 'option' argument. + * + * Input Parameters: + * psock Socket structure of socket to operate on + * level Protocol level to set the option + * option identifies the option to set + * value Points to the argument value + * value_len The length of the argument value + * + * Returned Value: + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error. See psock_setsockopt() for + * the list of possible error values. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_SOCKOPTS +static int netlink_setsockopt(FAR struct socket *psock, int level, + int option, FAR const void *value, + socklen_t value_len) +{ + FAR struct netlink_conn_s *conn; + int ret = OK; + + DEBUGASSERT(psock != NULL && psock->s_conn != NULL && value != NULL); + conn = (FAR struct netlink_conn_s *)psock->s_conn; + + if (level == SOL_NETLINK) + { + switch (option) + { + case NETLINK_ADD_MEMBERSHIP: + case NETLINK_DROP_MEMBERSHIP: + { + int group; + + if (value_len < sizeof(int)) + { + return -EINVAL; + } + + group = *(FAR const int *)value; + + if (group < 1 || group > 32) + { + return -EINVAL; + } + + if (option == NETLINK_ADD_MEMBERSHIP) + { + conn->groups |= (1u << (group - 1)); + } + else + { + conn->groups &= ~(1u << (group - 1)); + } + } + break; + + default: + ret = -ENOPROTOOPT; + break; + } + } + else + { + ret = -ENOPROTOOPT; + } + + return ret; +} +#endif + #endif /* CONFIG_NET_NETLINK */
