Skip to contentSkip to conversation

net/ipv6/udp.c: remove duplicate udp_get_port code

PATCH Superseded [email protected] 2 participants 3 messages Aug 16, 2006

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/ipv6/udp.c: remove duplicate udp_get_port code

UDPv4 and UDPv6 use an almost identical version of the get_port function, which is unnecessary since the (long) code differs in only one if-statement.

By disentangling the if statement and adding v4/v6 checks appropriately, this code duplication can be removed and further
* udp_port_rover can stay in net/ipv4/udp.c
* udp_lport_inuse can become static in net/ipv4/udp.c (only called by udp_get_port

The text below discusses the re-arrangement of the if-statement. This is implemented by enclosed patch (works both on stable and Torvalds' release). The patch also dispenses with a goto statement whose jump label is referenced only once.

D i s c u s s i o n

The following compares the statements for udp_v{4,6}_get_port.

A) In udp_v4_get_port():
=========================

if (inet2->num == snum                                 && 
    sk2 != sk                                          && 
    !ipv6_only_sock(sk2)                               &&
    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if 
     || sk2->sk_bound_dev_if == sk->sk_bound_dev_if)   && 
    (!inet2->rcv_saddr || !inet->rcv_saddr 
     || inet2->rcv_saddr == inet->rcv_saddr)           && 
    (!sk2->sk_reuse || !sk->sk_reuse)                     )

goto fail;

This function is called from IPv4 context, hence sk->sk_family == PF_INET.

B) In udp_v6_get_port():
=========================
if (inet_sk(sk2)->num == snum &&

    sk2 != sk                                           &&
    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if
     || sk2->sk_bound_dev_if == sk->sk_bound_dev_if)    && 
    (!sk2->sk_reuse || !sk->sk_reuse)                   && 
    ipv6_rcv_saddr_equal(sk, sk2)                          )

goto fail;

This function is called from IPv6 context, hence sk->sk_family == PF_INET6.

Common denominator:
===================
By re-ordering some of the last literals, both functions share the following conjunction of conditions:

if (inet_sk(sk2)->num == snum && sk2 != sk &&

    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if 
     || sk2->sk_bound_dev_if == sk->sk_bound_dev_if)    && 
    (!sk2->sk_reuse || !sk->sk_reuse)                      ) 

To make the function applicable to both v4 and v6 contexts, a second if statement is added, which branches according to sk's sk_family.

---

 include/net/udp.h |   17 +----------
 net/ipv4/udp.c    |   57 ++++++++++++++++++++++++--------------
 net/ipv6/udp.c    |   79 +----------------------------------------------------
 3 files changed, 38 insertions(+), 115 deletions(-)
include/net/udp.h +1 -16
index 766fba1..69d4288 100644
@@ -30,25 +30,9 @@ #include <linux/seq_file.h>
 
 #define UDP_HTABLE_SIZE		128
 
-/* udp.c: This needs to be shared by v4 and v6 because the lookup
- *        and hashing code needs to work with different AF's yet
- *        the port space is shared.
- */
 extern struct hlist_head udp_hash[UDP_HTABLE_SIZE];
 extern rwlock_t udp_hash_lock;
 
-extern int udp_port_rover;
-
-static inline int udp_lport_inuse(u16 num)
-{
-	struct sock *sk;
-	struct hlist_node *node;
-
-	sk_for_each(sk, node, &udp_hash[num & (UDP_HTABLE_SIZE - 1)])
-		if (inet_sk(sk)->num == num)
-			return 1;
-	return 0;
-}
 
 /* Note: this must match 'valbool' in sock_setsockopt */
 #define UDP_CSUM_NOXMIT		1
@@ -63,6 +47,7 @@ extern struct proto udp_prot;
 
 struct sk_buff;
 
+extern int	udp_get_port(struct sock *sk, unsigned short snum);
 extern void	udp_err(struct sk_buff *, u32);
 
 extern int	udp_sendmsg(struct kiocb *iocb, struct sock *sk,
net/ipv4/udp.c +36 -21

94 lines not on this page · raw message

net/ipv6/udp.c +1 -78

91 lines not on this page · raw message

#1 lore raw

Hello.

In article <200608160846.48174@strip-the-willow> (at Wed, 16 Aug 2006 08:46:48 +0100), [email protected] says:

Quoted 2 lines

UDPv4 and UDPv6 use an almost identical version of the get_port function, which is unnecessary since the (long) code differs in only one if-statement.

:

:

net/ipv4/udp.c gotit: · line 205

Quoted 6 lines
net/ipv4/udp.c +6 -0
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+				else if(sk->sk_family == PF_INET6     &&
+					ipv6_rcv_saddr_equal(sk, sk2)     )
+					goto fail;
+			}
+#endif

This is not good because you cannot link ipv6_rcv_saddr_equal() if you are compiling IPv6 as module.

How about retaining udp_v{4,6}_get_port() and call common udp_get_port() from both functions?

--yoshfuji

#2 lore raw

Hi Yoshifuji,

|  > +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
|  > +				else if(sk->sk_family == PF_INET6     &&
|  > +					ipv6_rcv_saddr_equal(sk, sk2)     )
|  > +					goto fail;
|  > +			}
|  > +#endif
|  
|  This is not good because you cannot link ipv6_rcv_saddr_equal()
|  if you are compiling IPv6 as module.

Yes and the second ugliness was that ipv4/udp.c suddenly had to include <net/addrconf.h>.

|  How about retaining udp_v{4,6}_get_port() and call
|  common udp_get_port() from both functions?

I enclose a realisation below - do you think that is better? Tested both IPv6 as module and as `y', double-checked all changes. Thank you for reviewing and comments.

---

 include/net/udp.h |   18 +---------
 net/ipv4/udp.c    |   95 ++++++++++++++++++++++++++++++++++--------------------
 net/ipv6/udp.c    |   76 +------------------------------------------
 3 files changed, 64 insertions(+), 125 deletions(-)
include/net/udp.h +2 -16
index 766fba1..c490a0f 100644
@@ -30,25 +30,9 @@ #include <linux/seq_file.h>
 
 #define UDP_HTABLE_SIZE		128
 
-/* udp.c: This needs to be shared by v4 and v6 because the lookup
- *        and hashing code needs to work with different AF's yet
- *        the port space is shared.
- */
 extern struct hlist_head udp_hash[UDP_HTABLE_SIZE];
 extern rwlock_t udp_hash_lock;
 
-extern int udp_port_rover;
-
-static inline int udp_lport_inuse(u16 num)
-{
-	struct sock *sk;
-	struct hlist_node *node;
-
-	sk_for_each(sk, node, &udp_hash[num & (UDP_HTABLE_SIZE - 1)])
-		if (inet_sk(sk)->num == num)
-			return 1;
-	return 0;
-}
 
 /* Note: this must match 'valbool' in sock_setsockopt */
 #define UDP_CSUM_NOXMIT		1
@@ -63,6 +47,8 @@ extern struct proto udp_prot;
 
 struct sk_buff;
 
+extern int	udp_get_port(struct sock *sk, unsigned short snum,
+			     int (*saddr_cmp)(struct sock *, struct sock *));
 extern void	udp_err(struct sk_buff *, u32);
 
 extern int	udp_sendmsg(struct kiocb *iocb, struct sock *sk,
net/ipv4/udp.c +60 -35

141 lines not on this page · raw message

net/ipv6/udp.c +2 -74

83 lines not on this page · raw message