Initial import
[sliver-openvswitch.git] / include / netlink.h
1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
2  *
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:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
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
19  * IN THE SOFTWARE.
20  */
21
22 #ifndef NETLINK_H
23 #define NETLINK_H 1
24
25 /* Netlink interface.
26  *
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".
30  *
31  * Netlink is not suitable for use in physical networks of heterogeneous
32  * machines because host byte order is used throughout. */
33
34 #include <stdbool.h>
35 #include <stdint.h>
36 #include <sys/socket.h>
37 #include <linux/netlink.h>
38 #include <linux/genetlink.h>
39 #include <linux/version.h>
40
41 #ifndef NLA_ALIGNTO
42 struct nlattr
43 {
44     __u16           nla_len;
45     __u16           nla_type;
46 };
47
48 #define NLA_ALIGNTO     4
49 #define NLA_ALIGN(len)      (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
50 #define NLA_HDRLEN      ((int) NLA_ALIGN(sizeof(struct nlattr)))
51
52 #endif 
53
54 struct buffer;
55 struct nl_sock;
56
57 /* Netlink sockets. */
58
59 int nl_sock_create(int protocol, int multicast_group,
60                    size_t so_sndbuf, size_t so_rcvbuf,
61                    struct nl_sock **);
62 void nl_sock_destroy(struct nl_sock *);
63
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,
66                   bool wait);
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);
70
71 int nl_sock_fd(const struct nl_sock *);
72 \f
73 /* Netlink messages. */
74
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);
80
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);
90
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 *);
101 \f
102 /* Netlink attribute types. */
103 enum nl_attr_type
104 {
105     NL_A_NO_ATTR = 0,
106     NL_A_UNSPEC,
107     NL_A_U8,
108     NL_A_U16,
109     NL_A_U32,
110     NL_A_U64,
111     NL_A_STRING,
112     NL_A_FLAG,
113     NL_A_NESTED,
114     N_NL_ATTR_TYPES
115 };
116
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 *);
127
128 /* Netlink attribute policy.
129  *
130  * Specifies how to parse a single attribute from a Netlink message payload.
131  *
132  * See Nl_policy for example.
133  */
134 struct nl_policy
135 {
136     enum nl_attr_type type;
137     size_t min_len, max_len;
138     bool optional;
139 };
140
141 bool nl_policy_parse(const struct buffer *, const struct nl_policy[],
142                      struct nlattr *[], size_t n_attrs);
143 \f
144 /* Miscellaneous. */
145
146 int nl_lookup_genl_family(const char *name, int *number);
147
148 #endif /* netlink.h */