ebt_ip6.c 4.5 KB

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