Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3284 lines
120 KiB

  1. /*++
  2. Copyright (c) 1990-2000 Microsoft Corporation
  3. Module Name:
  4. iprcv.c - IP receive routines.
  5. Abstract:
  6. This module contains all receive related IP routines.
  7. Author:
  8. [Environment:]
  9. kernel mode only
  10. [Notes:]
  11. optional-notes
  12. Revision History:
  13. --*/
  14. #include "precomp.h"
  15. #include "info.h"
  16. #include "iproute.h"
  17. #include "arpdef.h"
  18. #include "iprtdef.h"
  19. #include "igmp.h"
  20. #if IPMCAST
  21. void IPMForwardAfterTD(NetTableEntry *pPrimarySrcNte, PNDIS_PACKET pnpPacket,
  22. UINT uiBytesCopied);
  23. #endif
  24. // Following is to prevent ip fragment attack
  25. uint MaxRH = 100; // maximum number of reassembly headers allowed
  26. uint NumRH = 0; // Count of RH in use
  27. uint MaxOverlap = 5; // maximum number overlaps allowed for one
  28. // reassembled datagram
  29. uint FragmentAttackDrops = 0;
  30. extern IP_STATUS SendICMPErr(IPAddr, IPHeader UNALIGNED *, uchar, uchar, ulong);
  31. extern uint IPSecStatus;
  32. extern IPSecRcvFWPacketRtn IPSecRcvFWPacketPtr;
  33. extern uchar RATimeout;
  34. extern NDIS_HANDLE BufferPool;
  35. extern ProtInfo IPProtInfo[]; // Protocol information table.
  36. extern ProtInfo *LastPI; // Last protinfo structure looked at.
  37. extern int NextPI; // Next PI field to be used.
  38. extern ProtInfo *RawPI; // Raw IP protinfo
  39. extern NetTableEntry **NewNetTableList; // hash table for NTEs
  40. extern uint NET_TABLE_SIZE;
  41. extern NetTableEntry *LoopNTE;
  42. extern uint DisableIPSourceRouting;
  43. uchar CheckLocalOptions(NetTableEntry *SrcNTE, IPHeader UNALIGNED *Header,
  44. IPOptInfo *OptInfo, uchar DestType, uchar* Data,
  45. uint DataSize, BOOLEAN FilterOnDrop);
  46. #define PROT_RSVP 46 // Protocol number for RSVP
  47. //* FindUserRcv - Find the receive handler to be called for a particular
  48. // protocol.
  49. //
  50. // This functions takes as input a protocol value, and returns a pointer to
  51. // the receive routine for that protocol.
  52. //
  53. // Input: NTE - Pointer to NetTableEntry to be searched
  54. // Protocol - Protocol to be searched for.
  55. // UContext - Place to returns UL Context value.
  56. //
  57. // Returns: Pointer to the receive routine.
  58. //
  59. ULRcvProc
  60. FindUserRcv(uchar Protocol)
  61. {
  62. ULRcvProc RcvProc;
  63. int i;
  64. ProtInfo *TempPI;
  65. if ((TempPI = LastPI)->pi_protocol == Protocol) {
  66. RcvProc = TempPI->pi_rcv;
  67. return RcvProc;
  68. }
  69. RcvProc = (ULRcvProc) NULL;
  70. for (i = 0; i < NextPI; i++) {
  71. if (IPProtInfo[i].pi_protocol == Protocol) {
  72. if (IPProtInfo[i].pi_valid == PI_ENTRY_VALID) {
  73. InterlockedExchangePointer(&LastPI, &IPProtInfo[i]);
  74. RcvProc = IPProtInfo[i].pi_rcv;
  75. return RcvProc;
  76. } else {
  77. // Deregisterd entry. Treat this case as if
  78. // there is no matching protocol.
  79. break;
  80. }
  81. }
  82. }
  83. //
  84. // Didn't find a match. Use the raw protocol if it is registered.
  85. //
  86. if ((TempPI = RawPI) != NULL) {
  87. RcvProc = TempPI->pi_rcv;
  88. }
  89. return RcvProc;
  90. }
  91. //* IPRcvComplete - Handle a receive complete.
  92. //
  93. // Called by the lower layer when receives are temporarily done.
  94. //
  95. // Entry: Nothing.
  96. //
  97. // Returns: Nothing.
  98. //
  99. void
  100. __stdcall
  101. IPRcvComplete(void)
  102. {
  103. void (*ULRcvCmpltProc) (void);
  104. int i;
  105. for (i = 0; i < NextPI; i++) {
  106. if (((ULRcvCmpltProc = IPProtInfo[i].pi_rcvcmplt) != NULL) &&
  107. (IPProtInfo[i].pi_valid == PI_ENTRY_VALID)) {
  108. (*ULRcvCmpltProc) ();
  109. }
  110. }
  111. }
  112. //* UpdateIPSecRcvBuf - update an IPRcvBuf after IPSec receive-processing.
  113. //
  114. // Called to perform IPSec-related changes (e.g. setting checksum-verified)
  115. // for an IPRcvBuf.
  116. //
  117. // Input: RcvBuf - Pointer to IPRcvBuf.
  118. // IPSecFlags - Flags for required changes.
  119. //
  120. void
  121. UpdateIPSecRcvBuf(IPRcvBuf* RcvBuf, ulong IPSecFlags)
  122. {
  123. if (IPSecFlags & (IPSEC_FLAG_TCP_CHECKSUM_VALID |
  124. IPSEC_FLAG_UDP_CHECKSUM_VALID) &&
  125. RcvBuf->ipr_pClientCnt) {
  126. PNDIS_PACKET Packet;
  127. PNDIS_PACKET_EXTENSION PktExt;
  128. PNDIS_TCP_IP_CHECKSUM_PACKET_INFO ChksumPktInfo;
  129. if (RcvBuf->ipr_pMdl) {
  130. Packet = NDIS_GET_ORIGINAL_PACKET((PNDIS_PACKET)
  131. RcvBuf->ipr_RcvContext);
  132. if (Packet == NULL) {
  133. Packet = (PNDIS_PACKET)RcvBuf->ipr_RcvContext;
  134. }
  135. } else {
  136. Packet = (PNDIS_PACKET)RcvBuf->ipr_pClientCnt;
  137. }
  138. PktExt = NDIS_PACKET_EXTENSION_FROM_PACKET(Packet);
  139. ChksumPktInfo =
  140. (PNDIS_TCP_IP_CHECKSUM_PACKET_INFO)
  141. &PktExt->NdisPacketInfo[TcpIpChecksumPacketInfo];
  142. if (IPSecFlags & IPSEC_FLAG_TCP_CHECKSUM_VALID) {
  143. ChksumPktInfo->Receive.NdisPacketTcpChecksumSucceeded = TRUE;
  144. ChksumPktInfo->Receive.NdisPacketTcpChecksumFailed = FALSE;
  145. }
  146. if (IPSecFlags & IPSEC_FLAG_UDP_CHECKSUM_VALID) {
  147. ChksumPktInfo->Receive.NdisPacketUdpChecksumSucceeded = TRUE;
  148. ChksumPktInfo->Receive.NdisPacketUdpChecksumFailed = FALSE;
  149. }
  150. }
  151. }
  152. //* FindRH - Look up a reassembly header on an NTE.
  153. //
  154. // A utility function to look up a reassembly header. We assume the lock
  155. // on the NTE is taken when we are called. If we find a matching RH
  156. // we'll take the lock on it. We also return the predecessor of the RH,
  157. // for use in insertion or deletion.
  158. //
  159. // Input: PrevRH - Place to return pointer to previous RH
  160. // NTE - NTE to be searched.
  161. // Dest - Destination IP address
  162. // Src - Src IP address
  163. // ID - ID of RH
  164. // Protocol - Protocol of RH
  165. //
  166. // Returns: Pointer to RH, or NULL if none.
  167. //
  168. ReassemblyHeader *
  169. FindRH(ReassemblyHeader ** PrevRH, NetTableEntry * NTE, IPAddr Dest, IPAddr Src, ushort Id,
  170. uchar Protocol)
  171. {
  172. ReassemblyHeader *TempPrev, *Current;
  173. TempPrev = STRUCT_OF(ReassemblyHeader, &NTE->nte_ralist, rh_next);
  174. Current = NTE->nte_ralist;
  175. while (Current != (ReassemblyHeader *) NULL) {
  176. if (Current->rh_dest == Dest && Current->rh_src == Src && Current->rh_id == Id &&
  177. Current->rh_protocol == Protocol)
  178. break;
  179. TempPrev = Current;
  180. Current = Current->rh_next;
  181. }
  182. *PrevRH = TempPrev;
  183. return Current;
  184. }
  185. //* ParseRcvdOptions - Validate incoming options.
  186. //
  187. // Called during reception handling to validate incoming options. We make
  188. // sure that everything is OK as best we can, and find indices for any
  189. // source route option.
  190. //
  191. // Input: OptInfo - Pointer to option info. structure.
  192. // Index - Pointer to optindex struct to be filled in.
  193. //
  194. //
  195. // Returns: Index of error if any, MAX_OPT_SIZE if no errors.
  196. //
  197. uchar
  198. ParseRcvdOptions(IPOptInfo * OptInfo, OptIndex * Index)
  199. {
  200. uint i = 0; // Index variable.
  201. uchar *Options = OptInfo->ioi_options;
  202. uint OptLength = (uint) OptInfo->ioi_optlength;
  203. uchar Length; // Length of option.
  204. uchar Pointer; // Pointer field, for options that use it.
  205. if (OptLength < 3) {
  206. // Options should be at least 3 bytes, in the loop below we scan
  207. // first 3 bytes of the packet for finding code, len and ptr value
  208. return (uchar) IP_OPT_LENGTH;
  209. }
  210. while (i < OptLength && *Options != IP_OPT_EOL) {
  211. if (*Options == IP_OPT_NOP) {
  212. i++;
  213. Options++;
  214. continue;
  215. }
  216. if (((Length = Options[IP_OPT_LENGTH]) + i) > OptLength) {
  217. return (uchar) i + (uchar) IP_OPT_LENGTH; // Length exceeds
  218. //options length.
  219. }
  220. Pointer = Options[IP_OPT_DATA] - 1;
  221. if (*Options == IP_OPT_TS) {
  222. if (Length < (MIN_TS_PTR - 1))
  223. return (uchar) i + (uchar) IP_OPT_LENGTH;
  224. if ((Pointer > Length) || (Pointer + 1 < MIN_TS_PTR) || (Pointer % ROUTER_ALERT_SIZE))
  225. return (uchar) i + (uchar) IP_OPT_LENGTH;
  226. Index->oi_tsindex = (uchar) i;
  227. } else {
  228. if (Length < (MIN_RT_PTR - 1))
  229. return (uchar) i + (uchar) IP_OPT_LENGTH;
  230. if (*Options == IP_OPT_LSRR || *Options == IP_OPT_SSRR) {
  231. OptInfo->ioi_flags |= IP_FLAG_SSRR;
  232. if ((Pointer > Length) || (Pointer + 1 < MIN_RT_PTR) || ((Pointer + 1) % ROUTER_ALERT_SIZE))
  233. return (uchar) i + (uchar) IP_OPT_LENGTH;
  234. // A source route option
  235. if (Pointer < Length) { // Route not complete
  236. if ((Length - Pointer) < sizeof(IPAddr))
  237. return (uchar) i + (uchar) IP_OPT_LENGTH;
  238. Index->oi_srtype = *Options;
  239. Index->oi_srindex = (uchar) i;
  240. }
  241. } else {
  242. if (*Options == IP_OPT_RR) {
  243. if ((Pointer > Length) || (Pointer + 1 < MIN_RT_PTR) || ((Pointer + 1) % ROUTER_ALERT_SIZE))
  244. return (uchar) i + (uchar) IP_OPT_LENGTH;
  245. Index->oi_rrindex = (uchar) i;
  246. } else if (*Options == IP_OPT_ROUTER_ALERT) {
  247. Index->oi_rtrindex = (uchar) i;
  248. }
  249. }
  250. }
  251. i += Length;
  252. Options += Length;
  253. }
  254. return MAX_OPT_SIZE;
  255. }
  256. //* IsRtrAlertPacket - Finds whether an IP packet contains rtr alert option.
  257. // Input: Header - Pointer to incoming header.
  258. // Returns: TRUE if packet contains rtr alert option
  259. //
  260. BOOLEAN
  261. IsRtrAlertPacket(IPHeader UNALIGNED * Header)
  262. {
  263. uint HeaderLength;
  264. IPOptInfo OptInfo;
  265. OptIndex Index;
  266. uint i = 0; // Index variable.
  267. HeaderLength = (Header->iph_verlen & (uchar) ~ IP_VER_FLAG) << 2;
  268. if (HeaderLength <= sizeof(IPHeader)) {
  269. return FALSE;
  270. }
  271. OptInfo.ioi_options = (uchar *) (Header + 1);
  272. OptInfo.ioi_optlength = (uchar) (HeaderLength - sizeof(IPHeader));
  273. Index.oi_rtrindex = MAX_OPT_SIZE;
  274. ParseRcvdOptions(&OptInfo, &Index);
  275. if (Index.oi_rtrindex == MAX_OPT_SIZE) {
  276. return FALSE;
  277. }
  278. return TRUE;
  279. }
  280. BOOLEAN
  281. IsBCastAllowed(IPAddr DestAddr, IPAddr SrcAddr, uchar Protocol,
  282. NetTableEntry *NTE)
  283. {
  284. uchar DestType;
  285. DestType = IsBCastOnNTE(DestAddr, NTE);
  286. // Note that IGMP Queries must be immune to the source
  287. // filter or else we cannot over
  288. if (DestType == DEST_MCAST) {
  289. uint PromiscuousMode = 0;
  290. if (NTE->nte_flags & NTE_VALID) {
  291. PromiscuousMode = NTE->nte_if->if_promiscuousmode;
  292. }
  293. if (!PromiscuousMode) {
  294. DestType = IsMCastSourceAllowed(DestAddr, SrcAddr, Protocol, NTE);
  295. }
  296. }
  297. return IS_BCAST_DEST(DestType);
  298. }
  299. //* BCastRcv - Receive a broadcast or multicast packet.
  300. //
  301. // Called when we have to receive a broadcast packet. We loop through the
  302. // NTE table, calling the upper layer receive protocol for each net which
  303. // matches the receive I/F and for which the destination address is a
  304. // broadcast.
  305. //
  306. // Input: RcvProc - The receive procedure to be called.
  307. // SrcNTE - NTE on which the packet was originally received.
  308. // DestAddr - Destination address.
  309. // SrcAddr - Source address of packet.
  310. // Data - Pointer to received data.
  311. // DataLength - Size in bytes of data
  312. // Protocol - Upper layer protocol being called.
  313. // OptInfo - Pointer to received IP option info.
  314. //
  315. // Returns: Nothing.
  316. //
  317. void
  318. BCastRcv(ULRcvProc RcvProc, NetTableEntry * SrcNTE, IPAddr DestAddr,
  319. IPAddr SrcAddr, IPHeader UNALIGNED * Header, uint HeaderLength,
  320. IPRcvBuf * Data, uint DataLength, uchar Protocol, IPOptInfo * OptInfo)
  321. {
  322. NetTableEntry *CurrentNTE;
  323. const Interface *SrcIF = SrcNTE->nte_if;
  324. ulong Delivered = 0;
  325. uint i;
  326. for (i = 0; i < NET_TABLE_SIZE; i++) {
  327. NetTableEntry *NetTableList = NewNetTableList[i];
  328. for (CurrentNTE = NetTableList;
  329. CurrentNTE != NULL;
  330. CurrentNTE = CurrentNTE->nte_next) {
  331. if ((CurrentNTE->nte_flags & NTE_ACTIVE) &&
  332. (CurrentNTE->nte_if == SrcIF) &&
  333. (IsBCastAllowed(DestAddr, SrcAddr, Protocol, CurrentNTE)
  334. || (SrcNTE == LoopNTE))) {
  335. uchar *saveddata = Data->ipr_buffer;
  336. uint savedlen = Data->ipr_size;
  337. Delivered = 1;
  338. (*RcvProc) (CurrentNTE, DestAddr, SrcAddr, CurrentNTE->nte_addr,
  339. SrcNTE->nte_addr, Header, HeaderLength, Data, DataLength,
  340. TRUE, Protocol, OptInfo);
  341. // restore the buffers;
  342. Data->ipr_buffer = saveddata;
  343. Data->ipr_size = savedlen;
  344. }
  345. }
  346. }
  347. if (Delivered) {
  348. IPSIncrementInDeliverCount();
  349. }
  350. }
  351. //* DeliverToUser - Deliver data to a user protocol.
  352. //
  353. // This procedure is called when we have determined that an incoming
  354. // packet belongs here, and any options have been processed. We accept
  355. // it for upper layer processing, which means looking up the receive
  356. // procedure and calling it, or passing it to BCastRcv if neccessary.
  357. //
  358. // Input: SrcNTE - Pointer to NTE on which packet arrived.
  359. // DestNTE - Pointer to NTE that is accepting packet.
  360. // Header - Pointer to IP header of packet.
  361. // HeaderLength - Length of Header in bytes.
  362. // Data - Pointer to IPRcvBuf chain.
  363. // DataLength - Length in bytes of upper layer data.
  364. // OptInfo - Pointer to Option information for this receive.
  365. // DestType - Type of destination - LOCAL, BCAST.
  366. //
  367. // Returns: Nothing.
  368. void
  369. DeliverToUser(NetTableEntry * SrcNTE, NetTableEntry * DestNTE,
  370. IPHeader UNALIGNED * Header, uint HeaderLength, IPRcvBuf * Data,
  371. uint DataLength, IPOptInfo * OptInfo, PNDIS_PACKET Packet, uchar DestType)
  372. {
  373. ULRcvProc rcv;
  374. uint PromiscuousMode;
  375. uint FirewallMode;
  376. PromiscuousMode = SrcNTE->nte_if->if_promiscuousmode;
  377. FirewallMode = ProcessFirewallQ();
  378. //
  379. // Call into IPSEC so he can decrypt the data. Call only for remote packets.
  380. //
  381. if (IPSecHandlerPtr) {
  382. //
  383. // See if IPSEC is enabled, see if it needs to do anything with this
  384. // packet.
  385. //
  386. FORWARD_ACTION Action;
  387. ULONG ipsecByteCount = 0;
  388. ULONG ipsecMTU = 0;
  389. ULONG ipsecFlags = IPSEC_FLAG_INCOMING;
  390. PNDIS_BUFFER newBuf = NULL;
  391. ulong csum;
  392. IPHeader *IPH;
  393. if (!((ForwardFilterEnabled) || (FirewallMode) || (PromiscuousMode))) {
  394. // else ipsec is already called in DeliverToUserEx
  395. if (SrcNTE == LoopNTE) {
  396. ipsecFlags |= IPSEC_FLAG_LOOPBACK;
  397. }
  398. if (OptInfo->ioi_flags & IP_FLAG_SSRR) {
  399. ipsecFlags |= IPSEC_FLAG_SSRR;
  400. }
  401. Action = (*IPSecHandlerPtr) (
  402. (PUCHAR) Header,
  403. (PVOID) Data,
  404. SrcNTE->nte_if, // SrcIF
  405. Packet,
  406. &ipsecByteCount,
  407. &ipsecMTU,
  408. (PVOID *) & newBuf,
  409. &ipsecFlags,
  410. DestType);
  411. if (Action != eFORWARD) {
  412. IPSInfo.ipsi_indiscards++;
  413. return;
  414. } else {
  415. //
  416. // Update the data length if IPSEC changed it
  417. // (like by removing the AH)
  418. //
  419. DataLength -= ipsecByteCount;
  420. UpdateIPSecRcvBuf(Data, ipsecFlags);
  421. }
  422. }
  423. }
  424. Data->ipr_flags = 0;
  425. // Process this request right now. Look up the protocol. If we
  426. // find it, copy the data if we need to, and call the protocol's
  427. // receive handler. If we don't find it, send an ICMP
  428. // 'protocol unreachable' message.
  429. rcv = FindUserRcv(Header->iph_protocol);
  430. if (!PromiscuousMode) {
  431. if (rcv != NULL) {
  432. IP_STATUS Status;
  433. if (DestType == DEST_LOCAL) {
  434. Status = (*rcv) (SrcNTE, Header->iph_dest, Header->iph_src,
  435. DestNTE->nte_addr, SrcNTE->nte_addr, Header,
  436. HeaderLength, Data, DataLength, FALSE,
  437. Header->iph_protocol, OptInfo);
  438. if (Status == IP_SUCCESS) {
  439. IPSIncrementInDeliverCount();
  440. return;
  441. }
  442. if (Status == IP_DEST_PROT_UNREACHABLE) {
  443. IPSInfo.ipsi_inunknownprotos++;
  444. SendICMPErr(DestNTE->nte_addr, Header, ICMP_DEST_UNREACH,
  445. PROT_UNREACH, 0);
  446. } else {
  447. IPSIncrementInDeliverCount();
  448. SendICMPErr(DestNTE->nte_addr, Header, ICMP_DEST_UNREACH,
  449. PORT_UNREACH, 0);
  450. }
  451. return; // Just return out of here now.
  452. } else if (DestType < DEST_REMOTE) { // BCAST, SN_BCAST, MCAST
  453. BCastRcv(rcv, SrcNTE, Header->iph_dest, Header->iph_src,
  454. Header, HeaderLength, Data, DataLength,
  455. Header->iph_protocol, OptInfo);
  456. } else {
  457. // DestType >= DEST_REMOTE
  458. // Force Rcv protocol to be Raw
  459. rcv = NULL;
  460. if (RawPI != NULL) {
  461. rcv = RawPI->pi_rcv;
  462. }
  463. if ((rcv != NULL) && (DestType != DEST_INVALID)) {
  464. Data->ipr_flags |= IPR_FLAG_PROMISCUOUS;
  465. Status = (*rcv) (SrcNTE,Header->iph_dest,Header->iph_src,
  466. DestNTE->nte_addr, SrcNTE->nte_addr, Header,
  467. HeaderLength, Data, DataLength, FALSE,
  468. Header->iph_protocol, OptInfo);
  469. }
  470. return; // Just return out of here now.
  471. }
  472. } else {
  473. IPSInfo.ipsi_inunknownprotos++;
  474. // If we get here, we didn't find a matching protocol. Send an
  475. // ICMP message.
  476. SendICMPErr(DestNTE->nte_addr, Header,
  477. ICMP_DEST_UNREACH, PROT_UNREACH, 0);
  478. }
  479. } else { // PromiscuousMode = 1
  480. IP_STATUS Status;
  481. if (DestType == DEST_LOCAL) {
  482. if (rcv != NULL) {
  483. uchar *saveddata = Data->ipr_buffer;
  484. uint savedlen = Data->ipr_size;
  485. Data->ipr_flags |= IPR_FLAG_PROMISCUOUS;
  486. Status = (*rcv) (SrcNTE, Header->iph_dest, Header->iph_src,
  487. DestNTE->nte_addr, SrcNTE->nte_addr, Header,
  488. HeaderLength, Data, DataLength, FALSE,
  489. Header->iph_protocol, OptInfo);
  490. if (Status == IP_SUCCESS) {
  491. IPSIncrementInDeliverCount();
  492. // If succeed and promiscuous mode set
  493. // also do a raw rcv if previous wasn't a RawRcv
  494. if ((RawPI != NULL) && (RawPI->pi_rcv != NULL) && (RawPI->pi_rcv != rcv)) {
  495. // we hv registered for RAW protocol
  496. rcv = RawPI->pi_rcv;
  497. // restore the buffers;
  498. Data->ipr_buffer = saveddata;
  499. Data->ipr_size = savedlen;
  500. Status = (*rcv) (SrcNTE, Header->iph_dest, Header->iph_src,
  501. DestNTE->nte_addr, SrcNTE->nte_addr, Header,
  502. HeaderLength, Data, DataLength, FALSE,
  503. Header->iph_protocol, OptInfo);
  504. }
  505. return;
  506. }
  507. if (Status == IP_DEST_PROT_UNREACHABLE) {
  508. IPSInfo.ipsi_inunknownprotos++;
  509. SendICMPErr(DestNTE->nte_addr, Header, ICMP_DEST_UNREACH,
  510. PROT_UNREACH, 0);
  511. } else {
  512. IPSIncrementInDeliverCount();
  513. SendICMPErr(DestNTE->nte_addr, Header, ICMP_DEST_UNREACH,
  514. PORT_UNREACH, 0);
  515. }
  516. } else {
  517. IPSInfo.ipsi_inunknownprotos++;
  518. // If we get here, we didn't find a matching protocol. Send
  519. // an ICMP message.
  520. SendICMPErr(DestNTE->nte_addr, Header, ICMP_DEST_UNREACH, PROT_UNREACH, 0);
  521. }
  522. return; // Just return out of here now.
  523. } else if (DestType < DEST_REMOTE) { // BCAST, SN_BCAST, MCAST
  524. uchar *saveddata = Data->ipr_buffer;
  525. uint savedlen = Data->ipr_size;
  526. if (rcv != NULL) {
  527. Data->ipr_flags |= IPR_FLAG_PROMISCUOUS;
  528. BCastRcv(rcv, SrcNTE, Header->iph_dest, Header->iph_src,
  529. Header, HeaderLength, Data, DataLength,
  530. Header->iph_protocol, OptInfo);
  531. // If succeed and promiscuous mode set
  532. // also do a raw rcv if previous is not RawRcv
  533. if ((RawPI != NULL) && (RawPI->pi_rcv != NULL) && (RawPI->pi_rcv != rcv)) {
  534. // we hv registered for RAW protocol
  535. rcv = RawPI->pi_rcv;
  536. Data->ipr_buffer = saveddata;
  537. Data->ipr_size = savedlen;
  538. Status = (*rcv) (SrcNTE, Header->iph_dest, Header->iph_src,
  539. DestNTE->nte_addr, SrcNTE->nte_addr, Header,
  540. HeaderLength, Data, DataLength, FALSE,
  541. Header->iph_protocol, OptInfo);
  542. }
  543. } else {
  544. IPSInfo.ipsi_inunknownprotos++;
  545. // If we get here, we didn't find a matching protocol. Send an ICMP message.
  546. SendICMPErr(DestNTE->nte_addr, Header, ICMP_DEST_UNREACH, PROT_UNREACH, 0);
  547. }
  548. } else { // DestType >= DEST_REMOTE and promiscuous mode set
  549. // Force Rcv protocol to be Raw
  550. rcv = NULL;
  551. if (RawPI != NULL) {
  552. rcv = RawPI->pi_rcv;
  553. }
  554. if ((rcv != NULL) && (DestType != DEST_INVALID)) {
  555. Data->ipr_flags |= IPR_FLAG_PROMISCUOUS;
  556. Status = (*rcv) (SrcNTE, Header->iph_dest, Header->iph_src,
  557. DestNTE->nte_addr, SrcNTE->nte_addr, Header,
  558. HeaderLength, Data, DataLength, FALSE,
  559. Header->iph_protocol, OptInfo);
  560. return; // Just return out of here now.
  561. } else {
  562. if (rcv == NULL) {
  563. KdPrint(("Rcv is NULL \n"));
  564. } else {
  565. KdPrint(("Dest invalid \n"));
  566. }
  567. }
  568. } // DestType >= DEST_REMOTE
  569. } // Promiscuous Mode
  570. }
  571. uchar *
  572. ConvertIPRcvBufToFlatBuffer(IPRcvBuf * pRcvBuf, uint DataLength)
  573. {
  574. uchar *pBuff;
  575. IPRcvBuf *tmpRcvBuf;
  576. uint FrwlOffset;
  577. // convert RcvBuf chain to a flat buffer
  578. tmpRcvBuf = pRcvBuf;
  579. FrwlOffset = 0;
  580. pBuff = CTEAllocMemN(DataLength, 'aiCT');
  581. if (pBuff) {
  582. while (tmpRcvBuf != NULL) {
  583. ASSERT(tmpRcvBuf->ipr_buffer != NULL);
  584. RtlCopyMemory(pBuff + FrwlOffset, tmpRcvBuf->ipr_buffer, tmpRcvBuf->ipr_size);
  585. FrwlOffset += tmpRcvBuf->ipr_size;
  586. tmpRcvBuf = tmpRcvBuf->ipr_next;
  587. }
  588. }
  589. return pBuff;
  590. }
  591. //* DeliverToUserEx - Called when (IPSEC & Filter)/Firewall/Promiscuous set
  592. //
  593. // Input: SrcNTE - Pointer to NTE on which packet arrived.
  594. // DestNTE - Pointer to NTE that is accepting packet.
  595. // Header - Pointer to IP header of packet.
  596. // HeaderLength - Length of Header in bytes.
  597. // Data - Pointer to IPRcvBuf chain.
  598. // DataLength - Length in bytes of upper layer data +
  599. // HeaderLength.
  600. // OptInfo - Pointer to Option information for this receive.
  601. // DestType - Type of destination - LOCAL, BCAST.
  602. //
  603. // It is assumed that if firewall is present Data contains IPHeader also.
  604. // Also, DataLength includes HeaderLength in this case
  605. //
  606. // Returns: Nothing.
  607. void
  608. DeliverToUserEx(NetTableEntry * SrcNTE, NetTableEntry * DestNTE,
  609. IPHeader UNALIGNED * Header, uint HeaderLength, IPRcvBuf * Data,
  610. uint DataLength, IPOptInfo * OptInfo, PNDIS_PACKET Packet, uchar DestType, LinkEntry * LinkCtxt)
  611. {
  612. uint PromiscuousMode;
  613. uint FirewallMode;
  614. uint FirewallRef;
  615. Queue* FirewallQ;
  616. uint FastPath;
  617. IPRcvBuf *tmpRcvBuf;
  618. uint FrwlOffset;
  619. uchar *pBuff;
  620. BOOLEAN OneChunk;
  621. PromiscuousMode = SrcNTE->nte_if->if_promiscuousmode;
  622. FirewallMode = ProcessFirewallQ();
  623. if (DestType == DEST_PROMIS) {
  624. // We don't call any hook for this packet
  625. // if firewall is there take the header off
  626. // and then delivertouser
  627. if (FirewallMode) {
  628. if (Data->ipr_size > HeaderLength) { //1st buff contains data also
  629. uchar *saveddata = Data->ipr_buffer;
  630. Data->ipr_buffer += HeaderLength;
  631. Data->ipr_size -= HeaderLength;
  632. DataLength -= HeaderLength;
  633. DeliverToUser(SrcNTE, DestNTE, Header, HeaderLength, Data, DataLength, OptInfo, NULL, DestType);
  634. // restore the buffers;
  635. Data->ipr_buffer = saveddata;
  636. Data->ipr_size += HeaderLength;
  637. IPFreeBuff(Data);
  638. } else { // First buffer just contains Header
  639. uchar *saveddata;
  640. if (Data->ipr_next == NULL) {
  641. // we received the data s.t. datasize == headersize
  642. IPSInfo.ipsi_indiscards++;
  643. IPFreeBuff(Data);
  644. return;
  645. }
  646. saveddata = Data->ipr_next->ipr_buffer;
  647. DataLength -= HeaderLength;
  648. DeliverToUser(SrcNTE, DestNTE, Header, HeaderLength, Data->ipr_next, DataLength, OptInfo, NULL, DestType);
  649. // restore the buffers;
  650. Data->ipr_next->ipr_buffer = saveddata;
  651. IPFreeBuff(Data);
  652. }
  653. } else { // FirewallMode is 0
  654. DeliverToUser(SrcNTE, DestNTE, Header, HeaderLength,
  655. Data, DataLength, OptInfo, NULL, DestType);
  656. }
  657. return;
  658. }
  659. if (DestType >= DEST_REMOTE) {
  660. // Packet would have gone to the forward path, normally
  661. // Call the filter/firewall hook if its there
  662. if (FirewallMode) {
  663. FORWARD_ACTION Action = FORWARD;
  664. FIREWALL_CONTEXT_T FrCtx;
  665. IPAddr DAddr = Header->iph_dest;
  666. IPRcvBuf *pRcvBuf = Data;
  667. IPRcvBuf *pOutRcvBuf = NULL;
  668. NetTableEntry *DstNTE;
  669. Queue *CurrQ;
  670. FIREWALL_HOOK *CurrHook;
  671. uint DestIFIndex = INVALID_IF_INDEX;
  672. uchar DestinationType = DestType;
  673. uint BufferChanged = 0;
  674. KIRQL OldIrql;
  675. FrCtx.Direction = IP_RECEIVE;
  676. FrCtx.NTE = SrcNTE; //NTE the dg arrived on
  677. FrCtx.LinkCtxt = LinkCtxt;
  678. if (pRcvBuf->ipr_size > HeaderLength) { //1st buffer contains data also
  679. FastPath = 1;
  680. } else {
  681. FastPath = 0;
  682. if (pRcvBuf->ipr_next == NULL) {
  683. // we received the data s.t. datasize == headersize
  684. IPSInfo.ipsi_indiscards++;
  685. IPFreeBuff(pRcvBuf);
  686. return;
  687. }
  688. }
  689. // Call the filter hook if installed
  690. if (ForwardFilterEnabled) {
  691. FORWARD_ACTION Action = FORWARD;
  692. if (FastPath) {
  693. // first buffer contains data also
  694. Interface *IF = SrcNTE->nte_if;
  695. IPAddr LinkNextHop;
  696. if ((IF->if_flags & IF_FLAGS_P2MP) && LinkCtxt) {
  697. LinkNextHop = LinkCtxt->link_NextHop;
  698. } else {
  699. LinkNextHop = NULL_IP_ADDR;
  700. }
  701. CTEInterlockedIncrementLong(&ForwardFilterRefCount);
  702. Action = (*ForwardFilterPtr) (
  703. Header,
  704. pRcvBuf->ipr_buffer + HeaderLength,
  705. pRcvBuf->ipr_size - HeaderLength,
  706. IF->if_index,
  707. INVALID_IF_INDEX,
  708. LinkNextHop,
  709. NULL_IP_ADDR);
  710. DerefFilterPtr();
  711. } else { // Fast Path = 0
  712. // first buffer contains IPHeader only
  713. Interface *IF = SrcNTE->nte_if;
  714. IPAddr LinkNextHop;
  715. if ((IF->if_flags & IF_FLAGS_P2MP) && LinkCtxt) {
  716. LinkNextHop = LinkCtxt->link_NextHop;
  717. } else {
  718. LinkNextHop = NULL_IP_ADDR;
  719. }
  720. CTEInterlockedIncrementLong(&ForwardFilterRefCount);
  721. Action = (*ForwardFilterPtr) (
  722. Header,
  723. pRcvBuf->ipr_next->ipr_buffer,
  724. pRcvBuf->ipr_next->ipr_size,
  725. IF->if_index,
  726. INVALID_IF_INDEX,
  727. LinkNextHop,
  728. NULL_IP_ADDR);
  729. DerefFilterPtr();
  730. }
  731. if (Action != FORWARD) {
  732. IPSInfo.ipsi_indiscards++;
  733. IPFreeBuff(pRcvBuf);
  734. return;
  735. }
  736. }
  737. // call the firewallhook from front;
  738. // in xmit path we call it from rear
  739. #if MILLEN
  740. KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
  741. #else // MILLEN
  742. ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL);
  743. #endif // MILLEN
  744. FirewallRef = RefFirewallQ(&FirewallQ);
  745. CurrQ = QHEAD(FirewallQ);
  746. while (CurrQ != QEND(FirewallQ)) {
  747. CurrHook = QSTRUCT(FIREWALL_HOOK, CurrQ, hook_q);
  748. pOutRcvBuf = NULL;
  749. // pOutRcvBuf is assumed to be NULL before firewall hook is
  750. //called
  751. Action = (*CurrHook->hook_Ptr) (&pRcvBuf,
  752. SrcNTE->nte_if->if_index,
  753. &DestIFIndex,
  754. &DestinationType,
  755. &FrCtx,
  756. sizeof(FrCtx),
  757. &pOutRcvBuf);
  758. if (Action == DROP) {
  759. DerefFirewallQ(FirewallRef);
  760. #if MILLEN
  761. KeLowerIrql(OldIrql);
  762. #endif // MILLEN
  763. IPSInfo.ipsi_indiscards++;
  764. if (pRcvBuf != NULL) {
  765. IPFreeBuff(pRcvBuf);
  766. }
  767. if (pOutRcvBuf != NULL) {
  768. IPFreeBuff(pOutRcvBuf);
  769. }
  770. IPSInfo.ipsi_indiscards++;
  771. return;
  772. } else {
  773. ASSERT(Action == FORWARD);
  774. if (pOutRcvBuf != NULL) {
  775. // free the old buffer
  776. if (pRcvBuf != NULL) {
  777. IPFreeBuff(pRcvBuf);
  778. }
  779. pRcvBuf = pOutRcvBuf;
  780. BufferChanged = 1;
  781. }
  782. }
  783. CurrQ = QNEXT(CurrQ);
  784. }
  785. DerefFirewallQ(FirewallRef);
  786. #if MILLEN
  787. KeLowerIrql(OldIrql);
  788. #endif // MILLEN
  789. ASSERT(Action == FORWARD);
  790. if (BufferChanged) {
  791. // if packet touched compute the new length: DataSize
  792. DataLength = 0;
  793. tmpRcvBuf = pRcvBuf;
  794. while (tmpRcvBuf != NULL) {
  795. ASSERT(tmpRcvBuf->ipr_buffer != NULL);
  796. DataLength += tmpRcvBuf->ipr_size;
  797. tmpRcvBuf = tmpRcvBuf->ipr_next;
  798. }
  799. // also make Header point to new buffer
  800. Header = (IPHeader *) pRcvBuf->ipr_buffer;
  801. HeaderLength = (Header->iph_verlen & 0xf) << 2;
  802. }
  803. DataLength -= HeaderLength; // decrement the header length
  804. if (DestinationType == DEST_INVALID) { // Dest Addr changed by hook
  805. DAddr = Header->iph_dest;
  806. DstNTE = SrcNTE;
  807. DestType = GetLocalNTE(DAddr, &DstNTE);
  808. DestNTE = DstNTE;
  809. }
  810. if (DestType < DEST_REMOTE) {
  811. // Check to see options
  812. if (HeaderLength != sizeof(IPHeader)) {
  813. // We have options
  814. uchar NewDType;
  815. NewDType = CheckLocalOptions(
  816. SrcNTE,
  817. (IPHeader UNALIGNED *) Header,
  818. OptInfo,
  819. DestType,
  820. NULL,
  821. 0,
  822. FALSE);
  823. if (NewDType != DEST_LOCAL) {
  824. if (NewDType == DEST_REMOTE) {
  825. if (PromiscuousMode) {
  826. if (FastPath) {
  827. uchar *saveddata = pRcvBuf->ipr_buffer;
  828. pRcvBuf->ipr_buffer += HeaderLength;
  829. pRcvBuf->ipr_size -= HeaderLength;
  830. DeliverToUser(
  831. SrcNTE,
  832. DestNTE,
  833. (IPHeader UNALIGNED *) Header,
  834. HeaderLength,
  835. pRcvBuf,
  836. DataLength,
  837. OptInfo,
  838. NULL,
  839. DestType);
  840. // restore the buffer
  841. pRcvBuf->ipr_buffer = saveddata;
  842. pRcvBuf->ipr_size += HeaderLength;
  843. } else {
  844. uchar *saveddata = pRcvBuf->ipr_next->ipr_buffer;
  845. DeliverToUser(
  846. SrcNTE,
  847. DestNTE,
  848. (IPHeader UNALIGNED *)Header,
  849. HeaderLength,
  850. pRcvBuf->ipr_next,
  851. DataLength,
  852. OptInfo,
  853. NULL,
  854. DestType);
  855. // restore the buffers;
  856. pRcvBuf->ipr_next->ipr_buffer = saveddata;
  857. }
  858. }
  859. goto forward_remote;
  860. } else {
  861. IPSInfo.ipsi_inhdrerrors++;
  862. IPFreeBuff(pRcvBuf);
  863. //CTEFreeMem(pBuff);
  864. return; // Bad Options
  865. }
  866. } // NewDtype != LOCAL
  867. } // Options present
  868. } // DestType < DEST_REMOTE
  869. else { // DestType >=DEST_REMOTE
  870. if (PromiscuousMode) {
  871. if (FastPath) {
  872. uchar *savedata = pRcvBuf->ipr_buffer;
  873. pRcvBuf->ipr_buffer += HeaderLength;
  874. pRcvBuf->ipr_size -= HeaderLength;
  875. DeliverToUser(SrcNTE,
  876. DestNTE, (IPHeader UNALIGNED *) Header,
  877. HeaderLength,pRcvBuf, DataLength,
  878. OptInfo, NULL, DestType);
  879. // restore the buffer
  880. pRcvBuf->ipr_buffer = savedata;
  881. pRcvBuf->ipr_size += HeaderLength;
  882. } else {
  883. uchar *saveddata = pRcvBuf->ipr_next->ipr_buffer;
  884. DeliverToUser(SrcNTE, DestNTE,
  885. (IPHeader UNALIGNED *)Header,HeaderLength,
  886. pRcvBuf->ipr_next, DataLength, OptInfo,
  887. NULL, DestType);
  888. // restore the buffers;
  889. pRcvBuf->ipr_next->ipr_buffer = saveddata;
  890. }
  891. }
  892. goto forward_remote;
  893. }
  894. // DestType <= DEST_REMOTE
  895. if (FastPath) {
  896. uchar *saveddata = pRcvBuf->ipr_buffer;
  897. pRcvBuf->ipr_buffer += HeaderLength;
  898. pRcvBuf->ipr_size -= HeaderLength;
  899. DeliverToUser(SrcNTE, DestNTE, (IPHeader UNALIGNED *) Header,
  900. HeaderLength,pRcvBuf, DataLength, OptInfo, NULL,
  901. DestType);
  902. // restore the buffer
  903. pRcvBuf->ipr_buffer = saveddata;
  904. pRcvBuf->ipr_size += HeaderLength;
  905. } else {
  906. uchar *saveddata = pRcvBuf->ipr_next->ipr_buffer;
  907. DeliverToUser(SrcNTE, DestNTE, (IPHeader UNALIGNED *) Header,
  908. HeaderLength, pRcvBuf->ipr_next, DataLength,
  909. OptInfo, NULL, DestType);
  910. // restore the buffers;
  911. pRcvBuf->ipr_next->ipr_buffer = saveddata;
  912. }
  913. if (IS_BCAST_DEST(DestType)) {
  914. OneChunk = FALSE;
  915. if (pRcvBuf->ipr_next == NULL) {
  916. OneChunk = TRUE;
  917. pBuff = pRcvBuf->ipr_buffer;
  918. } else {
  919. pBuff = ConvertIPRcvBufToFlatBuffer(pRcvBuf,
  920. DataLength + HeaderLength);
  921. if (!pBuff) {
  922. IPSInfo.ipsi_indiscards++;
  923. IPFreeBuff(pRcvBuf);
  924. return;
  925. }
  926. }
  927. IPForwardPkt(SrcNTE, (IPHeader UNALIGNED *) pBuff,
  928. HeaderLength, pBuff + HeaderLength, DataLength,
  929. NULL, 0, DestType, 0, NULL, NULL, LinkCtxt);
  930. if (!OneChunk) {
  931. CTEFreeMem(pBuff); // free the flat buffer
  932. }
  933. }
  934. IPFreeBuff(pRcvBuf);
  935. return;
  936. forward_remote:
  937. OneChunk = FALSE;
  938. if (pRcvBuf->ipr_next == NULL) {
  939. OneChunk = TRUE;
  940. pBuff = pRcvBuf->ipr_buffer;
  941. } else {
  942. pBuff = ConvertIPRcvBufToFlatBuffer(pRcvBuf,
  943. DataLength + HeaderLength);
  944. if (!pBuff) {
  945. IPSInfo.ipsi_indiscards++;
  946. IPFreeBuff(pRcvBuf);
  947. return;
  948. }
  949. }
  950. IPForwardPkt(SrcNTE, (IPHeader UNALIGNED *) pBuff, HeaderLength,
  951. pBuff + HeaderLength, DataLength, NULL, 0,
  952. DestType, 0, NULL, NULL, LinkCtxt);
  953. IPFreeBuff(pRcvBuf);
  954. if (!OneChunk) {
  955. CTEFreeMem(pBuff); // free the flat buffer
  956. }
  957. return;
  958. } else { // No Firewall
  959. if (PromiscuousMode) {
  960. DeliverToUser(SrcNTE, DestNTE, (IPHeader UNALIGNED *) Header,
  961. HeaderLength, Data, DataLength, OptInfo, NULL,
  962. DestType);
  963. }
  964. // Convert IPRcvBuf chain to a flat buffer
  965. OneChunk = FALSE;
  966. if (Data != NULL && !Data->ipr_next) {
  967. OneChunk = TRUE;
  968. pBuff = Data->ipr_buffer;
  969. } else {
  970. pBuff = ConvertIPRcvBufToFlatBuffer(
  971. Data, DataLength + HeaderLength);
  972. if (!pBuff) {
  973. IPSInfo.ipsi_indiscards++;
  974. return;
  975. }
  976. }
  977. IPForwardPkt(SrcNTE, (IPHeader UNALIGNED *) Header, HeaderLength,
  978. pBuff, DataLength, NULL, 0, DestType, 0, NULL, NULL,
  979. LinkCtxt);
  980. if (!OneChunk) CTEFreeMem(pBuff);
  981. }
  982. return;
  983. } // DestType >= DEST_REMOTE
  984. ASSERT(DestType <= DEST_REMOTE);
  985. // Call IPSEC -> Filter -> Firewall
  986. // These are local packets only.
  987. if (FirewallMode) { // Header is part of the Data
  988. FORWARD_ACTION Action = FORWARD;
  989. ACTION_E SecondAction;
  990. FIREWALL_CONTEXT_T FrCtx;
  991. IPAddr DAddr = Header->iph_dest;
  992. IPRcvBuf *pRcvBuf = Data;
  993. IPRcvBuf *pOutRcvBuf = NULL;
  994. NetTableEntry *DstNTE;
  995. Queue *CurrQ;
  996. FIREWALL_HOOK *CurrHook;
  997. uint DestIFIndex = LOCAL_IF_INDEX;
  998. uchar DestinationType = DestType;
  999. uint BufferChanged = 0;
  1000. KIRQL OldIrql;
  1001. ULONG ipsecFlags = IPSEC_FLAG_INCOMING;
  1002. if (pRcvBuf->ipr_size > HeaderLength) { //1st buffer contains data also
  1003. FastPath = 1;
  1004. } else {
  1005. FastPath = 0;
  1006. if (pRcvBuf->ipr_next == NULL) {
  1007. // we received the data s.t. datasize == headersize
  1008. IPSInfo.ipsi_indiscards++;
  1009. IPFreeBuff(pRcvBuf);
  1010. return;
  1011. }
  1012. }
  1013. //
  1014. // Call into IPSEC so he can decrypt the data
  1015. //
  1016. // In case of firewall make sure we pass the data only but we don't actually strip the header
  1017. if (IPSecHandlerPtr) {
  1018. //
  1019. // See if IPSEC is enabled, see if it needs to do anything with this
  1020. // packet.
  1021. //
  1022. FORWARD_ACTION Action;
  1023. ULONG ipsecByteCount = 0;
  1024. ULONG ipsecMTU = 0;
  1025. PNDIS_BUFFER newBuf = NULL;
  1026. if (SrcNTE == LoopNTE) {
  1027. ipsecFlags |= IPSEC_FLAG_LOOPBACK;
  1028. }
  1029. if (OptInfo->ioi_flags & IP_FLAG_SSRR) {
  1030. ipsecFlags |= IPSEC_FLAG_SSRR;
  1031. }
  1032. if (FastPath) {
  1033. // first buffer contains IPHeader also
  1034. pRcvBuf->ipr_buffer += HeaderLength;
  1035. pRcvBuf->ipr_size -= HeaderLength;
  1036. // this tells IPSEC to move IPHeader after decryption
  1037. ipsecFlags |= IPSEC_FLAG_FASTRCV;
  1038. Action = (*IPSecHandlerPtr) (
  1039. (PUCHAR) Header,
  1040. (PVOID) pRcvBuf,
  1041. SrcNTE->nte_if, // SrcIF
  1042. Packet,
  1043. &ipsecByteCount,
  1044. &ipsecMTU,
  1045. (PVOID *) & newBuf,
  1046. &ipsecFlags,
  1047. DestType);
  1048. // restore the buffer
  1049. pRcvBuf->ipr_buffer -= HeaderLength;
  1050. pRcvBuf->ipr_size += HeaderLength;
  1051. Header = (IPHeader UNALIGNED *)pRcvBuf->ipr_buffer;
  1052. } else { // FastPath = 0
  1053. Action = (*IPSecHandlerPtr) (
  1054. (PUCHAR) Header,
  1055. (PVOID) (pRcvBuf->ipr_next),
  1056. SrcNTE->nte_if, // SrcIF
  1057. Packet,
  1058. &ipsecByteCount,
  1059. &ipsecMTU,
  1060. (PVOID *) & newBuf,
  1061. &ipsecFlags,
  1062. DestType);
  1063. }
  1064. if (Action != eFORWARD) {
  1065. IPSInfo.ipsi_indiscards++;
  1066. IPFreeBuff(pRcvBuf);
  1067. return;
  1068. } else {
  1069. //
  1070. // Update the data length if IPSEC changed it (like by removing the AH)
  1071. //
  1072. DataLength -= ipsecByteCount;
  1073. UpdateIPSecRcvBuf(pRcvBuf, ipsecFlags);
  1074. }
  1075. }
  1076. // If ipsec acted on this, mark ipr_flags for
  1077. // filter driver.
  1078. if (ipsecFlags & IPSEC_FLAG_TRANSFORMED) {
  1079. pRcvBuf->ipr_flags |= IPR_FLAG_IPSEC_TRANSFORMED;
  1080. }
  1081. // Call the filter hook if installed
  1082. if (ForwardFilterEnabled) {
  1083. FORWARD_ACTION Action = FORWARD;
  1084. if (FastPath) {
  1085. Interface *IF = SrcNTE->nte_if;
  1086. IPAddr LinkNextHop;
  1087. if ((IF->if_flags & IF_FLAGS_P2MP) && LinkCtxt) {
  1088. LinkNextHop = LinkCtxt->link_NextHop;
  1089. } else {
  1090. LinkNextHop = NULL_IP_ADDR;
  1091. }
  1092. CTEInterlockedIncrementLong(&ForwardFilterRefCount);
  1093. Action = (*ForwardFilterPtr) (
  1094. Header,
  1095. pRcvBuf->ipr_buffer + HeaderLength,
  1096. pRcvBuf->ipr_size - HeaderLength,
  1097. IF->if_index,
  1098. INVALID_IF_INDEX,
  1099. LinkNextHop,
  1100. NULL_IP_ADDR);
  1101. DerefFilterPtr();
  1102. } else { // Fast Path = 0
  1103. Interface *IF = SrcNTE->nte_if;
  1104. IPAddr LinkNextHop;
  1105. if ((IF->if_flags & IF_FLAGS_P2MP) && LinkCtxt) {
  1106. LinkNextHop = LinkCtxt->link_NextHop;
  1107. } else {
  1108. LinkNextHop = NULL_IP_ADDR;
  1109. }
  1110. CTEInterlockedIncrementLong(&ForwardFilterRefCount);
  1111. Action = (*ForwardFilterPtr) (Header,
  1112. pRcvBuf->ipr_next->ipr_buffer,
  1113. pRcvBuf->ipr_next->ipr_size,
  1114. IF->if_index,
  1115. INVALID_IF_INDEX,
  1116. LinkNextHop,
  1117. NULL_IP_ADDR);
  1118. DerefFilterPtr();
  1119. }
  1120. if (Action != FORWARD) {
  1121. IPSInfo.ipsi_indiscards++;
  1122. IPFreeBuff(pRcvBuf);
  1123. return;
  1124. }
  1125. }
  1126. // Call the firewall hook
  1127. FrCtx.Direction = IP_RECEIVE;
  1128. FrCtx.NTE = SrcNTE; //NTE the dg arrived on
  1129. FrCtx.LinkCtxt = LinkCtxt;
  1130. // call the firewall hooks from front of the Queue
  1131. #if MILLEN
  1132. KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
  1133. #else // MILLEN
  1134. ASSERT(KeGetCurrentIrql() >= DISPATCH_LEVEL);
  1135. #endif // MILLEN
  1136. FirewallRef = RefFirewallQ(&FirewallQ);
  1137. CurrQ = QHEAD(FirewallQ);
  1138. while (CurrQ != QEND(FirewallQ)) {
  1139. CurrHook = QSTRUCT(FIREWALL_HOOK, CurrQ, hook_q);
  1140. pOutRcvBuf = NULL;
  1141. Action = (*CurrHook->hook_Ptr) (&pRcvBuf,
  1142. SrcNTE->nte_if->if_index,
  1143. &DestIFIndex,
  1144. &DestinationType,
  1145. &FrCtx,
  1146. sizeof(FrCtx),
  1147. &pOutRcvBuf);
  1148. if (Action == DROP) {
  1149. DerefFirewallQ(FirewallRef);
  1150. #if MILLEN
  1151. KeLowerIrql(OldIrql);
  1152. #endif // MILLEN
  1153. IPSInfo.ipsi_indiscards++;
  1154. if (pRcvBuf != NULL) {
  1155. IPFreeBuff(pRcvBuf);
  1156. }
  1157. if (pOutRcvBuf != NULL) {
  1158. IPFreeBuff(pOutRcvBuf);
  1159. }
  1160. return;
  1161. } else {
  1162. ASSERT(Action == FORWARD);
  1163. if (pOutRcvBuf != NULL) {
  1164. // free the old buffer
  1165. if (pRcvBuf != NULL) {
  1166. IPFreeBuff(pRcvBuf);
  1167. }
  1168. pRcvBuf = pOutRcvBuf;
  1169. BufferChanged = 1;
  1170. }
  1171. }
  1172. CurrQ = QNEXT(CurrQ);
  1173. }
  1174. DerefFirewallQ(FirewallRef);
  1175. #if MILLEN
  1176. KeLowerIrql(OldIrql);
  1177. #endif // MILLEN
  1178. ASSERT(Action == FORWARD);
  1179. if (BufferChanged) {
  1180. // if packet touched compute the new length: DataSize
  1181. DataLength = 0;
  1182. tmpRcvBuf = pRcvBuf;
  1183. while (tmpRcvBuf != NULL) {
  1184. ASSERT(tmpRcvBuf->ipr_buffer != NULL);
  1185. DataLength += tmpRcvBuf->ipr_size;
  1186. tmpRcvBuf = tmpRcvBuf->ipr_next;
  1187. }
  1188. // also make Header point to new buffer
  1189. Header = (IPHeader *) pRcvBuf->ipr_buffer;
  1190. HeaderLength = (Header->iph_verlen & 0xf) << 2;
  1191. }
  1192. DataLength -= HeaderLength; // decrement the header length
  1193. if (DestinationType == DEST_INVALID) { // Dest Addr changed by hook
  1194. // Can IPSEC changed iph_dest ???
  1195. DAddr = Header->iph_dest;
  1196. DstNTE = SrcNTE;
  1197. DestType = GetLocalNTE(DAddr, &DstNTE);
  1198. DestNTE = DstNTE;
  1199. }
  1200. if (DestType < DEST_REMOTE) {
  1201. // Check to see options
  1202. if (HeaderLength != sizeof(IPHeader)) {
  1203. // We have options
  1204. uchar NewDType;
  1205. NewDType = CheckLocalOptions(SrcNTE,
  1206. (IPHeader UNALIGNED *) Header,
  1207. OptInfo,
  1208. DestType,
  1209. NULL,
  1210. 0,
  1211. FALSE);
  1212. if (NewDType != DEST_LOCAL) {
  1213. if (NewDType == DEST_REMOTE) {
  1214. if (PromiscuousMode) {
  1215. if (FastPath) {
  1216. uchar *saveddata = pRcvBuf->ipr_buffer;
  1217. pRcvBuf->ipr_buffer += HeaderLength;
  1218. pRcvBuf->ipr_size -= HeaderLength;
  1219. DeliverToUser(SrcNTE, DestNTE,
  1220. (IPHeader UNALIGNED *) Header,
  1221. HeaderLength, pRcvBuf,
  1222. DataLength, OptInfo, NULL,
  1223. DestType);
  1224. // restore the buffer
  1225. pRcvBuf->ipr_buffer = saveddata;
  1226. pRcvBuf->ipr_size += HeaderLength;
  1227. } else {
  1228. uchar *saveddata = pRcvBuf->ipr_next->ipr_buffer;
  1229. DeliverToUser(SrcNTE, DestNTE,
  1230. (IPHeader UNALIGNED *) Header,
  1231. HeaderLength, pRcvBuf->ipr_next,
  1232. DataLength, OptInfo, NULL,
  1233. DestType);
  1234. // restore the buffers;
  1235. pRcvBuf->ipr_next->ipr_buffer = saveddata;
  1236. }
  1237. }
  1238. goto forward_local;
  1239. } else {
  1240. IPSInfo.ipsi_inhdrerrors++;
  1241. IPFreeBuff(pRcvBuf);
  1242. //CTEFreeMem(pBuff);
  1243. return; // Bad Options
  1244. }
  1245. } // NewDtype != LOCAL
  1246. } // Options present
  1247. } // DestType < DEST_REMOTE
  1248. else { // DestType >=DEST_REMOTE
  1249. if (PromiscuousMode) {
  1250. if (FastPath) {
  1251. uchar *saveddata = pRcvBuf->ipr_buffer;
  1252. pRcvBuf->ipr_buffer += HeaderLength;
  1253. pRcvBuf->ipr_size -= HeaderLength;
  1254. DeliverToUser(SrcNTE, DestNTE,
  1255. (IPHeader UNALIGNED *) Header, HeaderLength,
  1256. pRcvBuf, DataLength, OptInfo, NULL, DestType);
  1257. // restore the buffer
  1258. pRcvBuf->ipr_buffer = saveddata;
  1259. pRcvBuf->ipr_size += HeaderLength;
  1260. } else {
  1261. uchar *saveddata = pRcvBuf->ipr_next->ipr_buffer;
  1262. DeliverToUser(SrcNTE, DestNTE,
  1263. (IPHeader UNALIGNED *) Header, HeaderLength,
  1264. pRcvBuf->ipr_next, DataLength, OptInfo,
  1265. NULL, DestType);
  1266. // restore the buffers;
  1267. pRcvBuf->ipr_next->ipr_buffer = saveddata;
  1268. }
  1269. }
  1270. goto forward_local;
  1271. }
  1272. if (FastPath) {
  1273. uchar *saveddata = pRcvBuf->ipr_buffer;
  1274. pRcvBuf->ipr_buffer += HeaderLength;
  1275. pRcvBuf->ipr_size -= HeaderLength;
  1276. DeliverToUser(SrcNTE, DestNTE, (IPHeader UNALIGNED *) Header, HeaderLength,
  1277. pRcvBuf, DataLength, OptInfo, NULL, DestType);
  1278. // restore the buffer
  1279. pRcvBuf->ipr_buffer = saveddata;
  1280. pRcvBuf->ipr_size += HeaderLength;
  1281. } else {
  1282. uchar *saveddata = pRcvBuf->ipr_next->ipr_buffer;
  1283. DeliverToUser(
  1284. SrcNTE, DestNTE, (IPHeader UNALIGNED *) Header,
  1285. HeaderLength, pRcvBuf->ipr_next, DataLength,
  1286. OptInfo, NULL, DestType);
  1287. // restore the buffers;
  1288. pRcvBuf->ipr_next->ipr_buffer = saveddata;
  1289. }
  1290. if (IS_BCAST_DEST(DestType)) {
  1291. pBuff = ConvertIPRcvBufToFlatBuffer(
  1292. pRcvBuf, DataLength + HeaderLength);
  1293. if (!pBuff) {
  1294. IPSInfo.ipsi_indiscards++;
  1295. IPFreeBuff(pRcvBuf);
  1296. return;
  1297. }
  1298. IPForwardPkt(SrcNTE, (IPHeader UNALIGNED *) pBuff,
  1299. HeaderLength, pBuff + HeaderLength,
  1300. DataLength, NULL, 0, DestType, 0, NULL,
  1301. NULL, LinkCtxt);
  1302. CTEFreeMem(pBuff); // free the flat buffer
  1303. }
  1304. IPFreeBuff(pRcvBuf);
  1305. //CTEFreeMem(pBuff); // free the flat buffer
  1306. return;
  1307. forward_local:
  1308. pBuff = ConvertIPRcvBufToFlatBuffer(pRcvBuf, DataLength + HeaderLength);
  1309. if (!pBuff) {
  1310. IPSInfo.ipsi_indiscards++;
  1311. IPFreeBuff(pRcvBuf);
  1312. return;
  1313. }
  1314. IPForwardPkt(SrcNTE, (IPHeader UNALIGNED *) pBuff, HeaderLength,
  1315. pBuff + HeaderLength, DataLength, NULL, 0, DestType,
  1316. 0, NULL, NULL, LinkCtxt);
  1317. IPFreeBuff(pRcvBuf);
  1318. CTEFreeMem(pBuff); // free the flat buffer
  1319. return;
  1320. } else { // No Firewall
  1321. //
  1322. // Call into IPSEC so he can decrypt the data
  1323. //
  1324. if (IPSecHandlerPtr) {
  1325. //
  1326. // See if IPSEC is enabled, see if it needs to do anything with this
  1327. // packet.
  1328. //
  1329. FORWARD_ACTION Action;
  1330. ULONG ipsecByteCount = 0;
  1331. ULONG ipsecMTU = 0;
  1332. ULONG ipsecFlags = IPSEC_FLAG_INCOMING;
  1333. PNDIS_BUFFER newBuf = NULL;
  1334. ulong csum;
  1335. IPHeader *IPH;
  1336. if (SrcNTE == LoopNTE) {
  1337. ipsecFlags |= IPSEC_FLAG_LOOPBACK;
  1338. }
  1339. if (OptInfo->ioi_flags & IP_FLAG_SSRR) {
  1340. ipsecFlags |= IPSEC_FLAG_SSRR;
  1341. }
  1342. Action = (*IPSecHandlerPtr) (
  1343. (PUCHAR) Header,
  1344. (PVOID) Data,
  1345. SrcNTE->nte_if, // SrcIF
  1346. Packet,
  1347. &ipsecByteCount,
  1348. &ipsecMTU,
  1349. (PVOID *) &newBuf,
  1350. &ipsecFlags,
  1351. DestType);
  1352. if (Action != eFORWARD) {
  1353. IPSInfo.ipsi_indiscards++;
  1354. return;
  1355. } else {
  1356. //
  1357. // Update the data length if IPSEC changed it
  1358. // (like by removing the AH)
  1359. //
  1360. DataLength -= ipsecByteCount;
  1361. UpdateIPSecRcvBuf(Data, ipsecFlags);
  1362. }
  1363. }
  1364. // Call the filter hook if installed
  1365. if (ForwardFilterEnabled) {
  1366. Interface *IF = SrcNTE->nte_if;
  1367. IPAddr LinkNextHop;
  1368. FORWARD_ACTION Action;
  1369. if ((IF->if_flags & IF_FLAGS_P2MP) && LinkCtxt) {
  1370. LinkNextHop = LinkCtxt->link_NextHop;
  1371. } else {
  1372. LinkNextHop = NULL_IP_ADDR;
  1373. }
  1374. CTEInterlockedIncrementLong(&ForwardFilterRefCount);
  1375. Action = (*ForwardFilterPtr) (Header,
  1376. Data->ipr_buffer,
  1377. Data->ipr_size,
  1378. IF->if_index,
  1379. INVALID_IF_INDEX,
  1380. LinkNextHop,
  1381. NULL_IP_ADDR);
  1382. DerefFilterPtr();
  1383. if (Action != FORWARD) {
  1384. IPSInfo.ipsi_indiscards++;
  1385. return;
  1386. }
  1387. }
  1388. // Packet was local only: so even if promiscuous mode set just call
  1389. // delivertouser
  1390. DeliverToUser(SrcNTE, DestNTE, (IPHeader UNALIGNED *) Header,
  1391. HeaderLength, Data, DataLength, OptInfo, NULL, DestType);
  1392. if (IS_BCAST_DEST(DestType)) {
  1393. uchar *pBuff;
  1394. pBuff = ConvertIPRcvBufToFlatBuffer(Data, DataLength);
  1395. if (!pBuff) {
  1396. return;
  1397. }
  1398. IPForwardPkt(SrcNTE, (IPHeader UNALIGNED *) Header, HeaderLength,
  1399. pBuff, DataLength, NULL, 0, DestType, 0, NULL, NULL,
  1400. LinkCtxt);
  1401. CTEFreeMem(pBuff);
  1402. }
  1403. }
  1404. }
  1405. //* FreeRH - Free a reassembly header.
  1406. //
  1407. // Called when we need to free a reassembly header, either because of a
  1408. // timeout or because we're done with it.
  1409. //
  1410. // Input: RH - RH to be freed.
  1411. //
  1412. // Returns: Nothing.
  1413. //
  1414. void
  1415. FreeRH(ReassemblyHeader *RH)
  1416. {
  1417. RABufDesc *RBD, *TempRBD;
  1418. RBD = RH->rh_rbd;
  1419. if (IPSecHandlerPtr) {
  1420. IPFreeBuff((IPRcvBuf *) RBD);
  1421. } else {
  1422. while (RBD != NULL) {
  1423. TempRBD = RBD;
  1424. RBD = (RABufDesc *) RBD->rbd_buf.ipr_next;
  1425. CTEFreeMem(TempRBD);
  1426. }
  1427. }
  1428. CTEFreeMem(RH);
  1429. // decrement NumRH
  1430. CTEInterlockedDecrementLong(&NumRH);
  1431. }
  1432. //* ReassembleFragment - Put a fragment into the reassembly list.
  1433. //
  1434. // This routine is called once we've put a fragment into the proper buffer.
  1435. // We look for a reassembly header for the fragment. If we don't find one,
  1436. // we create one. Otherwise we search the reassembly list, and insert the
  1437. // datagram in it's proper place.
  1438. //
  1439. // Input: NTE - NTE to reassemble on.
  1440. // SrcNTE - NTE datagram arrived on.
  1441. // NewRBD - New RBD to be inserted.
  1442. // IPH - Pointer to header of datagram.
  1443. // HeaderSize - Size in bytes of header.
  1444. // DestType - Type of destination address.
  1445. //
  1446. // Returns: Nothing.
  1447. //
  1448. void
  1449. ReassembleFragment(NetTableEntry * NTE, NetTableEntry * SrcNTE, RABufDesc * NewRBD,
  1450. IPHeader UNALIGNED * IPH, uint HeaderSize, uchar DestType, LinkEntry * LinkCtxt)
  1451. {
  1452. CTELockHandle NTEHandle; // Lock handle used for NTE
  1453. ReassemblyHeader *RH, *PrevRH; // Current and previous reassembly headers.
  1454. RABufDesc *PrevRBD; // Previous RBD in reassembly header list.
  1455. RABufDesc *CurrentRBD;
  1456. ushort DataLength = (ushort) NewRBD->rbd_buf.ipr_size, DataOffset;
  1457. ushort Offset; // Offset of this fragment.
  1458. ushort NewOffset; // Offset we'll copy from after checking RBD list.
  1459. ushort NewEnd; // End offset of fragment, after trimming (if any).
  1460. // used by the firewall code
  1461. char *pBuff;
  1462. IPRcvBuf *pOutRcvBuf;
  1463. NetTableEntry *DestNTE;
  1464. IPRcvBuf *pRcvBuf;
  1465. uint FirewallMode;
  1466. uint PromiscuousMode;
  1467. PromiscuousMode = SrcNTE->nte_if->if_promiscuousmode;
  1468. FirewallMode = ProcessFirewallQ();
  1469. // If this is a broadcast, go ahead and forward it now.
  1470. // if second condition is false then delivertouserex() will take care of
  1471. // this
  1472. if (IS_BCAST_DEST(DestType) &&
  1473. !(((IPSecHandlerPtr) && (ForwardFilterEnabled)) ||
  1474. (FirewallMode) || (PromiscuousMode))) {
  1475. IPForwardPkt(SrcNTE, IPH, HeaderSize, NewRBD->rbd_buf.ipr_buffer,
  1476. NewRBD->rbd_buf.ipr_size, NULL, 0, DestType, 0, NULL,
  1477. NULL, LinkCtxt);
  1478. }
  1479. if (NumRH > MaxRH) {
  1480. IPSInfo.ipsi_reasmfails++;
  1481. FragmentAttackDrops++;
  1482. CTEFreeMem(NewRBD);
  1483. return;
  1484. }
  1485. Offset = IPH->iph_offset & IP_OFFSET_MASK;
  1486. Offset = net_short(Offset) * 8;
  1487. if ((NumRH == MaxRH) && !Offset) {
  1488. IPSInfo.ipsi_reasmfails++;
  1489. CTEFreeMem(NewRBD);
  1490. return;
  1491. }
  1492. if ((ulong) (Offset + DataLength) > MAX_DATA_LENGTH) {
  1493. IPSInfo.ipsi_reasmfails++;
  1494. CTEFreeMem(NewRBD);
  1495. return;
  1496. }
  1497. // We've got the buffer we need. Now get the reassembly header, if there is one. If
  1498. // there isn't, create one.
  1499. CTEGetLockAtDPC(&NTE->nte_lock, &NTEHandle);
  1500. RH = FindRH(&PrevRH, NTE, IPH->iph_dest, IPH->iph_src, IPH->iph_id,
  1501. IPH->iph_protocol);
  1502. if (RH == (ReassemblyHeader *) NULL) { // Didn't find one, so create one.
  1503. ReassemblyHeader *NewRH;
  1504. CTEFreeLockFromDPC(&NTE->nte_lock, NTEHandle);
  1505. RH = CTEAllocMemN(sizeof(ReassemblyHeader), 'diCT');
  1506. if (RH == (ReassemblyHeader *) NULL) { // Couldn't get a buffer.
  1507. IPSInfo.ipsi_reasmfails++;
  1508. CTEFreeMem(NewRBD);
  1509. return;
  1510. }
  1511. CTEInterlockedIncrementLong(&NumRH);
  1512. CTEGetLockAtDPC(&NTE->nte_lock, &NTEHandle);
  1513. // Need to look it up again - it could have changed during above call.
  1514. NewRH = FindRH(&PrevRH, NTE, IPH->iph_dest, IPH->iph_src, IPH->iph_id, IPH->iph_protocol);
  1515. if (NewRH != (ReassemblyHeader *) NULL) {
  1516. CTEFreeMem(RH);
  1517. RH = NewRH;
  1518. CTEInterlockedDecrementLong(&NumRH);
  1519. } else {
  1520. RH->rh_next = PrevRH->rh_next;
  1521. PrevRH->rh_next = RH;
  1522. // Initialize our new reassembly header.
  1523. RH->rh_dest = IPH->iph_dest;
  1524. RH->rh_src = IPH->iph_src;
  1525. RH->rh_id = IPH->iph_id;
  1526. RH->rh_protocol = IPH->iph_protocol;
  1527. //RH->rh_ttl = RATimeout;
  1528. RH->rh_ttl = MAX(RATimeout, MIN(120, IPH->iph_ttl) + 1);
  1529. RH->rh_numoverlaps = 0;
  1530. RH->rh_datasize = MAX_TOTAL_LENGTH; // Default datasize to maximum.
  1531. RH->rh_rbd = (RABufDesc *) NULL; // And nothing on chain.
  1532. RH->rh_datarcvd = 0; // Haven't received any data yet.
  1533. RH->rh_headersize = 0;
  1534. }
  1535. }
  1536. // When we reach here RH points to the reassembly header we want to use.
  1537. // and we hold locks on the NTE and the RH. If this is the first fragment
  1538. // we'll save the options and header information here.
  1539. if (Offset == 0) { // First fragment.
  1540. RH->rh_headersize = (ushort)HeaderSize;
  1541. RtlCopyMemory(RH->rh_header, IPH, HeaderSize + 8);
  1542. }
  1543. // If this is the last fragment, update the amount of data we expect to
  1544. // receive.
  1545. if (!(IPH->iph_offset & IP_MF_FLAG)) {
  1546. RH->rh_datasize = Offset + DataLength;
  1547. }
  1548. if (RH->rh_datasize < RH->rh_datarcvd ||
  1549. (RH->rh_datasize != MAX_TOTAL_LENGTH &&
  1550. (RH->rh_datasize + RH->rh_headersize) > MAX_TOTAL_LENGTH)) {
  1551. // random packets. drop!
  1552. CTEFreeMem(NewRBD);
  1553. PrevRH->rh_next = RH->rh_next;
  1554. FreeRH(RH);
  1555. CTEFreeLockFromDPC(&NTE->nte_lock, NTEHandle);
  1556. return;
  1557. }
  1558. // Update the TTL value with the maximum of the current TTL and the
  1559. // incoming TTL (+1, to deal with rounding errors).
  1560. // Following is commented out to protect against fragmentation attack
  1561. // Default TTL now used is 120 seconds now, used only for the first header
  1562. // RH->rh_ttl = MAX(RH->rh_ttl, MIN(254, IPH->iph_ttl) + 1);
  1563. // Now we need to see where in the RBD list to put this.
  1564. //
  1565. // The idea is to go through the list of RBDs one at a time. The RBD
  1566. // currently being examined is CurrentRBD. If the start offset of the new
  1567. // fragment is less than (i.e. in front of) the offset of CurrentRBD, we
  1568. // need to insert the NewRBD in front of the CurrentRBD. If this is the
  1569. // case we need to check and see if the
  1570. // end of the new fragment overlaps some or all of the fragment described by
  1571. // CurrentRBD, and possibly subsequent fragment. If it overlaps part of a
  1572. // fragment we'll adjust our end down to be in front of the existing
  1573. // fragment. If it overlaps all of the fragment we'll free the old fragment.
  1574. //
  1575. // If the new fragment does not start in front of the current fragment
  1576. // we'll check to see if it starts somewhere in the middle of the current
  1577. // fragment. If this isn't the case, we move on the the next fragment. If
  1578. // this is the case, we check to see if the current fragment completely // covers the new fragment. If not we
  1579. // move our start up and continue with the next fragment.
  1580. //
  1581. NewOffset = Offset;
  1582. NewEnd = Offset + DataLength - 1;
  1583. PrevRBD = STRUCT_OF(RABufDesc, STRUCT_OF(IPRcvBuf, &RH->rh_rbd, ipr_next), rbd_buf);
  1584. CurrentRBD = RH->rh_rbd;
  1585. for (; CurrentRBD != NULL; PrevRBD = CurrentRBD, CurrentRBD = (RABufDesc *) CurrentRBD->rbd_buf.ipr_next) {
  1586. // See if it starts in front of this fragment.
  1587. if (NewOffset < CurrentRBD->rbd_start) {
  1588. // It does start in front. Check to see if there's any overlap.
  1589. if (NewEnd < CurrentRBD->rbd_start)
  1590. break; // No overlap, so get out.
  1591. else {
  1592. //
  1593. // It does overlap. While we have overlap, walk down the list
  1594. // looking for RBDs we overlap completely. If we find one,
  1595. // put it on our deletion list. If we have overlap but not
  1596. // complete overlap, move our end down if front of the
  1597. // fragment we overlap.
  1598. //
  1599. do {
  1600. RH->rh_numoverlaps++;
  1601. if (RH->rh_numoverlaps >= MaxOverlap) {
  1602. //Looks like we are being attacked.
  1603. //Just drop this whole datagram.
  1604. NewRBD->rbd_buf.ipr_next = (IPRcvBuf *) CurrentRBD;
  1605. PrevRBD->rbd_buf.ipr_next = &NewRBD->rbd_buf;
  1606. PrevRH->rh_next = RH->rh_next;
  1607. FreeRH(RH);
  1608. FragmentAttackDrops++;
  1609. CTEFreeLockFromDPC(&NTE->nte_lock, NTEHandle);
  1610. return;
  1611. }
  1612. if (NewEnd > CurrentRBD->rbd_end) { //overlaps completely.
  1613. RABufDesc *TempRBD;
  1614. RH->rh_datarcvd -= (ushort)CurrentRBD->rbd_buf.ipr_size;
  1615. TempRBD = CurrentRBD;
  1616. CurrentRBD = (RABufDesc *) CurrentRBD->rbd_buf.ipr_next;
  1617. CTEFreeMem(TempRBD);
  1618. } else { //partial ovelap.
  1619. if (NewOffset < CurrentRBD->rbd_start) {
  1620. NewEnd = CurrentRBD->rbd_start - 1;
  1621. } else {
  1622. // Looks like we are being attacked.
  1623. // Just drop this whole datagram.
  1624. NewRBD->rbd_buf.ipr_next = (IPRcvBuf *) CurrentRBD;
  1625. PrevRBD->rbd_buf.ipr_next = &NewRBD->rbd_buf;
  1626. PrevRH->rh_next = RH->rh_next;
  1627. FreeRH(RH);
  1628. CTEFreeLockFromDPC(&NTE->nte_lock, NTEHandle);
  1629. return;
  1630. }
  1631. }
  1632. // Update of NewEnd will force us out of loop.
  1633. } while (CurrentRBD != NULL && NewEnd >= CurrentRBD->rbd_start);
  1634. break;
  1635. }
  1636. } else {
  1637. // This fragment doesn't go in front of the current RBD. See if it
  1638. // is entirely beyond the end of the current fragment. If it is,
  1639. // just continue. Otherwise see if the current fragment
  1640. // completely subsumes us. If it does, get out, otherwise update
  1641. // our start offset and continue.
  1642. if (NewOffset > CurrentRBD->rbd_end)
  1643. continue; // No overlap at all.
  1644. else {
  1645. RH->rh_numoverlaps++;
  1646. if (RH->rh_numoverlaps >= MaxOverlap) {
  1647. //Looks like we are being attacked.
  1648. //Just drop this whole datagram.
  1649. NewRBD->rbd_buf.ipr_next = (IPRcvBuf *) CurrentRBD;
  1650. PrevRBD->rbd_buf.ipr_next = &NewRBD->rbd_buf;
  1651. PrevRH->rh_next = RH->rh_next;
  1652. FreeRH(RH);
  1653. FragmentAttackDrops++;
  1654. CTEFreeLockFromDPC(&NTE->nte_lock, NTEHandle);
  1655. return;
  1656. }
  1657. if (NewEnd <= CurrentRBD->rbd_end) {
  1658. //
  1659. // The current fragment overlaps the new fragment
  1660. // totally. Set our offsets so that we'll skip the copy
  1661. // below.
  1662. NewEnd = NewOffset - 1;
  1663. break;
  1664. } else // Only partial overlap.
  1665. NewOffset = CurrentRBD->rbd_end + 1;
  1666. }
  1667. }
  1668. } // End of for loop.
  1669. // Adjust the length and offset fields in the new RBD.
  1670. // If we've trimmed all the data away, ignore this fragment.
  1671. DataLength = NewEnd - NewOffset + 1;
  1672. DataOffset = NewOffset - Offset;
  1673. if (!DataLength) {
  1674. CTEFreeMem(NewRBD);
  1675. CTEFreeLockFromDPC(&NTE->nte_lock, NTEHandle);
  1676. return;
  1677. }
  1678. // Link him in chain.
  1679. NewRBD->rbd_buf.ipr_size = (uint) DataLength;
  1680. NewRBD->rbd_end = NewEnd;
  1681. NewRBD->rbd_start = NewOffset;
  1682. RH->rh_datarcvd += DataLength;
  1683. NewRBD->rbd_buf.ipr_buffer += DataOffset;
  1684. NewRBD->rbd_buf.ipr_next = (IPRcvBuf *) CurrentRBD;
  1685. NewRBD->rbd_buf.ipr_owner = IPR_OWNER_IP;
  1686. PrevRBD->rbd_buf.ipr_next = &NewRBD->rbd_buf;
  1687. // If we've received all the data, deliver it to the user.
  1688. // Only if header size is valid deliver to the user
  1689. // BUG #NTQFE 65742
  1690. if (RH->rh_datarcvd == RH->rh_datasize && RH->rh_headersize) { // We have it all.
  1691. IPOptInfo OptInfo;
  1692. IPHeader *Header;
  1693. IPRcvBuf *FirstBuf;
  1694. ulong Checksum;
  1695. PrevRH->rh_next = RH->rh_next;
  1696. CTEFreeLockFromDPC(&NTE->nte_lock, NTEHandle);
  1697. Header = (IPHeader *) RH->rh_header;
  1698. OptInfo.ioi_ttl = Header->iph_ttl;
  1699. OptInfo.ioi_tos = Header->iph_tos;
  1700. OptInfo.ioi_flags = 0; // Flags must be 0 - DF can't be set,
  1701. // this was reassembled.
  1702. if (RH->rh_headersize != sizeof(IPHeader)) { // We had options.
  1703. OptInfo.ioi_options = (uchar *) (Header + 1);
  1704. OptInfo.ioi_optlength = (uchar) (RH->rh_headersize - sizeof(IPHeader));
  1705. } else {
  1706. OptInfo.ioi_options = (uchar *) NULL;
  1707. OptInfo.ioi_optlength = 0;
  1708. }
  1709. //
  1710. // update the indicated header len to the total len; earlier we passed in
  1711. // just the first fragment's length.
  1712. // also update the 'MF' bit in the flags field.
  1713. //
  1714. // in the process update the header-checksum,
  1715. // by first adding the negation of the original length and flags,
  1716. // and then adding the new length and flags.
  1717. //
  1718. // extract the original checksum
  1719. Checksum = (ushort) ~ Header->iph_xsum;
  1720. // update the header length
  1721. Checksum += (ushort) ~ Header->iph_length;
  1722. Header->iph_length = net_short(RH->rh_datasize + RH->rh_headersize);
  1723. Checksum += (ushort) Header->iph_length;
  1724. // update the 'MF' flag if set
  1725. if (Header->iph_offset & IP_MF_FLAG) {
  1726. Checksum += (ushort) ~ IP_MF_FLAG;
  1727. Header->iph_offset &= ~IP_MF_FLAG;
  1728. }
  1729. // insert the new checksum
  1730. Checksum = (ushort) Checksum + (ushort) (Checksum >> 16);
  1731. Checksum += Checksum >> 16;
  1732. Header->iph_xsum = (ushort) ~ Checksum;
  1733. // Make sure that the first buffer contains enough data.
  1734. FirstBuf = (IPRcvBuf *) RH->rh_rbd;
  1735. // Make sure that this can hold MIN_FIRST_SIZE
  1736. // Else treat it as attack
  1737. if (RH->rh_rbd->rbd_AllocSize < MIN_FIRST_SIZE) {
  1738. //Attack???
  1739. FreeRH(RH);
  1740. return;
  1741. }
  1742. while (FirstBuf->ipr_size < MIN_FIRST_SIZE) {
  1743. IPRcvBuf *NextBuf = FirstBuf->ipr_next;
  1744. uint CopyLength;
  1745. if (NextBuf == NULL)
  1746. break;
  1747. CopyLength = MIN(MIN_FIRST_SIZE - FirstBuf->ipr_size,
  1748. NextBuf->ipr_size);
  1749. RtlCopyMemory(FirstBuf->ipr_buffer + FirstBuf->ipr_size,
  1750. NextBuf->ipr_buffer, CopyLength);
  1751. FirstBuf->ipr_size += CopyLength;
  1752. NextBuf->ipr_buffer += CopyLength;
  1753. NextBuf->ipr_size -= CopyLength;
  1754. if (NextBuf->ipr_size == 0) {
  1755. FirstBuf->ipr_next = NextBuf->ipr_next;
  1756. CTEFreeMem(NextBuf);
  1757. }
  1758. }
  1759. IPSInfo.ipsi_reasmoks++;
  1760. if (((IPSecHandlerPtr) && (ForwardFilterEnabled)) ||
  1761. (FirewallMode) || (PromiscuousMode) ) {
  1762. uint DataSize;
  1763. DataSize = RH->rh_datasize;
  1764. if (FirewallMode) {
  1765. // Attach header to pass to Firewall hook
  1766. pRcvBuf = (IPRcvBuf *) CTEAllocMemN(sizeof(IPRcvBuf), 'eiCT');
  1767. if (!pRcvBuf) {
  1768. FreeRH(RH);
  1769. return;
  1770. }
  1771. pRcvBuf->ipr_buffer = (uchar *) RH->rh_header;
  1772. pRcvBuf->ipr_size = RH->rh_headersize;
  1773. pRcvBuf->ipr_owner = IPR_OWNER_IP;
  1774. pRcvBuf->ipr_next = FirstBuf;
  1775. DataSize += RH->rh_headersize;
  1776. } else {
  1777. pRcvBuf = FirstBuf;
  1778. }
  1779. DeliverToUserEx(SrcNTE, NTE, Header, RH->rh_headersize, pRcvBuf,
  1780. DataSize, &OptInfo, NULL, DestType, LinkCtxt);
  1781. if (FirewallMode) {
  1782. // RH chain is already freed.
  1783. CTEFreeMem(RH);
  1784. CTEInterlockedDecrementLong(&NumRH);
  1785. } else {
  1786. FreeRH(RH);
  1787. }
  1788. } else { // Normal Path
  1789. DeliverToUser(SrcNTE, NTE, Header, RH->rh_headersize, FirstBuf,
  1790. RH->rh_datasize, &OptInfo, NULL, DestType);
  1791. FreeRH(RH);
  1792. }
  1793. } else
  1794. CTEFreeLockFromDPC(&NTE->nte_lock, NTEHandle);
  1795. }
  1796. //* RATDComplete - Completion routing for a reassembly transfer data.
  1797. //
  1798. // This is the completion handle for TDs invoked because we are reassembling
  1799. // a fragment.
  1800. //
  1801. // Input: NetContext - Ptr to the net table entry on which we received
  1802. // this.
  1803. // Packet - Packet we received into.
  1804. // Status - Final status of copy.
  1805. // DataSize - Size in bytes of data transferred.
  1806. //
  1807. // Returns: Nothing
  1808. //
  1809. void
  1810. RATDComplete(void *NetContext, PNDIS_PACKET Packet, NDIS_STATUS Status, uint DataSize)
  1811. {
  1812. NetTableEntry *NTE = (NetTableEntry *) NetContext;
  1813. Interface *SrcIF;
  1814. TDContext *Context = (TDContext *) Packet->ProtocolReserved;
  1815. CTELockHandle Handle;
  1816. PNDIS_BUFFER Buffer;
  1817. if (Status == NDIS_STATUS_SUCCESS) {
  1818. Context->tdc_rbd->rbd_buf.ipr_size = DataSize;
  1819. ReassembleFragment(Context->tdc_nte, NTE, Context->tdc_rbd,
  1820. (IPHeader *) Context->tdc_header, Context->tdc_hlength, Context->tdc_dtype, NULL);
  1821. }
  1822. NdisUnchainBufferAtFront(Packet, &Buffer);
  1823. NdisFreeBuffer(Buffer);
  1824. Context->tdc_common.pc_flags &= ~PACKET_FLAG_RA;
  1825. SrcIF = NTE->nte_if;
  1826. CTEGetLockAtDPC(&SrcIF->if_lock, &Handle);
  1827. Context->tdc_common.pc_link = SrcIF->if_tdpacket;
  1828. SrcIF->if_tdpacket = Packet;
  1829. CTEFreeLockFromDPC(&SrcIF->if_lock, Handle);
  1830. return;
  1831. }
  1832. //* IPReassemble - Reassemble an incoming datagram.
  1833. //
  1834. // Called when we receive an incoming fragment. The first thing we do is
  1835. // get a buffer to put the fragment in. If we can't we'll exit. Then we
  1836. // copy the data, either via transfer data or directly if it all fits.
  1837. //
  1838. // Input: SrcNTE - Pointer to NTE that received the datagram.
  1839. // NTE - Pointer to NTE on which to reassemble.
  1840. // IPH - Pointer to header of packet.
  1841. // HeaderSize - Size in bytes of header.
  1842. // Data - Pointer to data part of fragment.
  1843. // BufferLengt - Length in bytes of user data available in the
  1844. // buffer.
  1845. // DataLength - Length in bytes of the (upper-layer) data.
  1846. // DestType - Type of destination
  1847. // LContext1, LContext2 - Link layer context values.
  1848. //
  1849. // Returns: Nothing.
  1850. //
  1851. void
  1852. IPReassemble(NetTableEntry * SrcNTE, NetTableEntry * NTE, IPHeader UNALIGNED * IPH,
  1853. uint HeaderSize,
  1854. uchar * Data, uint BufferLength, uint DataLength, uchar DestType, NDIS_HANDLE LContext1,
  1855. uint LContext2, LinkEntry * LinkCtxt)
  1856. {
  1857. Interface *RcvIF;
  1858. PNDIS_PACKET TDPacket; // NDIS packet used for TD.
  1859. TDContext *TDC = (TDContext *) NULL; // Transfer data context.
  1860. NDIS_STATUS Status;
  1861. PNDIS_BUFFER Buffer;
  1862. RABufDesc *NewRBD; // Pointer to new RBD to hold
  1863. // arriving fragment.
  1864. CTELockHandle Handle;
  1865. uint AllocSize;
  1866. IPSInfo.ipsi_reasmreqds++;
  1867. //Drop zero length fragments
  1868. if (DataLength == 0) {
  1869. return;
  1870. }
  1871. //
  1872. // First, get a new RBD to hold the arriving fragment. If we can't,
  1873. // then just skip the rest. The RBD has the buffer implicitly at the end
  1874. // of it. The buffer for the first fragment must be at least
  1875. // MIN_FIRST_SIZE bytes.
  1876. //
  1877. if ((IPH->iph_offset & IP_OFFSET_MASK) == 0) {
  1878. AllocSize = MAX(MIN_FIRST_SIZE, DataLength);
  1879. } else
  1880. AllocSize = DataLength;
  1881. NewRBD = CTEAllocMemN(sizeof(RABufDesc) + AllocSize, 'fiCT');
  1882. if (NewRBD != (RABufDesc *) NULL) {
  1883. NewRBD->rbd_buf.ipr_buffer = (uchar *) (NewRBD + 1);
  1884. NewRBD->rbd_buf.ipr_size = DataLength;
  1885. NewRBD->rbd_buf.ipr_owner = IPR_OWNER_IP;
  1886. NewRBD->rbd_AllocSize = AllocSize;
  1887. NewRBD->rbd_buf.ipr_pMdl = NULL;
  1888. NewRBD->rbd_buf.ipr_pClientCnt = NULL;
  1889. //
  1890. // Copy the data into the buffer. If we need to call transfer data
  1891. // do so now.
  1892. //
  1893. if (DataLength > BufferLength) { // Need to call transfer data.
  1894. NdisAllocateBuffer(&Status, &Buffer, BufferPool, NewRBD + 1, DataLength);
  1895. if (Status != NDIS_STATUS_SUCCESS) {
  1896. IPSInfo.ipsi_reasmfails++;
  1897. CTEFreeMem(NewRBD);
  1898. return;
  1899. }
  1900. // Now get a packet for transferring the frame.
  1901. RcvIF = SrcNTE->nte_if;
  1902. CTEGetLockAtDPC(&RcvIF->if_lock, &Handle);
  1903. TDPacket = RcvIF->if_tdpacket;
  1904. if (TDPacket != (PNDIS_PACKET) NULL) {
  1905. TDC = (TDContext *) TDPacket->ProtocolReserved;
  1906. RcvIF->if_tdpacket = TDC->tdc_common.pc_link;
  1907. CTEFreeLockFromDPC(&RcvIF->if_lock, Handle);
  1908. TDC->tdc_common.pc_flags |= PACKET_FLAG_RA;
  1909. TDC->tdc_nte = NTE;
  1910. TDC->tdc_dtype = DestType;
  1911. TDC->tdc_hlength = (uchar) HeaderSize;
  1912. TDC->tdc_rbd = NewRBD;
  1913. RtlCopyMemory(TDC->tdc_header, IPH, HeaderSize + 8);
  1914. NdisChainBufferAtFront(TDPacket, Buffer);
  1915. Status = (*(RcvIF->if_transfer)) (RcvIF->if_lcontext,
  1916. LContext1, LContext2, HeaderSize,
  1917. DataLength, TDPacket, &DataLength);
  1918. if (Status != NDIS_STATUS_PENDING)
  1919. RATDComplete(SrcNTE, TDPacket, Status, DataLength);
  1920. else
  1921. return;
  1922. } else { // Couldn't get a TD packet.
  1923. CTEFreeLockFromDPC(&RcvIF->if_lock, Handle);
  1924. CTEFreeMem(NewRBD);
  1925. IPSInfo.ipsi_reasmfails++;
  1926. return;
  1927. }
  1928. } else { // It all fits, copy it.
  1929. RtlCopyMemory(NewRBD + 1, Data, DataLength);
  1930. ReassembleFragment(NTE, SrcNTE, NewRBD, IPH, HeaderSize, DestType, LinkCtxt);
  1931. }
  1932. } else {
  1933. IPSInfo.ipsi_reasmfails++;
  1934. }
  1935. }
  1936. //* CheckLocalOptions - Check the options received with a packet.
  1937. //
  1938. // A routine called when we've received a packet for this host and want to
  1939. // examine it for options. We process the options, and return TRUE or FALSE
  1940. // depending on whether or not it's for us.
  1941. //
  1942. // Input: SrcNTE - Pointer to NTE this came in on.
  1943. // Header - Pointer to incoming header.
  1944. // OptInfo - Place to put opt info.
  1945. // DestType - Type of incoming packet.
  1946. //
  1947. // Returns: DestType - Local or remote.
  1948. //
  1949. uchar
  1950. CheckLocalOptions(NetTableEntry * SrcNTE, IPHeader UNALIGNED * Header,
  1951. IPOptInfo * OptInfo, uchar DestType, uchar* Data,
  1952. uint DataSize, BOOLEAN FilterOnDrop)
  1953. {
  1954. uint HeaderLength; // Length in bytes of header.
  1955. OptIndex Index;
  1956. uchar ErrIndex;
  1957. HeaderLength = (Header->iph_verlen & (uchar) ~ IP_VER_FLAG) << 2;
  1958. ASSERT(HeaderLength > sizeof(IPHeader));
  1959. OptInfo->ioi_options = (uchar *) (Header + 1);
  1960. OptInfo->ioi_optlength = (uchar) (HeaderLength - sizeof(IPHeader));
  1961. // We have options of some sort. The packet may or may not be bound for us.
  1962. Index.oi_srindex = MAX_OPT_SIZE;
  1963. if ((ErrIndex = ParseRcvdOptions(OptInfo, &Index)) < MAX_OPT_SIZE) {
  1964. if (!FilterOnDrop || !ForwardFilterEnabled ||
  1965. NotifyFilterOfDiscard(SrcNTE, Header, Data, DataSize)) {
  1966. SendICMPErr(SrcNTE->nte_addr, Header, ICMP_PARAM_PROBLEM, PTR_VALID,
  1967. ((ulong) ErrIndex + sizeof(IPHeader)));
  1968. }
  1969. return DEST_INVALID; // Parameter error.
  1970. }
  1971. //
  1972. // If there's no source route, or if the destination is a broadcast, we'll
  1973. // take it. If it is a broadcast DeliverToUser will forward it when it's
  1974. // done, and the forwarding code will reprocess the options.
  1975. //
  1976. if (Index.oi_srindex == MAX_OPT_SIZE || IS_BCAST_DEST(DestType))
  1977. return DEST_LOCAL;
  1978. else
  1979. return DEST_REMOTE;
  1980. }
  1981. //* TDUserRcv - Completion routing for a user transfer data.
  1982. //
  1983. // This is the completion handle for TDs invoked because we need to give
  1984. // data to a upper layer client. All we really do is call the upper layer
  1985. // handler with the data.
  1986. //
  1987. // Input: NetContext - Pointer to the net table entry on which we
  1988. // received this.
  1989. // Packet - Packet we received into.
  1990. // Status - Final status of copy.
  1991. // DataSize - Size in bytes of data transferred.
  1992. //
  1993. // Returns: Nothing
  1994. //
  1995. void
  1996. TDUserRcv(void *NetContext, PNDIS_PACKET Packet, NDIS_STATUS Status,
  1997. uint DataSize)
  1998. {
  1999. NetTableEntry *NTE = (NetTableEntry *) NetContext;
  2000. Interface *SrcIF;
  2001. TDContext *Context = (TDContext *) Packet->ProtocolReserved;
  2002. CTELockHandle Handle;
  2003. uchar DestType;
  2004. IPRcvBuf RcvBuf;
  2005. IPOptInfo OptInfo;
  2006. IPHeader *Header;
  2007. uint PromiscuousMode = 0;
  2008. uint FirewallMode = 0;
  2009. if (NTE->nte_flags & NTE_VALID) {
  2010. FirewallMode = ProcessFirewallQ();
  2011. PromiscuousMode = NTE->nte_if->if_promiscuousmode;
  2012. }
  2013. if (Status == NDIS_STATUS_SUCCESS) {
  2014. Header = (IPHeader *) Context->tdc_header;
  2015. OptInfo.ioi_ttl = Header->iph_ttl;
  2016. OptInfo.ioi_tos = Header->iph_tos;
  2017. OptInfo.ioi_flags = (net_short(Header->iph_offset) >> 13) & IP_FLAG_DF;
  2018. if (Context->tdc_hlength != sizeof(IPHeader)) {
  2019. OptInfo.ioi_options = (uchar *) (Header + 1);
  2020. OptInfo.ioi_optlength = Context->tdc_hlength - sizeof(IPHeader);
  2021. } else {
  2022. OptInfo.ioi_options = (uchar *) NULL;
  2023. OptInfo.ioi_optlength = 0;
  2024. }
  2025. DestType = Context->tdc_dtype;
  2026. RcvBuf.ipr_next = NULL;
  2027. RcvBuf.ipr_owner = IPR_OWNER_IP;
  2028. RcvBuf.ipr_buffer = (uchar *) Context->tdc_buffer;
  2029. RcvBuf.ipr_size = DataSize;
  2030. RcvBuf.ipr_pMdl = NULL;
  2031. RcvBuf.ipr_pClientCnt = NULL;
  2032. if (((IPSecHandlerPtr) && (ForwardFilterEnabled)) ||
  2033. (FirewallMode) || (PromiscuousMode)) {
  2034. if (FirewallMode) {
  2035. // attach the header and allocate pRcvBuf on a heap, we free it if firewall is present
  2036. IPRcvBuf *pRcvBuf;
  2037. // attach the header
  2038. pRcvBuf = (IPRcvBuf *) CTEAllocMemN(sizeof(IPRcvBuf), 'giCT');
  2039. if (!pRcvBuf) {
  2040. return;
  2041. }
  2042. pRcvBuf->ipr_owner = IPR_OWNER_IP;
  2043. pRcvBuf->ipr_buffer = (uchar *) Header;
  2044. pRcvBuf->ipr_size = Context->tdc_hlength;
  2045. pRcvBuf->ipr_pMdl = NULL;
  2046. pRcvBuf->ipr_pClientCnt = NULL;
  2047. pRcvBuf->ipr_flags = 0;
  2048. // attach the data
  2049. pRcvBuf->ipr_next = (IPRcvBuf *) CTEAllocMemN(sizeof(IPRcvBuf), 'hiCT');
  2050. if (!pRcvBuf->ipr_next) {
  2051. CTEFreeMem(pRcvBuf);
  2052. return;
  2053. }
  2054. pRcvBuf->ipr_next->ipr_owner = IPR_OWNER_IP;
  2055. pRcvBuf->ipr_next->ipr_buffer = (uchar *) Context->tdc_buffer;
  2056. pRcvBuf->ipr_next->ipr_size = DataSize;
  2057. pRcvBuf->ipr_next->ipr_pMdl = NULL;
  2058. pRcvBuf->ipr_next->ipr_pClientCnt = NULL;
  2059. pRcvBuf->ipr_next->ipr_next = NULL;
  2060. pRcvBuf->ipr_next->ipr_flags = 0;
  2061. DataSize += Context->tdc_hlength;
  2062. DeliverToUserEx(NTE, Context->tdc_nte, Header, Context->tdc_hlength,
  2063. pRcvBuf, DataSize, &OptInfo, Packet, DestType, NULL);
  2064. } else {
  2065. DeliverToUserEx(NTE, Context->tdc_nte, Header, Context->tdc_hlength,
  2066. &RcvBuf, DataSize, &OptInfo, Packet, DestType, NULL);
  2067. }
  2068. } else {
  2069. DeliverToUser(NTE, Context->tdc_nte, Header, Context->tdc_hlength,
  2070. &RcvBuf, DataSize, &OptInfo, Packet, DestType);
  2071. // If it's a broadcast packet forward it on.
  2072. if (IS_BCAST_DEST(DestType))
  2073. IPForwardPkt(NTE, Header, Context->tdc_hlength, RcvBuf.ipr_buffer, DataSize,
  2074. NULL, 0, DestType, 0, NULL, NULL, NULL);
  2075. }
  2076. }
  2077. SrcIF = NTE->nte_if;
  2078. CTEGetLockAtDPC(&SrcIF->if_lock, &Handle);
  2079. Context->tdc_common.pc_link = SrcIF->if_tdpacket;
  2080. SrcIF->if_tdpacket = Packet;
  2081. CTEFreeLockFromDPC(&SrcIF->if_lock, Handle);
  2082. }
  2083. void
  2084. IPInjectPkt(FORWARD_ACTION Action, void *SavedContext, uint SavedContextLength,
  2085. struct IPHeader UNALIGNED *IPH, struct IPRcvBuf *DataChain)
  2086. {
  2087. char *Data;
  2088. char *PreservedData;
  2089. uint DataSize;
  2090. PFIREWALL_CONTEXT_T pFirCtx = (PFIREWALL_CONTEXT_T) SavedContext;
  2091. NetTableEntry *NTE = pFirCtx->NTE; // Local NTE received on
  2092. LinkEntry *LinkCtxt = pFirCtx->LinkCtxt; // Local NTE received on
  2093. NetTableEntry *DestNTE; // NTE to receive on.
  2094. IPAddr DAddr; // Dest. IP addr. of received packet.
  2095. uint HeaderLength; // Size in bytes of received header.
  2096. uint IPDataLength; // Length in bytes of IP (including UL) data in packet.
  2097. IPOptInfo OptInfo; // Incoming header information.
  2098. uchar DestType; // Type (LOCAL, REMOTE, SR) of Daddr.
  2099. IPRcvBuf RcvBuf;
  2100. IPRcvBuf *tmpRcvBuf;
  2101. ulong Offset;
  2102. KIRQL OldIrql;
  2103. //
  2104. // One can not inject a packet that was being transmitted earlier
  2105. //
  2106. ASSERT(pFirCtx->Direction == IP_RECEIVE);
  2107. if (Action == ICMP_ON_DROP) {
  2108. // send an ICMP message ?????
  2109. return;
  2110. }
  2111. ASSERT(Action == FORWARD);
  2112. DataSize = 0;
  2113. tmpRcvBuf = DataChain;
  2114. while (tmpRcvBuf != NULL) {
  2115. ASSERT(tmpRcvBuf->ipr_buffer != NULL);
  2116. DataSize += tmpRcvBuf->ipr_size;
  2117. tmpRcvBuf = tmpRcvBuf->ipr_next;
  2118. }
  2119. Data = (uchar *) CTEAllocMemN(DataSize, 'iiCT');
  2120. if (Data == NULL) {
  2121. return;
  2122. }
  2123. tmpRcvBuf = DataChain;
  2124. Offset = 0;
  2125. while (tmpRcvBuf != NULL) {
  2126. ASSERT(tmpRcvBuf->ipr_buffer != NULL);
  2127. #if DBG_VALIDITY_CHECK
  2128. if (Offset + tmpRcvBuf->ipr_size > DataSize) {
  2129. DbgPrint("Offset %d: tmpRcvBuf->ipr_size %d: DataSize %d ::::\n",
  2130. Offset, tmpRcvBuf->ipr_size, DataSize);
  2131. DbgBreakPoint();
  2132. }
  2133. #endif
  2134. RtlCopyMemory(Data + Offset, tmpRcvBuf->ipr_buffer, tmpRcvBuf->ipr_size);
  2135. Offset += tmpRcvBuf->ipr_size;
  2136. tmpRcvBuf = tmpRcvBuf->ipr_next;
  2137. }
  2138. PreservedData = Data;
  2139. // free the data chain
  2140. // IPFreeBuff(pContextInfo->DataChain);
  2141. IPH = (IPHeader UNALIGNED *) Data;
  2142. // Make sure we actually have data.
  2143. if (DataSize) {
  2144. // Check the header length, the xsum and the version. If any of these
  2145. // checks fail silently discard the packet.
  2146. HeaderLength = ((IPH->iph_verlen & (uchar) ~ IP_VER_FLAG) << 2);
  2147. if (HeaderLength >= sizeof(IPHeader) && HeaderLength <= DataSize) {
  2148. // Check the version, and sanity check the total length.
  2149. IPDataLength = (uint) net_short(IPH->iph_length);
  2150. if ((IPH->iph_verlen & IP_VER_FLAG) == IP_VERSION &&
  2151. IPDataLength > sizeof(IPHeader)) {
  2152. IPDataLength -= HeaderLength;
  2153. Data = (uchar *) Data + HeaderLength;
  2154. DataSize -= HeaderLength;
  2155. // IPDataLength should be equal to DataSize
  2156. ASSERT(IPDataLength == DataSize);
  2157. DAddr = IPH->iph_dest;
  2158. DestNTE = NTE;
  2159. // Find local NTE, if any.
  2160. DestType = GetLocalNTE(DAddr, &DestNTE);
  2161. OptInfo.ioi_ttl = IPH->iph_ttl;
  2162. OptInfo.ioi_tos = IPH->iph_tos;
  2163. OptInfo.ioi_flags = (net_short(IPH->iph_offset) >> 13) &
  2164. IP_FLAG_DF;
  2165. OptInfo.ioi_options = (uchar *) NULL;
  2166. OptInfo.ioi_optlength = 0;
  2167. if ((DestType < DEST_REMOTE)) {
  2168. // It's either local or some sort of broadcast.
  2169. // The data probably belongs at this station. If there
  2170. // aren't any options, it definetly belongs here, and we'll
  2171. // dispatch it either to our reasssmbly code or to the
  2172. // deliver to user code. If there are options, we'll check
  2173. // them and then either handle the packet locally or pass it
  2174. // to our forwarding code.
  2175. if (HeaderLength != sizeof(IPHeader)) {
  2176. // We have options.
  2177. uchar NewDType;
  2178. NewDType = CheckLocalOptions(NTE, IPH, &OptInfo,
  2179. DestType, NULL, 0, FALSE);
  2180. if (NewDType != DEST_LOCAL) {
  2181. if (NewDType == DEST_REMOTE)
  2182. goto forward;
  2183. else {
  2184. IPSInfo.ipsi_inhdrerrors++;
  2185. CTEFreeMem(PreservedData);
  2186. return; // Bad Options.
  2187. }
  2188. }
  2189. }
  2190. RcvBuf.ipr_next = NULL;
  2191. RcvBuf.ipr_owner = IPR_OWNER_IP;
  2192. RcvBuf.ipr_buffer = (uchar *) Data;
  2193. RcvBuf.ipr_size = IPDataLength;
  2194. RcvBuf.ipr_pMdl = NULL;
  2195. RcvBuf.ipr_pClientCnt = NULL;
  2196. // When we get here, we have the whole packet. Deliver
  2197. // it.
  2198. KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
  2199. DeliverToUser(NTE, DestNTE, IPH, HeaderLength, &RcvBuf,
  2200. IPDataLength, &OptInfo, NULL, DestType);
  2201. // When we're here, we're through with the packet
  2202. // locally. If it's a broadcast packet forward it on.
  2203. if (IS_BCAST_DEST(DestType)) {
  2204. IPForwardPkt(NTE, IPH, HeaderLength, Data, IPDataLength, NULL, 0, DestType, 0, NULL, NULL, LinkCtxt);
  2205. }
  2206. KeLowerIrql(OldIrql);
  2207. // free the data, work item and various fields within them.
  2208. CTEFreeMem(PreservedData);
  2209. return;
  2210. }
  2211. // Not for us, may need to be forwarded. It might be an outgoing
  2212. // broadcast that came in through a source route, so we need to
  2213. // check that.
  2214. forward:
  2215. if (DestType != DEST_INVALID) {
  2216. KeRaiseIrql(DISPATCH_LEVEL, &OldIrql);
  2217. IPForwardPkt(NTE, IPH, HeaderLength, Data, DataSize,
  2218. NULL, 0, DestType, 0, NULL, NULL, LinkCtxt);
  2219. KeLowerIrql(OldIrql);
  2220. } else
  2221. IPSInfo.ipsi_inaddrerrors++;
  2222. // free the data, work item and various fields within them.
  2223. CTEFreeMem(PreservedData);
  2224. return;
  2225. } // Bad Version
  2226. } // Bad checksum
  2227. } // No data
  2228. IPSInfo.ipsi_inhdrerrors++;
  2229. // free the data, work item and various fields within them.
  2230. CTEFreeMem(PreservedData);
  2231. }
  2232. //* IPRcvPacket - Receive an incoming IP datagram along with the ndis packet
  2233. //
  2234. // This is the routine called by the link layer module when an incoming IP
  2235. // datagram is to be processed. We validate the datagram (including doing
  2236. // the xsum), copy and process incoming options, and decide what to do
  2237. // with it.
  2238. //
  2239. // Entry: MyContext - The context valued we gave to the link layer.
  2240. // Data - Pointer to the data buffer.
  2241. // DataSize - Size in bytes of the data buffer.
  2242. // TotalSize - Total size in bytes available.
  2243. // LContext1 - 1st link context.
  2244. // LContext2 - 2nd link context.
  2245. // BCast - Indicates whether or not packet was received
  2246. // on bcast address.
  2247. // HeaderSize - size of the mac header
  2248. // pMdl - NDIS Packet from the MAC driver
  2249. // pClientCnt - Variable to indicate how many upper layer
  2250. // clients were given this packet
  2251. // for TCP it will be only 1.
  2252. //
  2253. // Returns: Nothing.
  2254. //
  2255. void
  2256. __stdcall
  2257. IPRcvPacket(void *MyContext, void *Data, uint DataSize, uint TotalSize,
  2258. NDIS_HANDLE LContext1, uint LContext2, uint BCast,
  2259. uint MacHeaderSize, PNDIS_BUFFER pNdisBuffer, uint *pClientCnt,
  2260. LinkEntry *LinkCtxt)
  2261. {
  2262. IPHeader UNALIGNED *IPH = (IPHeader UNALIGNED *) Data;
  2263. NetTableEntry *NTE = (NetTableEntry *) MyContext; // Local NTE received on
  2264. NetTableEntry *DestNTE; // NTE to receive on.
  2265. Interface *RcvIF; // Interface corresponding to NTE.
  2266. CTELockHandle Handle;
  2267. PNDIS_PACKET TDPacket; // NDIS packet used for TD.
  2268. TDContext *TDC = (TDContext *) NULL; // Transfer data context.
  2269. NDIS_STATUS Status;
  2270. IPAddr DAddr; // Dest. IP addr. of received packet.
  2271. uint HeaderLength; // Size in bytes of received header.
  2272. uint IPDataLength; // Length in bytes of IP (including UL)
  2273. // data in packet.
  2274. IPOptInfo OptInfo; // Incoming header information.
  2275. uchar DestType; // Type (LOCAL, REMOTE, SR) of Daddr.
  2276. IPRcvBuf RcvBuf;
  2277. BOOLEAN ChkSumOk = FALSE;
  2278. // used by firewall
  2279. uchar NewDType;
  2280. IPRcvBuf *pRcvBuf;
  2281. uint MoreData = 0;
  2282. uchar *PreservedData;
  2283. uchar *HdrBuf;
  2284. uint DataLength;
  2285. uint FirewallMode = 0;
  2286. uint PromiscuousMode = 0;
  2287. uint AbsorbFwdPkt = 0;
  2288. PNDIS_PACKET OffLoadPkt = NULL;
  2289. IPSIncrementInReceiveCount();
  2290. // Make sure we actually have data.
  2291. if (0 == DataSize) {
  2292. goto HeaderError;
  2293. }
  2294. // Check the header length, the xsum and the version. If any of these
  2295. // checks fail silently discard the packet.
  2296. HeaderLength = ((IPH->iph_verlen & (uchar)~IP_VER_FLAG) << 2);
  2297. if ((HeaderLength < sizeof(IPHeader)) || (HeaderLength > DataSize)) {
  2298. goto HeaderError;
  2299. }
  2300. //Check if hardware did the checksum or not by inspecting Lcontext1
  2301. if (pClientCnt) {
  2302. PNDIS_PACKET_EXTENSION PktExt;
  2303. NDIS_TCP_IP_CHECKSUM_PACKET_INFO ChksumPktInfo;
  2304. if (pNdisBuffer) {
  2305. OffLoadPkt = NDIS_GET_ORIGINAL_PACKET((PNDIS_PACKET) (LContext1));
  2306. if (!OffLoadPkt) {
  2307. OffLoadPkt = (PNDIS_PACKET) (LContext1);
  2308. }
  2309. } else {
  2310. OffLoadPkt = (PNDIS_PACKET) pClientCnt;
  2311. }
  2312. PktExt = NDIS_PACKET_EXTENSION_FROM_PACKET(OffLoadPkt);
  2313. ChksumPktInfo.Value = (USHORT) PktExt->NdisPacketInfo[TcpIpChecksumPacketInfo];
  2314. if (ChksumPktInfo.Value) {
  2315. if (ChksumPktInfo.Receive.NdisPacketIpChecksumSucceeded) {
  2316. ChkSumOk = TRUE;
  2317. }
  2318. }
  2319. }
  2320. // Unless the hardware says the checksum was correct, checksum the
  2321. // header ourselves and bail out if it is incorrect.
  2322. if (!ChkSumOk && (xsum(Data, HeaderLength) != (ushort) 0xffff)) {
  2323. goto HeaderError;
  2324. }
  2325. // Check the version, and sanity check the total length.
  2326. IPDataLength = (uint) net_short(IPH->iph_length);
  2327. if (((IPH->iph_verlen & IP_VER_FLAG) != IP_VERSION) ||
  2328. (IPDataLength < HeaderLength) || (IPDataLength > TotalSize)) {
  2329. goto HeaderError;
  2330. }
  2331. IPDataLength -= HeaderLength;
  2332. // In case of firewall, we need to pass the whole data including header
  2333. PreservedData = (uchar *) Data;
  2334. Data = (uchar *) Data + HeaderLength;
  2335. DataSize -= HeaderLength;
  2336. DAddr = IPH->iph_dest;
  2337. DestNTE = NTE;
  2338. // Find local NTE, if any.
  2339. if (BCast == AI_PROMIS_INDEX) {
  2340. DestType = DEST_PROMIS;
  2341. } else {
  2342. DestType = GetLocalNTE(DAddr, &DestNTE);
  2343. }
  2344. AbsorbFwdPkt = (DestType >= DEST_REMOTE) &&
  2345. (NTE->nte_if->if_absorbfwdpkts) &&
  2346. (IPH->iph_protocol == NTE->nte_if->if_absorbfwdpkts) &&
  2347. IsRtrAlertPacket(IPH);
  2348. PromiscuousMode = NTE->nte_if->if_promiscuousmode;
  2349. FirewallMode = ProcessFirewallQ();
  2350. // Check to see if this is a non-broadcast IP address that
  2351. // came in as a link layer broadcast. If it is, throw it out.
  2352. // This is an important check for DHCP, since if we're
  2353. // DHCPing an interface all otherwise unknown addresses will
  2354. // come in as DEST_LOCAL. This check here will throw them out
  2355. // if they didn't come in as unicast.
  2356. if ((BCast == AI_NONUCAST_INDEX) && !IS_BCAST_DEST(DestType)) {
  2357. IPSInfo.ipsi_inaddrerrors++;
  2358. return; // Non bcast packet on bcast address.
  2359. }
  2360. if (CLASSD_ADDR(DAddr)) {
  2361. NTE->nte_if->if_InMcastPkts++;
  2362. NTE->nte_if->if_InMcastOctets += IPDataLength;
  2363. }
  2364. OptInfo.ioi_ttl = IPH->iph_ttl;
  2365. OptInfo.ioi_tos = IPH->iph_tos;
  2366. OptInfo.ioi_flags = (net_short(IPH->iph_offset) >> 13) & IP_FLAG_DF;
  2367. OptInfo.ioi_options = (uchar *) NULL;
  2368. OptInfo.ioi_optlength = 0;
  2369. if ((DestType < DEST_REMOTE) || (AbsorbFwdPkt) ||
  2370. (((FirewallMode) || (PromiscuousMode)) && (DestType != DEST_INVALID)))
  2371. {
  2372. // It's either local or some sort of broadcast.
  2373. // The data probably belongs at this station. If there
  2374. // aren't any options, it definitely belongs here, and we'll
  2375. // dispatch it either to our reassembly code or to the
  2376. // deliver to user code. If there are options, we'll check
  2377. // them and then either handle the packet locally or pass it
  2378. // to our forwarding code.
  2379. NewDType = DestType;
  2380. if (DestType < DEST_REMOTE) {
  2381. if (HeaderLength != sizeof(IPHeader)) {
  2382. // We have options.
  2383. NewDType = CheckLocalOptions(NTE, IPH, &OptInfo, DestType,
  2384. Data, DataSize, TRUE);
  2385. if (NewDType != DEST_LOCAL) {
  2386. if (NewDType == DEST_REMOTE) {
  2387. if ((!FirewallMode) && (!PromiscuousMode) && (!AbsorbFwdPkt))
  2388. goto forward;
  2389. else
  2390. DestType = NewDType;
  2391. } else {
  2392. goto HeaderError;
  2393. }
  2394. }
  2395. if ((OptInfo.ioi_flags & IP_FLAG_SSRR) &&
  2396. DisableIPSourceRouting == 2) {
  2397. IPSInfo.ipsi_outdiscards++;
  2398. if (ForwardFilterEnabled) {
  2399. NotifyFilterOfDiscard(NTE, IPH, Data, DataSize);
  2400. }
  2401. return;
  2402. }
  2403. }
  2404. }
  2405. //
  2406. // Before we go further, if we have a filter installed
  2407. // call it to see if we should take this.
  2408. // if ForwardFirewall/Promiscuous, we can reach at this
  2409. // point
  2410. // if firewall/ipsec/promiscuous present, we will call
  2411. // filter hook in delivertouserex
  2412. // Except if we have a fragment, we also call filter hook
  2413. // now.
  2414. //
  2415. if (((ForwardFilterEnabled) && (!IPSecHandlerPtr) &&
  2416. (!FirewallMode) && (!PromiscuousMode) &&
  2417. (!AbsorbFwdPkt)) ||
  2418. ((ForwardFilterEnabled) &&
  2419. (IPH->iph_offset & ~(IP_DF_FLAG | IP_RSVD_FLAG)))) {
  2420. Interface *IF = NTE->nte_if;
  2421. IPAddr LinkNextHop;
  2422. FORWARD_ACTION Action;
  2423. if ((IF->if_flags & IF_FLAGS_P2MP) && LinkCtxt) {
  2424. LinkNextHop = LinkCtxt->link_NextHop;
  2425. } else {
  2426. LinkNextHop = NULL_IP_ADDR;
  2427. }
  2428. CTEInterlockedIncrementLong(&ForwardFilterRefCount);
  2429. Action = (*ForwardFilterPtr) (IPH,
  2430. Data,
  2431. MIN(DataSize, IPDataLength),
  2432. IF->if_index,
  2433. INVALID_IF_INDEX,
  2434. LinkNextHop,
  2435. NULL_IP_ADDR);
  2436. DerefFilterPtr();
  2437. if (Action != FORWARD) {
  2438. IPSInfo.ipsi_indiscards++;
  2439. return;
  2440. }
  2441. }
  2442. // No options. See if it's a fragment. If it is, call our
  2443. // reassembly handler.
  2444. if ((IPH->iph_offset & ~(IP_DF_FLAG | IP_RSVD_FLAG)) == 0) {
  2445. // We don't have a fragment. If the data all fits,
  2446. // handle it here. Otherwise transfer data it.
  2447. // Make sure data is all in buffer, and directly
  2448. // accesible.
  2449. if ((IPDataLength > DataSize) || !(NTE->nte_flags & NTE_COPY))
  2450. {
  2451. // The data isn't all here. Transfer data it.
  2452. // Needed by firewall since we need to attach the IPheader
  2453. MoreData = 1;
  2454. RcvIF = NTE->nte_if;
  2455. CTEGetLockAtDPC(&RcvIF->if_lock, &Handle);
  2456. TDPacket = RcvIF->if_tdpacket;
  2457. if (TDPacket != (PNDIS_PACKET) NULL) {
  2458. TDC = (TDContext *) TDPacket->ProtocolReserved;
  2459. RcvIF->if_tdpacket = TDC->tdc_common.pc_link;
  2460. CTEFreeLockFromDPC(&RcvIF->if_lock, Handle);
  2461. TDC->tdc_nte = DestNTE;
  2462. TDC->tdc_dtype = DestType;
  2463. TDC->tdc_hlength = (uchar) HeaderLength;
  2464. RtlCopyMemory(TDC->tdc_header, IPH,
  2465. HeaderLength + 8);
  2466. Status = (*(RcvIF->if_transfer)) (
  2467. RcvIF->if_lcontext, LContext1,
  2468. LContext2, HeaderLength,
  2469. IPDataLength, TDPacket,
  2470. &IPDataLength);
  2471. // Check the status. If it's success, call the
  2472. // receive procedure. Otherwise, if it's pending
  2473. // wait for the callback.
  2474. Data = TDC->tdc_buffer;
  2475. if (Status != NDIS_STATUS_PENDING) {
  2476. if (Status != NDIS_STATUS_SUCCESS) {
  2477. IPSInfo.ipsi_indiscards++;
  2478. CTEGetLockAtDPC(&RcvIF->if_lock, &Handle);
  2479. TDC->tdc_common.pc_link =
  2480. RcvIF->if_tdpacket;
  2481. RcvIF->if_tdpacket = TDPacket;
  2482. CTEFreeLockFromDPC(&RcvIF->if_lock,
  2483. Handle);
  2484. return;
  2485. }
  2486. } else {
  2487. return; // Status is pending.
  2488. }
  2489. } else { // Couldn't get a packet.
  2490. IPSInfo.ipsi_indiscards++;
  2491. CTEFreeLockFromDPC(&RcvIF->if_lock, Handle);
  2492. return;
  2493. }
  2494. }
  2495. if (!FirewallMode) {
  2496. // fast path
  2497. RcvBuf.ipr_next = NULL;
  2498. RcvBuf.ipr_owner = IPR_OWNER_IP;
  2499. RcvBuf.ipr_buffer = (uchar *) Data;
  2500. RcvBuf.ipr_size = IPDataLength;
  2501. //
  2502. //encapsulate the mdl and context info in RcvBuf
  2503. //structure
  2504. //
  2505. RcvBuf.ipr_pMdl = pNdisBuffer;
  2506. RcvBuf.ipr_pClientCnt = pClientCnt;
  2507. RcvBuf.ipr_RcvContext = (char *)LContext1;
  2508. //ASSERT(LContext2 <= 8);
  2509. RcvBuf.ipr_RcvOffset = MacHeaderSize +
  2510. HeaderLength + LContext2;
  2511. DataLength = IPDataLength;
  2512. pRcvBuf = &RcvBuf;
  2513. } else { // ForwardFirewallPtr != NULL
  2514. //
  2515. // if Firewall hooks are present we will allocate
  2516. // RcvBuf. Also we will pass IPHeader to
  2517. // DelivertoUserEx
  2518. pRcvBuf = (IPRcvBuf *) CTEAllocMemN(sizeof(IPRcvBuf), 'jiCT');
  2519. if (!pRcvBuf) {
  2520. IPSInfo.ipsi_indiscards++;
  2521. return;
  2522. }
  2523. if (!MoreData) {
  2524. pRcvBuf->ipr_next = NULL;
  2525. pRcvBuf->ipr_owner = IPR_OWNER_IP;
  2526. pRcvBuf->ipr_buffer = (uchar *) PreservedData;
  2527. pRcvBuf->ipr_size = IPDataLength + HeaderLength;
  2528. pRcvBuf->ipr_flags = 0;
  2529. //
  2530. //encapsulate the mdl and context info in
  2531. //RcvBuf structure
  2532. //
  2533. pRcvBuf->ipr_pMdl = NULL;
  2534. pRcvBuf->ipr_pClientCnt = NULL;
  2535. //ASSERT(LContext2 <= 8);
  2536. pRcvBuf->ipr_RcvContext = (char *)LContext1;
  2537. pRcvBuf->ipr_RcvOffset = MacHeaderSize + HeaderLength + LContext2;
  2538. } else { // MoreData=1; we have gone thru TD
  2539. // path attach the header
  2540. pRcvBuf->ipr_owner = IPR_OWNER_FIREWALL;
  2541. HdrBuf = (uchar *) CTEAllocMemN(HeaderLength, 'kiCT');
  2542. if (!HdrBuf) {
  2543. CTEFreeMem(pRcvBuf);
  2544. IPSInfo.ipsi_indiscards++;
  2545. return;
  2546. }
  2547. RtlCopyMemory(HdrBuf, IPH, HeaderLength);
  2548. pRcvBuf->ipr_buffer = HdrBuf; // remember to
  2549. // free HdrBuf &
  2550. //pRcvBuf
  2551. pRcvBuf->ipr_size = HeaderLength;
  2552. pRcvBuf->ipr_next = (IPRcvBuf *) CTEAllocMemN(sizeof(IPRcvBuf), 'liCT');
  2553. if (!pRcvBuf->ipr_next) {
  2554. CTEFreeMem(pRcvBuf);
  2555. CTEFreeMem(HdrBuf);
  2556. IPSInfo.ipsi_indiscards++;
  2557. return;
  2558. }
  2559. pRcvBuf->ipr_next->ipr_next = NULL;
  2560. pRcvBuf->ipr_next->ipr_owner = IPR_OWNER_IP;
  2561. pRcvBuf->ipr_next->ipr_buffer = (uchar *) Data;
  2562. pRcvBuf->ipr_next->ipr_size = IPDataLength;
  2563. //
  2564. //encapsulate the mdl and context info in
  2565. //RcvBuf structure
  2566. //
  2567. pRcvBuf->ipr_next->ipr_pMdl = NULL;
  2568. pRcvBuf->ipr_next->ipr_pClientCnt = NULL;
  2569. pRcvBuf->ipr_next->ipr_RcvContext = (char *)LContext1;
  2570. pRcvBuf->ipr_next->ipr_flags = 0;
  2571. //ASSERT(LContext2 <= 8);
  2572. pRcvBuf->ipr_next->ipr_RcvOffset =
  2573. MacHeaderSize + HeaderLength + LContext2;
  2574. }
  2575. // In case of firewall, Data includes ipheader also
  2576. DataLength = IPDataLength + HeaderLength;
  2577. }
  2578. // 3 cases when we go to DeliverToUserEx
  2579. // IPSEC & Filter present; Firewallhooks present;
  2580. // promiscuous mode set on the interface
  2581. if (((IPSecHandlerPtr) && (ForwardFilterEnabled)) ||
  2582. (FirewallMode) || (PromiscuousMode)) {
  2583. if (pClientCnt) {
  2584. DeliverToUserEx(NTE, DestNTE, IPH, HeaderLength,
  2585. pRcvBuf, DataLength, &OptInfo,
  2586. LContext1, DestType, LinkCtxt);
  2587. } else {
  2588. DeliverToUserEx(NTE, DestNTE, IPH, HeaderLength,
  2589. pRcvBuf, DataLength, &OptInfo,
  2590. NULL, DestType, LinkCtxt);
  2591. }
  2592. } else {
  2593. //
  2594. // When we get here, we have the whole packet.
  2595. // Deliver it.
  2596. //
  2597. if (pNdisBuffer) {
  2598. DeliverToUser(NTE, DestNTE, IPH, HeaderLength,
  2599. pRcvBuf, IPDataLength, &OptInfo,
  2600. (PNDIS_PACKET) (LContext1),
  2601. DestType);
  2602. } else if (OffLoadPkt) {
  2603. DeliverToUser(NTE, DestNTE, IPH, HeaderLength, pRcvBuf,
  2604. IPDataLength, &OptInfo, OffLoadPkt, DestType);
  2605. } else {
  2606. DeliverToUser(
  2607. NTE, DestNTE, IPH, HeaderLength, pRcvBuf,
  2608. IPDataLength, &OptInfo, NULL, DestType);
  2609. }
  2610. //
  2611. // When we're here, we're through with the packet
  2612. // locally. If it's a broadcast packet forward it
  2613. // on.
  2614. if (IS_BCAST_DEST(DestType)) {
  2615. IPForwardPkt(NTE, IPH, HeaderLength, Data,
  2616. IPDataLength, NULL, 0, DestType,
  2617. 0, NULL, NULL, LinkCtxt);
  2618. }
  2619. }
  2620. if (TDC != NULL) {
  2621. CTEGetLockAtDPC(&RcvIF->if_lock, &Handle);
  2622. TDC->tdc_common.pc_link = RcvIF->if_tdpacket;
  2623. RcvIF->if_tdpacket = TDPacket;
  2624. CTEFreeLockFromDPC(&RcvIF->if_lock, Handle);
  2625. }
  2626. return;
  2627. } else {
  2628. // This is a fragment. Reassemble it.
  2629. IPReassemble(NTE, DestNTE, IPH, HeaderLength, Data,
  2630. DataSize, IPDataLength, DestType, LContext1,
  2631. LContext2, LinkCtxt);
  2632. return;
  2633. }
  2634. }
  2635. // Not for us, may need to be forwarded. It might be an outgoing
  2636. // broadcast that came in through a source route, so we need to
  2637. // check that.
  2638. forward:
  2639. if (DestType != DEST_INVALID) {
  2640. //
  2641. // If IPSec is active, make sure there are no inbound policies
  2642. // that apply to this packet.
  2643. // N.B - IPSecStatus will be true if there is at least one ipsec policy.
  2644. //
  2645. if (IPSecStatus &&
  2646. (*IPSecRcvFWPacketPtr)((PCHAR) IPH, Data, DataSize, DestType) != eFORWARD) {
  2647. IPSInfo.ipsi_indiscards++;
  2648. return;
  2649. }
  2650. // Super Fast Forward
  2651. // chk the parameters
  2652. IPForwardPkt(NTE, IPH, HeaderLength, Data, DataSize,
  2653. LContext1, LContext2, DestType, MacHeaderSize, pNdisBuffer,
  2654. pClientCnt, LinkCtxt);
  2655. } else {
  2656. IPSInfo.ipsi_inaddrerrors++;
  2657. }
  2658. return;
  2659. HeaderError:
  2660. IPSInfo.ipsi_inhdrerrors++;
  2661. }
  2662. //* IPRcv - Receive an incoming IP datagram.
  2663. //
  2664. // This is the routine called by the link layer module when an incoming IP
  2665. // datagram is to be processed. We validate the datagram (including doing
  2666. // the xsum), copy and process incoming options, and decide what to do with it.
  2667. //
  2668. // Entry: MyContext - The context valued we gave to the link layer.
  2669. // Data - Pointer to the data buffer.
  2670. // DataSize - Size in bytes of the data buffer.
  2671. // TotalSize - Total size in bytes available.
  2672. // LContext1 - 1st link context.
  2673. // LContext2 - 2nd link context.
  2674. // BCast - Indicates whether or not packet was received on bcast address.
  2675. //
  2676. // Returns: Nothing.
  2677. //
  2678. // For buffer ownership version, we just call RcvPacket, with additional
  2679. // two null arguments. Currently LANARP supports buffer owner ship.
  2680. // Rest of the folks (rasarp, wanarp and atmarp) come this way.
  2681. //
  2682. void
  2683. __stdcall
  2684. IPRcv(void *MyContext, void *Data, uint DataSize, uint TotalSize,
  2685. NDIS_HANDLE LContext1, uint LContext2, uint BCast, LinkEntry * LinkCtxt)
  2686. {
  2687. IPRcvPacket(MyContext,
  2688. Data,
  2689. DataSize,
  2690. TotalSize,
  2691. LContext1,
  2692. LContext2,
  2693. BCast,
  2694. (uint) 0,
  2695. NULL,
  2696. NULL,
  2697. LinkCtxt);
  2698. }
  2699. //* IPTDComplete - IP Transfer data complete handler.
  2700. //
  2701. // This is the routine called by the link layer when a transfer data completes.
  2702. //
  2703. // Entry: MyContext - Context value we gave to the link layer.
  2704. // Packet - Packet we originally gave to transfer data.
  2705. // Status - Final status of command.
  2706. // BytesCopied - Number of bytes copied.
  2707. //
  2708. // Exit: Nothing
  2709. //
  2710. void
  2711. __stdcall
  2712. IPTDComplete(void *MyContext, PNDIS_PACKET Packet, NDIS_STATUS Status,
  2713. uint BytesCopied)
  2714. {
  2715. TDContext *TDC = (TDContext *) Packet->ProtocolReserved;
  2716. FWContext *pFWC = (FWContext *) Packet->ProtocolReserved;
  2717. NetTableEntry *NTE = (NetTableEntry *) MyContext;
  2718. uint PromiscuousMode = 0;
  2719. uint FirewallMode = 0;
  2720. if (NTE->nte_flags & NTE_VALID) {
  2721. PromiscuousMode = NTE->nte_if->if_promiscuousmode;
  2722. FirewallMode = ProcessFirewallQ();
  2723. }
  2724. if (((IPSecHandlerPtr) && (ForwardFilterEnabled)) ||
  2725. (FirewallMode) || (PromiscuousMode)) {
  2726. if (!(TDC->tdc_common.pc_flags & PACKET_FLAG_RA))
  2727. TDUserRcv(MyContext, Packet, Status, BytesCopied);
  2728. else
  2729. RATDComplete(MyContext, Packet, Status, BytesCopied);
  2730. } else { // Normal Path
  2731. if (!(TDC->tdc_common.pc_flags & PACKET_FLAG_FW))
  2732. if (!(TDC->tdc_common.pc_flags & PACKET_FLAG_RA))
  2733. TDUserRcv(MyContext, Packet, Status, BytesCopied);
  2734. else
  2735. RATDComplete(MyContext, Packet, Status, BytesCopied);
  2736. else {
  2737. #if IPMCAST
  2738. if (pFWC->fc_dtype == DEST_REM_MCAST) {
  2739. IPMForwardAfterTD(MyContext, Packet, BytesCopied);
  2740. return;
  2741. }
  2742. #endif
  2743. SendFWPacket(Packet, Status, BytesCopied);
  2744. }
  2745. }
  2746. }
  2747. //* IPFreeBuff -
  2748. // Frees the chain and the buffers associated with the chain if allocated
  2749. // by firewall hook
  2750. //
  2751. //
  2752. void
  2753. IPFreeBuff(IPRcvBuf * pRcvBuf)
  2754. {
  2755. IPRcvBuf *Curr = pRcvBuf;
  2756. IPRcvBuf *Prev;
  2757. //
  2758. // Free all blocks carried by pRcvbuf
  2759. //
  2760. while (pRcvBuf != NULL) {
  2761. FreeIprBuff(pRcvBuf);
  2762. pRcvBuf = pRcvBuf->ipr_next;
  2763. }
  2764. while (Curr != NULL) {
  2765. Prev = Curr;
  2766. Curr = Curr->ipr_next;
  2767. //
  2768. // Free pRcvBuf itself
  2769. //
  2770. CTEFreeMem(Prev);
  2771. }
  2772. }
  2773. //* FreeIprBuff -
  2774. // Frees the buffer associated by IPRcvBuf if tag in rcvbuf is firewall
  2775. // The idea is that if the buffer is allocated by firewall, the tag is firewall
  2776. // and its freed when we call ipfreebuff or this routine. However, there is a
  2777. // slight catch here. In the reassembly path, the buffer is tagged as ip but
  2778. // it has to be freed by ip driver only since the reassembly buffers are
  2779. // allocated by ip only. But in this case, the flat buffer is part of the
  2780. // Rcvbuf structure and so when Rcvbuf structure is freed the flat buffer is
  2781. // also freed. In other cases, fast path in Rcv and xmit path, respective
  2782. // lower and upper layers free the flat buffer. This makes sure that ip is
  2783. // not freeing the buffers which some other layer allocates. This technique
  2784. // is now used by IPSEC also.
  2785. //
  2786. void
  2787. FreeIprBuff(IPRcvBuf * pRcvBuf)
  2788. {
  2789. ASSERT(pRcvBuf != NULL);
  2790. if ((pRcvBuf->ipr_buffer != NULL) && (pRcvBuf->ipr_owner == IPR_OWNER_FIREWALL)) {
  2791. CTEFreeMem(pRcvBuf->ipr_buffer);
  2792. }
  2793. }
  2794. //* IPAllocBuff -
  2795. // Allocates a buffer of given size and attaches it to IPRcvBuf
  2796. //
  2797. // Returns: TRUE if success else FALSE
  2798. //
  2799. int
  2800. IPAllocBuff(IPRcvBuf * pRcvBuf, uint size)
  2801. {
  2802. ASSERT(pRcvBuf != NULL);
  2803. // put a tag in iprcvbuf that firewall allocated it so that
  2804. // FreeIprBuff / IPFreeBuff can free it
  2805. pRcvBuf->ipr_owner = IPR_OWNER_FIREWALL;
  2806. if ((pRcvBuf->ipr_buffer = (uchar *) CTEAllocMemN(size, 'miCT')) == NULL) {
  2807. return FALSE;
  2808. }
  2809. return TRUE;
  2810. }