Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'net-deduplicate-cookie-logic'

Willem de Bruijn says:

====================
net: deduplicate cookie logic

Reuse standard sk, ip and ipv6 cookie init handlers where possible.

Avoid repeated open coding of the same logic.
Harmonize feature sets across protocols.
Make IPv4 and IPv6 logic more alike.
Simplify adding future new fields with a single init point.
====================

Link: https://patch.msgid.link/20250214222720.3205500-1-willemdebruijn.kernel@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+30 -71
+5 -11
include/net/ip.h
··· 92 92 static inline void ipcm_init_sk(struct ipcm_cookie *ipcm, 93 93 const struct inet_sock *inet) 94 94 { 95 - ipcm_init(ipcm); 95 + *ipcm = (struct ipcm_cookie) { 96 + .tos = READ_ONCE(inet->tos), 97 + }; 96 98 97 - ipcm->sockc.mark = READ_ONCE(inet->sk.sk_mark); 98 - ipcm->sockc.priority = READ_ONCE(inet->sk.sk_priority); 99 - ipcm->sockc.tsflags = READ_ONCE(inet->sk.sk_tsflags); 99 + sockcm_init(&ipcm->sockc, &inet->sk); 100 + 100 101 ipcm->oif = READ_ONCE(inet->sk.sk_bound_dev_if); 101 102 ipcm->addr = inet->inet_saddr; 102 103 ipcm->protocol = inet->inet_num; ··· 256 255 return RT_SCOPE_LINK; 257 256 258 257 return RT_SCOPE_UNIVERSE; 259 - } 260 - 261 - static inline __u8 get_rttos(struct ipcm_cookie* ipc, struct inet_sock *inet) 262 - { 263 - u8 dsfield = ipc->tos != -1 ? ipc->tos : READ_ONCE(inet->tos); 264 - 265 - return dsfield & INET_DSCP_MASK; 266 258 } 267 259 268 260 /* datagram.c */
+2 -9
include/net/ipv6.h
··· 363 363 struct ipv6_txoptions *opt; 364 364 }; 365 365 366 - static inline void ipcm6_init(struct ipcm6_cookie *ipc6) 367 - { 368 - *ipc6 = (struct ipcm6_cookie) { 369 - .hlimit = -1, 370 - .tclass = -1, 371 - .dontfrag = -1, 372 - }; 373 - } 374 - 375 366 static inline void ipcm6_init_sk(struct ipcm6_cookie *ipc6, 376 367 const struct sock *sk) 377 368 { ··· 371 380 .tclass = inet6_sk(sk)->tclass, 372 381 .dontfrag = inet6_test_bit(DONTFRAG, sk), 373 382 }; 383 + 384 + sockcm_init(&ipc6->sockc, sk); 374 385 } 375 386 376 387 static inline struct ipv6_txoptions *txopt_get(const struct ipv6_pinfo *np)
+1
include/net/sock.h
··· 1829 1829 const struct sock *sk) 1830 1830 { 1831 1831 *sockc = (struct sockcm_cookie) { 1832 + .mark = READ_ONCE(sk->sk_mark), 1832 1833 .tsflags = READ_ONCE(sk->sk_tsflags), 1833 1834 .priority = READ_ONCE(sk->sk_priority), 1834 1835 };
+1 -1
net/can/raw.c
··· 963 963 964 964 skb->dev = dev; 965 965 skb->priority = sockc.priority; 966 - skb->mark = READ_ONCE(sk->sk_mark); 966 + skb->mark = sockc.mark; 967 967 skb->tstamp = sockc.transmit_time; 968 968 969 969 skb_setup_tx_timestamp(skb, &sockc);
+2 -4
net/ipv4/icmp.c
··· 405 405 struct ipcm_cookie ipc; 406 406 struct flowi4 fl4; 407 407 struct sock *sk; 408 - struct inet_sock *inet; 409 408 __be32 daddr, saddr; 410 409 u32 mark = IP4_REPLY_MARK(net, skb->mark); 411 410 int type = icmp_param->data.icmph.type; ··· 423 424 sk = icmp_xmit_lock(net); 424 425 if (!sk) 425 426 goto out_bh_enable; 426 - inet = inet_sk(sk); 427 427 428 428 icmp_param->data.icmph.checksum = 0; 429 429 430 430 ipcm_init(&ipc); 431 - inet->tos = ip_hdr(skb)->tos; 431 + ipc.tos = ip_hdr(skb)->tos; 432 432 ipc.sockc.mark = mark; 433 433 daddr = ipc.addr = ip_hdr(skb)->saddr; 434 434 saddr = fib_compute_spec_dst(skb); ··· 735 737 icmp_param.data.icmph.checksum = 0; 736 738 icmp_param.skb = skb_in; 737 739 icmp_param.offset = skb_network_offset(skb_in); 738 - inet_sk(sk)->tos = tos; 739 740 ipcm_init(&ipc); 741 + ipc.tos = tos; 740 742 ipc.addr = iph->saddr; 741 743 ipc.opt = &icmp_param.replyopts.opt; 742 744 ipc.sockc.mark = mark;
+3 -3
net/ipv4/ping.c
··· 705 705 struct ip_options_data opt_copy; 706 706 int free = 0; 707 707 __be32 saddr, daddr, faddr; 708 - u8 tos, scope; 708 + u8 scope; 709 709 int err; 710 710 711 711 pr_debug("ping_v4_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num); ··· 768 768 } 769 769 faddr = ipc.opt->opt.faddr; 770 770 } 771 - tos = get_rttos(&ipc, inet); 772 771 scope = ip_sendmsg_scope(inet, &ipc, msg); 773 772 774 773 if (ipv4_is_multicast(daddr)) { ··· 778 779 } else if (!ipc.oif) 779 780 ipc.oif = READ_ONCE(inet->uc_index); 780 781 781 - flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos, scope, 782 + flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, 783 + ipc.tos & INET_DSCP_MASK, scope, 782 784 sk->sk_protocol, inet_sk_flowi_flags(sk), faddr, 783 785 saddr, 0, 0, sk->sk_uid); 784 786
+3 -3
net/ipv4/raw.c
··· 486 486 struct ipcm_cookie ipc; 487 487 struct rtable *rt = NULL; 488 488 struct flowi4 fl4; 489 - u8 tos, scope; 489 + u8 scope; 490 490 int free = 0; 491 491 __be32 daddr; 492 492 __be32 saddr; ··· 581 581 daddr = ipc.opt->opt.faddr; 582 582 } 583 583 } 584 - tos = get_rttos(&ipc, inet); 585 584 scope = ip_sendmsg_scope(inet, &ipc, msg); 586 585 587 586 uc_index = READ_ONCE(inet->uc_index); ··· 605 606 } 606 607 } 607 608 608 - flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos, scope, 609 + flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, 610 + ipc.tos & INET_DSCP_MASK, scope, 609 611 hdrincl ? ipc.protocol : sk->sk_protocol, 610 612 inet_sk_flowi_flags(sk) | 611 613 (hdrincl ? FLOWI_FLAG_KNOWN_NH : 0),
+1 -1
net/ipv4/tcp.c
··· 1127 1127 /* 'common' sending to sendq */ 1128 1128 } 1129 1129 1130 - sockcm_init(&sockc, sk); 1130 + sockc = (struct sockcm_cookie) { .tsflags = READ_ONCE(sk->sk_tsflags)}; 1131 1131 if (msg->msg_controllen) { 1132 1132 err = sock_cmsg_send(sk, msg, &sockc); 1133 1133 if (unlikely(err)) {
+3 -3
net/ipv4/udp.c
··· 1280 1280 int free = 0; 1281 1281 int connected = 0; 1282 1282 __be32 daddr, faddr, saddr; 1283 - u8 tos, scope; 1283 + u8 scope; 1284 1284 __be16 dport; 1285 1285 int err, is_udplite = IS_UDPLITE(sk); 1286 1286 int corkreq = udp_test_bit(CORK, sk) || msg->msg_flags & MSG_MORE; ··· 1404 1404 faddr = ipc.opt->opt.faddr; 1405 1405 connected = 0; 1406 1406 } 1407 - tos = get_rttos(&ipc, inet); 1408 1407 scope = ip_sendmsg_scope(inet, &ipc, msg); 1409 1408 if (scope == RT_SCOPE_LINK) 1410 1409 connected = 0; ··· 1440 1441 1441 1442 fl4 = &fl4_stack; 1442 1443 1443 - flowi4_init_output(fl4, ipc.oif, ipc.sockc.mark, tos, scope, 1444 + flowi4_init_output(fl4, ipc.oif, ipc.sockc.mark, 1445 + ipc.tos & INET_DSCP_MASK, scope, 1444 1446 sk->sk_protocol, flow_flags, faddr, saddr, 1445 1447 dport, inet->inet_sport, sk->sk_uid); 1446 1448
-3
net/ipv6/ping.c
··· 119 119 return -EINVAL; 120 120 121 121 ipcm6_init_sk(&ipc6, sk); 122 - ipc6.sockc.priority = READ_ONCE(sk->sk_priority); 123 - ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags); 124 - ipc6.sockc.mark = READ_ONCE(sk->sk_mark); 125 122 126 123 fl6.flowi6_oif = oif; 127 124
+3 -12
net/ipv6/raw.c
··· 769 769 770 770 hdrincl = inet_test_bit(HDRINCL, sk); 771 771 772 + ipcm6_init_sk(&ipc6, sk); 773 + 772 774 /* 773 775 * Get and verify the address. 774 776 */ 775 777 memset(&fl6, 0, sizeof(fl6)); 776 778 777 - fl6.flowi6_mark = READ_ONCE(sk->sk_mark); 779 + fl6.flowi6_mark = ipc6.sockc.mark; 778 780 fl6.flowi6_uid = sk->sk_uid; 779 - 780 - ipcm6_init(&ipc6); 781 - ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags); 782 - ipc6.sockc.mark = fl6.flowi6_mark; 783 - ipc6.sockc.priority = READ_ONCE(sk->sk_priority); 784 781 785 782 if (sin6) { 786 783 if (addr_len < SIN6_LEN_RFC2133) ··· 888 891 if (hdrincl) 889 892 fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH; 890 893 891 - if (ipc6.tclass < 0) 892 - ipc6.tclass = np->tclass; 893 - 894 894 fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel); 895 895 896 896 dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p); ··· 897 903 } 898 904 if (ipc6.hlimit < 0) 899 905 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 900 - 901 - if (ipc6.dontfrag < 0) 902 - ipc6.dontfrag = inet6_test_bit(DONTFRAG, sk); 903 906 904 907 if (msg->msg_flags&MSG_CONFIRM) 905 908 goto do_confirm;
+1 -9
net/ipv6/udp.c
··· 1494 1494 int is_udplite = IS_UDPLITE(sk); 1495 1495 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); 1496 1496 1497 - ipcm6_init(&ipc6); 1497 + ipcm6_init_sk(&ipc6, sk); 1498 1498 ipc6.gso_size = READ_ONCE(up->gso_size); 1499 - ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags); 1500 - ipc6.sockc.mark = READ_ONCE(sk->sk_mark); 1501 - ipc6.sockc.priority = READ_ONCE(sk->sk_priority); 1502 1499 1503 1500 /* destination address check */ 1504 1501 if (sin6) { ··· 1701 1704 1702 1705 security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6)); 1703 1706 1704 - if (ipc6.tclass < 0) 1705 - ipc6.tclass = np->tclass; 1706 - 1707 1707 fl6->flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6->flowlabel); 1708 1708 1709 1709 dst = ip6_sk_dst_lookup_flow(sk, fl6, final_p, connected); ··· 1746 1752 WRITE_ONCE(up->pending, AF_INET6); 1747 1753 1748 1754 do_append_data: 1749 - if (ipc6.dontfrag < 0) 1750 - ipc6.dontfrag = inet6_test_bit(DONTFRAG, sk); 1751 1755 up->len += ulen; 1752 1756 err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr), 1753 1757 &ipc6, fl6, dst_rt6_info(dst),
+1 -7
net/l2tp/l2tp_ip6.c
··· 547 547 fl6.flowi6_mark = READ_ONCE(sk->sk_mark); 548 548 fl6.flowi6_uid = sk->sk_uid; 549 549 550 - ipcm6_init(&ipc6); 550 + ipcm6_init_sk(&ipc6, sk); 551 551 552 552 if (lsa) { 553 553 if (addr_len < SIN6_LEN_RFC2133) ··· 634 634 635 635 security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6)); 636 636 637 - if (ipc6.tclass < 0) 638 - ipc6.tclass = np->tclass; 639 - 640 637 fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel); 641 638 642 639 dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p); ··· 644 647 645 648 if (ipc6.hlimit < 0) 646 649 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst); 647 - 648 - if (ipc6.dontfrag < 0) 649 - ipc6.dontfrag = inet6_test_bit(DONTFRAG, sk); 650 650 651 651 if (msg->msg_flags & MSG_CONFIRM) 652 652 goto do_confirm;
+4 -5
net/packet/af_packet.c
··· 2102 2102 2103 2103 skb->protocol = proto; 2104 2104 skb->dev = dev; 2105 - skb->priority = READ_ONCE(sk->sk_priority); 2106 - skb->mark = READ_ONCE(sk->sk_mark); 2105 + skb->priority = sockc.priority; 2106 + skb->mark = sockc.mark; 2107 2107 skb_set_delivery_type_by_clockid(skb, sockc.transmit_time, sk->sk_clockid); 2108 2108 skb_setup_tx_timestamp(skb, &sockc); 2109 2109 ··· 2634 2634 2635 2635 skb->protocol = proto; 2636 2636 skb->dev = dev; 2637 - skb->priority = READ_ONCE(po->sk.sk_priority); 2638 - skb->mark = READ_ONCE(po->sk.sk_mark); 2637 + skb->priority = sockc->priority; 2638 + skb->mark = sockc->mark; 2639 2639 skb_set_delivery_type_by_clockid(skb, sockc->transmit_time, po->sk.sk_clockid); 2640 2640 skb_setup_tx_timestamp(skb, sockc); 2641 2641 skb_zcopy_set_nouarg(skb, ph.raw); ··· 3039 3039 goto out_unlock; 3040 3040 3041 3041 sockcm_init(&sockc, sk); 3042 - sockc.mark = READ_ONCE(sk->sk_mark); 3043 3042 if (msg->msg_controllen) { 3044 3043 err = sock_cmsg_send(sk, msg, &sockc); 3045 3044 if (unlikely(err))