netlink-protocol: Define missing symbols.
[sliver-openvswitch.git] / lib / netlink-protocol.h
1 /*
2  * Copyright (c) 2008, 2010, 2011 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
40 /* Some Xenddks have an outdated genetlink header file which doesn't have some
41  * symbols it should. */
42 #ifndef CTRL_ATTR_MCAST_GRP_MAX
43
44 #undef CTRL_ATTR_MAX
45 #define CTRL_ATTR_MAX 7
46 #define CTRL_ATTR_MCAST_GROUPS 7
47
48 enum {
49         CTRL_ATTR_MCAST_GRP_UNSPEC,
50         CTRL_ATTR_MCAST_GRP_NAME,
51         CTRL_ATTR_MCAST_GRP_ID,
52         __CTRL_ATTR_MCAST_GRP_MAX,
53 };
54
55 #define CTRL_ATTR_MCAST_GRP_MAX (__CTRL_ATTR_MCAST_GRP_MAX - 1)
56 #endif /* CTRL_ATTR_MCAST_GRP_MAX */
57
58 #else
59 #define NETLINK_GENERIC         16
60
61 struct sockaddr_nl {
62     sa_family_t nl_family;
63     unsigned short int nl_pad;
64     uint32_t nl_pid;
65     uint32_t nl_groups;
66 };
67 BUILD_ASSERT_DECL(sizeof(struct sockaddr_nl) == 12);
68
69 /* nlmsg_flags bits. */
70 #define NLM_F_REQUEST           0x001
71 #define NLM_F_MULTI             0x002
72 #define NLM_F_ACK               0x004
73 #define NLM_F_ECHO              0x008
74
75 #define NLM_F_ROOT              0x100
76 #define NLM_F_MATCH             0x200
77 #define NLM_F_ATOMIC            0x400
78 #define NLM_F_DUMP              (NLM_F_ROOT | NLM_F_MATCH)
79
80 /* nlmsg_type values. */
81 #define NLMSG_NOOP              1
82 #define NLMSG_ERROR             2
83 #define NLMSG_DONE              3
84 #define NLMSG_OVERRUN           4
85
86 #define NLMSG_MIN_TYPE          0x10
87
88 struct nlmsghdr {
89     uint32_t nlmsg_len;
90     uint16_t nlmsg_type;
91     uint16_t nlmsg_flags;
92     uint32_t nlmsg_seq;
93     uint32_t nlmsg_pid;
94 };
95 BUILD_ASSERT_DECL(sizeof(struct nlmsghdr) == 16);
96
97 #define NLMSG_ALIGNTO 4
98 #define NLMSG_ALIGN(SIZE) ROUND_UP(SIZE, NLMSG_ALIGNTO)
99 #define NLMSG_HDRLEN ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr)))
100
101 struct nlmsgerr
102 {
103         int error;
104         struct nlmsghdr msg;
105 };
106 BUILD_ASSERT_DECL(sizeof(struct nlmsgerr) == 20);
107
108 struct genlmsghdr {
109     uint8_t cmd;
110     uint8_t version;
111     uint16_t reserved;
112 };
113 BUILD_ASSERT_DECL(sizeof(struct genlmsghdr) == 4);
114
115 #define GENL_HDRLEN NLMSG_ALIGN(sizeof(struct genlmsghdr))
116
117 struct nlattr {
118     uint16_t nla_len;
119     uint16_t nla_type;
120 };
121 BUILD_ASSERT_DECL(sizeof(struct nlattr) == 4);
122
123 #define NLA_ALIGNTO 4
124 #define NLA_ALIGN(SIZE) ROUND_UP(SIZE, NLA_ALIGNTO)
125 #define NLA_HDRLEN ((int) NLA_ALIGN(sizeof(struct nlattr)))
126
127 #define GENL_MIN_ID     NLMSG_MIN_TYPE
128 #define GENL_MAX_ID     1023
129
130 #define GENL_ID_CTRL            NLMSG_MIN_TYPE
131
132 enum {
133         CTRL_CMD_UNSPEC,
134         CTRL_CMD_NEWFAMILY,
135         CTRL_CMD_DELFAMILY,
136         CTRL_CMD_GETFAMILY,
137         CTRL_CMD_NEWOPS,
138         CTRL_CMD_DELOPS,
139         CTRL_CMD_GETOPS,
140         __CTRL_CMD_MAX,
141 };
142
143 #define CTRL_CMD_MAX (__CTRL_CMD_MAX - 1)
144
145 enum {
146         CTRL_ATTR_UNSPEC,
147         CTRL_ATTR_FAMILY_ID,
148         CTRL_ATTR_FAMILY_NAME,
149         CTRL_ATTR_VERSION,
150         CTRL_ATTR_HDRSIZE,
151         CTRL_ATTR_MAXATTR,
152         CTRL_ATTR_OPS,
153         __CTRL_ATTR_MAX,
154 };
155
156 #define CTRL_ATTR_MAX (__CTRL_ATTR_MAX - 1)
157
158 enum {
159         CTRL_ATTR_OP_UNSPEC,
160         CTRL_ATTR_OP_ID,
161         CTRL_ATTR_OP_FLAGS,
162         __CTRL_ATTR_OP_MAX,
163 };
164
165 #define CTRL_ATTR_OP_MAX (__CTRL_ATTR_OP_MAX - 1)
166 #endif  /* !HAVE_NETLINK */
167
168 /* These were introduced all together in 2.6.24. */
169 #ifndef NLA_TYPE_MASK
170 #define NLA_F_NESTED            (1 << 15)
171 #define NLA_F_NET_BYTEORDER     (1 << 14)
172 #define NLA_TYPE_MASK           ~(NLA_F_NESTED | NLA_F_NET_BYTEORDER)
173 #endif
174
175 /* These were introduced all together in 2.6.14.  (We want our programs to
176  * support the newer kernel features even if compiled with older headers.) */
177 #ifndef NETLINK_ADD_MEMBERSHIP
178 #define NETLINK_ADD_MEMBERSHIP 1
179 #define NETLINK_DROP_MEMBERSHIP 2
180 #endif
181
182 #endif /* netlink-protocol.h */