ip_set_getport.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Copyright (C) 2003-2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 as
  5. * published by the Free Software Foundation.
  6. */
  7. /* Get Layer-4 data from the packets */
  8. #include <linux/ip.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/icmp.h>
  11. #include <linux/icmpv6.h>
  12. #include <linux/sctp.h>
  13. #include <linux/netfilter_ipv6/ip6_tables.h>
  14. #include <net/ip.h>
  15. #include <net/ipv6.h>
  16. #include <linux/netfilter/ipset/ip_set_getport.h>
  17. #include <linux/export.h>
  18. /* We must handle non-linear skbs */
  19. static bool
  20. get_port(const struct sk_buff *skb, int protocol, unsigned int protooff,
  21. bool src, __be16 *port, u8 *proto)
  22. {
  23. switch (protocol) {
  24. case IPPROTO_TCP: {
  25. struct tcphdr _tcph;
  26. const struct tcphdr *th;
  27. th = skb_header_pointer(skb, protooff, sizeof(_tcph), &_tcph);
  28. if (!th)
  29. /* No choice either */
  30. return false;
  31. *port = src ? th->source : th->dest;
  32. break;
  33. }
  34. case IPPROTO_SCTP: {
  35. sctp_sctphdr_t _sh;
  36. const sctp_sctphdr_t *sh;
  37. sh = skb_header_pointer(skb, protooff, sizeof(_sh), &_sh);
  38. if (!sh)
  39. /* No choice either */
  40. return false;
  41. *port = src ? sh->source : sh->dest;
  42. break;
  43. }
  44. case IPPROTO_UDP:
  45. case IPPROTO_UDPLITE: {
  46. struct udphdr _udph;
  47. const struct udphdr *uh;
  48. uh = skb_header_pointer(skb, protooff, sizeof(_udph), &_udph);
  49. if (!uh)
  50. /* No choice either */
  51. return false;
  52. *port = src ? uh->source : uh->dest;
  53. break;
  54. }
  55. case IPPROTO_ICMP: {
  56. struct icmphdr _ich;
  57. const struct icmphdr *ic;
  58. ic = skb_header_pointer(skb, protooff, sizeof(_ich), &_ich);
  59. if (!ic)
  60. return false;
  61. *port = (__force __be16)htons((ic->type << 8) | ic->code);
  62. break;
  63. }
  64. case IPPROTO_ICMPV6: {
  65. struct icmp6hdr _ich;
  66. const struct icmp6hdr *ic;
  67. ic = skb_header_pointer(skb, protooff, sizeof(_ich), &_ich);
  68. if (!ic)
  69. return false;
  70. *port = (__force __be16)
  71. htons((ic->icmp6_type << 8) | ic->icmp6_code);
  72. break;
  73. }
  74. default:
  75. break;
  76. }
  77. *proto = protocol;
  78. return true;
  79. }
  80. bool
  81. ip_set_get_ip4_port(const struct sk_buff *skb, bool src,
  82. __be16 *port, u8 *proto)
  83. {
  84. const struct iphdr *iph = ip_hdr(skb);
  85. unsigned int protooff = skb_network_offset(skb) + ip_hdrlen(skb);
  86. int protocol = iph->protocol;
  87. /* See comments at tcp_match in ip_tables.c */
  88. if (protocol <= 0)
  89. return false;
  90. if (ntohs(iph->frag_off) & IP_OFFSET)
  91. switch (protocol) {
  92. case IPPROTO_TCP:
  93. case IPPROTO_SCTP:
  94. case IPPROTO_UDP:
  95. case IPPROTO_UDPLITE:
  96. case IPPROTO_ICMP:
  97. /* Port info not available for fragment offset > 0 */
  98. return false;
  99. default:
  100. /* Other protocols doesn't have ports,
  101. * so we can match fragments.
  102. */
  103. *proto = protocol;
  104. return true;
  105. }
  106. return get_port(skb, protocol, protooff, src, port, proto);
  107. }
  108. EXPORT_SYMBOL_GPL(ip_set_get_ip4_port);
  109. #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
  110. bool
  111. ip_set_get_ip6_port(const struct sk_buff *skb, bool src,
  112. __be16 *port, u8 *proto)
  113. {
  114. int protoff;
  115. u8 nexthdr;
  116. __be16 frag_off = 0;
  117. nexthdr = ipv6_hdr(skb)->nexthdr;
  118. protoff = ipv6_skip_exthdr(skb,
  119. skb_network_offset(skb) +
  120. sizeof(struct ipv6hdr), &nexthdr,
  121. &frag_off);
  122. if (protoff < 0 || (frag_off & htons(~0x7)) != 0)
  123. return false;
  124. return get_port(skb, nexthdr, protoff, src, port, proto);
  125. }
  126. EXPORT_SYMBOL_GPL(ip_set_get_ip6_port);
  127. #endif
  128. bool
  129. ip_set_get_ip_port(const struct sk_buff *skb, u8 pf, bool src, __be16 *port)
  130. {
  131. bool ret;
  132. u8 proto;
  133. switch (pf) {
  134. case NFPROTO_IPV4:
  135. ret = ip_set_get_ip4_port(skb, src, port, &proto);
  136. break;
  137. case NFPROTO_IPV6:
  138. ret = ip_set_get_ip6_port(skb, src, port, &proto);
  139. break;
  140. default:
  141. return false;
  142. }
  143. if (!ret)
  144. return ret;
  145. switch (proto) {
  146. case IPPROTO_TCP:
  147. case IPPROTO_UDP:
  148. return true;
  149. default:
  150. return false;
  151. }
  152. }
  153. EXPORT_SYMBOL_GPL(ip_set_get_ip_port);