Skip to content

Commit b11a632

Browse files
jrfastabborkmann
authored andcommitted
net: add a UID to use for ULP socket assignment
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>
1 parent 7b4eb53 commit b11a632

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

include/net/tcp.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,10 @@ enum hrtimer_restart tcp_pace_kick(struct hrtimer *timer);
19831983
#define TCP_ULP_MAX 128
19841984
#define TCP_ULP_BUF_MAX (TCP_ULP_NAME_MAX*TCP_ULP_MAX)
19851985

1986+
enum {
1987+
TCP_ULP_TLS,
1988+
};
1989+
19861990
struct tcp_ulp_ops {
19871991
struct list_head list;
19881992

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

1998+
int uid;
19941999
char name[TCP_ULP_NAME_MAX];
2000+
bool user_visible;
19952001
struct module *owner;
19962002
};
19972003
int tcp_register_ulp(struct tcp_ulp_ops *type);

net/ipv4/tcp_ulp.c

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ static struct tcp_ulp_ops *tcp_ulp_find(const char *name)
2929
return NULL;
3030
}
3131

32+
static struct tcp_ulp_ops *tcp_ulp_find_id(const int ulp)
33+
{
34+
struct tcp_ulp_ops *e;
35+
36+
list_for_each_entry_rcu(e, &tcp_ulp_list, list) {
37+
if (e->uid == ulp)
38+
return e;
39+
}
40+
41+
return NULL;
42+
}
43+
3244
static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
3345
{
3446
const struct tcp_ulp_ops *ulp = NULL;
@@ -51,6 +63,18 @@ static const struct tcp_ulp_ops *__tcp_ulp_find_autoload(const char *name)
5163
return ulp;
5264
}
5365

66+
static const struct tcp_ulp_ops *__tcp_ulp_lookup(const int uid)
67+
{
68+
const struct tcp_ulp_ops *ulp;
69+
70+
rcu_read_lock();
71+
ulp = tcp_ulp_find_id(uid);
72+
if (!ulp || !try_module_get(ulp->owner))
73+
ulp = NULL;
74+
rcu_read_unlock();
75+
return ulp;
76+
}
77+
5478
/* Attach new upper layer protocol to the list
5579
* of available protocols.
5680
*/
@@ -59,13 +83,10 @@ int tcp_register_ulp(struct tcp_ulp_ops *ulp)
5983
int ret = 0;
6084

6185
spin_lock(&tcp_ulp_list_lock);
62-
if (tcp_ulp_find(ulp->name)) {
63-
pr_notice("%s already registered or non-unique name\n",
64-
ulp->name);
86+
if (tcp_ulp_find(ulp->name))
6587
ret = -EEXIST;
66-
} else {
88+
else
6789
list_add_tail_rcu(&ulp->list, &tcp_ulp_list);
68-
}
6990
spin_unlock(&tcp_ulp_list_lock);
7091

7192
return ret;
@@ -124,6 +145,34 @@ int tcp_set_ulp(struct sock *sk, const char *name)
124145
if (!ulp_ops)
125146
return -ENOENT;
126147

148+
if (!ulp_ops->user_visible) {
149+
module_put(ulp_ops->owner);
150+
return -ENOENT;
151+
}
152+
153+
err = ulp_ops->init(sk);
154+
if (err) {
155+
module_put(ulp_ops->owner);
156+
return err;
157+
}
158+
159+
icsk->icsk_ulp_ops = ulp_ops;
160+
return 0;
161+
}
162+
163+
int tcp_set_ulp_id(struct sock *sk, int ulp)
164+
{
165+
struct inet_connection_sock *icsk = inet_csk(sk);
166+
const struct tcp_ulp_ops *ulp_ops;
167+
int err;
168+
169+
if (icsk->icsk_ulp_ops)
170+
return -EEXIST;
171+
172+
ulp_ops = __tcp_ulp_lookup(ulp);
173+
if (!ulp_ops)
174+
return -ENOENT;
175+
127176
err = ulp_ops->init(sk);
128177
if (err) {
129178
module_put(ulp_ops->owner);

net/tls/tls_main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ static int tls_init(struct sock *sk)
484484

485485
static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
486486
.name = "tls",
487+
.uid = TCP_ULP_TLS,
488+
.user_visible = true,
487489
.owner = THIS_MODULE,
488490
.init = tls_init,
489491
};

0 commit comments

Comments
 (0)