1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 * Netlink is a datagram-based network protocol primarily for communication
28 * between user processes and the kernel, and mainly on Linux. Netlink is
29 * specified in RFC 3549, "Linux Netlink as an IP Services Protocol".
31 * Netlink is not suitable for use in physical networks of heterogeneous
32 * machines because host byte order is used throughout. */
36 #include <sys/socket.h>
37 #include <linux/netlink.h>
38 #include <linux/genetlink.h>
39 #include <linux/version.h>
49 #define NLA_ALIGN(len) (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
50 #define NLA_HDRLEN ((int) NLA_ALIGN(sizeof(struct nlattr)))
57 /* Netlink sockets. */
59 int nl_sock_create(int protocol, int multicast_group,
60 size_t so_sndbuf, size_t so_rcvbuf,
62 void nl_sock_destroy(struct nl_sock *);
64 int nl_sock_send(struct nl_sock *, const struct buffer *, bool wait);
65 int nl_sock_sendv(struct nl_sock *sock, const struct iovec iov[], size_t n_iov,
67 int nl_sock_recv(struct nl_sock *, struct buffer **, bool wait);
68 int nl_sock_transact(struct nl_sock *, const struct buffer *request,
69 struct buffer **reply);
71 int nl_sock_fd(const struct nl_sock *);
73 /* Netlink messages. */
75 /* Accessing headers and data. */
76 struct nlmsghdr *nl_msg_nlmsghdr(const struct buffer *);
77 struct genlmsghdr *nl_msg_genlmsghdr(const struct buffer *);
78 bool nl_msg_nlmsgerr(const struct buffer *, int *error);
79 void nl_msg_reserve(struct buffer *, size_t);
81 /* Appending headers and raw data. */
82 void nl_msg_put_nlmsghdr(struct buffer *, struct nl_sock *,
83 size_t expected_payload,
84 uint32_t type, uint32_t flags);
85 void nl_msg_put_genlmsghdr(struct buffer *, struct nl_sock *,
86 size_t expected_payload, int family, uint32_t flags,
87 uint8_t cmd, uint8_t version);
88 void nl_msg_put(struct buffer *, const void *, size_t);
89 void *nl_msg_put_uninit(struct buffer *, size_t);
91 /* Appending attributes. */
92 void *nl_msg_put_unspec_uninit(struct buffer *, uint16_t type, size_t);
93 void nl_msg_put_unspec(struct buffer *, uint16_t type, const void *, size_t);
94 void nl_msg_put_flag(struct buffer *, uint16_t type);
95 void nl_msg_put_u8(struct buffer *, uint16_t type, uint8_t value);
96 void nl_msg_put_u16(struct buffer *, uint16_t type, uint16_t value);
97 void nl_msg_put_u32(struct buffer *, uint16_t type, uint32_t value);
98 void nl_msg_put_u64(struct buffer *, uint16_t type, uint64_t value);
99 void nl_msg_put_string(struct buffer *, uint16_t type, const char *value);
100 void nl_msg_put_nested(struct buffer *, uint16_t type, struct buffer *);
102 /* Netlink attribute types. */
117 /* Netlink attribute parsing. */
118 const void* nl_attr_get(const struct nlattr *);
119 size_t nl_attr_get_size(const struct nlattr *);
120 const void* nl_attr_get_unspec(const struct nlattr *, size_t size);
121 bool nl_attr_get_flag(const struct nlattr *);
122 uint8_t nl_attr_get_u8(const struct nlattr *);
123 uint16_t nl_attr_get_u16(const struct nlattr *);
124 uint32_t nl_attr_get_u32(const struct nlattr *);
125 uint64_t nl_attr_get_u64(const struct nlattr *);
126 const char *nl_attr_get_string(const struct nlattr *);
128 /* Netlink attribute policy.
130 * Specifies how to parse a single attribute from a Netlink message payload.
132 * See Nl_policy for example.
136 enum nl_attr_type type;
137 size_t min_len, max_len;
141 bool nl_policy_parse(const struct buffer *, const struct nl_policy[],
142 struct nlattr *[], size_t n_attrs);
146 int nl_lookup_genl_family(const char *name, int *number);
148 #endif /* netlink.h */