ebt_log.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * ebt_log
  3. *
  4. * Authors:
  5. * Bart De Schuymer <bdschuym@pandora.be>
  6. * Harald Welte <laforge@netfilter.org>
  7. *
  8. * April, 2002
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/ip.h>
  13. #include <linux/in.h>
  14. #include <linux/if_arp.h>
  15. #include <linux/spinlock.h>
  16. #include <net/netfilter/nf_log.h>
  17. #include <linux/ipv6.h>
  18. #include <net/ipv6.h>
  19. #include <linux/in6.h>
  20. #include <linux/netfilter/x_tables.h>
  21. #include <linux/netfilter_bridge/ebtables.h>
  22. #include <linux/netfilter_bridge/ebt_log.h>
  23. #include <linux/netfilter.h>
  24. static DEFINE_SPINLOCK(ebt_log_lock);
  25. static int ebt_log_tg_check(const struct xt_tgchk_param *par)
  26. {
  27. struct ebt_log_info *info = par->targinfo;
  28. if (info->bitmask & ~EBT_LOG_MASK)
  29. return -EINVAL;
  30. if (info->loglevel >= 8)
  31. return -EINVAL;
  32. info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
  33. return 0;
  34. }
  35. struct tcpudphdr
  36. {
  37. __be16 src;
  38. __be16 dst;
  39. };
  40. struct arppayload
  41. {
  42. unsigned char mac_src[ETH_ALEN];
  43. unsigned char ip_src[4];
  44. unsigned char mac_dst[ETH_ALEN];
  45. unsigned char ip_dst[4];
  46. };
  47. static void
  48. print_ports(const struct sk_buff *skb, uint8_t protocol, int offset)
  49. {
  50. if (protocol == IPPROTO_TCP ||
  51. protocol == IPPROTO_UDP ||
  52. protocol == IPPROTO_UDPLITE ||
  53. protocol == IPPROTO_SCTP ||
  54. protocol == IPPROTO_DCCP) {
  55. const struct tcpudphdr *pptr;
  56. struct tcpudphdr _ports;
  57. pptr = skb_header_pointer(skb, offset,
  58. sizeof(_ports), &_ports);
  59. if (pptr == NULL) {
  60. printk(" INCOMPLETE TCP/UDP header");
  61. return;
  62. }
  63. printk(" SPT=%u DPT=%u", ntohs(pptr->src), ntohs(pptr->dst));
  64. }
  65. }
  66. static void
  67. ebt_log_packet(struct net *net, u_int8_t pf, unsigned int hooknum,
  68. const struct sk_buff *skb, const struct net_device *in,
  69. const struct net_device *out, const struct nf_loginfo *loginfo,
  70. const char *prefix)
  71. {
  72. unsigned int bitmask;
  73. /* FIXME: Disabled from containers until syslog ns is supported */
  74. if (!net_eq(net, &init_net))
  75. return;
  76. spin_lock_bh(&ebt_log_lock);
  77. printk(KERN_SOH "%c%s IN=%s OUT=%s MAC source = %pM MAC dest = %pM proto = 0x%04x",
  78. '0' + loginfo->u.log.level, prefix,
  79. in ? in->name : "", out ? out->name : "",
  80. eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  81. ntohs(eth_hdr(skb)->h_proto));
  82. if (loginfo->type == NF_LOG_TYPE_LOG)
  83. bitmask = loginfo->u.log.logflags;
  84. else
  85. bitmask = NF_LOG_MASK;
  86. if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto ==
  87. htons(ETH_P_IP)) {
  88. const struct iphdr *ih;
  89. struct iphdr _iph;
  90. ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
  91. if (ih == NULL) {
  92. printk(" INCOMPLETE IP header");
  93. goto out;
  94. }
  95. printk(" IP SRC=%pI4 IP DST=%pI4, IP tos=0x%02X, IP proto=%d",
  96. &ih->saddr, &ih->daddr, ih->tos, ih->protocol);
  97. print_ports(skb, ih->protocol, ih->ihl*4);
  98. goto out;
  99. }
  100. #if IS_ENABLED(CONFIG_BRIDGE_EBT_IP6)
  101. if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
  102. htons(ETH_P_IPV6)) {
  103. const struct ipv6hdr *ih;
  104. struct ipv6hdr _iph;
  105. uint8_t nexthdr;
  106. __be16 frag_off;
  107. int offset_ph;
  108. ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
  109. if (ih == NULL) {
  110. printk(" INCOMPLETE IPv6 header");
  111. goto out;
  112. }
  113. printk(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d",
  114. &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr);
  115. nexthdr = ih->nexthdr;
  116. offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr, &frag_off);
  117. if (offset_ph == -1)
  118. goto out;
  119. print_ports(skb, nexthdr, offset_ph);
  120. goto out;
  121. }
  122. #endif
  123. if ((bitmask & EBT_LOG_ARP) &&
  124. ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
  125. (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
  126. const struct arphdr *ah;
  127. struct arphdr _arph;
  128. ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
  129. if (ah == NULL) {
  130. printk(" INCOMPLETE ARP header");
  131. goto out;
  132. }
  133. printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
  134. ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
  135. ntohs(ah->ar_op));
  136. /* If it's for Ethernet and the lengths are OK,
  137. * then log the ARP payload */
  138. if (ah->ar_hrd == htons(1) &&
  139. ah->ar_hln == ETH_ALEN &&
  140. ah->ar_pln == sizeof(__be32)) {
  141. const struct arppayload *ap;
  142. struct arppayload _arpp;
  143. ap = skb_header_pointer(skb, sizeof(_arph),
  144. sizeof(_arpp), &_arpp);
  145. if (ap == NULL) {
  146. printk(" INCOMPLETE ARP payload");
  147. goto out;
  148. }
  149. printk(" ARP MAC SRC=%pM ARP IP SRC=%pI4 ARP MAC DST=%pM ARP IP DST=%pI4",
  150. ap->mac_src, ap->ip_src, ap->mac_dst, ap->ip_dst);
  151. }
  152. }
  153. out:
  154. printk("\n");
  155. spin_unlock_bh(&ebt_log_lock);
  156. }
  157. static unsigned int
  158. ebt_log_tg(struct sk_buff *skb, const struct xt_action_param *par)
  159. {
  160. const struct ebt_log_info *info = par->targinfo;
  161. struct nf_loginfo li;
  162. struct net *net = par->net;
  163. li.type = NF_LOG_TYPE_LOG;
  164. li.u.log.level = info->loglevel;
  165. li.u.log.logflags = info->bitmask;
  166. /* Remember that we have to use ebt_log_packet() not to break backward
  167. * compatibility. We cannot use the default bridge packet logger via
  168. * nf_log_packet() with NFT_LOG_TYPE_LOG here. --Pablo
  169. */
  170. if (info->bitmask & EBT_LOG_NFLOG)
  171. nf_log_packet(net, NFPROTO_BRIDGE, par->hooknum, skb,
  172. par->in, par->out, &li, "%s", info->prefix);
  173. else
  174. ebt_log_packet(net, NFPROTO_BRIDGE, par->hooknum, skb, par->in,
  175. par->out, &li, info->prefix);
  176. return EBT_CONTINUE;
  177. }
  178. static struct xt_target ebt_log_tg_reg __read_mostly = {
  179. .name = "log",
  180. .revision = 0,
  181. .family = NFPROTO_BRIDGE,
  182. .target = ebt_log_tg,
  183. .checkentry = ebt_log_tg_check,
  184. .targetsize = sizeof(struct ebt_log_info),
  185. .me = THIS_MODULE,
  186. };
  187. static int __init ebt_log_init(void)
  188. {
  189. return xt_register_target(&ebt_log_tg_reg);
  190. }
  191. static void __exit ebt_log_fini(void)
  192. {
  193. xt_unregister_target(&ebt_log_tg_reg);
  194. }
  195. module_init(ebt_log_init);
  196. module_exit(ebt_log_fini);
  197. MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
  198. MODULE_LICENSE("GPL");