small performance improvement in download (internet -> customer)
[l2tpns.git] / ppp.c
diff --git a/ppp.c b/ppp.c
index fb74ba9..e8c3b52 100644 (file)
--- a/ppp.c
+++ b/ppp.c
@@ -15,6 +15,7 @@
 #ifdef LAC
 #include "l2tplac.h"
 #endif
+#include "pppoe.h"
 
 extern tunnelt *tunnel;
 extern bundlet *bundle;
@@ -425,7 +426,7 @@ void lcp_open(sessionidt s, tunnelidt t)
        }
 }
 
-static void lcp_restart(sessionidt s)
+void lcp_restart(sessionidt s)
 {
        session[s].ppp.phase = Establish;
        // This-Layer-Down
@@ -558,7 +559,7 @@ void processlcp(sessionidt s, tunnelidt t, uint8_t *p, uint16_t l)
        if (session[s].die) // going down...
                return;
 
-       LOG((*p == EchoReq || *p == EchoReply) ? 4 : 3, s, t,
+       LOG(((*p == EchoReq || *p == EchoReply) ? 4 : 3), s, t,
                "LCP: recv %s\n", ppp_code(*p));
 
        if (config->debug > 3) dumplcp(p, l);
@@ -2521,6 +2522,11 @@ uint8_t *makeppp(uint8_t *b, int size, uint8_t *p, int l, sessionidt s, tunnelid
        uint16_t type = mtype;
        uint8_t *start = b;
 
+       if (t == TUNNEL_ID_PPPOE)
+       {
+               return pppoe_makeppp(b, size, p, l, s, t, mtype, prio, bid, mp_bits);
+       }
+
        if (size < 16) // Need more space than this!!
        {
                LOG(0, s, t, "makeppp buffer too small for L2TP header (size=%d)\n", size);
@@ -2601,6 +2607,85 @@ uint8_t *makeppp(uint8_t *b, int size, uint8_t *p, int l, sessionidt s, tunnelid
        return b;
 }
 
+// fill in a L2TP message with a PPP frame,
+// returns start of PPP frame
+//(note: THIS ROUTINE CAN WRITES TO p[-28]).
+uint8_t *opt_makeppp(uint8_t *p, int l, sessionidt s, tunnelidt t, uint16_t mtype, uint8_t prio, bundleidt bid, uint8_t mp_bits)
+{
+       uint16_t hdr = 0x0002; // L2TP with no options
+       uint16_t type = mtype;
+       uint8_t *b = p;
+
+       if (t == TUNNEL_ID_PPPOE)
+       {
+               return opt_pppoe_makeppp(p, l, s, t, mtype, prio, bid, mp_bits);
+       }
+
+       // Check whether this session is part of multilink
+       if (bid)
+       {
+               if (bundle[bid].num_of_links > 1)
+                       type = PPPMP; // Change PPP message type to the PPPMP
+               else
+                       bid = 0;
+       }
+
+       if (bid)
+       {
+               // Add the message type if this fragment has the begin bit set
+               if (mp_bits & MP_BEGIN)
+               {
+                       b -= 2;
+                       *(uint16_t *) b = htons(mtype); // Message type
+               }
+
+               // Set the sequence number and (B)egin (E)nd flags
+               if (session[s].mssf)
+               {
+                       // Set the multilink bits
+                       uint16_t bits_send = mp_bits;
+                       b -= 2;
+                       *(uint16_t *) b = htons((bundle[bid].seq_num_t & 0x0FFF)|bits_send);
+               }
+               else
+               {
+                       b -= 4;
+                       *(uint32_t *) b = htonl(bundle[bid].seq_num_t);
+                       // Set the multilink bits
+                       *b = mp_bits;
+               }
+
+               bundle[bid].seq_num_t++;
+       }
+
+       if (type < 0x100 && session[s].flags & SESSION_PFC)
+       {
+               b--;
+               *b = type;
+       }
+       else
+       {
+               b -= 2;
+               *(uint16_t *) b = htons(type);
+       }
+
+       if (type == PPPLCP || !(session[s].flags & SESSION_ACFC))
+       {
+               b -= 2;
+               *(uint16_t *) b = htons(0xFF03); // HDLC header
+       }
+
+       if (prio) hdr |= 0x0100; // set priority bit
+       b -= 2;
+       *(uint16_t *) b = htons(session[s].far); // session
+       b -= 2;
+       *(uint16_t *) b = htons(tunnel[t].far); // tunnel
+       b -= 2;
+       *(uint16_t *) b = htons(hdr);
+
+       return b;
+}
+
 static int add_lcp_auth(uint8_t *b, int size, int authtype)
 {
        int len = 0;