filter.txt 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. filter.txt: Linux Socket Filtering
  2. Written by: Jay Schulist <jschlst@samba.org>
  3. Introduction
  4. ============
  5. Linux Socket Filtering is derived from the Berkeley
  6. Packet Filter. There are some distinct differences between
  7. the BSD and Linux Kernel Filtering.
  8. Linux Socket Filtering (LSF) allows a user-space program to
  9. attach a filter onto any socket and allow or disallow certain
  10. types of data to come through the socket. LSF follows exactly
  11. the same filter code structure as the BSD Berkeley Packet Filter
  12. (BPF), so referring to the BSD bpf.4 manpage is very helpful in
  13. creating filters.
  14. LSF is much simpler than BPF. One does not have to worry about
  15. devices or anything like that. You simply create your filter
  16. code, send it to the kernel via the SO_ATTACH_FILTER ioctl and
  17. if your filter code passes the kernel check on it, you then
  18. immediately begin filtering data on that socket.
  19. You can also detach filters from your socket via the
  20. SO_DETACH_FILTER ioctl. This will probably not be used much
  21. since when you close a socket that has a filter on it the
  22. filter is automagically removed. The other less common case
  23. may be adding a different filter on the same socket where you had another
  24. filter that is still running: the kernel takes care of removing
  25. the old one and placing your new one in its place, assuming your
  26. filter has passed the checks, otherwise if it fails the old filter
  27. will remain on that socket.
  28. Examples
  29. ========
  30. Ioctls-
  31. setsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &Filter, sizeof(Filter));
  32. setsockopt(sockfd, SOL_SOCKET, SO_DETACH_FILTER, &value, sizeof(value));
  33. See the BSD bpf.4 manpage and the BSD Packet Filter paper written by
  34. Steven McCanne and Van Jacobson of Lawrence Berkeley Laboratory.