xt_socket.c 14 KB

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