ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani/***
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani This file is part of systemd.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Copyright (C) 2014 Tom Gundersen
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Copyright (C) 2014 Susant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani systemd is free software; you can redistribute it and/or modify it
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani under the terms of the GNU Lesser General Public License as published by
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani the Free Software Foundation; either version 2.1 of the License, or
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani (at your option) any later version.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani systemd is distributed in the hope that it will be useful, but
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani WITHOUT ANY WARRANTY; without even the implied warranty of
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani Lesser General Public License for more details.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani You should have received a copy of the GNU Lesser General Public License
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani along with systemd; If not, see <http://www.gnu.org/licenses/>.
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani***/
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani#include "async.h"
0037c2dc54ca5bc5a3ec428619ff136066467565Beniamino Galvani#include "lldp-internal.h"
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include "lldp-network.h"
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include "lldp-port.h"
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahaniint lldp_port_start(lldp_port *p) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani int r;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_return(p, -EINVAL);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = lldp_network_bind_raw_socket(p->ifindex);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (r < 0)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return r;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani p->rawfd = r;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = sd_event_add_io(p->event, &p->lldp_port_rx,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani p->rawfd, EPOLLIN, lldp_receive_packet, p);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (r < 0) {
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering log_debug_errno(r, "Failed to allocate event source: %m");
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering goto fail;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = sd_event_source_set_priority(p->lldp_port_rx, p->event_priority);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (r < 0) {
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering log_debug_errno(r, "Failed to set event priority: %m");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani goto fail;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani r = sd_event_source_set_description(p->lldp_port_rx, "lldp-port-rx");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (r < 0) {
e53fc357a9bb9d0a5362ccc4246d598cb0febd5eLennart Poettering log_debug_errno(r, "Failed to set event name: %m");
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani goto fail;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani }
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return 0;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanifail:
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani lldp_port_stop(p);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return r;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahaniint lldp_port_stop(lldp_port *p) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_return(p, -EINVAL);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani p->rawfd = asynchronous_close(p->rawfd);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani p->lldp_port_rx = sd_event_source_unref(p->lldp_port_rx);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return 0;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahanivoid lldp_port_free(lldp_port *p) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (!p)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani lldp_port_stop(p);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani free(p->ifname);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani free(p);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahaniint lldp_port_new(int ifindex,
7a6f1457462840ed52f2a332e2bd56ca210cff2dTom Gundersen const char *ifname,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani const struct ether_addr *addr,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani void *userdata,
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani lldp_port **ret) {
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani _cleanup_free_ lldp_port *p = NULL;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_return(ifindex, -EINVAL);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_return(ifname, -EINVAL);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani assert_return(addr, -EINVAL);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani p = new0(lldp_port, 1);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (!p)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return -ENOMEM;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani p->rawfd = -1;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani p->ifindex = ifindex;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani p->ifname = strdup(ifname);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani if (!p->ifname)
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return -ENOMEM;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani memcpy(&p->mac, addr, ETH_ALEN);
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani p->userdata = userdata;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani *ret = p;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani p = NULL;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani return 0;
ad1ad5c8e36ea795034fcdac660b15d7c141d55bSusant Sahani}