]> git.ipfire.org Git - thirdparty/squid.git/blame - src/acl/Asn.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / acl / Asn.cc
CommitLineData
445d18a4 1
3841dd46 2/*
262a0e14 3 * $Id$
3841dd46 4 *
445d18a4
AJ
5 * DEBUG: section 53 AS Number handling
6 * AUTHOR: Duane Wessels, Kostas Anagnostakis
3841dd46 7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
19 *
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.
26ac0430 24 *
3841dd46 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.
26ac0430 29 *
3841dd46 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.
33 *
3841dd46 34 */
35
f7f3304a 36#include "squid-old.h"
8822ebee 37#include "mgr/Registration.h"
445d18a4
AJ
38#include "radix.h"
39#include "HttpRequest.h"
40#include "StoreClient.h"
41#include "Store.h"
42#include "acl/Acl.h"
3ad63615
AR
43#include "acl/Asn.h"
44#include "acl/Checklist.h"
445d18a4
AJ
45#include "acl/SourceAsn.h"
46#include "acl/DestinationAsn.h"
47#include "acl/DestinationIp.h"
48#include "HttpReply.h"
714e68b7 49#include "ipcache.h"
445d18a4
AJ
50#include "forward.h"
51#include "wordlist.h"
52
53#define WHOIS_PORT 43
54#define AS_REQBUF_SZ 4096
55
56/* BEGIN of definitions for radix tree entries */
57
58
59/* 32/128 bits address in memory with length */
60class m_ADDR
61{
62public:
63 uint8_t len;
b7ac5457 64 Ip::Address addr;
445d18a4 65
b7ac5457 66 m_ADDR() : len(sizeof(Ip::Address)) {};
445d18a4
AJ
67};
68
69/* END of definitions for radix tree entries */
70
71/* Head for ip to asn radix tree */
72
73struct squid_radix_node_head *AS_tree_head;
74
75/* explicit instantiation required for some systems */
76
77/// \cond AUTODOCS-IGNORE
78template cbdata_type CbDataList<int>::CBDATA_CbDataList;
79/// \endcond
80
81/**
82 * Structure for as number information. it could be simply
83 * a list but it's coded as a structure for future
84 * enhancements (e.g. expires)
85 */
86struct as_info {
87 CbDataList<int> *as_number;
88 time_t expires; /* NOTUSED */
89};
90
91struct ASState {
92 StoreEntry *entry;
93 store_client *sc;
94 HttpRequest *request;
95 int as_number;
96 int64_t offset;
97 int reqofs;
98 char reqbuf[AS_REQBUF_SZ];
99 bool dataRead;
100};
101
102/** entry into the radix tree */
103struct rtentry_t {
104 struct squid_radix_node e_nodes[2];
105 as_info *e_info;
106 m_ADDR e_addr;
107 m_ADDR e_mask;
108};
109
110static int asnAddNet(char *, int);
111
112static void asnCacheStart(int as);
113
114static STCB asHandleReply;
115
445d18a4 116
062e0b16
FC
117#if defined(__cplusplus)
118extern "C" {
119#endif
120
182fbea5
A
121 static int destroyRadixNode(struct squid_radix_node *rn, void *w);
122 static int printRadixNode(struct squid_radix_node *rn, void *sentry);
445d18a4 123
062e0b16
FC
124#if defined(__cplusplus)
125}
126#endif
127
445d18a4
AJ
128void asnAclInitialize(ACL * acls);
129
130static void asStateFree(void *data);
131
132static void destroyRadixNodeInfo(as_info *);
133
134static OBJH asnStats;
135
136/* PUBLIC */
137
138int
b7ac5457 139asnMatchIp(CbDataList<int> *data, Ip::Address &addr)
445d18a4
AJ
140{
141 struct squid_radix_node *rn;
142 as_info *e;
143 m_ADDR m_addr;
144 CbDataList<int> *a = NULL;
145 CbDataList<int> *b = NULL;
146
147 debugs(53, 3, "asnMatchIp: Called for " << addr );
148
149 if (AS_tree_head == NULL)
150 return 0;
151
152 if (addr.IsNoAddr())
153 return 0;
154
155 if (addr.IsAnyAddr())
156 return 0;
157
158 m_addr.addr = addr;
159
160 rn = squid_rn_match(&m_addr, AS_tree_head);
161
162 if (rn == NULL) {
163 debugs(53, 3, "asnMatchIp: Address not in as db.");
164 return 0;
165 }
166
167 debugs(53, 3, "asnMatchIp: Found in db!");
168 e = ((rtentry_t *) rn)->e_info;
169 assert(e);
170
171 for (a = data; a; a = a->next)
172 for (b = e->as_number; b; b = b->next)
173 if (a->element == b->element) {
174 debugs(53, 5, "asnMatchIp: Found a match!");
175 return 1;
176 }
177
178 debugs(53, 5, "asnMatchIp: AS not in as db.");
179 return 0;
180}
181
182void
183ACLASN::prepareForUse()
184{
185 for (CbDataList<int> *i = data; i; i = i->
186 next)
187 asnCacheStart(i->element);
188}
189
190static void
191asnRegisterWithCacheManager(void)
192{
8822ebee 193 Mgr::RegisterAction("asndb", "AS Number Database", asnStats, 0, 1);
445d18a4
AJ
194}
195
196/* initialize the radix tree structure */
197
198SQUIDCEXTERN int squid_max_keylen; /* yuck.. this is in lib/radix.c */
199
200CBDATA_TYPE(ASState);
201void
202asnInit(void)
203{
204 static int inited = 0;
205 squid_max_keylen = 40;
206 CBDATA_INIT_TYPE(ASState);
207
208 if (0 == inited++)
209 squid_rn_init();
210
211 squid_rn_inithead(&AS_tree_head, 8);
212
213 asnRegisterWithCacheManager();
214}
215
216void
217asnFreeMemory(void)
218{
219 squid_rn_walktree(AS_tree_head, destroyRadixNode, AS_tree_head);
220
221 destroyRadixNode((struct squid_radix_node *) 0, (void *) AS_tree_head);
222}
223
224static void
225asnStats(StoreEntry * sentry)
226{
227 storeAppendPrintf(sentry, "Address \tAS Numbers\n");
228 squid_rn_walktree(AS_tree_head, printRadixNode, sentry);
229}
230
231/* PRIVATE */
232
233
234static void
235asnCacheStart(int as)
236{
237 LOCAL_ARRAY(char, asres, 4096);
238 StoreEntry *e;
239 HttpRequest *req;
240 ASState *asState;
241 asState = cbdataAlloc(ASState);
242 asState->dataRead = 0;
243 debugs(53, 3, "asnCacheStart: AS " << as);
244 snprintf(asres, 4096, "whois://%s/!gAS%d", Config.as_whois_server, as);
245 asState->as_number = as;
246 req = HttpRequest::CreateFromUrl(asres);
247 assert(NULL != req);
248 asState->request = HTTPMSGLOCK(req);
249
250 if ((e = storeGetPublic(asres, METHOD_GET)) == NULL) {
251 e = storeCreateEntry(asres, asres, request_flags(), METHOD_GET);
252 asState->sc = storeClientListAdd(e, asState);
e83cc785 253 FwdState::fwdStart(Comm::ConnectionPointer(), e, asState->request);
445d18a4
AJ
254 } else {
255
256 e->lock();
257 asState->sc = storeClientListAdd(e, asState);
258 }
259
260 asState->entry = e;
261 asState->offset = 0;
262 asState->reqofs = 0;
263 StoreIOBuffer readBuffer (AS_REQBUF_SZ, asState->offset, asState->reqbuf);
264 storeClientCopy(asState->sc,
265 e,
266 readBuffer,
267 asHandleReply,
268 asState);
269}
270
271static void
272asHandleReply(void *data, StoreIOBuffer result)
273{
274 ASState *asState = (ASState *)data;
275 StoreEntry *e = asState->entry;
276 char *s;
277 char *t;
278 char *buf = asState->reqbuf;
279 int leftoversz = -1;
280
281 debugs(53, 3, "asHandleReply: Called with size=" << (unsigned int)result.length);
282 debugs(53, 3, "asHandleReply: buffer='" << buf << "'");
283
284 /* First figure out whether we should abort the request */
285
286 if (EBIT_TEST(e->flags, ENTRY_ABORTED)) {
287 asStateFree(asState);
288 return;
289 }
290
291 if (result.length == 0 && asState->dataRead) {
292 debugs(53, 3, "asHandleReply: Done: " << e->url() );
293 asStateFree(asState);
294 return;
295 } else if (result.flags.error) {
296 debugs(53, 1, "asHandleReply: Called with Error set and size=" << (unsigned int) result.length);
297 asStateFree(asState);
298 return;
299 } else if (HTTP_OK != e->getReply()->sline.status) {
300 debugs(53, 1, "WARNING: AS " << asState->as_number << " whois request failed");
301 asStateFree(asState);
302 return;
303 }
304
305 /*
306 * Next, attempt to parse our request
307 * Remembering that the actual buffer size is retsize + reqofs!
308 */
309 s = buf;
310
311 while ((size_t)(s - buf) < result.length + asState->reqofs && *s != '\0') {
312 while (*s && xisspace(*s))
313 s++;
314
315 for (t = s; *t; t++) {
316 if (xisspace(*t))
317 break;
318 }
319
320 if (*t == '\0') {
321 /* oof, word should continue on next block */
322 break;
323 }
324
325 *t = '\0';
326 debugs(53, 3, "asHandleReply: AS# " << s << " (" << asState->as_number << ")");
327 asnAddNet(s, asState->as_number);
328 s = t + 1;
329 asState->dataRead = 1;
330 }
331
332 /*
333 * Next, grab the end of the 'valid data' in the buffer, and figure
334 * out how much data is left in our buffer, which we need to keep
335 * around for the next request
336 */
337 leftoversz = (asState->reqofs + result.length) - (s - buf);
338
339 assert(leftoversz >= 0);
340
341 /*
342 * Next, copy the left over data, from s to s + leftoversz to the
343 * beginning of the buffer
344 */
41d00cd3 345 memmove(buf, s, leftoversz);
445d18a4
AJ
346
347 /*
348 * Next, update our offset and reqofs, and kick off a copy if required
349 */
350 asState->offset += result.length;
351
352 asState->reqofs = leftoversz;
353
354 debugs(53, 3, "asState->offset = " << asState->offset);
355
356 if (e->store_status == STORE_PENDING) {
357 debugs(53, 3, "asHandleReply: store_status == STORE_PENDING: " << e->url() );
358 StoreIOBuffer tempBuffer (AS_REQBUF_SZ - asState->reqofs,
359 asState->offset,
360 asState->reqbuf + asState->reqofs);
361 storeClientCopy(asState->sc,
362 e,
363 tempBuffer,
364 asHandleReply,
365 asState);
366 } else {
367 StoreIOBuffer tempBuffer;
368 debugs(53, 3, "asHandleReply: store complete, but data received " << e->url() );
369 tempBuffer.offset = asState->offset;
370 tempBuffer.length = AS_REQBUF_SZ - asState->reqofs;
371 tempBuffer.data = asState->reqbuf + asState->reqofs;
372 storeClientCopy(asState->sc,
373 e,
374 tempBuffer,
375 asHandleReply,
376 asState);
377 }
378}
379
380static void
381asStateFree(void *data)
382{
383 ASState *asState = (ASState *)data;
384 debugs(53, 3, "asnStateFree: " << asState->entry->url() );
385 storeUnregister(asState->sc, asState->entry, asState);
386 asState->entry->unlock();
387 HTTPMSGUNLOCK(asState->request);
388 cbdataFree(asState);
389}
390
391
392/**
393 * add a network (addr, mask) to the radix tree, with matching AS number
394 */
395static int
396asnAddNet(char *as_string, int as_number)
397{
398 rtentry_t *e;
399
400 struct squid_radix_node *rn;
401 CbDataList<int> **Tail = NULL;
402 CbDataList<int> *q = NULL;
403 as_info *asinfo = NULL;
404
b7ac5457
AJ
405 Ip::Address mask;
406 Ip::Address addr;
445d18a4
AJ
407 char *t;
408 int bitl;
409
410 t = strchr(as_string, '/');
411
412 if (t == NULL) {
413 debugs(53, 3, "asnAddNet: failed, invalid response from whois server.");
414 return 0;
415 }
416
417 *t = '\0';
418 addr = as_string;
419 bitl = atoi(t + 1);
420
421 if (bitl < 0)
422 bitl = 0;
423
424 // INET6 TODO : find a better way of identifying the base IPA family for mask than this.
425 t = strchr(as_string, '.');
426
427 // generate Netbits Format Mask
428 mask.SetNoAddr();
429 mask.ApplyMask(bitl, (t!=NULL?AF_INET:AF_INET6) );
430
431 debugs(53, 3, "asnAddNet: called for " << addr << "/" << mask );
432
433 e = (rtentry_t *)xmalloc(sizeof(rtentry_t));
434
435 memset(e, '\0', sizeof(rtentry_t));
436
437 e->e_addr.addr = addr;
438
439 e->e_mask.addr = mask;
440
441 rn = squid_rn_lookup(&e->e_addr, &e->e_mask, AS_tree_head);
442
443 if (rn != NULL) {
444 asinfo = ((rtentry_t *) rn)->e_info;
445
446 if (asinfo->as_number->find(as_number)) {
447 debugs(53, 3, "asnAddNet: Ignoring repeated network '" << addr << "/" << bitl << "' for AS " << as_number);
448 } else {
449 debugs(53, 3, "asnAddNet: Warning: Found a network with multiple AS numbers!");
450
451 for (Tail = &asinfo->as_number; *Tail; Tail = &(*Tail)->next);
452 q = new CbDataList<int> (as_number);
453
454 *(Tail) = q;
455
456 e->e_info = asinfo;
457 }
458 } else {
459 q = new CbDataList<int> (as_number);
460 asinfo = (as_info *)xmalloc(sizeof(as_info));
461 asinfo->as_number = q;
462 rn = squid_rn_addroute(&e->e_addr, &e->e_mask, AS_tree_head, e->e_nodes);
463 rn = squid_rn_match(&e->e_addr, AS_tree_head);
464 assert(rn != NULL);
465 e->e_info = asinfo;
466 }
467
468 if (rn == 0) { /* assert might expand to nothing */
469 xfree(asinfo);
470 delete q;
471 xfree(e);
472 debugs(53, 3, "asnAddNet: Could not add entry.");
473 return 0;
474 }
475
476 e->e_info = asinfo;
477 return 1;
478}
479
480static int
481destroyRadixNode(struct squid_radix_node *rn, void *w)
482{
483
484 struct squid_radix_node_head *rnh = (struct squid_radix_node_head *) w;
485
486 if (rn && !(rn->rn_flags & RNF_ROOT)) {
487 rtentry_t *e = (rtentry_t *) rn;
488 rn = squid_rn_delete(rn->rn_key, rn->rn_mask, rnh);
489
490 if (rn == 0)
491 debugs(53, 3, "destroyRadixNode: internal screwup");
492
493 destroyRadixNodeInfo(e->e_info);
494
495 xfree(rn);
496 }
497
498 return 1;
499}
500
501static void
502destroyRadixNodeInfo(as_info * e_info)
503{
504 CbDataList<int> *prev = NULL;
505 CbDataList<int> *data = e_info->as_number;
506
507 while (data) {
508 prev = data;
509 data = data->next;
510 delete prev;
511 }
512
513 delete data;
514}
515
516static int
517printRadixNode(struct squid_radix_node *rn, void *_sentry)
518{
519 StoreEntry *sentry = (StoreEntry *)_sentry;
520 rtentry_t *e = (rtentry_t *) rn;
521 CbDataList<int> *q;
522 as_info *asinfo;
523 char buf[MAX_IPSTRLEN];
b7ac5457
AJ
524 Ip::Address addr;
525 Ip::Address mask;
445d18a4
AJ
526
527 assert(e);
528 assert(e->e_info);
529 addr = e->e_addr.addr;
530 mask = e->e_mask.addr;
531 storeAppendPrintf(sentry, "%s/%d\t",
532 addr.NtoA(buf, MAX_IPSTRLEN),
533 mask.GetCIDR() );
534 asinfo = e->e_info;
535 assert(asinfo->as_number);
536
537 for (q = asinfo->as_number; q; q = q->next)
538 storeAppendPrintf(sentry, " %d", q->element);
539
540 storeAppendPrintf(sentry, "\n");
541
542 return 0;
543}
544
545ACLASN::~ACLASN()
546{
547 if (data)
548 delete data;
549}
550
551bool
552
b7ac5457 553ACLASN::match(Ip::Address toMatch)
445d18a4
AJ
554{
555 return asnMatchIp(data, toMatch);
556}
557
558wordlist *
559ACLASN::dump()
560{
561 wordlist *W = NULL;
562 char buf[32];
563 CbDataList<int> *ldata = data;
564
565 while (ldata != NULL) {
566 snprintf(buf, sizeof(buf), "%d", ldata->element);
567 wordlistAdd(&W, buf);
568 ldata = ldata->next;
569 }
570
571 return W;
572}
573
574bool
575ACLASN::empty () const
576{
577 return data == NULL;
578}
579
580void
581ACLASN::parse()
582{
583 CbDataList<int> **curlist = &data;
584 CbDataList<int> **Tail;
585 CbDataList<int> *q = NULL;
586 char *t = NULL;
587
588 for (Tail = curlist; *Tail; Tail = &((*Tail)->next));
589 while ((t = strtokFile())) {
590 q = new CbDataList<int> (atoi(t));
591 *(Tail) = q;
592 Tail = &q->next;
593 }
594}
595
b7ac5457 596ACLData<Ip::Address> *
445d18a4
AJ
597ACLASN::clone() const
598{
599 if (data)
600 fatal ("cloning of ACLASN not implemented");
601
602 return new ACLASN(*this);
603}
604
605/* explicit template instantiation required for some systems */
606
b7ac5457 607template class ACLStrategised<Ip::Address>;
445d18a4
AJ
608
609ACL::Prototype ACLASN::SourceRegistryProtoype(&ACLASN::SourceRegistryEntry_, "src_as");
610
b7ac5457 611ACLStrategised<Ip::Address> ACLASN::SourceRegistryEntry_(new ACLASN, ACLSourceASNStrategy::Instance(), "src_as");
445d18a4
AJ
612
613ACL::Prototype ACLASN::DestinationRegistryProtoype(&ACLASN::DestinationRegistryEntry_, "dst_as");
614
b7ac5457 615ACLStrategised<Ip::Address> ACLASN::DestinationRegistryEntry_(new ACLASN, ACLDestinationASNStrategy::Instance(), "dst_as");
445d18a4
AJ
616
617int
b7ac5457 618ACLSourceASNStrategy::match (ACLData<Ip::Address> * &data, ACLFilledChecklist *checklist)
445d18a4
AJ
619{
620 return data->match(checklist->src_addr);
621}
622
623ACLSourceASNStrategy *
624ACLSourceASNStrategy::Instance()
625{
626 return &Instance_;
627}
628
629ACLSourceASNStrategy ACLSourceASNStrategy::Instance_;
630
631
632int
633ACLDestinationASNStrategy::match (ACLData<MatchType> * &data, ACLFilledChecklist *checklist)
634{
635 const ipcache_addrs *ia = ipcache_gethostbyname(checklist->request->GetHost(), IP_LOOKUP_IF_MISS);
636
637 if (ia) {
638 for (int k = 0; k < (int) ia->count; k++) {
639 if (data->match(ia->in_addrs[k]))
640 return 1;
641 }
642
643 return 0;
644
645 } else if (!checklist->request->flags.destinationIPLookedUp()) {
646 /* No entry in cache, lookup not attempted */
647 /* XXX FIXME: allow accessing the acl name here */
648 debugs(28, 3, "asnMatchAcl: Can't yet compare '" << "unknown" /*name*/ << "' ACL for '" << checklist->request->GetHost() << "'");
649 checklist->changeState (DestinationIPLookup::Instance());
650 } else {
b7ac5457 651 Ip::Address noaddr;
445d18a4
AJ
652 noaddr.SetNoAddr();
653 return data->match(noaddr);
654 }
655
656 return 0;
657}
658
659ACLDestinationASNStrategy *
660ACLDestinationASNStrategy::Instance()
661{
662 return &Instance_;
663}
3841dd46 664
445d18a4 665ACLDestinationASNStrategy ACLDestinationASNStrategy::Instance_;