xt_AUDIT.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Creates audit record for dropped/accepted packets
  3. *
  4. * (C) 2010-2011 Thomas Graf <tgraf@redhat.com>
  5. * (C) 2010-2011 Red Hat, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/audit.h>
  13. #include <linux/module.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/tcp.h>
  16. #include <linux/udp.h>
  17. #include <linux/if_arp.h>
  18. #include <linux/netfilter/x_tables.h>
  19. #include <linux/netfilter/xt_AUDIT.h>
  20. #include <linux/netfilter_bridge/ebtables.h>
  21. #include <net/ipv6.h>
  22. #include <net/ip.h>
  23. MODULE_LICENSE("GPL");
  24. MODULE_AUTHOR("Thomas Graf <tgraf@redhat.com>");
  25. MODULE_DESCRIPTION("Xtables: creates audit records for dropped/accepted packets");
  26. MODULE_ALIAS("ipt_AUDIT");
  27. MODULE_ALIAS("ip6t_AUDIT");
  28. MODULE_ALIAS("ebt_AUDIT");
  29. MODULE_ALIAS("arpt_AUDIT");
  30. static void audit_proto(struct audit_buffer *ab, struct sk_buff *skb,
  31. unsigned int proto, unsigned int offset)
  32. {
  33. switch (proto) {
  34. case IPPROTO_TCP:
  35. case IPPROTO_UDP:
  36. case IPPROTO_UDPLITE: {
  37. const __be16 *pptr;
  38. __be16 _ports[2];
  39. pptr = skb_header_pointer(skb, offset, sizeof(_ports), _ports);
  40. if (pptr == NULL) {
  41. audit_log_format(ab, " truncated=1");
  42. return;
  43. }
  44. audit_log_format(ab, " sport=%hu dport=%hu",
  45. ntohs(pptr[0]), ntohs(pptr[1]));
  46. }
  47. break;
  48. case IPPROTO_ICMP:
  49. case IPPROTO_ICMPV6: {
  50. const u8 *iptr;
  51. u8 _ih[2];
  52. iptr = skb_header_pointer(skb, offset, sizeof(_ih), &_ih);
  53. if (iptr == NULL) {
  54. audit_log_format(ab, " truncated=1");
  55. return;
  56. }
  57. audit_log_format(ab, " icmptype=%hhu icmpcode=%hhu",
  58. iptr[0], iptr[1]);
  59. }
  60. break;
  61. }
  62. }
  63. static void audit_ip4(struct audit_buffer *ab, struct sk_buff *skb)
  64. {
  65. struct iphdr _iph;
  66. const struct iphdr *ih;
  67. ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
  68. if (!ih) {
  69. audit_log_format(ab, " truncated=1");
  70. return;
  71. }
  72. audit_log_format(ab, " saddr=%pI4 daddr=%pI4 ipid=%hu proto=%hhu",
  73. &ih->saddr, &ih->daddr, ntohs(ih->id), ih->protocol);
  74. if (ntohs(ih->frag_off) & IP_OFFSET) {
  75. audit_log_format(ab, " frag=1");
  76. return;
  77. }
  78. audit_proto(ab, skb, ih->protocol, ih->ihl * 4);
  79. }
  80. static void audit_ip6(struct audit_buffer *ab, struct sk_buff *skb)
  81. {
  82. struct ipv6hdr _ip6h;
  83. const struct ipv6hdr *ih;
  84. u8 nexthdr;
  85. int offset;
  86. ih = skb_header_pointer(skb, skb_network_offset(skb), sizeof(_ip6h), &_ip6h);
  87. if (!ih) {
  88. audit_log_format(ab, " truncated=1");
  89. return;
  90. }
  91. nexthdr = ih->nexthdr;
  92. offset = ipv6_skip_exthdr(skb, skb_network_offset(skb) + sizeof(_ip6h),
  93. &nexthdr);
  94. audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu",
  95. &ih->saddr, &ih->daddr, nexthdr);
  96. if (offset)
  97. audit_proto(ab, skb, nexthdr, offset);
  98. }
  99. static unsigned int
  100. audit_tg(struct sk_buff *skb, const struct xt_action_param *par)
  101. {
  102. const struct xt_audit_info *info = par->targinfo;
  103. struct audit_buffer *ab;
  104. ab = audit_log_start(NULL, GFP_ATOMIC, AUDIT_NETFILTER_PKT);
  105. if (ab == NULL)
  106. goto errout;
  107. audit_log_format(ab, "action=%hhu hook=%u len=%u inif=%s outif=%s",
  108. info->type, par->hooknum, skb->len,
  109. par->in ? par->in->name : "?",
  110. par->out ? par->out->name : "?");
  111. if (skb->mark)
  112. audit_log_format(ab, " mark=%#x", skb->mark);
  113. if (skb->dev && skb->dev->type == ARPHRD_ETHER) {
  114. audit_log_format(ab, " smac=%pM dmac=%pM macproto=0x%04x",
  115. eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  116. ntohs(eth_hdr(skb)->h_proto));
  117. if (par->family == NFPROTO_BRIDGE) {
  118. switch (eth_hdr(skb)->h_proto) {
  119. case __constant_htons(ETH_P_IP):
  120. audit_ip4(ab, skb);
  121. break;
  122. case __constant_htons(ETH_P_IPV6):
  123. audit_ip6(ab, skb);
  124. break;
  125. }
  126. }
  127. }
  128. switch (par->family) {
  129. case NFPROTO_IPV4:
  130. audit_ip4(ab, skb);
  131. break;
  132. case NFPROTO_IPV6:
  133. audit_ip6(ab, skb);
  134. break;
  135. }
  136. audit_log_end(ab);
  137. errout:
  138. return XT_CONTINUE;
  139. }
  140. static unsigned int
  141. audit_tg_ebt(struct sk_buff *skb, const struct xt_action_param *par)
  142. {
  143. audit_tg(skb, par);
  144. return EBT_CONTINUE;
  145. }
  146. static int audit_tg_check(const struct xt_tgchk_param *par)
  147. {
  148. const struct xt_audit_info *info = par->targinfo;
  149. if (info->type > XT_AUDIT_TYPE_MAX) {
  150. pr_info("Audit type out of range (valid range: 0..%hhu)\n",
  151. XT_AUDIT_TYPE_MAX);
  152. return -ERANGE;
  153. }
  154. return 0;
  155. }
  156. static struct xt_target audit_tg_reg[] __read_mostly = {
  157. {
  158. .name = "AUDIT",
  159. .family = NFPROTO_UNSPEC,
  160. .target = audit_tg,
  161. .targetsize = sizeof(struct xt_audit_info),
  162. .checkentry = audit_tg_check,
  163. .me = THIS_MODULE,
  164. },
  165. {
  166. .name = "AUDIT",
  167. .family = NFPROTO_BRIDGE,
  168. .target = audit_tg_ebt,
  169. .targetsize = sizeof(struct xt_audit_info),
  170. .checkentry = audit_tg_check,
  171. .me = THIS_MODULE,
  172. },
  173. };
  174. static int __init audit_tg_init(void)
  175. {
  176. return xt_register_targets(audit_tg_reg, ARRAY_SIZE(audit_tg_reg));
  177. }
  178. static void __exit audit_tg_exit(void)
  179. {
  180. xt_unregister_targets(audit_tg_reg, ARRAY_SIZE(audit_tg_reg));
  181. }
  182. module_init(audit_tg_init);
  183. module_exit(audit_tg_exit);