This repository has been archived by the owner on Aug 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnetlink.c
101 lines (87 loc) · 2.46 KB
/
netlink.c
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
/*
*
* Authors:
* Lars Fenneberg <[email protected]>
* Reuben Hawkins <[email protected]>
*
* This software is Copyright 1996,1997 by the above mentioned author(s),
* All Rights Reserved.
*
* The license which is distributed with this software in the file COPYRIGHT
* applies to this software. If your distribution is missing this file, you
* may request it from <[email protected]>.
*
*/
#include "config.h"
#include "radvd.h"
#include "log.h"
#include "netlink.h"
#include <asm/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifndef SOL_NETLINK
#define SOL_NETLINK 270
#endif
void process_netlink_msg(int sock)
{
int len;
char buf[4096];
struct iovec iov = { buf, sizeof(buf) };
struct sockaddr_nl sa;
struct msghdr msg = { (void *)&sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
struct nlmsghdr *nh;
struct ifinfomsg * ifinfo;
char ifname[IF_NAMESIZE] = {""};
len = recvmsg (sock, &msg, 0);
if (len == -1) {
flog(LOG_ERR, "recvmsg failed: %s", strerror(errno));
}
for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len); nh = NLMSG_NEXT (nh, len)) {
/* The end of multipart message. */
if (nh->nlmsg_type == NLMSG_DONE)
return;
if (nh->nlmsg_type == NLMSG_ERROR) {
flog(LOG_ERR, "%s:%d Some type of netlink error.\n", __FILE__, __LINE__);
abort();
}
/* Continue with parsing payload. */
ifinfo = NLMSG_DATA(nh);
if_indextoname(ifinfo->ifi_index, ifname);
if (ifinfo->ifi_flags & IFF_RUNNING) {
dlog(LOG_DEBUG, 3, "%s, ifindex %d, flags is running", ifname, ifinfo->ifi_index);
}
else {
dlog(LOG_DEBUG, 3, "%s, ifindex %d, flags is *NOT* running", ifname, ifinfo->ifi_index);
}
reload_config();
}
}
int netlink_socket(void)
{
int rc, sock;
unsigned int val = 1;
struct sockaddr_nl snl;
sock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (sock == -1) {
flog(LOG_ERR, "Unable to open netlink socket: %s", strerror(errno));
}
else if (setsockopt(sock, SOL_NETLINK, NETLINK_NO_ENOBUFS, &val, sizeof(val)) < 0 ) {
flog(LOG_ERR, "Unable to setsockopt NETLINK_NO_ENOBUFS: %s", strerror(errno));
}
memset(&snl, 0, sizeof(snl));
snl.nl_family = AF_NETLINK;
snl.nl_groups = RTMGRP_LINK;
rc = bind(sock, (struct sockaddr*)&snl, sizeof(snl));
if (rc == -1) {
flog(LOG_ERR, "Unable to bind netlink socket: %s", strerror(errno));
close(sock);
sock = -1;
}
return sock;
}