Merge branch 'tomato-ND-USBmod' into tomato-RT
[tomato.git] / release / src / router / radvd / netlink.c
blob21d7bfa3aea50e567bdf065572d8dbb717b46003
1 /*
2 * $Id: netlink.c,v 1.1 2011/02/25 04:17:23 reubenhwk Exp $
4 * Authors:
5 * Lars Fenneberg <lf@elemental.net>
6 * Reuben Hawkins <reubenhwk@gmail.com>
8 * This software is Copyright 1996,1997 by the above mentioned author(s),
9 * All Rights Reserved.
11 * The license which is distributed with this software in the file COPYRIGHT
12 * applies to this software. If your distribution is missing this file, you
13 * may request it from <pekkas@netcore.fi>.
17 #include "config.h"
18 #include "radvd.h"
19 #include "log.h"
20 #include "netlink.h"
22 #include <asm/types.h>
23 #include <sys/socket.h>
24 #include <linux/netlink.h>
25 #include <linux/rtnetlink.h>
26 #include <net/if.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <string.h>
32 void process_netlink_msg(int sock)
34 int len;
35 char buf[4096];
36 struct iovec iov = { buf, sizeof(buf) };
37 struct sockaddr_nl sa;
38 struct msghdr msg = { (void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
39 struct nlmsghdr *nh;
40 struct ifinfomsg * ifinfo;
41 char ifname[IF_NAMESIZE] = {""};
42 char * rc = 0;
44 len = recvmsg (sock, &msg, 0);
45 if (len == -1) {
46 flog(LOG_ERR, "recvmsg failed: %s", strerror(errno));
49 for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len); nh = NLMSG_NEXT (nh, len)) {
50 /* The end of multipart message. */
51 if (nh->nlmsg_type == NLMSG_DONE)
52 return;
54 if (nh->nlmsg_type == NLMSG_ERROR) {
55 flog(LOG_ERR, "%s:%d Some type of netlink error.\n", __FILE__, __LINE__);
56 abort();
59 /* Continue with parsing payload. */
60 ifinfo = NLMSG_DATA(nh);
61 rc = if_indextoname(ifinfo->ifi_index, ifname);
62 if (ifinfo->ifi_flags & IFF_RUNNING) {
63 dlog(LOG_DEBUG, 3, "%s, ifindex %d, flags is running", ifname, ifinfo->ifi_index);
65 else {
66 dlog(LOG_DEBUG, 3, "%s, ifindex %d, flags is *NOT* running", ifname, ifinfo->ifi_index);
68 config_interface();
69 kickoff_adverts();
73 int netlink_socket(void)
75 int rc, sock;
76 struct sockaddr_nl snl;
78 sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
79 if (sock == -1) {
80 flog(LOG_ERR, "Unable to open netlink socket: %s", strerror(errno));
83 memset(&snl, 0, sizeof(snl));
84 snl.nl_family = AF_NETLINK;
85 snl.nl_groups = RTMGRP_LINK;
87 rc = bind(sock, (struct sockaddr*)&snl, sizeof(snl));
88 if (rc == -1) {
89 flog(LOG_ERR, "Unable to bind netlink socket: %s", strerror(errno));
90 close(sock);
91 sock = -1;
94 return sock;