Update copyright on all non-GPL files
[openvswitch] / include / netlink.h
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #ifndef NETLINK_H
35 #define NETLINK_H 1
36
37 /* Netlink interface.
38  *
39  * Netlink is a datagram-based network protocol primarily for communication
40  * between user processes and the kernel, and mainly on Linux.  Netlink is
41  * specified in RFC 3549, "Linux Netlink as an IP Services Protocol".
42  *
43  * Netlink is not suitable for use in physical networks of heterogeneous
44  * machines because host byte order is used throughout. */
45
46 #include <stdbool.h>
47 #include <stdint.h>
48 #include <sys/socket.h>
49 #include <linux/netlink.h>
50 #include <linux/genetlink.h>
51 #include <linux/version.h>
52
53 #ifndef NLA_ALIGNTO
54 struct nlattr
55 {
56     __u16           nla_len;
57     __u16           nla_type;
58 };
59
60 #define NLA_ALIGNTO     4
61 #define NLA_ALIGN(len)      (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
62 #define NLA_HDRLEN      ((int) NLA_ALIGN(sizeof(struct nlattr)))
63
64 #endif 
65
66 struct buffer;
67 struct nl_sock;
68
69 /* Netlink sockets. */
70
71 int nl_sock_create(int protocol, int multicast_group,
72                    size_t so_sndbuf, size_t so_rcvbuf,
73                    struct nl_sock **);
74 void nl_sock_destroy(struct nl_sock *);
75
76 int nl_sock_send(struct nl_sock *, const struct buffer *, bool wait);
77 int nl_sock_sendv(struct nl_sock *sock, const struct iovec iov[], size_t n_iov,
78                   bool wait);
79 int nl_sock_recv(struct nl_sock *, struct buffer **, bool wait);
80 int nl_sock_transact(struct nl_sock *, const struct buffer *request,
81                      struct buffer **reply);
82
83 int nl_sock_fd(const struct nl_sock *);
84 \f
85 /* Netlink messages. */
86
87 /* Accessing headers and data. */
88 struct nlmsghdr *nl_msg_nlmsghdr(const struct buffer *);
89 struct genlmsghdr *nl_msg_genlmsghdr(const struct buffer *);
90 bool nl_msg_nlmsgerr(const struct buffer *, int *error);
91 void nl_msg_reserve(struct buffer *, size_t);
92
93 /* Appending headers and raw data. */
94 void nl_msg_put_nlmsghdr(struct buffer *, struct nl_sock *,
95                          size_t expected_payload,
96                          uint32_t type, uint32_t flags);
97 void nl_msg_put_genlmsghdr(struct buffer *, struct nl_sock *,
98                            size_t expected_payload, int family, uint32_t flags,
99                            uint8_t cmd, uint8_t version);
100 void nl_msg_put(struct buffer *, const void *, size_t);
101 void *nl_msg_put_uninit(struct buffer *, size_t);
102
103 /* Appending attributes. */
104 void *nl_msg_put_unspec_uninit(struct buffer *, uint16_t type, size_t);
105 void nl_msg_put_unspec(struct buffer *, uint16_t type, const void *, size_t);
106 void nl_msg_put_flag(struct buffer *, uint16_t type);
107 void nl_msg_put_u8(struct buffer *, uint16_t type, uint8_t value);
108 void nl_msg_put_u16(struct buffer *, uint16_t type, uint16_t value);
109 void nl_msg_put_u32(struct buffer *, uint16_t type, uint32_t value);
110 void nl_msg_put_u64(struct buffer *, uint16_t type, uint64_t value);
111 void nl_msg_put_string(struct buffer *, uint16_t type, const char *value);
112 void nl_msg_put_nested(struct buffer *, uint16_t type, struct buffer *);
113 \f
114 /* Netlink attribute types. */
115 enum nl_attr_type
116 {
117     NL_A_NO_ATTR = 0,
118     NL_A_UNSPEC,
119     NL_A_U8,
120     NL_A_U16,
121     NL_A_U32,
122     NL_A_U64,
123     NL_A_STRING,
124     NL_A_FLAG,
125     NL_A_NESTED,
126     N_NL_ATTR_TYPES
127 };
128
129 /* Netlink attribute parsing. */
130 const void* nl_attr_get(const struct nlattr *);
131 size_t nl_attr_get_size(const struct nlattr *);
132 const void* nl_attr_get_unspec(const struct nlattr *, size_t size);
133 bool nl_attr_get_flag(const struct nlattr *);
134 uint8_t nl_attr_get_u8(const struct nlattr *);
135 uint16_t nl_attr_get_u16(const struct nlattr *);
136 uint32_t nl_attr_get_u32(const struct nlattr *);
137 uint64_t nl_attr_get_u64(const struct nlattr *);
138 const char *nl_attr_get_string(const struct nlattr *);
139
140 /* Netlink attribute policy.
141  *
142  * Specifies how to parse a single attribute from a Netlink message payload.
143  *
144  * See Nl_policy for example.
145  */
146 struct nl_policy
147 {
148     enum nl_attr_type type;
149     size_t min_len, max_len;
150     bool optional;
151 };
152
153 bool nl_policy_parse(const struct buffer *, const struct nl_policy[],
154                      struct nlattr *[], size_t n_attrs);
155 \f
156 /* Miscellaneous. */
157
158 int nl_lookup_genl_family(const char *name, int *number);
159
160 #endif /* netlink.h */