ip_tables.c 52 KB

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