-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhcp.c
6501 lines (5389 loc) · 165 KB
/
dhcp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: c; c-basic-offset: 2 -*- */
/*
* Copyright (C) 2007-2012 David Bird (Coova Technologies) <support@coova.com>
* Copyright (C) 2003, 2004, 2005, 2006 Mondru AB.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include "chilli.h"
#ifdef ENABLE_MODULES
#include "chilli_module.h"
#endif
#include "debug.h"
const uint32_t DHCP_OPTION_MAGIC = 0x63825363;
static uint8_t bmac[PKT_ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
static uint8_t nmac[PKT_ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
static int connections = 0;
extern struct ippool_t *ippool;
#define MAX_G_XID 20
static int g_xid[MAX_G_XID];
static int g_xid_idx = 0;
struct dhcp_ctx {
struct dhcp_t *parent;
int idx;
};
#ifdef ENABLE_CLUSTER
int chilli_peer_count = 0;
struct chilli_peer * chilli_peers = 0;
struct chilli_peer * get_chilli_peer(int id) {
if (id < 0) id = _options.peerid;
if (_options.peerid >= 8) {
log_err(0, "Valid PEERID is 0-7!");
exit(-1);
}
if (!chilli_peers) {
/* initialize */
struct chilli_peer *p;
chilli_peers = (struct chilli_peer *)calloc(8, sizeof(struct chilli_peer));
p = chilli_peers + _options.peerid;
memcpy(p->mac, dhcp_nexthop(dhcp), PKT_ETH_ALEN);
memcpy(&p->addr, &_options.uamlisten, sizeof(_options.uamlisten));
if (_options.peerid == 0)
p->state = PEER_STATE_ACTIVE;
else
p->state = PEER_STATE_STANDBY;
}
if (id < 8)
return chilli_peers + id;
return 0;
}
void print_peers(bstring s) {
time_t now = mainclock_now();
char line[512];
int i;
for (i=0; i<8; i++) {
struct chilli_peer *peer = chilli_peers + i;
int age = (int)(now - peer->last_update);
char *state = "";
switch(peer->state) {
case PEER_STATE_ACTIVE:
state = "Active";
break;
case PEER_STATE_STANDBY:
state = "Standby";
break;
case PEER_STATE_OFFLINE:
state = "Offline";
age = 0;
break;
case PEER_STATE_ADMCMD:
state = "AdminCmd";
break;
case PEER_STATE_MODULE:
state = "Module";
break;
}
safe_snprintf(line, sizeof(line),
"Peer %d %-4s %.2x:%.2x:%.2x:%.2x:%.2x:%.2x "
"/ %-16s %-8s %d sec",
i, _options.peerid == i ? "(*)" : "",
peer->mac[0],peer->mac[1],peer->mac[2],
peer->mac[3],peer->mac[4],peer->mac[5],
inet_ntoa(peer->addr), state, age);
bcatcstr(s, line);
bcatcstr(s, "\n");
}
}
int dhcp_send_chilli(uint8_t *pkt, size_t len) {
return 0;
}
static
int dhcp_sendCHILLI(uint8_t type, struct in_addr *addr, uint8_t *mac) {
EVP_CIPHER_CTX ctx;
uint8_t packet[1500];
struct pkt_ethhdr_t *packet_ethh;
struct pkt_chillihdr_t chilli_hdr;
unsigned char *outbuf;
int datalen = 0;
int olen = 0;
int tlen = 0;
int ret = -1;
unsigned char iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
if (_options.peerkey == 0)
_options.peerkey = "hello!";
/* Get packet default values */
memset(packet, 0, sizeof(packet));
packet_ethh = pkt_ethhdr(packet);
outbuf = (unsigned char *)chilli_ethhdr(packet);
packet_ethh->prot = htons(PKT_ETH_PROTO_CHILLI);
memcpy(packet_ethh->dst, bmac, PKT_ETH_ALEN);
memcpy(packet_ethh->src, dhcp_nexthop(dhcp), PKT_ETH_ALEN);
chilli_hdr.from = _options.peerid;
chilli_hdr.type = type;
if (!addr || !mac) {
struct chilli_peer *p = get_chilli_peer(-1);
addr = &p->addr;
mac = p->mac;
chilli_hdr.state = p->state;
}
memcpy(&chilli_hdr.addr, addr, sizeof(*addr));
memcpy(chilli_hdr.mac, mac, PKT_ETH_ALEN);
EVP_CIPHER_CTX_init(&ctx);
EVP_EncryptInit(&ctx, EVP_bf_cbc(),
(const unsigned char *)_options.peerkey, iv);
if (EVP_EncryptUpdate (&ctx, outbuf, &olen,
(const unsigned char *) &chilli_hdr,
sizeof(chilli_hdr)) != 1) {
log_err(errno, "CHILLI: peer %d error in encrypt update",
_options.peerid);
} else {
datalen += olen;
if (EVP_EncryptFinal (&ctx, outbuf + olen, &tlen) != 1) {
log_err(errno, "CHILLI: peer %d error in encrypt final",
_options.peerid);
} else {
datalen += tlen;
log_dbg("CHILLI: peer %d sending %d bytes",
_options.peerid, datalen);
ret = dhcp_send(dhcp, -1, bmac, packet,
sizeofeth(packet) + datalen);
}
}
EVP_CIPHER_CTX_cleanup(&ctx);
return ret;
}
#endif
#ifdef ENABLE_GARDENACCOUNTING
static int other_data(struct dhcp_conn_t *conn,
struct pkt_iphdr_t *iph, int len, int dst ) {
if (conn && _options.uamgardendata && _options.uamotherdata) {
struct app_conn_t *appconn = dhcp_get_appconn_pkt(conn, iph, dst);
if (appconn) {
extern struct app_conn_t admin_session;
if (!len && iph)
len = ntohs(iph->tot_len);
if (_options.swapoctets) {
if (dst)
appconn->s_state.other_output_octets +=len;
else
appconn->s_state.other_input_octets +=len;
if (admin_session.s_state.authenticated) {
if (dst)
admin_session.s_state.other_output_octets+=len;
else
admin_session.s_state.other_input_octets+=len;
}
} else {
if (dst)
appconn->s_state.other_input_octets +=len;
else
appconn->s_state.other_output_octets +=len;
if (admin_session.s_state.authenticated) {
if (dst)
admin_session.s_state.other_input_octets+=len;
else
admin_session.s_state.other_output_octets+=len;
}
}
}
}
return 0;
}
#define OTHER_RECEIVED(conn,iph) other_data((conn), (iph), 0, 0)
#define OTHER_RECEIVED_LEN(conn,len) other_data((conn), 0, (len), 0)
#define OTHER_SENDING(conn,iph) other_data((conn), (iph), 0, 1)
#define OTHER_SENDING_LEN(conn,len) other_data((conn), 0, (len), 1)
#else
#define OTHER_RECEIVED(conn,iph)
#define OTHER_RECEIVED_LEN(conn,iph)
#define OTHER_SENDING(conn,iph)
#define OTHER_SENDING_LEN(conn,len)
#endif
struct app_conn_t *
dhcp_get_appconn_ip(struct dhcp_conn_t *conn, struct in_addr *dst) {
struct app_conn_t *appconn = 0;
struct ippoolm_t *ipm = 0;
#if(_debug_ > 1)
log_dbg("Looking up appconn for %s", inet_ntoa(*dst));
#endif
if (ippool_getip(ippool, &ipm, dst)) {
log_dbg("No ip assigned for %s", inet_ntoa(*dst));
return 0;
}
if (!ipm) {
log_dbg("unknown ip");
return 0;
}
if ((appconn = (struct app_conn_t *)ipm->peer) == NULL) {
if (chilli_getconn(&appconn, dst->s_addr, 0, 0)) {
if (conn && chilli_connect(&appconn, conn)) {
log_err(0, "chilli_connect()");
return 0;
}
}
}
return appconn;
}
struct app_conn_t *
dhcp_get_appconn_pkt(struct dhcp_conn_t *conn,
struct pkt_iphdr_t *iph,
char is_dst) {
#ifdef ENABLE_LAYER3
switch (conn->authstate) {
case DHCP_AUTH_ROUTER:
{
struct in_addr dst;
dst.s_addr = ((is_dst) ?
iph->daddr :
iph->saddr);
return dhcp_get_appconn_ip(conn, &dst);
}
}
#endif
/* log_dbg("Layer2 appconn"); */
return (struct app_conn_t *) conn ? conn->peer : 0;
}
static struct app_conn_t *
dhcp_get_appconn_addr(struct dhcp_conn_t *conn, struct in_addr *dst) {
#ifdef ENABLE_LAYER3
switch (conn->authstate) {
case DHCP_AUTH_ROUTER:
return dhcp_get_appconn_ip(conn, dst);
}
#endif
/*log_dbg("Layer2 appconn");*/
return (struct app_conn_t *) conn->peer;
}
/**
* dhcp_sendGARP()
* Send Gratuitous ARP message to network
* http://wiki.wireshark.org/Gratuitous_ARP
**/
int
dhcp_sendGARP(struct dhcp_t *this, int idx) {
uint8_t packet[1500];
struct pkt_ethhdr_t *packet_ethh;
struct arp_packet_t *packet_arp;
memset(packet, 0, sizeof(packet));
packet_ethh = pkt_ethhdr(packet);
packet_arp = pkt_arppkt(packet);
/* ARP Payload */
packet_arp->hrd = htons(DHCP_HTYPE_ETH);
packet_arp->pro = htons(PKT_ETH_PROTO_IP);
packet_arp->hln = PKT_ETH_ALEN;
packet_arp->pln = PKT_IP_ALEN;
packet_arp->op = htons(DHCP_ARP_REPLY);
/* Source address */
memcpy(packet_arp->sha, dhcp_nexthop(this), PKT_ETH_ALEN);
memcpy(packet_arp->spa, &_options.dhcplisten.s_addr, PKT_IP_ALEN);
/* Target address */
memcpy(packet_arp->tha, bmac, PKT_ETH_ALEN);
memcpy(packet_arp->tpa, &_options.dhcplisten.s_addr, PKT_IP_ALEN);
log_dbg("GARP: Replying to broadcast");
/* Ethernet header */
memcpy(packet_ethh->dst, bmac, PKT_ETH_ALEN);
memcpy(packet_ethh->src, dhcp_nexthop(this), PKT_ETH_ALEN);
packet_ethh->prot = htons(PKT_ETH_PROTO_ARP);
return dhcp_send(this, idx, bmac, packet, sizeofarp(packet));
}
void dhcp_authorize_mac(struct dhcp_t *this, uint8_t *hwaddr,
struct session_params *params) {
struct dhcp_conn_t *conn;
if (!dhcp_hashget(this, &conn, hwaddr)) {
struct app_conn_t * appconn = (struct app_conn_t*) conn->peer;
if (appconn) {
memcpy(&appconn->s_params, params,
sizeof(struct session_params));
session_param_defaults(&appconn->s_params);
dnprot_accept(appconn);
}
}
}
void dhcp_release_mac(struct dhcp_t *this, uint8_t *hwaddr, int term_cause) {
struct dhcp_conn_t *conn;
if (!dhcp_hashget(this, &conn, hwaddr)) {
if (conn->authstate == DHCP_AUTH_DROP &&
term_cause != RADIUS_TERMINATE_CAUSE_ADMIN_RESET)
return;
dhcp_freeconn(conn, term_cause);
}
}
#if (0) /* ENABLE_TCPRESET */
void dhcp_reset_tcp_mac(struct dhcp_t *this, uint8_t *hwaddr) {
struct dhcp_conn_t *conn;
if (!dhcp_hashget(this, &conn, hwaddr)) {
int n;
for (n=0; n<DHCP_DNAT_MAX; n++) {
if (conn->dnat[n].dst_ip) {
uint8_t *ip = (uint8_t*)&conn->dnat[n].dst_ip;
log_dbg("Resetting dst %d.%d.%d.%d:%d",
ip[0], ip[1], ip[2], ip[3], conn->dnat[n].dst_port);
}
if (conn->dnat[n].src_ip) {
uint8_t *ip = (uint8_t*)&conn->dnat[n].src_ip;
log_dbg("Resetting src %d.%d.%d.%d:%d",
ip[0], ip[1], ip[2], ip[3], conn->dnat[n].src_port);
}
}
}
}
#endif
void dhcp_block_mac(struct dhcp_t *this, uint8_t *hwaddr) {
struct dhcp_conn_t *conn;
if (!dhcp_hashget(this, &conn, hwaddr)) {
struct app_conn_t *appconn = (struct app_conn_t *)conn->peer;
conn->authstate = DHCP_AUTH_DROP;
if (appconn) appconn->dnprot = DNPROT_NULL;
}
}
int dhcp_net_send(struct _net_interface *netif, unsigned char *hismac,
uint8_t *packet, size_t length) {
#if defined(__linux__)
#ifndef USING_MMAP
if (hismac) {
netif->dest.sll_halen = PKT_ETH_ALEN;
memcpy(netif->dest.sll_addr, hismac, PKT_ETH_ALEN);
} else {
netif->dest.sll_halen = 0;
memset(netif->dest.sll_addr, 0, sizeof(netif->dest.sll_addr));
}
#endif
#if(_debug_ > 1)
log_dbg("dhcp_send() len=%d", length);
#endif
return net_write_eth(netif, packet, length, &netif->dest);
#elif defined (__FreeBSD__) || defined (__APPLE__) || defined (__OpenBSD__) || defined (__NetBSD__)
if (safe_write(netif->fd, packet, length) < 0) {
log_err(errno, "write() failed");
return -1;
}
#endif
return -1;
}
int dhcp_send(struct dhcp_t *this, int idx,
unsigned char *hismac, uint8_t *packet, size_t length) {
net_interface *iface = 0;
if (_options.tcpwin)
pkt_shape_tcpwin(pkt_iphdr(packet), _options.tcpwin);
if (_options.tcpmss)
pkt_shape_tcpmss(packet, &length);
#ifdef ENABLE_MULTILAN
if (idx < 0) {
int i, ret = -1;
for (i=0; i < MAX_RAWIF && this->rawif[i].fd; i++)
ret = dhcp_net_send(&this->rawif[i], hismac, packet, length);
return ret;
}
iface = &this->rawif[idx];
#else
iface = &this->rawif[0];
#endif
return dhcp_net_send(iface, hismac, packet, length);
}
/**
* dhcp_hash()
* Generates a 32 bit hash based on a mac address
**/
uint32_t dhcp_hash(uint8_t *hwaddr) {
return lookup(hwaddr, PKT_ETH_ALEN, 0);
}
/**
* dhcp_hashinit()
* Initialises hash tables
**/
int dhcp_hashinit(struct dhcp_t *this, int listsize) {
/* Determine hashlog */
for ((this)->hashlog = 0;
((1 << (this)->hashlog) < listsize);
(this)->hashlog++);
/* Determine hashsize */
(this)->hashsize = 1 << (this)->hashlog;
(this)->hashmask = (this)->hashsize -1;
/* Allocate hash table */
if (!((this)->hash =
calloc(sizeof(struct dhcp_conn_t *), (this)->hashsize))) {
/* Failed to allocate memory for hash members */
return -1;
}
log_dbg("hash table size %d (%d)", this->hashsize, listsize);
return 0;
}
/**
* dhcp_hashadd()
* Adds a connection to the hash table
**/
int dhcp_hashadd(struct dhcp_t *this, struct dhcp_conn_t *conn) {
uint32_t hash;
struct dhcp_conn_t *p = (struct dhcp_conn_t *)0;
struct dhcp_conn_t *p_prev = NULL;
if (conn == (struct dhcp_conn_t *)0) {
syslog(LOG_ERR, "%s: Bad input param conn(%p)", __FUNCTION__, conn);
return -1;
}
/* Insert into hash table */
hash = dhcp_hash(conn->hismac) & this->hashmask;
for (p = this->hash[hash]; p; p = p->nexthash)
p_prev = p;
if (!p_prev)
this->hash[hash] = conn;
else
p_prev->nexthash = conn;
return 0; /* Always OK to insert */
}
/**
* dhcp_hashdel()
* Removes a connection from the hash table
**/
int dhcp_hashdel(struct dhcp_t *this, struct dhcp_conn_t *conn) {
uint32_t hash;
struct dhcp_conn_t *p;
struct dhcp_conn_t *p_prev = NULL;
/* Find in hash table */
hash = dhcp_hash(conn->hismac) & this->hashmask;
for (p = this->hash[hash]; p; p = p->nexthash) {
if (p == conn) {
break;
}
p_prev = p;
}
if (p != conn) {
syslog(LOG_ERR, "trying to remove connection not in hash table");
return -1;
}
if (!p_prev)
this->hash[hash] = p->nexthash;
else
p_prev->nexthash = p->nexthash;
return 0;
}
#ifdef ENABLE_IEEE8021Q
int vlanupdate_script(struct dhcp_conn_t *conn, char* script,
uint16_t oldtag) {
int status;
uint16_t tag;
if ((status = chilli_fork(CHILLI_PROC_SCRIPT, script)) < 0) {
log_err(errno, "forking %s", script);
return 0;
}
if (status > 0) { /* Parent */
return 0;
}
tag = ntohs(conn->tag8021q & PKT_8021Q_MASK_VID);
oldtag = ntohs(oldtag & PKT_8021Q_MASK_VID);
set_env("DEV", VAL_STRING, tun(tun, 0).devname, 0);
set_env("ADDR", VAL_IN_ADDR, &conn->ourip, 0);
set_env("FRAMED_IP_ADDRESS", VAL_IN_ADDR, &conn->hisip, 0);
set_env("CALLING_STATION_ID", VAL_MAC_ADDR, conn->hismac, 0);
set_env("CALLED_STATION_ID", VAL_MAC_ADDR, dhcp_nexthop(dhcp), 0);
set_env("NAS_ID", VAL_STRING, _options.radiusnasid, 0);
set_env("8021Q_TAG", VAL_USHORT, &tag, 0);
set_env("OLD_8021Q_TAG", VAL_USHORT, &oldtag, 0);
if (execl(
#ifdef ENABLE_CHILLISCRIPT
SBINDIR "/chilli_script", SBINDIR "/chilli_script", _options.binconfig,
#else
script,
#endif
script, (char *) 0) != 0) {
log_err(errno, "exec %s failed", script);
}
exit(0);
}
void dhcp_checktag(struct dhcp_conn_t *conn, uint8_t *pack) {
if (_options.ieee8021q && is_8021q(pack)) {
uint16_t tag = get8021q(pack);
struct app_conn_t * appconn = conn->peer;
if ((tag & PKT_8021Q_MASK_VID) !=
(conn->tag8021q & PKT_8021Q_MASK_VID)) {
uint16_t oldtag = conn->tag8021q;
conn->tag8021q = tag;
log_dbg("IEEE 802.1Q: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x on VLAN %d",
conn->hismac[0], conn->hismac[1], conn->hismac[2],
conn->hismac[3], conn->hismac[4], conn->hismac[5],
(int)ntohs(tag & PKT_8021Q_MASK_VID));
if (_options.vlanupdate) {
vlanupdate_script(conn, _options.vlanupdate, oldtag);
}
#ifdef ENABLE_LOCATION
if (_options.vlanlocation && appconn) {
char vid[16];
safe_snprintf(vid, sizeof(vid), "%d",
(int)ntohs(tag & PKT_8021Q_MASK_VID));
chilli_learn_location((uint8_t *)vid, strlen(vid), appconn, 1);
}
#endif
}
if (appconn) {
appconn->s_state.tag8021q = conn->tag8021q;
}
}
}
#endif
/**
* dhcp_hashget()
* Uses the hash tables to find a connection based on the mac address.
* Returns -1 if not found.
**/
int dhcp_hashget(struct dhcp_t *this, struct dhcp_conn_t **conn,
uint8_t *hwaddr) {
struct dhcp_conn_t *p;
uint32_t hash;
/* Find in hash table */
hash = dhcp_hash(hwaddr) & this->hashmask;
for (p = this->hash[hash]; p; p = p->nexthash) {
if ((!memcmp(p->hismac, hwaddr, PKT_ETH_ALEN)) && (p->inuse)) {
*conn = p;
return 0;
}
}
*conn = NULL;
return -1; /* Address could not be found */
}
/**
* dhcp_lnkconn()
* Allocates/link a new connection from the pool.
* Returns -1 if unsuccessful.
**/
int dhcp_lnkconn(struct dhcp_t *this, struct dhcp_conn_t **conn) {
if (!this->firstfreeconn) {
if (connections == _options.max_clients) {
log_err(0, "reached max connections %d!",
_options.max_clients);
return -1;
}
++connections;
if (!(*conn = calloc(1, sizeof(struct dhcp_conn_t)))) {
log_err(0, "Out of memory!");
return -1;
}
} else {
*conn = this->firstfreeconn;
/* Remove from link of free */
if (this->firstfreeconn->next) {
this->firstfreeconn->next->prev = NULL;
this->firstfreeconn = this->firstfreeconn->next;
}
else { /* Took the last one */
this->firstfreeconn = NULL;
this->lastfreeconn = NULL;
}
/* Initialise structures */
memset(*conn, 0, sizeof(struct dhcp_conn_t));
}
/* Insert into link of used */
if (this->firstusedconn) {
this->firstusedconn->prev = *conn;
(*conn)->next = this->firstusedconn;
}
else { /* First insert */
this->lastusedconn = *conn;
}
this->firstusedconn = *conn;
return 0; /* Success */
}
int dhcp_getconn(struct dhcp_t *this,
struct dhcp_conn_t **conn,
uint8_t *mac, uint8_t *pkt,
char do_alloc) {
if (dhcp_hashget(this, conn, mac)) {
if (!do_alloc)
return -1;
if (dhcp_newconn(this, conn, mac))
return -1;
}
if (!*conn)
return -1;
#ifdef ENABLE_IEEE8021Q
if (_options.ieee8021q) {
if (pkt) {
#if(_debug_ > 1)
log_dbg("%s calling dhcp_checktag",__FUNCTION__);
#endif
dhcp_checktag(*conn, pkt);
}
}
#endif
return 0;
}
/**
* dhcp_newconn()
* Allocates a new connection from the pool.
* Returns -1 if unsuccessful.
**/
int dhcp_newconn(struct dhcp_t *this,
struct dhcp_conn_t **conn,
uint8_t *hwaddr)
{
if (_options.debug)
log_dbg("DHCP newconn: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
hwaddr[0], hwaddr[1], hwaddr[2],
hwaddr[3], hwaddr[4], hwaddr[5]);
if (dhcp_lnkconn(dhcp, conn) != 0)
return -1;
(*conn)->inuse = 1;
(*conn)->parent = this;
(*conn)->mtu = this->mtu;
(*conn)->noc2c = this->noc2c;
/* Application specific initialisations */
memcpy((*conn)->hismac, hwaddr, PKT_ETH_ALEN);
/*memcpy((*conn)->ourmac, dhcp_nexthop(this), PKT_ETH_ALEN);*/
(*conn)->lasttime = mainclock_now();
dhcp_hashadd(this, *conn);
#ifdef ENABLE_LAYER3
if (_options.layer3) {
(*conn)->authstate = DHCP_AUTH_ROUTER;
(*conn)->dns1.s_addr = _options.dns1.s_addr;
(*conn)->dns2.s_addr = _options.dns2.s_addr;
}
else
#endif
/* Inform application that connection was created */
if (this->cb_connect)
this->cb_connect(*conn);
return 0; /* Success */
}
uint8_t * dhcp_nexthop(struct dhcp_t *this) {
#ifdef ENABLE_TAP
if (_options.usetap && _options.has_nexthop)
return _options.nexthop;
#endif
return this->rawif[0].hwaddr;
}
/**
* dhcp_freeconn()
* Returns a connection to the pool.
**/
int dhcp_freeconn(struct dhcp_conn_t *conn, int term_cause)
{
/* TODO: Always returns success? */
struct dhcp_t *this = conn->parent;
/* Tell application that we disconnected */
if (this->cb_disconnect)
this->cb_disconnect(conn, term_cause);
if (conn->is_reserved)
return 0;
log_dbg("DHCP freeconn: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
conn->hismac[0], conn->hismac[1], conn->hismac[2],
conn->hismac[3], conn->hismac[4], conn->hismac[5]);
#ifdef HAVE_NETFILTER_COOVA
if (_options.kname) {
kmod_coova_release(conn);
}
#endif
/* Application specific code */
/* First remove from hash table */
dhcp_hashdel(this, conn);
/* Remove from link of used */
if ((conn->next) && (conn->prev)) {
conn->next->prev = conn->prev;
conn->prev->next = conn->next;
}
else if (conn->next) { /* && prev == 0 */
conn->next->prev = NULL;
this->firstusedconn = conn->next;
}
else if (conn->prev) { /* && next == 0 */
conn->prev->next = NULL;
this->lastusedconn = conn->prev;
}
else { /* if ((next == 0) && (prev == 0)) */
this->firstusedconn = NULL;
this->lastusedconn = NULL;
}
/* Initialise structures */
memset(conn, 0, sizeof(*conn));
/* Insert into link of free */
if (this->firstfreeconn) {
this->firstfreeconn->prev = conn;
}
else { /* First insert */
this->lastfreeconn = conn;
}
conn->next = this->firstfreeconn;
this->firstfreeconn = conn;
return 0;
}
/**
* dhcp_checkconn()
* Checks connections to see if the lease has expired
**/
int dhcp_checkconn(struct dhcp_t *this) {
struct dhcp_conn_t *conn = this->firstusedconn;
while (conn) {
/*
if (_options.debug)
log_dbg("dhcp_checkconn: %d %d", mainclock_diff(conn->lasttime), (int) this->lease);
*/
struct dhcp_conn_t *check_conn = conn;
conn = conn->next;
if (!check_conn->is_reserved &&
mainclock_diff(check_conn->lasttime) > (int)this->lease + _options.leaseplus) {
log_dbg("DHCP timeout: Removing connection");
dhcp_freeconn(check_conn, RADIUS_TERMINATE_CAUSE_LOST_CARRIER);
}
}
return 0;
}
#ifdef HAVE_NETFILTER_QUEUE
static
int nfqueue_cb_in(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
struct nfq_data *nfa, void *cbdata) {
struct nfqnl_msg_packet_hdr *ph;
struct nfqnl_msg_packet_hw *hw;
u_int32_t id = 0;
unsigned char *data;
int ret;
int result = NF_DROP;
hw = nfq_get_packet_hw(nfa);
ph = nfq_get_msg_packet_hdr(nfa);
if (ph) {
id = ntohl(ph->packet_id);
}
/*
mark = nfq_get_nfmark(nfa);
ifi = nfq_get_indev(nfa);
ifi = nfq_get_outdev(nfa);
*/
ret = nfq_get_payload(nfa, &data);
if (hw && ret > 0) {
struct pkt_ipphdr_t *pack_iph = (struct pkt_ipphdr_t *)data;
struct pkt_tcphdr_t *pack_tcph = (struct pkt_tcphdr_t *)(data + PKT_IP_HLEN);
struct pkt_udphdr_t *pack_udph = (struct pkt_udphdr_t *)(data + PKT_IP_HLEN);
struct dhcp_conn_t *conn;
struct in_addr addr;
if (!dhcp_hashget(dhcp, &conn, hw->hw_addr)) {
struct app_conn_t *appconn = (struct app_conn_t *)conn->peer;
if (appconn->s_state.authenticated) {
if (chilli_acct_fromsub(appconn, pack_iph))
result = NF_DROP;
else
result = NF_ACCEPT;
}
}
if (_options.debug) {
addr.s_addr = pack_iph->saddr;
log_dbg("NFQUEUE: From %.2X-%.2X-%.2X-%.2X-%.2X-%.2X %s %s",
hw->hw_addr[0],hw->hw_addr[1],hw->hw_addr[2],
hw->hw_addr[3],hw->hw_addr[4],hw->hw_addr[5],
inet_ntoa(addr),
result == NF_ACCEPT ? "Accept" : "Drop");
addr.s_addr = pack_iph->daddr;
log_dbg("NFQUEUE: To %s %s %d %s",
inet_ntoa(addr),
pack_iph->protocol == PKT_IP_PROTO_UDP ? "UDP" :
pack_iph->protocol == PKT_IP_PROTO_TCP ? "TCP" : "Other",
pack_iph->protocol == PKT_IP_PROTO_UDP ? ntohs(pack_udph->dst) :
pack_iph->protocol == PKT_IP_PROTO_TCP ? ntohs(pack_tcph->dst) : 0,
result == NF_ACCEPT ? "Accept" : "Drop");
}
}
return nfq_set_verdict(qh, id, result, 0, NULL);
}
static
int nfqueue_cb_out(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg,
struct nfq_data *nfa, void *cbdata) {
struct nfqnl_msg_packet_hdr *ph;
u_int32_t id = 0;
unsigned char *data;
int ret;
int result = NF_DROP;
ph = nfq_get_msg_packet_hdr(nfa);
if (ph) {
id = ntohl(ph->packet_id);
}
ret = nfq_get_payload(nfa, &data);
if (ret > 0) {
struct pkt_ipphdr_t *pack_iph = (struct pkt_ipphdr_t *)data;
struct pkt_tcphdr_t *pack_tcph = (struct pkt_tcphdr_t *)(data + PKT_IP_HLEN);
struct pkt_udphdr_t *pack_udph = (struct pkt_udphdr_t *)(data + PKT_IP_HLEN);
struct in_addr addr;
struct ippoolm_t *ipm;
struct app_conn_t *appconn;
result = NF_DROP;
addr.s_addr = pack_iph->daddr;
if (ippool_getip(ippool, &ipm, &addr)) {
if (_options.debug)
log_dbg("dropping packet with unknown destination: %s", inet_ntoa(addr));
}
else {
if ((appconn = (struct app_conn_t *)ipm->peer) == NULL ||
(appconn->dnlink) == NULL) {
log_err(0, "No %s protocol defined for %s", appconn ? "dnlink" : "peer", inet_ntoa(addr));
}
else {
if (appconn->s_state.authenticated == 1) {
if (chilli_acct_tosub(appconn, pack_iph))
result = NF_DROP;
else
result = NF_ACCEPT;
}
}
}
if (_options.debug) {