Skip to content

Commit da7dfaa

Browse files
0x7f454c46Paolo Abeni
authored andcommitted
net/tcp: Consistently align TCP-AO option in the header
Currently functions that pre-calculate TCP header options length use unaligned TCP-AO header + MAC-length for skb reservation. And the functions that actually write TCP-AO options into skb do align the header. Nothing good can come out of this for ((maclen % 4) != 0). Provide tcp_ao_len_aligned() helper and use it everywhere for TCP header options space calculations. Fixes: 1e03d32 ("net/tcp: Add TCP-AO sign to outgoing packets") Signed-off-by: Dmitry Safonov <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent 714589c commit da7dfaa

File tree

6 files changed

+15
-9
lines changed

6 files changed

+15
-9
lines changed

include/net/tcp_ao.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,17 @@ static inline int tcp_ao_maclen(const struct tcp_ao_key *key)
6262
return key->maclen;
6363
}
6464

65+
/* Use tcp_ao_len_aligned() for TCP header calculations */
6566
static inline int tcp_ao_len(const struct tcp_ao_key *key)
6667
{
6768
return tcp_ao_maclen(key) + sizeof(struct tcp_ao_hdr);
6869
}
6970

71+
static inline int tcp_ao_len_aligned(const struct tcp_ao_key *key)
72+
{
73+
return round_up(tcp_ao_len(key), 4);
74+
}
75+
7076
static inline unsigned int tcp_ao_digest_size(struct tcp_ao_key *key)
7177
{
7278
return key->digest_size;

net/ipv4/tcp_ao.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ void tcp_ao_connect_init(struct sock *sk)
11001100
ao_info->current_key = key;
11011101
if (!ao_info->rnext_key)
11021102
ao_info->rnext_key = key;
1103-
tp->tcp_header_len += tcp_ao_len(key);
1103+
tp->tcp_header_len += tcp_ao_len_aligned(key);
11041104

11051105
ao_info->lisn = htonl(tp->write_seq);
11061106
ao_info->snd_sne = 0;
@@ -1346,7 +1346,7 @@ static int tcp_ao_parse_crypto(struct tcp_ao_add *cmd, struct tcp_ao_key *key)
13461346
syn_tcp_option_space -= TCPOLEN_MSS_ALIGNED;
13471347
syn_tcp_option_space -= TCPOLEN_TSTAMP_ALIGNED;
13481348
syn_tcp_option_space -= TCPOLEN_WSCALE_ALIGNED;
1349-
if (tcp_ao_len(key) > syn_tcp_option_space) {
1349+
if (tcp_ao_len_aligned(key) > syn_tcp_option_space) {
13501350
err = -EMSGSIZE;
13511351
goto err_kfree;
13521352
}

net/ipv4/tcp_ipv4.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ static bool tcp_v4_ao_sign_reset(const struct sock *sk, struct sk_buff *skb,
690690

691691
reply_options[0] = htonl((TCPOPT_AO << 24) | (tcp_ao_len(key) << 16) |
692692
(aoh->rnext_keyid << 8) | keyid);
693-
arg->iov[0].iov_len += round_up(tcp_ao_len(key), 4);
693+
arg->iov[0].iov_len += tcp_ao_len_aligned(key);
694694
reply->doff = arg->iov[0].iov_len / 4;
695695

696696
if (tcp_ao_hash_hdr(AF_INET, (char *)&reply_options[1],
@@ -978,7 +978,7 @@ static void tcp_v4_send_ack(const struct sock *sk,
978978
(tcp_ao_len(key->ao_key) << 16) |
979979
(key->ao_key->sndid << 8) |
980980
key->rcv_next);
981-
arg.iov[0].iov_len += round_up(tcp_ao_len(key->ao_key), 4);
981+
arg.iov[0].iov_len += tcp_ao_len_aligned(key->ao_key);
982982
rep.th.doff = arg.iov[0].iov_len / 4;
983983

984984
tcp_ao_hash_hdr(AF_INET, (char *)&rep.opt[offset],

net/ipv4/tcp_minisocks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,
615615
ao_key = treq->af_specific->ao_lookup(sk, req,
616616
tcp_rsk(req)->ao_keyid, -1);
617617
if (ao_key)
618-
newtp->tcp_header_len += tcp_ao_len(ao_key);
618+
newtp->tcp_header_len += tcp_ao_len_aligned(ao_key);
619619
#endif
620620
if (skb->len >= TCP_MSS_DEFAULT + newtp->tcp_header_len)
621621
newicsk->icsk_ack.last_seg_size = skb->len - newtp->tcp_header_len;

net/ipv4/tcp_output.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
825825
timestamps = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_timestamps);
826826
if (tcp_key_is_ao(key)) {
827827
opts->options |= OPTION_AO;
828-
remaining -= tcp_ao_len(key->ao_key);
828+
remaining -= tcp_ao_len_aligned(key->ao_key);
829829
}
830830
}
831831

@@ -915,7 +915,7 @@ static unsigned int tcp_synack_options(const struct sock *sk,
915915
ireq->tstamp_ok &= !ireq->sack_ok;
916916
} else if (tcp_key_is_ao(key)) {
917917
opts->options |= OPTION_AO;
918-
remaining -= tcp_ao_len(key->ao_key);
918+
remaining -= tcp_ao_len_aligned(key->ao_key);
919919
ireq->tstamp_ok &= !ireq->sack_ok;
920920
}
921921

@@ -982,7 +982,7 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb
982982
size += TCPOLEN_MD5SIG_ALIGNED;
983983
} else if (tcp_key_is_ao(key)) {
984984
opts->options |= OPTION_AO;
985-
size += tcp_ao_len(key->ao_key);
985+
size += tcp_ao_len_aligned(key->ao_key);
986986
}
987987

988988
if (likely(tp->rx_opt.tstamp_ok)) {

net/ipv6/tcp_ipv6.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32
881881
if (tcp_key_is_md5(key))
882882
tot_len += TCPOLEN_MD5SIG_ALIGNED;
883883
if (tcp_key_is_ao(key))
884-
tot_len += tcp_ao_len(key->ao_key);
884+
tot_len += tcp_ao_len_aligned(key->ao_key);
885885

886886
#ifdef CONFIG_MPTCP
887887
if (rst && !tcp_key_is_md5(key)) {

0 commit comments

Comments
 (0)