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
|
/*******************************************************************************
LLDP Agent Daemon (LLDPAD) Software
Copyright(c) 2007-2010 Intel Corporation.
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>
*******************************************************************************/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include "linux/netlink.h"
#include "linux/rtnetlink.h"
#include "linux/dcbnl.h"
#include "linux/if.h"
#include "lldp_util.h"
#include "lldp_rtnl.h"
#include "messages.h"
#include "lldp.h"
#define NLMSG(c) ((struct nlmsghdr *) (c))
#define NLMSG_SIZE 1024
typedef int rtnl_handler(struct nlmsghdr *nh, void *arg);
/**
* rtnl_recv - receive from a routing netlink socket
* @s: routing netlink socket with data ready to be received
*
* Returns: 0 when NLMSG_DONE is received
* <0 on error
* >0 when more data is expected
*/
static int rtnl_recv(int s, UNUSED rtnl_handler *fn, UNUSED void *arg)
{
char buf[8192];
struct nlmsghdr *nh;
int res;
int rc = 0;
unsigned len;
bool more = false;
more:
res = recv(s, buf, sizeof(buf), 0);
if (res < 0)
return res;
len = res;
for (nh = NLMSG(buf); NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) {
if (nh->nlmsg_flags & NLM_F_MULTI)
more = true;
switch (nh->nlmsg_type) {
case NLMSG_NOOP:
break;
case NLMSG_ERROR:
rc = ((struct nlmsgerr *)NLMSG_DATA(nh))->error;
break;
case NLMSG_DONE:
more = false;
break;
default:
break;
}
}
if (more)
goto more;
return rc;
}
#define NLMSG_TAIL(nmsg) \
((struct rtattr *)(((void *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
static void add_rtattr(struct nlmsghdr *n, int type, const void *data, int alen)
{
struct rtattr *rta = NLMSG_TAIL(n);
int len = RTA_LENGTH(alen);
rta->rta_type = type;
rta->rta_len = len;
if (data)
memcpy(RTA_DATA(rta), data, alen);
n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
}
static ssize_t rtnl_send_linkmode(int s, int ifindex,
const char *ifname, __u8 linkmode)
{
struct {
struct nlmsghdr nh;
struct ifinfomsg ifm;
char attrbuf[
RTA_SPACE(IFNAMSIZ) /* IFNAME */
+ RTA_SPACE(1)]; /* LINKMODE */
} req = {
.nh = {
.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
.nlmsg_type = RTM_SETLINK,
.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
},
.ifm = {
.ifi_index = ifindex,
},
};
if (ifname)
add_rtattr(&req.nh, IFLA_IFNAME, ifname, strlen(ifname));
add_rtattr(&req.nh, IFLA_LINKMODE, &linkmode, 1);
return send(s, &req, req.nh.nlmsg_len, 0);
}
int set_linkmode(int ifindex, const char *ifname, __u8 linkmode)
{
int s;
int rc;
s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
if (s < 0)
return s;
rc = rtnl_send_linkmode(s, ifindex, ifname, linkmode);
if (rc < 0)
goto out;
rc = rtnl_recv(s, NULL, NULL);
out:
close(s);
return rc;
}
static ssize_t rtnl_send_operstate(int s, int ifindex,
char *ifname, __u8 operstate)
{
struct {
struct nlmsghdr nh;
struct ifinfomsg ifm;
char attrbuf[
RTA_SPACE(IFNAMSIZ) /* IFNAME */
+ RTA_SPACE(1)]; /* OPERSTATE */
} req = {
.nh = {
.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
.nlmsg_type = RTM_SETLINK,
.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
},
.ifm = {
.ifi_index = ifindex,
},
};
if (ifname)
add_rtattr(&req.nh, IFLA_IFNAME, ifname, strlen(ifname));
add_rtattr(&req.nh, IFLA_OPERSTATE, &operstate, 1);
return send(s, &req, req.nh.nlmsg_len, 0);
}
static ssize_t rtnl_recv_operstate(int s, int ifindex,
char *ifname, __u8 *operstate)
{
struct nlmsghdr *nlh;
struct ifinfomsg *ifi;
struct rtattr *rta;
int attrlen;
int rc = -1;
nlh = malloc(NLMSG_SIZE);
if (!nlh)
return rc;
memset(nlh, 0, NLMSG_SIZE);
/* send ifname request */
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
nlh->nlmsg_type = RTM_GETLINK;
nlh->nlmsg_flags = NLM_F_REQUEST;
ifi = NLMSG_DATA(nlh);
ifi->ifi_family = AF_UNSPEC;
ifi->ifi_index = ifindex;
if (ifname)
add_rtattr(nlh, IFLA_IFNAME, ifname, strlen(ifname));
rc = send(s, nlh, nlh->nlmsg_len, 0);
if (rc < 0)
goto out;
/* recv ifname reply */
memset(nlh, 0, NLMSG_SIZE);
rc = recv(s, (void *) nlh, NLMSG_SIZE, MSG_DONTWAIT);
if (rc < 0)
goto out;
ifi = NLMSG_DATA(nlh);
rta = IFLA_RTA(ifi);
attrlen = NLMSG_PAYLOAD(nlh, 0) - sizeof(struct ifinfomsg);
while (RTA_OK(rta, attrlen)) {
if (rta->rta_type == IFLA_OPERSTATE)
memcpy(operstate, RTA_DATA(rta), sizeof(__u8));
rta = RTA_NEXT(rta, attrlen);
}
out:
free(nlh);
return rc;
}
int set_operstate(char *ifname, __u8 operstate)
{
int s;
int rc;
int ifindex = 0;
s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
if (s < 0)
return s;
rc = rtnl_send_operstate(s, ifindex, ifname, operstate);
if (rc < 0)
goto out;
rc = rtnl_recv(s, NULL, NULL);
out:
close(s);
return rc;
}
int get_operstate(char *ifname)
{
int s;
int ifindex;
__u8 operstate = IF_OPER_UNKNOWN;
ifindex = get_ifidx(ifname);
s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
if (s < 0)
return s;
rtnl_recv_operstate(s, ifindex, ifname, &operstate);
close(s);
return operstate;
}
int get_perm_hwaddr(const char *ifname, u8 *buf_perm, u8 *buf_san)
{
int s;
struct rtattr *rta;
int rc = 0;
struct {
struct nlmsghdr nh;
struct dcbmsg d;
union {
struct rtattr rta;
char attrbuf[RTA_SPACE(2 * IFNAMSIZ)];
} u;
} req = {
.nh = {
.nlmsg_len = NLMSG_LENGTH(sizeof(struct dcbmsg)),
.nlmsg_type = RTM_GETDCB,
.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
},
.d = {
.cmd = DCB_CMD_GPERM_HWADDR,
.dcb_family = AF_UNSPEC,
.dcb_pad = 0,
},
};
s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
if (s < 0) {
rc = -EIO;
goto out_nosock;
}
add_rtattr(&req.nh, DCB_ATTR_IFNAME, ifname, strlen(ifname) + 1);
add_rtattr(&req.nh, DCB_ATTR_PERM_HWADDR, NULL, 0);
rc = send(s, &req.nh, req.nh.nlmsg_len, 0);
if (rc < 0)
goto out;
/* recv ifname reply */
memset(&req, 0, sizeof(req));
rc = recv(s, (void *) &req, sizeof(req), MSG_DONTWAIT);
if (rc < 0)
goto out;
if (req.d.cmd != DCB_CMD_GPERM_HWADDR) {
rc = -EIO;
goto out;
}
rta = &req.u.rta;
if (rta->rta_type != DCB_ATTR_PERM_HWADDR) {
/* Do we really want to code up an attribute parser?? */
rc = -EIO;
goto out;
}
memcpy(buf_perm, RTA_DATA(rta), ETH_ALEN);
memcpy(buf_san, RTA_DATA(rta) + ETH_ALEN, ETH_ALEN);
rc = 0;
out:
close(s);
out_nosock:
return rc;
}
|