Skip to content

Commit 5976687

Browse files
jtlaytonJ. Bruce Fields
authored andcommitted
sunrpc: move address copy/cmp/convert routines and prototypes from clnt.h to addr.h
These routines are used by server and client code, so having them in a separate header would be best. Signed-off-by: Jeff Layton <[email protected]> Acked-by: Trond Myklebust <[email protected]> Signed-off-by: J. Bruce Fields <[email protected]>
1 parent 155a345 commit 5976687

File tree

21 files changed

+191
-163
lines changed

21 files changed

+191
-163
lines changed

fs/lockd/clntlock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <linux/slab.h>
1212
#include <linux/time.h>
1313
#include <linux/nfs_fs.h>
14-
#include <linux/sunrpc/clnt.h>
14+
#include <linux/sunrpc/addr.h>
1515
#include <linux/sunrpc/svc.h>
1616
#include <linux/lockd/lockd.h>
1717
#include <linux/kthread.h>

fs/lockd/host.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/in.h>
1414
#include <linux/in6.h>
1515
#include <linux/sunrpc/clnt.h>
16+
#include <linux/sunrpc/addr.h>
1617
#include <linux/sunrpc/svc.h>
1718
#include <linux/lockd/lockd.h>
1819
#include <linux/mutex.h>

fs/lockd/mon.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/slab.h>
1313

1414
#include <linux/sunrpc/clnt.h>
15+
#include <linux/sunrpc/addr.h>
1516
#include <linux/sunrpc/xprtsock.h>
1617
#include <linux/sunrpc/svc.h>
1718
#include <linux/lockd/lockd.h>

fs/lockd/svcsubs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <linux/slab.h>
1414
#include <linux/mutex.h>
1515
#include <linux/sunrpc/svc.h>
16-
#include <linux/sunrpc/clnt.h>
16+
#include <linux/sunrpc/addr.h>
1717
#include <linux/nfsd/nfsfh.h>
1818
#include <linux/nfsd/export.h>
1919
#include <linux/lockd/lockd.h>

fs/nfs/dns_resolve.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <linux/module.h>
1212
#include <linux/sunrpc/clnt.h>
13+
#include <linux/sunrpc/addr.h>
1314
#include <linux/dns_resolver.h>
1415
#include "dns_resolve.h"
1516

@@ -42,6 +43,7 @@ EXPORT_SYMBOL_GPL(nfs_dns_resolve_name);
4243
#include <linux/seq_file.h>
4344
#include <linux/inet.h>
4445
#include <linux/sunrpc/clnt.h>
46+
#include <linux/sunrpc/addr.h>
4547
#include <linux/sunrpc/cache.h>
4648
#include <linux/sunrpc/svcauth.h>
4749
#include <linux/sunrpc/rpc_pipe_fs.h>

fs/nfs/nfs4client.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/nfs_fs.h>
77
#include <linux/nfs_idmap.h>
88
#include <linux/nfs_mount.h>
9+
#include <linux/sunrpc/addr.h>
910
#include <linux/sunrpc/auth.h>
1011
#include <linux/sunrpc/xprt.h>
1112
#include <linux/sunrpc/bc_xprt.h>

fs/nfs/nfs4filelayoutdev.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/nfs_fs.h>
3232
#include <linux/vmalloc.h>
3333
#include <linux/module.h>
34+
#include <linux/sunrpc/addr.h>
3435

3536
#include "internal.h"
3637
#include "nfs4session.h"

fs/nfs/nfs4namespace.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <linux/slab.h>
1515
#include <linux/string.h>
1616
#include <linux/sunrpc/clnt.h>
17+
#include <linux/sunrpc/addr.h>
1718
#include <linux/vfs.h>
1819
#include <linux/inet.h>
1920
#include "internal.h"

fs/nfs/super.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/errno.h>
3232
#include <linux/unistd.h>
3333
#include <linux/sunrpc/clnt.h>
34+
#include <linux/sunrpc/addr.h>
3435
#include <linux/sunrpc/stats.h>
3536
#include <linux/sunrpc/metrics.h>
3637
#include <linux/sunrpc/xprtsock.h>

fs/nfsd/fault_inject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <linux/debugfs.h>
1010
#include <linux/module.h>
1111
#include <linux/nsproxy.h>
12-
#include <linux/sunrpc/clnt.h>
12+
#include <linux/sunrpc/addr.h>
1313
#include <asm/uaccess.h>
1414

1515
#include "state.h"

fs/nfsd/nfs4state.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#include <linux/pagemap.h>
4141
#include <linux/ratelimit.h>
4242
#include <linux/sunrpc/svcauth_gss.h>
43-
#include <linux/sunrpc/clnt.h>
43+
#include <linux/sunrpc/addr.h>
4444
#include "xdr4.h"
4545
#include "vfs.h"
4646
#include "current_stateid.h"

fs/nfsd/nfscache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
#include <linux/slab.h>
12-
#include <linux/sunrpc/clnt.h>
12+
#include <linux/sunrpc/addr.h>
1313
#include <linux/highmem.h>
1414

1515
#include "nfsd.h"

fs/nfsd/nfsctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#include <linux/sunrpc/svcsock.h>
1212
#include <linux/lockd/lockd.h>
13-
#include <linux/sunrpc/clnt.h>
13+
#include <linux/sunrpc/addr.h>
1414
#include <linux/sunrpc/gss_api.h>
1515
#include <linux/sunrpc/gss_krb5_enctypes.h>
1616
#include <linux/sunrpc/rpc_pipe_fs.h>

include/linux/sunrpc/addr.h

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* linux/include/linux/sunrpc/addr.h
3+
*
4+
* Various routines for copying and comparing sockaddrs and for
5+
* converting them to and from presentation format.
6+
*/
7+
#ifndef _LINUX_SUNRPC_ADDR_H
8+
#define _LINUX_SUNRPC_ADDR_H
9+
10+
#include <linux/socket.h>
11+
#include <linux/in.h>
12+
#include <linux/in6.h>
13+
#include <net/ipv6.h>
14+
15+
size_t rpc_ntop(const struct sockaddr *, char *, const size_t);
16+
size_t rpc_pton(struct net *, const char *, const size_t,
17+
struct sockaddr *, const size_t);
18+
char * rpc_sockaddr2uaddr(const struct sockaddr *, gfp_t);
19+
size_t rpc_uaddr2sockaddr(struct net *, const char *, const size_t,
20+
struct sockaddr *, const size_t);
21+
22+
static inline unsigned short rpc_get_port(const struct sockaddr *sap)
23+
{
24+
switch (sap->sa_family) {
25+
case AF_INET:
26+
return ntohs(((struct sockaddr_in *)sap)->sin_port);
27+
case AF_INET6:
28+
return ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
29+
}
30+
return 0;
31+
}
32+
33+
static inline void rpc_set_port(struct sockaddr *sap,
34+
const unsigned short port)
35+
{
36+
switch (sap->sa_family) {
37+
case AF_INET:
38+
((struct sockaddr_in *)sap)->sin_port = htons(port);
39+
break;
40+
case AF_INET6:
41+
((struct sockaddr_in6 *)sap)->sin6_port = htons(port);
42+
break;
43+
}
44+
}
45+
46+
#define IPV6_SCOPE_DELIMITER '%'
47+
#define IPV6_SCOPE_ID_LEN sizeof("%nnnnnnnnnn")
48+
49+
static inline bool __rpc_cmp_addr4(const struct sockaddr *sap1,
50+
const struct sockaddr *sap2)
51+
{
52+
const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sap1;
53+
const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sap2;
54+
55+
return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
56+
}
57+
58+
static inline bool __rpc_copy_addr4(struct sockaddr *dst,
59+
const struct sockaddr *src)
60+
{
61+
const struct sockaddr_in *ssin = (struct sockaddr_in *) src;
62+
struct sockaddr_in *dsin = (struct sockaddr_in *) dst;
63+
64+
dsin->sin_family = ssin->sin_family;
65+
dsin->sin_addr.s_addr = ssin->sin_addr.s_addr;
66+
return true;
67+
}
68+
69+
#if IS_ENABLED(CONFIG_IPV6)
70+
static inline bool __rpc_cmp_addr6(const struct sockaddr *sap1,
71+
const struct sockaddr *sap2)
72+
{
73+
const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sap1;
74+
const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sap2;
75+
76+
if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
77+
return false;
78+
else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
79+
return sin1->sin6_scope_id == sin2->sin6_scope_id;
80+
81+
return true;
82+
}
83+
84+
static inline bool __rpc_copy_addr6(struct sockaddr *dst,
85+
const struct sockaddr *src)
86+
{
87+
const struct sockaddr_in6 *ssin6 = (const struct sockaddr_in6 *) src;
88+
struct sockaddr_in6 *dsin6 = (struct sockaddr_in6 *) dst;
89+
90+
dsin6->sin6_family = ssin6->sin6_family;
91+
dsin6->sin6_addr = ssin6->sin6_addr;
92+
dsin6->sin6_scope_id = ssin6->sin6_scope_id;
93+
return true;
94+
}
95+
#else /* !(IS_ENABLED(CONFIG_IPV6) */
96+
static inline bool __rpc_cmp_addr6(const struct sockaddr *sap1,
97+
const struct sockaddr *sap2)
98+
{
99+
return false;
100+
}
101+
102+
static inline bool __rpc_copy_addr6(struct sockaddr *dst,
103+
const struct sockaddr *src)
104+
{
105+
return false;
106+
}
107+
#endif /* !(IS_ENABLED(CONFIG_IPV6) */
108+
109+
/**
110+
* rpc_cmp_addr - compare the address portion of two sockaddrs.
111+
* @sap1: first sockaddr
112+
* @sap2: second sockaddr
113+
*
114+
* Just compares the family and address portion. Ignores port, but
115+
* compares the scope if it's a link-local address.
116+
*
117+
* Returns true if the addrs are equal, false if they aren't.
118+
*/
119+
static inline bool rpc_cmp_addr(const struct sockaddr *sap1,
120+
const struct sockaddr *sap2)
121+
{
122+
if (sap1->sa_family == sap2->sa_family) {
123+
switch (sap1->sa_family) {
124+
case AF_INET:
125+
return __rpc_cmp_addr4(sap1, sap2);
126+
case AF_INET6:
127+
return __rpc_cmp_addr6(sap1, sap2);
128+
}
129+
}
130+
return false;
131+
}
132+
133+
/**
134+
* rpc_copy_addr - copy the address portion of one sockaddr to another
135+
* @dst: destination sockaddr
136+
* @src: source sockaddr
137+
*
138+
* Just copies the address portion and family. Ignores port, scope, etc.
139+
* Caller is responsible for making certain that dst is large enough to hold
140+
* the address in src. Returns true if address family is supported. Returns
141+
* false otherwise.
142+
*/
143+
static inline bool rpc_copy_addr(struct sockaddr *dst,
144+
const struct sockaddr *src)
145+
{
146+
switch (src->sa_family) {
147+
case AF_INET:
148+
return __rpc_copy_addr4(dst, src);
149+
case AF_INET6:
150+
return __rpc_copy_addr6(dst, src);
151+
}
152+
return false;
153+
}
154+
155+
/**
156+
* rpc_get_scope_id - return scopeid for a given sockaddr
157+
* @sa: sockaddr to get scopeid from
158+
*
159+
* Returns the value of the sin6_scope_id for AF_INET6 addrs, or 0 if
160+
* not an AF_INET6 address.
161+
*/
162+
static inline u32 rpc_get_scope_id(const struct sockaddr *sa)
163+
{
164+
if (sa->sa_family != AF_INET6)
165+
return 0;
166+
167+
return ((struct sockaddr_in6 *) sa)->sin6_scope_id;
168+
}
169+
170+
#endif /* _LINUX_SUNRPC_ADDR_H */

0 commit comments

Comments
 (0)