/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2021 Ng Peng Nam Sean4* Copyright (c) 2022 Alexander V. Chernikov <[email protected]>5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*27* Copyright (C) The Internet Society (2003). All Rights Reserved.28*29* This document and translations of it may be copied and furnished to30* others, and derivative works that comment on or otherwise explain it31* or assist in its implementation may be prepared, copied, published32* and distributed, in whole or in part, without restriction of any33* kind, provided that the above copyright notice and this paragraph are34* included on all such copies and derivative works. However, this35* document itself may not be modified in any way, such as by removing36* the copyright notice or references to the Internet Society or other37* Internet organizations, except as needed for the purpose of38* developing Internet standards in which case the procedures for39* copyrights defined in the Internet Standards process must be40* followed, or as required to translate it into languages other than41* English.42*43* The limited permissions granted above are perpetual and will not be44* revoked by the Internet Society or its successors or assignees.45*46* This document and the information contained herein is provided on an47* "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING48* TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING49* BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION50* HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF51* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.5253*/5455/*56* This file contains structures and constants for RFC 3549 (Netlink)57* protocol. Some values have been taken from Linux implementation.58*/5960#ifndef _NETLINK_NETLINK_H_61#define _NETLINK_NETLINK_H_6263#include <sys/param.h>64#include <sys/socket.h>6566struct sockaddr_nl {67uint8_t nl_len; /* sizeof(sockaddr_nl) */68sa_family_t nl_family; /* netlink family */69uint16_t nl_pad; /* reserved, set to 0 */70uint32_t nl_pid; /* desired port ID, 0 for auto-select */71uint32_t nl_groups; /* multicast groups mask to bind to */72};7374#define SOL_NETLINK 2707576/* Netlink socket options */77#define NETLINK_ADD_MEMBERSHIP 1 /* Subscribe for the specified group notifications */78#define NETLINK_DROP_MEMBERSHIP 2 /* Unsubscribe from the specified group */79#define NETLINK_PKTINFO 3 /* XXX: not supported */80#define NETLINK_BROADCAST_ERROR 4 /* XXX: not supported */81#define NETLINK_NO_ENOBUFS 5 /* XXX: not supported */82#define NETLINK_RX_RING 6 /* XXX: not supported */83#define NETLINK_TX_RING 7 /* XXX: not supported */84#define NETLINK_LISTEN_ALL_NSID 8 /* XXX: not supported */8586#define NETLINK_LIST_MEMBERSHIPS 987#define NETLINK_CAP_ACK 10 /* Send only original message header in the reply */88#define NETLINK_EXT_ACK 11 /* Ack support for receiving additional TLVs in ack */89#define NETLINK_GET_STRICT_CHK 12 /* Strict header checking */9091#define NETLINK_MSG_INFO 257 /* (FreeBSD-specific) Receive message originator data in cmsg */9293/*94* RFC 3549, 2.3.2 Netlink Message Header95*/96struct nlmsghdr {97uint32_t nlmsg_len; /* Length of message including header */98uint16_t nlmsg_type; /* Message type identifier */99uint16_t nlmsg_flags; /* Flags (NLM_F_) */100uint32_t nlmsg_seq; /* Sequence number */101uint32_t nlmsg_pid; /* Sending process port ID */102};103104/*105* RFC 3549, 2.3.2 standard flag bits (nlmsg_flags)106*/107#define NLM_F_REQUEST 0x01 /* Indicateds request to kernel */108#define NLM_F_MULTI 0x02 /* Message is part of a group terminated by NLMSG_DONE msg */109#define NLM_F_ACK 0x04 /* Reply with ack message containing resulting error code */110#define NLM_F_ECHO 0x08 /* (not supported) Echo this request back */111#define NLM_F_DUMP_INTR 0x10 /* Dump was inconsistent due to sequence change */112#define NLM_F_DUMP_FILTERED 0x20 /* Dump was filtered as requested */113114/*115* RFC 3549, 2.3.2 Additional flag bits for GET requests116*/117#define NLM_F_ROOT 0x100 /* Return the complete table */118#define NLM_F_MATCH 0x200 /* Return all entries matching criteria */119#define NLM_F_ATOMIC 0x400 /* Return an atomic snapshot (ignored) */120#define NLM_F_DUMP (NLM_F_ROOT | NLM_F_MATCH)121122/*123* RFC 3549, 2.3.2 Additional flag bits for NEW requests124*/125#define NLM_F_REPLACE 0x100 /* Replace existing matching config object */126#define NLM_F_EXCL 0x200 /* Don't replace the object if exists */127#define NLM_F_CREATE 0x400 /* Create if it does not exist */128#define NLM_F_APPEND 0x800 /* Add to end of list */129130/* Modifiers to DELETE requests */131#define NLM_F_NONREC 0x100 /* Do not delete recursively */132133/* Flags for ACK message */134#define NLM_F_CAPPED 0x100 /* request was capped */135#define NLM_F_ACK_TLVS 0x200 /* extended ACK TVLs were included */136137/*138* RFC 3549, 2.3.2 standard message types (nlmsg_type).139*/140#define NLMSG_NOOP 0x1 /* Message is ignored. */141#define NLMSG_ERROR 0x2 /* reply error code reporting */142#define NLMSG_DONE 0x3 /* Message terminates a multipart message. */143#define NLMSG_OVERRUN 0x4 /* overrun detected, data is lost */144145#define NLMSG_MIN_TYPE 0x10 /* < 0x10: reserved control messages */146147/*148* Defition of numbers assigned to the netlink subsystems.149*/150#define NETLINK_ROUTE 0 /* Routing/device hook */151#define NETLINK_UNUSED 1 /* not supported */152#define NETLINK_USERSOCK 2 /* not supported */153#define NETLINK_FIREWALL 3 /* not supported */154#define NETLINK_SOCK_DIAG 4 /* not supported */155#define NETLINK_NFLOG 5 /* not supported */156#define NETLINK_XFRM 6 /* (not supported) PF_SETKEY */157#define NETLINK_SELINUX 7 /* not supported */158#define NETLINK_ISCSI 8 /* not supported */159#define NETLINK_AUDIT 9 /* not supported */160#define NETLINK_FIB_LOOKUP 10 /* not supported */161#define NETLINK_CONNECTOR 11 /* not supported */162#define NETLINK_NETFILTER 12 /* not supported */163#define NETLINK_IP6_FW 13 /* not supported */164#define NETLINK_DNRTMSG 14 /* not supported */165#define NETLINK_KOBJECT_UEVENT 15 /* not supported */166#define NETLINK_GENERIC 16 /* Generic netlink (dynamic families) */167168/*169* RFC 3549, 2.3.2.2 The ACK Netlink Message170*/171struct nlmsgerr {172int error;173struct nlmsghdr msg;174};175176enum nlmsgerr_attrs {177NLMSGERR_ATTR_UNUSED,178NLMSGERR_ATTR_MSG = 1, /* string, error message */179NLMSGERR_ATTR_OFFS = 2, /* u32, offset of the invalid attr from nl header */180NLMSGERR_ATTR_COOKIE = 3, /* binary, data to pass to userland */181NLMSGERR_ATTR_POLICY = 4, /* not supported */182__NLMSGERR_ATTR_MAX,183NLMSGERR_ATTR_MAX = __NLMSGERR_ATTR_MAX - 1184};185186/* FreeBSD-specific debugging info */187188enum nlmsginfo_attrs {189NLMSGINFO_ATTR_UNUSED,190NLMSGINFO_ATTR_PROCESS_ID = 1, /* u32, source process PID */191NLMSGINFO_ATTR_PORT_ID = 2, /* u32, source socket nl_pid */192NLMSGINFO_ATTR_SEQ_ID = 3, /* u32, source message seq_id */193};194195196#define NL_ITEM_ALIGN_SIZE sizeof(uint32_t)197#define NL_ITEM_ALIGN(_len) __align_up(_len, NL_ITEM_ALIGN_SIZE)198#define NL_ITEM_DATA(_ptr, _off) ((void *)((char *)(_ptr) + _off))199#define NL_ITEM_DATA_CONST(_ptr, _off) ((const void *)((const char *)(_ptr) + _off))200201#define NL_ITEM_OK(_ptr, _len, _hlen, _LEN_M) \202((_len) >= _hlen && _LEN_M(_ptr) >= _hlen && _LEN_M(_ptr) <= (_len))203#define NL_ITEM_NEXT(_ptr, _LEN_M) ((__typeof(_ptr))((char *)(_ptr) + _LEN_M(_ptr)))204#define NL_ITEM_ITER(_ptr, _len, _LEN_MACRO) \205((_len) -= _LEN_MACRO(_ptr), NL_ITEM_NEXT(_ptr, _LEN_MACRO))206207/* part of netlink(3) API */208#define NLMSG_ALIGNTO NL_ITEM_ALIGN_SIZE209#define NLMSG_ALIGN(_len) NL_ITEM_ALIGN(_len)210211#ifndef _KERNEL212/* part of netlink(3) API */213#define NLMSG_HDRLEN (sizeof(struct nlmsghdr))214#define NLMSG_LENGTH(_len) ((_len) + NLMSG_HDRLEN)215#define NLMSG_SPACE(_len) NLMSG_ALIGN(NLMSG_LENGTH(_len))216#define NLMSG_DATA(_hdr) NL_ITEM_DATA(_hdr, NLMSG_HDRLEN)217#define _NLMSG_LEN(_hdr) ((_hdr)->nlmsg_len)218#define _NLMSG_ALIGNED_LEN(_hdr) NLMSG_ALIGN(_NLMSG_LEN(_hdr))219#define NLMSG_OK(_hdr, _len) NL_ITEM_OK(_hdr, _len, NLMSG_HDRLEN, _NLMSG_LEN)220#define NLMSG_PAYLOAD(_hdr,_len) (_NLMSG_LEN(_hdr) - NLMSG_SPACE((_len)))221#define NLMSG_NEXT(_hdr, _len) NL_ITEM_ITER(_hdr, _len, _NLMSG_ALIGNED_LEN)222223#else224#define NLMSG_HDRLEN (NLMSG_ALIGN(sizeof(struct nlmsghdr)))225#endif226227/*228* Base netlink attribute TLV header.229*/230struct nlattr {231uint16_t nla_len; /* Total attribute length */232uint16_t nla_type; /* Attribute type */233};234235/*236*237* nl_type field enconding:238*239* 0 1240* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6241* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+242* |N|O| Attribute type |243* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+244* N - attribute contains other attributes (mostly unused)245* O - encoded in network byte order (mostly unused)246* Note: N & O are mutually exclusive247*248* Note: attribute type value scope normally is either parent attribute249* or the message/message group.250*/251252#define NLA_F_NESTED (1 << 15)253#define NLA_F_NET_BYTEORDER (1 << 14)254#define NLA_TYPE_MASK ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER)255256#ifndef _KERNEL257#define NLA_ALIGNTO NL_ITEM_ALIGN_SIZE258#define NLA_ALIGN(_len) NL_ITEM_ALIGN(_len)259#define NLA_HDRLEN ((int)sizeof(struct nlattr))260#endif261262#endif263264265