Skip to content

Commit

Permalink
Add alloc_pkt() and free_pkt() helpers
Browse files Browse the repository at this point in the history
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
  • Loading branch information
dwmw2 committed Jun 28, 2021
1 parent 94a6e81 commit 6e7f451
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 50 deletions.
12 changes: 6 additions & 6 deletions array.c
Expand Up @@ -791,7 +791,7 @@ int array_connect(struct openconnect_info *vpninfo)
}
buf_free(reqbuf);

free(vpninfo->cstp_pkt);
free_pkt(vpninfo, vpninfo->cstp_pkt);
vpninfo->cstp_pkt = NULL;

vpninfo->ip_info.mtu = 1400;
Expand Down Expand Up @@ -821,7 +821,7 @@ int array_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
int len;

if (!vpninfo->cstp_pkt) {
vpninfo->cstp_pkt = malloc(sizeof(struct pkt) + receive_mtu);
vpninfo->cstp_pkt = alloc_pkt(vpninfo, receive_mtu);
if (!vpninfo->cstp_pkt) {
vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
break;
Expand Down Expand Up @@ -908,7 +908,7 @@ int array_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
/* Don't free the 'special' packets */
if (vpninfo->current_ssl_pkt != &dpd_pkt &&
vpninfo->current_ssl_pkt != &nodtls_pkt)
free(vpninfo->current_ssl_pkt);
free_pkt(vpninfo, vpninfo->current_ssl_pkt);

vpninfo->current_ssl_pkt = NULL;
}
Expand Down Expand Up @@ -1056,7 +1056,7 @@ int array_dtls_mainloop(struct openconnect_info *vpninfo, int *timeout, int read
* may be in active use while we attempt to connect DTLS.
* So use vpninfo->dtls_pkt for this. */
if (!vpninfo->dtls_pkt)
vpninfo->dtls_pkt = malloc(sizeof(struct pkt) + receive_mtu);
vpninfo->dtls_pkt = alloc_pkt(vpninfo, receive_mtu);
if (!vpninfo->dtls_pkt) {
vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
dtls_close(vpninfo);
Expand Down Expand Up @@ -1166,7 +1166,7 @@ int array_dtls_mainloop(struct openconnect_info *vpninfo, int *timeout, int read
unsigned char *buf;

if (!vpninfo->dtls_pkt) {
vpninfo->dtls_pkt = malloc(sizeof(struct pkt) + len);
vpninfo->dtls_pkt = alloc_pkt(vpninfo, len);
if (!vpninfo->dtls_pkt) {
vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
break;
Expand Down Expand Up @@ -1277,7 +1277,7 @@ int array_dtls_mainloop(struct openconnect_info *vpninfo, int *timeout, int read
vpn_progress(vpninfo, PRG_TRACE,
_("Sent DTLS packet of %d bytes; DTLS send returned %d\n"),
this->len, ret);
free(this);
free_pkt(vpninfo, this);
}

return work_done;
Expand Down
10 changes: 5 additions & 5 deletions cstp.c
Expand Up @@ -715,8 +715,8 @@ int cstp_connect(struct openconnect_info *vpninfo)

/* If *any* compression is enabled, we'll need a deflate_pkt to compress into */
if (deflate_bufsize > vpninfo->deflate_pkt_size) {
free(vpninfo->deflate_pkt);
vpninfo->deflate_pkt = malloc(sizeof(struct pkt) + deflate_bufsize);
free_pkt(vpninfo, vpninfo->deflate_pkt);
vpninfo->deflate_pkt = alloc_pkt(vpninfo, deflate_bufsize);
if (!vpninfo->deflate_pkt) {
vpninfo->deflate_pkt_size = 0;
vpn_progress(vpninfo, PRG_ERR,
Expand Down Expand Up @@ -921,7 +921,7 @@ int cstp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
int len, payload_len;

if (!vpninfo->cstp_pkt) {
vpninfo->cstp_pkt = malloc(sizeof(struct pkt) + receive_mtu);
vpninfo->cstp_pkt = alloc_pkt(vpninfo, receive_mtu);
if (!vpninfo->cstp_pkt) {
vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
break;
Expand Down Expand Up @@ -1071,12 +1071,12 @@ int cstp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
}
/* Don't free the 'special' packets */
if (vpninfo->current_ssl_pkt == vpninfo->deflate_pkt) {
free(vpninfo->pending_deflated_pkt);
free_pkt(vpninfo, vpninfo->pending_deflated_pkt);
vpninfo->pending_deflated_pkt = NULL;
} else if (vpninfo->current_ssl_pkt != &dpd_pkt &&
vpninfo->current_ssl_pkt != &dpd_resp_pkt &&
vpninfo->current_ssl_pkt != &keepalive_pkt)
free(vpninfo->current_ssl_pkt);
free_pkt(vpninfo, vpninfo->current_ssl_pkt);

vpninfo->current_ssl_pkt = NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions dtls.c
Expand Up @@ -275,7 +275,7 @@ int dtls_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
unsigned char *buf;

if (!vpninfo->dtls_pkt) {
vpninfo->dtls_pkt = malloc(sizeof(struct pkt) + len);
vpninfo->dtls_pkt = alloc_pkt(vpninfo, len);
if (!vpninfo->dtls_pkt) {
vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
break;
Expand Down Expand Up @@ -448,7 +448,7 @@ int dtls_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
vpn_progress(vpninfo, PRG_TRACE,
_("Sent DTLS packet of %d bytes; DTLS send returned %d\n"),
this->len, ret);
free(this);
free_pkt(vpninfo, this);
}

return work_done;
Expand Down
12 changes: 6 additions & 6 deletions esp.c
Expand Up @@ -156,7 +156,7 @@ int esp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
struct pkt *pkt;

if (!vpninfo->dtls_pkt) {
vpninfo->dtls_pkt = malloc(sizeof(struct pkt) + len);
vpninfo->dtls_pkt = alloc_pkt(vpninfo, len);
if (!vpninfo->dtls_pkt) {
vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
break;
Expand Down Expand Up @@ -244,7 +244,7 @@ int esp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
}
}
if (pkt->data[len - 1] == 0x05) {
struct pkt *newpkt = malloc(sizeof(*pkt) + receive_mtu + vpninfo->pkt_trailer);
struct pkt *newpkt = alloc_pkt(vpninfo, receive_mtu + vpninfo->pkt_trailer);
int newlen = receive_mtu;
if (!newpkt) {
vpn_progress(vpninfo, PRG_ERR,
Expand All @@ -255,7 +255,7 @@ int esp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
pkt->data, &pkt->len) || pkt->len) {
vpn_progress(vpninfo, PRG_ERR,
_("LZO decompression of ESP packet failed\n"));
free(newpkt);
free_pkt(vpninfo, newpkt);
continue;
}
newpkt->len = receive_mtu - newlen;
Expand Down Expand Up @@ -344,7 +344,7 @@ int esp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
len = construct_esp_packet(vpninfo, this, 0);
if (len < 0) {
/* Should we disable ESP? */
free(this);
free_pkt(vpninfo, this);
work_done = 1;
continue;
}
Expand Down Expand Up @@ -378,7 +378,7 @@ int esp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
unmonitor_write_fd(vpninfo, dtls);
vpninfo->deflate_pkt = NULL;
}
free(this);
free_pkt(vpninfo, this);
work_done = 1;
}

Expand All @@ -399,7 +399,7 @@ void esp_close(struct openconnect_info *vpninfo)
if (vpninfo->dtls_state > DTLS_DISABLED)
vpninfo->dtls_state = DTLS_SLEEPING;
if (vpninfo->deflate_pkt) {
free(vpninfo->deflate_pkt);
free_pkt(vpninfo, vpninfo->deflate_pkt);
vpninfo->deflate_pkt = NULL;
}
}
Expand Down
10 changes: 5 additions & 5 deletions gpst.c
Expand Up @@ -1128,7 +1128,7 @@ int gpst_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
int len, payload_len;

if (!vpninfo->cstp_pkt) {
vpninfo->cstp_pkt = malloc(sizeof(struct pkt) + receive_mtu);
vpninfo->cstp_pkt = alloc_pkt(vpninfo, receive_mtu);
if (!vpninfo->cstp_pkt) {
vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
break;
Expand Down Expand Up @@ -1239,7 +1239,7 @@ int gpst_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
}
/* Don't free the 'special' packets */
if (vpninfo->current_ssl_pkt != &dpd_pkt)
free(vpninfo->current_ssl_pkt);
free_pkt(vpninfo, vpninfo->current_ssl_pkt);

vpninfo->current_ssl_pkt = NULL;
}
Expand Down Expand Up @@ -1379,14 +1379,14 @@ int gpst_esp_send_probes(struct openconnect_info *vpninfo)
plen = sizeof(struct ip6_hdr) + icmplen;
else
plen = sizeof(struct ip) + icmplen;
struct pkt *pkt = malloc(sizeof(*pkt) + plen + vpninfo->pkt_trailer);
struct pkt *pkt = alloc_pkt(vpninfo, plen + vpninfo->pkt_trailer);
if (!pkt)
return -ENOMEM;

if (vpninfo->dtls_fd == -1) {
int fd = udp_connect(vpninfo);
if (fd < 0) {
free(pkt);
free_pkt(vpninfo, pkt);
return fd;
}
/* We are not connected until we get an ESP packet back */
Expand Down Expand Up @@ -1496,7 +1496,7 @@ int gpst_esp_send_probes(struct openconnect_info *vpninfo)
vpn_progress(vpninfo, PRG_DEBUG, _("Failed to send ESP probe\n"));
}

free(pkt);
free_pkt(vpninfo, pkt);

vpninfo->dtls_times.last_tx = time(&vpninfo->new_dtls_started);

Expand Down
8 changes: 4 additions & 4 deletions library.c
Expand Up @@ -687,10 +687,10 @@ void openconnect_vpninfo_free(struct openconnect_info *vpninfo)
inflateEnd(&vpninfo->inflate_strm);
deflateEnd(&vpninfo->deflate_strm);

free(vpninfo->deflate_pkt);
free(vpninfo->tun_pkt);
free(vpninfo->dtls_pkt);
free(vpninfo->cstp_pkt);
free_pkt(vpninfo, vpninfo->deflate_pkt);
free_pkt(vpninfo, vpninfo->tun_pkt);
free_pkt(vpninfo, vpninfo->dtls_pkt);
free_pkt(vpninfo, vpninfo->cstp_pkt);
free(vpninfo->bearer_token);
free(vpninfo);
}
Expand Down
6 changes: 3 additions & 3 deletions mainloop.c
Expand Up @@ -53,7 +53,7 @@ int tun_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
if (!tun_is_up(vpninfo)) {
/* no tun yet; clear any queued packets */
while ((this = dequeue_packet(&vpninfo->incoming_queue)))
free(this);
free_pkt(vpninfo, this);

return 0;
}
Expand All @@ -64,7 +64,7 @@ int tun_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
int len = vpninfo->ip_info.mtu;

if (!out_pkt) {
out_pkt = malloc(sizeof(struct pkt) + len + vpninfo->pkt_trailer);
out_pkt = alloc_pkt(vpninfo, len + vpninfo->pkt_trailer);
if (!out_pkt) {
vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
break;
Expand Down Expand Up @@ -104,7 +104,7 @@ int tun_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
vpninfo->stats.rx_pkts++;
vpninfo->stats.rx_bytes += this->len;

free(this);
free_pkt(vpninfo, this);
}
/* Work is not done if we just got rid of packets off the queue */
return work_done;
Expand Down
12 changes: 6 additions & 6 deletions oncp.c
Expand Up @@ -730,7 +730,7 @@ int oncp_connect(struct openconnect_info *vpninfo)
buf_free(reqbuf);

vpninfo->partial_rec_size = 0;
free(vpninfo->cstp_pkt);
free_pkt(vpninfo, vpninfo->cstp_pkt);
vpninfo->cstp_pkt = NULL;

return ret;
Expand Down Expand Up @@ -858,7 +858,7 @@ int oncp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)

len = receive_mtu + vpninfo->pkt_trailer;
if (!vpninfo->cstp_pkt) {
vpninfo->cstp_pkt = malloc(sizeof(struct pkt) + len);
vpninfo->cstp_pkt = alloc_pkt(vpninfo, len);
if (!vpninfo->cstp_pkt) {
vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
break;
Expand Down Expand Up @@ -1080,7 +1080,7 @@ int oncp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
}
/* Don't free the 'special' packets */
if (vpninfo->current_ssl_pkt == vpninfo->deflate_pkt) {
free(vpninfo->pending_deflated_pkt);
free_pkt(vpninfo, vpninfo->pending_deflated_pkt);
vpninfo->pending_deflated_pkt = NULL;
} else if (vpninfo->current_ssl_pkt == &esp_enable_pkt) {
/* Only set the ESP state to connected and actually start
Expand All @@ -1091,7 +1091,7 @@ int oncp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
vpninfo->dtls_state = DTLS_ESTABLISHED;
work_done = 1;
} else {
free(vpninfo->current_ssl_pkt);
free_pkt(vpninfo, vpninfo->current_ssl_pkt);
}
vpninfo->current_ssl_pkt = NULL;
}
Expand Down Expand Up @@ -1254,7 +1254,7 @@ int oncp_esp_send_probes(struct openconnect_info *vpninfo)
monitor_except_fd(vpninfo, dtls);
}

pkt = malloc(sizeof(*pkt) + 1 + vpninfo->pkt_trailer);
pkt = alloc_pkt(vpninfo, 1 + vpninfo->pkt_trailer);
if (!pkt)
return -ENOMEM;

Expand All @@ -1267,7 +1267,7 @@ int oncp_esp_send_probes(struct openconnect_info *vpninfo)
send(vpninfo->dtls_fd, (void *)&pkt->esp, pktlen, 0) < 0)
vpn_progress(vpninfo, PRG_DEBUG, _("Failed to send ESP probe\n"));
}
free(pkt);
free_pkt(vpninfo, pkt);

vpninfo->dtls_times.last_tx = time(&vpninfo->new_dtls_started);

Expand Down
11 changes: 11 additions & 0 deletions openconnect-internal.h
Expand Up @@ -388,6 +388,17 @@ static inline void init_pkt_queue(struct pkt_q *q)
q->tail = &q->head;
}


static inline struct pkt *alloc_pkt(struct openconnect_info *vpninfo, int len)
{
return malloc(sizeof(struct pkt) + len);
}

static inline void free_pkt(struct openconnect_info *vpninfo, struct pkt *pkt)
{
free(pkt);
}

#define TLS_OVERHEAD 5 /* packet + header */
#define DTLS_OVERHEAD (1 /* packet + header */ + 13 /* DTLS header */ + \
20 /* biggest supported MAC (SHA1) */ + 32 /* biggest supported IV (AES-256) */ + \
Expand Down
14 changes: 7 additions & 7 deletions ppp.c
Expand Up @@ -888,7 +888,7 @@ static int handle_state_transition(struct openconnect_info *vpninfo, int dtls,

/* Drop any failed outgoing packet from previous connection;
* we need to reconfigure before we can send data packets. */
free(vpninfo->current_ssl_pkt);
free_pkt(vpninfo, vpninfo->current_ssl_pkt);
vpninfo->current_ssl_pkt = NULL;
vpninfo->partial_rec_size = 0;
ppp->ppp_state = PPPS_ESTABLISH;
Expand Down Expand Up @@ -1089,7 +1089,7 @@ static int ppp_mainloop(struct openconnect_info *vpninfo, int dtls,
int len, payload_len, next_len;

if (!vpninfo->cstp_pkt) {
vpninfo->cstp_pkt = malloc(sizeof(struct pkt) + receive_mtu);
vpninfo->cstp_pkt = alloc_pkt(vpninfo, receive_mtu);
if (!vpninfo->cstp_pkt) {
vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
break;
Expand Down Expand Up @@ -1333,7 +1333,7 @@ static int ppp_mainloop(struct openconnect_info *vpninfo, int dtls,
* full sized packet so it can remain in vpninfo->cstp_pkt and be reused
* for receiving the next packet, if it's something other than data and
* doesn't get queued and freed. */
this = vpninfo->cstp_pkt = malloc(sizeof(struct pkt) + receive_mtu);
this = vpninfo->cstp_pkt = alloc_pkt(vpninfo, receive_mtu);
if (!this)
return -ENOMEM;
eh = this->data - rsv_hdr_size;
Expand Down Expand Up @@ -1384,7 +1384,7 @@ static int ppp_mainloop(struct openconnect_info *vpninfo, int dtls,
return 1;
}

free(this);
free_pkt(vpninfo, this);
vpninfo->current_ssl_pkt = NULL;
}

Expand Down Expand Up @@ -1469,7 +1469,7 @@ static int ppp_mainloop(struct openconnect_info *vpninfo, int dtls,
proto == PPP_LCP ? ASYNCMAP_LCP : ppp->out_asyncmap);
if (!this)
return 1; /* XX */
free(vpninfo->current_ssl_pkt);
free_pkt(vpninfo, vpninfo->current_ssl_pkt);
vpninfo->current_ssl_pkt = this;
}

Expand Down Expand Up @@ -1676,7 +1676,7 @@ int ppp_udp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readabl
* may be in active use while we attempt to connect DTLS.
* So use vpninfo->dtls_pkt for this. */
if (!vpninfo->dtls_pkt)
vpninfo->dtls_pkt = malloc(sizeof(struct pkt) + receive_mtu);
vpninfo->dtls_pkt = alloc_pkt(vpninfo, receive_mtu);
if (!vpninfo->dtls_pkt) {
vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
dtls_close(vpninfo);
Expand Down Expand Up @@ -1707,7 +1707,7 @@ int ppp_udp_mainloop(struct openconnect_info *vpninfo, int *timeout, int readabl
} else if (ret > 0) {
vpninfo->dtls_state = DTLS_ESTABLISHED;
vpninfo->dtls_pkt = NULL;
free(this);
free_pkt(vpninfo, this);

/* We are going to take over the PPP now; reset the TCP one */
ret = ppp_reset(vpninfo);
Expand Down

0 comments on commit 6e7f451

Please sign in to comment.