[systemd-commits] 5 commits - Makefile.am man/systemd.network.xml src/libsystemd src/libsystemd-network src/network src/systemd

Tom Gundersen tomegun at kemper.freedesktop.org
Thu Dec 18 23:34:24 PST 2014


 Makefile.am                              |   27 +
 man/systemd.network.xml                  |    7 
 src/libsystemd-network/lldp-internal.c   |  532 +++++++++++++++++++++++
 src/libsystemd-network/lldp-internal.h   |   97 ++++
 src/libsystemd-network/lldp-network.c    |  111 ++++
 src/libsystemd-network/lldp-network.h    |   28 +
 src/libsystemd-network/lldp-port.c       |  116 +++++
 src/libsystemd-network/lldp-port.h       |   61 ++
 src/libsystemd-network/lldp-tlv.c        |  321 ++++++++++++++
 src/libsystemd-network/lldp-tlv.h        |   87 +++
 src/libsystemd-network/lldp.h            |  115 +++++
 src/libsystemd-network/sd-lldp.c         |  705 +++++++++++++++++++++++++++++++
 src/libsystemd-network/sd-lldp.h         |   60 ++
 src/libsystemd-network/test-lldp.c       |  233 ++++++++++
 src/libsystemd/sd-network/sd-network.c   |   26 +
 src/network/networkctl.c                 |  262 +++++++++++
 src/network/networkd-link.c              |   90 +++
 src/network/networkd-link.h              |    3 
 src/network/networkd-network-gperf.gperf |    1 
 src/network/networkd.c                   |    6 
 src/network/networkd.h                   |    3 
 src/systemd/sd-network.h                 |    2 
 22 files changed, 2891 insertions(+), 2 deletions(-)

New commits:
commit bfcdba8d565041275319e5b1605c543d243ad7c4
Author: Tom Gundersen <teg at jklm.no>
Date:   Fri Dec 19 08:34:04 2014 +0100

    networkd: link - plug leak

diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 93b53ee..63c7a8b 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -259,6 +259,7 @@ static void link_free(Link *link) {
                 address_free(address);
         }
 
+        sd_dhcp_server_unref(link->dhcp_server);
         sd_dhcp_client_unref(link->dhcp_client);
         sd_dhcp_lease_unref(link->dhcp_lease);
 

commit 19727828d22057e9eb9160cae879ed5ced708517
Author: Tom Gundersen <teg at jklm.no>
Date:   Fri Dec 19 08:33:46 2014 +0100

    networkctl: lldp - respect arg_legend

diff --git a/src/network/networkctl.c b/src/network/networkctl.c
index 74735ef..21e9b38 100644
--- a/src/network/networkctl.c
+++ b/src/network/networkctl.c
@@ -927,11 +927,8 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
         if (c < 0)
                 return rtnl_log_parse_error(c);
 
-        printf("Capability Codes: (O) - Other, (P) - Repeater,  (B) - Bridge , (W) - WLAN Access Point, (R) = Router, (T) - Telephone,\n"
-               "(D) - Data Over Cable Service Interface Specifications, (A) - Station, (C) - Customer VLAN, (S) - Service VLAN,\n"
-               "(M) - Two-port MAC Relay (TPMR)\n\n");
-
-        printf("%s %16s %24s %16s %16s\n", "Local Intf", "Device ID", "Port ID", "TTL", "Capability");
+        if (arg_legend)
+                printf("%s %16s %24s %16s %16s\n", "Local Intf", "Device ID", "Port ID", "TTL", "Capability");
 
         for (i = j = 0; i < c; i++) {
                 _cleanup_free_ char *chassis = NULL, *port = NULL, *cap = NULL, *lldp = NULL;
@@ -995,7 +992,14 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
                 }
         }
 
-        printf("\nTotal entries displayed: %d\n", j);
+        if (arg_legend) {
+                printf("\nCapability Codes:\n"
+                       "(O) - Other, (P) - Repeater,  (B) - Bridge , (W) - WLAN Access Point, (R) = Router,\n"
+                       "(T) - Telephone, (D) - Data Over Cable Service Interface Specifications, (A) - Station,\n"
+                       "(C) - Customer VLAN, (S) - Service VLAN, (M) - Two-port MAC Relay (TPMR)\n\n");
+
+                printf("Total entries displayed: %d\n", j);
+        }
 
         return 0;
 }

commit 49699bac94d24b444274f91f85c82e6fad04d029
Author: Susant Sahani <susant at redhat.com>
Date:   Thu Dec 11 09:59:55 2014 +0530

    LLDP: Add support for networkctl

diff --git a/src/libsystemd-network/lldp-internal.c b/src/libsystemd-network/lldp-internal.c
index 7085a02..f86c11e 100644
--- a/src/libsystemd-network/lldp-internal.c
+++ b/src/libsystemd-network/lldp-internal.c
@@ -86,6 +86,8 @@ int lldp_read_port_id(tlv_packet *tlv,
                 goto out1;
 
         switch (subtype) {
+        case LLDP_PORT_SUBTYPE_PORT_COMPONENT:
+        case LLDP_PORT_SUBTYPE_INTERFACE_ALIAS:
         case LLDP_PORT_SUBTYPE_INTERFACE_NAME:
 
                 r = tlv_packet_read_string(tlv, &s, length);
@@ -127,12 +129,105 @@ int lldp_read_ttl(tlv_packet *tlv, uint16_t *ttl) {
 
         r = tlv_packet_read_u16(tlv, ttl);
 
-        (void) lldp_tlv_packet_exit_container(tlv);
+        (void)lldp_tlv_packet_exit_container(tlv);
 
  out:
         return r;
 }
 
+int lldp_read_system_name(tlv_packet *tlv,
+                          uint16_t *length,
+                          char **data) {
+        char *s;
+        int r;
+
+        assert_return(tlv, -EINVAL);
+
+        r = lldp_tlv_packet_enter_container(tlv, LLDP_TYPE_SYSTEM_NAME);
+        if (r < 0)
+                return r;
+
+        r = tlv_packet_read_string(tlv, &s, length);
+        if (r < 0)
+                goto out;
+
+        *data = (char *) s;
+
+ out:
+        (void)lldp_tlv_packet_exit_container(tlv);
+
+        return r;
+}
+
+int lldp_read_system_description(tlv_packet *tlv,
+                                 uint16_t *length,
+                                 char **data) {
+        char *s;
+        int r;
+
+        assert_return(tlv, -EINVAL);
+
+        r = lldp_tlv_packet_enter_container(tlv, LLDP_TYPE_SYSTEM_DESCRIPTION);
+        if (r < 0)
+                return r;
+
+        r = tlv_packet_read_string(tlv, &s, length);
+        if (r < 0)
+                goto out;
+
+        *data = (char *) s;
+
+ out:
+        (void)lldp_tlv_packet_exit_container(tlv);
+
+        return r;
+}
+
+int lldp_read_port_description(tlv_packet *tlv,
+                               uint16_t *length,
+                               char **data) {
+        char *s;
+        int r;
+
+        assert_return(tlv, -EINVAL);
+
+        r = lldp_tlv_packet_enter_container(tlv, LLDP_TYPE_PORT_DESCRIPTION);
+        if (r < 0)
+                return r;
+
+        r = tlv_packet_read_string(tlv, &s, length);
+        if (r < 0)
+                goto out;
+
+        *data = (char *) s;
+
+ out:
+        (void)lldp_tlv_packet_exit_container(tlv);
+
+        return r;
+}
+
+int lldp_read_system_capability(tlv_packet *tlv, uint16_t *data) {
+        int r;
+
+        assert_return(tlv, -EINVAL);
+
+        r = lldp_tlv_packet_enter_container(tlv, LLDP_TYPE_SYSTEM_CAPABILITIES);
+        if (r < 0)
+                return r;
+
+        r = tlv_packet_read_u16(tlv, data);
+        if (r < 0)
+                goto out;
+
+        return 0;
+ out:
+
+        (void)lldp_tlv_packet_exit_container(tlv);
+
+        return r;
+}
+
 /* 10.5.5.2.2 mibUpdateObjects ()
  * The mibUpdateObjects () procedure updates the MIB objects corresponding to
  * the TLVs contained in the received LLDPDU for the LLDP remote system
diff --git a/src/libsystemd-network/lldp-internal.h b/src/libsystemd-network/lldp-internal.h
index dbaf389..028a35f 100644
--- a/src/libsystemd-network/lldp-internal.h
+++ b/src/libsystemd-network/lldp-internal.h
@@ -89,5 +89,9 @@ int lldp_mib_remove_objects(lldp_chassis *c, tlv_packet *tlv);
 int lldp_read_chassis_id(tlv_packet *tlv, uint8_t *type, uint16_t *length, uint8_t **data);
 int lldp_read_port_id(tlv_packet *tlv, uint8_t *type, uint16_t *length, uint8_t **data);
 int lldp_read_ttl(tlv_packet *tlv, uint16_t *ttl);
+int lldp_read_system_name(tlv_packet *tlv, uint16_t *length, char **data);
+int lldp_read_system_description(tlv_packet *tlv, uint16_t *length, char **data);
+int lldp_read_system_capability(tlv_packet *tlv, uint16_t *data);
+int lldp_read_port_description(tlv_packet *tlv, uint16_t *length, char **data);
 
 #define log_lldp(fmt, ...) log_internal(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, "LLDP: " fmt, ##__VA_ARGS__)
diff --git a/src/libsystemd-network/sd-lldp.c b/src/libsystemd-network/sd-lldp.c
index 97d1578..45881e5 100644
--- a/src/libsystemd-network/sd-lldp.c
+++ b/src/libsystemd-network/sd-lldp.c
@@ -30,7 +30,21 @@
 #include "lldp-port.h"
 #include "sd-lldp.h"
 #include "prioq.h"
+#include "strv.h"
 #include "lldp-internal.h"
+#include "ether-addr-util.h"
+
+typedef enum LLDPAgentRXState {
+        LLDP_AGENT_RX_WAIT_PORT_OPERATIONAL = 4,
+        LLDP_AGENT_RX_DELETE_AGED_INFO,
+        LLDP_AGENT_RX_LLDP_INITIALIZE,
+        LLDP_AGENT_RX_WAIT_FOR_FRAME,
+        LLDP_AGENT_RX_RX_FRAME,
+        LLDP_AGENT_RX_DELETE_INFO,
+        LLDP_AGENT_RX_UPDATE_INFO,
+        _LLDP_AGENT_RX_STATE_MAX,
+        _LLDP_AGENT_RX_INVALID = -1,
+} LLDPAgentRXState;
 
 /* Section 10.5.2.2 Reception counters */
 struct lldp_agent_statitics {
@@ -48,6 +62,11 @@ struct sd_lldp {
         Prioq *by_expiry;
         Hashmap *neighbour_mib;
 
+        sd_lldp_cb_t cb;
+
+        void *userdata;
+
+        LLDPAgentRXState rx_state;
         lldp_agent_statitics statitics;
 };
 
@@ -87,6 +106,8 @@ static const struct hash_ops chassis_id_hash_ops = {
 };
 
 static void lldp_mib_delete_objects(sd_lldp *lldp);
+static void lldp_set_state(sd_lldp *lldp, LLDPAgentRXState state);
+static void lldp_run_state_machine(sd_lldp *ll);
 
 static int lldp_receive_frame(sd_lldp *lldp, tlv_packet *tlv) {
         int r;
@@ -95,13 +116,19 @@ static int lldp_receive_frame(sd_lldp *lldp, tlv_packet *tlv) {
         assert(tlv);
 
         /* Remove expired packets */
-        if (prioq_size(lldp->by_expiry) > 0)
+        if (prioq_size(lldp->by_expiry) > 0) {
+
+                lldp_set_state(lldp, LLDP_AGENT_RX_DELETE_INFO);
+
                 lldp_mib_delete_objects(lldp);
+        }
 
         r = lldp_mib_add_objects(lldp->by_expiry, lldp->neighbour_mib, tlv);
         if (r < 0)
                 goto out;
 
+        lldp_set_state(lldp, LLDP_AGENT_RX_UPDATE_INFO);
+
         log_lldp("Packet added. MIB size: %d , PQ size: %d",
                  hashmap_size(lldp->neighbour_mib),
                  prioq_size(lldp->by_expiry));
@@ -114,6 +141,8 @@ static int lldp_receive_frame(sd_lldp *lldp, tlv_packet *tlv) {
         if (r < 0)
                 log_lldp("Receive frame failed: %s", strerror(-r));
 
+        lldp_set_state(lldp, LLDP_AGENT_RX_WAIT_FOR_FRAME);
+
         return 0;
 }
 
@@ -142,6 +171,8 @@ int lldp_handle_packet(tlv_packet *tlv, uint16_t length) {
                 goto out;
         }
 
+        lldp_set_state(lldp, LLDP_AGENT_RX_RX_FRAME);
+
         p = tlv->pdu;
         p += sizeof(struct ether_header);
 
@@ -304,6 +335,8 @@ int lldp_handle_packet(tlv_packet *tlv, uint16_t length) {
         return lldp_receive_frame(lldp, tlv);
 
  out:
+        lldp_set_state(lldp, LLDP_AGENT_RX_WAIT_FOR_FRAME);
+
         if (malformed) {
                 lldp->statitics.stats_frames_discarded_total ++;
                 lldp->statitics.stats_frames_in_errors_total ++;
@@ -326,6 +359,23 @@ static int ttl_expiry_item_prioq_compare_func(const void *a, const void *b) {
         return 0;
 }
 
+static void lldp_set_state(sd_lldp *lldp, LLDPAgentRXState state) {
+
+        assert(lldp);
+        assert(state < _LLDP_AGENT_RX_STATE_MAX);
+
+        lldp->rx_state = state;
+
+        lldp_run_state_machine(lldp);
+}
+
+static void lldp_run_state_machine(sd_lldp *lldp) {
+
+        if (lldp->rx_state == LLDP_AGENT_RX_UPDATE_INFO)
+                if (lldp->cb)
+                        lldp->cb(lldp, LLDP_AGENT_RX_UPDATE_INFO, lldp->userdata);
+}
+
 /* 10.5.5.2.1 mibDeleteObjects ()
  * The mibDeleteObjects () procedure deletes all information in the LLDP remote
  * systems MIB associated with the MSAP identifier if an LLDPDU is received with
@@ -377,6 +427,150 @@ static void lldp_mib_objects_flush(sd_lldp *lldp) {
         assert(prioq_size(lldp->by_expiry) == 0);
 }
 
+int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
+        _cleanup_free_ char *s = NULL, *t = NULL, *k = NULL;
+        _cleanup_free_ char *temp_path = NULL;
+        _cleanup_fclose_ FILE *f = NULL;
+        uint8_t *mac, *port_id, type;
+        lldp_neighbour_port *p;
+        uint16_t data = 0, length = 0;
+        char buf[LINE_MAX];
+        lldp_chassis *c;
+        usec_t time;
+        Iterator i;
+        int r;
+
+        assert(lldp);
+        assert(lldp_file);
+
+        r = fopen_temporary(lldp_file, &f, &temp_path);
+        if (r < 0)
+                goto finish;
+
+        fchmod(fileno(f), 0644);
+
+        HASHMAP_FOREACH(c, lldp->neighbour_mib, i) {
+                LIST_FOREACH(port, p, c->ports) {
+
+                        r = lldp_read_chassis_id(p->packet, &type, &length, &mac);
+                        if (r < 0)
+                                continue;
+
+                        memzero(buf, LINE_MAX);
+
+                        sprintf(buf, "'_Chassis=%02x:%02x:%02x:%02x:%02x:%02x' '_CType=%d' ",
+                                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], type);
+
+                        s = strdup(buf);
+                        if (!s)
+                                return -ENOMEM;
+
+                        r = lldp_read_port_id(p->packet, &type, &length, &port_id);
+                        if (r < 0) {
+                                free(s);
+                                continue;
+                        }
+
+                        memzero(buf, LINE_MAX);
+                        if (type != LLDP_PORT_SUBTYPE_MAC_ADDRESS) {
+
+                                k = strndup((char *) port_id, length -1);
+                                if (!k)
+                                        return -ENOMEM;
+
+                                sprintf(buf, "'_Port=%s' '_PType=%d' ", k , type);
+
+                                t = strappend(s, buf);
+
+                                free(k);
+                        } else {
+
+                                mac = port_id;
+
+                                sprintf(buf, "'_Port=%02x:%02x:%02x:%02x:%02x:%02x' '_PType=%d' ",
+                                        mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], type);
+
+                                t = strappend(s, buf);
+                        }
+
+                        if (!t)
+                                return -ENOMEM;
+
+                        free(s);
+                        s = t;
+
+                        time = now(CLOCK_BOOTTIME);
+
+                        /* Don't write expired packets */
+                        if(time - p->until <= 0) {
+                                free(s);
+                                continue;
+                        }
+
+                        memzero(buf, LINE_MAX);
+                        sprintf(buf, "'_TTL=%lu' ", p->until);
+
+                        t = strappend(s, buf);
+                        if (!t)
+                                return -ENOMEM;
+
+                        free(s);
+                        s = t;
+
+                        r = lldp_read_system_name(p->packet, &length, &k);
+                        if (r < 0)
+                                k = strappend(s, "'_NAME=N/A' ");
+                        else {
+                                t = strndup(k, length);
+                                if (!t)
+                                        return -ENOMEM;
+
+                                k = strjoin(s, "'_NAME=", t, "' ", NULL);
+                        }
+
+                        if (!k)
+                                return -ENOMEM;
+
+                        free(s);
+                        s = k;
+
+                        memzero(buf, LINE_MAX);
+
+                        (void)lldp_read_system_capability(p->packet, &data);
+
+                        sprintf(buf, "'_CAP=%x'", data);
+
+                        t = strappend(s, buf);
+                        if (!t)
+                                return -ENOMEM;
+
+                        fprintf(f, "%s\n", t);
+
+                        free(s);
+                        free(t);
+                }
+        }
+        r = 0;
+
+        s = NULL;
+        t = NULL;
+        k = NULL;
+
+        fflush(f);
+
+        if (ferror(f) || rename(temp_path, lldp_file) < 0) {
+                r = -errno;
+                unlink(lldp_file);
+                unlink(temp_path);
+        }
+
+ finish:
+        if (r < 0)
+                log_error("Failed to save lldp data %s: %s", lldp_file, strerror(-r));
+
+        return r;
+}
+
 int sd_lldp_start(sd_lldp *lldp) {
         int r;
 
@@ -385,14 +579,21 @@ int sd_lldp_start(sd_lldp *lldp) {
 
         lldp->port->status = LLDP_PORT_STATUS_ENABLED;
 
+        lldp_set_state(lldp, LLDP_AGENT_RX_LLDP_INITIALIZE);
+
         r = lldp_port_start(lldp->port);
         if (r < 0) {
                 log_lldp("Failed to start Port : %s , %s",
                          lldp->port->ifname,
                          strerror(-r));
+
+                lldp_set_state(lldp, LLDP_AGENT_RX_WAIT_PORT_OPERATIONAL);
+
                 return r;
         }
 
+        lldp_set_state(lldp, LLDP_AGENT_RX_WAIT_FOR_FRAME);
+
         return 0;
 }
 
@@ -441,6 +642,15 @@ int sd_lldp_detach_event(sd_lldp *lldp) {
         return 0;
 }
 
+int sd_lldp_set_callback(sd_lldp *lldp, sd_lldp_cb_t cb, void *userdata) {
+        assert_return(lldp, -EINVAL);
+
+        lldp->cb = cb;
+        lldp->userdata = userdata;
+
+        return 0;
+}
+
 void sd_lldp_free(sd_lldp *lldp) {
 
         if (!lldp)
@@ -486,6 +696,8 @@ int sd_lldp_new(int ifindex,
         if (r < 0)
                 return r;
 
+        lldp->rx_state = LLDP_AGENT_RX_WAIT_PORT_OPERATIONAL;
+
         *ret = lldp;
         lldp = NULL;
 
diff --git a/src/libsystemd-network/sd-lldp.h b/src/libsystemd-network/sd-lldp.h
index 95d802d..ee064c6 100644
--- a/src/libsystemd-network/sd-lldp.h
+++ b/src/libsystemd-network/sd-lldp.h
@@ -28,6 +28,12 @@
 typedef struct sd_lldp sd_lldp;
 typedef struct lldp_agent_statitics lldp_agent_statitics;
 
+typedef void (*sd_lldp_cb_t)(sd_lldp *lldp, int event, void *userdata);
+
+enum {
+        UPDATE_INFO = 10,
+};
+
 typedef enum LLDPPortStatus {
         LLDP_PORT_STATUS_NONE,
         LLDP_PORT_STATUS_ENABLED,
@@ -48,4 +54,7 @@ int sd_lldp_stop(sd_lldp *lldp);
 int sd_lldp_attach_event(sd_lldp *lldp, sd_event *event, int priority);
 int sd_lldp_detach_event(sd_lldp *lldp);
 
+int sd_lldp_set_callback(sd_lldp *lldp, sd_lldp_cb_t cb, void *userdata);
+int sd_lldp_save(sd_lldp *lldp, const char *file);
+
 int lldp_handle_packet(tlv_packet *m, uint16_t length);
diff --git a/src/libsystemd/sd-network/sd-network.c b/src/libsystemd/sd-network/sd-network.c
index d63e6f9..c735cac 100644
--- a/src/libsystemd/sd-network/sd-network.c
+++ b/src/libsystemd/sd-network/sd-network.c
@@ -192,6 +192,32 @@ _public_ int sd_network_link_get_llmnr(int ifindex, char **llmnr) {
         return 0;
 }
 
+_public_ int sd_network_link_get_lldp(int ifindex, char **lldp) {
+        _cleanup_free_ char *s = NULL, *p = NULL;
+        size_t size;
+        int r;
+
+        assert_return(ifindex > 0, -EINVAL);
+        assert_return(lldp, -EINVAL);
+
+        if (asprintf(&p, "/run/systemd/netif/lldp/%d", ifindex) < 0)
+                return -ENOMEM;
+
+        r = read_full_file(p, &s, &size);
+        if (r == -ENOENT)
+                return -ENODATA;
+        if (r < 0)
+                return r;
+        if (size <= 0)
+                return -ENODATA;
+
+        *lldp = s;
+        s = NULL;
+
+        return 0;
+}
+
+
 static int network_get_link_strv(const char *key, int ifindex, char ***ret) {
         _cleanup_free_ char *p = NULL, *s = NULL;
         _cleanup_strv_free_ char **a = NULL;
diff --git a/src/network/networkctl.c b/src/network/networkctl.c
index aa868f5..74735ef 100644
--- a/src/network/networkctl.c
+++ b/src/network/networkctl.c
@@ -32,6 +32,7 @@
 #include "build.h"
 #include "util.h"
 #include "pager.h"
+#include "lldp.h"
 #include "rtnl-util.h"
 #include "udev-util.h"
 #include "hwdb-util.h"
@@ -744,6 +745,261 @@ static int link_status(int argc, char *argv[], void *userdata) {
         return 0;
 }
 
+const char *lldp_system_capability_to_string(LLDPSystemCapabilities d) _const_;
+LLDPSystemCapabilities lldp_system_capability_from_string(const char *d) _pure_;
+
+static const char* const lldp_system_capability_table[_LLDP_SYSTEM_CAPABILITIES_MAX + 1] = {
+        [LLDP_SYSTEM_CAPABILITIES_OTHER] = "O",
+        [LLDP_SYSTEM_CAPABILITIES_REPEATER] = "P",
+        [LLDP_SYSTEM_CAPABILITIES_BRIDGE] = "B",
+        [LLDP_SYSTEM_CAPABILITIES_WLAN_AP] = "W",
+        [LLDP_SYSTEM_CAPABILITIES_ROUTER] = "R",
+        [LLDP_SYSTEM_CAPABILITIES_PHONE] = "T",
+        [LLDP_SYSTEM_CAPABILITIES_DOCSIS] = "D",
+        [LLDP_SYSTEM_CAPABILITIES_STATION] = "A",
+        [LLDP_SYSTEM_CAPABILITIES_CVLAN] = "C",
+        [LLDP_SYSTEM_CAPABILITIES_SVLAN] = "S",
+        [LLDP_SYSTEM_CAPABILITIES_TPMR] = "M",
+        [_LLDP_SYSTEM_CAPABILITIES_MAX] = "N/A",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(lldp_system_capability, LLDPSystemCapabilities);
+
+static char *lldp_system_caps(uint16_t cap) {
+        _cleanup_free_ char *s = NULL, *t = NULL;
+        char *capability;
+
+        t = strdup("[ ");
+        if (!t)
+                return NULL;
+
+        if (cap & LLDP_SYSTEM_CAPABILITIES_OTHER) {
+                s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_OTHER), " ", NULL);
+                if (!s)
+                        return NULL;
+
+                free(t);
+                t = s;
+        }
+
+        if (cap & LLDP_SYSTEM_CAPABILITIES_REPEATER) {
+                s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_REPEATER), " ", NULL);
+                if (!s)
+                        return NULL;
+
+                free(t);
+                t = s;
+        }
+
+        if (cap & LLDP_SYSTEM_CAPABILITIES_BRIDGE) {
+                s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_BRIDGE), " ", NULL);
+                if (!s)
+                        return NULL;
+
+                free(t);
+                t = s;
+        }
+
+        if (cap & LLDP_SYSTEM_CAPABILITIES_WLAN_AP) {
+                s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_WLAN_AP), " ", NULL);
+                if (!s)
+                        return NULL;
+
+                free(t);
+                t = s;
+        }
+
+        if (cap & LLDP_SYSTEM_CAPABILITIES_ROUTER) {
+                s =  strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_ROUTER), " ", NULL);
+                if (!s)
+                        return NULL;
+
+                free(t);
+                t = s;
+        }
+
+        if (cap & LLDP_SYSTEM_CAPABILITIES_PHONE) {
+                s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_PHONE), " ", NULL);
+                if (!s)
+                        return NULL;
+
+                free(t);
+                t = s;
+        }
+
+        if (cap & LLDP_SYSTEM_CAPABILITIES_DOCSIS) {
+                s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_DOCSIS), " ", NULL);
+                if (!s)
+                        return NULL;
+
+                free(t);
+                t = s;
+        }
+
+        if (cap & LLDP_SYSTEM_CAPABILITIES_STATION) {
+                s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_STATION), " ", NULL);
+                if (!s)
+                        return NULL;
+
+                free(t);
+                t = s;
+        }
+
+        if (cap & LLDP_SYSTEM_CAPABILITIES_CVLAN) {
+                s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_CVLAN), " ", NULL);
+                if (!s)
+                        return NULL;
+
+                free(t);
+                t = s;
+        }
+
+        if (cap & LLDP_SYSTEM_CAPABILITIES_SVLAN) {
+                s = strjoin(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_SVLAN), " ", NULL);
+                if (!s)
+                        return NULL;
+
+                free(t);
+                t = s;
+        }
+
+        if (cap & LLDP_SYSTEM_CAPABILITIES_TPMR) {
+                s = strappend(t, lldp_system_capability_to_string(LLDP_SYSTEM_CAPABILITIES_TPMR));
+                if (!s)
+                        return NULL;
+
+                free(t);
+        }
+
+        if (!s) {
+                s = strappend(t, lldp_system_capability_to_string(_LLDP_SYSTEM_CAPABILITIES_MAX));
+                if (!s)
+                        return NULL;
+
+                free(t);
+        }
+
+        t = strappend(s, "]");
+        if (!s)
+                return NULL;
+
+        free(s);
+        capability = t;
+
+        s = NULL;
+        t = NULL;
+
+        return capability;
+}
+
+static int link_lldp_status(int argc, char *argv[], void *userdata) {
+        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
+        _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
+        _cleanup_free_ LinkInfo *links = NULL;
+        const char *state, *word;
+
+        usec_t time, until, ttl;
+        uint32_t capability;
+        char buf[LINE_MAX];
+        int i, r, c, j;
+        size_t ll;
+        char **s;
+
+        pager_open_if_enabled();
+
+        r = sd_rtnl_open(&rtnl, 0);
+        if (r < 0)
+                return log_error_errno(r, "Failed to connect to netlink: %m");
+
+        r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
+        if (r < 0)
+                return rtnl_log_create_error(r);
+
+        r = sd_rtnl_message_request_dump(req, true);
+        if (r < 0)
+                return rtnl_log_create_error(r);
+
+        r = sd_rtnl_call(rtnl, req, 0, &reply);
+        if (r < 0)
+                return log_error_errno(r, "Failed to enumerate links: %m");
+
+        c = decode_and_sort_links(reply, &links);
+        if (c < 0)
+                return rtnl_log_parse_error(c);
+
+        printf("Capability Codes: (O) - Other, (P) - Repeater,  (B) - Bridge , (W) - WLAN Access Point, (R) = Router, (T) - Telephone,\n"
+               "(D) - Data Over Cable Service Interface Specifications, (A) - Station, (C) - Customer VLAN, (S) - Service VLAN,\n"
+               "(M) - Two-port MAC Relay (TPMR)\n\n");
+
+        printf("%s %16s %24s %16s %16s\n", "Local Intf", "Device ID", "Port ID", "TTL", "Capability");
+
+        for (i = j = 0; i < c; i++) {
+                _cleanup_free_ char *chassis = NULL, *port = NULL, *cap = NULL, *lldp = NULL;
+                _cleanup_strv_free_ char **l = NULL;
+
+                r = sd_network_link_get_lldp(links[i].ifindex, &lldp);
+                if (r < 0)
+                        continue;
+
+                l = strv_split_newlines(lldp);
+                if (!l)
+                        return -ENOMEM;
+
+                STRV_FOREACH(s, l) {
+                        FOREACH_WORD_QUOTED(word, ll, *s, state) {
+                                _cleanup_free_ char *t = NULL, *a = NULL, *b = NULL;
+
+                                t = strndup(word, ll);
+                                if (!t)
+                                        return -ENOMEM;
+
+                                r = split_pair(t, "=", &a, &b);
+                                if (r < 0)
+                                        continue;
+
+                                if (streq(a, "_Chassis")) {
+
+                                        memzero(buf, LINE_MAX);
+
+                                        chassis = strdup(b);
+                                        if (!chassis)
+                                                return -ENOMEM;
+
+                                } else if (streq(a, "_Port")) {
+
+                                        port = strdup(b);
+                                        if (!port)
+                                                return -ENOMEM;
+
+                                } else if (streq(a, "_TTL")) {
+
+                                        time = now(CLOCK_BOOTTIME);
+
+                                        sscanf(b, "%lu", &until);
+
+                                        ttl = (until - time) / USEC_PER_SEC;
+
+
+                                } else if (streq(a, "_CAP")) {
+                                        sscanf(b, "%x", &capability);
+
+                                        cap = lldp_system_caps(capability);
+                                }
+
+                        }
+
+                        if (until > time) {
+                                printf("%10s %24s %16s %16lu %16s\n", links[i].name, chassis, port, ttl, cap);
+                                j++;
+                        }
+                }
+        }
+
+        printf("\nTotal entries displayed: %d\n", j);
+
+        return 0;
+}
+
 static void help(void) {
         printf("%s [OPTIONS...]\n\n"
                "Query and control the networking subsystem.\n\n"
@@ -755,6 +1011,7 @@ static void help(void) {
                "Commands:\n"
                "  list                  List links\n"
                "  status LINK           Show link status\n"
+               "  lldp                  Show lldp information\n"
                , program_invocation_short_name);
 }
 
@@ -820,6 +1077,7 @@ static int networkctl_main(int argc, char *argv[]) {
         const Verb verbs[] = {
                 { "list", VERB_ANY, 1, VERB_DEFAULT, list_links },
                 { "status", 1, VERB_ANY, 0, link_status },
+                { "lldp", VERB_ANY, 1, VERB_DEFAULT, link_lldp_status },
                 {}
         };
 
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 725e22b..93b53ee 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -219,6 +219,12 @@ static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
         if (r < 0)
                 return -ENOMEM;
 
+        r = asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d",
+                     link->ifindex);
+        if (r < 0)
+                return -ENOMEM;
+
+
         r = hashmap_ensure_allocated(&manager->links, NULL);
         if (r < 0)
                 return r;
@@ -259,6 +265,9 @@ static void link_free(Link *link) {
         unlink(link->lease_file);
         free(link->lease_file);
 
+        unlink(link->lldp_file);
+        free(link->lldp_file);
+
         sd_ipv4ll_unref(link->ipv4ll);
         sd_dhcp6_client_unref(link->dhcp6_client);
         sd_icmp6_nd_unref(link->icmp6_router_discovery);
@@ -895,6 +904,23 @@ static int link_set_bridge(Link *link) {
         return r;
 }
 
+static void lldp_handler(sd_lldp *lldp, int event, void *userdata) {
+        Link *link = userdata;
+        int r;
+
+        assert(link);
+        assert(link->network);
+        assert(link->manager);
+
+        if (event != UPDATE_INFO)
+                return;
+
+        r = sd_lldp_save(link->lldp, link->lldp_file);
+        if (r < 0)
+                log_link_warning(link, "could not save LLDP");
+
+}
+
 static int link_acquire_conf(Link *link) {
         int r;
 
@@ -1237,6 +1263,11 @@ static int link_configure(Link *link) {
                 r = sd_lldp_attach_event(link->lldp, NULL, 0);
                 if (r < 0)
                         return r;
+
+                r = sd_lldp_set_callback(link->lldp,
+                                         lldp_handler, link);
+                if (r < 0)
+                        return r;
         }
 
         if (link_has_carrier(link)) {
@@ -1855,6 +1886,19 @@ int link_save(Link *link) {
         } else
                 unlink(link->lease_file);
 
+        if (link->lldp) {
+                assert(link->network);
+
+                r = sd_lldp_save(link->lldp, link->lldp_file);
+                if (r < 0)
+                        goto fail;
+
+                fprintf(f,
+                        "LLDP_FILE=%s\n",
+                        link->lldp_file);
+        } else
+                unlink(link->lldp_file);
+
         r = fflush_and_check(f);
         if (r < 0)
                 goto fail;
diff --git a/src/network/networkd-link.h b/src/network/networkd-link.h
index 6bb59d2..0e2f558 100644
--- a/src/network/networkd-link.h
+++ b/src/network/networkd-link.h
@@ -93,6 +93,7 @@ struct Link {
         sd_dhcp6_client *dhcp6_client;
 
         sd_lldp *lldp;
+        char *lldp_file;
 };
 
 Link *link_unref(Link *link);
diff --git a/src/network/networkd.c b/src/network/networkd.c
index 0b386d4..ced319d 100644
--- a/src/network/networkd.c
+++ b/src/network/networkd.c
@@ -64,6 +64,12 @@ int main(int argc, char *argv[]) {
         if (r < 0)
                 log_error_errno(r, "Could not create runtime directory 'leases': %m");
 
+        r = mkdir_safe_label("/run/systemd/netif/lldp", 0755, uid, gid);
+        if (r < 0)
+                log_error("Could not create runtime directory 'lldp': %s",
+                          strerror(-r));
+
+
         r = drop_privileges(uid, gid,
                             (1ULL << CAP_NET_ADMIN) |
                             (1ULL << CAP_NET_BIND_SERVICE) |
diff --git a/src/systemd/sd-network.h b/src/systemd/sd-network.h
index bb69940..027730d 100644
--- a/src/systemd/sd-network.h
+++ b/src/systemd/sd-network.h
@@ -111,6 +111,8 @@ int sd_network_link_get_ntp(int ifindex, char ***addr);
  */
 int sd_network_link_get_llmnr(int ifindex, char **llmnr);
 
+int sd_network_link_get_lldp(int ifindex, char **lldp);
+
 /* Get the DNS domain names for a given link. */
 int sd_network_link_get_domains(int ifindex, char ***domains);
 

commit ce43e484465050c619ea9a1991d49b3d6215028b
Author: Susant Sahani <susant at redhat.com>
Date:   Sun Nov 23 09:56:14 2014 +0530

    networkd: integrate LLDP
    
    This patch integrates LLDP with networkd.
    
    Example conf:
    file : lldp.network
    
    [Match]
    Name=em1
    
    [Network]
    LLDP=yes

diff --git a/man/systemd.network.xml b/man/systemd.network.xml
index 360c57c..ea278c7 100644
--- a/man/systemd.network.xml
+++ b/man/systemd.network.xml
@@ -258,6 +258,13 @@
                                         </listitem>
                                 </varlistentry>
                                 <varlistentry>
+                                        <term><varname>LLDP=</varname></term>
+                                        <listitem>
+                                                <para>A boolean. When true, enables LLDP link receive support.
+                                                </para>
+                                        </listitem>
+                                </varlistentry>
+                                <varlistentry>
                                         <term><varname>Address=</varname></term>
                                         <listitem>
                                                 <para>A static IPv4 or IPv6 address and its prefix length,
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 341ae88..725e22b 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -75,6 +75,19 @@ static bool link_ipv4ll_enabled(Link *link) {
         return link->network->ipv4ll;
 }
 
+static bool link_lldp_enabled(Link *link) {
+        if (link->flags & IFF_LOOPBACK)
+                return false;
+
+        if (!link->network)
+                return false;
+
+        if(link->network->bridge)
+                return false;
+
+        return link->network->lldp;
+}
+
 #define FLAG_STRING(string, flag, old, new) \
         (((old ^ new) & flag) \
                 ? ((old & flag) ? (" -" string) : (" +" string)) \
@@ -364,6 +377,16 @@ static int link_stop_clients(Link *link) {
                 }
         }
 
+        if (link->lldp) {
+
+                k = sd_lldp_stop(link->lldp);
+                if (k < 0) {
+                        log_link_warning(link, "Could not stop LLDP : %s",
+                                         strerror(-r));
+                        r = k;
+                }
+        }
+
         return r;
 }
 
@@ -919,6 +942,18 @@ static int link_acquire_conf(Link *link) {
                 }
         }
 
+        if (link_lldp_enabled(link)) {
+                assert(link->lldp);
+
+                log_link_debug(link, "Starting LLDP");
+
+                r = sd_lldp_start(link->lldp);
+                if (r < 0) {
+                        log_link_warning(link, "could not start LLDP ");
+                        return r;
+                }
+        }
+
         return 0;
 }
 
@@ -1194,6 +1229,16 @@ static int link_configure(Link *link) {
                         return r;
         }
 
+        if (link_lldp_enabled(link)) {
+                r = sd_lldp_new(link->ifindex, link->ifname, &link->mac, &link->lldp);
+                if (r < 0)
+                        return r;
+
+                r = sd_lldp_attach_event(link->lldp, NULL, 0);
+                if (r < 0)
+                        return r;
+        }
+
         if (link_has_carrier(link)) {
                 r = link_acquire_conf(link);
                 if (r < 0)
diff --git a/src/network/networkd-link.h b/src/network/networkd-link.h
index 05c34ee..6bb59d2 100644
--- a/src/network/networkd-link.h
+++ b/src/network/networkd-link.h
@@ -91,6 +91,8 @@ struct Link {
 
         sd_icmp6_nd *icmp6_router_discovery;
         sd_dhcp6_client *dhcp6_client;
+
+        sd_lldp *lldp;
 };
 
 Link *link_unref(Link *link);
diff --git a/src/network/networkd-network-gperf.gperf b/src/network/networkd-network-gperf.gperf
index fb0a209..5094b48 100644
--- a/src/network/networkd-network-gperf.gperf
+++ b/src/network/networkd-network-gperf.gperf
@@ -37,6 +37,7 @@ Network.DHCP,                config_parse_dhcp,                  0,
 Network.DHCPServer,          config_parse_bool,                  0,                             offsetof(Network, dhcp_server)
 Network.IPv4LL,              config_parse_bool,                  0,                             offsetof(Network, ipv4ll)
 Network.IPv4LLRoute,         config_parse_bool,                  0,                             offsetof(Network, ipv4ll_route)
+Network.LLDP,                config_parse_bool,                  0,                             offsetof(Network, lldp)
 Network.Address,             config_parse_address,               0,                             0
 Network.Gateway,             config_parse_gateway,               0,                             0
 Network.Domains,             config_parse_domains,               0,                             offsetof(Network, domains)
diff --git a/src/network/networkd.h b/src/network/networkd.h
index a5c5b08..7107c5f 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -32,6 +32,7 @@
 #include "sd-icmp6-nd.h"
 #include "sd-dhcp6-client.h"
 #include "udev.h"
+#include "sd-lldp.h"
 
 #include "rtnl-util.h"
 #include "hashmap.h"
@@ -122,6 +123,8 @@ struct Network {
         struct ether_addr *mac;
         unsigned mtu;
 
+        bool lldp;
+
         LIST_HEAD(Address, static_addresses);
         LIST_HEAD(Route, static_routes);
         LIST_HEAD(FdbEntry, static_fdb_entries);

commit ad1ad5c8e36ea795034fcdac660b15d7c141d55b
Author: Susant Sahani <susant at redhat.com>
Date:   Sun Nov 23 09:46:36 2014 +0530

    networkd: Introduce Link Layer Discovery Protocol (LLDP)
    
    This patch introduces LLDP support to networkd. it implements the
    receiver side of the protocol.
    
    The Link Layer Discovery Protocol (LLDP) is an industry-standard,
    vendor-neutral method to allow networked devices to advertise
    capabilities, identity, and other information onto a LAN. The Layer 2
    protocol, detailed in IEEE 802.1AB-2005.LLDP allows network devices
    that operate at the lower layers of a protocol stack (such as
    Layer 2 bridges and switches) to learn some of the capabilities
    and characteristics of LAN devices available to higher
    layer protocols.

diff --git a/Makefile.am b/Makefile.am
index 2cc19a9..a7a2b6d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3079,7 +3079,18 @@ libsystemd_network_la_SOURCES = \
 	src/libsystemd-network/dhcp6-network.c \
 	src/libsystemd-network/dhcp6-option.c \
 	src/libsystemd-network/dhcp6-lease-internal.h \
-	src/libsystemd-network/sd-dhcp6-lease.c
+	src/libsystemd-network/sd-dhcp6-lease.c \
+	src/libsystemd-network/lldp.h \
+	src/libsystemd-network/lldp-tlv.h \
+	src/libsystemd-network/lldp-tlv.c \
+	src/libsystemd-network/lldp-network.h \
+	src/libsystemd-network/lldp-network.c \
+	src/libsystemd-network/lldp-port.h \
+	src/libsystemd-network/lldp-port.c \
+	src/libsystemd-network/lldp-internal.h \
+	src/libsystemd-network/lldp-internal.c \
+	src/libsystemd-network/sd-lldp.h \
+	src/libsystemd-network/sd-lldp.c
 
 libsystemd_network_la_LIBADD = \
 	libudev-internal.la \
@@ -3158,13 +3169,25 @@ test_dhcp6_client_LDADD = \
 	libsystemd-internal.la \
 	libsystemd-shared.la
 
+test_lldp_SOURCES = \
+       src/libsystemd-network/lldp.h \
+       src/libsystemd-network/lldp-tlv.h \
+       src/libsystemd-network/lldp-tlv.c \
+       src/libsystemd-network/test-lldp.c
+
+test_lldp_LDADD = \
+	libsystemd-network.la \
+	libsystemd-internal.la \
+	libsystemd-shared.la
+
 tests += \
 	test-dhcp-option \
 	test-dhcp-client \
 	test-dhcp-server \
 	test-ipv4ll \
 	test-icmp6-rs \
-	test-dhcp6-client
+	test-dhcp6-client \
+	test-lldp
 
 manual_tests += \
 	test-pppoe
diff --git a/src/libsystemd-network/lldp-internal.c b/src/libsystemd-network/lldp-internal.c
new file mode 100644
index 0000000..7085a02
--- /dev/null
+++ b/src/libsystemd-network/lldp-internal.c
@@ -0,0 +1,437 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2014 Tom Gundersen
+  Copyright (C) 2014 Susant Sahani
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "lldp-internal.h"
+
+/* We store maximum 1K chassis entries */
+#define LLDP_MIB_MAX_CHASSIS 1024
+
+/* Maximum Ports can be attached to any chassis */
+#define LLDP_MIB_MAX_PORT_PER_CHASSIS 32
+
+int lldp_read_chassis_id(tlv_packet *tlv,
+                         uint8_t *type,
+                         uint16_t *length,
+                         uint8_t **data) {
+        uint8_t subtype;
+        int r;
+
+        assert_return(tlv, -EINVAL);
+
+        r = lldp_tlv_packet_enter_container(tlv, LLDP_TYPE_CHASSIS_ID);
+        if (r < 0)
+                goto out2;
+
+        r = tlv_packet_read_u8(tlv, &subtype);
+        if (r < 0)
+                goto out1;
+
+        switch (subtype) {
+        case LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS:
+
+                r = tlv_packet_read_bytes(tlv, data, length);
+                if (r < 0)
+                        goto out1;
+
+                break;
+        default:
+                r = -ENOTSUP;
+                break;
+        }
+
+        *type = subtype;
+
+ out1:
+        (void)lldp_tlv_packet_exit_container(tlv);
+
+ out2:
+        return r;
+}
+
+int lldp_read_port_id(tlv_packet *tlv,
+                      uint8_t *type,
+                      uint16_t *length,
+                      uint8_t **data) {
+        uint8_t subtype;
+        char *s;
+        int r;
+
+        assert_return(tlv, -EINVAL);
+
+        r = lldp_tlv_packet_enter_container(tlv, LLDP_TYPE_PORT_ID);
+        if (r < 0)
+                goto out2;
+
+        r = tlv_packet_read_u8(tlv, &subtype);
+        if (r < 0)
+                goto out1;
+
+        switch (subtype) {
+        case LLDP_PORT_SUBTYPE_INTERFACE_NAME:
+
+                r = tlv_packet_read_string(tlv, &s, length);
+                if (r < 0)
+                        goto out1;
+
+                *data = (uint8_t *) s;
+
+                break;
+        case LLDP_PORT_SUBTYPE_MAC_ADDRESS:
+
+                r = tlv_packet_read_bytes(tlv, data, length);
+                if (r < 0)
+                        goto out1;
+
+                break;
+        default:
+                r = -ENOTSUP;
+                break;
+        }
+
+        *type = subtype;
+
+ out1:
+        (void)lldp_tlv_packet_exit_container(tlv);
+
+ out2:
+        return r;
+}
+
+int lldp_read_ttl(tlv_packet *tlv, uint16_t *ttl) {
+        int r;
+
+        assert_return(tlv, -EINVAL);
+
+        r = lldp_tlv_packet_enter_container(tlv, LLDP_TYPE_TTL);
+        if (r < 0)
+                goto out;
+
+        r = tlv_packet_read_u16(tlv, ttl);
+
+        (void) lldp_tlv_packet_exit_container(tlv);
+
+ out:
+        return r;
+}
+
+/* 10.5.5.2.2 mibUpdateObjects ()
+ * The mibUpdateObjects () procedure updates the MIB objects corresponding to
+ * the TLVs contained in the received LLDPDU for the LLDP remote system
+ * indicated by the LLDP remote systems update process defined in 10.3.5 */
+
+int lldp_mib_update_objects(lldp_chassis *c, tlv_packet *tlv) {
+        lldp_neighbour_port *p;
+        uint16_t length, ttl;
+        uint8_t *data;
+        uint8_t type;
+        int r;
+
+        assert_return(c, -EINVAL);
+        assert_return(tlv, -EINVAL);
+
+        r = lldp_read_port_id(tlv, &type, &length, &data);
+        if (r < 0)
+                return r;
+
+        /* Update the packet if we already have */
+        LIST_FOREACH(port, p, c->ports) {
+
+                if ((p->type == type && p->length == length && !memcmp(p->data, data, p->length))) {
+
+                        r = lldp_read_ttl(tlv, &ttl);
+                        if (r < 0)
+                                return r;
+
+                        p->until = ttl * USEC_PER_SEC + now(clock_boottime_or_monotonic());
+
+                        tlv_packet_free(p->packet);
+                        p->packet = tlv;
+
+                        prioq_reshuffle(p->c->by_expiry, p, &p->prioq_idx);
+
+                        return 0;
+                }
+        }
+
+        return -1;
+}
+
+int lldp_mib_remove_objects(lldp_chassis *c, tlv_packet *tlv) {
+        lldp_neighbour_port *p, *q;
+        uint8_t *data;
+        uint16_t length;
+        uint8_t type;
+        int r;
+
+        assert_return(c, -EINVAL);
+        assert_return(tlv, -EINVAL);
+
+        r = lldp_read_port_id(tlv, &type, &length, &data);
+        if (r < 0)
+                return r;
+
+        LIST_FOREACH_SAFE(port, p, q, c->ports) {
+
+                /* Find the port */
+                if (p->type == type && p->length == length && !memcmp(p->data, data, p->length)) {
+                        lldp_neighbour_port_remove_and_free(p);
+                        break;
+                }
+        }
+
+        return 0;
+}
+
+int lldp_mib_add_objects(Prioq *by_expiry,
+                         Hashmap *neighbour_mib,
+                         tlv_packet *tlv) {
+        _cleanup_lldp_neighbour_port_free_ lldp_neighbour_port *p = NULL;
+        _cleanup_lldp_chassis_free_ lldp_chassis *c = NULL;
+        lldp_chassis_id chassis_id;
+        bool new_chassis = false;
+        uint8_t subtype, *data;
+        uint16_t ttl, length;
+        int r;
+
+        assert_return(by_expiry, -EINVAL);
+        assert_return(neighbour_mib, -EINVAL);
+        assert_return(tlv, -EINVAL);
+
+        r = lldp_read_chassis_id(tlv, &subtype, &length, &data);
+        if (r < 0)
+                goto drop;
+
+        r = lldp_read_ttl(tlv, &ttl);
+        if (r < 0)
+                goto drop;
+
+        /* Make hash key */
+        chassis_id.type = subtype;
+        chassis_id.length = length;
+        chassis_id.data = data;
+
+        /* Try to find the Chassis */
+        c = hashmap_get(neighbour_mib, &chassis_id);
+        if (!c) {
+
+                /* Don't create chassis if ttl 0 is received . Silently drop it */
+                if (ttl == 0) {
+                        log_lldp("TTL value 0 received. Skiping Chassis creation.");
+                        goto drop;
+                }
+
+                /* Admission Control: Can we store this packet ? */
+                if (hashmap_size(neighbour_mib) >= LLDP_MIB_MAX_CHASSIS) {
+
+                        log_lldp("Exceeding number of chassie: %d. Dropping ...",
+                                 hashmap_size(neighbour_mib));
+                        goto drop;
+                }
+
+                r = lldp_chassis_new(tlv, by_expiry, neighbour_mib, &c);
+                if (r < 0)
+                        goto drop;
+
+                new_chassis = true;
+
+                r = hashmap_put(neighbour_mib, &c->chassis_id, c);
+                if (r < 0)
+                        goto drop;
+
+        } else {
+
+                /* When the TTL field is set to zero, the receiving LLDP agent is notified all
+                 * system information associated with the LLDP agent/port is to be deleted */
+                if (ttl == 0) {
+                        log_lldp("TTL value 0 received . Deleting associated Port ...");
+
+                        lldp_mib_remove_objects(c, tlv);
+
+                        c = NULL;
+                        goto drop;
+                }
+
+                /* if we already have this port just update it */
+                r = lldp_mib_update_objects(c, tlv);
+                if (r >= 0) {
+                        c = NULL;
+                        return r;
+                }
+
+                /* Admission Control: Can this port attached to the existing chassis ? */
+                if (REFCNT_GET(c->n_ref) >= LLDP_MIB_MAX_PORT_PER_CHASSIS) {
+                        log_lldp("Port limit reached. Chassis has: %d ports. Dropping ...",
+                                 REFCNT_GET(c->n_ref));
+
+                        c = NULL;
+                        goto drop;
+                }
+        }
+
+        /* This is a new port */
+        r = lldp_neighbour_port_new(c, tlv, &p);
+        if (r < 0)
+                goto drop;
+
+        r = prioq_put(c->by_expiry, p, &p->prioq_idx);
+        if (r < 0)
+                goto drop;
+
+        /* Attach new port to chassis */
+        LIST_PREPEND(port, c->ports, p);
+        REFCNT_INC(c->n_ref);
+
+        p = NULL;
+        c = NULL;
+
+        return 0;
+
+ drop:
+        tlv_packet_free(tlv);
+
+        if (new_chassis)
+                hashmap_remove(neighbour_mib, &c->chassis_id);
+
+        return r;
+}
+
+void lldp_neighbour_port_remove_and_free(lldp_neighbour_port *p) {
+        lldp_chassis *c;
+
+        assert(p);
+        assert(p->c);
+
+        c = p->c;
+
+        prioq_remove(c->by_expiry, p, &p->prioq_idx);
+
+        LIST_REMOVE(port, c->ports, p);
+        lldp_neighbour_port_free(p);
+
+        /* Drop the Chassis if no port is attached  */
+        if (REFCNT_DEC(c->n_ref) <= 1) {
+                hashmap_remove(c->neighbour_mib, &c->chassis_id);
+                lldp_chassis_free(c);
+        }
+}
+
+void lldp_neighbour_port_free(lldp_neighbour_port *p) {
+
+        if(!p)
+                return;
+
+        tlv_packet_free(p->packet);
+
+        free(p->data);
+        free(p);
+}
+
+int lldp_neighbour_port_new(lldp_chassis *c,
+                            tlv_packet *tlv,
+                            lldp_neighbour_port **ret) {
+        _cleanup_lldp_neighbour_port_free_ lldp_neighbour_port *p;
+        uint16_t length, ttl;
+        uint8_t *data;
+        uint8_t type;
+        int r;
+
+        assert(tlv);
+
+        r = lldp_read_port_id(tlv, &type, &length, &data);
+        if (r < 0)
+                return r;
+
+        r = lldp_read_ttl(tlv, &ttl);
+        if (r < 0)
+                return r;
+
+        p = new0(lldp_neighbour_port, 1);
+        if (!p)
+                return -ENOMEM;
+
+        p->c = c;
+        p->type = type;
+        p->length = length;
+        p->packet = tlv;
+        p->prioq_idx = PRIOQ_IDX_NULL;
+        p->until = ttl * USEC_PER_SEC + now(clock_boottime_or_monotonic());
+
+        p->data = memdup(data, length);
+        if (!p->data)
+                return -ENOMEM;
+
+        *ret = p;
+        p = NULL;
+
+        return 0;
+}
+
+void lldp_chassis_free(lldp_chassis *c) {
+
+        if (!c)
+                return;
+
+        if (REFCNT_GET(c->n_ref) > 1)
+                return;
+
+        free(c->chassis_id.data);
+        free(c);
+}
+
+int lldp_chassis_new(tlv_packet *tlv,
+                     Prioq *by_expiry,
+                     Hashmap *neighbour_mib,
+                     lldp_chassis **ret) {
+        _cleanup_lldp_chassis_free_ lldp_chassis *c;
+        uint16_t length;
+        uint8_t *data;
+        uint8_t type;
+        int r;
+
+        assert(tlv);
+
+        r = lldp_read_chassis_id(tlv, &type, &length, &data);
+        if (r < 0)
+                return r;
+
+        c = new0(lldp_chassis, 1);
+        if (!c)
+                return -ENOMEM;
+
+        c->n_ref = REFCNT_INIT;
+        c->chassis_id.type = type;
+        c->chassis_id.length = length;
+
+        c->chassis_id.data = memdup(data, length);
+        if (!c->chassis_id.data)
+                return -ENOMEM;
+
+        LIST_HEAD_INIT(c->ports);
+
+        c->by_expiry = by_expiry;
+        c->neighbour_mib = neighbour_mib;
+
+        *ret = c;
+        c = NULL;
+
+        return 0;
+}
diff --git a/src/libsystemd-network/lldp-internal.h b/src/libsystemd-network/lldp-internal.h
new file mode 100644
index 0000000..dbaf389
--- /dev/null
+++ b/src/libsystemd-network/lldp-internal.h
@@ -0,0 +1,93 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2014 Tom Gundersen
+  Copyright (C) 2014 Susant Sahani
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#pragma once
+
+#include "log.h"
+#include "list.h"
+#include "refcnt.h"
+#include "lldp-tlv.h"
+#include "prioq.h"
+
+typedef struct lldp_neighbour_port lldp_neighbour_port;
+typedef struct lldp_chassis lldp_chassis;
+typedef struct lldp_chassis_id lldp_chassis_id;
+
+struct lldp_neighbour_port {
+        uint8_t type;
+        uint8_t *data;
+
+        uint16_t length;
+        usec_t until;
+
+        unsigned prioq_idx;
+
+        lldp_chassis *c;
+        tlv_packet *packet;
+
+        LIST_FIELDS(lldp_neighbour_port, port);
+};
+
+int lldp_neighbour_port_new(lldp_chassis *c, tlv_packet *tlv, lldp_neighbour_port **ret);
+void lldp_neighbour_port_free(lldp_neighbour_port *p);
+void lldp_neighbour_port_remove_and_free(lldp_neighbour_port *p);
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(lldp_neighbour_port *, lldp_neighbour_port_free);
+#define _cleanup_lldp_neighbour_port_free_ _cleanup_(lldp_neighbour_port_freep)
+
+struct lldp_chassis_id {
+        uint8_t type;
+        uint16_t length;
+
+        uint8_t *data;
+};
+
+struct lldp_chassis {
+        RefCount n_ref;
+
+        lldp_chassis_id chassis_id;
+
+        Prioq *by_expiry;
+        Hashmap *neighbour_mib;
+
+        LIST_HEAD(lldp_neighbour_port, ports);
+};
+
+int lldp_chassis_new(tlv_packet *tlv,
+                     Prioq *by_expiry,
+                     Hashmap *neighbour_mib,
+                     lldp_chassis **ret);
+
+void lldp_chassis_free(lldp_chassis *c);
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(lldp_chassis *, lldp_chassis_free);
+#define _cleanup_lldp_chassis_free_ _cleanup_(lldp_chassis_freep)
+
+int lldp_mib_update_objects(lldp_chassis *c, tlv_packet *tlv);
+int lldp_mib_add_objects(Prioq *by_expiry, Hashmap *neighbour_mib, tlv_packet *tlv);
+int lldp_mib_remove_objects(lldp_chassis *c, tlv_packet *tlv);
+
+int lldp_read_chassis_id(tlv_packet *tlv, uint8_t *type, uint16_t *length, uint8_t **data);
+int lldp_read_port_id(tlv_packet *tlv, uint8_t *type, uint16_t *length, uint8_t **data);
+int lldp_read_ttl(tlv_packet *tlv, uint16_t *ttl);
+
+#define log_lldp(fmt, ...) log_internal(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, "LLDP: " fmt, ##__VA_ARGS__)
diff --git a/src/libsystemd-network/lldp-network.c b/src/libsystemd-network/lldp-network.c
new file mode 100644
index 0000000..cfab911
--- /dev/null
+++ b/src/libsystemd-network/lldp-network.c
@@ -0,0 +1,111 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2014 Tom Gundersen
+  Copyright (C) 2014 Susant Sahani
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <linux/filter.h>
+#include <linux/if_ether.h>
+
+#include "socket-util.h"
+#include "lldp-tlv.h"
+#include "lldp-network.h"
+#include "sd-lldp.h"
+
+int lldp_network_bind_raw_socket(int ifindex) {
+        typedef struct LLDPFrame {
+                struct ethhdr hdr;
+                uint8_t tlvs[0];
+        } LLDPFrame;
+
+        struct sock_filter filter[] = {
+                BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(LLDPFrame, hdr.h_dest)),      /* A <- 4 bytes of destination MAC */
+                BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0x0180c200, 1, 0),                    /* A != 01:80:c2:00 */
+                BPF_STMT(BPF_RET + BPF_K, 0),                                             /* drop packet */
+                BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(LLDPFrame, hdr.h_dest) + 4),  /* A <- remaining 2 bytes of destination MAC */
+                BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0x0000, 3, 0),                        /* A != 00:00 */
+                BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0x0003, 2, 0),                        /* A != 00:03 */
+                BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0x000e, 1, 0),                        /* A != 00:0e */
+                BPF_STMT(BPF_RET + BPF_K, 0),                                             /* drop packet */
+                BPF_STMT(BPF_LD + BPF_H + BPF_ABS, offsetof(LLDPFrame, hdr.h_proto)),     /* A <- protocol */
+                BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_LLDP, 1, 0),                /* A != ETHERTYPE_LLDP */
+                BPF_STMT(BPF_RET + BPF_K, 0),                                             /* drop packet */
+                BPF_STMT(BPF_RET + BPF_K, (uint32_t) -1),                                 /* accept packet */
+        };
+
+        struct sock_fprog fprog = {
+                .len = ELEMENTSOF(filter),
+                .filter = filter
+        };
+
+        _cleanup_close_ int s = -1;
+
+        union sockaddr_union saddrll = {
+                .ll.sll_family = AF_PACKET,
+                .ll.sll_ifindex = ifindex,
+        };
+
+        int r;
+
+        assert(ifindex > 0);
+
+        s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
+        if (s < 0)
+                return -errno;
+
+        r = setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
+        if (r < 0)
+                return -errno;
+
+        r = bind(s, &saddrll.sa, sizeof(saddrll.ll));
+        if (r < 0)
+                return -errno;
+
+        r = s;
+        s = -1;
+
+        return r;
+}
+
+int lldp_receive_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+        _cleanup_tlv_packet_free_ tlv_packet *packet = NULL;
+        tlv_packet *p;
+        uint16_t length;
+        int r;
+
+        assert(fd);
+        assert(userdata);
+
+        r = tlv_packet_new(&packet);
+        if (r < 0)
+                return r;
+
+        length = read(fd, &packet->pdu, sizeof(packet->pdu));
+
+        /* Silently drop the packet */
+        if ((size_t) length > ETHER_MAX_LEN)
+                return 0;
+
+        packet->userdata = userdata;
+
+        p = packet;
+        packet = NULL;
+
+        return lldp_handle_packet(p, (uint16_t) length);
+}
diff --git a/src/libsystemd-network/lldp-network.h b/src/libsystemd-network/lldp-network.h
new file mode 100644
index 0000000..b7f8d3b
--- /dev/null
+++ b/src/libsystemd-network/lldp-network.h
@@ -0,0 +1,28 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2014 Tom Gundersen
+  Copyright (C) 2014 Susant Sahani
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#pragma once
+
+#include "sd-event.h"
+
+int lldp_network_bind_raw_socket(int ifindex);
+int lldp_receive_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata);
diff --git a/src/libsystemd-network/lldp-port.c b/src/libsystemd-network/lldp-port.c
new file mode 100644
index 0000000..12b72ee
--- /dev/null
+++ b/src/libsystemd-network/lldp-port.c
@@ -0,0 +1,116 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2014 Tom Gundersen
+  Copyright (C) 2014 Susant Sahani
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "async.h"
+#include "lldp-port.h"
+#include "lldp-network.h"
+
+int lldp_port_start(lldp_port *p) {
+        int r;
+
+        assert_return(p, -EINVAL);
+
+        r = lldp_network_bind_raw_socket(p->ifindex);
+        if (r < 0)
+                return r;
+
+        p->rawfd = r;
+
+        r = sd_event_add_io(p->event, &p->lldp_port_rx,
+                            p->rawfd, EPOLLIN, lldp_receive_packet, p);
+        if (r < 0) {
+                log_debug("Failed to allocate event source: %s", strerror(-r));
+                return r;
+        }
+
+        r = sd_event_source_set_priority(p->lldp_port_rx, p->event_priority);
+        if (r < 0) {
+                log_debug("Failed to set event priority: %s", strerror(-r));
+                goto fail;
+        }
+
+        r = sd_event_source_set_description(p->lldp_port_rx, "lldp-port-rx");
+        if (r < 0) {
+                log_debug("Failed to set event name: %s", strerror(-r));
+                goto fail;
+        }
+
+        return 0;
+
+fail:
+        lldp_port_stop(p);
+
+        return r;
+}
+
+int lldp_port_stop(lldp_port *p) {
+
+        assert_return(p, -EINVAL);
+
+        p->rawfd = asynchronous_close(p->rawfd);
+        p->lldp_port_rx = sd_event_source_unref(p->lldp_port_rx);
+
+        return 0;
+}
+
+void lldp_port_free(lldp_port *p) {
+        if (!p)
+                return;
+
+        lldp_port_stop(p);
+
+        free(p->ifname);
+        free(p);
+}
+
+int lldp_port_new(int ifindex,
+                  char *ifname,
+                  const struct ether_addr *addr,
+                  void *userdata,
+                  lldp_port **ret) {
+        _cleanup_free_ lldp_port *p = NULL;
+
+        assert_return(ifindex, -EINVAL);
+        assert_return(ifname, -EINVAL);
+        assert_return(addr, -EINVAL);
+
+        p = new0(lldp_port, 1);
+        if (!p)
+                return -ENOMEM;
+
+        p->rawfd = -1;
+        p->ifindex = ifindex;
+
+        p->ifname = strdup(ifname);
+        if (!p->ifname)
+                return -ENOMEM;
+
+        memcpy(&p->mac, addr, ETH_ALEN);
+
+        p->userdata = userdata;
+
+        *ret = p;
+
+        p = NULL;
+
+        return 0;
+}
diff --git a/src/libsystemd-network/lldp-port.h b/src/libsystemd-network/lldp-port.h
new file mode 100644
index 0000000..1b1ae04
--- /dev/null
+++ b/src/libsystemd-network/lldp-port.h
@@ -0,0 +1,61 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2014 Tom Gundersen
+  Copyright (C) 2014 Susant Sahani
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#pragma once
+
+#include <net/ethernet.h>
+
+#include "sd-event.h"
+#include "sd-lldp.h"
+
+typedef struct lldp_port lldp_port;
+
+struct lldp_port {
+        LLDPPortStatus status;
+
+        int ifindex;
+        char *ifname;
+
+        struct ether_addr mac;
+
+        int rawfd;
+
+        sd_event *event;
+        sd_event_source *lldp_port_rx;
+
+        int event_priority;
+
+        void *userdata;
+};
+
+int lldp_port_new(int ifindex,
+                  char *ifname,
+                  const struct ether_addr *addr,
+                  void *userdata,
+                  lldp_port **ret);
+void lldp_port_free(lldp_port *p);
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(lldp_port*, lldp_port_free);
+#define _cleanup_lldp_port_free_ _cleanup_(lldp_port_freep)
+
+int lldp_port_start(lldp_port *p);
+int lldp_port_stop(lldp_port *p);
diff --git a/src/libsystemd-network/lldp-tlv.c b/src/libsystemd-network/lldp-tlv.c
new file mode 100644
index 0000000..e43d70d
--- /dev/null
+++ b/src/libsystemd-network/lldp-tlv.c
@@ -0,0 +1,321 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2014 Tom Gundersen
+  Copyright (C) 2014 Susant Sahani
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <net/ethernet.h>
+#include <arpa/inet.h>
+
+#include "macro.h"
+#include "lldp-tlv.h"
+
+int tlv_section_new(tlv_section **ret) {
+        tlv_section *s;
+
+        s = new0(tlv_section, 1);
+        if (!s)
+                return -ENOMEM;
+
+        *ret = s;
+
+        return 0;
+}
+
+void tlv_section_free(tlv_section *m) {
+
+        if (!m)
+                return;
+
+        free(m);
+}
+
+int tlv_packet_new(tlv_packet **ret) {
+        tlv_packet *m;
+
+        m = new0(tlv_packet, 1);
+        if (!m)
+                return -ENOMEM;
+
+        LIST_HEAD_INIT(m->sections);
+
+        *ret = m;
+
+        return 0;
+}
+
+void tlv_packet_free(tlv_packet *m) {
+        tlv_section *s, *n;
+
+        if (!m)
+                return;
+
+        LIST_FOREACH_SAFE(section, s, n, m->sections)
+                tlv_section_free(s);
+
+        free(m);
+}
+
+int tlv_packet_append_bytes(tlv_packet *m, const void *data, size_t data_length) {
+        uint8_t *p;
+
+        assert_return(m, -EINVAL);
+        assert_return(data, -EINVAL);
+        assert_return(data_length, -EINVAL);
+
+        if (m->length + data_length > ETHER_MAX_LEN)
+                return -ENOMEM;
+
+        p = m->pdu + m->length;
+        memcpy(p, data, data_length);
+        m->length += data_length;
+
+        return 0;
+}
+
+int tlv_packet_append_u8(tlv_packet *m, uint8_t data) {
+
+        assert_return(m, -EINVAL);
+
+        return tlv_packet_append_bytes(m, &data, sizeof(uint8_t));
+}
+
+int tlv_packet_append_u16(tlv_packet *m, uint16_t data) {
+        uint16_t type;
+
+        assert_return(m, -EINVAL);
+
+        type = htons(data);
+
+        return tlv_packet_append_bytes(m, &type, sizeof(uint16_t));
+}
+
+int tlv_packet_append_u32(tlv_packet *m, uint32_t data) {
+        uint32_t type;
+
+        assert_return(m, -EINVAL);
+
+        type = htonl(data);
+
+        return tlv_packet_append_bytes(m, &type, sizeof(uint32_t));
+}
+
+int tlv_packet_append_string(tlv_packet *m, char *data, uint16_t size) {
+
+        assert_return(m, -EINVAL);
+
+        return tlv_packet_append_bytes(m, data, size);
+}
+
+int lldp_tlv_packet_open_container(tlv_packet *m, uint16_t type) {
+
+        assert_return(m, -EINVAL);
+
+        m->container_pos = m->pdu + m->length;
+
+        return tlv_packet_append_u16(m, type << 9);
+}
+
+int lldp_tlv_packet_close_container(tlv_packet *m) {
+        uint16_t type;
+
+        assert_return(m, -EINVAL);
+        assert_return(m->container_pos, -EINVAL);
+
+        memcpy(&type, m->container_pos, sizeof(uint16_t));
+
+        type |= htons(((m->pdu + m->length) - (m->container_pos + 2)) & 0x01ff);
+        memcpy(m->container_pos, &type, sizeof(uint16_t));
+
+        return 0;
+}
+
+static inline int tlv_packet_read_internal(tlv_section *m, void **data) {
+
+        assert_return(m->read_pos, -EINVAL);
+
+        *data = m->read_pos;
+
+        return 0;
+}
+
+int tlv_packet_read_u8(tlv_packet *m, uint8_t *data) {
+        void *val;
+        int r;
+
+        assert_return(m, -EINVAL);
+
+        r = tlv_packet_read_internal(m->container,  &val);
+        if (r < 0)
+                return r;
+
+        memcpy(data, val, sizeof(uint8_t));
+
+        m->container->read_pos ++;
+
+        return 0;
+}
+
+int tlv_packet_read_u16(tlv_packet *m, uint16_t *data) {
+        uint16_t t;
+        void *val;
+        int r;
+
+        assert_return(m, -EINVAL);
+
+        r = tlv_packet_read_internal(m->container, &val);
+        if (r < 0)
+                return r;
+
+        memcpy(&t, val, sizeof(uint16_t));
+        *data = ntohs(t);
+
+        m->container->read_pos += 2;
+
+        return 0;
+}
+
+int tlv_packet_read_u32(tlv_packet *m, uint32_t *data) {
+        uint32_t t;
+        void *val;
+        int r;
+
+        assert_return(m, -EINVAL);
+
+        r = tlv_packet_read_internal(m->container, &val);
+        if (r < 0)
+                return r;
+
+        memcpy(&t, val, sizeof(uint32_t));
+        *data = ntohl(t);
+
+        m->container->read_pos += 4;
+
+        return r;
+}
+
+int tlv_packet_read_string(tlv_packet *m, char **data, uint16_t *data_length) {
+        void *val;
+        int r;
+
+        assert_return(m, -EINVAL);
+
+        r = tlv_packet_read_internal(m->container, &val);
+        if (r < 0)
+                return r;
+
+        *data = (char *) val;
+        *data_length =  m->container->length;
+
+        m->container->read_pos += m->container->length;
+
+        return 0;
+}
+
+int tlv_packet_read_bytes(tlv_packet *m, uint8_t **data, uint16_t *data_length) {
+        void *val;
+        int r;
+
+        assert_return(m, -EINVAL);
+
+        r = tlv_packet_read_internal(m->container, &val);
+        if (r < 0)
+                return r;
+
+        *data = (uint8_t *) val;
+        *data_length = m->container->length;
+
+        m->container->read_pos += m->container->length;
+
+        return 0;
+}
+
+/* parse raw TLV packet */
+int tlv_packet_parse_pdu(tlv_packet *m, uint16_t size) {
+        tlv_section *section, *tail;
+        uint16_t t, l;
+        uint8_t *p;
+        int r;
+
+        assert_return(m, -EINVAL);
+        assert_return(size, -EINVAL);
+
+        p = m->pdu;
+
+        /* extract ethernet herader */
+        memcpy(&m->mac, p, ETH_ALEN);
+        p += sizeof(struct ether_header);
+
+        for (l = 0; l <= size; ) {
+                r = tlv_section_new(&section);
+                if (r < 0)
+                        return r;
+
+                memcpy(&t, p, sizeof(uint16_t));
+
+                section->type = ntohs(t) >> 9;
+                section->length = ntohs(t) & 0x01ff;
+
+                if (section->type == LLDP_TYPE_END || section->type >=_LLDP_TYPE_MAX) {
+                        tlv_section_free(section);
+                        break;
+                }
+
+                p += 2;
+                section->data = p;
+
+                LIST_FIND_TAIL(section, m->sections, tail);
+                LIST_INSERT_AFTER(section, m->sections, tail, section);
+
+                p += section->length;
+                l += (section->length + 2);
+        }
+
+        return 0;
+}
+
+int lldp_tlv_packet_enter_container(tlv_packet *m, uint16_t type) {
+        tlv_section *s;
+
+        assert_return(m, -EINVAL);
+
+        LIST_FOREACH(section, s, m->sections)
+                if (s->type == type)
+                        break;
+        if (!s)
+                return -1;
+
+        m->container = s;
+
+        m->container->read_pos = s->data;
+        if (!m->container->read_pos) {
+                m->container = 0;
+                return -1;
+        }
+
+        return 0;
+}
+
+int lldp_tlv_packet_exit_container(tlv_packet *m) {
+        assert_return(m, -EINVAL);
+
+        m->container = 0;
+
+        return 0;
+}
diff --git a/src/libsystemd-network/lldp-tlv.h b/src/libsystemd-network/lldp-tlv.h
new file mode 100644
index 0000000..ce3334e
--- /dev/null
+++ b/src/libsystemd-network/lldp-tlv.h
@@ -0,0 +1,87 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2014 Tom Gundersen
+  Copyright (C) 2014 Susant Sahani
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#pragma once
+
+#include <net/ethernet.h>
+
+#include "util.h"
+#include "lldp.h"
+#include "list.h"
+
+typedef struct tlv_packet tlv_packet;
+typedef struct tlv_section tlv_section;
+
+struct tlv_section {
+        uint16_t type;
+        uint16_t length;
+
+        uint8_t *read_pos;
+        uint8_t *data;
+
+        LIST_FIELDS(tlv_section, section);
+};
+
+int tlv_section_new(tlv_section **ret);
+void tlv_section_free(tlv_section *ret);
+
+struct tlv_packet {
+        uint16_t type;
+        uint16_t length;
+        usec_t ts;
+
+        uint8_t *container_pos;
+        uint8_t pdu[ETHER_MAX_LEN];
+
+        void *userdata;
+
+        struct ether_addr mac;
+        tlv_section *container;
+
+        LIST_HEAD(tlv_section, sections);
+};
+
+int tlv_packet_new(tlv_packet **ret);
+void tlv_packet_free(tlv_packet *m);
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(tlv_packet*, tlv_packet_free);
+#define _cleanup_tlv_packet_free_ _cleanup_(tlv_packet_freep)
+
+int lldp_tlv_packet_open_container(tlv_packet *m, uint16_t type);
+int lldp_tlv_packet_close_container(tlv_packet *m);
+
+int tlv_packet_append_bytes(tlv_packet *m, const void *data, size_t data_length);
+int tlv_packet_append_u8(tlv_packet *m, uint8_t data);
+int tlv_packet_append_u16(tlv_packet *m, uint16_t data);
+int tlv_packet_append_u32(tlv_packet *m, uint32_t data);
+int tlv_packet_append_string(tlv_packet *m, char *data, uint16_t size);
+
+int lldp_tlv_packet_enter_container(tlv_packet *m, uint16_t type);
+int lldp_tlv_packet_exit_container(tlv_packet *m);
+
+int tlv_packet_read_bytes(tlv_packet *m, uint8_t **data, uint16_t *data_length);
+int tlv_packet_read_string(tlv_packet *m, char **data, uint16_t *data_length);
+int tlv_packet_read_u8(tlv_packet *m, uint8_t *data);
+int tlv_packet_read_u16(tlv_packet *m, uint16_t *data);
+int tlv_packet_read_u32(tlv_packet *m, uint32_t *data);
+
+int tlv_packet_parse_pdu(tlv_packet *t, uint16_t size);
diff --git a/src/libsystemd-network/lldp.h b/src/libsystemd-network/lldp.h
new file mode 100644
index 0000000..5e4b283
--- /dev/null
+++ b/src/libsystemd-network/lldp.h
@@ -0,0 +1,115 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2014 Tom Gundersen
+  Copyright (C) 2014 Susant Sahani
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#pragma once
+
+#define LLDP_MULTICAST_ADDR     { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e }
+
+#define ETHERTYPE_LLDP          0x88cc
+
+/* IEEE 802.3AB Clause 9: TLV Types */
+typedef enum LLDPTypes {
+        LLDP_TYPE_END                  =   0,
+        LLDP_TYPE_CHASSIS_ID           =   1,
+        LLDP_TYPE_PORT_ID              =   2,
+        LLDP_TYPE_TTL                  =   3,
+        LLDP_TYPE_PORT_DESCRIPTION     =   4,
+        LLDP_TYPE_SYSTEM_NAME          =   5,
+        LLDP_TYPE_SYSTEM_DESCRIPTION   =   6,
+        LLDP_TYPE_SYSTEM_CAPABILITIES  =   7,
+        LLDP_TYPE_MGMT_ADDRESS         =   8,
+        LLDP_TYPE_PRIVATE              =   127,
+        _LLDP_TYPE_MAX,
+        _LLDP_TYPE_INVALID             = -1,
+} LLDPTypes;
+
+/* IEEE 802.3AB Clause 9.5.2: Chassis subtypes */
+typedef enum LLDPChassisSubtypes {
+        LLDP_CHASSIS_SUBTYPE_RESERVED            = 0,
+        LLDP_CHASSIS_SUBTYPE_CHASSIS_COMPONENT   = 1,
+        LLDP_CHASSIS_SUBTYPE_INTERFACE_ALIAS     = 2,
+        LLDP_CHASSIS_SUBTYPE_PORT_COMPONENT      = 3,
+        LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS         = 4,
+        LLDP_CHASSIS_SUBTYPE_NETWORK_ADDRESS     = 5,
+        LLDP_CHASSIS_SUBTYPE_INTERFACE_NAME      = 6,
+        LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED    = 7,
+        _LLDP_CHASSIS_SUBTYPE_MAX,
+        _LLDP_CHASSIS_SUBTYPE_INVALID            = -1,
+} LLDPChassisSubtypes;
+
+/* IEEE 802.3AB Clause 9.5.3: Port subtype */
+typedef enum LLDPPortSubtypes  {
+        LLDP_PORT_SUBTYPE_RESERVED           = 0,
+        LLDP_PORT_SUBTYPE_INTERFACE_ALIAS    = 1,
+        LLDP_PORT_SUBTYPE_PORT_COMPONENT     = 2,
+        LLDP_PORT_SUBTYPE_MAC_ADDRESS        = 3,
+        LLDP_PORT_SUBTYPE_NETWORK            = 4,
+        LLDP_PORT_SUBTYPE_INTERFACE_NAME     = 5,
+        LLDP_PORT_SUBTYPE_AGENT_CIRCUIT_ID   = 6,
+        LLDP_PORT_SUBTYPE_LOCALLY_ASSIGNED   = 7,
+        _LLDP_PORT_SUBTYPE_MAX,
+        _LLDP_PORT_SUBTYPE_INVALID           = -1
+} LLDPPortSubtypes;
+
+typedef enum LLDPSystemCapabilities {
+        LLDP_SYSTEM_CAPABILITIES_OTHER        = 1 << 0,
+        LLDP_SYSTEM_CAPABILITIES_REPEATER     = 1 << 1,
+        LLDP_SYSTEM_CAPABILITIES_BRIDGE       = 1 << 2,
+        LLDP_SYSTEM_CAPABILITIES_WLAN_AP      = 1 << 3,
+        LLDP_SYSTEM_CAPABILITIES_ROUTER       = 1 << 4,
+        LLDP_SYSTEM_CAPABILITIES_PHONE        = 1 << 5,
+        LLDP_SYSTEM_CAPABILITIES_DOCSIS       = 1 << 6,
+        LLDP_SYSTEM_CAPABILITIES_STATION      = 1 << 7,
+        LLDP_SYSTEM_CAPABILITIES_CVLAN        = 1 << 8,
+        LLDP_SYSTEM_CAPABILITIES_SVLAN        = 1 << 9,
+        LLDP_SYSTEM_CAPABILITIES_TPMR         = 1 << 10,
+        _LLDP_SYSTEM_CAPABILITIES_MAX,
+        _LLDP_SYSTEM_CAPABILITIES_INVALID     = -1,
+} LLDPSystemCapabilities;
+
+typedef enum LLDPMedSubtype {
+        LLDP_MED_SUBTYPE_RESERVED          = 0,
+        LLDP_MED_SUBTYPE_CAPABILITIES      = 1,
+        LLDP_MED_SUBTYPE_NETWORK_POLICY    = 2,
+        LLDP_MED_SUBTYPE_LOCATION_ID       = 3,
+        LLDP_MED_SUBTYPE_EXTENDED_PVMDI    = 4,
+        LLDP_MED_SUBTYPE_INV_HWREV         = 5,
+        LLDP_MED_SUBTYPE_INV_FWREV         = 6,
+        LLDP_MED_SUBTYPE_INV_SWREV         = 7,
+        LLDP_MED_SUBTYPE_INV_SERIAL        = 8,
+        LLDP_MED_SUBTYPE_INV_MANUFACTURER  = 9,
+        LLDP_MED_SUBTYPE_INV_MODELNAME     = 10,
+        LLDP_MED_SUBTYPE_INV_ASSETID       = 11,
+        _LLDP_MED_SUBTYPE_MAX,
+        _LLDP_MED_SUBTYPE_INVALID          = -1,
+} LLDPMedSubtype;
+
+typedef enum LLDPMedCapability {
+        LLDP_MED_CAPABILITY_CAPAPILITIES   = 1 << 0,
+        LLDP_MED_CAPABILITY_NETWORK_POLICY = 1 << 1,
+        LLDP_MED_CAPABILITY_LOCATION_ID    = 1 << 2,
+        LLDP_MED_CAPABILITY_EXTENDED_PSE   = 1 << 3,
+        LLDP_MED_CAPABILITY_EXTENDED_PD    = 1 << 4,
+        LLDP_MED_CAPABILITY_INVENTORY      = 1 << 5,
+        LLDP_MED_CAPABILITY_MAX,
+        LLDP_MED_CAPABILITY_INVALID        = -1,
+} LLDPMedCapability;
diff --git a/src/libsystemd-network/sd-lldp.c b/src/libsystemd-network/sd-lldp.c
new file mode 100644
index 0000000..97d1578
--- /dev/null
+++ b/src/libsystemd-network/sd-lldp.c
@@ -0,0 +1,493 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+    This file is part of systemd.
+
+    Copyright (C) 2014 Tom Gundersen
+    Copyright (C) 2014 Susant Sahani
+
+    systemd is free software; you can redistribute it and/or modify it
+    under the terms of the GNU Lesser General Public License as published by
+    the Free Software Foundation; either version 2.1 of the License, or
+    (at your option) any later version.
+
+    systemd is distributed in the hope that it will be useful, but
+    WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public License
+    along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <arpa/inet.h>
+
+#include "siphash24.h"
+#include "hashmap.h"
+#include "event-util.h"
+
+#include "lldp-tlv.h"
+#include "lldp-port.h"
+#include "sd-lldp.h"
+#include "prioq.h"
+#include "lldp-internal.h"
+
+/* Section 10.5.2.2 Reception counters */
+struct lldp_agent_statitics {
+        uint64_t stats_ageouts_total;
+        uint64_t stats_frames_discarded_total;
+        uint64_t stats_frames_in_errors_total;
+        uint64_t stats_frames_in_total;
+        uint64_t stats_tlvs_discarded_total;
+        uint64_t stats_tlvs_unrecognized_total;
+};
+
+struct sd_lldp {
+        lldp_port *port;
+
+        Prioq *by_expiry;
+        Hashmap *neighbour_mib;
+
+        lldp_agent_statitics statitics;
+};
+
+static unsigned long chassis_id_hash_func(const void *p,
+                                          const uint8_t hash_key[HASH_KEY_SIZE]) {
+        uint64_t u;
+        const lldp_chassis_id *id = p;
+
+        assert(id);
+
+        siphash24((uint8_t *) &u, id->data, id->length, hash_key);
+
+        return (unsigned long) u;
+}
+
+static int chassis_id_compare_func(const void *_a, const void *_b) {
+        const lldp_chassis_id *a, *b;
+
+        a = _a;
+        b = _b;
+
+        assert(!a->length || a->data);
+        assert(!b->length || b->data);
+
+        if (a->type != b->type)
+                return -1;
+
+        if (a->length != b->length)
+                return a->length < b->length ? -1 : 1;
+
+        return memcmp(a->data, b->data, a->length);
+}
+
+static const struct hash_ops chassis_id_hash_ops = {
+        .hash = chassis_id_hash_func,
+        .compare = chassis_id_compare_func
+};
+
+static void lldp_mib_delete_objects(sd_lldp *lldp);
+
+static int lldp_receive_frame(sd_lldp *lldp, tlv_packet *tlv) {
+        int r;
+
+        assert(lldp);
+        assert(tlv);
+
+        /* Remove expired packets */
+        if (prioq_size(lldp->by_expiry) > 0)
+                lldp_mib_delete_objects(lldp);
+
+        r = lldp_mib_add_objects(lldp->by_expiry, lldp->neighbour_mib, tlv);
+        if (r < 0)
+                goto out;
+
+        log_lldp("Packet added. MIB size: %d , PQ size: %d",
+                 hashmap_size(lldp->neighbour_mib),
+                 prioq_size(lldp->by_expiry));
+
+        lldp->statitics.stats_frames_in_total ++;
+
+        return 0;
+
+ out:
+        if (r < 0)
+                log_lldp("Receive frame failed: %s", strerror(-r));
+
+        return 0;
+}
+
+/* 10.3.2 LLDPDU validation: rxProcessFrame() */
+int lldp_handle_packet(tlv_packet *tlv, uint16_t length) {
+        uint16_t type, len, i, l, t;
+        bool chassis_id = false;
+        bool malformed = false;
+        bool port_id = false;
+        bool ttl = false;
+        bool end = false;
+        lldp_port *port;
+        uint8_t *p, *q;
+        sd_lldp *lldp;
+        int r;
+
+        assert(tlv);
+        assert(length > 0);
+
+        port = (lldp_port *) tlv->userdata;
+        lldp = (sd_lldp *) port->userdata;
+
+        if (lldp->port->status == LLDP_PORT_STATUS_DISABLED) {
+                log_lldp("Port is disabled : %s . Dropping ...",
+                         lldp->port->ifname);
+                goto out;
+        }
+
+        p = tlv->pdu;
+        p += sizeof(struct ether_header);
+
+        for (i = 1, l = 0; l <= length; i++) {
+
+                memcpy(&t, p, sizeof(uint16_t));
+
+                type = ntohs(t) >> 9;
+                len = ntohs(t) & 0x01ff;
+
+                if (type == LLDP_TYPE_END) {
+                        if (len != 0) {
+                                log_lldp("TLV type end is not length 0. Length:%d received . Dropping ...",
+                                         len);
+
+                                malformed = true;
+                                goto out;
+                        }
+
+                        end = true;
+
+                        break;
+                } else if (type >=_LLDP_TYPE_MAX) {
+                        log_lldp("TLV type not recognized %d . Dropping ...",
+                                 type);
+
+                        malformed = true;
+                        goto out;
+                }
+
+                /* skip type and lengh encoding */
+                p += 2;
+                q = p;
+
+                p += len;
+                l += (len + 2);
+
+                if (i <= 3) {
+                        if (i != type) {
+                                log_lldp("TLV missing or out of order. Dropping ...");
+
+                                malformed = true;
+                                goto out;
+                        }
+                }
+
+                switch(type) {
+                case LLDP_TYPE_CHASSIS_ID:
+
+                        if (len < 2) {
+                                log_lldp("Received malformed Chassis ID TLV len = %d. Dropping",
+                                         len);
+
+                                malformed = true;
+                                goto out;
+                        }
+
+                        if (chassis_id) {
+                                log_lldp("Duplicate Chassis ID TLV found. Dropping ...");
+
+                                malformed = true;
+                                goto out;
+                        }
+
+                        /* Look what subtype it has */
+                        if (*q == LLDP_CHASSIS_SUBTYPE_RESERVED ||
+                            *q > LLDP_CHASSIS_SUBTYPE_LOCALLY_ASSIGNED) {
+                                log_lldp("Unknown subtype: %d found in Chassis ID TLV . Dropping ...",
+                                         *q);
+
+                                malformed = true;
+                                goto out;
+
+                        }
+
+                        chassis_id = true;
+
+                        break;
+                case LLDP_TYPE_PORT_ID:
+
+                        if (len < 2) {
+                                log_lldp("Received malformed Port ID TLV len = %d. Dropping",
+                                         len);
+
+                                malformed = true;
+                                goto out;
+                        }
+
+                        if (port_id) {
+                                log_lldp("Duplicate Port ID TLV found. Dropping ...");
+
+                                malformed = true;
+                                goto out;
+                        }
+
+                        /* Look what subtype it has */
+                        if (*q == LLDP_PORT_SUBTYPE_RESERVED ||
+                            *q > LLDP_PORT_SUBTYPE_LOCALLY_ASSIGNED) {
+                                log_lldp("Unknown subtype: %d found in Port ID TLV . Dropping ...",
+                                         *q);
+
+                                malformed = true;
+                                goto out;
+
+                        }
+
+                        port_id = true;
+
+                        break;
+                case LLDP_TYPE_TTL:
+
+                        if(len != 2) {
+                                log_lldp(
+                                         "Received invalid lenth: %d TTL TLV. Dropping ...",
+                                         len);
+
+                                malformed = true;
+                                goto out;
+                        }
+
+                        if (ttl) {
+                                log_lldp("Duplicate TTL TLV found. Dropping ...");
+
+                                malformed = true;
+                                goto out;
+                        }
+
+                        ttl = true;
+
+                        break;
+                default:
+
+                        if (len == 0) {
+                                log_lldp("TLV type = %d's, length 0 received . Dropping ...",
+                                         type);
+
+                                malformed = true;
+                                goto out;
+                        }
+                        break;
+                }
+        }
+
+        if(!chassis_id || !port_id || !ttl || !end) {
+                log_lldp( "One or more mandotory TLV missing . Dropping ...");
+
+                malformed = true;
+                goto out;
+
+        }
+
+        r = tlv_packet_parse_pdu(tlv, length);
+        if (r < 0) {
+                log_lldp( "Failed to parse the TLV. Dropping ...");
+
+                malformed = true;
+                goto out;
+        }
+
+        return lldp_receive_frame(lldp, tlv);
+
+ out:
+        if (malformed) {
+                lldp->statitics.stats_frames_discarded_total ++;
+                lldp->statitics.stats_frames_in_errors_total ++;
+        }
+
+        tlv_packet_free(tlv);
+
+        return 0;
+}
+
+static int ttl_expiry_item_prioq_compare_func(const void *a, const void *b) {
+        const lldp_neighbour_port *p = a, *q = b;
+
+        if (p->until < q->until)
+                return -1;
+
+        if (p->until > q->until)
+                return 1;
+
+        return 0;
+}
+
+/* 10.5.5.2.1 mibDeleteObjects ()
+ * The mibDeleteObjects () procedure deletes all information in the LLDP remote
+ * systems MIB associated with the MSAP identifier if an LLDPDU is received with
+ * an rxTTL value of zero (see 10.3.2) or the timing counter rxInfoTTL expires. */
+
+static void lldp_mib_delete_objects(sd_lldp *lldp) {
+        lldp_neighbour_port *p;
+        usec_t t = 0;
+
+        /* Remove all entries that are past their TTL */
+        for (;;) {
+
+                if (prioq_size(lldp->by_expiry) <= 0)
+                        break;
+
+                p = prioq_peek(lldp->by_expiry);
+                if (!p)
+                        break;
+
+                if (t <= 0)
+                        t = now(CLOCK_BOOTTIME);
+
+                if (p->until > t)
+                        break;
+
+                lldp_neighbour_port_remove_and_free(p);
+
+                lldp->statitics.stats_ageouts_total ++;
+        }
+}
+
+static void lldp_mib_objects_flush(sd_lldp *lldp) {
+        lldp_neighbour_port *p, *q;
+        lldp_chassis *c;
+
+        assert(lldp);
+        assert(lldp->neighbour_mib);
+        assert(lldp->by_expiry);
+
+        /* Drop all packets */
+        while ((c = hashmap_steal_first(lldp->neighbour_mib))) {
+
+                LIST_FOREACH_SAFE(port, p, q, c->ports) {
+                        lldp_neighbour_port_remove_and_free(p);
+                }
+        }
+
+        assert(hashmap_size(lldp->neighbour_mib) == 0);
+        assert(prioq_size(lldp->by_expiry) == 0);
+}
+
+int sd_lldp_start(sd_lldp *lldp) {
+        int r;
+
+        assert_return(lldp, -EINVAL);
+        assert_return(lldp->port, -EINVAL);
+
+        lldp->port->status = LLDP_PORT_STATUS_ENABLED;
+
+        r = lldp_port_start(lldp->port);
+        if (r < 0) {
+                log_lldp("Failed to start Port : %s , %s",
+                         lldp->port->ifname,
+                         strerror(-r));
+                return r;
+        }
+
+        return 0;
+}
+
+int sd_lldp_stop(sd_lldp *lldp) {
+        int r;
+
+        assert_return(lldp, -EINVAL);
+        assert_return(lldp->port, -EINVAL);
+
+        lldp->port->status = LLDP_PORT_STATUS_DISABLED;
+
+        r = lldp_port_stop(lldp->port);
+        if (r < 0)
+                return r;
+
+        lldp_mib_objects_flush(lldp);
+
+        return 0;
+}
+
+int sd_lldp_attach_event(sd_lldp *lldp, sd_event *event, int priority) {
+        int r;
+
+        assert_return(lldp, -EINVAL);
+        assert_return(!lldp->port->event, -EBUSY);
+
+        if (event)
+                lldp->port->event = sd_event_ref(event);
+        else {
+                r = sd_event_default(&lldp->port->event);
+                if (r < 0)
+                        return r;
+        }
+
+        lldp->port->event_priority = priority;
+
+        return 0;
+}
+
+int sd_lldp_detach_event(sd_lldp *lldp) {
+
+        assert_return(lldp, -EINVAL);
+
+        lldp->port->event = sd_event_unref(lldp->port->event);
+
+        return 0;
+}
+
+void sd_lldp_free(sd_lldp *lldp) {
+
+        if (!lldp)
+                return;
+
+        /* Drop all packets */
+        lldp_mib_objects_flush(lldp);
+
+        lldp_port_free(lldp->port);
+
+        hashmap_free(lldp->neighbour_mib);
+        prioq_free(lldp->by_expiry);
+
+        free(lldp);
+}
+
+int sd_lldp_new(int ifindex,
+                char *ifname,
+                struct ether_addr *mac,
+                sd_lldp **ret) {
+        _cleanup_sd_lldp_free_ sd_lldp *lldp = NULL;
+        int r;
+
+        assert_return(ret, -EINVAL);
+        assert_return(ifindex > 0, -EINVAL);
+        assert_return(ifname, -EINVAL);
+        assert_return(mac, -EINVAL);
+
+        lldp = new0(sd_lldp, 1);
+        if (!lldp)
+                return -ENOMEM;
+
+        r = lldp_port_new(ifindex, ifname, mac, lldp, &lldp->port);
+        if (r < 0)
+                return r;
+
+        lldp->neighbour_mib = hashmap_new(&chassis_id_hash_ops);
+        if (!lldp->neighbour_mib)
+                return -ENOMEM;
+
+        r = prioq_ensure_allocated(&lldp->by_expiry,
+                                   ttl_expiry_item_prioq_compare_func);
+        if (r < 0)
+                return r;
+
+        *ret = lldp;
+        lldp = NULL;
+
+        return 0;
+}
diff --git a/src/libsystemd-network/sd-lldp.h b/src/libsystemd-network/sd-lldp.h
new file mode 100644
index 0000000..95d802d
--- /dev/null
+++ b/src/libsystemd-network/sd-lldp.h
@@ -0,0 +1,51 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2014 Tom Gundersen
+  Copyright (C) 2014 Susant Sahani
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#pragma once
+
+#include "lldp-tlv.h"
+#include "sd-event.h"
+
+typedef struct sd_lldp sd_lldp;
+typedef struct lldp_agent_statitics lldp_agent_statitics;
+
+typedef enum LLDPPortStatus {
+        LLDP_PORT_STATUS_NONE,
+        LLDP_PORT_STATUS_ENABLED,
+        LLDP_PORT_STATUS_DISABLED,
+        _LLDP_PORT_STATUS_MAX,
+        _LLDP_PORT_STATUS_INVALID = -1,
+} LLDPPortStatus;
+
+int sd_lldp_new(int ifindex, char *ifname, struct ether_addr *mac, sd_lldp **ret);
+void sd_lldp_free(sd_lldp *lldp);
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(sd_lldp *, sd_lldp_free);
+#define _cleanup_sd_lldp_free_ _cleanup_(sd_lldp_freep)
+
+int sd_lldp_start(sd_lldp *lldp);
+int sd_lldp_stop(sd_lldp *lldp);
+
+int sd_lldp_attach_event(sd_lldp *lldp, sd_event *event, int priority);
+int sd_lldp_detach_event(sd_lldp *lldp);
+
+int lldp_handle_packet(tlv_packet *m, uint16_t length);
diff --git a/src/libsystemd-network/test-lldp.c b/src/libsystemd-network/test-lldp.c
new file mode 100644
index 0000000..e9d5d7b
--- /dev/null
+++ b/src/libsystemd-network/test-lldp.c
@@ -0,0 +1,233 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright (C) 2014 Tom Gundersen
+  Copyright (C) 2014 Susant Sahani
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <linux/if.h>
+#include <linux/if_ether.h>
+#include <net/ethernet.h>
+#include <sys/types.h>
+#include <arpa/inet.h>
+
+#include "macro.h"
+#include "lldp.h"
+#include "lldp-tlv.h"
+
+#define TEST_LLDP_PORT "em1"
+#define TEST_LLDP_TYPE_SYSTEM_NAME "systemd-lldp"
+#define TEST_LLDP_TYPE_SYSTEM_DESC "systemd-lldp-desc"
+
+static struct ether_addr mac_addr = {
+        .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}
+};
+
+static int lldp_build_tlv_packet(tlv_packet **ret) {
+        _cleanup_tlv_packet_free_ tlv_packet *m = NULL;
+        const uint8_t lldp_dst[] = LLDP_MULTICAST_ADDR;
+        struct ether_header ether;
+
+        /* Append ethernet header */
+        memset(&ether, 0, sizeof(ether));
+        memcpy(&ether.ether_dhost, lldp_dst, ETHER_ADDR_LEN);
+        memcpy(&ether.ether_shost, &mac_addr, ETHER_ADDR_LEN);
+        ether.ether_type = htons(ETHERTYPE_LLDP);
+
+        assert_se(tlv_packet_new(&m) >= 0);
+
+        assert_se(tlv_packet_append_bytes(m, &ether, sizeof(struct ether_header)) >= 0);
+
+        assert_se(lldp_tlv_packet_open_container(m, LLDP_TYPE_CHASSIS_ID) >= 0);
+
+        assert_se(tlv_packet_append_u8(m, LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS) >= 0);
+        assert_se(tlv_packet_append_bytes(m, &mac_addr, ETHER_ADDR_LEN) >= 0);
+
+        assert_se(lldp_tlv_packet_close_container(m) >= 0);
+
+        /* port name */
+        assert_se(lldp_tlv_packet_open_container(m, LLDP_TYPE_PORT_ID) >= 0);
+
+        assert_se(tlv_packet_append_u8(m, LLDP_PORT_SUBTYPE_INTERFACE_NAME) >= 0);
+        assert_se(tlv_packet_append_bytes(m, TEST_LLDP_PORT, strlen(TEST_LLDP_PORT) + 1) >= 0);
+
+        assert_se(lldp_tlv_packet_close_container(m) >= 0);
+
+        /* ttl */
+        assert_se(lldp_tlv_packet_open_container(m, LLDP_TYPE_TTL) >= 0);
+
+        assert_se(tlv_packet_append_u16(m, 170) >= 0);
+
+        assert_se(lldp_tlv_packet_close_container(m) >= 0);
+
+        /* system name */
+        assert_se(lldp_tlv_packet_open_container(m, LLDP_TYPE_SYSTEM_NAME) >= 0);
+
+        assert_se(tlv_packet_append_bytes(m, TEST_LLDP_TYPE_SYSTEM_NAME,
+                                          strlen(TEST_LLDP_TYPE_SYSTEM_NAME)) >= 0);
+        assert_se(lldp_tlv_packet_close_container(m) >= 0);
+
+        /* system descrition */
+        assert_se(lldp_tlv_packet_open_container(m, LLDP_TYPE_SYSTEM_DESCRIPTION) >= 0);
+
+        assert_se(tlv_packet_append_bytes(m, TEST_LLDP_TYPE_SYSTEM_DESC,
+                                          strlen(TEST_LLDP_TYPE_SYSTEM_DESC)) >= 0);
+
+        assert_se(lldp_tlv_packet_close_container(m) >= 0);
+
+        /* Mark end of packet */
+        assert_se(lldp_tlv_packet_open_container(m, LLDP_TYPE_END) >= 0);
+        assert_se(lldp_tlv_packet_close_container(m) >= 0);
+
+        *ret = m;
+
+        m = NULL;
+
+        return 0;
+}
+
+static int lldp_parse_chassis_tlv(tlv_packet *m, uint8_t *type) {
+        uint8_t *p, subtype;
+        uint16_t length;
+
+        assert_se(lldp_tlv_packet_enter_container(m, LLDP_TYPE_CHASSIS_ID) >= 0);
+        assert_se(tlv_packet_read_u8(m, &subtype) >= 0);
+
+        switch (subtype) {
+        case LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS:
+
+                *type = LLDP_CHASSIS_SUBTYPE_MAC_ADDRESS;
+                assert_se(tlv_packet_read_bytes(m, &p, &length) >= 0);
+
+                assert_se(memcmp(p, &mac_addr.ether_addr_octet, ETHER_ADDR_LEN) == 0);
+
+                break;
+        default:
+                assert_not_reached("Unhandled option");
+        }
+
+        assert_se(lldp_tlv_packet_exit_container(m) >= 0);
+
+        return 0;
+}
+
+static int lldp_parse_port_id_tlv(tlv_packet *m) {
+        char *str = NULL, *p;
+        uint16_t length;
+        uint8_t subtype;
+
+        assert_se(lldp_tlv_packet_enter_container(m, LLDP_TYPE_PORT_ID) >= 0);
+
+        assert_se(tlv_packet_read_u8(m, &subtype) >= 0);
+
+        switch (subtype) {
+        case LLDP_PORT_SUBTYPE_INTERFACE_NAME:
+                assert_se(tlv_packet_read_string(m, &str, &length) >= 0);
+
+                p = malloc0(length + 1);
+                strncpy(p, str, length-1);
+
+                assert_se(streq(p, TEST_LLDP_PORT) == 1);
+                break;
+        default:
+                assert_not_reached("Unhandled option");
+        }
+
+        assert_se(lldp_tlv_packet_exit_container(m) >= 0);
+
+        return 0;
+}
+
+static int lldp_parse_system_name_tlv(tlv_packet *m) {
+        char *str = NULL, *p;
+        uint16_t length;
+
+        assert_se(lldp_tlv_packet_enter_container(m, LLDP_TYPE_SYSTEM_NAME) >= 0);
+        assert_se(tlv_packet_read_string(m, &str, &length) >= 0);
+
+        p = malloc0(length + 1);
+        strncpy(p, str, length);
+
+        assert_se(streq(p, TEST_LLDP_TYPE_SYSTEM_NAME) == 1);
+
+        assert_se(lldp_tlv_packet_exit_container(m) >= 0);
+
+        return 1;
+}
+
+static int lldp_parse_system_desc_tlv(tlv_packet *m) {
+        char *str = NULL, *p;
+        uint16_t length;
+
+        assert_se(lldp_tlv_packet_enter_container(m, LLDP_TYPE_SYSTEM_DESCRIPTION) >= 0);
+        assert_se(tlv_packet_read_string(m, &str, &length) >= 0);
+
+        p = malloc0(length + 1);
+        strncpy(p, str, length);
+
+        assert_se(streq(p, TEST_LLDP_TYPE_SYSTEM_DESC) == 1);
+
+        assert_se(lldp_tlv_packet_exit_container(m) >= 0);
+
+        return 0;
+}
+
+static int lldp_parse_ttl_tlv(tlv_packet *m) {
+        uint16_t ttl;
+
+        assert_se(lldp_tlv_packet_enter_container(m, LLDP_TYPE_TTL) >= 0);
+        assert_se(tlv_packet_read_u16(m, &ttl) >= 0);
+
+        assert_se(ttl == 170);
+
+        assert_se(lldp_tlv_packet_exit_container(m) >= 0);
+
+        return 0;
+}
+
+static int lldp_parse_tlv_packet(tlv_packet *m, int len) {
+        uint8_t subtype;
+
+        assert_se(tlv_packet_parse_pdu(m, len) >= 0);
+        assert_se(lldp_parse_chassis_tlv(m, &subtype) >= 0);
+        assert_se(lldp_parse_port_id_tlv(m) >= 0);
+        assert_se(lldp_parse_system_name_tlv(m) >= 0);
+        assert_se(lldp_parse_ttl_tlv(m) >= 0);
+        assert_se(lldp_parse_system_desc_tlv(m) >= 0);
+
+        return 0;
+}
+
+int main(int argc, char *argv[]) {
+        _cleanup_tlv_packet_free_ tlv_packet *tlv = NULL;
+
+        /* form a packet */
+        lldp_build_tlv_packet(&tlv);
+
+        /* parse the packet */
+        tlv_packet_parse_pdu(tlv, tlv->length);
+
+        /* verify */
+        lldp_parse_tlv_packet(tlv, tlv->length);
+
+        return 0;
+}



More information about the systemd-commits mailing list