3 * $Id: ACLARP.cc,v 1.29 2008/02/05 22:38:24 amosjeffries Exp $
5 * DEBUG: section 28 Access Control
6 * AUTHOR: Duane Wessels
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
35 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
40 #include <squid_windows.h>
43 #include "IPAddress.h"
50 IPAddress arp_pa
; /* protocol address */
52 struct sockaddr arp_ha
; /* hardware address */
53 int arp_flags
; /* flags */
59 #ifdef _SQUID_SOLARIS_
60 #include <sys/sockio.h>
62 #include <sys/sysctl.h>
65 #include <net/if_arp.h>
66 #include <sys/ioctl.h>
68 #include <net/if_dl.h>
70 #include <net/route.h>
72 #if defined(_SQUID_FREEBSD_) || defined(_SQUID_NETBSD_) || defined(_SQUID_OPENBSD_) || defined(_SQUID_DRAGONFLY_)
73 #include <net/if_arp.h>
75 #if HAVE_NETINET_IF_ETHER_H
76 #include <netinet/if_ether.h>
84 #error USE_ARP_ACL Not defined
86 static void aclParseArpList(SplayNode
<acl_arp_data
*> **curlist
);
87 static int decode_eth(const char *asc
, char *eth
);
88 static int aclMatchArp(SplayNode
<acl_arp_data
*> **dataptr
, IPAddress
&c
);
89 static SplayNode
<acl_arp_data
*>::SPLAYCMP aclArpCompare
;
90 static SplayNode
<acl_arp_data
*>::SPLAYWALKEE aclDumpArpListWalkee
;
92 ACL::Prototype
ACLARP::RegistryProtoype(&ACLARP::RegistryEntry_
, "arp");
94 ACLARP
ACLARP::RegistryEntry_("arp");
99 return new ACLARP(*this);
102 ACLARP::ACLARP (char const *theClass
) : data (NULL
), class_ (theClass
)
105 ACLARP::ACLARP (ACLARP
const & old
) : data (NULL
), class_ (old
.class_
)
107 /* we don't have copy constructors for the data yet */
114 data
->destroy(SplayNode
<acl_arp_data
*>::DefaultFree
);
118 ACLARP::typeString() const
124 ACLARP::empty () const
126 return data
->empty();
129 /* ==== BEGIN ARP ACL SUPPORT ============================================= */
132 * From: dale@server.ctam.bitmcnit.bryansk.su (Dale)
133 * To: wessels@nlanr.net
134 * Subject: Another Squid patch... :)
135 * Date: Thu, 04 Dec 1997 19:55:01 +0300
136 * ============================================================================
138 * Working on setting up a proper firewall for a network containing some
139 * Win'95 computers at our Univ, I've discovered that some smart students
140 * avoid the restrictions easily just changing their IP addresses in Win'95
141 * Contol Panel... It has been getting boring, so I took Squid-1.1.18
142 * sources and added a new acl type for hard-wired access control:
144 * acl <name> arp <Ethernet address> ...
148 * acl students arp 00:00:21:55:ed:22 00:00:21:ff:55:38
150 * NOTE: Linux code by David Luyer <luyer@ucs.uwa.edu.au>.
151 * Original (BSD-specific) code no longer works.
152 * Solaris code by R. Gancarz <radekg@solaris.elektrownia-lagisza.com.pl>
156 * Decode an ascii representation (asc) of an ethernet adress.
158 \param asc[in] ASCII representation of an ethernet (MAC) address
159 \param eth[out] Binary representation of the ethernet address
160 \retval 0 Conversion to binary failed. Invalid address
161 \retval 1 Conversion completed successfully
164 decode_eth(const char *asc
, char *eth
)
166 int a1
= 0, a2
= 0, a3
= 0, a4
= 0, a5
= 0, a6
= 0;
168 if (sscanf(asc
, "%x:%x:%x:%x:%x:%x", &a1
, &a2
, &a3
, &a4
, &a5
, &a6
) != 6) {
169 debugs(28, 0, "decode_eth: Invalid ethernet address '" << asc
<< "'");
170 return 0; /* This is not valid address */
173 eth
[0] = (u_char
) a1
;
174 eth
[1] = (u_char
) a2
;
175 eth
[2] = (u_char
) a3
;
176 eth
[3] = (u_char
) a4
;
177 eth
[4] = (u_char
) a5
;
178 eth
[5] = (u_char
) a6
;
183 aclParseArpData(const char *t
)
185 LOCAL_ARRAY(char, eth
, 256);
186 acl_arp_data
*q
= new acl_arp_data
;
187 debugs(28, 5, "aclParseArpData: " << t
);
189 if (sscanf(t
, "%[0-9a-fA-F:]", eth
) != 1) {
190 debugs(28, 0, "aclParseArpData: Bad ethernet address: '" << t
<< "'");
195 if (!decode_eth(eth
, q
->eth
)) {
196 debugs(28, 0, "" << cfg_filename
<< " line " << config_lineno
<< ": " << config_input_line
);
197 debugs(28, 0, "aclParseArpData: Ignoring invalid ARP acl entry: can't parse '" << eth
<< "'");
206 /*******************/
207 /* aclParseArpList */
208 /*******************/
212 aclParseArpList (&data
);
216 aclParseArpList(SplayNode
<acl_arp_data
*> **curlist
)
219 SplayNode
<acl_arp_data
*> **Top
= curlist
;
220 acl_arp_data
*q
= NULL
;
222 while ((t
= strtokFile())) {
223 if ((q
= aclParseArpData(t
)) == NULL
)
226 *Top
= (*Top
)->insert(q
, aclArpCompare
);
231 ACLARP::match(ACLChecklist
*checklist
)
233 /* IPv6 does not do ARP */
234 if(!checklist
->src_addr
.IsIPv4())
236 debugs(14, 3, "ACLARP::match: IPv4 Required for ARP Lookups. Skipping " << checklist
->src_addr
);
240 return aclMatchArp(&data
, checklist
->src_addr
);
247 aclMatchArp(SplayNode
<acl_arp_data
*> **dataptr
, IPAddress
&c
)
249 struct arpreq arpReq
;
250 struct sockaddr_in
*sa
= NULL
;
252 IPAddress ipAddr
= c
;
254 #if defined(_SQUID_LINUX_)
256 unsigned char ifbuffer
[sizeof(struct ifreq
) * 64];
262 SplayNode
<acl_arp_data
*> **Top
= dataptr
;
264 * The linux kernel 2.2 maintains per interface ARP caches and
265 * thus requires an interface name when doing ARP queries.
267 * The older 2.0 kernels appear to use a unified ARP cache,
268 * and require an empty interface name
270 * To support both, we attempt the lookup with a blank interface
271 * name first. If that does not succeed, the try each interface
276 * Set up structures for ARP lookup with blank interface name
278 memset(&arpReq
, '\0', sizeof(arpReq
));
280 sa
= (sockaddr_in
*)&arpReq
.arp_pa
;
281 ipAddr
.GetSockAddr(*sa
);
282 /* Query ARP table */
284 if (ioctl(HttpSockets
[0], SIOCGARP
, &arpReq
) != -1) {
285 /* Skip non-ethernet interfaces */
287 if (arpReq
.arp_ha
.sa_family
!= ARPHRD_ETHER
) {
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));
301 memcpy (X
.eth
, arpReq
.arp_ha
.sa_data
, 6);
302 *Top
= (*Top
)->splay(&X
, aclArpCompare
);
303 debugs(28, 3, "aclMatchArp: '" << c
<< "' " << (splayLastResult
? "NOT found" : "found"));
304 return (0 == splayLastResult
);
307 /* lookup list of interface names */
308 ifc
.ifc_len
= sizeof(ifbuffer
);
310 ifc
.ifc_buf
= (char *)ifbuffer
;
312 if (ioctl(HttpSockets
[0], SIOCGIFCONF
, &ifc
) < 0) {
313 debugs(28, 1, "Attempt to retrieve interface list failed: " << xstrerror());
317 if (ifc
.ifc_len
> (int)sizeof(ifbuffer
)) {
318 debugs(28, 1, "Interface list too long - " << ifc
.ifc_len
);
322 /* Attempt ARP lookup on each interface */
325 while (offset
< ifc
.ifc_len
) {
327 ifr
= (struct ifreq
*) (ifbuffer
+ offset
);
328 offset
+= sizeof(*ifr
);
329 /* Skip loopback and aliased interfaces */
331 if (0 == strncmp(ifr
->ifr_name
, "lo", 2))
334 if (NULL
!= strchr(ifr
->ifr_name
, ':'))
337 debugs(28, 4, "Looking up ARP address for " << c
<< " on " << ifr
->ifr_name
);
339 /* Set up structures for ARP lookup */
341 memset(&arpReq
, '\0', sizeof(arpReq
));
343 sa
= (sockaddr_in
*)&arpReq
.arp_pa
;
344 ipAddr
.GetSockAddr(*sa
);
346 strncpy(arpReq
.arp_dev
, ifr
->ifr_name
, sizeof(arpReq
.arp_dev
) - 1);
348 arpReq
.arp_dev
[sizeof(arpReq
.arp_dev
) - 1] = '\0';
350 /* Query ARP table */
351 if (-1 == ioctl(HttpSockets
[0], SIOCGARP
, &arpReq
)) {
353 * Query failed. Do not log failed lookups or "device
359 else if (ENODEV
== errno
)
362 debugs(28, 1, "ARP query failed: " << ifr
->ifr_name
<< ": " << xstrerror());
367 /* Skip non-ethernet interfaces */
368 if (arpReq
.arp_ha
.sa_family
!= ARPHRD_ETHER
)
371 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex
<<
372 std::setw(2) << (arpReq
.arp_ha
.sa_data
[0] & 0xff) << ":" <<
373 std::setw(2) << (arpReq
.arp_ha
.sa_data
[1] & 0xff) << ":" <<
374 std::setw(2) << (arpReq
.arp_ha
.sa_data
[2] & 0xff) << ":" <<
375 std::setw(2) << (arpReq
.arp_ha
.sa_data
[3] & 0xff) << ":" <<
376 std::setw(2) << (arpReq
.arp_ha
.sa_data
[4] & 0xff) << ":" <<
377 std::setw(2) << (arpReq
.arp_ha
.sa_data
[5] & 0xff) << " on "<<
378 std::setfill(' ') << ifr
->ifr_name
);
383 memcpy (X
.eth
, arpReq
.arp_ha
.sa_data
, 6);
385 *Top
= (*Top
)->splay(&X
, aclArpCompare
);
387 /* Return if match, otherwise continue to other interfaces */
388 if (0 == splayLastResult
) {
389 debugs(28, 3, "aclMatchArp: " << c
<< " found on " << ifr
->ifr_name
);
394 * Should we stop looking here? Can the same IP address
395 * exist on multiple interfaces?
399 #elif defined(_SQUID_SOLARIS_)
401 SplayNode
<acl_arp_data
*> **Top
= dataptr
;
404 * Set up structures for ARP lookup with blank interface name
407 memset(&arpReq
, '\0', sizeof(arpReq
));
409 sa
= (sockaddr_in
*)&arpReq
.arp_pa
;
410 ipAddr
.GetSockAddr(*sa
);
412 /* Query ARP table */
413 if (ioctl(HttpSockets
[0], SIOCGARP
, &arpReq
) != -1) {
415 * Solaris (at least 2.6/x86) does not use arp_ha.sa_family -
416 * it returns 00:00:00:00:00:00 for non-ethernet media
419 if (arpReq
.arp_ha
.sa_data
[0] == 0 &&
420 arpReq
.arp_ha
.sa_data
[1] == 0 &&
421 arpReq
.arp_ha
.sa_data
[2] == 0 &&
422 arpReq
.arp_ha
.sa_data
[3] == 0 &&
423 arpReq
.arp_ha
.sa_data
[4] == 0 && arpReq
.arp_ha
.sa_data
[5] == 0)
426 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex
<<
427 std::setw(2) << (arpReq
.arp_ha
.sa_data
[0] & 0xff) << ":" <<
428 std::setw(2) << (arpReq
.arp_ha
.sa_data
[1] & 0xff) << ":" <<
429 std::setw(2) << (arpReq
.arp_ha
.sa_data
[2] & 0xff) << ":" <<
430 std::setw(2) << (arpReq
.arp_ha
.sa_data
[3] & 0xff) << ":" <<
431 std::setw(2) << (arpReq
.arp_ha
.sa_data
[4] & 0xff) << ":" <<
432 std::setw(2) << (arpReq
.arp_ha
.sa_data
[5] & 0xff));
435 *Top
= (*Top
)->splay((acl_arp_data
*)&arpReq
.arp_ha
.sa_data
, aclArpCompare
);
437 debugs(28, 3, "aclMatchArp: '" << c
<< "' " << (splayLastResult
? "NOT found" : "found"));
439 return (0 == splayLastResult
);
442 #elif defined(_SQUID_FREEBSD_) || defined(_SQUID_NETBSD_) || defined(_SQUID_OPENBSD_) || defined(_SQUID_DRAGONFLY_)
444 SplayNode
<acl_arp_data
*> **Top
= dataptr
;
450 char *lim
, *buf
, *next
;
452 struct rt_msghdr
*rtm
;
454 struct sockaddr_inarp
*sin
;
456 struct sockaddr_dl
*sdl
;
459 * Set up structures for ARP lookup with blank interface name
462 memset(&arpReq
, '\0', sizeof(arpReq
));
464 ipAddr
.GetSockAddr(arpReq
.arp_pa
);
466 /* Query ARP table */
475 mib
[4] = NET_RT_FLAGS
;
479 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0) {
480 debugs(28, 0, "Can't estimate ARP table size!");
484 if ((buf
= (char *)xmalloc(needed
)) == NULL
) {
485 debugs(28, 0, "Can't allocate temporary ARP table!");
489 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0) {
490 debugs(28, 0, "Can't retrieve ARP table!");
497 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
499 rtm
= (struct rt_msghdr
*) next
;
501 sin
= (struct sockaddr_inarp
*) (rtm
+ 1);
502 /*sdl = (struct sockaddr_dl *) (sin + 1); */
505 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
507 sdl
= (struct sockaddr_dl
*)((char *) sin
+ ROUNDUP(sin
->sin_len
));
509 if (c
== sin
->sin_addr
) {
512 arpReq
.arp_ha
.sa_len
= sizeof(struct sockaddr
);
513 arpReq
.arp_ha
.sa_family
= AF_UNSPEC
;
514 memcpy(arpReq
.arp_ha
.sa_data
, LLADDR(sdl
), sdl
->sdl_alen
);
521 if (arpReq
.arp_ha
.sa_data
[0] == 0 && arpReq
.arp_ha
.sa_data
[1] == 0 &&
522 arpReq
.arp_ha
.sa_data
[2] == 0 && arpReq
.arp_ha
.sa_data
[3] == 0 &&
523 arpReq
.arp_ha
.sa_data
[4] == 0 && arpReq
.arp_ha
.sa_data
[5] == 0)
526 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex
<<
527 std::setw(2) << (arpReq
.arp_ha
.sa_data
[0] & 0xff) << ":" <<
528 std::setw(2) << (arpReq
.arp_ha
.sa_data
[1] & 0xff) << ":" <<
529 std::setw(2) << (arpReq
.arp_ha
.sa_data
[2] & 0xff) << ":" <<
530 std::setw(2) << (arpReq
.arp_ha
.sa_data
[3] & 0xff) << ":" <<
531 std::setw(2) << (arpReq
.arp_ha
.sa_data
[4] & 0xff) << ":" <<
532 std::setw(2) << (arpReq
.arp_ha
.sa_data
[5] & 0xff));
535 *Top
= (*Top
)->splay((acl_arp_data
*)&arpReq
.arp_ha
.sa_data
, aclArpCompare
);
537 debugs(28, 3, "aclMatchArp: '" << c
<< "' " << (splayLastResult
? "NOT found" : "found"));
539 return (0 == splayLastResult
);
541 #elif defined(_SQUID_WIN32_)
543 DWORD dwNetTable
= 0;
545 DWORD ipNetTableLen
= 0;
547 PMIB_IPNETTABLE NetTable
= NULL
;
551 SplayNode
<acl_arp_data
*> **Top
= dataptr
;
553 memset(&arpReq
, '\0', sizeof(arpReq
));
555 /* Get size of Windows ARP table */
556 if (GetIpNetTable(NetTable
, &ipNetTableLen
, FALSE
) != ERROR_INSUFFICIENT_BUFFER
) {
557 debugs(28, 0, "Can't estimate ARP table size!");
561 /* Allocate space for ARP table and assign pointers */
562 if ((NetTable
= (PMIB_IPNETTABLE
)xmalloc(ipNetTableLen
)) == NULL
) {
563 debugs(28, 0, "Can't allocate temporary ARP table!");
567 /* Get actual ARP table */
568 if ((dwNetTable
= GetIpNetTable(NetTable
, &ipNetTableLen
, FALSE
)) != NO_ERROR
) {
569 debugs(28, 0, "Can't retrieve ARP table!");
574 /* Find MAC address from net table */
575 for (i
= 0 ; i
< NetTable
->dwNumEntries
; i
++) {
577 a
.s_addr
= NetTable
->table
[i
].dwAddr
;
578 if (c
== a
&& (NetTable
->table
[i
].dwType
> 2)) {
579 arpReq
.arp_ha
.sa_family
= AF_UNSPEC
;
580 memcpy(arpReq
.arp_ha
.sa_data
, NetTable
->table
[i
].bPhysAddr
, NetTable
->table
[i
].dwPhysAddrLen
);
586 if (arpReq
.arp_ha
.sa_data
[0] == 0 && arpReq
.arp_ha
.sa_data
[1] == 0 &&
587 arpReq
.arp_ha
.sa_data
[2] == 0 && arpReq
.arp_ha
.sa_data
[3] == 0 &&
588 arpReq
.arp_ha
.sa_data
[4] == 0 && arpReq
.arp_ha
.sa_data
[5] == 0)
591 debugs(28, 4, "Got address "<< std::setfill('0') << std::hex
<<
592 std::setw(2) << (arpReq
.arp_ha
.sa_data
[0] & 0xff) << ":" <<
593 std::setw(2) << (arpReq
.arp_ha
.sa_data
[1] & 0xff) << ":" <<
594 std::setw(2) << (arpReq
.arp_ha
.sa_data
[2] & 0xff) << ":" <<
595 std::setw(2) << (arpReq
.arp_ha
.sa_data
[3] & 0xff) << ":" <<
596 std::setw(2) << (arpReq
.arp_ha
.sa_data
[4] & 0xff) << ":" <<
597 std::setw(2) << (arpReq
.arp_ha
.sa_data
[5] & 0xff));
600 *Top
= (*Top
)->splay((acl_arp_data
*)&arpReq
.arp_ha
.sa_data
, aclArpCompare
);
602 debugs(28, 3, "aclMatchArp: '" << c
<< "' " << (splayLastResult
? "NOT found" : "found"));
604 return (0 == splayLastResult
);
608 #error "ARP type ACL not supported on this operating system."
612 * Address was not found on any interface
614 debugs(28, 3, "aclMatchArp: " << c
<< " NOT found");
620 aclArpCompare(acl_arp_data
* const &a
, acl_arp_data
* const &b
)
622 return memcmp(a
->eth
, b
->eth
, 6);
626 aclDumpArpListWalkee(acl_arp_data
* const &node
, void *state
)
628 acl_arp_data
*arp
= node
;
630 snprintf(buf
, sizeof(buf
), "%02x:%02x:%02x:%02x:%02x:%02x",
631 arp
->eth
[0] & 0xff, arp
->eth
[1] & 0xff,
632 arp
->eth
[2] & 0xff, arp
->eth
[3] & 0xff,
633 arp
->eth
[4] & 0xff, arp
->eth
[5] & 0xff);
634 wordlistAdd((wordlist
**)state
, buf
);
641 data
->walk(aclDumpArpListWalkee
, &w
);
645 /* ==== END ARP ACL SUPPORT =============================================== */