nf_conntrack_proto_udp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* (C) 1999-2001 Paul `Rusty' Russell
  2. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  3. * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
  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/types.h>
  10. #include <linux/timer.h>
  11. #include <linux/module.h>
  12. #include <linux/udp.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/ipv6.h>
  16. #include <net/ip6_checksum.h>
  17. #include <net/checksum.h>
  18. #include <linux/netfilter.h>
  19. #include <linux/netfilter_ipv4.h>
  20. #include <linux/netfilter_ipv6.h>
  21. #include <net/netfilter/nf_conntrack_l4proto.h>
  22. #include <net/netfilter/nf_conntrack_ecache.h>
  23. #include <net/netfilter/nf_log.h>
  24. #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
  25. #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
  26. static unsigned int udp_timeouts[UDP_CT_MAX] = {
  27. [UDP_CT_UNREPLIED] = 30*HZ,
  28. [UDP_CT_REPLIED] = 180*HZ,
  29. };
  30. static inline struct nf_udp_net *udp_pernet(struct net *net)
  31. {
  32. return &net->ct.nf_ct_proto.udp;
  33. }
  34. static bool udp_pkt_to_tuple(const struct sk_buff *skb,
  35. unsigned int dataoff,
  36. struct net *net,
  37. struct nf_conntrack_tuple *tuple)
  38. {
  39. const struct udphdr *hp;
  40. struct udphdr _hdr;
  41. /* Actually only need first 8 bytes. */
  42. hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
  43. if (hp == NULL)
  44. return false;
  45. tuple->src.u.udp.port = hp->source;
  46. tuple->dst.u.udp.port = hp->dest;
  47. return true;
  48. }
  49. static bool udp_invert_tuple(struct nf_conntrack_tuple *tuple,
  50. const struct nf_conntrack_tuple *orig)
  51. {
  52. tuple->src.u.udp.port = orig->dst.u.udp.port;
  53. tuple->dst.u.udp.port = orig->src.u.udp.port;
  54. return true;
  55. }
  56. /* Print out the per-protocol part of the tuple. */
  57. static void udp_print_tuple(struct seq_file *s,
  58. const struct nf_conntrack_tuple *tuple)
  59. {
  60. seq_printf(s, "sport=%hu dport=%hu ",
  61. ntohs(tuple->src.u.udp.port),
  62. ntohs(tuple->dst.u.udp.port));
  63. }
  64. static unsigned int *udp_get_timeouts(struct net *net)
  65. {
  66. return udp_pernet(net)->timeouts;
  67. }
  68. /* Returns verdict for packet, and may modify conntracktype */
  69. static int udp_packet(struct nf_conn *ct,
  70. const struct sk_buff *skb,
  71. unsigned int dataoff,
  72. enum ip_conntrack_info ctinfo,
  73. u_int8_t pf,
  74. unsigned int hooknum,
  75. unsigned int *timeouts)
  76. {
  77. /* If we've seen traffic both ways, this is some kind of UDP
  78. stream. Extend timeout. */
  79. if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
  80. nf_ct_refresh_acct(ct, ctinfo, skb,
  81. timeouts[UDP_CT_REPLIED]);
  82. /* Also, more likely to be important, and not a probe */
  83. if (!test_and_set_bit(IPS_ASSURED_BIT, &ct->status))
  84. nf_conntrack_event_cache(IPCT_ASSURED, ct);
  85. } else {
  86. nf_ct_refresh_acct(ct, ctinfo, skb,
  87. timeouts[UDP_CT_UNREPLIED]);
  88. }
  89. return NF_ACCEPT;
  90. }
  91. /* Called when a new connection for this protocol found. */
  92. static bool udp_new(struct nf_conn *ct, const struct sk_buff *skb,
  93. unsigned int dataoff, unsigned int *timeouts)
  94. {
  95. return true;
  96. }
  97. static int udp_error(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
  98. unsigned int dataoff, enum ip_conntrack_info *ctinfo,
  99. u_int8_t pf,
  100. unsigned int hooknum)
  101. {
  102. unsigned int udplen = skb->len - dataoff;
  103. const struct udphdr *hdr;
  104. struct udphdr _hdr;
  105. /* Header is too small? */
  106. hdr = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
  107. if (hdr == NULL) {
  108. if (LOG_INVALID(net, IPPROTO_UDP))
  109. nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
  110. "nf_ct_udp: short packet ");
  111. return -NF_ACCEPT;
  112. }
  113. /* Truncated/malformed packets */
  114. if (ntohs(hdr->len) > udplen || ntohs(hdr->len) < sizeof(*hdr)) {
  115. if (LOG_INVALID(net, IPPROTO_UDP))
  116. nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
  117. "nf_ct_udp: truncated/malformed packet ");
  118. return -NF_ACCEPT;
  119. }
  120. /* Packet with no checksum */
  121. if (!hdr->check)
  122. return NF_ACCEPT;
  123. /* Checksum invalid? Ignore.
  124. * We skip checking packets on the outgoing path
  125. * because the checksum is assumed to be correct.
  126. * FIXME: Source route IP option packets --RR */
  127. if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
  128. nf_checksum(skb, hooknum, dataoff, IPPROTO_UDP, pf)) {
  129. if (LOG_INVALID(net, IPPROTO_UDP))
  130. nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
  131. "nf_ct_udp: bad UDP checksum ");
  132. return -NF_ACCEPT;
  133. }
  134. return NF_ACCEPT;
  135. }
  136. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  137. #include <linux/netfilter/nfnetlink.h>
  138. #include <linux/netfilter/nfnetlink_cttimeout.h>
  139. static int udp_timeout_nlattr_to_obj(struct nlattr *tb[],
  140. struct net *net, void *data)
  141. {
  142. unsigned int *timeouts = data;
  143. struct nf_udp_net *un = udp_pernet(net);
  144. /* set default timeouts for UDP. */
  145. timeouts[UDP_CT_UNREPLIED] = un->timeouts[UDP_CT_UNREPLIED];
  146. timeouts[UDP_CT_REPLIED] = un->timeouts[UDP_CT_REPLIED];
  147. if (tb[CTA_TIMEOUT_UDP_UNREPLIED]) {
  148. timeouts[UDP_CT_UNREPLIED] =
  149. ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_UNREPLIED])) * HZ;
  150. }
  151. if (tb[CTA_TIMEOUT_UDP_REPLIED]) {
  152. timeouts[UDP_CT_REPLIED] =
  153. ntohl(nla_get_be32(tb[CTA_TIMEOUT_UDP_REPLIED])) * HZ;
  154. }
  155. return 0;
  156. }
  157. static int
  158. udp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
  159. {
  160. const unsigned int *timeouts = data;
  161. if (nla_put_be32(skb, CTA_TIMEOUT_UDP_UNREPLIED,
  162. htonl(timeouts[UDP_CT_UNREPLIED] / HZ)) ||
  163. nla_put_be32(skb, CTA_TIMEOUT_UDP_REPLIED,
  164. htonl(timeouts[UDP_CT_REPLIED] / HZ)))
  165. goto nla_put_failure;
  166. return 0;
  167. nla_put_failure:
  168. return -ENOSPC;
  169. }
  170. static const struct nla_policy
  171. udp_timeout_nla_policy[CTA_TIMEOUT_UDP_MAX+1] = {
  172. [CTA_TIMEOUT_UDP_UNREPLIED] = { .type = NLA_U32 },
  173. [CTA_TIMEOUT_UDP_REPLIED] = { .type = NLA_U32 },
  174. };
  175. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  176. #ifdef CONFIG_SYSCTL
  177. static struct ctl_table udp_sysctl_table[] = {
  178. {
  179. .procname = "nf_conntrack_udp_timeout",
  180. .maxlen = sizeof(unsigned int),
  181. .mode = 0644,
  182. .proc_handler = proc_dointvec_jiffies,
  183. },
  184. {
  185. .procname = "nf_conntrack_udp_timeout_stream",
  186. .maxlen = sizeof(unsigned int),
  187. .mode = 0644,
  188. .proc_handler = proc_dointvec_jiffies,
  189. },
  190. { }
  191. };
  192. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  193. static struct ctl_table udp_compat_sysctl_table[] = {
  194. {
  195. .procname = "ip_conntrack_udp_timeout",
  196. .maxlen = sizeof(unsigned int),
  197. .mode = 0644,
  198. .proc_handler = proc_dointvec_jiffies,
  199. },
  200. {
  201. .procname = "ip_conntrack_udp_timeout_stream",
  202. .maxlen = sizeof(unsigned int),
  203. .mode = 0644,
  204. .proc_handler = proc_dointvec_jiffies,
  205. },
  206. { }
  207. };
  208. #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
  209. #endif /* CONFIG_SYSCTL */
  210. static int udp_kmemdup_sysctl_table(struct nf_proto_net *pn,
  211. struct nf_udp_net *un)
  212. {
  213. #ifdef CONFIG_SYSCTL
  214. if (pn->ctl_table)
  215. return 0;
  216. pn->ctl_table = kmemdup(udp_sysctl_table,
  217. sizeof(udp_sysctl_table),
  218. GFP_KERNEL);
  219. if (!pn->ctl_table)
  220. return -ENOMEM;
  221. pn->ctl_table[0].data = &un->timeouts[UDP_CT_UNREPLIED];
  222. pn->ctl_table[1].data = &un->timeouts[UDP_CT_REPLIED];
  223. #endif
  224. return 0;
  225. }
  226. static int udp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
  227. struct nf_udp_net *un)
  228. {
  229. #ifdef CONFIG_SYSCTL
  230. #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
  231. pn->ctl_compat_table = kmemdup(udp_compat_sysctl_table,
  232. sizeof(udp_compat_sysctl_table),
  233. GFP_KERNEL);
  234. if (!pn->ctl_compat_table)
  235. return -ENOMEM;
  236. pn->ctl_compat_table[0].data = &un->timeouts[UDP_CT_UNREPLIED];
  237. pn->ctl_compat_table[1].data = &un->timeouts[UDP_CT_REPLIED];
  238. #endif
  239. #endif
  240. return 0;
  241. }
  242. static int udp_init_net(struct net *net, u_int16_t proto)
  243. {
  244. int ret;
  245. struct nf_udp_net *un = udp_pernet(net);
  246. struct nf_proto_net *pn = &un->pn;
  247. if (!pn->users) {
  248. int i;
  249. for (i = 0; i < UDP_CT_MAX; i++)
  250. un->timeouts[i] = udp_timeouts[i];
  251. }
  252. if (proto == AF_INET) {
  253. ret = udp_kmemdup_compat_sysctl_table(pn, un);
  254. if (ret < 0)
  255. return ret;
  256. ret = udp_kmemdup_sysctl_table(pn, un);
  257. if (ret < 0)
  258. nf_ct_kfree_compat_sysctl_table(pn);
  259. } else
  260. ret = udp_kmemdup_sysctl_table(pn, un);
  261. return ret;
  262. }
  263. static struct nf_proto_net *udp_get_net_proto(struct net *net)
  264. {
  265. return &net->ct.nf_ct_proto.udp.pn;
  266. }
  267. struct nf_conntrack_l4proto nf_conntrack_l4proto_udp4 __read_mostly =
  268. {
  269. .l3proto = PF_INET,
  270. .l4proto = IPPROTO_UDP,
  271. .name = "udp",
  272. .pkt_to_tuple = udp_pkt_to_tuple,
  273. .invert_tuple = udp_invert_tuple,
  274. .print_tuple = udp_print_tuple,
  275. .packet = udp_packet,
  276. .get_timeouts = udp_get_timeouts,
  277. .new = udp_new,
  278. .error = udp_error,
  279. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  280. .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
  281. .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
  282. .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
  283. .nla_policy = nf_ct_port_nla_policy,
  284. #endif
  285. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  286. .ctnl_timeout = {
  287. .nlattr_to_obj = udp_timeout_nlattr_to_obj,
  288. .obj_to_nlattr = udp_timeout_obj_to_nlattr,
  289. .nlattr_max = CTA_TIMEOUT_UDP_MAX,
  290. .obj_size = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
  291. .nla_policy = udp_timeout_nla_policy,
  292. },
  293. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  294. .init_net = udp_init_net,
  295. .get_net_proto = udp_get_net_proto,
  296. };
  297. EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp4);
  298. struct nf_conntrack_l4proto nf_conntrack_l4proto_udp6 __read_mostly =
  299. {
  300. .l3proto = PF_INET6,
  301. .l4proto = IPPROTO_UDP,
  302. .name = "udp",
  303. .pkt_to_tuple = udp_pkt_to_tuple,
  304. .invert_tuple = udp_invert_tuple,
  305. .print_tuple = udp_print_tuple,
  306. .packet = udp_packet,
  307. .get_timeouts = udp_get_timeouts,
  308. .new = udp_new,
  309. .error = udp_error,
  310. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  311. .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr,
  312. .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple,
  313. .nlattr_tuple_size = nf_ct_port_nlattr_tuple_size,
  314. .nla_policy = nf_ct_port_nla_policy,
  315. #endif
  316. #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
  317. .ctnl_timeout = {
  318. .nlattr_to_obj = udp_timeout_nlattr_to_obj,
  319. .obj_to_nlattr = udp_timeout_obj_to_nlattr,
  320. .nlattr_max = CTA_TIMEOUT_UDP_MAX,
  321. .obj_size = sizeof(unsigned int) * CTA_TIMEOUT_UDP_MAX,
  322. .nla_policy = udp_timeout_nla_policy,
  323. },
  324. #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
  325. .init_net = udp_init_net,
  326. .get_net_proto = udp_get_net_proto,
  327. };
  328. EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_udp6);