4 * Lars Fenneberg <lf@elemental.net>
5 * Reuben Hawkins <reubenhwk@gmail.com>
7 * This software is Copyright 1996,1997 by the above mentioned author(s),
10 * The license which is distributed with this software in the file COPYRIGHT
11 * applies to this software. If your distribution is missing this file, you
12 * may request it from <pekkas@netcore.fi>.
21 #include <asm/types.h>
22 #include <sys/socket.h>
23 #include <linux/netlink.h>
24 #include <linux/rtnetlink.h>
32 #define SOL_NETLINK 270
35 void process_netlink_msg(int sock
)
39 struct iovec iov
= { buf
, sizeof(buf
) };
40 struct sockaddr_nl sa
;
41 struct msghdr msg
= { (void *)&sa
, sizeof(sa
), &iov
, 1, NULL
, 0, 0 };
43 struct ifinfomsg
* ifinfo
;
44 char ifname
[IF_NAMESIZE
] = {""};
46 len
= recvmsg (sock
, &msg
, 0);
48 flog(LOG_ERR
, "recvmsg failed: %s", strerror(errno
));
51 for (nh
= (struct nlmsghdr
*) buf
; NLMSG_OK (nh
, len
); nh
= NLMSG_NEXT (nh
, len
)) {
52 /* The end of multipart message. */
53 if (nh
->nlmsg_type
== NLMSG_DONE
)
56 if (nh
->nlmsg_type
== NLMSG_ERROR
) {
57 flog(LOG_ERR
, "%s:%d Some type of netlink error.\n", __FILE__
, __LINE__
);
61 /* Continue with parsing payload. */
62 ifinfo
= NLMSG_DATA(nh
);
63 if_indextoname(ifinfo
->ifi_index
, ifname
);
64 if (ifinfo
->ifi_flags
& IFF_RUNNING
) {
65 dlog(LOG_DEBUG
, 3, "%s, ifindex %d, flags is running", ifname
, ifinfo
->ifi_index
);
68 dlog(LOG_DEBUG
, 3, "%s, ifindex %d, flags is *NOT* running", ifname
, ifinfo
->ifi_index
);
74 int netlink_socket(void)
78 struct sockaddr_nl snl
;
80 sock
= socket(PF_NETLINK
, SOCK_RAW
, NETLINK_ROUTE
);
82 flog(LOG_ERR
, "Unable to open netlink socket: %s", strerror(errno
));
84 #ifdef NETLINK_NO_ENOBUFS
85 else if (setsockopt(sock
, SOL_NETLINK
, NETLINK_NO_ENOBUFS
, &val
, sizeof(val
)) < 0 ) {
86 flog(LOG_ERR
, "Unable to setsockopt NETLINK_NO_ENOBUFS: %s", strerror(errno
));
90 memset(&snl
, 0, sizeof(snl
));
91 snl
.nl_family
= AF_NETLINK
;
92 snl
.nl_groups
= RTMGRP_LINK
;
94 rc
= bind(sock
, (struct sockaddr
*)&snl
, sizeof(snl
));
96 flog(LOG_ERR
, "Unable to bind netlink socket: %s", strerror(errno
));