Debian Codegraph @

agent/mibgroup/ip-mib/data_access/arp_netlink.c

raw
1 /*
2 * Interface MIB architecture support
3 */
4
5 #include <net-snmp/net-snmp-config.h>
6 #include <net-snmp/net-snmp-includes.h>
7
8 #include <net-snmp/agent/net-snmp-agent-includes.h>
9 #include <net-snmp/data_access/arp.h>
10 #include <net-snmp/data_access/interface.h>
11
12 #include <errno.h>
13 #include <stdint.h>
14 #include <sys/types.h>
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <linux/types.h>
19 #include <linux/rtnetlink.h>
20
21
22 static int fillup_entry_info(netsnmp_arp_entry *entry, struct nlmsghdr *h);
23 static void netsnmp_access_arp_read_netlink(int fd, void *data);
24
25 /**
26 */
27 netsnmp_arp_access *
28 netsnmp_access_arp_create(u_int init_flags,
29 NetsnmpAccessArpUpdate *update_hook,
30 NetsnmpAccessArpGC *gc_hook,
31 int *cache_timeout, int *cache_flags,
32 char *cache_expired)
33 {
34 netsnmp_arp_access *access;
35
36 access = SNMP_MALLOC_TYPEDEF(netsnmp_arp_access);
37 if (NULL == access) {
38 snmp_log(LOG_ERR,"malloc error in netsnmp_access_arp_create\n");
39 return NULL;
40 }
41
42 access->arch_magic = NULL;
43 access->magic = NULL;
44 access->update_hook = update_hook;
45 access->gc_hook = gc_hook;
46 access->synchronized = 0;
47
48 if (cache_timeout != NULL)
49 *cache_timeout = 5;
50 if (cache_flags != NULL)
51 *cache_flags |= NETSNMP_CACHE_RESET_TIMER_ON_USE | NETSNMP_CACHE_DONT_FREE_BEFORE_LOAD;
52 access->cache_expired = cache_expired;
53
54 DEBUGMSGTL(("access:netlink:arp", "create arp cache\n"));
55
56 return access;
57 }
58
59 int netsnmp_access_arp_delete(netsnmp_arp_access *access)
60 {
61 if (NULL == access)
62 return 0;
63
64 netsnmp_access_arp_unload(access);
65 free(access);
66
67 return 0;
68 }
69
70 int netsnmp_access_arp_load(netsnmp_arp_access *access)
71 {
72 int r, fd = (uintptr_t) access->arch_magic;
73 struct {
74 struct nlmsghdr n;
75 struct ndmsg r;
76 } req;
77
78 if (access->synchronized)
79 return 0;
80
81 if (fd == 0) {
82 struct sockaddr_nl sa;
83
84 fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
85 if (fd < 0) {
86 snmp_log_perror("netsnmp_access_arp_load: netlink socket create error");
87 return -1;
88 }
89 access->arch_magic = (void *)(uintptr_t)fd;
90
91 memset(&sa, 0, sizeof(sa));
92 sa.nl_family = AF_NETLINK;
93 sa.nl_groups = RTMGRP_NEIGH;
94 if (bind(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
95 snmp_log_perror("netsnmp_access_arp_load: netlink bind failed");
96 return -1;
97 }
98
99 if (register_readfd(fd, netsnmp_access_arp_read_netlink, access) != 0) {
100 snmp_log(LOG_ERR,"netsnmp_access_arp_load: error registering netlink socket\n");
101 return -1;
102 }
103 }
104
105 DEBUGMSGTL(("access:netlink:arp", "synchronizing arp table\n"));
106
107 access->generation++;
108
109 memset(&req, 0, sizeof(req));
110 req.n.nlmsg_len = sizeof(req);
111 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
112 req.n.nlmsg_type = RTM_GETNEIGH;
113 req.r.ndm_family = AF_UNSPEC;
114
115 r = send(fd, &req, req.n.nlmsg_len, 0);
116 if (r < 0) {
117 snmp_log_perror("netsnmp_access_arp_refresh: send failed");
118 return -1;
119 }
120
121 while (!access->synchronized)
122 netsnmp_access_arp_read_netlink(fd, access);
123 access->gc_hook(access);
124
125 return 0;
126 }
127
128 int netsnmp_access_arp_unload(netsnmp_arp_access *access)
129 {
130 int fd;
131
132 DEBUGMSGTL(("access:netlink:arp", "unload arp cache\n"));
133
134 fd = (uintptr_t) access->arch_magic;
135 if (fd > 0) {
136 unregister_readfd(fd);
137 close(fd);
138 access->arch_magic = NULL;
139 access->synchronized = 0;
140 }
141 return 0;
142 }
143
144 static void netsnmp_access_arp_read_netlink(int fd, void *data)
145 {
146 netsnmp_arp_access *access = (netsnmp_arp_access *) data;
147 netsnmp_arp_entry *entry;
148 char buf[16384];
149 struct nlmsghdr *h;
150 int r, len;
151
152 do {
153 r = recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
154 if (r < 0) {
155 if (errno == EINTR)
156 continue;
157 if (errno == EAGAIN)
158 return;
159 snmp_log(LOG_WARNING, "netlink buffer overrun\n");
160 access->synchronized = 0;
161 if (access->cache_expired != NULL)
162 *access->cache_expired = 1;
163 return;
164 }
165 } while (r < 0 && errno == EINTR);
166 len = r;
167
168 for (h = (struct nlmsghdr *) buf; NLMSG_OK(h, len); h = NLMSG_NEXT(h, len)) {
169 if (h->nlmsg_type == NLMSG_DONE) {
170 access->synchronized = 1;
171 continue;
172 }
173
174 entry = netsnmp_access_arp_entry_create();
175 if (NULL == entry)
176 break;
177
178 DEBUGMSGTL(("access:netlink:arp", "arp netlink notification\n"));
179
180 entry->generation = access->generation;
181 r = fillup_entry_info (entry, h);
182 if (r > 0) {
183 access->update_hook(access, entry);
184 } else {
185 if (r < 0) {
186 NETSNMP_LOGONCE((LOG_ERR, "filling entry info failed\n"));
187 DEBUGMSGTL(("access:netlink:arp", "filling entry info failed\n"));
188 }
189 netsnmp_access_arp_entry_free(entry);
190 }
191 }
192 }
193
194 static int
195 fillup_entry_info(netsnmp_arp_entry *entry, struct nlmsghdr *nlmp)
196 {
197 struct ndmsg *rtmp;
198 struct rtattr *tb[NDA_MAX + 1], *rta;
199 int length;
200
201 rtmp = (struct ndmsg *) NLMSG_DATA(nlmp);
202 switch (nlmp->nlmsg_type) {
203 case RTM_NEWNEIGH:
204 if (rtmp->ndm_state == NUD_FAILED)
205 entry->flags = NETSNMP_ACCESS_ARP_ENTRY_FLAG_DELETE;
206 else
207 entry->flags = 0;
208 break;
209 case RTM_DELNEIGH:
210 entry->flags = NETSNMP_ACCESS_ARP_ENTRY_FLAG_DELETE;
211 break;
212 case RTM_GETNEIGH:
213 return 0;
214 default:
215 DEBUGMSGTL(("access:netlink:arp",
216 "Wrong Netlink message type %d\n", nlmp->nlmsg_type));
217 return -1;
218 }
219
220 if (rtmp->ndm_family != AF_INET && rtmp->ndm_family != AF_INET6) {
221 /* Some address families, notably AF_BRIDGE, have RTM_NEWNEIGH
222 * and RTM_DELNEIGH messages that may not contain an IP address
223 * that we need later. So we drop everything that is not AF_INET or
224 * AF_INET6 silently.*/
225 return 0;
226 }
227
228 if (rtmp->ndm_state == NUD_NOARP) {
229 /* NUD_NOARP is for broadcast addresses and similar,
230 * drop them silently */
231 return 0;
232 }
233
234 memset(tb, 0, sizeof(struct rtattr *) * (NDA_MAX + 1));
235 length = nlmp->nlmsg_len - NLMSG_LENGTH(sizeof(*rtmp));
236 rta = ((struct rtattr *) (((char *) (rtmp)) + NLMSG_ALIGN(sizeof(struct ndmsg))));
237 while (RTA_OK(rta, length)) {
238 if (rta->rta_type <= NDA_MAX)
239 tb[rta->rta_type] = rta;
240 rta = RTA_NEXT(rta, length);
241 }
242
243 /*
244 * Fill up the index and addresses
245 */
246 entry->if_index = rtmp->ndm_ifindex;
247 if (tb[NDA_DST]) {
248 entry->arp_ipaddress_len = RTA_PAYLOAD(tb[NDA_DST]);
249 if (entry->arp_ipaddress_len > sizeof(entry->arp_ipaddress)) {
250 snmp_log(LOG_ERR, "netlink ip address length %d is too long\n",
251 entry->arp_ipaddress_len);
252 return -1;
253 }
254 memcpy(entry->arp_ipaddress, RTA_DATA(tb[NDA_DST]),
255 entry->arp_ipaddress_len);
256 }
257 if (tb[NDA_LLADDR]) {
258 entry->arp_physaddress_len = RTA_PAYLOAD(tb[NDA_LLADDR]);
259 if (entry->arp_physaddress_len > sizeof(entry->arp_physaddress)) {
260 snmp_log(LOG_ERR, "netlink hw address length %d is too long\n",
261 entry->arp_physaddress_len);
262 return -1;
263 }
264 memcpy(entry->arp_physaddress, RTA_DATA(tb[NDA_LLADDR]),
265 entry->arp_physaddress_len);
266 }
267
268 switch (rtmp->ndm_state) {
269 case NUD_INCOMPLETE:
270 entry->arp_state = INETNETTOMEDIASTATE_INCOMPLETE;
271 break;
272 case NUD_REACHABLE:
273 case NUD_PERMANENT:
274 entry->arp_state = INETNETTOMEDIASTATE_REACHABLE;
275 break;
276 case NUD_STALE:
277 entry->arp_state = INETNETTOMEDIASTATE_STALE;
278 break;
279 case NUD_DELAY:
280 entry->arp_state = INETNETTOMEDIASTATE_DELAY;
281 break;
282 case NUD_PROBE:
283 entry->arp_state = INETNETTOMEDIASTATE_PROBE;
284 break;
285 case NUD_FAILED:
286 entry->arp_state = INETNETTOMEDIASTATE_INVALID;
287 break;
288 case NUD_NONE:
289 entry->arp_state = INETNETTOMEDIASTATE_UNKNOWN;
290 break;
291 default:
292 snmp_log(LOG_ERR, "Unrecognized ARP entry state %d", rtmp->ndm_state);
293 break;
294 }
295
296 switch (rtmp->ndm_state) {
297 case NUD_INCOMPLETE:
298 case NUD_FAILED:
299 case NUD_NONE:
300 entry->arp_type = INETNETTOMEDIATYPE_INVALID;
301 break;
302 case NUD_REACHABLE:
303 case NUD_STALE:
304 case NUD_DELAY:
305 case NUD_PROBE:
306 entry->arp_type = INETNETTOMEDIATYPE_DYNAMIC;
307 break;
308 case NUD_PERMANENT:
309 entry->arp_type = INETNETTOMEDIATYPE_STATIC;
310 break;
311 default:
312 entry->arp_type = INETNETTOMEDIATYPE_LOCAL;
313 break;
314 }
315
316 return 1;
317 }