xt_socket.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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, struct sk_buff *skb, const int doff,
  104. const u8 protocol,
  105. const __be32 saddr, const __be32 daddr,
  106. const __be16 sport, const __be16 dport,
  107. const struct net_device *in)
  108. {
  109. switch (protocol) {
  110. case IPPROTO_TCP:
  111. return inet_lookup(net, &tcp_hashinfo, skb, doff,
  112. saddr, sport, daddr, dport,
  113. in->ifindex);
  114. case IPPROTO_UDP:
  115. return udp4_lib_lookup(net, saddr, sport, daddr, dport,
  116. in->ifindex);
  117. }
  118. return NULL;
  119. }
  120. static bool xt_socket_sk_is_transparent(struct sock *sk)
  121. {
  122. switch (sk->sk_state) {
  123. case TCP_TIME_WAIT:
  124. return inet_twsk(sk)->tw_transparent;
  125. case TCP_NEW_SYN_RECV:
  126. return inet_rsk(inet_reqsk(sk))->no_srccheck;
  127. default:
  128. return inet_sk(sk)->transparent;
  129. }
  130. }
  131. static struct sock *xt_socket_lookup_slow_v4(struct net *net,
  132. const struct sk_buff *skb,
  133. const struct net_device *indev)
  134. {
  135. const struct iphdr *iph = ip_hdr(skb);
  136. struct sk_buff *data_skb = NULL;
  137. int doff = 0;
  138. __be32 uninitialized_var(daddr), uninitialized_var(saddr);
  139. __be16 uninitialized_var(dport), uninitialized_var(sport);
  140. u8 uninitialized_var(protocol);
  141. #ifdef XT_SOCKET_HAVE_CONNTRACK
  142. struct nf_conn const *ct;
  143. enum ip_conntrack_info ctinfo;
  144. #endif
  145. if (iph->protocol == IPPROTO_UDP || iph->protocol == IPPROTO_TCP) {
  146. struct udphdr _hdr, *hp;
  147. hp = skb_header_pointer(skb, ip_hdrlen(skb),
  148. sizeof(_hdr), &_hdr);
  149. if (hp == NULL)
  150. return NULL;
  151. protocol = iph->protocol;
  152. saddr = iph->saddr;
  153. sport = hp->source;
  154. daddr = iph->daddr;
  155. dport = hp->dest;
  156. data_skb = (struct sk_buff *)skb;
  157. doff = iph->protocol == IPPROTO_TCP ?
  158. ip_hdrlen(skb) + __tcp_hdrlen((struct tcphdr *)hp) :
  159. ip_hdrlen(skb) + sizeof(*hp);
  160. } else if (iph->protocol == IPPROTO_ICMP) {
  161. if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr,
  162. &sport, &dport))
  163. return NULL;
  164. } else {
  165. return NULL;
  166. }
  167. #ifdef XT_SOCKET_HAVE_CONNTRACK
  168. /* Do the lookup with the original socket address in
  169. * case this is a reply packet of an established
  170. * SNAT-ted connection.
  171. */
  172. ct = nf_ct_get(skb, &ctinfo);
  173. if (ct && !nf_ct_is_untracked(ct) &&
  174. ((iph->protocol != IPPROTO_ICMP &&
  175. ctinfo == IP_CT_ESTABLISHED_REPLY) ||
  176. (iph->protocol == IPPROTO_ICMP &&
  177. ctinfo == IP_CT_RELATED_REPLY)) &&
  178. (ct->status & IPS_SRC_NAT_DONE)) {
  179. daddr = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
  180. dport = (iph->protocol == IPPROTO_TCP) ?
  181. ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.tcp.port :
  182. ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u.udp.port;
  183. }
  184. #endif
  185. return xt_socket_get_sock_v4(net, data_skb, doff, protocol, saddr,
  186. daddr, sport, dport, indev);
  187. }
  188. static bool
  189. socket_match(const struct sk_buff *skb, struct xt_action_param *par,
  190. const struct xt_socket_mtinfo1 *info)
  191. {
  192. struct sk_buff *pskb = (struct sk_buff *)skb;
  193. struct sock *sk = skb->sk;
  194. if (!sk)
  195. sk = xt_socket_lookup_slow_v4(par->net, skb, par->in);
  196. if (sk) {
  197. bool wildcard;
  198. bool transparent = true;
  199. /* Ignore sockets listening on INADDR_ANY,
  200. * unless XT_SOCKET_NOWILDCARD is set
  201. */
  202. wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
  203. sk_fullsock(sk) &&
  204. inet_sk(sk)->inet_rcv_saddr == 0);
  205. /* Ignore non-transparent sockets,
  206. * if XT_SOCKET_TRANSPARENT is used
  207. */
  208. if (info->flags & XT_SOCKET_TRANSPARENT)
  209. transparent = xt_socket_sk_is_transparent(sk);
  210. if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard &&
  211. transparent)
  212. pskb->mark = sk->sk_mark;
  213. if (sk != skb->sk)
  214. sock_gen_put(sk);
  215. if (wildcard || !transparent)
  216. sk = NULL;
  217. }
  218. return sk != NULL;
  219. }
  220. static bool
  221. socket_mt4_v0(const struct sk_buff *skb, struct xt_action_param *par)
  222. {
  223. static struct xt_socket_mtinfo1 xt_info_v0 = {
  224. .flags = 0,
  225. };
  226. return socket_match(skb, par, &xt_info_v0);
  227. }
  228. static bool
  229. socket_mt4_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
  230. {
  231. return socket_match(skb, par, par->matchinfo);
  232. }
  233. #ifdef XT_SOCKET_HAVE_IPV6
  234. static int
  235. extract_icmp6_fields(const struct sk_buff *skb,
  236. unsigned int outside_hdrlen,
  237. int *protocol,
  238. const struct in6_addr **raddr,
  239. const struct in6_addr **laddr,
  240. __be16 *rport,
  241. __be16 *lport,
  242. struct ipv6hdr *ipv6_var)
  243. {
  244. const struct ipv6hdr *inside_iph;
  245. struct icmp6hdr *icmph, _icmph;
  246. __be16 *ports, _ports[2];
  247. u8 inside_nexthdr;
  248. __be16 inside_fragoff;
  249. int inside_hdrlen;
  250. icmph = skb_header_pointer(skb, outside_hdrlen,
  251. sizeof(_icmph), &_icmph);
  252. if (icmph == NULL)
  253. return 1;
  254. if (icmph->icmp6_type & ICMPV6_INFOMSG_MASK)
  255. return 1;
  256. inside_iph = skb_header_pointer(skb, outside_hdrlen + sizeof(_icmph),
  257. sizeof(*ipv6_var), ipv6_var);
  258. if (inside_iph == NULL)
  259. return 1;
  260. inside_nexthdr = inside_iph->nexthdr;
  261. inside_hdrlen = ipv6_skip_exthdr(skb, outside_hdrlen + sizeof(_icmph) +
  262. sizeof(*ipv6_var),
  263. &inside_nexthdr, &inside_fragoff);
  264. if (inside_hdrlen < 0)
  265. return 1; /* hjm: Packet has no/incomplete transport layer headers. */
  266. if (inside_nexthdr != IPPROTO_TCP &&
  267. inside_nexthdr != IPPROTO_UDP)
  268. return 1;
  269. ports = skb_header_pointer(skb, inside_hdrlen,
  270. sizeof(_ports), &_ports);
  271. if (ports == NULL)
  272. return 1;
  273. /* the inside IP packet is the one quoted from our side, thus
  274. * its saddr is the local address */
  275. *protocol = inside_nexthdr;
  276. *laddr = &inside_iph->saddr;
  277. *lport = ports[0];
  278. *raddr = &inside_iph->daddr;
  279. *rport = ports[1];
  280. return 0;
  281. }
  282. static struct sock *
  283. xt_socket_get_sock_v6(struct net *net, struct sk_buff *skb, int doff,
  284. const u8 protocol,
  285. const struct in6_addr *saddr, const struct in6_addr *daddr,
  286. const __be16 sport, const __be16 dport,
  287. const struct net_device *in)
  288. {
  289. switch (protocol) {
  290. case IPPROTO_TCP:
  291. return inet6_lookup(net, &tcp_hashinfo, skb, doff,
  292. saddr, sport, daddr, dport,
  293. in->ifindex);
  294. case IPPROTO_UDP:
  295. return udp6_lib_lookup(net, saddr, sport, daddr, dport,
  296. in->ifindex);
  297. }
  298. return NULL;
  299. }
  300. static struct sock *xt_socket_lookup_slow_v6(struct net *net,
  301. const struct sk_buff *skb,
  302. const struct net_device *indev)
  303. {
  304. __be16 uninitialized_var(dport), uninitialized_var(sport);
  305. const struct in6_addr *daddr = NULL, *saddr = NULL;
  306. struct ipv6hdr *iph = ipv6_hdr(skb);
  307. struct sk_buff *data_skb = NULL;
  308. int doff = 0;
  309. int thoff = 0, tproto;
  310. tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
  311. if (tproto < 0) {
  312. pr_debug("unable to find transport header in IPv6 packet, dropping\n");
  313. return NULL;
  314. }
  315. if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
  316. struct udphdr _hdr, *hp;
  317. hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
  318. if (hp == NULL)
  319. return NULL;
  320. saddr = &iph->saddr;
  321. sport = hp->source;
  322. daddr = &iph->daddr;
  323. dport = hp->dest;
  324. data_skb = (struct sk_buff *)skb;
  325. doff = tproto == IPPROTO_TCP ?
  326. thoff + __tcp_hdrlen((struct tcphdr *)hp) :
  327. thoff + sizeof(*hp);
  328. } else if (tproto == IPPROTO_ICMPV6) {
  329. struct ipv6hdr ipv6_var;
  330. if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr,
  331. &sport, &dport, &ipv6_var))
  332. return NULL;
  333. } else {
  334. return NULL;
  335. }
  336. return xt_socket_get_sock_v6(net, data_skb, doff, tproto, saddr, daddr,
  337. sport, dport, indev);
  338. }
  339. static bool
  340. socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par)
  341. {
  342. const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
  343. struct sk_buff *pskb = (struct sk_buff *)skb;
  344. struct sock *sk = skb->sk;
  345. if (!sk)
  346. sk = xt_socket_lookup_slow_v6(par->net, skb, par->in);
  347. if (sk) {
  348. bool wildcard;
  349. bool transparent = true;
  350. /* Ignore sockets listening on INADDR_ANY
  351. * unless XT_SOCKET_NOWILDCARD is set
  352. */
  353. wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
  354. sk_fullsock(sk) &&
  355. ipv6_addr_any(&sk->sk_v6_rcv_saddr));
  356. /* Ignore non-transparent sockets,
  357. * if XT_SOCKET_TRANSPARENT is used
  358. */
  359. if (info->flags & XT_SOCKET_TRANSPARENT)
  360. transparent = xt_socket_sk_is_transparent(sk);
  361. if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard &&
  362. transparent)
  363. pskb->mark = sk->sk_mark;
  364. if (sk != skb->sk)
  365. sock_gen_put(sk);
  366. if (wildcard || !transparent)
  367. sk = NULL;
  368. }
  369. return sk != NULL;
  370. }
  371. #endif
  372. static int socket_mt_v1_check(const struct xt_mtchk_param *par)
  373. {
  374. const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
  375. if (info->flags & ~XT_SOCKET_FLAGS_V1) {
  376. pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V1);
  377. return -EINVAL;
  378. }
  379. return 0;
  380. }
  381. static int socket_mt_v2_check(const struct xt_mtchk_param *par)
  382. {
  383. const struct xt_socket_mtinfo2 *info = (struct xt_socket_mtinfo2 *) par->matchinfo;
  384. if (info->flags & ~XT_SOCKET_FLAGS_V2) {
  385. pr_info("unknown flags 0x%x\n", info->flags & ~XT_SOCKET_FLAGS_V2);
  386. return -EINVAL;
  387. }
  388. return 0;
  389. }
  390. static int socket_mt_v3_check(const struct xt_mtchk_param *par)
  391. {
  392. const struct xt_socket_mtinfo3 *info =
  393. (struct xt_socket_mtinfo3 *)par->matchinfo;
  394. if (info->flags & ~XT_SOCKET_FLAGS_V3) {
  395. pr_info("unknown flags 0x%x\n",
  396. info->flags & ~XT_SOCKET_FLAGS_V3);
  397. return -EINVAL;
  398. }
  399. return 0;
  400. }
  401. static struct xt_match socket_mt_reg[] __read_mostly = {
  402. {
  403. .name = "socket",
  404. .revision = 0,
  405. .family = NFPROTO_IPV4,
  406. .match = socket_mt4_v0,
  407. .hooks = (1 << NF_INET_PRE_ROUTING) |
  408. (1 << NF_INET_LOCAL_IN),
  409. .me = THIS_MODULE,
  410. },
  411. {
  412. .name = "socket",
  413. .revision = 1,
  414. .family = NFPROTO_IPV4,
  415. .match = socket_mt4_v1_v2_v3,
  416. .checkentry = socket_mt_v1_check,
  417. .matchsize = sizeof(struct xt_socket_mtinfo1),
  418. .hooks = (1 << NF_INET_PRE_ROUTING) |
  419. (1 << NF_INET_LOCAL_IN),
  420. .me = THIS_MODULE,
  421. },
  422. #ifdef XT_SOCKET_HAVE_IPV6
  423. {
  424. .name = "socket",
  425. .revision = 1,
  426. .family = NFPROTO_IPV6,
  427. .match = socket_mt6_v1_v2_v3,
  428. .checkentry = socket_mt_v1_check,
  429. .matchsize = sizeof(struct xt_socket_mtinfo1),
  430. .hooks = (1 << NF_INET_PRE_ROUTING) |
  431. (1 << NF_INET_LOCAL_IN),
  432. .me = THIS_MODULE,
  433. },
  434. #endif
  435. {
  436. .name = "socket",
  437. .revision = 2,
  438. .family = NFPROTO_IPV4,
  439. .match = socket_mt4_v1_v2_v3,
  440. .checkentry = socket_mt_v2_check,
  441. .matchsize = sizeof(struct xt_socket_mtinfo1),
  442. .hooks = (1 << NF_INET_PRE_ROUTING) |
  443. (1 << NF_INET_LOCAL_IN),
  444. .me = THIS_MODULE,
  445. },
  446. #ifdef XT_SOCKET_HAVE_IPV6
  447. {
  448. .name = "socket",
  449. .revision = 2,
  450. .family = NFPROTO_IPV6,
  451. .match = socket_mt6_v1_v2_v3,
  452. .checkentry = socket_mt_v2_check,
  453. .matchsize = sizeof(struct xt_socket_mtinfo1),
  454. .hooks = (1 << NF_INET_PRE_ROUTING) |
  455. (1 << NF_INET_LOCAL_IN),
  456. .me = THIS_MODULE,
  457. },
  458. #endif
  459. {
  460. .name = "socket",
  461. .revision = 3,
  462. .family = NFPROTO_IPV4,
  463. .match = socket_mt4_v1_v2_v3,
  464. .checkentry = socket_mt_v3_check,
  465. .matchsize = sizeof(struct xt_socket_mtinfo1),
  466. .hooks = (1 << NF_INET_PRE_ROUTING) |
  467. (1 << NF_INET_LOCAL_IN),
  468. .me = THIS_MODULE,
  469. },
  470. #ifdef XT_SOCKET_HAVE_IPV6
  471. {
  472. .name = "socket",
  473. .revision = 3,
  474. .family = NFPROTO_IPV6,
  475. .match = socket_mt6_v1_v2_v3,
  476. .checkentry = socket_mt_v3_check,
  477. .matchsize = sizeof(struct xt_socket_mtinfo1),
  478. .hooks = (1 << NF_INET_PRE_ROUTING) |
  479. (1 << NF_INET_LOCAL_IN),
  480. .me = THIS_MODULE,
  481. },
  482. #endif
  483. };
  484. static int __init socket_mt_init(void)
  485. {
  486. nf_defrag_ipv4_enable();
  487. #ifdef XT_SOCKET_HAVE_IPV6
  488. nf_defrag_ipv6_enable();
  489. #endif
  490. return xt_register_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
  491. }
  492. static void __exit socket_mt_exit(void)
  493. {
  494. xt_unregister_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg));
  495. }
  496. module_init(socket_mt_init);
  497. module_exit(socket_mt_exit);
  498. MODULE_LICENSE("GPL");
  499. MODULE_AUTHOR("Krisztian Kovacs, Balazs Scheidler");
  500. MODULE_DESCRIPTION("x_tables socket match module");
  501. MODULE_ALIAS("ipt_socket");
  502. MODULE_ALIAS("ip6t_socket");