Commit 7a8d8a46 authored by David S. Miller's avatar David S. Miller

Merge branch 'tcp-Add-support-for-L3-domains-to-MD5-auth'

David Ahern says:

====================
tcp: Add support for L3 domains to MD5 auth

With VRF, the scope of network addresses is limited to the L3 domain
the device is associated. MD5 keys are based on addresses, so proper
VRF support requires an L3 domain to be considered for the lookups.

Leverage the new TCP_MD5SIG_EXT option to add support for a device index
to MD5 keys. The __tcpm_pad entry in tcp_md5sig is renamed to tcpm_ifindex
and a new flag, TCP_MD5SIG_FLAG_IFINDEX, in tcpm_flags determines if the
entry is examined. This follows what was done for MD5 and prefixes with
commits
   8917a777 ("tcp: md5: add TCP_MD5SIG_EXT socket option to set a key address prefix")
   6797318e ("tcp: md5: add an address prefix for key lookup")

Handling both a device AND L3 domain is much more complicated for the
response paths. This set focuses only on L3 support - requiring the
device index to be an l3mdev (ie, VRF). Support for slave devices can
be added later if desired, much like the progression of support for
sockets bound to a VRF and then bound to a device in a VRF. Kernel
code is setup to explicitly call out that current lookup is for an L3
index, while the uapi just references a device index allowing its
meaning to include other devices in the future.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 98c81476 5cad8bce
...@@ -1532,8 +1532,9 @@ struct tcp_md5sig_key { ...@@ -1532,8 +1532,9 @@ struct tcp_md5sig_key {
struct hlist_node node; struct hlist_node node;
u8 keylen; u8 keylen;
u8 family; /* AF_INET or AF_INET6 */ u8 family; /* AF_INET or AF_INET6 */
union tcp_md5_addr addr;
u8 prefixlen; u8 prefixlen;
union tcp_md5_addr addr;
int l3index; /* set if key added with L3 scope */
u8 key[TCP_MD5SIG_MAXKEYLEN]; u8 key[TCP_MD5SIG_MAXKEYLEN];
struct rcu_head rcu; struct rcu_head rcu;
}; };
...@@ -1577,34 +1578,33 @@ struct tcp_md5sig_pool { ...@@ -1577,34 +1578,33 @@ struct tcp_md5sig_pool {
int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key, int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
const struct sock *sk, const struct sk_buff *skb); const struct sock *sk, const struct sk_buff *skb);
int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr, int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
int family, u8 prefixlen, const u8 *newkey, u8 newkeylen, int family, u8 prefixlen, int l3index,
gfp_t gfp); const u8 *newkey, u8 newkeylen, gfp_t gfp);
int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr, int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
int family, u8 prefixlen); int family, u8 prefixlen, int l3index);
struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk, struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk,
const struct sock *addr_sk); const struct sock *addr_sk);
#ifdef CONFIG_TCP_MD5SIG #ifdef CONFIG_TCP_MD5SIG
#include <linux/jump_label.h> #include <linux/jump_label.h>
extern struct static_key_false tcp_md5_needed; extern struct static_key_false tcp_md5_needed;
struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, int l3index,
const union tcp_md5_addr *addr, const union tcp_md5_addr *addr,
int family); int family);
static inline struct tcp_md5sig_key * static inline struct tcp_md5sig_key *
tcp_md5_do_lookup(const struct sock *sk, tcp_md5_do_lookup(const struct sock *sk, int l3index,
const union tcp_md5_addr *addr, const union tcp_md5_addr *addr, int family)
int family)
{ {
if (!static_branch_unlikely(&tcp_md5_needed)) if (!static_branch_unlikely(&tcp_md5_needed))
return NULL; return NULL;
return __tcp_md5_do_lookup(sk, addr, family); return __tcp_md5_do_lookup(sk, l3index, addr, family);
} }
#define tcp_twsk_md5_key(twsk) ((twsk)->tw_md5_key) #define tcp_twsk_md5_key(twsk) ((twsk)->tw_md5_key)
#else #else
static inline struct tcp_md5sig_key *tcp_md5_do_lookup(const struct sock *sk, static inline struct tcp_md5sig_key *
const union tcp_md5_addr *addr, tcp_md5_do_lookup(const struct sock *sk, int l3index,
int family) const union tcp_md5_addr *addr, int family)
{ {
return NULL; return NULL;
} }
......
...@@ -317,14 +317,15 @@ enum { ...@@ -317,14 +317,15 @@ enum {
#define TCP_MD5SIG_MAXKEYLEN 80 #define TCP_MD5SIG_MAXKEYLEN 80
/* tcp_md5sig extension flags for TCP_MD5SIG_EXT */ /* tcp_md5sig extension flags for TCP_MD5SIG_EXT */
#define TCP_MD5SIG_FLAG_PREFIX 1 /* address prefix length */ #define TCP_MD5SIG_FLAG_PREFIX 0x1 /* address prefix length */
#define TCP_MD5SIG_FLAG_IFINDEX 0x2 /* ifindex set */
struct tcp_md5sig { struct tcp_md5sig {
struct __kernel_sockaddr_storage tcpm_addr; /* address associated */ struct __kernel_sockaddr_storage tcpm_addr; /* address associated */
__u8 tcpm_flags; /* extension flags */ __u8 tcpm_flags; /* extension flags */
__u8 tcpm_prefixlen; /* address prefix */ __u8 tcpm_prefixlen; /* address prefix */
__u16 tcpm_keylen; /* key length */ __u16 tcpm_keylen; /* key length */
__u32 __tcpm_pad; /* zero */ int tcpm_ifindex; /* device index for scope */
__u8 tcpm_key[TCP_MD5SIG_MAXKEYLEN]; /* key (binary) */ __u8 tcpm_key[TCP_MD5SIG_MAXKEYLEN]; /* key (binary) */
}; };
......
This diff is collapsed.
...@@ -81,7 +81,8 @@ static const struct tcp_sock_af_ops tcp_sock_ipv6_specific; ...@@ -81,7 +81,8 @@ static const struct tcp_sock_af_ops tcp_sock_ipv6_specific;
static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific; static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific;
#else #else
static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(const struct sock *sk, static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(const struct sock *sk,
const struct in6_addr *addr) const struct in6_addr *addr,
int l3index)
{ {
return NULL; return NULL;
} }
...@@ -532,15 +533,22 @@ static void tcp_v6_reqsk_destructor(struct request_sock *req) ...@@ -532,15 +533,22 @@ static void tcp_v6_reqsk_destructor(struct request_sock *req)
#ifdef CONFIG_TCP_MD5SIG #ifdef CONFIG_TCP_MD5SIG
static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(const struct sock *sk, static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(const struct sock *sk,
const struct in6_addr *addr) const struct in6_addr *addr,
int l3index)
{ {
return tcp_md5_do_lookup(sk, (union tcp_md5_addr *)addr, AF_INET6); return tcp_md5_do_lookup(sk, l3index,
(union tcp_md5_addr *)addr, AF_INET6);
} }
static struct tcp_md5sig_key *tcp_v6_md5_lookup(const struct sock *sk, static struct tcp_md5sig_key *tcp_v6_md5_lookup(const struct sock *sk,
const struct sock *addr_sk) const struct sock *addr_sk)
{ {
return tcp_v6_md5_do_lookup(sk, &addr_sk->sk_v6_daddr); int l3index;
l3index = l3mdev_master_ifindex_by_index(sock_net(sk),
addr_sk->sk_bound_dev_if);
return tcp_v6_md5_do_lookup(sk, &addr_sk->sk_v6_daddr,
l3index);
} }
static int tcp_v6_parse_md5_keys(struct sock *sk, int optname, static int tcp_v6_parse_md5_keys(struct sock *sk, int optname,
...@@ -548,6 +556,7 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int optname, ...@@ -548,6 +556,7 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int optname,
{ {
struct tcp_md5sig cmd; struct tcp_md5sig cmd;
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.tcpm_addr; struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.tcpm_addr;
int l3index = 0;
u8 prefixlen; u8 prefixlen;
if (optlen < sizeof(cmd)) if (optlen < sizeof(cmd))
...@@ -569,12 +578,30 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int optname, ...@@ -569,12 +578,30 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int optname,
prefixlen = ipv6_addr_v4mapped(&sin6->sin6_addr) ? 32 : 128; prefixlen = ipv6_addr_v4mapped(&sin6->sin6_addr) ? 32 : 128;
} }
if (optname == TCP_MD5SIG_EXT &&
cmd.tcpm_flags & TCP_MD5SIG_FLAG_IFINDEX) {
struct net_device *dev;
rcu_read_lock();
dev = dev_get_by_index_rcu(sock_net(sk), cmd.tcpm_ifindex);
if (dev && netif_is_l3_master(dev))
l3index = dev->ifindex;
rcu_read_unlock();
/* ok to reference set/not set outside of rcu;
* right now device MUST be an L3 master
*/
if (!dev || !l3index)
return -EINVAL;
}
if (!cmd.tcpm_keylen) { if (!cmd.tcpm_keylen) {
if (ipv6_addr_v4mapped(&sin6->sin6_addr)) if (ipv6_addr_v4mapped(&sin6->sin6_addr))
return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3], return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3],
AF_INET, prefixlen); AF_INET, prefixlen,
l3index);
return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr, return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr,
AF_INET6, prefixlen); AF_INET6, prefixlen, l3index);
} }
if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN) if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
...@@ -582,12 +609,13 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int optname, ...@@ -582,12 +609,13 @@ static int tcp_v6_parse_md5_keys(struct sock *sk, int optname,
if (ipv6_addr_v4mapped(&sin6->sin6_addr)) if (ipv6_addr_v4mapped(&sin6->sin6_addr))
return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3], return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3],
AF_INET, prefixlen, cmd.tcpm_key, AF_INET, prefixlen, l3index,
cmd.tcpm_keylen, GFP_KERNEL); cmd.tcpm_key, cmd.tcpm_keylen,
GFP_KERNEL);
return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr, return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr,
AF_INET6, prefixlen, cmd.tcpm_key, AF_INET6, prefixlen, l3index,
cmd.tcpm_keylen, GFP_KERNEL); cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
} }
static int tcp_v6_md5_hash_headers(struct tcp_md5sig_pool *hp, static int tcp_v6_md5_hash_headers(struct tcp_md5sig_pool *hp,
...@@ -698,17 +726,23 @@ static int tcp_v6_md5_hash_skb(char *md5_hash, ...@@ -698,17 +726,23 @@ static int tcp_v6_md5_hash_skb(char *md5_hash,
#endif #endif
static bool tcp_v6_inbound_md5_hash(const struct sock *sk, static bool tcp_v6_inbound_md5_hash(const struct sock *sk,
const struct sk_buff *skb) const struct sk_buff *skb,
int dif, int sdif)
{ {
#ifdef CONFIG_TCP_MD5SIG #ifdef CONFIG_TCP_MD5SIG
const __u8 *hash_location = NULL; const __u8 *hash_location = NULL;
struct tcp_md5sig_key *hash_expected; struct tcp_md5sig_key *hash_expected;
const struct ipv6hdr *ip6h = ipv6_hdr(skb); const struct ipv6hdr *ip6h = ipv6_hdr(skb);
const struct tcphdr *th = tcp_hdr(skb); const struct tcphdr *th = tcp_hdr(skb);
int genhash; int genhash, l3index;
u8 newhash[16]; u8 newhash[16];
hash_expected = tcp_v6_md5_do_lookup(sk, &ip6h->saddr); /* sdif set, means packet ingressed via a device
* in an L3 domain and dif is set to the l3mdev
*/
l3index = sdif ? dif : 0;
hash_expected = tcp_v6_md5_do_lookup(sk, &ip6h->saddr, l3index);
hash_location = tcp_parse_md5sig_option(th); hash_location = tcp_parse_md5sig_option(th);
/* We've parsed the options - do we have a hash? */ /* We've parsed the options - do we have a hash? */
...@@ -732,10 +766,10 @@ static bool tcp_v6_inbound_md5_hash(const struct sock *sk, ...@@ -732,10 +766,10 @@ static bool tcp_v6_inbound_md5_hash(const struct sock *sk,
if (genhash || memcmp(hash_location, newhash, 16) != 0) { if (genhash || memcmp(hash_location, newhash, 16) != 0) {
NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5FAILURE); NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5FAILURE);
net_info_ratelimited("MD5 Hash %s for [%pI6c]:%u->[%pI6c]:%u\n", net_info_ratelimited("MD5 Hash %s for [%pI6c]:%u->[%pI6c]:%u L3 index %d\n",
genhash ? "failed" : "mismatch", genhash ? "failed" : "mismatch",
&ip6h->saddr, ntohs(th->source), &ip6h->saddr, ntohs(th->source),
&ip6h->daddr, ntohs(th->dest)); &ip6h->daddr, ntohs(th->dest), l3index);
return true; return true;
} }
#endif #endif
...@@ -951,8 +985,18 @@ static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb) ...@@ -951,8 +985,18 @@ static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb)
rcu_read_lock(); rcu_read_lock();
hash_location = tcp_parse_md5sig_option(th); hash_location = tcp_parse_md5sig_option(th);
if (sk && sk_fullsock(sk)) { if (sk && sk_fullsock(sk)) {
key = tcp_v6_md5_do_lookup(sk, &ipv6h->saddr); int l3index;
/* sdif set, means packet ingressed via a device
* in an L3 domain and inet_iif is set to it.
*/
l3index = tcp_v6_sdif(skb) ? tcp_v6_iif_l3_slave(skb) : 0;
key = tcp_v6_md5_do_lookup(sk, &ipv6h->saddr, l3index);
} else if (hash_location) { } else if (hash_location) {
int dif = tcp_v6_iif_l3_slave(skb);
int sdif = tcp_v6_sdif(skb);
int l3index;
/* /*
* active side is lost. Try to find listening socket through * active side is lost. Try to find listening socket through
* source port, and then find md5 key through listening socket. * source port, and then find md5 key through listening socket.
...@@ -964,13 +1008,16 @@ static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb) ...@@ -964,13 +1008,16 @@ static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb)
&tcp_hashinfo, NULL, 0, &tcp_hashinfo, NULL, 0,
&ipv6h->saddr, &ipv6h->saddr,
th->source, &ipv6h->daddr, th->source, &ipv6h->daddr,
ntohs(th->source), ntohs(th->source), dif, sdif);
tcp_v6_iif_l3_slave(skb),
tcp_v6_sdif(skb));
if (!sk1) if (!sk1)
goto out; goto out;
key = tcp_v6_md5_do_lookup(sk1, &ipv6h->saddr); /* sdif set, means packet ingressed via a device
* in an L3 domain and dif is set to it.
*/
l3index = tcp_v6_sdif(skb) ? dif : 0;
key = tcp_v6_md5_do_lookup(sk1, &ipv6h->saddr, l3index);
if (!key) if (!key)
goto out; goto out;
...@@ -1040,6 +1087,10 @@ static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb) ...@@ -1040,6 +1087,10 @@ static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
static void tcp_v6_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb, static void tcp_v6_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
struct request_sock *req) struct request_sock *req)
{ {
int l3index;
l3index = tcp_v6_sdif(skb) ? tcp_v6_iif_l3_slave(skb) : 0;
/* sk->sk_state == TCP_LISTEN -> for regular TCP_SYN_RECV /* sk->sk_state == TCP_LISTEN -> for regular TCP_SYN_RECV
* sk->sk_state == TCP_SYN_RECV -> for Fast Open. * sk->sk_state == TCP_SYN_RECV -> for Fast Open.
*/ */
...@@ -1054,7 +1105,7 @@ static void tcp_v6_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb, ...@@ -1054,7 +1105,7 @@ static void tcp_v6_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
req->rsk_rcv_wnd >> inet_rsk(req)->rcv_wscale, req->rsk_rcv_wnd >> inet_rsk(req)->rcv_wscale,
tcp_time_stamp_raw() + tcp_rsk(req)->ts_off, tcp_time_stamp_raw() + tcp_rsk(req)->ts_off,
req->ts_recent, sk->sk_bound_dev_if, req->ts_recent, sk->sk_bound_dev_if,
tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->saddr), tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->saddr, l3index),
0, 0, sk->sk_priority); 0, 0, sk->sk_priority);
} }
...@@ -1126,6 +1177,7 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff * ...@@ -1126,6 +1177,7 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
struct sock *newsk; struct sock *newsk;
#ifdef CONFIG_TCP_MD5SIG #ifdef CONFIG_TCP_MD5SIG
struct tcp_md5sig_key *key; struct tcp_md5sig_key *key;
int l3index;
#endif #endif
struct flowi6 fl6; struct flowi6 fl6;
...@@ -1269,8 +1321,10 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff * ...@@ -1269,8 +1321,10 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
newinet->inet_rcv_saddr = LOOPBACK4_IPV6; newinet->inet_rcv_saddr = LOOPBACK4_IPV6;
#ifdef CONFIG_TCP_MD5SIG #ifdef CONFIG_TCP_MD5SIG
l3index = l3mdev_master_ifindex_by_index(sock_net(sk), ireq->ir_iif);
/* Copy over the MD5 key from the original socket */ /* Copy over the MD5 key from the original socket */
key = tcp_v6_md5_do_lookup(sk, &newsk->sk_v6_daddr); key = tcp_v6_md5_do_lookup(sk, &newsk->sk_v6_daddr, l3index);
if (key) { if (key) {
/* We're using one, so create a matching key /* We're using one, so create a matching key
* on the newsk structure. If we fail to get * on the newsk structure. If we fail to get
...@@ -1278,7 +1332,7 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff * ...@@ -1278,7 +1332,7 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
* across. Shucks. * across. Shucks.
*/ */
tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newsk->sk_v6_daddr, tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newsk->sk_v6_daddr,
AF_INET6, 128, key->key, key->keylen, AF_INET6, 128, l3index, key->key, key->keylen,
sk_gfp_mask(sk, GFP_ATOMIC)); sk_gfp_mask(sk, GFP_ATOMIC));
} }
#endif #endif
...@@ -1480,6 +1534,7 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb) ...@@ -1480,6 +1534,7 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
{ {
struct sk_buff *skb_to_free; struct sk_buff *skb_to_free;
int sdif = inet6_sdif(skb); int sdif = inet6_sdif(skb);
int dif = inet6_iif(skb);
const struct tcphdr *th; const struct tcphdr *th;
const struct ipv6hdr *hdr; const struct ipv6hdr *hdr;
bool refcounted; bool refcounted;
...@@ -1528,7 +1583,7 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb) ...@@ -1528,7 +1583,7 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
struct sock *nsk; struct sock *nsk;
sk = req->rsk_listener; sk = req->rsk_listener;
if (tcp_v6_inbound_md5_hash(sk, skb)) { if (tcp_v6_inbound_md5_hash(sk, skb, dif, sdif)) {
sk_drops_add(sk, skb); sk_drops_add(sk, skb);
reqsk_put(req); reqsk_put(req);
goto discard_it; goto discard_it;
...@@ -1583,7 +1638,7 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb) ...@@ -1583,7 +1638,7 @@ INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
goto discard_and_relse; goto discard_and_relse;
if (tcp_v6_inbound_md5_hash(sk, skb)) if (tcp_v6_inbound_md5_hash(sk, skb, dif, sdif))
goto discard_and_relse; goto discard_and_relse;
if (tcp_filter(sk, skb)) if (tcp_filter(sk, skb))
......
This diff is collapsed.
...@@ -74,7 +74,14 @@ struct sock_args { ...@@ -74,7 +74,14 @@ struct sock_args {
int use_cmsg; int use_cmsg;
const char *dev; const char *dev;
int ifindex; int ifindex;
const char *password; const char *password;
/* prefix for MD5 password */
union {
struct sockaddr_in v4;
struct sockaddr_in6 v6;
} md5_prefix;
unsigned int prefix_len;
/* expected addresses and device index for connection */ /* expected addresses and device index for connection */
int expected_ifindex; int expected_ifindex;
...@@ -200,20 +207,33 @@ static void log_address(const char *desc, struct sockaddr *sa) ...@@ -200,20 +207,33 @@ static void log_address(const char *desc, struct sockaddr *sa)
fflush(stdout); fflush(stdout);
} }
static int tcp_md5sig(int sd, void *addr, socklen_t alen, const char *password) static int tcp_md5sig(int sd, void *addr, socklen_t alen, struct sock_args *args)
{ {
struct tcp_md5sig md5sig; int keylen = strlen(args->password);
int keylen = password ? strlen(password) : 0; struct tcp_md5sig md5sig = {};
int opt = TCP_MD5SIG;
int rc; int rc;
memset(&md5sig, 0, sizeof(md5sig));
memcpy(&md5sig.tcpm_addr, addr, alen);
md5sig.tcpm_keylen = keylen; md5sig.tcpm_keylen = keylen;
memcpy(md5sig.tcpm_key, args->password, keylen);
if (args->prefix_len) {
opt = TCP_MD5SIG_EXT;
md5sig.tcpm_flags |= TCP_MD5SIG_FLAG_PREFIX;
md5sig.tcpm_prefixlen = args->prefix_len;
addr = &args->md5_prefix;
}
memcpy(&md5sig.tcpm_addr, addr, alen);
if (args->ifindex) {
opt = TCP_MD5SIG_EXT;
md5sig.tcpm_flags |= TCP_MD5SIG_FLAG_IFINDEX;
if (keylen) md5sig.tcpm_ifindex = args->ifindex;
memcpy(md5sig.tcpm_key, password, keylen); }
rc = setsockopt(sd, IPPROTO_TCP, TCP_MD5SIG, &md5sig, sizeof(md5sig)); rc = setsockopt(sd, IPPROTO_TCP, opt, &md5sig, sizeof(md5sig));
if (rc < 0) { if (rc < 0) {
/* ENOENT is harmless. Returned when a password is cleared */ /* ENOENT is harmless. Returned when a password is cleared */
if (errno == ENOENT) if (errno == ENOENT)
...@@ -254,7 +274,7 @@ static int tcp_md5_remote(int sd, struct sock_args *args) ...@@ -254,7 +274,7 @@ static int tcp_md5_remote(int sd, struct sock_args *args)
exit(1); exit(1);
} }
if (tcp_md5sig(sd, addr, alen, args->password)) if (tcp_md5sig(sd, addr, alen, args))
return -1; return -1;
return 0; return 0;
...@@ -1194,7 +1214,7 @@ static int do_server(struct sock_args *args) ...@@ -1194,7 +1214,7 @@ static int do_server(struct sock_args *args)
if (args->password && tcp_md5_remote(lsd, args)) { if (args->password && tcp_md5_remote(lsd, args)) {
close(lsd); close(lsd);
return -1; return 1;
} }
while (1) { while (1) {
...@@ -1313,7 +1333,7 @@ static int connectsock(void *addr, socklen_t alen, struct sock_args *args) ...@@ -1313,7 +1333,7 @@ static int connectsock(void *addr, socklen_t alen, struct sock_args *args)
if (args->type != SOCK_STREAM) if (args->type != SOCK_STREAM)
goto out; goto out;
if (args->password && tcp_md5sig(sd, addr, alen, args->password)) if (args->password && tcp_md5sig(sd, addr, alen, args))
goto err; goto err;
if (args->bind_test_only) if (args->bind_test_only)
...@@ -1405,16 +1425,18 @@ enum addr_type { ...@@ -1405,16 +1425,18 @@ enum addr_type {
ADDR_TYPE_MCAST, ADDR_TYPE_MCAST,
ADDR_TYPE_EXPECTED_LOCAL, ADDR_TYPE_EXPECTED_LOCAL,
ADDR_TYPE_EXPECTED_REMOTE, ADDR_TYPE_EXPECTED_REMOTE,
ADDR_TYPE_MD5_PREFIX,
}; };
static int convert_addr(struct sock_args *args, const char *_str, static int convert_addr(struct sock_args *args, const char *_str,
enum addr_type atype) enum addr_type atype)
{ {
int pfx_len_max = args->version == AF_INET6 ? 128 : 32;
int family = args->version; int family = args->version;
char *str, *dev, *sep;
struct in6_addr *in6; struct in6_addr *in6;
struct in_addr *in; struct in_addr *in;
const char *desc; const char *desc;
char *str, *dev;
void *addr; void *addr;
int rc = 0; int rc = 0;
...@@ -1443,6 +1465,30 @@ static int convert_addr(struct sock_args *args, const char *_str, ...@@ -1443,6 +1465,30 @@ static int convert_addr(struct sock_args *args, const char *_str,
desc = "expected remote"; desc = "expected remote";
addr = &args->expected_raddr; addr = &args->expected_raddr;
break; break;
case ADDR_TYPE_MD5_PREFIX:
desc = "md5 prefix";
if (family == AF_INET) {
args->md5_prefix.v4.sin_family = AF_INET;
addr = &args->md5_prefix.v4.sin_addr;
} else if (family == AF_INET6) {
args->md5_prefix.v6.sin6_family = AF_INET6;
addr = &args->md5_prefix.v6.sin6_addr;
} else
return 1;
sep = strchr(str, '/');
if (sep) {
*sep = '\0';
sep++;
if (str_to_uint(sep, 1, pfx_len_max,
&args->prefix_len) != 0) {
fprintf(stderr, "Invalid port\n");
return 1;
}
} else {
args->prefix_len = pfx_len_max;
}
break;
default: default:
log_error("unknown address type"); log_error("unknown address type");
exit(1); exit(1);
...@@ -1522,7 +1568,7 @@ static char *random_msg(int len) ...@@ -1522,7 +1568,7 @@ static char *random_msg(int len)
return m; return m;
} }
#define GETOPT_STR "sr:l:p:t:g:P:DRn:M:d:SCi6L:0:1:2:Fbq" #define GETOPT_STR "sr:l:p:t:g:P:DRn:M:m:d:SCi6L:0:1:2:Fbq"
static void print_usage(char *prog) static void print_usage(char *prog)
{ {
...@@ -1551,6 +1597,7 @@ static void print_usage(char *prog) ...@@ -1551,6 +1597,7 @@ static void print_usage(char *prog)
" -n num number of times to send message\n" " -n num number of times to send message\n"
"\n" "\n"
" -M password use MD5 sum protection\n" " -M password use MD5 sum protection\n"
" -m prefix/len prefix and length to use for MD5 key\n"
" -g grp multicast group (e.g., 239.1.1.1)\n" " -g grp multicast group (e.g., 239.1.1.1)\n"
" -i interactive mode (default is echo and terminate)\n" " -i interactive mode (default is echo and terminate)\n"
"\n" "\n"
...@@ -1642,6 +1689,10 @@ int main(int argc, char *argv[]) ...@@ -1642,6 +1689,10 @@ int main(int argc, char *argv[])
case 'M': case 'M':
args.password = optarg; args.password = optarg;
break; break;
case 'm':
if (convert_addr(&args, optarg, ADDR_TYPE_MD5_PREFIX) < 0)
return 1;
break;
case 'S': case 'S':
args.use_setsockopt = 1; args.use_setsockopt = 1;
break; break;
...@@ -1706,11 +1757,16 @@ int main(int argc, char *argv[]) ...@@ -1706,11 +1757,16 @@ int main(int argc, char *argv[])
} }
if (args.password && if (args.password &&
(!args.has_remote_ip || args.type != SOCK_STREAM)) { ((!args.has_remote_ip && !args.prefix_len) || args.type != SOCK_STREAM)) {
log_error("MD5 passwords apply to TCP only and require a remote ip for the password\n"); log_error("MD5 passwords apply to TCP only and require a remote ip for the password\n");
return 1; return 1;
} }
if (args.prefix_len && !args.password) {
log_error("Prefix range for MD5 protection specified without a password\n");
return 1;
}
if ((args.use_setsockopt || args.use_cmsg) && !args.ifindex) { if ((args.use_setsockopt || args.use_cmsg) && !args.ifindex) {
fprintf(stderr, "Device binding not specified\n"); fprintf(stderr, "Device binding not specified\n");
return 1; return 1;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment