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
|
/*
* netlink_read_reply generic netlink response, for libreswan
*
* Copyright (C) 2012-2013 Kim B. Heino <b@bbbs.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* This program 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 General Public License
* for more details.
*/
#include <stdio.h>
#include <linux/rtnetlink.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include "lswalloc.h"
#include "kernel_netlink_reply.h"
/* ??? one caller thinks errno is meaningful after a failure */
ssize_t netlink_read_reply(int sock, char **pbuf, size_t bufsize,
unsigned int seqnum, __u32 pid)
{
size_t msglen = 0;
for (;;) {
struct sockaddr_nl sa;
ssize_t readlen;
/* Read netlink message, verifying kernel origin. */
do {
socklen_t salen = sizeof(sa);
readlen = recvfrom(sock, *pbuf + msglen,
bufsize - msglen, 0,
(struct sockaddr *)&sa, &salen);
if (readlen <= 0 || salen != sizeof(sa))
return -1;
} while (sa.nl_pid != 0);
/* Verify it's valid */
struct nlmsghdr *nlhdr = (struct nlmsghdr *)(*pbuf + msglen);
if (!NLMSG_OK(nlhdr, (size_t)readlen) ||
nlhdr->nlmsg_type == NLMSG_ERROR)
return -1;
/* Move read pointer */
msglen += readlen;
/* Check if it is the last message */
if (nlhdr->nlmsg_type == NLMSG_DONE)
break;
/* all done if it's not a multi part */
if ((nlhdr->nlmsg_flags & NLM_F_MULTI) == 0)
break;
/* all done if this is the one we were searching for */
if (nlhdr->nlmsg_seq == seqnum && nlhdr->nlmsg_pid == pid)
break;
/* Allocate more memory for buffer if needed. */
if (msglen >= bufsize - NL_BUFMARGIN) {
bufsize = bufsize * 2;
char *newbuf = alloc_bytes(bufsize, "netlink query netlink query()");
memcpy(newbuf, *pbuf, msglen);
pfree(*pbuf);
*pbuf = newbuf;
}
}
return msglen;
}
|