Debian Codegraph @

src/filtration/rpc_filtrator.h

raw symbols SCIP
1 //------------------------------------------------------------------------------
2 // Author: Pavel Karneliuk
3 // Description: RPC filtrator
4 // Copyright (c) 2013 EPAM Systems
5 //------------------------------------------------------------------------------
6 /*
7 This file is part of Nfstrace.
8
9 Nfstrace is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, version 2 of the License.
12
13 Nfstrace is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Nfstrace. If not, see <http://www.gnu.org/licenses/>.
20 */
21 //------------------------------------------------------------------------------
22 #ifndef RPC_FILTRATOR_H
23 #define RPC_FILTRATOR_H
24 //------------------------------------------------------------------------------
25 #include <algorithm>
26 #include <cassert>
27
28 #include <pcap/pcap.h>
29
30 #include "filtration/filtratorimpl.h"
31 #include "protocols/netbios/netbios.h"
32 #include "protocols/nfs3/nfs3_utils.h"
33 #include "protocols/nfs4/nfs4_utils.h"
34 #include "protocols/rpc/rpc_header.h"
35 #include "utils/log.h"
36 //------------------------------------------------------------------------------
37 namespace NST
38 {
39 namespace filtration
40 {
41 /*
42 Stateful reader of Sun RPC messages
43 Reads data from PacketInfo passed via push() method
44 aggregates length of current RPC message and length of RPC message useful for analysis
45 TODO: add matching Calls and replies by XID of message
46 */
47 template <typename Writer>
48 class RPCFiltrator final : public FiltratorImpl<RPCFiltrator<Writer>, Writer>
49 {
50 using BaseImpl = FiltratorImpl<RPCFiltrator<Writer>, Writer>;
51
52 public:
53 RPCFiltrator() = default;
54
55 inline void set_writer(utils::NetworkSession* session_ptr, Writer* w, uint32_t max_rpc_hdr)
56 {
57 assert(w);
58 nfs3_rw_hdr_max = max_rpc_hdr;
59 BaseImpl::setWriterImpl(session_ptr, w, max_rpc_hdr);
60 }
61
62 constexpr static size_t lengthOfBaseHeader()
63 {
64 return sizeof(RecordMark) + sizeof(ReplyHeader); // Minimum of replay&call headers
65 }
66
67 constexpr static size_t lengthOfReplyHeader()
68 {
69 return sizeof(RecordMark) + sizeof(ReplyHeader);
70 }
71
72 constexpr static size_t lengthOfCallHeader()
73 {
74 return sizeof(RecordMark) + sizeof(CallHeader);
75 }
76
77 inline static bool isRightHeader(const uint8_t* header)
78 {
79 const RecordMark* rm{reinterpret_cast<const RecordMark*>(header)};
80 const MessageHeader* const msg = rm->fragment();
81 if(msg->type() == MsgType::REPLY)
82 {
83 return RPCValidator::check(static_cast<const ReplyHeader*>(msg));
84 }
85 if(msg->type() == MsgType::CALL)
86 {
87 return RPCValidator::check(static_cast<const CallHeader*>(msg));
88 }
89 return false;
90 }
91
92 inline constexpr static size_t lengthOfFirstSkipedPart()
93 {
94 return sizeof(RecordMark);
95 }
96
97 inline bool collect_header(PacketInfo& info, typename Writer::Collection&)
98 {
99 return BaseImpl::collect_header(info, lengthOfCallHeader(), lengthOfReplyHeader());
100 }
101
102 inline bool find_and_read_message(PacketInfo& info, typename Writer::Collection& collection)
103 {
104 const RecordMark* rm{reinterpret_cast<const RecordMark*>(collection.data())};
105 //if(rm->is_last()); // TODO: handle sequence field of record mark
106 if(collection.data_size() < (sizeof(CallHeader) + sizeof(RecordMark)) && (rm->fragment())->type() != MsgType::REPLY) // if message not Reply, try collect the rest for Call
107 {
108 return true;
109 }
110 if(rm->fragment_len() >= sizeof(ReplyHeader)) // incorrect fragment len, not valid rpc message
111 {
112 if(validate_header(rm->fragment(), rm->fragment_len() + sizeof(RecordMark)))
113 {
114 return BaseImpl::read_message(info);
115 }
116 }
117 return false;
118 }
119
120 inline bool validate_header(const MessageHeader* const msg, const size_t len)
121 {
122 switch(msg->type())
123 {
124 case MsgType::CALL:
125 {
126 auto call = static_cast<const CallHeader*>(msg);
127 if(RPCValidator::check(call))
128 {
129 BaseImpl::setMsgLen(len); // length of current RPC message
130 if(protocols::NFS3::Validator::check(call))
131 {
132 uint32_t proc{call->proc()};
133 if(API::ProcEnumNFS3::WRITE == proc) // truncate NFSv3 WRITE call message to NFSv3-RW-limit
134 {
135 BaseImpl::setToBeCopied(nfs3_rw_hdr_max < len ? nfs3_rw_hdr_max : len);
136 }
137 else
138 {
139 if(API::ProcEnumNFS3::READ == proc)
140 {
141 nfs3_read_match.insert(call->xid());
142 }
143 BaseImpl::setToBeCopied(len);
144 }
145 //TRACE("%p| MATCH RPC Call xid:%u len: %u procedure: %u", this, call->xid(), msg_len, call->proc());
146 }
147 else if(protocols::NFS4::Validator::check(call))
148 {
149 BaseImpl::setToBeCopied(len);
150 }
151 else
152 {
153 //* RPC call message must be read out ==> msg_len !=0
154 BaseImpl::setToBeCopied(0); // don't collect headers of unknown calls
155 //TRACE("Unknown RPC call of program: %u version: %u procedure: %u", call->prog(), call->vers(), call->proc());
156 }
157 return true;
158 }
159 else
160 {
161 return false; // isn't RPC Call, stream is corrupt
162 }
163 }
164 break;
165 case MsgType::REPLY:
166 {
167 auto reply = static_cast<const ReplyHeader*>(msg);
168 if(RPCValidator::check(reply))
169 {
170 BaseImpl::setMsgLen(len); // length of current RPC message
171 // Truncate NFSv3 READ reply message to NFSv3-RW-limit
172 //* Collect fully if reply received before matching call
173 if(nfs3_read_match.erase(reply->xid()) > 0)
174 {
175 BaseImpl::setToBeCopied(std::min(nfs3_rw_hdr_max, len));
176 }
177 else
178 {
179 BaseImpl::setToBeCopied(len); // length of current RPC message
180 }
181 //TRACE("%p| MATCH RPC Reply xid:%u len: %u", this, reply->xid(), msg_len);
182 return true;
183 }
184 else // isn't RPC reply, stream is corrupt
185 {
186 BaseImpl::setMsgLen(0);
187 BaseImpl::setToBeCopied(0);
188 return false;
189 }
190 }
191 break;
192 default:
193 {
194 //isn't RPC message
195 }
196 break;
197 }
198
199 return false;
200 }
201
202 private:
203 size_t nfs3_rw_hdr_max{512}; // limit for NFSv3 to truncate WRITE call and READ reply messages
204 MessageSet nfs3_read_match;
205 };
206
207 } // namespace filtration
208 } // namespace NST
209 //------------------------------------------------------------------------------
210 #endif // RPC_FILTRATOR_H
211 //------------------------------------------------------------------------------