2 * Copyright (c) 2008, 2009, 2010, 2011, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #define __NETLINK_H_ 1
21 #include "NetlinkProto.h"
22 #include "NetlinkBuf.h"
23 #include "..\..\include\OvsDpInterface.h"
26 * Structure of any message passed between userspace and kernel.
28 typedef struct _OVS_MESSAGE {
32 /* Variable length nl_attrs follow. */
33 } OVS_MESSAGE, *POVS_MESSAGE;
36 * Structure of an error message sent as a reply from kernel.
38 typedef struct _OVS_MESSAGE_ERROR {
41 } OVS_MESSAGE_ERROR, *POVS_MESSAGE_ERROR;
43 /* Netlink attribute types. */
62 /* Netlink attribute policy.
63 * Specifies the policy for parsing for netlink attribute. */
64 typedef struct _NL_POLICY
70 } NL_POLICY, *PNL_POLICY;
72 /* This macro is careful to check for attributes with bad lengths. */
73 #define NL_ATTR_FOR_EACH(ITER, LEFT, ATTRS, ATTRS_LEN) \
74 for ((ITER) = (ATTRS), (LEFT) = (ATTRS_LEN); \
75 NlAttrIsValid(ITER, LEFT); \
76 (LEFT) -= NlAttrLenPad(ITER, LEFT), (ITER) = NlAttrNext(ITER))
78 /* This macro does not check for attributes with bad lengths. It should only
79 * be used with messages from trusted sources or with messages that have
80 * already been validated (e.g. with NL_ATTR_FOR_EACH). */
81 #define NL_ATTR_FOR_EACH_UNSAFE(ITER, LEFT, ATTRS, ATTRS_LEN) \
82 for ((ITER) = (ATTRS), (LEFT) = (ATTRS_LEN); \
84 (LEFT) -= NLA_ALIGN((ITER)->nlaLen), (ITER) = NlAttrNext(ITER))
86 #define NL_ATTR_GET_AS(NLA, TYPE) \
87 (*(TYPE*) NlAttrGetUnspec(nla, sizeof(TYPE)))
89 BOOLEAN NlFillOvsMsg(PNL_BUFFER nlBuf,
90 UINT16 nlmsgType, UINT16 nlmsgFlags,
91 UINT32 nlmsgSeq, UINT32 nlmsgPid,
92 UINT8 genlCmd, UINT8 genlVer, UINT32 dpNo);
93 BOOLEAN NlFillNlHdr(PNL_BUFFER nlBuf,
94 UINT16 nlmsgType, UINT16 nlmsgFlags,
95 UINT32 nlmsgSeq, UINT32 nlmsgPid);
97 VOID NlBuildErrorMsg(POVS_MESSAGE msgIn, POVS_MESSAGE_ERROR msgOut,
100 /* Netlink message accessing the payload */
101 PVOID NlMsgAt(const PNL_MSG_HDR nlh, UINT32 offset);
102 UINT32 NlMsgSize(const PNL_MSG_HDR nlh);
103 VOID NlMsgAlignSize(const PNL_MSG_HDR nlh);
104 VOID NlMsgSetSize(const PNL_MSG_HDR nlh, UINT32 msgLen);
105 PCHAR NlHdrPayload(const PNL_MSG_HDR nlh);
106 UINT32 NlHdrPayloadLen(const PNL_MSG_HDR nlh);
107 PNL_ATTR NlMsgAttrs(const PNL_MSG_HDR nlh);
108 UINT32 NlMsgAttrsLen(const PNL_MSG_HDR nlh);
110 /* Netlink message parse */
111 PNL_MSG_HDR NlMsgNext(const PNL_MSG_HDR nlh);
112 INT NlAttrIsValid(const PNL_ATTR nla, UINT32 maxlen);
113 UINT32 NlAttrLenPad(const PNL_ATTR nla, UINT32 maxlen);
115 /* Netlink attribute parsing. */
116 UINT32 NlAttrMinLen(NL_ATTR_TYPE type);
117 UINT32 NlAttrMinLen(NL_ATTR_TYPE type);
118 PNL_ATTR NlAttrNext(const PNL_ATTR nla);
119 UINT16 NlAttrType(const PNL_ATTR nla);
120 PVOID NlAttrData(const PNL_ATTR nla);
121 UINT32 NlAttrGetSize(const PNL_ATTR nla);
122 const PVOID NlAttrGet(const PNL_ATTR nla);
123 const PVOID NlAttrGetUnspec(const PNL_ATTR nla, UINT32 size);
124 BE64 NlAttrGetBe64(const PNL_ATTR nla);
125 BE32 NlAttrGetBe32(const PNL_ATTR nla);
126 UINT8 NlAttrGetU8(const PNL_ATTR nla);
127 UINT16 NlAttrGetU16(const PNL_ATTR nla);
128 UINT32 NlAttrGetU32(const PNL_ATTR nla);
129 UINT64 NlAttrGetU64(const PNL_ATTR nla);
130 const PNL_ATTR NlAttrFind__(const PNL_ATTR attrs,
131 UINT32 size, UINT16 type);
132 const PNL_ATTR NlAttrFindNested(const PNL_ATTR nla,
134 BOOLEAN NlAttrParse(const PNL_MSG_HDR nlMsg, UINT32 attrOffset,
135 UINT32 totalAttrLen, const NL_POLICY policy[],
136 const UINT32 numPolicy, PNL_ATTR attrs[],
138 BOOLEAN NlAttrParseNested(const PNL_MSG_HDR nlMsg, UINT32 attrOffset,
139 UINT32 totalAttrLen, const NL_POLICY policy[],
140 const UINT32 numPolicy, PNL_ATTR attrs[],
143 * --------------------------------------------------------------------------
144 * Returns the length of attribute.
145 * --------------------------------------------------------------------------
147 static __inline UINT16
148 NlAttrLen(const PNL_ATTR nla)
154 * ---------------------------------------------------------------------------
155 * Default maximum payload size for each type of attribute.
156 * ---------------------------------------------------------------------------
159 static __inline NlAttrSize(UINT32 payloadSize)
161 return NLA_HDRLEN + payloadSize;
165 * ---------------------------------------------------------------------------
166 * Total length including padding.
167 * ---------------------------------------------------------------------------
170 static __inline NlAttrTotalSize(UINT32 payloadSize)
172 return NLA_ALIGN(NlAttrSize(payloadSize));
175 /* Netlink attribute validation */
176 BOOLEAN NlAttrValidate(const PNL_ATTR, const PNL_POLICY);
179 BOOLEAN NlMsgPutNlHdr(PNL_BUFFER buf, PNL_MSG_HDR nlMsg);
180 BOOLEAN NlMsgPutGenlHdr(PNL_BUFFER buf, PGENL_MSG_HDR genlMsg);
181 BOOLEAN NlMsgPutOvsHdr(PNL_BUFFER buf, POVS_HDR ovsHdr);
183 BOOLEAN NlMsgPutTail(PNL_BUFFER buf, const PCHAR data, UINT32 len);
184 PCHAR NlMsgPutTailUninit(PNL_BUFFER buf, UINT32 len);
185 PCHAR NlMsgPutTailUnspecUninit(PNL_BUFFER buf, UINT16 type, UINT16 len);
186 BOOLEAN NlMsgPutTailUnspec(PNL_BUFFER buf, UINT16 type, PCHAR data, UINT16 len);
187 BOOLEAN NlMsgPutTailFlag(PNL_BUFFER buf, UINT16 type);
188 BOOLEAN NlMsgPutTailU8(PNL_BUFFER buf, UINT16 type, UINT8 value);
189 BOOLEAN NlMsgPutTailU16(PNL_BUFFER buf, UINT16 type, UINT16 value);
190 BOOLEAN NlMsgPutTailU32(PNL_BUFFER buf, UINT16 type, UINT32 value);
191 BOOLEAN NlMsgPutTailU64(PNL_BUFFER buf, UINT16 type, UINT64 value);
192 BOOLEAN NlMsgPutTailString(PNL_BUFFER buf, UINT16 type, PCHAR value);
194 BOOLEAN NlMsgPutHead(PNL_BUFFER buf, const PCHAR data, UINT32 len);
195 PCHAR NlMsgPutHeadUninit(PNL_BUFFER buf, UINT32 len);
196 PCHAR NlMsgPutHeadUnspecUninit(PNL_BUFFER buf, UINT16 type, UINT16 len);
197 BOOLEAN NlMsgPutHeadUnspec(PNL_BUFFER buf, UINT16 type, PCHAR data, UINT16 len);
198 BOOLEAN NlMsgPutHeadFlag(PNL_BUFFER buf, UINT16 type);
199 BOOLEAN NlMsgPutHeadU8(PNL_BUFFER buf, UINT16 type, UINT8 value);
200 BOOLEAN NlMsgPutHeadU16(PNL_BUFFER buf, UINT16 type, UINT16 value);
201 BOOLEAN NlMsgPutHeadU32(PNL_BUFFER buf, UINT16 type, UINT32 value);
202 BOOLEAN NlMsgPutHeadU64(PNL_BUFFER buf, UINT16 type, UINT64 value);
203 BOOLEAN NlMsgPutHeadString(PNL_BUFFER buf, UINT16 type, PCHAR value);
204 UINT32 NlMsgStartNested(PNL_BUFFER buf, UINT16 type);
205 VOID NlMsgEndNested(PNL_BUFFER buf, UINT32 offset);
206 VOID NlMsgPutNested(PNL_BUFFER buf, UINT16 type,
207 const PVOID data, UINT32 size);
209 /* These variants are convenient for iterating nested attributes. */
210 #define NL_NESTED_FOR_EACH(ITER, LEFT, A) \
211 NL_ATTR_FOR_EACH(ITER, LEFT, NlAttrGet(A), NlAttrGetSize(A))
212 #define NL_NESTED_FOR_EACH_UNSAFE(ITER, LEFT, A) \
213 NL_ATTR_FOR_EACH_UNSAFE(ITER, LEFT, NlAttrGet(A), NlAttrGetSize(A))
215 #endif /* __NETLINK_H_ */