libiptc.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756
  1. /* Library which manipulates firewall rules. Version $Revision$ */
  2. /* Architecture of firewall rules is as follows:
  3. *
  4. * Chains go INPUT, FORWARD, OUTPUT then user chains.
  5. * Each user chain starts with an ERROR node.
  6. * Every chain ends with an unconditional jump: a RETURN for user chains,
  7. * and a POLICY for built-ins.
  8. */
  9. /* (C) 1999 Paul ``Rusty'' Russell - Placed under the GNU GPL (See
  10. * COPYING for details).
  11. * (C) 2000-2004 by the Netfilter Core Team <coreteam@netfilter.org>
  12. *
  13. * 2003-Jun-20: Harald Welte <laforge@netfilter.org>:
  14. * - Reimplementation of chain cache to use offsets instead of entries
  15. * 2003-Jun-23: Harald Welte <laforge@netfilter.org>:
  16. * - performance optimization, sponsored by Astaro AG (http://www.astaro.com/)
  17. * don't rebuild the chain cache after every operation, instead fix it
  18. * up after a ruleset change.
  19. * 2004-Aug-18: Harald Welte <laforge@netfilter.org>:
  20. * - further performance work: total reimplementation of libiptc.
  21. * - libiptc now has a real internal (linked-list) represntation of the
  22. * ruleset and a parser/compiler from/to this internal representation
  23. * - again sponsored by Astaro AG (http://www.astaro.com/)
  24. *
  25. * 2008-Jan+Jul: Jesper Dangaard Brouer <hawk@comx.dk>
  26. * - performance work: speedup chain list "name" searching.
  27. * - performance work: speedup initial ruleset parsing.
  28. * - sponsored by ComX Networks A/S (http://www.comx.dk/)
  29. */
  30. #include <unistd.h>
  31. #include <fcntl.h>
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include <stdbool.h>
  35. #include <xtables.h>
  36. #include <libiptc/xtcshared.h>
  37. #include "linux_list.h"
  38. //#define IPTC_DEBUG2 1
  39. #ifdef IPTC_DEBUG2
  40. #include <fcntl.h>
  41. #define DEBUGP(x, args...) fprintf(stderr, "%s: " x, __FUNCTION__, ## args)
  42. #define DEBUGP_C(x, args...) fprintf(stderr, x, ## args)
  43. #else
  44. #define DEBUGP(x, args...)
  45. #define DEBUGP_C(x, args...)
  46. #endif
  47. #ifdef DEBUG
  48. #define debug(x, args...) fprintf(stderr, x, ## args)
  49. #else
  50. #define debug(x, args...)
  51. #endif
  52. static void *iptc_fn = NULL;
  53. static const char *hooknames[] = {
  54. [HOOK_PRE_ROUTING] = "PREROUTING",
  55. [HOOK_LOCAL_IN] = "INPUT",
  56. [HOOK_FORWARD] = "FORWARD",
  57. [HOOK_LOCAL_OUT] = "OUTPUT",
  58. [HOOK_POST_ROUTING] = "POSTROUTING",
  59. };
  60. /* Convenience structures */
  61. struct chain_head;
  62. struct rule_head;
  63. struct counter_map
  64. {
  65. enum {
  66. COUNTER_MAP_NOMAP,
  67. COUNTER_MAP_NORMAL_MAP,
  68. COUNTER_MAP_ZEROED,
  69. COUNTER_MAP_SET
  70. } maptype;
  71. unsigned int mappos;
  72. };
  73. enum iptcc_rule_type {
  74. IPTCC_R_STANDARD, /* standard target (ACCEPT, ...) */
  75. IPTCC_R_MODULE, /* extension module (SNAT, ...) */
  76. IPTCC_R_FALLTHROUGH, /* fallthrough rule */
  77. IPTCC_R_JUMP, /* jump to other chain */
  78. };
  79. struct rule_head
  80. {
  81. struct list_head list;
  82. struct chain_head *chain;
  83. struct counter_map counter_map;
  84. unsigned int index; /* index (needed for counter_map) */
  85. unsigned int offset; /* offset in rule blob */
  86. enum iptcc_rule_type type;
  87. struct chain_head *jump; /* jump target, if IPTCC_R_JUMP */
  88. unsigned int size; /* size of entry data */
  89. STRUCT_ENTRY entry[0];
  90. };
  91. struct chain_head
  92. {
  93. struct list_head list;
  94. char name[TABLE_MAXNAMELEN];
  95. unsigned int hooknum; /* hook number+1 if builtin */
  96. unsigned int references; /* how many jumps reference us */
  97. int verdict; /* verdict if builtin */
  98. STRUCT_COUNTERS counters; /* per-chain counters */
  99. struct counter_map counter_map;
  100. unsigned int num_rules; /* number of rules in list */
  101. struct list_head rules; /* list of rules */
  102. unsigned int index; /* index (needed for jump resolval) */
  103. unsigned int head_offset; /* offset in rule blob */
  104. unsigned int foot_index; /* index (needed for counter_map) */
  105. unsigned int foot_offset; /* offset in rule blob */
  106. };
  107. struct xtc_handle {
  108. int sockfd;
  109. int changed; /* Have changes been made? */
  110. struct list_head chains;
  111. struct chain_head *chain_iterator_cur;
  112. struct rule_head *rule_iterator_cur;
  113. unsigned int num_chains; /* number of user defined chains */
  114. struct chain_head **chain_index; /* array for fast chain list access*/
  115. unsigned int chain_index_sz;/* size of chain index array */
  116. int sorted_offsets; /* if chains are received sorted from kernel,
  117. * then the offsets are also sorted. Says if its
  118. * possible to bsearch offsets using chain_index.
  119. */
  120. STRUCT_GETINFO info;
  121. STRUCT_GET_ENTRIES *entries;
  122. };
  123. enum bsearch_type {
  124. BSEARCH_NAME, /* Binary search after chain name */
  125. BSEARCH_OFFSET, /* Binary search based on offset */
  126. };
  127. /* allocate a new chain head for the cache */
  128. static struct chain_head *iptcc_alloc_chain_head(const char *name, int hooknum)
  129. {
  130. struct chain_head *c = malloc(sizeof(*c));
  131. if (!c)
  132. return NULL;
  133. memset(c, 0, sizeof(*c));
  134. strncpy(c->name, name, TABLE_MAXNAMELEN);
  135. c->hooknum = hooknum;
  136. INIT_LIST_HEAD(&c->rules);
  137. return c;
  138. }
  139. /* allocate and initialize a new rule for the cache */
  140. static struct rule_head *iptcc_alloc_rule(struct chain_head *c, unsigned int size)
  141. {
  142. struct rule_head *r = malloc(sizeof(*r)+size);
  143. if (!r)
  144. return NULL;
  145. memset(r, 0, sizeof(*r));
  146. r->chain = c;
  147. r->size = size;
  148. return r;
  149. }
  150. /* notify us that the ruleset has been modified by the user */
  151. static inline void
  152. set_changed(struct xtc_handle *h)
  153. {
  154. h->changed = 1;
  155. }
  156. #ifdef IPTC_DEBUG
  157. static void do_check(struct xtc_handle *h, unsigned int line);
  158. #define CHECK(h) do { if (!getenv("IPTC_NO_CHECK")) do_check((h), __LINE__); } while(0)
  159. #else
  160. #define CHECK(h)
  161. #endif
  162. /**********************************************************************
  163. * iptc blob utility functions (iptcb_*)
  164. **********************************************************************/
  165. static inline int
  166. iptcb_get_number(const STRUCT_ENTRY *i,
  167. const STRUCT_ENTRY *seek,
  168. unsigned int *pos)
  169. {
  170. if (i == seek)
  171. return 1;
  172. (*pos)++;
  173. return 0;
  174. }
  175. static inline int
  176. iptcb_get_entry_n(STRUCT_ENTRY *i,
  177. unsigned int number,
  178. unsigned int *pos,
  179. STRUCT_ENTRY **pe)
  180. {
  181. if (*pos == number) {
  182. *pe = i;
  183. return 1;
  184. }
  185. (*pos)++;
  186. return 0;
  187. }
  188. static inline STRUCT_ENTRY *
  189. iptcb_get_entry(struct xtc_handle *h, unsigned int offset)
  190. {
  191. return (STRUCT_ENTRY *)((char *)h->entries->entrytable + offset);
  192. }
  193. static unsigned int
  194. iptcb_entry2index(struct xtc_handle *const h, const STRUCT_ENTRY *seek)
  195. {
  196. unsigned int pos = 0;
  197. if (ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
  198. iptcb_get_number, seek, &pos) == 0) {
  199. fprintf(stderr, "ERROR: offset %u not an entry!\n",
  200. (unsigned int)((char *)seek - (char *)h->entries->entrytable));
  201. abort();
  202. }
  203. return pos;
  204. }
  205. static inline STRUCT_ENTRY *
  206. iptcb_offset2entry(struct xtc_handle *h, unsigned int offset)
  207. {
  208. return (STRUCT_ENTRY *) ((void *)h->entries->entrytable+offset);
  209. }
  210. static inline unsigned long
  211. iptcb_entry2offset(struct xtc_handle *const h, const STRUCT_ENTRY *e)
  212. {
  213. return (void *)e - (void *)h->entries->entrytable;
  214. }
  215. static inline unsigned int
  216. iptcb_offset2index(struct xtc_handle *const h, unsigned int offset)
  217. {
  218. return iptcb_entry2index(h, iptcb_offset2entry(h, offset));
  219. }
  220. /* Returns 0 if not hook entry, else hooknumber + 1 */
  221. static inline unsigned int
  222. iptcb_ent_is_hook_entry(STRUCT_ENTRY *e, struct xtc_handle *h)
  223. {
  224. unsigned int i;
  225. for (i = 0; i < NUMHOOKS; i++) {
  226. if ((h->info.valid_hooks & (1 << i))
  227. && iptcb_get_entry(h, h->info.hook_entry[i]) == e)
  228. return i+1;
  229. }
  230. return 0;
  231. }
  232. /**********************************************************************
  233. * Chain index (cache utility) functions
  234. **********************************************************************
  235. * The chain index is an array with pointers into the chain list, with
  236. * CHAIN_INDEX_BUCKET_LEN spacing. This facilitates the ability to
  237. * speedup chain list searching, by find a more optimal starting
  238. * points when searching the linked list.
  239. *
  240. * The starting point can be found fast by using a binary search of
  241. * the chain index. Thus, reducing the previous search complexity of
  242. * O(n) to O(log(n/k) + k) where k is CHAIN_INDEX_BUCKET_LEN.
  243. *
  244. * A nice property of the chain index, is that the "bucket" list
  245. * length is max CHAIN_INDEX_BUCKET_LEN (when just build, inserts will
  246. * change this). Oppose to hashing, where the "bucket" list length can
  247. * vary a lot.
  248. */
  249. #ifndef CHAIN_INDEX_BUCKET_LEN
  250. #define CHAIN_INDEX_BUCKET_LEN 40
  251. #endif
  252. /* Another nice property of the chain index is that inserting/creating
  253. * chains in chain list don't change the correctness of the chain
  254. * index, it only causes longer lists in the buckets.
  255. *
  256. * To mitigate the performance penalty of longer bucket lists and the
  257. * penalty of rebuilding, the chain index is rebuild only when
  258. * CHAIN_INDEX_INSERT_MAX chains has been added.
  259. */
  260. #ifndef CHAIN_INDEX_INSERT_MAX
  261. #define CHAIN_INDEX_INSERT_MAX 355
  262. #endif
  263. static inline unsigned int iptcc_is_builtin(struct chain_head *c);
  264. /* Use binary search in the chain index array, to find a chain_head
  265. * pointer closest to the place of the searched name element.
  266. *
  267. * Notes that, binary search (obviously) requires that the chain list
  268. * is sorted by name.
  269. *
  270. * The not so obvious: The chain index array, is actually both sorted
  271. * by name and offset, at the same time!. This is only true because,
  272. * chain are stored sorted in the kernel (as we pushed it in sorted).
  273. *
  274. */
  275. static struct list_head *
  276. __iptcc_bsearch_chain_index(const char *name, unsigned int offset,
  277. unsigned int *idx, struct xtc_handle *handle,
  278. enum bsearch_type type)
  279. {
  280. unsigned int pos, end;
  281. int res;
  282. struct list_head *list_pos;
  283. list_pos=&handle->chains;
  284. /* Check for empty array, e.g. no user defined chains */
  285. if (handle->chain_index_sz == 0) {
  286. debug("WARNING: handle->chain_index_sz == 0\n");
  287. return list_pos;
  288. }
  289. /* Init */
  290. end = handle->chain_index_sz;
  291. pos = end / 2;
  292. debug("bsearch Find chain:%s (pos:%d end:%d) (offset:%d)\n",
  293. name, pos, end, offset);
  294. /* Loop */
  295. loop:
  296. if (!handle->chain_index[pos]) {
  297. fprintf(stderr, "ERROR: NULL pointer chain_index[%d]\n", pos);
  298. return &handle->chains; /* Be safe, return orig start pos */
  299. }
  300. debug("bsearch Index[%d] name:%s ",
  301. pos, handle->chain_index[pos]->name);
  302. /* Support for different compare functions */
  303. switch (type) {
  304. case BSEARCH_NAME:
  305. res = strcmp(name, handle->chain_index[pos]->name);
  306. break;
  307. case BSEARCH_OFFSET:
  308. debug("head_offset:[%d] foot_offset:[%d] ",
  309. handle->chain_index[pos]->head_offset,
  310. handle->chain_index[pos]->foot_offset);
  311. res = offset - handle->chain_index[pos]->head_offset;
  312. break;
  313. default:
  314. fprintf(stderr, "ERROR: %d not a valid bsearch type\n",
  315. type);
  316. abort();
  317. break;
  318. }
  319. debug("res:%d ", res);
  320. list_pos = &handle->chain_index[pos]->list;
  321. *idx = pos;
  322. if (res == 0) { /* Found element, by direct hit */
  323. debug("[found] Direct hit pos:%d end:%d\n", pos, end);
  324. return list_pos;
  325. } else if (res < 0) { /* Too far, jump back */
  326. end = pos;
  327. pos = pos / 2;
  328. /* Exit case: First element of array */
  329. if (end == 0) {
  330. debug("[found] Reached first array elem (end%d)\n",end);
  331. return list_pos;
  332. }
  333. debug("jump back to pos:%d (end:%d)\n", pos, end);
  334. goto loop;
  335. } else { /* res > 0; Not far enough, jump forward */
  336. /* Exit case: Last element of array */
  337. if (pos == handle->chain_index_sz-1) {
  338. debug("[found] Last array elem (end:%d)\n", end);
  339. return list_pos;
  340. }
  341. /* Exit case: Next index less, thus elem in this list section */
  342. switch (type) {
  343. case BSEARCH_NAME:
  344. res = strcmp(name, handle->chain_index[pos+1]->name);
  345. break;
  346. case BSEARCH_OFFSET:
  347. res = offset - handle->chain_index[pos+1]->head_offset;
  348. break;
  349. }
  350. if (res < 0) {
  351. debug("[found] closest list (end:%d)\n", end);
  352. return list_pos;
  353. }
  354. pos = (pos+end)/2;
  355. debug("jump forward to pos:%d (end:%d)\n", pos, end);
  356. goto loop;
  357. }
  358. }
  359. /* Wrapper for string chain name based bsearch */
  360. static struct list_head *
  361. iptcc_bsearch_chain_index(const char *name, unsigned int *idx,
  362. struct xtc_handle *handle)
  363. {
  364. return __iptcc_bsearch_chain_index(name, 0, idx, handle, BSEARCH_NAME);
  365. }
  366. /* Wrapper for offset chain based bsearch */
  367. static struct list_head *
  368. iptcc_bsearch_chain_offset(unsigned int offset, unsigned int *idx,
  369. struct xtc_handle *handle)
  370. {
  371. struct list_head *pos;
  372. /* If chains were not received sorted from kernel, then the
  373. * offset bsearch is not possible.
  374. */
  375. if (!handle->sorted_offsets)
  376. pos = handle->chains.next;
  377. else
  378. pos = __iptcc_bsearch_chain_index(NULL, offset, idx, handle,
  379. BSEARCH_OFFSET);
  380. return pos;
  381. }
  382. #ifdef DEBUG
  383. /* Trivial linear search of chain index. Function used for verifying
  384. the output of bsearch function */
  385. static struct list_head *
  386. iptcc_linearly_search_chain_index(const char *name, struct xtc_handle *handle)
  387. {
  388. unsigned int i=0;
  389. int res=0;
  390. struct list_head *list_pos;
  391. list_pos = &handle->chains;
  392. if (handle->chain_index_sz)
  393. list_pos = &handle->chain_index[0]->list;
  394. /* Linearly walk of chain index array */
  395. for (i=0; i < handle->chain_index_sz; i++) {
  396. if (handle->chain_index[i]) {
  397. res = strcmp(handle->chain_index[i]->name, name);
  398. if (res > 0)
  399. break; // One step too far
  400. list_pos = &handle->chain_index[i]->list;
  401. if (res == 0)
  402. break; // Direct hit
  403. }
  404. }
  405. return list_pos;
  406. }
  407. #endif
  408. static int iptcc_chain_index_alloc(struct xtc_handle *h)
  409. {
  410. unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
  411. unsigned int array_elems;
  412. unsigned int array_mem;
  413. /* Allocate memory for the chain index array */
  414. array_elems = (h->num_chains / list_length) +
  415. (h->num_chains % list_length ? 1 : 0);
  416. array_mem = sizeof(h->chain_index) * array_elems;
  417. debug("Alloc Chain index, elems:%d mem:%d bytes\n",
  418. array_elems, array_mem);
  419. h->chain_index = malloc(array_mem);
  420. if (h->chain_index == NULL && array_mem > 0) {
  421. h->chain_index_sz = 0;
  422. return -ENOMEM;
  423. }
  424. memset(h->chain_index, 0, array_mem);
  425. h->chain_index_sz = array_elems;
  426. return 1;
  427. }
  428. static void iptcc_chain_index_free(struct xtc_handle *h)
  429. {
  430. h->chain_index_sz = 0;
  431. free(h->chain_index);
  432. }
  433. #ifdef DEBUG
  434. static void iptcc_chain_index_dump(struct xtc_handle *h)
  435. {
  436. unsigned int i = 0;
  437. /* Dump: contents of chain index array */
  438. for (i=0; i < h->chain_index_sz; i++) {
  439. if (h->chain_index[i]) {
  440. fprintf(stderr, "Chain index[%d].name: %s\n",
  441. i, h->chain_index[i]->name);
  442. }
  443. }
  444. }
  445. #endif
  446. /* Build the chain index */
  447. static int iptcc_chain_index_build(struct xtc_handle *h)
  448. {
  449. unsigned int list_length = CHAIN_INDEX_BUCKET_LEN;
  450. unsigned int chains = 0;
  451. unsigned int cindex = 0;
  452. struct chain_head *c;
  453. /* Build up the chain index array here */
  454. debug("Building chain index\n");
  455. debug("Number of user defined chains:%d bucket_sz:%d array_sz:%d\n",
  456. h->num_chains, list_length, h->chain_index_sz);
  457. if (h->chain_index_sz == 0)
  458. return 0;
  459. list_for_each_entry(c, &h->chains, list) {
  460. /* Issue: The index array needs to start after the
  461. * builtin chains, as they are not sorted */
  462. if (!iptcc_is_builtin(c)) {
  463. cindex=chains / list_length;
  464. /* Safe guard, break out on array limit, this
  465. * is useful if chains are added and array is
  466. * rebuild, without realloc of memory. */
  467. if (cindex >= h->chain_index_sz)
  468. break;
  469. if ((chains % list_length)== 0) {
  470. debug("\nIndex[%d] Chains:", cindex);
  471. h->chain_index[cindex] = c;
  472. }
  473. chains++;
  474. }
  475. debug("%s, ", c->name);
  476. }
  477. debug("\n");
  478. return 1;
  479. }
  480. static int iptcc_chain_index_rebuild(struct xtc_handle *h)
  481. {
  482. debug("REBUILD chain index array\n");
  483. iptcc_chain_index_free(h);
  484. if ((iptcc_chain_index_alloc(h)) < 0)
  485. return -ENOMEM;
  486. iptcc_chain_index_build(h);
  487. return 1;
  488. }
  489. /* Delete chain (pointer) from index array. Removing an element from
  490. * the chain list only affects the chain index array, if the chain
  491. * index points-to/uses that list pointer.
  492. *
  493. * There are different strategies, the simple and safe is to rebuild
  494. * the chain index every time. The more advanced is to update the
  495. * array index to point to the next element, but that requires some
  496. * house keeping and boundry checks. The advanced is implemented, as
  497. * the simple approach behaves badly when all chains are deleted
  498. * because list_for_each processing will always hit the first chain
  499. * index, thus causing a rebuild for every chain.
  500. */
  501. static int iptcc_chain_index_delete_chain(struct chain_head *c, struct xtc_handle *h)
  502. {
  503. struct list_head *index_ptr, *next;
  504. struct chain_head *c2;
  505. unsigned int idx, idx2;
  506. index_ptr = iptcc_bsearch_chain_index(c->name, &idx, h);
  507. debug("Del chain[%s] c->list:%p index_ptr:%p\n",
  508. c->name, &c->list, index_ptr);
  509. /* Save the next pointer */
  510. next = c->list.next;
  511. list_del(&c->list);
  512. if (index_ptr == &c->list) { /* Chain used as index ptr */
  513. /* See if its possible to avoid a rebuild, by shifting
  514. * to next pointer. Its possible if the next pointer
  515. * is located in the same index bucket.
  516. */
  517. c2 = list_entry(next, struct chain_head, list);
  518. iptcc_bsearch_chain_index(c2->name, &idx2, h);
  519. if (idx != idx2) {
  520. /* Rebuild needed */
  521. return iptcc_chain_index_rebuild(h);
  522. } else {
  523. /* Avoiding rebuild */
  524. debug("Update cindex[%d] with next ptr name:[%s]\n",
  525. idx, c2->name);
  526. h->chain_index[idx]=c2;
  527. return 0;
  528. }
  529. }
  530. return 0;
  531. }
  532. /**********************************************************************
  533. * iptc cache utility functions (iptcc_*)
  534. **********************************************************************/
  535. /* Is the given chain builtin (1) or user-defined (0) */
  536. static inline unsigned int iptcc_is_builtin(struct chain_head *c)
  537. {
  538. return (c->hooknum ? 1 : 0);
  539. }
  540. /* Get a specific rule within a chain */
  541. static struct rule_head *iptcc_get_rule_num(struct chain_head *c,
  542. unsigned int rulenum)
  543. {
  544. struct rule_head *r;
  545. unsigned int num = 0;
  546. list_for_each_entry(r, &c->rules, list) {
  547. num++;
  548. if (num == rulenum)
  549. return r;
  550. }
  551. return NULL;
  552. }
  553. /* Get a specific rule within a chain backwards */
  554. static struct rule_head *iptcc_get_rule_num_reverse(struct chain_head *c,
  555. unsigned int rulenum)
  556. {
  557. struct rule_head *r;
  558. unsigned int num = 0;
  559. list_for_each_entry_reverse(r, &c->rules, list) {
  560. num++;
  561. if (num == rulenum)
  562. return r;
  563. }
  564. return NULL;
  565. }
  566. /* Returns chain head if found, otherwise NULL. */
  567. static struct chain_head *
  568. iptcc_find_chain_by_offset(struct xtc_handle *handle, unsigned int offset)
  569. {
  570. struct list_head *pos;
  571. struct list_head *list_start_pos;
  572. unsigned int i;
  573. if (list_empty(&handle->chains))
  574. return NULL;
  575. /* Find a smart place to start the search */
  576. list_start_pos = iptcc_bsearch_chain_offset(offset, &i, handle);
  577. /* Note that iptcc_bsearch_chain_offset() skips builtin
  578. * chains, but this function is only used for finding jump
  579. * targets, and a buildin chain is not a valid jump target */
  580. debug("Offset:[%u] starting search at index:[%u]\n", offset, i);
  581. // list_for_each(pos, &handle->chains) {
  582. list_for_each(pos, list_start_pos->prev) {
  583. struct chain_head *c = list_entry(pos, struct chain_head, list);
  584. debug(".");
  585. if (offset >= c->head_offset && offset <= c->foot_offset) {
  586. debug("Offset search found chain:[%s]\n", c->name);
  587. return c;
  588. }
  589. }
  590. return NULL;
  591. }
  592. /* Returns chain head if found, otherwise NULL. */
  593. static struct chain_head *
  594. iptcc_find_label(const char *name, struct xtc_handle *handle)
  595. {
  596. struct list_head *pos;
  597. struct list_head *list_start_pos;
  598. unsigned int i=0;
  599. int res;
  600. if (list_empty(&handle->chains))
  601. return NULL;
  602. /* First look at builtin chains */
  603. list_for_each(pos, &handle->chains) {
  604. struct chain_head *c = list_entry(pos, struct chain_head, list);
  605. if (!iptcc_is_builtin(c))
  606. break;
  607. if (!strcmp(c->name, name))
  608. return c;
  609. }
  610. /* Find a smart place to start the search via chain index */
  611. //list_start_pos = iptcc_linearly_search_chain_index(name, handle);
  612. list_start_pos = iptcc_bsearch_chain_index(name, &i, handle);
  613. /* Handel if bsearch bails out early */
  614. if (list_start_pos == &handle->chains) {
  615. list_start_pos = pos;
  616. }
  617. #ifdef DEBUG
  618. else {
  619. /* Verify result of bsearch against linearly index search */
  620. struct list_head *test_pos;
  621. struct chain_head *test_c, *tmp_c;
  622. test_pos = iptcc_linearly_search_chain_index(name, handle);
  623. if (list_start_pos != test_pos) {
  624. debug("BUG in chain_index search\n");
  625. test_c=list_entry(test_pos, struct chain_head,list);
  626. tmp_c =list_entry(list_start_pos,struct chain_head,list);
  627. debug("Verify search found:\n");
  628. debug(" Chain:%s\n", test_c->name);
  629. debug("BSearch found:\n");
  630. debug(" Chain:%s\n", tmp_c->name);
  631. exit(42);
  632. }
  633. }
  634. #endif
  635. /* Initial/special case, no user defined chains */
  636. if (handle->num_chains == 0)
  637. return NULL;
  638. /* Start searching through the chain list */
  639. list_for_each(pos, list_start_pos->prev) {
  640. struct chain_head *c = list_entry(pos, struct chain_head, list);
  641. res = strcmp(c->name, name);
  642. debug("List search name:%s == %s res:%d\n", name, c->name, res);
  643. if (res==0)
  644. return c;
  645. /* We can stop earlier as we know list is sorted */
  646. if (res>0 && !iptcc_is_builtin(c)) { /* Walked too far*/
  647. debug(" Not in list, walked too far, sorted list\n");
  648. return NULL;
  649. }
  650. /* Stop on wrap around, if list head is reached */
  651. if (pos == &handle->chains) {
  652. debug("Stop, list head reached\n");
  653. return NULL;
  654. }
  655. }
  656. debug("List search NOT found name:%s\n", name);
  657. return NULL;
  658. }
  659. /* called when rule is to be removed from cache */
  660. static void iptcc_delete_rule(struct rule_head *r)
  661. {
  662. DEBUGP("deleting rule %p (offset %u)\n", r, r->offset);
  663. /* clean up reference count of called chain */
  664. if (r->type == IPTCC_R_JUMP
  665. && r->jump)
  666. r->jump->references--;
  667. list_del(&r->list);
  668. free(r);
  669. }
  670. /**********************************************************************
  671. * RULESET PARSER (blob -> cache)
  672. **********************************************************************/
  673. /* Delete policy rule of previous chain, since cache doesn't contain
  674. * chain policy rules.
  675. * WARNING: This function has ugly design and relies on a lot of context, only
  676. * to be called from specific places within the parser */
  677. static int __iptcc_p_del_policy(struct xtc_handle *h, unsigned int num)
  678. {
  679. const unsigned char *data;
  680. if (h->chain_iterator_cur) {
  681. /* policy rule is last rule */
  682. struct rule_head *pr = (struct rule_head *)
  683. h->chain_iterator_cur->rules.prev;
  684. /* save verdict */
  685. data = GET_TARGET(pr->entry)->data;
  686. h->chain_iterator_cur->verdict = *(const int *)data;
  687. /* save counter and counter_map information */
  688. h->chain_iterator_cur->counter_map.maptype =
  689. COUNTER_MAP_ZEROED;
  690. h->chain_iterator_cur->counter_map.mappos = num-1;
  691. memcpy(&h->chain_iterator_cur->counters, &pr->entry->counters,
  692. sizeof(h->chain_iterator_cur->counters));
  693. /* foot_offset points to verdict rule */
  694. h->chain_iterator_cur->foot_index = num;
  695. h->chain_iterator_cur->foot_offset = pr->offset;
  696. /* delete rule from cache */
  697. iptcc_delete_rule(pr);
  698. h->chain_iterator_cur->num_rules--;
  699. return 1;
  700. }
  701. return 0;
  702. }
  703. /* alphabetically insert a chain into the list */
  704. static void iptc_insert_chain(struct xtc_handle *h, struct chain_head *c)
  705. {
  706. struct chain_head *tmp;
  707. struct list_head *list_start_pos;
  708. unsigned int i=1;
  709. /* Find a smart place to start the insert search */
  710. list_start_pos = iptcc_bsearch_chain_index(c->name, &i, h);
  711. /* Handle the case, where chain.name is smaller than index[0] */
  712. if (i==0 && strcmp(c->name, h->chain_index[0]->name) <= 0) {
  713. h->chain_index[0] = c; /* Update chain index head */
  714. list_start_pos = h->chains.next;
  715. debug("Update chain_index[0] with %s\n", c->name);
  716. }
  717. /* Handel if bsearch bails out early */
  718. if (list_start_pos == &h->chains) {
  719. list_start_pos = h->chains.next;
  720. }
  721. /* sort only user defined chains */
  722. if (!c->hooknum) {
  723. list_for_each_entry(tmp, list_start_pos->prev, list) {
  724. if (!tmp->hooknum && strcmp(c->name, tmp->name) <= 0) {
  725. list_add(&c->list, tmp->list.prev);
  726. return;
  727. }
  728. /* Stop if list head is reached */
  729. if (&tmp->list == &h->chains) {
  730. debug("Insert, list head reached add to tail\n");
  731. break;
  732. }
  733. }
  734. }
  735. /* survived till end of list: add at tail */
  736. list_add_tail(&c->list, &h->chains);
  737. }
  738. /* Another ugly helper function split out of cache_add_entry to make it less
  739. * spaghetti code */
  740. static void __iptcc_p_add_chain(struct xtc_handle *h, struct chain_head *c,
  741. unsigned int offset, unsigned int *num)
  742. {
  743. struct list_head *tail = h->chains.prev;
  744. struct chain_head *ctail;
  745. __iptcc_p_del_policy(h, *num);
  746. c->head_offset = offset;
  747. c->index = *num;
  748. /* Chains from kernel are already sorted, as they are inserted
  749. * sorted. But there exists an issue when shifting to 1.4.0
  750. * from an older version, as old versions allow last created
  751. * chain to be unsorted.
  752. */
  753. if (iptcc_is_builtin(c)) /* Only user defined chains are sorted*/
  754. list_add_tail(&c->list, &h->chains);
  755. else {
  756. ctail = list_entry(tail, struct chain_head, list);
  757. if (strcmp(c->name, ctail->name) > 0 ||
  758. iptcc_is_builtin(ctail))
  759. list_add_tail(&c->list, &h->chains);/* Already sorted*/
  760. else {
  761. iptc_insert_chain(h, c);/* Was not sorted */
  762. /* Notice, if chains were not received sorted
  763. * from kernel, then an offset bsearch is no
  764. * longer valid.
  765. */
  766. h->sorted_offsets = 0;
  767. debug("NOTICE: chain:[%s] was NOT sorted(ctail:%s)\n",
  768. c->name, ctail->name);
  769. }
  770. }
  771. h->chain_iterator_cur = c;
  772. }
  773. /* main parser function: add an entry from the blob to the cache */
  774. static int cache_add_entry(STRUCT_ENTRY *e,
  775. struct xtc_handle *h,
  776. STRUCT_ENTRY **prev,
  777. unsigned int *num)
  778. {
  779. unsigned int builtin;
  780. unsigned int offset = (char *)e - (char *)h->entries->entrytable;
  781. DEBUGP("entering...");
  782. /* Last entry ("policy rule"). End it.*/
  783. if (iptcb_entry2offset(h,e) + e->next_offset == h->entries->size) {
  784. /* This is the ERROR node at the end of the chain */
  785. DEBUGP_C("%u:%u: end of table:\n", *num, offset);
  786. __iptcc_p_del_policy(h, *num);
  787. h->chain_iterator_cur = NULL;
  788. goto out_inc;
  789. }
  790. /* We know this is the start of a new chain if it's an ERROR
  791. * target, or a hook entry point */
  792. if (strcmp(GET_TARGET(e)->u.user.name, ERROR_TARGET) == 0) {
  793. struct chain_head *c =
  794. iptcc_alloc_chain_head((const char *)GET_TARGET(e)->data, 0);
  795. DEBUGP_C("%u:%u:new userdefined chain %s: %p\n", *num, offset,
  796. (char *)c->name, c);
  797. if (!c) {
  798. errno = -ENOMEM;
  799. return -1;
  800. }
  801. h->num_chains++; /* New user defined chain */
  802. __iptcc_p_add_chain(h, c, offset, num);
  803. } else if ((builtin = iptcb_ent_is_hook_entry(e, h)) != 0) {
  804. struct chain_head *c =
  805. iptcc_alloc_chain_head((char *)hooknames[builtin-1],
  806. builtin);
  807. DEBUGP_C("%u:%u new builtin chain: %p (rules=%p)\n",
  808. *num, offset, c, &c->rules);
  809. if (!c) {
  810. errno = -ENOMEM;
  811. return -1;
  812. }
  813. c->hooknum = builtin;
  814. __iptcc_p_add_chain(h, c, offset, num);
  815. /* FIXME: this is ugly. */
  816. goto new_rule;
  817. } else {
  818. /* has to be normal rule */
  819. struct rule_head *r;
  820. new_rule:
  821. if (!(r = iptcc_alloc_rule(h->chain_iterator_cur,
  822. e->next_offset))) {
  823. errno = ENOMEM;
  824. return -1;
  825. }
  826. DEBUGP_C("%u:%u normal rule: %p: ", *num, offset, r);
  827. r->index = *num;
  828. r->offset = offset;
  829. memcpy(r->entry, e, e->next_offset);
  830. r->counter_map.maptype = COUNTER_MAP_NORMAL_MAP;
  831. r->counter_map.mappos = r->index;
  832. /* handling of jumps, etc. */
  833. if (!strcmp(GET_TARGET(e)->u.user.name, STANDARD_TARGET)) {
  834. STRUCT_STANDARD_TARGET *t;
  835. t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
  836. if (t->target.u.target_size
  837. != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
  838. errno = EINVAL;
  839. free(r);
  840. return -1;
  841. }
  842. if (t->verdict < 0) {
  843. DEBUGP_C("standard, verdict=%d\n", t->verdict);
  844. r->type = IPTCC_R_STANDARD;
  845. } else if (t->verdict == r->offset+e->next_offset) {
  846. DEBUGP_C("fallthrough\n");
  847. r->type = IPTCC_R_FALLTHROUGH;
  848. } else {
  849. DEBUGP_C("jump, target=%u\n", t->verdict);
  850. r->type = IPTCC_R_JUMP;
  851. /* Jump target fixup has to be deferred
  852. * until second pass, since we migh not
  853. * yet have parsed the target */
  854. }
  855. } else {
  856. DEBUGP_C("module, target=%s\n", GET_TARGET(e)->u.user.name);
  857. r->type = IPTCC_R_MODULE;
  858. }
  859. list_add_tail(&r->list, &h->chain_iterator_cur->rules);
  860. h->chain_iterator_cur->num_rules++;
  861. }
  862. out_inc:
  863. (*num)++;
  864. return 0;
  865. }
  866. /* parse an iptables blob into it's pieces */
  867. static int parse_table(struct xtc_handle *h)
  868. {
  869. STRUCT_ENTRY *prev;
  870. unsigned int num = 0;
  871. struct chain_head *c;
  872. /* Assume that chains offsets are sorted, this verified during
  873. parsing of ruleset (in __iptcc_p_add_chain())*/
  874. h->sorted_offsets = 1;
  875. /* First pass: over ruleset blob */
  876. ENTRY_ITERATE(h->entries->entrytable, h->entries->size,
  877. cache_add_entry, h, &prev, &num);
  878. /* Build the chain index, used for chain list search speedup */
  879. if ((iptcc_chain_index_alloc(h)) < 0)
  880. return -ENOMEM;
  881. iptcc_chain_index_build(h);
  882. /* Second pass: fixup parsed data from first pass */
  883. list_for_each_entry(c, &h->chains, list) {
  884. struct rule_head *r;
  885. list_for_each_entry(r, &c->rules, list) {
  886. struct chain_head *lc;
  887. STRUCT_STANDARD_TARGET *t;
  888. if (r->type != IPTCC_R_JUMP)
  889. continue;
  890. t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
  891. lc = iptcc_find_chain_by_offset(h, t->verdict);
  892. if (!lc)
  893. return -1;
  894. r->jump = lc;
  895. lc->references++;
  896. }
  897. }
  898. return 1;
  899. }
  900. /**********************************************************************
  901. * RULESET COMPILATION (cache -> blob)
  902. **********************************************************************/
  903. /* Convenience structures */
  904. struct iptcb_chain_start{
  905. STRUCT_ENTRY e;
  906. struct xt_error_target name;
  907. };
  908. #define IPTCB_CHAIN_START_SIZE (sizeof(STRUCT_ENTRY) + \
  909. ALIGN(sizeof(struct xt_error_target)))
  910. struct iptcb_chain_foot {
  911. STRUCT_ENTRY e;
  912. STRUCT_STANDARD_TARGET target;
  913. };
  914. #define IPTCB_CHAIN_FOOT_SIZE (sizeof(STRUCT_ENTRY) + \
  915. ALIGN(sizeof(STRUCT_STANDARD_TARGET)))
  916. struct iptcb_chain_error {
  917. STRUCT_ENTRY entry;
  918. struct xt_error_target target;
  919. };
  920. #define IPTCB_CHAIN_ERROR_SIZE (sizeof(STRUCT_ENTRY) + \
  921. ALIGN(sizeof(struct xt_error_target)))
  922. /* compile rule from cache into blob */
  923. static inline int iptcc_compile_rule (struct xtc_handle *h, STRUCT_REPLACE *repl, struct rule_head *r)
  924. {
  925. /* handle jumps */
  926. if (r->type == IPTCC_R_JUMP) {
  927. STRUCT_STANDARD_TARGET *t;
  928. t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
  929. /* memset for memcmp convenience on delete/replace */
  930. memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
  931. strcpy(t->target.u.user.name, STANDARD_TARGET);
  932. /* Jumps can only happen to builtin chains, so we
  933. * can safely assume that they always have a header */
  934. t->verdict = r->jump->head_offset + IPTCB_CHAIN_START_SIZE;
  935. } else if (r->type == IPTCC_R_FALLTHROUGH) {
  936. STRUCT_STANDARD_TARGET *t;
  937. t = (STRUCT_STANDARD_TARGET *)GET_TARGET(r->entry);
  938. t->verdict = r->offset + r->size;
  939. }
  940. /* copy entry from cache to blob */
  941. memcpy((char *)repl->entries+r->offset, r->entry, r->size);
  942. return 1;
  943. }
  944. /* compile chain from cache into blob */
  945. static int iptcc_compile_chain(struct xtc_handle *h, STRUCT_REPLACE *repl, struct chain_head *c)
  946. {
  947. int ret;
  948. struct rule_head *r;
  949. struct iptcb_chain_start *head;
  950. struct iptcb_chain_foot *foot;
  951. /* only user-defined chains have heaer */
  952. if (!iptcc_is_builtin(c)) {
  953. /* put chain header in place */
  954. head = (void *)repl->entries + c->head_offset;
  955. head->e.target_offset = sizeof(STRUCT_ENTRY);
  956. head->e.next_offset = IPTCB_CHAIN_START_SIZE;
  957. strcpy(head->name.target.u.user.name, ERROR_TARGET);
  958. head->name.target.u.target_size =
  959. ALIGN(sizeof(struct xt_error_target));
  960. strcpy(head->name.errorname, c->name);
  961. } else {
  962. repl->hook_entry[c->hooknum-1] = c->head_offset;
  963. repl->underflow[c->hooknum-1] = c->foot_offset;
  964. }
  965. /* iterate over rules */
  966. list_for_each_entry(r, &c->rules, list) {
  967. ret = iptcc_compile_rule(h, repl, r);
  968. if (ret < 0)
  969. return ret;
  970. }
  971. /* put chain footer in place */
  972. foot = (void *)repl->entries + c->foot_offset;
  973. foot->e.target_offset = sizeof(STRUCT_ENTRY);
  974. foot->e.next_offset = IPTCB_CHAIN_FOOT_SIZE;
  975. strcpy(foot->target.target.u.user.name, STANDARD_TARGET);
  976. foot->target.target.u.target_size =
  977. ALIGN(sizeof(STRUCT_STANDARD_TARGET));
  978. /* builtin targets have verdict, others return */
  979. if (iptcc_is_builtin(c))
  980. foot->target.verdict = c->verdict;
  981. else
  982. foot->target.verdict = RETURN;
  983. /* set policy-counters */
  984. memcpy(&foot->e.counters, &c->counters, sizeof(STRUCT_COUNTERS));
  985. return 0;
  986. }
  987. /* calculate offset and number for every rule in the cache */
  988. static int iptcc_compile_chain_offsets(struct xtc_handle *h, struct chain_head *c,
  989. unsigned int *offset, unsigned int *num)
  990. {
  991. struct rule_head *r;
  992. c->head_offset = *offset;
  993. DEBUGP("%s: chain_head %u, offset=%u\n", c->name, *num, *offset);
  994. if (!iptcc_is_builtin(c)) {
  995. /* Chain has header */
  996. *offset += sizeof(STRUCT_ENTRY)
  997. + ALIGN(sizeof(struct xt_error_target));
  998. (*num)++;
  999. }
  1000. list_for_each_entry(r, &c->rules, list) {
  1001. DEBUGP("rule %u, offset=%u, index=%u\n", *num, *offset, *num);
  1002. r->offset = *offset;
  1003. r->index = *num;
  1004. *offset += r->size;
  1005. (*num)++;
  1006. }
  1007. DEBUGP("%s; chain_foot %u, offset=%u, index=%u\n", c->name, *num,
  1008. *offset, *num);
  1009. c->foot_offset = *offset;
  1010. c->foot_index = *num;
  1011. *offset += sizeof(STRUCT_ENTRY)
  1012. + ALIGN(sizeof(STRUCT_STANDARD_TARGET));
  1013. (*num)++;
  1014. return 1;
  1015. }
  1016. /* put the pieces back together again */
  1017. static int iptcc_compile_table_prep(struct xtc_handle *h, unsigned int *size)
  1018. {
  1019. struct chain_head *c;
  1020. unsigned int offset = 0, num = 0;
  1021. int ret = 0;
  1022. /* First pass: calculate offset for every rule */
  1023. list_for_each_entry(c, &h->chains, list) {
  1024. ret = iptcc_compile_chain_offsets(h, c, &offset, &num);
  1025. if (ret < 0)
  1026. return ret;
  1027. }
  1028. /* Append one error rule at end of chain */
  1029. num++;
  1030. offset += sizeof(STRUCT_ENTRY)
  1031. + ALIGN(sizeof(struct xt_error_target));
  1032. /* ruleset size is now in offset */
  1033. *size = offset;
  1034. return num;
  1035. }
  1036. static int iptcc_compile_table(struct xtc_handle *h, STRUCT_REPLACE *repl)
  1037. {
  1038. struct chain_head *c;
  1039. struct iptcb_chain_error *error;
  1040. /* Second pass: copy from cache to offsets, fill in jumps */
  1041. list_for_each_entry(c, &h->chains, list) {
  1042. int ret = iptcc_compile_chain(h, repl, c);
  1043. if (ret < 0)
  1044. return ret;
  1045. }
  1046. /* Append error rule at end of chain */
  1047. error = (void *)repl->entries + repl->size - IPTCB_CHAIN_ERROR_SIZE;
  1048. error->entry.target_offset = sizeof(STRUCT_ENTRY);
  1049. error->entry.next_offset = IPTCB_CHAIN_ERROR_SIZE;
  1050. error->target.target.u.user.target_size =
  1051. ALIGN(sizeof(struct xt_error_target));
  1052. strcpy((char *)&error->target.target.u.user.name, ERROR_TARGET);
  1053. strcpy((char *)&error->target.errorname, "ERROR");
  1054. return 1;
  1055. }
  1056. /**********************************************************************
  1057. * EXTERNAL API (operates on cache only)
  1058. **********************************************************************/
  1059. /* Allocate handle of given size */
  1060. static struct xtc_handle *
  1061. alloc_handle(const char *tablename, unsigned int size, unsigned int num_rules)
  1062. {
  1063. struct xtc_handle *h;
  1064. h = malloc(sizeof(*h));
  1065. if (!h) {
  1066. errno = ENOMEM;
  1067. return NULL;
  1068. }
  1069. memset(h, 0, sizeof(*h));
  1070. INIT_LIST_HEAD(&h->chains);
  1071. strcpy(h->info.name, tablename);
  1072. h->entries = malloc(sizeof(STRUCT_GET_ENTRIES) + size);
  1073. if (!h->entries)
  1074. goto out_free_handle;
  1075. strcpy(h->entries->name, tablename);
  1076. h->entries->size = size;
  1077. return h;
  1078. out_free_handle:
  1079. free(h);
  1080. return NULL;
  1081. }
  1082. struct xtc_handle *
  1083. TC_INIT(const char *tablename)
  1084. {
  1085. struct xtc_handle *h;
  1086. STRUCT_GETINFO info;
  1087. unsigned int tmp;
  1088. socklen_t s;
  1089. int sockfd;
  1090. retry:
  1091. iptc_fn = TC_INIT;
  1092. if (strlen(tablename) >= TABLE_MAXNAMELEN) {
  1093. errno = EINVAL;
  1094. return NULL;
  1095. }
  1096. sockfd = socket(TC_AF, SOCK_RAW, IPPROTO_RAW);
  1097. if (sockfd < 0)
  1098. return NULL;
  1099. if (fcntl(sockfd, F_SETFD, FD_CLOEXEC) == -1) {
  1100. fprintf(stderr, "Could not set close on exec: %s\n",
  1101. strerror(errno));
  1102. abort();
  1103. }
  1104. s = sizeof(info);
  1105. strcpy(info.name, tablename);
  1106. if (getsockopt(sockfd, TC_IPPROTO, SO_GET_INFO, &info, &s) < 0) {
  1107. close(sockfd);
  1108. return NULL;
  1109. }
  1110. DEBUGP("valid_hooks=0x%08x, num_entries=%u, size=%u\n",
  1111. info.valid_hooks, info.num_entries, info.size);
  1112. if ((h = alloc_handle(info.name, info.size, info.num_entries))
  1113. == NULL) {
  1114. close(sockfd);
  1115. return NULL;
  1116. }
  1117. /* Initialize current state */
  1118. h->sockfd = sockfd;
  1119. h->info = info;
  1120. h->entries->size = h->info.size;
  1121. tmp = sizeof(STRUCT_GET_ENTRIES) + h->info.size;
  1122. if (getsockopt(h->sockfd, TC_IPPROTO, SO_GET_ENTRIES, h->entries,
  1123. &tmp) < 0)
  1124. goto error;
  1125. #ifdef IPTC_DEBUG2
  1126. {
  1127. int fd = open("/tmp/libiptc-so_get_entries.blob",
  1128. O_CREAT|O_WRONLY);
  1129. if (fd >= 0) {
  1130. write(fd, h->entries, tmp);
  1131. close(fd);
  1132. }
  1133. }
  1134. #endif
  1135. if (parse_table(h) < 0)
  1136. goto error;
  1137. CHECK(h);
  1138. return h;
  1139. error:
  1140. TC_FREE(h);
  1141. /* A different process changed the ruleset size, retry */
  1142. if (errno == EAGAIN)
  1143. goto retry;
  1144. return NULL;
  1145. }
  1146. void
  1147. TC_FREE(struct xtc_handle *h)
  1148. {
  1149. struct chain_head *c, *tmp;
  1150. iptc_fn = TC_FREE;
  1151. close(h->sockfd);
  1152. list_for_each_entry_safe(c, tmp, &h->chains, list) {
  1153. struct rule_head *r, *rtmp;
  1154. list_for_each_entry_safe(r, rtmp, &c->rules, list) {
  1155. free(r);
  1156. }
  1157. free(c);
  1158. }
  1159. iptcc_chain_index_free(h);
  1160. free(h->entries);
  1161. free(h);
  1162. }
  1163. static inline int
  1164. print_match(const STRUCT_ENTRY_MATCH *m)
  1165. {
  1166. printf("Match name: `%s'\n", m->u.user.name);
  1167. return 0;
  1168. }
  1169. static int dump_entry(STRUCT_ENTRY *e, struct xtc_handle *const handle);
  1170. void
  1171. TC_DUMP_ENTRIES(struct xtc_handle *const handle)
  1172. {
  1173. iptc_fn = TC_DUMP_ENTRIES;
  1174. CHECK(handle);
  1175. printf("libiptc v%s. %u bytes.\n",
  1176. XTABLES_VERSION, handle->entries->size);
  1177. printf("Table `%s'\n", handle->info.name);
  1178. printf("Hooks: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
  1179. handle->info.hook_entry[HOOK_PRE_ROUTING],
  1180. handle->info.hook_entry[HOOK_LOCAL_IN],
  1181. handle->info.hook_entry[HOOK_FORWARD],
  1182. handle->info.hook_entry[HOOK_LOCAL_OUT],
  1183. handle->info.hook_entry[HOOK_POST_ROUTING]);
  1184. printf("Underflows: pre/in/fwd/out/post = %x/%x/%x/%x/%x\n",
  1185. handle->info.underflow[HOOK_PRE_ROUTING],
  1186. handle->info.underflow[HOOK_LOCAL_IN],
  1187. handle->info.underflow[HOOK_FORWARD],
  1188. handle->info.underflow[HOOK_LOCAL_OUT],
  1189. handle->info.underflow[HOOK_POST_ROUTING]);
  1190. ENTRY_ITERATE(handle->entries->entrytable, handle->entries->size,
  1191. dump_entry, handle);
  1192. }
  1193. /* Does this chain exist? */
  1194. int TC_IS_CHAIN(const char *chain, struct xtc_handle *const handle)
  1195. {
  1196. iptc_fn = TC_IS_CHAIN;
  1197. return iptcc_find_label(chain, handle) != NULL;
  1198. }
  1199. static void iptcc_chain_iterator_advance(struct xtc_handle *handle)
  1200. {
  1201. struct chain_head *c = handle->chain_iterator_cur;
  1202. if (c->list.next == &handle->chains)
  1203. handle->chain_iterator_cur = NULL;
  1204. else
  1205. handle->chain_iterator_cur =
  1206. list_entry(c->list.next, struct chain_head, list);
  1207. }
  1208. /* Iterator functions to run through the chains. */
  1209. const char *
  1210. TC_FIRST_CHAIN(struct xtc_handle *handle)
  1211. {
  1212. struct chain_head *c = list_entry(handle->chains.next,
  1213. struct chain_head, list);
  1214. iptc_fn = TC_FIRST_CHAIN;
  1215. if (list_empty(&handle->chains)) {
  1216. DEBUGP(": no chains\n");
  1217. return NULL;
  1218. }
  1219. handle->chain_iterator_cur = c;
  1220. iptcc_chain_iterator_advance(handle);
  1221. DEBUGP(": returning `%s'\n", c->name);
  1222. return c->name;
  1223. }
  1224. /* Iterator functions to run through the chains. Returns NULL at end. */
  1225. const char *
  1226. TC_NEXT_CHAIN(struct xtc_handle *handle)
  1227. {
  1228. struct chain_head *c = handle->chain_iterator_cur;
  1229. iptc_fn = TC_NEXT_CHAIN;
  1230. if (!c) {
  1231. DEBUGP(": no more chains\n");
  1232. return NULL;
  1233. }
  1234. iptcc_chain_iterator_advance(handle);
  1235. DEBUGP(": returning `%s'\n", c->name);
  1236. return c->name;
  1237. }
  1238. /* Get first rule in the given chain: NULL for empty chain. */
  1239. const STRUCT_ENTRY *
  1240. TC_FIRST_RULE(const char *chain, struct xtc_handle *handle)
  1241. {
  1242. struct chain_head *c;
  1243. struct rule_head *r;
  1244. iptc_fn = TC_FIRST_RULE;
  1245. DEBUGP("first rule(%s): ", chain);
  1246. c = iptcc_find_label(chain, handle);
  1247. if (!c) {
  1248. errno = ENOENT;
  1249. return NULL;
  1250. }
  1251. /* Empty chain: single return/policy rule */
  1252. if (list_empty(&c->rules)) {
  1253. DEBUGP_C("no rules, returning NULL\n");
  1254. return NULL;
  1255. }
  1256. r = list_entry(c->rules.next, struct rule_head, list);
  1257. handle->rule_iterator_cur = r;
  1258. DEBUGP_C("%p\n", r);
  1259. return r->entry;
  1260. }
  1261. /* Returns NULL when rules run out. */
  1262. const STRUCT_ENTRY *
  1263. TC_NEXT_RULE(const STRUCT_ENTRY *prev, struct xtc_handle *handle)
  1264. {
  1265. struct rule_head *r;
  1266. iptc_fn = TC_NEXT_RULE;
  1267. DEBUGP("rule_iterator_cur=%p...", handle->rule_iterator_cur);
  1268. if (handle->rule_iterator_cur == NULL) {
  1269. DEBUGP_C("returning NULL\n");
  1270. return NULL;
  1271. }
  1272. r = list_entry(handle->rule_iterator_cur->list.next,
  1273. struct rule_head, list);
  1274. iptc_fn = TC_NEXT_RULE;
  1275. DEBUGP_C("next=%p, head=%p...", &r->list,
  1276. &handle->rule_iterator_cur->chain->rules);
  1277. if (&r->list == &handle->rule_iterator_cur->chain->rules) {
  1278. handle->rule_iterator_cur = NULL;
  1279. DEBUGP_C("finished, returning NULL\n");
  1280. return NULL;
  1281. }
  1282. handle->rule_iterator_cur = r;
  1283. /* NOTE: prev is without any influence ! */
  1284. DEBUGP_C("returning rule %p\n", r);
  1285. return r->entry;
  1286. }
  1287. /* Returns a pointer to the target name of this position. */
  1288. static const char *standard_target_map(int verdict)
  1289. {
  1290. switch (verdict) {
  1291. case RETURN:
  1292. return LABEL_RETURN;
  1293. break;
  1294. case -NF_ACCEPT-1:
  1295. return LABEL_ACCEPT;
  1296. break;
  1297. case -NF_DROP-1:
  1298. return LABEL_DROP;
  1299. break;
  1300. case -NF_QUEUE-1:
  1301. return LABEL_QUEUE;
  1302. break;
  1303. default:
  1304. fprintf(stderr, "ERROR: %d not a valid target)\n",
  1305. verdict);
  1306. abort();
  1307. break;
  1308. }
  1309. /* not reached */
  1310. return NULL;
  1311. }
  1312. /* Returns a pointer to the target name of this position. */
  1313. const char *TC_GET_TARGET(const STRUCT_ENTRY *ce,
  1314. struct xtc_handle *handle)
  1315. {
  1316. STRUCT_ENTRY *e = (STRUCT_ENTRY *)ce;
  1317. struct rule_head *r = container_of(e, struct rule_head, entry[0]);
  1318. const unsigned char *data;
  1319. iptc_fn = TC_GET_TARGET;
  1320. switch(r->type) {
  1321. int spos;
  1322. case IPTCC_R_FALLTHROUGH:
  1323. return "";
  1324. break;
  1325. case IPTCC_R_JUMP:
  1326. DEBUGP("r=%p, jump=%p, name=`%s'\n", r, r->jump, r->jump->name);
  1327. return r->jump->name;
  1328. break;
  1329. case IPTCC_R_STANDARD:
  1330. data = GET_TARGET(e)->data;
  1331. spos = *(const int *)data;
  1332. DEBUGP("r=%p, spos=%d'\n", r, spos);
  1333. return standard_target_map(spos);
  1334. break;
  1335. case IPTCC_R_MODULE:
  1336. return GET_TARGET(e)->u.user.name;
  1337. break;
  1338. }
  1339. return NULL;
  1340. }
  1341. /* Is this a built-in chain? Actually returns hook + 1. */
  1342. int
  1343. TC_BUILTIN(const char *chain, struct xtc_handle *const handle)
  1344. {
  1345. struct chain_head *c;
  1346. iptc_fn = TC_BUILTIN;
  1347. c = iptcc_find_label(chain, handle);
  1348. if (!c) {
  1349. errno = ENOENT;
  1350. return 0;
  1351. }
  1352. return iptcc_is_builtin(c);
  1353. }
  1354. /* Get the policy of a given built-in chain */
  1355. const char *
  1356. TC_GET_POLICY(const char *chain,
  1357. STRUCT_COUNTERS *counters,
  1358. struct xtc_handle *handle)
  1359. {
  1360. struct chain_head *c;
  1361. iptc_fn = TC_GET_POLICY;
  1362. DEBUGP("called for chain %s\n", chain);
  1363. c = iptcc_find_label(chain, handle);
  1364. if (!c) {
  1365. errno = ENOENT;
  1366. return NULL;
  1367. }
  1368. if (!iptcc_is_builtin(c))
  1369. return NULL;
  1370. *counters = c->counters;
  1371. return standard_target_map(c->verdict);
  1372. }
  1373. static int
  1374. iptcc_standard_map(struct rule_head *r, int verdict)
  1375. {
  1376. STRUCT_ENTRY *e = r->entry;
  1377. STRUCT_STANDARD_TARGET *t;
  1378. t = (STRUCT_STANDARD_TARGET *)GET_TARGET(e);
  1379. if (t->target.u.target_size
  1380. != ALIGN(sizeof(STRUCT_STANDARD_TARGET))) {
  1381. errno = EINVAL;
  1382. return 0;
  1383. }
  1384. /* memset for memcmp convenience on delete/replace */
  1385. memset(t->target.u.user.name, 0, FUNCTION_MAXNAMELEN);
  1386. strcpy(t->target.u.user.name, STANDARD_TARGET);
  1387. t->verdict = verdict;
  1388. r->type = IPTCC_R_STANDARD;
  1389. return 1;
  1390. }
  1391. static int
  1392. iptcc_map_target(struct xtc_handle *const handle,
  1393. struct rule_head *r)
  1394. {
  1395. STRUCT_ENTRY *e = r->entry;
  1396. STRUCT_ENTRY_TARGET *t = GET_TARGET(e);
  1397. /* Maybe it's empty (=> fall through) */
  1398. if (strcmp(t->u.user.name, "") == 0) {
  1399. r->type = IPTCC_R_FALLTHROUGH;
  1400. return 1;
  1401. }
  1402. /* Maybe it's a standard target name... */
  1403. else if (strcmp(t->u.user.name, LABEL_ACCEPT) == 0)
  1404. return iptcc_standard_map(r, -NF_ACCEPT - 1);
  1405. else if (strcmp(t->u.user.name, LABEL_DROP) == 0)
  1406. return iptcc_standard_map(r, -NF_DROP - 1);
  1407. else if (strcmp(t->u.user.name, LABEL_QUEUE) == 0)
  1408. return iptcc_standard_map(r, -NF_QUEUE - 1);
  1409. else if (strcmp(t->u.user.name, LABEL_RETURN) == 0)
  1410. return iptcc_standard_map(r, RETURN);
  1411. else if (TC_BUILTIN(t->u.user.name, handle)) {
  1412. /* Can't jump to builtins. */
  1413. errno = EINVAL;
  1414. return 0;
  1415. } else {
  1416. /* Maybe it's an existing chain name. */
  1417. struct chain_head *c;
  1418. DEBUGP("trying to find chain `%s': ", t->u.user.name);
  1419. c = iptcc_find_label(t->u.user.name, handle);
  1420. if (c) {
  1421. DEBUGP_C("found!\n");
  1422. r->type = IPTCC_R_JUMP;
  1423. r->jump = c;
  1424. c->references++;
  1425. return 1;
  1426. }
  1427. DEBUGP_C("not found :(\n");
  1428. }
  1429. /* Must be a module? If not, kernel will reject... */
  1430. /* memset to all 0 for your memcmp convenience: don't clear version */
  1431. memset(t->u.user.name + strlen(t->u.user.name),
  1432. 0,
  1433. FUNCTION_MAXNAMELEN - 1 - strlen(t->u.user.name));
  1434. r->type = IPTCC_R_MODULE;
  1435. set_changed(handle);
  1436. return 1;
  1437. }
  1438. /* Insert the entry `fw' in chain `chain' into position `rulenum'. */
  1439. int
  1440. TC_INSERT_ENTRY(const IPT_CHAINLABEL chain,
  1441. const STRUCT_ENTRY *e,
  1442. unsigned int rulenum,
  1443. struct xtc_handle *handle)
  1444. {
  1445. struct chain_head *c;
  1446. struct rule_head *r;
  1447. struct list_head *prev;
  1448. iptc_fn = TC_INSERT_ENTRY;
  1449. if (!(c = iptcc_find_label(chain, handle))) {
  1450. errno = ENOENT;
  1451. return 0;
  1452. }
  1453. /* first rulenum index = 0
  1454. first c->num_rules index = 1 */
  1455. if (rulenum > c->num_rules) {
  1456. errno = E2BIG;
  1457. return 0;
  1458. }
  1459. /* If we are inserting at the end just take advantage of the
  1460. double linked list, insert will happen before the entry
  1461. prev points to. */
  1462. if (rulenum == c->num_rules) {
  1463. prev = &c->rules;
  1464. } else if (rulenum + 1 <= c->num_rules/2) {
  1465. r = iptcc_get_rule_num(c, rulenum + 1);
  1466. prev = &r->list;
  1467. } else {
  1468. r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
  1469. prev = &r->list;
  1470. }
  1471. if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
  1472. errno = ENOMEM;
  1473. return 0;
  1474. }
  1475. memcpy(r->entry, e, e->next_offset);
  1476. r->counter_map.maptype = COUNTER_MAP_SET;
  1477. if (!iptcc_map_target(handle, r)) {
  1478. free(r);
  1479. return 0;
  1480. }
  1481. list_add_tail(&r->list, prev);
  1482. c->num_rules++;
  1483. set_changed(handle);
  1484. return 1;
  1485. }
  1486. /* Atomically replace rule `rulenum' in `chain' with `fw'. */
  1487. int
  1488. TC_REPLACE_ENTRY(const IPT_CHAINLABEL chain,
  1489. const STRUCT_ENTRY *e,
  1490. unsigned int rulenum,
  1491. struct xtc_handle *handle)
  1492. {
  1493. struct chain_head *c;
  1494. struct rule_head *r, *old;
  1495. iptc_fn = TC_REPLACE_ENTRY;
  1496. if (!(c = iptcc_find_label(chain, handle))) {
  1497. errno = ENOENT;
  1498. return 0;
  1499. }
  1500. if (rulenum >= c->num_rules) {
  1501. errno = E2BIG;
  1502. return 0;
  1503. }
  1504. /* Take advantage of the double linked list if possible. */
  1505. if (rulenum + 1 <= c->num_rules/2) {
  1506. old = iptcc_get_rule_num(c, rulenum + 1);
  1507. } else {
  1508. old = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
  1509. }
  1510. if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
  1511. errno = ENOMEM;
  1512. return 0;
  1513. }
  1514. memcpy(r->entry, e, e->next_offset);
  1515. r->counter_map.maptype = COUNTER_MAP_SET;
  1516. if (!iptcc_map_target(handle, r)) {
  1517. free(r);
  1518. return 0;
  1519. }
  1520. list_add(&r->list, &old->list);
  1521. iptcc_delete_rule(old);
  1522. set_changed(handle);
  1523. return 1;
  1524. }
  1525. /* Append entry `fw' to chain `chain'. Equivalent to insert with
  1526. rulenum = length of chain. */
  1527. int
  1528. TC_APPEND_ENTRY(const IPT_CHAINLABEL chain,
  1529. const STRUCT_ENTRY *e,
  1530. struct xtc_handle *handle)
  1531. {
  1532. struct chain_head *c;
  1533. struct rule_head *r;
  1534. iptc_fn = TC_APPEND_ENTRY;
  1535. if (!(c = iptcc_find_label(chain, handle))) {
  1536. DEBUGP("unable to find chain `%s'\n", chain);
  1537. errno = ENOENT;
  1538. return 0;
  1539. }
  1540. if (!(r = iptcc_alloc_rule(c, e->next_offset))) {
  1541. DEBUGP("unable to allocate rule for chain `%s'\n", chain);
  1542. errno = ENOMEM;
  1543. return 0;
  1544. }
  1545. memcpy(r->entry, e, e->next_offset);
  1546. r->counter_map.maptype = COUNTER_MAP_SET;
  1547. if (!iptcc_map_target(handle, r)) {
  1548. DEBUGP("unable to map target of rule for chain `%s'\n", chain);
  1549. free(r);
  1550. return 0;
  1551. }
  1552. list_add_tail(&r->list, &c->rules);
  1553. c->num_rules++;
  1554. set_changed(handle);
  1555. return 1;
  1556. }
  1557. static inline int
  1558. match_different(const STRUCT_ENTRY_MATCH *a,
  1559. const unsigned char *a_elems,
  1560. const unsigned char *b_elems,
  1561. unsigned char **maskptr)
  1562. {
  1563. const STRUCT_ENTRY_MATCH *b;
  1564. unsigned int i;
  1565. /* Offset of b is the same as a. */
  1566. b = (void *)b_elems + ((unsigned char *)a - a_elems);
  1567. if (a->u.match_size != b->u.match_size)
  1568. return 1;
  1569. if (strcmp(a->u.user.name, b->u.user.name) != 0)
  1570. return 1;
  1571. *maskptr += ALIGN(sizeof(*a));
  1572. for (i = 0; i < a->u.match_size - ALIGN(sizeof(*a)); i++)
  1573. if (((a->data[i] ^ b->data[i]) & (*maskptr)[i]) != 0)
  1574. return 1;
  1575. *maskptr += i;
  1576. return 0;
  1577. }
  1578. static inline int
  1579. target_same(struct rule_head *a, struct rule_head *b,const unsigned char *mask)
  1580. {
  1581. unsigned int i;
  1582. STRUCT_ENTRY_TARGET *ta, *tb;
  1583. if (a->type != b->type)
  1584. return 0;
  1585. ta = GET_TARGET(a->entry);
  1586. tb = GET_TARGET(b->entry);
  1587. switch (a->type) {
  1588. case IPTCC_R_FALLTHROUGH:
  1589. return 1;
  1590. case IPTCC_R_JUMP:
  1591. return a->jump == b->jump;
  1592. case IPTCC_R_STANDARD:
  1593. return ((STRUCT_STANDARD_TARGET *)ta)->verdict
  1594. == ((STRUCT_STANDARD_TARGET *)tb)->verdict;
  1595. case IPTCC_R_MODULE:
  1596. if (ta->u.target_size != tb->u.target_size)
  1597. return 0;
  1598. if (strcmp(ta->u.user.name, tb->u.user.name) != 0)
  1599. return 0;
  1600. for (i = 0; i < ta->u.target_size - sizeof(*ta); i++)
  1601. if (((ta->data[i] ^ tb->data[i]) & mask[i]) != 0)
  1602. return 0;
  1603. return 1;
  1604. default:
  1605. fprintf(stderr, "ERROR: bad type %i\n", a->type);
  1606. abort();
  1607. }
  1608. }
  1609. static unsigned char *
  1610. is_same(const STRUCT_ENTRY *a,
  1611. const STRUCT_ENTRY *b,
  1612. unsigned char *matchmask);
  1613. /* find the first rule in `chain' which matches `fw' and remove it unless dry_run is set */
  1614. static int delete_entry(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
  1615. unsigned char *matchmask, struct xtc_handle *handle,
  1616. bool dry_run)
  1617. {
  1618. struct chain_head *c;
  1619. struct rule_head *r, *i;
  1620. iptc_fn = TC_DELETE_ENTRY;
  1621. if (!(c = iptcc_find_label(chain, handle))) {
  1622. errno = ENOENT;
  1623. return 0;
  1624. }
  1625. /* Create a rule_head from origfw. */
  1626. r = iptcc_alloc_rule(c, origfw->next_offset);
  1627. if (!r) {
  1628. errno = ENOMEM;
  1629. return 0;
  1630. }
  1631. memcpy(r->entry, origfw, origfw->next_offset);
  1632. r->counter_map.maptype = COUNTER_MAP_NOMAP;
  1633. if (!iptcc_map_target(handle, r)) {
  1634. DEBUGP("unable to map target of rule for chain `%s'\n", chain);
  1635. free(r);
  1636. return 0;
  1637. } else {
  1638. /* iptcc_map_target increment target chain references
  1639. * since this is a fake rule only used for matching
  1640. * the chain references count is decremented again.
  1641. */
  1642. if (r->type == IPTCC_R_JUMP
  1643. && r->jump)
  1644. r->jump->references--;
  1645. }
  1646. list_for_each_entry(i, &c->rules, list) {
  1647. unsigned char *mask;
  1648. mask = is_same(r->entry, i->entry, matchmask);
  1649. if (!mask)
  1650. continue;
  1651. if (!target_same(r, i, mask))
  1652. continue;
  1653. /* if we are just doing a dry run, we simply skip the rest */
  1654. if (dry_run){
  1655. free(r);
  1656. return 1;
  1657. }
  1658. /* If we are about to delete the rule that is the
  1659. * current iterator, move rule iterator back. next
  1660. * pointer will then point to real next node */
  1661. if (i == handle->rule_iterator_cur) {
  1662. handle->rule_iterator_cur =
  1663. list_entry(handle->rule_iterator_cur->list.prev,
  1664. struct rule_head, list);
  1665. }
  1666. c->num_rules--;
  1667. iptcc_delete_rule(i);
  1668. set_changed(handle);
  1669. free(r);
  1670. return 1;
  1671. }
  1672. free(r);
  1673. errno = ENOENT;
  1674. return 0;
  1675. }
  1676. /* check whether a specified rule is present */
  1677. int TC_CHECK_ENTRY(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
  1678. unsigned char *matchmask, struct xtc_handle *handle)
  1679. {
  1680. /* do a dry-run delete to find out whether a matching rule exists */
  1681. return delete_entry(chain, origfw, matchmask, handle, true);
  1682. }
  1683. /* Delete the first rule in `chain' which matches `fw'. */
  1684. int TC_DELETE_ENTRY(const IPT_CHAINLABEL chain, const STRUCT_ENTRY *origfw,
  1685. unsigned char *matchmask, struct xtc_handle *handle)
  1686. {
  1687. return delete_entry(chain, origfw, matchmask, handle, false);
  1688. }
  1689. /* Delete the rule in position `rulenum' in `chain'. */
  1690. int
  1691. TC_DELETE_NUM_ENTRY(const IPT_CHAINLABEL chain,
  1692. unsigned int rulenum,
  1693. struct xtc_handle *handle)
  1694. {
  1695. struct chain_head *c;
  1696. struct rule_head *r;
  1697. iptc_fn = TC_DELETE_NUM_ENTRY;
  1698. if (!(c = iptcc_find_label(chain, handle))) {
  1699. errno = ENOENT;
  1700. return 0;
  1701. }
  1702. if (rulenum >= c->num_rules) {
  1703. errno = E2BIG;
  1704. return 0;
  1705. }
  1706. /* Take advantage of the double linked list if possible. */
  1707. if (rulenum + 1 <= c->num_rules/2) {
  1708. r = iptcc_get_rule_num(c, rulenum + 1);
  1709. } else {
  1710. r = iptcc_get_rule_num_reverse(c, c->num_rules - rulenum);
  1711. }
  1712. /* If we are about to delete the rule that is the current
  1713. * iterator, move rule iterator back. next pointer will then
  1714. * point to real next node */
  1715. if (r == handle->rule_iterator_cur) {
  1716. handle->rule_iterator_cur =
  1717. list_entry(handle->rule_iterator_cur->list.prev,
  1718. struct rule_head, list);
  1719. }
  1720. c->num_rules--;
  1721. iptcc_delete_rule(r);
  1722. set_changed(handle);
  1723. return 1;
  1724. }
  1725. /* Flushes the entries in the given chain (ie. empties chain). */
  1726. int
  1727. TC_FLUSH_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
  1728. {
  1729. struct chain_head *c;
  1730. struct rule_head *r, *tmp;
  1731. iptc_fn = TC_FLUSH_ENTRIES;
  1732. if (!(c = iptcc_find_label(chain, handle))) {
  1733. errno = ENOENT;
  1734. return 0;
  1735. }
  1736. list_for_each_entry_safe(r, tmp, &c->rules, list) {
  1737. iptcc_delete_rule(r);
  1738. }
  1739. c->num_rules = 0;
  1740. set_changed(handle);
  1741. return 1;
  1742. }
  1743. /* Zeroes the counters in a chain. */
  1744. int
  1745. TC_ZERO_ENTRIES(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
  1746. {
  1747. struct chain_head *c;
  1748. struct rule_head *r;
  1749. iptc_fn = TC_ZERO_ENTRIES;
  1750. if (!(c = iptcc_find_label(chain, handle))) {
  1751. errno = ENOENT;
  1752. return 0;
  1753. }
  1754. if (c->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
  1755. c->counter_map.maptype = COUNTER_MAP_ZEROED;
  1756. list_for_each_entry(r, &c->rules, list) {
  1757. if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
  1758. r->counter_map.maptype = COUNTER_MAP_ZEROED;
  1759. }
  1760. set_changed(handle);
  1761. return 1;
  1762. }
  1763. STRUCT_COUNTERS *
  1764. TC_READ_COUNTER(const IPT_CHAINLABEL chain,
  1765. unsigned int rulenum,
  1766. struct xtc_handle *handle)
  1767. {
  1768. struct chain_head *c;
  1769. struct rule_head *r;
  1770. iptc_fn = TC_READ_COUNTER;
  1771. CHECK(*handle);
  1772. if (!(c = iptcc_find_label(chain, handle))) {
  1773. errno = ENOENT;
  1774. return NULL;
  1775. }
  1776. if (!(r = iptcc_get_rule_num(c, rulenum))) {
  1777. errno = E2BIG;
  1778. return NULL;
  1779. }
  1780. return &r->entry[0].counters;
  1781. }
  1782. int
  1783. TC_ZERO_COUNTER(const IPT_CHAINLABEL chain,
  1784. unsigned int rulenum,
  1785. struct xtc_handle *handle)
  1786. {
  1787. struct chain_head *c;
  1788. struct rule_head *r;
  1789. iptc_fn = TC_ZERO_COUNTER;
  1790. CHECK(handle);
  1791. if (!(c = iptcc_find_label(chain, handle))) {
  1792. errno = ENOENT;
  1793. return 0;
  1794. }
  1795. if (!(r = iptcc_get_rule_num(c, rulenum))) {
  1796. errno = E2BIG;
  1797. return 0;
  1798. }
  1799. if (r->counter_map.maptype == COUNTER_MAP_NORMAL_MAP)
  1800. r->counter_map.maptype = COUNTER_MAP_ZEROED;
  1801. set_changed(handle);
  1802. return 1;
  1803. }
  1804. int
  1805. TC_SET_COUNTER(const IPT_CHAINLABEL chain,
  1806. unsigned int rulenum,
  1807. STRUCT_COUNTERS *counters,
  1808. struct xtc_handle *handle)
  1809. {
  1810. struct chain_head *c;
  1811. struct rule_head *r;
  1812. STRUCT_ENTRY *e;
  1813. iptc_fn = TC_SET_COUNTER;
  1814. CHECK(handle);
  1815. if (!(c = iptcc_find_label(chain, handle))) {
  1816. errno = ENOENT;
  1817. return 0;
  1818. }
  1819. if (!(r = iptcc_get_rule_num(c, rulenum))) {
  1820. errno = E2BIG;
  1821. return 0;
  1822. }
  1823. e = r->entry;
  1824. r->counter_map.maptype = COUNTER_MAP_SET;
  1825. memcpy(&e->counters, counters, sizeof(STRUCT_COUNTERS));
  1826. set_changed(handle);
  1827. return 1;
  1828. }
  1829. /* Creates a new chain. */
  1830. /* To create a chain, create two rules: error node and unconditional
  1831. * return. */
  1832. int
  1833. TC_CREATE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
  1834. {
  1835. static struct chain_head *c;
  1836. int capacity;
  1837. int exceeded;
  1838. iptc_fn = TC_CREATE_CHAIN;
  1839. /* find_label doesn't cover built-in targets: DROP, ACCEPT,
  1840. QUEUE, RETURN. */
  1841. if (iptcc_find_label(chain, handle)
  1842. || strcmp(chain, LABEL_DROP) == 0
  1843. || strcmp(chain, LABEL_ACCEPT) == 0
  1844. || strcmp(chain, LABEL_QUEUE) == 0
  1845. || strcmp(chain, LABEL_RETURN) == 0) {
  1846. DEBUGP("Chain `%s' already exists\n", chain);
  1847. errno = EEXIST;
  1848. return 0;
  1849. }
  1850. if (strlen(chain)+1 > sizeof(IPT_CHAINLABEL)) {
  1851. DEBUGP("Chain name `%s' too long\n", chain);
  1852. errno = EINVAL;
  1853. return 0;
  1854. }
  1855. c = iptcc_alloc_chain_head(chain, 0);
  1856. if (!c) {
  1857. DEBUGP("Cannot allocate memory for chain `%s'\n", chain);
  1858. errno = ENOMEM;
  1859. return 0;
  1860. }
  1861. handle->num_chains++; /* New user defined chain */
  1862. DEBUGP("Creating chain `%s'\n", chain);
  1863. iptc_insert_chain(handle, c); /* Insert sorted */
  1864. /* Inserting chains don't change the correctness of the chain
  1865. * index (except if its smaller than index[0], but that
  1866. * handled by iptc_insert_chain). It only causes longer lists
  1867. * in the buckets. Thus, only rebuild chain index when the
  1868. * capacity is exceed with CHAIN_INDEX_INSERT_MAX chains.
  1869. */
  1870. capacity = handle->chain_index_sz * CHAIN_INDEX_BUCKET_LEN;
  1871. exceeded = handle->num_chains - capacity;
  1872. if (exceeded > CHAIN_INDEX_INSERT_MAX) {
  1873. debug("Capacity(%d) exceeded(%d) rebuild (chains:%d)\n",
  1874. capacity, exceeded, handle->num_chains);
  1875. iptcc_chain_index_rebuild(handle);
  1876. }
  1877. set_changed(handle);
  1878. return 1;
  1879. }
  1880. /* Get the number of references to this chain. */
  1881. int
  1882. TC_GET_REFERENCES(unsigned int *ref, const IPT_CHAINLABEL chain,
  1883. struct xtc_handle *handle)
  1884. {
  1885. struct chain_head *c;
  1886. iptc_fn = TC_GET_REFERENCES;
  1887. if (!(c = iptcc_find_label(chain, handle))) {
  1888. errno = ENOENT;
  1889. return 0;
  1890. }
  1891. *ref = c->references;
  1892. return 1;
  1893. }
  1894. /* Deletes a chain. */
  1895. int
  1896. TC_DELETE_CHAIN(const IPT_CHAINLABEL chain, struct xtc_handle *handle)
  1897. {
  1898. unsigned int references;
  1899. struct chain_head *c;
  1900. iptc_fn = TC_DELETE_CHAIN;
  1901. if (!(c = iptcc_find_label(chain, handle))) {
  1902. DEBUGP("cannot find chain `%s'\n", chain);
  1903. errno = ENOENT;
  1904. return 0;
  1905. }
  1906. if (TC_BUILTIN(chain, handle)) {
  1907. DEBUGP("cannot remove builtin chain `%s'\n", chain);
  1908. errno = EINVAL;
  1909. return 0;
  1910. }
  1911. if (!TC_GET_REFERENCES(&references, chain, handle)) {
  1912. DEBUGP("cannot get references on chain `%s'\n", chain);
  1913. return 0;
  1914. }
  1915. if (references > 0) {
  1916. DEBUGP("chain `%s' still has references\n", chain);
  1917. errno = EMLINK;
  1918. return 0;
  1919. }
  1920. if (c->num_rules) {
  1921. DEBUGP("chain `%s' is not empty\n", chain);
  1922. errno = ENOTEMPTY;
  1923. return 0;
  1924. }
  1925. /* If we are about to delete the chain that is the current
  1926. * iterator, move chain iterator forward. */
  1927. if (c == handle->chain_iterator_cur)
  1928. iptcc_chain_iterator_advance(handle);
  1929. handle->num_chains--; /* One user defined chain deleted */
  1930. //list_del(&c->list); /* Done in iptcc_chain_index_delete_chain() */
  1931. iptcc_chain_index_delete_chain(c, handle);
  1932. free(c);
  1933. DEBUGP("chain `%s' deleted\n", chain);
  1934. set_changed(handle);
  1935. return 1;
  1936. }
  1937. /* Renames a chain. */
  1938. int TC_RENAME_CHAIN(const IPT_CHAINLABEL oldname,
  1939. const IPT_CHAINLABEL newname,
  1940. struct xtc_handle *handle)
  1941. {
  1942. struct chain_head *c;
  1943. iptc_fn = TC_RENAME_CHAIN;
  1944. /* find_label doesn't cover built-in targets: DROP, ACCEPT,
  1945. QUEUE, RETURN. */
  1946. if (iptcc_find_label(newname, handle)
  1947. || strcmp(newname, LABEL_DROP) == 0
  1948. || strcmp(newname, LABEL_ACCEPT) == 0
  1949. || strcmp(newname, LABEL_QUEUE) == 0
  1950. || strcmp(newname, LABEL_RETURN) == 0) {
  1951. errno = EEXIST;
  1952. return 0;
  1953. }
  1954. if (!(c = iptcc_find_label(oldname, handle))
  1955. || TC_BUILTIN(oldname, handle)) {
  1956. errno = ENOENT;
  1957. return 0;
  1958. }
  1959. if (strlen(newname)+1 > sizeof(IPT_CHAINLABEL)) {
  1960. errno = EINVAL;
  1961. return 0;
  1962. }
  1963. /* This only unlinks "c" from the list, thus no free(c) */
  1964. iptcc_chain_index_delete_chain(c, handle);
  1965. /* Change the name of the chain */
  1966. strncpy(c->name, newname, sizeof(IPT_CHAINLABEL));
  1967. /* Insert sorted into to list again */
  1968. iptc_insert_chain(handle, c);
  1969. set_changed(handle);
  1970. return 1;
  1971. }
  1972. /* Sets the policy on a built-in chain. */
  1973. int
  1974. TC_SET_POLICY(const IPT_CHAINLABEL chain,
  1975. const IPT_CHAINLABEL policy,
  1976. STRUCT_COUNTERS *counters,
  1977. struct xtc_handle *handle)
  1978. {
  1979. struct chain_head *c;
  1980. iptc_fn = TC_SET_POLICY;
  1981. if (!(c = iptcc_find_label(chain, handle))) {
  1982. DEBUGP("cannot find chain `%s'\n", chain);
  1983. errno = ENOENT;
  1984. return 0;
  1985. }
  1986. if (!iptcc_is_builtin(c)) {
  1987. DEBUGP("cannot set policy of userdefinedchain `%s'\n", chain);
  1988. errno = ENOENT;
  1989. return 0;
  1990. }
  1991. if (strcmp(policy, LABEL_ACCEPT) == 0)
  1992. c->verdict = -NF_ACCEPT - 1;
  1993. else if (strcmp(policy, LABEL_DROP) == 0)
  1994. c->verdict = -NF_DROP - 1;
  1995. else {
  1996. errno = EINVAL;
  1997. return 0;
  1998. }
  1999. if (counters) {
  2000. /* set byte and packet counters */
  2001. memcpy(&c->counters, counters, sizeof(STRUCT_COUNTERS));
  2002. c->counter_map.maptype = COUNTER_MAP_SET;
  2003. } else {
  2004. c->counter_map.maptype = COUNTER_MAP_NOMAP;
  2005. }
  2006. set_changed(handle);
  2007. return 1;
  2008. }
  2009. /* Without this, on gcc 2.7.2.3, we get:
  2010. libiptc.c: In function `TC_COMMIT':
  2011. libiptc.c:833: fixed or forbidden register was spilled.
  2012. This may be due to a compiler bug or to impossible asm
  2013. statements or clauses.
  2014. */
  2015. static void
  2016. subtract_counters(STRUCT_COUNTERS *answer,
  2017. const STRUCT_COUNTERS *a,
  2018. const STRUCT_COUNTERS *b)
  2019. {
  2020. answer->pcnt = a->pcnt - b->pcnt;
  2021. answer->bcnt = a->bcnt - b->bcnt;
  2022. }
  2023. static void counters_nomap(STRUCT_COUNTERS_INFO *newcounters, unsigned int idx)
  2024. {
  2025. newcounters->counters[idx] = ((STRUCT_COUNTERS) { 0, 0});
  2026. DEBUGP_C("NOMAP => zero\n");
  2027. }
  2028. static void counters_normal_map(STRUCT_COUNTERS_INFO *newcounters,
  2029. STRUCT_REPLACE *repl, unsigned int idx,
  2030. unsigned int mappos)
  2031. {
  2032. /* Original read: X.
  2033. * Atomic read on replacement: X + Y.
  2034. * Currently in kernel: Z.
  2035. * Want in kernel: X + Y + Z.
  2036. * => Add in X + Y
  2037. * => Add in replacement read.
  2038. */
  2039. newcounters->counters[idx] = repl->counters[mappos];
  2040. DEBUGP_C("NORMAL_MAP => mappos %u \n", mappos);
  2041. }
  2042. static void counters_map_zeroed(STRUCT_COUNTERS_INFO *newcounters,
  2043. STRUCT_REPLACE *repl, unsigned int idx,
  2044. unsigned int mappos, STRUCT_COUNTERS *counters)
  2045. {
  2046. /* Original read: X.
  2047. * Atomic read on replacement: X + Y.
  2048. * Currently in kernel: Z.
  2049. * Want in kernel: Y + Z.
  2050. * => Add in Y.
  2051. * => Add in (replacement read - original read).
  2052. */
  2053. subtract_counters(&newcounters->counters[idx],
  2054. &repl->counters[mappos],
  2055. counters);
  2056. DEBUGP_C("ZEROED => mappos %u\n", mappos);
  2057. }
  2058. static void counters_map_set(STRUCT_COUNTERS_INFO *newcounters,
  2059. unsigned int idx, STRUCT_COUNTERS *counters)
  2060. {
  2061. /* Want to set counter (iptables-restore) */
  2062. memcpy(&newcounters->counters[idx], counters,
  2063. sizeof(STRUCT_COUNTERS));
  2064. DEBUGP_C("SET\n");
  2065. }
  2066. int
  2067. TC_COMMIT(struct xtc_handle *handle)
  2068. {
  2069. /* Replace, then map back the counters. */
  2070. STRUCT_REPLACE *repl;
  2071. STRUCT_COUNTERS_INFO *newcounters;
  2072. struct chain_head *c;
  2073. int ret;
  2074. size_t counterlen;
  2075. int new_number;
  2076. unsigned int new_size;
  2077. iptc_fn = TC_COMMIT;
  2078. CHECK(*handle);
  2079. /* Don't commit if nothing changed. */
  2080. if (!handle->changed)
  2081. goto finished;
  2082. new_number = iptcc_compile_table_prep(handle, &new_size);
  2083. if (new_number < 0) {
  2084. errno = ENOMEM;
  2085. goto out_zero;
  2086. }
  2087. repl = malloc(sizeof(*repl) + new_size);
  2088. if (!repl) {
  2089. errno = ENOMEM;
  2090. goto out_zero;
  2091. }
  2092. memset(repl, 0, sizeof(*repl) + new_size);
  2093. #if 0
  2094. TC_DUMP_ENTRIES(*handle);
  2095. #endif
  2096. counterlen = sizeof(STRUCT_COUNTERS_INFO)
  2097. + sizeof(STRUCT_COUNTERS) * new_number;
  2098. /* These are the old counters we will get from kernel */
  2099. repl->counters = malloc(sizeof(STRUCT_COUNTERS)
  2100. * handle->info.num_entries);
  2101. if (!repl->counters) {
  2102. errno = ENOMEM;
  2103. goto out_free_repl;
  2104. }
  2105. /* These are the counters we're going to put back, later. */
  2106. newcounters = malloc(counterlen);
  2107. if (!newcounters) {
  2108. errno = ENOMEM;
  2109. goto out_free_repl_counters;
  2110. }
  2111. memset(newcounters, 0, counterlen);
  2112. strcpy(repl->name, handle->info.name);
  2113. repl->num_entries = new_number;
  2114. repl->size = new_size;
  2115. repl->num_counters = handle->info.num_entries;
  2116. repl->valid_hooks = handle->info.valid_hooks;
  2117. DEBUGP("num_entries=%u, size=%u, num_counters=%u\n",
  2118. repl->num_entries, repl->size, repl->num_counters);
  2119. ret = iptcc_compile_table(handle, repl);
  2120. if (ret < 0) {
  2121. errno = ret;
  2122. goto out_free_newcounters;
  2123. }
  2124. #ifdef IPTC_DEBUG2
  2125. {
  2126. int fd = open("/tmp/libiptc-so_set_replace.blob",
  2127. O_CREAT|O_WRONLY);
  2128. if (fd >= 0) {
  2129. write(fd, repl, sizeof(*repl) + repl->size);
  2130. close(fd);
  2131. }
  2132. }
  2133. #endif
  2134. ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_REPLACE, repl,
  2135. sizeof(*repl) + repl->size);
  2136. if (ret < 0)
  2137. goto out_free_newcounters;
  2138. /* Put counters back. */
  2139. strcpy(newcounters->name, handle->info.name);
  2140. newcounters->num_counters = new_number;
  2141. list_for_each_entry(c, &handle->chains, list) {
  2142. struct rule_head *r;
  2143. /* Builtin chains have their own counters */
  2144. if (iptcc_is_builtin(c)) {
  2145. DEBUGP("counter for chain-index %u: ", c->foot_index);
  2146. switch(c->counter_map.maptype) {
  2147. case COUNTER_MAP_NOMAP:
  2148. counters_nomap(newcounters, c->foot_index);
  2149. break;
  2150. case COUNTER_MAP_NORMAL_MAP:
  2151. counters_normal_map(newcounters, repl,
  2152. c->foot_index,
  2153. c->counter_map.mappos);
  2154. break;
  2155. case COUNTER_MAP_ZEROED:
  2156. counters_map_zeroed(newcounters, repl,
  2157. c->foot_index,
  2158. c->counter_map.mappos,
  2159. &c->counters);
  2160. break;
  2161. case COUNTER_MAP_SET:
  2162. counters_map_set(newcounters, c->foot_index,
  2163. &c->counters);
  2164. break;
  2165. }
  2166. }
  2167. list_for_each_entry(r, &c->rules, list) {
  2168. DEBUGP("counter for index %u: ", r->index);
  2169. switch (r->counter_map.maptype) {
  2170. case COUNTER_MAP_NOMAP:
  2171. counters_nomap(newcounters, r->index);
  2172. break;
  2173. case COUNTER_MAP_NORMAL_MAP:
  2174. counters_normal_map(newcounters, repl,
  2175. r->index,
  2176. r->counter_map.mappos);
  2177. break;
  2178. case COUNTER_MAP_ZEROED:
  2179. counters_map_zeroed(newcounters, repl,
  2180. r->index,
  2181. r->counter_map.mappos,
  2182. &r->entry->counters);
  2183. break;
  2184. case COUNTER_MAP_SET:
  2185. counters_map_set(newcounters, r->index,
  2186. &r->entry->counters);
  2187. break;
  2188. }
  2189. }
  2190. }
  2191. #ifdef IPTC_DEBUG2
  2192. {
  2193. int fd = open("/tmp/libiptc-so_set_add_counters.blob",
  2194. O_CREAT|O_WRONLY);
  2195. if (fd >= 0) {
  2196. write(fd, newcounters, counterlen);
  2197. close(fd);
  2198. }
  2199. }
  2200. #endif
  2201. ret = setsockopt(handle->sockfd, TC_IPPROTO, SO_SET_ADD_COUNTERS,
  2202. newcounters, counterlen);
  2203. if (ret < 0)
  2204. goto out_free_newcounters;
  2205. free(repl->counters);
  2206. free(repl);
  2207. free(newcounters);
  2208. finished:
  2209. return 1;
  2210. out_free_newcounters:
  2211. free(newcounters);
  2212. out_free_repl_counters:
  2213. free(repl->counters);
  2214. out_free_repl:
  2215. free(repl);
  2216. out_zero:
  2217. return 0;
  2218. }
  2219. /* Translates errno numbers into more human-readable form than strerror. */
  2220. const char *
  2221. TC_STRERROR(int err)
  2222. {
  2223. unsigned int i;
  2224. struct table_struct {
  2225. void *fn;
  2226. int err;
  2227. const char *message;
  2228. } table [] =
  2229. { { TC_INIT, EPERM, "Permission denied (you must be root)" },
  2230. { TC_INIT, EINVAL, "Module is wrong version" },
  2231. { TC_INIT, ENOENT,
  2232. "Table does not exist (do you need to insmod?)" },
  2233. { TC_DELETE_CHAIN, ENOTEMPTY, "Chain is not empty" },
  2234. { TC_DELETE_CHAIN, EINVAL, "Can't delete built-in chain" },
  2235. { TC_DELETE_CHAIN, EMLINK,
  2236. "Can't delete chain with references left" },
  2237. { TC_CREATE_CHAIN, EEXIST, "Chain already exists" },
  2238. { TC_INSERT_ENTRY, E2BIG, "Index of insertion too big" },
  2239. { TC_REPLACE_ENTRY, E2BIG, "Index of replacement too big" },
  2240. { TC_DELETE_NUM_ENTRY, E2BIG, "Index of deletion too big" },
  2241. { TC_READ_COUNTER, E2BIG, "Index of counter too big" },
  2242. { TC_ZERO_COUNTER, E2BIG, "Index of counter too big" },
  2243. { TC_INSERT_ENTRY, ELOOP, "Loop found in table" },
  2244. { TC_INSERT_ENTRY, EINVAL, "Target problem" },
  2245. /* ENOENT for DELETE probably means no matching rule */
  2246. { TC_DELETE_ENTRY, ENOENT,
  2247. "Bad rule (does a matching rule exist in that chain?)" },
  2248. { TC_SET_POLICY, ENOENT,
  2249. "Bad built-in chain name" },
  2250. { TC_SET_POLICY, EINVAL,
  2251. "Bad policy name" },
  2252. { NULL, 0, "Incompatible with this kernel" },
  2253. { NULL, ENOPROTOOPT, "iptables who? (do you need to insmod?)" },
  2254. { NULL, ENOSYS, "Will be implemented real soon. I promise ;)" },
  2255. { NULL, ENOMEM, "Memory allocation problem" },
  2256. { NULL, ENOENT, "No chain/target/match by that name" },
  2257. };
  2258. for (i = 0; i < sizeof(table)/sizeof(struct table_struct); i++) {
  2259. if ((!table[i].fn || table[i].fn == iptc_fn)
  2260. && table[i].err == err)
  2261. return table[i].message;
  2262. }
  2263. return strerror(err);
  2264. }
  2265. const struct xtc_ops TC_OPS = {
  2266. .commit = TC_COMMIT,
  2267. .free = TC_FREE,
  2268. .builtin = TC_BUILTIN,
  2269. .is_chain = TC_IS_CHAIN,
  2270. .flush_entries = TC_FLUSH_ENTRIES,
  2271. .create_chain = TC_CREATE_CHAIN,
  2272. .set_policy = TC_SET_POLICY,
  2273. .strerror = TC_STRERROR,
  2274. };