Debian Codegraph @

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

raw
1 /*
2 * Interface MIB architecture support
3 */
4 #include <net-snmp/net-snmp-config.h>
5 #include <net-snmp/net-snmp-includes.h>
6
7 #include <net-snmp/agent/net-snmp-agent-includes.h>
8 #include <net-snmp/data_access/arp.h>
9 #include <net-snmp/data_access/interface.h>
10
11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <net/if_arp.h>
14 #include <arpa/inet.h>
15 #include <linux/types.h>
16 #include <asm/types.h>
17
18 static int _load_v4(netsnmp_arp_access *access);
19
20 netsnmp_arp_access *
21 netsnmp_access_arp_create(u_int init_flags,
22 NetsnmpAccessArpUpdate *update_hook,
23 NetsnmpAccessArpGC *gc_hook,
24 int *cache_timeout, int *cache_flags,
25 char *cache_expired)
26 {
27 netsnmp_arp_access *access;
28
29 access = SNMP_MALLOC_TYPEDEF(netsnmp_arp_access);
30 if (NULL == access) {
31 snmp_log(LOG_ERR,"malloc error in netsnmp_access_arp_create\n");
32 return NULL;
33 }
34
35 access->arch_magic = NULL;
36 access->magic = NULL;
37 access->update_hook = update_hook;
38 access->gc_hook = gc_hook;
39 access->synchronized = 0;
40
41 if (cache_timeout != NULL)
42 *cache_timeout = 5;
43 if (cache_flags != NULL)
44 *cache_flags |= NETSNMP_CACHE_DONT_FREE_BEFORE_LOAD
45 | NETSNMP_CACHE_AUTO_RELOAD;
46 access->cache_expired = cache_expired;
47
48 return access;
49 }
50
51 int netsnmp_access_arp_delete(netsnmp_arp_access *access)
52 {
53 if (NULL == access)
54 return 0;
55
56 netsnmp_access_arp_unload(access);
57 free(access);
58
59 return 0;
60 }
61
62 int netsnmp_access_arp_load(netsnmp_arp_access *access)
63 {
64 int rc = 0;
65
66 access->generation++;
67 rc =_load_v4(access);
68 access->gc_hook(access);
69 access->synchronized = (rc == 0);
70
71 return rc;
72 }
73
74 int netsnmp_access_arp_unload(netsnmp_arp_access *access)
75 {
76 access->synchronized = 0;
77
78 return 0;
79 }
80
81 /**
82 */
83 static int
84 _load_v4(netsnmp_arp_access *access)
85 {
86 FILE *in;
87 char line[128];
88 int rc = 0;
89 netsnmp_arp_entry *entry;
90 char arp[3*NETSNMP_ACCESS_ARP_PHYSADDR_BUF_SIZE+1];
91 char *arp_token;
92 int i;
93
94 netsnmp_assert(NULL != access);
95
96 #define PROCFILE "/proc/net/arp"
97 if (!(in = fopen(PROCFILE, "r"))) {
98 snmp_log_perror("arp_linux: could not open " PROCFILE);
99 return -2;
100 }
101
102 /*
103 * Get rid of the header line
104 */
105 fgets(line, sizeof(line), in);
106
107 /*
108 * IP address | HW | Flag | HW address | Mask | Device
109 * 192.168.1.4 0x1 0x2 00:40:63:CC:1C:8C * eth0
110 */
111 while (fgets(line, sizeof(line), in)) {
112
113 int za, zb, zc, zd;
114 unsigned int tmp_flags;
115 char ifname[21];
116
117 rc = sscanf(line,
118 "%d.%d.%d.%d 0x%*x 0x%x %96s %*[^ ] %20s\n",
119 &za, &zb, &zc, &zd, &tmp_flags, arp, ifname);
120 if (7 != rc) {
121 snmp_log(LOG_ERR, PROCFILE " data format error (%d!=12)\n", rc);
122 snmp_log(LOG_ERR, " line ==|%s|\n", line);
123 continue;
124 }
125 DEBUGMSGTL(("access:arp:container",
126 "ip addr %d.%d.%d.%d, flags 0x%X, hw addr "
127 "%s, name %s\n",
128 za,zb,zc,zd, tmp_flags, arp, ifname ));
129
130 /*
131 */
132 entry = netsnmp_access_arp_entry_create();
133 if(NULL == entry) {
134 rc = -3;
135 break;
136 }
137
138 /*
139 * look up ifIndex
140 */
141 entry->generation = access->generation;
142 entry->if_index = netsnmp_access_interface_index_find(ifname);
143 if(0 == entry->if_index) {
144 snmp_log(LOG_ERR,"couldn't find ifIndex for '%s', skipping\n",
145 ifname);
146 netsnmp_access_arp_entry_free(entry);
147 continue;
148 }
149
150 /*
151 * now that we've passed all the possible 'continue', assign
152 * index offset.
153 */
154 /* entry->ns_arp_index = ++idx_offset; */
155
156 /*
157 * parse ip addr
158 */
159 entry->arp_ipaddress[0] = za;
160 entry->arp_ipaddress[1] = zb;
161 entry->arp_ipaddress[2] = zc;
162 entry->arp_ipaddress[3] = zd;
163 entry->arp_ipaddress_len = 4;
164
165 /*
166 * parse hw addr
167 */
168 for (arp_token = strtok(arp, ":"), i=0; arp_token != NULL; arp_token = strtok(NULL, ":"), i++) {
169 entry->arp_physaddress[i] = strtol(arp_token, NULL, 16);
170 }
171 entry->arp_physaddress_len = i;
172
173 /*
174 * what can we do with hw? from arp manpage:
175
176 default value of this parameter is ether (i.e. hardware code
177 0x01 for IEEE 802.3 10Mbps Ethernet). Other values might
178 include network technologies such as ARCnet (arcnet) , PROnet
179 (pronet) , AX.25 (ax25) and NET/ROM (netrom).
180 */
181
182 /*
183 * parse mask
184 */
185 /* xxx-rks: what is mask? how to interpret '*'? */
186
187
188 /*
189 * process type
190 */
191 if(tmp_flags & ATF_PERM)
192 entry->arp_type = INETNETTOMEDIATYPE_STATIC;
193 else
194 entry->arp_type = INETNETTOMEDIATYPE_DYNAMIC;
195
196 /*
197 * process status
198 * if flags are 0, we can't tell the difference between
199 * stale or incomplete.
200 */
201 if(tmp_flags & ATF_COM)
202 entry->arp_state = INETNETTOMEDIASTATE_REACHABLE;
203 else
204 entry->arp_state = INETNETTOMEDIASTATE_UNKNOWN;
205
206 /*
207 * add entry to container
208 */
209 access->update_hook(access, entry);
210 }
211
212 fclose(in);
213 return 0;
214 }