| @@ -5156,15 +5156,15 @@ static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple, · 5155 unmodified lines |
| 5156 | 5156 | return sk; |
| 5157 | 5157 | } |
| 5158 | 5158 | |
| 5159 | | −/* bpf_sk_lookup performs the core lookup for different types of sockets, |
| 5159 | +/* bpf_skc_lookup performs the core lookup for different types of sockets, |
| 5160 | 5160 | * taking a reference on the socket if it doesn't have the flag SOCK_RCU_FREE. |
| 5161 | 5161 | * Returns the socket as an 'unsigned long' to simplify the casting in the |
| 5162 | 5162 | * callers to satisfy BPF_CALL declarations. |
| 5163 | 5163 | */ |
| 5164 | | −static unsigned long |
| 5165 | | −__bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, |
| 5166 | | − struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id, |
| 5167 | | − u64 flags) |
| 5164 | +static struct sock * |
| 5165 | +__bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, |
| 5166 | + struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id, |
| 5167 | + u64 flags) |
| 5168 | 5168 | { |
| 5169 | 5169 | struct sock *sk = NULL; |
| 5170 | 5170 | u8 family = AF_UNSPEC; |
| @@ -5192,15 +5192,27 @@ __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, · 21 unmodified lines |
| 5192 | 5192 | put_net(net); |
| 5193 | 5193 | } |
| 5194 | 5194 | |
| 5195 | +out: |
| 5196 | + return sk; |
| 5197 | +} |
| 5198 | + |
| 5199 | +static struct sock * |
| 5200 | +__bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, |
| 5201 | + struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id, |
| 5202 | + u64 flags) |
| 5203 | +{ |
| 5204 | + struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net, |
| 5205 | + ifindex, proto, netns_id, flags); |
| 5206 | + |
| 5195 | 5207 | if (sk) |
| 5196 | 5208 | sk = sk_to_full_sk(sk); |
| 5197 | | −out: |
| 5198 | | − return (unsigned long) sk; |
| 5209 | + |
| 5210 | + return sk; |
| 5199 | 5211 | } |
| 5200 | 5212 | |
| 5201 | | −static unsigned long |
| 5202 | | −bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, |
| 5203 | | − u8 proto, u64 netns_id, u64 flags) |
| 5213 | +static struct sock * |
| 5214 | +bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, |
| 5215 | + u8 proto, u64 netns_id, u64 flags) |
| 5204 | 5216 | { |
| 5205 | 5217 | struct net *caller_net; |
| 5206 | 5218 | int ifindex; |
| @@ -5213,14 +5225,47 @@ bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, · 6 unmodified lines |
| 5213 | 5225 | ifindex = 0; |
| 5214 | 5226 | } |
| 5215 | 5227 | |
| 5216 | | − return __bpf_sk_lookup(skb, tuple, len, caller_net, ifindex, |
| 5217 | | − proto, netns_id, flags); |
| 5228 | + return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto, |
| 5229 | + netns_id, flags); |
| 5218 | 5230 | } |
| 5219 | 5231 | |
| 5232 | +static struct sock * |
| 5233 | +bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, |
| 5234 | + u8 proto, u64 netns_id, u64 flags) |
| 5235 | +{ |
| 5236 | + struct sock *sk = bpf_skc_lookup(skb, tuple, len, proto, netns_id, |
| 5237 | + flags); |
| 5238 | + |
| 5239 | + if (sk) |
| 5240 | + sk = sk_to_full_sk(sk); |
| 5241 | + |
| 5242 | + return sk; |
| 5243 | +} |
| 5244 | + |
| 5245 | +BPF_CALL_5(bpf_skc_lookup_tcp, struct sk_buff *, skb, |
| 5246 | + struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) |
| 5247 | +{ |
| 5248 | + return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP, |
| 5249 | + netns_id, flags); |
| 5250 | +} |
| 5251 | + |
| 5252 | +static const struct bpf_func_proto bpf_skc_lookup_tcp_proto = { |
| 5253 | + .func = bpf_skc_lookup_tcp, |
| 5254 | + .gpl_only = false, |
| 5255 | + .pkt_access = true, |
| 5256 | + .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, |
| 5257 | + .arg1_type = ARG_PTR_TO_CTX, |
| 5258 | + .arg2_type = ARG_PTR_TO_MEM, |
| 5259 | + .arg3_type = ARG_CONST_SIZE, |
| 5260 | + .arg4_type = ARG_ANYTHING, |
| 5261 | + .arg5_type = ARG_ANYTHING, |
| 5262 | +}; |
| 5263 | + |
| 5220 | 5264 | BPF_CALL_5(bpf_sk_lookup_tcp, struct sk_buff *, skb, |
| 5221 | 5265 | struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) |
| 5222 | 5266 | { |
| 5223 | | − return bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP, netns_id, flags); |
| 5267 | + return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP, |
| 5268 | + netns_id, flags); |
| 5224 | 5269 | } |
| 5225 | 5270 | |
| 5226 | 5271 | static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = { |
| @@ -5238,7 +5283,8 @@ static const struct bpf_func_proto bpf_sk_lookup_tcp_proto = { · 11 unmodified lines |
| 5238 | 5283 | BPF_CALL_5(bpf_sk_lookup_udp, struct sk_buff *, skb, |
| 5239 | 5284 | struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) |
| 5240 | 5285 | { |
| 5241 | | − return bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP, netns_id, flags); |
| 5286 | + return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP, |
| 5287 | + netns_id, flags); |
| 5242 | 5288 | } |
| 5243 | 5289 | |
| 5244 | 5290 | static const struct bpf_func_proto bpf_sk_lookup_udp_proto = { |
| @@ -5273,8 +5319,9 @@ BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx, · 28 unmodified lines |
| 5273 | 5319 | struct net *caller_net = dev_net(ctx->rxq->dev); |
| 5274 | 5320 | int ifindex = ctx->rxq->dev->ifindex; |
| 5275 | 5321 | |
| 5276 | | − return __bpf_sk_lookup(NULL, tuple, len, caller_net, ifindex, |
| 5277 | | − IPPROTO_UDP, netns_id, flags); |
| 5322 | + return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net, |
| 5323 | + ifindex, IPPROTO_UDP, netns_id, |
| 5324 | + flags); |
| 5278 | 5325 | } |
| 5279 | 5326 | |
| 5280 | 5327 | static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = { |
| @@ -5289,14 +5336,38 @@ static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = { · 8 unmodified lines |
| 5289 | 5336 | .arg5_type = ARG_ANYTHING, |
| 5290 | 5337 | }; |
| 5291 | 5338 | |
| 5339 | +BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx, |
| 5340 | + struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) |
| 5341 | +{ |
| 5342 | + struct net *caller_net = dev_net(ctx->rxq->dev); |
| 5343 | + int ifindex = ctx->rxq->dev->ifindex; |
| 5344 | + |
| 5345 | + return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net, |
| 5346 | + ifindex, IPPROTO_TCP, netns_id, |
| 5347 | + flags); |
| 5348 | +} |
| 5349 | + |
| 5350 | +static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = { |
| 5351 | + .func = bpf_xdp_skc_lookup_tcp, |
| 5352 | + .gpl_only = false, |
| 5353 | + .pkt_access = true, |
| 5354 | + .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, |
| 5355 | + .arg1_type = ARG_PTR_TO_CTX, |
| 5356 | + .arg2_type = ARG_PTR_TO_MEM, |
| 5357 | + .arg3_type = ARG_CONST_SIZE, |
| 5358 | + .arg4_type = ARG_ANYTHING, |
| 5359 | + .arg5_type = ARG_ANYTHING, |
| 5360 | +}; |
| 5361 | + |
| 5292 | 5362 | BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx, |
| 5293 | 5363 | struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) |
| 5294 | 5364 | { |
| 5295 | 5365 | struct net *caller_net = dev_net(ctx->rxq->dev); |
| 5296 | 5366 | int ifindex = ctx->rxq->dev->ifindex; |
| 5297 | 5367 | |
| 5298 | | − return __bpf_sk_lookup(NULL, tuple, len, caller_net, ifindex, |
| 5299 | | − IPPROTO_TCP, netns_id, flags); |
| 5368 | + return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net, |
| 5369 | + ifindex, IPPROTO_TCP, netns_id, |
| 5370 | + flags); |
| 5300 | 5371 | } |
| 5301 | 5372 | |
| 5302 | 5373 | static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = { |
| @@ -5311,11 +5382,31 @@ static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = { · 8 unmodified lines |
| 5311 | 5382 | .arg5_type = ARG_ANYTHING, |
| 5312 | 5383 | }; |
| 5313 | 5384 | |
| 5385 | +BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx, |
| 5386 | + struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) |
| 5387 | +{ |
| 5388 | + return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, |
| 5389 | + sock_net(ctx->sk), 0, |
| 5390 | + IPPROTO_TCP, netns_id, flags); |
| 5391 | +} |
| 5392 | + |
| 5393 | +static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = { |
| 5394 | + .func = bpf_sock_addr_skc_lookup_tcp, |
| 5395 | + .gpl_only = false, |
| 5396 | + .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, |
| 5397 | + .arg1_type = ARG_PTR_TO_CTX, |
| 5398 | + .arg2_type = ARG_PTR_TO_MEM, |
| 5399 | + .arg3_type = ARG_CONST_SIZE, |
| 5400 | + .arg4_type = ARG_ANYTHING, |
| 5401 | + .arg5_type = ARG_ANYTHING, |
| 5402 | +}; |
| 5403 | + |
| 5314 | 5404 | BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx, |
| 5315 | 5405 | struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) |
| 5316 | 5406 | { |
| 5317 | | − return __bpf_sk_lookup(NULL, tuple, len, sock_net(ctx->sk), 0, |
| 5318 | | − IPPROTO_TCP, netns_id, flags); |
| 5407 | + return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, |
| 5408 | + sock_net(ctx->sk), 0, IPPROTO_TCP, |
| 5409 | + netns_id, flags); |
| 5319 | 5410 | } |
| 5320 | 5411 | |
| 5321 | 5412 | static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = { |
| @@ -5332,8 +5423,9 @@ static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = { · 10 unmodified lines |
| 5332 | 5423 | BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx, |
| 5333 | 5424 | struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) |
| 5334 | 5425 | { |
| 5335 | | − return __bpf_sk_lookup(NULL, tuple, len, sock_net(ctx->sk), 0, |
| 5336 | | − IPPROTO_UDP, netns_id, flags); |
| 5426 | + return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, |
| 5427 | + sock_net(ctx->sk), 0, IPPROTO_UDP, |
| 5428 | + netns_id, flags); |
| 5337 | 5429 | } |
| 5338 | 5430 | |
| 5339 | 5431 | static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = { |
| @@ -5586,6 +5678,8 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 246 unmodified lines |
| 5586 | 5678 | return &bpf_sock_addr_sk_lookup_udp_proto; |
| 5587 | 5679 | case BPF_FUNC_sk_release: |
| 5588 | 5680 | return &bpf_sk_release_proto; |
| 5681 | + case BPF_FUNC_skc_lookup_tcp: |
| 5682 | + return &bpf_sock_addr_skc_lookup_tcp_proto; |
| 5589 | 5683 | #endif /* CONFIG_INET */ |
| 5590 | 5684 | default: |
| 5591 | 5685 | return bpf_base_func_proto(func_id); |
| @@ -5719,6 +5813,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 127 unmodified lines |
| 5719 | 5813 | return &bpf_tcp_sock_proto; |
| 5720 | 5814 | case BPF_FUNC_get_listener_sock: |
| 5721 | 5815 | return &bpf_get_listener_sock_proto; |
| 5816 | + case BPF_FUNC_skc_lookup_tcp: |
| 5817 | + return &bpf_skc_lookup_tcp_proto; |
| 5722 | 5818 | #endif |
| 5723 | 5819 | default: |
| 5724 | 5820 | return bpf_base_func_proto(func_id); |
| @@ -5754,6 +5850,8 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 29 unmodified lines |
| 5754 | 5850 | return &bpf_xdp_sk_lookup_tcp_proto; |
| 5755 | 5851 | case BPF_FUNC_sk_release: |
| 5756 | 5852 | return &bpf_sk_release_proto; |
| 5853 | + case BPF_FUNC_skc_lookup_tcp: |
| 5854 | + return &bpf_xdp_skc_lookup_tcp_proto; |
| 5757 | 5855 | #endif |
| 5758 | 5856 | default: |
| 5759 | 5857 | return bpf_base_func_proto(func_id); |
| @@ -5846,6 +5944,8 @@ sk_skb_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 86 unmodified lines |
| 5846 | 5944 | return &bpf_sk_lookup_udp_proto; |
| 5847 | 5945 | case BPF_FUNC_sk_release: |
| 5848 | 5946 | return &bpf_sk_release_proto; |
| 5947 | + case BPF_FUNC_skc_lookup_tcp: |
| 5948 | + return &bpf_skc_lookup_tcp_proto; |
| 5849 | 5949 | #endif |
| 5850 | 5950 | default: |
| 5851 | 5951 | return bpf_base_func_proto(func_id); |