save state is no more
[l2tpns.git] / ppp.c
diff --git a/ppp.c b/ppp.c
index e627cda..b18840f 100644 (file)
--- a/ppp.c
+++ b/ppp.c
@@ -1,6 +1,6 @@
 // L2TPNS PPP Stuff
 
-char const *cvs_id_ppp = "$Id: ppp.c,v 1.39 2004/12/16 08:49:53 bodea Exp $";
+char const *cvs_id_ppp = "$Id: ppp.c,v 1.45 2005/03/10 03:31:25 bodea Exp $";
 
 #include <stdio.h>
 #include <string.h>
@@ -28,11 +28,11 @@ static void initccp(tunnelidt t, sessionidt s);
 // Process PAP messages
 void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
 {
-       char user[129];
-       char pass[129];
+       char user[MAXUSER];
+       char pass[MAXPASS];
        uint16_t hl;
 
-       CSTAT(call_processpap);
+       CSTAT(processpap);
 
        LOG_HEX(5, "PAP", p, l);
        if (l < 4)
@@ -60,13 +60,18 @@ void processpap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        {
                uint8_t *b = p;
                b += 4;
+               user[0] = pass[0] = 0;
                if (*b && *b < sizeof(user))
+               {
                        memcpy(user, b + 1, *b);
-               user[*b] = 0;
-               b += 1 + *b;
-               if (*b && *b < sizeof(pass))
-                       memcpy(pass, b + 1, *b);
-               pass[*b] = 0;
+                       user[*b] = 0;
+                       b += 1 + *b;
+                       if (*b && *b < sizeof(pass))
+                       {
+                               memcpy(pass, b + 1, *b);
+                               pass[*b] = 0;
+                       }
+               }
                LOG(3, s, t, "PAP login %s/%s\n", user, pass);
        }
        if (session[s].ip || !session[s].radius)
@@ -132,16 +137,13 @@ void processchap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        uint16_t r;
        uint16_t hl;
 
-       CSTAT(call_processchap);
+       CSTAT(processchap);
 
        LOG_HEX(5, "CHAP", p, l);
        r = session[s].radius;
        if (!r)
        {
                LOG(1, s, t, "Unexpected CHAP message\n");
-
-// FIXME: Need to drop the session here.
-
                STAT(tunnel_rx_errors);
                return;
        }
@@ -224,29 +226,13 @@ void processchap(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        radiussend(r, RADIUSAUTH);
 }
 
-static char *ppp_lcp_types[] = {
-       NULL,
-       "ConfigReq",
-       "ConfigAck",
-       "ConfigNak",
-       "ConfigRej",
-       "TerminateReq",
-       "TerminateAck",
-       "CodeRej",
-       "ProtocolRej",
-       "EchoReq",
-       "EchoReply",
-       "DiscardRequest",
-       "IdentRequest",
-};
-
 static void dumplcp(uint8_t *p, int l)
 {
        int x = l - 4;
        uint8_t *o = (p + 4);
 
        LOG_HEX(5, "PPP LCP Packet", p, l);
-       LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p, ppp_lcp_types[(int)*p], ntohs( ((uint16_t *) p)[1]) );
+       LOG(4, 0, 0, "PPP LCP Packet type %d (%s len %d)\n", *p, ppp_lcp_type((int)*p), ntohs( ((uint16_t *) p)[1]) );
        LOG(4, 0, 0, "Length: %d\n", l);
        if (*p != ConfigReq && *p != ConfigRej && *p != ConfigAck)
                return;
@@ -271,48 +257,48 @@ static void dumplcp(uint8_t *p, int l)
                {
                        case 1: // Maximum-Receive-Unit
                                if (length == 4)
-                                       LOG(4, 0, 0, "    %s %d\n", lcp_types[type], ntohs(*(uint16_t *)(o + 2)));
+                                       LOG(4, 0, 0, "    %s %d\n", lcp_type(type), ntohs(*(uint16_t *)(o + 2)));
                                else
-                                       LOG(4, 0, 0, "    %s odd length %d\n", lcp_types[type], length);
+                                       LOG(4, 0, 0, "    %s odd length %d\n", lcp_type(type), length);
                                break;
                        case 2: // Async-Control-Character-Map
                                if (length == 6)
                                {
                                        uint32_t asyncmap = ntohl(*(uint32_t *)(o + 2));
-                                       LOG(4, 0, 0, "    %s %x\n", lcp_types[type], asyncmap);
+                                       LOG(4, 0, 0, "    %s %x\n", lcp_type(type), asyncmap);
                                }
                                else
-                                       LOG(4, 0, 0, "   %s odd length %d\n", lcp_types[type], length);
+                                       LOG(4, 0, 0, "   %s odd length %d\n", lcp_type(type), length);
                                break;
                        case 3: // Authentication-Protocol
                                if (length == 4)
                                {
                                        int proto = ntohs(*(uint16_t *)(o + 2));
-                                       LOG(4, 0, 0, "   %s 0x%x (%s)\n", lcp_types[type], proto,
+                                       LOG(4, 0, 0, "   %s 0x%x (%s)\n", lcp_type(type), proto,
                                                proto == PPPCHAP ? "CHAP" :
                                                proto == PPPPAP  ? "PAP"  : "UNKNOWN");
                                }
                                else
-                                       LOG(4, 0, 0, "   %s odd length %d\n", lcp_types[type], length);
+                                       LOG(4, 0, 0, "   %s odd length %d\n", lcp_type(type), length);
                                break;
                        case 4: // Quality-Protocol
                                {
                                        uint32_t qp = ntohl(*(uint32_t *)(o + 2));
-                                       LOG(4, 0, 0, "    %s %x\n", lcp_types[type], qp);
+                                       LOG(4, 0, 0, "    %s %x\n", lcp_type(type), qp);
                                }
                                break;
                        case 5: // Magic-Number
                                if (length == 6)
                                {
                                        uint32_t magicno = ntohl(*(uint32_t *)(o + 2));
-                                       LOG(4, 0, 0, "    %s %x\n", lcp_types[type], magicno);
+                                       LOG(4, 0, 0, "    %s %x\n", lcp_type(type), magicno);
                                }
                                else
-                                       LOG(4, 0, 0, "   %s odd length %d\n", lcp_types[type], length);
+                                       LOG(4, 0, 0, "   %s odd length %d\n", lcp_type(type), length);
                                break;
                        case 7: // Protocol-Field-Compression
                        case 8: // Address-And-Control-Field-Compression
-                               LOG(4, 0, 0, "    %s\n", lcp_types[type]);
+                               LOG(4, 0, 0, "    %s\n", lcp_type(type));
                                break;
                        default:
                                LOG(2, 0, 0, "    Unknown PPP LCP Option type %d\n", type);
@@ -331,7 +317,7 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        uint32_t magicno = 0;
        uint16_t hl;
 
-       CSTAT(call_processlcp);
+       CSTAT(processlcp);
 
        LOG_HEX(5, "LCP", p, l);
        if (l < 4)
@@ -483,7 +469,7 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
                        *q = ConfigAck;
                }
 
-               LOG(3, s, t, "Sending %s\n", ppp_lcp_types[*response]);
+               LOG(3, s, t, "Sending %s\n", ppp_lcp_type(*response));
                tunnelsend(b, l + (q - b), t);
 
                if (!(session[s].flags & SF_LCP_ACKED))
@@ -508,6 +494,20 @@ void processlcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        {
                sessionshutdown(s, "Connection closed.");
        }
+       else if (*p == ProtocolRej)
+       {
+               if (*(uint16_t *) (p+4) == htons(PPPIPV6CP))
+               {
+                       LOG(3, s, t, "IPv6 rejected\n");
+                       session[s].flags |= SF_IPV6_NACKED;
+               }
+               else
+               {
+                       LOG(1, s, t, "Unexpected LCP protocol reject 0x%X\n",
+                               ntohs(*(uint16_t *) (p+4)));
+                       STAT(tunnel_rx_errors);
+               }
+       }
        else if (*p == EchoReq)
        {
                LOG(5, s, t, "LCP: Received EchoReq.  Sending EchoReply\n");
@@ -567,7 +567,7 @@ void processipcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
 {
        uint16_t hl;
 
-       CSTAT(call_processipcp);
+       CSTAT(processipcp);
 
        LOG_HEX(5, "IPCP", p, l);
        if (l < 5)
@@ -701,6 +701,128 @@ void processipcp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        }
 }
 
+// Process IPV6CP messages
+void processipv6cp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
+{
+
+       CSTAT(processipv6cp);
+
+       LOG_HEX(5, "IPV6CP", p, l);
+       if (l < 4)
+       {
+               LOG(1, s, t, "Short IPV6CP %d bytes\n", l);
+               STAT(tunnel_rx_errors);
+               return ;
+       }
+       if (*p == ConfigAck)
+       {
+               // happy with our IPV6CP
+               session[s].flags |= SF_IPV6CP_ACKED;
+
+               LOG(3, s, t, "IPV6CP Acked, IPv6 is now active\n");
+               // Add a routed block if configured.
+               if (session[s].ipv6prefixlen)
+               {
+                       route6set(s, session[s].ipv6route, session[s].ipv6prefixlen, 1);
+                       session[s].flags |= SF_IPV6_ROUTED;
+               }
+
+               // Send an initial RA (TODO: Should we send these regularly?)
+               send_ipv6_ra(t, s, NULL);
+               return;
+       }
+       if (*p != ConfigReq)
+       {
+               LOG(1, s, t, "Unexpected IPV6CP code %d\n", *p);
+               STAT(tunnel_rx_errors);
+               return;
+       }
+
+       LOG(4, s, t, "IPV6CP ConfigReq received\n");
+       if (ntohs(*(uint16_t *) (p + 2)) > l)
+       {
+               LOG(1, s, t, "Length mismatch IPV6CP %d/%d\n", ntohs(*(uint16_t *) (p + 2)), l);
+               STAT(tunnel_rx_errors);
+               return ;
+       }
+       if (!session[s].ip)
+       {
+               LOG(3, s, t, "Waiting on radius reply\n");
+               return;                 // have to wait on RADIUS reply
+       }
+       // form a config reply quoting the IP in the session
+       {
+               uint8_t b[MAXCONTROL];
+               uint8_t *i,
+               *q;
+
+               l = ntohs(*(uint16_t *) (p + 2)); // We must use length from IPV6CP len field
+               q = p + 4;
+               i = p + l;
+               while (q < i && q[1])
+               {
+                       if (*q != 1)
+                               break;
+                       q += q[1];
+               }
+               if (q < i)
+               {
+                       // reject
+                       uint16_t n = 4;
+                       i = p + l;
+                       if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPIPV6CP)))
+                       {
+                               LOG(2, s, t, "Failed to send IPV6CP ConfigRej\n");
+                               return;
+                       }
+                       *q = ConfigRej;
+                       p += 4;
+                       while (p < i && p[1])
+                       {
+                               if (*p != 1)
+                               {
+                                       LOG(2, s, t, "IPV6CP reject %d\n", *p);
+                                       memcpy(q + n, p, p[1]);
+                                       n += p[1];
+                               }
+                               p += p[1];
+                       }
+                       *(uint16_t *) (q + 2) = htons(n);
+                       LOG(4, s, t, "Sending ConfigRej\n");
+                       tunnelsend(b, n + (q - b), t); // send it
+               }
+               else
+               {
+                       LOG(4, s, t, "Sending ConfigAck\n");
+                       *p = ConfigAck;
+                       i = findppp(p, 1);              // IP address
+                       if (!i || i[1] != 10)
+                       {
+                               LOG(1, s, t, "No IP in IPV6CP request\n");
+                               STAT(tunnel_rx_errors);
+                               return ;
+                       }
+                       if ((*(uint32_t *) (i + 2) != htonl(session[s].ip)) || 
+                                       (*(uint32_t *) (i + 6) != 0))
+                       {
+                               *(uint32_t *) (i + 2) = htonl(session[s].ip);
+                               *(uint32_t *) (i + 6) = 0;
+                               *p = ConfigNak;
+                               LOG(4, s, t,
+                                       " No, a ConfigNak, client is "
+                                       "requesting IP - sending %s\n",
+                                       fmtaddr(htonl(session[s].ip), 0));
+                       }
+                       if (!(q = makeppp(b, sizeof(b), p, l, t, s, PPPIPV6CP)))
+                       {
+                               LOG(2, s, t, " Failed to send IPV6CP packet.\n");
+                               return;
+                       }
+                       tunnelsend(b, l + (q - b), t); // send it
+               }
+       }
+}
+
 // process IP packet received
 //
 // This MUST be called with at least 4 byte behind 'p'.
@@ -709,7 +831,7 @@ void processipin(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
 {
        in_addr_t ip;
 
-       CSTAT(call_processipin);
+       CSTAT(processipin);
 
        LOG_HEX(5, "IP", p, l);
 
@@ -735,15 +857,107 @@ void processipin(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
 
        // Add on the tun header
        p -= 4;
-       *(uint32_t *) p = htonl(0x00000800);
+       *(uint32_t *) p = htonl(PKTIP);
+       l += 4;
+
+       // Are we throttled and a slave?
+       if (session[s].tbf_in && !config->cluster_iam_master) {
+               // Pass it to the master for handling.
+               master_throttle_packet(session[s].tbf_in, p, l);
+               return;
+       }
+
+       // Are we throttled and a master??
+       if (session[s].tbf_in && config->cluster_iam_master) {
+               // Actually handle the throttled packets.
+               tbf_queue_packet(session[s].tbf_in, p, l);
+               return;
+       }
+
+       // send to ethernet
+       if (tun_write(p, l) < 0)
+       {
+               STAT(tun_tx_errors);
+               LOG(0, s, t, "Error writing %d bytes to TUN device: %s (tunfd=%d, p=%p)\n",
+                       l, strerror(errno), tunfd, p);
+
+               return;
+       }
+
+       if (session[s].snoop_ip && session[s].snoop_port)
+       {
+               // Snooping this session
+               snoop_send_packet(p + 4, l - 4, session[s].snoop_ip, session[s].snoop_port);
+       }
+
+       session[s].cin += l - 4;
+       session[s].total_cin += l - 4;
+       sess_local[s].cin += l - 4;
+
+       session[s].pin++;
+       eth_tx += l - 4;
+
+       STAT(tun_tx_packets);
+       INC_STAT(tun_tx_bytes, l - 4);
+}
+
+// process IPv6 packet received
+//
+// This MUST be called with at least 4 byte behind 'p'.
+// (i.e. this routine writes to p[-4]).
+void processipv6in(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
+{
+       struct in6_addr ip;
+       in_addr_t ipv4;
+
+       CSTAT(processipv6in);
+
+       LOG_HEX(5, "IPv6", p, l);
+
+       ip = *(struct in6_addr *) (p + 8);
+       ipv4 = ntohl(*(uint32_t *)(p + 16));
+
+       if (l > MAXETHER)
+       {
+               LOG(1, s, t, "IP packet too long %d\n", l);
+               STAT(tunnel_rx_errors);
+               return ;
+       }
+
+       // no spoof
+       if (ipv4 != session[s].ip && memcmp(&config->ipv6_prefix, &ip, 8) && sessionbyipv6(ip) != s)
+       {
+               char str[INET6_ADDRSTRLEN];
+               LOG(5, s, t, "Dropping packet with spoofed IP %s\n",
+                               inet_ntop(AF_INET6, &ip, str, INET6_ADDRSTRLEN));
+               return;
+       }
+
+       // Check if it's a Router Solicition message.
+       if (*(p + 6) == 58 && *(p + 7) == 255 && *(p + 24) == 0xFF && *(p + 25) == 2 &&
+                       *(uint32_t *)(p + 26) == 0 && *(uint32_t *)(p + 30) == 0 &&
+                       *(uint32_t *)(p + 34) == 0 &&
+                       *(p + 38) == 0 && *(p + 39) == 2 && *(p + 40) == 133) {
+               LOG(3, s, t, "Got IPv6 RS\n");
+               send_ipv6_ra(t, s, &ip);
+               return;
+       }
+
+       // Add on the tun header
+       p -= 4;
+       *(uint32_t *) p = htonl(PKTIPV6);
        l += 4;
 
-       if (session[s].tbf_in && !config->cluster_iam_master) { // Are we throttled and a slave?
-               master_throttle_packet(session[s].tbf_in, p, l); // Pass it to the master for handling.
+       // Are we throttled and a slave?
+       if (session[s].tbf_in && !config->cluster_iam_master) {
+               // Pass it to the master for handling.
+               master_throttle_packet(session[s].tbf_in, p, l);
                return;
        }
 
-       if (session[s].tbf_in && config->cluster_iam_master) { // Are we throttled and a master?? actually handle the throttled packets.
+       // Are we throttled and a master??
+       if (session[s].tbf_in && config->cluster_iam_master) {
+               // Actually handle the throttled packets.
                tbf_queue_packet(session[s].tbf_in, p, l);
                return;
        }
@@ -766,7 +980,7 @@ void processipin(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
 
        session[s].cin += l - 4;
        session[s].total_cin += l - 4;
-       sess_count[s].cin += l - 4;
+       sess_local[s].cin += l - 4;
 
        session[s].pin++;
        eth_tx += l - 4;
@@ -801,7 +1015,7 @@ void send_ipin(sessionidt s, uint8_t *buf, int len)
        // Increment packet counters
        session[s].cin += len - 4;
        session[s].total_cin += len - 4;
-       sess_count[s].cin += len - 4;
+       sess_local[s].cin += len - 4;
 
        session[s].pin++;
        eth_tx += len - 4;
@@ -817,7 +1031,7 @@ void processccp(tunnelidt t, sessionidt s, uint8_t *p, uint16_t l)
        uint8_t b[MAXCONTROL];
        uint8_t *q;
 
-       CSTAT(call_processccp);
+       CSTAT(processccp);
 
        LOG_HEX(5, "CCP", p, l);
        switch (l > 1 ? *p : 0)
@@ -869,25 +1083,24 @@ void sendchap(tunnelidt t, sessionidt s)
        uint16_t r = session[s].radius;
        uint8_t *q;
 
-       CSTAT(call_sendchap);
+       CSTAT(sendchap);
 
        if (!r)
        {
                LOG(1, s, t, "No RADIUS to send challenge\n");
                STAT(tunnel_tx_errors);
-               return ;
+               return;
        }
+
        LOG(1, s, t, "Send CHAP challenge\n");
-       {
-               // new challenge
-               int n;
-               for (n = 0; n < 15; n++)
-                       radius[r].auth[n] = rand();
-       }
+
+       // new challenge
+       random_data(radius[r].auth, sizeof(radius[r].auth));
        radius[r].chap = 1;             // CHAP not PAP
        radius[r].id++;
        if (radius[r].state != RADIUSCHAP)
                radius[r].try = 0;
+
        radius[r].state = RADIUSCHAP;
        radius[r].retry = backoff(radius[r].try++);
        if (radius[r].try > 5)