ce2838f76a3ff72ea51db63817328b799211d85d
[openwrt/staging/mkresin.git] / target / linux / generic-2.4 / patches / 603-netfilter_nat_pptp.patch
1 diff -urN linux.old/include/linux/netfilter_ipv4/ip_conntrack.h linux.dev/include/linux/netfilter_ipv4/ip_conntrack.h
2 --- linux.old/include/linux/netfilter_ipv4/ip_conntrack.h 2006-12-06 00:31:46.000000000 +0100
3 +++ linux.dev/include/linux/netfilter_ipv4/ip_conntrack.h 2006-12-06 00:34:27.000000000 +0100
4 @@ -50,16 +50,19 @@
5
6 #include <linux/netfilter_ipv4/ip_conntrack_tcp.h>
7 #include <linux/netfilter_ipv4/ip_conntrack_icmp.h>
8 +#include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
9
10 /* per conntrack: protocol private data */
11 union ip_conntrack_proto {
12 /* insert conntrack proto private data here */
13 struct ip_ct_tcp tcp;
14 struct ip_ct_icmp icmp;
15 + struct ip_ct_gre gre;
16 };
17
18 union ip_conntrack_expect_proto {
19 /* insert expect proto private data here */
20 + struct ip_ct_gre_expect gre;
21 };
22
23 /* Add protocol helper include file here */
24 @@ -67,6 +70,7 @@
25
26 #include <linux/netfilter_ipv4/ip_conntrack_ftp.h>
27 #include <linux/netfilter_ipv4/ip_conntrack_irc.h>
28 +#include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
29
30 /* per expectation: application helper private data */
31 union ip_conntrack_expect_help {
32 @@ -74,6 +78,7 @@
33 struct ip_ct_amanda_expect exp_amanda_info;
34 struct ip_ct_ftp_expect exp_ftp_info;
35 struct ip_ct_irc_expect exp_irc_info;
36 + struct ip_ct_pptp_expect exp_pptp_info;
37
38 #ifdef CONFIG_IP_NF_NAT_NEEDED
39 union {
40 @@ -87,14 +92,17 @@
41 /* insert conntrack helper private data (master) here */
42 struct ip_ct_ftp_master ct_ftp_info;
43 struct ip_ct_irc_master ct_irc_info;
44 + struct ip_ct_pptp_master ct_pptp_info;
45 };
46
47 #ifdef CONFIG_IP_NF_NAT_NEEDED
48 #include <linux/netfilter_ipv4/ip_nat.h>
49 +#include <linux/netfilter_ipv4/ip_nat_pptp.h>
50
51 /* per conntrack: nat application helper private data */
52 union ip_conntrack_nat_help {
53 /* insert nat helper private data here */
54 + struct ip_nat_pptp nat_pptp_info;
55 };
56 #endif
57
58 diff -urN linux.old/include/linux/netfilter_ipv4/ip_conntrack_pptp.h linux.dev/include/linux/netfilter_ipv4/ip_conntrack_pptp.h
59 --- linux.old/include/linux/netfilter_ipv4/ip_conntrack_pptp.h 1970-01-01 01:00:00.000000000 +0100
60 +++ linux.dev/include/linux/netfilter_ipv4/ip_conntrack_pptp.h 2006-12-06 00:34:27.000000000 +0100
61 @@ -0,0 +1,313 @@
62 +/* PPTP constants and structs */
63 +#ifndef _CONNTRACK_PPTP_H
64 +#define _CONNTRACK_PPTP_H
65 +
66 +/* state of the control session */
67 +enum pptp_ctrlsess_state {
68 + PPTP_SESSION_NONE, /* no session present */
69 + PPTP_SESSION_ERROR, /* some session error */
70 + PPTP_SESSION_STOPREQ, /* stop_sess request seen */
71 + PPTP_SESSION_REQUESTED, /* start_sess request seen */
72 + PPTP_SESSION_CONFIRMED, /* session established */
73 +};
74 +
75 +/* state of the call inside the control session */
76 +enum pptp_ctrlcall_state {
77 + PPTP_CALL_NONE,
78 + PPTP_CALL_ERROR,
79 + PPTP_CALL_OUT_REQ,
80 + PPTP_CALL_OUT_CONF,
81 + PPTP_CALL_IN_REQ,
82 + PPTP_CALL_IN_REP,
83 + PPTP_CALL_IN_CONF,
84 + PPTP_CALL_CLEAR_REQ,
85 +};
86 +
87 +
88 +/* conntrack private data */
89 +struct ip_ct_pptp_master {
90 + enum pptp_ctrlsess_state sstate; /* session state */
91 +
92 + /* everything below is going to be per-expectation in newnat,
93 + * since there could be more than one call within one session */
94 + enum pptp_ctrlcall_state cstate; /* call state */
95 + u_int16_t pac_call_id; /* call id of PAC, host byte order */
96 + u_int16_t pns_call_id; /* call id of PNS, host byte order */
97 +};
98 +
99 +/* conntrack_expect private member */
100 +struct ip_ct_pptp_expect {
101 + enum pptp_ctrlcall_state cstate; /* call state */
102 + u_int16_t pac_call_id; /* call id of PAC */
103 + u_int16_t pns_call_id; /* call id of PNS */
104 +};
105 +
106 +
107 +#ifdef __KERNEL__
108 +
109 +#include <linux/netfilter_ipv4/lockhelp.h>
110 +DECLARE_LOCK_EXTERN(ip_pptp_lock);
111 +
112 +#define IP_CONNTR_PPTP PPTP_CONTROL_PORT
113 +
114 +union pptp_ctrl_union {
115 + void *rawreq;
116 + struct PptpStartSessionRequest *sreq;
117 + struct PptpStartSessionReply *srep;
118 + struct PptpStopSessionRequest *streq;
119 + struct PptpStopSessionReply *strep;
120 + struct PptpOutCallRequest *ocreq;
121 + struct PptpOutCallReply *ocack;
122 + struct PptpInCallRequest *icreq;
123 + struct PptpInCallReply *icack;
124 + struct PptpInCallConnected *iccon;
125 + struct PptpClearCallRequest *clrreq;
126 + struct PptpCallDisconnectNotify *disc;
127 + struct PptpWanErrorNotify *wanerr;
128 + struct PptpSetLinkInfo *setlink;
129 +};
130 +
131 +
132 +
133 +#define PPTP_CONTROL_PORT 1723
134 +
135 +#define PPTP_PACKET_CONTROL 1
136 +#define PPTP_PACKET_MGMT 2
137 +
138 +#define PPTP_MAGIC_COOKIE 0x1a2b3c4d
139 +
140 +struct pptp_pkt_hdr {
141 + __u16 packetLength;
142 + __u16 packetType;
143 + __u32 magicCookie;
144 +};
145 +
146 +/* PptpControlMessageType values */
147 +#define PPTP_START_SESSION_REQUEST 1
148 +#define PPTP_START_SESSION_REPLY 2
149 +#define PPTP_STOP_SESSION_REQUEST 3
150 +#define PPTP_STOP_SESSION_REPLY 4
151 +#define PPTP_ECHO_REQUEST 5
152 +#define PPTP_ECHO_REPLY 6
153 +#define PPTP_OUT_CALL_REQUEST 7
154 +#define PPTP_OUT_CALL_REPLY 8
155 +#define PPTP_IN_CALL_REQUEST 9
156 +#define PPTP_IN_CALL_REPLY 10
157 +#define PPTP_IN_CALL_CONNECT 11
158 +#define PPTP_CALL_CLEAR_REQUEST 12
159 +#define PPTP_CALL_DISCONNECT_NOTIFY 13
160 +#define PPTP_WAN_ERROR_NOTIFY 14
161 +#define PPTP_SET_LINK_INFO 15
162 +
163 +#define PPTP_MSG_MAX 15
164 +
165 +/* PptpGeneralError values */
166 +#define PPTP_ERROR_CODE_NONE 0
167 +#define PPTP_NOT_CONNECTED 1
168 +#define PPTP_BAD_FORMAT 2
169 +#define PPTP_BAD_VALUE 3
170 +#define PPTP_NO_RESOURCE 4
171 +#define PPTP_BAD_CALLID 5
172 +#define PPTP_REMOVE_DEVICE_ERROR 6
173 +
174 +struct PptpControlHeader {
175 + __u16 messageType;
176 + __u16 reserved;
177 +};
178 +
179 +/* FramingCapability Bitmap Values */
180 +#define PPTP_FRAME_CAP_ASYNC 0x1
181 +#define PPTP_FRAME_CAP_SYNC 0x2
182 +
183 +/* BearerCapability Bitmap Values */
184 +#define PPTP_BEARER_CAP_ANALOG 0x1
185 +#define PPTP_BEARER_CAP_DIGITAL 0x2
186 +
187 +struct PptpStartSessionRequest {
188 + __u16 protocolVersion;
189 + __u8 reserved1;
190 + __u8 reserved2;
191 + __u32 framingCapability;
192 + __u32 bearerCapability;
193 + __u16 maxChannels;
194 + __u16 firmwareRevision;
195 + __u8 hostName[64];
196 + __u8 vendorString[64];
197 +};
198 +
199 +/* PptpStartSessionResultCode Values */
200 +#define PPTP_START_OK 1
201 +#define PPTP_START_GENERAL_ERROR 2
202 +#define PPTP_START_ALREADY_CONNECTED 3
203 +#define PPTP_START_NOT_AUTHORIZED 4
204 +#define PPTP_START_UNKNOWN_PROTOCOL 5
205 +
206 +struct PptpStartSessionReply {
207 + __u16 protocolVersion;
208 + __u8 resultCode;
209 + __u8 generalErrorCode;
210 + __u32 framingCapability;
211 + __u32 bearerCapability;
212 + __u16 maxChannels;
213 + __u16 firmwareRevision;
214 + __u8 hostName[64];
215 + __u8 vendorString[64];
216 +};
217 +
218 +/* PptpStopReasons */
219 +#define PPTP_STOP_NONE 1
220 +#define PPTP_STOP_PROTOCOL 2
221 +#define PPTP_STOP_LOCAL_SHUTDOWN 3
222 +
223 +struct PptpStopSessionRequest {
224 + __u8 reason;
225 +};
226 +
227 +/* PptpStopSessionResultCode */
228 +#define PPTP_STOP_OK 1
229 +#define PPTP_STOP_GENERAL_ERROR 2
230 +
231 +struct PptpStopSessionReply {
232 + __u8 resultCode;
233 + __u8 generalErrorCode;
234 +};
235 +
236 +struct PptpEchoRequest {
237 + __u32 identNumber;
238 +};
239 +
240 +/* PptpEchoReplyResultCode */
241 +#define PPTP_ECHO_OK 1
242 +#define PPTP_ECHO_GENERAL_ERROR 2
243 +
244 +struct PptpEchoReply {
245 + __u32 identNumber;
246 + __u8 resultCode;
247 + __u8 generalErrorCode;
248 + __u16 reserved;
249 +};
250 +
251 +/* PptpFramingType */
252 +#define PPTP_ASYNC_FRAMING 1
253 +#define PPTP_SYNC_FRAMING 2
254 +#define PPTP_DONT_CARE_FRAMING 3
255 +
256 +/* PptpCallBearerType */
257 +#define PPTP_ANALOG_TYPE 1
258 +#define PPTP_DIGITAL_TYPE 2
259 +#define PPTP_DONT_CARE_BEARER_TYPE 3
260 +
261 +struct PptpOutCallRequest {
262 + __u16 callID;
263 + __u16 callSerialNumber;
264 + __u32 minBPS;
265 + __u32 maxBPS;
266 + __u32 bearerType;
267 + __u32 framingType;
268 + __u16 packetWindow;
269 + __u16 packetProcDelay;
270 + __u16 reserved1;
271 + __u16 phoneNumberLength;
272 + __u16 reserved2;
273 + __u8 phoneNumber[64];
274 + __u8 subAddress[64];
275 +};
276 +
277 +/* PptpCallResultCode */
278 +#define PPTP_OUTCALL_CONNECT 1
279 +#define PPTP_OUTCALL_GENERAL_ERROR 2
280 +#define PPTP_OUTCALL_NO_CARRIER 3
281 +#define PPTP_OUTCALL_BUSY 4
282 +#define PPTP_OUTCALL_NO_DIAL_TONE 5
283 +#define PPTP_OUTCALL_TIMEOUT 6
284 +#define PPTP_OUTCALL_DONT_ACCEPT 7
285 +
286 +struct PptpOutCallReply {
287 + __u16 callID;
288 + __u16 peersCallID;
289 + __u8 resultCode;
290 + __u8 generalErrorCode;
291 + __u16 causeCode;
292 + __u32 connectSpeed;
293 + __u16 packetWindow;
294 + __u16 packetProcDelay;
295 + __u32 physChannelID;
296 +};
297 +
298 +struct PptpInCallRequest {
299 + __u16 callID;
300 + __u16 callSerialNumber;
301 + __u32 callBearerType;
302 + __u32 physChannelID;
303 + __u16 dialedNumberLength;
304 + __u16 dialingNumberLength;
305 + __u8 dialedNumber[64];
306 + __u8 dialingNumber[64];
307 + __u8 subAddress[64];
308 +};
309 +
310 +/* PptpInCallResultCode */
311 +#define PPTP_INCALL_ACCEPT 1
312 +#define PPTP_INCALL_GENERAL_ERROR 2
313 +#define PPTP_INCALL_DONT_ACCEPT 3
314 +
315 +struct PptpInCallReply {
316 + __u16 callID;
317 + __u16 peersCallID;
318 + __u8 resultCode;
319 + __u8 generalErrorCode;
320 + __u16 packetWindow;
321 + __u16 packetProcDelay;
322 + __u16 reserved;
323 +};
324 +
325 +struct PptpInCallConnected {
326 + __u16 peersCallID;
327 + __u16 reserved;
328 + __u32 connectSpeed;
329 + __u16 packetWindow;
330 + __u16 packetProcDelay;
331 + __u32 callFramingType;
332 +};
333 +
334 +struct PptpClearCallRequest {
335 + __u16 callID;
336 + __u16 reserved;
337 +};
338 +
339 +struct PptpCallDisconnectNotify {
340 + __u16 callID;
341 + __u8 resultCode;
342 + __u8 generalErrorCode;
343 + __u16 causeCode;
344 + __u16 reserved;
345 + __u8 callStatistics[128];
346 +};
347 +
348 +struct PptpWanErrorNotify {
349 + __u16 peersCallID;
350 + __u16 reserved;
351 + __u32 crcErrors;
352 + __u32 framingErrors;
353 + __u32 hardwareOverRuns;
354 + __u32 bufferOverRuns;
355 + __u32 timeoutErrors;
356 + __u32 alignmentErrors;
357 +};
358 +
359 +struct PptpSetLinkInfo {
360 + __u16 peersCallID;
361 + __u16 reserved;
362 + __u32 sendAccm;
363 + __u32 recvAccm;
364 +};
365 +
366 +
367 +struct pptp_priv_data {
368 + __u16 call_id;
369 + __u16 mcall_id;
370 + __u16 pcall_id;
371 +};
372 +
373 +#endif /* __KERNEL__ */
374 +#endif /* _CONNTRACK_PPTP_H */
375 diff -urN linux.old/include/linux/netfilter_ipv4/ip_conntrack_proto_gre.h linux.dev/include/linux/netfilter_ipv4/ip_conntrack_proto_gre.h
376 --- linux.old/include/linux/netfilter_ipv4/ip_conntrack_proto_gre.h 1970-01-01 01:00:00.000000000 +0100
377 +++ linux.dev/include/linux/netfilter_ipv4/ip_conntrack_proto_gre.h 2006-12-06 00:34:27.000000000 +0100
378 @@ -0,0 +1,123 @@
379 +#ifndef _CONNTRACK_PROTO_GRE_H
380 +#define _CONNTRACK_PROTO_GRE_H
381 +#include <asm/byteorder.h>
382 +
383 +/* GRE PROTOCOL HEADER */
384 +
385 +/* GRE Version field */
386 +#define GRE_VERSION_1701 0x0
387 +#define GRE_VERSION_PPTP 0x1
388 +
389 +/* GRE Protocol field */
390 +#define GRE_PROTOCOL_PPTP 0x880B
391 +
392 +/* GRE Flags */
393 +#define GRE_FLAG_C 0x80
394 +#define GRE_FLAG_R 0x40
395 +#define GRE_FLAG_K 0x20
396 +#define GRE_FLAG_S 0x10
397 +#define GRE_FLAG_A 0x80
398 +
399 +#define GRE_IS_C(f) ((f)&GRE_FLAG_C)
400 +#define GRE_IS_R(f) ((f)&GRE_FLAG_R)
401 +#define GRE_IS_K(f) ((f)&GRE_FLAG_K)
402 +#define GRE_IS_S(f) ((f)&GRE_FLAG_S)
403 +#define GRE_IS_A(f) ((f)&GRE_FLAG_A)
404 +
405 +/* GRE is a mess: Four different standards */
406 +struct gre_hdr {
407 +#if defined(__LITTLE_ENDIAN_BITFIELD)
408 + __u16 rec:3,
409 + srr:1,
410 + seq:1,
411 + key:1,
412 + routing:1,
413 + csum:1,
414 + version:3,
415 + reserved:4,
416 + ack:1;
417 +#elif defined(__BIG_ENDIAN_BITFIELD)
418 + __u16 csum:1,
419 + routing:1,
420 + key:1,
421 + seq:1,
422 + srr:1,
423 + rec:3,
424 + ack:1,
425 + reserved:4,
426 + version:3;
427 +#else
428 +#error "Adjust your <asm/byteorder.h> defines"
429 +#endif
430 + __u16 protocol;
431 +};
432 +
433 +/* modified GRE header for PPTP */
434 +struct gre_hdr_pptp {
435 + __u8 flags; /* bitfield */
436 + __u8 version; /* should be GRE_VERSION_PPTP */
437 + __u16 protocol; /* should be GRE_PROTOCOL_PPTP */
438 + __u16 payload_len; /* size of ppp payload, not inc. gre header */
439 + __u16 call_id; /* peer's call_id for this session */
440 + __u32 seq; /* sequence number. Present if S==1 */
441 + __u32 ack; /* seq number of highest packet recieved by */
442 + /* sender in this session */
443 +};
444 +
445 +
446 +/* this is part of ip_conntrack */
447 +struct ip_ct_gre {
448 + unsigned int stream_timeout;
449 + unsigned int timeout;
450 +};
451 +
452 +/* this is part of ip_conntrack_expect */
453 +struct ip_ct_gre_expect {
454 + struct ip_ct_gre_keymap *keymap_orig, *keymap_reply;
455 +};
456 +
457 +#ifdef __KERNEL__
458 +struct ip_conntrack_expect;
459 +
460 +/* structure for original <-> reply keymap */
461 +struct ip_ct_gre_keymap {
462 + struct list_head list;
463 +
464 + struct ip_conntrack_tuple tuple;
465 +};
466 +
467 +
468 +/* add new tuple->key_reply pair to keymap */
469 +int ip_ct_gre_keymap_add(struct ip_conntrack_expect *exp,
470 + struct ip_conntrack_tuple *t,
471 + int reply);
472 +
473 +/* change an existing keymap entry */
474 +void ip_ct_gre_keymap_change(struct ip_ct_gre_keymap *km,
475 + struct ip_conntrack_tuple *t);
476 +
477 +/* delete keymap entries */
478 +void ip_ct_gre_keymap_destroy(struct ip_conntrack_expect *exp);
479 +
480 +
481 +/* get pointer to gre key, if present */
482 +static inline u_int32_t *gre_key(struct gre_hdr *greh)
483 +{
484 + if (!greh->key)
485 + return NULL;
486 + if (greh->csum || greh->routing)
487 + return (u_int32_t *) (greh+sizeof(*greh)+4);
488 + return (u_int32_t *) (greh+sizeof(*greh));
489 +}
490 +
491 +/* get pointer ot gre csum, if present */
492 +static inline u_int16_t *gre_csum(struct gre_hdr *greh)
493 +{
494 + if (!greh->csum)
495 + return NULL;
496 + return (u_int16_t *) (greh+sizeof(*greh));
497 +}
498 +
499 +#endif /* __KERNEL__ */
500 +
501 +#endif /* _CONNTRACK_PROTO_GRE_H */
502 diff -urN linux.old/include/linux/netfilter_ipv4/ip_conntrack_tuple.h linux.dev/include/linux/netfilter_ipv4/ip_conntrack_tuple.h
503 --- linux.old/include/linux/netfilter_ipv4/ip_conntrack_tuple.h 2003-11-28 19:26:21.000000000 +0100
504 +++ linux.dev/include/linux/netfilter_ipv4/ip_conntrack_tuple.h 2006-12-06 00:34:27.000000000 +0100
505 @@ -14,7 +14,7 @@
506 union ip_conntrack_manip_proto
507 {
508 /* Add other protocols here. */
509 - u_int16_t all;
510 + u_int32_t all;
511
512 struct {
513 u_int16_t port;
514 @@ -25,6 +25,9 @@
515 struct {
516 u_int16_t id;
517 } icmp;
518 + struct {
519 + u_int32_t key;
520 + } gre;
521 };
522
523 /* The manipulable part of the tuple. */
524 @@ -44,7 +47,7 @@
525 u_int32_t ip;
526 union {
527 /* Add other protocols here. */
528 - u_int16_t all;
529 + u_int32_t all;
530
531 struct {
532 u_int16_t port;
533 @@ -55,6 +58,9 @@
534 struct {
535 u_int8_t type, code;
536 } icmp;
537 + struct {
538 + u_int32_t key;
539 + } gre;
540 } u;
541
542 /* The protocol. */
543 @@ -80,10 +86,16 @@
544 #ifdef __KERNEL__
545
546 #define DUMP_TUPLE(tp) \
547 -DEBUGP("tuple %p: %u %u.%u.%u.%u:%hu -> %u.%u.%u.%u:%hu\n", \
548 +DEBUGP("tuple %p: %u %u.%u.%u.%u:%u -> %u.%u.%u.%u:%u\n", \
549 (tp), (tp)->dst.protonum, \
550 - NIPQUAD((tp)->src.ip), ntohs((tp)->src.u.all), \
551 - NIPQUAD((tp)->dst.ip), ntohs((tp)->dst.u.all))
552 + NIPQUAD((tp)->src.ip), ntohl((tp)->src.u.all), \
553 + NIPQUAD((tp)->dst.ip), ntohl((tp)->dst.u.all))
554 +
555 +#define DUMP_TUPLE_RAW(x) \
556 + DEBUGP("tuple %p: %u %u.%u.%u.%u:0x%08x -> %u.%u.%u.%u:0x%08x\n",\
557 + (x), (x)->dst.protonum, \
558 + NIPQUAD((x)->src.ip), ntohl((x)->src.u.all), \
559 + NIPQUAD((x)->dst.ip), ntohl((x)->dst.u.all))
560
561 #define CTINFO2DIR(ctinfo) ((ctinfo) >= IP_CT_IS_REPLY ? IP_CT_DIR_REPLY : IP_CT_DIR_ORIGINAL)
562
563 diff -urN linux.old/include/linux/netfilter_ipv4/ip_nat_pptp.h linux.dev/include/linux/netfilter_ipv4/ip_nat_pptp.h
564 --- linux.old/include/linux/netfilter_ipv4/ip_nat_pptp.h 1970-01-01 01:00:00.000000000 +0100
565 +++ linux.dev/include/linux/netfilter_ipv4/ip_nat_pptp.h 2006-12-06 00:34:27.000000000 +0100
566 @@ -0,0 +1,11 @@
567 +/* PPTP constants and structs */
568 +#ifndef _NAT_PPTP_H
569 +#define _NAT_PPTP_H
570 +
571 +/* conntrack private data */
572 +struct ip_nat_pptp {
573 + u_int16_t pns_call_id; /* NAT'ed PNS call id */
574 + u_int16_t pac_call_id; /* NAT'ed PAC call id */
575 +};
576 +
577 +#endif /* _NAT_PPTP_H */
578 diff -urN linux.old/net/ipv4/netfilter/Config.in linux.dev/net/ipv4/netfilter/Config.in
579 --- linux.old/net/ipv4/netfilter/Config.in 2006-12-06 00:31:46.000000000 +0100
580 +++ linux.dev/net/ipv4/netfilter/Config.in 2006-12-06 00:34:27.000000000 +0100
581 @@ -10,6 +10,8 @@
582 dep_tristate ' Amanda protocol support' CONFIG_IP_NF_AMANDA $CONFIG_IP_NF_CONNTRACK
583 dep_tristate ' TFTP protocol support' CONFIG_IP_NF_TFTP $CONFIG_IP_NF_CONNTRACK
584 dep_tristate ' IRC protocol support' CONFIG_IP_NF_IRC $CONFIG_IP_NF_CONNTRACK
585 + dep_tristate ' GRE protocol support' CONFIG_IP_NF_CT_PROTO_GRE $CONFIG_IP_NF_CONNTRACK
586 + dep_tristate ' PPTP protocol support' CONFIG_IP_NF_PPTP $CONFIG_IP_NF_CT_PROTO_GRE
587 fi
588
589 if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
590 @@ -64,6 +66,20 @@
591 define_bool CONFIG_IP_NF_NAT_NEEDED y
592 dep_tristate ' MASQUERADE target support' CONFIG_IP_NF_TARGET_MASQUERADE $CONFIG_IP_NF_NAT
593 dep_tristate ' REDIRECT target support' CONFIG_IP_NF_TARGET_REDIRECT $CONFIG_IP_NF_NAT
594 + if [ "$CONFIG_IP_NF_PPTP" = "m" ]; then
595 + define_tristate CONFIG_IP_NF_NAT_PPTP m
596 + else
597 + if [ "$CONFIG_IP_NF_PPTP" = "y" ]; then
598 + define_tristate CONFIG_IP_NF_NAT_PPTP $CONFIG_IP_NF_NAT
599 + fi
600 + fi
601 + if [ "$CONFIG_IP_NF_CT_PROTO_GRE" = "m" ]; then
602 + define_tristate CONFIG_IP_NF_NAT_PROTO_GRE m
603 + else
604 + if [ "$CONFIG_IP_NF_CT_PROTO_GRE" = "y" ]; then
605 + define_tristate CONFIG_IP_NF_NAT_PROTO_GRE $CONFIG_IP_NF_NAT
606 + fi
607 + fi
608 if [ "$CONFIG_IP_NF_AMANDA" = "m" ]; then
609 define_tristate CONFIG_IP_NF_NAT_AMANDA m
610 else
611 diff -urN linux.old/net/ipv4/netfilter/ip_conntrack_core.c linux.dev/net/ipv4/netfilter/ip_conntrack_core.c
612 --- linux.old/net/ipv4/netfilter/ip_conntrack_core.c 2006-12-06 00:31:46.000000000 +0100
613 +++ linux.dev/net/ipv4/netfilter/ip_conntrack_core.c 2006-12-06 00:34:27.000000000 +0100
614 @@ -143,6 +143,8 @@
615 tuple->dst.ip = iph->daddr;
616 tuple->dst.protonum = iph->protocol;
617
618 + tuple->src.u.all = tuple->dst.u.all = 0;
619 +
620 ret = protocol->pkt_to_tuple((u_int32_t *)iph + iph->ihl,
621 len - 4*iph->ihl,
622 tuple);
623 @@ -158,6 +160,8 @@
624 inverse->dst.ip = orig->src.ip;
625 inverse->dst.protonum = orig->dst.protonum;
626
627 + inverse->src.u.all = inverse->dst.u.all = 0;
628 +
629 return protocol->invert_tuple(inverse, orig);
630 }
631
632 @@ -970,8 +974,8 @@
633 * so there is no need to use the tuple lock too */
634
635 DEBUGP("ip_conntrack_expect_related %p\n", related_to);
636 - DEBUGP("tuple: "); DUMP_TUPLE(&expect->tuple);
637 - DEBUGP("mask: "); DUMP_TUPLE(&expect->mask);
638 + DEBUGP("tuple: "); DUMP_TUPLE_RAW(&expect->tuple);
639 + DEBUGP("mask: "); DUMP_TUPLE_RAW(&expect->mask);
640
641 old = LIST_FIND(&ip_conntrack_expect_list, resent_expect,
642 struct ip_conntrack_expect *, &expect->tuple,
643 @@ -1089,15 +1093,14 @@
644
645 MUST_BE_READ_LOCKED(&ip_conntrack_lock);
646 WRITE_LOCK(&ip_conntrack_expect_tuple_lock);
647 -
648 DEBUGP("change_expect:\n");
649 - DEBUGP("exp tuple: "); DUMP_TUPLE(&expect->tuple);
650 - DEBUGP("exp mask: "); DUMP_TUPLE(&expect->mask);
651 - DEBUGP("newtuple: "); DUMP_TUPLE(newtuple);
652 + DEBUGP("exp tuple: "); DUMP_TUPLE_RAW(&expect->tuple);
653 + DEBUGP("exp mask: "); DUMP_TUPLE_RAW(&expect->mask);
654 + DEBUGP("newtuple: "); DUMP_TUPLE_RAW(newtuple);
655 if (expect->ct_tuple.dst.protonum == 0) {
656 /* Never seen before */
657 DEBUGP("change expect: never seen before\n");
658 - if (!ip_ct_tuple_equal(&expect->tuple, newtuple)
659 + if (!ip_ct_tuple_mask_cmp(&expect->tuple, newtuple, &expect->mask)
660 && LIST_FIND(&ip_conntrack_expect_list, expect_clash,
661 struct ip_conntrack_expect *, newtuple, &expect->mask)) {
662 /* Force NAT to find an unused tuple */
663 diff -urN linux.old/net/ipv4/netfilter/ip_conntrack_pptp.c linux.dev/net/ipv4/netfilter/ip_conntrack_pptp.c
664 --- linux.old/net/ipv4/netfilter/ip_conntrack_pptp.c 1970-01-01 01:00:00.000000000 +0100
665 +++ linux.dev/net/ipv4/netfilter/ip_conntrack_pptp.c 2006-12-06 00:34:27.000000000 +0100
666 @@ -0,0 +1,633 @@
667 +/*
668 + * ip_conntrack_pptp.c - Version 1.9
669 + *
670 + * Connection tracking support for PPTP (Point to Point Tunneling Protocol).
671 + * PPTP is a a protocol for creating virtual private networks.
672 + * It is a specification defined by Microsoft and some vendors
673 + * working with Microsoft. PPTP is built on top of a modified
674 + * version of the Internet Generic Routing Encapsulation Protocol.
675 + * GRE is defined in RFC 1701 and RFC 1702. Documentation of
676 + * PPTP can be found in RFC 2637
677 + *
678 + * (C) 2000-2003 by Harald Welte <laforge@gnumonks.org>
679 + *
680 + * Development of this code funded by Astaro AG (http://www.astaro.com/)
681 + *
682 + * Limitations:
683 + * - We blindly assume that control connections are always
684 + * established in PNS->PAC direction. This is a violation
685 + * of RFFC2673
686 + *
687 + * TODO: - finish support for multiple calls within one session
688 + * (needs expect reservations in newnat)
689 + * - testing of incoming PPTP calls
690 + *
691 + * Changes:
692 + * 2002-02-05 - Version 1.3
693 + * - Call ip_conntrack_unexpect_related() from
694 + * pptp_timeout_related() to destroy expectations in case
695 + * CALL_DISCONNECT_NOTIFY or tcp fin packet was seen
696 + * (Philip Craig <philipc@snapgear.com>)
697 + * - Add Version information at module loadtime
698 + * 2002-02-10 - Version 1.6
699 + * - move to C99 style initializers
700 + * - remove second expectation if first arrives
701 + *
702 + */
703 +
704 +#include <linux/config.h>
705 +#include <linux/module.h>
706 +#include <linux/netfilter.h>
707 +#include <linux/ip.h>
708 +#include <net/checksum.h>
709 +#include <net/tcp.h>
710 +
711 +#include <linux/netfilter_ipv4/lockhelp.h>
712 +#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
713 +#include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
714 +#include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
715 +
716 +#define IP_CT_PPTP_VERSION "1.9"
717 +
718 +MODULE_LICENSE("GPL");
719 +MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
720 +MODULE_DESCRIPTION("Netfilter connection tracking helper module for PPTP");
721 +
722 +DECLARE_LOCK(ip_pptp_lock);
723 +
724 +#if 0
725 +#include "ip_conntrack_pptp_priv.h"
726 +#define DEBUGP(format, args...) printk(KERN_DEBUG __FILE__ ":" __FUNCTION__ \
727 + ": " format, ## args)
728 +#else
729 +#define DEBUGP(format, args...)
730 +#endif
731 +
732 +#define SECS *HZ
733 +#define MINS * 60 SECS
734 +#define HOURS * 60 MINS
735 +#define DAYS * 24 HOURS
736 +
737 +#define PPTP_GRE_TIMEOUT (10 MINS)
738 +#define PPTP_GRE_STREAM_TIMEOUT (5 DAYS)
739 +
740 +static int pptp_expectfn(struct ip_conntrack *ct)
741 +{
742 + struct ip_conntrack *master;
743 + struct ip_conntrack_expect *exp;
744 +
745 + DEBUGP("increasing timeouts\n");
746 + /* increase timeout of GRE data channel conntrack entry */
747 + ct->proto.gre.timeout = PPTP_GRE_TIMEOUT;
748 + ct->proto.gre.stream_timeout = PPTP_GRE_STREAM_TIMEOUT;
749 +
750 + master = master_ct(ct);
751 + if (!master) {
752 + DEBUGP(" no master!!!\n");
753 + return 0;
754 + }
755 +
756 + exp = ct->master;
757 + if (!exp) {
758 + DEBUGP("no expectation!!\n");
759 + return 0;
760 + }
761 +
762 + DEBUGP("completing tuples with ct info\n");
763 + /* we can do this, since we're unconfirmed */
764 + if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.gre.key ==
765 + htonl(master->help.ct_pptp_info.pac_call_id)) {
766 + /* assume PNS->PAC */
767 + ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.gre.key =
768 + htonl(master->help.ct_pptp_info.pns_call_id);
769 + ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.gre.key =
770 + htonl(master->help.ct_pptp_info.pns_call_id);
771 + } else {
772 + /* assume PAC->PNS */
773 + ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.gre.key =
774 + htonl(master->help.ct_pptp_info.pac_call_id);
775 + ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.gre.key =
776 + htonl(master->help.ct_pptp_info.pac_call_id);
777 + }
778 +
779 + /* delete other expectation */
780 + if (exp->expected_list.next != &exp->expected_list) {
781 + struct ip_conntrack_expect *other_exp;
782 + struct list_head *cur_item, *next;
783 +
784 + for (cur_item = master->sibling_list.next;
785 + cur_item != &master->sibling_list; cur_item = next) {
786 + next = cur_item->next;
787 + other_exp = list_entry(cur_item,
788 + struct ip_conntrack_expect,
789 + expected_list);
790 + /* remove only if occurred at same sequence number */
791 + if (other_exp != exp && other_exp->seq == exp->seq) {
792 + DEBUGP("unexpecting other direction\n");
793 + ip_ct_gre_keymap_destroy(other_exp);
794 + ip_conntrack_unexpect_related(other_exp);
795 + }
796 + }
797 + }
798 +
799 + return 0;
800 +}
801 +
802 +/* timeout GRE data connections */
803 +static int pptp_timeout_related(struct ip_conntrack *ct)
804 +{
805 + struct list_head *cur_item, *next;
806 + struct ip_conntrack_expect *exp;
807 +
808 + /* FIXME: do we have to lock something ? */
809 + for (cur_item = ct->sibling_list.next;
810 + cur_item != &ct->sibling_list; cur_item = next) {
811 + next = cur_item->next;
812 + exp = list_entry(cur_item, struct ip_conntrack_expect,
813 + expected_list);
814 +
815 + ip_ct_gre_keymap_destroy(exp);
816 + if (!exp->sibling) {
817 + ip_conntrack_unexpect_related(exp);
818 + continue;
819 + }
820 +
821 + DEBUGP("setting timeout of conntrack %p to 0\n",
822 + exp->sibling);
823 + exp->sibling->proto.gre.timeout = 0;
824 + exp->sibling->proto.gre.stream_timeout = 0;
825 + ip_ct_refresh(exp->sibling, 0);
826 + }
827 +
828 + return 0;
829 +}
830 +
831 +/* expect GRE connections (PNS->PAC and PAC->PNS direction) */
832 +static inline int
833 +exp_gre(struct ip_conntrack *master,
834 + u_int32_t seq,
835 + u_int16_t callid,
836 + u_int16_t peer_callid)
837 +{
838 + struct ip_conntrack_expect exp;
839 + struct ip_conntrack_tuple inv_tuple;
840 +
841 + memset(&exp, 0, sizeof(exp));
842 + /* tuple in original direction, PNS->PAC */
843 + exp.tuple.src.ip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
844 + exp.tuple.src.u.gre.key = htonl(ntohs(peer_callid));
845 + exp.tuple.dst.ip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
846 + exp.tuple.dst.u.gre.key = htonl(ntohs(callid));
847 + exp.tuple.dst.protonum = IPPROTO_GRE;
848 +
849 + exp.mask.src.ip = 0xffffffff;
850 + exp.mask.src.u.all = 0;
851 + exp.mask.dst.u.all = 0;
852 + exp.mask.dst.u.gre.key = 0xffffffff;
853 + exp.mask.dst.ip = 0xffffffff;
854 + exp.mask.dst.protonum = 0xffff;
855 +
856 + exp.seq = seq;
857 + exp.expectfn = pptp_expectfn;
858 +
859 + exp.help.exp_pptp_info.pac_call_id = ntohs(callid);
860 + exp.help.exp_pptp_info.pns_call_id = ntohs(peer_callid);
861 +
862 + DEBUGP("calling expect_related ");
863 + DUMP_TUPLE_RAW(&exp.tuple);
864 +
865 + /* Add GRE keymap entries */
866 + if (ip_ct_gre_keymap_add(&exp, &exp.tuple, 0) != 0)
867 + return 1;
868 +
869 + invert_tuplepr(&inv_tuple, &exp.tuple);
870 + if (ip_ct_gre_keymap_add(&exp, &inv_tuple, 1) != 0) {
871 + ip_ct_gre_keymap_destroy(&exp);
872 + return 1;
873 + }
874 +
875 + if (ip_conntrack_expect_related(master, &exp) != 0) {
876 + ip_ct_gre_keymap_destroy(&exp);
877 + DEBUGP("cannot expect_related()\n");
878 + return 1;
879 + }
880 +
881 + /* tuple in reply direction, PAC->PNS */
882 + exp.tuple.src.ip = master->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip;
883 + exp.tuple.src.u.gre.key = htonl(ntohs(callid));
884 + exp.tuple.dst.ip = master->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
885 + exp.tuple.dst.u.gre.key = htonl(ntohs(peer_callid));
886 +
887 + DEBUGP("calling expect_related ");
888 + DUMP_TUPLE_RAW(&exp.tuple);
889 +
890 + /* Add GRE keymap entries */
891 + ip_ct_gre_keymap_add(&exp, &exp.tuple, 0);
892 + invert_tuplepr(&inv_tuple, &exp.tuple);
893 + ip_ct_gre_keymap_add(&exp, &inv_tuple, 1);
894 + /* FIXME: cannot handle error correctly, since we need to free
895 + * the above keymap :( */
896 +
897 + if (ip_conntrack_expect_related(master, &exp) != 0) {
898 + /* free the second pair of keypmaps */
899 + ip_ct_gre_keymap_destroy(&exp);
900 + DEBUGP("cannot expect_related():\n");
901 + return 1;
902 + }
903 +
904 + return 0;
905 +}
906 +
907 +static inline int
908 +pptp_inbound_pkt(struct tcphdr *tcph,
909 + struct pptp_pkt_hdr *pptph,
910 + size_t datalen,
911 + struct ip_conntrack *ct,
912 + enum ip_conntrack_info ctinfo)
913 +{
914 + struct PptpControlHeader *ctlh;
915 + union pptp_ctrl_union pptpReq;
916 +
917 + struct ip_ct_pptp_master *info = &ct->help.ct_pptp_info;
918 + u_int16_t msg, *cid, *pcid;
919 + u_int32_t seq;
920 +
921 + ctlh = (struct PptpControlHeader *)
922 + ((char *) pptph + sizeof(struct pptp_pkt_hdr));
923 + pptpReq.rawreq = (void *)
924 + ((char *) ctlh + sizeof(struct PptpControlHeader));
925 +
926 + msg = ntohs(ctlh->messageType);
927 + DEBUGP("inbound control message %s\n", strMName[msg]);
928 +
929 + switch (msg) {
930 + case PPTP_START_SESSION_REPLY:
931 + /* server confirms new control session */
932 + if (info->sstate < PPTP_SESSION_REQUESTED) {
933 + DEBUGP("%s without START_SESS_REQUEST\n",
934 + strMName[msg]);
935 + break;
936 + }
937 + if (pptpReq.srep->resultCode == PPTP_START_OK)
938 + info->sstate = PPTP_SESSION_CONFIRMED;
939 + else
940 + info->sstate = PPTP_SESSION_ERROR;
941 + break;
942 +
943 + case PPTP_STOP_SESSION_REPLY:
944 + /* server confirms end of control session */
945 + if (info->sstate > PPTP_SESSION_STOPREQ) {
946 + DEBUGP("%s without STOP_SESS_REQUEST\n",
947 + strMName[msg]);
948 + break;
949 + }
950 + if (pptpReq.strep->resultCode == PPTP_STOP_OK)
951 + info->sstate = PPTP_SESSION_NONE;
952 + else
953 + info->sstate = PPTP_SESSION_ERROR;
954 + break;
955 +
956 + case PPTP_OUT_CALL_REPLY:
957 + /* server accepted call, we now expect GRE frames */
958 + if (info->sstate != PPTP_SESSION_CONFIRMED) {
959 + DEBUGP("%s but no session\n", strMName[msg]);
960 + break;
961 + }
962 + if (info->cstate != PPTP_CALL_OUT_REQ &&
963 + info->cstate != PPTP_CALL_OUT_CONF) {
964 + DEBUGP("%s without OUTCALL_REQ\n", strMName[msg]);
965 + break;
966 + }
967 + if (pptpReq.ocack->resultCode != PPTP_OUTCALL_CONNECT) {
968 + info->cstate = PPTP_CALL_NONE;
969 + break;
970 + }
971 +
972 + cid = &pptpReq.ocack->callID;
973 + pcid = &pptpReq.ocack->peersCallID;
974 +
975 + info->pac_call_id = ntohs(*cid);
976 +
977 + if (htons(info->pns_call_id) != *pcid) {
978 + DEBUGP("%s for unknown callid %u\n",
979 + strMName[msg], ntohs(*pcid));
980 + break;
981 + }
982 +
983 + DEBUGP("%s, CID=%X, PCID=%X\n", strMName[msg],
984 + ntohs(*cid), ntohs(*pcid));
985 +
986 + info->cstate = PPTP_CALL_OUT_CONF;
987 +
988 + seq = ntohl(tcph->seq) + ((void *)pcid - (void *)pptph);
989 + if (exp_gre(ct, seq, *cid, *pcid) != 0)
990 + printk("ip_conntrack_pptp: error during exp_gre\n");
991 + break;
992 +
993 + case PPTP_IN_CALL_REQUEST:
994 + /* server tells us about incoming call request */
995 + if (info->sstate != PPTP_SESSION_CONFIRMED) {
996 + DEBUGP("%s but no session\n", strMName[msg]);
997 + break;
998 + }
999 + pcid = &pptpReq.icack->peersCallID;
1000 + DEBUGP("%s, PCID=%X\n", strMName[msg], ntohs(*pcid));
1001 + info->cstate = PPTP_CALL_IN_REQ;
1002 + info->pac_call_id= ntohs(*pcid);
1003 + break;
1004 +
1005 + case PPTP_IN_CALL_CONNECT:
1006 + /* server tells us about incoming call established */
1007 + if (info->sstate != PPTP_SESSION_CONFIRMED) {
1008 + DEBUGP("%s but no session\n", strMName[msg]);
1009 + break;
1010 + }
1011 + if (info->sstate != PPTP_CALL_IN_REP
1012 + && info->sstate != PPTP_CALL_IN_CONF) {
1013 + DEBUGP("%s but never sent IN_CALL_REPLY\n",
1014 + strMName[msg]);
1015 + break;
1016 + }
1017 +
1018 + pcid = &pptpReq.iccon->peersCallID;
1019 + cid = &info->pac_call_id;
1020 +
1021 + if (info->pns_call_id != ntohs(*pcid)) {
1022 + DEBUGP("%s for unknown CallID %u\n",
1023 + strMName[msg], ntohs(*cid));
1024 + break;
1025 + }
1026 +
1027 + DEBUGP("%s, PCID=%X\n", strMName[msg], ntohs(*pcid));
1028 + info->cstate = PPTP_CALL_IN_CONF;
1029 +
1030 + /* we expect a GRE connection from PAC to PNS */
1031 + seq = ntohl(tcph->seq) + ((void *)pcid - (void *)pptph);
1032 + if (exp_gre(ct, seq, *cid, *pcid) != 0)
1033 + printk("ip_conntrack_pptp: error during exp_gre\n");
1034 +
1035 + break;
1036 +
1037 + case PPTP_CALL_DISCONNECT_NOTIFY:
1038 + /* server confirms disconnect */
1039 + cid = &pptpReq.disc->callID;
1040 + DEBUGP("%s, CID=%X\n", strMName[msg], ntohs(*cid));
1041 + info->cstate = PPTP_CALL_NONE;
1042 +
1043 + /* untrack this call id, unexpect GRE packets */
1044 + pptp_timeout_related(ct);
1045 + break;
1046 +
1047 + case PPTP_WAN_ERROR_NOTIFY:
1048 + break;
1049 +
1050 + case PPTP_ECHO_REQUEST:
1051 + case PPTP_ECHO_REPLY:
1052 + /* I don't have to explain these ;) */
1053 + break;
1054 + default:
1055 + DEBUGP("invalid %s (TY=%d)\n", (msg <= PPTP_MSG_MAX)
1056 + ? strMName[msg]:strMName[0], msg);
1057 + break;
1058 + }
1059 +
1060 + return NF_ACCEPT;
1061 +
1062 +}
1063 +
1064 +static inline int
1065 +pptp_outbound_pkt(struct tcphdr *tcph,
1066 + struct pptp_pkt_hdr *pptph,
1067 + size_t datalen,
1068 + struct ip_conntrack *ct,
1069 + enum ip_conntrack_info ctinfo)
1070 +{
1071 + struct PptpControlHeader *ctlh;
1072 + union pptp_ctrl_union pptpReq;
1073 + struct ip_ct_pptp_master *info = &ct->help.ct_pptp_info;
1074 + u_int16_t msg, *cid, *pcid;
1075 +
1076 + ctlh = (struct PptpControlHeader *) ((void *) pptph + sizeof(*pptph));
1077 + pptpReq.rawreq = (void *) ((void *) ctlh + sizeof(*ctlh));
1078 +
1079 + msg = ntohs(ctlh->messageType);
1080 + DEBUGP("outbound control message %s\n", strMName[msg]);
1081 +
1082 + switch (msg) {
1083 + case PPTP_START_SESSION_REQUEST:
1084 + /* client requests for new control session */
1085 + if (info->sstate != PPTP_SESSION_NONE) {
1086 + DEBUGP("%s but we already have one",
1087 + strMName[msg]);
1088 + }
1089 + info->sstate = PPTP_SESSION_REQUESTED;
1090 + break;
1091 + case PPTP_STOP_SESSION_REQUEST:
1092 + /* client requests end of control session */
1093 + info->sstate = PPTP_SESSION_STOPREQ;
1094 + break;
1095 +
1096 + case PPTP_OUT_CALL_REQUEST:
1097 + /* client initiating connection to server */
1098 + if (info->sstate != PPTP_SESSION_CONFIRMED) {
1099 + DEBUGP("%s but no session\n",
1100 + strMName[msg]);
1101 + break;
1102 + }
1103 + info->cstate = PPTP_CALL_OUT_REQ;
1104 + /* track PNS call id */
1105 + cid = &pptpReq.ocreq->callID;
1106 + DEBUGP("%s, CID=%X\n", strMName[msg], ntohs(*cid));
1107 + info->pns_call_id = ntohs(*cid);
1108 + break;
1109 + case PPTP_IN_CALL_REPLY:
1110 + /* client answers incoming call */
1111 + if (info->cstate != PPTP_CALL_IN_REQ
1112 + && info->cstate != PPTP_CALL_IN_REP) {
1113 + DEBUGP("%s without incall_req\n",
1114 + strMName[msg]);
1115 + break;
1116 + }
1117 + if (pptpReq.icack->resultCode != PPTP_INCALL_ACCEPT) {
1118 + info->cstate = PPTP_CALL_NONE;
1119 + break;
1120 + }
1121 + pcid = &pptpReq.icack->peersCallID;
1122 + if (info->pac_call_id != ntohs(*pcid)) {
1123 + DEBUGP("%s for unknown call %u\n",
1124 + strMName[msg], ntohs(*pcid));
1125 + break;
1126 + }
1127 + DEBUGP("%s, CID=%X\n", strMName[msg], ntohs(*pcid));
1128 + /* part two of the three-way handshake */
1129 + info->cstate = PPTP_CALL_IN_REP;
1130 + info->pns_call_id = ntohs(pptpReq.icack->callID);
1131 + break;
1132 +
1133 + case PPTP_CALL_CLEAR_REQUEST:
1134 + /* client requests hangup of call */
1135 + if (info->sstate != PPTP_SESSION_CONFIRMED) {
1136 + DEBUGP("CLEAR_CALL but no session\n");
1137 + break;
1138 + }
1139 + /* FUTURE: iterate over all calls and check if
1140 + * call ID is valid. We don't do this without newnat,
1141 + * because we only know about last call */
1142 + info->cstate = PPTP_CALL_CLEAR_REQ;
1143 + break;
1144 + case PPTP_SET_LINK_INFO:
1145 + break;
1146 + case PPTP_ECHO_REQUEST:
1147 + case PPTP_ECHO_REPLY:
1148 + /* I don't have to explain these ;) */
1149 + break;
1150 + default:
1151 + DEBUGP("invalid %s (TY=%d)\n", (msg <= PPTP_MSG_MAX)?
1152 + strMName[msg]:strMName[0], msg);
1153 + /* unknown: no need to create GRE masq table entry */
1154 + break;
1155 + }
1156 +
1157 + return NF_ACCEPT;
1158 +}
1159 +
1160 +
1161 +/* track caller id inside control connection, call expect_related */
1162 +static int
1163 +conntrack_pptp_help(const struct iphdr *iph, size_t len,
1164 + struct ip_conntrack *ct, enum ip_conntrack_info ctinfo)
1165 +
1166 +{
1167 + struct pptp_pkt_hdr *pptph;
1168 +
1169 + struct tcphdr *tcph = (void *) iph + iph->ihl * 4;
1170 + u_int32_t tcplen = len - iph->ihl * 4;
1171 + u_int32_t datalen = tcplen - tcph->doff * 4;
1172 + void *datalimit;
1173 + int dir = CTINFO2DIR(ctinfo);
1174 + struct ip_ct_pptp_master *info = &ct->help.ct_pptp_info;
1175 +
1176 + int oldsstate, oldcstate;
1177 + int ret;
1178 +
1179 + /* don't do any tracking before tcp handshake complete */
1180 + if (ctinfo != IP_CT_ESTABLISHED
1181 + && ctinfo != IP_CT_ESTABLISHED+IP_CT_IS_REPLY) {
1182 + DEBUGP("ctinfo = %u, skipping\n", ctinfo);
1183 + return NF_ACCEPT;
1184 + }
1185 +
1186 + /* not a complete TCP header? */
1187 + if (tcplen < sizeof(struct tcphdr) || tcplen < tcph->doff * 4) {
1188 + DEBUGP("tcplen = %u\n", tcplen);
1189 + return NF_ACCEPT;
1190 + }
1191 +
1192 + /* checksum invalid? */
1193 + if (tcp_v4_check(tcph, tcplen, iph->saddr, iph->daddr,
1194 + csum_partial((char *) tcph, tcplen, 0))) {
1195 + printk(KERN_NOTICE __FILE__ ": bad csum\n");
1196 + /* W2K PPTP server sends TCP packets with wrong checksum :(( */
1197 + //return NF_ACCEPT;
1198 + }
1199 +
1200 + if (tcph->fin || tcph->rst) {
1201 + DEBUGP("RST/FIN received, timeouting GRE\n");
1202 + /* can't do this after real newnat */
1203 + info->cstate = PPTP_CALL_NONE;
1204 +
1205 + /* untrack this call id, unexpect GRE packets */
1206 + pptp_timeout_related(ct);
1207 + }
1208 +
1209 +
1210 + pptph = (struct pptp_pkt_hdr *) ((void *) tcph + tcph->doff * 4);
1211 + datalimit = (void *) pptph + datalen;
1212 +
1213 + /* not a full pptp packet header? */
1214 + if ((void *) pptph+sizeof(*pptph) >= datalimit) {
1215 + DEBUGP("no full PPTP header, can't track\n");
1216 + return NF_ACCEPT;
1217 + }
1218 +
1219 + /* if it's not a control message we can't do anything with it */
1220 + if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
1221 + ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
1222 + DEBUGP("not a control packet\n");
1223 + return NF_ACCEPT;
1224 + }
1225 +
1226 + oldsstate = info->sstate;
1227 + oldcstate = info->cstate;
1228 +
1229 + LOCK_BH(&ip_pptp_lock);
1230 +
1231 + /* FIXME: We just blindly assume that the control connection is always
1232 + * established from PNS->PAC. However, RFC makes no guarantee */
1233 + if (dir == IP_CT_DIR_ORIGINAL)
1234 + /* client -> server (PNS -> PAC) */
1235 + ret = pptp_outbound_pkt(tcph, pptph, datalen, ct, ctinfo);
1236 + else
1237 + /* server -> client (PAC -> PNS) */
1238 + ret = pptp_inbound_pkt(tcph, pptph, datalen, ct, ctinfo);
1239 + DEBUGP("sstate: %d->%d, cstate: %d->%d\n",
1240 + oldsstate, info->sstate, oldcstate, info->cstate);
1241 + UNLOCK_BH(&ip_pptp_lock);
1242 +
1243 + return ret;
1244 +}
1245 +
1246 +/* control protocol helper */
1247 +static struct ip_conntrack_helper pptp = {
1248 + .list = { NULL, NULL },
1249 + .name = "pptp",
1250 + .flags = IP_CT_HELPER_F_REUSE_EXPECT,
1251 + .me = THIS_MODULE,
1252 + .max_expected = 2,
1253 + .timeout = 0,
1254 + .tuple = { .src = { .ip = 0,
1255 + .u = { .tcp = { .port =
1256 + __constant_htons(PPTP_CONTROL_PORT) } }
1257 + },
1258 + .dst = { .ip = 0,
1259 + .u = { .all = 0 },
1260 + .protonum = IPPROTO_TCP
1261 + }
1262 + },
1263 + .mask = { .src = { .ip = 0,
1264 + .u = { .tcp = { .port = 0xffff } }
1265 + },
1266 + .dst = { .ip = 0,
1267 + .u = { .all = 0 },
1268 + .protonum = 0xffff
1269 + }
1270 + },
1271 + .help = conntrack_pptp_help
1272 +};
1273 +
1274 +/* ip_conntrack_pptp initialization */
1275 +static int __init init(void)
1276 +{
1277 + int retcode;
1278 +
1279 + DEBUGP(__FILE__ ": registering helper\n");
1280 + if ((retcode = ip_conntrack_helper_register(&pptp))) {
1281 + printk(KERN_ERR "Unable to register conntrack application "
1282 + "helper for pptp: %d\n", retcode);
1283 + return -EIO;
1284 + }
1285 +
1286 + printk("ip_conntrack_pptp version %s loaded\n", IP_CT_PPTP_VERSION);
1287 + return 0;
1288 +}
1289 +
1290 +static void __exit fini(void)
1291 +{
1292 + ip_conntrack_helper_unregister(&pptp);
1293 + printk("ip_conntrack_pptp version %s unloaded\n", IP_CT_PPTP_VERSION);
1294 +}
1295 +
1296 +module_init(init);
1297 +module_exit(fini);
1298 +
1299 +EXPORT_SYMBOL(ip_pptp_lock);
1300 diff -urN linux.old/net/ipv4/netfilter/ip_conntrack_pptp_priv.h linux.dev/net/ipv4/netfilter/ip_conntrack_pptp_priv.h
1301 --- linux.old/net/ipv4/netfilter/ip_conntrack_pptp_priv.h 1970-01-01 01:00:00.000000000 +0100
1302 +++ linux.dev/net/ipv4/netfilter/ip_conntrack_pptp_priv.h 2006-12-06 00:34:27.000000000 +0100
1303 @@ -0,0 +1,24 @@
1304 +#ifndef _IP_CT_PPTP_PRIV_H
1305 +#define _IP_CT_PPTP_PRIV_H
1306 +
1307 +/* PptpControlMessageType names */
1308 +static const char *strMName[] = {
1309 + "UNKNOWN_MESSAGE",
1310 + "START_SESSION_REQUEST",
1311 + "START_SESSION_REPLY",
1312 + "STOP_SESSION_REQUEST",
1313 + "STOP_SESSION_REPLY",
1314 + "ECHO_REQUEST",
1315 + "ECHO_REPLY",
1316 + "OUT_CALL_REQUEST",
1317 + "OUT_CALL_REPLY",
1318 + "IN_CALL_REQUEST",
1319 + "IN_CALL_REPLY",
1320 + "IN_CALL_CONNECT",
1321 + "CALL_CLEAR_REQUEST",
1322 + "CALL_DISCONNECT_NOTIFY",
1323 + "WAN_ERROR_NOTIFY",
1324 + "SET_LINK_INFO"
1325 +};
1326 +
1327 +#endif
1328 diff -urN linux.old/net/ipv4/netfilter/ip_conntrack_proto_gre.c linux.dev/net/ipv4/netfilter/ip_conntrack_proto_gre.c
1329 --- linux.old/net/ipv4/netfilter/ip_conntrack_proto_gre.c 1970-01-01 01:00:00.000000000 +0100
1330 +++ linux.dev/net/ipv4/netfilter/ip_conntrack_proto_gre.c 2006-12-06 00:34:27.000000000 +0100
1331 @@ -0,0 +1,333 @@
1332 +/*
1333 + * ip_conntrack_proto_gre.c - Version 1.2
1334 + *
1335 + * Connection tracking protocol helper module for GRE.
1336 + *
1337 + * GRE is a generic encapsulation protocol, which is generally not very
1338 + * suited for NAT, as it has no protocol-specific part as port numbers.
1339 + *
1340 + * It has an optional key field, which may help us distinguishing two
1341 + * connections between the same two hosts.
1342 + *
1343 + * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
1344 + *
1345 + * PPTP is built on top of a modified version of GRE, and has a mandatory
1346 + * field called "CallID", which serves us for the same purpose as the key
1347 + * field in plain GRE.
1348 + *
1349 + * Documentation about PPTP can be found in RFC 2637
1350 + *
1351 + * (C) 2000-2003 by Harald Welte <laforge@gnumonks.org>
1352 + *
1353 + * Development of this code funded by Astaro AG (http://www.astaro.com/)
1354 + *
1355 + */
1356 +
1357 +#include <linux/config.h>
1358 +#include <linux/module.h>
1359 +#include <linux/types.h>
1360 +#include <linux/timer.h>
1361 +#include <linux/netfilter.h>
1362 +#include <linux/ip.h>
1363 +#include <linux/in.h>
1364 +#include <linux/list.h>
1365 +
1366 +#include <linux/netfilter_ipv4/lockhelp.h>
1367 +
1368 +DECLARE_RWLOCK(ip_ct_gre_lock);
1369 +#define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_ct_gre_lock)
1370 +#define ASSERT_WRITE_LOCK(x) MUST_BE_WRITE_LOCKED(&ip_ct_gre_lock)
1371 +
1372 +#include <linux/netfilter_ipv4/listhelp.h>
1373 +#include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
1374 +#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
1375 +#include <linux/netfilter_ipv4/ip_conntrack_core.h>
1376 +
1377 +#include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
1378 +#include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
1379 +
1380 +MODULE_LICENSE("GPL");
1381 +MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
1382 +MODULE_DESCRIPTION("netfilter connection tracking protocol helper for GRE");
1383 +
1384 +/* shamelessly stolen from ip_conntrack_proto_udp.c */
1385 +#define GRE_TIMEOUT (30*HZ)
1386 +#define GRE_STREAM_TIMEOUT (180*HZ)
1387 +
1388 +#if 0
1389 +#define DEBUGP(format, args...) printk(KERN_DEBUG __FILE__ ":" __FUNCTION__ \
1390 + ": " format, ## args)
1391 +#define DUMP_TUPLE_GRE(x) printk("%u.%u.%u.%u:0x%x -> %u.%u.%u.%u:0x%x:%u:0x%x\n", \
1392 + NIPQUAD((x)->src.ip), ntohl((x)->src.u.gre.key), \
1393 + NIPQUAD((x)->dst.ip), ntohl((x)->dst.u.gre.key))
1394 +#else
1395 +#define DEBUGP(x, args...)
1396 +#define DUMP_TUPLE_GRE(x)
1397 +#endif
1398 +
1399 +/* GRE KEYMAP HANDLING FUNCTIONS */
1400 +static LIST_HEAD(gre_keymap_list);
1401 +
1402 +static inline int gre_key_cmpfn(const struct ip_ct_gre_keymap *km,
1403 + const struct ip_conntrack_tuple *t)
1404 +{
1405 + return ((km->tuple.src.ip == t->src.ip) &&
1406 + (km->tuple.dst.ip == t->dst.ip) &&
1407 + (km->tuple.dst.protonum == t->dst.protonum) &&
1408 + (km->tuple.dst.u.all == t->dst.u.all));
1409 +}
1410 +
1411 +/* look up the source key for a given tuple */
1412 +static u_int32_t gre_keymap_lookup(struct ip_conntrack_tuple *t)
1413 +{
1414 + struct ip_ct_gre_keymap *km;
1415 + u_int32_t key;
1416 +
1417 + READ_LOCK(&ip_ct_gre_lock);
1418 + km = LIST_FIND(&gre_keymap_list, gre_key_cmpfn,
1419 + struct ip_ct_gre_keymap *, t);
1420 + if (!km) {
1421 + READ_UNLOCK(&ip_ct_gre_lock);
1422 + return 0;
1423 + }
1424 +
1425 + key = km->tuple.src.u.gre.key;
1426 + READ_UNLOCK(&ip_ct_gre_lock);
1427 +
1428 + return key;
1429 +}
1430 +
1431 +/* add a single keymap entry, associate with specified expect */
1432 +int ip_ct_gre_keymap_add(struct ip_conntrack_expect *exp,
1433 + struct ip_conntrack_tuple *t, int reply)
1434 +{
1435 + struct ip_ct_gre_keymap *km;
1436 +
1437 + km = kmalloc(sizeof(*km), GFP_ATOMIC);
1438 + if (!km)
1439 + return -1;
1440 +
1441 + /* initializing list head should be sufficient */
1442 + memset(km, 0, sizeof(*km));
1443 +
1444 + memcpy(&km->tuple, t, sizeof(*t));
1445 +
1446 + if (!reply)
1447 + exp->proto.gre.keymap_orig = km;
1448 + else
1449 + exp->proto.gre.keymap_reply = km;
1450 +
1451 + DEBUGP("adding new entry %p: ", km);
1452 + DUMP_TUPLE_GRE(&km->tuple);
1453 +
1454 + WRITE_LOCK(&ip_ct_gre_lock);
1455 + list_append(&gre_keymap_list, km);
1456 + WRITE_UNLOCK(&ip_ct_gre_lock);
1457 +
1458 + return 0;
1459 +}
1460 +
1461 +/* change the tuple of a keymap entry (used by nat helper) */
1462 +void ip_ct_gre_keymap_change(struct ip_ct_gre_keymap *km,
1463 + struct ip_conntrack_tuple *t)
1464 +{
1465 + DEBUGP("changing entry %p to: ", km);
1466 + DUMP_TUPLE_GRE(t);
1467 +
1468 + WRITE_LOCK(&ip_ct_gre_lock);
1469 + memcpy(&km->tuple, t, sizeof(km->tuple));
1470 + WRITE_UNLOCK(&ip_ct_gre_lock);
1471 +}
1472 +
1473 +/* destroy the keymap entries associated with specified expect */
1474 +void ip_ct_gre_keymap_destroy(struct ip_conntrack_expect *exp)
1475 +{
1476 + DEBUGP("entering for exp %p\n", exp);
1477 + WRITE_LOCK(&ip_ct_gre_lock);
1478 + if (exp->proto.gre.keymap_orig) {
1479 + DEBUGP("removing %p from list\n", exp->proto.gre.keymap_orig);
1480 + list_del(&exp->proto.gre.keymap_orig->list);
1481 + kfree(exp->proto.gre.keymap_orig);
1482 + exp->proto.gre.keymap_orig = NULL;
1483 + }
1484 + if (exp->proto.gre.keymap_reply) {
1485 + DEBUGP("removing %p from list\n", exp->proto.gre.keymap_reply);
1486 + list_del(&exp->proto.gre.keymap_reply->list);
1487 + kfree(exp->proto.gre.keymap_reply);
1488 + exp->proto.gre.keymap_reply = NULL;
1489 + }
1490 + WRITE_UNLOCK(&ip_ct_gre_lock);
1491 +}
1492 +
1493 +
1494 +/* PUBLIC CONNTRACK PROTO HELPER FUNCTIONS */
1495 +
1496 +/* invert gre part of tuple */
1497 +static int gre_invert_tuple(struct ip_conntrack_tuple *tuple,
1498 + const struct ip_conntrack_tuple *orig)
1499 +{
1500 + tuple->dst.u.gre.key = orig->src.u.gre.key;
1501 + tuple->src.u.gre.key = orig->dst.u.gre.key;
1502 +
1503 + return 1;
1504 +}
1505 +
1506 +/* gre hdr info to tuple */
1507 +static int gre_pkt_to_tuple(const void *datah, size_t datalen,
1508 + struct ip_conntrack_tuple *tuple)
1509 +{
1510 + struct gre_hdr *grehdr = (struct gre_hdr *) datah;
1511 + struct gre_hdr_pptp *pgrehdr = (struct gre_hdr_pptp *) datah;
1512 + u_int32_t srckey;
1513 +
1514 + /* core guarantees 8 protocol bytes, no need for size check */
1515 +
1516 + switch (grehdr->version) {
1517 + case GRE_VERSION_1701:
1518 + if (!grehdr->key) {
1519 + DEBUGP("Can't track GRE without key\n");
1520 + return 0;
1521 + }
1522 + tuple->dst.u.gre.key = *(gre_key(grehdr));
1523 + break;
1524 +
1525 + case GRE_VERSION_PPTP:
1526 + if (ntohs(grehdr->protocol) != GRE_PROTOCOL_PPTP) {
1527 + DEBUGP("GRE_VERSION_PPTP but unknown proto\n");
1528 + return 0;
1529 + }
1530 + tuple->dst.u.gre.key = htonl(ntohs(pgrehdr->call_id));
1531 + break;
1532 +
1533 + default:
1534 + printk(KERN_WARNING "unknown GRE version %hu\n",
1535 + grehdr->version);
1536 + return 0;
1537 + }
1538 +
1539 + srckey = gre_keymap_lookup(tuple);
1540 +
1541 +#if 0
1542 + DEBUGP("found src key %x for tuple ", ntohl(srckey));
1543 + DUMP_TUPLE_GRE(tuple);
1544 +#endif
1545 + tuple->src.u.gre.key = srckey;
1546 +
1547 + return 1;
1548 +}
1549 +
1550 +/* print gre part of tuple */
1551 +static unsigned int gre_print_tuple(char *buffer,
1552 + const struct ip_conntrack_tuple *tuple)
1553 +{
1554 + return sprintf(buffer, "srckey=0x%x dstkey=0x%x ",
1555 + ntohl(tuple->src.u.gre.key),
1556 + ntohl(tuple->dst.u.gre.key));
1557 +}
1558 +
1559 +/* print private data for conntrack */
1560 +static unsigned int gre_print_conntrack(char *buffer,
1561 + const struct ip_conntrack *ct)
1562 +{
1563 + return sprintf(buffer, "timeout=%u, stream_timeout=%u ",
1564 + (ct->proto.gre.timeout / HZ),
1565 + (ct->proto.gre.stream_timeout / HZ));
1566 +}
1567 +
1568 +/* Returns verdict for packet, and may modify conntrack */
1569 +static int gre_packet(struct ip_conntrack *ct,
1570 + struct iphdr *iph, size_t len,
1571 + enum ip_conntrack_info conntrackinfo)
1572 +{
1573 + /* If we've seen traffic both ways, this is a GRE connection.
1574 + * Extend timeout. */
1575 + if (ct->status & IPS_SEEN_REPLY) {
1576 + ip_ct_refresh(ct, ct->proto.gre.stream_timeout);
1577 + /* Also, more likely to be important, and not a probe. */
1578 + set_bit(IPS_ASSURED_BIT, &ct->status);
1579 + } else
1580 + ip_ct_refresh(ct, ct->proto.gre.timeout);
1581 +
1582 + return NF_ACCEPT;
1583 +}
1584 +
1585 +/* Called when a new connection for this protocol found. */
1586 +static int gre_new(struct ip_conntrack *ct,
1587 + struct iphdr *iph, size_t len)
1588 +{
1589 + DEBUGP(": ");
1590 + DUMP_TUPLE_GRE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
1591 +
1592 + /* initialize to sane value. Ideally a conntrack helper
1593 + * (e.g. in case of pptp) is increasing them */
1594 + ct->proto.gre.stream_timeout = GRE_STREAM_TIMEOUT;
1595 + ct->proto.gre.timeout = GRE_TIMEOUT;
1596 +
1597 + return 1;
1598 +}
1599 +
1600 +/* Called when a conntrack entry has already been removed from the hashes
1601 + * and is about to be deleted from memory */
1602 +static void gre_destroy(struct ip_conntrack *ct)
1603 +{
1604 + struct ip_conntrack_expect *master = ct->master;
1605 +
1606 + DEBUGP(" entering\n");
1607 +
1608 + if (!master) {
1609 + DEBUGP("no master exp for ct %p\n", ct);
1610 + return;
1611 + }
1612 +
1613 + ip_ct_gre_keymap_destroy(master);
1614 +}
1615 +
1616 +/* protocol helper struct */
1617 +static struct ip_conntrack_protocol gre = { { NULL, NULL }, IPPROTO_GRE,
1618 + "gre",
1619 + gre_pkt_to_tuple,
1620 + gre_invert_tuple,
1621 + gre_print_tuple,
1622 + gre_print_conntrack,
1623 + gre_packet,
1624 + gre_new,
1625 + gre_destroy,
1626 + NULL,
1627 + THIS_MODULE };
1628 +
1629 +/* ip_conntrack_proto_gre initialization */
1630 +static int __init init(void)
1631 +{
1632 + int retcode;
1633 +
1634 + if ((retcode = ip_conntrack_protocol_register(&gre))) {
1635 + printk(KERN_ERR "Unable to register conntrack protocol "
1636 + "helper for gre: %d\n", retcode);
1637 + return -EIO;
1638 + }
1639 +
1640 + return 0;
1641 +}
1642 +
1643 +static void __exit fini(void)
1644 +{
1645 + struct list_head *pos, *n;
1646 +
1647 + /* delete all keymap entries */
1648 + WRITE_LOCK(&ip_ct_gre_lock);
1649 + list_for_each_safe(pos, n, &gre_keymap_list) {
1650 + DEBUGP("deleting keymap %p at module unload time\n", pos);
1651 + list_del(pos);
1652 + kfree(pos);
1653 + }
1654 + WRITE_UNLOCK(&ip_ct_gre_lock);
1655 +
1656 + ip_conntrack_protocol_unregister(&gre);
1657 +}
1658 +
1659 +EXPORT_SYMBOL(ip_ct_gre_keymap_add);
1660 +EXPORT_SYMBOL(ip_ct_gre_keymap_change);
1661 +EXPORT_SYMBOL(ip_ct_gre_keymap_destroy);
1662 +
1663 +module_init(init);
1664 +module_exit(fini);
1665 diff -urN linux.old/net/ipv4/netfilter/ip_nat_core.c linux.dev/net/ipv4/netfilter/ip_nat_core.c
1666 --- linux.old/net/ipv4/netfilter/ip_nat_core.c 2005-04-04 03:42:20.000000000 +0200
1667 +++ linux.dev/net/ipv4/netfilter/ip_nat_core.c 2006-12-06 00:34:27.000000000 +0100
1668 @@ -424,7 +424,7 @@
1669 *tuple = *orig_tuple;
1670 while ((rptr = find_best_ips_proto_fast(tuple, mr, conntrack, hooknum))
1671 != NULL) {
1672 - DEBUGP("Found best for "); DUMP_TUPLE(tuple);
1673 + DEBUGP("Found best for "); DUMP_TUPLE_RAW(tuple);
1674 /* 3) The per-protocol part of the manip is made to
1675 map into the range to make a unique tuple. */
1676
1677 @@ -564,9 +564,9 @@
1678 HOOK2MANIP(hooknum)==IP_NAT_MANIP_SRC ? "SRC" : "DST",
1679 conntrack);
1680 DEBUGP("Original: ");
1681 - DUMP_TUPLE(&orig_tp);
1682 + DUMP_TUPLE_RAW(&orig_tp);
1683 DEBUGP("New: ");
1684 - DUMP_TUPLE(&new_tuple);
1685 + DUMP_TUPLE_RAW(&new_tuple);
1686 #endif
1687
1688 /* We now have two tuples (SRCIP/SRCPT/DSTIP/DSTPT):
1689 diff -urN linux.old/net/ipv4/netfilter/ip_nat_pptp.c linux.dev/net/ipv4/netfilter/ip_nat_pptp.c
1690 --- linux.old/net/ipv4/netfilter/ip_nat_pptp.c 1970-01-01 01:00:00.000000000 +0100
1691 +++ linux.dev/net/ipv4/netfilter/ip_nat_pptp.c 2006-12-06 00:34:38.000000000 +0100
1692 @@ -0,0 +1,482 @@
1693 +/*
1694 + * ip_nat_pptp.c - Version 1.5
1695 + *
1696 + * NAT support for PPTP (Point to Point Tunneling Protocol).
1697 + * PPTP is a a protocol for creating virtual private networks.
1698 + * It is a specification defined by Microsoft and some vendors
1699 + * working with Microsoft. PPTP is built on top of a modified
1700 + * version of the Internet Generic Routing Encapsulation Protocol.
1701 + * GRE is defined in RFC 1701 and RFC 1702. Documentation of
1702 + * PPTP can be found in RFC 2637
1703 + *
1704 + * (C) 2000-2003 by Harald Welte <laforge@gnumonks.org>
1705 + *
1706 + * Development of this code funded by Astaro AG (http://www.astaro.com/)
1707 + *
1708 + * TODO: - Support for multiple calls within one session
1709 + * (needs netfilter newnat code)
1710 + * - NAT to a unique tuple, not to TCP source port
1711 + * (needs netfilter tuple reservation)
1712 + *
1713 + * Changes:
1714 + * 2002-02-10 - Version 1.3
1715 + * - Use ip_nat_mangle_tcp_packet() because of cloned skb's
1716 + * in local connections (Philip Craig <philipc@snapgear.com>)
1717 + * - add checks for magicCookie and pptp version
1718 + * - make argument list of pptp_{out,in}bound_packet() shorter
1719 + * - move to C99 style initializers
1720 + * - print version number at module loadtime
1721 + * 2003-09-22 - Version 1.5
1722 + * - use SNATed tcp sourceport as callid, since we get called before
1723 + * TCP header is mangled (Philip Craig <philipc@snapgear.com>)
1724 + *
1725 + */
1726 +
1727 +#include <linux/config.h>
1728 +#include <linux/module.h>
1729 +#include <linux/ip.h>
1730 +#include <linux/tcp.h>
1731 +#include <net/tcp.h>
1732 +#include <linux/netfilter_ipv4/ip_nat.h>
1733 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
1734 +#include <linux/netfilter_ipv4/ip_nat_helper.h>
1735 +#include <linux/netfilter_ipv4/ip_nat_pptp.h>
1736 +#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
1737 +#include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
1738 +#include <linux/netfilter_ipv4/ip_conntrack_pptp.h>
1739 +
1740 +#define IP_NAT_PPTP_VERSION "1.5"
1741 +
1742 +MODULE_LICENSE("GPL");
1743 +MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
1744 +MODULE_DESCRIPTION("Netfilter NAT helper module for PPTP");
1745 +
1746 +
1747 +#if 0
1748 +#include "ip_conntrack_pptp_priv.h"
1749 +#define DEBUGP(format, args...) printk(KERN_DEBUG __FILE__ ":" __FUNCTION__ \
1750 + ": " format, ## args)
1751 +#else
1752 +#define DEBUGP(format, args...)
1753 +#endif
1754 +
1755 +static unsigned int
1756 +pptp_nat_expected(struct sk_buff **pskb,
1757 + unsigned int hooknum,
1758 + struct ip_conntrack *ct,
1759 + struct ip_nat_info *info)
1760 +{
1761 + struct ip_conntrack *master = master_ct(ct);
1762 + struct ip_nat_multi_range mr;
1763 + struct ip_ct_pptp_master *ct_pptp_info;
1764 + struct ip_nat_pptp *nat_pptp_info;
1765 + u_int32_t newip, newcid;
1766 + int ret;
1767 +
1768 + IP_NF_ASSERT(info);
1769 + IP_NF_ASSERT(master);
1770 + IP_NF_ASSERT(!(info->initialized & (1 << HOOK2MANIP(hooknum))));
1771 +
1772 + DEBUGP("we have a connection!\n");
1773 +
1774 + LOCK_BH(&ip_pptp_lock);
1775 + ct_pptp_info = &master->help.ct_pptp_info;
1776 + nat_pptp_info = &master->nat.help.nat_pptp_info;
1777 +
1778 + /* need to alter GRE tuple because conntrack expectfn() used 'wrong'
1779 + * (unmanipulated) values */
1780 + if (HOOK2MANIP(hooknum) == IP_NAT_MANIP_DST) {
1781 + DEBUGP("completing tuples with NAT info \n");
1782 + /* we can do this, since we're unconfirmed */
1783 + if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.gre.key ==
1784 + htonl(ct_pptp_info->pac_call_id)) {
1785 + /* assume PNS->PAC */
1786 + ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.gre.key =
1787 + htonl(nat_pptp_info->pns_call_id);
1788 + ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.gre.key =
1789 + htonl(nat_pptp_info->pns_call_id);
1790 + newip = master->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip;
1791 + newcid = htonl(nat_pptp_info->pac_call_id);
1792 + } else {
1793 + /* assume PAC->PNS */
1794 + ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.gre.key =
1795 + htonl(nat_pptp_info->pac_call_id);
1796 + ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.gre.key =
1797 + htonl(nat_pptp_info->pac_call_id);
1798 + newip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip;
1799 + newcid = htonl(nat_pptp_info->pns_call_id);
1800 + }
1801 + } else {
1802 + if (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u.gre.key ==
1803 + htonl(ct_pptp_info->pac_call_id)) {
1804 + /* assume PNS->PAC */
1805 + newip = master->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
1806 + newcid = htonl(ct_pptp_info->pns_call_id);
1807 + }
1808 + else {
1809 + /* assume PAC->PNS */
1810 + newip = master->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip;
1811 + newcid = htonl(ct_pptp_info->pac_call_id);
1812 + }
1813 + }
1814 +
1815 + mr.rangesize = 1;
1816 + mr.range[0].flags = IP_NAT_RANGE_MAP_IPS | IP_NAT_RANGE_PROTO_SPECIFIED;
1817 + mr.range[0].min_ip = mr.range[0].max_ip = newip;
1818 + mr.range[0].min = mr.range[0].max =
1819 + ((union ip_conntrack_manip_proto ) { newcid });
1820 + DEBUGP("change ip to %u.%u.%u.%u\n",
1821 + NIPQUAD(newip));
1822 + DEBUGP("change key to 0x%x\n", ntohl(newcid));
1823 + ret = ip_nat_setup_info(ct, &mr, hooknum);
1824 +
1825 + UNLOCK_BH(&ip_pptp_lock);
1826 +
1827 + return ret;
1828 +
1829 +}
1830 +
1831 +/* outbound packets == from PNS to PAC */
1832 +static inline unsigned int
1833 +pptp_outbound_pkt(struct sk_buff **pskb,
1834 + struct ip_conntrack *ct,
1835 + enum ip_conntrack_info ctinfo,
1836 + struct ip_conntrack_expect *exp)
1837 +
1838 +{
1839 + struct iphdr *iph = (*pskb)->nh.iph;
1840 + struct tcphdr *tcph = (void *) iph + iph->ihl*4;
1841 + struct pptp_pkt_hdr *pptph = (struct pptp_pkt_hdr *)
1842 + ((void *)tcph + tcph->doff*4);
1843 +
1844 + struct PptpControlHeader *ctlh;
1845 + union pptp_ctrl_union pptpReq;
1846 + struct ip_ct_pptp_master *ct_pptp_info = &ct->help.ct_pptp_info;
1847 + struct ip_nat_pptp *nat_pptp_info = &ct->nat.help.nat_pptp_info;
1848 +
1849 + u_int16_t msg, *cid = NULL, new_callid;
1850 +
1851 + /* FIXME: size checks !!! */
1852 + ctlh = (struct PptpControlHeader *) ((void *) pptph + sizeof(*pptph));
1853 + pptpReq.rawreq = (void *) ((void *) ctlh + sizeof(*ctlh));
1854 +
1855 + new_callid = htons(ct_pptp_info->pns_call_id);
1856 +
1857 + switch (msg = ntohs(ctlh->messageType)) {
1858 + case PPTP_OUT_CALL_REQUEST:
1859 + cid = &pptpReq.ocreq->callID;
1860 + /* FIXME: ideally we would want to reserve a call ID
1861 + * here. current netfilter NAT core is not able to do
1862 + * this :( For now we use TCP source port. This breaks
1863 + * multiple calls within one control session */
1864 +
1865 + /* save original call ID in nat_info */
1866 + nat_pptp_info->pns_call_id = ct_pptp_info->pns_call_id;
1867 +
1868 + /* don't use tcph->source since we are at a DSTmanip
1869 + * hook (e.g. PREROUTING) and pkt is not mangled yet */
1870 + new_callid = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u.tcp.port;
1871 +
1872 + /* save new call ID in ct info */
1873 + ct_pptp_info->pns_call_id = ntohs(new_callid);
1874 + break;
1875 + case PPTP_IN_CALL_REPLY:
1876 + cid = &pptpReq.icreq->callID;
1877 + break;
1878 + case PPTP_CALL_CLEAR_REQUEST:
1879 + cid = &pptpReq.clrreq->callID;
1880 + break;
1881 + default:
1882 + DEBUGP("unknown outbound packet 0x%04x:%s\n", msg,
1883 + (msg <= PPTP_MSG_MAX)? strMName[msg]:strMName[0]);
1884 + /* fall through */
1885 +
1886 + case PPTP_SET_LINK_INFO:
1887 + /* only need to NAT in case PAC is behind NAT box */
1888 + case PPTP_START_SESSION_REQUEST:
1889 + case PPTP_START_SESSION_REPLY:
1890 + case PPTP_STOP_SESSION_REQUEST:
1891 + case PPTP_STOP_SESSION_REPLY:
1892 + case PPTP_ECHO_REQUEST:
1893 + case PPTP_ECHO_REPLY:
1894 + /* no need to alter packet */
1895 + return NF_ACCEPT;
1896 + }
1897 +
1898 + IP_NF_ASSERT(cid);
1899 +
1900 + DEBUGP("altering call id from 0x%04x to 0x%04x\n",
1901 + ntohs(*cid), ntohs(new_callid));
1902 +
1903 + /* mangle packet */
1904 + ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, (void *)cid - (void *)pptph,
1905 + sizeof(new_callid), (char *)&new_callid,
1906 + sizeof(new_callid));
1907 +
1908 + return NF_ACCEPT;
1909 +}
1910 +
1911 +/* inbound packets == from PAC to PNS */
1912 +static inline unsigned int
1913 +pptp_inbound_pkt(struct sk_buff **pskb,
1914 + struct ip_conntrack *ct,
1915 + enum ip_conntrack_info ctinfo,
1916 + struct ip_conntrack_expect *oldexp)
1917 +{
1918 + struct iphdr *iph = (*pskb)->nh.iph;
1919 + struct tcphdr *tcph = (void *) iph + iph->ihl*4;
1920 + struct pptp_pkt_hdr *pptph = (struct pptp_pkt_hdr *)
1921 + ((void *)tcph + tcph->doff*4);
1922 +
1923 + struct PptpControlHeader *ctlh;
1924 + union pptp_ctrl_union pptpReq;
1925 + struct ip_ct_pptp_master *ct_pptp_info = &ct->help.ct_pptp_info;
1926 + struct ip_nat_pptp *nat_pptp_info = &ct->nat.help.nat_pptp_info;
1927 +
1928 + u_int16_t msg, new_cid = 0, new_pcid, *pcid = NULL, *cid = NULL;
1929 + u_int32_t old_dst_ip;
1930 +
1931 + struct ip_conntrack_tuple t, inv_t;
1932 + struct ip_conntrack_tuple *orig_t, *reply_t;
1933 +
1934 + /* FIXME: size checks !!! */
1935 + ctlh = (struct PptpControlHeader *) ((void *) pptph + sizeof(*pptph));
1936 + pptpReq.rawreq = (void *) ((void *) ctlh + sizeof(*ctlh));
1937 +
1938 + new_pcid = htons(nat_pptp_info->pns_call_id);
1939 +
1940 + switch (msg = ntohs(ctlh->messageType)) {
1941 + case PPTP_OUT_CALL_REPLY:
1942 + pcid = &pptpReq.ocack->peersCallID;
1943 + cid = &pptpReq.ocack->callID;
1944 + if (!oldexp) {
1945 + DEBUGP("outcall but no expectation\n");
1946 + break;
1947 + }
1948 + old_dst_ip = oldexp->tuple.dst.ip;
1949 + t = oldexp->tuple;
1950 + invert_tuplepr(&inv_t, &t);
1951 +
1952 + /* save original PAC call ID in nat_info */
1953 + nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
1954 +
1955 + /* alter expectation */
1956 + orig_t = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
1957 + reply_t = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
1958 + if (t.src.ip == orig_t->src.ip && t.dst.ip == orig_t->dst.ip) {
1959 + /* expectation for PNS->PAC direction */
1960 + t.src.u.gre.key = htonl(nat_pptp_info->pns_call_id);
1961 + t.dst.u.gre.key = htonl(ct_pptp_info->pac_call_id);
1962 + inv_t.src.ip = reply_t->src.ip;
1963 + inv_t.dst.ip = reply_t->dst.ip;
1964 + inv_t.src.u.gre.key = htonl(nat_pptp_info->pac_call_id);
1965 + inv_t.dst.u.gre.key = htonl(ct_pptp_info->pns_call_id);
1966 + } else {
1967 + /* expectation for PAC->PNS direction */
1968 + t.src.u.gre.key = htonl(nat_pptp_info->pac_call_id);
1969 + t.dst.u.gre.key = htonl(ct_pptp_info->pns_call_id);
1970 + inv_t.src.ip = orig_t->src.ip;
1971 + inv_t.dst.ip = orig_t->dst.ip;
1972 + inv_t.src.u.gre.key = htonl(nat_pptp_info->pns_call_id);
1973 + inv_t.dst.u.gre.key = htonl(ct_pptp_info->pac_call_id);
1974 + }
1975 +
1976 + if (!ip_conntrack_change_expect(oldexp, &t)) {
1977 + DEBUGP("successfully changed expect\n");
1978 + } else {
1979 + DEBUGP("can't change expect\n");
1980 + }
1981 + if (oldexp->proto.gre.keymap_orig)
1982 + ip_ct_gre_keymap_change(oldexp->proto.gre.keymap_orig,
1983 + &t);
1984 + if (oldexp->proto.gre.keymap_reply)
1985 + ip_ct_gre_keymap_change(oldexp->proto.gre.keymap_reply,
1986 + &inv_t);
1987 + break;
1988 + case PPTP_IN_CALL_CONNECT:
1989 + pcid = &pptpReq.iccon->peersCallID;
1990 + if (!oldexp)
1991 + break;
1992 + old_dst_ip = oldexp->tuple.dst.ip;
1993 + t = oldexp->tuple;
1994 +
1995 + /* alter expectation, no need for callID */
1996 + if (t.dst.ip == ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip) {
1997 + /* expectation for PNS->PAC direction */
1998 + t.src.ip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
1999 + } else {
2000 + /* expectation for PAC->PNS direction */
2001 + t.dst.ip = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip;
2002 + }
2003 +
2004 + if (!ip_conntrack_change_expect(oldexp, &t)) {
2005 + DEBUGP("successfully changed expect\n");
2006 + } else {
2007 + DEBUGP("can't change expect\n");
2008 + }
2009 + break;
2010 + case PPTP_IN_CALL_REQUEST:
2011 + /* only need to nat in case PAC is behind NAT box */
2012 + break;
2013 + case PPTP_WAN_ERROR_NOTIFY:
2014 + pcid = &pptpReq.wanerr->peersCallID;
2015 + break;
2016 + case PPTP_CALL_DISCONNECT_NOTIFY:
2017 + pcid = &pptpReq.disc->callID;
2018 + break;
2019 + case PPTP_SET_LINK_INFO:
2020 + pcid = &pptpReq.setlink->peersCallID;
2021 + break;
2022 +
2023 + default:
2024 + DEBUGP("unknown inbound packet %s\n",
2025 + (msg <= PPTP_MSG_MAX)? strMName[msg]:strMName[0]);
2026 + /* fall through */
2027 +
2028 + case PPTP_START_SESSION_REQUEST:
2029 + case PPTP_START_SESSION_REPLY:
2030 + case PPTP_STOP_SESSION_REQUEST:
2031 + case PPTP_STOP_SESSION_REPLY:
2032 + case PPTP_ECHO_REQUEST:
2033 + case PPTP_ECHO_REPLY:
2034 + /* no need to alter packet */
2035 + return NF_ACCEPT;
2036 + }
2037 +
2038 + /* mangle packet */
2039 + IP_NF_ASSERT(pcid);
2040 + DEBUGP("altering peer call id from 0x%04x to 0x%04x\n",
2041 + ntohs(*pcid), ntohs(new_pcid));
2042 + ip_nat_mangle_tcp_packet(pskb, ct, ctinfo, (void *)pcid - (void *)pptph,
2043 + sizeof(new_pcid), (char *)&new_pcid,
2044 + sizeof(new_pcid));
2045 +
2046 + if (new_cid) {
2047 + IP_NF_ASSERT(cid);
2048 + DEBUGP("altering call id from 0x%04x to 0x%04x\n",
2049 + ntohs(*cid), ntohs(new_cid));
2050 + ip_nat_mangle_tcp_packet(pskb, ct, ctinfo,
2051 + (void *)cid - (void *)pptph,
2052 + sizeof(new_cid), (char *)&new_cid,
2053 + sizeof(new_cid));
2054 + }
2055 +
2056 + /* great, at least we don't need to resize packets */
2057 + return NF_ACCEPT;
2058 +}
2059 +
2060 +
2061 +static unsigned int tcp_help(struct ip_conntrack *ct,
2062 + struct ip_conntrack_expect *exp,
2063 + struct ip_nat_info *info,
2064 + enum ip_conntrack_info ctinfo,
2065 + unsigned int hooknum, struct sk_buff **pskb)
2066 +{
2067 + struct iphdr *iph = (*pskb)->nh.iph;
2068 + struct tcphdr *tcph = (void *) iph + iph->ihl*4;
2069 + unsigned int datalen = (*pskb)->len - iph->ihl*4 - tcph->doff*4;
2070 + struct pptp_pkt_hdr *pptph;
2071 +
2072 + int dir;
2073 +
2074 + DEBUGP("entering\n");
2075 +
2076 + /* Only mangle things once: DST for original direction
2077 + and SRC for reply direction. */
2078 + dir = CTINFO2DIR(ctinfo);
2079 + if (!((HOOK2MANIP(hooknum) == IP_NAT_MANIP_SRC
2080 + && dir == IP_CT_DIR_ORIGINAL)
2081 + || (HOOK2MANIP(hooknum) == IP_NAT_MANIP_DST
2082 + && dir == IP_CT_DIR_REPLY))) {
2083 + DEBUGP("Not touching dir %s at hook %s\n",
2084 + dir == IP_CT_DIR_ORIGINAL ? "ORIG" : "REPLY",
2085 + hooknum == NF_IP_POST_ROUTING ? "POSTROUTING"
2086 + : hooknum == NF_IP_PRE_ROUTING ? "PREROUTING"
2087 + : hooknum == NF_IP_LOCAL_OUT ? "OUTPUT"
2088 + : hooknum == NF_IP_LOCAL_IN ? "INPUT" : "???");
2089 + return NF_ACCEPT;
2090 + }
2091 +
2092 + /* if packet is too small, just skip it */
2093 + if (datalen < sizeof(struct pptp_pkt_hdr)+
2094 + sizeof(struct PptpControlHeader)) {
2095 + DEBUGP("pptp packet too short\n");
2096 + return NF_ACCEPT;
2097 + }
2098 +
2099 + pptph = (struct pptp_pkt_hdr *) ((void *)tcph + tcph->doff*4);
2100 +
2101 + /* if it's not a control message, we can't handle it */
2102 + if (ntohs(pptph->packetType) != PPTP_PACKET_CONTROL ||
2103 + ntohl(pptph->magicCookie) != PPTP_MAGIC_COOKIE) {
2104 + DEBUGP("not a pptp control packet\n");
2105 + return NF_ACCEPT;
2106 + }
2107 +
2108 + LOCK_BH(&ip_pptp_lock);
2109 +
2110 + if (dir == IP_CT_DIR_ORIGINAL) {
2111 + /* reuqests sent by client to server (PNS->PAC) */
2112 + pptp_outbound_pkt(pskb, ct, ctinfo, exp);
2113 + } else {
2114 + /* response from the server to the client (PAC->PNS) */
2115 + pptp_inbound_pkt(pskb, ct, ctinfo, exp);
2116 + }
2117 +
2118 + UNLOCK_BH(&ip_pptp_lock);
2119 +
2120 + return NF_ACCEPT;
2121 +}
2122 +
2123 +/* nat helper struct for control connection */
2124 +static struct ip_nat_helper pptp_tcp_helper = {
2125 + .list = { NULL, NULL },
2126 + .name = "pptp",
2127 + .flags = IP_NAT_HELPER_F_ALWAYS,
2128 + .me = THIS_MODULE,
2129 + .tuple = { .src = { .ip = 0,
2130 + .u = { .tcp = { .port =
2131 + __constant_htons(PPTP_CONTROL_PORT) }
2132 + }
2133 + },
2134 + .dst = { .ip = 0,
2135 + .u = { .all = 0 },
2136 + .protonum = IPPROTO_TCP
2137 + }
2138 + },
2139 +
2140 + .mask = { .src = { .ip = 0,
2141 + .u = { .tcp = { .port = 0xFFFF } }
2142 + },
2143 + .dst = { .ip = 0,
2144 + .u = { .all = 0 },
2145 + .protonum = 0xFFFF
2146 + }
2147 + },
2148 + .help = tcp_help,
2149 + .expect = pptp_nat_expected
2150 +};
2151 +
2152 +
2153 +static int __init init(void)
2154 +{
2155 + DEBUGP("%s: registering NAT helper\n", __FILE__);
2156 + if (ip_nat_helper_register(&pptp_tcp_helper)) {
2157 + printk(KERN_ERR "Unable to register NAT application helper "
2158 + "for pptp\n");
2159 + return -EIO;
2160 + }
2161 +
2162 + printk("ip_nat_pptp version %s loaded\n", IP_NAT_PPTP_VERSION);
2163 + return 0;
2164 +}
2165 +
2166 +static void __exit fini(void)
2167 +{
2168 + DEBUGP("cleanup_module\n" );
2169 + ip_nat_helper_unregister(&pptp_tcp_helper);
2170 + printk("ip_nat_pptp version %s unloaded\n", IP_NAT_PPTP_VERSION);
2171 +}
2172 +
2173 +module_init(init);
2174 +module_exit(fini);
2175 diff -urN linux.old/net/ipv4/netfilter/ip_nat_proto_gre.c linux.dev/net/ipv4/netfilter/ip_nat_proto_gre.c
2176 --- linux.old/net/ipv4/netfilter/ip_nat_proto_gre.c 1970-01-01 01:00:00.000000000 +0100
2177 +++ linux.dev/net/ipv4/netfilter/ip_nat_proto_gre.c 2006-12-06 00:34:27.000000000 +0100
2178 @@ -0,0 +1,202 @@
2179 +/*
2180 + * ip_nat_proto_gre.c - Version 1.2
2181 + *
2182 + * NAT protocol helper module for GRE.
2183 + *
2184 + * GRE is a generic encapsulation protocol, which is generally not very
2185 + * suited for NAT, as it has no protocol-specific part as port numbers.
2186 + *
2187 + * It has an optional key field, which may help us distinguishing two
2188 + * connections between the same two hosts.
2189 + *
2190 + * GRE is defined in RFC 1701 and RFC 1702, as well as RFC 2784
2191 + *
2192 + * PPTP is built on top of a modified version of GRE, and has a mandatory
2193 + * field called "CallID", which serves us for the same purpose as the key
2194 + * field in plain GRE.
2195 + *
2196 + * Documentation about PPTP can be found in RFC 2637
2197 + *
2198 + * (C) 2000-2003 by Harald Welte <laforge@gnumonks.org>
2199 + *
2200 + * Development of this code funded by Astaro AG (http://www.astaro.com/)
2201 + *
2202 + */
2203 +
2204 +#include <linux/config.h>
2205 +#include <linux/module.h>
2206 +#include <linux/ip.h>
2207 +#include <linux/netfilter_ipv4/ip_nat.h>
2208 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
2209 +#include <linux/netfilter_ipv4/ip_nat_protocol.h>
2210 +#include <linux/netfilter_ipv4/ip_conntrack_proto_gre.h>
2211 +
2212 +MODULE_LICENSE("GPL");
2213 +MODULE_AUTHOR("Harald Welte <laforge@gnumonks.org>");
2214 +MODULE_DESCRIPTION("Netfilter NAT protocol helper module for GRE");
2215 +
2216 +#if 0
2217 +#define DEBUGP(format, args...) printk(KERN_DEBUG __FILE__ ":" __FUNCTION__ \
2218 + ": " format, ## args)
2219 +#else
2220 +#define DEBUGP(x, args...)
2221 +#endif
2222 +
2223 +/* is key in given range between min and max */
2224 +static int
2225 +gre_in_range(const struct ip_conntrack_tuple *tuple,
2226 + enum ip_nat_manip_type maniptype,
2227 + const union ip_conntrack_manip_proto *min,
2228 + const union ip_conntrack_manip_proto *max)
2229 +{
2230 + u_int32_t key;
2231 +
2232 + if (maniptype == IP_NAT_MANIP_SRC)
2233 + key = tuple->src.u.gre.key;
2234 + else
2235 + key = tuple->dst.u.gre.key;
2236 +
2237 + return ntohl(key) >= ntohl(min->gre.key)
2238 + && ntohl(key) <= ntohl(max->gre.key);
2239 +}
2240 +
2241 +/* generate unique tuple ... */
2242 +static int
2243 +gre_unique_tuple(struct ip_conntrack_tuple *tuple,
2244 + const struct ip_nat_range *range,
2245 + enum ip_nat_manip_type maniptype,
2246 + const struct ip_conntrack *conntrack)
2247 +{
2248 + u_int32_t min, i, range_size;
2249 + u_int32_t key = 0, *keyptr;
2250 +
2251 + if (maniptype == IP_NAT_MANIP_SRC)
2252 + keyptr = &tuple->src.u.gre.key;
2253 + else
2254 + keyptr = &tuple->dst.u.gre.key;
2255 +
2256 + if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED)) {
2257 +
2258 + DEBUGP("%p: NATing GRE PPTP\n", conntrack);
2259 + min = 1;
2260 + range_size = 0xffff;
2261 +
2262 + } else {
2263 + min = ntohl(range->min.gre.key);
2264 + range_size = ntohl(range->max.gre.key) - min + 1;
2265 + }
2266 +
2267 + DEBUGP("min = %u, range_size = %u\n", min, range_size);
2268 +
2269 + for (i = 0; i < range_size; i++, key++) {
2270 + *keyptr = htonl(min + key % range_size);
2271 + if (!ip_nat_used_tuple(tuple, conntrack))
2272 + return 1;
2273 + }
2274 +
2275 + DEBUGP("%p: no NAT mapping\n", conntrack);
2276 +
2277 + return 0;
2278 +}
2279 +
2280 +/* manipulate a GRE packet according to maniptype */
2281 +static void
2282 +gre_manip_pkt(struct iphdr *iph, size_t len,
2283 + const struct ip_conntrack_manip *manip,
2284 + enum ip_nat_manip_type maniptype)
2285 +{
2286 + struct gre_hdr *greh = (struct gre_hdr *)((u_int32_t *)iph+iph->ihl);
2287 + struct gre_hdr_pptp *pgreh = (struct gre_hdr_pptp *) greh;
2288 +
2289 + /* we only have destination manip of a packet, since 'source key'
2290 + * is not present in the packet itself */
2291 + if (maniptype == IP_NAT_MANIP_DST) {
2292 + /* key manipulation is always dest */
2293 + switch (greh->version) {
2294 + case 0:
2295 + if (!greh->key) {
2296 + DEBUGP("can't nat GRE w/o key\n");
2297 + break;
2298 + }
2299 + if (greh->csum) {
2300 + /* FIXME: Never tested this code... */
2301 + *(gre_csum(greh)) =
2302 + ip_nat_cheat_check(~*(gre_key(greh)),
2303 + manip->u.gre.key,
2304 + *(gre_csum(greh)));
2305 + }
2306 + *(gre_key(greh)) = manip->u.gre.key;
2307 + break;
2308 + case GRE_VERSION_PPTP:
2309 + DEBUGP("call_id -> 0x%04x\n",
2310 + ntohl(manip->u.gre.key));
2311 + pgreh->call_id = htons(ntohl(manip->u.gre.key));
2312 + break;
2313 + default:
2314 + DEBUGP("can't nat unknown GRE version\n");
2315 + break;
2316 + }
2317 + }
2318 +}
2319 +
2320 +/* print out a nat tuple */
2321 +static unsigned int
2322 +gre_print(char *buffer,
2323 + const struct ip_conntrack_tuple *match,
2324 + const struct ip_conntrack_tuple *mask)
2325 +{
2326 + unsigned int len = 0;
2327 +
2328 + if (mask->src.u.gre.key)
2329 + len += sprintf(buffer + len, "srckey=0x%x ",
2330 + ntohl(match->src.u.gre.key));
2331 +
2332 + if (mask->dst.u.gre.key)
2333 + len += sprintf(buffer + len, "dstkey=0x%x ",
2334 + ntohl(match->src.u.gre.key));
2335 +
2336 + return len;
2337 +}
2338 +
2339 +/* print a range of keys */
2340 +static unsigned int
2341 +gre_print_range(char *buffer, const struct ip_nat_range *range)
2342 +{
2343 + if (range->min.gre.key != 0
2344 + || range->max.gre.key != 0xFFFF) {
2345 + if (range->min.gre.key == range->max.gre.key)
2346 + return sprintf(buffer, "key 0x%x ",
2347 + ntohl(range->min.gre.key));
2348 + else
2349 + return sprintf(buffer, "keys 0x%u-0x%u ",
2350 + ntohl(range->min.gre.key),
2351 + ntohl(range->max.gre.key));
2352 + } else
2353 + return 0;
2354 +}
2355 +
2356 +/* nat helper struct */
2357 +static struct ip_nat_protocol gre =
2358 + { { NULL, NULL }, "GRE", IPPROTO_GRE,
2359 + gre_manip_pkt,
2360 + gre_in_range,
2361 + gre_unique_tuple,
2362 + gre_print,
2363 + gre_print_range
2364 + };
2365 +
2366 +static int __init init(void)
2367 +{
2368 + if (ip_nat_protocol_register(&gre))
2369 + return -EIO;
2370 +
2371 + return 0;
2372 +}
2373 +
2374 +static void __exit fini(void)
2375 +{
2376 + ip_nat_protocol_unregister(&gre);
2377 +}
2378 +
2379 +module_init(init);
2380 +module_exit(fini);
2381 diff -urN linux.old/net/ipv4/netfilter/Makefile linux.dev/net/ipv4/netfilter/Makefile
2382 --- linux.old/net/ipv4/netfilter/Makefile 2006-12-06 00:31:46.000000000 +0100
2383 +++ linux.dev/net/ipv4/netfilter/Makefile 2006-12-06 00:34:27.000000000 +0100
2384 @@ -36,23 +36,32 @@
2385 ifdef CONFIG_IP_NF_AMANDA
2386 export-objs += ip_conntrack_amanda.o
2387 endif
2388 -
2389 obj-$(CONFIG_IP_NF_TFTP) += ip_conntrack_tftp.o
2390 obj-$(CONFIG_IP_NF_FTP) += ip_conntrack_ftp.o
2391 ifdef CONFIG_IP_NF_FTP
2392 export-objs += ip_conntrack_ftp.o
2393 endif
2394 -
2395 obj-$(CONFIG_IP_NF_IRC) += ip_conntrack_irc.o
2396 ifdef CONFIG_IP_NF_IRC
2397 export-objs += ip_conntrack_irc.o
2398 endif
2399 +obj-$(CONFIG_IP_NF_CT_PROTO_GRE) += ip_conntrack_proto_gre.o
2400 +ifdef CONFIG_IP_NF_CT_PROTO_GRE
2401 + export-objs += ip_conntrack_proto_gre.o
2402 +endif
2403 +obj-$(CONFIG_IP_NF_PPTP) += ip_conntrack_pptp.o
2404 +ifdef CONFIG_IP_NF_NAT_PPTP
2405 + export-objs += ip_conntrack_pptp.o
2406 +endif
2407 +
2408
2409 # NAT helpers
2410 obj-$(CONFIG_IP_NF_NAT_AMANDA) += ip_nat_amanda.o
2411 obj-$(CONFIG_IP_NF_NAT_TFTP) += ip_nat_tftp.o
2412 obj-$(CONFIG_IP_NF_NAT_FTP) += ip_nat_ftp.o
2413 obj-$(CONFIG_IP_NF_NAT_IRC) += ip_nat_irc.o
2414 +obj-$(CONFIG_IP_NF_NAT_PROTO_GRE) += ip_nat_proto_gre.o
2415 +obj-$(CONFIG_IP_NF_NAT_PPTP) += ip_nat_pptp.o
2416
2417 # generic IP tables
2418 obj-$(CONFIG_IP_NF_IPTABLES) += ip_tables.o