/net/ipv4/netfilter/ip_tables.c

http://github.com/mirrors/linux · C · 1963 lines · 1572 code · 299 blank · 92 comment · 270 complexity · b338167c00eebe6d1fa03d377872997f MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Packet matching code.
  4. *
  5. * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
  6. * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
  7. * Copyright (C) 2006-2010 Patrick McHardy <kaber@trash.net>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/cache.h>
  11. #include <linux/capability.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/kmod.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/module.h>
  17. #include <linux/icmp.h>
  18. #include <net/ip.h>
  19. #include <net/compat.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/mutex.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/err.h>
  24. #include <linux/cpumask.h>
  25. #include <linux/netfilter/x_tables.h>
  26. #include <linux/netfilter_ipv4/ip_tables.h>
  27. #include <net/netfilter/nf_log.h>
  28. #include "../../netfilter/xt_repldata.h"
  29. MODULE_LICENSE("GPL");
  30. MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  31. MODULE_DESCRIPTION("IPv4 packet filter");
  32. MODULE_ALIAS("ipt_icmp");
  33. void *ipt_alloc_initial_table(const struct xt_table *info)
  34. {
  35. return xt_alloc_initial_table(ipt, IPT);
  36. }
  37. EXPORT_SYMBOL_GPL(ipt_alloc_initial_table);
  38. /* Returns whether matches rule or not. */
  39. /* Performance critical - called for every packet */
  40. static inline bool
  41. ip_packet_match(const struct iphdr *ip,
  42. const char *indev,
  43. const char *outdev,
  44. const struct ipt_ip *ipinfo,
  45. int isfrag)
  46. {
  47. unsigned long ret;
  48. if (NF_INVF(ipinfo, IPT_INV_SRCIP,
  49. (ip->saddr & ipinfo->smsk.s_addr) != ipinfo->src.s_addr) ||
  50. NF_INVF(ipinfo, IPT_INV_DSTIP,
  51. (ip->daddr & ipinfo->dmsk.s_addr) != ipinfo->dst.s_addr))
  52. return false;
  53. ret = ifname_compare_aligned(indev, ipinfo->iniface, ipinfo->iniface_mask);
  54. if (NF_INVF(ipinfo, IPT_INV_VIA_IN, ret != 0))
  55. return false;
  56. ret = ifname_compare_aligned(outdev, ipinfo->outiface, ipinfo->outiface_mask);
  57. if (NF_INVF(ipinfo, IPT_INV_VIA_OUT, ret != 0))
  58. return false;
  59. /* Check specific protocol */
  60. if (ipinfo->proto &&
  61. NF_INVF(ipinfo, IPT_INV_PROTO, ip->protocol != ipinfo->proto))
  62. return false;
  63. /* If we have a fragment rule but the packet is not a fragment
  64. * then we return zero */
  65. if (NF_INVF(ipinfo, IPT_INV_FRAG,
  66. (ipinfo->flags & IPT_F_FRAG) && !isfrag))
  67. return false;
  68. return true;
  69. }
  70. static bool
  71. ip_checkentry(const struct ipt_ip *ip)
  72. {
  73. if (ip->flags & ~IPT_F_MASK)
  74. return false;
  75. if (ip->invflags & ~IPT_INV_MASK)
  76. return false;
  77. return true;
  78. }
  79. static unsigned int
  80. ipt_error(struct sk_buff *skb, const struct xt_action_param *par)
  81. {
  82. net_info_ratelimited("error: `%s'\n", (const char *)par->targinfo);
  83. return NF_DROP;
  84. }
  85. /* Performance critical */
  86. static inline struct ipt_entry *
  87. get_entry(const void *base, unsigned int offset)
  88. {
  89. return (struct ipt_entry *)(base + offset);
  90. }
  91. /* All zeroes == unconditional rule. */
  92. /* Mildly perf critical (only if packet tracing is on) */
  93. static inline bool unconditional(const struct ipt_entry *e)
  94. {
  95. static const struct ipt_ip uncond;
  96. return e->target_offset == sizeof(struct ipt_entry) &&
  97. memcmp(&e->ip, &uncond, sizeof(uncond)) == 0;
  98. }
  99. /* for const-correctness */
  100. static inline const struct xt_entry_target *
  101. ipt_get_target_c(const struct ipt_entry *e)
  102. {
  103. return ipt_get_target((struct ipt_entry *)e);
  104. }
  105. #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
  106. static const char *const hooknames[] = {
  107. [NF_INET_PRE_ROUTING] = "PREROUTING",
  108. [NF_INET_LOCAL_IN] = "INPUT",
  109. [NF_INET_FORWARD] = "FORWARD",
  110. [NF_INET_LOCAL_OUT] = "OUTPUT",
  111. [NF_INET_POST_ROUTING] = "POSTROUTING",
  112. };
  113. enum nf_ip_trace_comments {
  114. NF_IP_TRACE_COMMENT_RULE,
  115. NF_IP_TRACE_COMMENT_RETURN,
  116. NF_IP_TRACE_COMMENT_POLICY,
  117. };
  118. static const char *const comments[] = {
  119. [NF_IP_TRACE_COMMENT_RULE] = "rule",
  120. [NF_IP_TRACE_COMMENT_RETURN] = "return",
  121. [NF_IP_TRACE_COMMENT_POLICY] = "policy",
  122. };
  123. static const struct nf_loginfo trace_loginfo = {
  124. .type = NF_LOG_TYPE_LOG,
  125. .u = {
  126. .log = {
  127. .level = 4,
  128. .logflags = NF_LOG_DEFAULT_MASK,
  129. },
  130. },
  131. };
  132. /* Mildly perf critical (only if packet tracing is on) */
  133. static inline int
  134. get_chainname_rulenum(const struct ipt_entry *s, const struct ipt_entry *e,
  135. const char *hookname, const char **chainname,
  136. const char **comment, unsigned int *rulenum)
  137. {
  138. const struct xt_standard_target *t = (void *)ipt_get_target_c(s);
  139. if (strcmp(t->target.u.kernel.target->name, XT_ERROR_TARGET) == 0) {
  140. /* Head of user chain: ERROR target with chainname */
  141. *chainname = t->target.data;
  142. (*rulenum) = 0;
  143. } else if (s == e) {
  144. (*rulenum)++;
  145. if (unconditional(s) &&
  146. strcmp(t->target.u.kernel.target->name,
  147. XT_STANDARD_TARGET) == 0 &&
  148. t->verdict < 0) {
  149. /* Tail of chains: STANDARD target (return/policy) */
  150. *comment = *chainname == hookname
  151. ? comments[NF_IP_TRACE_COMMENT_POLICY]
  152. : comments[NF_IP_TRACE_COMMENT_RETURN];
  153. }
  154. return 1;
  155. } else
  156. (*rulenum)++;
  157. return 0;
  158. }
  159. static void trace_packet(struct net *net,
  160. const struct sk_buff *skb,
  161. unsigned int hook,
  162. const struct net_device *in,
  163. const struct net_device *out,
  164. const char *tablename,
  165. const struct xt_table_info *private,
  166. const struct ipt_entry *e)
  167. {
  168. const struct ipt_entry *root;
  169. const char *hookname, *chainname, *comment;
  170. const struct ipt_entry *iter;
  171. unsigned int rulenum = 0;
  172. root = get_entry(private->entries, private->hook_entry[hook]);
  173. hookname = chainname = hooknames[hook];
  174. comment = comments[NF_IP_TRACE_COMMENT_RULE];
  175. xt_entry_foreach(iter, root, private->size - private->hook_entry[hook])
  176. if (get_chainname_rulenum(iter, e, hookname,
  177. &chainname, &comment, &rulenum) != 0)
  178. break;
  179. nf_log_trace(net, AF_INET, hook, skb, in, out, &trace_loginfo,
  180. "TRACE: %s:%s:%s:%u ",
  181. tablename, chainname, comment, rulenum);
  182. }
  183. #endif
  184. static inline
  185. struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
  186. {
  187. return (void *)entry + entry->next_offset;
  188. }
  189. /* Returns one of the generic firewall policies, like NF_ACCEPT. */
  190. unsigned int
  191. ipt_do_table(struct sk_buff *skb,
  192. const struct nf_hook_state *state,
  193. struct xt_table *table)
  194. {
  195. unsigned int hook = state->hook;
  196. static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
  197. const struct iphdr *ip;
  198. /* Initializing verdict to NF_DROP keeps gcc happy. */
  199. unsigned int verdict = NF_DROP;
  200. const char *indev, *outdev;
  201. const void *table_base;
  202. struct ipt_entry *e, **jumpstack;
  203. unsigned int stackidx, cpu;
  204. const struct xt_table_info *private;
  205. struct xt_action_param acpar;
  206. unsigned int addend;
  207. /* Initialization */
  208. stackidx = 0;
  209. ip = ip_hdr(skb);
  210. indev = state->in ? state->in->name : nulldevname;
  211. outdev = state->out ? state->out->name : nulldevname;
  212. /* We handle fragments by dealing with the first fragment as
  213. * if it was a normal packet. All other fragments are treated
  214. * normally, except that they will NEVER match rules that ask
  215. * things we don't know, ie. tcp syn flag or ports). If the
  216. * rule is also a fragment-specific rule, non-fragments won't
  217. * match it. */
  218. acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
  219. acpar.thoff = ip_hdrlen(skb);
  220. acpar.hotdrop = false;
  221. acpar.state = state;
  222. WARN_ON(!(table->valid_hooks & (1 << hook)));
  223. local_bh_disable();
  224. addend = xt_write_recseq_begin();
  225. private = READ_ONCE(table->private); /* Address dependency. */
  226. cpu = smp_processor_id();
  227. table_base = private->entries;
  228. jumpstack = (struct ipt_entry **)private->jumpstack[cpu];
  229. /* Switch to alternate jumpstack if we're being invoked via TEE.
  230. * TEE issues XT_CONTINUE verdict on original skb so we must not
  231. * clobber the jumpstack.
  232. *
  233. * For recursion via REJECT or SYNPROXY the stack will be clobbered
  234. * but it is no problem since absolute verdict is issued by these.
  235. */
  236. if (static_key_false(&xt_tee_enabled))
  237. jumpstack += private->stacksize * __this_cpu_read(nf_skb_duplicated);
  238. e = get_entry(table_base, private->hook_entry[hook]);
  239. do {
  240. const struct xt_entry_target *t;
  241. const struct xt_entry_match *ematch;
  242. struct xt_counters *counter;
  243. WARN_ON(!e);
  244. if (!ip_packet_match(ip, indev, outdev,
  245. &e->ip, acpar.fragoff)) {
  246. no_match:
  247. e = ipt_next_entry(e);
  248. continue;
  249. }
  250. xt_ematch_foreach(ematch, e) {
  251. acpar.match = ematch->u.kernel.match;
  252. acpar.matchinfo = ematch->data;
  253. if (!acpar.match->match(skb, &acpar))
  254. goto no_match;
  255. }
  256. counter = xt_get_this_cpu_counter(&e->counters);
  257. ADD_COUNTER(*counter, skb->len, 1);
  258. t = ipt_get_target_c(e);
  259. WARN_ON(!t->u.kernel.target);
  260. #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
  261. /* The packet is traced: log it */
  262. if (unlikely(skb->nf_trace))
  263. trace_packet(state->net, skb, hook, state->in,
  264. state->out, table->name, private, e);
  265. #endif
  266. /* Standard target? */
  267. if (!t->u.kernel.target->target) {
  268. int v;
  269. v = ((struct xt_standard_target *)t)->verdict;
  270. if (v < 0) {
  271. /* Pop from stack? */
  272. if (v != XT_RETURN) {
  273. verdict = (unsigned int)(-v) - 1;
  274. break;
  275. }
  276. if (stackidx == 0) {
  277. e = get_entry(table_base,
  278. private->underflow[hook]);
  279. } else {
  280. e = jumpstack[--stackidx];
  281. e = ipt_next_entry(e);
  282. }
  283. continue;
  284. }
  285. if (table_base + v != ipt_next_entry(e) &&
  286. !(e->ip.flags & IPT_F_GOTO)) {
  287. if (unlikely(stackidx >= private->stacksize)) {
  288. verdict = NF_DROP;
  289. break;
  290. }
  291. jumpstack[stackidx++] = e;
  292. }
  293. e = get_entry(table_base, v);
  294. continue;
  295. }
  296. acpar.target = t->u.kernel.target;
  297. acpar.targinfo = t->data;
  298. verdict = t->u.kernel.target->target(skb, &acpar);
  299. if (verdict == XT_CONTINUE) {
  300. /* Target might have changed stuff. */
  301. ip = ip_hdr(skb);
  302. e = ipt_next_entry(e);
  303. } else {
  304. /* Verdict */
  305. break;
  306. }
  307. } while (!acpar.hotdrop);
  308. xt_write_recseq_end(addend);
  309. local_bh_enable();
  310. if (acpar.hotdrop)
  311. return NF_DROP;
  312. else return verdict;
  313. }
  314. /* Figures out from what hook each rule can be called: returns 0 if
  315. there are loops. Puts hook bitmask in comefrom. */
  316. static int
  317. mark_source_chains(const struct xt_table_info *newinfo,
  318. unsigned int valid_hooks, void *entry0,
  319. unsigned int *offsets)
  320. {
  321. unsigned int hook;
  322. /* No recursion; use packet counter to save back ptrs (reset
  323. to 0 as we leave), and comefrom to save source hook bitmask */
  324. for (hook = 0; hook < NF_INET_NUMHOOKS; hook++) {
  325. unsigned int pos = newinfo->hook_entry[hook];
  326. struct ipt_entry *e = entry0 + pos;
  327. if (!(valid_hooks & (1 << hook)))
  328. continue;
  329. /* Set initial back pointer. */
  330. e->counters.pcnt = pos;
  331. for (;;) {
  332. const struct xt_standard_target *t
  333. = (void *)ipt_get_target_c(e);
  334. int visited = e->comefrom & (1 << hook);
  335. if (e->comefrom & (1 << NF_INET_NUMHOOKS))
  336. return 0;
  337. e->comefrom |= ((1 << hook) | (1 << NF_INET_NUMHOOKS));
  338. /* Unconditional return/END. */
  339. if ((unconditional(e) &&
  340. (strcmp(t->target.u.user.name,
  341. XT_STANDARD_TARGET) == 0) &&
  342. t->verdict < 0) || visited) {
  343. unsigned int oldpos, size;
  344. /* Return: backtrack through the last
  345. big jump. */
  346. do {
  347. e->comefrom ^= (1<<NF_INET_NUMHOOKS);
  348. oldpos = pos;
  349. pos = e->counters.pcnt;
  350. e->counters.pcnt = 0;
  351. /* We're at the start. */
  352. if (pos == oldpos)
  353. goto next;
  354. e = entry0 + pos;
  355. } while (oldpos == pos + e->next_offset);
  356. /* Move along one */
  357. size = e->next_offset;
  358. e = entry0 + pos + size;
  359. if (pos + size >= newinfo->size)
  360. return 0;
  361. e->counters.pcnt = pos;
  362. pos += size;
  363. } else {
  364. int newpos = t->verdict;
  365. if (strcmp(t->target.u.user.name,
  366. XT_STANDARD_TARGET) == 0 &&
  367. newpos >= 0) {
  368. /* This a jump; chase it. */
  369. if (!xt_find_jump_offset(offsets, newpos,
  370. newinfo->number))
  371. return 0;
  372. } else {
  373. /* ... this is a fallthru */
  374. newpos = pos + e->next_offset;
  375. if (newpos >= newinfo->size)
  376. return 0;
  377. }
  378. e = entry0 + newpos;
  379. e->counters.pcnt = pos;
  380. pos = newpos;
  381. }
  382. }
  383. next: ;
  384. }
  385. return 1;
  386. }
  387. static void cleanup_match(struct xt_entry_match *m, struct net *net)
  388. {
  389. struct xt_mtdtor_param par;
  390. par.net = net;
  391. par.match = m->u.kernel.match;
  392. par.matchinfo = m->data;
  393. par.family = NFPROTO_IPV4;
  394. if (par.match->destroy != NULL)
  395. par.match->destroy(&par);
  396. module_put(par.match->me);
  397. }
  398. static int
  399. check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
  400. {
  401. const struct ipt_ip *ip = par->entryinfo;
  402. par->match = m->u.kernel.match;
  403. par->matchinfo = m->data;
  404. return xt_check_match(par, m->u.match_size - sizeof(*m),
  405. ip->proto, ip->invflags & IPT_INV_PROTO);
  406. }
  407. static int
  408. find_check_match(struct xt_entry_match *m, struct xt_mtchk_param *par)
  409. {
  410. struct xt_match *match;
  411. int ret;
  412. match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
  413. m->u.user.revision);
  414. if (IS_ERR(match))
  415. return PTR_ERR(match);
  416. m->u.kernel.match = match;
  417. ret = check_match(m, par);
  418. if (ret)
  419. goto err;
  420. return 0;
  421. err:
  422. module_put(m->u.kernel.match->me);
  423. return ret;
  424. }
  425. static int check_target(struct ipt_entry *e, struct net *net, const char *name)
  426. {
  427. struct xt_entry_target *t = ipt_get_target(e);
  428. struct xt_tgchk_param par = {
  429. .net = net,
  430. .table = name,
  431. .entryinfo = e,
  432. .target = t->u.kernel.target,
  433. .targinfo = t->data,
  434. .hook_mask = e->comefrom,
  435. .family = NFPROTO_IPV4,
  436. };
  437. return xt_check_target(&par, t->u.target_size - sizeof(*t),
  438. e->ip.proto, e->ip.invflags & IPT_INV_PROTO);
  439. }
  440. static int
  441. find_check_entry(struct ipt_entry *e, struct net *net, const char *name,
  442. unsigned int size,
  443. struct xt_percpu_counter_alloc_state *alloc_state)
  444. {
  445. struct xt_entry_target *t;
  446. struct xt_target *target;
  447. int ret;
  448. unsigned int j;
  449. struct xt_mtchk_param mtpar;
  450. struct xt_entry_match *ematch;
  451. if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
  452. return -ENOMEM;
  453. j = 0;
  454. memset(&mtpar, 0, sizeof(mtpar));
  455. mtpar.net = net;
  456. mtpar.table = name;
  457. mtpar.entryinfo = &e->ip;
  458. mtpar.hook_mask = e->comefrom;
  459. mtpar.family = NFPROTO_IPV4;
  460. xt_ematch_foreach(ematch, e) {
  461. ret = find_check_match(ematch, &mtpar);
  462. if (ret != 0)
  463. goto cleanup_matches;
  464. ++j;
  465. }
  466. t = ipt_get_target(e);
  467. target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
  468. t->u.user.revision);
  469. if (IS_ERR(target)) {
  470. ret = PTR_ERR(target);
  471. goto cleanup_matches;
  472. }
  473. t->u.kernel.target = target;
  474. ret = check_target(e, net, name);
  475. if (ret)
  476. goto err;
  477. return 0;
  478. err:
  479. module_put(t->u.kernel.target->me);
  480. cleanup_matches:
  481. xt_ematch_foreach(ematch, e) {
  482. if (j-- == 0)
  483. break;
  484. cleanup_match(ematch, net);
  485. }
  486. xt_percpu_counter_free(&e->counters);
  487. return ret;
  488. }
  489. static bool check_underflow(const struct ipt_entry *e)
  490. {
  491. const struct xt_entry_target *t;
  492. unsigned int verdict;
  493. if (!unconditional(e))
  494. return false;
  495. t = ipt_get_target_c(e);
  496. if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
  497. return false;
  498. verdict = ((struct xt_standard_target *)t)->verdict;
  499. verdict = -verdict - 1;
  500. return verdict == NF_DROP || verdict == NF_ACCEPT;
  501. }
  502. static int
  503. check_entry_size_and_hooks(struct ipt_entry *e,
  504. struct xt_table_info *newinfo,
  505. const unsigned char *base,
  506. const unsigned char *limit,
  507. const unsigned int *hook_entries,
  508. const unsigned int *underflows,
  509. unsigned int valid_hooks)
  510. {
  511. unsigned int h;
  512. int err;
  513. if ((unsigned long)e % __alignof__(struct ipt_entry) != 0 ||
  514. (unsigned char *)e + sizeof(struct ipt_entry) >= limit ||
  515. (unsigned char *)e + e->next_offset > limit)
  516. return -EINVAL;
  517. if (e->next_offset
  518. < sizeof(struct ipt_entry) + sizeof(struct xt_entry_target))
  519. return -EINVAL;
  520. if (!ip_checkentry(&e->ip))
  521. return -EINVAL;
  522. err = xt_check_entry_offsets(e, e->elems, e->target_offset,
  523. e->next_offset);
  524. if (err)
  525. return err;
  526. /* Check hooks & underflows */
  527. for (h = 0; h < NF_INET_NUMHOOKS; h++) {
  528. if (!(valid_hooks & (1 << h)))
  529. continue;
  530. if ((unsigned char *)e - base == hook_entries[h])
  531. newinfo->hook_entry[h] = hook_entries[h];
  532. if ((unsigned char *)e - base == underflows[h]) {
  533. if (!check_underflow(e))
  534. return -EINVAL;
  535. newinfo->underflow[h] = underflows[h];
  536. }
  537. }
  538. /* Clear counters and comefrom */
  539. e->counters = ((struct xt_counters) { 0, 0 });
  540. e->comefrom = 0;
  541. return 0;
  542. }
  543. static void
  544. cleanup_entry(struct ipt_entry *e, struct net *net)
  545. {
  546. struct xt_tgdtor_param par;
  547. struct xt_entry_target *t;
  548. struct xt_entry_match *ematch;
  549. /* Cleanup all matches */
  550. xt_ematch_foreach(ematch, e)
  551. cleanup_match(ematch, net);
  552. t = ipt_get_target(e);
  553. par.net = net;
  554. par.target = t->u.kernel.target;
  555. par.targinfo = t->data;
  556. par.family = NFPROTO_IPV4;
  557. if (par.target->destroy != NULL)
  558. par.target->destroy(&par);
  559. module_put(par.target->me);
  560. xt_percpu_counter_free(&e->counters);
  561. }
  562. /* Checks and translates the user-supplied table segment (held in
  563. newinfo) */
  564. static int
  565. translate_table(struct net *net, struct xt_table_info *newinfo, void *entry0,
  566. const struct ipt_replace *repl)
  567. {
  568. struct xt_percpu_counter_alloc_state alloc_state = { 0 };
  569. struct ipt_entry *iter;
  570. unsigned int *offsets;
  571. unsigned int i;
  572. int ret = 0;
  573. newinfo->size = repl->size;
  574. newinfo->number = repl->num_entries;
  575. /* Init all hooks to impossible value. */
  576. for (i = 0; i < NF_INET_NUMHOOKS; i++) {
  577. newinfo->hook_entry[i] = 0xFFFFFFFF;
  578. newinfo->underflow[i] = 0xFFFFFFFF;
  579. }
  580. offsets = xt_alloc_entry_offsets(newinfo->number);
  581. if (!offsets)
  582. return -ENOMEM;
  583. i = 0;
  584. /* Walk through entries, checking offsets. */
  585. xt_entry_foreach(iter, entry0, newinfo->size) {
  586. ret = check_entry_size_and_hooks(iter, newinfo, entry0,
  587. entry0 + repl->size,
  588. repl->hook_entry,
  589. repl->underflow,
  590. repl->valid_hooks);
  591. if (ret != 0)
  592. goto out_free;
  593. if (i < repl->num_entries)
  594. offsets[i] = (void *)iter - entry0;
  595. ++i;
  596. if (strcmp(ipt_get_target(iter)->u.user.name,
  597. XT_ERROR_TARGET) == 0)
  598. ++newinfo->stacksize;
  599. }
  600. ret = -EINVAL;
  601. if (i != repl->num_entries)
  602. goto out_free;
  603. ret = xt_check_table_hooks(newinfo, repl->valid_hooks);
  604. if (ret)
  605. goto out_free;
  606. if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
  607. ret = -ELOOP;
  608. goto out_free;
  609. }
  610. kvfree(offsets);
  611. /* Finally, each sanity check must pass */
  612. i = 0;
  613. xt_entry_foreach(iter, entry0, newinfo->size) {
  614. ret = find_check_entry(iter, net, repl->name, repl->size,
  615. &alloc_state);
  616. if (ret != 0)
  617. break;
  618. ++i;
  619. }
  620. if (ret != 0) {
  621. xt_entry_foreach(iter, entry0, newinfo->size) {
  622. if (i-- == 0)
  623. break;
  624. cleanup_entry(iter, net);
  625. }
  626. return ret;
  627. }
  628. return ret;
  629. out_free:
  630. kvfree(offsets);
  631. return ret;
  632. }
  633. static void
  634. get_counters(const struct xt_table_info *t,
  635. struct xt_counters counters[])
  636. {
  637. struct ipt_entry *iter;
  638. unsigned int cpu;
  639. unsigned int i;
  640. for_each_possible_cpu(cpu) {
  641. seqcount_t *s = &per_cpu(xt_recseq, cpu);
  642. i = 0;
  643. xt_entry_foreach(iter, t->entries, t->size) {
  644. struct xt_counters *tmp;
  645. u64 bcnt, pcnt;
  646. unsigned int start;
  647. tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
  648. do {
  649. start = read_seqcount_begin(s);
  650. bcnt = tmp->bcnt;
  651. pcnt = tmp->pcnt;
  652. } while (read_seqcount_retry(s, start));
  653. ADD_COUNTER(counters[i], bcnt, pcnt);
  654. ++i; /* macro does multi eval of i */
  655. cond_resched();
  656. }
  657. }
  658. }
  659. static void get_old_counters(const struct xt_table_info *t,
  660. struct xt_counters counters[])
  661. {
  662. struct ipt_entry *iter;
  663. unsigned int cpu, i;
  664. for_each_possible_cpu(cpu) {
  665. i = 0;
  666. xt_entry_foreach(iter, t->entries, t->size) {
  667. const struct xt_counters *tmp;
  668. tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
  669. ADD_COUNTER(counters[i], tmp->bcnt, tmp->pcnt);
  670. ++i; /* macro does multi eval of i */
  671. }
  672. cond_resched();
  673. }
  674. }
  675. static struct xt_counters *alloc_counters(const struct xt_table *table)
  676. {
  677. unsigned int countersize;
  678. struct xt_counters *counters;
  679. const struct xt_table_info *private = table->private;
  680. /* We need atomic snapshot of counters: rest doesn't change
  681. (other than comefrom, which userspace doesn't care
  682. about). */
  683. countersize = sizeof(struct xt_counters) * private->number;
  684. counters = vzalloc(countersize);
  685. if (counters == NULL)
  686. return ERR_PTR(-ENOMEM);
  687. get_counters(private, counters);
  688. return counters;
  689. }
  690. static int
  691. copy_entries_to_user(unsigned int total_size,
  692. const struct xt_table *table,
  693. void __user *userptr)
  694. {
  695. unsigned int off, num;
  696. const struct ipt_entry *e;
  697. struct xt_counters *counters;
  698. const struct xt_table_info *private = table->private;
  699. int ret = 0;
  700. const void *loc_cpu_entry;
  701. counters = alloc_counters(table);
  702. if (IS_ERR(counters))
  703. return PTR_ERR(counters);
  704. loc_cpu_entry = private->entries;
  705. /* FIXME: use iterator macros --RR */
  706. /* ... then go back and fix counters and names */
  707. for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
  708. unsigned int i;
  709. const struct xt_entry_match *m;
  710. const struct xt_entry_target *t;
  711. e = loc_cpu_entry + off;
  712. if (copy_to_user(userptr + off, e, sizeof(*e))) {
  713. ret = -EFAULT;
  714. goto free_counters;
  715. }
  716. if (copy_to_user(userptr + off
  717. + offsetof(struct ipt_entry, counters),
  718. &counters[num],
  719. sizeof(counters[num])) != 0) {
  720. ret = -EFAULT;
  721. goto free_counters;
  722. }
  723. for (i = sizeof(struct ipt_entry);
  724. i < e->target_offset;
  725. i += m->u.match_size) {
  726. m = (void *)e + i;
  727. if (xt_match_to_user(m, userptr + off + i)) {
  728. ret = -EFAULT;
  729. goto free_counters;
  730. }
  731. }
  732. t = ipt_get_target_c(e);
  733. if (xt_target_to_user(t, userptr + off + e->target_offset)) {
  734. ret = -EFAULT;
  735. goto free_counters;
  736. }
  737. }
  738. free_counters:
  739. vfree(counters);
  740. return ret;
  741. }
  742. #ifdef CONFIG_COMPAT
  743. static void compat_standard_from_user(void *dst, const void *src)
  744. {
  745. int v = *(compat_int_t *)src;
  746. if (v > 0)
  747. v += xt_compat_calc_jump(AF_INET, v);
  748. memcpy(dst, &v, sizeof(v));
  749. }
  750. static int compat_standard_to_user(void __user *dst, const void *src)
  751. {
  752. compat_int_t cv = *(int *)src;
  753. if (cv > 0)
  754. cv -= xt_compat_calc_jump(AF_INET, cv);
  755. return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
  756. }
  757. static int compat_calc_entry(const struct ipt_entry *e,
  758. const struct xt_table_info *info,
  759. const void *base, struct xt_table_info *newinfo)
  760. {
  761. const struct xt_entry_match *ematch;
  762. const struct xt_entry_target *t;
  763. unsigned int entry_offset;
  764. int off, i, ret;
  765. off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
  766. entry_offset = (void *)e - base;
  767. xt_ematch_foreach(ematch, e)
  768. off += xt_compat_match_offset(ematch->u.kernel.match);
  769. t = ipt_get_target_c(e);
  770. off += xt_compat_target_offset(t->u.kernel.target);
  771. newinfo->size -= off;
  772. ret = xt_compat_add_offset(AF_INET, entry_offset, off);
  773. if (ret)
  774. return ret;
  775. for (i = 0; i < NF_INET_NUMHOOKS; i++) {
  776. if (info->hook_entry[i] &&
  777. (e < (struct ipt_entry *)(base + info->hook_entry[i])))
  778. newinfo->hook_entry[i] -= off;
  779. if (info->underflow[i] &&
  780. (e < (struct ipt_entry *)(base + info->underflow[i])))
  781. newinfo->underflow[i] -= off;
  782. }
  783. return 0;
  784. }
  785. static int compat_table_info(const struct xt_table_info *info,
  786. struct xt_table_info *newinfo)
  787. {
  788. struct ipt_entry *iter;
  789. const void *loc_cpu_entry;
  790. int ret;
  791. if (!newinfo || !info)
  792. return -EINVAL;
  793. /* we dont care about newinfo->entries */
  794. memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
  795. newinfo->initial_entries = 0;
  796. loc_cpu_entry = info->entries;
  797. ret = xt_compat_init_offsets(AF_INET, info->number);
  798. if (ret)
  799. return ret;
  800. xt_entry_foreach(iter, loc_cpu_entry, info->size) {
  801. ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
  802. if (ret != 0)
  803. return ret;
  804. }
  805. return 0;
  806. }
  807. #endif
  808. static int get_info(struct net *net, void __user *user,
  809. const int *len, int compat)
  810. {
  811. char name[XT_TABLE_MAXNAMELEN];
  812. struct xt_table *t;
  813. int ret;
  814. if (*len != sizeof(struct ipt_getinfo))
  815. return -EINVAL;
  816. if (copy_from_user(name, user, sizeof(name)) != 0)
  817. return -EFAULT;
  818. name[XT_TABLE_MAXNAMELEN-1] = '\0';
  819. #ifdef CONFIG_COMPAT
  820. if (compat)
  821. xt_compat_lock(AF_INET);
  822. #endif
  823. t = xt_request_find_table_lock(net, AF_INET, name);
  824. if (!IS_ERR(t)) {
  825. struct ipt_getinfo info;
  826. const struct xt_table_info *private = t->private;
  827. #ifdef CONFIG_COMPAT
  828. struct xt_table_info tmp;
  829. if (compat) {
  830. ret = compat_table_info(private, &tmp);
  831. xt_compat_flush_offsets(AF_INET);
  832. private = &tmp;
  833. }
  834. #endif
  835. memset(&info, 0, sizeof(info));
  836. info.valid_hooks = t->valid_hooks;
  837. memcpy(info.hook_entry, private->hook_entry,
  838. sizeof(info.hook_entry));
  839. memcpy(info.underflow, private->underflow,
  840. sizeof(info.underflow));
  841. info.num_entries = private->number;
  842. info.size = private->size;
  843. strcpy(info.name, name);
  844. if (copy_to_user(user, &info, *len) != 0)
  845. ret = -EFAULT;
  846. else
  847. ret = 0;
  848. xt_table_unlock(t);
  849. module_put(t->me);
  850. } else
  851. ret = PTR_ERR(t);
  852. #ifdef CONFIG_COMPAT
  853. if (compat)
  854. xt_compat_unlock(AF_INET);
  855. #endif
  856. return ret;
  857. }
  858. static int
  859. get_entries(struct net *net, struct ipt_get_entries __user *uptr,
  860. const int *len)
  861. {
  862. int ret;
  863. struct ipt_get_entries get;
  864. struct xt_table *t;
  865. if (*len < sizeof(get))
  866. return -EINVAL;
  867. if (copy_from_user(&get, uptr, sizeof(get)) != 0)
  868. return -EFAULT;
  869. if (*len != sizeof(struct ipt_get_entries) + get.size)
  870. return -EINVAL;
  871. get.name[sizeof(get.name) - 1] = '\0';
  872. t = xt_find_table_lock(net, AF_INET, get.name);
  873. if (!IS_ERR(t)) {
  874. const struct xt_table_info *private = t->private;
  875. if (get.size == private->size)
  876. ret = copy_entries_to_user(private->size,
  877. t, uptr->entrytable);
  878. else
  879. ret = -EAGAIN;
  880. module_put(t->me);
  881. xt_table_unlock(t);
  882. } else
  883. ret = PTR_ERR(t);
  884. return ret;
  885. }
  886. static int
  887. __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
  888. struct xt_table_info *newinfo, unsigned int num_counters,
  889. void __user *counters_ptr)
  890. {
  891. int ret;
  892. struct xt_table *t;
  893. struct xt_table_info *oldinfo;
  894. struct xt_counters *counters;
  895. struct ipt_entry *iter;
  896. ret = 0;
  897. counters = xt_counters_alloc(num_counters);
  898. if (!counters) {
  899. ret = -ENOMEM;
  900. goto out;
  901. }
  902. t = xt_request_find_table_lock(net, AF_INET, name);
  903. if (IS_ERR(t)) {
  904. ret = PTR_ERR(t);
  905. goto free_newinfo_counters_untrans;
  906. }
  907. /* You lied! */
  908. if (valid_hooks != t->valid_hooks) {
  909. ret = -EINVAL;
  910. goto put_module;
  911. }
  912. oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
  913. if (!oldinfo)
  914. goto put_module;
  915. /* Update module usage count based on number of rules */
  916. if ((oldinfo->number > oldinfo->initial_entries) ||
  917. (newinfo->number <= oldinfo->initial_entries))
  918. module_put(t->me);
  919. if ((oldinfo->number > oldinfo->initial_entries) &&
  920. (newinfo->number <= oldinfo->initial_entries))
  921. module_put(t->me);
  922. xt_table_unlock(t);
  923. get_old_counters(oldinfo, counters);
  924. /* Decrease module usage counts and free resource */
  925. xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
  926. cleanup_entry(iter, net);
  927. xt_free_table_info(oldinfo);
  928. if (copy_to_user(counters_ptr, counters,
  929. sizeof(struct xt_counters) * num_counters) != 0) {
  930. /* Silent error, can't fail, new table is already in place */
  931. net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
  932. }
  933. vfree(counters);
  934. return ret;
  935. put_module:
  936. module_put(t->me);
  937. xt_table_unlock(t);
  938. free_newinfo_counters_untrans:
  939. vfree(counters);
  940. out:
  941. return ret;
  942. }
  943. static int
  944. do_replace(struct net *net, const void __user *user, unsigned int len)
  945. {
  946. int ret;
  947. struct ipt_replace tmp;
  948. struct xt_table_info *newinfo;
  949. void *loc_cpu_entry;
  950. struct ipt_entry *iter;
  951. if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
  952. return -EFAULT;
  953. /* overflow check */
  954. if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
  955. return -ENOMEM;
  956. if (tmp.num_counters == 0)
  957. return -EINVAL;
  958. tmp.name[sizeof(tmp.name)-1] = 0;
  959. newinfo = xt_alloc_table_info(tmp.size);
  960. if (!newinfo)
  961. return -ENOMEM;
  962. loc_cpu_entry = newinfo->entries;
  963. if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
  964. tmp.size) != 0) {
  965. ret = -EFAULT;
  966. goto free_newinfo;
  967. }
  968. ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
  969. if (ret != 0)
  970. goto free_newinfo;
  971. ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
  972. tmp.num_counters, tmp.counters);
  973. if (ret)
  974. goto free_newinfo_untrans;
  975. return 0;
  976. free_newinfo_untrans:
  977. xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
  978. cleanup_entry(iter, net);
  979. free_newinfo:
  980. xt_free_table_info(newinfo);
  981. return ret;
  982. }
  983. static int
  984. do_add_counters(struct net *net, const void __user *user,
  985. unsigned int len, int compat)
  986. {
  987. unsigned int i;
  988. struct xt_counters_info tmp;
  989. struct xt_counters *paddc;
  990. struct xt_table *t;
  991. const struct xt_table_info *private;
  992. int ret = 0;
  993. struct ipt_entry *iter;
  994. unsigned int addend;
  995. paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
  996. if (IS_ERR(paddc))
  997. return PTR_ERR(paddc);
  998. t = xt_find_table_lock(net, AF_INET, tmp.name);
  999. if (IS_ERR(t)) {
  1000. ret = PTR_ERR(t);
  1001. goto free;
  1002. }
  1003. local_bh_disable();
  1004. private = t->private;
  1005. if (private->number != tmp.num_counters) {
  1006. ret = -EINVAL;
  1007. goto unlock_up_free;
  1008. }
  1009. i = 0;
  1010. addend = xt_write_recseq_begin();
  1011. xt_entry_foreach(iter, private->entries, private->size) {
  1012. struct xt_counters *tmp;
  1013. tmp = xt_get_this_cpu_counter(&iter->counters);
  1014. ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
  1015. ++i;
  1016. }
  1017. xt_write_recseq_end(addend);
  1018. unlock_up_free:
  1019. local_bh_enable();
  1020. xt_table_unlock(t);
  1021. module_put(t->me);
  1022. free:
  1023. vfree(paddc);
  1024. return ret;
  1025. }
  1026. #ifdef CONFIG_COMPAT
  1027. struct compat_ipt_replace {
  1028. char name[XT_TABLE_MAXNAMELEN];
  1029. u32 valid_hooks;
  1030. u32 num_entries;
  1031. u32 size;
  1032. u32 hook_entry[NF_INET_NUMHOOKS];
  1033. u32 underflow[NF_INET_NUMHOOKS];
  1034. u32 num_counters;
  1035. compat_uptr_t counters; /* struct xt_counters * */
  1036. struct compat_ipt_entry entries[];
  1037. };
  1038. static int
  1039. compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
  1040. unsigned int *size, struct xt_counters *counters,
  1041. unsigned int i)
  1042. {
  1043. struct xt_entry_target *t;
  1044. struct compat_ipt_entry __user *ce;
  1045. u_int16_t target_offset, next_offset;
  1046. compat_uint_t origsize;
  1047. const struct xt_entry_match *ematch;
  1048. int ret = 0;
  1049. origsize = *size;
  1050. ce = *dstptr;
  1051. if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 ||
  1052. copy_to_user(&ce->counters, &counters[i],
  1053. sizeof(counters[i])) != 0)
  1054. return -EFAULT;
  1055. *dstptr += sizeof(struct compat_ipt_entry);
  1056. *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
  1057. xt_ematch_foreach(ematch, e) {
  1058. ret = xt_compat_match_to_user(ematch, dstptr, size);
  1059. if (ret != 0)
  1060. return ret;
  1061. }
  1062. target_offset = e->target_offset - (origsize - *size);
  1063. t = ipt_get_target(e);
  1064. ret = xt_compat_target_to_user(t, dstptr, size);
  1065. if (ret)
  1066. return ret;
  1067. next_offset = e->next_offset - (origsize - *size);
  1068. if (put_user(target_offset, &ce->target_offset) != 0 ||
  1069. put_user(next_offset, &ce->next_offset) != 0)
  1070. return -EFAULT;
  1071. return 0;
  1072. }
  1073. static int
  1074. compat_find_calc_match(struct xt_entry_match *m,
  1075. const struct ipt_ip *ip,
  1076. int *size)
  1077. {
  1078. struct xt_match *match;
  1079. match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
  1080. m->u.user.revision);
  1081. if (IS_ERR(match))
  1082. return PTR_ERR(match);
  1083. m->u.kernel.match = match;
  1084. *size += xt_compat_match_offset(match);
  1085. return 0;
  1086. }
  1087. static void compat_release_entry(struct compat_ipt_entry *e)
  1088. {
  1089. struct xt_entry_target *t;
  1090. struct xt_entry_match *ematch;
  1091. /* Cleanup all matches */
  1092. xt_ematch_foreach(ematch, e)
  1093. module_put(ematch->u.kernel.match->me);
  1094. t = compat_ipt_get_target(e);
  1095. module_put(t->u.kernel.target->me);
  1096. }
  1097. static int
  1098. check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
  1099. struct xt_table_info *newinfo,
  1100. unsigned int *size,
  1101. const unsigned char *base,
  1102. const unsigned char *limit)
  1103. {
  1104. struct xt_entry_match *ematch;
  1105. struct xt_entry_target *t;
  1106. struct xt_target *target;
  1107. unsigned int entry_offset;
  1108. unsigned int j;
  1109. int ret, off;
  1110. if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
  1111. (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit ||
  1112. (unsigned char *)e + e->next_offset > limit)
  1113. return -EINVAL;
  1114. if (e->next_offset < sizeof(struct compat_ipt_entry) +
  1115. sizeof(struct compat_xt_entry_target))
  1116. return -EINVAL;
  1117. if (!ip_checkentry(&e->ip))
  1118. return -EINVAL;
  1119. ret = xt_compat_check_entry_offsets(e, e->elems,
  1120. e->target_offset, e->next_offset);
  1121. if (ret)
  1122. return ret;
  1123. off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
  1124. entry_offset = (void *)e - (void *)base;
  1125. j = 0;
  1126. xt_ematch_foreach(ematch, e) {
  1127. ret = compat_find_calc_match(ematch, &e->ip, &off);
  1128. if (ret != 0)
  1129. goto release_matches;
  1130. ++j;
  1131. }
  1132. t = compat_ipt_get_target(e);
  1133. target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
  1134. t->u.user.revision);
  1135. if (IS_ERR(target)) {
  1136. ret = PTR_ERR(target);
  1137. goto release_matches;
  1138. }
  1139. t->u.kernel.target = target;
  1140. off += xt_compat_target_offset(target);
  1141. *size += off;
  1142. ret = xt_compat_add_offset(AF_INET, entry_offset, off);
  1143. if (ret)
  1144. goto out;
  1145. return 0;
  1146. out:
  1147. module_put(t->u.kernel.target->me);
  1148. release_matches:
  1149. xt_ematch_foreach(ematch, e) {
  1150. if (j-- == 0)
  1151. break;
  1152. module_put(ematch->u.kernel.match->me);
  1153. }
  1154. return ret;
  1155. }
  1156. static void
  1157. compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
  1158. unsigned int *size,
  1159. struct xt_table_info *newinfo, unsigned char *base)
  1160. {
  1161. struct xt_entry_target *t;
  1162. struct ipt_entry *de;
  1163. unsigned int origsize;
  1164. int h;
  1165. struct xt_entry_match *ematch;
  1166. origsize = *size;
  1167. de = *dstptr;
  1168. memcpy(de, e, sizeof(struct ipt_entry));
  1169. memcpy(&de->counters, &e->counters, sizeof(e->counters));
  1170. *dstptr += sizeof(struct ipt_entry);
  1171. *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
  1172. xt_ematch_foreach(ematch, e)
  1173. xt_compat_match_from_user(ematch, dstptr, size);
  1174. de->target_offset = e->target_offset - (origsize - *size);
  1175. t = compat_ipt_get_target(e);
  1176. xt_compat_target_from_user(t, dstptr, size);
  1177. de->next_offset = e->next_offset - (origsize - *size);
  1178. for (h = 0; h < NF_INET_NUMHOOKS; h++) {
  1179. if ((unsigned char *)de - base < newinfo->hook_entry[h])
  1180. newinfo->hook_entry[h] -= origsize - *size;
  1181. if ((unsigned char *)de - base < newinfo->underflow[h])
  1182. newinfo->underflow[h] -= origsize - *size;
  1183. }
  1184. }
  1185. static int
  1186. translate_compat_table(struct net *net,
  1187. struct xt_table_info **pinfo,
  1188. void **pentry0,
  1189. const struct compat_ipt_replace *compatr)
  1190. {
  1191. unsigned int i, j;
  1192. struct xt_table_info *newinfo, *info;
  1193. void *pos, *entry0, *entry1;
  1194. struct compat_ipt_entry *iter0;
  1195. struct ipt_replace repl;
  1196. unsigned int size;
  1197. int ret;
  1198. info = *pinfo;
  1199. entry0 = *pentry0;
  1200. size = compatr->size;
  1201. info->number = compatr->num_entries;
  1202. j = 0;
  1203. xt_compat_lock(AF_INET);
  1204. ret = xt_compat_init_offsets(AF_INET, compatr->num_entries);
  1205. if (ret)
  1206. goto out_unlock;
  1207. /* Walk through entries, checking offsets. */
  1208. xt_entry_foreach(iter0, entry0, compatr->size) {
  1209. ret = check_compat_entry_size_and_hooks(iter0, info, &size,
  1210. entry0,
  1211. entry0 + compatr->size);
  1212. if (ret != 0)
  1213. goto out_unlock;
  1214. ++j;
  1215. }
  1216. ret = -EINVAL;
  1217. if (j != compatr->num_entries)
  1218. goto out_unlock;
  1219. ret = -ENOMEM;
  1220. newinfo = xt_alloc_table_info(size);
  1221. if (!newinfo)
  1222. goto out_unlock;
  1223. newinfo->number = compatr->num_entries;
  1224. for (i = 0; i < NF_INET_NUMHOOKS; i++) {
  1225. newinfo->hook_entry[i] = compatr->hook_entry[i];
  1226. newinfo->underflow[i] = compatr->underflow[i];
  1227. }
  1228. entry1 = newinfo->entries;
  1229. pos = entry1;
  1230. size = compatr->size;
  1231. xt_entry_foreach(iter0, entry0, compatr->size)
  1232. compat_copy_entry_from_user(iter0, &pos, &size,
  1233. newinfo, entry1);
  1234. /* all module references in entry0 are now gone.
  1235. * entry1/newinfo contains a 64bit ruleset that looks exactly as
  1236. * generated by 64bit userspace.
  1237. *
  1238. * Call standard translate_table() to validate all hook_entrys,
  1239. * underflows, check for loops, etc.
  1240. */
  1241. xt_compat_flush_offsets(AF_INET);
  1242. xt_compat_unlock(AF_INET);
  1243. memcpy(&repl, compatr, sizeof(*compatr));
  1244. for (i = 0; i < NF_INET_NUMHOOKS; i++) {
  1245. repl.hook_entry[i] = newinfo->hook_entry[i];
  1246. repl.underflow[i] = newinfo->underflow[i];
  1247. }
  1248. repl.num_counters = 0;
  1249. repl.counters = NULL;
  1250. repl.size = newinfo->size;
  1251. ret = translate_table(net, newinfo, entry1, &repl);
  1252. if (ret)
  1253. goto free_newinfo;
  1254. *pinfo = newinfo;
  1255. *pentry0 = entry1;
  1256. xt_free_table_info(info);
  1257. return 0;
  1258. free_newinfo:
  1259. xt_free_table_info(newinfo);
  1260. return ret;
  1261. out_unlock:
  1262. xt_compat_flush_offsets(AF_INET);
  1263. xt_compat_unlock(AF_INET);
  1264. xt_entry_foreach(iter0, entry0, compatr->size) {
  1265. if (j-- == 0)
  1266. break;
  1267. compat_release_entry(iter0);
  1268. }
  1269. return ret;
  1270. }
  1271. static int
  1272. compat_do_replace(struct net *net, void __user *user, unsigned int len)
  1273. {
  1274. int ret;
  1275. struct compat_ipt_replace tmp;
  1276. struct xt_table_info *newinfo;
  1277. void *loc_cpu_entry;
  1278. struct ipt_entry *iter;
  1279. if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
  1280. return -EFAULT;
  1281. /* overflow check */
  1282. if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
  1283. return -ENOMEM;
  1284. if (tmp.num_counters == 0)
  1285. return -EINVAL;
  1286. tmp.name[sizeof(tmp.name)-1] = 0;
  1287. newinfo = xt_alloc_table_info(tmp.size);
  1288. if (!newinfo)
  1289. return -ENOMEM;
  1290. loc_cpu_entry = newinfo->entries;
  1291. if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
  1292. tmp.size) != 0) {
  1293. ret = -EFAULT;
  1294. goto free_newinfo;
  1295. }
  1296. ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
  1297. if (ret != 0)
  1298. goto free_newinfo;
  1299. ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
  1300. tmp.num_counters, compat_ptr(tmp.counters));
  1301. if (ret)
  1302. goto free_newinfo_untrans;
  1303. return 0;
  1304. free_newinfo_untrans:
  1305. xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
  1306. cleanup_entry(iter, net);
  1307. free_newinfo:
  1308. xt_free_table_info(newinfo);
  1309. return ret;
  1310. }
  1311. static int
  1312. compat_do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user,
  1313. unsigned int len)
  1314. {
  1315. int ret;
  1316. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  1317. return -EPERM;
  1318. switch (cmd) {
  1319. case IPT_SO_SET_REPLACE:
  1320. ret = compat_do_replace(sock_net(sk), user, len);
  1321. break;
  1322. case IPT_SO_SET_ADD_COUNTERS:
  1323. ret = do_add_counters(sock_net(sk), user, len, 1);
  1324. break;
  1325. default:
  1326. ret = -EINVAL;
  1327. }
  1328. return ret;
  1329. }
  1330. struct compat_ipt_get_entries {
  1331. char name[XT_TABLE_MAXNAMELEN];
  1332. compat_uint_t size;
  1333. struct compat_ipt_entry entrytable[];
  1334. };
  1335. static int
  1336. compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
  1337. void __user *userptr)
  1338. {
  1339. struct xt_counters *counters;
  1340. const struct xt_table_info *private = table->private;
  1341. void __user *pos;
  1342. unsigned int size;
  1343. int ret = 0;
  1344. unsigned int i = 0;
  1345. struct ipt_entry *iter;
  1346. counters = alloc_counters(table);
  1347. if (IS_ERR(counters))
  1348. return PTR_ERR(counters);
  1349. pos = userptr;
  1350. size = total_size;
  1351. xt_entry_foreach(iter, private->entries, total_size) {
  1352. ret = compat_copy_entry_to_user(iter, &pos,
  1353. &size, counters, i++);
  1354. if (ret != 0)
  1355. break;
  1356. }
  1357. vfree(counters);
  1358. return ret;
  1359. }
  1360. static int
  1361. compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
  1362. int *len)
  1363. {
  1364. int ret;
  1365. struct compat_ipt_get_entries get;
  1366. struct xt_table *t;
  1367. if (*len < sizeof(get))
  1368. return -EINVAL;
  1369. if (copy_from_user(&get, uptr, sizeof(get)) != 0)
  1370. return -EFAULT;
  1371. if (*len != sizeof(struct compat_ipt_get_entries) + get.size)
  1372. return -EINVAL;
  1373. get.name[sizeof(get.name) - 1] = '\0';
  1374. xt_compat_lock(AF_INET);
  1375. t = xt_find_table_lock(net, AF_INET, get.name);
  1376. if (!IS_ERR(t)) {
  1377. const struct xt_table_info *private = t->private;
  1378. struct xt_table_info info;
  1379. ret = compat_table_info(private, &info);
  1380. if (!ret && get.size == info.size)
  1381. ret = compat_copy_entries_to_user(private->size,
  1382. t, uptr->entrytable);
  1383. else if (!ret)
  1384. ret = -EAGAIN;
  1385. xt_compat_flush_offsets(AF_INET);
  1386. module_put(t->me);
  1387. xt_table_unlock(t);
  1388. } else
  1389. ret = PTR_ERR(t);
  1390. xt_compat_unlock(AF_INET);
  1391. return ret;
  1392. }
  1393. static int do_ipt_get_ctl(struct sock *, int, void __user *, int *);
  1394. static int
  1395. compat_do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
  1396. {
  1397. int ret;
  1398. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  1399. return -EPERM;
  1400. switch (cmd) {
  1401. case IPT_SO_GET_INFO:
  1402. ret = get_info(sock_net(sk), user, len, 1);
  1403. break;
  1404. case IPT_SO_GET_ENTRIES:
  1405. ret = compat_get_entries(sock_net(sk), user, len);
  1406. break;
  1407. default:
  1408. ret = do_ipt_get_ctl(sk, cmd, user, len);
  1409. }
  1410. return ret;
  1411. }
  1412. #endif
  1413. static int
  1414. do_ipt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
  1415. {
  1416. int ret;
  1417. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  1418. return -EPERM;
  1419. switch (cmd) {
  1420. case IPT_SO_SET_REPLACE:
  1421. ret = do_replace(sock_net(sk), user, len);
  1422. break;
  1423. case IPT_SO_SET_ADD_COUNTERS:
  1424. ret = do_add_counters(sock_net(sk), user, len, 0);
  1425. break;
  1426. default:
  1427. ret = -EINVAL;
  1428. }
  1429. return ret;
  1430. }
  1431. static int
  1432. do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
  1433. {
  1434. int ret;
  1435. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  1436. return -EPERM;
  1437. switch (cmd) {
  1438. case IPT_SO_GET_INFO:
  1439. ret = get_info(sock_net(sk), user, len, 0);
  1440. break;
  1441. case IPT_SO_GET_ENTRIES:
  1442. ret = get_entries(sock_net(sk), user, len);
  1443. break;
  1444. case IPT_SO_GET_REVISION_MATCH:
  1445. case IPT_SO_GET_REVISION_TARGET: {
  1446. struct xt_get_revision rev;
  1447. int target;
  1448. if (*len != sizeof(rev)) {
  1449. ret = -EINVAL;
  1450. break;
  1451. }
  1452. if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
  1453. ret = -EFAULT;
  1454. break;
  1455. }
  1456. rev.name[sizeof(rev.name)-1] = 0;
  1457. if (cmd == IPT_SO_GET_REVISION_TARGET)
  1458. target = 1;
  1459. else
  1460. target = 0;
  1461. try_then_request_module(xt_find_revision(AF_INET, rev.name,
  1462. rev.revision,
  1463. target, &ret),
  1464. "ipt_%s", rev.name);
  1465. break;
  1466. }
  1467. default:
  1468. ret = -EINVAL;
  1469. }
  1470. return ret;
  1471. }
  1472. static void __ipt_unregister_table(struct net *net, struct xt_table *table)
  1473. {
  1474. struct xt_table_info *private;
  1475. void *loc_cpu_entry;
  1476. struct module *table_owner = table->me;
  1477. struct ipt_entry *iter;
  1478. private = xt_unregister_table(table);
  1479. /* Decrease module usage counts and free resources */
  1480. loc_cpu_entry = private->entries;
  1481. xt_entry_foreach(iter, loc_cpu_entry, private->size)
  1482. cleanup_entry(iter, net);
  1483. if (private->number > private->initial_entries)
  1484. module_put(table_owner);
  1485. xt_free_table_info(private);
  1486. }
  1487. int ipt_register_table(struct net *net, const struct xt_table *table,
  1488. const struct ipt_replace *repl,
  1489. const struct nf_hook_ops *ops, struct xt_table **res)
  1490. {
  1491. int ret;
  1492. struct xt_table_info *newinfo;
  1493. struct xt_table_info bootstrap = {0};
  1494. void *loc_cpu_entry;
  1495. struct xt_table *new_table;
  1496. newinfo = xt_alloc_table_info(repl->size);
  1497. if (!newinfo)
  1498. return -ENOMEM;
  1499. loc_cpu_entry = newinfo->entries;
  1500. memcpy(loc_cpu_entry, repl->entries, repl->size);
  1501. ret = translate_table(net, newinfo, loc_cpu_entry, repl);
  1502. if (ret != 0)
  1503. goto out_free;
  1504. new_table = xt_register_table(net, table, &bootstrap, newinfo);
  1505. if (IS_ERR(new_table)) {
  1506. ret = PTR_ERR(new_table);
  1507. goto out_free;
  1508. }
  1509. /* set res now, will see skbs right after nf_register_net_hooks */
  1510. WRITE_ONCE(*res, new_table);
  1511. if (!ops)
  1512. return 0;
  1513. ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
  1514. if (ret != 0) {
  1515. __ipt_unregister_table(net, new_table);
  1516. *res = NULL;
  1517. }
  1518. return ret;
  1519. out_free:
  1520. xt_free_table_info(newinfo);
  1521. return ret;
  1522. }
  1523. void ipt_unregister_table(struct net *net, struct xt_table *table,
  1524. const struct nf_hook_ops *ops)
  1525. {
  1526. if (ops)
  1527. nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
  1528. __ipt_unregister_table(net, table);
  1529. }
  1530. /* Returns 1 if the type and code is matched by the range, 0 otherwise */
  1531. static inline bool
  1532. icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
  1533. u_int8_t type, u_int8_t code,
  1534. bool invert)
  1535. {
  1536. return ((test_type == 0xFF) ||
  1537. (type == test_type && code >= min_code && code <= max_code))
  1538. ^ invert;
  1539. }
  1540. static bool
  1541. icmp_match(const struct sk_buff *skb, struct xt_action_param *par)
  1542. {
  1543. const struct icmphdr *ic;
  1544. struct icmphdr _icmph;
  1545. const struct ipt_icmp *icmpinfo = par->matchinfo;
  1546. /* Must not be a fragment. */
  1547. if (par->fragoff != 0)
  1548. return false;
  1549. ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
  1550. if (ic == NULL) {
  1551. /* We've been asked to examine this packet, and we
  1552. * can't. Hence, no choice but to drop.
  1553. */
  1554. par->hotdrop = true;
  1555. return false;
  1556. }
  1557. return icmp_type_code_match(icmpinfo->type,
  1558. icmpinfo->code[0],
  1559. icmpinfo->code[1],
  1560. ic->type, ic->code,
  1561. !!(icmpinfo->invflags&IPT_ICMP_INV));
  1562. }
  1563. static int icmp_checkentry(const struct xt_mtchk_param *par)
  1564. {
  1565. const struct ipt_icmp *icmpinfo = par->matchinfo;
  1566. /* Must specify no unknown invflags */
  1567. return (icmpinfo->invflags & ~IPT_ICMP_INV) ? -EINVAL : 0;
  1568. }
  1569. static struct xt_target ipt_builtin_tg[] __read_mostly = {
  1570. {
  1571. .name = XT_STANDARD_TARGET,
  1572. .targetsize = sizeof(int),
  1573. .family = NFPROTO_IPV4,
  1574. #ifdef CONFIG_COMPAT
  1575. .compatsize = sizeof(compat_int_t),
  1576. .compat_from_user = compat_standard_from_user,
  1577. .compat_to_user = compat_standard_to_user,
  1578. #endif
  1579. },
  1580. {
  1581. .name = XT_ERROR_TARGET,
  1582. .target = ipt_error,
  1583. .targetsize = XT_FUNCTION_MAXNAMELEN,
  1584. .family = NFPROTO_IPV4,
  1585. },
  1586. };
  1587. static struct nf_sockopt_ops ipt_sockopts = {
  1588. .pf = PF_INET,
  1589. .set_optmin = IPT_BASE_CTL,
  1590. .set_optmax = IPT_SO_SET_MAX+1,
  1591. .set = do_ipt_set_ctl,
  1592. #ifdef CONFIG_COMPAT
  1593. .compat_set = compat_do_ipt_set_ctl,
  1594. #endif
  1595. .get_optmin = IPT_BASE_CTL,
  1596. .get_optmax = IPT_SO_GET_MAX+1,
  1597. .get = do_ipt_get_ctl,
  1598. #ifdef CONFIG_COMPAT
  1599. .compat_get = compat_do_ipt_get_ctl,
  1600. #endif
  1601. .owner = THIS_MODULE,
  1602. };
  1603. static struct xt_match ipt_builtin_mt[] __read_mostly = {
  1604. {
  1605. .name = "icmp",
  1606. .match = icmp_match,
  1607. .matchsize = sizeof(struct ipt_icmp),
  1608. .checkentry = icmp_checkentry,
  1609. .proto = IPPROTO_ICMP,
  1610. .family = NFPROTO_IPV4,
  1611. .me = THIS_MODULE,
  1612. },
  1613. };
  1614. static int __net_init ip_tables_net_init(struct net *net)
  1615. {
  1616. return xt_proto_init(net, NFPROTO_IPV4);
  1617. }
  1618. static void __net_exit ip_tables_net_exit(struct net *net)
  1619. {
  1620. xt_proto_fini(net, NFPROTO_IPV4);
  1621. }
  1622. static struct pernet_operations ip_tables_net_ops = {
  1623. .init = ip_tables_net_init,
  1624. .exit = ip_tables_net_exit,
  1625. };
  1626. static int __init ip_tables_init(void)
  1627. {
  1628. int ret;
  1629. ret = register_pernet_subsys(&ip_tables_net_ops);
  1630. if (ret < 0)
  1631. goto err1;
  1632. /* No one else will be downing sem now, so we won't sleep */
  1633. ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
  1634. if (ret < 0)
  1635. goto err2;
  1636. ret = xt_register_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
  1637. if (ret < 0)
  1638. goto err4;
  1639. /* Register setsockopt */
  1640. ret = nf_register_sockopt(&ipt_sockopts);
  1641. if (ret < 0)
  1642. goto err5;
  1643. return 0;
  1644. err5:
  1645. xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
  1646. err4:
  1647. xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
  1648. err2:
  1649. unregister_pernet_subsys(&ip_tables_net_ops);
  1650. err1:
  1651. return ret;
  1652. }
  1653. static void __exit ip_tables_fini(void)
  1654. {
  1655. nf_unregister_sockopt(&ipt_sockopts);
  1656. xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
  1657. xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
  1658. unregister_pernet_subsys(&ip_tables_net_ops);
  1659. }
  1660. EXPORT_SYMBOL(ipt_register_table);
  1661. EXPORT_SYMBOL(ipt_unregister_table);
  1662. EXPORT_SYMBOL(ipt_do_table);
  1663. module_init(ip_tables_init);
  1664. module_exit(ip_tables_fini);