ip_tables.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932
  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, const int *len)
  809. {
  810. char name[XT_TABLE_MAXNAMELEN];
  811. struct xt_table *t;
  812. int ret;
  813. if (*len != sizeof(struct ipt_getinfo))
  814. return -EINVAL;
  815. if (copy_from_user(name, user, sizeof(name)) != 0)
  816. return -EFAULT;
  817. name[XT_TABLE_MAXNAMELEN-1] = '\0';
  818. #ifdef CONFIG_COMPAT
  819. if (in_compat_syscall())
  820. xt_compat_lock(AF_INET);
  821. #endif
  822. t = xt_request_find_table_lock(net, AF_INET, name);
  823. if (!IS_ERR(t)) {
  824. struct ipt_getinfo info;
  825. const struct xt_table_info *private = t->private;
  826. #ifdef CONFIG_COMPAT
  827. struct xt_table_info tmp;
  828. if (in_compat_syscall()) {
  829. ret = compat_table_info(private, &tmp);
  830. xt_compat_flush_offsets(AF_INET);
  831. private = &tmp;
  832. }
  833. #endif
  834. memset(&info, 0, sizeof(info));
  835. info.valid_hooks = t->valid_hooks;
  836. memcpy(info.hook_entry, private->hook_entry,
  837. sizeof(info.hook_entry));
  838. memcpy(info.underflow, private->underflow,
  839. sizeof(info.underflow));
  840. info.num_entries = private->number;
  841. info.size = private->size;
  842. strcpy(info.name, name);
  843. if (copy_to_user(user, &info, *len) != 0)
  844. ret = -EFAULT;
  845. else
  846. ret = 0;
  847. xt_table_unlock(t);
  848. module_put(t->me);
  849. } else
  850. ret = PTR_ERR(t);
  851. #ifdef CONFIG_COMPAT
  852. if (in_compat_syscall())
  853. xt_compat_unlock(AF_INET);
  854. #endif
  855. return ret;
  856. }
  857. static int
  858. get_entries(struct net *net, struct ipt_get_entries __user *uptr,
  859. const int *len)
  860. {
  861. int ret;
  862. struct ipt_get_entries get;
  863. struct xt_table *t;
  864. if (*len < sizeof(get))
  865. return -EINVAL;
  866. if (copy_from_user(&get, uptr, sizeof(get)) != 0)
  867. return -EFAULT;
  868. if (*len != sizeof(struct ipt_get_entries) + get.size)
  869. return -EINVAL;
  870. get.name[sizeof(get.name) - 1] = '\0';
  871. t = xt_find_table_lock(net, AF_INET, get.name);
  872. if (!IS_ERR(t)) {
  873. const struct xt_table_info *private = t->private;
  874. if (get.size == private->size)
  875. ret = copy_entries_to_user(private->size,
  876. t, uptr->entrytable);
  877. else
  878. ret = -EAGAIN;
  879. module_put(t->me);
  880. xt_table_unlock(t);
  881. } else
  882. ret = PTR_ERR(t);
  883. return ret;
  884. }
  885. static int
  886. __do_replace(struct net *net, const char *name, unsigned int valid_hooks,
  887. struct xt_table_info *newinfo, unsigned int num_counters,
  888. void __user *counters_ptr)
  889. {
  890. int ret;
  891. struct xt_table *t;
  892. struct xt_table_info *oldinfo;
  893. struct xt_counters *counters;
  894. struct ipt_entry *iter;
  895. ret = 0;
  896. counters = xt_counters_alloc(num_counters);
  897. if (!counters) {
  898. ret = -ENOMEM;
  899. goto out;
  900. }
  901. t = xt_request_find_table_lock(net, AF_INET, name);
  902. if (IS_ERR(t)) {
  903. ret = PTR_ERR(t);
  904. goto free_newinfo_counters_untrans;
  905. }
  906. /* You lied! */
  907. if (valid_hooks != t->valid_hooks) {
  908. ret = -EINVAL;
  909. goto put_module;
  910. }
  911. oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
  912. if (!oldinfo)
  913. goto put_module;
  914. /* Update module usage count based on number of rules */
  915. if ((oldinfo->number > oldinfo->initial_entries) ||
  916. (newinfo->number <= oldinfo->initial_entries))
  917. module_put(t->me);
  918. if ((oldinfo->number > oldinfo->initial_entries) &&
  919. (newinfo->number <= oldinfo->initial_entries))
  920. module_put(t->me);
  921. xt_table_unlock(t);
  922. get_old_counters(oldinfo, counters);
  923. /* Decrease module usage counts and free resource */
  924. xt_entry_foreach(iter, oldinfo->entries, oldinfo->size)
  925. cleanup_entry(iter, net);
  926. xt_free_table_info(oldinfo);
  927. if (copy_to_user(counters_ptr, counters,
  928. sizeof(struct xt_counters) * num_counters) != 0) {
  929. /* Silent error, can't fail, new table is already in place */
  930. net_warn_ratelimited("iptables: counters copy to user failed while replacing table\n");
  931. }
  932. vfree(counters);
  933. return ret;
  934. put_module:
  935. module_put(t->me);
  936. xt_table_unlock(t);
  937. free_newinfo_counters_untrans:
  938. vfree(counters);
  939. out:
  940. return ret;
  941. }
  942. static int
  943. do_replace(struct net *net, sockptr_t arg, unsigned int len)
  944. {
  945. int ret;
  946. struct ipt_replace tmp;
  947. struct xt_table_info *newinfo;
  948. void *loc_cpu_entry;
  949. struct ipt_entry *iter;
  950. if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
  951. return -EFAULT;
  952. /* overflow check */
  953. if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
  954. return -ENOMEM;
  955. if (tmp.num_counters == 0)
  956. return -EINVAL;
  957. tmp.name[sizeof(tmp.name)-1] = 0;
  958. newinfo = xt_alloc_table_info(tmp.size);
  959. if (!newinfo)
  960. return -ENOMEM;
  961. loc_cpu_entry = newinfo->entries;
  962. if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp),
  963. tmp.size) != 0) {
  964. ret = -EFAULT;
  965. goto free_newinfo;
  966. }
  967. ret = translate_table(net, newinfo, loc_cpu_entry, &tmp);
  968. if (ret != 0)
  969. goto free_newinfo;
  970. ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
  971. tmp.num_counters, tmp.counters);
  972. if (ret)
  973. goto free_newinfo_untrans;
  974. return 0;
  975. free_newinfo_untrans:
  976. xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
  977. cleanup_entry(iter, net);
  978. free_newinfo:
  979. xt_free_table_info(newinfo);
  980. return ret;
  981. }
  982. static int
  983. do_add_counters(struct net *net, sockptr_t arg, unsigned int len)
  984. {
  985. unsigned int i;
  986. struct xt_counters_info tmp;
  987. struct xt_counters *paddc;
  988. struct xt_table *t;
  989. const struct xt_table_info *private;
  990. int ret = 0;
  991. struct ipt_entry *iter;
  992. unsigned int addend;
  993. paddc = xt_copy_counters(arg, len, &tmp);
  994. if (IS_ERR(paddc))
  995. return PTR_ERR(paddc);
  996. t = xt_find_table_lock(net, AF_INET, tmp.name);
  997. if (IS_ERR(t)) {
  998. ret = PTR_ERR(t);
  999. goto free;
  1000. }
  1001. local_bh_disable();
  1002. private = t->private;
  1003. if (private->number != tmp.num_counters) {
  1004. ret = -EINVAL;
  1005. goto unlock_up_free;
  1006. }
  1007. i = 0;
  1008. addend = xt_write_recseq_begin();
  1009. xt_entry_foreach(iter, private->entries, private->size) {
  1010. struct xt_counters *tmp;
  1011. tmp = xt_get_this_cpu_counter(&iter->counters);
  1012. ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
  1013. ++i;
  1014. }
  1015. xt_write_recseq_end(addend);
  1016. unlock_up_free:
  1017. local_bh_enable();
  1018. xt_table_unlock(t);
  1019. module_put(t->me);
  1020. free:
  1021. vfree(paddc);
  1022. return ret;
  1023. }
  1024. #ifdef CONFIG_COMPAT
  1025. struct compat_ipt_replace {
  1026. char name[XT_TABLE_MAXNAMELEN];
  1027. u32 valid_hooks;
  1028. u32 num_entries;
  1029. u32 size;
  1030. u32 hook_entry[NF_INET_NUMHOOKS];
  1031. u32 underflow[NF_INET_NUMHOOKS];
  1032. u32 num_counters;
  1033. compat_uptr_t counters; /* struct xt_counters * */
  1034. struct compat_ipt_entry entries[];
  1035. };
  1036. static int
  1037. compat_copy_entry_to_user(struct ipt_entry *e, void __user **dstptr,
  1038. unsigned int *size, struct xt_counters *counters,
  1039. unsigned int i)
  1040. {
  1041. struct xt_entry_target *t;
  1042. struct compat_ipt_entry __user *ce;
  1043. u_int16_t target_offset, next_offset;
  1044. compat_uint_t origsize;
  1045. const struct xt_entry_match *ematch;
  1046. int ret = 0;
  1047. origsize = *size;
  1048. ce = *dstptr;
  1049. if (copy_to_user(ce, e, sizeof(struct ipt_entry)) != 0 ||
  1050. copy_to_user(&ce->counters, &counters[i],
  1051. sizeof(counters[i])) != 0)
  1052. return -EFAULT;
  1053. *dstptr += sizeof(struct compat_ipt_entry);
  1054. *size -= sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
  1055. xt_ematch_foreach(ematch, e) {
  1056. ret = xt_compat_match_to_user(ematch, dstptr, size);
  1057. if (ret != 0)
  1058. return ret;
  1059. }
  1060. target_offset = e->target_offset - (origsize - *size);
  1061. t = ipt_get_target(e);
  1062. ret = xt_compat_target_to_user(t, dstptr, size);
  1063. if (ret)
  1064. return ret;
  1065. next_offset = e->next_offset - (origsize - *size);
  1066. if (put_user(target_offset, &ce->target_offset) != 0 ||
  1067. put_user(next_offset, &ce->next_offset) != 0)
  1068. return -EFAULT;
  1069. return 0;
  1070. }
  1071. static int
  1072. compat_find_calc_match(struct xt_entry_match *m,
  1073. const struct ipt_ip *ip,
  1074. int *size)
  1075. {
  1076. struct xt_match *match;
  1077. match = xt_request_find_match(NFPROTO_IPV4, m->u.user.name,
  1078. m->u.user.revision);
  1079. if (IS_ERR(match))
  1080. return PTR_ERR(match);
  1081. m->u.kernel.match = match;
  1082. *size += xt_compat_match_offset(match);
  1083. return 0;
  1084. }
  1085. static void compat_release_entry(struct compat_ipt_entry *e)
  1086. {
  1087. struct xt_entry_target *t;
  1088. struct xt_entry_match *ematch;
  1089. /* Cleanup all matches */
  1090. xt_ematch_foreach(ematch, e)
  1091. module_put(ematch->u.kernel.match->me);
  1092. t = compat_ipt_get_target(e);
  1093. module_put(t->u.kernel.target->me);
  1094. }
  1095. static int
  1096. check_compat_entry_size_and_hooks(struct compat_ipt_entry *e,
  1097. struct xt_table_info *newinfo,
  1098. unsigned int *size,
  1099. const unsigned char *base,
  1100. const unsigned char *limit)
  1101. {
  1102. struct xt_entry_match *ematch;
  1103. struct xt_entry_target *t;
  1104. struct xt_target *target;
  1105. unsigned int entry_offset;
  1106. unsigned int j;
  1107. int ret, off;
  1108. if ((unsigned long)e % __alignof__(struct compat_ipt_entry) != 0 ||
  1109. (unsigned char *)e + sizeof(struct compat_ipt_entry) >= limit ||
  1110. (unsigned char *)e + e->next_offset > limit)
  1111. return -EINVAL;
  1112. if (e->next_offset < sizeof(struct compat_ipt_entry) +
  1113. sizeof(struct compat_xt_entry_target))
  1114. return -EINVAL;
  1115. if (!ip_checkentry(&e->ip))
  1116. return -EINVAL;
  1117. ret = xt_compat_check_entry_offsets(e, e->elems,
  1118. e->target_offset, e->next_offset);
  1119. if (ret)
  1120. return ret;
  1121. off = sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
  1122. entry_offset = (void *)e - (void *)base;
  1123. j = 0;
  1124. xt_ematch_foreach(ematch, e) {
  1125. ret = compat_find_calc_match(ematch, &e->ip, &off);
  1126. if (ret != 0)
  1127. goto release_matches;
  1128. ++j;
  1129. }
  1130. t = compat_ipt_get_target(e);
  1131. target = xt_request_find_target(NFPROTO_IPV4, t->u.user.name,
  1132. t->u.user.revision);
  1133. if (IS_ERR(target)) {
  1134. ret = PTR_ERR(target);
  1135. goto release_matches;
  1136. }
  1137. t->u.kernel.target = target;
  1138. off += xt_compat_target_offset(target);
  1139. *size += off;
  1140. ret = xt_compat_add_offset(AF_INET, entry_offset, off);
  1141. if (ret)
  1142. goto out;
  1143. return 0;
  1144. out:
  1145. module_put(t->u.kernel.target->me);
  1146. release_matches:
  1147. xt_ematch_foreach(ematch, e) {
  1148. if (j-- == 0)
  1149. break;
  1150. module_put(ematch->u.kernel.match->me);
  1151. }
  1152. return ret;
  1153. }
  1154. static void
  1155. compat_copy_entry_from_user(struct compat_ipt_entry *e, void **dstptr,
  1156. unsigned int *size,
  1157. struct xt_table_info *newinfo, unsigned char *base)
  1158. {
  1159. struct xt_entry_target *t;
  1160. struct ipt_entry *de;
  1161. unsigned int origsize;
  1162. int h;
  1163. struct xt_entry_match *ematch;
  1164. origsize = *size;
  1165. de = *dstptr;
  1166. memcpy(de, e, sizeof(struct ipt_entry));
  1167. memcpy(&de->counters, &e->counters, sizeof(e->counters));
  1168. *dstptr += sizeof(struct ipt_entry);
  1169. *size += sizeof(struct ipt_entry) - sizeof(struct compat_ipt_entry);
  1170. xt_ematch_foreach(ematch, e)
  1171. xt_compat_match_from_user(ematch, dstptr, size);
  1172. de->target_offset = e->target_offset - (origsize - *size);
  1173. t = compat_ipt_get_target(e);
  1174. xt_compat_target_from_user(t, dstptr, size);
  1175. de->next_offset = e->next_offset - (origsize - *size);
  1176. for (h = 0; h < NF_INET_NUMHOOKS; h++) {
  1177. if ((unsigned char *)de - base < newinfo->hook_entry[h])
  1178. newinfo->hook_entry[h] -= origsize - *size;
  1179. if ((unsigned char *)de - base < newinfo->underflow[h])
  1180. newinfo->underflow[h] -= origsize - *size;
  1181. }
  1182. }
  1183. static int
  1184. translate_compat_table(struct net *net,
  1185. struct xt_table_info **pinfo,
  1186. void **pentry0,
  1187. const struct compat_ipt_replace *compatr)
  1188. {
  1189. unsigned int i, j;
  1190. struct xt_table_info *newinfo, *info;
  1191. void *pos, *entry0, *entry1;
  1192. struct compat_ipt_entry *iter0;
  1193. struct ipt_replace repl;
  1194. unsigned int size;
  1195. int ret;
  1196. info = *pinfo;
  1197. entry0 = *pentry0;
  1198. size = compatr->size;
  1199. info->number = compatr->num_entries;
  1200. j = 0;
  1201. xt_compat_lock(AF_INET);
  1202. ret = xt_compat_init_offsets(AF_INET, compatr->num_entries);
  1203. if (ret)
  1204. goto out_unlock;
  1205. /* Walk through entries, checking offsets. */
  1206. xt_entry_foreach(iter0, entry0, compatr->size) {
  1207. ret = check_compat_entry_size_and_hooks(iter0, info, &size,
  1208. entry0,
  1209. entry0 + compatr->size);
  1210. if (ret != 0)
  1211. goto out_unlock;
  1212. ++j;
  1213. }
  1214. ret = -EINVAL;
  1215. if (j != compatr->num_entries)
  1216. goto out_unlock;
  1217. ret = -ENOMEM;
  1218. newinfo = xt_alloc_table_info(size);
  1219. if (!newinfo)
  1220. goto out_unlock;
  1221. memset(newinfo->entries, 0, size);
  1222. newinfo->number = compatr->num_entries;
  1223. for (i = 0; i < NF_INET_NUMHOOKS; i++) {
  1224. newinfo->hook_entry[i] = compatr->hook_entry[i];
  1225. newinfo->underflow[i] = compatr->underflow[i];
  1226. }
  1227. entry1 = newinfo->entries;
  1228. pos = entry1;
  1229. size = compatr->size;
  1230. xt_entry_foreach(iter0, entry0, compatr->size)
  1231. compat_copy_entry_from_user(iter0, &pos, &size,
  1232. newinfo, entry1);
  1233. /* all module references in entry0 are now gone.
  1234. * entry1/newinfo contains a 64bit ruleset that looks exactly as
  1235. * generated by 64bit userspace.
  1236. *
  1237. * Call standard translate_table() to validate all hook_entrys,
  1238. * underflows, check for loops, etc.
  1239. */
  1240. xt_compat_flush_offsets(AF_INET);
  1241. xt_compat_unlock(AF_INET);
  1242. memcpy(&repl, compatr, sizeof(*compatr));
  1243. for (i = 0; i < NF_INET_NUMHOOKS; i++) {
  1244. repl.hook_entry[i] = newinfo->hook_entry[i];
  1245. repl.underflow[i] = newinfo->underflow[i];
  1246. }
  1247. repl.num_counters = 0;
  1248. repl.counters = NULL;
  1249. repl.size = newinfo->size;
  1250. ret = translate_table(net, newinfo, entry1, &repl);
  1251. if (ret)
  1252. goto free_newinfo;
  1253. *pinfo = newinfo;
  1254. *pentry0 = entry1;
  1255. xt_free_table_info(info);
  1256. return 0;
  1257. free_newinfo:
  1258. xt_free_table_info(newinfo);
  1259. return ret;
  1260. out_unlock:
  1261. xt_compat_flush_offsets(AF_INET);
  1262. xt_compat_unlock(AF_INET);
  1263. xt_entry_foreach(iter0, entry0, compatr->size) {
  1264. if (j-- == 0)
  1265. break;
  1266. compat_release_entry(iter0);
  1267. }
  1268. return ret;
  1269. }
  1270. static int
  1271. compat_do_replace(struct net *net, sockptr_t arg, unsigned int len)
  1272. {
  1273. int ret;
  1274. struct compat_ipt_replace tmp;
  1275. struct xt_table_info *newinfo;
  1276. void *loc_cpu_entry;
  1277. struct ipt_entry *iter;
  1278. if (copy_from_sockptr(&tmp, arg, sizeof(tmp)) != 0)
  1279. return -EFAULT;
  1280. /* overflow check */
  1281. if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
  1282. return -ENOMEM;
  1283. if (tmp.num_counters == 0)
  1284. return -EINVAL;
  1285. tmp.name[sizeof(tmp.name)-1] = 0;
  1286. newinfo = xt_alloc_table_info(tmp.size);
  1287. if (!newinfo)
  1288. return -ENOMEM;
  1289. loc_cpu_entry = newinfo->entries;
  1290. if (copy_from_sockptr_offset(loc_cpu_entry, arg, sizeof(tmp),
  1291. tmp.size) != 0) {
  1292. ret = -EFAULT;
  1293. goto free_newinfo;
  1294. }
  1295. ret = translate_compat_table(net, &newinfo, &loc_cpu_entry, &tmp);
  1296. if (ret != 0)
  1297. goto free_newinfo;
  1298. ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
  1299. tmp.num_counters, compat_ptr(tmp.counters));
  1300. if (ret)
  1301. goto free_newinfo_untrans;
  1302. return 0;
  1303. free_newinfo_untrans:
  1304. xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
  1305. cleanup_entry(iter, net);
  1306. free_newinfo:
  1307. xt_free_table_info(newinfo);
  1308. return ret;
  1309. }
  1310. struct compat_ipt_get_entries {
  1311. char name[XT_TABLE_MAXNAMELEN];
  1312. compat_uint_t size;
  1313. struct compat_ipt_entry entrytable[];
  1314. };
  1315. static int
  1316. compat_copy_entries_to_user(unsigned int total_size, struct xt_table *table,
  1317. void __user *userptr)
  1318. {
  1319. struct xt_counters *counters;
  1320. const struct xt_table_info *private = table->private;
  1321. void __user *pos;
  1322. unsigned int size;
  1323. int ret = 0;
  1324. unsigned int i = 0;
  1325. struct ipt_entry *iter;
  1326. counters = alloc_counters(table);
  1327. if (IS_ERR(counters))
  1328. return PTR_ERR(counters);
  1329. pos = userptr;
  1330. size = total_size;
  1331. xt_entry_foreach(iter, private->entries, total_size) {
  1332. ret = compat_copy_entry_to_user(iter, &pos,
  1333. &size, counters, i++);
  1334. if (ret != 0)
  1335. break;
  1336. }
  1337. vfree(counters);
  1338. return ret;
  1339. }
  1340. static int
  1341. compat_get_entries(struct net *net, struct compat_ipt_get_entries __user *uptr,
  1342. int *len)
  1343. {
  1344. int ret;
  1345. struct compat_ipt_get_entries get;
  1346. struct xt_table *t;
  1347. if (*len < sizeof(get))
  1348. return -EINVAL;
  1349. if (copy_from_user(&get, uptr, sizeof(get)) != 0)
  1350. return -EFAULT;
  1351. if (*len != sizeof(struct compat_ipt_get_entries) + get.size)
  1352. return -EINVAL;
  1353. get.name[sizeof(get.name) - 1] = '\0';
  1354. xt_compat_lock(AF_INET);
  1355. t = xt_find_table_lock(net, AF_INET, get.name);
  1356. if (!IS_ERR(t)) {
  1357. const struct xt_table_info *private = t->private;
  1358. struct xt_table_info info;
  1359. ret = compat_table_info(private, &info);
  1360. if (!ret && get.size == info.size)
  1361. ret = compat_copy_entries_to_user(private->size,
  1362. t, uptr->entrytable);
  1363. else if (!ret)
  1364. ret = -EAGAIN;
  1365. xt_compat_flush_offsets(AF_INET);
  1366. module_put(t->me);
  1367. xt_table_unlock(t);
  1368. } else
  1369. ret = PTR_ERR(t);
  1370. xt_compat_unlock(AF_INET);
  1371. return ret;
  1372. }
  1373. #endif
  1374. static int
  1375. do_ipt_set_ctl(struct sock *sk, int cmd, sockptr_t arg, unsigned int len)
  1376. {
  1377. int ret;
  1378. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  1379. return -EPERM;
  1380. switch (cmd) {
  1381. case IPT_SO_SET_REPLACE:
  1382. #ifdef CONFIG_COMPAT
  1383. if (in_compat_syscall())
  1384. ret = compat_do_replace(sock_net(sk), arg, len);
  1385. else
  1386. #endif
  1387. ret = do_replace(sock_net(sk), arg, len);
  1388. break;
  1389. case IPT_SO_SET_ADD_COUNTERS:
  1390. ret = do_add_counters(sock_net(sk), arg, len);
  1391. break;
  1392. default:
  1393. ret = -EINVAL;
  1394. }
  1395. return ret;
  1396. }
  1397. static int
  1398. do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
  1399. {
  1400. int ret;
  1401. if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
  1402. return -EPERM;
  1403. switch (cmd) {
  1404. case IPT_SO_GET_INFO:
  1405. ret = get_info(sock_net(sk), user, len);
  1406. break;
  1407. case IPT_SO_GET_ENTRIES:
  1408. #ifdef CONFIG_COMPAT
  1409. if (in_compat_syscall())
  1410. ret = compat_get_entries(sock_net(sk), user, len);
  1411. else
  1412. #endif
  1413. ret = get_entries(sock_net(sk), user, len);
  1414. break;
  1415. case IPT_SO_GET_REVISION_MATCH:
  1416. case IPT_SO_GET_REVISION_TARGET: {
  1417. struct xt_get_revision rev;
  1418. int target;
  1419. if (*len != sizeof(rev)) {
  1420. ret = -EINVAL;
  1421. break;
  1422. }
  1423. if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
  1424. ret = -EFAULT;
  1425. break;
  1426. }
  1427. rev.name[sizeof(rev.name)-1] = 0;
  1428. if (cmd == IPT_SO_GET_REVISION_TARGET)
  1429. target = 1;
  1430. else
  1431. target = 0;
  1432. try_then_request_module(xt_find_revision(AF_INET, rev.name,
  1433. rev.revision,
  1434. target, &ret),
  1435. "ipt_%s", rev.name);
  1436. break;
  1437. }
  1438. default:
  1439. ret = -EINVAL;
  1440. }
  1441. return ret;
  1442. }
  1443. static void __ipt_unregister_table(struct net *net, struct xt_table *table)
  1444. {
  1445. struct xt_table_info *private;
  1446. void *loc_cpu_entry;
  1447. struct module *table_owner = table->me;
  1448. struct ipt_entry *iter;
  1449. private = xt_unregister_table(table);
  1450. /* Decrease module usage counts and free resources */
  1451. loc_cpu_entry = private->entries;
  1452. xt_entry_foreach(iter, loc_cpu_entry, private->size)
  1453. cleanup_entry(iter, net);
  1454. if (private->number > private->initial_entries)
  1455. module_put(table_owner);
  1456. xt_free_table_info(private);
  1457. }
  1458. int ipt_register_table(struct net *net, const struct xt_table *table,
  1459. const struct ipt_replace *repl,
  1460. const struct nf_hook_ops *ops, struct xt_table **res)
  1461. {
  1462. int ret;
  1463. struct xt_table_info *newinfo;
  1464. struct xt_table_info bootstrap = {0};
  1465. void *loc_cpu_entry;
  1466. struct xt_table *new_table;
  1467. newinfo = xt_alloc_table_info(repl->size);
  1468. if (!newinfo)
  1469. return -ENOMEM;
  1470. loc_cpu_entry = newinfo->entries;
  1471. memcpy(loc_cpu_entry, repl->entries, repl->size);
  1472. ret = translate_table(net, newinfo, loc_cpu_entry, repl);
  1473. if (ret != 0)
  1474. goto out_free;
  1475. new_table = xt_register_table(net, table, &bootstrap, newinfo);
  1476. if (IS_ERR(new_table)) {
  1477. ret = PTR_ERR(new_table);
  1478. goto out_free;
  1479. }
  1480. /* set res now, will see skbs right after nf_register_net_hooks */
  1481. WRITE_ONCE(*res, new_table);
  1482. if (!ops)
  1483. return 0;
  1484. ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
  1485. if (ret != 0) {
  1486. __ipt_unregister_table(net, new_table);
  1487. *res = NULL;
  1488. }
  1489. return ret;
  1490. out_free:
  1491. xt_free_table_info(newinfo);
  1492. return ret;
  1493. }
  1494. void ipt_unregister_table_pre_exit(struct net *net, struct xt_table *table,
  1495. const struct nf_hook_ops *ops)
  1496. {
  1497. nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
  1498. }
  1499. void ipt_unregister_table_exit(struct net *net, struct xt_table *table)
  1500. {
  1501. __ipt_unregister_table(net, table);
  1502. }
  1503. void ipt_unregister_table(struct net *net, struct xt_table *table,
  1504. const struct nf_hook_ops *ops)
  1505. {
  1506. if (ops)
  1507. ipt_unregister_table_pre_exit(net, table, ops);
  1508. __ipt_unregister_table(net, table);
  1509. }
  1510. /* Returns 1 if the type and code is matched by the range, 0 otherwise */
  1511. static inline bool
  1512. icmp_type_code_match(u_int8_t test_type, u_int8_t min_code, u_int8_t max_code,
  1513. u_int8_t type, u_int8_t code,
  1514. bool invert)
  1515. {
  1516. return ((test_type == 0xFF) ||
  1517. (type == test_type && code >= min_code && code <= max_code))
  1518. ^ invert;
  1519. }
  1520. static bool
  1521. icmp_match(const struct sk_buff *skb, struct xt_action_param *par)
  1522. {
  1523. const struct icmphdr *ic;
  1524. struct icmphdr _icmph;
  1525. const struct ipt_icmp *icmpinfo = par->matchinfo;
  1526. /* Must not be a fragment. */
  1527. if (par->fragoff != 0)
  1528. return false;
  1529. ic = skb_header_pointer(skb, par->thoff, sizeof(_icmph), &_icmph);
  1530. if (ic == NULL) {
  1531. /* We've been asked to examine this packet, and we
  1532. * can't. Hence, no choice but to drop.
  1533. */
  1534. par->hotdrop = true;
  1535. return false;
  1536. }
  1537. return icmp_type_code_match(icmpinfo->type,
  1538. icmpinfo->code[0],
  1539. icmpinfo->code[1],
  1540. ic->type, ic->code,
  1541. !!(icmpinfo->invflags&IPT_ICMP_INV));
  1542. }
  1543. static int icmp_checkentry(const struct xt_mtchk_param *par)
  1544. {
  1545. const struct ipt_icmp *icmpinfo = par->matchinfo;
  1546. /* Must specify no unknown invflags */
  1547. return (icmpinfo->invflags & ~IPT_ICMP_INV) ? -EINVAL : 0;
  1548. }
  1549. static struct xt_target ipt_builtin_tg[] __read_mostly = {
  1550. {
  1551. .name = XT_STANDARD_TARGET,
  1552. .targetsize = sizeof(int),
  1553. .family = NFPROTO_IPV4,
  1554. #ifdef CONFIG_COMPAT
  1555. .compatsize = sizeof(compat_int_t),
  1556. .compat_from_user = compat_standard_from_user,
  1557. .compat_to_user = compat_standard_to_user,
  1558. #endif
  1559. },
  1560. {
  1561. .name = XT_ERROR_TARGET,
  1562. .target = ipt_error,
  1563. .targetsize = XT_FUNCTION_MAXNAMELEN,
  1564. .family = NFPROTO_IPV4,
  1565. },
  1566. };
  1567. static struct nf_sockopt_ops ipt_sockopts = {
  1568. .pf = PF_INET,
  1569. .set_optmin = IPT_BASE_CTL,
  1570. .set_optmax = IPT_SO_SET_MAX+1,
  1571. .set = do_ipt_set_ctl,
  1572. .get_optmin = IPT_BASE_CTL,
  1573. .get_optmax = IPT_SO_GET_MAX+1,
  1574. .get = do_ipt_get_ctl,
  1575. .owner = THIS_MODULE,
  1576. };
  1577. static struct xt_match ipt_builtin_mt[] __read_mostly = {
  1578. {
  1579. .name = "icmp",
  1580. .match = icmp_match,
  1581. .matchsize = sizeof(struct ipt_icmp),
  1582. .checkentry = icmp_checkentry,
  1583. .proto = IPPROTO_ICMP,
  1584. .family = NFPROTO_IPV4,
  1585. .me = THIS_MODULE,
  1586. },
  1587. };
  1588. static int __net_init ip_tables_net_init(struct net *net)
  1589. {
  1590. return xt_proto_init(net, NFPROTO_IPV4);
  1591. }
  1592. static void __net_exit ip_tables_net_exit(struct net *net)
  1593. {
  1594. xt_proto_fini(net, NFPROTO_IPV4);
  1595. }
  1596. static struct pernet_operations ip_tables_net_ops = {
  1597. .init = ip_tables_net_init,
  1598. .exit = ip_tables_net_exit,
  1599. };
  1600. static int __init ip_tables_init(void)
  1601. {
  1602. int ret;
  1603. ret = register_pernet_subsys(&ip_tables_net_ops);
  1604. if (ret < 0)
  1605. goto err1;
  1606. /* No one else will be downing sem now, so we won't sleep */
  1607. ret = xt_register_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
  1608. if (ret < 0)
  1609. goto err2;
  1610. ret = xt_register_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
  1611. if (ret < 0)
  1612. goto err4;
  1613. /* Register setsockopt */
  1614. ret = nf_register_sockopt(&ipt_sockopts);
  1615. if (ret < 0)
  1616. goto err5;
  1617. return 0;
  1618. err5:
  1619. xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
  1620. err4:
  1621. xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
  1622. err2:
  1623. unregister_pernet_subsys(&ip_tables_net_ops);
  1624. err1:
  1625. return ret;
  1626. }
  1627. static void __exit ip_tables_fini(void)
  1628. {
  1629. nf_unregister_sockopt(&ipt_sockopts);
  1630. xt_unregister_matches(ipt_builtin_mt, ARRAY_SIZE(ipt_builtin_mt));
  1631. xt_unregister_targets(ipt_builtin_tg, ARRAY_SIZE(ipt_builtin_tg));
  1632. unregister_pernet_subsys(&ip_tables_net_ops);
  1633. }
  1634. EXPORT_SYMBOL(ipt_register_table);
  1635. EXPORT_SYMBOL(ipt_unregister_table);
  1636. EXPORT_SYMBOL(ipt_unregister_table_pre_exit);
  1637. EXPORT_SYMBOL(ipt_unregister_table_exit);
  1638. EXPORT_SYMBOL(ipt_do_table);
  1639. module_init(ip_tables_init);
  1640. module_exit(ip_tables_fini);