- Write pid file if filename is set
[l2tpns.git] / ppp.c
diff --git a/ppp.c b/ppp.c
index deab284..de37bf2 100644 (file)
--- a/ppp.c
+++ b/ppp.c
@@ -1,5 +1,6 @@
 // L2TPNS PPP Stuff
-// $Id: ppp.c,v 1.5 2004-06-23 03:52:24 fred_nerk Exp $
+
+char const *cvs_id_ppp = "$Id: ppp.c,v 1.9 2004-08-02 03:38:01 fred_nerk Exp $";
 
 #include <stdio.h>
 #include <string.h>
@@ -16,7 +17,7 @@
 extern tunnelt *tunnel;
 extern sessiont *session;
 extern radiust *radius;
-extern int tapfd;
+extern int tunfd;
 extern char hostname[];
 extern u32 eth_tx;
 extern time_t time_now;
@@ -28,13 +29,13 @@ void processpap(tunnelidt t, sessionidt s, u8 * p, u16 l)
        char user[129];
        char pass[129];
 
-#ifdef STAT_CALLS
-       STAT(call_processpap);
-#endif
+
+       CSTAT(call_processpap);
+
        log_hex(5, "PAP", p, l);
        if (l < 4)
        {
-               log(1, 0, s, t, "Short PAP %u bytes", l);
+               log(1, 0, s, t, "Short PAP %u bytes\n", l);
                STAT(tunnel_rx_errors);
                return ;
        }
@@ -123,9 +124,9 @@ void processchap(tunnelidt t, sessionidt s, u8 * p, u16 l)
        u16 r;
        u16 len;
 
-#ifdef STAT_CALLS
-       STAT(call_processchap);
-#endif
+
+       CSTAT(call_processchap);
+
        log_hex(5, "CHAP", p, l);
        r = session[s].radius;
        if (!r)
@@ -229,9 +230,9 @@ void dumplcp(u8 *p, int l)
        {
                int type = o[0];
                int length = o[1];
-               if (length == 0)
+               if (length < 2)
                {
-                       log(4, 0, 0, 0, "       Option length is 0...\n");
+                       log(4, 0, 0, 0, "       Option length is %d...\n", length);
                        break;
                }
                if (type == 0)
@@ -244,13 +245,22 @@ void dumplcp(u8 *p, int l)
                switch (type)
                {
                        case 1: // Maximum-Receive-Unit
+                               if (length == 4)
                                log(4, 0, 0, 0, "    %s %d\n", lcp_types[type], ntohs(*(u16 *)(o + 2)));
+                               else
+                                       log(4, 0, 0, 0, "    %s odd length %d\n", lcp_types[type], length);
                                break;
                        case 3: // Authentication-Protocol
                                {
+                                       if (length == 4)
+                                       {
                                        int proto = ntohs(*(u16 *)(o + 2));
-                                       log(4, 0, 0, 0, "    %s %s\n", lcp_types[type],
-                                                       proto == 0xC223 ? "CHAP" : "PAP");
+                                               log(4, 0, 0, 0, "   %s 0x%x (%s)\n", lcp_types[type], proto,
+                                                       proto == 0xC223 ? "CHAP" :
+                                                       proto == 0xC023 ? "PAP"  : "UNKNOWN");
+                                       }
+                                       else
+                                               log(4, 0, 0, 0, "   %s odd length %d\n", lcp_types[type], length);
                                        break;
                                }
                        case 4: // Quality-Protocol
@@ -261,20 +271,23 @@ void dumplcp(u8 *p, int l)
                                }
                        case 5: // Magic-Number
                                {
+                                       if (length == 6)
+                                       {
                                        u32 magicno = ntohl(*(u32 *)(o + 2));
                                        log(4, 0, 0, 0, "    %s %x\n", lcp_types[type], magicno);
+                                       }
+                                       else
+                                               log(4, 0, 0, 0, "   %s odd length %d\n", lcp_types[type], length);
                                        break;
                                }
                        case 7: // Protocol-Field-Compression
                                {
-                                       u32 pfc = ntohl(*(u32 *)(o + 2));
-                                       log(4, 0, 0, 0, "    %s %x\n", lcp_types[type], pfc);
+                                       log(4, 0, 0, 0, "    %s\n", lcp_types[type]);
                                        break;
                                }
                        case 8: // Address-And-Control-Field-Compression
                                {
-                                       u32 afc = ntohl(*(u32 *)(o + 2));
-                                       log(4, 0, 0, 0, "    %s %x\n", lcp_types[type], afc);
+                                       log(4, 0, 0, 0, "    %s\n", lcp_types[type]);
                                        break;
                                }
                        default:
@@ -292,19 +305,20 @@ void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
        u8 b[MAXCONTROL];
        u8 *q = NULL;
 
-#ifdef STAT_CALLS
-       STAT(call_processlcp);
-#endif
+
+       CSTAT(call_processlcp);
+
        log_hex(5, "LCP", p, l);
        if (l < 4)
        {
-               log(1, session[s].ip, s, t, "Short LCP %d bytes", l);
+               log(1, session[s].ip, s, t, "Short LCP %d bytes\n", l);
                STAT(tunnel_rx_errors);
                return ;
        }
        if (*p == ConfigAck)
        {
                log(3, session[s].ip, s, t, "LCP: Discarding ConfigAck\n");
+               session[s].flags |= SESSIONLCPACK;
        }
        else if (*p == ConfigReq)
        {
@@ -391,15 +405,10 @@ void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
                        // Already built a ConfigNak... send it
                        log(3, session[s].ip, s, t, "Sending ConfigNak\n");
                        tunnelsend(b, l + (q - b), t);
-
-                       log(3, session[s].ip, s, t, "Sending ConfigReq, requesting PAP login\n");
-                       q = makeppp(b, sizeof(b), NULL, 0, t, s, PPPLCP);
-                       *q++ = ConfigReq;
-                       *(u8 *)(q++) = 3;
-                       *(u8 *)(q++) = 4;
-                       *(u16 *)(q += 2) = htons(0xC023);
-                       tunnelsend(b, l + (q - b), t);
                }
+
+               if (!(session[s].flags & SESSIONLCPACK))
+                       initlcp(t, s);
        }
        else if (*p == ConfigNak)
        {
@@ -419,9 +428,9 @@ void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
                sessionshutdown(s, "Remote end closed connection.");
                tunnelsend(b, l + (q - b), t); // send it
        }
-       else if (*p == TerminateReq)
+       else if (*p == TerminateAck)
        {
-               sessionshutdown(s, "Remote end closed connection.");
+               sessionshutdown(s, "Connection closed.");
        }
        else if (*p == EchoReq)
        {
@@ -450,13 +459,13 @@ void processlcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
 // Process IPCP messages
 void processipcp(tunnelidt t, sessionidt s, u8 * p, u16 l)
 {
-#ifdef STAT_CALLS
-       STAT(call_processipcp);
-#endif
+
+       CSTAT(call_processipcp);
+
        log_hex(5, "IPCP", p, l);
        if (l < 5)
        {
-               log(1, 0, s, t, "Short IPCP %d bytes", l);
+               log(1, 0, s, t, "Short IPCP %d bytes\n", l);
                STAT(tunnel_rx_errors);
                return ;
        }
@@ -579,9 +588,9 @@ void processipin(tunnelidt t, sessionidt s, u8 * p, u16 l)
 {
        ipt ip;
 
-#ifdef STAT_CALLS
-       STAT(call_processipin);
-#endif
+
+       CSTAT(call_processipin);
+
        log_hex(5, "IP", p, l);
 
        ip = ntohl(*(u32 *)(p + 12));
@@ -622,8 +631,8 @@ void processipin(tunnelidt t, sessionidt s, u8 * p, u16 l)
                // Snooping this session, send it to ASIO
                snoop_send_packet(p, l, session[s].snoop_ip, session[s].snoop_port);
        }
-       STAT(tap_tx_packets);
-       INC_STAT(tap_tx_bytes, l);
+       STAT(tun_tx_packets);
+       INC_STAT(tun_tx_bytes, l);
 
        if (session[s].tbf_in && config->cluster_iam_master) { // Are we throttled and a master?? actually handle the throttled packets.
                tbf_queue_packet(session[s].tbf_in, p, l);
@@ -633,9 +642,9 @@ void processipin(tunnelidt t, sessionidt s, u8 * p, u16 l)
        // send to ethernet
        if (tun_write(p, l) < 0)
        {
-               STAT(tap_tx_errors);
-               log(0, 0, s, t, "Error writing %d bytes to TAP device: %s (tapfd=%d, p=%p)\n",
-                       l, strerror(errno), tapfd, p);
+               STAT(tun_tx_errors);
+               log(0, 0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
+                       l, strerror(errno), tunfd, p);
        }
 
 }
@@ -647,11 +656,11 @@ void processipin(tunnelidt t, sessionidt s, u8 * p, u16 l)
 void send_ipin(sessionidt s, u8 *buf, int len)
 {
        log_hex(5, "IP in throttled", buf, len);
-       if (write(tapfd, buf, len) < 0)
+       if (write(tunfd, buf, len) < 0)
        {
-               STAT(tap_tx_errors);
-               log(0, 0, 0, 0, "Error writing %d bytes to TAP device: %s (tapfd=%d, p=%p)\n",
-                       len, strerror(errno), tapfd, buf);
+               STAT(tun_tx_errors);
+               log(0, 0, 0, 0, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
+                       len, strerror(errno), tunfd, buf);
        }
 
        // Increment packet counters
@@ -667,9 +676,9 @@ void send_ipin(sessionidt s, u8 *buf, int len)
 // Process LCP messages
 void processccp(tunnelidt t, sessionidt s, u8 * p, u16 l)
 {
-#ifdef STAT_CALLS
-       STAT(call_processccp);
-#endif
+
+       CSTAT(call_processccp);
+
        log_hex(5, "CCP", p, l);
        if (l < 2 || (*p != ConfigReq && *p != TerminateReq))
        {
@@ -709,9 +718,9 @@ void sendchap(tunnelidt t, sessionidt s)
        u8 b[MAXCONTROL];
        u16 r = session[s].radius;
        u8 *q;
-#ifdef STAT_CALLS
-       STAT(call_sendchap);
-#endif
+
+       CSTAT(call_sendchap);
+
        if (!r)
        {
                log(1, 0, s, t, "No RADIUS to send challenge\n");