Skip to content

Commit 26879da

Browse files
tracywwnjdavem330
authored andcommitted
ipv6: add new struct ipcm6_cookie
In the sendmsg function of UDP, raw, ICMP and l2tp sockets, we use local variables like hlimits, tclass, opt and dontfrag and pass them to corresponding functions like ip6_make_skb, ip6_append_data and xxx_push_pending_frames. This is not a good practice and makes it hard to add new parameters. This fix introduces a new struct ipcm6_cookie similar to ipcm_cookie in ipv4 and include the above mentioned variables. And we only pass the pointer to this structure to corresponding functions. This makes it easier to add new parameters in the future and makes the function cleaner. Signed-off-by: Wei Wang <weiwan@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1d2077a commit 26879da

11 files changed

Lines changed: 123 additions & 109 deletions

File tree

‎include/net/ipv6.h‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ struct ipv6_fl_socklist {
251251
struct rcu_head rcu;
252252
};
253253

254+
struct ipcm6_cookie {
255+
__s16 hlimit;
256+
__s16 tclass;
257+
__s8 dontfrag;
258+
struct ipv6_txoptions *opt;
259+
};
260+
254261
static inline struct ipv6_txoptions *txopt_get(const struct ipv6_pinfo *np)
255262
{
256263
struct ipv6_txoptions *opt;
@@ -863,9 +870,9 @@ int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr);
863870
int ip6_append_data(struct sock *sk,
864871
int getfrag(void *from, char *to, int offset, int len,
865872
int odd, struct sk_buff *skb),
866-
void *from, int length, int transhdrlen, int hlimit,
867-
int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6,
868-
struct rt6_info *rt, unsigned int flags, int dontfrag,
873+
void *from, int length, int transhdrlen,
874+
struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
875+
struct rt6_info *rt, unsigned int flags,
869876
const struct sockcm_cookie *sockc);
870877

871878
int ip6_push_pending_frames(struct sock *sk);
@@ -881,9 +888,8 @@ struct sk_buff *ip6_make_skb(struct sock *sk,
881888
int getfrag(void *from, char *to, int offset,
882889
int len, int odd, struct sk_buff *skb),
883890
void *from, int length, int transhdrlen,
884-
int hlimit, int tclass, struct ipv6_txoptions *opt,
885-
struct flowi6 *fl6, struct rt6_info *rt,
886-
unsigned int flags, int dontfrag,
891+
struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
892+
struct rt6_info *rt, unsigned int flags,
887893
const struct sockcm_cookie *sockc);
888894

889895
static inline struct sk_buff *ip6_finish_skb(struct sock *sk)

‎include/net/transp_v6.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ void ip6_datagram_recv_specific_ctl(struct sock *sk, struct msghdr *msg,
4141
struct sk_buff *skb);
4242

4343
int ip6_datagram_send_ctl(struct net *net, struct sock *sk, struct msghdr *msg,
44-
struct flowi6 *fl6, struct ipv6_txoptions *opt,
45-
int *hlimit, int *tclass, int *dontfrag,
44+
struct flowi6 *fl6, struct ipcm6_cookie *ipc6,
4645
struct sockcm_cookie *sockc);
4746

4847
void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,

‎net/ipv6/datagram.c‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -727,14 +727,13 @@ EXPORT_SYMBOL_GPL(ip6_datagram_recv_ctl);
727727

728728
int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
729729
struct msghdr *msg, struct flowi6 *fl6,
730-
struct ipv6_txoptions *opt,
731-
int *hlimit, int *tclass, int *dontfrag,
732-
struct sockcm_cookie *sockc)
730+
struct ipcm6_cookie *ipc6, struct sockcm_cookie *sockc)
733731
{
734732
struct in6_pktinfo *src_info;
735733
struct cmsghdr *cmsg;
736734
struct ipv6_rt_hdr *rthdr;
737735
struct ipv6_opt_hdr *hdr;
736+
struct ipv6_txoptions *opt = ipc6->opt;
738737
int len;
739738
int err = 0;
740739

@@ -953,8 +952,8 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
953952
goto exit_f;
954953
}
955954

956-
*hlimit = *(int *)CMSG_DATA(cmsg);
957-
if (*hlimit < -1 || *hlimit > 0xff) {
955+
ipc6->hlimit = *(int *)CMSG_DATA(cmsg);
956+
if (ipc6->hlimit < -1 || ipc6->hlimit > 0xff) {
958957
err = -EINVAL;
959958
goto exit_f;
960959
}
@@ -974,7 +973,7 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
974973
goto exit_f;
975974

976975
err = 0;
977-
*tclass = tc;
976+
ipc6->tclass = tc;
978977

979978
break;
980979
}
@@ -992,7 +991,7 @@ int ip6_datagram_send_ctl(struct net *net, struct sock *sk,
992991
goto exit_f;
993992

994993
err = 0;
995-
*dontfrag = df;
994+
ipc6->dontfrag = df;
996995

997996
break;
998997
}

‎net/ipv6/icmp.c‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,10 @@ static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info)
401401
struct flowi6 fl6;
402402
struct icmpv6_msg msg;
403403
struct sockcm_cookie sockc_unused = {0};
404+
struct ipcm6_cookie ipc6;
404405
int iif = 0;
405406
int addr_type = 0;
406407
int len;
407-
int hlimit;
408408
int err = 0;
409409
u32 mark = IP6_REPLY_MARK(net, skb->mark);
410410

@@ -507,7 +507,10 @@ static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info)
507507
if (IS_ERR(dst))
508508
goto out;
509509

510-
hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
510+
ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
511+
ipc6.tclass = np->tclass;
512+
ipc6.dontfrag = np->dontfrag;
513+
ipc6.opt = NULL;
511514

512515
msg.skb = skb;
513516
msg.offset = skb_network_offset(skb);
@@ -526,9 +529,9 @@ static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info)
526529

527530
err = ip6_append_data(sk, icmpv6_getfrag, &msg,
528531
len + sizeof(struct icmp6hdr),
529-
sizeof(struct icmp6hdr), hlimit,
530-
np->tclass, NULL, &fl6, (struct rt6_info *)dst,
531-
MSG_DONTWAIT, np->dontfrag, &sockc_unused);
532+
sizeof(struct icmp6hdr),
533+
&ipc6, &fl6, (struct rt6_info *)dst,
534+
MSG_DONTWAIT, &sockc_unused);
532535
if (err) {
533536
ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);
534537
ip6_flush_pending_frames(sk);
@@ -563,9 +566,8 @@ static void icmpv6_echo_reply(struct sk_buff *skb)
563566
struct flowi6 fl6;
564567
struct icmpv6_msg msg;
565568
struct dst_entry *dst;
569+
struct ipcm6_cookie ipc6;
566570
int err = 0;
567-
int hlimit;
568-
u8 tclass;
569571
u32 mark = IP6_REPLY_MARK(net, skb->mark);
570572
struct sockcm_cookie sockc_unused = {0};
571573

@@ -607,19 +609,21 @@ static void icmpv6_echo_reply(struct sk_buff *skb)
607609
if (IS_ERR(dst))
608610
goto out;
609611

610-
hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
611-
612612
idev = __in6_dev_get(skb->dev);
613613

614614
msg.skb = skb;
615615
msg.offset = 0;
616616
msg.type = ICMPV6_ECHO_REPLY;
617617

618-
tclass = ipv6_get_dsfield(ipv6_hdr(skb));
618+
ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
619+
ipc6.tclass = ipv6_get_dsfield(ipv6_hdr(skb));
620+
ipc6.dontfrag = np->dontfrag;
621+
ipc6.opt = NULL;
622+
619623
err = ip6_append_data(sk, icmpv6_getfrag, &msg, skb->len + sizeof(struct icmp6hdr),
620-
sizeof(struct icmp6hdr), hlimit, tclass, NULL, &fl6,
624+
sizeof(struct icmp6hdr), &ipc6, &fl6,
621625
(struct rt6_info *)dst, MSG_DONTWAIT,
622-
np->dontfrag, &sockc_unused);
626+
&sockc_unused);
623627

624628
if (err) {
625629
__ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTERRORS);

‎net/ipv6/ip6_flowlabel.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq,
373373
struct msghdr msg;
374374
struct flowi6 flowi6;
375375
struct sockcm_cookie sockc_junk;
376-
int junk;
376+
struct ipcm6_cookie ipc6;
377377

378378
err = -ENOMEM;
379379
fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
@@ -390,8 +390,8 @@ fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq,
390390
msg.msg_control = (void *)(fl->opt+1);
391391
memset(&flowi6, 0, sizeof(flowi6));
392392

393-
err = ip6_datagram_send_ctl(net, sk, &msg, &flowi6, fl->opt,
394-
&junk, &junk, &junk, &sockc_junk);
393+
ipc6.opt = fl->opt;
394+
err = ip6_datagram_send_ctl(net, sk, &msg, &flowi6, &ipc6, &sockc_junk);
395395
if (err)
396396
goto done;
397397
err = -EINVAL;

‎net/ipv6/ip6_output.c‎

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,12 +1182,12 @@ static void ip6_append_data_mtu(unsigned int *mtu,
11821182
}
11831183

11841184
static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork,
1185-
struct inet6_cork *v6_cork,
1186-
int hlimit, int tclass, struct ipv6_txoptions *opt,
1185+
struct inet6_cork *v6_cork, struct ipcm6_cookie *ipc6,
11871186
struct rt6_info *rt, struct flowi6 *fl6)
11881187
{
11891188
struct ipv6_pinfo *np = inet6_sk(sk);
11901189
unsigned int mtu;
1190+
struct ipv6_txoptions *opt = ipc6->opt;
11911191

11921192
/*
11931193
* setup for corking
@@ -1229,8 +1229,8 @@ static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork,
12291229
dst_hold(&rt->dst);
12301230
cork->base.dst = &rt->dst;
12311231
cork->fl.u.ip6 = *fl6;
1232-
v6_cork->hop_limit = hlimit;
1233-
v6_cork->tclass = tclass;
1232+
v6_cork->hop_limit = ipc6->hlimit;
1233+
v6_cork->tclass = ipc6->tclass;
12341234
if (rt->dst.flags & DST_XFRM_TUNNEL)
12351235
mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ?
12361236
rt->dst.dev->mtu : dst_mtu(&rt->dst);
@@ -1258,7 +1258,7 @@ static int __ip6_append_data(struct sock *sk,
12581258
int getfrag(void *from, char *to, int offset,
12591259
int len, int odd, struct sk_buff *skb),
12601260
void *from, int length, int transhdrlen,
1261-
unsigned int flags, int dontfrag,
1261+
unsigned int flags, struct ipcm6_cookie *ipc6,
12621262
const struct sockcm_cookie *sockc)
12631263
{
12641264
struct sk_buff *skb, *skb_prev = NULL;
@@ -1298,7 +1298,7 @@ static int __ip6_append_data(struct sock *sk,
12981298
sizeof(struct frag_hdr) : 0) +
12991299
rt->rt6i_nfheader_len;
13001300

1301-
if (cork->length + length > mtu - headersize && dontfrag &&
1301+
if (cork->length + length > mtu - headersize && ipc6->dontfrag &&
13021302
(sk->sk_protocol == IPPROTO_UDP ||
13031303
sk->sk_protocol == IPPROTO_RAW)) {
13041304
ipv6_local_rxpmtu(sk, fl6, mtu - headersize +
@@ -1564,9 +1564,9 @@ static int __ip6_append_data(struct sock *sk,
15641564
int ip6_append_data(struct sock *sk,
15651565
int getfrag(void *from, char *to, int offset, int len,
15661566
int odd, struct sk_buff *skb),
1567-
void *from, int length, int transhdrlen, int hlimit,
1568-
int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6,
1569-
struct rt6_info *rt, unsigned int flags, int dontfrag,
1567+
void *from, int length, int transhdrlen,
1568+
struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
1569+
struct rt6_info *rt, unsigned int flags,
15701570
const struct sockcm_cookie *sockc)
15711571
{
15721572
struct inet_sock *inet = inet_sk(sk);
@@ -1580,12 +1580,12 @@ int ip6_append_data(struct sock *sk,
15801580
/*
15811581
* setup for corking
15821582
*/
1583-
err = ip6_setup_cork(sk, &inet->cork, &np->cork, hlimit,
1584-
tclass, opt, rt, fl6);
1583+
err = ip6_setup_cork(sk, &inet->cork, &np->cork,
1584+
ipc6, rt, fl6);
15851585
if (err)
15861586
return err;
15871587

1588-
exthdrlen = (opt ? opt->opt_flen : 0);
1588+
exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
15891589
length += exthdrlen;
15901590
transhdrlen += exthdrlen;
15911591
} else {
@@ -1595,8 +1595,7 @@ int ip6_append_data(struct sock *sk,
15951595

15961596
return __ip6_append_data(sk, fl6, &sk->sk_write_queue, &inet->cork.base,
15971597
&np->cork, sk_page_frag(sk), getfrag,
1598-
from, length, transhdrlen, flags, dontfrag,
1599-
sockc);
1598+
from, length, transhdrlen, flags, ipc6, sockc);
16001599
}
16011600
EXPORT_SYMBOL_GPL(ip6_append_data);
16021601

@@ -1752,15 +1751,14 @@ struct sk_buff *ip6_make_skb(struct sock *sk,
17521751
int getfrag(void *from, char *to, int offset,
17531752
int len, int odd, struct sk_buff *skb),
17541753
void *from, int length, int transhdrlen,
1755-
int hlimit, int tclass,
1756-
struct ipv6_txoptions *opt, struct flowi6 *fl6,
1754+
struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
17571755
struct rt6_info *rt, unsigned int flags,
1758-
int dontfrag, const struct sockcm_cookie *sockc)
1756+
const struct sockcm_cookie *sockc)
17591757
{
17601758
struct inet_cork_full cork;
17611759
struct inet6_cork v6_cork;
17621760
struct sk_buff_head queue;
1763-
int exthdrlen = (opt ? opt->opt_flen : 0);
1761+
int exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
17641762
int err;
17651763

17661764
if (flags & MSG_PROBE)
@@ -1772,17 +1770,17 @@ struct sk_buff *ip6_make_skb(struct sock *sk,
17721770
cork.base.addr = 0;
17731771
cork.base.opt = NULL;
17741772
v6_cork.opt = NULL;
1775-
err = ip6_setup_cork(sk, &cork, &v6_cork, hlimit, tclass, opt, rt, fl6);
1773+
err = ip6_setup_cork(sk, &cork, &v6_cork, ipc6, rt, fl6);
17761774
if (err)
17771775
return ERR_PTR(err);
17781776

1779-
if (dontfrag < 0)
1780-
dontfrag = inet6_sk(sk)->dontfrag;
1777+
if (ipc6->dontfrag < 0)
1778+
ipc6->dontfrag = inet6_sk(sk)->dontfrag;
17811779

17821780
err = __ip6_append_data(sk, fl6, &queue, &cork.base, &v6_cork,
17831781
&current->task_frag, getfrag, from,
17841782
length + exthdrlen, transhdrlen + exthdrlen,
1785-
flags, dontfrag, sockc);
1783+
flags, ipc6, sockc);
17861784
if (err) {
17871785
__ip6_flush_pending_frames(sk, &queue, &cork, &v6_cork);
17881786
return ERR_PTR(err);

‎net/ipv6/ipv6_sockglue.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
473473
struct msghdr msg;
474474
struct flowi6 fl6;
475475
struct sockcm_cookie sockc_junk;
476-
int junk;
476+
struct ipcm6_cookie ipc6;
477477

478478
memset(&fl6, 0, sizeof(fl6));
479479
fl6.flowi6_oif = sk->sk_bound_dev_if;
@@ -503,9 +503,9 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
503503

504504
msg.msg_controllen = optlen;
505505
msg.msg_control = (void *)(opt+1);
506+
ipc6.opt = opt;
506507

507-
retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, opt, &junk,
508-
&junk, &junk, &sockc_junk);
508+
retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6, &sockc_junk);
509509
if (retv)
510510
goto done;
511511
update:

‎net/ipv6/ping.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
5858
int iif = 0;
5959
struct flowi6 fl6;
6060
int err;
61-
int hlimit;
6261
struct dst_entry *dst;
6362
struct rt6_info *rt;
6463
struct pingfakehdr pfh;
6564
struct sockcm_cookie junk = {0};
65+
struct ipcm6_cookie ipc6;
6666

6767
pr_debug("ping_v6_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
6868

@@ -139,13 +139,15 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
139139
pfh.wcheck = 0;
140140
pfh.family = AF_INET6;
141141

142-
hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
142+
ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
143+
ipc6.tclass = np->tclass;
144+
ipc6.dontfrag = np->dontfrag;
145+
ipc6.opt = NULL;
143146

144147
lock_sock(sk);
145148
err = ip6_append_data(sk, ping_getfrag, &pfh, len,
146-
0, hlimit,
147-
np->tclass, NULL, &fl6, rt,
148-
MSG_DONTWAIT, np->dontfrag, &junk);
149+
0, &ipc6, &fl6, rt,
150+
MSG_DONTWAIT, &junk);
149151

150152
if (err) {
151153
ICMP6_INC_STATS(sock_net(sk), rt->rt6i_idev,

0 commit comments

Comments
 (0)