Skip to content

Commit cf533ea

Browse files
Eric Dumazetdavem330
authored andcommitted
tcp: add const qualifiers where possible
Adding const qualifiers to pointers can ease code review, and spot some bugs. It might allow compiler to optimize code further. For example, is it legal to temporary write a null cksum into tcphdr in tcp_md5_hash_header() ? I am afraid a sniffer could catch the temporary null value... Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent f04565d commit cf533ea

11 files changed

Lines changed: 160 additions & 152 deletions

File tree

‎include/net/secure_seq.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extern u32 secure_ipv6_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
1010
__be16 dport);
1111
extern __u32 secure_tcp_sequence_number(__be32 saddr, __be32 daddr,
1212
__be16 sport, __be16 dport);
13-
extern __u32 secure_tcpv6_sequence_number(__be32 *saddr, __be32 *daddr,
13+
extern __u32 secure_tcpv6_sequence_number(const __be32 *saddr, const __be32 *daddr,
1414
__be16 sport, __be16 dport);
1515
extern u64 secure_dccp_sequence_number(__be32 saddr, __be32 daddr,
1616
__be16 sport, __be16 dport);

‎include/net/tcp.h‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,9 @@ extern int tcp_sendpage(struct sock *sk, struct page *page, int offset,
327327
size_t size, int flags);
328328
extern int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
329329
extern int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
330-
struct tcphdr *th, unsigned len);
330+
const struct tcphdr *th, unsigned int len);
331331
extern int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
332-
struct tcphdr *th, unsigned len);
332+
const struct tcphdr *th, unsigned int len);
333333
extern void tcp_rcv_space_adjust(struct sock *sk);
334334
extern void tcp_cleanup_rbuf(struct sock *sk, int copied);
335335
extern int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
@@ -401,10 +401,10 @@ extern void tcp_set_keepalive(struct sock *sk, int val);
401401
extern void tcp_syn_ack_timeout(struct sock *sk, struct request_sock *req);
402402
extern int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
403403
size_t len, int nonblock, int flags, int *addr_len);
404-
extern void tcp_parse_options(struct sk_buff *skb,
405-
struct tcp_options_received *opt_rx, u8 **hvpp,
404+
extern void tcp_parse_options(const struct sk_buff *skb,
405+
struct tcp_options_received *opt_rx, const u8 **hvpp,
406406
int estab);
407-
extern u8 *tcp_parse_md5sig_option(struct tcphdr *th);
407+
extern const u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
408408

409409
/*
410410
* TCP v4 functions exported for the inet6 API
@@ -450,7 +450,7 @@ extern bool cookie_check_timestamp(struct tcp_options_received *opt, bool *);
450450
/* From net/ipv6/syncookies.c */
451451
extern struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
452452
#ifdef CONFIG_SYN_COOKIES
453-
extern __u32 cookie_v6_init_sequence(struct sock *sk, struct sk_buff *skb,
453+
extern __u32 cookie_v6_init_sequence(struct sock *sk, const struct sk_buff *skb,
454454
__u16 *mss);
455455
#else
456456
static inline __u32 cookie_v6_init_sequence(struct sock *sk,
@@ -522,7 +522,7 @@ static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
522522
}
523523

524524
/* tcp.c */
525-
extern void tcp_get_info(struct sock *, struct tcp_info *);
525+
extern void tcp_get_info(const struct sock *, struct tcp_info *);
526526

527527
/* Read 'sendfile()'-style from a TCP socket */
528528
typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
@@ -532,8 +532,8 @@ extern int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
532532

533533
extern void tcp_initialize_rcv_mss(struct sock *sk);
534534

535-
extern int tcp_mtu_to_mss(struct sock *sk, int pmtu);
536-
extern int tcp_mss_to_mtu(struct sock *sk, int mss);
535+
extern int tcp_mtu_to_mss(const struct sock *sk, int pmtu);
536+
extern int tcp_mss_to_mtu(const struct sock *sk, int mss);
537537
extern void tcp_mtup_init(struct sock *sk);
538538
extern void tcp_valid_rtt_meas(struct sock *sk, u32 seq_rtt);
539539

@@ -574,7 +574,7 @@ static inline void tcp_fast_path_check(struct sock *sk)
574574
/* Compute the actual rto_min value */
575575
static inline u32 tcp_rto_min(struct sock *sk)
576576
{
577-
struct dst_entry *dst = __sk_dst_get(sk);
577+
const struct dst_entry *dst = __sk_dst_get(sk);
578578
u32 rto_min = TCP_RTO_MIN;
579579

580580
if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
@@ -820,6 +820,7 @@ static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
820820
static inline __u32 tcp_current_ssthresh(const struct sock *sk)
821821
{
822822
const struct tcp_sock *tp = tcp_sk(sk);
823+
823824
if ((1 << inet_csk(sk)->icsk_ca_state) & (TCPF_CA_CWR | TCPF_CA_Recovery))
824825
return tp->snd_ssthresh;
825826
else
@@ -832,7 +833,7 @@ static inline __u32 tcp_current_ssthresh(const struct sock *sk)
832833
#define tcp_verify_left_out(tp) WARN_ON(tcp_left_out(tp) > tp->packets_out)
833834

834835
extern void tcp_enter_cwr(struct sock *sk, const int set_ssthresh);
835-
extern __u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst);
836+
extern __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
836837

837838
/* Slow start with delack produces 3 packets of burst, so that
838839
* it is safe "de facto". This will be the default - same as
@@ -861,7 +862,7 @@ static inline void tcp_minshall_update(struct tcp_sock *tp, unsigned int mss,
861862

862863
static inline void tcp_check_probe_timer(struct sock *sk)
863864
{
864-
struct tcp_sock *tp = tcp_sk(sk);
865+
const struct tcp_sock *tp = tcp_sk(sk);
865866
const struct inet_connection_sock *icsk = inet_csk(sk);
866867

867868
if (!tp->packets_out && !icsk->icsk_pending)
@@ -1209,10 +1210,10 @@ extern struct tcp_md5sig_pool *tcp_get_md5sig_pool(void);
12091210
extern void tcp_put_md5sig_pool(void);
12101211

12111212
extern int tcp_md5_hash_header(struct tcp_md5sig_pool *, struct tcphdr *);
1212-
extern int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, struct sk_buff *,
1213+
extern int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *,
12131214
unsigned header_len);
12141215
extern int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
1215-
struct tcp_md5sig_key *key);
1216+
const struct tcp_md5sig_key *key);
12161217

12171218
/* write queue abstraction */
12181219
static inline void tcp_write_queue_purge(struct sock *sk)
@@ -1225,22 +1226,24 @@ static inline void tcp_write_queue_purge(struct sock *sk)
12251226
tcp_clear_all_retrans_hints(tcp_sk(sk));
12261227
}
12271228

1228-
static inline struct sk_buff *tcp_write_queue_head(struct sock *sk)
1229+
static inline struct sk_buff *tcp_write_queue_head(const struct sock *sk)
12291230
{
12301231
return skb_peek(&sk->sk_write_queue);
12311232
}
12321233

1233-
static inline struct sk_buff *tcp_write_queue_tail(struct sock *sk)
1234+
static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
12341235
{
12351236
return skb_peek_tail(&sk->sk_write_queue);
12361237
}
12371238

1238-
static inline struct sk_buff *tcp_write_queue_next(struct sock *sk, struct sk_buff *skb)
1239+
static inline struct sk_buff *tcp_write_queue_next(const struct sock *sk,
1240+
const struct sk_buff *skb)
12391241
{
12401242
return skb_queue_next(&sk->sk_write_queue, skb);
12411243
}
12421244

1243-
static inline struct sk_buff *tcp_write_queue_prev(struct sock *sk, struct sk_buff *skb)
1245+
static inline struct sk_buff *tcp_write_queue_prev(const struct sock *sk,
1246+
const struct sk_buff *skb)
12441247
{
12451248
return skb_queue_prev(&sk->sk_write_queue, skb);
12461249
}
@@ -1254,7 +1257,7 @@ static inline struct sk_buff *tcp_write_queue_prev(struct sock *sk, struct sk_bu
12541257
#define tcp_for_write_queue_from_safe(skb, tmp, sk) \
12551258
skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
12561259

1257-
static inline struct sk_buff *tcp_send_head(struct sock *sk)
1260+
static inline struct sk_buff *tcp_send_head(const struct sock *sk)
12581261
{
12591262
return sk->sk_send_head;
12601263
}
@@ -1265,7 +1268,7 @@ static inline bool tcp_skb_is_last(const struct sock *sk,
12651268
return skb_queue_is_last(&sk->sk_write_queue, skb);
12661269
}
12671270

1268-
static inline void tcp_advance_send_head(struct sock *sk, struct sk_buff *skb)
1271+
static inline void tcp_advance_send_head(struct sock *sk, const struct sk_buff *skb)
12691272
{
12701273
if (tcp_skb_is_last(sk, skb))
12711274
sk->sk_send_head = NULL;

‎net/core/secure_seq.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static u32 seq_scale(u32 seq)
3535
}
3636

3737
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
38-
__u32 secure_tcpv6_sequence_number(__be32 *saddr, __be32 *daddr,
38+
__u32 secure_tcpv6_sequence_number(const __be32 *saddr, const __be32 *daddr,
3939
__be16 sport, __be16 dport)
4040
{
4141
u32 secret[MD5_MESSAGE_BYTES / 4];

‎net/ipv4/syncookies.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
265265
struct ip_options *opt)
266266
{
267267
struct tcp_options_received tcp_opt;
268-
u8 *hash_location;
268+
const u8 *hash_location;
269269
struct inet_request_sock *ireq;
270270
struct tcp_request_sock *treq;
271271
struct tcp_sock *tp = tcp_sk(sk);

‎net/ipv4/tcp.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ unsigned int tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
374374
{
375375
unsigned int mask;
376376
struct sock *sk = sock->sk;
377-
struct tcp_sock *tp = tcp_sk(sk);
377+
const struct tcp_sock *tp = tcp_sk(sk);
378378

379379
sock_poll_wait(file, sk_sleep(sk), wait);
380380
if (sk->sk_state == TCP_LISTEN)
@@ -528,7 +528,7 @@ static inline void tcp_mark_push(struct tcp_sock *tp, struct sk_buff *skb)
528528
tp->pushed_seq = tp->write_seq;
529529
}
530530

531-
static inline int forced_push(struct tcp_sock *tp)
531+
static inline int forced_push(const struct tcp_sock *tp)
532532
{
533533
return after(tp->write_seq, tp->pushed_seq + (tp->max_window >> 1));
534534
}
@@ -891,9 +891,9 @@ EXPORT_SYMBOL(tcp_sendpage);
891891
#define TCP_PAGE(sk) (sk->sk_sndmsg_page)
892892
#define TCP_OFF(sk) (sk->sk_sndmsg_off)
893893

894-
static inline int select_size(struct sock *sk, int sg)
894+
static inline int select_size(const struct sock *sk, int sg)
895895
{
896-
struct tcp_sock *tp = tcp_sk(sk);
896+
const struct tcp_sock *tp = tcp_sk(sk);
897897
int tmp = tp->mss_cache;
898898

899899
if (sg) {
@@ -2408,7 +2408,7 @@ static int do_tcp_setsockopt(struct sock *sk, int level,
24082408
int tcp_setsockopt(struct sock *sk, int level, int optname, char __user *optval,
24092409
unsigned int optlen)
24102410
{
2411-
struct inet_connection_sock *icsk = inet_csk(sk);
2411+
const struct inet_connection_sock *icsk = inet_csk(sk);
24122412

24132413
if (level != SOL_TCP)
24142414
return icsk->icsk_af_ops->setsockopt(sk, level, optname,
@@ -2430,9 +2430,9 @@ EXPORT_SYMBOL(compat_tcp_setsockopt);
24302430
#endif
24312431

24322432
/* Return information about state of tcp endpoint in API format. */
2433-
void tcp_get_info(struct sock *sk, struct tcp_info *info)
2433+
void tcp_get_info(const struct sock *sk, struct tcp_info *info)
24342434
{
2435-
struct tcp_sock *tp = tcp_sk(sk);
2435+
const struct tcp_sock *tp = tcp_sk(sk);
24362436
const struct inet_connection_sock *icsk = inet_csk(sk);
24372437
u32 now = tcp_time_stamp;
24382438

@@ -3010,7 +3010,7 @@ int tcp_md5_hash_header(struct tcp_md5sig_pool *hp,
30103010
EXPORT_SYMBOL(tcp_md5_hash_header);
30113011

30123012
int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp,
3013-
struct sk_buff *skb, unsigned header_len)
3013+
const struct sk_buff *skb, unsigned int header_len)
30143014
{
30153015
struct scatterlist sg;
30163016
const struct tcphdr *tp = tcp_hdr(skb);
@@ -3043,7 +3043,7 @@ int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *hp,
30433043
}
30443044
EXPORT_SYMBOL(tcp_md5_hash_skb_data);
30453045

3046-
int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, struct tcp_md5sig_key *key)
3046+
int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, const struct tcp_md5sig_key *key)
30473047
{
30483048
struct scatterlist sg;
30493049

0 commit comments

Comments
 (0)