bc896662b6a6060b9df2fcdf0997edeba46b0962
[cascardo/ovs.git] / datapath-windows / ovsext / Netlink.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2013, 2014 Nicira, Inc.
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_H_
18 #define __NETLINK_H_ 1
19
20 #include "OvsTypes.h"
21 #include "NetlinkProto.h"
22
23 /* Netlink attribute types. */
24 typedef enum
25 {
26     NL_A_NO_ATTR = 0,
27     NL_A_UNSPEC,
28     NL_A_U8,
29     NL_A_U16,
30     NL_A_BE16 = NL_A_U16,
31     NL_A_U32,
32     NL_A_BE32 = NL_A_U32,
33     NL_A_U64,
34     NL_A_BE64 = NL_A_U64,
35     NL_A_STRING,
36     NL_A_FLAG,
37     NL_A_NESTED,
38     N_NL_ATTR_TYPES
39 } NL_ATTR_TYPE;
40
41 /* Netlink attribute policy.
42  * Specifies the policy for parsing for netlink attribute. */
43 typedef struct _NL_POLICY
44 {
45     NL_ATTR_TYPE type;
46     UINT32 minLen;
47     UINT32 maxLen;
48 } NL_POLICY, *PNL_POLICY;
49
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))
55
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);                        \
61          (LEFT) > 0;                                                    \
62          (LEFT) -= NLA_ALIGN((ITER)->nlaLen), (ITER) = NlAttrNext(ITER))
63
64 #define NL_ATTR_GET_AS(NLA, TYPE) \
65         (*(TYPE*) NlAttrGetUnspec(nla, sizeof(TYPE)))
66
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);
74
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);
79
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,
96                                 UINT16 type);
97 BOOLEAN NlAttrParse(const PNL_MSG_HDR nlMsg, UINT32 attrOffset,
98                     const NL_POLICY policy[],
99                     PNL_ATTR attrs[], UINT32 n_attrs);
100
101 /* Netlink attribute validation */
102 BOOLEAN NlAttrValidate(const PNL_ATTR, const PNL_POLICY);
103
104 #endif /* __NETLINK_H_ */