Skip to content

Commit

Permalink
net: add a UID to use for ULP socket assignment
Browse files Browse the repository at this point in the history
Create a UID field and enum that can be used to assign ULPs to
sockets. This saves a set of string comparisons if the ULP id
is known.

For sockmap, which is added in the next patches, a ULP is used to
hook into TCP sockets close state. In this case the ULP being added
is done at map insert time and the ULP is known and done on the kernel
side. In this case the named lookup is not needed. Because we don't
want to expose psock internals to user space socket options a user
visible flag is also added. For TLS this is set for BPF it will be
cleared.

Alos remove pr_notice, user gets an error code back and should check
that rather than rely on logs.

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
  • Loading branch information
John Fastabend authored and Daniel Borkmann committed Feb 6, 2018
1 parent 7b4eb53 commit b11a632
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
6 changes: 6 additions & 0 deletions include/net/tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,10 @@ enum hrtimer_restart tcp_pace_kick(struct hrtimer *timer);
#define TCP_ULP_MAX 128
#define TCP_ULP_BUF_MAX (TCP_ULP_NAME_MAX*TCP_ULP_MAX)

enum {
TCP_ULP_TLS,
};

struct tcp_ulp_ops {
struct list_head list;

Expand All @@ -1991,7 +1995,9 @@ struct tcp_ulp_ops {
/* cleanup ulp */
void (*release)(struct sock *sk);

int uid;
char name[TCP_ULP_NAME_MAX];
bool user_visible;
struct module *owner;
};
int tcp_register_ulp(struct tcp_ulp_ops *type);
Expand Down
59 changes: 54 additions & 5 deletions net/ipv4/tcp_ulp.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ static struct tcp_ulp_ops *tcp_ulp_find(const char *name)
return NULL;
}

static struct tcp_ulp_ops *tcp_ulp_find_id(const int ulp)
{
struct tcp_ulp_ops *e;

list_for_each_entry_rcu(e, &tcp_ulp_list, list) {
if (e->uid == ulp)
return e;
}

return NULL;
}

static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
{
const struct tcp_ulp_ops *ulp = NULL;
Expand All @@ -51,6 +63,18 @@ static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
return ulp;
}

static const struct tcp_ulp_ops *__tcp_ulp_lookup(const int uid)
{
const struct tcp_ulp_ops *ulp;

rcu_read_lock();
ulp = tcp_ulp_find_id(uid);
if (!ulp || !try_module_get(ulp->owner))
ulp = NULL;
rcu_read_unlock();
return ulp;
}

/* Attach new upper layer protocol to the list
* of available protocols.
*/
Expand All @@ -59,13 +83,10 @@ int tcp_register_ulp(struct tcp_ulp_ops *ulp)
int ret = 0;

spin_lock(&tcp_ulp_list_lock);
if (tcp_ulp_find(ulp->name)) {
pr_notice("%s already registered or non-unique name\n",
ulp->name);
if (tcp_ulp_find(ulp->name))
ret = -EEXIST;
} else {
else
list_add_tail_rcu(&ulp->list, &tcp_ulp_list);
}
spin_unlock(&tcp_ulp_list_lock);

return ret;
Expand Down Expand Up @@ -124,6 +145,34 @@ int tcp_set_ulp(struct sock *sk, const char *name)
if (!ulp_ops)
return -ENOENT;

if (!ulp_ops->user_visible) {
module_put(ulp_ops->owner);
return -ENOENT;
}

err = ulp_ops->init(sk);
if (err) {
module_put(ulp_ops->owner);
return err;
}

icsk->icsk_ulp_ops = ulp_ops;
return 0;
}

int tcp_set_ulp_id(struct sock *sk, int ulp)
{
struct inet_connection_sock *icsk = inet_csk(sk);
const struct tcp_ulp_ops *ulp_ops;
int err;

if (icsk->icsk_ulp_ops)
return -EEXIST;

ulp_ops = __tcp_ulp_lookup(ulp);
if (!ulp_ops)
return -ENOENT;

err = ulp_ops->init(sk);
if (err) {
module_put(ulp_ops->owner);
Expand Down
2 changes: 2 additions & 0 deletions net/tls/tls_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ static int tls_init(struct sock *sk)

static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
.name = "tls",
.uid = TCP_ULP_TLS,
.user_visible = true,
.owner = THIS_MODULE,
.init = tls_init,
};
Expand Down

0 comments on commit b11a632

Please sign in to comment.