/net/netfilter/xt_AUDIT.c

http://github.com/mirrors/linux · C · 158 lines · 124 code · 27 blank · 7 comment · 11 complexity · fae172f714815c450e9cccfe6f2263a1 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Creates audit record for dropped/accepted packets
  4. *
  5. * (C) 2010-2011 Thomas Graf <tgraf@redhat.com>
  6. * (C) 2010-2011 Red Hat, Inc.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/audit.h>
  10. #include <linux/module.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/tcp.h>
  13. #include <linux/udp.h>
  14. #include <linux/if_arp.h>
  15. #include <linux/netfilter/x_tables.h>
  16. #include <linux/netfilter/xt_AUDIT.h>
  17. #include <linux/netfilter_bridge/ebtables.h>
  18. #include <net/ipv6.h>
  19. #include <net/ip.h>
  20. MODULE_LICENSE("GPL");
  21. MODULE_AUTHOR("Thomas Graf <tgraf@redhat.com>");
  22. MODULE_DESCRIPTION("Xtables: creates audit records for dropped/accepted packets");
  23. MODULE_ALIAS("ipt_AUDIT");
  24. MODULE_ALIAS("ip6t_AUDIT");
  25. MODULE_ALIAS("ebt_AUDIT");
  26. MODULE_ALIAS("arpt_AUDIT");
  27. static bool audit_ip4(struct audit_buffer *ab, struct sk_buff *skb)
  28. {
  29. struct iphdr _iph;
  30. const struct iphdr *ih;
  31. ih = skb_header_pointer(skb, skb_network_offset(skb), sizeof(_iph), &_iph);
  32. if (!ih)
  33. return false;
  34. audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu",
  35. &ih->saddr, &ih->daddr, ih->protocol);
  36. return true;
  37. }
  38. static bool audit_ip6(struct audit_buffer *ab, struct sk_buff *skb)
  39. {
  40. struct ipv6hdr _ip6h;
  41. const struct ipv6hdr *ih;
  42. u8 nexthdr;
  43. __be16 frag_off;
  44. ih = skb_header_pointer(skb, skb_network_offset(skb), sizeof(_ip6h), &_ip6h);
  45. if (!ih)
  46. return false;
  47. nexthdr = ih->nexthdr;
  48. ipv6_skip_exthdr(skb, skb_network_offset(skb) + sizeof(_ip6h), &nexthdr, &frag_off);
  49. audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu",
  50. &ih->saddr, &ih->daddr, nexthdr);
  51. return true;
  52. }
  53. static unsigned int
  54. audit_tg(struct sk_buff *skb, const struct xt_action_param *par)
  55. {
  56. struct audit_buffer *ab;
  57. int fam = -1;
  58. if (audit_enabled == AUDIT_OFF)
  59. goto errout;
  60. ab = audit_log_start(NULL, GFP_ATOMIC, AUDIT_NETFILTER_PKT);
  61. if (ab == NULL)
  62. goto errout;
  63. audit_log_format(ab, "mark=%#x", skb->mark);
  64. switch (xt_family(par)) {
  65. case NFPROTO_BRIDGE:
  66. switch (eth_hdr(skb)->h_proto) {
  67. case htons(ETH_P_IP):
  68. fam = audit_ip4(ab, skb) ? NFPROTO_IPV4 : -1;
  69. break;
  70. case htons(ETH_P_IPV6):
  71. fam = audit_ip6(ab, skb) ? NFPROTO_IPV6 : -1;
  72. break;
  73. }
  74. break;
  75. case NFPROTO_IPV4:
  76. fam = audit_ip4(ab, skb) ? NFPROTO_IPV4 : -1;
  77. break;
  78. case NFPROTO_IPV6:
  79. fam = audit_ip6(ab, skb) ? NFPROTO_IPV6 : -1;
  80. break;
  81. }
  82. if (fam == -1)
  83. audit_log_format(ab, " saddr=? daddr=? proto=-1");
  84. audit_log_end(ab);
  85. errout:
  86. return XT_CONTINUE;
  87. }
  88. static unsigned int
  89. audit_tg_ebt(struct sk_buff *skb, const struct xt_action_param *par)
  90. {
  91. audit_tg(skb, par);
  92. return EBT_CONTINUE;
  93. }
  94. static int audit_tg_check(const struct xt_tgchk_param *par)
  95. {
  96. const struct xt_audit_info *info = par->targinfo;
  97. if (info->type > XT_AUDIT_TYPE_MAX) {
  98. pr_info_ratelimited("Audit type out of range (valid range: 0..%hhu)\n",
  99. XT_AUDIT_TYPE_MAX);
  100. return -ERANGE;
  101. }
  102. return 0;
  103. }
  104. static struct xt_target audit_tg_reg[] __read_mostly = {
  105. {
  106. .name = "AUDIT",
  107. .family = NFPROTO_UNSPEC,
  108. .target = audit_tg,
  109. .targetsize = sizeof(struct xt_audit_info),
  110. .checkentry = audit_tg_check,
  111. .me = THIS_MODULE,
  112. },
  113. {
  114. .name = "AUDIT",
  115. .family = NFPROTO_BRIDGE,
  116. .target = audit_tg_ebt,
  117. .targetsize = sizeof(struct xt_audit_info),
  118. .checkentry = audit_tg_check,
  119. .me = THIS_MODULE,
  120. },
  121. };
  122. static int __init audit_tg_init(void)
  123. {
  124. return xt_register_targets(audit_tg_reg, ARRAY_SIZE(audit_tg_reg));
  125. }
  126. static void __exit audit_tg_exit(void)
  127. {
  128. xt_unregister_targets(audit_tg_reg, ARRAY_SIZE(audit_tg_reg));
  129. }
  130. module_init(audit_tg_init);
  131. module_exit(audit_tg_exit);