ebt_ip6.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * ebt_ip6
  3. *
  4. * Authors:
  5. * Manohar Castelino <manohar.r.castelino@intel.com>
  6. * Kuo-Lang Tseng <kuo-lang.tseng@intel.com>
  7. * Jan Engelhardt <jengelh@medozas.de>
  8. *
  9. * Summary:
  10. * This is just a modification of the IPv4 code written by
  11. * Bart De Schuymer <bdschuym@pandora.be>
  12. * with the changes required to support IPv6
  13. *
  14. * Jan, 2008
  15. */
  16. #include <linux/ipv6.h>
  17. #include <net/ipv6.h>
  18. #include <linux/in.h>
  19. #include <linux/module.h>
  20. #include <net/dsfield.h>
  21. #include <linux/netfilter/x_tables.h>
  22. #include <linux/netfilter_bridge/ebtables.h>
  23. #include <linux/netfilter_bridge/ebt_ip6.h>
  24. union pkthdr {
  25. struct {
  26. __be16 src;
  27. __be16 dst;
  28. } tcpudphdr;
  29. struct {
  30. u8 type;
  31. u8 code;
  32. } icmphdr;
  33. };
  34. static bool
  35. ebt_ip6_mt(const struct sk_buff *skb, struct xt_action_param *par)
  36. {
  37. const struct ebt_ip6_info *info = par->matchinfo;
  38. const struct ipv6hdr *ih6;
  39. struct ipv6hdr _ip6h;
  40. const union pkthdr *pptr;
  41. union pkthdr _pkthdr;
  42. ih6 = skb_header_pointer(skb, 0, sizeof(_ip6h), &_ip6h);
  43. if (ih6 == NULL)
  44. return false;
  45. if (info->bitmask & EBT_IP6_TCLASS &&
  46. FWINV(info->tclass != ipv6_get_dsfield(ih6), EBT_IP6_TCLASS))
  47. return false;
  48. if ((info->bitmask & EBT_IP6_SOURCE &&
  49. FWINV(ipv6_masked_addr_cmp(&ih6->saddr, &info->smsk,
  50. &info->saddr), EBT_IP6_SOURCE)) ||
  51. (info->bitmask & EBT_IP6_DEST &&
  52. FWINV(ipv6_masked_addr_cmp(&ih6->daddr, &info->dmsk,
  53. &info->daddr), EBT_IP6_DEST)))
  54. return false;
  55. if (info->bitmask & EBT_IP6_PROTO) {
  56. uint8_t nexthdr = ih6->nexthdr;
  57. __be16 frag_off;
  58. int offset_ph;
  59. offset_ph = ipv6_skip_exthdr(skb, sizeof(_ip6h), &nexthdr, &frag_off);
  60. if (offset_ph == -1)
  61. return false;
  62. if (FWINV(info->protocol != nexthdr, EBT_IP6_PROTO))
  63. return false;
  64. if (!(info->bitmask & ( EBT_IP6_DPORT |
  65. EBT_IP6_SPORT | EBT_IP6_ICMP6)))
  66. return true;
  67. /* min icmpv6 headersize is 4, so sizeof(_pkthdr) is ok. */
  68. pptr = skb_header_pointer(skb, offset_ph, sizeof(_pkthdr),
  69. &_pkthdr);
  70. if (pptr == NULL)
  71. return false;
  72. if (info->bitmask & EBT_IP6_DPORT) {
  73. u16 dst = ntohs(pptr->tcpudphdr.dst);
  74. if (FWINV(dst < info->dport[0] ||
  75. dst > info->dport[1], EBT_IP6_DPORT))
  76. return false;
  77. }
  78. if (info->bitmask & EBT_IP6_SPORT) {
  79. u16 src = ntohs(pptr->tcpudphdr.src);
  80. if (FWINV(src < info->sport[0] ||
  81. src > info->sport[1], EBT_IP6_SPORT))
  82. return false;
  83. }
  84. if ((info->bitmask & EBT_IP6_ICMP6) &&
  85. FWINV(pptr->icmphdr.type < info->icmpv6_type[0] ||
  86. pptr->icmphdr.type > info->icmpv6_type[1] ||
  87. pptr->icmphdr.code < info->icmpv6_code[0] ||
  88. pptr->icmphdr.code > info->icmpv6_code[1],
  89. EBT_IP6_ICMP6))
  90. return false;
  91. }
  92. return true;
  93. }
  94. static int ebt_ip6_mt_check(const struct xt_mtchk_param *par)
  95. {
  96. const struct ebt_entry *e = par->entryinfo;
  97. struct ebt_ip6_info *info = par->matchinfo;
  98. if (e->ethproto != htons(ETH_P_IPV6) || e->invflags & EBT_IPROTO)
  99. return -EINVAL;
  100. if (info->bitmask & ~EBT_IP6_MASK || info->invflags & ~EBT_IP6_MASK)
  101. return -EINVAL;
  102. if (info->bitmask & (EBT_IP6_DPORT | EBT_IP6_SPORT)) {
  103. if (info->invflags & EBT_IP6_PROTO)
  104. return -EINVAL;
  105. if (info->protocol != IPPROTO_TCP &&
  106. info->protocol != IPPROTO_UDP &&
  107. info->protocol != IPPROTO_UDPLITE &&
  108. info->protocol != IPPROTO_SCTP &&
  109. info->protocol != IPPROTO_DCCP)
  110. return -EINVAL;
  111. }
  112. if (info->bitmask & EBT_IP6_DPORT && info->dport[0] > info->dport[1])
  113. return -EINVAL;
  114. if (info->bitmask & EBT_IP6_SPORT && info->sport[0] > info->sport[1])
  115. return -EINVAL;
  116. if (info->bitmask & EBT_IP6_ICMP6) {
  117. if ((info->invflags & EBT_IP6_PROTO) ||
  118. info->protocol != IPPROTO_ICMPV6)
  119. return -EINVAL;
  120. if (info->icmpv6_type[0] > info->icmpv6_type[1] ||
  121. info->icmpv6_code[0] > info->icmpv6_code[1])
  122. return -EINVAL;
  123. }
  124. return 0;
  125. }
  126. static struct xt_match ebt_ip6_mt_reg __read_mostly = {
  127. .name = "ip6",
  128. .revision = 0,
  129. .family = NFPROTO_BRIDGE,
  130. .match = ebt_ip6_mt,
  131. .checkentry = ebt_ip6_mt_check,
  132. .matchsize = sizeof(struct ebt_ip6_info),
  133. .me = THIS_MODULE,
  134. };
  135. static int __init ebt_ip6_init(void)
  136. {
  137. return xt_register_match(&ebt_ip6_mt_reg);
  138. }
  139. static void __exit ebt_ip6_fini(void)
  140. {
  141. xt_unregister_match(&ebt_ip6_mt_reg);
  142. }
  143. module_init(ebt_ip6_init);
  144. module_exit(ebt_ip6_fini);
  145. MODULE_DESCRIPTION("Ebtables: IPv6 protocol packet match");
  146. MODULE_AUTHOR("Kuo-Lang Tseng <kuo-lang.tseng@intel.com>");
  147. MODULE_LICENSE("GPL");