1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/filter.h>
5 #include <net/net_namespace.h>
8 * Functions to manage BPF programs attached to netns
11 struct bpf_netns_link
{
13 enum bpf_attach_type type
;
14 enum netns_bpf_attach_type netns_type
;
16 /* We don't hold a ref to net in order to auto-detach the link
17 * when netns is going away. Instead we rely on pernet
18 * pre_exit callback to clear this pointer. Must be accessed
19 * with netns_bpf_mutex held.
22 struct list_head node
; /* node in list of links attached to net */
25 /* Protects updates to netns_bpf */
26 DEFINE_MUTEX(netns_bpf_mutex
);
28 static void netns_bpf_attach_type_unneed(enum netns_bpf_attach_type type
)
32 case NETNS_BPF_SK_LOOKUP
:
33 static_branch_dec(&bpf_sk_lookup_enabled
);
41 static void netns_bpf_attach_type_need(enum netns_bpf_attach_type type
)
45 case NETNS_BPF_SK_LOOKUP
:
46 static_branch_inc(&bpf_sk_lookup_enabled
);
54 /* Must be called with netns_bpf_mutex held. */
55 static void netns_bpf_run_array_detach(struct net
*net
,
56 enum netns_bpf_attach_type type
)
58 struct bpf_prog_array
*run_array
;
60 run_array
= rcu_replace_pointer(net
->bpf
.run_array
[type
], NULL
,
61 lockdep_is_held(&netns_bpf_mutex
));
62 bpf_prog_array_free(run_array
);
65 static int link_index(struct net
*net
, enum netns_bpf_attach_type type
,
66 struct bpf_netns_link
*link
)
68 struct bpf_netns_link
*pos
;
71 list_for_each_entry(pos
, &net
->bpf
.links
[type
], node
) {
79 static int link_count(struct net
*net
, enum netns_bpf_attach_type type
)
81 struct list_head
*pos
;
84 list_for_each(pos
, &net
->bpf
.links
[type
])
89 static void fill_prog_array(struct net
*net
, enum netns_bpf_attach_type type
,
90 struct bpf_prog_array
*prog_array
)
92 struct bpf_netns_link
*pos
;
95 list_for_each_entry(pos
, &net
->bpf
.links
[type
], node
) {
96 prog_array
->items
[i
].prog
= pos
->link
.prog
;
101 static void bpf_netns_link_release(struct bpf_link
*link
)
103 struct bpf_netns_link
*net_link
=
104 container_of(link
, struct bpf_netns_link
, link
);
105 enum netns_bpf_attach_type type
= net_link
->netns_type
;
106 struct bpf_prog_array
*old_array
, *new_array
;
110 mutex_lock(&netns_bpf_mutex
);
112 /* We can race with cleanup_net, but if we see a non-NULL
113 * struct net pointer, pre_exit has not run yet and wait for
120 /* Mark attach point as unused */
121 netns_bpf_attach_type_unneed(type
);
123 /* Remember link position in case of safe delete */
124 idx
= link_index(net
, type
, net_link
);
125 list_del(&net_link
->node
);
127 cnt
= link_count(net
, type
);
129 netns_bpf_run_array_detach(net
, type
);
133 old_array
= rcu_dereference_protected(net
->bpf
.run_array
[type
],
134 lockdep_is_held(&netns_bpf_mutex
));
135 new_array
= bpf_prog_array_alloc(cnt
, GFP_KERNEL
);
137 WARN_ON(bpf_prog_array_delete_safe_at(old_array
, idx
));
140 fill_prog_array(net
, type
, new_array
);
141 rcu_assign_pointer(net
->bpf
.run_array
[type
], new_array
);
142 bpf_prog_array_free(old_array
);
145 net_link
->net
= NULL
;
146 mutex_unlock(&netns_bpf_mutex
);
149 static int bpf_netns_link_detach(struct bpf_link
*link
)
151 bpf_netns_link_release(link
);
155 static void bpf_netns_link_dealloc(struct bpf_link
*link
)
157 struct bpf_netns_link
*net_link
=
158 container_of(link
, struct bpf_netns_link
, link
);
163 static int bpf_netns_link_update_prog(struct bpf_link
*link
,
164 struct bpf_prog
*new_prog
,
165 struct bpf_prog
*old_prog
)
167 struct bpf_netns_link
*net_link
=
168 container_of(link
, struct bpf_netns_link
, link
);
169 enum netns_bpf_attach_type type
= net_link
->netns_type
;
170 struct bpf_prog_array
*run_array
;
174 if (old_prog
&& old_prog
!= link
->prog
)
176 if (new_prog
->type
!= link
->prog
->type
)
179 mutex_lock(&netns_bpf_mutex
);
182 if (!net
|| !check_net(net
)) {
183 /* Link auto-detached or netns dying */
188 run_array
= rcu_dereference_protected(net
->bpf
.run_array
[type
],
189 lockdep_is_held(&netns_bpf_mutex
));
190 idx
= link_index(net
, type
, net_link
);
191 ret
= bpf_prog_array_update_at(run_array
, idx
, new_prog
);
195 old_prog
= xchg(&link
->prog
, new_prog
);
196 bpf_prog_put(old_prog
);
199 mutex_unlock(&netns_bpf_mutex
);
203 static int bpf_netns_link_fill_info(const struct bpf_link
*link
,
204 struct bpf_link_info
*info
)
206 const struct bpf_netns_link
*net_link
=
207 container_of(link
, struct bpf_netns_link
, link
);
208 unsigned int inum
= 0;
211 mutex_lock(&netns_bpf_mutex
);
213 if (net
&& check_net(net
))
215 mutex_unlock(&netns_bpf_mutex
);
217 info
->netns
.netns_ino
= inum
;
218 info
->netns
.attach_type
= net_link
->type
;
222 static void bpf_netns_link_show_fdinfo(const struct bpf_link
*link
,
223 struct seq_file
*seq
)
225 struct bpf_link_info info
= {};
227 bpf_netns_link_fill_info(link
, &info
);
230 "attach_type:\t%u\n",
231 info
.netns
.netns_ino
,
232 info
.netns
.attach_type
);
235 static const struct bpf_link_ops bpf_netns_link_ops
= {
236 .release
= bpf_netns_link_release
,
237 .dealloc
= bpf_netns_link_dealloc
,
238 .detach
= bpf_netns_link_detach
,
239 .update_prog
= bpf_netns_link_update_prog
,
240 .fill_link_info
= bpf_netns_link_fill_info
,
241 .show_fdinfo
= bpf_netns_link_show_fdinfo
,
244 /* Must be called with netns_bpf_mutex held. */
245 static int __netns_bpf_prog_query(const union bpf_attr
*attr
,
246 union bpf_attr __user
*uattr
,
248 enum netns_bpf_attach_type type
)
250 __u32 __user
*prog_ids
= u64_to_user_ptr(attr
->query
.prog_ids
);
251 struct bpf_prog_array
*run_array
;
252 u32 prog_cnt
= 0, flags
= 0;
254 run_array
= rcu_dereference_protected(net
->bpf
.run_array
[type
],
255 lockdep_is_held(&netns_bpf_mutex
));
257 prog_cnt
= bpf_prog_array_length(run_array
);
259 if (copy_to_user(&uattr
->query
.attach_flags
, &flags
, sizeof(flags
)))
261 if (copy_to_user(&uattr
->query
.prog_cnt
, &prog_cnt
, sizeof(prog_cnt
)))
263 if (!attr
->query
.prog_cnt
|| !prog_ids
|| !prog_cnt
)
266 return bpf_prog_array_copy_to_user(run_array
, prog_ids
,
267 attr
->query
.prog_cnt
);
270 int netns_bpf_prog_query(const union bpf_attr
*attr
,
271 union bpf_attr __user
*uattr
)
273 enum netns_bpf_attach_type type
;
277 if (attr
->query
.query_flags
)
280 type
= to_netns_bpf_attach_type(attr
->query
.attach_type
);
284 net
= get_net_ns_by_fd(attr
->query
.target_fd
);
288 mutex_lock(&netns_bpf_mutex
);
289 ret
= __netns_bpf_prog_query(attr
, uattr
, net
, type
);
290 mutex_unlock(&netns_bpf_mutex
);
296 int netns_bpf_prog_attach(const union bpf_attr
*attr
, struct bpf_prog
*prog
)
298 struct bpf_prog_array
*run_array
;
299 enum netns_bpf_attach_type type
;
300 struct bpf_prog
*attached
;
304 if (attr
->target_fd
|| attr
->attach_flags
|| attr
->replace_bpf_fd
)
307 type
= to_netns_bpf_attach_type(attr
->attach_type
);
311 net
= current
->nsproxy
->net_ns
;
312 mutex_lock(&netns_bpf_mutex
);
314 /* Attaching prog directly is not compatible with links */
315 if (!list_empty(&net
->bpf
.links
[type
])) {
321 case NETNS_BPF_FLOW_DISSECTOR
:
322 ret
= flow_dissector_bpf_prog_attach_check(net
, prog
);
331 attached
= net
->bpf
.progs
[type
];
332 if (attached
== prog
) {
333 /* The same program cannot be attached twice */
338 run_array
= rcu_dereference_protected(net
->bpf
.run_array
[type
],
339 lockdep_is_held(&netns_bpf_mutex
));
341 WRITE_ONCE(run_array
->items
[0].prog
, prog
);
343 run_array
= bpf_prog_array_alloc(1, GFP_KERNEL
);
348 run_array
->items
[0].prog
= prog
;
349 rcu_assign_pointer(net
->bpf
.run_array
[type
], run_array
);
352 net
->bpf
.progs
[type
] = prog
;
354 bpf_prog_put(attached
);
357 mutex_unlock(&netns_bpf_mutex
);
362 /* Must be called with netns_bpf_mutex held. */
363 static int __netns_bpf_prog_detach(struct net
*net
,
364 enum netns_bpf_attach_type type
,
365 struct bpf_prog
*old
)
367 struct bpf_prog
*attached
;
369 /* Progs attached via links cannot be detached */
370 if (!list_empty(&net
->bpf
.links
[type
]))
373 attached
= net
->bpf
.progs
[type
];
374 if (!attached
|| attached
!= old
)
376 netns_bpf_run_array_detach(net
, type
);
377 net
->bpf
.progs
[type
] = NULL
;
378 bpf_prog_put(attached
);
382 int netns_bpf_prog_detach(const union bpf_attr
*attr
, enum bpf_prog_type ptype
)
384 enum netns_bpf_attach_type type
;
385 struct bpf_prog
*prog
;
391 type
= to_netns_bpf_attach_type(attr
->attach_type
);
395 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
, ptype
);
397 return PTR_ERR(prog
);
399 mutex_lock(&netns_bpf_mutex
);
400 ret
= __netns_bpf_prog_detach(current
->nsproxy
->net_ns
, type
, prog
);
401 mutex_unlock(&netns_bpf_mutex
);
408 static int netns_bpf_max_progs(enum netns_bpf_attach_type type
)
411 case NETNS_BPF_FLOW_DISSECTOR
:
413 case NETNS_BPF_SK_LOOKUP
:
420 static int netns_bpf_link_attach(struct net
*net
, struct bpf_link
*link
,
421 enum netns_bpf_attach_type type
)
423 struct bpf_netns_link
*net_link
=
424 container_of(link
, struct bpf_netns_link
, link
);
425 struct bpf_prog_array
*run_array
;
428 mutex_lock(&netns_bpf_mutex
);
430 cnt
= link_count(net
, type
);
431 if (cnt
>= netns_bpf_max_progs(type
)) {
435 /* Links are not compatible with attaching prog directly */
436 if (net
->bpf
.progs
[type
]) {
442 case NETNS_BPF_FLOW_DISSECTOR
:
443 err
= flow_dissector_bpf_prog_attach_check(net
, link
->prog
);
445 case NETNS_BPF_SK_LOOKUP
:
446 err
= 0; /* nothing to check */
455 run_array
= bpf_prog_array_alloc(cnt
+ 1, GFP_KERNEL
);
461 list_add_tail(&net_link
->node
, &net
->bpf
.links
[type
]);
463 fill_prog_array(net
, type
, run_array
);
464 run_array
= rcu_replace_pointer(net
->bpf
.run_array
[type
], run_array
,
465 lockdep_is_held(&netns_bpf_mutex
));
466 bpf_prog_array_free(run_array
);
468 /* Mark attach point as used */
469 netns_bpf_attach_type_need(type
);
472 mutex_unlock(&netns_bpf_mutex
);
476 int netns_bpf_link_create(const union bpf_attr
*attr
, struct bpf_prog
*prog
)
478 enum netns_bpf_attach_type netns_type
;
479 struct bpf_link_primer link_primer
;
480 struct bpf_netns_link
*net_link
;
481 enum bpf_attach_type type
;
485 if (attr
->link_create
.flags
)
488 type
= attr
->link_create
.attach_type
;
489 netns_type
= to_netns_bpf_attach_type(type
);
493 net
= get_net_ns_by_fd(attr
->link_create
.target_fd
);
497 net_link
= kzalloc(sizeof(*net_link
), GFP_USER
);
502 bpf_link_init(&net_link
->link
, BPF_LINK_TYPE_NETNS
,
503 &bpf_netns_link_ops
, prog
);
505 net_link
->type
= type
;
506 net_link
->netns_type
= netns_type
;
508 err
= bpf_link_prime(&net_link
->link
, &link_primer
);
514 err
= netns_bpf_link_attach(net
, &net_link
->link
, netns_type
);
516 bpf_link_cleanup(&link_primer
);
521 return bpf_link_settle(&link_primer
);
528 static int __net_init
netns_bpf_pernet_init(struct net
*net
)
532 for (type
= 0; type
< MAX_NETNS_BPF_ATTACH_TYPE
; type
++)
533 INIT_LIST_HEAD(&net
->bpf
.links
[type
]);
538 static void __net_exit
netns_bpf_pernet_pre_exit(struct net
*net
)
540 enum netns_bpf_attach_type type
;
541 struct bpf_netns_link
*net_link
;
543 mutex_lock(&netns_bpf_mutex
);
544 for (type
= 0; type
< MAX_NETNS_BPF_ATTACH_TYPE
; type
++) {
545 netns_bpf_run_array_detach(net
, type
);
546 list_for_each_entry(net_link
, &net
->bpf
.links
[type
], node
) {
547 net_link
->net
= NULL
; /* auto-detach link */
548 netns_bpf_attach_type_unneed(type
);
550 if (net
->bpf
.progs
[type
])
551 bpf_prog_put(net
->bpf
.progs
[type
]);
553 mutex_unlock(&netns_bpf_mutex
);
556 static struct pernet_operations netns_bpf_pernet_ops __net_initdata
= {
557 .init
= netns_bpf_pernet_init
,
558 .pre_exit
= netns_bpf_pernet_pre_exit
,
561 static int __init
netns_bpf_init(void)
563 return register_pernet_subsys(&netns_bpf_pernet_ops
);
566 subsys_initcall(netns_bpf_init
);