diff mbox series

[net-next,4/7] ipv4: remove get_rttos

Message ID 20250206193521.2285488-5-willemdebruijn.kernel@gmail.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series net: deduplicate cookie logic | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 12 this patch: 12
netdev/build_tools success Errors and warnings before: 26 (+1) this patch: 26 (+1)
netdev/cc_maintainers warning 2 maintainers not CCed: horms@kernel.org dsahern@kernel.org
netdev/build_clang fail Errors and warnings before: 2113 this patch: 2119
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1742 this patch: 1742
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 54 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 3 this patch: 3
netdev/source_inline success Was 0 now: 0

Commit Message

Willem de Bruijn Feb. 6, 2025, 7:34 p.m. UTC
From: Willem de Bruijn <willemb@google.com>

Initialize the ip cookie tos field when initializing the cookie, in
ipcm_init_sk.

The existing code inverts the standard pattern for initializing cookie
fields. Default is to initialize the field from the sk, then possibly
overwrite that when parsing cmsgs (the unlikely case).

This field inverts that, setting the field to an illegal value and
after cmsg parsing checking whether the value is still illegal and
thus should be overridden.

Be careful to always apply mask INET_DSCP_MASK, as before.

Signed-off-by: Willem de Bruijn <willemb@google.com>
---
 include/net/ip.h       | 11 +++--------
 net/ipv4/ip_sockglue.c |  4 ++--
 net/ipv4/ping.c        |  1 -
 net/ipv4/raw.c         |  1 -
 net/ipv4/udp.c         |  1 -
 5 files changed, 5 insertions(+), 13 deletions(-)

Comments

Willem de Bruijn Feb. 7, 2025, 12:59 a.m. UTC | #1
Willem de Bruijn wrote:
> From: Willem de Bruijn <willemb@google.com>
> 
> Initialize the ip cookie tos field when initializing the cookie, in
> ipcm_init_sk.
> 
> The existing code inverts the standard pattern for initializing cookie
> fields. Default is to initialize the field from the sk, then possibly
> overwrite that when parsing cmsgs (the unlikely case).
> 
> This field inverts that, setting the field to an illegal value and
> after cmsg parsing checking whether the value is still illegal and
> thus should be overridden.
> 
> Be careful to always apply mask INET_DSCP_MASK, as before.
> 
> Signed-off-by: Willem de Bruijn <willemb@google.com>
> ---
>  include/net/ip.h       | 11 +++--------
>  net/ipv4/ip_sockglue.c |  4 ++--
>  net/ipv4/ping.c        |  1 -
>  net/ipv4/raw.c         |  1 -
>  net/ipv4/udp.c         |  1 -
>  5 files changed, 5 insertions(+), 13 deletions(-)
> 
> diff --git a/include/net/ip.h b/include/net/ip.h
> index 6af16545b3e3..6819704e2642 100644
> --- a/include/net/ip.h
> +++ b/include/net/ip.h
> @@ -92,7 +92,9 @@ static inline void ipcm_init(struct ipcm_cookie *ipcm)
>  static inline void ipcm_init_sk(struct ipcm_cookie *ipcm,
>  				const struct inet_sock *inet)
>  {
> -	ipcm_init(ipcm);
> +	*ipcm = (struct ipcm_cookie) {
> +		.tos = READ_ONCE(inet->tos) & INET_DSCP_MASK,
> +	};
>  
>  	sockcm_init(&ipcm->sockc, &inet->sk);
>  
> @@ -256,13 +258,6 @@ static inline u8 ip_sendmsg_scope(const struct inet_sock *inet,
>  	return RT_SCOPE_UNIVERSE;
>  }
>  
> -static inline __u8 get_rttos(struct ipcm_cookie* ipc, struct inet_sock *inet)
> -{
> -	u8 dsfield = ipc->tos != -1 ? ipc->tos : READ_ONCE(inet->tos);
> -
> -	return dsfield & INET_DSCP_MASK;
> -}
> -
>  /* datagram.c */
>  int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
>  int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
> diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
> index 6d9c5c20b1c4..98b1e4a8b72e 100644
> --- a/net/ipv4/ip_sockglue.c
> +++ b/net/ipv4/ip_sockglue.c
> @@ -314,8 +314,8 @@ int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
>  				return -EINVAL;
>  			if (val < 0 || val > 255)
>  				return -EINVAL;
> -			ipc->tos = val;
> -			ipc->sockc.priority = rt_tos2priority(ipc->tos);
> +			ipc->sockc.priority = rt_tos2priority(val);
> +			ipc->tos = val & INET_DSCP_MASK;
>  			break;
>  		case IP_PROTOCOL:
>  			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
> diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
> index 619ddc087957..0215885c6df5 100644
> --- a/net/ipv4/ping.c
> +++ b/net/ipv4/ping.c
> @@ -768,7 +768,6 @@ static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
>  		}
>  		faddr = ipc.opt->opt.faddr;
>  	}
> -	tos = get_rttos(&ipc, inet);

Here and elsewhere, subsequent code needs to use ipc.tos directly.
Willem de Bruijn Feb. 7, 2025, 5:33 p.m. UTC | #2
Willem de Bruijn wrote:
> Willem de Bruijn wrote:
> > From: Willem de Bruijn <willemb@google.com>
> > 
> > Initialize the ip cookie tos field when initializing the cookie, in
> > ipcm_init_sk.
> > 
> > The existing code inverts the standard pattern for initializing cookie
> > fields. Default is to initialize the field from the sk, then possibly
> > overwrite that when parsing cmsgs (the unlikely case).
> > 
> > This field inverts that, setting the field to an illegal value and
> > after cmsg parsing checking whether the value is still illegal and
> > thus should be overridden.
> > 
> > Be careful to always apply mask INET_DSCP_MASK, as before.
> > 
> > Signed-off-by: Willem de Bruijn <willemb@google.com>
> > ---
> >  include/net/ip.h       | 11 +++--------
> >  net/ipv4/ip_sockglue.c |  4 ++--
> >  net/ipv4/ping.c        |  1 -
> >  net/ipv4/raw.c         |  1 -
> >  net/ipv4/udp.c         |  1 -
> >  5 files changed, 5 insertions(+), 13 deletions(-)
> > 
> > diff --git a/include/net/ip.h b/include/net/ip.h
> > index 6af16545b3e3..6819704e2642 100644
> > --- a/include/net/ip.h
> > +++ b/include/net/ip.h
> > @@ -92,7 +92,9 @@ static inline void ipcm_init(struct ipcm_cookie *ipcm)
> >  static inline void ipcm_init_sk(struct ipcm_cookie *ipcm,
> >  				const struct inet_sock *inet)
> >  {
> > -	ipcm_init(ipcm);
> > +	*ipcm = (struct ipcm_cookie) {
> > +		.tos = READ_ONCE(inet->tos) & INET_DSCP_MASK,
> > +	};
> >  
> >  	sockcm_init(&ipcm->sockc, &inet->sk);
> >  
> > @@ -256,13 +258,6 @@ static inline u8 ip_sendmsg_scope(const struct inet_sock *inet,
> >  	return RT_SCOPE_UNIVERSE;
> >  }
> >  
> > -static inline __u8 get_rttos(struct ipcm_cookie* ipc, struct inet_sock *inet)
> > -{
> > -	u8 dsfield = ipc->tos != -1 ? ipc->tos : READ_ONCE(inet->tos);
> > -
> > -	return dsfield & INET_DSCP_MASK;
> > -}
> > -
> >  /* datagram.c */
> >  int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
> >  int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
> > diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
> > index 6d9c5c20b1c4..98b1e4a8b72e 100644
> > --- a/net/ipv4/ip_sockglue.c
> > +++ b/net/ipv4/ip_sockglue.c
> > @@ -314,8 +314,8 @@ int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
> >  				return -EINVAL;
> >  			if (val < 0 || val > 255)
> >  				return -EINVAL;
> > -			ipc->tos = val;
> > -			ipc->sockc.priority = rt_tos2priority(ipc->tos);
> > +			ipc->sockc.priority = rt_tos2priority(val);
> > +			ipc->tos = val & INET_DSCP_MASK;
> >  			break;
> >  		case IP_PROTOCOL:
> >  			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
> > diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
> > index 619ddc087957..0215885c6df5 100644
> > --- a/net/ipv4/ping.c
> > +++ b/net/ipv4/ping.c
> > @@ -768,7 +768,6 @@ static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
> >  		}
> >  		faddr = ipc.opt->opt.faddr;
> >  	}
> > -	tos = get_rttos(&ipc, inet);
> 
> Here and elsewhere, subsequent code needs to use ipc.tos directly.

Actually I misunderstood the purpose of get_rttos.

It only masks the dsfield when passed to the routing layer, with
flowi4_init_output().

The other purpose of ipc->tos, to initialize iph->tos, takes the
unmasked version including ECN bits.
kernel test robot Feb. 8, 2025, 9:24 a.m. UTC | #3
Hi Willem,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Willem-de-Bruijn/tcp-only-initialize-sockcm-tsflags-field/20250207-033912
base:   net-next/main
patch link:    https://lore.kernel.org/r/20250206193521.2285488-5-willemdebruijn.kernel%40gmail.com
patch subject: [PATCH net-next 4/7] ipv4: remove get_rttos
config: x86_64-buildonly-randconfig-002-20250207 (https://download.01.org/0day-ci/archive/20250208/202502081713.QmBbIMec-lkp@intel.com/config)
compiler: clang version 19.1.3 (https://github.com/llvm/llvm-project ab51eccf88f5321e7c60591c5546b254b6afab99)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250208/202502081713.QmBbIMec-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202502081713.QmBbIMec-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> net/ipv4/raw.c:608:52: warning: variable 'tos' is uninitialized when used here [-Wuninitialized]
     608 |         flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos, scope,
         |                                                           ^~~
   net/ipv4/raw.c:489:8: note: initialize the variable 'tos' to silence this warning
     489 |         u8 tos, scope;
         |               ^
         |                = '\0'
   1 warning generated.
--
>> net/ipv4/udp.c:1444:52: warning: variable 'tos' is uninitialized when used here [-Wuninitialized]
    1444 |                 flowi4_init_output(fl4, ipc.oif, ipc.sockc.mark, tos, scope,
         |                                                                  ^~~
   net/ipv4/udp.c:1284:8: note: initialize the variable 'tos' to silence this warning
    1284 |         u8 tos, scope;
         |               ^
         |                = '\0'
   net/ipv4/udp.c:3883:27: warning: bitwise operation between different enumeration types ('enum bpf_reg_type' and 'enum bpf_type_flag') [-Wenum-enum-conversion]
    3883 |                   PTR_TO_BTF_ID_OR_NULL | PTR_TRUSTED },
         |                   ~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~
   2 warnings generated.
--
>> net/ipv4/ping.c:781:52: warning: variable 'tos' is uninitialized when used here [-Wuninitialized]
     781 |         flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos, scope,
         |                                                           ^~~
   net/ipv4/ping.c:708:8: note: initialize the variable 'tos' to silence this warning
     708 |         u8 tos, scope;
         |               ^
         |                = '\0'
   1 warning generated.


vim +/tos +608 net/ipv4/raw.c

c008ba5bdc9fa8 Herbert Xu            2014-11-07  481  
1b784140474e4f Ying Xue              2015-03-02  482  static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
^1da177e4c3f41 Linus Torvalds        2005-04-16  483  {
^1da177e4c3f41 Linus Torvalds        2005-04-16  484  	struct inet_sock *inet = inet_sk(sk);
bb191c3e874650 David Ahern           2015-10-05  485  	struct net *net = sock_net(sk);
^1da177e4c3f41 Linus Torvalds        2005-04-16  486  	struct ipcm_cookie ipc;
^1da177e4c3f41 Linus Torvalds        2005-04-16  487  	struct rtable *rt = NULL;
77968b78242ee2 David S. Miller       2011-05-08  488  	struct flowi4 fl4;
c85be08fc4fa44 Guillaume Nault       2023-05-22  489  	u8 tos, scope;
^1da177e4c3f41 Linus Torvalds        2005-04-16  490  	int free = 0;
3ca3c68e76686b Al Viro               2006-09-27  491  	__be32 daddr;
c1d18f9fa09489 Al Viro               2006-09-27  492  	__be32 saddr;
959d5c11601b2b Eric Dumazet          2023-09-22  493  	int uc_index, err;
f6d8bd051c391c Eric Dumazet          2011-04-21  494  	struct ip_options_data opt_copy;
c008ba5bdc9fa8 Herbert Xu            2014-11-07  495  	struct raw_frag_vec rfv;
8f659a03a0ba92 Mohamed Ghannam       2017-12-10  496  	int hdrincl;
^1da177e4c3f41 Linus Torvalds        2005-04-16  497  
^1da177e4c3f41 Linus Torvalds        2005-04-16  498  	err = -EMSGSIZE;
926d4b8122fb32 Jesper Juhl           2005-06-18  499  	if (len > 0xFFFF)
^1da177e4c3f41 Linus Torvalds        2005-04-16  500  		goto out;
^1da177e4c3f41 Linus Torvalds        2005-04-16  501  
cafbe182a467bf Eric Dumazet          2023-08-16  502  	hdrincl = inet_test_bit(HDRINCL, sk);
cafbe182a467bf Eric Dumazet          2023-08-16  503  
^1da177e4c3f41 Linus Torvalds        2005-04-16  504  	/*
^1da177e4c3f41 Linus Torvalds        2005-04-16  505  	 *	Check the flags.
^1da177e4c3f41 Linus Torvalds        2005-04-16  506  	 */
^1da177e4c3f41 Linus Torvalds        2005-04-16  507  
^1da177e4c3f41 Linus Torvalds        2005-04-16  508  	err = -EOPNOTSUPP;
^1da177e4c3f41 Linus Torvalds        2005-04-16  509  	if (msg->msg_flags & MSG_OOB)	/* Mirror BSD error message */
^1da177e4c3f41 Linus Torvalds        2005-04-16  510  		goto out;               /* compatibility */
^1da177e4c3f41 Linus Torvalds        2005-04-16  511  
^1da177e4c3f41 Linus Torvalds        2005-04-16  512  	/*
^1da177e4c3f41 Linus Torvalds        2005-04-16  513  	 *	Get and verify the address.
^1da177e4c3f41 Linus Torvalds        2005-04-16  514  	 */
^1da177e4c3f41 Linus Torvalds        2005-04-16  515  
^1da177e4c3f41 Linus Torvalds        2005-04-16  516  	if (msg->msg_namelen) {
342dfc306fb321 Steffen Hurrle        2014-01-17  517  		DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
^1da177e4c3f41 Linus Torvalds        2005-04-16  518  		err = -EINVAL;
^1da177e4c3f41 Linus Torvalds        2005-04-16  519  		if (msg->msg_namelen < sizeof(*usin))
^1da177e4c3f41 Linus Torvalds        2005-04-16  520  			goto out;
^1da177e4c3f41 Linus Torvalds        2005-04-16  521  		if (usin->sin_family != AF_INET) {
058bd4d2a4ff0a Joe Perches           2012-03-11  522  			pr_info_once("%s: %s forgot to set AF_INET. Fix it!\n",
058bd4d2a4ff0a Joe Perches           2012-03-11  523  				     __func__, current->comm);
^1da177e4c3f41 Linus Torvalds        2005-04-16  524  			err = -EAFNOSUPPORT;
^1da177e4c3f41 Linus Torvalds        2005-04-16  525  			if (usin->sin_family)
^1da177e4c3f41 Linus Torvalds        2005-04-16  526  				goto out;
^1da177e4c3f41 Linus Torvalds        2005-04-16  527  		}
^1da177e4c3f41 Linus Torvalds        2005-04-16  528  		daddr = usin->sin_addr.s_addr;
^1da177e4c3f41 Linus Torvalds        2005-04-16  529  		/* ANK: I did not forget to get protocol from port field.
^1da177e4c3f41 Linus Torvalds        2005-04-16  530  		 * I just do not know, who uses this weirdness.
^1da177e4c3f41 Linus Torvalds        2005-04-16  531  		 * IP_HDRINCL is much more convenient.
^1da177e4c3f41 Linus Torvalds        2005-04-16  532  		 */
^1da177e4c3f41 Linus Torvalds        2005-04-16  533  	} else {
^1da177e4c3f41 Linus Torvalds        2005-04-16  534  		err = -EDESTADDRREQ;
^1da177e4c3f41 Linus Torvalds        2005-04-16  535  		if (sk->sk_state != TCP_ESTABLISHED)
^1da177e4c3f41 Linus Torvalds        2005-04-16  536  			goto out;
c720c7e8383aff Eric Dumazet          2009-10-15  537  		daddr = inet->inet_daddr;
^1da177e4c3f41 Linus Torvalds        2005-04-16  538  	}
^1da177e4c3f41 Linus Torvalds        2005-04-16  539  
351782067b6be8 Willem de Bruijn      2018-07-06  540  	ipcm_init_sk(&ipc, inet);
3632679d9e4f87 Nicolas Dichtel       2023-05-22  541  	/* Keep backward compat */
3632679d9e4f87 Nicolas Dichtel       2023-05-22  542  	if (hdrincl)
3632679d9e4f87 Nicolas Dichtel       2023-05-22  543  		ipc.protocol = IPPROTO_RAW;
^1da177e4c3f41 Linus Torvalds        2005-04-16  544  
^1da177e4c3f41 Linus Torvalds        2005-04-16  545  	if (msg->msg_controllen) {
24025c465f77c3 Soheil Hassas Yeganeh 2016-04-02  546  		err = ip_cmsg_send(sk, msg, &ipc, false);
919483096bfe75 Eric Dumazet          2016-02-04  547  		if (unlikely(err)) {
919483096bfe75 Eric Dumazet          2016-02-04  548  			kfree(ipc.opt);
^1da177e4c3f41 Linus Torvalds        2005-04-16  549  			goto out;
919483096bfe75 Eric Dumazet          2016-02-04  550  		}
^1da177e4c3f41 Linus Torvalds        2005-04-16  551  		if (ipc.opt)
^1da177e4c3f41 Linus Torvalds        2005-04-16  552  			free = 1;
^1da177e4c3f41 Linus Torvalds        2005-04-16  553  	}
^1da177e4c3f41 Linus Torvalds        2005-04-16  554  
^1da177e4c3f41 Linus Torvalds        2005-04-16  555  	saddr = ipc.addr;
^1da177e4c3f41 Linus Torvalds        2005-04-16  556  	ipc.addr = daddr;
^1da177e4c3f41 Linus Torvalds        2005-04-16  557  
f6d8bd051c391c Eric Dumazet          2011-04-21  558  	if (!ipc.opt) {
f6d8bd051c391c Eric Dumazet          2011-04-21  559  		struct ip_options_rcu *inet_opt;
f6d8bd051c391c Eric Dumazet          2011-04-21  560  
f6d8bd051c391c Eric Dumazet          2011-04-21  561  		rcu_read_lock();
f6d8bd051c391c Eric Dumazet          2011-04-21  562  		inet_opt = rcu_dereference(inet->inet_opt);
f6d8bd051c391c Eric Dumazet          2011-04-21  563  		if (inet_opt) {
f6d8bd051c391c Eric Dumazet          2011-04-21  564  			memcpy(&opt_copy, inet_opt,
f6d8bd051c391c Eric Dumazet          2011-04-21  565  			       sizeof(*inet_opt) + inet_opt->opt.optlen);
f6d8bd051c391c Eric Dumazet          2011-04-21  566  			ipc.opt = &opt_copy.opt;
f6d8bd051c391c Eric Dumazet          2011-04-21  567  		}
f6d8bd051c391c Eric Dumazet          2011-04-21  568  		rcu_read_unlock();
f6d8bd051c391c Eric Dumazet          2011-04-21  569  	}
^1da177e4c3f41 Linus Torvalds        2005-04-16  570  
^1da177e4c3f41 Linus Torvalds        2005-04-16  571  	if (ipc.opt) {
^1da177e4c3f41 Linus Torvalds        2005-04-16  572  		err = -EINVAL;
^1da177e4c3f41 Linus Torvalds        2005-04-16  573  		/* Linux does not mangle headers on raw sockets,
^1da177e4c3f41 Linus Torvalds        2005-04-16  574  		 * so that IP options + IP_HDRINCL is non-sense.
^1da177e4c3f41 Linus Torvalds        2005-04-16  575  		 */
8f659a03a0ba92 Mohamed Ghannam       2017-12-10  576  		if (hdrincl)
^1da177e4c3f41 Linus Torvalds        2005-04-16  577  			goto done;
f6d8bd051c391c Eric Dumazet          2011-04-21  578  		if (ipc.opt->opt.srr) {
^1da177e4c3f41 Linus Torvalds        2005-04-16  579  			if (!daddr)
^1da177e4c3f41 Linus Torvalds        2005-04-16  580  				goto done;
f6d8bd051c391c Eric Dumazet          2011-04-21  581  			daddr = ipc.opt->opt.faddr;
^1da177e4c3f41 Linus Torvalds        2005-04-16  582  		}
^1da177e4c3f41 Linus Torvalds        2005-04-16  583  	}
c85be08fc4fa44 Guillaume Nault       2023-05-22  584  	scope = ip_sendmsg_scope(inet, &ipc, msg);
^1da177e4c3f41 Linus Torvalds        2005-04-16  585  
959d5c11601b2b Eric Dumazet          2023-09-22  586  	uc_index = READ_ONCE(inet->uc_index);
f97c1e0c6ebdb6 Joe Perches           2007-12-16  587  	if (ipv4_is_multicast(daddr)) {
854da991733d1b Robert Shearman       2018-10-01  588  		if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif))
02715925222c13 Eric Dumazet          2023-09-22  589  			ipc.oif = READ_ONCE(inet->mc_index);
^1da177e4c3f41 Linus Torvalds        2005-04-16  590  		if (!saddr)
02715925222c13 Eric Dumazet          2023-09-22  591  			saddr = READ_ONCE(inet->mc_addr);
9515a2e082f914 David Ahern           2018-01-24  592  	} else if (!ipc.oif) {
959d5c11601b2b Eric Dumazet          2023-09-22  593  		ipc.oif = uc_index;
959d5c11601b2b Eric Dumazet          2023-09-22  594  	} else if (ipv4_is_lbcast(daddr) && uc_index) {
645f08975f4944 Miaohe Lin            2020-08-27  595  		/* oif is set, packet is to local broadcast
9515a2e082f914 David Ahern           2018-01-24  596  		 * and uc_index is set. oif is most likely set
9515a2e082f914 David Ahern           2018-01-24  597  		 * by sk_bound_dev_if. If uc_index != oif check if the
9515a2e082f914 David Ahern           2018-01-24  598  		 * oif is an L3 master and uc_index is an L3 slave.
9515a2e082f914 David Ahern           2018-01-24  599  		 * If so, we want to allow the send using the uc_index.
9515a2e082f914 David Ahern           2018-01-24  600  		 */
959d5c11601b2b Eric Dumazet          2023-09-22  601  		if (ipc.oif != uc_index &&
9515a2e082f914 David Ahern           2018-01-24  602  		    ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk),
959d5c11601b2b Eric Dumazet          2023-09-22  603  							      uc_index)) {
959d5c11601b2b Eric Dumazet          2023-09-22  604  			ipc.oif = uc_index;
9515a2e082f914 David Ahern           2018-01-24  605  		}
9515a2e082f914 David Ahern           2018-01-24  606  	}
^1da177e4c3f41 Linus Torvalds        2005-04-16  607  
c85be08fc4fa44 Guillaume Nault       2023-05-22 @608  	flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos, scope,
3632679d9e4f87 Nicolas Dichtel       2023-05-22  609  			   hdrincl ? ipc.protocol : sk->sk_protocol,
0e0d44ab427554 Steffen Klassert      2013-08-28  610  			   inet_sk_flowi_flags(sk) |
8f659a03a0ba92 Mohamed Ghannam       2017-12-10  611  			    (hdrincl ? FLOWI_FLAG_KNOWN_NH : 0),
e2d118a1cb5e60 Lorenzo Colitti       2016-11-04  612  			   daddr, saddr, 0, 0, sk->sk_uid);
ef164ae3563bf4 David S. Miller       2011-03-31  613  
fc1092f5156727 Shigeru Yoshida       2024-04-30  614  	fl4.fl4_icmp_type = 0;
fc1092f5156727 Shigeru Yoshida       2024-04-30  615  	fl4.fl4_icmp_code = 0;
fc1092f5156727 Shigeru Yoshida       2024-04-30  616  
8f659a03a0ba92 Mohamed Ghannam       2017-12-10  617  	if (!hdrincl) {
b61e9dcc5e77d5 Al Viro               2014-11-24  618  		rfv.msg = msg;
c008ba5bdc9fa8 Herbert Xu            2014-11-07  619  		rfv.hlen = 0;
c008ba5bdc9fa8 Herbert Xu            2014-11-07  620  
c008ba5bdc9fa8 Herbert Xu            2014-11-07  621  		err = raw_probe_proto_opt(&rfv, &fl4);
a27b58fed90cc5 Heiko Carstens        2006-10-30  622  		if (err)
a27b58fed90cc5 Heiko Carstens        2006-10-30  623  			goto done;
a27b58fed90cc5 Heiko Carstens        2006-10-30  624  	}
^1da177e4c3f41 Linus Torvalds        2005-04-16  625  
3df98d79215ace Paul Moore            2020-09-27  626  	security_sk_classify_flow(sk, flowi4_to_flowi_common(&fl4));
bb191c3e874650 David Ahern           2015-10-05  627  	rt = ip_route_output_flow(net, &fl4, sk);
b23dd4fe42b455 David S. Miller       2011-03-02  628  	if (IS_ERR(rt)) {
b23dd4fe42b455 David S. Miller       2011-03-02  629  		err = PTR_ERR(rt);
4910ac6c526d28 David S. Miller       2011-03-28  630  		rt = NULL;
^1da177e4c3f41 Linus Torvalds        2005-04-16  631  		goto done;
b23dd4fe42b455 David S. Miller       2011-03-02  632  	}
^1da177e4c3f41 Linus Torvalds        2005-04-16  633  
^1da177e4c3f41 Linus Torvalds        2005-04-16  634  	err = -EACCES;
^1da177e4c3f41 Linus Torvalds        2005-04-16  635  	if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
^1da177e4c3f41 Linus Torvalds        2005-04-16  636  		goto done;
^1da177e4c3f41 Linus Torvalds        2005-04-16  637  
^1da177e4c3f41 Linus Torvalds        2005-04-16  638  	if (msg->msg_flags & MSG_CONFIRM)
^1da177e4c3f41 Linus Torvalds        2005-04-16  639  		goto do_confirm;
^1da177e4c3f41 Linus Torvalds        2005-04-16  640  back_from_confirm:
^1da177e4c3f41 Linus Torvalds        2005-04-16  641  
8f659a03a0ba92 Mohamed Ghannam       2017-12-10  642  	if (hdrincl)
7ae9abfd9d6f32 Al Viro               2014-11-27  643  		err = raw_send_hdrinc(sk, &fl4, msg, len,
c14ac9451c3483 Soheil Hassas Yeganeh 2016-04-02  644  				      &rt, msg->msg_flags, &ipc.sockc);
^1da177e4c3f41 Linus Torvalds        2005-04-16  645  
^1da177e4c3f41 Linus Torvalds        2005-04-16  646  	 else {
^1da177e4c3f41 Linus Torvalds        2005-04-16  647  		if (!ipc.addr)
77968b78242ee2 David S. Miller       2011-05-08  648  			ipc.addr = fl4.daddr;
^1da177e4c3f41 Linus Torvalds        2005-04-16  649  		lock_sock(sk);
c008ba5bdc9fa8 Herbert Xu            2014-11-07  650  		err = ip_append_data(sk, &fl4, raw_getfrag,
c008ba5bdc9fa8 Herbert Xu            2014-11-07  651  				     &rfv, len, 0,
2e77d89b2fa8e3 Eric Dumazet          2008-11-24  652  				     &ipc, &rt, msg->msg_flags);
^1da177e4c3f41 Linus Torvalds        2005-04-16  653  		if (err)
^1da177e4c3f41 Linus Torvalds        2005-04-16  654  			ip_flush_pending_frames(sk);
6ce9e7b5fe3195 Eric Dumazet          2009-09-02  655  		else if (!(msg->msg_flags & MSG_MORE)) {
77968b78242ee2 David S. Miller       2011-05-08  656  			err = ip_push_pending_frames(sk, &fl4);
6b5f43ea08150e Eric Dumazet          2023-08-16  657  			if (err == -ENOBUFS && !inet_test_bit(RECVERR, sk))
6ce9e7b5fe3195 Eric Dumazet          2009-09-02  658  				err = 0;
6ce9e7b5fe3195 Eric Dumazet          2009-09-02  659  		}
^1da177e4c3f41 Linus Torvalds        2005-04-16  660  		release_sock(sk);
^1da177e4c3f41 Linus Torvalds        2005-04-16  661  	}
^1da177e4c3f41 Linus Torvalds        2005-04-16  662  done:
^1da177e4c3f41 Linus Torvalds        2005-04-16  663  	if (free)
^1da177e4c3f41 Linus Torvalds        2005-04-16  664  		kfree(ipc.opt);
^1da177e4c3f41 Linus Torvalds        2005-04-16  665  	ip_rt_put(rt);
^1da177e4c3f41 Linus Torvalds        2005-04-16  666  
5418c6926fcb0e Jesper Juhl           2005-06-18  667  out:
5418c6926fcb0e Jesper Juhl           2005-06-18  668  	if (err < 0)
5418c6926fcb0e Jesper Juhl           2005-06-18  669  		return err;
5418c6926fcb0e Jesper Juhl           2005-06-18  670  	return len;
^1da177e4c3f41 Linus Torvalds        2005-04-16  671  
^1da177e4c3f41 Linus Torvalds        2005-04-16  672  do_confirm:
0dec879f636f11 Julian Anastasov      2017-02-06  673  	if (msg->msg_flags & MSG_PROBE)
0dec879f636f11 Julian Anastasov      2017-02-06  674  		dst_confirm_neigh(&rt->dst, &fl4.daddr);
^1da177e4c3f41 Linus Torvalds        2005-04-16  675  	if (!(msg->msg_flags & MSG_PROBE) || len)
^1da177e4c3f41 Linus Torvalds        2005-04-16  676  		goto back_from_confirm;
^1da177e4c3f41 Linus Torvalds        2005-04-16  677  	err = 0;
^1da177e4c3f41 Linus Torvalds        2005-04-16  678  	goto done;
^1da177e4c3f41 Linus Torvalds        2005-04-16  679  }
^1da177e4c3f41 Linus Torvalds        2005-04-16  680
diff mbox series

Patch

diff --git a/include/net/ip.h b/include/net/ip.h
index 6af16545b3e3..6819704e2642 100644
--- a/include/net/ip.h
+++ b/include/net/ip.h
@@ -92,7 +92,9 @@  static inline void ipcm_init(struct ipcm_cookie *ipcm)
 static inline void ipcm_init_sk(struct ipcm_cookie *ipcm,
 				const struct inet_sock *inet)
 {
-	ipcm_init(ipcm);
+	*ipcm = (struct ipcm_cookie) {
+		.tos = READ_ONCE(inet->tos) & INET_DSCP_MASK,
+	};
 
 	sockcm_init(&ipcm->sockc, &inet->sk);
 
@@ -256,13 +258,6 @@  static inline u8 ip_sendmsg_scope(const struct inet_sock *inet,
 	return RT_SCOPE_UNIVERSE;
 }
 
-static inline __u8 get_rttos(struct ipcm_cookie* ipc, struct inet_sock *inet)
-{
-	u8 dsfield = ipc->tos != -1 ? ipc->tos : READ_ONCE(inet->tos);
-
-	return dsfield & INET_DSCP_MASK;
-}
-
 /* datagram.c */
 int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
 int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
index 6d9c5c20b1c4..98b1e4a8b72e 100644
--- a/net/ipv4/ip_sockglue.c
+++ b/net/ipv4/ip_sockglue.c
@@ -314,8 +314,8 @@  int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
 				return -EINVAL;
 			if (val < 0 || val > 255)
 				return -EINVAL;
-			ipc->tos = val;
-			ipc->sockc.priority = rt_tos2priority(ipc->tos);
+			ipc->sockc.priority = rt_tos2priority(val);
+			ipc->tos = val & INET_DSCP_MASK;
 			break;
 		case IP_PROTOCOL:
 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
index 619ddc087957..0215885c6df5 100644
--- a/net/ipv4/ping.c
+++ b/net/ipv4/ping.c
@@ -768,7 +768,6 @@  static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 		}
 		faddr = ipc.opt->opt.faddr;
 	}
-	tos = get_rttos(&ipc, inet);
 	scope = ip_sendmsg_scope(inet, &ipc, msg);
 
 	if (ipv4_is_multicast(daddr)) {
diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c
index 4304a68d1db0..b1f3fe7962bf 100644
--- a/net/ipv4/raw.c
+++ b/net/ipv4/raw.c
@@ -581,7 +581,6 @@  static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 			daddr = ipc.opt->opt.faddr;
 		}
 	}
-	tos = get_rttos(&ipc, inet);
 	scope = ip_sendmsg_scope(inet, &ipc, msg);
 
 	uc_index = READ_ONCE(inet->uc_index);
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index c472c9a57cf6..97ded5f0ae6c 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -1405,7 +1405,6 @@  int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 		faddr = ipc.opt->opt.faddr;
 		connected = 0;
 	}
-	tos = get_rttos(&ipc, inet);
 	scope = ip_sendmsg_scope(inet, &ipc, msg);
 	if (scope == RT_SCOPE_LINK)
 		connected = 0;