ebt_among.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * ebt_among
  3. *
  4. * Authors:
  5. * Grzegorz Borowiak <grzes@gnu.univ.gda.pl>
  6. *
  7. * August, 2003
  8. *
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/ip.h>
  12. #include <linux/if_arp.h>
  13. #include <linux/module.h>
  14. #include <linux/netfilter/x_tables.h>
  15. #include <linux/netfilter_bridge/ebtables.h>
  16. #include <linux/netfilter_bridge/ebt_among.h>
  17. static bool ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh,
  18. const char *mac, __be32 ip)
  19. {
  20. /* You may be puzzled as to how this code works.
  21. * Some tricks were used, refer to
  22. * include/linux/netfilter_bridge/ebt_among.h
  23. * as there you can find a solution of this mystery.
  24. */
  25. const struct ebt_mac_wormhash_tuple *p;
  26. int start, limit, i;
  27. uint32_t cmp[2] = { 0, 0 };
  28. int key = ((const unsigned char *)mac)[5];
  29. ether_addr_copy(((char *) cmp) + 2, mac);
  30. start = wh->table[key];
  31. limit = wh->table[key + 1];
  32. if (ip) {
  33. for (i = start; i < limit; i++) {
  34. p = &wh->pool[i];
  35. if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0])
  36. if (p->ip == 0 || p->ip == ip)
  37. return true;
  38. }
  39. } else {
  40. for (i = start; i < limit; i++) {
  41. p = &wh->pool[i];
  42. if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0])
  43. if (p->ip == 0)
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash
  50. *wh)
  51. {
  52. int i;
  53. for (i = 0; i < 256; i++) {
  54. if (wh->table[i] > wh->table[i + 1])
  55. return -0x100 - i;
  56. if (wh->table[i] < 0)
  57. return -0x200 - i;
  58. if (wh->table[i] > wh->poolsize)
  59. return -0x300 - i;
  60. }
  61. if (wh->table[256] > wh->poolsize)
  62. return -0xc00;
  63. return 0;
  64. }
  65. static int get_ip_dst(const struct sk_buff *skb, __be32 *addr)
  66. {
  67. if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
  68. const struct iphdr *ih;
  69. struct iphdr _iph;
  70. ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
  71. if (ih == NULL)
  72. return -1;
  73. *addr = ih->daddr;
  74. } else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
  75. const struct arphdr *ah;
  76. struct arphdr _arph;
  77. const __be32 *bp;
  78. __be32 buf;
  79. ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
  80. if (ah == NULL ||
  81. ah->ar_pln != sizeof(__be32) ||
  82. ah->ar_hln != ETH_ALEN)
  83. return -1;
  84. bp = skb_header_pointer(skb, sizeof(struct arphdr) +
  85. 2 * ETH_ALEN + sizeof(__be32),
  86. sizeof(__be32), &buf);
  87. if (bp == NULL)
  88. return -1;
  89. *addr = *bp;
  90. }
  91. return 0;
  92. }
  93. static int get_ip_src(const struct sk_buff *skb, __be32 *addr)
  94. {
  95. if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
  96. const struct iphdr *ih;
  97. struct iphdr _iph;
  98. ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
  99. if (ih == NULL)
  100. return -1;
  101. *addr = ih->saddr;
  102. } else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
  103. const struct arphdr *ah;
  104. struct arphdr _arph;
  105. const __be32 *bp;
  106. __be32 buf;
  107. ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
  108. if (ah == NULL ||
  109. ah->ar_pln != sizeof(__be32) ||
  110. ah->ar_hln != ETH_ALEN)
  111. return -1;
  112. bp = skb_header_pointer(skb, sizeof(struct arphdr) +
  113. ETH_ALEN, sizeof(__be32), &buf);
  114. if (bp == NULL)
  115. return -1;
  116. *addr = *bp;
  117. }
  118. return 0;
  119. }
  120. static bool
  121. ebt_among_mt(const struct sk_buff *skb, struct xt_action_param *par)
  122. {
  123. const struct ebt_among_info *info = par->matchinfo;
  124. const char *dmac, *smac;
  125. const struct ebt_mac_wormhash *wh_dst, *wh_src;
  126. __be32 dip = 0, sip = 0;
  127. wh_dst = ebt_among_wh_dst(info);
  128. wh_src = ebt_among_wh_src(info);
  129. if (wh_src) {
  130. smac = eth_hdr(skb)->h_source;
  131. if (get_ip_src(skb, &sip))
  132. return false;
  133. if (!(info->bitmask & EBT_AMONG_SRC_NEG)) {
  134. /* we match only if it contains */
  135. if (!ebt_mac_wormhash_contains(wh_src, smac, sip))
  136. return false;
  137. } else {
  138. /* we match only if it DOES NOT contain */
  139. if (ebt_mac_wormhash_contains(wh_src, smac, sip))
  140. return false;
  141. }
  142. }
  143. if (wh_dst) {
  144. dmac = eth_hdr(skb)->h_dest;
  145. if (get_ip_dst(skb, &dip))
  146. return false;
  147. if (!(info->bitmask & EBT_AMONG_DST_NEG)) {
  148. /* we match only if it contains */
  149. if (!ebt_mac_wormhash_contains(wh_dst, dmac, dip))
  150. return false;
  151. } else {
  152. /* we match only if it DOES NOT contain */
  153. if (ebt_mac_wormhash_contains(wh_dst, dmac, dip))
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. static bool poolsize_invalid(const struct ebt_mac_wormhash *w)
  160. {
  161. return w && w->poolsize >= (INT_MAX / sizeof(struct ebt_mac_wormhash_tuple));
  162. }
  163. static bool wormhash_offset_invalid(int off, unsigned int len)
  164. {
  165. if (off == 0) /* not present */
  166. return false;
  167. if (off < (int)sizeof(struct ebt_among_info) ||
  168. off % __alignof__(struct ebt_mac_wormhash))
  169. return true;
  170. off += sizeof(struct ebt_mac_wormhash);
  171. return off > len;
  172. }
  173. static bool wormhash_sizes_valid(const struct ebt_mac_wormhash *wh, int a, int b)
  174. {
  175. if (a == 0)
  176. a = sizeof(struct ebt_among_info);
  177. return ebt_mac_wormhash_size(wh) + a == b;
  178. }
  179. static int ebt_among_mt_check(const struct xt_mtchk_param *par)
  180. {
  181. const struct ebt_among_info *info = par->matchinfo;
  182. const struct ebt_entry_match *em =
  183. container_of(par->matchinfo, const struct ebt_entry_match, data);
  184. unsigned int expected_length = sizeof(struct ebt_among_info);
  185. const struct ebt_mac_wormhash *wh_dst, *wh_src;
  186. int err;
  187. if (expected_length > em->match_size)
  188. return -EINVAL;
  189. if (wormhash_offset_invalid(info->wh_dst_ofs, em->match_size) ||
  190. wormhash_offset_invalid(info->wh_src_ofs, em->match_size))
  191. return -EINVAL;
  192. wh_dst = ebt_among_wh_dst(info);
  193. if (poolsize_invalid(wh_dst))
  194. return -EINVAL;
  195. expected_length += ebt_mac_wormhash_size(wh_dst);
  196. if (expected_length > em->match_size)
  197. return -EINVAL;
  198. wh_src = ebt_among_wh_src(info);
  199. if (poolsize_invalid(wh_src))
  200. return -EINVAL;
  201. if (info->wh_src_ofs < info->wh_dst_ofs) {
  202. if (!wormhash_sizes_valid(wh_src, info->wh_src_ofs, info->wh_dst_ofs))
  203. return -EINVAL;
  204. } else {
  205. if (!wormhash_sizes_valid(wh_dst, info->wh_dst_ofs, info->wh_src_ofs))
  206. return -EINVAL;
  207. }
  208. expected_length += ebt_mac_wormhash_size(wh_src);
  209. if (em->match_size != EBT_ALIGN(expected_length)) {
  210. pr_info("wrong size: %d against expected %d, rounded to %Zd\n",
  211. em->match_size, expected_length,
  212. EBT_ALIGN(expected_length));
  213. return -EINVAL;
  214. }
  215. if (wh_dst && (err = ebt_mac_wormhash_check_integrity(wh_dst))) {
  216. pr_info("dst integrity fail: %x\n", -err);
  217. return -EINVAL;
  218. }
  219. if (wh_src && (err = ebt_mac_wormhash_check_integrity(wh_src))) {
  220. pr_info("src integrity fail: %x\n", -err);
  221. return -EINVAL;
  222. }
  223. return 0;
  224. }
  225. static struct xt_match ebt_among_mt_reg __read_mostly = {
  226. .name = "among",
  227. .revision = 0,
  228. .family = NFPROTO_BRIDGE,
  229. .match = ebt_among_mt,
  230. .checkentry = ebt_among_mt_check,
  231. .matchsize = -1, /* special case */
  232. .me = THIS_MODULE,
  233. };
  234. static int __init ebt_among_init(void)
  235. {
  236. return xt_register_match(&ebt_among_mt_reg);
  237. }
  238. static void __exit ebt_among_fini(void)
  239. {
  240. xt_unregister_match(&ebt_among_mt_reg);
  241. }
  242. module_init(ebt_among_init);
  243. module_exit(ebt_among_fini);
  244. MODULE_DESCRIPTION("Ebtables: Combined MAC/IP address list matching");
  245. MODULE_LICENSE("GPL");