xt_physdev.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Kernel module to match the bridge port in and
  2. * out device for IP packets coming into contact with a bridge. */
  3. /* (C) 2001-2003 Bart De Schuymer <bdschuym@pandora.be>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/netfilter_bridge.h>
  12. #include <linux/netfilter/xt_physdev.h>
  13. #include <linux/netfilter/x_tables.h>
  14. #include <linux/netfilter_bridge.h>
  15. #define MATCH 1
  16. #define NOMATCH 0
  17. MODULE_LICENSE("GPL");
  18. MODULE_AUTHOR("Bart De Schuymer <bdschuym@pandora.be>");
  19. MODULE_DESCRIPTION("iptables bridge physical device match module");
  20. MODULE_ALIAS("ipt_physdev");
  21. MODULE_ALIAS("ip6t_physdev");
  22. static int
  23. match(const struct sk_buff *skb,
  24. const struct net_device *in,
  25. const struct net_device *out,
  26. const struct xt_match *match,
  27. const void *matchinfo,
  28. int offset,
  29. unsigned int protoff,
  30. int *hotdrop)
  31. {
  32. int i;
  33. static const char nulldevname[IFNAMSIZ];
  34. const struct xt_physdev_info *info = matchinfo;
  35. unsigned int ret;
  36. const char *indev, *outdev;
  37. struct nf_bridge_info *nf_bridge;
  38. /* Not a bridged IP packet or no info available yet:
  39. * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
  40. * the destination device will be a bridge. */
  41. if (!(nf_bridge = skb->nf_bridge)) {
  42. /* Return MATCH if the invert flags of the used options are on */
  43. if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
  44. !(info->invert & XT_PHYSDEV_OP_BRIDGED))
  45. return NOMATCH;
  46. if ((info->bitmask & XT_PHYSDEV_OP_ISIN) &&
  47. !(info->invert & XT_PHYSDEV_OP_ISIN))
  48. return NOMATCH;
  49. if ((info->bitmask & XT_PHYSDEV_OP_ISOUT) &&
  50. !(info->invert & XT_PHYSDEV_OP_ISOUT))
  51. return NOMATCH;
  52. if ((info->bitmask & XT_PHYSDEV_OP_IN) &&
  53. !(info->invert & XT_PHYSDEV_OP_IN))
  54. return NOMATCH;
  55. if ((info->bitmask & XT_PHYSDEV_OP_OUT) &&
  56. !(info->invert & XT_PHYSDEV_OP_OUT))
  57. return NOMATCH;
  58. return MATCH;
  59. }
  60. /* This only makes sense in the FORWARD and POSTROUTING chains */
  61. if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
  62. (!!(nf_bridge->mask & BRNF_BRIDGED) ^
  63. !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
  64. return NOMATCH;
  65. if ((info->bitmask & XT_PHYSDEV_OP_ISIN &&
  66. (!nf_bridge->physindev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) ||
  67. (info->bitmask & XT_PHYSDEV_OP_ISOUT &&
  68. (!nf_bridge->physoutdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT))))
  69. return NOMATCH;
  70. if (!(info->bitmask & XT_PHYSDEV_OP_IN))
  71. goto match_outdev;
  72. indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname;
  73. for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned int); i++) {
  74. ret |= (((const unsigned int *)indev)[i]
  75. ^ ((const unsigned int *)info->physindev)[i])
  76. & ((const unsigned int *)info->in_mask)[i];
  77. }
  78. if ((ret == 0) ^ !(info->invert & XT_PHYSDEV_OP_IN))
  79. return NOMATCH;
  80. match_outdev:
  81. if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
  82. return MATCH;
  83. outdev = nf_bridge->physoutdev ?
  84. nf_bridge->physoutdev->name : nulldevname;
  85. for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned int); i++) {
  86. ret |= (((const unsigned int *)outdev)[i]
  87. ^ ((const unsigned int *)info->physoutdev)[i])
  88. & ((const unsigned int *)info->out_mask)[i];
  89. }
  90. return (ret != 0) ^ !(info->invert & XT_PHYSDEV_OP_OUT);
  91. }
  92. static int
  93. checkentry(const char *tablename,
  94. const void *ip,
  95. const struct xt_match *match,
  96. void *matchinfo,
  97. unsigned int hook_mask)
  98. {
  99. const struct xt_physdev_info *info = matchinfo;
  100. if (!(info->bitmask & XT_PHYSDEV_OP_MASK) ||
  101. info->bitmask & ~XT_PHYSDEV_OP_MASK)
  102. return 0;
  103. if (info->bitmask & XT_PHYSDEV_OP_OUT &&
  104. (!(info->bitmask & XT_PHYSDEV_OP_BRIDGED) ||
  105. info->invert & XT_PHYSDEV_OP_BRIDGED) &&
  106. hook_mask & ((1 << NF_IP_LOCAL_OUT) | (1 << NF_IP_FORWARD) |
  107. (1 << NF_IP_POST_ROUTING))) {
  108. printk(KERN_WARNING "physdev match: using --physdev-out in the "
  109. "OUTPUT, FORWARD and POSTROUTING chains for non-bridged "
  110. "traffic is not supported anymore.\n");
  111. if (hook_mask & (1 << NF_IP_LOCAL_OUT))
  112. return 0;
  113. }
  114. return 1;
  115. }
  116. static struct xt_match xt_physdev_match[] = {
  117. {
  118. .name = "physdev",
  119. .family = AF_INET,
  120. .checkentry = checkentry,
  121. .match = match,
  122. .matchsize = sizeof(struct xt_physdev_info),
  123. .me = THIS_MODULE,
  124. },
  125. {
  126. .name = "physdev",
  127. .family = AF_INET6,
  128. .checkentry = checkentry,
  129. .match = match,
  130. .matchsize = sizeof(struct xt_physdev_info),
  131. .me = THIS_MODULE,
  132. },
  133. };
  134. static int __init xt_physdev_init(void)
  135. {
  136. return xt_register_matches(xt_physdev_match,
  137. ARRAY_SIZE(xt_physdev_match));
  138. }
  139. static void __exit xt_physdev_fini(void)
  140. {
  141. xt_unregister_matches(xt_physdev_match, ARRAY_SIZE(xt_physdev_match));
  142. }
  143. module_init(xt_physdev_init);
  144. module_exit(xt_physdev_fini);