Don't enable dnsmasq IPSET functionality on 2.4 kernel
[tomato.git] / release / src / router / radvd / netlink.c
blob8e062056fede9688f58f5c94174ae41e466eabe1
1 /*
3 * Authors:
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),
8 * All Rights Reserved.
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>.
16 #include "config.h"
17 #include "radvd.h"
18 #include "log.h"
19 #include "netlink.h"
21 #include <asm/types.h>
22 #include <sys/socket.h>
23 #include <linux/netlink.h>
24 #include <linux/rtnetlink.h>
25 #include <net/if.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <string.h>
31 #ifndef SOL_NETLINK
32 #define SOL_NETLINK 270
33 #endif
35 void process_netlink_msg(int sock)
37 int len;
38 char buf[4096];
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 };
42 struct nlmsghdr *nh;
43 struct ifinfomsg * ifinfo;
44 char ifname[IF_NAMESIZE] = {""};
46 len = recvmsg (sock, &msg, 0);
47 if (len == -1) {
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)
54 return;
56 if (nh->nlmsg_type == NLMSG_ERROR) {
57 flog(LOG_ERR, "%s:%d Some type of netlink error.\n", __FILE__, __LINE__);
58 abort();
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);
67 else {
68 dlog(LOG_DEBUG, 3, "%s, ifindex %d, flags is *NOT* running", ifname, ifinfo->ifi_index);
70 reload_config();
74 int netlink_socket(void)
76 int rc, sock;
77 unsigned int val = 1;
78 struct sockaddr_nl snl;
80 sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
81 if (sock == -1) {
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));
88 #endif
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));
95 if (rc == -1) {
96 flog(LOG_ERR, "Unable to bind netlink socket: %s", strerror(errno));
97 close(sock);
98 sock = -1;
101 return sock;