1 /* SPDX-License-Identifier: LGPL-2.1-only */ 2 /* 3 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch> 4 */ 5 6 #ifndef NETLINK_MSG_H_ 7 #define NETLINK_MSG_H_ 8 9 #include <netlink/netlink.h> 10 #include <netlink/object.h> 11 #include <netlink/attr.h> 12 13 #ifdef __cplusplus 14 extern "C" { 15 #endif 16 17 struct nlmsghdr; 18 19 #define NL_DONTPAD 0 20 21 /** 22 * @ingroup msg 23 * @brief 24 * Will cause the netlink port to be set to the port assigned to 25 * the netlink icoket ust before sending the message off. 26 * 27 * @note Requires the use of nl_send_auto()! 28 */ 29 #define NL_AUTO_PORT 0 30 #define NL_AUTO_PID NL_AUTO_PORT 31 32 /** 33 * @ingroup msg 34 * @brief 35 * May be used to refer to a sequence number which should be 36 * automatically set just before sending the message off. 37 * 38 * @note Requires the use of nl_send_auto()! 39 */ 40 #define NL_AUTO_SEQ 0 41 42 struct nl_msg; 43 struct nl_tree; 44 struct ucred; 45 46 extern int nlmsg_size(int); 47 extern int nlmsg_total_size(int); 48 extern int nlmsg_padlen(int); 49 50 extern void * nlmsg_data(const struct nlmsghdr *); 51 extern int nlmsg_datalen(const struct nlmsghdr *); 52 extern void * nlmsg_tail(const struct nlmsghdr *); 53 54 /* attribute access */ 55 extern struct nlattr * nlmsg_attrdata(const struct nlmsghdr *, int); 56 extern int nlmsg_attrlen(const struct nlmsghdr *, int); 57 58 /* message parsing */ 59 extern int nlmsg_valid_hdr(const struct nlmsghdr *, int); 60 extern int nlmsg_ok(const struct nlmsghdr *, int); 61 extern struct nlmsghdr * nlmsg_next(struct nlmsghdr *, int *); 62 extern int nlmsg_parse(struct nlmsghdr *, int, struct nlattr **, 63 int, const struct nla_policy *); 64 extern struct nlattr * nlmsg_find_attr(struct nlmsghdr *, int, int); 65 extern int nlmsg_validate(struct nlmsghdr *, int, int, 66 const struct nla_policy *); 67 68 extern struct nl_msg * nlmsg_alloc(void); 69 extern struct nl_msg * nlmsg_alloc_size(size_t); 70 extern struct nl_msg * nlmsg_alloc_simple(int, int); 71 extern void nlmsg_set_default_size(size_t); 72 extern struct nl_msg * nlmsg_inherit(struct nlmsghdr *); 73 extern struct nl_msg * nlmsg_convert(struct nlmsghdr *); 74 extern void * nlmsg_reserve(struct nl_msg *, size_t, int); 75 extern int nlmsg_append(struct nl_msg *, void *, size_t, int); 76 extern int nlmsg_expand(struct nl_msg *, size_t); 77 78 extern struct nlmsghdr * nlmsg_put(struct nl_msg *, uint32_t, uint32_t, 79 int, int, int); 80 extern struct nlmsghdr * nlmsg_hdr(struct nl_msg *); 81 extern void nlmsg_get(struct nl_msg *); 82 extern void nlmsg_free(struct nl_msg *); 83 84 /* attribute modification */ 85 extern void nlmsg_set_proto(struct nl_msg *, int); 86 extern int nlmsg_get_proto(struct nl_msg *); 87 extern size_t nlmsg_get_max_size(struct nl_msg *); 88 extern void nlmsg_set_src(struct nl_msg *, struct sockaddr_nl *); 89 extern struct sockaddr_nl *nlmsg_get_src(struct nl_msg *); 90 extern void nlmsg_set_dst(struct nl_msg *, struct sockaddr_nl *); 91 extern struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *); 92 extern void nlmsg_set_creds(struct nl_msg *, struct ucred *); 93 extern struct ucred * nlmsg_get_creds(struct nl_msg *); 94 95 extern char * nl_nlmsgtype2str(int, char *, size_t); 96 extern int nl_str2nlmsgtype(const char *); 97 98 extern char * nl_nlmsg_flags2str(int, char *, size_t); 99 100 extern int nl_msg_parse(struct nl_msg *, 101 void (*cb)(struct nl_object *, void *), 102 void *); 103 104 extern void nl_msg_dump(struct nl_msg *, FILE *); 105 106 /** 107 * @name Iterators 108 * @{ 109 */ 110 111 /** 112 * @ingroup msg 113 * Iterate over a stream of attributes in a message 114 * @arg pos loop counter, set to current attribute 115 * @arg nlh netlink message header 116 * @arg hdrlen length of family header 117 * @arg rem initialized to len, holds bytes currently remaining in stream 118 */ 119 #define nlmsg_for_each_attr(pos, nlh, hdrlen, rem) \ 120 nla_for_each_attr(pos, nlmsg_attrdata(nlh, hdrlen), \ 121 nlmsg_attrlen(nlh, hdrlen), rem) 122 123 /** 124 * Iterate over a stream of messages 125 * @arg pos loop counter, set to current message 126 * @arg head head of message stream 127 * @arg len length of message stream 128 */ 129 #define nlmsg_for_each(pos, head, len) \ 130 for (int rem = len, pos = head; \ 131 nlmsg_ok(pos, rem); \ 132 pos = nlmsg_next(pos, &rem)) 133 134 #define nlmsg_for_each_msg(pos, head, len, rem) \ 135 nlmsg_for_each(pos, head, len) 136 137 /** @} */ 138 139 #ifdef __cplusplus 140 } 141 #endif 142 143 #endif 144