1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
|
/*******************************************************************************
LLDP Agent Daemon (LLDPAD) Software
Copyright(c) 2007-2010 Intel Corporation.
Substantially modified from:
hostapd-0.5.7
Copyright (c) 2002-2007, Jouni Malinen <jkmaline@cc.hut.fi> and
contributors
This program is free software; you can redistribute it and/or modify it
under the terms and conditions of the GNU General Public License,
version 2, as published by the Free Software Foundation.
This program is distributed in the hope it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
The full GNU General Public License is included in this distribution in
the file called "COPYING".
Contact Information:
open-lldp Mailing List <lldp-devel@open-lldp.org>
*******************************************************************************/
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include "lldpad.h"
#include "eloop.h"
#include "ctrl_iface.h"
#include "dcb_protocol.h"
#include "list.h"
#include "lldp_mod.h"
#include "clif_msgs.h"
#include "lldpad_status.h"
#include "lldp/ports.h"
#include "lldp_dcbx.h"
#include "lldp_util.h"
#include "messages.h"
extern struct lldp_head lldp_head;
struct ctrl_dst {
struct ctrl_dst *next;
struct sockaddr_un addr;
socklen_t addrlen;
int debug_level;
int errors;
u32 *tlv_types; /*tlv event types to recv */
};
static char *hexlist = "0123456789abcdef";
struct clif_cmds {
int cmd;
int (*cmd_handler)(struct clif_data *cd,
struct sockaddr_un *from,
socklen_t fromlen,
char *ibuf, int ilen,
char *rbuf, int rlen);
};
static const struct clif_cmds cmd_tbl[] = {
{ DCB_CMD, clif_iface_module },
{ MOD_CMD, clif_iface_module },
{ ATTACH_CMD, clif_iface_attach },
{ DETACH_CMD, clif_iface_detach },
{ LEVEL_CMD, clif_iface_level },
{ PING_CMD, clif_iface_ping },
{ UNKNOWN_CMD, clif_iface_cmd_unknown }
};
int clif_iface_module(struct clif_data *clifd,
struct sockaddr_un *from,
socklen_t fromlen,
char *ibuf, int ilen,
char *rbuf, int rlen)
{
u32 module_id;
char *cmd_start;
int cmd_len;
struct lldp_module *mod = NULL;
/* identify the module and start of command */
switch (*ibuf) {
case DCB_CMD:
/* Message does not contain a module id, therefore this
* message is a DCBX message. Set module_id to correct value.
*/
module_id = 0x001b2101;
cmd_start = ibuf;
cmd_len = ilen;
break;
case MOD_CMD:
hexstr2bin(ibuf+MOD_ID, (u8 *)&module_id, sizeof(module_id));
module_id = ntohl(module_id);
cmd_start = ibuf + MOD_ID + 2*sizeof(module_id);
cmd_len = ilen - MOD_ID - 2*sizeof(module_id);
break;
default:
return cmd_invalid;
}
mod = find_module_by_id(&lldp_head, module_id);
if (mod && mod->ops && mod->ops->client_cmd)
return (mod->ops->client_cmd)(clifd, from, fromlen,
cmd_start, cmd_len, rbuf+strlen(rbuf), rlen);
else
return cmd_device_not_found;
}
int clif_iface_cmd_unknown(UNUSED struct clif_data *clifd,
UNUSED struct sockaddr_un *from,
UNUSED socklen_t fromlen,
UNUSED char *ibuf, UNUSED int ilen,
UNUSED char *rbuf, UNUSED int rlen)
{
return cmd_invalid;
}
int clif_iface_ping(UNUSED struct clif_data *clifd,
UNUSED struct sockaddr_un *from,
UNUSED socklen_t fromlen,
UNUSED char *ibuf, UNUSED int ilen,
char *rbuf, int rlen)
{
snprintf(rbuf, rlen, "%cPONG%d", PING_CMD, getpid());
return 0;
}
int clif_iface_attach(struct clif_data *clifd,
struct sockaddr_un *from,
socklen_t fromlen,
char *ibuf, UNUSED int ilen,
char *rbuf, int rlen)
{
struct ctrl_dst *dst;
char *tlv, *str, *tokenize;
const char *delim = ",";
int i, tlv_count = 0;
dst = malloc(sizeof(*dst));
if (dst == NULL)
return cmd_failed;
memset(dst, 0, sizeof(*dst));
memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
dst->addrlen = fromlen;
dst->debug_level = MSG_INFO;
/*
* There are two cases here one, the user provided
* no string in which case we must send DCBX events
* to be compatible with legacy clients. Two the
* user sent a comma seperated string of tlv module
* ids it expects events from
*/
/* set default string to DCBX Events */
if (ibuf[1] == '\0') {
dst->tlv_types = malloc(sizeof(u32) * 2);
if (!dst->tlv_types)
goto err_tlv;
dst->tlv_types[0] = LLDP_MOD_DCBX;
/* Insert Termination Pattern */
dst->tlv_types[1] = ~0;
} else {
tlv = strdup(ibuf);
str = tlv;
str++;
/* Count number of TLV Modules */
tokenize = strtok(str, delim);
tlv_count++;
do {
tokenize = strtok(NULL, delim);
tlv_count++;
} while (tokenize);
dst->tlv_types = malloc(sizeof(u32) * tlv_count);
if (!dst->tlv_types)
goto err_types;
memset(dst->tlv_types, 0, sizeof(u32) * tlv_count);
/* Populate tlv_types from comma separated string */
tokenize = strtok(str, delim);
for (i = 0; tokenize; i++) {
char *myend;
dst->tlv_types[i] = strtol(tokenize, &myend, 16);
if (*myend) /* No hexnumber for module id */
goto err_types;
tokenize = strtok(NULL, delim);
}
free(tlv);
/* Insert Termination Pattern */
dst->tlv_types[i] = ~0;
}
/* Insert new node at beginning */
dst->next = clifd->ctrl_dst;
clifd->ctrl_dst = dst;
LLDPAD_DBG("CTRL_IFACE monitor attached\n");
snprintf(rbuf, rlen, "%c", ATTACH_CMD);
return cmd_success;
err_types:
free(tlv);
err_tlv:
free(dst);
LLDPAD_DBG("CTRL_IFACE monitor attach error\n");
snprintf(rbuf, rlen, "%c", ATTACH_CMD);
return cmd_failed;
}
static int detach_clif_monitor(struct clif_data *clifd,
struct sockaddr_un *from,
socklen_t fromlen)
{
struct ctrl_dst *dst, *prev = NULL;
dst = clifd->ctrl_dst;
while (dst) {
if (fromlen == dst->addrlen &&
memcmp(from->sun_path, dst->addr.sun_path,
fromlen-sizeof(from->sun_family)) == 0) {
if (prev == NULL)
clifd->ctrl_dst = dst->next;
else
prev->next = dst->next;
free(dst->tlv_types);
free(dst);
dst = NULL;
LLDPAD_DBG("CTRL_IFACE monitor detached\n");
return 0;
}
prev = dst;
dst = dst->next;
}
return cmd_failed;
}
int clif_iface_detach(struct clif_data *clifd,
struct sockaddr_un *from,
socklen_t fromlen,
UNUSED char *ibuf, UNUSED int ilen,
char *rbuf, int rlen)
{
snprintf(rbuf, rlen, "%c", DETACH_CMD);
return detach_clif_monitor(clifd, from, fromlen);
}
int clif_iface_level(struct clif_data *clifd,
struct sockaddr_un *from,
socklen_t fromlen,
char *ibuf, UNUSED int ilen,
char *rbuf, int rlen)
{
struct ctrl_dst *dst;
char *level;
level = ibuf+1;
snprintf(rbuf, rlen, "%c", LEVEL_CMD);
LLDPAD_DBG("CTRL_IFACE LEVEL %s\n", level);
dst = clifd->ctrl_dst;
while (dst) {
if (fromlen == dst->addrlen &&
memcmp(from->sun_path, dst->addr.sun_path,
fromlen-sizeof(from->sun_family)) == 0) {
LLDPAD_DBG("CTRL_IFACE changed monitor level\n");
return 0;
}
dst = dst->next;
}
return cmd_failed;
}
static int find_cmd_entry(int cmd)
{
int i;
for (i = 0; cmd_tbl[i].cmd != cmd && cmd_tbl[i].cmd != UNKNOWN_CMD; i++)
;
return (i);
}
/* process_clif_cmd - routine to pass command to correct module routine and
* compute length and status.
*
* Note: ibuf and ilen must be verified by caller.
*/
static void process_clif_cmd( struct clif_data *cd,
struct sockaddr_un *from,
socklen_t fromlen,
char *ibuf, int ilen, char *rbuf, int rsize, int *rlen)
{
int status;
/* setup minimum command response message
* status will be updated at end */
snprintf(rbuf, rsize, "%c%02x", CMD_RESPONSE, cmd_failed);
status = cmd_tbl[find_cmd_entry((int)ibuf[0])].cmd_handler(
cd, from, fromlen, ibuf, ilen,
rbuf + strlen(rbuf),
rsize - strlen(rbuf) - 1);
/* update status and compute final length */
rbuf[CLIF_STAT_OFF] = hexlist[(status & 0xf0) >> 4];
rbuf[CLIF_STAT_OFF+1] = hexlist[status & 0x0f];
*rlen = strlen(rbuf);
}
static void ctrl_iface_receive(int sock, void *eloop_ctx,
UNUSED void *sock_ctx)
{
struct clif_data *clifd = eloop_ctx;
char buf[MAX_CLIF_MSGBUF];
struct msghdr smsg;
struct cmsghdr *cmsg;
struct iovec iov;
struct ucred *cred;
char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
int res;
struct sockaddr_un from;
socklen_t fromlen = sizeof(from);
char *reply;
const int reply_size = MAX_CLIF_MSGBUF;
int reply_len;
memset(&buf, 0x00, sizeof(buf));
iov.iov_base = buf;
iov.iov_len = sizeof(buf) - 1;
memset(&smsg, 0x00, sizeof(struct msghdr));
smsg.msg_name = &from;
smsg.msg_namelen = fromlen;
smsg.msg_iov = &iov;
smsg.msg_iovlen = 1;
smsg.msg_control = cred_msg;
smsg.msg_controllen = sizeof(cred_msg);
res = recvmsg(sock, &smsg, 0);
if (res < 0) {
perror("recvfrom(ctrl_iface)");
return;
}
cmsg = CMSG_FIRSTHDR(&smsg);
fromlen = smsg.msg_namelen;
cred = (struct ucred *)CMSG_DATA(cmsg);
if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
LLDPAD_INFO("%s: No sender credentials, ignoring\n",
__FUNCTION__);
sprintf(buf,"R%02x", cmd_bad_params);
sendto(sock, buf, 3, 0, (struct sockaddr *) &from, fromlen);
return;
}
if (cred->uid != 0) {
LLDPAD_INFO("%s: sender uid=%i, ignoring\n",
__FUNCTION__, cred->uid);
sprintf(buf,"R%02x", cmd_no_access);
sendto(sock, buf, 3, 0, (struct sockaddr *) &from,
fromlen);
return;
}
buf[res] = '\0';
/* wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res); */
reply = malloc(reply_size);
if (reply == NULL) {
sendto(sock, "R01", 3, 0, (struct sockaddr *) &from,
fromlen);
return;
}
memset(reply, 0, reply_size);
process_clif_cmd(clifd, &from, fromlen, buf, res,
reply, reply_size, &reply_len);
/* wpa_hexdump_ascii(MSG_DEBUG, "TX ctrl_iface", (u8 *) reply, reply_len); */
sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
free(reply);
}
int ctrl_iface_register(struct clif_data *clifd)
{
return eloop_register_read_sock(clifd->ctrl_sock, ctrl_iface_receive,
clifd, NULL);
}
int ctrl_iface_systemd_socket()
{
char *env, *ptr;
unsigned int p, l;
env = getenv("LISTEN_PID");
if (!env)
return -1;
p = strtoul(env, &ptr, 10);
if (ptr && ptr == env) {
LLDPAD_DBG("Invalid value '%s' for LISTEN_PID\n", env);
return -1;
}
if ((pid_t)p != getpid()) {
LLDPAD_DBG("Invalid PID '%d' from LISTEN_PID\n", p);
return -1;
}
env = getenv("LISTEN_FDS");
if (!env) {
LLDPAD_DBG("LISTEN_FDS is not set\n");
return -1;
}
l = strtoul(env, &ptr, 10);
if (ptr && ptr == env) {
LLDPAD_INFO("Invalid value '%s' for LISTEN_FDS\n", env);
return -1;
}
if (l != 1) {
LLDPAD_INFO("LISTEN_FDS specified %d fds\n", l);
return -1;
}
/* systemd returns fds with an offset of '3' */
return 3;
}
int ctrl_iface_init(struct clif_data *clifd)
{
struct sockaddr_un addr;
int s = -1;
socklen_t addrlen;
const int feature_on = 1;
clifd->ctrl_sock = -1;
clifd->ctrl_dst = NULL;
s = ctrl_iface_systemd_socket();
if (s != -1) {
LLDPAD_INFO("using fd %d from systemd\n", s);
goto out;
}
s = socket(AF_LOCAL, SOCK_DGRAM, 0);
if (s < 0) {
LLDPAD_WARN("failed to create CLI socket: %m\n");
goto fail;
}
/* enable receiving of the sender credentials */
setsockopt(s, SOL_SOCKET, SO_PASSCRED,
&feature_on, sizeof(feature_on));
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
snprintf(&addr.sun_path[1], sizeof(addr.sun_path) - 1,
"%s", LLDP_CLIF_SOCK);
addrlen = sizeof(sa_family_t) + strlen(addr.sun_path + 1) + 1;
if (bind(s, (struct sockaddr *) &addr, addrlen) < 0) {
if (errno == EADDRINUSE)
LLDPAD_WARN("another lldpad instance is running\n");
else
LLDPAD_WARN("failed to bind CLI socket address: %m");
goto fail;
}
/* enable receiving of the sender credentials */
setsockopt(s, SOL_SOCKET, SO_PASSCRED,
&feature_on, sizeof(feature_on));
LLDPAD_INFO("bound ctrl iface to %s\n", &addr.sun_path[1]);
out:
clifd->ctrl_sock = s;
return 0;
fail:
if (s >= 0)
close(s);
return -1;
}
void ctrl_iface_deinit(struct clif_data *clifd)
{
struct ctrl_dst *dst, *prev;
if (clifd->ctrl_sock > -1) {
eloop_unregister_read_sock(clifd->ctrl_sock);
close(clifd->ctrl_sock);
clifd->ctrl_sock = -1;
}
dst = clifd->ctrl_dst;
while (dst) {
prev = dst;
dst = dst->next;
free(prev);
}
free(clifd);
}
int is_ctrl_listening(struct ctrl_dst *dst, u32 type)
{
int i;
u32 term = ~0;
u32 all = 0;
u32 dcbx = LLDP_MOD_DCBX;
if (!dst)
return 0;
for (i=0; dst->tlv_types[i] != term; i++) {
if ((!type && dst->tlv_types[i] == dcbx) ||
dst->tlv_types[i] == type || dst->tlv_types[i] == all)
return 1;
}
return 0;
}
void ctrl_iface_send(struct clif_data *clifd, int level, u32 moduleid,
char *buf, size_t len)
{
struct ctrl_dst *dst, *next;
struct msghdr msg;
int idx, send;
struct iovec io[3];
char levelstr[10] = "";
char modulestr[10] = "";
dst = clifd->ctrl_dst;
if (clifd->ctrl_sock < 0 || dst == NULL)
return;
snprintf(levelstr, sizeof(levelstr), "%c%d", EVENT_MSG, level);
if (moduleid) {
snprintf(modulestr, sizeof(modulestr), "M%08x",moduleid);
io[0].iov_base = modulestr;
io[0].iov_len = strlen(modulestr);
} else {
io[0].iov_base = NULL;
io[0].iov_len = 0;
}
io[1].iov_base = levelstr;
io[1].iov_len = strlen(levelstr);
io[2].iov_base = buf;
io[2].iov_len = len;
memset(&msg, 0, sizeof(msg));
msg.msg_iov = io;
msg.msg_iovlen = 3;
idx = 0;
while (dst) {
next = dst->next;
send = 0;
/* Does dst receive these event messages? */
send = is_ctrl_listening(dst, moduleid);
/* Yes */
if (send && level >= dst->debug_level) {
msg.msg_name = &dst->addr;
msg.msg_namelen = dst->addrlen;
if (sendmsg(clifd->ctrl_sock, &msg, 0) < 0) {
fprintf(stderr,
"CTRL_IFACE monitor[%d][%d] %d:%s: ",
idx, clifd->ctrl_sock, dst->addrlen,
dst->addr.sun_path);
perror("sendmsg");
dst->errors++;
if (dst->errors > 10) {
detach_clif_monitor(
clifd, &dst->addr,
dst->addrlen);
}
} else {
fprintf(stderr,
"CTRL_IFACE monitor[%d][%d] %d:%s: ",
idx, clifd->ctrl_sock, dst->addrlen,
dst->addr.sun_path);
dst->errors = 0;
}
}
idx++;
dst = next;
}
}
|