Merge branch 'master' into samesversion
[l2tpns.git] / pppoe.c
diff --git a/pppoe.c b/pppoe.c
index d00c64f..40e898e 100644 (file)
--- a/pppoe.c
+++ b/pppoe.c
@@ -1,6 +1,7 @@
 /*
  * Fernando ALVES 2013
  * Add functionality "server pppoe" to l2tpns.
 /*
  * Fernando ALVES 2013
  * Add functionality "server pppoe" to l2tpns.
+ * inspiration pppoe.c of accel-ppp
  * GPL licenced
  */
 
  * GPL licenced
  */
 
@@ -20,6 +21,7 @@
 #include <netpacket/packet.h>
 #include <arpa/inet.h>
 #include <linux/if_pppox.h>
 #include <netpacket/packet.h>
 #include <arpa/inet.h>
 #include <linux/if_pppox.h>
+#include <linux/rtnetlink.h>
 
 #include "l2tpns.h"
 #include "cluster.h"
 
 #include "l2tpns.h"
 #include "cluster.h"
@@ -97,9 +99,9 @@ static void init_pppoe_disc(void)
                exit(1);
        }
 
                exit(1);
        }
 
-       assert(strlen(ifr.ifr_name) < sizeof(config->pppoe_if_name) - 1);
-       if (*config->pppoe_if_name)
-               strncpy(ifr.ifr_name, config->pppoe_if_name, IFNAMSIZ);
+       assert(strlen(ifr.ifr_name) < sizeof(config->pppoe_if_to_bind) - 1);
+       if (*config->pppoe_if_to_bind)
+               strncpy(ifr.ifr_name, config->pppoe_if_to_bind, IFNAMSIZ);
 
        if (ioctl(pppoediscfd, SIOCGIFHWADDR, &ifr))
        {
 
        if (ioctl(pppoediscfd, SIOCGIFHWADDR, &ifr))
        {
@@ -109,7 +111,7 @@ static void init_pppoe_disc(void)
 
        if ((ifr.ifr_hwaddr.sa_data[0] & 1) != 0)
        {
 
        if ((ifr.ifr_hwaddr.sa_data[0] & 1) != 0)
        {
-               LOG(0, 0, 0, "Error pppoe: interface %s has not unicast address\n", config->pppoe_if_name);
+               LOG(0, 0, 0, "Error pppoe: interface %s has not unicast address\n", config->pppoe_if_to_bind);
                exit(1);
        }
 
                exit(1);
        }
 
@@ -122,7 +124,7 @@ static void init_pppoe_disc(void)
        }
 
        if (ifr.ifr_mtu < ETH_DATA_LEN)
        }
 
        if (ifr.ifr_mtu < ETH_DATA_LEN)
-               LOG(0, 0, 0, "Error pppoe: interface %s has MTU of %i, should be %i\n", config->pppoe_if_name, ifr.ifr_mtu, ETH_DATA_LEN);
+               LOG(0, 0, 0, "Error pppoe: interface %s has MTU of %i, should be %i\n", config->pppoe_if_to_bind, ifr.ifr_mtu, ETH_DATA_LEN);
 
        if (ioctl(pppoediscfd, SIOCGIFINDEX, &ifr))
        {
 
        if (ioctl(pppoediscfd, SIOCGIFINDEX, &ifr))
        {
@@ -174,9 +176,9 @@ static void init_pppoe_sess(void)
                exit(1);
        }
 
                exit(1);
        }
 
-       assert(strlen(ifr.ifr_name) < sizeof(config->pppoe_if_name) - 1);
-       if (*config->pppoe_if_name)
-               strncpy(ifr.ifr_name, config->pppoe_if_name, IFNAMSIZ);
+       assert(strlen(ifr.ifr_name) < sizeof(config->pppoe_if_to_bind) - 1);
+       if (*config->pppoe_if_to_bind)
+               strncpy(ifr.ifr_name, config->pppoe_if_to_bind, IFNAMSIZ);
 
        if (ioctl(pppoesessfd, SIOCGIFHWADDR, &ifr))
        {
 
        if (ioctl(pppoesessfd, SIOCGIFHWADDR, &ifr))
        {
@@ -186,7 +188,7 @@ static void init_pppoe_sess(void)
 
        if ((ifr.ifr_hwaddr.sa_data[0] & 1) != 0)
        {
 
        if ((ifr.ifr_hwaddr.sa_data[0] & 1) != 0)
        {
-               LOG(0, 0, 0, "Error pppoe: interface %s has not unicast address\n", config->pppoe_if_name);
+               LOG(0, 0, 0, "Error pppoe: interface %s has not unicast address\n", config->pppoe_if_to_bind);
                exit(1);
        }
 
                exit(1);
        }
 
@@ -199,7 +201,7 @@ static void init_pppoe_sess(void)
        }
 
        if (ifr.ifr_mtu < ETH_DATA_LEN)
        }
 
        if (ifr.ifr_mtu < ETH_DATA_LEN)
-               LOG(0, 0, 0, "Error pppoe: interface %s has MTU of %i, should be %i\n", config->pppoe_if_name, ifr.ifr_mtu, ETH_DATA_LEN);
+               LOG(0, 0, 0, "Error pppoe: interface %s has MTU of %i, should be %i\n", config->pppoe_if_to_bind, ifr.ifr_mtu, ETH_DATA_LEN);
 
        if (ioctl(pppoesessfd, SIOCGIFINDEX, &ifr))
        {
 
        if (ioctl(pppoesessfd, SIOCGIFINDEX, &ifr))
        {
@@ -501,20 +503,27 @@ static void pppoe_recv_PADI(uint8_t *pack, int size)
                return;
 
        len = ntohs(hdr->length);
                return;
 
        len = ntohs(hdr->length);
-       for (n = 0; n < len; n += sizeof(*tag) + ntohs(tag->tag_len)) {
+       for (n = 0; n < len; n += sizeof(*tag) + ntohs(tag->tag_len))
+       {
                tag = (struct pppoe_tag *)(pack + ETH_HLEN + sizeof(*hdr) + n);
                if (n + sizeof(*tag) + ntohs(tag->tag_len) > len)
                        return;
                tag = (struct pppoe_tag *)(pack + ETH_HLEN + sizeof(*hdr) + n);
                if (n + sizeof(*tag) + ntohs(tag->tag_len) > len)
                        return;
-               switch (ntohs(tag->tag_type)) {
+               switch (ntohs(tag->tag_type))
+               {
                        case TAG_END_OF_LIST:
                                break;
                        case TAG_SERVICE_NAME:
                        case TAG_END_OF_LIST:
                                break;
                        case TAG_SERVICE_NAME:
-                               if (*config->pppoe_service_name && tag->tag_len)
+                               if (config->pppoe_only_equal_svc_name && *config->pppoe_service_name && !tag->tag_len)
+                               {
+                                       break;
+                               }
+                               else if (*config->pppoe_service_name && tag->tag_len)
                                {
                                        if (ntohs(tag->tag_len) != strlen(config->pppoe_service_name))
                                                break;
                                        if (memcmp(tag->tag_data, config->pppoe_service_name, ntohs(tag->tag_len)))
                                                break;
                                {
                                        if (ntohs(tag->tag_len) != strlen(config->pppoe_service_name))
                                                break;
                                        if (memcmp(tag->tag_data, config->pppoe_service_name, ntohs(tag->tag_len)))
                                                break;
+                                       service_name_tag = tag;
                                        service_match = 1;
                                }
                                else
                                        service_match = 1;
                                }
                                else
@@ -762,6 +771,72 @@ uint8_t *pppoe_makeppp(uint8_t *b, int size, uint8_t *p, int l, sessionidt s, tu
        return b;
 }
 
        return b;
 }
 
+// fill in a PPPOE message with a PPP frame,
+// returns start of PPP frame
+//(note: THIS ROUTINE WRITES TO p[-28]).
+uint8_t *opt_pppoe_makeppp(uint8_t *p, int l, sessionidt s, tunnelidt t, uint16_t mtype, uint8_t prio, bundleidt bid, uint8_t mp_bits)
+{
+       uint16_t type = mtype;
+       uint16_t hdrlen = l;
+       uint8_t *b = p;
+       struct pppoe_hdr *hdr;
+
+       if (t != TUNNEL_ID_PPPOE)
+               return NULL;
+
+       // 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++;
+       }
+
+       b -= 2;
+       *(uint16_t *) b = htons(type);
+
+       // Size ppp packet
+       hdrlen += (p - b);
+
+       // 14 bytes ethernet Header + 6 bytes header pppoe
+       b -= (ETH_HLEN + sizeof(*hdr));
+       setup_header(b, config->pppoe_hwaddr, session[s].src_hwaddr, CODE_SESS, s, ETH_P_PPP_SES);
+       hdr = (struct pppoe_hdr *)(b + ETH_HLEN);
+       // Store length on header pppoe
+       hdr->length = hdrlen;
+
+       return b;
+}
+
 // pppoe discovery recv data
 void process_pppoe_disc(uint8_t *pack, int size)
 {
 // pppoe discovery recv data
 void process_pppoe_disc(uint8_t *pack, int size)
 {
@@ -835,7 +910,6 @@ void process_pppoe_disc(uint8_t *pack, int size)
        }
 }
 
        }
 }
 
-#ifdef LAC
 // Forward from pppoe to l2tp remote LNS
 static void pppoe_forwardto_session_rmlns(uint8_t *pack, int size, sessionidt sess, uint16_t proto)
 {
 // Forward from pppoe to l2tp remote LNS
 static void pppoe_forwardto_session_rmlns(uint8_t *pack, int size, sessionidt sess, uint16_t proto)
 {
@@ -969,7 +1043,6 @@ void pppoe_forwardto_session_pppoe(uint8_t *pack, int size, sessionidt sess, uin
 
        tunnelsend(p, lpppoe, t); // send it....
 }
 
        tunnelsend(p, lpppoe, t); // send it....
 }
-#endif
 
 void process_pppoe_sess(uint8_t *pack, int size)
 {
 
 void process_pppoe_sess(uint8_t *pack, int size)
 {
@@ -1034,13 +1107,11 @@ void process_pppoe_sess(uint8_t *pack, int size)
                lppp -= 2;
        }
 
                lppp -= 2;
        }
 
-#ifdef LAC
        if (session[sid].forwardtosession)
        {       // Must be forwaded to a remote lns tunnel l2tp
                pppoe_forwardto_session_rmlns(pack, size, sid, proto);
                return;
        }
        if (session[sid].forwardtosession)
        {       // Must be forwaded to a remote lns tunnel l2tp
                pppoe_forwardto_session_rmlns(pack, size, sid, proto);
                return;
        }
-#endif
 
        if (proto == PPPPAP)
        {
 
        if (proto == PPPPAP)
        {
@@ -1115,7 +1186,7 @@ void pppoe_send_garp()
        struct ifreq ifr;
        uint8_t mac[6];
 
        struct ifreq ifr;
        uint8_t mac[6];
 
-       if (!*config->pppoe_if_name)
+       if (!*config->pppoe_if_to_bind)
                return;
 
        s = socket(PF_INET, SOCK_DGRAM, 0);
                return;
 
        s = socket(PF_INET, SOCK_DGRAM, 0);
@@ -1125,7 +1196,7 @@ void pppoe_send_garp()
                return;
        }
        memset(&ifr, 0, sizeof(ifr));
                return;
        }
        memset(&ifr, 0, sizeof(ifr));
-       strncpy(ifr.ifr_name, config->pppoe_if_name, sizeof(ifr.ifr_name) - 1);
+       strncpy(ifr.ifr_name, config->pppoe_if_to_bind, sizeof(ifr.ifr_name) - 1);
        if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0)
        {
                LOG(0, 0, 0, "Error getting eth0 hardware address for GARP: %s\n", strerror(errno));
        if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0)
        {
                LOG(0, 0, 0, "Error getting eth0 hardware address for GARP: %s\n", strerror(errno));