ebtables.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484
  1. /*
  2. * ebtables
  3. *
  4. * Author:
  5. * Bart De Schuymer <bdschuym@pandora.be>
  6. *
  7. * ebtables.c,v 2.0, July, 2002
  8. *
  9. * This code is strongly inspired by the iptables code which is
  10. * Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/kmod.h>
  19. #include <linux/module.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/netfilter/x_tables.h>
  22. #include <linux/netfilter_bridge/ebtables.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/mutex.h>
  25. #include <linux/slab.h>
  26. #include <asm/uaccess.h>
  27. #include <linux/smp.h>
  28. #include <linux/cpumask.h>
  29. #include <linux/audit.h>
  30. #include <net/sock.h>
  31. /* needed for logical [in,out]-dev filtering */
  32. #include "../br_private.h"
  33. #define BUGPRINT(format, args...) printk("kernel msg: ebtables bug: please "\
  34. "report to author: "format, ## args)
  35. /* #define BUGPRINT(format, args...) */
  36. /* Each cpu has its own set of counters, so there is no need for write_lock in
  37. * the softirq
  38. * For reading or updating the counters, the user context needs to
  39. * get a write_lock
  40. */
  41. /* The size of each set of counters is altered to get cache alignment */
  42. #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
  43. #define COUNTER_OFFSET(n) (SMP_ALIGN(n * sizeof(struct ebt_counter)))
  44. #define COUNTER_BASE(c, n, cpu) ((struct ebt_counter *)(((char *)c) + \
  45. COUNTER_OFFSET(n) * cpu))
  46. static DEFINE_MUTEX(ebt_mutex);
  47. #ifdef CONFIG_COMPAT
  48. static void ebt_standard_compat_from_user(void *dst, const void *src)
  49. {
  50. int v = *(compat_int_t *)src;
  51. if (v >= 0)
  52. v += xt_compat_calc_jump(NFPROTO_BRIDGE, v);
  53. memcpy(dst, &v, sizeof(v));
  54. }
  55. static int ebt_standard_compat_to_user(void __user *dst, const void *src)
  56. {
  57. compat_int_t cv = *(int *)src;
  58. if (cv >= 0)
  59. cv -= xt_compat_calc_jump(NFPROTO_BRIDGE, cv);
  60. return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
  61. }
  62. #endif
  63. static struct xt_target ebt_standard_target = {
  64. .name = "standard",
  65. .revision = 0,
  66. .family = NFPROTO_BRIDGE,
  67. .targetsize = sizeof(int),
  68. #ifdef CONFIG_COMPAT
  69. .compatsize = sizeof(compat_int_t),
  70. .compat_from_user = ebt_standard_compat_from_user,
  71. .compat_to_user = ebt_standard_compat_to_user,
  72. #endif
  73. };
  74. static inline int
  75. ebt_do_watcher(const struct ebt_entry_watcher *w, struct sk_buff *skb,
  76. struct xt_action_param *par)
  77. {
  78. par->target = w->u.watcher;
  79. par->targinfo = w->data;
  80. w->u.watcher->target(skb, par);
  81. /* watchers don't give a verdict */
  82. return 0;
  83. }
  84. static inline int
  85. ebt_do_match(struct ebt_entry_match *m, const struct sk_buff *skb,
  86. struct xt_action_param *par)
  87. {
  88. par->match = m->u.match;
  89. par->matchinfo = m->data;
  90. return m->u.match->match(skb, par) ? EBT_MATCH : EBT_NOMATCH;
  91. }
  92. static inline int
  93. ebt_dev_check(const char *entry, const struct net_device *device)
  94. {
  95. int i = 0;
  96. const char *devname;
  97. if (*entry == '\0')
  98. return 0;
  99. if (!device)
  100. return 1;
  101. devname = device->name;
  102. /* 1 is the wildcard token */
  103. while (entry[i] != '\0' && entry[i] != 1 && entry[i] == devname[i])
  104. i++;
  105. return devname[i] != entry[i] && entry[i] != 1;
  106. }
  107. /* process standard matches */
  108. static inline int
  109. ebt_basic_match(const struct ebt_entry *e, const struct sk_buff *skb,
  110. const struct net_device *in, const struct net_device *out)
  111. {
  112. const struct ethhdr *h = eth_hdr(skb);
  113. const struct net_bridge_port *p;
  114. __be16 ethproto;
  115. if (skb_vlan_tag_present(skb))
  116. ethproto = htons(ETH_P_8021Q);
  117. else
  118. ethproto = h->h_proto;
  119. if (e->bitmask & EBT_802_3) {
  120. if (NF_INVF(e, EBT_IPROTO, eth_proto_is_802_3(ethproto)))
  121. return 1;
  122. } else if (!(e->bitmask & EBT_NOPROTO) &&
  123. NF_INVF(e, EBT_IPROTO, e->ethproto != ethproto))
  124. return 1;
  125. if (NF_INVF(e, EBT_IIN, ebt_dev_check(e->in, in)))
  126. return 1;
  127. if (NF_INVF(e, EBT_IOUT, ebt_dev_check(e->out, out)))
  128. return 1;
  129. /* rcu_read_lock()ed by nf_hook_thresh */
  130. if (in && (p = br_port_get_rcu(in)) != NULL &&
  131. NF_INVF(e, EBT_ILOGICALIN,
  132. ebt_dev_check(e->logical_in, p->br->dev)))
  133. return 1;
  134. if (out && (p = br_port_get_rcu(out)) != NULL &&
  135. NF_INVF(e, EBT_ILOGICALOUT,
  136. ebt_dev_check(e->logical_out, p->br->dev)))
  137. return 1;
  138. if (e->bitmask & EBT_SOURCEMAC) {
  139. if (NF_INVF(e, EBT_ISOURCE,
  140. !ether_addr_equal_masked(h->h_source, e->sourcemac,
  141. e->sourcemsk)))
  142. return 1;
  143. }
  144. if (e->bitmask & EBT_DESTMAC) {
  145. if (NF_INVF(e, EBT_IDEST,
  146. !ether_addr_equal_masked(h->h_dest, e->destmac,
  147. e->destmsk)))
  148. return 1;
  149. }
  150. return 0;
  151. }
  152. static inline
  153. struct ebt_entry *ebt_next_entry(const struct ebt_entry *entry)
  154. {
  155. return (void *)entry + entry->next_offset;
  156. }
  157. /* Do some firewalling */
  158. unsigned int ebt_do_table(struct sk_buff *skb,
  159. const struct nf_hook_state *state,
  160. struct ebt_table *table)
  161. {
  162. unsigned int hook = state->hook;
  163. int i, nentries;
  164. struct ebt_entry *point;
  165. struct ebt_counter *counter_base, *cb_base;
  166. const struct ebt_entry_target *t;
  167. int verdict, sp = 0;
  168. struct ebt_chainstack *cs;
  169. struct ebt_entries *chaininfo;
  170. const char *base;
  171. const struct ebt_table_info *private;
  172. struct xt_action_param acpar;
  173. acpar.family = NFPROTO_BRIDGE;
  174. acpar.net = state->net;
  175. acpar.in = state->in;
  176. acpar.out = state->out;
  177. acpar.hotdrop = false;
  178. acpar.hooknum = hook;
  179. read_lock_bh(&table->lock);
  180. private = table->private;
  181. cb_base = COUNTER_BASE(private->counters, private->nentries,
  182. smp_processor_id());
  183. if (private->chainstack)
  184. cs = private->chainstack[smp_processor_id()];
  185. else
  186. cs = NULL;
  187. chaininfo = private->hook_entry[hook];
  188. nentries = private->hook_entry[hook]->nentries;
  189. point = (struct ebt_entry *)(private->hook_entry[hook]->data);
  190. counter_base = cb_base + private->hook_entry[hook]->counter_offset;
  191. /* base for chain jumps */
  192. base = private->entries;
  193. i = 0;
  194. while (i < nentries) {
  195. if (ebt_basic_match(point, skb, state->in, state->out))
  196. goto letscontinue;
  197. if (EBT_MATCH_ITERATE(point, ebt_do_match, skb, &acpar) != 0)
  198. goto letscontinue;
  199. if (acpar.hotdrop) {
  200. read_unlock_bh(&table->lock);
  201. return NF_DROP;
  202. }
  203. /* increase counter */
  204. (*(counter_base + i)).pcnt++;
  205. (*(counter_base + i)).bcnt += skb->len;
  206. /* these should only watch: not modify, nor tell us
  207. * what to do with the packet
  208. */
  209. EBT_WATCHER_ITERATE(point, ebt_do_watcher, skb, &acpar);
  210. t = (struct ebt_entry_target *)
  211. (((char *)point) + point->target_offset);
  212. /* standard target */
  213. if (!t->u.target->target)
  214. verdict = ((struct ebt_standard_target *)t)->verdict;
  215. else {
  216. acpar.target = t->u.target;
  217. acpar.targinfo = t->data;
  218. verdict = t->u.target->target(skb, &acpar);
  219. }
  220. if (verdict == EBT_ACCEPT) {
  221. read_unlock_bh(&table->lock);
  222. return NF_ACCEPT;
  223. }
  224. if (verdict == EBT_DROP) {
  225. read_unlock_bh(&table->lock);
  226. return NF_DROP;
  227. }
  228. if (verdict == EBT_RETURN) {
  229. letsreturn:
  230. #ifdef CONFIG_NETFILTER_DEBUG
  231. if (sp == 0) {
  232. BUGPRINT("RETURN on base chain");
  233. /* act like this is EBT_CONTINUE */
  234. goto letscontinue;
  235. }
  236. #endif
  237. sp--;
  238. /* put all the local variables right */
  239. i = cs[sp].n;
  240. chaininfo = cs[sp].chaininfo;
  241. nentries = chaininfo->nentries;
  242. point = cs[sp].e;
  243. counter_base = cb_base +
  244. chaininfo->counter_offset;
  245. continue;
  246. }
  247. if (verdict == EBT_CONTINUE)
  248. goto letscontinue;
  249. #ifdef CONFIG_NETFILTER_DEBUG
  250. if (verdict < 0) {
  251. BUGPRINT("bogus standard verdict\n");
  252. read_unlock_bh(&table->lock);
  253. return NF_DROP;
  254. }
  255. #endif
  256. /* jump to a udc */
  257. cs[sp].n = i + 1;
  258. cs[sp].chaininfo = chaininfo;
  259. cs[sp].e = ebt_next_entry(point);
  260. i = 0;
  261. chaininfo = (struct ebt_entries *) (base + verdict);
  262. #ifdef CONFIG_NETFILTER_DEBUG
  263. if (chaininfo->distinguisher) {
  264. BUGPRINT("jump to non-chain\n");
  265. read_unlock_bh(&table->lock);
  266. return NF_DROP;
  267. }
  268. #endif
  269. nentries = chaininfo->nentries;
  270. point = (struct ebt_entry *)chaininfo->data;
  271. counter_base = cb_base + chaininfo->counter_offset;
  272. sp++;
  273. continue;
  274. letscontinue:
  275. point = ebt_next_entry(point);
  276. i++;
  277. }
  278. /* I actually like this :) */
  279. if (chaininfo->policy == EBT_RETURN)
  280. goto letsreturn;
  281. if (chaininfo->policy == EBT_ACCEPT) {
  282. read_unlock_bh(&table->lock);
  283. return NF_ACCEPT;
  284. }
  285. read_unlock_bh(&table->lock);
  286. return NF_DROP;
  287. }
  288. /* If it succeeds, returns element and locks mutex */
  289. static inline void *
  290. find_inlist_lock_noload(struct list_head *head, const char *name, int *error,
  291. struct mutex *mutex)
  292. {
  293. struct {
  294. struct list_head list;
  295. char name[EBT_FUNCTION_MAXNAMELEN];
  296. } *e;
  297. mutex_lock(mutex);
  298. list_for_each_entry(e, head, list) {
  299. if (strcmp(e->name, name) == 0)
  300. return e;
  301. }
  302. *error = -ENOENT;
  303. mutex_unlock(mutex);
  304. return NULL;
  305. }
  306. static void *
  307. find_inlist_lock(struct list_head *head, const char *name, const char *prefix,
  308. int *error, struct mutex *mutex)
  309. {
  310. return try_then_request_module(
  311. find_inlist_lock_noload(head, name, error, mutex),
  312. "%s%s", prefix, name);
  313. }
  314. static inline struct ebt_table *
  315. find_table_lock(struct net *net, const char *name, int *error,
  316. struct mutex *mutex)
  317. {
  318. return find_inlist_lock(&net->xt.tables[NFPROTO_BRIDGE], name,
  319. "ebtable_", error, mutex);
  320. }
  321. static inline int
  322. ebt_check_match(struct ebt_entry_match *m, struct xt_mtchk_param *par,
  323. unsigned int *cnt)
  324. {
  325. const struct ebt_entry *e = par->entryinfo;
  326. struct xt_match *match;
  327. size_t left = ((char *)e + e->watchers_offset) - (char *)m;
  328. int ret;
  329. if (left < sizeof(struct ebt_entry_match) ||
  330. left - sizeof(struct ebt_entry_match) < m->match_size)
  331. return -EINVAL;
  332. match = xt_find_match(NFPROTO_BRIDGE, m->u.name, 0);
  333. if (IS_ERR(match) || match->family != NFPROTO_BRIDGE) {
  334. if (!IS_ERR(match))
  335. module_put(match->me);
  336. request_module("ebt_%s", m->u.name);
  337. match = xt_find_match(NFPROTO_BRIDGE, m->u.name, 0);
  338. }
  339. if (IS_ERR(match))
  340. return PTR_ERR(match);
  341. m->u.match = match;
  342. par->match = match;
  343. par->matchinfo = m->data;
  344. ret = xt_check_match(par, m->match_size,
  345. e->ethproto, e->invflags & EBT_IPROTO);
  346. if (ret < 0) {
  347. module_put(match->me);
  348. return ret;
  349. }
  350. (*cnt)++;
  351. return 0;
  352. }
  353. static inline int
  354. ebt_check_watcher(struct ebt_entry_watcher *w, struct xt_tgchk_param *par,
  355. unsigned int *cnt)
  356. {
  357. const struct ebt_entry *e = par->entryinfo;
  358. struct xt_target *watcher;
  359. size_t left = ((char *)e + e->target_offset) - (char *)w;
  360. int ret;
  361. if (left < sizeof(struct ebt_entry_watcher) ||
  362. left - sizeof(struct ebt_entry_watcher) < w->watcher_size)
  363. return -EINVAL;
  364. watcher = xt_request_find_target(NFPROTO_BRIDGE, w->u.name, 0);
  365. if (IS_ERR(watcher))
  366. return PTR_ERR(watcher);
  367. if (watcher->family != NFPROTO_BRIDGE) {
  368. module_put(watcher->me);
  369. return -ENOENT;
  370. }
  371. w->u.watcher = watcher;
  372. par->target = watcher;
  373. par->targinfo = w->data;
  374. ret = xt_check_target(par, w->watcher_size,
  375. e->ethproto, e->invflags & EBT_IPROTO);
  376. if (ret < 0) {
  377. module_put(watcher->me);
  378. return ret;
  379. }
  380. (*cnt)++;
  381. return 0;
  382. }
  383. static int ebt_verify_pointers(const struct ebt_replace *repl,
  384. struct ebt_table_info *newinfo)
  385. {
  386. unsigned int limit = repl->entries_size;
  387. unsigned int valid_hooks = repl->valid_hooks;
  388. unsigned int offset = 0;
  389. int i;
  390. for (i = 0; i < NF_BR_NUMHOOKS; i++)
  391. newinfo->hook_entry[i] = NULL;
  392. newinfo->entries_size = repl->entries_size;
  393. newinfo->nentries = repl->nentries;
  394. while (offset < limit) {
  395. size_t left = limit - offset;
  396. struct ebt_entry *e = (void *)newinfo->entries + offset;
  397. if (left < sizeof(unsigned int))
  398. break;
  399. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  400. if ((valid_hooks & (1 << i)) == 0)
  401. continue;
  402. if ((char __user *)repl->hook_entry[i] ==
  403. repl->entries + offset)
  404. break;
  405. }
  406. if (i != NF_BR_NUMHOOKS || !(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
  407. if (e->bitmask != 0) {
  408. /* we make userspace set this right,
  409. * so there is no misunderstanding
  410. */
  411. BUGPRINT("EBT_ENTRY_OR_ENTRIES shouldn't be set "
  412. "in distinguisher\n");
  413. return -EINVAL;
  414. }
  415. if (i != NF_BR_NUMHOOKS)
  416. newinfo->hook_entry[i] = (struct ebt_entries *)e;
  417. if (left < sizeof(struct ebt_entries))
  418. break;
  419. offset += sizeof(struct ebt_entries);
  420. } else {
  421. if (left < sizeof(struct ebt_entry))
  422. break;
  423. if (left < e->next_offset)
  424. break;
  425. if (e->next_offset < sizeof(struct ebt_entry))
  426. return -EINVAL;
  427. offset += e->next_offset;
  428. }
  429. }
  430. if (offset != limit) {
  431. BUGPRINT("entries_size too small\n");
  432. return -EINVAL;
  433. }
  434. /* check if all valid hooks have a chain */
  435. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  436. if (!newinfo->hook_entry[i] &&
  437. (valid_hooks & (1 << i))) {
  438. BUGPRINT("Valid hook without chain\n");
  439. return -EINVAL;
  440. }
  441. }
  442. return 0;
  443. }
  444. /* this one is very careful, as it is the first function
  445. * to parse the userspace data
  446. */
  447. static inline int
  448. ebt_check_entry_size_and_hooks(const struct ebt_entry *e,
  449. const struct ebt_table_info *newinfo,
  450. unsigned int *n, unsigned int *cnt,
  451. unsigned int *totalcnt, unsigned int *udc_cnt)
  452. {
  453. int i;
  454. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  455. if ((void *)e == (void *)newinfo->hook_entry[i])
  456. break;
  457. }
  458. /* beginning of a new chain
  459. * if i == NF_BR_NUMHOOKS it must be a user defined chain
  460. */
  461. if (i != NF_BR_NUMHOOKS || !e->bitmask) {
  462. /* this checks if the previous chain has as many entries
  463. * as it said it has
  464. */
  465. if (*n != *cnt) {
  466. BUGPRINT("nentries does not equal the nr of entries "
  467. "in the chain\n");
  468. return -EINVAL;
  469. }
  470. if (((struct ebt_entries *)e)->policy != EBT_DROP &&
  471. ((struct ebt_entries *)e)->policy != EBT_ACCEPT) {
  472. /* only RETURN from udc */
  473. if (i != NF_BR_NUMHOOKS ||
  474. ((struct ebt_entries *)e)->policy != EBT_RETURN) {
  475. BUGPRINT("bad policy\n");
  476. return -EINVAL;
  477. }
  478. }
  479. if (i == NF_BR_NUMHOOKS) /* it's a user defined chain */
  480. (*udc_cnt)++;
  481. if (((struct ebt_entries *)e)->counter_offset != *totalcnt) {
  482. BUGPRINT("counter_offset != totalcnt");
  483. return -EINVAL;
  484. }
  485. *n = ((struct ebt_entries *)e)->nentries;
  486. *cnt = 0;
  487. return 0;
  488. }
  489. /* a plain old entry, heh */
  490. if (sizeof(struct ebt_entry) > e->watchers_offset ||
  491. e->watchers_offset > e->target_offset ||
  492. e->target_offset >= e->next_offset) {
  493. BUGPRINT("entry offsets not in right order\n");
  494. return -EINVAL;
  495. }
  496. /* this is not checked anywhere else */
  497. if (e->next_offset - e->target_offset < sizeof(struct ebt_entry_target)) {
  498. BUGPRINT("target size too small\n");
  499. return -EINVAL;
  500. }
  501. (*cnt)++;
  502. (*totalcnt)++;
  503. return 0;
  504. }
  505. struct ebt_cl_stack {
  506. struct ebt_chainstack cs;
  507. int from;
  508. unsigned int hookmask;
  509. };
  510. /* We need these positions to check that the jumps to a different part of the
  511. * entries is a jump to the beginning of a new chain.
  512. */
  513. static inline int
  514. ebt_get_udc_positions(struct ebt_entry *e, struct ebt_table_info *newinfo,
  515. unsigned int *n, struct ebt_cl_stack *udc)
  516. {
  517. int i;
  518. /* we're only interested in chain starts */
  519. if (e->bitmask)
  520. return 0;
  521. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  522. if (newinfo->hook_entry[i] == (struct ebt_entries *)e)
  523. break;
  524. }
  525. /* only care about udc */
  526. if (i != NF_BR_NUMHOOKS)
  527. return 0;
  528. udc[*n].cs.chaininfo = (struct ebt_entries *)e;
  529. /* these initialisations are depended on later in check_chainloops() */
  530. udc[*n].cs.n = 0;
  531. udc[*n].hookmask = 0;
  532. (*n)++;
  533. return 0;
  534. }
  535. static inline int
  536. ebt_cleanup_match(struct ebt_entry_match *m, struct net *net, unsigned int *i)
  537. {
  538. struct xt_mtdtor_param par;
  539. if (i && (*i)-- == 0)
  540. return 1;
  541. par.net = net;
  542. par.match = m->u.match;
  543. par.matchinfo = m->data;
  544. par.family = NFPROTO_BRIDGE;
  545. if (par.match->destroy != NULL)
  546. par.match->destroy(&par);
  547. module_put(par.match->me);
  548. return 0;
  549. }
  550. static inline int
  551. ebt_cleanup_watcher(struct ebt_entry_watcher *w, struct net *net, unsigned int *i)
  552. {
  553. struct xt_tgdtor_param par;
  554. if (i && (*i)-- == 0)
  555. return 1;
  556. par.net = net;
  557. par.target = w->u.watcher;
  558. par.targinfo = w->data;
  559. par.family = NFPROTO_BRIDGE;
  560. if (par.target->destroy != NULL)
  561. par.target->destroy(&par);
  562. module_put(par.target->me);
  563. return 0;
  564. }
  565. static inline int
  566. ebt_cleanup_entry(struct ebt_entry *e, struct net *net, unsigned int *cnt)
  567. {
  568. struct xt_tgdtor_param par;
  569. struct ebt_entry_target *t;
  570. if (e->bitmask == 0)
  571. return 0;
  572. /* we're done */
  573. if (cnt && (*cnt)-- == 0)
  574. return 1;
  575. EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, net, NULL);
  576. EBT_MATCH_ITERATE(e, ebt_cleanup_match, net, NULL);
  577. t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
  578. par.net = net;
  579. par.target = t->u.target;
  580. par.targinfo = t->data;
  581. par.family = NFPROTO_BRIDGE;
  582. if (par.target->destroy != NULL)
  583. par.target->destroy(&par);
  584. module_put(par.target->me);
  585. return 0;
  586. }
  587. static inline int
  588. ebt_check_entry(struct ebt_entry *e, struct net *net,
  589. const struct ebt_table_info *newinfo,
  590. const char *name, unsigned int *cnt,
  591. struct ebt_cl_stack *cl_s, unsigned int udc_cnt)
  592. {
  593. struct ebt_entry_target *t;
  594. struct xt_target *target;
  595. unsigned int i, j, hook = 0, hookmask = 0;
  596. size_t gap;
  597. int ret;
  598. struct xt_mtchk_param mtpar;
  599. struct xt_tgchk_param tgpar;
  600. /* don't mess with the struct ebt_entries */
  601. if (e->bitmask == 0)
  602. return 0;
  603. if (e->bitmask & ~EBT_F_MASK) {
  604. BUGPRINT("Unknown flag for bitmask\n");
  605. return -EINVAL;
  606. }
  607. if (e->invflags & ~EBT_INV_MASK) {
  608. BUGPRINT("Unknown flag for inv bitmask\n");
  609. return -EINVAL;
  610. }
  611. if ((e->bitmask & EBT_NOPROTO) && (e->bitmask & EBT_802_3)) {
  612. BUGPRINT("NOPROTO & 802_3 not allowed\n");
  613. return -EINVAL;
  614. }
  615. /* what hook do we belong to? */
  616. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  617. if (!newinfo->hook_entry[i])
  618. continue;
  619. if ((char *)newinfo->hook_entry[i] < (char *)e)
  620. hook = i;
  621. else
  622. break;
  623. }
  624. /* (1 << NF_BR_NUMHOOKS) tells the check functions the rule is on
  625. * a base chain
  626. */
  627. if (i < NF_BR_NUMHOOKS)
  628. hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
  629. else {
  630. for (i = 0; i < udc_cnt; i++)
  631. if ((char *)(cl_s[i].cs.chaininfo) > (char *)e)
  632. break;
  633. if (i == 0)
  634. hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
  635. else
  636. hookmask = cl_s[i - 1].hookmask;
  637. }
  638. i = 0;
  639. memset(&mtpar, 0, sizeof(mtpar));
  640. memset(&tgpar, 0, sizeof(tgpar));
  641. mtpar.net = tgpar.net = net;
  642. mtpar.table = tgpar.table = name;
  643. mtpar.entryinfo = tgpar.entryinfo = e;
  644. mtpar.hook_mask = tgpar.hook_mask = hookmask;
  645. mtpar.family = tgpar.family = NFPROTO_BRIDGE;
  646. ret = EBT_MATCH_ITERATE(e, ebt_check_match, &mtpar, &i);
  647. if (ret != 0)
  648. goto cleanup_matches;
  649. j = 0;
  650. ret = EBT_WATCHER_ITERATE(e, ebt_check_watcher, &tgpar, &j);
  651. if (ret != 0)
  652. goto cleanup_watchers;
  653. t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
  654. gap = e->next_offset - e->target_offset;
  655. target = xt_request_find_target(NFPROTO_BRIDGE, t->u.name, 0);
  656. if (IS_ERR(target)) {
  657. ret = PTR_ERR(target);
  658. goto cleanup_watchers;
  659. }
  660. /* Reject UNSPEC, xtables verdicts/return values are incompatible */
  661. if (target->family != NFPROTO_BRIDGE) {
  662. module_put(target->me);
  663. ret = -ENOENT;
  664. goto cleanup_watchers;
  665. }
  666. t->u.target = target;
  667. if (t->u.target == &ebt_standard_target) {
  668. if (gap < sizeof(struct ebt_standard_target)) {
  669. BUGPRINT("Standard target size too big\n");
  670. ret = -EFAULT;
  671. goto cleanup_watchers;
  672. }
  673. if (((struct ebt_standard_target *)t)->verdict <
  674. -NUM_STANDARD_TARGETS) {
  675. BUGPRINT("Invalid standard target\n");
  676. ret = -EFAULT;
  677. goto cleanup_watchers;
  678. }
  679. } else if (t->target_size > gap - sizeof(struct ebt_entry_target)) {
  680. module_put(t->u.target->me);
  681. ret = -EFAULT;
  682. goto cleanup_watchers;
  683. }
  684. tgpar.target = target;
  685. tgpar.targinfo = t->data;
  686. ret = xt_check_target(&tgpar, t->target_size,
  687. e->ethproto, e->invflags & EBT_IPROTO);
  688. if (ret < 0) {
  689. module_put(target->me);
  690. goto cleanup_watchers;
  691. }
  692. (*cnt)++;
  693. return 0;
  694. cleanup_watchers:
  695. EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, net, &j);
  696. cleanup_matches:
  697. EBT_MATCH_ITERATE(e, ebt_cleanup_match, net, &i);
  698. return ret;
  699. }
  700. /* checks for loops and sets the hook mask for udc
  701. * the hook mask for udc tells us from which base chains the udc can be
  702. * accessed. This mask is a parameter to the check() functions of the extensions
  703. */
  704. static int check_chainloops(const struct ebt_entries *chain, struct ebt_cl_stack *cl_s,
  705. unsigned int udc_cnt, unsigned int hooknr, char *base)
  706. {
  707. int i, chain_nr = -1, pos = 0, nentries = chain->nentries, verdict;
  708. const struct ebt_entry *e = (struct ebt_entry *)chain->data;
  709. const struct ebt_entry_target *t;
  710. while (pos < nentries || chain_nr != -1) {
  711. /* end of udc, go back one 'recursion' step */
  712. if (pos == nentries) {
  713. /* put back values of the time when this chain was called */
  714. e = cl_s[chain_nr].cs.e;
  715. if (cl_s[chain_nr].from != -1)
  716. nentries =
  717. cl_s[cl_s[chain_nr].from].cs.chaininfo->nentries;
  718. else
  719. nentries = chain->nentries;
  720. pos = cl_s[chain_nr].cs.n;
  721. /* make sure we won't see a loop that isn't one */
  722. cl_s[chain_nr].cs.n = 0;
  723. chain_nr = cl_s[chain_nr].from;
  724. if (pos == nentries)
  725. continue;
  726. }
  727. t = (struct ebt_entry_target *)
  728. (((char *)e) + e->target_offset);
  729. if (strcmp(t->u.name, EBT_STANDARD_TARGET))
  730. goto letscontinue;
  731. if (e->target_offset + sizeof(struct ebt_standard_target) >
  732. e->next_offset) {
  733. BUGPRINT("Standard target size too big\n");
  734. return -1;
  735. }
  736. verdict = ((struct ebt_standard_target *)t)->verdict;
  737. if (verdict >= 0) { /* jump to another chain */
  738. struct ebt_entries *hlp2 =
  739. (struct ebt_entries *)(base + verdict);
  740. for (i = 0; i < udc_cnt; i++)
  741. if (hlp2 == cl_s[i].cs.chaininfo)
  742. break;
  743. /* bad destination or loop */
  744. if (i == udc_cnt) {
  745. BUGPRINT("bad destination\n");
  746. return -1;
  747. }
  748. if (cl_s[i].cs.n) {
  749. BUGPRINT("loop\n");
  750. return -1;
  751. }
  752. if (cl_s[i].hookmask & (1 << hooknr))
  753. goto letscontinue;
  754. /* this can't be 0, so the loop test is correct */
  755. cl_s[i].cs.n = pos + 1;
  756. pos = 0;
  757. cl_s[i].cs.e = ebt_next_entry(e);
  758. e = (struct ebt_entry *)(hlp2->data);
  759. nentries = hlp2->nentries;
  760. cl_s[i].from = chain_nr;
  761. chain_nr = i;
  762. /* this udc is accessible from the base chain for hooknr */
  763. cl_s[i].hookmask |= (1 << hooknr);
  764. continue;
  765. }
  766. letscontinue:
  767. e = ebt_next_entry(e);
  768. pos++;
  769. }
  770. return 0;
  771. }
  772. /* do the parsing of the table/chains/entries/matches/watchers/targets, heh */
  773. static int translate_table(struct net *net, const char *name,
  774. struct ebt_table_info *newinfo)
  775. {
  776. unsigned int i, j, k, udc_cnt;
  777. int ret;
  778. struct ebt_cl_stack *cl_s = NULL; /* used in the checking for chain loops */
  779. i = 0;
  780. while (i < NF_BR_NUMHOOKS && !newinfo->hook_entry[i])
  781. i++;
  782. if (i == NF_BR_NUMHOOKS) {
  783. BUGPRINT("No valid hooks specified\n");
  784. return -EINVAL;
  785. }
  786. if (newinfo->hook_entry[i] != (struct ebt_entries *)newinfo->entries) {
  787. BUGPRINT("Chains don't start at beginning\n");
  788. return -EINVAL;
  789. }
  790. /* make sure chains are ordered after each other in same order
  791. * as their corresponding hooks
  792. */
  793. for (j = i + 1; j < NF_BR_NUMHOOKS; j++) {
  794. if (!newinfo->hook_entry[j])
  795. continue;
  796. if (newinfo->hook_entry[j] <= newinfo->hook_entry[i]) {
  797. BUGPRINT("Hook order must be followed\n");
  798. return -EINVAL;
  799. }
  800. i = j;
  801. }
  802. /* do some early checkings and initialize some things */
  803. i = 0; /* holds the expected nr. of entries for the chain */
  804. j = 0; /* holds the up to now counted entries for the chain */
  805. k = 0; /* holds the total nr. of entries, should equal
  806. * newinfo->nentries afterwards
  807. */
  808. udc_cnt = 0; /* will hold the nr. of user defined chains (udc) */
  809. ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
  810. ebt_check_entry_size_and_hooks, newinfo,
  811. &i, &j, &k, &udc_cnt);
  812. if (ret != 0)
  813. return ret;
  814. if (i != j) {
  815. BUGPRINT("nentries does not equal the nr of entries in the "
  816. "(last) chain\n");
  817. return -EINVAL;
  818. }
  819. if (k != newinfo->nentries) {
  820. BUGPRINT("Total nentries is wrong\n");
  821. return -EINVAL;
  822. }
  823. /* get the location of the udc, put them in an array
  824. * while we're at it, allocate the chainstack
  825. */
  826. if (udc_cnt) {
  827. /* this will get free'd in do_replace()/ebt_register_table()
  828. * if an error occurs
  829. */
  830. newinfo->chainstack =
  831. vmalloc(nr_cpu_ids * sizeof(*(newinfo->chainstack)));
  832. if (!newinfo->chainstack)
  833. return -ENOMEM;
  834. for_each_possible_cpu(i) {
  835. newinfo->chainstack[i] =
  836. vmalloc(udc_cnt * sizeof(*(newinfo->chainstack[0])));
  837. if (!newinfo->chainstack[i]) {
  838. while (i)
  839. vfree(newinfo->chainstack[--i]);
  840. vfree(newinfo->chainstack);
  841. newinfo->chainstack = NULL;
  842. return -ENOMEM;
  843. }
  844. }
  845. cl_s = vmalloc(udc_cnt * sizeof(*cl_s));
  846. if (!cl_s)
  847. return -ENOMEM;
  848. i = 0; /* the i'th udc */
  849. EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
  850. ebt_get_udc_positions, newinfo, &i, cl_s);
  851. /* sanity check */
  852. if (i != udc_cnt) {
  853. BUGPRINT("i != udc_cnt\n");
  854. vfree(cl_s);
  855. return -EFAULT;
  856. }
  857. }
  858. /* Check for loops */
  859. for (i = 0; i < NF_BR_NUMHOOKS; i++)
  860. if (newinfo->hook_entry[i])
  861. if (check_chainloops(newinfo->hook_entry[i],
  862. cl_s, udc_cnt, i, newinfo->entries)) {
  863. vfree(cl_s);
  864. return -EINVAL;
  865. }
  866. /* we now know the following (along with E=mc²):
  867. * - the nr of entries in each chain is right
  868. * - the size of the allocated space is right
  869. * - all valid hooks have a corresponding chain
  870. * - there are no loops
  871. * - wrong data can still be on the level of a single entry
  872. * - could be there are jumps to places that are not the
  873. * beginning of a chain. This can only occur in chains that
  874. * are not accessible from any base chains, so we don't care.
  875. */
  876. /* used to know what we need to clean up if something goes wrong */
  877. i = 0;
  878. ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
  879. ebt_check_entry, net, newinfo, name, &i, cl_s, udc_cnt);
  880. if (ret != 0) {
  881. EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
  882. ebt_cleanup_entry, net, &i);
  883. }
  884. vfree(cl_s);
  885. return ret;
  886. }
  887. /* called under write_lock */
  888. static void get_counters(const struct ebt_counter *oldcounters,
  889. struct ebt_counter *counters, unsigned int nentries)
  890. {
  891. int i, cpu;
  892. struct ebt_counter *counter_base;
  893. /* counters of cpu 0 */
  894. memcpy(counters, oldcounters,
  895. sizeof(struct ebt_counter) * nentries);
  896. /* add other counters to those of cpu 0 */
  897. for_each_possible_cpu(cpu) {
  898. if (cpu == 0)
  899. continue;
  900. counter_base = COUNTER_BASE(oldcounters, nentries, cpu);
  901. for (i = 0; i < nentries; i++) {
  902. counters[i].pcnt += counter_base[i].pcnt;
  903. counters[i].bcnt += counter_base[i].bcnt;
  904. }
  905. }
  906. }
  907. static int do_replace_finish(struct net *net, struct ebt_replace *repl,
  908. struct ebt_table_info *newinfo)
  909. {
  910. int ret, i;
  911. struct ebt_counter *counterstmp = NULL;
  912. /* used to be able to unlock earlier */
  913. struct ebt_table_info *table;
  914. struct ebt_table *t;
  915. /* the user wants counters back
  916. * the check on the size is done later, when we have the lock
  917. */
  918. if (repl->num_counters) {
  919. unsigned long size = repl->num_counters * sizeof(*counterstmp);
  920. counterstmp = vmalloc(size);
  921. if (!counterstmp)
  922. return -ENOMEM;
  923. }
  924. newinfo->chainstack = NULL;
  925. ret = ebt_verify_pointers(repl, newinfo);
  926. if (ret != 0)
  927. goto free_counterstmp;
  928. ret = translate_table(net, repl->name, newinfo);
  929. if (ret != 0)
  930. goto free_counterstmp;
  931. t = find_table_lock(net, repl->name, &ret, &ebt_mutex);
  932. if (!t) {
  933. ret = -ENOENT;
  934. goto free_iterate;
  935. }
  936. /* the table doesn't like it */
  937. if (t->check && (ret = t->check(newinfo, repl->valid_hooks)))
  938. goto free_unlock;
  939. if (repl->num_counters && repl->num_counters != t->private->nentries) {
  940. BUGPRINT("Wrong nr. of counters requested\n");
  941. ret = -EINVAL;
  942. goto free_unlock;
  943. }
  944. /* we have the mutex lock, so no danger in reading this pointer */
  945. table = t->private;
  946. /* make sure the table can only be rmmod'ed if it contains no rules */
  947. if (!table->nentries && newinfo->nentries && !try_module_get(t->me)) {
  948. ret = -ENOENT;
  949. goto free_unlock;
  950. } else if (table->nentries && !newinfo->nentries)
  951. module_put(t->me);
  952. /* we need an atomic snapshot of the counters */
  953. write_lock_bh(&t->lock);
  954. if (repl->num_counters)
  955. get_counters(t->private->counters, counterstmp,
  956. t->private->nentries);
  957. t->private = newinfo;
  958. write_unlock_bh(&t->lock);
  959. mutex_unlock(&ebt_mutex);
  960. /* so, a user can change the chains while having messed up her counter
  961. * allocation. Only reason why this is done is because this way the lock
  962. * is held only once, while this doesn't bring the kernel into a
  963. * dangerous state.
  964. */
  965. if (repl->num_counters &&
  966. copy_to_user(repl->counters, counterstmp,
  967. repl->num_counters * sizeof(struct ebt_counter))) {
  968. /* Silent error, can't fail, new table is already in place */
  969. net_warn_ratelimited("ebtables: counters copy to user failed while replacing table\n");
  970. }
  971. /* decrease module count and free resources */
  972. EBT_ENTRY_ITERATE(table->entries, table->entries_size,
  973. ebt_cleanup_entry, net, NULL);
  974. vfree(table->entries);
  975. if (table->chainstack) {
  976. for_each_possible_cpu(i)
  977. vfree(table->chainstack[i]);
  978. vfree(table->chainstack);
  979. }
  980. vfree(table);
  981. vfree(counterstmp);
  982. #ifdef CONFIG_AUDIT
  983. if (audit_enabled) {
  984. struct audit_buffer *ab;
  985. ab = audit_log_start(current->audit_context, GFP_KERNEL,
  986. AUDIT_NETFILTER_CFG);
  987. if (ab) {
  988. audit_log_format(ab, "table=%s family=%u entries=%u",
  989. repl->name, AF_BRIDGE, repl->nentries);
  990. audit_log_end(ab);
  991. }
  992. }
  993. #endif
  994. return ret;
  995. free_unlock:
  996. mutex_unlock(&ebt_mutex);
  997. free_iterate:
  998. EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
  999. ebt_cleanup_entry, net, NULL);
  1000. free_counterstmp:
  1001. vfree(counterstmp);
  1002. /* can be initialized in translate_table() */
  1003. if (newinfo->chainstack) {
  1004. for_each_possible_cpu(i)
  1005. vfree(newinfo->chainstack[i]);
  1006. vfree(newinfo->chainstack);
  1007. }
  1008. return ret;
  1009. }
  1010. /* replace the table */
  1011. static int do_replace(struct net *net, const void __user *user,
  1012. unsigned int len)
  1013. {
  1014. int ret, countersize;
  1015. struct ebt_table_info *newinfo;
  1016. struct ebt_replace tmp;
  1017. if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
  1018. return -EFAULT;
  1019. if (len != sizeof(tmp) + tmp.entries_size) {
  1020. BUGPRINT("Wrong len argument\n");
  1021. return -EINVAL;
  1022. }
  1023. if (tmp.entries_size == 0) {
  1024. BUGPRINT("Entries_size never zero\n");
  1025. return -EINVAL;
  1026. }
  1027. /* overflow check */
  1028. if (tmp.nentries >= ((INT_MAX - sizeof(struct ebt_table_info)) /
  1029. NR_CPUS - SMP_CACHE_BYTES) / sizeof(struct ebt_counter))
  1030. return -ENOMEM;
  1031. if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter))
  1032. return -ENOMEM;
  1033. tmp.name[sizeof(tmp.name) - 1] = 0;
  1034. countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids;
  1035. newinfo = vmalloc(sizeof(*newinfo) + countersize);
  1036. if (!newinfo)
  1037. return -ENOMEM;
  1038. if (countersize)
  1039. memset(newinfo->counters, 0, countersize);
  1040. newinfo->entries = vmalloc(tmp.entries_size);
  1041. if (!newinfo->entries) {
  1042. ret = -ENOMEM;
  1043. goto free_newinfo;
  1044. }
  1045. if (copy_from_user(
  1046. newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
  1047. BUGPRINT("Couldn't copy entries from userspace\n");
  1048. ret = -EFAULT;
  1049. goto free_entries;
  1050. }
  1051. ret = do_replace_finish(net, &tmp, newinfo);
  1052. if (ret == 0)
  1053. return ret;
  1054. free_entries:
  1055. vfree(newinfo->entries);
  1056. free_newinfo:
  1057. vfree(newinfo);
  1058. return ret;
  1059. }
  1060. struct ebt_table *
  1061. ebt_register_table(struct net *net, const struct ebt_table *input_table)
  1062. {
  1063. struct ebt_table_info *newinfo;
  1064. struct ebt_table *t, *table;
  1065. struct ebt_replace_kernel *repl;
  1066. int ret, i, countersize;
  1067. void *p;
  1068. if (input_table == NULL || (repl = input_table->table) == NULL ||
  1069. repl->entries == NULL || repl->entries_size == 0 ||
  1070. repl->counters != NULL || input_table->private != NULL) {
  1071. BUGPRINT("Bad table data for ebt_register_table!!!\n");
  1072. return ERR_PTR(-EINVAL);
  1073. }
  1074. /* Don't add one table to multiple lists. */
  1075. table = kmemdup(input_table, sizeof(struct ebt_table), GFP_KERNEL);
  1076. if (!table) {
  1077. ret = -ENOMEM;
  1078. goto out;
  1079. }
  1080. countersize = COUNTER_OFFSET(repl->nentries) * nr_cpu_ids;
  1081. newinfo = vmalloc(sizeof(*newinfo) + countersize);
  1082. ret = -ENOMEM;
  1083. if (!newinfo)
  1084. goto free_table;
  1085. p = vmalloc(repl->entries_size);
  1086. if (!p)
  1087. goto free_newinfo;
  1088. memcpy(p, repl->entries, repl->entries_size);
  1089. newinfo->entries = p;
  1090. newinfo->entries_size = repl->entries_size;
  1091. newinfo->nentries = repl->nentries;
  1092. if (countersize)
  1093. memset(newinfo->counters, 0, countersize);
  1094. /* fill in newinfo and parse the entries */
  1095. newinfo->chainstack = NULL;
  1096. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  1097. if ((repl->valid_hooks & (1 << i)) == 0)
  1098. newinfo->hook_entry[i] = NULL;
  1099. else
  1100. newinfo->hook_entry[i] = p +
  1101. ((char *)repl->hook_entry[i] - repl->entries);
  1102. }
  1103. ret = translate_table(net, repl->name, newinfo);
  1104. if (ret != 0) {
  1105. BUGPRINT("Translate_table failed\n");
  1106. goto free_chainstack;
  1107. }
  1108. if (table->check && table->check(newinfo, table->valid_hooks)) {
  1109. BUGPRINT("The table doesn't like its own initial data, lol\n");
  1110. ret = -EINVAL;
  1111. goto free_chainstack;
  1112. }
  1113. table->private = newinfo;
  1114. rwlock_init(&table->lock);
  1115. mutex_lock(&ebt_mutex);
  1116. list_for_each_entry(t, &net->xt.tables[NFPROTO_BRIDGE], list) {
  1117. if (strcmp(t->name, table->name) == 0) {
  1118. ret = -EEXIST;
  1119. BUGPRINT("Table name already exists\n");
  1120. goto free_unlock;
  1121. }
  1122. }
  1123. /* Hold a reference count if the chains aren't empty */
  1124. if (newinfo->nentries && !try_module_get(table->me)) {
  1125. ret = -ENOENT;
  1126. goto free_unlock;
  1127. }
  1128. list_add(&table->list, &net->xt.tables[NFPROTO_BRIDGE]);
  1129. mutex_unlock(&ebt_mutex);
  1130. return table;
  1131. free_unlock:
  1132. mutex_unlock(&ebt_mutex);
  1133. free_chainstack:
  1134. if (newinfo->chainstack) {
  1135. for_each_possible_cpu(i)
  1136. vfree(newinfo->chainstack[i]);
  1137. vfree(newinfo->chainstack);
  1138. }
  1139. vfree(newinfo->entries);
  1140. free_newinfo:
  1141. vfree(newinfo);
  1142. free_table:
  1143. kfree(table);
  1144. out:
  1145. return ERR_PTR(ret);
  1146. }
  1147. void ebt_unregister_table(struct net *net, struct ebt_table *table)
  1148. {
  1149. int i;
  1150. if (!table) {
  1151. BUGPRINT("Request to unregister NULL table!!!\n");
  1152. return;
  1153. }
  1154. mutex_lock(&ebt_mutex);
  1155. list_del(&table->list);
  1156. mutex_unlock(&ebt_mutex);
  1157. EBT_ENTRY_ITERATE(table->private->entries, table->private->entries_size,
  1158. ebt_cleanup_entry, net, NULL);
  1159. if (table->private->nentries)
  1160. module_put(table->me);
  1161. vfree(table->private->entries);
  1162. if (table->private->chainstack) {
  1163. for_each_possible_cpu(i)
  1164. vfree(table->private->chainstack[i]);
  1165. vfree(table->private->chainstack);
  1166. }
  1167. vfree(table->private);
  1168. kfree(table);
  1169. }
  1170. /* userspace just supplied us with counters */
  1171. static int do_update_counters(struct net *net, const char *name,
  1172. struct ebt_counter __user *counters,
  1173. unsigned int num_counters,
  1174. const void __user *user, unsigned int len)
  1175. {
  1176. int i, ret;
  1177. struct ebt_counter *tmp;
  1178. struct ebt_table *t;
  1179. if (num_counters == 0)
  1180. return -EINVAL;
  1181. tmp = vmalloc(num_counters * sizeof(*tmp));
  1182. if (!tmp)
  1183. return -ENOMEM;
  1184. t = find_table_lock(net, name, &ret, &ebt_mutex);
  1185. if (!t)
  1186. goto free_tmp;
  1187. if (num_counters != t->private->nentries) {
  1188. BUGPRINT("Wrong nr of counters\n");
  1189. ret = -EINVAL;
  1190. goto unlock_mutex;
  1191. }
  1192. if (copy_from_user(tmp, counters, num_counters * sizeof(*counters))) {
  1193. ret = -EFAULT;
  1194. goto unlock_mutex;
  1195. }
  1196. /* we want an atomic add of the counters */
  1197. write_lock_bh(&t->lock);
  1198. /* we add to the counters of the first cpu */
  1199. for (i = 0; i < num_counters; i++) {
  1200. t->private->counters[i].pcnt += tmp[i].pcnt;
  1201. t->private->counters[i].bcnt += tmp[i].bcnt;
  1202. }
  1203. write_unlock_bh(&t->lock);
  1204. ret = 0;
  1205. unlock_mutex:
  1206. mutex_unlock(&ebt_mutex);
  1207. free_tmp:
  1208. vfree(tmp);
  1209. return ret;
  1210. }
  1211. static int update_counters(struct net *net, const void __user *user,
  1212. unsigned int len)
  1213. {
  1214. struct ebt_replace hlp;
  1215. if (copy_from_user(&hlp, user, sizeof(hlp)))
  1216. return -EFAULT;
  1217. if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
  1218. return -EINVAL;
  1219. return do_update_counters(net, hlp.name, hlp.counters,
  1220. hlp.num_counters, user, len);
  1221. }
  1222. static inline int ebt_make_matchname(const struct ebt_entry_match *m,
  1223. const char *base, char __user *ubase)
  1224. {
  1225. char __user *hlp = ubase + ((char *)m - base);
  1226. char name[EBT_FUNCTION_MAXNAMELEN] = {};
  1227. /* ebtables expects 32 bytes long names but xt_match names are 29 bytes
  1228. * long. Copy 29 bytes and fill remaining bytes with zeroes.
  1229. */
  1230. strlcpy(name, m->u.match->name, sizeof(name));
  1231. if (copy_to_user(hlp, name, EBT_FUNCTION_MAXNAMELEN))
  1232. return -EFAULT;
  1233. return 0;
  1234. }
  1235. static inline int ebt_make_watchername(const struct ebt_entry_watcher *w,
  1236. const char *base, char __user *ubase)
  1237. {
  1238. char __user *hlp = ubase + ((char *)w - base);
  1239. char name[EBT_FUNCTION_MAXNAMELEN] = {};
  1240. strlcpy(name, w->u.watcher->name, sizeof(name));
  1241. if (copy_to_user(hlp, name, EBT_FUNCTION_MAXNAMELEN))
  1242. return -EFAULT;
  1243. return 0;
  1244. }
  1245. static inline int ebt_make_names(struct ebt_entry *e, const char *base,
  1246. char __user *ubase)
  1247. {
  1248. int ret;
  1249. char __user *hlp;
  1250. const struct ebt_entry_target *t;
  1251. char name[EBT_FUNCTION_MAXNAMELEN] = {};
  1252. if (e->bitmask == 0)
  1253. return 0;
  1254. hlp = ubase + (((char *)e + e->target_offset) - base);
  1255. t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
  1256. ret = EBT_MATCH_ITERATE(e, ebt_make_matchname, base, ubase);
  1257. if (ret != 0)
  1258. return ret;
  1259. ret = EBT_WATCHER_ITERATE(e, ebt_make_watchername, base, ubase);
  1260. if (ret != 0)
  1261. return ret;
  1262. strlcpy(name, t->u.target->name, sizeof(name));
  1263. if (copy_to_user(hlp, name, EBT_FUNCTION_MAXNAMELEN))
  1264. return -EFAULT;
  1265. return 0;
  1266. }
  1267. static int copy_counters_to_user(struct ebt_table *t,
  1268. const struct ebt_counter *oldcounters,
  1269. void __user *user, unsigned int num_counters,
  1270. unsigned int nentries)
  1271. {
  1272. struct ebt_counter *counterstmp;
  1273. int ret = 0;
  1274. /* userspace might not need the counters */
  1275. if (num_counters == 0)
  1276. return 0;
  1277. if (num_counters != nentries) {
  1278. BUGPRINT("Num_counters wrong\n");
  1279. return -EINVAL;
  1280. }
  1281. counterstmp = vmalloc(nentries * sizeof(*counterstmp));
  1282. if (!counterstmp)
  1283. return -ENOMEM;
  1284. write_lock_bh(&t->lock);
  1285. get_counters(oldcounters, counterstmp, nentries);
  1286. write_unlock_bh(&t->lock);
  1287. if (copy_to_user(user, counterstmp,
  1288. nentries * sizeof(struct ebt_counter)))
  1289. ret = -EFAULT;
  1290. vfree(counterstmp);
  1291. return ret;
  1292. }
  1293. /* called with ebt_mutex locked */
  1294. static int copy_everything_to_user(struct ebt_table *t, void __user *user,
  1295. const int *len, int cmd)
  1296. {
  1297. struct ebt_replace tmp;
  1298. const struct ebt_counter *oldcounters;
  1299. unsigned int entries_size, nentries;
  1300. int ret;
  1301. char *entries;
  1302. if (cmd == EBT_SO_GET_ENTRIES) {
  1303. entries_size = t->private->entries_size;
  1304. nentries = t->private->nentries;
  1305. entries = t->private->entries;
  1306. oldcounters = t->private->counters;
  1307. } else {
  1308. entries_size = t->table->entries_size;
  1309. nentries = t->table->nentries;
  1310. entries = t->table->entries;
  1311. oldcounters = t->table->counters;
  1312. }
  1313. if (copy_from_user(&tmp, user, sizeof(tmp)))
  1314. return -EFAULT;
  1315. if (*len != sizeof(struct ebt_replace) + entries_size +
  1316. (tmp.num_counters ? nentries * sizeof(struct ebt_counter) : 0))
  1317. return -EINVAL;
  1318. if (tmp.nentries != nentries) {
  1319. BUGPRINT("Nentries wrong\n");
  1320. return -EINVAL;
  1321. }
  1322. if (tmp.entries_size != entries_size) {
  1323. BUGPRINT("Wrong size\n");
  1324. return -EINVAL;
  1325. }
  1326. ret = copy_counters_to_user(t, oldcounters, tmp.counters,
  1327. tmp.num_counters, nentries);
  1328. if (ret)
  1329. return ret;
  1330. if (copy_to_user(tmp.entries, entries, entries_size)) {
  1331. BUGPRINT("Couldn't copy entries to userspace\n");
  1332. return -EFAULT;
  1333. }
  1334. /* set the match/watcher/target names right */
  1335. return EBT_ENTRY_ITERATE(entries, entries_size,
  1336. ebt_make_names, entries, tmp.entries);
  1337. }
  1338. static int do_ebt_set_ctl(struct sock *sk,
  1339. int cmd, void __user *user, unsigned int len)
  1340. {
  1341. int ret;
  1342. struct net *net = sock_net(sk);
  1343. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1344. return -EPERM;
  1345. switch (cmd) {
  1346. case EBT_SO_SET_ENTRIES:
  1347. ret = do_replace(net, user, len);
  1348. break;
  1349. case EBT_SO_SET_COUNTERS:
  1350. ret = update_counters(net, user, len);
  1351. break;
  1352. default:
  1353. ret = -EINVAL;
  1354. }
  1355. return ret;
  1356. }
  1357. static int do_ebt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
  1358. {
  1359. int ret;
  1360. struct ebt_replace tmp;
  1361. struct ebt_table *t;
  1362. struct net *net = sock_net(sk);
  1363. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1364. return -EPERM;
  1365. if (copy_from_user(&tmp, user, sizeof(tmp)))
  1366. return -EFAULT;
  1367. tmp.name[sizeof(tmp.name) - 1] = '\0';
  1368. t = find_table_lock(net, tmp.name, &ret, &ebt_mutex);
  1369. if (!t)
  1370. return ret;
  1371. switch (cmd) {
  1372. case EBT_SO_GET_INFO:
  1373. case EBT_SO_GET_INIT_INFO:
  1374. if (*len != sizeof(struct ebt_replace)) {
  1375. ret = -EINVAL;
  1376. mutex_unlock(&ebt_mutex);
  1377. break;
  1378. }
  1379. if (cmd == EBT_SO_GET_INFO) {
  1380. tmp.nentries = t->private->nentries;
  1381. tmp.entries_size = t->private->entries_size;
  1382. tmp.valid_hooks = t->valid_hooks;
  1383. } else {
  1384. tmp.nentries = t->table->nentries;
  1385. tmp.entries_size = t->table->entries_size;
  1386. tmp.valid_hooks = t->table->valid_hooks;
  1387. }
  1388. mutex_unlock(&ebt_mutex);
  1389. if (copy_to_user(user, &tmp, *len) != 0) {
  1390. BUGPRINT("c2u Didn't work\n");
  1391. ret = -EFAULT;
  1392. break;
  1393. }
  1394. ret = 0;
  1395. break;
  1396. case EBT_SO_GET_ENTRIES:
  1397. case EBT_SO_GET_INIT_ENTRIES:
  1398. ret = copy_everything_to_user(t, user, len, cmd);
  1399. mutex_unlock(&ebt_mutex);
  1400. break;
  1401. default:
  1402. mutex_unlock(&ebt_mutex);
  1403. ret = -EINVAL;
  1404. }
  1405. return ret;
  1406. }
  1407. #ifdef CONFIG_COMPAT
  1408. /* 32 bit-userspace compatibility definitions. */
  1409. struct compat_ebt_replace {
  1410. char name[EBT_TABLE_MAXNAMELEN];
  1411. compat_uint_t valid_hooks;
  1412. compat_uint_t nentries;
  1413. compat_uint_t entries_size;
  1414. /* start of the chains */
  1415. compat_uptr_t hook_entry[NF_BR_NUMHOOKS];
  1416. /* nr of counters userspace expects back */
  1417. compat_uint_t num_counters;
  1418. /* where the kernel will put the old counters. */
  1419. compat_uptr_t counters;
  1420. compat_uptr_t entries;
  1421. };
  1422. /* struct ebt_entry_match, _target and _watcher have same layout */
  1423. struct compat_ebt_entry_mwt {
  1424. union {
  1425. char name[EBT_FUNCTION_MAXNAMELEN];
  1426. compat_uptr_t ptr;
  1427. } u;
  1428. compat_uint_t match_size;
  1429. compat_uint_t data[0];
  1430. };
  1431. /* account for possible padding between match_size and ->data */
  1432. static int ebt_compat_entry_padsize(void)
  1433. {
  1434. BUILD_BUG_ON(XT_ALIGN(sizeof(struct ebt_entry_match)) <
  1435. COMPAT_XT_ALIGN(sizeof(struct compat_ebt_entry_mwt)));
  1436. return (int) XT_ALIGN(sizeof(struct ebt_entry_match)) -
  1437. COMPAT_XT_ALIGN(sizeof(struct compat_ebt_entry_mwt));
  1438. }
  1439. static int ebt_compat_match_offset(const struct xt_match *match,
  1440. unsigned int userlen)
  1441. {
  1442. /* ebt_among needs special handling. The kernel .matchsize is
  1443. * set to -1 at registration time; at runtime an EBT_ALIGN()ed
  1444. * value is expected.
  1445. * Example: userspace sends 4500, ebt_among.c wants 4504.
  1446. */
  1447. if (unlikely(match->matchsize == -1))
  1448. return XT_ALIGN(userlen) - COMPAT_XT_ALIGN(userlen);
  1449. return xt_compat_match_offset(match);
  1450. }
  1451. static int compat_match_to_user(struct ebt_entry_match *m, void __user **dstptr,
  1452. unsigned int *size)
  1453. {
  1454. const struct xt_match *match = m->u.match;
  1455. struct compat_ebt_entry_mwt __user *cm = *dstptr;
  1456. int off = ebt_compat_match_offset(match, m->match_size);
  1457. compat_uint_t msize = m->match_size - off;
  1458. if (WARN_ON(off >= m->match_size))
  1459. return -EINVAL;
  1460. if (copy_to_user(cm->u.name, match->name,
  1461. strlen(match->name) + 1) || put_user(msize, &cm->match_size))
  1462. return -EFAULT;
  1463. if (match->compat_to_user) {
  1464. if (match->compat_to_user(cm->data, m->data))
  1465. return -EFAULT;
  1466. } else if (copy_to_user(cm->data, m->data, msize))
  1467. return -EFAULT;
  1468. *size -= ebt_compat_entry_padsize() + off;
  1469. *dstptr = cm->data;
  1470. *dstptr += msize;
  1471. return 0;
  1472. }
  1473. static int compat_target_to_user(struct ebt_entry_target *t,
  1474. void __user **dstptr,
  1475. unsigned int *size)
  1476. {
  1477. const struct xt_target *target = t->u.target;
  1478. struct compat_ebt_entry_mwt __user *cm = *dstptr;
  1479. int off = xt_compat_target_offset(target);
  1480. compat_uint_t tsize = t->target_size - off;
  1481. if (WARN_ON(off >= t->target_size))
  1482. return -EINVAL;
  1483. if (copy_to_user(cm->u.name, target->name,
  1484. strlen(target->name) + 1) || put_user(tsize, &cm->match_size))
  1485. return -EFAULT;
  1486. if (target->compat_to_user) {
  1487. if (target->compat_to_user(cm->data, t->data))
  1488. return -EFAULT;
  1489. } else if (copy_to_user(cm->data, t->data, tsize))
  1490. return -EFAULT;
  1491. *size -= ebt_compat_entry_padsize() + off;
  1492. *dstptr = cm->data;
  1493. *dstptr += tsize;
  1494. return 0;
  1495. }
  1496. static int compat_watcher_to_user(struct ebt_entry_watcher *w,
  1497. void __user **dstptr,
  1498. unsigned int *size)
  1499. {
  1500. return compat_target_to_user((struct ebt_entry_target *)w,
  1501. dstptr, size);
  1502. }
  1503. static int compat_copy_entry_to_user(struct ebt_entry *e, void __user **dstptr,
  1504. unsigned int *size)
  1505. {
  1506. struct ebt_entry_target *t;
  1507. struct ebt_entry __user *ce;
  1508. u32 watchers_offset, target_offset, next_offset;
  1509. compat_uint_t origsize;
  1510. int ret;
  1511. if (e->bitmask == 0) {
  1512. if (*size < sizeof(struct ebt_entries))
  1513. return -EINVAL;
  1514. if (copy_to_user(*dstptr, e, sizeof(struct ebt_entries)))
  1515. return -EFAULT;
  1516. *dstptr += sizeof(struct ebt_entries);
  1517. *size -= sizeof(struct ebt_entries);
  1518. return 0;
  1519. }
  1520. if (*size < sizeof(*ce))
  1521. return -EINVAL;
  1522. ce = (struct ebt_entry __user *)*dstptr;
  1523. if (copy_to_user(ce, e, sizeof(*ce)))
  1524. return -EFAULT;
  1525. origsize = *size;
  1526. *dstptr += sizeof(*ce);
  1527. ret = EBT_MATCH_ITERATE(e, compat_match_to_user, dstptr, size);
  1528. if (ret)
  1529. return ret;
  1530. watchers_offset = e->watchers_offset - (origsize - *size);
  1531. ret = EBT_WATCHER_ITERATE(e, compat_watcher_to_user, dstptr, size);
  1532. if (ret)
  1533. return ret;
  1534. target_offset = e->target_offset - (origsize - *size);
  1535. t = (struct ebt_entry_target *) ((char *) e + e->target_offset);
  1536. ret = compat_target_to_user(t, dstptr, size);
  1537. if (ret)
  1538. return ret;
  1539. next_offset = e->next_offset - (origsize - *size);
  1540. if (put_user(watchers_offset, &ce->watchers_offset) ||
  1541. put_user(target_offset, &ce->target_offset) ||
  1542. put_user(next_offset, &ce->next_offset))
  1543. return -EFAULT;
  1544. *size -= sizeof(*ce);
  1545. return 0;
  1546. }
  1547. static int compat_calc_match(struct ebt_entry_match *m, int *off)
  1548. {
  1549. *off += ebt_compat_match_offset(m->u.match, m->match_size);
  1550. *off += ebt_compat_entry_padsize();
  1551. return 0;
  1552. }
  1553. static int compat_calc_watcher(struct ebt_entry_watcher *w, int *off)
  1554. {
  1555. *off += xt_compat_target_offset(w->u.watcher);
  1556. *off += ebt_compat_entry_padsize();
  1557. return 0;
  1558. }
  1559. static int compat_calc_entry(const struct ebt_entry *e,
  1560. const struct ebt_table_info *info,
  1561. const void *base,
  1562. struct compat_ebt_replace *newinfo)
  1563. {
  1564. const struct ebt_entry_target *t;
  1565. unsigned int entry_offset;
  1566. int off, ret, i;
  1567. if (e->bitmask == 0)
  1568. return 0;
  1569. off = 0;
  1570. entry_offset = (void *)e - base;
  1571. EBT_MATCH_ITERATE(e, compat_calc_match, &off);
  1572. EBT_WATCHER_ITERATE(e, compat_calc_watcher, &off);
  1573. t = (const struct ebt_entry_target *) ((char *) e + e->target_offset);
  1574. off += xt_compat_target_offset(t->u.target);
  1575. off += ebt_compat_entry_padsize();
  1576. newinfo->entries_size -= off;
  1577. ret = xt_compat_add_offset(NFPROTO_BRIDGE, entry_offset, off);
  1578. if (ret)
  1579. return ret;
  1580. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  1581. const void *hookptr = info->hook_entry[i];
  1582. if (info->hook_entry[i] &&
  1583. (e < (struct ebt_entry *)(base - hookptr))) {
  1584. newinfo->hook_entry[i] -= off;
  1585. pr_debug("0x%08X -> 0x%08X\n",
  1586. newinfo->hook_entry[i] + off,
  1587. newinfo->hook_entry[i]);
  1588. }
  1589. }
  1590. return 0;
  1591. }
  1592. static int compat_table_info(const struct ebt_table_info *info,
  1593. struct compat_ebt_replace *newinfo)
  1594. {
  1595. unsigned int size = info->entries_size;
  1596. const void *entries = info->entries;
  1597. newinfo->entries_size = size;
  1598. xt_compat_init_offsets(NFPROTO_BRIDGE, info->nentries);
  1599. return EBT_ENTRY_ITERATE(entries, size, compat_calc_entry, info,
  1600. entries, newinfo);
  1601. }
  1602. static int compat_copy_everything_to_user(struct ebt_table *t,
  1603. void __user *user, int *len, int cmd)
  1604. {
  1605. struct compat_ebt_replace repl, tmp;
  1606. struct ebt_counter *oldcounters;
  1607. struct ebt_table_info tinfo;
  1608. int ret;
  1609. void __user *pos;
  1610. memset(&tinfo, 0, sizeof(tinfo));
  1611. if (cmd == EBT_SO_GET_ENTRIES) {
  1612. tinfo.entries_size = t->private->entries_size;
  1613. tinfo.nentries = t->private->nentries;
  1614. tinfo.entries = t->private->entries;
  1615. oldcounters = t->private->counters;
  1616. } else {
  1617. tinfo.entries_size = t->table->entries_size;
  1618. tinfo.nentries = t->table->nentries;
  1619. tinfo.entries = t->table->entries;
  1620. oldcounters = t->table->counters;
  1621. }
  1622. if (copy_from_user(&tmp, user, sizeof(tmp)))
  1623. return -EFAULT;
  1624. if (tmp.nentries != tinfo.nentries ||
  1625. (tmp.num_counters && tmp.num_counters != tinfo.nentries))
  1626. return -EINVAL;
  1627. memcpy(&repl, &tmp, sizeof(repl));
  1628. if (cmd == EBT_SO_GET_ENTRIES)
  1629. ret = compat_table_info(t->private, &repl);
  1630. else
  1631. ret = compat_table_info(&tinfo, &repl);
  1632. if (ret)
  1633. return ret;
  1634. if (*len != sizeof(tmp) + repl.entries_size +
  1635. (tmp.num_counters? tinfo.nentries * sizeof(struct ebt_counter): 0)) {
  1636. pr_err("wrong size: *len %d, entries_size %u, replsz %d\n",
  1637. *len, tinfo.entries_size, repl.entries_size);
  1638. return -EINVAL;
  1639. }
  1640. /* userspace might not need the counters */
  1641. ret = copy_counters_to_user(t, oldcounters, compat_ptr(tmp.counters),
  1642. tmp.num_counters, tinfo.nentries);
  1643. if (ret)
  1644. return ret;
  1645. pos = compat_ptr(tmp.entries);
  1646. return EBT_ENTRY_ITERATE(tinfo.entries, tinfo.entries_size,
  1647. compat_copy_entry_to_user, &pos, &tmp.entries_size);
  1648. }
  1649. struct ebt_entries_buf_state {
  1650. char *buf_kern_start; /* kernel buffer to copy (translated) data to */
  1651. u32 buf_kern_len; /* total size of kernel buffer */
  1652. u32 buf_kern_offset; /* amount of data copied so far */
  1653. u32 buf_user_offset; /* read position in userspace buffer */
  1654. };
  1655. static int ebt_buf_count(struct ebt_entries_buf_state *state, unsigned int sz)
  1656. {
  1657. state->buf_kern_offset += sz;
  1658. return state->buf_kern_offset >= sz ? 0 : -EINVAL;
  1659. }
  1660. static int ebt_buf_add(struct ebt_entries_buf_state *state,
  1661. void *data, unsigned int sz)
  1662. {
  1663. if (state->buf_kern_start == NULL)
  1664. goto count_only;
  1665. if (WARN_ON(state->buf_kern_offset + sz > state->buf_kern_len))
  1666. return -EINVAL;
  1667. memcpy(state->buf_kern_start + state->buf_kern_offset, data, sz);
  1668. count_only:
  1669. state->buf_user_offset += sz;
  1670. return ebt_buf_count(state, sz);
  1671. }
  1672. static int ebt_buf_add_pad(struct ebt_entries_buf_state *state, unsigned int sz)
  1673. {
  1674. char *b = state->buf_kern_start;
  1675. if (WARN_ON(b && state->buf_kern_offset > state->buf_kern_len))
  1676. return -EINVAL;
  1677. if (b != NULL && sz > 0)
  1678. memset(b + state->buf_kern_offset, 0, sz);
  1679. /* do not adjust ->buf_user_offset here, we added kernel-side padding */
  1680. return ebt_buf_count(state, sz);
  1681. }
  1682. enum compat_mwt {
  1683. EBT_COMPAT_MATCH,
  1684. EBT_COMPAT_WATCHER,
  1685. EBT_COMPAT_TARGET,
  1686. };
  1687. static int compat_mtw_from_user(struct compat_ebt_entry_mwt *mwt,
  1688. enum compat_mwt compat_mwt,
  1689. struct ebt_entries_buf_state *state,
  1690. const unsigned char *base)
  1691. {
  1692. char name[EBT_FUNCTION_MAXNAMELEN];
  1693. struct xt_match *match;
  1694. struct xt_target *wt;
  1695. void *dst = NULL;
  1696. int off, pad = 0;
  1697. unsigned int size_kern, match_size = mwt->match_size;
  1698. if (strscpy(name, mwt->u.name, sizeof(name)) < 0)
  1699. return -EINVAL;
  1700. if (state->buf_kern_start)
  1701. dst = state->buf_kern_start + state->buf_kern_offset;
  1702. switch (compat_mwt) {
  1703. case EBT_COMPAT_MATCH:
  1704. match = xt_request_find_match(NFPROTO_BRIDGE, name, 0);
  1705. if (IS_ERR(match))
  1706. return PTR_ERR(match);
  1707. off = ebt_compat_match_offset(match, match_size);
  1708. if (dst) {
  1709. if (match->compat_from_user)
  1710. match->compat_from_user(dst, mwt->data);
  1711. else
  1712. memcpy(dst, mwt->data, match_size);
  1713. }
  1714. size_kern = match->matchsize;
  1715. if (unlikely(size_kern == -1))
  1716. size_kern = match_size;
  1717. module_put(match->me);
  1718. break;
  1719. case EBT_COMPAT_WATCHER: /* fallthrough */
  1720. case EBT_COMPAT_TARGET:
  1721. wt = xt_request_find_target(NFPROTO_BRIDGE, name, 0);
  1722. if (IS_ERR(wt))
  1723. return PTR_ERR(wt);
  1724. off = xt_compat_target_offset(wt);
  1725. if (dst) {
  1726. if (wt->compat_from_user)
  1727. wt->compat_from_user(dst, mwt->data);
  1728. else
  1729. memcpy(dst, mwt->data, match_size);
  1730. }
  1731. size_kern = wt->targetsize;
  1732. module_put(wt->me);
  1733. break;
  1734. default:
  1735. return -EINVAL;
  1736. }
  1737. state->buf_kern_offset += match_size + off;
  1738. state->buf_user_offset += match_size;
  1739. pad = XT_ALIGN(size_kern) - size_kern;
  1740. if (pad > 0 && dst) {
  1741. if (WARN_ON(state->buf_kern_len <= pad))
  1742. return -EINVAL;
  1743. if (WARN_ON(state->buf_kern_offset - (match_size + off) + size_kern > state->buf_kern_len - pad))
  1744. return -EINVAL;
  1745. memset(dst + size_kern, 0, pad);
  1746. }
  1747. return off + match_size;
  1748. }
  1749. /* return size of all matches, watchers or target, including necessary
  1750. * alignment and padding.
  1751. */
  1752. static int ebt_size_mwt(struct compat_ebt_entry_mwt *match32,
  1753. unsigned int size_left, enum compat_mwt type,
  1754. struct ebt_entries_buf_state *state, const void *base)
  1755. {
  1756. int growth = 0;
  1757. char *buf;
  1758. if (size_left == 0)
  1759. return 0;
  1760. buf = (char *) match32;
  1761. while (size_left >= sizeof(*match32)) {
  1762. struct ebt_entry_match *match_kern;
  1763. int ret;
  1764. match_kern = (struct ebt_entry_match *) state->buf_kern_start;
  1765. if (match_kern) {
  1766. char *tmp;
  1767. tmp = state->buf_kern_start + state->buf_kern_offset;
  1768. match_kern = (struct ebt_entry_match *) tmp;
  1769. }
  1770. ret = ebt_buf_add(state, buf, sizeof(*match32));
  1771. if (ret < 0)
  1772. return ret;
  1773. size_left -= sizeof(*match32);
  1774. /* add padding before match->data (if any) */
  1775. ret = ebt_buf_add_pad(state, ebt_compat_entry_padsize());
  1776. if (ret < 0)
  1777. return ret;
  1778. if (match32->match_size > size_left)
  1779. return -EINVAL;
  1780. size_left -= match32->match_size;
  1781. ret = compat_mtw_from_user(match32, type, state, base);
  1782. if (ret < 0)
  1783. return ret;
  1784. if (WARN_ON(ret < match32->match_size))
  1785. return -EINVAL;
  1786. growth += ret - match32->match_size;
  1787. growth += ebt_compat_entry_padsize();
  1788. buf += sizeof(*match32);
  1789. buf += match32->match_size;
  1790. if (match_kern)
  1791. match_kern->match_size = ret;
  1792. if (WARN_ON(type == EBT_COMPAT_TARGET && size_left))
  1793. return -EINVAL;
  1794. match32 = (struct compat_ebt_entry_mwt *) buf;
  1795. }
  1796. return growth;
  1797. }
  1798. /* called for all ebt_entry structures. */
  1799. static int size_entry_mwt(struct ebt_entry *entry, const unsigned char *base,
  1800. unsigned int *total,
  1801. struct ebt_entries_buf_state *state)
  1802. {
  1803. unsigned int i, j, startoff, new_offset = 0;
  1804. /* stores match/watchers/targets & offset of next struct ebt_entry: */
  1805. unsigned int offsets[4];
  1806. unsigned int *offsets_update = NULL;
  1807. int ret;
  1808. char *buf_start;
  1809. if (*total < sizeof(struct ebt_entries))
  1810. return -EINVAL;
  1811. if (!entry->bitmask) {
  1812. *total -= sizeof(struct ebt_entries);
  1813. return ebt_buf_add(state, entry, sizeof(struct ebt_entries));
  1814. }
  1815. if (*total < sizeof(*entry) || entry->next_offset < sizeof(*entry))
  1816. return -EINVAL;
  1817. startoff = state->buf_user_offset;
  1818. /* pull in most part of ebt_entry, it does not need to be changed. */
  1819. ret = ebt_buf_add(state, entry,
  1820. offsetof(struct ebt_entry, watchers_offset));
  1821. if (ret < 0)
  1822. return ret;
  1823. offsets[0] = sizeof(struct ebt_entry); /* matches come first */
  1824. memcpy(&offsets[1], &entry->watchers_offset,
  1825. sizeof(offsets) - sizeof(offsets[0]));
  1826. if (state->buf_kern_start) {
  1827. buf_start = state->buf_kern_start + state->buf_kern_offset;
  1828. offsets_update = (unsigned int *) buf_start;
  1829. }
  1830. ret = ebt_buf_add(state, &offsets[1],
  1831. sizeof(offsets) - sizeof(offsets[0]));
  1832. if (ret < 0)
  1833. return ret;
  1834. buf_start = (char *) entry;
  1835. /* 0: matches offset, always follows ebt_entry.
  1836. * 1: watchers offset, from ebt_entry structure
  1837. * 2: target offset, from ebt_entry structure
  1838. * 3: next ebt_entry offset, from ebt_entry structure
  1839. *
  1840. * offsets are relative to beginning of struct ebt_entry (i.e., 0).
  1841. */
  1842. for (i = 0; i < 4 ; ++i) {
  1843. if (offsets[i] > *total)
  1844. return -EINVAL;
  1845. if (i < 3 && offsets[i] == *total)
  1846. return -EINVAL;
  1847. if (i == 0)
  1848. continue;
  1849. if (offsets[i-1] > offsets[i])
  1850. return -EINVAL;
  1851. }
  1852. for (i = 0, j = 1 ; j < 4 ; j++, i++) {
  1853. struct compat_ebt_entry_mwt *match32;
  1854. unsigned int size;
  1855. char *buf = buf_start;
  1856. buf = buf_start + offsets[i];
  1857. if (offsets[i] > offsets[j])
  1858. return -EINVAL;
  1859. match32 = (struct compat_ebt_entry_mwt *) buf;
  1860. size = offsets[j] - offsets[i];
  1861. ret = ebt_size_mwt(match32, size, i, state, base);
  1862. if (ret < 0)
  1863. return ret;
  1864. new_offset += ret;
  1865. if (offsets_update && new_offset) {
  1866. pr_debug("change offset %d to %d\n",
  1867. offsets_update[i], offsets[j] + new_offset);
  1868. offsets_update[i] = offsets[j] + new_offset;
  1869. }
  1870. }
  1871. if (state->buf_kern_start == NULL) {
  1872. unsigned int offset = buf_start - (char *) base;
  1873. ret = xt_compat_add_offset(NFPROTO_BRIDGE, offset, new_offset);
  1874. if (ret < 0)
  1875. return ret;
  1876. }
  1877. startoff = state->buf_user_offset - startoff;
  1878. if (WARN_ON(*total < startoff))
  1879. return -EINVAL;
  1880. *total -= startoff;
  1881. return 0;
  1882. }
  1883. /* repl->entries_size is the size of the ebt_entry blob in userspace.
  1884. * It might need more memory when copied to a 64 bit kernel in case
  1885. * userspace is 32-bit. So, first task: find out how much memory is needed.
  1886. *
  1887. * Called before validation is performed.
  1888. */
  1889. static int compat_copy_entries(unsigned char *data, unsigned int size_user,
  1890. struct ebt_entries_buf_state *state)
  1891. {
  1892. unsigned int size_remaining = size_user;
  1893. int ret;
  1894. ret = EBT_ENTRY_ITERATE(data, size_user, size_entry_mwt, data,
  1895. &size_remaining, state);
  1896. if (ret < 0)
  1897. return ret;
  1898. WARN_ON(size_remaining);
  1899. return state->buf_kern_offset;
  1900. }
  1901. static int compat_copy_ebt_replace_from_user(struct ebt_replace *repl,
  1902. void __user *user, unsigned int len)
  1903. {
  1904. struct compat_ebt_replace tmp;
  1905. int i;
  1906. if (len < sizeof(tmp))
  1907. return -EINVAL;
  1908. if (copy_from_user(&tmp, user, sizeof(tmp)))
  1909. return -EFAULT;
  1910. if (len != sizeof(tmp) + tmp.entries_size)
  1911. return -EINVAL;
  1912. if (tmp.entries_size == 0)
  1913. return -EINVAL;
  1914. if (tmp.nentries >= ((INT_MAX - sizeof(struct ebt_table_info)) /
  1915. NR_CPUS - SMP_CACHE_BYTES) / sizeof(struct ebt_counter))
  1916. return -ENOMEM;
  1917. if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter))
  1918. return -ENOMEM;
  1919. memcpy(repl, &tmp, offsetof(struct ebt_replace, hook_entry));
  1920. /* starting with hook_entry, 32 vs. 64 bit structures are different */
  1921. for (i = 0; i < NF_BR_NUMHOOKS; i++)
  1922. repl->hook_entry[i] = compat_ptr(tmp.hook_entry[i]);
  1923. repl->num_counters = tmp.num_counters;
  1924. repl->counters = compat_ptr(tmp.counters);
  1925. repl->entries = compat_ptr(tmp.entries);
  1926. return 0;
  1927. }
  1928. static int compat_do_replace(struct net *net, void __user *user,
  1929. unsigned int len)
  1930. {
  1931. int ret, i, countersize, size64;
  1932. struct ebt_table_info *newinfo;
  1933. struct ebt_replace tmp;
  1934. struct ebt_entries_buf_state state;
  1935. void *entries_tmp;
  1936. ret = compat_copy_ebt_replace_from_user(&tmp, user, len);
  1937. if (ret) {
  1938. /* try real handler in case userland supplied needed padding */
  1939. if (ret == -EINVAL && do_replace(net, user, len) == 0)
  1940. ret = 0;
  1941. return ret;
  1942. }
  1943. countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids;
  1944. newinfo = vmalloc(sizeof(*newinfo) + countersize);
  1945. if (!newinfo)
  1946. return -ENOMEM;
  1947. if (countersize)
  1948. memset(newinfo->counters, 0, countersize);
  1949. memset(&state, 0, sizeof(state));
  1950. newinfo->entries = vmalloc(tmp.entries_size);
  1951. if (!newinfo->entries) {
  1952. ret = -ENOMEM;
  1953. goto free_newinfo;
  1954. }
  1955. if (copy_from_user(
  1956. newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
  1957. ret = -EFAULT;
  1958. goto free_entries;
  1959. }
  1960. entries_tmp = newinfo->entries;
  1961. xt_compat_lock(NFPROTO_BRIDGE);
  1962. xt_compat_init_offsets(NFPROTO_BRIDGE, tmp.nentries);
  1963. ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
  1964. if (ret < 0)
  1965. goto out_unlock;
  1966. pr_debug("tmp.entries_size %d, kern off %d, user off %d delta %d\n",
  1967. tmp.entries_size, state.buf_kern_offset, state.buf_user_offset,
  1968. xt_compat_calc_jump(NFPROTO_BRIDGE, tmp.entries_size));
  1969. size64 = ret;
  1970. newinfo->entries = vmalloc(size64);
  1971. if (!newinfo->entries) {
  1972. vfree(entries_tmp);
  1973. ret = -ENOMEM;
  1974. goto out_unlock;
  1975. }
  1976. memset(&state, 0, sizeof(state));
  1977. state.buf_kern_start = newinfo->entries;
  1978. state.buf_kern_len = size64;
  1979. ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
  1980. if (WARN_ON(ret < 0))
  1981. goto out_unlock;
  1982. vfree(entries_tmp);
  1983. tmp.entries_size = size64;
  1984. for (i = 0; i < NF_BR_NUMHOOKS; i++) {
  1985. char __user *usrptr;
  1986. if (tmp.hook_entry[i]) {
  1987. unsigned int delta;
  1988. usrptr = (char __user *) tmp.hook_entry[i];
  1989. delta = usrptr - tmp.entries;
  1990. usrptr += xt_compat_calc_jump(NFPROTO_BRIDGE, delta);
  1991. tmp.hook_entry[i] = (struct ebt_entries __user *)usrptr;
  1992. }
  1993. }
  1994. xt_compat_flush_offsets(NFPROTO_BRIDGE);
  1995. xt_compat_unlock(NFPROTO_BRIDGE);
  1996. ret = do_replace_finish(net, &tmp, newinfo);
  1997. if (ret == 0)
  1998. return ret;
  1999. free_entries:
  2000. vfree(newinfo->entries);
  2001. free_newinfo:
  2002. vfree(newinfo);
  2003. return ret;
  2004. out_unlock:
  2005. xt_compat_flush_offsets(NFPROTO_BRIDGE);
  2006. xt_compat_unlock(NFPROTO_BRIDGE);
  2007. goto free_entries;
  2008. }
  2009. static int compat_update_counters(struct net *net, void __user *user,
  2010. unsigned int len)
  2011. {
  2012. struct compat_ebt_replace hlp;
  2013. if (copy_from_user(&hlp, user, sizeof(hlp)))
  2014. return -EFAULT;
  2015. /* try real handler in case userland supplied needed padding */
  2016. if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
  2017. return update_counters(net, user, len);
  2018. return do_update_counters(net, hlp.name, compat_ptr(hlp.counters),
  2019. hlp.num_counters, user, len);
  2020. }
  2021. static int compat_do_ebt_set_ctl(struct sock *sk,
  2022. int cmd, void __user *user, unsigned int len)
  2023. {
  2024. int ret;
  2025. struct net *net = sock_net(sk);
  2026. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2027. return -EPERM;
  2028. switch (cmd) {
  2029. case EBT_SO_SET_ENTRIES:
  2030. ret = compat_do_replace(net, user, len);
  2031. break;
  2032. case EBT_SO_SET_COUNTERS:
  2033. ret = compat_update_counters(net, user, len);
  2034. break;
  2035. default:
  2036. ret = -EINVAL;
  2037. }
  2038. return ret;
  2039. }
  2040. static int compat_do_ebt_get_ctl(struct sock *sk, int cmd,
  2041. void __user *user, int *len)
  2042. {
  2043. int ret;
  2044. struct compat_ebt_replace tmp;
  2045. struct ebt_table *t;
  2046. struct net *net = sock_net(sk);
  2047. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2048. return -EPERM;
  2049. /* try real handler in case userland supplied needed padding */
  2050. if ((cmd == EBT_SO_GET_INFO ||
  2051. cmd == EBT_SO_GET_INIT_INFO) && *len != sizeof(tmp))
  2052. return do_ebt_get_ctl(sk, cmd, user, len);
  2053. if (copy_from_user(&tmp, user, sizeof(tmp)))
  2054. return -EFAULT;
  2055. tmp.name[sizeof(tmp.name) - 1] = '\0';
  2056. t = find_table_lock(net, tmp.name, &ret, &ebt_mutex);
  2057. if (!t)
  2058. return ret;
  2059. xt_compat_lock(NFPROTO_BRIDGE);
  2060. switch (cmd) {
  2061. case EBT_SO_GET_INFO:
  2062. tmp.nentries = t->private->nentries;
  2063. ret = compat_table_info(t->private, &tmp);
  2064. if (ret)
  2065. goto out;
  2066. tmp.valid_hooks = t->valid_hooks;
  2067. if (copy_to_user(user, &tmp, *len) != 0) {
  2068. ret = -EFAULT;
  2069. break;
  2070. }
  2071. ret = 0;
  2072. break;
  2073. case EBT_SO_GET_INIT_INFO:
  2074. tmp.nentries = t->table->nentries;
  2075. tmp.entries_size = t->table->entries_size;
  2076. tmp.valid_hooks = t->table->valid_hooks;
  2077. if (copy_to_user(user, &tmp, *len) != 0) {
  2078. ret = -EFAULT;
  2079. break;
  2080. }
  2081. ret = 0;
  2082. break;
  2083. case EBT_SO_GET_ENTRIES:
  2084. case EBT_SO_GET_INIT_ENTRIES:
  2085. /* try real handler first in case of userland-side padding.
  2086. * in case we are dealing with an 'ordinary' 32 bit binary
  2087. * without 64bit compatibility padding, this will fail right
  2088. * after copy_from_user when the *len argument is validated.
  2089. *
  2090. * the compat_ variant needs to do one pass over the kernel
  2091. * data set to adjust for size differences before it the check.
  2092. */
  2093. if (copy_everything_to_user(t, user, len, cmd) == 0)
  2094. ret = 0;
  2095. else
  2096. ret = compat_copy_everything_to_user(t, user, len, cmd);
  2097. break;
  2098. default:
  2099. ret = -EINVAL;
  2100. }
  2101. out:
  2102. xt_compat_flush_offsets(NFPROTO_BRIDGE);
  2103. xt_compat_unlock(NFPROTO_BRIDGE);
  2104. mutex_unlock(&ebt_mutex);
  2105. return ret;
  2106. }
  2107. #endif
  2108. static struct nf_sockopt_ops ebt_sockopts = {
  2109. .pf = PF_INET,
  2110. .set_optmin = EBT_BASE_CTL,
  2111. .set_optmax = EBT_SO_SET_MAX + 1,
  2112. .set = do_ebt_set_ctl,
  2113. #ifdef CONFIG_COMPAT
  2114. .compat_set = compat_do_ebt_set_ctl,
  2115. #endif
  2116. .get_optmin = EBT_BASE_CTL,
  2117. .get_optmax = EBT_SO_GET_MAX + 1,
  2118. .get = do_ebt_get_ctl,
  2119. #ifdef CONFIG_COMPAT
  2120. .compat_get = compat_do_ebt_get_ctl,
  2121. #endif
  2122. .owner = THIS_MODULE,
  2123. };
  2124. static int __init ebtables_init(void)
  2125. {
  2126. int ret;
  2127. ret = xt_register_target(&ebt_standard_target);
  2128. if (ret < 0)
  2129. return ret;
  2130. ret = nf_register_sockopt(&ebt_sockopts);
  2131. if (ret < 0) {
  2132. xt_unregister_target(&ebt_standard_target);
  2133. return ret;
  2134. }
  2135. printk(KERN_INFO "Ebtables v2.0 registered\n");
  2136. return 0;
  2137. }
  2138. static void __exit ebtables_fini(void)
  2139. {
  2140. nf_unregister_sockopt(&ebt_sockopts);
  2141. xt_unregister_target(&ebt_standard_target);
  2142. printk(KERN_INFO "Ebtables v2.0 unregistered\n");
  2143. }
  2144. EXPORT_SYMBOL(ebt_register_table);
  2145. EXPORT_SYMBOL(ebt_unregister_table);
  2146. EXPORT_SYMBOL(ebt_do_table);
  2147. module_init(ebtables_init);
  2148. module_exit(ebtables_fini);
  2149. MODULE_LICENSE("GPL");