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"
23 /* Netlink attribute types. */
41 /* Netlink attribute policy.
42 * Specifies the policy for parsing for netlink attribute. */
43 typedef struct _NL_POLICY
48 } NL_POLICY, *PNL_POLICY;
50 /* This macro is careful to check for attributes with bad lengths. */
51 #define NL_ATTR_FOR_EACH(ITER, LEFT, ATTRS, ATTRS_LEN) \
52 for ((ITER) = (ATTRS), (LEFT) = (ATTRS_LEN); \
53 NlAttrIsValid(ITER, LEFT); \
54 (LEFT) -= NlAttrLenPad(ITER, LEFT), (ITER) = NlAttrNext(ITER))
56 /* This macro does not check for attributes with bad lengths. It should only
57 * be used with messages from trusted sources or with messages that have
58 * already been validated (e.g. with NL_ATTR_FOR_EACH). */
59 #define NL_ATTR_FOR_EACH_UNSAFE(ITER, LEFT, ATTRS, ATTRS_LEN) \
60 for ((ITER) = (ATTRS), (LEFT) = (ATTRS_LEN); \
62 (LEFT) -= NLA_ALIGN((ITER)->nlaLen), (ITER) = NlAttrNext(ITER))
64 #define NL_ATTR_GET_AS(NLA, TYPE) \
65 (*(TYPE*) NlAttrGetUnspec(nla, sizeof(TYPE)))
67 /* Netlink message accessing the payload */
68 PVOID NlMsgAt(const PNL_MSG_HDR nlh, UINT32 offset);
69 UINT32 NlMsgSize(const PNL_MSG_HDR nlh);
70 PCHAR NlMsgPayload(const PNL_MSG_HDR nlh);
71 UINT32 NlMsgPayloadLen(const PNL_MSG_HDR nlh);
72 PNL_ATTR NlMsgAttrs(const PNL_MSG_HDR nlh);
73 INT NlMsgAttrLen(const PNL_MSG_HDR nlh);
75 /* Netlink message parse */
76 PNL_MSG_HDR NlMsgNext(const PNL_MSG_HDR nlh);
77 INT NlAttrIsValid(const PNL_ATTR nla, UINT32 maxlen);
78 UINT32 NlAttrLenPad(const PNL_ATTR nla, UINT32 maxlen);
80 /* Netlink attribute parsing. */
81 UINT32 NlAttrMinLen(NL_ATTR_TYPE type);
82 UINT32 NlAttrMinLen(NL_ATTR_TYPE type);
83 PNL_ATTR NlAttrNext(const PNL_ATTR nla);
84 UINT16 NlAttrType(const PNL_ATTR nla);
85 PVOID NlAttrData(const PNL_ATTR nla);
86 UINT32 NlAttrGetSize(const PNL_ATTR nla);
87 const PVOID NlAttrGet(const PNL_ATTR nla);
88 const PVOID NlAttrGetUnspec(const PNL_ATTR nla, UINT32 size);
89 BE64 NlAttrGetBe64(const PNL_ATTR nla);
90 BE32 NlAttrGetBe32(const PNL_ATTR nla);
91 UINT8 NlAttrGetU8(const PNL_ATTR nla);
92 UINT32 NlAttrGetU32(const PNL_ATTR nla);
93 const PNL_ATTR NlAttrFind__(const PNL_ATTR attrs,
94 UINT32 size, UINT16 type);
95 const PNL_ATTR NlAttrFindNested(const PNL_ATTR nla,
97 BOOLEAN NlAttrParse(const PNL_MSG_HDR nlMsg, UINT32 attrOffset,
98 const NL_POLICY policy[],
99 PNL_ATTR attrs[], UINT32 n_attrs);
101 /* Netlink attribute validation */
102 BOOLEAN NlAttrValidate(const PNL_ATTR, const PNL_POLICY);
104 #endif /* __NETLINK_H_ */