Skip to content

Commit 3541f9e

Browse files
edumazetdavem330
authored andcommitted
tcp: add tcp_mss_clamp() helper
Small cleanup factorizing code doing the TCP_MAXSEG clamping. Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent ff3edc9 commit 3541f9e

File tree

5 files changed

+17
-23
lines changed

5 files changed

+17
-23
lines changed

include/linux/tcp.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,13 @@ static inline void tcp_saved_syn_free(struct tcp_sock *tp)
445445

446446
struct sk_buff *tcp_get_timestamping_opt_stats(const struct sock *sk);
447447

448+
static inline u16 tcp_mss_clamp(const struct tcp_sock *tp, u16 mss)
449+
{
450+
/* We use READ_ONCE() here because socket might not be locked.
451+
* This happens for listeners.
452+
*/
453+
u16 user_mss = READ_ONCE(tp->rx_opt.user_mss);
454+
455+
return (user_mss && user_mss < mss) ? user_mss : mss;
456+
}
448457
#endif /* _LINUX_TCP_H */

net/ipv4/tcp_ipv4.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,10 +1324,7 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
13241324
tcp_ca_openreq_child(newsk, dst);
13251325

13261326
tcp_sync_mss(newsk, dst_mtu(dst));
1327-
newtp->advmss = dst_metric_advmss(dst);
1328-
if (tcp_sk(sk)->rx_opt.user_mss &&
1329-
tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
1330-
newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
1327+
newtp->advmss = tcp_mss_clamp(tcp_sk(sk), dst_metric_advmss(dst));
13311328

13321329
tcp_initialize_rcv_mss(newsk);
13331330

net/ipv4/tcp_minisocks.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,12 @@ void tcp_openreq_init_rwin(struct request_sock *req,
360360
{
361361
struct inet_request_sock *ireq = inet_rsk(req);
362362
const struct tcp_sock *tp = tcp_sk(sk_listener);
363-
u16 user_mss = READ_ONCE(tp->rx_opt.user_mss);
364363
int full_space = tcp_full_space(sk_listener);
365-
int mss = dst_metric_advmss(dst);
366364
u32 window_clamp;
367365
__u8 rcv_wscale;
366+
int mss;
368367

369-
if (user_mss && user_mss < mss)
370-
mss = user_mss;
371-
368+
mss = tcp_mss_clamp(tp, dst_metric_advmss(dst));
372369
window_clamp = READ_ONCE(tp->window_clamp);
373370
/* Set this up on the first call only */
374371
req->rsk_window_clamp = window_clamp ? : dst_metric(dst, RTAX_WINDOW);

net/ipv4/tcp_output.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3062,7 +3062,6 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
30623062
struct sk_buff *skb;
30633063
int tcp_header_size;
30643064
struct tcphdr *th;
3065-
u16 user_mss;
30663065
int mss;
30673066

30683067
skb = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
@@ -3092,10 +3091,7 @@ struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
30923091
}
30933092
skb_dst_set(skb, dst);
30943093

3095-
mss = dst_metric_advmss(dst);
3096-
user_mss = READ_ONCE(tp->rx_opt.user_mss);
3097-
if (user_mss && user_mss < mss)
3098-
mss = user_mss;
3094+
mss = tcp_mss_clamp(tp, dst_metric_advmss(dst));
30993095

31003096
memset(&opts, 0, sizeof(opts));
31013097
#ifdef CONFIG_SYN_COOKIES
@@ -3201,9 +3197,7 @@ static void tcp_connect_init(struct sock *sk)
32013197

32023198
if (!tp->window_clamp)
32033199
tp->window_clamp = dst_metric(dst, RTAX_WINDOW);
3204-
tp->advmss = dst_metric_advmss(dst);
3205-
if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < tp->advmss)
3206-
tp->advmss = tp->rx_opt.user_mss;
3200+
tp->advmss = tcp_mss_clamp(tp, dst_metric_advmss(dst));
32073201

32083202
tcp_initialize_rcv_mss(sk);
32093203

@@ -3280,8 +3274,8 @@ static int tcp_send_syn_data(struct sock *sk, struct sk_buff *syn)
32803274
* user-MSS. Reserve maximum option space for middleboxes that add
32813275
* private TCP options. The cost is reduced data space in SYN :(
32823276
*/
3283-
if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < tp->rx_opt.mss_clamp)
3284-
tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;
3277+
tp->rx_opt.mss_clamp = tcp_mss_clamp(tp, tp->rx_opt.mss_clamp);
3278+
32853279
space = __tcp_mtu_to_mss(sk, inet_csk(sk)->icsk_pmtu_cookie) -
32863280
MAX_TCP_OPTION_SPACE;
32873281

net/ipv6/tcp_ipv6.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,10 +1147,7 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
11471147
tcp_ca_openreq_child(newsk, dst);
11481148

11491149
tcp_sync_mss(newsk, dst_mtu(dst));
1150-
newtp->advmss = dst_metric_advmss(dst);
1151-
if (tcp_sk(sk)->rx_opt.user_mss &&
1152-
tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
1153-
newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
1150+
newtp->advmss = tcp_mss_clamp(tcp_sk(sk), dst_metric_advmss(dst));
11541151

11551152
tcp_initialize_rcv_mss(newsk);
11561153

0 commit comments

Comments
 (0)