forked from sonic-net/sonic-dhcp-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelay.cpp
More file actions
877 lines (782 loc) · 31.3 KB
/
Copy pathrelay.cpp
File metadata and controls
877 lines (782 loc) · 31.3 KB
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
#include <errno.h>
#include <unistd.h>
#include <event.h>
#include <sstream>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <syslog.h>
#include <signal.h>
#include "configdb.h"
#include "sonicv2connector.h"
#include "dbconnector.h"
#include "configInterface.h"
struct event *listen_event;
struct event *server_listen_event;
struct event_base *base;
struct event *ev_sigint;
struct event *ev_sigterm;
static std::string counter_table = "DHCPv6_COUNTER_TABLE|";
/* DHCPv6 filter */
/* sudo tcpdump -dd "ip6 dst ff02::1:2 && udp dst port 547" */
static struct sock_filter ether_relay_filter[] = {
{ 0x28, 0, 0, 0x0000000c },
{ 0x15, 0, 13, 0x000086dd },
{ 0x20, 0, 0, 0x00000026 },
{ 0x15, 0, 11, 0xff020000 },
{ 0x20, 0, 0, 0x0000002a },
{ 0x15, 0, 9, 0x00000000 },
{ 0x20, 0, 0, 0x0000002e },
{ 0x15, 0, 7, 0x00000000 },
{ 0x20, 0, 0, 0x00000032 },
{ 0x15, 0, 5, 0x00010002 },
{ 0x30, 0, 0, 0x00000014 },
{ 0x15, 0, 3, 0x00000011 },
{ 0x28, 0, 0, 0x00000038 },
{ 0x15, 0, 1, 0x00000223 },
{ 0x6, 0, 0, 0x00040000 },
{ 0x6, 0, 0, 0x00000000 },
};
const struct sock_fprog ether_relay_fprog = {
lengthof(ether_relay_filter),
ether_relay_filter
};
/* DHCPv6 Counter */
uint64_t counters[DHCPv6_MESSAGE_TYPE_COUNT];
std::map<int, std::string> counterMap = {{DHCPv6_MESSAGE_TYPE_UNKNOWN, "Unknown"},
{DHCPv6_MESSAGE_TYPE_SOLICIT, "Solicit"},
{DHCPv6_MESSAGE_TYPE_ADVERTISE, "Advertise"},
{DHCPv6_MESSAGE_TYPE_REQUEST, "Request"},
{DHCPv6_MESSAGE_TYPE_CONFIRM, "Confirm"},
{DHCPv6_MESSAGE_TYPE_RENEW, "Renew"},
{DHCPv6_MESSAGE_TYPE_REBIND, "Rebind"},
{DHCPv6_MESSAGE_TYPE_REPLY, "Reply"},
{DHCPv6_MESSAGE_TYPE_RELEASE, "Release"},
{DHCPv6_MESSAGE_TYPE_DECLINE, "Decline"},
{DHCPv6_MESSAGE_TYPE_RECONFIGURE, "Reconfigure"},
{DHCPv6_MESSAGE_TYPE_INFORMATION_REQUEST, "Information-Request"},
{DHCPv6_MESSAGE_TYPE_RELAY_FORW, "Relay-Forward"},
{DHCPv6_MESSAGE_TYPE_RELAY_REPL, "Relay-Reply"},
{DHCPv6_MESSAGE_TYPE_MALFORMED, "Malformed"}};
/**
* @code void initialize_counter(swss::DBConnector *db, std::string counterVlan);
*
* @brief initialize the counter by each Vlan
*
* @param swss::DBConnector *db state_db connector
* @param counterVlan counter table with interface name
*
* @return none
*/
void initialize_counter(swss::DBConnector *db, std::string counterVlan) {
db->hset(counterVlan, "Unknown", toString(counters[DHCPv6_MESSAGE_TYPE_UNKNOWN]));
db->hset(counterVlan, "Solicit", toString(counters[DHCPv6_MESSAGE_TYPE_SOLICIT]));
db->hset(counterVlan, "Advertise", toString(counters[DHCPv6_MESSAGE_TYPE_ADVERTISE]));
db->hset(counterVlan, "Request", toString(counters[DHCPv6_MESSAGE_TYPE_REQUEST]));
db->hset(counterVlan, "Confirm", toString(counters[DHCPv6_MESSAGE_TYPE_CONFIRM]));
db->hset(counterVlan, "Renew", toString(counters[DHCPv6_MESSAGE_TYPE_RENEW]));
db->hset(counterVlan, "Rebind", toString(counters[DHCPv6_MESSAGE_TYPE_REBIND]));
db->hset(counterVlan, "Reply", toString(counters[DHCPv6_MESSAGE_TYPE_REPLY]));
db->hset(counterVlan, "Release", toString(counters[DHCPv6_MESSAGE_TYPE_RELEASE]));
db->hset(counterVlan, "Decline", toString(counters[DHCPv6_MESSAGE_TYPE_DECLINE]));
db->hset(counterVlan, "Reconfigure", toString(counters[DHCPv6_MESSAGE_TYPE_RECONFIGURE]));
db->hset(counterVlan, "Information-Request", toString(counters[DHCPv6_MESSAGE_TYPE_INFORMATION_REQUEST]));
db->hset(counterVlan, "Relay-Forward", toString(counters[DHCPv6_MESSAGE_TYPE_RELAY_FORW]));
db->hset(counterVlan, "Relay-Reply", toString(counters[DHCPv6_MESSAGE_TYPE_RELAY_REPL]));
db->hset(counterVlan, "Malformed", toString(counters[DHCPv6_MESSAGE_TYPE_MALFORMED]));
}
/**
* @code void update_counter(swss::DBConnector *db, std::string CounterVlan, uint8_t msg_type);
*
* @brief update the counter in state_db with count of each DHCPv6 message type
*
* @param swss::DBConnector *db state_db connector
* @param counterVlan counter table with interface name
* @param msg_type dhcpv6 message type to be updated in counter
*
* @return none
*/
void update_counter(swss::DBConnector *db, std::string counterVlan, uint8_t msg_type) {
db->hset(counterVlan, counterMap.find(msg_type)->second, toString(counters[msg_type]));
}
/**
* @code std::string toString(uint16_t count);
*
* @brief convert uint16_t to string
*
* @param count count of messages in counter
*
* @return count in string
*/
std::string toString(uint16_t count) {
std::stringstream ss;
ss << count;
std::string countValue = ss.str();
return countValue;
}
/**
* @code const struct ether_header *parse_ether_frame(const uint8_t *buffer, const uint8_t **out_end);
*
* @brief parse through ethernet frame
*
* @param *buffer message buffer
* @param **out_end pointer
*
* @return ether_header end of ethernet header position
*/
const struct ether_header *parse_ether_frame(const uint8_t *buffer, const uint8_t **out_end) {
(*out_end) = buffer + sizeof(struct ether_header);
return (const struct ether_header *)buffer;
}
/**
* @code const struct ip6_hdr *parse_ip6_hdr(const uint8_t *buffer, const uint8_t **out_end);
*
* @brief parse through ipv6 header
*
* @param *buffer message buffer
* @param **out_end pointer
*
* @return ip6_hdr end of ipv6 header position
*/
const struct ip6_hdr *parse_ip6_hdr(const uint8_t *buffer, const uint8_t **out_end) {
(*out_end) = buffer + sizeof(struct ip6_hdr);
return (struct ip6_hdr *)buffer;
}
/**
* @code const struct udphdr *parse_udp(const uint8_t *buffer, const uint8_t **out_end);
*
* @brief parse through udp header
*
* @param *buffer message buffer
* @param **out_end pointer
*
* @return udphdr end of udp header position
*/
const struct udphdr *parse_udp(const uint8_t *buffer, const uint8_t **out_end) {
(*out_end) = buffer + sizeof(struct udphdr);
return (const struct udphdr *)buffer;
}
/**
* @code const struct dhcpv6_msg *parse_dhcpv6_hdr(const uint8_t *buffer);
*
* @brief parse through dhcpv6 header
*
* @param *buffer message buffer
* @param **out_end pointer
*
* @return dhcpv6_msg end of dhcpv6 header position
*/
const struct dhcpv6_msg *parse_dhcpv6_hdr(const uint8_t *buffer) {
return (const struct dhcpv6_msg *)buffer;
}
/**
* @code const struct dhcpv6_relay_msg *parse_dhcpv6_relay(const uint8_t *buffer);
*
* @brief parse through dhcpv6 relay message
*
* @param *buffer message buffer
* @param **out_end pointer
*
* @return dhcpv6_relay_msg start of dhcpv6 relay message or end of dhcpv6 message type position
*/
const struct dhcpv6_relay_msg *parse_dhcpv6_relay(const uint8_t *buffer) {
return (const struct dhcpv6_relay_msg *)buffer;
}
/**
* @code const struct dhcpv6_option *parse_dhcpv6_opt(const uint8_t *buffer, const uint8_t **out_end);
*
* @brief parse through dhcpv6 option
*
* @param *buffer message buffer
* @param **out_end pointer
*
* @return dhcpv6_option end of dhcpv6 message option
*/
const struct dhcpv6_option *parse_dhcpv6_opt(const uint8_t *buffer, const uint8_t **out_end) {
auto option = (const struct dhcpv6_option *)buffer;
uint8_t size = 4; // option-code + option-len
size += *(uint16_t *)(buffer);
(*out_end) = buffer + size + ntohs(option->option_length);
return option;
}
/**
* @code void send_udp(int sock, uint8_t *buffer, struct sockaddr_in6 target, uint32_t n, relay_config *config, uint8_t msg_type);
*
* @brief send udp packet
*
* @param *buffer message buffer
* @param sockaddr_in6 target target socket
* @param n length of message
* @param relay_config *config pointer to relay_config
* @param uint8_t msg_type message type of dhcpv6 option of relayed message
*
* @return dhcpv6_option end of dhcpv6 message option
*/
void send_udp(int sock, uint8_t *buffer, struct sockaddr_in6 target, uint32_t n, relay_config *config, uint8_t msg_type) {
std::string counterVlan = counter_table;
if(sendto(sock, buffer, n, 0, (const struct sockaddr *)&target, sizeof(target)) == -1)
syslog(LOG_ERR, "sendto: Failed to send to target address\n");
else if (counterMap.find(msg_type) != counterMap.end()) {
counters[msg_type]++;
update_counter(config->db, counterVlan.append(config->interface), msg_type);
} else {
syslog(LOG_WARNING, "unexpected message type %d(0x%x)\n", msg_type, msg_type);
}
}
/**
* @code relay_forward(uint8_t *buffer, const struct dhcpv6_msg *msg, uint16_t msg_length);
*
* @brief embed the DHCPv6 message received into DHCPv6 relay forward message
*
* @param buffer pointer to buffer
* @param msg pointer to parsed DHCPv6 message
* @param msg_length length of DHCPv6 message
*
* @return none
*/
void relay_forward(uint8_t *buffer, const struct dhcpv6_msg *msg, uint16_t msg_length) {
struct dhcpv6_option option;
option.option_code = htons(OPTION_RELAY_MSG);
option.option_length = htons(msg_length);
memcpy(buffer, &option, sizeof(struct dhcpv6_option));
memcpy(buffer + sizeof(struct dhcpv6_option), msg, msg_length);
}
/**
* @code sock_open(int ifindex, const struct sock_fprog *fprog);
*
* @brief prepare L2 socket to attach to "udp and port 547" filter
*
* @param ifindex interface index
* @param fprog bpf filter "udp and port 547"
*
* @return socket descriptor
*/
int sock_open(int ifindex, const struct sock_fprog *fprog)
{
if (!ifindex) {
errno = EINVAL;
return -1;
}
int s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (s == -1) {
syslog(LOG_ERR, "socket: Failed to create socket\n");
return -1;
}
struct sockaddr_ll sll = {
.sll_family = AF_PACKET,
.sll_protocol = htons(ETH_P_ALL),
.sll_ifindex = ifindex
};
if (bind(s, (struct sockaddr *)&sll, sizeof sll) == -1) {
syslog(LOG_ERR, "bind: Failed to bind to specified interface\n");
(void) close(s);
return -1;
}
if (fprog && setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, fprog, sizeof *fprog) == -1)
{
syslog(LOG_ERR, "setsockopt: Failed to attach filter\n");
(void) close(s);
return -1;
}
return s;
}
/**
* @code prepare_relay_config(relay_config *interface_config, int local_sock, int filter);
*
* @brief prepare for specified relay interface config: server and link address
*
* @param interface_config pointer to relay config to be prepared
* @param local_sock L3 socket used for relaying messages
* @param filter socket attached with filter
*
* @return none
*/
void prepare_relay_config(relay_config *interface_config, int *local_sock, int filter) {
struct ifaddrs *ifa, *ifa_tmp;
sockaddr_in6 non_link_local;
sockaddr_in6 link_local;
interface_config->local_sock = *local_sock;
interface_config->filter = filter;
for(auto server: interface_config->servers) {
sockaddr_in6 tmp;
if(inet_pton(AF_INET6, server.c_str(), &tmp.sin6_addr) != 1)
{
syslog(LOG_WARNING, "inet_pton: Failed to convert IPv6 address\n");
}
tmp.sin6_family = AF_INET6;
tmp.sin6_flowinfo = 0;
tmp.sin6_port = htons(RELAY_PORT);
tmp.sin6_scope_id = 0;
interface_config->servers_sock.push_back(tmp);
}
if (getifaddrs(&ifa) == -1) {
syslog(LOG_WARNING, "getifaddrs: Unable to get network interfaces\n");
exit(1);
}
ifa_tmp = ifa;
while (ifa_tmp) {
if (ifa_tmp->ifa_addr && ifa_tmp->ifa_addr->sa_family == AF_INET6) {
struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa_tmp->ifa_addr;
if((strcmp(ifa_tmp->ifa_name, interface_config->interface.c_str()) == 0) && !IN6_IS_ADDR_LINKLOCAL(&in6->sin6_addr)) {
non_link_local = *in6;
break;
}
if((strcmp(ifa_tmp->ifa_name, interface_config->interface.c_str()) == 0) && IN6_IS_ADDR_LINKLOCAL(&in6->sin6_addr)) {
link_local = *in6;
}
}
ifa_tmp = ifa_tmp->ifa_next;
}
freeifaddrs(ifa);
if(!IN6_IS_ADDR_LINKLOCAL(&non_link_local.sin6_addr)) {
interface_config->link_address = non_link_local;
}
else {
interface_config->link_address = link_local;
}
}
/**
* @code prepare_socket(int *local_sock, int *server_sock, relay_config *config, int index);
*
* @brief prepare L3 socket for sending
*
* @param local_sock pointer to socket binded to global address for relaying client message to server and listening for server message
* @param server_sock pointer to socket binded to link_local address for relaying server message to client
* @param index scope id of interface
*
* @return none
*/
void prepare_socket(int *local_sock, int *server_sock, relay_config *config, int index) {
struct ifaddrs *ifa, *ifa_tmp;
sockaddr_in6 addr;
sockaddr_in6 ll_addr;
memset(&addr, 0, sizeof(addr));
memset(&ll_addr, 0, sizeof(ll_addr));
if ((*local_sock = socket(AF_INET6, SOCK_DGRAM, 0)) == -1) {
syslog(LOG_ERR, "socket: Failed to create socket on interface %s\n", config->interface.c_str());
}
if ((*server_sock= socket(AF_INET6, SOCK_DGRAM, 0)) == -1) {
syslog(LOG_ERR, "socket: Failed to create socket on interface %s\n", config->interface.c_str());
}
int retry = 0;
bool bind_addr = false;
bool bind_ll_addr = false;
do {
if (getifaddrs(&ifa) == -1) {
syslog(LOG_WARNING, "getifaddrs: Unable to get network interfaces with %s\n", strerror(errno));
}
else {
ifa_tmp = ifa;
while (ifa_tmp) {
if (ifa_tmp->ifa_addr && (ifa_tmp->ifa_addr->sa_family == AF_INET6) && (strcmp(ifa_tmp->ifa_name, config->interface.c_str()) == 0)) {
struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa_tmp->ifa_addr;
if(!IN6_IS_ADDR_LINKLOCAL(&in6->sin6_addr)) {
bind_addr = true;
in6->sin6_family = AF_INET6;
in6->sin6_port = htons(RELAY_PORT);
addr = *in6;
}
if(IN6_IS_ADDR_LINKLOCAL(&in6->sin6_addr)) {
bind_ll_addr = true;
in6->sin6_family = AF_INET6;
in6->sin6_port = htons(RELAY_PORT);
ll_addr = *in6;
}
}
ifa_tmp = ifa_tmp->ifa_next;
}
freeifaddrs(ifa);
}
if (bind_addr && bind_ll_addr) {
break;
}
syslog(LOG_WARNING, "Retry #%d to bind to sockets on interface %s\n", ++retry, config->interface.c_str());
sleep(5);
} while (retry < 6);
if ((!bind_addr) || (bind(*local_sock, (sockaddr *)&addr, sizeof(addr)) == -1)) {
syslog(LOG_ERR, "bind: Failed to bind socket to global ipv6 address on interface %s after %d retries with %s\n", config->interface.c_str(), retry, strerror(errno));
}
if ((!bind_ll_addr) || (bind(*server_sock, (sockaddr *)&ll_addr, sizeof(addr)) == -1)) {
syslog(LOG_ERR, "bind: Failed to bind socket to link local ipv6 address on interface %s after %d retries with %s\n", config->interface.c_str(), retry, strerror(errno));
}
}
/**
* @code relay_client(int sock, const uint8_t *msg, int32_t len, ip6_hdr *ip_hdr, const ether_header *ether_hdr, relay_config *config);
*
* @brief construct relay-forward message
*
* @param sock L3 socket for sending data to servers
* @param msg pointer to dhcpv6 message header position
* @param len size of data received
* @param ip_hdr pointer to IPv6 header
* @param ether_hdr pointer to Ethernet header
* @param config pointer to the relay interface config
*
* @return none
*/
void relay_client(int sock, const uint8_t *msg, int32_t len, const ip6_hdr *ip_hdr, const ether_header *ether_hdr, relay_config *config) {
static uint8_t buffer[4096];
auto current_buffer_position = buffer;
dhcpv6_relay_msg new_message;
new_message.msg_type = DHCPv6_MESSAGE_TYPE_RELAY_FORW;
memcpy(&new_message.peer_address, &ip_hdr->ip6_src, sizeof(in6_addr));
new_message.hop_count = 0;
memcpy(&new_message.link_address, &config->link_address.sin6_addr, sizeof(in6_addr));
memcpy(current_buffer_position, &new_message, sizeof(dhcpv6_relay_msg));
current_buffer_position += sizeof(dhcpv6_relay_msg);
if(config->is_option_79) {
linklayer_addr_option option79;
option79.link_layer_type = htons(1);
option79.option_code = htons(OPTION_CLIENT_LINKLAYER_ADDR);
option79.option_length = htons(2 + 6); // link_layer_type field + address
if ((unsigned)(current_buffer_position + sizeof(linklayer_addr_option) - buffer) > sizeof(buffer)) {
return;
}
memcpy(current_buffer_position, &option79, sizeof(linklayer_addr_option));
current_buffer_position += sizeof(linklayer_addr_option);
memcpy(current_buffer_position, ðer_hdr->ether_shost, sizeof(ether_hdr->ether_shost));
current_buffer_position += sizeof(ether_hdr->ether_shost);
}
if(config->is_interface_id) {
interface_id_option intf_id;
intf_id.option_code = htons(OPTION_INTERFACE_ID);
intf_id.option_length = htons(sizeof(in6_addr));
intf_id.interface_id = config->link_address.sin6_addr;
if ((unsigned)(current_buffer_position + sizeof(linklayer_addr_option) - buffer) > sizeof(buffer)) {
return;
}
memcpy(current_buffer_position, &intf_id, sizeof(interface_id_option));
current_buffer_position += sizeof(interface_id_option);
}
auto dhcp_message_length = len;
relay_forward(current_buffer_position, parse_dhcpv6_hdr(msg), dhcp_message_length);
current_buffer_position += dhcp_message_length + sizeof(dhcpv6_option);
for(auto server: config->servers_sock) {
send_udp(sock, buffer, server, current_buffer_position - buffer, config, new_message.msg_type);
}
}
/**
* @code relay_relay_forw(int sock, const uint8_t *msg, int32_t len, const ip6_hdr *ip_hdr, relay_config *config)
*
* @brief construct a relay-forward message encapsulated relay-forward message
*
* @param sock L3 socket for sending data to servers
* @param msg pointer to dhcpv6 message header position
* @param len size of data received
* @param ip_hdr pointer to IPv6 header
* @param config pointer to the relay interface config
*
* @return none
*/
void relay_relay_forw(int sock, const uint8_t *msg, int32_t len, const ip6_hdr *ip_hdr, relay_config *config) {
static uint8_t buffer[4096];
dhcpv6_relay_msg new_message;
auto current_buffer_position = buffer;
auto dhcp_relay_header = parse_dhcpv6_relay(msg);
if (dhcp_relay_header->hop_count >= HOP_LIMIT)
return;
new_message.msg_type = DHCPv6_MESSAGE_TYPE_RELAY_FORW;
memcpy(&new_message.peer_address, &ip_hdr->ip6_src, sizeof(in6_addr));
new_message.hop_count = dhcp_relay_header->hop_count + 1;
memset(&new_message.link_address, 0, sizeof(in6_addr));
memcpy(current_buffer_position, &new_message, sizeof(dhcpv6_relay_msg));
current_buffer_position += sizeof(dhcpv6_relay_msg);
auto dhcp_message_length = len;
relay_forward(current_buffer_position, parse_dhcpv6_hdr(msg), dhcp_message_length);
current_buffer_position += dhcp_message_length + sizeof(dhcpv6_option);
for(auto server: config->servers_sock) {
send_udp(sock, buffer, server, current_buffer_position - buffer, config, new_message.msg_type);
}
}
/**
* @code relay_relay_reply(int sock, const uint8_t *msg, int32_t len, relay_config *configs);
*
* @brief relay and unwrap a relay-reply message
*
* @param sock L3 socket for sending data to servers
* @param msg pointer to dhcpv6 message header position
* @param len size of data received
* @param config relay interface config
*
* @return none
*/
void relay_relay_reply(int sock, const uint8_t *msg, int32_t len, relay_config *config) {
static uint8_t buffer[4096];
uint8_t type = 0;
struct sockaddr_in6 target_addr;
auto current_buffer_position = buffer;
auto current_position = msg;
const uint8_t *tmp = NULL;
auto dhcp_relay_header = parse_dhcpv6_relay(msg);
current_position += sizeof(struct dhcpv6_relay_msg);
auto position = current_position + sizeof(struct dhcpv6_option);
auto dhcpv6msg = parse_dhcpv6_hdr(position);
while ((current_position - msg) < len) {
auto option = parse_dhcpv6_opt(current_position, &tmp);
current_position = tmp;
if (current_position - msg > len || ntohs(option->option_length) > sizeof(buffer) - (current_buffer_position - buffer)) {
break;
}
switch (ntohs(option->option_code)) {
case OPTION_RELAY_MSG:
memcpy(current_buffer_position, ((uint8_t *)option) + sizeof(struct dhcpv6_option), ntohs(option->option_length));
current_buffer_position += ntohs(option->option_length);
type = dhcpv6msg->msg_type;
break;
default:
break;
}
}
memcpy(&target_addr.sin6_addr, &dhcp_relay_header->peer_address, sizeof(struct in6_addr));
target_addr.sin6_family = AF_INET6;
target_addr.sin6_flowinfo = 0;
target_addr.sin6_port = htons(CLIENT_PORT);
target_addr.sin6_scope_id = if_nametoindex(config->interface.c_str());
send_udp(sock, buffer, target_addr, current_buffer_position - buffer, config, type);
}
/**
* @code callback(evutil_socket_t fd, short event, void *arg);
*
* @brief callback for libevent that is called everytime data is received at the filter socket
*
* @param fd filter socket
* @param event libevent triggered event
* @param arg callback argument provided by user
*
* @return none
*/
void callback(evutil_socket_t fd, short event, void *arg) {
struct relay_config *config = (struct relay_config *)arg;
static uint8_t message_buffer[4096];
std::string counterVlan = counter_table;
int32_t len = recv(config->filter, message_buffer, 4096, 0);
if (len <= 0) {
syslog(LOG_WARNING, "recv: Failed to receive data at filter socket: %s\n", strerror(errno));
return;
}
char* ptr = (char *)message_buffer;
const uint8_t *current_position = (uint8_t *)ptr;
const uint8_t *tmp = NULL;
const uint8_t *prev = NULL;
auto ether_header = parse_ether_frame(current_position, &tmp);
current_position = tmp;
auto ip_header = parse_ip6_hdr(current_position, &tmp);
current_position = tmp;
prev = current_position;
if (ip_header->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_UDP) {
const struct ip6_ext *ext_header;
do {
ext_header = (const struct ip6_ext *)current_position;
current_position += ext_header->ip6e_len;
if((current_position == prev) || (current_position >= (uint8_t *)ptr + sizeof(message_buffer))) {
return;
}
prev = current_position;
}
while (ext_header->ip6e_nxt != IPPROTO_UDP);
}
auto udp_header = parse_udp(current_position, &tmp);
current_position = tmp;
auto msg = parse_dhcpv6_hdr(current_position);
auto option_position = current_position + sizeof(struct dhcpv6_msg);
switch (msg->msg_type) {
case DHCPv6_MESSAGE_TYPE_RELAY_FORW:
{
relay_relay_forw(config->local_sock, current_position, ntohs(udp_header->len) - sizeof(udphdr), ip_header, config);
break;
}
case DHCPv6_MESSAGE_TYPE_SOLICIT:
case DHCPv6_MESSAGE_TYPE_REQUEST:
case DHCPv6_MESSAGE_TYPE_CONFIRM:
case DHCPv6_MESSAGE_TYPE_RENEW:
case DHCPv6_MESSAGE_TYPE_REBIND:
case DHCPv6_MESSAGE_TYPE_RELEASE:
case DHCPv6_MESSAGE_TYPE_DECLINE:
case DHCPv6_MESSAGE_TYPE_INFORMATION_REQUEST:
{
while (option_position - message_buffer < len) {
auto option = parse_dhcpv6_opt(option_position, &tmp);
option_position = tmp;
if (ntohs(option->option_code) > DHCPv6_OPTION_LIMIT) {
counters[DHCPv6_MESSAGE_TYPE_MALFORMED]++;
update_counter(config->db, counterVlan.append(config->interface), DHCPv6_MESSAGE_TYPE_MALFORMED);
syslog(LOG_WARNING, "DHCPv6 option is invalid or contains malformed payload\n");
return;
}
}
counters[msg->msg_type]++;
update_counter(config->db, counterVlan.append(config->interface), msg->msg_type);
relay_client(config->local_sock, current_position, ntohs(udp_header->len) - sizeof(udphdr), ip_header, ether_header, config);
break;
}
default:
{
syslog(LOG_WARNING, "DHCPv6 client message received was not relayed\n");
break;
}
}
}
/**
* @code void server_callback(evutil_socket_t fd, short event, void *arg);
*
* @brief callback for libevent that is called everytime data is received at the server socket
*
* @param fd filter socket
* @param event libevent triggered event
* @param arg callback argument provided by user
*
* @return none
*/
void server_callback(evutil_socket_t fd, short event, void *arg) {
struct relay_config *config = (struct relay_config *)arg;
sockaddr_in6 from;
socklen_t len = sizeof(from);
int32_t data = 0;
static uint8_t message_buffer[4096];
if ((data = recvfrom(config->local_sock, message_buffer, 4096, 0, (sockaddr *)&from, &len)) == -1) {
syslog(LOG_WARNING, "recv: Failed to receive data from server\n");
}
auto msg = parse_dhcpv6_hdr(message_buffer);
counters[msg->msg_type]++;
std::string counterVlan = counter_table;
update_counter(config->db, counterVlan.append(config->interface), msg->msg_type);
if (msg->msg_type == DHCPv6_MESSAGE_TYPE_RELAY_REPL) {
relay_relay_reply(config->server_sock, message_buffer, data, config);
}
}
/**
* @code signal_init();
*
* @brief initialize DHCPv6 Relay libevent signals
*/
int signal_init() {
int rv = -1;
do {
ev_sigint = evsignal_new(base, SIGINT, signal_callback, base);
if (ev_sigint == NULL) {
syslog(LOG_ERR, "Could not create SIGINT libevent signal\n");
break;
}
ev_sigterm = evsignal_new(base, SIGTERM, signal_callback, base);
if (ev_sigterm == NULL) {
syslog(LOG_ERR, "Could not create SIGTERM libevent signal\n");
break;
}
rv = 0;
} while(0);
return rv;
}
/**
* @code signal_start();
*
* @brief start DHCPv6 Relay libevent base and add signals
*/
int signal_start()
{
int rv = -1;
do
{
if (evsignal_add(ev_sigint, NULL) != 0) {
syslog(LOG_ERR, "Could not add SIGINT libevent signal\n");
break;
}
if (evsignal_add(ev_sigterm, NULL) != 0) {
syslog(LOG_ERR, "Could not add SIGTERM libevent signal\n");
break;
}
if (event_base_dispatch(base) != 0) {
syslog(LOG_ERR, "Could not start libevent dispatching loop\n");
}
rv = 0;
} while (0);
return rv;
}
/**
* @code signal_callback(fd, event, arg);
*
* @brief signal handler for dhcp6relay. Initiate shutdown when signal is caught
*
* @param fd libevent socket
* @param event event triggered
* @param arg pointer to libevent base
*
* @return none
*/
void signal_callback(evutil_socket_t fd, short event, void *arg)
{
syslog(LOG_ALERT, "Received signal: '%s'\n", strsignal(fd));
if ((fd == SIGTERM) || (fd == SIGINT)) {
dhcp6relay_stop();
}
}
/**
* @code dhcp6relay_stop();
*
* @brief stop DHCPv6 Relay libevent loop upon signal
*/
void dhcp6relay_stop()
{
event_base_loopexit(base, NULL);
}
/**
* @code loop_relay(std::vector<relay_config> *vlans, swss::DBConnector *db);
*
* @brief main loop: configure sockets, create libevent base, start server listener thread
*
* @param vlans list of vlans retrieved from config_db
* @param db state_db connector
*/
void loop_relay(std::vector<relay_config> *vlans, swss::DBConnector *db) {
std::vector<int> sockets;
base = event_base_new();
if(base == NULL) {
syslog(LOG_ERR, "libevent: Failed to create base\n");
}
for(relay_config &vlan : *vlans) {
relay_config *config = &vlan;
int filter = 0;
int local_sock = 0;
int server_sock = 0;
int index = if_nametoindex(config->interface.c_str());
config->db = db;
std::string counterVlan = counter_table;
initialize_counter(config->db, counterVlan.append(config->interface));
filter = sock_open(index, ðer_relay_fprog);
prepare_socket(&local_sock, &server_sock, config, index);
config->local_sock = local_sock;
config->server_sock = server_sock;
sockets.push_back(filter);
sockets.push_back(local_sock);
sockets.push_back(server_sock);
prepare_relay_config(config, &local_sock, filter);
evutil_make_listen_socket_reuseable(filter);
evutil_make_socket_nonblocking(filter);
evutil_make_listen_socket_reuseable(local_sock);
evutil_make_socket_nonblocking(local_sock);
listen_event = event_new(base, filter, EV_READ|EV_PERSIST, callback, config);
server_listen_event = event_new(base, local_sock, EV_READ|EV_PERSIST, server_callback, config);
if (listen_event == NULL || server_listen_event == NULL) {
syslog(LOG_ERR, "libevent: Failed to create libevent\n");
}
event_add(listen_event, NULL);
event_add(server_listen_event, NULL);
}
if((signal_init() == 0) && signal_start() == 0) {
shutdown();
for(std::size_t i = 0; i<sockets.size(); i++) {
close(sockets.at(i));
}
}
}
/**
* @code shutdown();
*
* @brief free signals and terminate threads
*/
void shutdown() {
event_del(ev_sigint);
event_del(ev_sigterm);
event_free(ev_sigint);
event_free(ev_sigterm);
event_base_free(base);
deinitialize_swss();
}