6281fb2c3af2678ec86587a55d71ecad60efbc78
[sliver-openvswitch.git] / lib / netlink-protocol.h
1 /*
2  * Copyright (c) 2008, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef NETLINK_PROTOCOL_H
18 #define NETLINK_PROTOCOL_H 1
19
20 /* Netlink protocol definitions.
21  *
22  * Netlink is a message framing format described in RFC 3549 and used heavily
23  * in Linux to access the network stack.  Open vSwitch uses AF_NETLINK sockets
24  * for this purpose on Linux.  But on all platforms, Open vSwitch uses Netlink
25  * message framing internally for certain purposes.
26  *
27  * This header provides access to the Netlink message framing definitions
28  * regardless of platform.  On Linux, it includes the proper headers directly;
29  * on other platforms it directly defines the structures and macros itself.
30  */
31
32 #include <stdint.h>
33 #include <sys/socket.h>
34 #include "util.h"
35
36 #ifdef HAVE_NETLINK
37 #include <linux/netlink.h>
38 #include <linux/genetlink.h>
39 #else
40 #define NETLINK_GENERIC         16
41
42 struct sockaddr_nl {
43     sa_family_t nl_family;
44     unsigned short int nl_pad;
45     uint32_t nl_pid;
46     uint32_t nl_groups;
47 };
48 BUILD_ASSERT_DECL(sizeof(struct sockaddr_nl) == 12);
49
50 /* nlmsg_flags bits. */
51 #define NLM_F_REQUEST           0x001
52 #define NLM_F_MULTI             0x002
53 #define NLM_F_ACK               0x004
54 #define NLM_F_ECHO              0x008
55
56 #define NLM_F_ROOT              0x100
57 #define NLM_F_MATCH             0x200
58 #define NLM_F_ATOMIC            0x400
59 #define NLM_F_DUMP              (NLM_F_ROOT | NLM_F_MATCH)
60
61 /* nlmsg_type values. */
62 #define NLMSG_NOOP              1
63 #define NLMSG_ERROR             2
64 #define NLMSG_DONE              3
65 #define NLMSG_OVERRUN           4
66
67 #define NLMSG_MIN_TYPE          0x10
68
69 struct nlmsghdr {
70     uint32_t nlmsg_len;
71     uint16_t nlmsg_type;
72     uint16_t nlmsg_flags;
73     uint32_t nlmsg_seq;
74     uint32_t nlmsg_pid;
75 };
76 BUILD_ASSERT_DECL(sizeof(struct nlmsghdr) == 16);
77
78 #define NLMSG_ALIGNTO 4
79 #define NLMSG_ALIGN(SIZE) ROUND_UP(SIZE, NLMSG_ALIGNTO)
80 #define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
81
82 struct nlmsgerr
83 {
84         int error;
85         struct nlmsghdr msg;
86 };
87 BUILD_ASSERT_DECL(sizeof(struct nlmsgerr) == 20);
88
89 #define NETLINK_ADD_MEMBERSHIP  1
90 #define NETLINK_DROP_MEMBERSHIP 2
91 #define NETLINK_PKTINFO         3
92
93 struct genlmsghdr {
94     uint8_t cmd;
95     uint8_t version;
96     uint16_t reserved;
97 };
98 BUILD_ASSERT_DECL(sizeof(struct genlmsghdr) == 4);
99
100 #define GENL_HDRLEN NLMSG_ALIGN(sizeof(struct genlmsghdr))
101
102 struct nlattr {
103     uint16_t nla_len;
104     uint16_t nla_type;
105 };
106 BUILD_ASSERT_DECL(sizeof(struct nlattr) == 4);
107
108 #define NLA_ALIGNTO 4
109 #define NLA_ALIGN(SIZE) ROUND_UP(SIZE, NLA_ALIGNTO)
110 #define NLA_HDRLEN ((int) NLA_ALIGN(sizeof(struct nlattr)))
111
112 #define GENL_MIN_ID     NLMSG_MIN_TYPE
113 #define GENL_MAX_ID     1023
114
115 #define GENL_ID_CTRL            NLMSG_MIN_TYPE
116
117 enum {
118         CTRL_CMD_UNSPEC,
119         CTRL_CMD_NEWFAMILY,
120         CTRL_CMD_DELFAMILY,
121         CTRL_CMD_GETFAMILY,
122         CTRL_CMD_NEWOPS,
123         CTRL_CMD_DELOPS,
124         CTRL_CMD_GETOPS,
125         __CTRL_CMD_MAX,
126 };
127
128 #define CTRL_CMD_MAX (__CTRL_CMD_MAX - 1)
129
130 enum {
131         CTRL_ATTR_UNSPEC,
132         CTRL_ATTR_FAMILY_ID,
133         CTRL_ATTR_FAMILY_NAME,
134         CTRL_ATTR_VERSION,
135         CTRL_ATTR_HDRSIZE,
136         CTRL_ATTR_MAXATTR,
137         CTRL_ATTR_OPS,
138         __CTRL_ATTR_MAX,
139 };
140
141 #define CTRL_ATTR_MAX (__CTRL_ATTR_MAX - 1)
142
143 enum {
144         CTRL_ATTR_OP_UNSPEC,
145         CTRL_ATTR_OP_ID,
146         CTRL_ATTR_OP_FLAGS,
147         __CTRL_ATTR_OP_MAX,
148 };
149
150 #define CTRL_ATTR_OP_MAX (__CTRL_ATTR_OP_MAX - 1)
151 #endif  /* !HAVE_NETLINK */
152
153 /* These were introduced all together in 2.6.24. */
154 #ifndef NLA_TYPE_MASK
155 #define NLA_F_NESTED            (1 << 15)
156 #define NLA_F_NET_BYTEORDER     (1 << 14)
157 #define NLA_TYPE_MASK           ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER)
158 #endif
159
160 #endif /* netlink-protocol.h */