| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 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); |
| 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 |
|
| 106 |
if(collection.data_size() < (sizeof(CallHeader) + sizeof(RecordMark)) && (rm->fragment())->type() != MsgType::REPLY) |
| 107 |
{ |
| 108 |
return true; |
| 109 |
} |
| 110 |
if(rm->fragment_len() >= sizeof(ReplyHeader)) |
| 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); |
| 130 |
if(protocols::NFS3::Validator::check(call)) |
| 131 |
{ |
| 132 |
uint32_t proc{call->proc()}; |
| 133 |
if(API::ProcEnumNFS3::WRITE == proc) |
| 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 |
|
| 146 |
} |
| 147 |
else if(protocols::NFS4::Validator::check(call)) |
| 148 |
{ |
| 149 |
BaseImpl::setToBeCopied(len); |
| 150 |
} |
| 151 |
else |
| 152 |
{ |
| 153 |
|
| 154 |
BaseImpl::setToBeCopied(0); |
| 155 |
|
| 156 |
} |
| 157 |
return true; |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
return false; |
| 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); |
| 171 |
|
| 172 |
|
| 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); |
| 180 |
} |
| 181 |
|
| 182 |
return true; |
| 183 |
} |
| 184 |
else |
| 185 |
{ |
| 186 |
BaseImpl::setMsgLen(0); |
| 187 |
BaseImpl::setToBeCopied(0); |
| 188 |
return false; |
| 189 |
} |
| 190 |
} |
| 191 |
break; |
| 192 |
default: |
| 193 |
{ |
| 194 |
|
| 195 |
} |
| 196 |
break; |
| 197 |
} |
| 198 |
|
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
private: |
| 203 |
size_t nfs3_rw_hdr_max{512}; |
| 204 |
MessageSet nfs3_read_match; |
| 205 |
}; |
| 206 |
|
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
#endif |
| 211 |
|