Skip to content

Commit 1f08590

Browse files
committed
small cleanup.
1 parent 854c6e5 commit 1f08590

2 files changed

Lines changed: 50 additions & 47 deletions

File tree

src/wolfesp.c

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ static int esp_unwrap(struct wolfIP *s, struct wolfIP_ip_packet *ip,
429429
pad_len = *(ip->data + esp_len - esp_sa->icv_len - ESP_NEXT_HEADER_LEN
430430
- ESP_PADDING_LEN);
431431
nxt_hdr = *(ip->data + esp_len - esp_sa->icv_len - ESP_NEXT_HEADER_LEN);
432-
ip->proto = nxt_hdr;
433432

434433
#ifdef WOLFIP_DEBUG_ESP
435434
wolfIP_print_esp(esp_sa, ip->data, esp_len, pad_len, nxt_hdr);
@@ -452,7 +451,12 @@ static int esp_unwrap(struct wolfIP *s, struct wolfIP_ip_packet *ip,
452451
ESP_NEXT_HEADER_LEN + esp_sa->icv_len);
453452
ip->len = ip->len - (pad_len + ESP_PADDING_LEN +
454453
ESP_NEXT_HEADER_LEN + esp_sa->icv_len);
454+
455+
/* update len, set proto to next header, recalculate iphdr checksum. */
455456
ip->len = ee16(ip->len);
457+
ip->proto = nxt_hdr;
458+
ip->csum = 0;
459+
iphdr_set_checksum(ip);
456460

457461
#ifdef WOLFIP_DEBUG_ESP_VERBOSE
458462
esp_dump_data_verbose("esp_packet after unwrap", ip->data,
@@ -608,4 +612,48 @@ static int esp_wrap(struct wolfIP_ip_packet *ip, uint16_t * ip_len)
608612

609613
return 0;
610614
}
615+
616+
/**
617+
* Copy frame to new packet so we can expand and wrap in place
618+
* without stepping on the fifo tcp circular buffer.
619+
* */
620+
static int esp_output(struct wolfIP *s, const struct wolfIP_ip_packet *ip,
621+
uint16_t len)
622+
{
623+
/**
624+
* 56 is reasonable max ESP overhead (for now), rounded up to 4 bytes.
625+
* 8 bytes (esp header)
626+
* + 16 bytes (iv, prepended to payload)
627+
* + 15 bytes (max padding with block cipher)
628+
* + 2 bytes (pad_len + nxt_hdr fields)
629+
* + 12 bytes (icv)
630+
* may need to increase depending on algs supported.
631+
* */
632+
struct wolfIP_ip_packet * esp;
633+
uint8_t frame[LINK_MTU + 56];
634+
uint16_t ip_final_len = len;
635+
int esp_rc = 0;
636+
637+
esp = (struct wolfIP_ip_packet *) frame;
638+
memcpy(esp, ip, sizeof(struct wolfIP_ip_packet) + len);
639+
640+
esp_rc = esp_wrap(esp, &ip_final_len);
641+
642+
if (esp_rc) {
643+
#ifdef WOLFIP_DEBUG_ESP
644+
printf("error: esp_wrap returned: %d\n", esp_rc);
645+
#endif /* WOLFIP_DEBUG_ESP */
646+
return esp_rc;
647+
}
648+
649+
/* update len, set proto to ESP 0x32 (50), recalculate iphdr checksum. */
650+
esp->len = ee16(ip_final_len);
651+
esp->proto = 0x32;
652+
esp->csum = 0;
653+
iphdr_set_checksum(esp);
654+
655+
s->ll_dev.send(&s->ll_dev, esp, ip_final_len + ETH_HEADER_LEN);
656+
657+
return 0;
658+
}
611659
#endif /* WOLFIP_ESP && !WOLFESP_SRC */

src/wolfip.c

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -958,52 +958,6 @@ static int ip_output_add_header(struct tsocket *t, struct wolfIP_ip_packet *ip,
958958
return 0;
959959
}
960960

961-
#ifdef WOLFIP_ESP
962-
/**
963-
* Copy frame to new packet so we can expand and wrap in place
964-
* without stepping on the fifo tcp circular buffer.
965-
* */
966-
static int esp_output(struct wolfIP *s, const struct wolfIP_ip_packet *ip,
967-
uint16_t len)
968-
{
969-
/**
970-
* 56 is reasonable max ESP overhead (for now), rounded up to 4 bytes.
971-
* 8 bytes (esp header)
972-
* + 16 bytes (iv, prepended to payload)
973-
* + 15 bytes (max padding with block cipher)
974-
* + 2 bytes (pad_len + nxt_hdr fields)
975-
* + 12 bytes (icv)
976-
* may need to increase depending on algs supported.
977-
* */
978-
struct wolfIP_ip_packet * esp;
979-
uint8_t frame[LINK_MTU + 56];
980-
uint16_t ip_final_len = len;
981-
int esp_rc = 0;
982-
983-
esp = (struct wolfIP_ip_packet *) frame;
984-
memcpy(esp, ip, sizeof(struct wolfIP_ip_packet) + len);
985-
986-
esp_rc = esp_wrap(esp, &ip_final_len);
987-
988-
if (esp_rc) {
989-
#ifdef WOLFIP_DEBUG_ESP
990-
printf("error: esp_wrap returned: %d\n", esp_rc);
991-
#endif /* WOLFIP_DEBUG_ESP */
992-
return esp_rc;
993-
}
994-
995-
/* update len, set proto to ESP 0x32 (50), recalculate iphdr checksum. */
996-
esp->len = ee16(ip_final_len);
997-
esp->proto = 0x32;
998-
esp->csum = 0;
999-
iphdr_set_checksum(esp);
1000-
1001-
s->ll_dev.send(&s->ll_dev, esp, ip_final_len + ETH_HEADER_LEN);
1002-
1003-
return 0;
1004-
}
1005-
#endif /* WOLFIP_ESP */
1006-
1007961
/* Process timestamp option, calculate RTT */
1008962
static int tcp_process_ts(struct tsocket *t, const struct wolfIP_tcp_seg *tcp)
1009963
{
@@ -2093,6 +2047,7 @@ static inline void ip_recv(struct wolfIP *s, struct wolfIP_ip_packet *ip,
20932047

20942048
#ifdef WOLFIP_ESP
20952049
if (ip->proto == 0x32) {
2050+
/* proto is ESP 0x32 (50), try to unwrap. */
20962051
int esp_rc = 0;
20972052
esp_rc = esp_unwrap(s, ip, &len);
20982053
if (esp_rc) {

0 commit comments

Comments
 (0)