]> git.ipfire.org Git - thirdparty/squid.git/blob - src/eui/Eui48.cc
Add missing includes
[thirdparty/squid.git] / src / eui / Eui48.cc
1 /*
2 * DEBUG: section 89 EUI-48 Lookup
3 * AUTHOR: Duane Wessels
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 *
32 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
33 */
34
35 #include "squid.h"
36
37 #if USE_SQUID_EUI
38
39 #include "Debug.h"
40 #include "eui/Eui48.h"
41 #include "globals.h"
42 #include "ip/Address.h"
43
44 #if HAVE_ERRNO_H
45 #include <errno.h>
46 #endif
47 #if _SQUID_CYGWIN_
48 #include <squid_windows.h>
49 #endif
50
51 /* START Legacy includes pattern */
52 /* TODO: clean this up so we dont have per-OS requirements.
53 The files are checked for existence individually
54 and can be wrapped
55 */
56
57 #if _SQUID_WINDOWS_
58 struct arpreq {
59
60 Ip::Address arp_pa; /* protocol address */
61
62 struct sockaddr arp_ha; /* hardware address */
63 int arp_flags; /* flags */
64 };
65
66 #include <Iphlpapi.h>
67 #endif
68
69 #if HAVE_SYS_TYPES_H
70 #include <sys/types.h>
71 #endif
72 #if HAVE_SYS_PARAM_H
73 #include <sys/param.h>
74 #endif
75 #if HAVE_SYS_SOCKIO_H
76 /* required by Solaris */
77 #include <sys/sockio.h>
78 #endif
79 #if HAVE_SYS_SYSCTL_H
80 #include <sys/sysctl.h>
81 #endif
82 #if HAVE_NET_ROUTE_H
83 #include <net/route.h>
84 #endif
85 #if HAVE_NET_IF_H
86 #include <net/if.h>
87 #endif
88 #if HAVE_NET_IF_ARP_H
89 #include <net/if_arp.h>
90 #endif
91 #if HAVE_NET_IF_DL_H
92 #include <net/if_dl.h>
93 #endif
94 #if HAVE_NETINET_IF_ETHER_H
95 #include <netinet/if_ether.h>
96 #endif
97 #if HAVE_SYS_IOCTL_H
98 #include <sys/ioctl.h>
99 #endif
100
101 /* ==== BEGIN EUI LOOKUP SUPPORT ============================================= */
102
103 /*
104 * From: dale@server.ctam.bitmcnit.bryansk.su (Dale)
105 * To: wessels@nlanr.net
106 * Subject: Another Squid patch... :)
107 * Date: Thu, 04 Dec 1997 19:55:01 +0300
108 * ============================================================================
109 *
110 * Working on setting up a proper firewall for a network containing some
111 * Win'95 computers at our Univ, I've discovered that some smart students
112 * avoid the restrictions easily just changing their IP addresses in Win'95
113 * Contol Panel... It has been getting boring, so I took Squid-1.1.18
114 * sources and added a new acl type for hard-wired access control:
115 *
116 * acl <name> arp <Ethernet address> ...
117 *
118 * For example,
119 *
120 * acl students arp 00:00:21:55:ed:22 00:00:21:ff:55:38
121 *
122 * NOTE: Linux code by David Luyer <luyer@ucs.uwa.edu.au>.
123 * Original (BSD-specific) code no longer works.
124 * Solaris code by R. Gancarz <radekg@solaris.elektrownia-lagisza.com.pl>
125 */
126
127 bool
128 Eui::Eui48::decode(const char *asc)
129 {
130 int a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0;
131
132 if (sscanf(asc, "%x:%x:%x:%x:%x:%x", &a1, &a2, &a3, &a4, &a5, &a6) != 6) {
133 debugs(28, DBG_CRITICAL, "Decode EUI-48: Invalid ethernet address '" << asc << "'");
134 clear();
135 return false; /* This is not valid address */
136 }
137
138 eui[0] = (u_char) a1;
139 eui[1] = (u_char) a2;
140 eui[2] = (u_char) a3;
141 eui[3] = (u_char) a4;
142 eui[4] = (u_char) a5;
143 eui[5] = (u_char) a6;
144 return true;
145 }
146
147 bool
148 Eui::Eui48::encode(char *buf, const int len)
149 {
150 if (len < SZ_EUI48_BUF) return false;
151
152 snprintf(buf, len, "%02x:%02x:%02x:%02x:%02x:%02x",
153 eui[0] & 0xff, eui[1] & 0xff,
154 eui[2] & 0xff, eui[3] & 0xff,
155 eui[4] & 0xff, eui[5] & 0xff);
156 return true;
157 }
158
159 // return binary representation of the EUI
160 bool
161 Eui::Eui48::lookup(const Ip::Address &c)
162 {
163 #if !_SQUID_WINDOWS_
164 #endif /* !_SQUID_WINDOWS_ */
165
166 Ip::Address ipAddr = c;
167 ipAddr.SetPort(0);
168
169 #if _SQUID_LINUX_
170
171 unsigned char ifbuffer[sizeof(struct ifreq) * 64];
172 struct ifconf ifc;
173
174 struct ifreq *ifr;
175 int offset;
176
177 /* IPv6 builds do not provide the first http_port as an IPv4 socket for ARP */
178 int tmpSocket = socket(AF_INET,SOCK_STREAM,0);
179
180 /*
181 * The linux kernel 2.2 maintains per interface ARP caches and
182 * thus requires an interface name when doing ARP queries.
183 *
184 * The older 2.0 kernels appear to use a unified ARP cache,
185 * and require an empty interface name
186 *
187 * To support both, we attempt the lookup with a blank interface
188 * name first. If that does not succeed, the try each interface
189 * in turn
190 */
191
192 /*
193 * Set up structures for ARP lookup with blank interface name
194 */
195 struct arpreq arpReq;
196 memset(&arpReq, '\0', sizeof(arpReq));
197
198 struct sockaddr_in *sa = (struct sockaddr_in*)&arpReq.arp_pa;
199 ipAddr.GetSockAddr(*sa);
200
201 /* Query ARP table */
202 if (ioctl(tmpSocket, SIOCGARP, &arpReq) != -1) {
203 /* Skip non-ethernet interfaces */
204 close(tmpSocket);
205
206 if (arpReq.arp_ha.sa_family != ARPHRD_ETHER) {
207 clear();
208 return false;
209 }
210
211 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex <<
212 std::setw(2) << (arpReq.arp_ha.sa_data[0] & 0xff) << ":" <<
213 std::setw(2) << (arpReq.arp_ha.sa_data[1] & 0xff) << ":" <<
214 std::setw(2) << (arpReq.arp_ha.sa_data[2] & 0xff) << ":" <<
215 std::setw(2) << (arpReq.arp_ha.sa_data[3] & 0xff) << ":" <<
216 std::setw(2) << (arpReq.arp_ha.sa_data[4] & 0xff) << ":" <<
217 std::setw(2) << (arpReq.arp_ha.sa_data[5] & 0xff));
218
219 set(arpReq.arp_ha.sa_data, 6);
220 return true;
221 }
222
223 /* lookup list of interface names */
224 ifc.ifc_len = sizeof(ifbuffer);
225
226 ifc.ifc_buf = (char *)ifbuffer;
227
228 if (ioctl(tmpSocket, SIOCGIFCONF, &ifc) < 0) {
229 debugs(28, DBG_IMPORTANT, "Attempt to retrieve interface list failed: " << xstrerror());
230 clear();
231 close(tmpSocket);
232 return false;
233 }
234
235 if (ifc.ifc_len > (int)sizeof(ifbuffer)) {
236 debugs(28, DBG_IMPORTANT, "Interface list too long - " << ifc.ifc_len);
237 clear();
238 close(tmpSocket);
239 return false;
240 }
241
242 /* Attempt ARP lookup on each interface */
243 offset = 0;
244
245 while (offset < ifc.ifc_len) {
246
247 ifr = (struct ifreq *) (ifbuffer + offset);
248 offset += sizeof(*ifr);
249 /* Skip loopback and aliased interfaces */
250
251 if (0 == strncmp(ifr->ifr_name, "lo", 2))
252 continue;
253
254 if (NULL != strchr(ifr->ifr_name, ':'))
255 continue;
256
257 debugs(28, 4, "Looking up ARP address for " << ipAddr << " on " << ifr->ifr_name);
258
259 /* Set up structures for ARP lookup */
260
261 memset(&arpReq, '\0', sizeof(arpReq));
262
263 sa = (sockaddr_in*)&arpReq.arp_pa;
264 ipAddr.GetSockAddr(*sa);
265
266 strncpy(arpReq.arp_dev, ifr->ifr_name, sizeof(arpReq.arp_dev) - 1);
267
268 arpReq.arp_dev[sizeof(arpReq.arp_dev) - 1] = '\0';
269
270 /* Query ARP table */
271 if (-1 == ioctl(tmpSocket, SIOCGARP, &arpReq)) {
272 /*
273 * Query failed. Do not log failed lookups or "device
274 * not supported"
275 */
276
277 if (ENXIO == errno)
278 (void) 0;
279 else if (ENODEV == errno)
280 (void) 0;
281 else
282 debugs(28, DBG_IMPORTANT, "ARP query " << ipAddr << " failed: " << ifr->ifr_name << ": " << xstrerror());
283
284 continue;
285 }
286
287 /* Skip non-ethernet interfaces */
288 if (arpReq.arp_ha.sa_family != ARPHRD_ETHER)
289 continue;
290
291 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex <<
292 std::setw(2) << (arpReq.arp_ha.sa_data[0] & 0xff) << ":" <<
293 std::setw(2) << (arpReq.arp_ha.sa_data[1] & 0xff) << ":" <<
294 std::setw(2) << (arpReq.arp_ha.sa_data[2] & 0xff) << ":" <<
295 std::setw(2) << (arpReq.arp_ha.sa_data[3] & 0xff) << ":" <<
296 std::setw(2) << (arpReq.arp_ha.sa_data[4] & 0xff) << ":" <<
297 std::setw(2) << (arpReq.arp_ha.sa_data[5] & 0xff) << " on "<<
298 std::setfill(' ') << ifr->ifr_name);
299
300 set(arpReq.arp_ha.sa_data, 6);
301
302 /*
303 * Should we stop looking here? Can the same IP address
304 * exist on multiple interfaces?
305 */
306
307 /* AYJ: 2009-10-06: for now we have to. We can only store one EUI at a time. */
308 close(tmpSocket);
309 return true;
310 }
311
312 close(tmpSocket);
313
314 #elif _SQUID_SOLARIS_
315
316 /* IPv6 builds do not provide the first http_port as an IPv4 socket for ARP */
317 int tmpSocket = socket(AF_INET,SOCK_STREAM,0);
318
319 /* Set up structures for ARP lookup with blank interface name */
320 struct arpreq arpReq;
321 memset(&arpReq, '\0', sizeof(arpReq));
322
323 struct sockaddr_in *sa = (struct sockaddr_in*)&arpReq.arp_pa;
324 ipAddr.GetSockAddr(*sa);
325
326 /* Query ARP table */
327 if (ioctl(tmpSocket, SIOCGARP, &arpReq) != -1) {
328 /*
329 * Solaris (at least 2.6/x86) does not use arp_ha.sa_family -
330 * it returns 00:00:00:00:00:00 for non-ethernet media
331 */
332 close(tmpSocket);
333
334 if (arpReq.arp_ha.sa_data[0] == 0 &&
335 arpReq.arp_ha.sa_data[1] == 0 &&
336 arpReq.arp_ha.sa_data[2] == 0 &&
337 arpReq.arp_ha.sa_data[3] == 0 &&
338 arpReq.arp_ha.sa_data[4] == 0 && arpReq.arp_ha.sa_data[5] == 0) {
339 clear();
340 return false;
341 }
342
343 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex <<
344 std::setw(2) << (arpReq.arp_ha.sa_data[0] & 0xff) << ":" <<
345 std::setw(2) << (arpReq.arp_ha.sa_data[1] & 0xff) << ":" <<
346 std::setw(2) << (arpReq.arp_ha.sa_data[2] & 0xff) << ":" <<
347 std::setw(2) << (arpReq.arp_ha.sa_data[3] & 0xff) << ":" <<
348 std::setw(2) << (arpReq.arp_ha.sa_data[4] & 0xff) << ":" <<
349 std::setw(2) << (arpReq.arp_ha.sa_data[5] & 0xff));
350
351 set(arpReq.arp_ha.sa_data, 6);
352 return true;
353 }
354
355 #elif _SQUID_FREEBSD_ || _SQUID_NETBSD_ || _SQUID_OPENBSD_ || _SQUID_DRAGONFLY_ || _SQUID_KFREEBSD_
356
357 int mib[6];
358
359 size_t needed;
360
361 char *lim, *buf, *next;
362
363 struct rt_msghdr *rtm;
364
365 struct sockaddr_inarp *sin;
366
367 struct sockaddr_dl *sdl;
368
369 /*
370 * Set up structures for ARP lookup with blank interface name
371 */
372 struct arpreq arpReq;
373 memset(&arpReq, '\0', sizeof(arpReq));
374
375 struct sockaddr_in *sa = (struct sockaddr_in*)&arpReq.arp_pa;
376 ipAddr.GetSockAddr(*sa);
377
378 /* Query ARP table */
379 mib[0] = CTL_NET;
380
381 mib[1] = PF_ROUTE;
382
383 mib[2] = 0;
384
385 mib[3] = AF_INET;
386
387 mib[4] = NET_RT_FLAGS;
388
389 mib[5] = RTF_LLINFO;
390
391 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
392 debugs(28, DBG_CRITICAL, "Can't estimate ARP table size!");
393 clear();
394 return false;
395 }
396
397 if ((buf = (char *)xmalloc(needed)) == NULL) {
398 debugs(28, DBG_CRITICAL, "Can't allocate temporary ARP table!");
399 clear();
400 return false;
401 }
402
403 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
404 debugs(28, DBG_CRITICAL, "Can't retrieve ARP table!");
405 xfree(buf);
406 clear();
407 return false;
408 }
409
410 lim = buf + needed;
411
412 for (next = buf; next < lim; next += rtm->rtm_msglen) {
413
414 rtm = (struct rt_msghdr *) next;
415
416 sin = (struct sockaddr_inarp *) (rtm + 1);
417 /*sdl = (struct sockaddr_dl *) (sin + 1); */
418
419 #define ROUNDUP(a) \
420 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
421
422 sdl = (struct sockaddr_dl *)((char *) sin + ROUNDUP(sin->sin_len));
423
424 if (ipAddr == sin->sin_addr) {
425 if (sdl->sdl_alen) {
426
427 arpReq.arp_ha.sa_len = sizeof(struct sockaddr);
428 arpReq.arp_ha.sa_family = AF_UNSPEC;
429 memcpy(arpReq.arp_ha.sa_data, LLADDR(sdl), sdl->sdl_alen);
430 }
431 }
432 }
433
434 xfree(buf);
435
436 if (arpReq.arp_ha.sa_data[0] == 0 && arpReq.arp_ha.sa_data[1] == 0 &&
437 arpReq.arp_ha.sa_data[2] == 0 && arpReq.arp_ha.sa_data[3] == 0 &&
438 arpReq.arp_ha.sa_data[4] == 0 && arpReq.arp_ha.sa_data[5] == 0) {
439 clear();
440 return false;
441 }
442
443 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex <<
444 std::setw(2) << (arpReq.arp_ha.sa_data[0] & 0xff) << ":" <<
445 std::setw(2) << (arpReq.arp_ha.sa_data[1] & 0xff) << ":" <<
446 std::setw(2) << (arpReq.arp_ha.sa_data[2] & 0xff) << ":" <<
447 std::setw(2) << (arpReq.arp_ha.sa_data[3] & 0xff) << ":" <<
448 std::setw(2) << (arpReq.arp_ha.sa_data[4] & 0xff) << ":" <<
449 std::setw(2) << (arpReq.arp_ha.sa_data[5] & 0xff));
450
451 set(arpReq.arp_ha.sa_data, 6);
452 return true;
453
454 #elif _SQUID_WINDOWS_
455
456 DWORD dwNetTable = 0;
457
458 DWORD ipNetTableLen = 0;
459
460 PMIB_IPNETTABLE NetTable = NULL;
461
462 DWORD i;
463
464 struct arpreq arpReq;
465 memset(&arpReq, '\0', sizeof(arpReq));
466
467 /* Get size of Windows ARP table */
468 if (GetIpNetTable(NetTable, &ipNetTableLen, FALSE) != ERROR_INSUFFICIENT_BUFFER) {
469 debugs(28, DBG_CRITICAL, "Can't estimate ARP table size!");
470 clear();
471 return false;
472 }
473
474 /* Allocate space for ARP table and assign pointers */
475 if ((NetTable = (PMIB_IPNETTABLE)xmalloc(ipNetTableLen)) == NULL) {
476 debugs(28, DBG_CRITICAL, "Can't allocate temporary ARP table!");
477 clear();
478 return false;
479 }
480
481 /* Get actual ARP table */
482 if ((dwNetTable = GetIpNetTable(NetTable, &ipNetTableLen, FALSE)) != NO_ERROR) {
483 debugs(28, DBG_CRITICAL, "Can't retrieve ARP table!");
484 xfree(NetTable);
485 clear();
486 return false;
487 }
488
489 /* Find MAC address from net table */
490 for (i = 0 ; i < NetTable->dwNumEntries ; ++i) {
491 in_addr a;
492 a.s_addr = NetTable->table[i].dwAddr;
493 if (c == a && (NetTable->table[i].dwType > 2)) {
494 arpReq.arp_ha.sa_family = AF_UNSPEC;
495 memcpy(arpReq.arp_ha.sa_data, NetTable->table[i].bPhysAddr, NetTable->table[i].dwPhysAddrLen);
496 }
497 }
498
499 xfree(NetTable);
500
501 if (arpReq.arp_ha.sa_data[0] == 0 && arpReq.arp_ha.sa_data[1] == 0 &&
502 arpReq.arp_ha.sa_data[2] == 0 && arpReq.arp_ha.sa_data[3] == 0 &&
503 arpReq.arp_ha.sa_data[4] == 0 && arpReq.arp_ha.sa_data[5] == 0) {
504 clear();
505 return false;
506 }
507
508 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex <<
509 std::setw(2) << (arpReq.arp_ha.sa_data[0] & 0xff) << ":" <<
510 std::setw(2) << (arpReq.arp_ha.sa_data[1] & 0xff) << ":" <<
511 std::setw(2) << (arpReq.arp_ha.sa_data[2] & 0xff) << ":" <<
512 std::setw(2) << (arpReq.arp_ha.sa_data[3] & 0xff) << ":" <<
513 std::setw(2) << (arpReq.arp_ha.sa_data[4] & 0xff) << ":" <<
514 std::setw(2) << (arpReq.arp_ha.sa_data[5] & 0xff));
515
516 set(arpReq.arp_ha.sa_data, 6);
517 return true;
518
519 #else
520
521 debugs(28, DBG_CRITICAL, "ERROR: ARP / MAC / EUI-* operations not supported on this operating system.");
522
523 #endif
524 /*
525 * Address was not found on any interface
526 */
527 debugs(28, 3, HERE << ipAddr << " NOT found");
528
529 clear();
530 return false;
531 }
532
533 /* ==== END EUI LOOKUP SUPPORT =============================================== */
534
535 #endif /* USE_SQUID_EUI */