net: introduce SO_BPF_EXTENSIONS
Subsystems (13)
PATCH 1/1 net: introduce SO_BPF_EXTENSIONS
userspace packet capturing libraries (e.g. libpcap) don't have a way how to find out which BPF extensions are supported by the kernel, except compiling a filter which uses extensions and trying to apply filter to the socket. This is very inconvenient.
Therefore this commit introduces new option which can be used as an argument for getsockopt() and allows one to obtain information about which BPF extensions are supported by the kernel.
--- arch/alpha/include/uapi/asm/socket.h | 2 ++ arch/avr32/include/uapi/asm/socket.h | 2 ++ arch/cris/include/uapi/asm/socket.h | 2 ++ arch/frv/include/uapi/asm/socket.h | 2 ++ arch/ia64/include/uapi/asm/socket.h | 2 ++ arch/m32r/include/uapi/asm/socket.h | 2 ++ arch/mips/include/uapi/asm/socket.h | 2 ++ arch/mn10300/include/uapi/asm/socket.h | 2 ++ arch/parisc/include/uapi/asm/socket.h | 2 ++ arch/powerpc/include/uapi/asm/socket.h | 2 ++ arch/s390/include/uapi/asm/socket.h | 2 ++ arch/sparc/include/uapi/asm/socket.h | 2 ++ arch/xtensa/include/uapi/asm/socket.h | 2 ++ include/linux/filter.h | 18 ++++++++++++++++++ include/net/sock.h | 19 +++++++++++++++++++ include/uapi/asm-generic/socket.h | 2 ++ net/core/sock.c | 4 ++++ 17 files changed, 69 insertions(+)
arch/alpha/include/uapi/asm/socket.h +2 -0
@@ -85,4 +85,6 @@#define SO_MAX_PACING_RATE 47 +#define SO_BPF_EXTENSIONS 48 +#endif /* _UAPI_ASM_SOCKET_H */
arch/avr32/include/uapi/asm/socket.h +2 -0
@@ -78,4 +78,6 @@#define SO_MAX_PACING_RATE 47 +#define SO_BPF_EXTENSIONS 48 +#endif /* __ASM_AVR32_SOCKET_H */
arch/cris/include/uapi/asm/socket.h +2 -0
@@ -80,6 +80,8 @@#define SO_MAX_PACING_RATE 47 +#define SO_BPF_EXTENSIONS 48 +#endif /* _ASM_SOCKET_H */
arch/frv/include/uapi/asm/socket.h +2 -0
@@ -78,5 +78,7 @@#define SO_MAX_PACING_RATE 47 +#define SO_BPF_EXTENSIONS 48 +#endif /* _ASM_SOCKET_H */
arch/ia64/include/uapi/asm/socket.h +2 -0
@@ -87,4 +87,6 @@#define SO_MAX_PACING_RATE 47 +#define SO_BPF_EXTENSIONS 48 +#endif /* _ASM_IA64_SOCKET_H */
arch/m32r/include/uapi/asm/socket.h +2 -0
@@ -78,4 +78,6 @@#define SO_MAX_PACING_RATE 47 +#define SO_BPF_EXTENSIONS 48 +#endif /* _ASM_M32R_SOCKET_H */
arch/mips/include/uapi/asm/socket.h +2 -0
@@ -96,4 +96,6 @@#define SO_MAX_PACING_RATE 47 +#define SO_BPF_EXTENSIONS 48 +#endif /* _UAPI_ASM_SOCKET_H */
arch/mn10300/include/uapi/asm/socket.h +2 -0
@@ -78,4 +78,6 @@#define SO_MAX_PACING_RATE 47 +#define SO_BPF_EXTENSIONS 48 +#endif /* _ASM_SOCKET_H */
arch/parisc/include/uapi/asm/socket.h +2 -0
@@ -77,6 +77,8 @@#define SO_MAX_PACING_RATE 0x4048 +#define SO_BPF_EXTENSIONS 0x4029 +/* O_NONBLOCK clashes with the bits used for socket types. Therefore we * have to define SOCK_NONBLOCK to a different value here. */
arch/powerpc/include/uapi/asm/socket.h +2 -0
@@ -85,4 +85,6 @@#define SO_MAX_PACING_RATE 47 +#define SO_BPF_EXTENSIONS 48 +#endif /* _ASM_POWERPC_SOCKET_H */
arch/s390/include/uapi/asm/socket.h +2 -0
@@ -84,4 +84,6 @@#define SO_MAX_PACING_RATE 47 +#define SO_BPF_EXTENSIONS 48 +#endif /* _ASM_SOCKET_H */
arch/sparc/include/uapi/asm/socket.h +2 -0
@@ -74,6 +74,8 @@#define SO_MAX_PACING_RATE 0x0031 +#define SO_BPF_EXTENSIONS 0x0032 +/* Security levels - as per NRL IPv6 - don't actually do anything */ #define SO_SECURITY_AUTHENTICATION 0x5001#define SO_SECURITY_ENCRYPTION_TRANSPORT 0x5002
arch/xtensa/include/uapi/asm/socket.h +2 -0
@@ -89,4 +89,6 @@#define SO_MAX_PACING_RATE 47 +#define SO_BPF_EXTENSIONS 48 +#endif /* _XTENSA_SOCKET_H */
include/linux/filter.h +18 -0
@@ -151,4 +151,22 @@ enum { BPF_S_ANC_PAY_OFFSET, }; +enum { +BPF_ANC_FLAG_PROTOCOL = (1 <<0 ), +BPF_ANC_FLAG_PKTTYPE = (1 <<1 ), +BPF_ANC_FLAG_IFINDEX = (1 <<2 ), +BPF_ANC_FLAG_NLATTR = (1 <<3 ), +BPF_ANC_FLAG_NLATTR_NEST = (1 <<4 ), +BPF_ANC_FLAG_MARK = (1 <<5 ), +BPF_ANC_FLAG_QUEUE = (1 <<6 ), +BPF_ANC_FLAG_HATYPE = (1 <<7 ), +BPF_ANC_FLAG_RXHASH = (1 <<8 ), +BPF_ANC_FLAG_CPU = (1 <<9 ), +BPF_ANC_FLAG_ALU_XOR_X = (1 <<10 ), +BPF_ANC_FLAG_SECCOMP_LD_W = (1 <<11 ), +BPF_ANC_FLAG_VLAN_TAG = (1 <<12 ), +BPF_ANC_FLAG_VLAN_TAG_PRESENT = (1 <<13 ), +BPF_ANC_FLAG_PAY_OFFSET = (1 <<14 ), +}; +#endif /* __LINUX_FILTER_H__ */
include/net/sock.h +19 -0
@@ -2282,4 +2282,23 @@ extern int sysctl_optmem_max;extern __u32 sysctl_wmem_default;extern __u32 sysctl_rmem_default; +static inline int bpf_get_extensions (void ) +{ +return BPF_ANC_FLAG_PROTOCOL | +BPF_ANC_FLAG_PKTTYPE | +BPF_ANC_FLAG_IFINDEX | +BPF_ANC_FLAG_NLATTR | +BPF_ANC_FLAG_NLATTR_NEST | +BPF_ANC_FLAG_MARK | +BPF_ANC_FLAG_QUEUE | +BPF_ANC_FLAG_HATYPE | +BPF_ANC_FLAG_RXHASH | +BPF_ANC_FLAG_CPU | +BPF_ANC_FLAG_ALU_XOR_X | +BPF_ANC_FLAG_SECCOMP_LD_W | +BPF_ANC_FLAG_VLAN_TAG | +BPF_ANC_FLAG_VLAN_TAG_PRESENT | +BPF_ANC_FLAG_PAY_OFFSET ; +} +#endif /* _SOCK_H */
include/uapi/asm-generic/socket.h +2 -0
@@ -80,4 +80,6 @@#define SO_MAX_PACING_RATE 47 +#define SO_BPF_EXTENSIONS 48 +#endif /* __ASM_GENERIC_SOCKET_H */
net/core/sock.c +4 -0
@@ -1168,6 +1168,10 @@ int sock_getsockopt(struct socket *sock, int level, int optname, v.val =sock_flag (sk,SOCK_FILTER_LOCKED );break ; +case SO_BPF_EXTENSIONS : + v.val =bpf_get_extensions (); +break ; +case SO_SELECT_ERR_QUEUE : v.val =sock_flag (sk,SOCK_SELECT_ERR_QUEUE );break ;
Signature · 1 line
1.8.3.1
Quoted 8 lines · Michal Sekletar
include/linux/filter.h +8
-0+enum { +BPF_ANC_FLAG_PROTOCOL = (1 <<0 ), +BPF_ANC_FLAG_PKTTYPE = (1 <<1 ), +BPF_ANC_FLAG_IFINDEX = (1 <<2 ), +BPF_ANC_FLAG_NLATTR = (1 <<3 ), +BPF_ANC_FLAG_NLATTR_NEST = (1 <<4 ), +BPF_ANC_FLAG_MARK = (1 <<5 ), +BPF_ANC_FLAG_QUEUE = (1 <<6 ),
include/linux/filter.h enum { · line 171
Quoted 10 lines
include/linux/filter.h +10
-0+BPF_ANC_FLAG_HATYPE = (1 <<7 ), +BPF_ANC_FLAG_RXHASH = (1 <<8 ), +BPF_ANC_FLAG_CPU = (1 <<9 ), +BPF_ANC_FLAG_ALU_XOR_X = (1 <<10 ), +BPF_ANC_FLAG_SECCOMP_LD_W = (1 <<11 ), +BPF_ANC_FLAG_VLAN_TAG = (1 <<12 ), +BPF_ANC_FLAG_VLAN_TAG_PRESENT = (1 <<13 ), + BPF_ANC_FLAG_PAY_OFFSET = (1 << 14), +}; +
Why spending 15 bits (out of 32), for all these extensions ?
It seems a single one should be enough.
I do not think we will ever remove one of these extension.
Quoted 23 lines · Eric Dumazet
Quoted 8 lines · Michal Sekletar
include/linux/filter.h +8
-0+enum { +BPF_ANC_FLAG_PROTOCOL = (1 <<0 ), +BPF_ANC_FLAG_PKTTYPE = (1 <<1 ), +BPF_ANC_FLAG_IFINDEX = (1 <<2 ), +BPF_ANC_FLAG_NLATTR = (1 <<3 ), +BPF_ANC_FLAG_NLATTR_NEST = (1 <<4 ), +BPF_ANC_FLAG_MARK = (1 <<5 ), +BPF_ANC_FLAG_QUEUE = (1 <<6 ),include/linux/filter.h enum { · line 171
Quoted 10 lines
include/linux/filter.h +10
-0+BPF_ANC_FLAG_HATYPE = (1 <<7 ), +BPF_ANC_FLAG_RXHASH = (1 <<8 ), +BPF_ANC_FLAG_CPU = (1 <<9 ), +BPF_ANC_FLAG_ALU_XOR_X = (1 <<10 ), +BPF_ANC_FLAG_SECCOMP_LD_W = (1 <<11 ), +BPF_ANC_FLAG_VLAN_TAG = (1 <<12 ), +BPF_ANC_FLAG_VLAN_TAG_PRESENT = (1 <<13 ), + BPF_ANC_FLAG_PAY_OFFSET = (1 << 14), +}; +Why spending 15 bits (out of 32), for all these extensions ?
Quoted 3 lines
It seems a single one should be enough.
I do not think we will ever remove one of these extension.
Agreed, this will just cripple of us adding other extensions in terms of uapi. I assume there won't be so much more extensions, but it's of course hard to predict the future. ;-)
Quoted 26 lines · Daniel Borkmann
Quoted 25 lines · Eric Dumazet
Quoted 8 lines · Michal Sekletar
include/linux/filter.h +8
-0+enum { +BPF_ANC_FLAG_PROTOCOL = (1 <<0 ), +BPF_ANC_FLAG_PKTTYPE = (1 <<1 ), +BPF_ANC_FLAG_IFINDEX = (1 <<2 ), +BPF_ANC_FLAG_NLATTR = (1 <<3 ), +BPF_ANC_FLAG_NLATTR_NEST = (1 <<4 ), +BPF_ANC_FLAG_MARK = (1 <<5 ), +BPF_ANC_FLAG_QUEUE = (1 <<6 ),include/linux/filter.h enum { · line 171
Quoted 10 lines
include/linux/filter.h +10
-0+BPF_ANC_FLAG_HATYPE = (1 <<7 ), +BPF_ANC_FLAG_RXHASH = (1 <<8 ), +BPF_ANC_FLAG_CPU = (1 <<9 ), +BPF_ANC_FLAG_ALU_XOR_X = (1 <<10 ), +BPF_ANC_FLAG_SECCOMP_LD_W = (1 <<11 ), +BPF_ANC_FLAG_VLAN_TAG = (1 <<12 ), +BPF_ANC_FLAG_VLAN_TAG_PRESENT = (1 <<13 ), + BPF_ANC_FLAG_PAY_OFFSET = (1 << 14), +}; +Why spending 15 bits (out of 32), for all these extensions ?
Quoted 9 lines
Quoted 3 lines
It seems a single one should be enough.
I do not think we will ever remove one of these extension.
Agreed, this will just cripple of us adding other extensions in terms of uapi. I assume there won't be so much more extensions, but it's of course hard to predict the future. ;-)
Not sure I follow. Can you please elaborate on the explanation. Please bare with me this is my first kernel patch submission.
Thanks,
Michal
Quoted 4 lines
--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 32 lines · Michal Sekletár
Quoted 31 lines · Daniel Borkmann
Quoted 28 lines · Eric Dumazet
Quoted 8 lines · Michal Sekletar
include/linux/filter.h +8
-0+enum { +BPF_ANC_FLAG_PROTOCOL = (1 <<0 ), +BPF_ANC_FLAG_PKTTYPE = (1 <<1 ), +BPF_ANC_FLAG_IFINDEX = (1 <<2 ), +BPF_ANC_FLAG_NLATTR = (1 <<3 ), +BPF_ANC_FLAG_NLATTR_NEST = (1 <<4 ), +BPF_ANC_FLAG_MARK = (1 <<5 ), +BPF_ANC_FLAG_QUEUE = (1 <<6 ),include/linux/filter.h enum { · line 171
Quoted 10 lines
include/linux/filter.h +10
-0+BPF_ANC_FLAG_HATYPE = (1 <<7 ), +BPF_ANC_FLAG_RXHASH = (1 <<8 ), +BPF_ANC_FLAG_CPU = (1 <<9 ), +BPF_ANC_FLAG_ALU_XOR_X = (1 <<10 ), +BPF_ANC_FLAG_SECCOMP_LD_W = (1 <<11 ), +BPF_ANC_FLAG_VLAN_TAG = (1 <<12 ), +BPF_ANC_FLAG_VLAN_TAG_PRESENT = (1 <<13 ), + BPF_ANC_FLAG_PAY_OFFSET = (1 << 14), +}; +Why spending 15 bits (out of 32), for all these extensions ?
It seems a single one should be enough.
I do not think we will ever remove one of these extension.
Quoted 7 lines
Quoted 4 lines
Agreed, this will just cripple of us adding other extensions in terms of uapi. I assume there won't be so much more extensions, but it's of course hard to predict the future. ;-)
Not sure I follow. Can you please elaborate on the explanation. Please bare with me this is my first kernel patch submission.
if SO_BPF_EXTENSIONS is supported, than you can assume that all current extensions are supported.
No need to consume one bit per feature, as all these features wont ever disappear from linux.
Quoted 5 lines
if
SO_BPF_EXTENSIONSis supported, than you can assume that all current extensions are supported.No need to consume one bit per feature, as all these features wont ever disappear from linux.
However one of the BSDs could add a subset of the features and wish to advertise the fact.
So using extra flags for non-trivial extensions could be useful.
David
Quoted 9 lines · David Laight
Quoted 5 lines
if
SO_BPF_EXTENSIONSis supported, than you can assume that all current extensions are supported.No need to consume one bit per feature, as all these features wont ever disappear from linux.
However one of the BSDs could add a subset of the features and wish to advertise the fact.
So using extra flags for non-trivial extensions could be useful.
Haven't had a closer look at the BSD BPF code /yet/, so ...
i) Does BSD have such extensions and if so do we overlap some?
ii) Is it planned to also introduce SO_BPF_EXTENSIONS for BSD kernels
to have one common api (that i.e. libpcap would then make use of)?
Quoted 2 lines
David
Quoted 5 lines
From: Daniel Borkmann
Quoted 3 lines · David Laight
Quoted 3 lines
if
SO_BPF_EXTENSIONSis supported, than you can assume that all current extensions are supported.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
No need to consume one bit per feature, as all these features wont ever disappear from linux.
However one of the BSDs could add a subset of the features and wish to advertise the fact.
So using extra flags for non-trivial extensions could be useful.Haven't had a closer look at the BSD BPF code /yet/, so ...
I've not looked either.
Quoted 4 lines
i) Does BSD have such extensions and if so do we overlap some?
ii) Is it planned to also introduce
SO_BPF_EXTENSIONSfor BSD kernels
to have one common api (that i.e. libpcap would then make use of)?
If it is useful to add any of the extensions, and they don't collide with any other existing changes, then they might be added. Adding a request that indicates which extensions are supported should be easier than adding the extensions themselves.
Adding all the Linux extensions at once could be problematic.
David
Quoted 8 lines · David Laight
Quoted 3 lines
From: Daniel Borkmann
Haven't had a closer look at the BSD BPF code /yet/, so ...
I've not looked either.
Quoted 2 lines
i) Does BSD have such extensions and if so do we overlap some?
Quoted 7 lines
Quoted 2 lines
ii) Is it planned to also introduce
SO_BPF_EXTENSIONSfor BSD kernels
to have one common api (that i.e. libpcap would then make use of)?If it is useful to add any of the extensions, and they don't collide with any other existing changes, then they might be added. Adding a request that indicates which extensions are supported should be easier than adding the extensions themselves.
Btw, we have aa1113d9f85da5 for that, but I see the point to get all at once.
Ideally, they shouldn't even be added manually to this getsockopt, plus we need to be careful of not preventing us in future from adding additional extensions if they are needed for some good reason (hence, the 1 bit, but maybe there's another better solution; haven't had time to think about it so far).
David Miller
maintainerFrom: "David Laight" <[email protected]>
Date: Fri, 6 Dec 2013 09:50:49 -0000
Quoted 9 lines
Quoted 5 lines
if
SO_BPF_EXTENSIONSis supported, than you can assume that all current extensions are supported.No need to consume one bit per feature, as all these features wont ever disappear from linux.
However one of the BSDs could add a subset of the features and wish to advertise the fact.
So using extra flags for non-trivial extensions could be useful.
Sorry, we're not going to accomodate this.
David Miller
maintainerFrom: "David Laight" <[email protected]>
Date: Fri, 6 Dec 2013 11:15:58 -0000
Quoted 10 lines
Quoted 4 lines
i) Does BSD have such extensions and if so do we overlap some?
ii) Is it planned to also introduce
SO_BPF_EXTENSIONSfor BSD kernels
to have one common api (that i.e. libpcap would then make use of)?If it is useful to add any of the extensions, and they don't collide with any other existing changes, then they might be added. Adding a request that indicates which extensions are supported should be easier than adding the extensions themselves.
Adding all the Linux extensions at once could be problematic.
Again, we're not going to accomodate this, sorry.
Not sure I follow. Not going to accommodate, meaning at all or you just my original approach? If later applies, I'll be happy to rewrite the patch.
Quoted 15 lines · David Miller
From: "David Laight" <[email protected]>
Date: Fri, 6 Dec 2013 11:15:58 -0000Quoted 10 lines
Quoted 4 lines
i) Does BSD have such extensions and if so do we overlap some?
ii) Is it planned to also introduce
SO_BPF_EXTENSIONSfor BSD kernels
to have one common api (that i.e. libpcap would then make use of)?If it is useful to add any of the extensions, and they don't collide with any other existing changes, then they might be added. Adding a request that indicates which extensions are supported should be easier than adding the extensions themselves.
Adding all the Linux extensions at once could be problematic.Again, we're not going to accomodate this, sorry.
David Miller
maintainerAnd please do not top post in the future, thank you.