nf_conntrack_broadcast.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * broadcast connection tracking helper
  3. *
  4. * (c) 2005 Patrick McHardy <kaber@trash.net>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/ip.h>
  13. #include <net/route.h>
  14. #include <linux/inetdevice.h>
  15. #include <linux/skbuff.h>
  16. #include <net/netfilter/nf_conntrack.h>
  17. #include <net/netfilter/nf_conntrack_helper.h>
  18. #include <net/netfilter/nf_conntrack_expect.h>
  19. int nf_conntrack_broadcast_help(struct sk_buff *skb,
  20. unsigned int protoff,
  21. struct nf_conn *ct,
  22. enum ip_conntrack_info ctinfo,
  23. unsigned int timeout)
  24. {
  25. struct nf_conntrack_expect *exp;
  26. struct iphdr *iph = ip_hdr(skb);
  27. struct rtable *rt = skb_rtable(skb);
  28. struct in_device *in_dev;
  29. struct nf_conn_help *help = nfct_help(ct);
  30. __be32 mask = 0;
  31. /* we're only interested in locally generated packets */
  32. if (skb->sk == NULL)
  33. goto out;
  34. if (rt == NULL || !(rt->rt_flags & RTCF_BROADCAST))
  35. goto out;
  36. if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
  37. goto out;
  38. rcu_read_lock();
  39. in_dev = __in_dev_get_rcu(rt->dst.dev);
  40. if (in_dev != NULL) {
  41. for_primary_ifa(in_dev) {
  42. if (ifa->ifa_broadcast == iph->daddr) {
  43. mask = ifa->ifa_mask;
  44. break;
  45. }
  46. } endfor_ifa(in_dev);
  47. }
  48. rcu_read_unlock();
  49. if (mask == 0)
  50. goto out;
  51. exp = nf_ct_expect_alloc(ct);
  52. if (exp == NULL)
  53. goto out;
  54. exp->tuple = ct->tuplehash[IP_CT_DIR_REPLY].tuple;
  55. exp->tuple.src.u.udp.port = help->helper->tuple.src.u.udp.port;
  56. exp->mask.src.u3.ip = mask;
  57. exp->mask.src.u.udp.port = htons(0xFFFF);
  58. exp->expectfn = NULL;
  59. exp->flags = NF_CT_EXPECT_PERMANENT;
  60. exp->class = NF_CT_EXPECT_CLASS_DEFAULT;
  61. exp->helper = NULL;
  62. nf_ct_expect_related(exp);
  63. nf_ct_expect_put(exp);
  64. nf_ct_refresh(ct, skb, timeout * HZ);
  65. out:
  66. return NF_ACCEPT;
  67. }
  68. EXPORT_SYMBOL_GPL(nf_conntrack_broadcast_help);
  69. MODULE_LICENSE("GPL");