prog_cgroup_sockopt.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ============================
  3. BPF_PROG_TYPE_CGROUP_SOCKOPT
  4. ============================
  5. ``BPF_PROG_TYPE_CGROUP_SOCKOPT`` program type can be attached to two
  6. cgroup hooks:
  7. * ``BPF_CGROUP_GETSOCKOPT`` - called every time process executes ``getsockopt``
  8. system call.
  9. * ``BPF_CGROUP_SETSOCKOPT`` - called every time process executes ``setsockopt``
  10. system call.
  11. The context (``struct bpf_sockopt``) has associated socket (``sk``) and
  12. all input arguments: ``level``, ``optname``, ``optval`` and ``optlen``.
  13. BPF_CGROUP_SETSOCKOPT
  14. =====================
  15. ``BPF_CGROUP_SETSOCKOPT`` is triggered *before* the kernel handling of
  16. sockopt and it has writable context: it can modify the supplied arguments
  17. before passing them down to the kernel. This hook has access to the cgroup
  18. and socket local storage.
  19. If BPF program sets ``optlen`` to -1, the control will be returned
  20. back to the userspace after all other BPF programs in the cgroup
  21. chain finish (i.e. kernel ``setsockopt`` handling will *not* be executed).
  22. Note, that ``optlen`` can not be increased beyond the user-supplied
  23. value. It can only be decreased or set to -1. Any other value will
  24. trigger ``EFAULT``.
  25. Return Type
  26. -----------
  27. * ``0`` - reject the syscall, ``EPERM`` will be returned to the userspace.
  28. * ``1`` - success, continue with next BPF program in the cgroup chain.
  29. BPF_CGROUP_GETSOCKOPT
  30. =====================
  31. ``BPF_CGROUP_GETSOCKOPT`` is triggered *after* the kernel handing of
  32. sockopt. The BPF hook can observe ``optval``, ``optlen`` and ``retval``
  33. if it's interested in whatever kernel has returned. BPF hook can override
  34. the values above, adjust ``optlen`` and reset ``retval`` to 0. If ``optlen``
  35. has been increased above initial ``getsockopt`` value (i.e. userspace
  36. buffer is too small), ``EFAULT`` is returned.
  37. This hook has access to the cgroup and socket local storage.
  38. Note, that the only acceptable value to set to ``retval`` is 0 and the
  39. original value that the kernel returned. Any other value will trigger
  40. ``EFAULT``.
  41. Return Type
  42. -----------
  43. * ``0`` - reject the syscall, ``EPERM`` will be returned to the userspace.
  44. * ``1`` - success: copy ``optval`` and ``optlen`` to userspace, return
  45. ``retval`` from the syscall (note that this can be overwritten by
  46. the BPF program from the parent cgroup).
  47. Cgroup Inheritance
  48. ==================
  49. Suppose, there is the following cgroup hierarchy where each cgroup
  50. has ``BPF_CGROUP_GETSOCKOPT`` attached at each level with
  51. ``BPF_F_ALLOW_MULTI`` flag::
  52. A (root, parent)
  53. \
  54. B (child)
  55. When the application calls ``getsockopt`` syscall from the cgroup B,
  56. the programs are executed from the bottom up: B, A. First program
  57. (B) sees the result of kernel's ``getsockopt``. It can optionally
  58. adjust ``optval``, ``optlen`` and reset ``retval`` to 0. After that
  59. control will be passed to the second (A) program which will see the
  60. same context as B including any potential modifications.
  61. Same for ``BPF_CGROUP_SETSOCKOPT``: if the program is attached to
  62. A and B, the trigger order is B, then A. If B does any changes
  63. to the input arguments (``level``, ``optname``, ``optval``, ``optlen``),
  64. then the next program in the chain (A) will see those changes,
  65. *not* the original input ``setsockopt`` arguments. The potentially
  66. modified values will be then passed down to the kernel.
  67. Large optval
  68. ============
  69. When the ``optval`` is greater than the ``PAGE_SIZE``, the BPF program
  70. can access only the first ``PAGE_SIZE`` of that data. So it has to options:
  71. * Set ``optlen`` to zero, which indicates that the kernel should
  72. use the original buffer from the userspace. Any modifications
  73. done by the BPF program to the ``optval`` are ignored.
  74. * Set ``optlen`` to the value less than ``PAGE_SIZE``, which
  75. indicates that the kernel should use BPF's trimmed ``optval``.
  76. When the BPF program returns with the ``optlen`` greater than
  77. ``PAGE_SIZE``, the userspace will receive ``EFAULT`` errno.
  78. Example
  79. =======
  80. See ``tools/testing/selftests/bpf/progs/sockopt_sk.c`` for an example
  81. of BPF program that handles socket options.