xt_socket.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Transparent proxy support for Linux/iptables
  3. *
  4. * Copyright (C) 2007-2008 BalaBit IT Ltd.
  5. * Author: Krisztian Kovacs
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/netfilter/x_tables.h>
  16. #include <linux/netfilter_ipv4/ip_tables.h>
  17. #include <net/tcp.h>
  18. #include <net/udp.h>
  19. #include <net/icmp.h>
  20. #include <net/sock.h>
  21. #include <net/inet_sock.h>
  22. #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
  23. #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
  24. #define XT_SOCKET_HAVE_IPV6 1
  25. #include <linux/netfilter_ipv6/ip6_tables.h>
  26. #include <net/inet6_hashtables.h>
  27. #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
  28. #endif
  29. #include <linux/netfilter/xt_socket.h>
  30. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  31. #define XT_SOCKET_HAVE_CONNTRACK 1
  32. #include <net/netfilter/nf_conntrack.h>
  33. #endif
  34. static int
  35. extract_icmp4_fields(const struct sk_buff *skb,
  36. u8 *protocol,
  37. __be32 *raddr,
  38. __be32 *laddr,
  39. __be16 *rport,
  40. __be16 *lport)
  41. {
  42. unsigned int outside_hdrlen = ip_hdrlen(skb);
  43. struct iphdr *inside_iph, _inside_iph;
  44. struct icmphdr *icmph, _icmph;
  45. __be16 *ports, _ports[2];
  46. icmph = skb_header_pointer(skb, outside_hdrlen,
  47. sizeof(_icmph), &_icmph);
  48. if (icmph == NULL)
  49. return 1;
  50. switch (icmph->type) {
  51. case ICMP_DEST_UNREACH:
  52. case ICMP_SOURCE_QUENCH:
  53. case ICMP_REDIRECT:
  54. case ICMP_TIME_EXCEEDED:
  55. case ICMP_PARAMETERPROB:
  56. break;
  57. default:
  58. return 1;
  59. }
  60. inside_iph = skb_header_pointer(skb, outside_hdrlen +
  61. sizeof(struct icmphdr),
  62. sizeof(_inside_iph), &_inside_iph);
  63. if (inside_iph == NULL)
  64. return 1;
  65. if (inside_iph->protocol != IPPROTO_TCP &&
  66. inside_iph->protocol != IPPROTO_UDP)
  67. return 1;
  68. ports = skb_header_pointer(skb, outside_hdrlen +
  69. sizeof(struct icmphdr) +
  70. (inside_iph->ihl << 2),
  71. sizeof(_ports), &_ports);
  72. if (ports == NULL)
  73. return 1;
  74. /* the inside IP packet is the one quoted from our side, thus
  75. * its saddr is the local address */
  76. *protocol = inside_iph->protocol;
  77. *laddr = inside_iph->saddr;
  78. *lport = ports[0];
  79. *raddr = inside_iph->daddr;
  80. *rport = ports[1];
  81. return 0;
  82. }
  83. /* "socket" match based redirection (no specific rule)
  84. * ===================================================
  85. *
  86. * There are connections with dynamic endpoints (e.g. FTP data
  87. * connection) that the user is unable to add explicit rules
  88. * for. These are taken care of by a generic "socket" rule. It is
  89. * assumed that the proxy application is trusted to open such
  90. * connections without explicit iptables rule (except of course the
  91. * generic 'socket' rule). In this case the following sockets are
  92. * matched in preference order:
  93. *
  94. * - match: if there's a fully established connection matching the
  95. * _packet_ tuple
  96. *
  97. * - match: if there's a non-zero bound listener (possibly with a
  98. * non-local address) We don't accept zero-bound listeners, since
  99. * then local services could intercept traffic going through the
  100. * box.
  101. */
  102. static struct sock *
  103. xt_socket_get_sock_v4(struct net *net, const u8 protocol,
  104. const __be32 saddr, const __be32 daddr,
  105. const __be16 sport, const __be16 dport,
  106. const struct net_device *in)
  107. {
  108. switch (protocol) {
  109. case IPPROTO_TCP:
  110. return __inet_lookup(net, &tcp_hashinfo,
  111. saddr, sport, daddr, dport,
  112. in->ifindex);
  113. case IPPROTO_UDP:
  114. return udp4_lib_lookup(net, saddr, sport, daddr, dport,
  115. in->ifindex);
  116. }
  117. return NULL;
  118. }
  119. struct sock*
  120. xt_socket_get4_sk(const struct sk_buff *skb, struct xt_action_param *par)
  121. {
  122. const struct iphdr *iph = ip_hdr(skb);
  123. struct udphdr _hdr, *hp = NULL;
  124. struct sock *sk = skb->sk;
  125. __be32 uninitialized_var(daddr), uninitialized_var(saddr);
  126. __be16 uninitialized_var(dport), uninitialized_var(sport);
  127. u8 uninitialized_var(protocol);
  128. #ifdef XT_SOCKET_HAVE_CONNTRACK
  129. struct nf_conn const *ct;
  130. enum ip_conntrack_info ctinfo;
  131. #endif
  132. if (iph->protocol == IPPROTO_UDP || iph->protocol == IPPROTO_TCP) {
  133. hp = skb_header_pointer(skb, ip_hdrlen(skb),
  134. sizeof(_hdr), &_hdr);
  135. if (hp == NULL)
  136. return NULL;
  137. protocol = iph->protocol;
  138. saddr = iph->saddr;
  139. sport = hp->source;
  140. daddr = iph->daddr;
  141. dport = hp->dest;
  142. } else if (iph->protocol == IPPROTO_ICMP) {
  143. if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr,
  144. &sport, &dport))
  145. return NULL;
  146. } else {
  147. return NULL;
  148. }
  149. #ifdef XT_SOCKET_HAVE_CONNTRACK
  150. /* Do the lookup with the original socket address in case this is a
  151. * reply packet of an established SNAT-ted connection. */
  152. ct = nf_ct_get(skb, &ctinfo);
  153. if (ct && !nf_ct_is_untracked(ct) &&
  154. ((iph->protocol != IPPROTO_ICMP &&
  155. ctinfo == IP_CT_ESTABLISHED_REPLY) ||
  156. (iph->protocol == IPPROTO_ICMP &&
  157. ctinfo == IP_CT_RELATED_REPLY)) &&
  158. (ct->status & IPS_SRC_NAT_DONE)) {
  159. daddr = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
  160. dport = (iph->protocol == IPPROTO_TCP) ?
  161. ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.tcp.port :
  162. ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
  163. }
  164. #endif
  165. if (sk)
  166. atomic_inc(&sk->sk_refcnt);
  167. else
  168. sk = xt_socket_get_sock_v4(dev_net(skb->dev), protocol,
  169. saddr, daddr, sport, dport,
  170. par->in);
  171. pr_debug("proto %hhu %pI4:%hu -> %pI4:%hu (orig %pI4:%hu) sock %p\n",
  172. protocol, &saddr, ntohs(sport),
  173. &daddr, ntohs(dport),
  174. &iph->daddr, hp ? ntohs(hp->dest) : 0, sk);
  175. return sk;
  176. }
  177. EXPORT_SYMBOL(xt_socket_get4_sk);
  178. static bool
  179. socket_match(const struct sk_buff *skb, struct xt_action_param *par,
  180. const struct xt_socket_mtinfo1 *info)
  181. {
  182. struct sock *sk;
  183. sk = xt_socket_get4_sk(skb, par);
  184. if (sk) {
  185. bool wildcard;
  186. bool transparent = true;
  187. /* Ignore sockets listening on INADDR_ANY,
  188. * unless XT_SOCKET_NOWILDCARD is set
  189. */
  190. wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
  191. sk->sk_state != TCP_TIME_WAIT &&
  192. inet_sk(sk)->inet_rcv_saddr == 0);
  193. /* Ignore non-transparent sockets,
  194. if XT_SOCKET_TRANSPARENT is used */
  195. if (info->flags & XT_SOCKET_TRANSPARENT)
  196. transparent = ((sk->sk_state != TCP_TIME_WAIT &&
  197. inet_sk(sk)->transparent) ||
  198. (sk->sk_state == TCP_TIME_WAIT &&
  199. inet_twsk(sk)->tw_transparent));
  200. sock_gen_put(sk);
  201. if (wildcard || !transparent)
  202. sk = NULL;
  203. }
  204. return (sk != NULL);
  205. }
  206. static bool
  207. socket_mt4_v0(const struct sk_buff *skb, struct xt_action_param *par)
  208. {
  209. static struct xt_socket_mtinfo1 xt_info_v0 = {
  210. .flags = 0,
  211. };
  212. return socket_match(skb, par, &xt_info_v0);
  213. }
  214. static bool
  215. socket_mt4_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
  216. {
  217. return socket_match(skb, par, par->matchinfo);
  218. }
  219. #ifdef XT_SOCKET_HAVE_IPV6
  220. static int
  221. extract_icmp6_fields(const struct sk_buff *skb,
  222. unsigned int outside_hdrlen,
  223. int *protocol,
  224. struct in6_addr **raddr,
  225. struct in6_addr **laddr,
  226. __be16 *rport,
  227. __be16 *lport)
  228. {
  229. struct ipv6hdr *inside_iph, _inside_iph;
  230. struct icmp6hdr *icmph, _icmph;
  231. __be16 *ports, _ports[2];
  232. u8 inside_nexthdr;
  233. __be16 inside_fragoff;
  234. int inside_hdrlen;
  235. icmph = skb_header_pointer(skb, outside_hdrlen,
  236. sizeof(_icmph), &_icmph);
  237. if (icmph == NULL)
  238. return 1;
  239. if (icmph->icmp6_type & ICMPV6_INFOMSG_MASK)
  240. return 1;
  241. inside_iph = skb_header_pointer(skb, outside_hdrlen + sizeof(_icmph), sizeof(_inside_iph), &_inside_iph);
  242. if (inside_iph == NULL)
  243. return 1;
  244. inside_nexthdr = inside_iph->nexthdr;
  245. inside_hdrlen = ipv6_skip_exthdr(skb, outside_hdrlen + sizeof(_icmph) + sizeof(_inside_iph),
  246. &inside_nexthdr, &inside_fragoff);
  247. if (inside_hdrlen < 0)
  248. return 1; /* hjm: Packet has no/incomplete transport layer headers. */
  249. if (inside_nexthdr != IPPROTO_TCP &&
  250. inside_nexthdr != IPPROTO_UDP)
  251. return 1;
  252. ports = skb_header_pointer(skb, inside_hdrlen,
  253. sizeof(_ports), &_ports);
  254. if (ports == NULL)
  255. return 1;
  256. /* the inside IP packet is the one quoted from our side, thus
  257. * its saddr is the local address */
  258. *protocol = inside_nexthdr;
  259. *laddr = &inside_iph->saddr;
  260. *lport = ports[0];
  261. *raddr = &inside_iph->daddr;
  262. *rport = ports[1];
  263. return 0;
  264. }
  265. static struct sock *
  266. xt_socket_get_sock_v6(struct net *net, const u8 protocol,
  267. const struct in6_addr *saddr, const struct in6_addr *daddr,
  268. const __be16 sport, const __be16 dport,
  269. const struct net_device *in)
  270. {
  271. switch (protocol) {
  272. case IPPROTO_TCP:
  273. return inet6_lookup(net, &tcp_hashinfo,
  274. saddr, sport, daddr, dport,
  275. in->ifindex);
  276. case IPPROTO_UDP:
  277. return udp6_lib_lookup(net, saddr, sport, daddr, dport,
  278. in->ifindex);
  279. }
  280. return NULL;
  281. }
  282. struct sock*
  283. xt_socket_get6_sk(const struct sk_buff *skb, struct xt_action_param *par)
  284. {
  285. struct ipv6hdr *iph = ipv6_hdr(skb);
  286. struct udphdr _hdr, *hp = NULL;
  287. struct sock *sk = skb->sk;
  288. struct in6_addr *daddr = NULL, *saddr = NULL;
  289. __be16 uninitialized_var(dport), uninitialized_var(sport);
  290. int thoff = 0, uninitialized_var(tproto);
  291. tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
  292. if (tproto < 0) {
  293. pr_debug("unable to find transport header in IPv6 packet, dropping\n");
  294. return NF_DROP;
  295. }
  296. if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
  297. hp = skb_header_pointer(skb, thoff,
  298. sizeof(_hdr), &_hdr);
  299. if (hp == NULL)
  300. return NULL;
  301. saddr = &iph->saddr;
  302. sport = hp->source;
  303. daddr = &iph->daddr;
  304. dport = hp->dest;
  305. } else if (tproto == IPPROTO_ICMPV6) {
  306. if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr,
  307. &sport, &dport))
  308. return NULL;
  309. } else {
  310. return NULL;
  311. }
  312. if (sk)
  313. atomic_inc(&sk->sk_refcnt);
  314. else
  315. sk = xt_socket_get_sock_v6(dev_net(skb->dev), tproto,
  316. saddr, daddr, sport, dport,
  317. par->in);
  318. pr_debug("proto %hhd %pI6:%hu -> %pI6:%hu "
  319. "(orig %pI6:%hu) sock %p\n",
  320. tproto, saddr, ntohs(sport),
  321. daddr, ntohs(dport),
  322. &iph->daddr, hp ? ntohs(hp->dest) : 0, sk);
  323. return sk;
  324. }
  325. EXPORT_SYMBOL(xt_socket_get6_sk);
  326. static bool
  327. socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
  328. {
  329. struct sock *sk;
  330. const struct xt_socket_mtinfo1 *info;
  331. info = (struct xt_socket_mtinfo1 *) par->matchinfo;
  332. sk = xt_socket_get6_sk(skb, par);
  333. if (sk) {
  334. bool wildcard;
  335. bool transparent = true;
  336. /* Ignore sockets listening on INADDR_ANY
  337. * unless XT_SOCKET_NOWILDCARD is set
  338. */
  339. wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
  340. sk->sk_state != TCP_TIME_WAIT &&
  341. ipv6_addr_any(&sk->sk_v6_rcv_saddr));
  342. /* Ignore non-transparent sockets,
  343. if XT_SOCKET_TRANSPARENT is used */
  344. if (info->flags & XT_SOCKET_TRANSPARENT)
  345. transparent = ((sk->sk_state != TCP_TIME_WAIT &&
  346. inet_sk(sk)->transparent) ||
  347. (sk->sk_state == TCP_TIME_WAIT &&
  348. inet_twsk(sk)->tw_transparent));
  349. if (sk != skb->sk)
  350. sock_gen_put(sk);
  351. if (wildcard || !transparent)
  352. sk = NULL;
  353. }
  354. return (sk != NULL);
  355. }
  356. #endif
  357. static int socket_mt_v1_check(const struct xt_mtchk_param *par)
  358. {
  359. const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
  360. if (info->flags & ~XT_SOCKET_FLAGS_V1) {
  361. pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V1);
  362. return -EINVAL;
  363. }
  364. return 0;
  365. }
  366. static int socket_mt_v2_check(const struct xt_mtchk_param *par)
  367. {
  368. const struct xt_socket_mtinfo2 *info = (struct xt_socket_mtinfo2 *) par->matchinfo;
  369. if (info->flags & ~XT_SOCKET_FLAGS_V2) {
  370. pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V2);
  371. return -EINVAL;
  372. }
  373. return 0;
  374. }
  375. static struct xt_match socket_mt_reg[] __read_mostly = {
  376. {
  377. .name = "socket",
  378. .revision = 0,
  379. .family = NFPROTO_IPV4,
  380. .match = socket_mt4_v0,
  381. .hooks = (1 << NF_INET_PRE_ROUTING) |
  382. (1 << NF_INET_LOCAL_IN),
  383. .me = THIS_MODULE,
  384. },
  385. {
  386. .name = "socket",
  387. .revision = 1,
  388. .family = NFPROTO_IPV4,
  389. .match = socket_mt4_v1_v2,
  390. .checkentry = socket_mt_v1_check,
  391. .matchsize = sizeof(struct xt_socket_mtinfo1),
  392. .hooks = (1 << NF_INET_PRE_ROUTING) |
  393. (1 << NF_INET_LOCAL_IN),
  394. .me = THIS_MODULE,
  395. },
  396. #ifdef XT_SOCKET_HAVE_IPV6
  397. {
  398. .name = "socket",
  399. .revision = 1,
  400. .family = NFPROTO_IPV6,
  401. .match = socket_mt6_v1_v2,
  402. .checkentry = socket_mt_v1_check,
  403. .matchsize = sizeof(struct xt_socket_mtinfo1),
  404. .hooks = (1 << NF_INET_PRE_ROUTING) |
  405. (1 << NF_INET_LOCAL_IN),
  406. .me = THIS_MODULE,
  407. },
  408. #endif
  409. {
  410. .name = "socket",
  411. .revision = 2,
  412. .family = NFPROTO_IPV4,
  413. .match = socket_mt4_v1_v2,
  414. .checkentry = socket_mt_v2_check,
  415. .matchsize = sizeof(struct xt_socket_mtinfo1),
  416. .hooks = (1 << NF_INET_PRE_ROUTING) |
  417. (1 << NF_INET_LOCAL_IN),
  418. .me = THIS_MODULE,
  419. },
  420. #ifdef XT_SOCKET_HAVE_IPV6
  421. {
  422. .name = "socket",
  423. .revision = 2,
  424. .family = NFPROTO_IPV6,
  425. .match = socket_mt6_v1_v2,
  426. .checkentry = socket_mt_v2_check,
  427. .matchsize = sizeof(struct xt_socket_mtinfo1),
  428. .hooks = (1 << NF_INET_PRE_ROUTING) |
  429. (1 << NF_INET_LOCAL_IN),
  430. .me = THIS_MODULE,
  431. },
  432. #endif
  433. };
  434. static int __init socket_mt_init(void)
  435. {
  436. nf_defrag_ipv4_enable();
  437. #ifdef XT_SOCKET_HAVE_IPV6
  438. nf_defrag_ipv6_enable();
  439. #endif
  440. return xt_register_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
  441. }
  442. static void __exit socket_mt_exit(void)
  443. {
  444. xt_unregister_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
  445. }
  446. module_init(socket_mt_init);
  447. module_exit(socket_mt_exit);
  448. MODULE_LICENSE("GPL");
  449. MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
  450. MODULE_DESCRIPTION("x_tables socket match module");
  451. MODULE_ALIAS("ipt_socket");
  452. MODULE_ALIAS("ip6t_socket");