2 * DEBUG: section 89 EUI-48 Lookup
3 * AUTHOR: Duane Wessels
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
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.
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.
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.
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.
32 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
40 #include "eui/Eui48.h"
42 #include "ip/Address.h"
46 #include <squid_windows.h>
49 /* START Legacy includes pattern */
50 /* TODO: clean this up so we dont have per-OS requirements.
51 The files are checked for existence individually
59 Ip::Address arp_pa
; /* protocol address */
61 struct sockaddr arp_ha
; /* hardware address */
62 int arp_flags
; /* flags */
70 /* required by Solaris */
71 #include <sys/sockio.h>
74 #include <sys/sysctl.h>
77 #include <net/route.h>
83 #include <net/if_arp.h>
86 #include <net/if_dl.h>
88 #if HAVE_NETINET_IF_ETHER_H
89 #include <netinet/if_ether.h>
92 #include <sys/ioctl.h>
95 /* ==== BEGIN EUI LOOKUP SUPPORT ============================================= */
98 * From: dale@server.ctam.bitmcnit.bryansk.su (Dale)
99 * To: wessels@nlanr.net
100 * Subject: Another Squid patch... :)
101 * Date: Thu, 04 Dec 1997 19:55:01 +0300
102 * ============================================================================
104 * Working on setting up a proper firewall for a network containing some
105 * Win'95 computers at our Univ, I've discovered that some smart students
106 * avoid the restrictions easily just changing their IP addresses in Win'95
107 * Contol Panel... It has been getting boring, so I took Squid-1.1.18
108 * sources and added a new acl type for hard-wired access control:
110 * acl <name> arp <Ethernet address> ...
114 * acl students arp 00:00:21:55:ed:22 00:00:21:ff:55:38
116 * NOTE: Linux code by David Luyer <luyer@ucs.uwa.edu.au>.
117 * Original (BSD-specific) code no longer works.
118 * Solaris code by R. Gancarz <radekg@solaris.elektrownia-lagisza.com.pl>
122 Eui::Eui48::decode(const char *asc
)
124 int a1
= 0, a2
= 0, a3
= 0, a4
= 0, a5
= 0, a6
= 0;
126 if (sscanf(asc
, "%x:%x:%x:%x:%x:%x", &a1
, &a2
, &a3
, &a4
, &a5
, &a6
) != 6) {
127 debugs(28, 0, "Decode EUI-48: Invalid ethernet address '" << asc
<< "'");
129 return false; /* This is not valid address */
132 eui
[0] = (u_char
) a1
;
133 eui
[1] = (u_char
) a2
;
134 eui
[2] = (u_char
) a3
;
135 eui
[3] = (u_char
) a4
;
136 eui
[4] = (u_char
) a5
;
137 eui
[5] = (u_char
) a6
;
142 Eui::Eui48::encode(char *buf
, const int len
)
144 if (len
< SZ_EUI48_BUF
) return false;
146 snprintf(buf
, len
, "%02x:%02x:%02x:%02x:%02x:%02x",
147 eui
[0] & 0xff, eui
[1] & 0xff,
148 eui
[2] & 0xff, eui
[3] & 0xff,
149 eui
[4] & 0xff, eui
[5] & 0xff);
153 // return binary representation of the EUI
155 Eui::Eui48::lookup(Ip::Address
&c
)
157 struct arpreq arpReq
;
159 struct sockaddr_in
*sa
= NULL
;
160 #endif /* !_SQUID_WIN32_ */
162 Ip::Address ipAddr
= c
;
167 unsigned char ifbuffer
[sizeof(struct ifreq
) * 64];
173 /* IPv6 builds do not provide the first http_port as an IPv4 socket for ARP */
174 int tmpSocket
= socket(AF_INET
,SOCK_STREAM
,0);
177 * The linux kernel 2.2 maintains per interface ARP caches and
178 * thus requires an interface name when doing ARP queries.
180 * The older 2.0 kernels appear to use a unified ARP cache,
181 * and require an empty interface name
183 * To support both, we attempt the lookup with a blank interface
184 * name first. If that does not succeed, the try each interface
189 * Set up structures for ARP lookup with blank interface name
191 memset(&arpReq
, '\0', sizeof(arpReq
));
193 sa
= (sockaddr_in
*)&arpReq
.arp_pa
;
194 ipAddr
.GetSockAddr(*sa
);
196 /* Query ARP table */
197 if (ioctl(tmpSocket
, SIOCGARP
, &arpReq
) != -1) {
198 /* Skip non-ethernet interfaces */
201 if (arpReq
.arp_ha
.sa_family
!= ARPHRD_ETHER
) {
206 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex
<<
207 std::setw(2) << (arpReq
.arp_ha
.sa_data
[0] & 0xff) << ":" <<
208 std::setw(2) << (arpReq
.arp_ha
.sa_data
[1] & 0xff) << ":" <<
209 std::setw(2) << (arpReq
.arp_ha
.sa_data
[2] & 0xff) << ":" <<
210 std::setw(2) << (arpReq
.arp_ha
.sa_data
[3] & 0xff) << ":" <<
211 std::setw(2) << (arpReq
.arp_ha
.sa_data
[4] & 0xff) << ":" <<
212 std::setw(2) << (arpReq
.arp_ha
.sa_data
[5] & 0xff));
214 set(arpReq
.arp_ha
.sa_data
, 6);
218 /* lookup list of interface names */
219 ifc
.ifc_len
= sizeof(ifbuffer
);
221 ifc
.ifc_buf
= (char *)ifbuffer
;
223 if (ioctl(tmpSocket
, SIOCGIFCONF
, &ifc
) < 0) {
224 debugs(28, 1, "Attempt to retrieve interface list failed: " << xstrerror());
230 if (ifc
.ifc_len
> (int)sizeof(ifbuffer
)) {
231 debugs(28, 1, "Interface list too long - " << ifc
.ifc_len
);
237 /* Attempt ARP lookup on each interface */
240 while (offset
< ifc
.ifc_len
) {
242 ifr
= (struct ifreq
*) (ifbuffer
+ offset
);
243 offset
+= sizeof(*ifr
);
244 /* Skip loopback and aliased interfaces */
246 if (0 == strncmp(ifr
->ifr_name
, "lo", 2))
249 if (NULL
!= strchr(ifr
->ifr_name
, ':'))
252 debugs(28, 4, "Looking up ARP address for " << ipAddr
<< " on " << ifr
->ifr_name
);
254 /* Set up structures for ARP lookup */
256 memset(&arpReq
, '\0', sizeof(arpReq
));
258 sa
= (sockaddr_in
*)&arpReq
.arp_pa
;
259 ipAddr
.GetSockAddr(*sa
);
261 strncpy(arpReq
.arp_dev
, ifr
->ifr_name
, sizeof(arpReq
.arp_dev
) - 1);
263 arpReq
.arp_dev
[sizeof(arpReq
.arp_dev
) - 1] = '\0';
265 /* Query ARP table */
266 if (-1 == ioctl(tmpSocket
, SIOCGARP
, &arpReq
)) {
268 * Query failed. Do not log failed lookups or "device
274 else if (ENODEV
== errno
)
277 debugs(28, 1, "ARP query " << ipAddr
<< " failed: " << ifr
->ifr_name
<< ": " << xstrerror());
282 /* Skip non-ethernet interfaces */
283 if (arpReq
.arp_ha
.sa_family
!= ARPHRD_ETHER
)
286 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex
<<
287 std::setw(2) << (arpReq
.arp_ha
.sa_data
[0] & 0xff) << ":" <<
288 std::setw(2) << (arpReq
.arp_ha
.sa_data
[1] & 0xff) << ":" <<
289 std::setw(2) << (arpReq
.arp_ha
.sa_data
[2] & 0xff) << ":" <<
290 std::setw(2) << (arpReq
.arp_ha
.sa_data
[3] & 0xff) << ":" <<
291 std::setw(2) << (arpReq
.arp_ha
.sa_data
[4] & 0xff) << ":" <<
292 std::setw(2) << (arpReq
.arp_ha
.sa_data
[5] & 0xff) << " on "<<
293 std::setfill(' ') << ifr
->ifr_name
);
295 set(arpReq
.arp_ha
.sa_data
, 6);
298 * Should we stop looking here? Can the same IP address
299 * exist on multiple interfaces?
302 /* AYJ: 2009-10-06: for now we have to. We can only store one EUI at a time. */
309 #elif _SQUID_SOLARIS_
311 /* IPv6 builds do not provide the first http_port as an IPv4 socket for ARP */
312 int tmpSocket
= socket(AF_INET
,SOCK_STREAM
,0);
314 /* Set up structures for ARP lookup with blank interface name */
316 memset(&arpReq
, '\0', sizeof(arpReq
));
318 sa
= (sockaddr_in
*)&arpReq
.arp_pa
;
319 ipAddr
.GetSockAddr(*sa
);
321 /* Query ARP table */
322 if (ioctl(tmpSocket
, SIOCGARP
, &arpReq
) != -1) {
324 * Solaris (at least 2.6/x86) does not use arp_ha.sa_family -
325 * it returns 00:00:00:00:00:00 for non-ethernet media
329 if (arpReq
.arp_ha
.sa_data
[0] == 0 &&
330 arpReq
.arp_ha
.sa_data
[1] == 0 &&
331 arpReq
.arp_ha
.sa_data
[2] == 0 &&
332 arpReq
.arp_ha
.sa_data
[3] == 0 &&
333 arpReq
.arp_ha
.sa_data
[4] == 0 && arpReq
.arp_ha
.sa_data
[5] == 0) {
338 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex
<<
339 std::setw(2) << (arpReq
.arp_ha
.sa_data
[0] & 0xff) << ":" <<
340 std::setw(2) << (arpReq
.arp_ha
.sa_data
[1] & 0xff) << ":" <<
341 std::setw(2) << (arpReq
.arp_ha
.sa_data
[2] & 0xff) << ":" <<
342 std::setw(2) << (arpReq
.arp_ha
.sa_data
[3] & 0xff) << ":" <<
343 std::setw(2) << (arpReq
.arp_ha
.sa_data
[4] & 0xff) << ":" <<
344 std::setw(2) << (arpReq
.arp_ha
.sa_data
[5] & 0xff));
346 set(arpReq
.arp_ha
.sa_data
, 6);
350 #elif _SQUID_FREEBSD_ || _SQUID_NETBSD_ || _SQUID_OPENBSD_ || _SQUID_DRAGONFLY_ || _SQUID_KFREEBSD_
356 char *lim
, *buf
, *next
;
358 struct rt_msghdr
*rtm
;
360 struct sockaddr_inarp
*sin
;
362 struct sockaddr_dl
*sdl
;
365 * Set up structures for ARP lookup with blank interface name
368 memset(&arpReq
, '\0', sizeof(arpReq
));
370 sa
= (struct sockaddr_in
*) &arpReq
.arp_pa
;
371 ipAddr
.GetSockAddr(*sa
);
373 /* Query ARP table */
382 mib
[4] = NET_RT_FLAGS
;
386 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0) {
387 debugs(28, 0, "Can't estimate ARP table size!");
392 if ((buf
= (char *)xmalloc(needed
)) == NULL
) {
393 debugs(28, 0, "Can't allocate temporary ARP table!");
398 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0) {
399 debugs(28, 0, "Can't retrieve ARP table!");
407 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
409 rtm
= (struct rt_msghdr
*) next
;
411 sin
= (struct sockaddr_inarp
*) (rtm
+ 1);
412 /*sdl = (struct sockaddr_dl *) (sin + 1); */
415 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
417 sdl
= (struct sockaddr_dl
*)((char *) sin
+ ROUNDUP(sin
->sin_len
));
419 if (ipAddr
== sin
->sin_addr
) {
422 arpReq
.arp_ha
.sa_len
= sizeof(struct sockaddr
);
423 arpReq
.arp_ha
.sa_family
= AF_UNSPEC
;
424 memcpy(arpReq
.arp_ha
.sa_data
, LLADDR(sdl
), sdl
->sdl_alen
);
431 if (arpReq
.arp_ha
.sa_data
[0] == 0 && arpReq
.arp_ha
.sa_data
[1] == 0 &&
432 arpReq
.arp_ha
.sa_data
[2] == 0 && arpReq
.arp_ha
.sa_data
[3] == 0 &&
433 arpReq
.arp_ha
.sa_data
[4] == 0 && arpReq
.arp_ha
.sa_data
[5] == 0) {
438 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex
<<
439 std::setw(2) << (arpReq
.arp_ha
.sa_data
[0] & 0xff) << ":" <<
440 std::setw(2) << (arpReq
.arp_ha
.sa_data
[1] & 0xff) << ":" <<
441 std::setw(2) << (arpReq
.arp_ha
.sa_data
[2] & 0xff) << ":" <<
442 std::setw(2) << (arpReq
.arp_ha
.sa_data
[3] & 0xff) << ":" <<
443 std::setw(2) << (arpReq
.arp_ha
.sa_data
[4] & 0xff) << ":" <<
444 std::setw(2) << (arpReq
.arp_ha
.sa_data
[5] & 0xff));
446 set(arpReq
.arp_ha
.sa_data
, 6);
451 DWORD dwNetTable
= 0;
453 DWORD ipNetTableLen
= 0;
455 PMIB_IPNETTABLE NetTable
= NULL
;
459 memset(&arpReq
, '\0', sizeof(arpReq
));
461 /* Get size of Windows ARP table */
462 if (GetIpNetTable(NetTable
, &ipNetTableLen
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
) {
463 debugs(28, 0, "Can't estimate ARP table size!");
468 /* Allocate space for ARP table and assign pointers */
469 if ((NetTable
= (PMIB_IPNETTABLE
)xmalloc(ipNetTableLen
)) == NULL
) {
470 debugs(28, 0, "Can't allocate temporary ARP table!");
475 /* Get actual ARP table */
476 if ((dwNetTable
= GetIpNetTable(NetTable
, &ipNetTableLen
, FALSE
)) != NO_ERROR
) {
477 debugs(28, 0, "Can't retrieve ARP table!");
483 /* Find MAC address from net table */
484 for (i
= 0 ; i
< NetTable
->dwNumEntries
; i
++) {
486 a
.s_addr
= NetTable
->table
[i
].dwAddr
;
487 if (c
== a
&& (NetTable
->table
[i
].dwType
> 2)) {
488 arpReq
.arp_ha
.sa_family
= AF_UNSPEC
;
489 memcpy(arpReq
.arp_ha
.sa_data
, NetTable
->table
[i
].bPhysAddr
, NetTable
->table
[i
].dwPhysAddrLen
);
495 if (arpReq
.arp_ha
.sa_data
[0] == 0 && arpReq
.arp_ha
.sa_data
[1] == 0 &&
496 arpReq
.arp_ha
.sa_data
[2] == 0 && arpReq
.arp_ha
.sa_data
[3] == 0 &&
497 arpReq
.arp_ha
.sa_data
[4] == 0 && arpReq
.arp_ha
.sa_data
[5] == 0) {
502 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex
<<
503 std::setw(2) << (arpReq
.arp_ha
.sa_data
[0] & 0xff) << ":" <<
504 std::setw(2) << (arpReq
.arp_ha
.sa_data
[1] & 0xff) << ":" <<
505 std::setw(2) << (arpReq
.arp_ha
.sa_data
[2] & 0xff) << ":" <<
506 std::setw(2) << (arpReq
.arp_ha
.sa_data
[3] & 0xff) << ":" <<
507 std::setw(2) << (arpReq
.arp_ha
.sa_data
[4] & 0xff) << ":" <<
508 std::setw(2) << (arpReq
.arp_ha
.sa_data
[5] & 0xff));
510 set(arpReq
.arp_ha
.sa_data
, 6);
515 debugs(28, 0, "ERROR: ARP / MAC / EUI-* operations not supported on this operating system.");
519 * Address was not found on any interface
521 debugs(28, 3, HERE
<< ipAddr
<< " NOT found");
527 /* ==== END EUI LOOKUP SUPPORT =============================================== */
529 #endif /* USE_SQUID_EUI */