xt_AUDIT.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. __be16 frag_off;
  86. int offset;
  87. ih = skb_header_pointer(skb, skb_network_offset(skb), sizeof(_ip6h), &_ip6h);
  88. if (!ih) {
  89. audit_log_format(ab, " truncated=1");
  90. return;
  91. }
  92. nexthdr = ih->nexthdr;
  93. offset = ipv6_skip_exthdr(skb, skb_network_offset(skb) + sizeof(_ip6h),
  94. &nexthdr, &frag_off);
  95. audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu",
  96. &ih->saddr, &ih->daddr, nexthdr);
  97. if (offset)
  98. audit_proto(ab, skb, nexthdr, offset);
  99. }
  100. static unsigned int
  101. audit_tg(struct sk_buff *skb, const struct xt_action_param *par)
  102. {
  103. const struct xt_audit_info *info = par->targinfo;
  104. struct audit_buffer *ab;
  105. if (audit_enabled == 0)
  106. goto errout;
  107. ab = audit_log_start(NULL, GFP_ATOMIC, AUDIT_NETFILTER_PKT);
  108. if (ab == NULL)
  109. goto errout;
  110. audit_log_format(ab, "action=%hhu hook=%u len=%u inif=%s outif=%s",
  111. info->type, par->hooknum, skb->len,
  112. par->in ? par->in->name : "?",
  113. par->out ? par->out->name : "?");
  114. if (skb->mark)
  115. audit_log_format(ab, " mark=%#x", skb->mark);
  116. if (skb->dev && skb->dev->type == ARPHRD_ETHER) {
  117. audit_log_format(ab, " smac=%pM dmac=%pM macproto=0x%04x",
  118. eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  119. ntohs(eth_hdr(skb)->h_proto));
  120. if (par->family == NFPROTO_BRIDGE) {
  121. switch (eth_hdr(skb)->h_proto) {
  122. case htons(ETH_P_IP):
  123. audit_ip4(ab, skb);
  124. break;
  125. case htons(ETH_P_IPV6):
  126. audit_ip6(ab, skb);
  127. break;
  128. }
  129. }
  130. }
  131. switch (par->family) {
  132. case NFPROTO_IPV4:
  133. audit_ip4(ab, skb);
  134. break;
  135. case NFPROTO_IPV6:
  136. audit_ip6(ab, skb);
  137. break;
  138. }
  139. #ifdef CONFIG_NETWORK_SECMARK
  140. if (skb->secmark)
  141. audit_log_secctx(ab, skb->secmark);
  142. #endif
  143. audit_log_end(ab);
  144. errout:
  145. return XT_CONTINUE;
  146. }
  147. static unsigned int
  148. audit_tg_ebt(struct sk_buff *skb, const struct xt_action_param *par)
  149. {
  150. audit_tg(skb, par);
  151. return EBT_CONTINUE;
  152. }
  153. static int audit_tg_check(const struct xt_tgchk_param *par)
  154. {
  155. const struct xt_audit_info *info = par->targinfo;
  156. if (info->type > XT_AUDIT_TYPE_MAX) {
  157. pr_info("Audit type out of range (valid range: 0..%hhu)\n",
  158. XT_AUDIT_TYPE_MAX);
  159. return -ERANGE;
  160. }
  161. return 0;
  162. }
  163. static struct xt_target audit_tg_reg[] __read_mostly = {
  164. {
  165. .name = "AUDIT",
  166. .family = NFPROTO_UNSPEC,
  167. .target = audit_tg,
  168. .targetsize = sizeof(struct xt_audit_info),
  169. .checkentry = audit_tg_check,
  170. .me = THIS_MODULE,
  171. },
  172. {
  173. .name = "AUDIT",
  174. .family = NFPROTO_BRIDGE,
  175. .target = audit_tg_ebt,
  176. .targetsize = sizeof(struct xt_audit_info),
  177. .checkentry = audit_tg_check,
  178. .me = THIS_MODULE,
  179. },
  180. };
  181. static int __init audit_tg_init(void)
  182. {
  183. return xt_register_targets(audit_tg_reg, ARRAY_SIZE(audit_tg_reg));
  184. }
  185. static void __exit audit_tg_exit(void)
  186. {
  187. xt_unregister_targets(audit_tg_reg, ARRAY_SIZE(audit_tg_reg));
  188. }
  189. module_init(audit_tg_init);
  190. module_exit(audit_tg_exit);