7 #include <netinet/in.h>
12 #include <sys/socket.h>
13 #include <sys/types.h>
15 #include <linux/udp.h>
23 #define PPPCHAP 0xc223
24 #define PPPIPCP 0x8021
40 #define PACKET_LENGTH 1000
41 #define TARGET_PPS 5000
42 #define TARGET "211.29.131.33"
43 #define GWADDR "211.29.131.30"
44 #define NUM_SESSIONS 1
48 typedef unsigned short u16
;
49 typedef unsigned int u32
;
50 typedef unsigned char u8
;
87 char *attributes
[] = {
90 "Protocol Version", // 2
91 "Framing Capabilities", // 3
92 "Bearer Capabilities", // 4
94 "Firmware Revision", // 6
97 "Assigned Tunnel ID", // 9
98 "Receive Window Size", // 10
100 "Q.931 Cause Code", // 12
101 "Challenge Response", // 13
102 "Assigned Session ID", // 14
103 "Call Serial Number", // 15
106 "Bearer Type", // 18 (2 = Analog, 1 = Digital)
107 "Framing Type", // 19 (2 = Async, 1 = Sync)
109 "Called Number", // 21
110 "Calling Number", // 22
112 "Tx Connect Speed", // 24
113 "Physical Channel ID", // 25
114 "Initial Received LCP CONFREQ", // 26
115 "Last Sent LCP CONFREQ", // 27
116 "Last Received LCP CONFREQ", // 28
117 "Proxy Authen Type", // 29
118 "Proxy Authen Name", // 30
119 "Proxy Authen Challenge", // 31
120 "Proxy Authen ID", // 32
121 "Proxy Authen Response", // 33
124 "Random Vector", // 36
125 "Private Group ID", // 37
126 "Rx Connect Speed", // 38
127 "Sequencing Required", // 39
130 char *result_codes
[] = {
132 "General request to clear control connection",
133 "General error--Error Code indicates the problem",
134 "Control channel already exists",
135 "Requester is not authorized to establish a control channel",
136 "The protocol version of the requester is not supported",
137 "Requester is being shut down",
138 "Finite State Machine error",
141 char *error_codes
[] = {
143 "No control connection exists yet for this LAC-LNS pair",
145 "One of the field values was out of range or reserved field was non-zero",
146 "Insufficient resources to handle this operation now",
147 "The Session ID is invalid in this context",
148 "A generic vendor-specific error occurred in the LAC",
150 "Session or tunnel was shutdown due to receipt of an unknown AVP with the M-bit set",
182 unsigned long long send_count
, recv_count
;
183 unsigned long long spkt
, rpkt
;
184 unsigned int dropped
;
185 unsigned long sbytes
, rbytes
;
189 short remote_session
;
192 unsigned char ppp_identifier
;
196 int active_sessions
;
201 void controlsend(controlt
* c
, short t
, short s
);
202 void controlnull(short t
);
203 controlt
*controlnew(u16 mtype
);
204 void controls(controlt
* c
, u16 avp
, char *val
, u8 m
);
205 void control16(controlt
* c
, u16 avp
, u16 val
, u8 m
);
206 void control32(controlt
* c
, u16 avp
, u32 val
, u8 m
);
207 void controlfree(controlt
*c
);
208 control_message
*parsecontrol(char *buf
, int length
);
209 void dump_control_message(control_message
*c
);
210 u32
avp_get_32(control_message
*c
, int id
);
211 u16
avp_get_16(control_message
*c
, int id
);
212 char *avp_get_s(control_message
*c
, int id
);
213 void reader_thread(int udpfd
);
215 void cm_free(control_message
*m
);
216 controlt
*ppp_new(u16 session
, int protocol
);
217 void ppp_free(controlt
*packet
);
218 controlt
*ppp_lcp(u16 s
, unsigned char type
, char identifier
);
219 controlt
*ppp_ipcp(u16 s
, unsigned char type
, char identifier
);
220 void ppp_send(controlt
*c
);
221 void ppp_add_16(controlt
* c
, u16 val
);
222 void ppp_add_32(controlt
* c
, u32 val
);
223 void ppp_add_s(controlt
* c
, char *val
);
224 void ppp_lcp_add_option(controlt
*c
, unsigned char option
, unsigned char length
, int data
);
225 void dump_ppp_packet(char *packet
, int l
);
226 controlt
*ppp_pap(u16 s
, unsigned char type
, char identifier
, char *username
, char *password
);
227 char *inet_toa(unsigned long addr
);
228 __u16
checksum(unsigned char *addr
, int count
);
229 void sigalarm(int junk
);
230 void sigint(int signal
);
231 void clean_shutdown();
234 pthread_t pthread_reader
;
239 struct sockaddr_in gatewayaddr
= {0};
240 int numsessions
= NUM_SESSIONS
;
241 int packet_length
= PACKET_LENGTH
;
242 int target_pps
= TARGET_PPS
;
243 char *target
= TARGET
;
244 char *gwaddr
= GWADDR
;
245 int max_packets
= MAX_PACKETS
;
248 char **session_usernames
;
249 char *base_username
= "dslloadtest";
250 char *base_password
= "testing";
251 char *suffix
= "@optusnet.com.au";
253 int main(int argc
, char *argv
[])
258 ss
= (sharedt
*) mmap(NULL
, sizeof(*ss
), PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
260 // Process Arguments {{{
261 while ((s
= getopt(argc
, argv
, "?hs:g:l:p:m:t:nU:P:")) > 0)
266 numsessions
= atoi(optarg
);
267 if (numsessions
<= 0)
269 printf("You must have at least 1 session\n");
274 packet_length
= atoi(optarg
);
275 if (packet_length
< 64)
277 printf("You must have at least 64 byte packets\n");
285 target_pps
= atoi(optarg
);
288 max_packets
= atoi(optarg
);
289 if (packet_length
< 64)
291 printf("You must send at least 50 packets.\n");
296 target
= strdup(optarg
);
299 gwaddr
= strdup(optarg
);
302 base_username
= strdup(optarg
);
305 base_password
= strdup(optarg
);
309 printf("Options:\n");
310 printf("\t-s number of ss->sessions\n");
311 printf("\t-l packet length\n");
312 printf("\t-p target pps\n");
313 printf("\t-m maximum number of packets\n");
314 printf("\t-t target IP address\n");
315 printf("\t-g gateway IP address\n");
316 printf("\t-U username (or base if multiple)\n");
317 printf("\t-P password\n");
323 ppsend
= target_pps
/ 50;
327 packet
= calloc(4096, 1);
329 memset(ss
->sessions
, 0, sizeof(ss
->sessions
));
332 printf("Creating %d ss->sessions to %s\n", numsessions
, gwaddr
);
333 printf("Targeting %d packets per second\n", target_pps
);
334 if (max_packets
) printf("Sending a maximum of %d packets\n", max_packets
);
335 printf("Sending packets to %s\n", target
);
336 printf("Sending %d byte packets\n", packet_length
);
338 session_usernames
= (char **)calloc(sizeof(char *), numsessions
);
341 int sul
= strlen(base_username
) + 10;
344 for (i
= 0; i
< numsessions
; i
++)
346 session_usernames
[i
] = (char *)calloc(sul
, 1);
347 snprintf(session_usernames
[i
], sul
, "%s%d", base_username
, i
+1);
352 session_usernames
[0] = strdup(base_username
);
356 // Create socket/*{{{*/
359 struct sockaddr_in addr
;
361 memset(&addr
, 0, sizeof(addr
));
362 addr
.sin_family
= AF_INET
;
363 addr
.sin_port
= htons(38001);
365 udpfd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
372 setsockopt(udpfd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
373 if (bind(udpfd
, (void *) &addr
, sizeof(addr
)) < 0)
379 printf("Bound to port %d\n", htons(addr
.sin_port
));
382 gatewayaddr
.sin_family
= AF_INET
;
383 gatewayaddr
.sin_port
= htons(1701);
384 inet_aton(gwaddr
, &gatewayaddr
.sin_addr
);
386 // Create tunnel/*{{{*/
391 c
= controlnew(1); // SCCRQ
392 controls(c
, 7, "loadtest", 0); // Tunnel Hostname
393 controls(c
, 8, "OIE", 0); // Vendor Name
394 control16(c
, 9, 1, 0); // Assigned Tunnel ID
395 control16(c
, 2, 256, 0); // Version 1.0
396 control16(c
, 3, 1, 0); // Framing (Async)
397 control16(c
, 4, 1, 0); // Bearer (Digital)
398 control16(c
, 10, 20, 0); // Receive Window Size
399 controlsend(c
, 0, 0);
402 // Receive reply/*{{{*/
404 struct sockaddr_in addr
;
405 int alen
= sizeof(addr
), l
;
407 l
= recvfrom(udpfd
, packet
, 4096, 0, (void *) &addr
, &alen
);
410 printf("Error creating tunnel: %s\n", strerror(errno
));
414 r
= parsecontrol(packet
, l
);
417 printf("Invalid packet.. no first avp\n");
421 printf("Assigned tunnel: %d\n", t
= avp_get_16(r
, 9));
424 c
= controlnew(3); // SCCCN
425 controlsend(c
, t
, 0);
432 // Create ss->sessions/*{{{*/
435 for (s
= 1; s
<= numsessions
; s
++)
439 c
= controlnew(10); // ICRQ
440 controls(c
, 21, "12356", 0); // Called Number
441 controls(c
, 22, "000", 0); // Calling Number
442 control16(c
, 14, s
, 0); // Assigned Session ID
443 controlsend(c
, t
, 0);
445 usleep(15000); // 15 ms
448 printf("All session create requests sent...\n");/*}}}*/
451 reader_thread(udpfd
);
457 fprintf(stderr
, "Press enter to begin sending traffic\n");
458 fgets(tmp
, 512, stdin
);
461 fprintf(stderr
, "Beginning sending traffic through %d ss->sessions\n", ss
->active_sessions
);
462 printf(" TS: Total Packets Sent\n");
463 printf(" TL: Total Packets Lost\n");
464 printf(" PL: Packet Loss\n");
465 printf(" SS: Send Speed\n");
466 printf(" RS: Receive Speed\n");
467 printf(" SP: Packets/Second Sent\n");
468 printf(" RP: Packets/Second Received\n");
469 printf(" NS: Number of active ss->sessions\n");
471 signal(SIGALRM
, sigalarm
);
472 signal(SIGINT
, sigint
);
475 // Traffic generation loop {{{
477 struct sockaddr_in to
;
482 unsigned int seq
= 0;
486 memset(&to
, 0, sizeof(struct sockaddr_in
));
487 to
.sin_family
= AF_INET
;
488 inet_aton(target
, &to
.sin_addr
);
490 c
= ppp_new(1, PPPIP
);
492 iph
= (struct iphdr
*)(c
->buf
+ c
->length
);
493 udph
= (struct udphdr
*)(c
->buf
+ c
->length
+ sizeof(struct iphdr
));
494 data
= (char *)(c
->buf
+ c
->length
+ sizeof(struct iphdr
) + sizeof(struct udphdr
));
495 len
= sizeof(struct iphdr
) + sizeof(struct udphdr
);
499 c
->length
+= sizeof(struct iphdr
);
502 iph
->frag_off
= ntohs(1 << 14);
508 memcpy(&iph
->daddr
, &to
.sin_addr
, sizeof(iph
->daddr
));
511 udph
->source
= ntohs(39999);
512 udph
->dest
= ntohs(39000);
516 memset(data
, 64, 1500);
518 udph
->len
= ntohs(sizeof(struct udphdr
) + packet_length
);
519 iph
->tot_len
= ntohs(len
+ packet_length
);
520 c
->length
+= packet_length
;
522 while (!ss
->quitit
&& ss
->active_sessions
)
525 for (i
= 1; i
<= numsessions
&& !ss
->quitit
; i
++)
527 // Skip ss->sessions that aren't active yet
528 if (!ss
->sessions
[i
].open
|| ss
->sessions
[i
].ppp_state
!= 2)
531 *(u16
*)(c
->buf
+ 4) = htons(ss
->sessions
[i
].remote_session
); // Session ID
532 iph
->saddr
= ss
->sessions
[i
].addr
;
534 iph
->check
= ntohs(checksum((char *)iph
, sizeof(struct iphdr
)));
536 *((unsigned int *) data
) = seq
++;
541 ss
->sbytes
+= c
->length
;
543 if (ppsend
&& ss
->send_count
% ppsend
== 0)
547 req
.tv_nsec
= 5 * 1000 * 1000;
548 nanosleep(&req
, NULL
);
551 if (max_packets
&& ss
->send_count
>= max_packets
) ss
->quitit
++;
555 c
->length
-= packet_length
;
570 loss
= 100 - (((ss
->recv_count
* 1.0) / (ss
->send_count
* 1.0)) * 100.0);
573 printf("Total Packets Sent: %llu\n", ss
->send_count
);
574 printf("Total Packets Received: %llu\n", ss
->recv_count
);
575 printf("Overall Packet Loss: %0.2f%%", loss
);
579 void clean_shutdown()/*{{{*/
582 for (i
= 0; i
< numsessions
; i
++)
587 if (!ss
->sessions
[i
].open
) continue;
588 c
= controlnew(14); // CDN
589 control16(c
, 14, i
, 0); // Assigned Session ID
590 control16(c
, 1, 1, 0); // Result Code
591 controlsend(c
, t
, ss
->sessions
[i
].remote_session
);
599 c
= controlnew(4); // StopCCN
600 control16(c
, 9, 1, 0); // Assigned Tunnel ID
601 control16(c
, 1, 1, 0); // Result Code
602 controlsend(c
, t
, 0);
607 void sigint(int signal
)
612 void sigalarm(int junk
)
614 static unsigned long long last_rpkts
[AVG_SIZE
], last_spkts
[AVG_SIZE
];
615 static int last
= 0, avg_count
= 0;
616 register unsigned int avg_s
= 0, avg_r
= 0, i
;
619 last_rpkts
[last
] = ss
->rpkt
;
620 last_spkts
[last
] = ss
->spkt
;
621 last
= (last
+ 1) % AVG_SIZE
;
622 if (avg_count
< AVG_SIZE
) avg_count
++;
624 for (i
= 0; i
< avg_count
; i
++)
626 avg_s
+= last_spkts
[i
];
627 avg_r
+= last_rpkts
[i
];
632 loss
= 100 - (((avg_r
* 1.0) / (avg_s
* 1.0)) * 100.0);
633 fprintf(stderr
, "TS:%llu TL:%lld DR:%4d PL:%-3.2f%% SS:%0.1fMbits/s RS:%0.1fMbits/s NS:%u SP:%u RP:%u\n",
634 ss
->send_count
, ss
->send_count
-ss
->recv_count
, ss
->dropped
, loss
,
635 (ss
->sbytes
/1024.0/1024.0*8), (ss
->rbytes
/1024.0/1024.0*8),
639 ss
->spkt
= ss
->rpkt
= 0;
640 ss
->sbytes
= ss
->rbytes
= 0;
644 __u16
checksum(unsigned char *addr
, int count
)
646 register long sum
= 0;
648 for (; count
> 1; count
-= 2)
650 sum
+= ntohs(*(u32
*)addr
);
654 if (count
> 1) sum
+= *(unsigned char *)addr
;
656 // take only 16 bits out of the 32 bit sum and add up the carries
658 sum
= (sum
& 0xFFFF) + (sum
>> 16);
660 // one's complement the result
667 void control16(controlt
* c
, u16 avp
, u16 val
, u8 m
)
669 u16 l
= (m
? 0x8008 : 0x0008);
670 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
671 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
672 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
673 *(u16
*) (c
->buf
+ c
->length
+ 6) = htons(val
);
677 // add an AVP (32 bit)
678 void control32(controlt
* c
, u16 avp
, u32 val
, u8 m
)
680 u16 l
= (m
? 0x800A : 0x000A);
681 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
682 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
683 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
684 *(u32
*) (c
->buf
+ c
->length
+ 6) = htonl(val
);
688 // add an AVP (32 bit)
689 void controls(controlt
* c
, u16 avp
, char *val
, u8 m
)
691 u16 l
= ((m
? 0x8000 : 0) + strlen(val
) + 6);
692 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
693 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
694 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
695 memcpy(c
->buf
+ c
->length
+ 6, val
, strlen(val
));
696 c
->length
+= 6 + strlen(val
);
699 // new control connection
700 controlt
*controlnew(u16 mtype
)
703 c
= calloc(sizeof(controlt
), 1);
705 control16(c
, 0, mtype
, 1);
709 void controlnull(short t
)
712 c
= calloc(sizeof(controlt
), 1);
714 controlsend(c
, t
, 0);
719 // add a control message to a tunnel, and send if within window
720 void controlsend(controlt
* c
, short t
, short s
)
722 *(u16
*) (c
->buf
+ 0) = htons(0xC802); // flags/ver
723 *(u16
*) (c
->buf
+ 2) = htons(c
->length
); // length
724 *(u16
*) (c
->buf
+ 4) = htons(t
); // tunnel
725 *(u16
*) (c
->buf
+ 6) = htons(s
); // session
726 *(u16
*) (c
->buf
+ 8) = htons(ns
++); // sequence
727 *(u16
*) (c
->buf
+ 10) = htons(nr
); // sequence
728 // printf("Sending ");
729 // cm_free(parsecontrol(c->buf, c->length));
730 sendto(udpfd
, c
->buf
, c
->length
, 0, (struct sockaddr
*)&gatewayaddr
, sizeof(gatewayaddr
));
733 void controlfree(controlt
*c
)
739 control_message
*parsecontrol(char *buf
, int length
)
744 c
= calloc(sizeof(control_message
), 1);
748 c
->tunnel
= ntohs(*(u16
*)(buf
+ 4));
749 c
->session
= ntohs(*(u16
*)(buf
+ 6));
750 c
->ns
= ntohs(*(u16
*)(buf
+ 8));
751 c
->nr
= nr
= ntohs(*(u16
*)(buf
+ 10));
753 while ((p
- buf
) < length
)
755 avp
*a
= calloc(sizeof(avp
), 1);
756 a
->length
= ntohs(*(short *)(p
)) & 0x3FF;
757 a
->type
= ntohs(*(short *)(p
+ 4));
758 memcpy(a
->value
, p
+ 6, a
->length
- 6);
759 if (a
->type
== 0) c
->mtype
= ntohs(*(short *)a
->value
);
768 dump_control_message(c
);
772 void dump_control_message(control_message
*c
)
775 printf("Control Message (type=%u s=%u t=%d ns=%d nr=%d)\n", c
->mtype
, c
->session
, c
->tunnel
, c
->ns
, c
->nr
);
776 for (a
= c
->first
; a
; a
= a
->next
)
778 printf(" avp: %s, len: %d", attributes
[a
->type
], a
->length
- 6);
786 case 14 : printf(", value: %u\n", ntohs(*(short *)a
->value
));
795 case 15 : printf(", value: %u\n", ntohl(*(u32
*)a
->value
));
804 case 8 : printf(", value: \"%s\"\n", a
->value
);
807 case 2 : printf(", value: %d.%d\n", *(char *)a
->value
, *(char *)a
->value
+ 1);
809 case 0 : printf(", value: %s\n", mtypes
[ntohs(*(short *)a
->value
)]);
812 case 3 : printf(", value: (%d) %s %s\n", ntohl(*(u32
*)a
->value
),
813 (ntohl(*(u32
*)a
->value
) & 0x01) ? "synchronous" : "",
814 (ntohl(*(u32
*)a
->value
) & 0x02) ? "asynchronous" : "");
817 case 4 : printf(", value: (%d) %s %s\n", ntohl(*(u32
*)a
->value
),
818 (ntohl(*(u32
*)a
->value
) & 0x01) ? "digital" : "",
819 (ntohl(*(u32
*)a
->value
) & 0x02) ? "analog" : "");
822 default : printf("\n");
829 u16
avp_get_16(control_message
*c
, int id
)
833 for (a
= c
->first
; a
; a
= a
->next
)
834 if (a
->type
== id
) return ntohs(*(short *)a
->value
);
838 u32
avp_get_32(control_message
*c
, int id
)
842 for (a
= c
->first
; a
; a
= a
->next
)
843 if (a
->type
== id
) return ntohl(*(u32
*)a
->value
);
847 char *avp_get_s(control_message
*c
, int id
)
851 for (a
= c
->first
; a
; a
= a
->next
)
852 if (a
->type
== id
) return (char *)a
->value
;
856 void cm_free(control_message
*m
)
860 for (a
= m
->first
; a
; )
872 void reader_thread(int updfd
)/*{{{*/
874 unsigned char *packet
;
875 unsigned int seq
= 0;
877 printf("Starting reader thread\n");
878 packet
= malloc(4096);
881 struct sockaddr_in addr
;
882 int alen
= sizeof(addr
);
888 // memset(packet, 0, 4096);
889 if ((l
= recvfrom(udpfd
, packet
, 4096, 0, (void *) &addr
, &alen
)) < 0) break;
899 printf("Short packet received: %d bytes\n", l
);
901 s
= ntohs(*(u16
*)(packet
+ 4));
904 printf("Invalid session ID\n");
907 if (packet
[0] == 0xc8)
910 printf("Reader Received ");
911 m
= parsecontrol(packet
, l
);
917 case 4 : printf("StopCCN\n");
918 printf("Killing tunnel %d\n", avp_get_16(m
, 9));
921 case 6 : printf("HELLO, sending ZLB ACK\n");
928 printf("Received ICRP. Responding with CONFREQ\n");
930 ss
->sessions
[s
].remote_session
= avp_get_16(m
, 14);
931 ss
->sessions
[s
].open
= 1;
932 ss
->sessions
[s
].ppp_state
= 1;
934 c
= controlnew(12); // ICCN
935 controlsend(c
, t
, ss
->sessions
[s
].remote_session
);
938 c
= ppp_lcp(s
, CONFREQ
, 0);
939 ppp_lcp_add_option(c
, 1, 2, htons(1500)); // MRU = 1400
940 ppp_lcp_add_option(c
, 3, 2, htons(0xC023)); // Authentication Protocol - PAP
948 s
= avp_get_16(m
, 14);
949 printf("Killing session %d\n", s
);
950 ss
->sessions
[s
].open
= 0;
951 ss
->sessions
[s
].ppp_state
= 0;
952 ss
->active_sessions
--;
960 printf("StopCCN Received.. Dieing\n");
969 unsigned short protocol
= ntohs(*(u16
*)(packet
+ 6));
971 if (protocol
== 0xff03)
975 protocol
= ntohs(*(u16
*)(packet
+ 6));
977 if (protocol
!= PPPIP
)
980 dump_ppp_packet(packet
+ 6, l
- 6);
983 if (protocol
== PPPLCP
)
986 unsigned char ppp_id
= *(char *)(packet
+ 9);
988 switch (*(char *)(packet
+ 8))
991 r
= ppp_lcp(s
, CONFACK
, ppp_id
);
995 r
= ppp_pap(s
, CONFREQ
, 0, session_usernames
[s
-1], base_password
);
999 r
= ppp_lcp(s
, TERMACK
, ppp_id
);
1003 r
= ppp_lcp(s
, ECHOREP
, ppp_id
);
1009 else if (protocol
== PPPIPCP
)
1013 u32 address
= *(u32
*)(packet
+ 14);
1015 switch (*(char *)(packet
+ 8))
1018 r
= ppp_ipcp(s
, CONFREQ
, time(NULL
) % 255);
1019 ppp_lcp_add_option(r
, 3, 4, htonl(taddr
)); // Request 0.0.0.0
1022 r
= ppp_ipcp(s
, CONFACK
, time(NULL
) % 255);
1023 ppp_lcp_add_option(r
, 3, 4, address
); // ACK gateway IP
1028 // Ack whatever address we are given - it's ours
1029 r
= ppp_ipcp(s
, CONFACK
, time(NULL
) % 255);
1030 ppp_lcp_add_option(r
, 3, 4, address
); // Request 0.0.0.0
1033 printf("Session %d: %s\n", s
, inet_toa(address
));
1034 ss
->sessions
[s
].ppp_state
= 2;
1035 ss
->sessions
[s
].addr
= address
;
1036 ss
->active_sessions
++;
1039 printf("Conf-Ack Received\n");
1042 printf("Term-Req Received\n");
1045 printf("Echo-Req Received\n");
1048 printf("Echo-Rep Received\n");
1052 else if (protocol
== PPPPAP
)
1054 if (*(u16
*)(packet
+ 8) == 3)
1057 printf("Closing Connection\n");
1059 c
= controlnew(14); // CDN
1060 control16(c
, 14, ss
->sessions
[s
].remote_session
, 0); // Assigned Session ID
1061 controlsend(c
, t
, 0);
1063 ss
->sessions
[s
].open
= 0;
1066 else if (protocol
== PPPIP
)
1068 struct iphdr
*iph
= (struct iphdr
*)(packet
+ 8);
1069 char * data
= (char*) (packet
+ 8 + sizeof(struct iphdr
) + sizeof(struct udphdr
));
1070 if (!ss
->sessions
[s
].open
)
1072 printf("Packet for closed session %d\n", s
);
1076 if (iph
->protocol
== 17)
1081 iseq
= *((unsigned int *) data
);
1083 ss
->dropped
+= (iseq
- seq
) ;
1085 seq
= iseq
+ 1; // Next sequence number to expect.
1093 printf("Closing reader thread\n");
1097 void skip_zlb() /*{{{*/
1099 struct sockaddr_in addr
;
1100 int alen
= sizeof(addr
);
1103 l
= recvfrom(udpfd
, buf
, 1024, MSG_PEEK
, (void *) &addr
, &alen
);
1106 printf("recvfrom: %s\n", strerror(errno
));
1111 printf("Skipping ZLB (l=%d)\n", l
);
1112 recvfrom(udpfd
, buf
, 1024, 0, (void *) &addr
, &alen
);
1118 controlt
*ppp_new(u16 session
, int protocol
)
1120 controlt
*c
= calloc(sizeof(controlt
), 1);
1121 *(u16
*)(c
->buf
+ 4) = htons(ss
->sessions
[session
].remote_session
); // Tunnel
1122 *(u16
*)(c
->buf
+ 6) = htons(protocol
);
1128 void ppp_free(controlt
*c
)
1133 controlt
*ppp_lcp(u16 s
, unsigned char type
, char identifier
)
1137 if (!identifier
) identifier
= ss
->sessions
[s
].ppp_identifier
++;
1138 c
= ppp_new(s
, PPPLCP
);
1139 *(char *)(c
->buf
+ c
->length
+ 0) = type
;
1140 *(char *)(c
->buf
+ c
->length
+ 1) = identifier
;
1141 *(u16
*)(c
->buf
+ c
->length
+ 2) = ntohs(4);
1147 controlt
*ppp_ipcp(u16 s
, unsigned char type
, char identifier
)
1151 if (!identifier
) identifier
= ss
->sessions
[s
].ppp_identifier
++;
1152 c
= ppp_new(s
, PPPIPCP
);
1153 *(char *)(c
->buf
+ c
->length
+ 0) = type
;
1154 *(char *)(c
->buf
+ c
->length
+ 1) = identifier
;
1155 *(u16
*)(c
->buf
+ c
->length
+ 2) = ntohs(4);
1161 controlt
*ppp_pap(u16 s
, unsigned char type
, char identifier
, char *username
, char *password
)
1165 if (!identifier
) identifier
= ss
->sessions
[s
].ppp_identifier
++;
1166 c
= ppp_new(s
, PPPPAP
);
1167 *(char *)(c
->buf
+ c
->length
+ 0) = type
;
1168 *(char *)(c
->buf
+ c
->length
+ 1) = identifier
;
1169 *(u16
*)(c
->buf
+ c
->length
+ 2) = ntohs(4);
1172 *(char *)(c
->buf
+ c
->length
) = strlen(username
) + strlen(suffix
);
1173 memcpy((c
->buf
+ c
->length
+ 1), username
, strlen(username
));
1174 memcpy((c
->buf
+ c
->length
+ 1 + strlen(username
)), suffix
, strlen(suffix
));
1175 c
->length
+= strlen(username
) + 1 + strlen(suffix
);
1177 *(char *)(c
->buf
+ c
->length
) = strlen(password
);
1178 memcpy((c
->buf
+ c
->length
+ 1), password
, strlen(password
));
1179 c
->length
+= strlen(password
) + 1;
1184 void ppp_send(controlt
*c
)
1186 *(u16
*)(c
->buf
+ 0) = htons(0x0002); // flags/ver
1187 *(u16
*)(c
->buf
+ 2) = htons(t
); // tunnel
1188 *(u16
*)(c
->buf
+ 10) = ntohs(c
->length
- 8);
1189 if (sendto(udpfd
, c
->buf
, c
->length
, 0, (struct sockaddr
*)&gatewayaddr
, sizeof(gatewayaddr
)) < 0)
1191 if (htons(*(u16
*)(c
->buf
+ 6)) != PPPIP
)
1193 printf("PPP Sending ");
1194 dump_ppp_packet(c
->buf
+ 6, c
->length
- 6);
1198 void ppp_add_16(controlt
*c
, u16 val
)
1200 *(u16
*) (c
->buf
+ c
->length
) = htons(val
);
1204 void ppp_add_32(controlt
*c
, u32 val
)
1206 *(u32
*) (c
->buf
+ c
->length
) = htons(val
);
1210 void ppp_add_s(controlt
*c
, char *val
)
1212 memcpy(c
->buf
+ c
->length
, val
, strlen(val
));
1213 c
->length
+= strlen(val
);
1216 void ppp_lcp_add_option(controlt
*c
, unsigned char option
, unsigned char length
, int data
)
1218 *(char *)(c
->buf
+ c
->length
+ 0) = option
;
1219 *(char *)(c
->buf
+ c
->length
+ 1) = length
+ 2;
1220 memcpy(c
->buf
+ c
->length
+ 2, &data
, length
);
1221 c
->length
+= 2 + length
;
1224 void dump_ppp_packet(char *packet
, int l
)
1228 if (*(unsigned char *)p
== 0xff) p
+= 2;
1229 protocol
= ntohs(*(u16
*)(p
));
1230 printf("PPP Packet\n");
1233 case PPPCCP
: printf(" Protocol: PPPCCP\n"); break;
1235 if (protocol
== PPPLCP
)
1237 printf(" Protocol: PPPLCP\n");
1238 printf(" LCP Code: %s\n", lcp_codes
[*(u8
*)(p
+ 2)]);
1240 else if (protocol
== PPPPAP
)
1242 printf(" Protocol: PPPPAP\n");
1243 if (*(char *)(p
+ 2) == 2)
1245 printf(" Authentication accepted\n");
1247 else if (*(char *)(p
+ 2) == 3)
1249 printf(" Authentication denied\n");
1252 else if (protocol
== PPPIPCP
)
1254 printf(" Protocol: PPPIPCP\n");
1255 printf(" IPCP Code: %s\n", lcp_codes
[*(u8
*)(p
+ 2)]);
1256 printf(" Address: %s\n", inet_toa(*(u32
*)(p
+ 8)));
1258 else if (protocol
== PPPIP
)
1261 struct protoent
*pr
;
1263 iph
= (struct iphdr
*)(p
+ 2);
1265 printf(" Protocol: PPPIP\n");
1266 printf(" Length: %d\n", l
);
1267 printf(" IP Version: %d\n", iph
->version
);
1268 if (iph
->version
!= 4) return;
1269 pr
= getprotobynumber(iph
->protocol
);
1270 printf(" IP Header Length: %d\n", iph
->ihl
);
1271 printf(" IP TTL: %d\n", iph
->ttl
);
1272 printf(" IP Protocol: %s (%d)\n", (pr
? pr
->p_name
: "unknown"), iph
->protocol
);
1273 printf(" IP Checksum: %x\n", ntohs(iph
->check
));
1277 printf(" Protocol: unknown 0x%x\n", protocol
);
1282 char *inet_toa(unsigned long addr
)
1285 memcpy(&in
, &addr
, sizeof(unsigned long));
1286 return inet_ntoa(in
);