Skip to content

Commit

Permalink
raw: use more conventional iterators
Browse files Browse the repository at this point in the history
In order to prepare the following patch,
I change raw v4 & v6 code to use more conventional
iterators.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Eric Dumazet authored and David S. Miller committed Jun 19, 2022
1 parent 8670dc3 commit ba44f81
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 150 deletions.
5 changes: 2 additions & 3 deletions include/net/raw.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
extern struct proto raw_prot;

extern struct raw_hashinfo raw_v4_hashinfo;
struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
unsigned short num, __be32 raddr,
__be32 laddr, int dif, int sdif);
bool raw_v4_match(struct net *net, struct sock *sk, unsigned short num,
__be32 raddr, __be32 laddr, int dif, int sdif);

int raw_abort(struct sock *sk, int err);
void raw_icmp_error(struct sk_buff *, int, u32);
Expand Down
6 changes: 3 additions & 3 deletions include/net/rawv6.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include <net/protocol.h>

extern struct raw_hashinfo raw_v6_hashinfo;
struct sock *__raw_v6_lookup(struct net *net, struct sock *sk,
unsigned short num, const struct in6_addr *loc_addr,
const struct in6_addr *rmt_addr, int dif, int sdif);
bool raw_v6_match(struct net *net, struct sock *sk, unsigned short num,
const struct in6_addr *loc_addr,
const struct in6_addr *rmt_addr, int dif, int sdif);

int raw_abort(struct sock *sk, int err);

Expand Down
93 changes: 33 additions & 60 deletions net/ipv4/raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,19 @@ void raw_unhash_sk(struct sock *sk)
}
EXPORT_SYMBOL_GPL(raw_unhash_sk);

struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
unsigned short num, __be32 raddr, __be32 laddr,
int dif, int sdif)
bool raw_v4_match(struct net *net, struct sock *sk, unsigned short num,
__be32 raddr, __be32 laddr, int dif, int sdif)
{
sk_for_each_from(sk) {
struct inet_sock *inet = inet_sk(sk);

if (net_eq(sock_net(sk), net) && inet->inet_num == num &&
!(inet->inet_daddr && inet->inet_daddr != raddr) &&
!(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
raw_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
goto found; /* gotcha */
}
sk = NULL;
found:
return sk;
struct inet_sock *inet = inet_sk(sk);

if (net_eq(sock_net(sk), net) && inet->inet_num == num &&
!(inet->inet_daddr && inet->inet_daddr != raddr) &&
!(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
raw_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
return true;
return false;
}
EXPORT_SYMBOL_GPL(__raw_v4_lookup);
EXPORT_SYMBOL_GPL(raw_v4_match);

/*
* 0 - deliver
Expand Down Expand Up @@ -168,23 +163,21 @@ static int icmp_filter(const struct sock *sk, const struct sk_buff *skb)
*/
static int raw_v4_input(struct sk_buff *skb, const struct iphdr *iph, int hash)
{
struct net *net = dev_net(skb->dev);
int sdif = inet_sdif(skb);
int dif = inet_iif(skb);
struct sock *sk;
struct hlist_head *head;
int delivered = 0;
struct net *net;
struct sock *sk;

read_lock(&raw_v4_hashinfo.lock);
head = &raw_v4_hashinfo.ht[hash];
if (hlist_empty(head))
goto out;

net = dev_net(skb->dev);
sk = __raw_v4_lookup(net, __sk_head(head), iph->protocol,
iph->saddr, iph->daddr, dif, sdif);

while (sk) {
return 0;
read_lock(&raw_v4_hashinfo.lock);
sk_for_each(sk, head) {
if (!raw_v4_match(net, sk, iph->protocol,
iph->saddr, iph->daddr, dif, sdif))
continue;
delivered = 1;
if ((iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) &&
ip_mc_sf_allow(sk, iph->daddr, iph->saddr,
Expand All @@ -195,31 +188,16 @@ static int raw_v4_input(struct sk_buff *skb, const struct iphdr *iph, int hash)
if (clone)
raw_rcv(sk, clone);
}
sk = __raw_v4_lookup(net, sk_next(sk), iph->protocol,
iph->saddr, iph->daddr,
dif, sdif);
}
out:
read_unlock(&raw_v4_hashinfo.lock);
return delivered;
}

int raw_local_deliver(struct sk_buff *skb, int protocol)
{
int hash;
struct sock *raw_sk;

hash = protocol & (RAW_HTABLE_SIZE - 1);
raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);

/* If there maybe a raw socket we must check - if not we
* don't care less
*/
if (raw_sk && !raw_v4_input(skb, ip_hdr(skb), hash))
raw_sk = NULL;

return raw_sk != NULL;
int hash = protocol & (RAW_HTABLE_SIZE - 1);

return raw_v4_input(skb, ip_hdr(skb), hash);
}

static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)
Expand Down Expand Up @@ -286,29 +264,24 @@ static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)

void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info)
{
int hash;
struct sock *raw_sk;
struct net *net = dev_net(skb->dev);;
int dif = skb->dev->ifindex;
int sdif = inet_sdif(skb);
struct hlist_head *head;
const struct iphdr *iph;
struct net *net;
struct sock *sk;
int hash;

hash = protocol & (RAW_HTABLE_SIZE - 1);
head = &raw_v4_hashinfo.ht[hash];

read_lock(&raw_v4_hashinfo.lock);
raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
if (raw_sk) {
int dif = skb->dev->ifindex;
int sdif = inet_sdif(skb);

sk_for_each(sk, head) {
iph = (const struct iphdr *)skb->data;
net = dev_net(skb->dev);

while ((raw_sk = __raw_v4_lookup(net, raw_sk, protocol,
iph->daddr, iph->saddr,
dif, sdif)) != NULL) {
raw_err(raw_sk, skb, info);
raw_sk = sk_next(raw_sk);
iph = (const struct iphdr *)skb->data;
}
if (!raw_v4_match(net, sk, iph->protocol,
iph->saddr, iph->daddr, dif, sdif))
continue;
raw_err(sk, skb, info);
}
read_unlock(&raw_v4_hashinfo.lock);
}
Expand Down
33 changes: 16 additions & 17 deletions net/ipv4/raw_diag.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,41 +34,39 @@ raw_get_hashinfo(const struct inet_diag_req_v2 *r)
* use helper to figure it out.
*/

static struct sock *raw_lookup(struct net *net, struct sock *from,
const struct inet_diag_req_v2 *req)
static bool raw_lookup(struct net *net, struct sock *sk,
const struct inet_diag_req_v2 *req)
{
struct inet_diag_req_raw *r = (void *)req;
struct sock *sk = NULL;

if (r->sdiag_family == AF_INET)
sk = __raw_v4_lookup(net, from, r->sdiag_raw_protocol,
r->id.idiag_dst[0],
r->id.idiag_src[0],
r->id.idiag_if, 0);
return raw_v4_match(net, sk, r->sdiag_raw_protocol,
r->id.idiag_dst[0],
r->id.idiag_src[0],
r->id.idiag_if, 0);
#if IS_ENABLED(CONFIG_IPV6)
else
sk = __raw_v6_lookup(net, from, r->sdiag_raw_protocol,
(const struct in6_addr *)r->id.idiag_src,
(const struct in6_addr *)r->id.idiag_dst,
r->id.idiag_if, 0);
return raw_v6_match(net, sk, r->sdiag_raw_protocol,
(const struct in6_addr *)r->id.idiag_src,
(const struct in6_addr *)r->id.idiag_dst,
r->id.idiag_if, 0);
#endif
return sk;
return false;
}

static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2 *r)
{
struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
struct sock *sk = NULL, *s;
struct sock *sk;
int slot;

if (IS_ERR(hashinfo))
return ERR_CAST(hashinfo);

read_lock(&hashinfo->lock);
for (slot = 0; slot < RAW_HTABLE_SIZE; slot++) {
sk_for_each(s, &hashinfo->ht[slot]) {
sk = raw_lookup(net, s, r);
if (sk) {
sk_for_each(sk, &hashinfo->ht[slot]) {
if (raw_lookup(net, sk, r)) {
/*
* Grab it and keep until we fill
* diag meaage to be reported, so
Expand All @@ -81,10 +79,11 @@ static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2
}
}
}
sk = ERR_PTR(-ENOENT);
out_unlock:
read_unlock(&hashinfo->lock);

return sk ? sk : ERR_PTR(-ENOENT);
return sk;
}

static int raw_diag_dump_one(struct netlink_callback *cb,
Expand Down
Loading

0 comments on commit ba44f81

Please sign in to comment.