x_tables.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  1. /*
  2. * x_tables core - Backend for {ip,ip6,arp}_tables
  3. *
  4. * Copyright (C) 2006-2006 Harald Welte <laforge@netfilter.org>
  5. * Copyright (C) 2006-2012 Patrick McHardy <kaber@trash.net>
  6. *
  7. * Based on existing ip_tables code which is
  8. * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
  9. * Copyright (C) 2000-2005 Netfilter Core Team <coreteam@netfilter.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/socket.h>
  20. #include <linux/net.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/string.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/mutex.h>
  26. #include <linux/mm.h>
  27. #include <linux/slab.h>
  28. #include <linux/audit.h>
  29. #include <linux/user_namespace.h>
  30. #include <net/net_namespace.h>
  31. #include <linux/netfilter/x_tables.h>
  32. #include <linux/netfilter_arp.h>
  33. #include <linux/netfilter_ipv4/ip_tables.h>
  34. #include <linux/netfilter_ipv6/ip6_tables.h>
  35. #include <linux/netfilter_arp/arp_tables.h>
  36. MODULE_LICENSE("GPL");
  37. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  38. MODULE_DESCRIPTION("{ip,ip6,arp,eb}_tables backend module");
  39. #define XT_PCPU_BLOCK_SIZE 4096
  40. struct compat_delta {
  41. unsigned int offset; /* offset in kernel */
  42. int delta; /* delta in 32bit user land */
  43. };
  44. struct xt_af {
  45. struct mutex mutex;
  46. struct list_head match;
  47. struct list_head target;
  48. #ifdef CONFIG_COMPAT
  49. struct mutex compat_mutex;
  50. struct compat_delta *compat_tab;
  51. unsigned int number; /* number of slots in compat_tab[] */
  52. unsigned int cur; /* number of used slots in compat_tab[] */
  53. #endif
  54. };
  55. static struct xt_af *xt;
  56. static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
  57. [NFPROTO_UNSPEC] = "x",
  58. [NFPROTO_IPV4] = "ip",
  59. [NFPROTO_ARP] = "arp",
  60. [NFPROTO_BRIDGE] = "eb",
  61. [NFPROTO_IPV6] = "ip6",
  62. };
  63. /* Registration hooks for targets. */
  64. int xt_register_target(struct xt_target *target)
  65. {
  66. u_int8_t af = target->family;
  67. mutex_lock(&xt[af].mutex);
  68. list_add(&target->list, &xt[af].target);
  69. mutex_unlock(&xt[af].mutex);
  70. return 0;
  71. }
  72. EXPORT_SYMBOL(xt_register_target);
  73. void
  74. xt_unregister_target(struct xt_target *target)
  75. {
  76. u_int8_t af = target->family;
  77. mutex_lock(&xt[af].mutex);
  78. list_del(&target->list);
  79. mutex_unlock(&xt[af].mutex);
  80. }
  81. EXPORT_SYMBOL(xt_unregister_target);
  82. int
  83. xt_register_targets(struct xt_target *target, unsigned int n)
  84. {
  85. unsigned int i;
  86. int err = 0;
  87. for (i = 0; i < n; i++) {
  88. err = xt_register_target(&target[i]);
  89. if (err)
  90. goto err;
  91. }
  92. return err;
  93. err:
  94. if (i > 0)
  95. xt_unregister_targets(target, i);
  96. return err;
  97. }
  98. EXPORT_SYMBOL(xt_register_targets);
  99. void
  100. xt_unregister_targets(struct xt_target *target, unsigned int n)
  101. {
  102. while (n-- > 0)
  103. xt_unregister_target(&target[n]);
  104. }
  105. EXPORT_SYMBOL(xt_unregister_targets);
  106. int xt_register_match(struct xt_match *match)
  107. {
  108. u_int8_t af = match->family;
  109. mutex_lock(&xt[af].mutex);
  110. list_add(&match->list, &xt[af].match);
  111. mutex_unlock(&xt[af].mutex);
  112. return 0;
  113. }
  114. EXPORT_SYMBOL(xt_register_match);
  115. void
  116. xt_unregister_match(struct xt_match *match)
  117. {
  118. u_int8_t af = match->family;
  119. mutex_lock(&xt[af].mutex);
  120. list_del(&match->list);
  121. mutex_unlock(&xt[af].mutex);
  122. }
  123. EXPORT_SYMBOL(xt_unregister_match);
  124. int
  125. xt_register_matches(struct xt_match *match, unsigned int n)
  126. {
  127. unsigned int i;
  128. int err = 0;
  129. for (i = 0; i < n; i++) {
  130. err = xt_register_match(&match[i]);
  131. if (err)
  132. goto err;
  133. }
  134. return err;
  135. err:
  136. if (i > 0)
  137. xt_unregister_matches(match, i);
  138. return err;
  139. }
  140. EXPORT_SYMBOL(xt_register_matches);
  141. void
  142. xt_unregister_matches(struct xt_match *match, unsigned int n)
  143. {
  144. while (n-- > 0)
  145. xt_unregister_match(&match[n]);
  146. }
  147. EXPORT_SYMBOL(xt_unregister_matches);
  148. /*
  149. * These are weird, but module loading must not be done with mutex
  150. * held (since they will register), and we have to have a single
  151. * function to use.
  152. */
  153. /* Find match, grabs ref. Returns ERR_PTR() on error. */
  154. struct xt_match *xt_find_match(u8 af, const char *name, u8 revision)
  155. {
  156. struct xt_match *m;
  157. int err = -ENOENT;
  158. mutex_lock(&xt[af].mutex);
  159. list_for_each_entry(m, &xt[af].match, list) {
  160. if (strcmp(m->name, name) == 0) {
  161. if (m->revision == revision) {
  162. if (try_module_get(m->me)) {
  163. mutex_unlock(&xt[af].mutex);
  164. return m;
  165. }
  166. } else
  167. err = -EPROTOTYPE; /* Found something. */
  168. }
  169. }
  170. mutex_unlock(&xt[af].mutex);
  171. if (af != NFPROTO_UNSPEC)
  172. /* Try searching again in the family-independent list */
  173. return xt_find_match(NFPROTO_UNSPEC, name, revision);
  174. return ERR_PTR(err);
  175. }
  176. EXPORT_SYMBOL(xt_find_match);
  177. struct xt_match *
  178. xt_request_find_match(uint8_t nfproto, const char *name, uint8_t revision)
  179. {
  180. struct xt_match *match;
  181. if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
  182. return ERR_PTR(-EINVAL);
  183. match = xt_find_match(nfproto, name, revision);
  184. if (IS_ERR(match)) {
  185. request_module("%st_%s", xt_prefix[nfproto], name);
  186. match = xt_find_match(nfproto, name, revision);
  187. }
  188. return match;
  189. }
  190. EXPORT_SYMBOL_GPL(xt_request_find_match);
  191. /* Find target, grabs ref. Returns ERR_PTR() on error. */
  192. struct xt_target *xt_find_target(u8 af, const char *name, u8 revision)
  193. {
  194. struct xt_target *t;
  195. int err = -ENOENT;
  196. mutex_lock(&xt[af].mutex);
  197. list_for_each_entry(t, &xt[af].target, list) {
  198. if (strcmp(t->name, name) == 0) {
  199. if (t->revision == revision) {
  200. if (try_module_get(t->me)) {
  201. mutex_unlock(&xt[af].mutex);
  202. return t;
  203. }
  204. } else
  205. err = -EPROTOTYPE; /* Found something. */
  206. }
  207. }
  208. mutex_unlock(&xt[af].mutex);
  209. if (af != NFPROTO_UNSPEC)
  210. /* Try searching again in the family-independent list */
  211. return xt_find_target(NFPROTO_UNSPEC, name, revision);
  212. return ERR_PTR(err);
  213. }
  214. EXPORT_SYMBOL(xt_find_target);
  215. struct xt_target *xt_request_find_target(u8 af, const char *name, u8 revision)
  216. {
  217. struct xt_target *target;
  218. if (strnlen(name, XT_EXTENSION_MAXNAMELEN) == XT_EXTENSION_MAXNAMELEN)
  219. return ERR_PTR(-EINVAL);
  220. target = xt_find_target(af, name, revision);
  221. if (IS_ERR(target)) {
  222. request_module("%st_%s", xt_prefix[af], name);
  223. target = xt_find_target(af, name, revision);
  224. }
  225. return target;
  226. }
  227. EXPORT_SYMBOL_GPL(xt_request_find_target);
  228. static int match_revfn(u8 af, const char *name, u8 revision, int *bestp)
  229. {
  230. const struct xt_match *m;
  231. int have_rev = 0;
  232. list_for_each_entry(m, &xt[af].match, list) {
  233. if (strcmp(m->name, name) == 0) {
  234. if (m->revision > *bestp)
  235. *bestp = m->revision;
  236. if (m->revision == revision)
  237. have_rev = 1;
  238. }
  239. }
  240. if (af != NFPROTO_UNSPEC && !have_rev)
  241. return match_revfn(NFPROTO_UNSPEC, name, revision, bestp);
  242. return have_rev;
  243. }
  244. static int target_revfn(u8 af, const char *name, u8 revision, int *bestp)
  245. {
  246. const struct xt_target *t;
  247. int have_rev = 0;
  248. list_for_each_entry(t, &xt[af].target, list) {
  249. if (strcmp(t->name, name) == 0) {
  250. if (t->revision > *bestp)
  251. *bestp = t->revision;
  252. if (t->revision == revision)
  253. have_rev = 1;
  254. }
  255. }
  256. if (af != NFPROTO_UNSPEC && !have_rev)
  257. return target_revfn(NFPROTO_UNSPEC, name, revision, bestp);
  258. return have_rev;
  259. }
  260. /* Returns true or false (if no such extension at all) */
  261. int xt_find_revision(u8 af, const char *name, u8 revision, int target,
  262. int *err)
  263. {
  264. int have_rev, best = -1;
  265. mutex_lock(&xt[af].mutex);
  266. if (target == 1)
  267. have_rev = target_revfn(af, name, revision, &best);
  268. else
  269. have_rev = match_revfn(af, name, revision, &best);
  270. mutex_unlock(&xt[af].mutex);
  271. /* Nothing at all? Return 0 to try loading module. */
  272. if (best == -1) {
  273. *err = -ENOENT;
  274. return 0;
  275. }
  276. *err = best;
  277. if (!have_rev)
  278. *err = -EPROTONOSUPPORT;
  279. return 1;
  280. }
  281. EXPORT_SYMBOL_GPL(xt_find_revision);
  282. static char *
  283. textify_hooks(char *buf, size_t size, unsigned int mask, uint8_t nfproto)
  284. {
  285. static const char *const inetbr_names[] = {
  286. "PREROUTING", "INPUT", "FORWARD",
  287. "OUTPUT", "POSTROUTING", "BROUTING",
  288. };
  289. static const char *const arp_names[] = {
  290. "INPUT", "FORWARD", "OUTPUT",
  291. };
  292. const char *const *names;
  293. unsigned int i, max;
  294. char *p = buf;
  295. bool np = false;
  296. int res;
  297. names = (nfproto == NFPROTO_ARP) ? arp_names : inetbr_names;
  298. max = (nfproto == NFPROTO_ARP) ? ARRAY_SIZE(arp_names) :
  299. ARRAY_SIZE(inetbr_names);
  300. *p = '\0';
  301. for (i = 0; i < max; ++i) {
  302. if (!(mask & (1 << i)))
  303. continue;
  304. res = snprintf(p, size, "%s%s", np ? "/" : "", names[i]);
  305. if (res > 0) {
  306. size -= res;
  307. p += res;
  308. }
  309. np = true;
  310. }
  311. return buf;
  312. }
  313. /**
  314. * xt_check_proc_name - check that name is suitable for /proc file creation
  315. *
  316. * @name: file name candidate
  317. * @size: length of buffer
  318. *
  319. * some x_tables modules wish to create a file in /proc.
  320. * This function makes sure that the name is suitable for this
  321. * purpose, it checks that name is NUL terminated and isn't a 'special'
  322. * name, like "..".
  323. *
  324. * returns negative number on error or 0 if name is useable.
  325. */
  326. int xt_check_proc_name(const char *name, unsigned int size)
  327. {
  328. if (name[0] == '\0')
  329. return -EINVAL;
  330. if (strnlen(name, size) == size)
  331. return -ENAMETOOLONG;
  332. if (strcmp(name, ".") == 0 ||
  333. strcmp(name, "..") == 0 ||
  334. strchr(name, '/'))
  335. return -EINVAL;
  336. return 0;
  337. }
  338. EXPORT_SYMBOL(xt_check_proc_name);
  339. int xt_check_match(struct xt_mtchk_param *par,
  340. unsigned int size, u_int8_t proto, bool inv_proto)
  341. {
  342. int ret;
  343. if (XT_ALIGN(par->match->matchsize) != size &&
  344. par->match->matchsize != -1) {
  345. /*
  346. * ebt_among is exempt from centralized matchsize checking
  347. * because it uses a dynamic-size data set.
  348. */
  349. pr_err("%s_tables: %s.%u match: invalid size "
  350. "%u (kernel) != (user) %u\n",
  351. xt_prefix[par->family], par->match->name,
  352. par->match->revision,
  353. XT_ALIGN(par->match->matchsize), size);
  354. return -EINVAL;
  355. }
  356. if (par->match->table != NULL &&
  357. strcmp(par->match->table, par->table) != 0) {
  358. pr_err("%s_tables: %s match: only valid in %s table, not %s\n",
  359. xt_prefix[par->family], par->match->name,
  360. par->match->table, par->table);
  361. return -EINVAL;
  362. }
  363. if (par->match->hooks && (par->hook_mask & ~par->match->hooks) != 0) {
  364. char used[64], allow[64];
  365. pr_err("%s_tables: %s match: used from hooks %s, but only "
  366. "valid from %s\n",
  367. xt_prefix[par->family], par->match->name,
  368. textify_hooks(used, sizeof(used), par->hook_mask,
  369. par->family),
  370. textify_hooks(allow, sizeof(allow), par->match->hooks,
  371. par->family));
  372. return -EINVAL;
  373. }
  374. if (par->match->proto && (par->match->proto != proto || inv_proto)) {
  375. pr_err("%s_tables: %s match: only valid for protocol %u\n",
  376. xt_prefix[par->family], par->match->name,
  377. par->match->proto);
  378. return -EINVAL;
  379. }
  380. if (par->match->checkentry != NULL) {
  381. ret = par->match->checkentry(par);
  382. if (ret < 0)
  383. return ret;
  384. else if (ret > 0)
  385. /* Flag up potential errors. */
  386. return -EIO;
  387. }
  388. return 0;
  389. }
  390. EXPORT_SYMBOL_GPL(xt_check_match);
  391. /** xt_check_entry_match - check that matches end before start of target
  392. *
  393. * @match: beginning of xt_entry_match
  394. * @target: beginning of this rules target (alleged end of matches)
  395. * @alignment: alignment requirement of match structures
  396. *
  397. * Validates that all matches add up to the beginning of the target,
  398. * and that each match covers at least the base structure size.
  399. *
  400. * Return: 0 on success, negative errno on failure.
  401. */
  402. static int xt_check_entry_match(const char *match, const char *target,
  403. const size_t alignment)
  404. {
  405. const struct xt_entry_match *pos;
  406. int length = target - match;
  407. if (length == 0) /* no matches */
  408. return 0;
  409. pos = (struct xt_entry_match *)match;
  410. do {
  411. if ((unsigned long)pos % alignment)
  412. return -EINVAL;
  413. if (length < (int)sizeof(struct xt_entry_match))
  414. return -EINVAL;
  415. if (pos->u.match_size < sizeof(struct xt_entry_match))
  416. return -EINVAL;
  417. if (pos->u.match_size > length)
  418. return -EINVAL;
  419. length -= pos->u.match_size;
  420. pos = ((void *)((char *)(pos) + (pos)->u.match_size));
  421. } while (length > 0);
  422. return 0;
  423. }
  424. #ifdef CONFIG_COMPAT
  425. int xt_compat_add_offset(u_int8_t af, unsigned int offset, int delta)
  426. {
  427. struct xt_af *xp = &xt[af];
  428. if (!xp->compat_tab) {
  429. if (!xp->number)
  430. return -EINVAL;
  431. xp->compat_tab = vmalloc(sizeof(struct compat_delta) * xp->number);
  432. if (!xp->compat_tab)
  433. return -ENOMEM;
  434. xp->cur = 0;
  435. }
  436. if (xp->cur >= xp->number)
  437. return -EINVAL;
  438. if (xp->cur)
  439. delta += xp->compat_tab[xp->cur - 1].delta;
  440. xp->compat_tab[xp->cur].offset = offset;
  441. xp->compat_tab[xp->cur].delta = delta;
  442. xp->cur++;
  443. return 0;
  444. }
  445. EXPORT_SYMBOL_GPL(xt_compat_add_offset);
  446. void xt_compat_flush_offsets(u_int8_t af)
  447. {
  448. if (xt[af].compat_tab) {
  449. vfree(xt[af].compat_tab);
  450. xt[af].compat_tab = NULL;
  451. xt[af].number = 0;
  452. xt[af].cur = 0;
  453. }
  454. }
  455. EXPORT_SYMBOL_GPL(xt_compat_flush_offsets);
  456. int xt_compat_calc_jump(u_int8_t af, unsigned int offset)
  457. {
  458. struct compat_delta *tmp = xt[af].compat_tab;
  459. int mid, left = 0, right = xt[af].cur - 1;
  460. while (left <= right) {
  461. mid = (left + right) >> 1;
  462. if (offset > tmp[mid].offset)
  463. left = mid + 1;
  464. else if (offset < tmp[mid].offset)
  465. right = mid - 1;
  466. else
  467. return mid ? tmp[mid - 1].delta : 0;
  468. }
  469. return left ? tmp[left - 1].delta : 0;
  470. }
  471. EXPORT_SYMBOL_GPL(xt_compat_calc_jump);
  472. void xt_compat_init_offsets(u_int8_t af, unsigned int number)
  473. {
  474. xt[af].number = number;
  475. xt[af].cur = 0;
  476. }
  477. EXPORT_SYMBOL(xt_compat_init_offsets);
  478. int xt_compat_match_offset(const struct xt_match *match)
  479. {
  480. u_int16_t csize = match->compatsize ? : match->matchsize;
  481. return XT_ALIGN(match->matchsize) - COMPAT_XT_ALIGN(csize);
  482. }
  483. EXPORT_SYMBOL_GPL(xt_compat_match_offset);
  484. void xt_compat_match_from_user(struct xt_entry_match *m, void **dstptr,
  485. unsigned int *size)
  486. {
  487. const struct xt_match *match = m->u.kernel.match;
  488. struct compat_xt_entry_match *cm = (struct compat_xt_entry_match *)m;
  489. int pad, off = xt_compat_match_offset(match);
  490. u_int16_t msize = cm->u.user.match_size;
  491. char name[sizeof(m->u.user.name)];
  492. m = *dstptr;
  493. memcpy(m, cm, sizeof(*cm));
  494. if (match->compat_from_user)
  495. match->compat_from_user(m->data, cm->data);
  496. else
  497. memcpy(m->data, cm->data, msize - sizeof(*cm));
  498. pad = XT_ALIGN(match->matchsize) - match->matchsize;
  499. if (pad > 0)
  500. memset(m->data + match->matchsize, 0, pad);
  501. msize += off;
  502. m->u.user.match_size = msize;
  503. strlcpy(name, match->name, sizeof(name));
  504. module_put(match->me);
  505. strncpy(m->u.user.name, name, sizeof(m->u.user.name));
  506. *size += off;
  507. *dstptr += msize;
  508. }
  509. EXPORT_SYMBOL_GPL(xt_compat_match_from_user);
  510. int xt_compat_match_to_user(const struct xt_entry_match *m,
  511. void __user **dstptr, unsigned int *size)
  512. {
  513. const struct xt_match *match = m->u.kernel.match;
  514. struct compat_xt_entry_match __user *cm = *dstptr;
  515. int off = xt_compat_match_offset(match);
  516. u_int16_t msize = m->u.user.match_size - off;
  517. if (copy_to_user(cm, m, sizeof(*cm)) ||
  518. put_user(msize, &cm->u.user.match_size) ||
  519. copy_to_user(cm->u.user.name, m->u.kernel.match->name,
  520. strlen(m->u.kernel.match->name) + 1))
  521. return -EFAULT;
  522. if (match->compat_to_user) {
  523. if (match->compat_to_user((void __user *)cm->data, m->data))
  524. return -EFAULT;
  525. } else {
  526. if (copy_to_user(cm->data, m->data, msize - sizeof(*cm)))
  527. return -EFAULT;
  528. }
  529. *size -= off;
  530. *dstptr += msize;
  531. return 0;
  532. }
  533. EXPORT_SYMBOL_GPL(xt_compat_match_to_user);
  534. /* non-compat version may have padding after verdict */
  535. struct compat_xt_standard_target {
  536. struct compat_xt_entry_target t;
  537. compat_uint_t verdict;
  538. };
  539. int xt_compat_check_entry_offsets(const void *base, const char *elems,
  540. unsigned int target_offset,
  541. unsigned int next_offset)
  542. {
  543. long size_of_base_struct = elems - (const char *)base;
  544. const struct compat_xt_entry_target *t;
  545. const char *e = base;
  546. if (target_offset < size_of_base_struct)
  547. return -EINVAL;
  548. if (target_offset + sizeof(*t) > next_offset)
  549. return -EINVAL;
  550. t = (void *)(e + target_offset);
  551. if (t->u.target_size < sizeof(*t))
  552. return -EINVAL;
  553. if (target_offset + t->u.target_size > next_offset)
  554. return -EINVAL;
  555. if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
  556. COMPAT_XT_ALIGN(target_offset + sizeof(struct compat_xt_standard_target)) != next_offset)
  557. return -EINVAL;
  558. /* compat_xt_entry match has less strict aligment requirements,
  559. * otherwise they are identical. In case of padding differences
  560. * we need to add compat version of xt_check_entry_match.
  561. */
  562. BUILD_BUG_ON(sizeof(struct compat_xt_entry_match) != sizeof(struct xt_entry_match));
  563. return xt_check_entry_match(elems, base + target_offset,
  564. __alignof__(struct compat_xt_entry_match));
  565. }
  566. EXPORT_SYMBOL(xt_compat_check_entry_offsets);
  567. #endif /* CONFIG_COMPAT */
  568. /**
  569. * xt_check_entry_offsets - validate arp/ip/ip6t_entry
  570. *
  571. * @base: pointer to arp/ip/ip6t_entry
  572. * @elems: pointer to first xt_entry_match, i.e. ip(6)t_entry->elems
  573. * @target_offset: the arp/ip/ip6_t->target_offset
  574. * @next_offset: the arp/ip/ip6_t->next_offset
  575. *
  576. * validates that target_offset and next_offset are sane and that all
  577. * match sizes (if any) align with the target offset.
  578. *
  579. * This function does not validate the targets or matches themselves, it
  580. * only tests that all the offsets and sizes are correct, that all
  581. * match structures are aligned, and that the last structure ends where
  582. * the target structure begins.
  583. *
  584. * Also see xt_compat_check_entry_offsets for CONFIG_COMPAT version.
  585. *
  586. * The arp/ip/ip6t_entry structure @base must have passed following tests:
  587. * - it must point to a valid memory location
  588. * - base to base + next_offset must be accessible, i.e. not exceed allocated
  589. * length.
  590. *
  591. * A well-formed entry looks like this:
  592. *
  593. * ip(6)t_entry match [mtdata] match [mtdata] target [tgdata] ip(6)t_entry
  594. * e->elems[]-----' | |
  595. * matchsize | |
  596. * matchsize | |
  597. * | |
  598. * target_offset---------------------------------' |
  599. * next_offset---------------------------------------------------'
  600. *
  601. * elems[]: flexible array member at end of ip(6)/arpt_entry struct.
  602. * This is where matches (if any) and the target reside.
  603. * target_offset: beginning of target.
  604. * next_offset: start of the next rule; also: size of this rule.
  605. * Since targets have a minimum size, target_offset + minlen <= next_offset.
  606. *
  607. * Every match stores its size, sum of sizes must not exceed target_offset.
  608. *
  609. * Return: 0 on success, negative errno on failure.
  610. */
  611. int xt_check_entry_offsets(const void *base,
  612. const char *elems,
  613. unsigned int target_offset,
  614. unsigned int next_offset)
  615. {
  616. long size_of_base_struct = elems - (const char *)base;
  617. const struct xt_entry_target *t;
  618. const char *e = base;
  619. /* target start is within the ip/ip6/arpt_entry struct */
  620. if (target_offset < size_of_base_struct)
  621. return -EINVAL;
  622. if (target_offset + sizeof(*t) > next_offset)
  623. return -EINVAL;
  624. t = (void *)(e + target_offset);
  625. if (t->u.target_size < sizeof(*t))
  626. return -EINVAL;
  627. if (target_offset + t->u.target_size > next_offset)
  628. return -EINVAL;
  629. if (strcmp(t->u.user.name, XT_STANDARD_TARGET) == 0 &&
  630. XT_ALIGN(target_offset + sizeof(struct xt_standard_target)) != next_offset)
  631. return -EINVAL;
  632. return xt_check_entry_match(elems, base + target_offset,
  633. __alignof__(struct xt_entry_match));
  634. }
  635. EXPORT_SYMBOL(xt_check_entry_offsets);
  636. /**
  637. * xt_alloc_entry_offsets - allocate array to store rule head offsets
  638. *
  639. * @size: number of entries
  640. *
  641. * Return: NULL or kmalloc'd or vmalloc'd array
  642. */
  643. unsigned int *xt_alloc_entry_offsets(unsigned int size)
  644. {
  645. unsigned int *off;
  646. off = kcalloc(size, sizeof(unsigned int), GFP_KERNEL | __GFP_NOWARN);
  647. if (off)
  648. return off;
  649. if (size < (SIZE_MAX / sizeof(unsigned int)))
  650. off = vmalloc(size * sizeof(unsigned int));
  651. return off;
  652. }
  653. EXPORT_SYMBOL(xt_alloc_entry_offsets);
  654. /**
  655. * xt_find_jump_offset - check if target is a valid jump offset
  656. *
  657. * @offsets: array containing all valid rule start offsets of a rule blob
  658. * @target: the jump target to search for
  659. * @size: entries in @offset
  660. */
  661. bool xt_find_jump_offset(const unsigned int *offsets,
  662. unsigned int target, unsigned int size)
  663. {
  664. int m, low = 0, hi = size;
  665. while (hi > low) {
  666. m = (low + hi) / 2u;
  667. if (offsets[m] > target)
  668. hi = m;
  669. else if (offsets[m] < target)
  670. low = m + 1;
  671. else
  672. return true;
  673. }
  674. return false;
  675. }
  676. EXPORT_SYMBOL(xt_find_jump_offset);
  677. int xt_check_target(struct xt_tgchk_param *par,
  678. unsigned int size, u_int8_t proto, bool inv_proto)
  679. {
  680. int ret;
  681. if (XT_ALIGN(par->target->targetsize) != size) {
  682. pr_err("%s_tables: %s.%u target: invalid size "
  683. "%u (kernel) != (user) %u\n",
  684. xt_prefix[par->family], par->target->name,
  685. par->target->revision,
  686. XT_ALIGN(par->target->targetsize), size);
  687. return -EINVAL;
  688. }
  689. if (par->target->table != NULL &&
  690. strcmp(par->target->table, par->table) != 0) {
  691. pr_err("%s_tables: %s target: only valid in %s table, not %s\n",
  692. xt_prefix[par->family], par->target->name,
  693. par->target->table, par->table);
  694. return -EINVAL;
  695. }
  696. if (par->target->hooks && (par->hook_mask & ~par->target->hooks) != 0) {
  697. char used[64], allow[64];
  698. pr_err("%s_tables: %s target: used from hooks %s, but only "
  699. "usable from %s\n",
  700. xt_prefix[par->family], par->target->name,
  701. textify_hooks(used, sizeof(used), par->hook_mask,
  702. par->family),
  703. textify_hooks(allow, sizeof(allow), par->target->hooks,
  704. par->family));
  705. return -EINVAL;
  706. }
  707. if (par->target->proto && (par->target->proto != proto || inv_proto)) {
  708. pr_err("%s_tables: %s target: only valid for protocol %u\n",
  709. xt_prefix[par->family], par->target->name,
  710. par->target->proto);
  711. return -EINVAL;
  712. }
  713. if (par->target->checkentry != NULL) {
  714. ret = par->target->checkentry(par);
  715. if (ret < 0)
  716. return ret;
  717. else if (ret > 0)
  718. /* Flag up potential errors. */
  719. return -EIO;
  720. }
  721. return 0;
  722. }
  723. EXPORT_SYMBOL_GPL(xt_check_target);
  724. /**
  725. * xt_copy_counters_from_user - copy counters and metadata from userspace
  726. *
  727. * @user: src pointer to userspace memory
  728. * @len: alleged size of userspace memory
  729. * @info: where to store the xt_counters_info metadata
  730. * @compat: true if we setsockopt call is done by 32bit task on 64bit kernel
  731. *
  732. * Copies counter meta data from @user and stores it in @info.
  733. *
  734. * vmallocs memory to hold the counters, then copies the counter data
  735. * from @user to the new memory and returns a pointer to it.
  736. *
  737. * If @compat is true, @info gets converted automatically to the 64bit
  738. * representation.
  739. *
  740. * The metadata associated with the counters is stored in @info.
  741. *
  742. * Return: returns pointer that caller has to test via IS_ERR().
  743. * If IS_ERR is false, caller has to vfree the pointer.
  744. */
  745. void *xt_copy_counters_from_user(const void __user *user, unsigned int len,
  746. struct xt_counters_info *info, bool compat)
  747. {
  748. void *mem;
  749. u64 size;
  750. #ifdef CONFIG_COMPAT
  751. if (compat) {
  752. /* structures only differ in size due to alignment */
  753. struct compat_xt_counters_info compat_tmp;
  754. if (len <= sizeof(compat_tmp))
  755. return ERR_PTR(-EINVAL);
  756. len -= sizeof(compat_tmp);
  757. if (copy_from_user(&compat_tmp, user, sizeof(compat_tmp)) != 0)
  758. return ERR_PTR(-EFAULT);
  759. memcpy(info->name, compat_tmp.name, sizeof(info->name) - 1);
  760. info->num_counters = compat_tmp.num_counters;
  761. user += sizeof(compat_tmp);
  762. } else
  763. #endif
  764. {
  765. if (len <= sizeof(*info))
  766. return ERR_PTR(-EINVAL);
  767. len -= sizeof(*info);
  768. if (copy_from_user(info, user, sizeof(*info)) != 0)
  769. return ERR_PTR(-EFAULT);
  770. user += sizeof(*info);
  771. }
  772. info->name[sizeof(info->name) - 1] = '\0';
  773. size = sizeof(struct xt_counters);
  774. size *= info->num_counters;
  775. if (size != (u64)len)
  776. return ERR_PTR(-EINVAL);
  777. mem = vmalloc(len);
  778. if (!mem)
  779. return ERR_PTR(-ENOMEM);
  780. if (copy_from_user(mem, user, len) == 0)
  781. return mem;
  782. vfree(mem);
  783. return ERR_PTR(-EFAULT);
  784. }
  785. EXPORT_SYMBOL_GPL(xt_copy_counters_from_user);
  786. #ifdef CONFIG_COMPAT
  787. int xt_compat_target_offset(const struct xt_target *target)
  788. {
  789. u_int16_t csize = target->compatsize ? : target->targetsize;
  790. return XT_ALIGN(target->targetsize) - COMPAT_XT_ALIGN(csize);
  791. }
  792. EXPORT_SYMBOL_GPL(xt_compat_target_offset);
  793. void xt_compat_target_from_user(struct xt_entry_target *t, void **dstptr,
  794. unsigned int *size)
  795. {
  796. const struct xt_target *target = t->u.kernel.target;
  797. struct compat_xt_entry_target *ct = (struct compat_xt_entry_target *)t;
  798. int pad, off = xt_compat_target_offset(target);
  799. u_int16_t tsize = ct->u.user.target_size;
  800. char name[sizeof(t->u.user.name)];
  801. t = *dstptr;
  802. memcpy(t, ct, sizeof(*ct));
  803. if (target->compat_from_user)
  804. target->compat_from_user(t->data, ct->data);
  805. else
  806. memcpy(t->data, ct->data, tsize - sizeof(*ct));
  807. pad = XT_ALIGN(target->targetsize) - target->targetsize;
  808. if (pad > 0)
  809. memset(t->data + target->targetsize, 0, pad);
  810. tsize += off;
  811. t->u.user.target_size = tsize;
  812. strlcpy(name, target->name, sizeof(name));
  813. module_put(target->me);
  814. strncpy(t->u.user.name, name, sizeof(t->u.user.name));
  815. *size += off;
  816. *dstptr += tsize;
  817. }
  818. EXPORT_SYMBOL_GPL(xt_compat_target_from_user);
  819. int xt_compat_target_to_user(const struct xt_entry_target *t,
  820. void __user **dstptr, unsigned int *size)
  821. {
  822. const struct xt_target *target = t->u.kernel.target;
  823. struct compat_xt_entry_target __user *ct = *dstptr;
  824. int off = xt_compat_target_offset(target);
  825. u_int16_t tsize = t->u.user.target_size - off;
  826. if (copy_to_user(ct, t, sizeof(*ct)) ||
  827. put_user(tsize, &ct->u.user.target_size) ||
  828. copy_to_user(ct->u.user.name, t->u.kernel.target->name,
  829. strlen(t->u.kernel.target->name) + 1))
  830. return -EFAULT;
  831. if (target->compat_to_user) {
  832. if (target->compat_to_user((void __user *)ct->data, t->data))
  833. return -EFAULT;
  834. } else {
  835. if (copy_to_user(ct->data, t->data, tsize - sizeof(*ct)))
  836. return -EFAULT;
  837. }
  838. *size -= off;
  839. *dstptr += tsize;
  840. return 0;
  841. }
  842. EXPORT_SYMBOL_GPL(xt_compat_target_to_user);
  843. #endif
  844. struct xt_table_info *xt_alloc_table_info(unsigned int size)
  845. {
  846. struct xt_table_info *info = NULL;
  847. size_t sz = sizeof(*info) + size;
  848. if (sz < sizeof(*info))
  849. return NULL;
  850. /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
  851. if ((size >> PAGE_SHIFT) + 2 > totalram_pages)
  852. return NULL;
  853. if (sz <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
  854. info = kmalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
  855. if (!info) {
  856. info = vmalloc(sz);
  857. if (!info)
  858. return NULL;
  859. }
  860. memset(info, 0, sizeof(*info));
  861. info->size = size;
  862. return info;
  863. }
  864. EXPORT_SYMBOL(xt_alloc_table_info);
  865. void xt_free_table_info(struct xt_table_info *info)
  866. {
  867. int cpu;
  868. if (info->jumpstack != NULL) {
  869. for_each_possible_cpu(cpu)
  870. kvfree(info->jumpstack[cpu]);
  871. kvfree(info->jumpstack);
  872. }
  873. kvfree(info);
  874. }
  875. EXPORT_SYMBOL(xt_free_table_info);
  876. /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
  877. struct xt_table *xt_find_table_lock(struct net *net, u_int8_t af,
  878. const char *name)
  879. {
  880. struct xt_table *t, *found = NULL;
  881. mutex_lock(&xt[af].mutex);
  882. list_for_each_entry(t, &net->xt.tables[af], list)
  883. if (strcmp(t->name, name) == 0 && try_module_get(t->me))
  884. return t;
  885. if (net == &init_net)
  886. goto out;
  887. /* Table doesn't exist in this netns, re-try init */
  888. list_for_each_entry(t, &init_net.xt.tables[af], list) {
  889. if (strcmp(t->name, name))
  890. continue;
  891. if (!try_module_get(t->me)) {
  892. mutex_unlock(&xt[af].mutex);
  893. return NULL;
  894. }
  895. mutex_unlock(&xt[af].mutex);
  896. if (t->table_init(net) != 0) {
  897. module_put(t->me);
  898. return NULL;
  899. }
  900. found = t;
  901. mutex_lock(&xt[af].mutex);
  902. break;
  903. }
  904. if (!found)
  905. goto out;
  906. /* and once again: */
  907. list_for_each_entry(t, &net->xt.tables[af], list)
  908. if (strcmp(t->name, name) == 0)
  909. return t;
  910. module_put(found->me);
  911. out:
  912. mutex_unlock(&xt[af].mutex);
  913. return NULL;
  914. }
  915. EXPORT_SYMBOL_GPL(xt_find_table_lock);
  916. void xt_table_unlock(struct xt_table *table)
  917. {
  918. mutex_unlock(&xt[table->af].mutex);
  919. }
  920. EXPORT_SYMBOL_GPL(xt_table_unlock);
  921. #ifdef CONFIG_COMPAT
  922. void xt_compat_lock(u_int8_t af)
  923. {
  924. mutex_lock(&xt[af].compat_mutex);
  925. }
  926. EXPORT_SYMBOL_GPL(xt_compat_lock);
  927. void xt_compat_unlock(u_int8_t af)
  928. {
  929. mutex_unlock(&xt[af].compat_mutex);
  930. }
  931. EXPORT_SYMBOL_GPL(xt_compat_unlock);
  932. #endif
  933. DEFINE_PER_CPU(seqcount_t, xt_recseq);
  934. EXPORT_PER_CPU_SYMBOL_GPL(xt_recseq);
  935. struct static_key xt_tee_enabled __read_mostly;
  936. EXPORT_SYMBOL_GPL(xt_tee_enabled);
  937. static int xt_jumpstack_alloc(struct xt_table_info *i)
  938. {
  939. unsigned int size;
  940. int cpu;
  941. size = sizeof(void **) * nr_cpu_ids;
  942. if (size > PAGE_SIZE)
  943. i->jumpstack = vzalloc(size);
  944. else
  945. i->jumpstack = kzalloc(size, GFP_KERNEL);
  946. if (i->jumpstack == NULL)
  947. return -ENOMEM;
  948. /* ruleset without jumps -- no stack needed */
  949. if (i->stacksize == 0)
  950. return 0;
  951. /* Jumpstack needs to be able to record two full callchains, one
  952. * from the first rule set traversal, plus one table reentrancy
  953. * via -j TEE without clobbering the callchain that brought us to
  954. * TEE target.
  955. *
  956. * This is done by allocating two jumpstacks per cpu, on reentry
  957. * the upper half of the stack is used.
  958. *
  959. * see the jumpstack setup in ipt_do_table() for more details.
  960. */
  961. size = sizeof(void *) * i->stacksize * 2u;
  962. for_each_possible_cpu(cpu) {
  963. if (size > PAGE_SIZE)
  964. i->jumpstack[cpu] = vmalloc_node(size,
  965. cpu_to_node(cpu));
  966. else
  967. i->jumpstack[cpu] = kmalloc_node(size,
  968. GFP_KERNEL, cpu_to_node(cpu));
  969. if (i->jumpstack[cpu] == NULL)
  970. /*
  971. * Freeing will be done later on by the callers. The
  972. * chain is: xt_replace_table -> __do_replace ->
  973. * do_replace -> xt_free_table_info.
  974. */
  975. return -ENOMEM;
  976. }
  977. return 0;
  978. }
  979. struct xt_table_info *
  980. xt_replace_table(struct xt_table *table,
  981. unsigned int num_counters,
  982. struct xt_table_info *newinfo,
  983. int *error)
  984. {
  985. struct xt_table_info *private;
  986. int ret;
  987. ret = xt_jumpstack_alloc(newinfo);
  988. if (ret < 0) {
  989. *error = ret;
  990. return NULL;
  991. }
  992. /* Do the substitution. */
  993. local_bh_disable();
  994. private = table->private;
  995. /* Check inside lock: is the old number correct? */
  996. if (num_counters != private->number) {
  997. pr_debug("num_counters != table->private->number (%u/%u)\n",
  998. num_counters, private->number);
  999. local_bh_enable();
  1000. *error = -EAGAIN;
  1001. return NULL;
  1002. }
  1003. newinfo->initial_entries = private->initial_entries;
  1004. /*
  1005. * Ensure contents of newinfo are visible before assigning to
  1006. * private.
  1007. */
  1008. smp_wmb();
  1009. table->private = newinfo;
  1010. /*
  1011. * Even though table entries have now been swapped, other CPU's
  1012. * may still be using the old entries. This is okay, because
  1013. * resynchronization happens because of the locking done
  1014. * during the get_counters() routine.
  1015. */
  1016. local_bh_enable();
  1017. #ifdef CONFIG_AUDIT
  1018. if (audit_enabled) {
  1019. struct audit_buffer *ab;
  1020. ab = audit_log_start(current->audit_context, GFP_KERNEL,
  1021. AUDIT_NETFILTER_CFG);
  1022. if (ab) {
  1023. audit_log_format(ab, "table=%s family=%u entries=%u",
  1024. table->name, table->af,
  1025. private->number);
  1026. audit_log_end(ab);
  1027. }
  1028. }
  1029. #endif
  1030. return private;
  1031. }
  1032. EXPORT_SYMBOL_GPL(xt_replace_table);
  1033. struct xt_table *xt_register_table(struct net *net,
  1034. const struct xt_table *input_table,
  1035. struct xt_table_info *bootstrap,
  1036. struct xt_table_info *newinfo)
  1037. {
  1038. int ret;
  1039. struct xt_table_info *private;
  1040. struct xt_table *t, *table;
  1041. /* Don't add one object to multiple lists. */
  1042. table = kmemdup(input_table, sizeof(struct xt_table), GFP_KERNEL);
  1043. if (!table) {
  1044. ret = -ENOMEM;
  1045. goto out;
  1046. }
  1047. mutex_lock(&xt[table->af].mutex);
  1048. /* Don't autoload: we'd eat our tail... */
  1049. list_for_each_entry(t, &net->xt.tables[table->af], list) {
  1050. if (strcmp(t->name, table->name) == 0) {
  1051. ret = -EEXIST;
  1052. goto unlock;
  1053. }
  1054. }
  1055. /* Simplifies replace_table code. */
  1056. table->private = bootstrap;
  1057. if (!xt_replace_table(table, 0, newinfo, &ret))
  1058. goto unlock;
  1059. private = table->private;
  1060. pr_debug("table->private->number = %u\n", private->number);
  1061. /* save number of initial entries */
  1062. private->initial_entries = private->number;
  1063. list_add(&table->list, &net->xt.tables[table->af]);
  1064. mutex_unlock(&xt[table->af].mutex);
  1065. return table;
  1066. unlock:
  1067. mutex_unlock(&xt[table->af].mutex);
  1068. kfree(table);
  1069. out:
  1070. return ERR_PTR(ret);
  1071. }
  1072. EXPORT_SYMBOL_GPL(xt_register_table);
  1073. void *xt_unregister_table(struct xt_table *table)
  1074. {
  1075. struct xt_table_info *private;
  1076. mutex_lock(&xt[table->af].mutex);
  1077. private = table->private;
  1078. list_del(&table->list);
  1079. mutex_unlock(&xt[table->af].mutex);
  1080. kfree(table);
  1081. return private;
  1082. }
  1083. EXPORT_SYMBOL_GPL(xt_unregister_table);
  1084. #ifdef CONFIG_PROC_FS
  1085. struct xt_names_priv {
  1086. struct seq_net_private p;
  1087. u_int8_t af;
  1088. };
  1089. static void *xt_table_seq_start(struct seq_file *seq, loff_t *pos)
  1090. {
  1091. struct xt_names_priv *priv = seq->private;
  1092. struct net *net = seq_file_net(seq);
  1093. u_int8_t af = priv->af;
  1094. mutex_lock(&xt[af].mutex);
  1095. return seq_list_start(&net->xt.tables[af], *pos);
  1096. }
  1097. static void *xt_table_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1098. {
  1099. struct xt_names_priv *priv = seq->private;
  1100. struct net *net = seq_file_net(seq);
  1101. u_int8_t af = priv->af;
  1102. return seq_list_next(v, &net->xt.tables[af], pos);
  1103. }
  1104. static void xt_table_seq_stop(struct seq_file *seq, void *v)
  1105. {
  1106. struct xt_names_priv *priv = seq->private;
  1107. u_int8_t af = priv->af;
  1108. mutex_unlock(&xt[af].mutex);
  1109. }
  1110. static int xt_table_seq_show(struct seq_file *seq, void *v)
  1111. {
  1112. struct xt_table *table = list_entry(v, struct xt_table, list);
  1113. if (*table->name)
  1114. seq_printf(seq, "%s\n", table->name);
  1115. return 0;
  1116. }
  1117. static const struct seq_operations xt_table_seq_ops = {
  1118. .start = xt_table_seq_start,
  1119. .next = xt_table_seq_next,
  1120. .stop = xt_table_seq_stop,
  1121. .show = xt_table_seq_show,
  1122. };
  1123. static int xt_table_open(struct inode *inode, struct file *file)
  1124. {
  1125. int ret;
  1126. struct xt_names_priv *priv;
  1127. ret = seq_open_net(inode, file, &xt_table_seq_ops,
  1128. sizeof(struct xt_names_priv));
  1129. if (!ret) {
  1130. priv = ((struct seq_file *)file->private_data)->private;
  1131. priv->af = (unsigned long)PDE_DATA(inode);
  1132. }
  1133. return ret;
  1134. }
  1135. static const struct file_operations xt_table_ops = {
  1136. .owner = THIS_MODULE,
  1137. .open = xt_table_open,
  1138. .read = seq_read,
  1139. .llseek = seq_lseek,
  1140. .release = seq_release_net,
  1141. };
  1142. /*
  1143. * Traverse state for ip{,6}_{tables,matches} for helping crossing
  1144. * the multi-AF mutexes.
  1145. */
  1146. struct nf_mttg_trav {
  1147. struct list_head *head, *curr;
  1148. uint8_t class, nfproto;
  1149. };
  1150. enum {
  1151. MTTG_TRAV_INIT,
  1152. MTTG_TRAV_NFP_UNSPEC,
  1153. MTTG_TRAV_NFP_SPEC,
  1154. MTTG_TRAV_DONE,
  1155. };
  1156. static void *xt_mttg_seq_next(struct seq_file *seq, void *v, loff_t *ppos,
  1157. bool is_target)
  1158. {
  1159. static const uint8_t next_class[] = {
  1160. [MTTG_TRAV_NFP_UNSPEC] = MTTG_TRAV_NFP_SPEC,
  1161. [MTTG_TRAV_NFP_SPEC] = MTTG_TRAV_DONE,
  1162. };
  1163. struct nf_mttg_trav *trav = seq->private;
  1164. switch (trav->class) {
  1165. case MTTG_TRAV_INIT:
  1166. trav->class = MTTG_TRAV_NFP_UNSPEC;
  1167. mutex_lock(&xt[NFPROTO_UNSPEC].mutex);
  1168. trav->head = trav->curr = is_target ?
  1169. &xt[NFPROTO_UNSPEC].target : &xt[NFPROTO_UNSPEC].match;
  1170. break;
  1171. case MTTG_TRAV_NFP_UNSPEC:
  1172. trav->curr = trav->curr->next;
  1173. if (trav->curr != trav->head)
  1174. break;
  1175. mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
  1176. mutex_lock(&xt[trav->nfproto].mutex);
  1177. trav->head = trav->curr = is_target ?
  1178. &xt[trav->nfproto].target : &xt[trav->nfproto].match;
  1179. trav->class = next_class[trav->class];
  1180. break;
  1181. case MTTG_TRAV_NFP_SPEC:
  1182. trav->curr = trav->curr->next;
  1183. if (trav->curr != trav->head)
  1184. break;
  1185. /* fallthru, _stop will unlock */
  1186. default:
  1187. return NULL;
  1188. }
  1189. if (ppos != NULL)
  1190. ++*ppos;
  1191. return trav;
  1192. }
  1193. static void *xt_mttg_seq_start(struct seq_file *seq, loff_t *pos,
  1194. bool is_target)
  1195. {
  1196. struct nf_mttg_trav *trav = seq->private;
  1197. unsigned int j;
  1198. trav->class = MTTG_TRAV_INIT;
  1199. for (j = 0; j < *pos; ++j)
  1200. if (xt_mttg_seq_next(seq, NULL, NULL, is_target) == NULL)
  1201. return NULL;
  1202. return trav;
  1203. }
  1204. static void xt_mttg_seq_stop(struct seq_file *seq, void *v)
  1205. {
  1206. struct nf_mttg_trav *trav = seq->private;
  1207. switch (trav->class) {
  1208. case MTTG_TRAV_NFP_UNSPEC:
  1209. mutex_unlock(&xt[NFPROTO_UNSPEC].mutex);
  1210. break;
  1211. case MTTG_TRAV_NFP_SPEC:
  1212. mutex_unlock(&xt[trav->nfproto].mutex);
  1213. break;
  1214. }
  1215. }
  1216. static void *xt_match_seq_start(struct seq_file *seq, loff_t *pos)
  1217. {
  1218. return xt_mttg_seq_start(seq, pos, false);
  1219. }
  1220. static void *xt_match_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
  1221. {
  1222. return xt_mttg_seq_next(seq, v, ppos, false);
  1223. }
  1224. static int xt_match_seq_show(struct seq_file *seq, void *v)
  1225. {
  1226. const struct nf_mttg_trav *trav = seq->private;
  1227. const struct xt_match *match;
  1228. switch (trav->class) {
  1229. case MTTG_TRAV_NFP_UNSPEC:
  1230. case MTTG_TRAV_NFP_SPEC:
  1231. if (trav->curr == trav->head)
  1232. return 0;
  1233. match = list_entry(trav->curr, struct xt_match, list);
  1234. if (*match->name)
  1235. seq_printf(seq, "%s\n", match->name);
  1236. }
  1237. return 0;
  1238. }
  1239. static const struct seq_operations xt_match_seq_ops = {
  1240. .start = xt_match_seq_start,
  1241. .next = xt_match_seq_next,
  1242. .stop = xt_mttg_seq_stop,
  1243. .show = xt_match_seq_show,
  1244. };
  1245. static int xt_match_open(struct inode *inode, struct file *file)
  1246. {
  1247. struct nf_mttg_trav *trav;
  1248. trav = __seq_open_private(file, &xt_match_seq_ops, sizeof(*trav));
  1249. if (!trav)
  1250. return -ENOMEM;
  1251. trav->nfproto = (unsigned long)PDE_DATA(inode);
  1252. return 0;
  1253. }
  1254. static const struct file_operations xt_match_ops = {
  1255. .owner = THIS_MODULE,
  1256. .open = xt_match_open,
  1257. .read = seq_read,
  1258. .llseek = seq_lseek,
  1259. .release = seq_release_private,
  1260. };
  1261. static void *xt_target_seq_start(struct seq_file *seq, loff_t *pos)
  1262. {
  1263. return xt_mttg_seq_start(seq, pos, true);
  1264. }
  1265. static void *xt_target_seq_next(struct seq_file *seq, void *v, loff_t *ppos)
  1266. {
  1267. return xt_mttg_seq_next(seq, v, ppos, true);
  1268. }
  1269. static int xt_target_seq_show(struct seq_file *seq, void *v)
  1270. {
  1271. const struct nf_mttg_trav *trav = seq->private;
  1272. const struct xt_target *target;
  1273. switch (trav->class) {
  1274. case MTTG_TRAV_NFP_UNSPEC:
  1275. case MTTG_TRAV_NFP_SPEC:
  1276. if (trav->curr == trav->head)
  1277. return 0;
  1278. target = list_entry(trav->curr, struct xt_target, list);
  1279. if (*target->name)
  1280. seq_printf(seq, "%s\n", target->name);
  1281. }
  1282. return 0;
  1283. }
  1284. static const struct seq_operations xt_target_seq_ops = {
  1285. .start = xt_target_seq_start,
  1286. .next = xt_target_seq_next,
  1287. .stop = xt_mttg_seq_stop,
  1288. .show = xt_target_seq_show,
  1289. };
  1290. static int xt_target_open(struct inode *inode, struct file *file)
  1291. {
  1292. struct nf_mttg_trav *trav;
  1293. trav = __seq_open_private(file, &xt_target_seq_ops, sizeof(*trav));
  1294. if (!trav)
  1295. return -ENOMEM;
  1296. trav->nfproto = (unsigned long)PDE_DATA(inode);
  1297. return 0;
  1298. }
  1299. static const struct file_operations xt_target_ops = {
  1300. .owner = THIS_MODULE,
  1301. .open = xt_target_open,
  1302. .read = seq_read,
  1303. .llseek = seq_lseek,
  1304. .release = seq_release_private,
  1305. };
  1306. #define FORMAT_TABLES "_tables_names"
  1307. #define FORMAT_MATCHES "_tables_matches"
  1308. #define FORMAT_TARGETS "_tables_targets"
  1309. #endif /* CONFIG_PROC_FS */
  1310. /**
  1311. * xt_hook_ops_alloc - set up hooks for a new table
  1312. * @table: table with metadata needed to set up hooks
  1313. * @fn: Hook function
  1314. *
  1315. * This function will create the nf_hook_ops that the x_table needs
  1316. * to hand to xt_hook_link_net().
  1317. */
  1318. struct nf_hook_ops *
  1319. xt_hook_ops_alloc(const struct xt_table *table, nf_hookfn *fn)
  1320. {
  1321. unsigned int hook_mask = table->valid_hooks;
  1322. uint8_t i, num_hooks = hweight32(hook_mask);
  1323. uint8_t hooknum;
  1324. struct nf_hook_ops *ops;
  1325. if (!num_hooks)
  1326. return ERR_PTR(-EINVAL);
  1327. ops = kcalloc(num_hooks, sizeof(*ops), GFP_KERNEL);
  1328. if (ops == NULL)
  1329. return ERR_PTR(-ENOMEM);
  1330. for (i = 0, hooknum = 0; i < num_hooks && hook_mask != 0;
  1331. hook_mask >>= 1, ++hooknum) {
  1332. if (!(hook_mask & 1))
  1333. continue;
  1334. ops[i].hook = fn;
  1335. ops[i].pf = table->af;
  1336. ops[i].hooknum = hooknum;
  1337. ops[i].priority = table->priority;
  1338. ++i;
  1339. }
  1340. return ops;
  1341. }
  1342. EXPORT_SYMBOL_GPL(xt_hook_ops_alloc);
  1343. int xt_proto_init(struct net *net, u_int8_t af)
  1344. {
  1345. #ifdef CONFIG_PROC_FS
  1346. char buf[XT_FUNCTION_MAXNAMELEN];
  1347. struct proc_dir_entry *proc;
  1348. kuid_t root_uid;
  1349. kgid_t root_gid;
  1350. #endif
  1351. if (af >= ARRAY_SIZE(xt_prefix))
  1352. return -EINVAL;
  1353. #ifdef CONFIG_PROC_FS
  1354. root_uid = make_kuid(net->user_ns, 0);
  1355. root_gid = make_kgid(net->user_ns, 0);
  1356. strlcpy(buf, xt_prefix[af], sizeof(buf));
  1357. strlcat(buf, FORMAT_TABLES, sizeof(buf));
  1358. proc = proc_create_data(buf, 0440, net->proc_net, &xt_table_ops,
  1359. (void *)(unsigned long)af);
  1360. if (!proc)
  1361. goto out;
  1362. if (uid_valid(root_uid) && gid_valid(root_gid))
  1363. proc_set_user(proc, root_uid, root_gid);
  1364. strlcpy(buf, xt_prefix[af], sizeof(buf));
  1365. strlcat(buf, FORMAT_MATCHES, sizeof(buf));
  1366. proc = proc_create_data(buf, 0440, net->proc_net, &xt_match_ops,
  1367. (void *)(unsigned long)af);
  1368. if (!proc)
  1369. goto out_remove_tables;
  1370. if (uid_valid(root_uid) && gid_valid(root_gid))
  1371. proc_set_user(proc, root_uid, root_gid);
  1372. strlcpy(buf, xt_prefix[af], sizeof(buf));
  1373. strlcat(buf, FORMAT_TARGETS, sizeof(buf));
  1374. proc = proc_create_data(buf, 0440, net->proc_net, &xt_target_ops,
  1375. (void *)(unsigned long)af);
  1376. if (!proc)
  1377. goto out_remove_matches;
  1378. if (uid_valid(root_uid) && gid_valid(root_gid))
  1379. proc_set_user(proc, root_uid, root_gid);
  1380. #endif
  1381. return 0;
  1382. #ifdef CONFIG_PROC_FS
  1383. out_remove_matches:
  1384. strlcpy(buf, xt_prefix[af], sizeof(buf));
  1385. strlcat(buf, FORMAT_MATCHES, sizeof(buf));
  1386. remove_proc_entry(buf, net->proc_net);
  1387. out_remove_tables:
  1388. strlcpy(buf, xt_prefix[af], sizeof(buf));
  1389. strlcat(buf, FORMAT_TABLES, sizeof(buf));
  1390. remove_proc_entry(buf, net->proc_net);
  1391. out:
  1392. return -1;
  1393. #endif
  1394. }
  1395. EXPORT_SYMBOL_GPL(xt_proto_init);
  1396. void xt_proto_fini(struct net *net, u_int8_t af)
  1397. {
  1398. #ifdef CONFIG_PROC_FS
  1399. char buf[XT_FUNCTION_MAXNAMELEN];
  1400. strlcpy(buf, xt_prefix[af], sizeof(buf));
  1401. strlcat(buf, FORMAT_TABLES, sizeof(buf));
  1402. remove_proc_entry(buf, net->proc_net);
  1403. strlcpy(buf, xt_prefix[af], sizeof(buf));
  1404. strlcat(buf, FORMAT_TARGETS, sizeof(buf));
  1405. remove_proc_entry(buf, net->proc_net);
  1406. strlcpy(buf, xt_prefix[af], sizeof(buf));
  1407. strlcat(buf, FORMAT_MATCHES, sizeof(buf));
  1408. remove_proc_entry(buf, net->proc_net);
  1409. #endif /*CONFIG_PROC_FS*/
  1410. }
  1411. EXPORT_SYMBOL_GPL(xt_proto_fini);
  1412. /**
  1413. * xt_percpu_counter_alloc - allocate x_tables rule counter
  1414. *
  1415. * @state: pointer to xt_percpu allocation state
  1416. * @counter: pointer to counter struct inside the ip(6)/arpt_entry struct
  1417. *
  1418. * On SMP, the packet counter [ ip(6)t_entry->counters.pcnt ] will then
  1419. * contain the address of the real (percpu) counter.
  1420. *
  1421. * Rule evaluation needs to use xt_get_this_cpu_counter() helper
  1422. * to fetch the real percpu counter.
  1423. *
  1424. * To speed up allocation and improve data locality, a 4kb block is
  1425. * allocated.
  1426. *
  1427. * xt_percpu_counter_alloc_state contains the base address of the
  1428. * allocated page and the current sub-offset.
  1429. *
  1430. * returns false on error.
  1431. */
  1432. bool xt_percpu_counter_alloc(struct xt_percpu_counter_alloc_state *state,
  1433. struct xt_counters *counter)
  1434. {
  1435. BUILD_BUG_ON(XT_PCPU_BLOCK_SIZE < (sizeof(*counter) * 2));
  1436. if (nr_cpu_ids <= 1)
  1437. return true;
  1438. if (!state->mem) {
  1439. state->mem = __alloc_percpu(XT_PCPU_BLOCK_SIZE,
  1440. XT_PCPU_BLOCK_SIZE);
  1441. if (!state->mem)
  1442. return false;
  1443. }
  1444. counter->pcnt = (__force unsigned long)(state->mem + state->off);
  1445. state->off += sizeof(*counter);
  1446. if (state->off > (XT_PCPU_BLOCK_SIZE - sizeof(*counter))) {
  1447. state->mem = NULL;
  1448. state->off = 0;
  1449. }
  1450. return true;
  1451. }
  1452. EXPORT_SYMBOL_GPL(xt_percpu_counter_alloc);
  1453. void xt_percpu_counter_free(struct xt_counters *counters)
  1454. {
  1455. unsigned long pcnt = counters->pcnt;
  1456. if (nr_cpu_ids > 1 && (pcnt & (XT_PCPU_BLOCK_SIZE - 1)) == 0)
  1457. free_percpu((void __percpu *)pcnt);
  1458. }
  1459. EXPORT_SYMBOL_GPL(xt_percpu_counter_free);
  1460. static int __net_init xt_net_init(struct net *net)
  1461. {
  1462. int i;
  1463. for (i = 0; i < NFPROTO_NUMPROTO; i++)
  1464. INIT_LIST_HEAD(&net->xt.tables[i]);
  1465. return 0;
  1466. }
  1467. static struct pernet_operations xt_net_ops = {
  1468. .init = xt_net_init,
  1469. };
  1470. static int __init xt_init(void)
  1471. {
  1472. unsigned int i;
  1473. int rv;
  1474. for_each_possible_cpu(i) {
  1475. seqcount_init(&per_cpu(xt_recseq, i));
  1476. }
  1477. xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
  1478. if (!xt)
  1479. return -ENOMEM;
  1480. for (i = 0; i < NFPROTO_NUMPROTO; i++) {
  1481. mutex_init(&xt[i].mutex);
  1482. #ifdef CONFIG_COMPAT
  1483. mutex_init(&xt[i].compat_mutex);
  1484. xt[i].compat_tab = NULL;
  1485. #endif
  1486. INIT_LIST_HEAD(&xt[i].target);
  1487. INIT_LIST_HEAD(&xt[i].match);
  1488. }
  1489. rv = register_pernet_subsys(&xt_net_ops);
  1490. if (rv < 0)
  1491. kfree(xt);
  1492. return rv;
  1493. }
  1494. static void __exit xt_fini(void)
  1495. {
  1496. unregister_pernet_subsys(&xt_net_ops);
  1497. kfree(xt);
  1498. }
  1499. module_init(xt_init);
  1500. module_exit(xt_fini);