tweak for building in fedora20
[ipfw-google.git] / kipfw / win-passthru.diff
1 diff -ubwrp original_passthru/miniport.c kipfw/miniport.c
2 --- original_passthru/miniport.c        2012-08-01 14:34:15.096679600 +0200
3 +++ kipfw/miniport.c    2012-08-01 14:34:11.377929600 +0200
4 @@ -223,6 +223,7 @@ Return Value:
5      //\r
6      // Use NDIS 5.1 packet stacking:\r
7      //\r
8 +    if (0)     // XXX IPFW - make sure we don't go in here
9      {\r
10          PNDIS_PACKET_STACK        pStack;\r
11          BOOLEAN                   Remaining;\r
12 @@ -347,6 +348,25 @@ Return Value:
13                                                  MediaSpecificInfo,\r
14                                                  MediaSpecificInfoSize);\r
15          }\r
16 +#if 1  /* IPFW: query the firewall */
17 +       /* if dummynet keeps the packet, we mimic success.
18 +        * otherwise continue as usual.
19 +        */
20 +               {
21 +                       int ret = ipfw2_qhandler_w32(MyPacket, OUTGOING,
22 +                                       MiniportAdapterContext);
23 +                       if (ret != PASS) {
24 +                               if (ret == DROP)
25 +                                       return NDIS_STATUS_FAILURE;
26 +                               else {  //dummynet kept the packet
27 +#ifndef WIN9X
28 +                                       NdisIMCopySendCompletePerPacketInfo (Packet, MyPacket);
29 +#endif
30 +                                       return NDIS_STATUS_SUCCESS; //otherwise simply continue
31 +                               }
32 +                       }
33 +               }
34 +#endif /* end of IPFW code */
35  \r
36          NdisSend(&Status,\r
37                   pAdapt->BindingHandle,\r
38 diff -ubwrp original_passthru/passthru.c kipfw/passthru.c
39 --- original_passthru/passthru.c        2012-08-01 14:34:15.268554600 +0200
40 +++ kipfw/passthru.c    2012-08-01 14:34:11.534179600 +0200
41 @@ -47,8 +47,15 @@ NDIS_HANDLE        NdisWrapperHandle;
42  // To support ioctls from user-mode:\r
43  //\r
44  \r
45 -#define LINKNAME_STRING     L"\\DosDevices\\Passthru"\r
46 -#define NTDEVICE_STRING     L"\\Device\\Passthru"\r
47 +#define STR2(x) #x
48 +#define STR(x) STR2(x)
49 +#define DOSPREFIX "\\DosDevices\\"
50 +#define NTPREFIX "\\Device\\"
51 +#define WIDEN2(x) L ## x
52 +#define WIDEN(x) WIDEN2(x)
53 +#define LINKNAME_STRING                        WIDEN(DOSPREFIX) WIDEN(STR(MODULENAME))
54 +#define NTDEVICE_STRING                        WIDEN(NTPREFIX) WIDEN(STR(MODULENAME))
55 +#define PROTOCOLNAME_STRING            WIDEN(STR(MODULENAME))
56  \r
57  NDIS_HANDLE     NdisDeviceHandle = NULL;\r
58  PDEVICE_OBJECT  ControlDeviceObject = NULL;\r
59 @@ -136,8 +143,8 @@ Return Value:
60          // Either the Send or the SendPackets handler should be specified.\r
61          // If SendPackets handler is specified, SendHandler is ignored\r
62          //\r
63 -        MChars.SendHandler = NULL;    // MPSend;\r
64 -        MChars.SendPacketsHandler = MPSendPackets;\r
65 +        MChars.SendHandler = MPSend;    // IPFW: use MPSend, not SendPackets
66 +        MChars.SendPacketsHandler = NULL;
67  \r
68          Status = NdisIMRegisterLayeredMiniport(NdisWrapperHandle,\r
69                                                    &MChars,\r
70 @@ -165,7 +172,7 @@ Return Value:
71          // This is needed to ensure that NDIS can correctly determine\r
72          // the binding and call us to bind to miniports below.\r
73          //\r
74 -        NdisInitUnicodeString(&Name, L"Passthru");    // Protocol name\r
75 +        NdisInitUnicodeString(&Name, PROTOCOLNAME_STRING);    // Protocol name
76          PChars.Name = Name;\r
77          PChars.OpenAdapterCompleteHandler = PtOpenAdapterComplete;\r
78          PChars.CloseAdapterCompleteHandler = PtCloseAdapterComplete;\r
79 @@ -205,6 +212,8 @@ Return Value:
80          NdisTerminateWrapper(NdisWrapperHandle, NULL);\r
81      }\r
82  \r
83 +    ipfw_module_init();        // IPFW - start the system
84 +
85      return(Status);\r
86  }\r
87  \r
88 @@ -276,7 +285,8 @@ Return Value:
89          DispatchTable[IRP_MJ_CREATE] = PtDispatch;\r
90          DispatchTable[IRP_MJ_CLEANUP] = PtDispatch;\r
91          DispatchTable[IRP_MJ_CLOSE] = PtDispatch;\r
92 -        DispatchTable[IRP_MJ_DEVICE_CONTROL] = PtDispatch;\r
93 +       // IPFW we use DevIoControl ?
94 +        DispatchTable[IRP_MJ_DEVICE_CONTROL] = DevIoControl;
95          \r
96  \r
97          NdisInitUnicodeString(&DeviceName, NTDEVICE_STRING);\r
98 @@ -453,6 +463,7 @@ PtUnload(
99      \r
100      NdisFreeSpinLock(&GlobalLock);\r
101  \r
102 +    ipfw_module_exit(); // IPFW unloading dummynet
103 +
104      DBGPRINT(("PtUnload: done!\n"));\r
105  }\r
106 -\r
107 diff -ubwrp original_passthru/passthru.h kipfw/passthru.h
108 --- original_passthru/passthru.h        2012-08-01 14:34:15.049804600 +0200
109 +++ kipfw/passthru.h    2012-08-01 14:34:11.362304600 +0200
110 @@ -61,6 +61,13 @@ PtDispatch(
111      IN PIRP                      Irp\r
112      );\r
113  \r
114 +DRIVER_DISPATCH DevIoControl;\r
115 +NTSTATUS\r
116 +DevIoControl(\r
117 +    IN PDEVICE_OBJECT            pDeviceObject,\r
118 +    IN PIRP                      pIrp\r
119 +    );\r
120 +\r
121  NDIS_STATUS\r
122  PtRegisterDevice(\r
123      VOID\r
124 @@ -366,6 +373,7 @@ PtDereferenceAdapt(
125  typedef struct _SEND_RSVD\r
126  {\r
127      PNDIS_PACKET    OriginalPkt;\r
128 +    struct mbuf*    pMbuf; // IPFW extension, reference to the mbuf\r
129  } SEND_RSVD, *PSEND_RSVD;\r
130  \r
131  //\r
132 @@ -376,6 +384,7 @@ typedef struct _SEND_RSVD
133  typedef struct _RECV_RSVD\r
134  {\r
135      PNDIS_PACKET    OriginalPkt;\r
136 +    struct mbuf*    pMbuf; // IPFW extension, reference to the mbuf\r
137  } RECV_RSVD, *PRECV_RSVD;\r
138  \r
139  C_ASSERT(sizeof(RECV_RSVD) <= sizeof(((PNDIS_PACKET)0)->MiniportReserved));\r
140 @@ -475,3 +484,17 @@ IsIMDeviceStateOn(
141  */\r
142  #define IsIMDeviceStateOn(_pP)        ((_pP)->MPDeviceState == NdisDeviceStateD0 && (_pP)->PTDeviceState == NdisDeviceStateD0 ) \r
143  \r
144 +#include "winmissing.h"\r
145 +\r
146 +int ipfw_module_init(void);\r
147 +void ipfw_module_exit(void);\r
148 +int ipfw2_qhandler_w32(PNDIS_PACKET pNdisPacket, int direction,\r
149 +       NDIS_HANDLE Context);\r
150 +int ipfw2_qhandler_w32_oldstyle(int direction, NDIS_HANDLE ProtocolBindingContext,\r
151 +               unsigned char* HeaderBuffer, unsigned int HeaderBufferSize,\r
152 +               unsigned char* LookAheadBuffer, unsigned int LookAheadBufferSize,\r
153 +           unsigned int PacketSize);\r
154 +void CleanupReinjected(PNDIS_PACKET Packet, struct mbuf* m, PADAPT pAdapt);\r
155 +void hexdump(PUCHAR,int, const char *);\r
156 +void my_init();\r
157 +void my_exit();
158 \ Manca newline alla fine del file
159 Solo in original_passthru: passthru.htm
160 Solo in original_passthru: passthru.rc
161 diff -ubwrp original_passthru/protocol.c kipfw/protocol.c
162 --- original_passthru/protocol.c        2012-08-01 14:34:15.112304600 +0200
163 +++ kipfw/protocol.c    2012-08-01 14:34:11.409179600 +0200
164 @@ -841,6 +841,14 @@ Return Value:
165          SendRsvd = (PSEND_RSVD)(Packet->ProtocolReserved);\r
166          Pkt = SendRsvd->OriginalPkt;\r
167      \r
168 +#if 1  // IPFW - new code
169 +       //DbgPrint("SendComplete: packet %p pkt %p\n", Packet, Pkt);
170 +       if (Pkt == NULL) { //this is a reinjected packet, with no 'father'
171 +               CleanupReinjected(Packet, SendRsvd->pMbuf, pAdapt);
172 +               return;
173 +       }
174 +#endif /* IPFW */
175 +    
176  #ifndef WIN9X\r
177          NdisIMCopySendCompletePerPacketInfo (Pkt, Packet);\r
178  #endif\r
179 @@ -1021,6 +1029,13 @@ Return Value:
180  \r
181                  if (pAdapt->MiniportHandle != NULL)\r
182                  {\r
183 +#if 1  /* IPFW: query the firewall */
184 +                                       int     ret;
185 +                                       ret = ipfw2_qhandler_w32(MyPacket, INCOMING,
186 +                                               ProtocolBindingContext);
187 +                                       if (ret != PASS)
188 +                                       return 0; //otherwise simply continue
189 +#endif /* end of IPFW code */
190                      NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &MyPacket, 1);\r
191                  }\r
192  \r
193 @@ -1055,6 +1070,13 @@ Return Value:
194          {\r
195              case NdisMedium802_3:\r
196              case NdisMediumWan:\r
197 +                               //DbgPrint("EthIndicateReceive context %p, header at %p len %u, lookahead at %p len %u, packetsize %u\n",ProtocolBindingContext,HeaderBuffer,HeaderBufferSize,LookAheadBuffer,LookAheadBufferSize,PacketSize);
198 +                               //hexdump(HeaderBuffer,HeaderBufferSize+LookAheadBufferSize,"EthIndicateReceive");
199 +                       {
200 +                               int ret = ipfw2_qhandler_w32_oldstyle(INCOMING, ProtocolBindingContext, HeaderBuffer, HeaderBufferSize, LookAheadBuffer, LookAheadBufferSize, PacketSize);
201 +                               if (ret != PASS)
202 +                                       return NDIS_STATUS_SUCCESS;
203 +                       }
204                  NdisMEthIndicateReceive(pAdapt->MiniportHandle,\r
205                                               MacReceiveContext,\r
206                                               HeaderBuffer,\r
207 @@ -1120,6 +1142,21 @@ Return Value:
208      PADAPT        pAdapt =(PADAPT)ProtocolBindingContext;\r
209      ULONG         Proc = KeGetCurrentProcessorNumber();      \r
210  \r
211 +       /* Warning: this is a poor implementation of the PtReceiveComplete
212 +        * made by MS, and it's a well known (but never fixed) issue.
213 +        * Since the ProcessorNumber here can be different from the one
214 +        * that processed the PtReceive, sometimes NdisMEthIndicateReceiveComplete
215 +        * will not be called, causing poor performance in the incoming traffic.
216 +        * In our driver, PtReceive is called for IP packets ONLY by particulary 
217 +        * old NIC drivers, and the poor performance can be seen even 
218 +        * in traffic not handled by ipfw or dummynet.
219 +        * Fortunately, this is quite rare, all the incoming IP packets
220 +        * will arrive through PtReceivePacket, and this callback will never
221 +        * be called. For reinjected traffic, a workaround is done
222 +        * commuting the ReceivedIndicationFlag and calling
223 +        * NdisMEthIndicateReceiveComplete manually for each packet.
224 +        */
225 +
226      if (((pAdapt->MiniportHandle != NULL)\r
227                  && (pAdapt->MPDeviceState == NdisDeviceStateD0))\r
228                  && (pAdapt->ReceivedIndicationFlags[Proc]))\r
229 @@ -1199,7 +1236,7 @@ Return Value:
230      // See also: PtReceive(). \r
231      //\r
232      (VOID)NdisIMGetCurrentPacketStack(Packet, &Remaining);\r
233 -    if (Remaining)\r
234 +    if (0 && Remaining)
235      {\r
236          //\r
237          // We can reuse "Packet". Indicate it up and be done with it.\r
238 @@ -1247,6 +1284,13 @@ Return Value:
239  \r
240          if (pAdapt->MiniportHandle != NULL)\r
241          {\r
242 +#if 1  /* IPFW: query the firewall */
243 +           int ret;
244 +           ret = ipfw2_qhandler_w32(MyPacket, INCOMING,
245 +                       ProtocolBindingContext);
246 +           if (ret != PASS)
247 +                       return 0; //otherwise simply continue
248 +#endif /* end of IPFW code */
249              NdisMIndicateReceivePacket(pAdapt->MiniportHandle, &MyPacket, 1);\r
250          }\r
251  \r