Merge "Return errors to caller" into tizen_5.5
[platform/core/connectivity/stc-manager.git] / src / helper / helper-iptables.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "stc-manager-gdbus.h"
18 #include "helper-iptables.h"
19
20 #define STC_IPTABLES_DBUS_SERVICE                    "net.stc.iptables"
21 #define STC_IPTABLES_DBUS_RULE_INTERFACE             STC_IPTABLES_DBUS_SERVICE ".rule"
22 #define STC_IPTABLES_DBUS_CHAIN_INTERFACE            STC_IPTABLES_DBUS_SERVICE ".chain"
23 #define STC_IPTABLES_DBUS_RULE_PATH                  "/net/stc/iptables/rule"
24 #define STC_IPTABLES_DBUS_CHAIN_PATH                 "/net/stc/iptables/chain"
25 #define STC_IPTABLES_DBUS_METHOD_IPT_ADD_CHAIN       "IptAddChain"
26 #define STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_CHAIN    "IptRemoveChain"
27 #define STC_IPTABLES_DBUS_METHOD_IPT_FLUSH_CHAIN     "IptFlushChain"
28 #define STC_IPTABLES_DBUS_METHOD_IP6T_ADD_CHAIN      "Ip6tAddChain"
29 #define STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_CHAIN   "Ip6tRemoveChain"
30 #define STC_IPTABLES_DBUS_METHOD_IP6T_FLUSH_CHAIN    "Ip6tFlushChain"
31 #define STC_IPTABLES_DBUS_METHOD_IPT_ADD_RULE        "IptAddRule"
32 #define STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_RULE     "IptRemoveRule"
33 #define STC_IPTABLES_DBUS_METHOD_IP6T_ADD_RULE       "Ip6tAddRule"
34 #define STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_RULE    "Ip6tRemoveRule"
35 #define STC_IPTABLES_DBUS_METHOD_IPT_ADD_LIST        "IptAddList"
36 #define STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_LIST     "IptRemoveList"
37 #define STC_IPTABLES_DBUS_METHOD_IP6T_ADD_LIST       "Ip6tAddList"
38 #define STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_LIST    "Ip6tRemoveList"
39
40 #define RULE_CHAIN      "chain"
41 #define RULE_DIRECTION  "direction"
42 #define RULE_IFNAME     "ifname"
43 #define RULE_CGROUP     "cgroup"
44 #define RULE_NFACCT     "nfacct"
45 #define RULE_TARGET     "target"
46 #define RULE_SIPTYPE    "s_ip_type"
47 #define RULE_SIP1       "s_ip1"
48 #define RULE_SIP2       "s_ip2"
49 #define RULE_DIPTYPE    "d_ip_type"
50 #define RULE_DIP1       "d_ip1"
51 #define RULE_DIP2       "d_ip2"
52
53 static void __add_rule_info_to_builder(GVariantBuilder *builder,
54                                        iptables_rule_s *rule)
55 {
56         if (builder == NULL || rule == NULL)
57                 return; //LCOV_EXCL_LINE
58
59         g_variant_builder_add(builder, "{sv}", RULE_CHAIN,
60                               g_variant_new_string(rule->chain));
61
62         g_variant_builder_add(builder, "{sv}", RULE_DIRECTION,
63                               g_variant_new_uint16(rule->direction));
64
65         if (rule->ifname)
66                 g_variant_builder_add(builder, "{sv}", RULE_IFNAME,
67                                       g_variant_new_string(rule->ifname));
68
69         if (rule->classid > 0)
70                 g_variant_builder_add(builder, "{sv}", RULE_CGROUP,
71                                       g_variant_new_uint32(rule->classid));
72
73         if (rule->nfacct_name)
74                 g_variant_builder_add(builder, "{sv}", RULE_NFACCT,
75                                       g_variant_new_string(rule->nfacct_name));
76
77         if (rule->target)
78                 g_variant_builder_add(builder, "{sv}", RULE_TARGET,
79                                       g_variant_new_string(rule->target));
80
81         g_variant_builder_add(builder, "{sv}", RULE_SIPTYPE,
82                                       g_variant_new_uint16(rule->s_iprange_type));
83
84         g_variant_builder_add(builder, "{sv}", RULE_DIPTYPE,
85                                       g_variant_new_uint16(rule->d_iprange_type));
86
87         if (rule->s_ip1.s_addr)
88                 g_variant_builder_add(builder, "{sv}", RULE_SIP1,
89                                       g_variant_new_uint32(rule->s_ip1.s_addr));
90
91         if (rule->s_ip2.s_addr)
92                 g_variant_builder_add(builder, "{sv}", RULE_SIP2,
93                                       g_variant_new_uint32(rule->s_ip2.s_addr));
94
95         if (rule->d_ip1.s_addr)
96                 g_variant_builder_add(builder, "{sv}", RULE_DIP1,
97                                       g_variant_new_uint32(rule->d_ip1.s_addr));
98
99         if (rule->d_ip2.s_addr)
100                 g_variant_builder_add(builder, "{sv}", RULE_DIP2,
101                                       g_variant_new_uint32(rule->d_ip2.s_addr));
102 }
103
104 static void __add_rule_reply(
105                         GObject *source_object, GAsyncResult *res, gpointer user_data)
106 {
107         GDBusConnection *conn = NULL;
108         GVariant *dbus_data = NULL;
109         GError *dbus_error = NULL;
110         int result = 0;
111         char *nfacct_name = user_data;
112
113         conn = G_DBUS_CONNECTION(source_object);
114         dbus_data = g_dbus_connection_call_finish(conn, res, &dbus_error);
115         if (dbus_error != NULL) {
116                 STC_LOGE("Dbus reply error [%s]", dbus_error->message);
117                 g_error_free(dbus_error);
118         } else {
119                 g_variant_get(dbus_data, "(i)", &result);
120                 STC_LOGI("Added rule [%d:%s]", result, nfacct_name);
121         }
122
123         g_free(nfacct_name);
124 }
125
126 static void __remove_rule_reply(
127                         GObject *source_object, GAsyncResult *res, gpointer user_data)
128 {
129         GDBusConnection *conn = NULL;
130         GVariant *dbus_data = NULL;
131         GError *dbus_error = NULL;
132         int result = 0;
133         char *nfacct_name = user_data;
134
135         conn = G_DBUS_CONNECTION(source_object);
136         dbus_data = g_dbus_connection_call_finish(conn, res, &dbus_error);
137         if (dbus_error != NULL) {
138                 STC_LOGE("Dbus reply error [%s]", dbus_error->message);
139                 g_error_free(dbus_error);
140         } else {
141                 g_variant_get(dbus_data, "(i)", &result);
142                 STC_LOGI("Removed rule [%d:%s]", result, nfacct_name);
143         }
144
145         g_free(nfacct_name);
146 }
147
148 static void __add_list_info_to_builder(GVariantBuilder *builder,
149                                 GSList *iptables_list)
150 {
151         GSList *list;
152         GVariantBuilder sub_builder;
153
154         for (list = iptables_list; list; list = list->next) {
155                 iptables_rule_s *rule = list->data;
156
157                 g_variant_builder_init(&sub_builder, G_VARIANT_TYPE("a{sv}"));
158
159                 g_variant_builder_add(&sub_builder, "{sv}", RULE_CHAIN,
160                               g_variant_new_string(rule->chain));
161
162                 g_variant_builder_add(&sub_builder, "{sv}", RULE_DIRECTION,
163                                       g_variant_new_uint16(rule->direction));
164
165                 if (rule->ifname)
166                         g_variant_builder_add(&sub_builder, "{sv}", RULE_IFNAME,
167                                               g_variant_new_string(rule->ifname));
168
169                 if (rule->classid > 0)
170                         g_variant_builder_add(&sub_builder, "{sv}", RULE_CGROUP,
171                                               g_variant_new_uint32(rule->classid));
172
173                 if (rule->nfacct_name)
174                         g_variant_builder_add(&sub_builder, "{sv}", RULE_NFACCT,
175                                               g_variant_new_string(rule->nfacct_name));
176
177                 if (rule->target)
178                         g_variant_builder_add(&sub_builder, "{sv}", RULE_TARGET,
179                                               g_variant_new_string(rule->target));
180
181                 g_variant_builder_add_value(builder, g_variant_builder_end(&sub_builder));
182         }
183 }
184
185 static int __iptables_rule_add(GDBusConnection *connection,
186                                iptables_rule_s *rule)
187 {
188         int result = STC_ERROR_NONE;
189         GVariantBuilder *builder = NULL;
190         GVariant *params = NULL;
191         char *nfacct_name = NULL;
192
193         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
194         __add_rule_info_to_builder(builder, rule);
195         params = g_variant_new("(a{sv})", builder);
196         g_variant_builder_unref(builder);
197
198         nfacct_name = g_strdup_printf("4:%s", rule->nfacct_name);
199
200         result = stc_manager_gdbus_call_async(connection,
201                                               STC_IPTABLES_DBUS_SERVICE,
202                                               STC_IPTABLES_DBUS_RULE_PATH,
203                                               STC_IPTABLES_DBUS_RULE_INTERFACE,
204                                               STC_IPTABLES_DBUS_METHOD_IPT_ADD_RULE,
205                                               params,
206                                               __add_rule_reply,
207                                               nfacct_name);
208
209         if (result != STC_ERROR_NONE) {
210                 STC_LOGE("Failed to invoke dbus method async");
211                 g_free(nfacct_name);
212         }
213
214         return result;
215 }
216
217 static int __iptables_rule_remove(GDBusConnection *connection,
218                                   iptables_rule_s *rule)
219 {
220         int result = STC_ERROR_NONE;
221         GVariantBuilder *builder = NULL;
222         GVariant *params = NULL;
223         char *nfacct_name = NULL;
224
225         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
226         __add_rule_info_to_builder(builder, rule);
227         params = g_variant_new("(a{sv})", builder);
228         g_variant_builder_unref(builder);
229
230         nfacct_name = g_strdup_printf("4:%s", rule->nfacct_name);
231
232         result = stc_manager_gdbus_call_async(connection,
233                                               STC_IPTABLES_DBUS_SERVICE,
234                                               STC_IPTABLES_DBUS_RULE_PATH,
235                                               STC_IPTABLES_DBUS_RULE_INTERFACE,
236                                               STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_RULE,
237                                               params,
238                                               __remove_rule_reply,
239                                               nfacct_name);
240
241         if (result != STC_ERROR_NONE) {
242                 STC_LOGE("Failed to invoke dbus method async");
243                 g_free(nfacct_name);
244         }
245
246         return result;
247 }
248
249 static int __ip6tables_rule_add(GDBusConnection *connection,
250                                 iptables_rule_s *rule)
251 {
252         int result = STC_ERROR_NONE;
253         GVariantBuilder *builder = NULL;
254         GVariant *params = NULL;
255         char *nfacct_name = NULL;
256
257         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
258         __add_rule_info_to_builder(builder, rule);
259         params = g_variant_new("(a{sv})", builder);
260         g_variant_builder_unref(builder);
261
262         nfacct_name = g_strdup_printf("6:%s", rule->nfacct_name);
263
264         result = stc_manager_gdbus_call_async(connection,
265                                               STC_IPTABLES_DBUS_SERVICE,
266                                               STC_IPTABLES_DBUS_RULE_PATH,
267                                               STC_IPTABLES_DBUS_RULE_INTERFACE,
268                                               STC_IPTABLES_DBUS_METHOD_IP6T_ADD_RULE,
269                                               params,
270                                               __add_rule_reply,
271                                               nfacct_name);
272
273         if (result != STC_ERROR_NONE) {
274                 STC_LOGE("Failed to invoke dbus method async");
275                 g_free(nfacct_name);
276         }
277
278         return result;
279 }
280
281 static int __ip6tables_rule_remove(GDBusConnection *connection,
282                                    iptables_rule_s *rule)
283 {
284         int result = STC_ERROR_NONE;
285         GVariantBuilder *builder = NULL;
286         GVariant *params = NULL;
287         char *nfacct_name = NULL;
288
289         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
290         __add_rule_info_to_builder(builder, rule);
291         params = g_variant_new("(a{sv})", builder);
292         g_variant_builder_unref(builder);
293
294         nfacct_name = g_strdup_printf("6:%s", rule->nfacct_name);
295
296         result = stc_manager_gdbus_call_async(connection,
297                                               STC_IPTABLES_DBUS_SERVICE,
298                                               STC_IPTABLES_DBUS_RULE_PATH,
299                                               STC_IPTABLES_DBUS_RULE_INTERFACE,
300                                               STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_RULE,
301                                               params,
302                                               __remove_rule_reply,
303                                               nfacct_name);
304
305         if (result != STC_ERROR_NONE) {
306                 STC_LOGE("Failed to invoke dbus method async");
307                 g_free(nfacct_name);
308         }
309
310         return result;
311 }
312
313 static int __iptables_list_add(GDBusConnection *connection,
314                                 GSList *iptables_list, iptables_ip_type_e iptype)
315 {
316         stc_error_e result = STC_ERROR_NONE;
317         GVariantBuilder *builder = NULL;
318         GVariant *params = NULL;
319         GVariant *message = NULL;
320         const char *method = (iptype == IP_TYPE_IPV4) ?
321                 STC_IPTABLES_DBUS_METHOD_IPT_ADD_LIST :
322                 STC_IPTABLES_DBUS_METHOD_IP6T_ADD_LIST;
323
324         builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
325         __add_list_info_to_builder(builder, iptables_list);
326         params = g_variant_new("(aa{sv})", builder);
327         g_variant_builder_unref(builder);
328
329         message = stc_manager_gdbus_call_sync(connection,
330                                               STC_IPTABLES_DBUS_SERVICE,
331                                               STC_IPTABLES_DBUS_RULE_PATH,
332                                               STC_IPTABLES_DBUS_RULE_INTERFACE,
333                                               method,
334                                               params);
335
336         if (message == NULL) {
337                 STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE
338                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
339         }
340
341         g_variant_get(message, "(i)", &result);
342
343         STC_LOGD("%s to add list [%s:%d]",
344                 result == STC_ERROR_NONE ? "Successed" : "Failed",
345                 iptype == IP_TYPE_IPV4 ? "IPv4" : "IPv6", result);
346
347         g_variant_unref(message);
348         return result;
349 }
350
351 static int __iptables_list_remove(GDBusConnection *connection,
352                                 GSList *iptables_list, iptables_ip_type_e iptype)
353 {
354         int result = 0;
355         GVariantBuilder *builder = NULL;
356         GVariant *params = NULL;
357         GVariant *message = NULL;
358         const char *method = (iptype == IP_TYPE_IPV4) ?
359                 STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_LIST :
360                 STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_LIST;
361
362         builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
363         __add_list_info_to_builder(builder, iptables_list);
364         params = g_variant_new("(aa{sv})", builder);
365         g_variant_builder_unref(builder);
366
367         message = stc_manager_gdbus_call_sync(connection,
368                                               STC_IPTABLES_DBUS_SERVICE,
369                                               STC_IPTABLES_DBUS_RULE_PATH,
370                                               STC_IPTABLES_DBUS_RULE_INTERFACE,
371                                               method,
372                                               params);
373
374         if (message == NULL) {
375                 STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE
376                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
377         }
378
379         g_variant_get(message, "(i)", &result);
380
381         STC_LOGD("%s to add list [%s:%d]",
382                 result == STC_ERROR_NONE ? "Successed" : "Failed",
383                 iptype == IP_TYPE_IPV4 ? "IPv4" : "IPv6", result);
384
385         g_variant_unref(message);
386         return STC_ERROR_NONE;
387 }
388
389 static int __iptables_add_chain(GDBusConnection *connection,
390                                 const char *chain)
391 {
392         int result = 0;
393         GVariant *message = NULL;
394
395         message = stc_manager_gdbus_call_sync(connection,
396                                               STC_IPTABLES_DBUS_SERVICE,
397                                               STC_IPTABLES_DBUS_CHAIN_PATH,
398                                               STC_IPTABLES_DBUS_CHAIN_INTERFACE,
399                                               STC_IPTABLES_DBUS_METHOD_IPT_ADD_CHAIN,
400                                               g_variant_new("(s)", chain));
401
402         if (message == NULL) {
403                 STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE
404                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
405         }
406
407         g_variant_get(message, "(i)", &result);
408         STC_LOGD("Successfully added ipv4 chain [%d:%s]", result, chain);
409         g_variant_unref(message);
410
411         return STC_ERROR_NONE;
412 }
413
414 static int __ip6tables_add_chain(GDBusConnection *connection,
415                                  const char *chain)
416 {
417         int result = 0;
418         GVariant *message = NULL;
419
420         message = stc_manager_gdbus_call_sync(connection,
421                                               STC_IPTABLES_DBUS_SERVICE,
422                                               STC_IPTABLES_DBUS_CHAIN_PATH,
423                                               STC_IPTABLES_DBUS_CHAIN_INTERFACE,
424                                               STC_IPTABLES_DBUS_METHOD_IP6T_ADD_CHAIN,
425                                               g_variant_new("(s)", chain));
426
427         if (message == NULL) {
428                 STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE
429                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
430         }
431
432         g_variant_get(message, "(i)", &result);
433         STC_LOGD("Successfully added ipv6 chain [%d:%s]", result, chain);
434         g_variant_unref(message);
435
436         return STC_ERROR_NONE;
437 }
438
439 static int __iptables_remove_chain(GDBusConnection *connection,
440                                    const char *chain)
441 {
442         int result = 0;
443         GVariant *message = NULL;
444
445         message = stc_manager_gdbus_call_sync(connection,
446                                               STC_IPTABLES_DBUS_SERVICE,
447                                               STC_IPTABLES_DBUS_CHAIN_PATH,
448                                               STC_IPTABLES_DBUS_CHAIN_INTERFACE,
449                                               STC_IPTABLES_DBUS_METHOD_IPT_REMOVE_CHAIN,
450                                               g_variant_new("(s)", chain));
451
452         if (message == NULL) {
453                 STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE
454                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
455         }
456
457         g_variant_get(message, "(i)", &result);
458         STC_LOGD("Successfully removed ipv4 chain [%d:%s]", result, chain);
459         g_variant_unref(message);
460
461         return STC_ERROR_NONE;
462 }
463
464 static int __ip6tables_remove_chain(GDBusConnection *connection,
465                                     const char *chain)
466 {
467         int result = 0;
468         GVariant *message = NULL;
469
470         message = stc_manager_gdbus_call_sync(connection,
471                                               STC_IPTABLES_DBUS_SERVICE,
472                                               STC_IPTABLES_DBUS_CHAIN_PATH,
473                                               STC_IPTABLES_DBUS_CHAIN_INTERFACE,
474                                               STC_IPTABLES_DBUS_METHOD_IP6T_REMOVE_CHAIN,
475                                               g_variant_new("(s)", chain));
476
477         if (message == NULL) {
478                 STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE
479                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
480         }
481
482         g_variant_get(message, "(i)", &result);
483         STC_LOGD("Successfully removed ipv6 chain [%d:%s]", result, chain);
484         g_variant_unref(message);
485
486         return STC_ERROR_NONE;
487 }
488
489 static int __iptables_flush_chain(GDBusConnection *connection,
490                                   const char *chain)
491 {
492         int result = 0;
493         GVariant *message = NULL;
494
495         message = stc_manager_gdbus_call_sync(connection,
496                                               STC_IPTABLES_DBUS_SERVICE,
497                                               STC_IPTABLES_DBUS_CHAIN_PATH,
498                                               STC_IPTABLES_DBUS_CHAIN_INTERFACE,
499                                               STC_IPTABLES_DBUS_METHOD_IPT_FLUSH_CHAIN,
500                                               g_variant_new("(s)", chain));
501
502         if (message == NULL) {
503                 STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE
504                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
505         }
506
507         g_variant_get(message, "(i)", &result);
508         STC_LOGD("Successfully flushed ipv4 chain [%d:%s]", result, chain);
509         g_variant_unref(message);
510
511         return STC_ERROR_NONE;
512 }
513
514 static int __ip6tables_flush_chain(GDBusConnection *connection,
515                                    const char *chain)
516 {
517         int result = 0;
518         GVariant *message = NULL;
519
520         message = stc_manager_gdbus_call_sync(connection,
521                                               STC_IPTABLES_DBUS_SERVICE,
522                                               STC_IPTABLES_DBUS_CHAIN_PATH,
523                                               STC_IPTABLES_DBUS_CHAIN_INTERFACE,
524                                               STC_IPTABLES_DBUS_METHOD_IP6T_FLUSH_CHAIN,
525                                               g_variant_new("(s)", chain));
526
527         if (message == NULL) {
528                 STC_LOGE("Failed to invoke dbus method"); //LCOV_EXCL_LINE
529                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
530         }
531
532         g_variant_get(message, "(i)", &result);
533         STC_LOGD("Successfully flushed ipv6 chain [%d:%s]", result, chain);
534         g_variant_unref(message);
535
536         return STC_ERROR_NONE;
537 }
538
539 static int __iptables_add_chain_jump_rule(const char *chain,
540                                           const char *target)
541 {
542         stc_error_e ret = STC_ERROR_NONE;
543         iptables_rule_s iptables_rule;
544         memset(&iptables_rule, 0, sizeof(iptables_rule_s));
545
546         iptables_rule.target = g_strdup(target);
547         iptables_rule.chain = g_strdup(chain);
548
549         ret = iptables_add(&iptables_rule, IP_TYPE_IPV4_IPV6);
550
551         g_free(iptables_rule.target);
552         g_free(iptables_rule.chain);
553
554         return ret;
555 }
556
557 static stc_error_e _iptables_add_in_chain(stc_s *stc)
558 {
559         stc_error_e ret = STC_ERROR_NONE;
560
561         ret = __iptables_add_chain(stc->connection, STC_IN_CHAIN);
562         if (ret != STC_ERROR_NONE)
563                 goto done; //LCOV_EXCL_LINE
564
565         ret = __iptables_add_chain(stc->connection, STC_IN_DROP_CHAIN);
566         if (ret != STC_ERROR_NONE)
567                 goto done; //LCOV_EXCL_LINE
568
569         ret = __iptables_add_chain(stc->connection, STC_IN_FG_CHAIN);
570         if (ret != STC_ERROR_NONE)
571                 goto done; //LCOV_EXCL_LINE
572
573         ret = __iptables_add_chain(stc->connection, STC_IN_ACCEPT_CHAIN);
574         if (ret != STC_ERROR_NONE)
575                 goto done; //LCOV_EXCL_LINE
576
577         ret = __iptables_add_chain(stc->connection, STC_IN_BG_DROP_CHAIN);
578         if (ret != STC_ERROR_NONE)
579                 goto done; //LCOV_EXCL_LINE
580
581         ret = __iptables_add_chain(stc->connection, STC_IN_BG_CHAIN);
582         if (ret != STC_ERROR_NONE)
583                 goto done; //LCOV_EXCL_LINE
584
585 done:
586         return ret;
587 }
588
589 static stc_error_e _iptables_add_out_chain(stc_s *stc)
590 {
591         stc_error_e ret = STC_ERROR_NONE;
592
593         ret = __iptables_add_chain(stc->connection, STC_OUT_CHAIN);
594         if (ret != STC_ERROR_NONE)
595                 goto done; //LCOV_EXCL_LINE
596
597         ret = __iptables_add_chain(stc->connection, STC_OUT_DROP_CHAIN);
598         if (ret != STC_ERROR_NONE)
599                 goto done; //LCOV_EXCL_LINE
600
601         ret = __iptables_add_chain(stc->connection, STC_OUT_FG_CHAIN);
602         if (ret != STC_ERROR_NONE)
603                 goto done; //LCOV_EXCL_LINE
604
605         ret = __iptables_add_chain(stc->connection, STC_OUT_ACCEPT_CHAIN);
606         if (ret != STC_ERROR_NONE)
607                 goto done; //LCOV_EXCL_LINE
608
609         ret = __iptables_add_chain(stc->connection, STC_OUT_BG_DROP_CHAIN);
610         if (ret != STC_ERROR_NONE)
611                 goto done; //LCOV_EXCL_LINE
612
613         ret = __iptables_add_chain(stc->connection, STC_OUT_BG_CHAIN);
614         if (ret != STC_ERROR_NONE)
615                 goto done; //LCOV_EXCL_LINE
616
617 done:
618         return ret;
619 }
620
621 static stc_error_e _ip6tables_add_in_chain(stc_s *stc)
622 {
623         stc_error_e ret = STC_ERROR_NONE;
624
625         ret = __ip6tables_add_chain(stc->connection, STC_IN_CHAIN);
626         if (ret != STC_ERROR_NONE)
627                 goto done; //LCOV_EXCL_LINE
628
629         ret = __ip6tables_add_chain(stc->connection, STC_IN_DROP_CHAIN);
630         if (ret != STC_ERROR_NONE)
631                 goto done; //LCOV_EXCL_LINE
632
633         ret = __ip6tables_add_chain(stc->connection, STC_IN_FG_CHAIN);
634         if (ret != STC_ERROR_NONE)
635                 goto done; //LCOV_EXCL_LINE
636
637         ret = __ip6tables_add_chain(stc->connection, STC_IN_ACCEPT_CHAIN);
638         if (ret != STC_ERROR_NONE)
639                 goto done; //LCOV_EXCL_LINE
640
641         ret = __ip6tables_add_chain(stc->connection, STC_IN_BG_DROP_CHAIN);
642         if (ret != STC_ERROR_NONE)
643                 goto done; //LCOV_EXCL_LINE
644
645         ret = __ip6tables_add_chain(stc->connection, STC_IN_BG_CHAIN);
646         if (ret != STC_ERROR_NONE)
647                 goto done; //LCOV_EXCL_LINE
648
649 done:
650         return ret;
651 }
652
653 static stc_error_e _ip6tables_add_out_chain(stc_s *stc)
654 {
655         stc_error_e ret = STC_ERROR_NONE;
656
657         ret = __ip6tables_add_chain(stc->connection, STC_OUT_CHAIN);
658         if (ret != STC_ERROR_NONE)
659                 goto done; //LCOV_EXCL_LINE
660
661         ret = __ip6tables_add_chain(stc->connection, STC_OUT_DROP_CHAIN);
662         if (ret != STC_ERROR_NONE)
663                 goto done; //LCOV_EXCL_LINE
664
665         ret = __ip6tables_add_chain(stc->connection, STC_OUT_FG_CHAIN);
666         if (ret != STC_ERROR_NONE)
667                 goto done; //LCOV_EXCL_LINE
668
669         ret = __ip6tables_add_chain(stc->connection, STC_OUT_ACCEPT_CHAIN);
670         if (ret != STC_ERROR_NONE)
671                 goto done; //LCOV_EXCL_LINE
672
673         ret = __ip6tables_add_chain(stc->connection, STC_OUT_BG_DROP_CHAIN);
674         if (ret != STC_ERROR_NONE)
675                 goto done; //LCOV_EXCL_LINE
676
677         ret = __ip6tables_add_chain(stc->connection, STC_OUT_BG_CHAIN);
678         if (ret != STC_ERROR_NONE)
679                 goto done; //LCOV_EXCL_LINE
680
681 done:
682         return ret;
683 }
684
685 static stc_error_e _iptables_add_in_chain_jump_rule(void)
686 {
687         stc_error_e ret = STC_ERROR_NONE;
688
689         ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_CHAIN);
690         if (ret != STC_ERROR_NONE)
691                 goto done; //LCOV_EXCL_LINE
692
693         ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_DROP_CHAIN);
694         if (ret != STC_ERROR_NONE)
695                 goto done; //LCOV_EXCL_LINE
696
697         ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_FG_CHAIN);
698         if (ret != STC_ERROR_NONE)
699                 goto done; //LCOV_EXCL_LINE
700
701         ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_ACCEPT_CHAIN);
702         if (ret != STC_ERROR_NONE)
703                 goto done; //LCOV_EXCL_LINE
704
705         ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_BG_DROP_CHAIN);
706         if (ret != STC_ERROR_NONE)
707                 goto done; //LCOV_EXCL_LINE
708
709         ret = __iptables_add_chain_jump_rule("INPUT", STC_IN_BG_CHAIN);
710         if (ret != STC_ERROR_NONE)
711                 goto done; //LCOV_EXCL_LINE
712
713 done:
714         return ret;
715 }
716
717 static stc_error_e _iptables_add_out_chain_jump_rule(void)
718 {
719         stc_error_e ret = STC_ERROR_NONE;
720
721         ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_CHAIN);
722         if (ret != STC_ERROR_NONE)
723                 goto done; //LCOV_EXCL_LINE
724
725         ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_DROP_CHAIN);
726         if (ret != STC_ERROR_NONE)
727                 goto done; //LCOV_EXCL_LINE
728
729         ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_FG_CHAIN);
730         if (ret != STC_ERROR_NONE)
731                 goto done; //LCOV_EXCL_LINE
732
733         ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_ACCEPT_CHAIN);
734         if (ret != STC_ERROR_NONE)
735                 goto done; //LCOV_EXCL_LINE
736
737         ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_BG_DROP_CHAIN);
738         if (ret != STC_ERROR_NONE)
739                 goto done; //LCOV_EXCL_LINE
740
741         ret = __iptables_add_chain_jump_rule("OUTPUT", STC_OUT_BG_CHAIN);
742         if (ret != STC_ERROR_NONE)
743                 goto done; //LCOV_EXCL_LINE
744
745 done:
746         return ret;
747 }
748
749 static stc_error_e _iptables_remove_in_chain(stc_s *stc)
750 {
751         stc_error_e ret = STC_ERROR_NONE;
752
753         ret = __iptables_remove_chain(stc->connection, STC_IN_DROP_CHAIN);
754         if (ret != STC_ERROR_NONE)
755                 goto done; //LCOV_EXCL_LINE
756
757         ret = __iptables_remove_chain(stc->connection, STC_IN_FG_CHAIN);
758         if (ret != STC_ERROR_NONE)
759                 goto done; //LCOV_EXCL_LINE
760
761         ret = __iptables_remove_chain(stc->connection, STC_IN_ACCEPT_CHAIN);
762         if (ret != STC_ERROR_NONE)
763                 goto done; //LCOV_EXCL_LINE
764
765         ret = __iptables_remove_chain(stc->connection, STC_IN_BG_DROP_CHAIN);
766         if (ret != STC_ERROR_NONE)
767                 goto done; //LCOV_EXCL_LINE
768
769         ret = __iptables_remove_chain(stc->connection, STC_IN_BG_CHAIN);
770         if (ret != STC_ERROR_NONE)
771                 goto done; //LCOV_EXCL_LINE
772
773         ret = __iptables_remove_chain(stc->connection, STC_IN_CHAIN);
774         if (ret != STC_ERROR_NONE)
775                 goto done; //LCOV_EXCL_LINE
776
777 done:
778         return ret;
779 }
780
781 static stc_error_e _iptables_remove_out_chain(stc_s *stc)
782 {
783         stc_error_e ret = STC_ERROR_NONE;
784
785         ret = __iptables_remove_chain(stc->connection, STC_OUT_DROP_CHAIN);
786         if (ret != STC_ERROR_NONE)
787                 goto done; //LCOV_EXCL_LINE
788
789         ret = __iptables_remove_chain(stc->connection, STC_OUT_FG_CHAIN);
790         if (ret != STC_ERROR_NONE)
791                 goto done; //LCOV_EXCL_LINE
792
793         ret = __iptables_remove_chain(stc->connection, STC_OUT_ACCEPT_CHAIN);
794         if (ret != STC_ERROR_NONE)
795                 goto done; //LCOV_EXCL_LINE
796
797         ret = __iptables_remove_chain(stc->connection, STC_OUT_BG_DROP_CHAIN);
798         if (ret != STC_ERROR_NONE)
799                 goto done; //LCOV_EXCL_LINE
800
801         ret = __iptables_remove_chain(stc->connection, STC_OUT_BG_CHAIN);
802         if (ret != STC_ERROR_NONE)
803                 goto done; //LCOV_EXCL_LINE
804
805         ret = __iptables_remove_chain(stc->connection, STC_OUT_CHAIN);
806         if (ret != STC_ERROR_NONE)
807                 goto done; //LCOV_EXCL_LINE
808
809 done:
810         return ret;
811 }
812
813 static stc_error_e _ip6tables_remove_in_chain(stc_s *stc)
814 {
815         stc_error_e ret = STC_ERROR_NONE;
816
817         ret = __ip6tables_remove_chain(stc->connection, STC_IN_DROP_CHAIN);
818         if (ret != STC_ERROR_NONE)
819                 goto done; //LCOV_EXCL_LINE
820
821         ret = __ip6tables_remove_chain(stc->connection, STC_IN_FG_CHAIN);
822         if (ret != STC_ERROR_NONE)
823                 goto done; //LCOV_EXCL_LINE
824
825         ret = __ip6tables_remove_chain(stc->connection, STC_IN_ACCEPT_CHAIN);
826         if (ret != STC_ERROR_NONE)
827                 goto done; //LCOV_EXCL_LINE
828
829         ret = __ip6tables_remove_chain(stc->connection, STC_IN_BG_DROP_CHAIN);
830         if (ret != STC_ERROR_NONE)
831                 goto done; //LCOV_EXCL_LINE
832
833         ret = __ip6tables_remove_chain(stc->connection, STC_IN_BG_CHAIN);
834         if (ret != STC_ERROR_NONE)
835                 goto done; //LCOV_EXCL_LINE
836
837         ret = __ip6tables_remove_chain(stc->connection, STC_IN_CHAIN);
838         if (ret != STC_ERROR_NONE)
839                 goto done; //LCOV_EXCL_LINE
840
841 done:
842         return ret;
843 }
844
845 static stc_error_e _ip6tables_remove_out_chain(stc_s *stc)
846 {
847         stc_error_e ret = STC_ERROR_NONE;
848
849         ret = __ip6tables_remove_chain(stc->connection, STC_OUT_DROP_CHAIN);
850         if (ret != STC_ERROR_NONE)
851                 goto done; //LCOV_EXCL_LINE
852
853         ret = __ip6tables_remove_chain(stc->connection, STC_OUT_FG_CHAIN);
854         if (ret != STC_ERROR_NONE)
855                 goto done; //LCOV_EXCL_LINE
856
857         ret = __ip6tables_remove_chain(stc->connection, STC_OUT_ACCEPT_CHAIN);
858         if (ret != STC_ERROR_NONE)
859                 goto done; //LCOV_EXCL_LINE
860
861         ret = __ip6tables_remove_chain(stc->connection, STC_OUT_BG_DROP_CHAIN);
862         if (ret != STC_ERROR_NONE)
863                 goto done; //LCOV_EXCL_LINE
864
865         ret = __ip6tables_remove_chain(stc->connection, STC_OUT_BG_CHAIN);
866         if (ret != STC_ERROR_NONE)
867                 goto done; //LCOV_EXCL_LINE
868
869         ret = __ip6tables_remove_chain(stc->connection, STC_OUT_CHAIN);
870         if (ret != STC_ERROR_NONE)
871                 goto done; //LCOV_EXCL_LINE
872
873 done:
874         return ret;
875 }
876
877 static stc_error_e _iptables_flush_in_chain(stc_s *stc)
878 {
879         stc_error_e ret = STC_ERROR_NONE;
880
881         ret = __iptables_flush_chain(stc->connection, STC_IN_DROP_CHAIN);
882         if (ret != STC_ERROR_NONE)
883                 goto done; //LCOV_EXCL_LINE
884
885         ret = __iptables_flush_chain(stc->connection, STC_IN_FG_CHAIN);
886         if (ret != STC_ERROR_NONE)
887                 goto done; //LCOV_EXCL_LINE
888
889         ret = __iptables_flush_chain(stc->connection, STC_IN_ACCEPT_CHAIN);
890         if (ret != STC_ERROR_NONE)
891                 goto done; //LCOV_EXCL_LINE
892
893         ret = __iptables_flush_chain(stc->connection, STC_IN_BG_DROP_CHAIN);
894         if (ret != STC_ERROR_NONE)
895                 goto done; //LCOV_EXCL_LINE
896
897         ret = __iptables_flush_chain(stc->connection, STC_IN_BG_CHAIN);
898         if (ret != STC_ERROR_NONE)
899                 goto done; //LCOV_EXCL_LINE
900
901         ret = __iptables_flush_chain(stc->connection, STC_IN_CHAIN);
902         if (ret != STC_ERROR_NONE)
903                 goto done; //LCOV_EXCL_LINE
904
905 done:
906         return ret;
907 }
908
909 static stc_error_e _iptables_flush_out_chain(stc_s *stc)
910 {
911         stc_error_e ret = STC_ERROR_NONE;
912
913         ret = __iptables_flush_chain(stc->connection, STC_OUT_DROP_CHAIN);
914         if (ret != STC_ERROR_NONE)
915                 goto done; //LCOV_EXCL_LINE
916
917         ret = __iptables_flush_chain(stc->connection, STC_OUT_FG_CHAIN);
918         if (ret != STC_ERROR_NONE)
919                 goto done; //LCOV_EXCL_LINE
920
921         ret = __iptables_flush_chain(stc->connection, STC_OUT_ACCEPT_CHAIN);
922         if (ret != STC_ERROR_NONE)
923                 goto done; //LCOV_EXCL_LINE
924
925         ret = __iptables_flush_chain(stc->connection, STC_OUT_BG_DROP_CHAIN);
926         if (ret != STC_ERROR_NONE)
927                 goto done; //LCOV_EXCL_LINE
928
929         ret = __iptables_flush_chain(stc->connection, STC_OUT_BG_CHAIN);
930         if (ret != STC_ERROR_NONE)
931                 goto done; //LCOV_EXCL_LINE
932
933         ret = __iptables_flush_chain(stc->connection, STC_OUT_CHAIN);
934         if (ret != STC_ERROR_NONE)
935                 goto done; //LCOV_EXCL_LINE
936
937 done:
938         return ret;
939 }
940
941 static stc_error_e _ip6tables_flush_in_chain(stc_s *stc)
942 {
943         stc_error_e ret = STC_ERROR_NONE;
944
945         ret = __ip6tables_flush_chain(stc->connection, STC_IN_DROP_CHAIN);
946         if (ret != STC_ERROR_NONE)
947                 goto done; //LCOV_EXCL_LINE
948
949         ret = __ip6tables_flush_chain(stc->connection, STC_IN_FG_CHAIN);
950         if (ret != STC_ERROR_NONE)
951                 goto done; //LCOV_EXCL_LINE
952
953         ret = __ip6tables_flush_chain(stc->connection, STC_IN_ACCEPT_CHAIN);
954         if (ret != STC_ERROR_NONE)
955                 goto done; //LCOV_EXCL_LINE
956
957         ret = __ip6tables_flush_chain(stc->connection, STC_IN_BG_DROP_CHAIN);
958         if (ret != STC_ERROR_NONE)
959                 goto done; //LCOV_EXCL_LINE
960
961         ret = __ip6tables_flush_chain(stc->connection, STC_IN_BG_CHAIN);
962         if (ret != STC_ERROR_NONE)
963                 goto done; //LCOV_EXCL_LINE
964
965         ret = __ip6tables_flush_chain(stc->connection, STC_IN_CHAIN);
966         if (ret != STC_ERROR_NONE)
967                 goto done; //LCOV_EXCL_LINE
968
969 done:
970         return ret;
971 }
972
973 static stc_error_e _ip6tables_flush_out_chain(stc_s *stc)
974 {
975         stc_error_e ret = STC_ERROR_NONE;
976
977         ret = __ip6tables_flush_chain(stc->connection, STC_OUT_DROP_CHAIN);
978         if (ret != STC_ERROR_NONE)
979                 goto done; //LCOV_EXCL_LINE
980
981         ret = __ip6tables_flush_chain(stc->connection, STC_OUT_FG_CHAIN);
982         if (ret != STC_ERROR_NONE)
983                 goto done; //LCOV_EXCL_LINE
984
985         ret = __ip6tables_flush_chain(stc->connection, STC_OUT_ACCEPT_CHAIN);
986         if (ret != STC_ERROR_NONE)
987                 goto done; //LCOV_EXCL_LINE
988
989         ret = __ip6tables_flush_chain(stc->connection, STC_OUT_BG_DROP_CHAIN);
990         if (ret != STC_ERROR_NONE)
991                 goto done; //LCOV_EXCL_LINE
992
993         ret = __ip6tables_flush_chain(stc->connection, STC_OUT_BG_CHAIN);
994         if (ret != STC_ERROR_NONE)
995                 goto done; //LCOV_EXCL_LINE
996
997         ret = __ip6tables_flush_chain(stc->connection, STC_OUT_CHAIN);
998         if (ret != STC_ERROR_NONE)
999                 goto done; //LCOV_EXCL_LINE
1000
1001 done:
1002         return ret;
1003 }
1004
1005 stc_error_e iptables_add(iptables_rule_s *rule, iptables_ip_type_e iptype)
1006 {
1007         stc_error_e ret = STC_ERROR_NONE;
1008         stc_s *stc = stc_get_manager();
1009
1010         if (!stc || !stc->connection)
1011                 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1012
1013         if (iptype == IP_TYPE_IPV4 ||
1014                 iptype == IP_TYPE_IPV4_IPV6) {
1015                 ret = __iptables_rule_add(stc->connection, rule);
1016                 if (ret != STC_ERROR_NONE)
1017                         goto done; //LCOV_EXCL_LINE
1018         }
1019
1020         if (iptype == IP_TYPE_IPV6 ||
1021                 iptype == IP_TYPE_IPV4_IPV6)
1022                 ret = __ip6tables_rule_add(stc->connection, rule);
1023
1024 done:
1025         return ret;
1026 }
1027
1028 stc_error_e iptables_remove(iptables_rule_s *rule, iptables_ip_type_e iptype)
1029 {
1030         stc_error_e ret = STC_ERROR_NONE;
1031         stc_s *stc = stc_get_manager();
1032
1033         if (!stc || !stc->connection)
1034                 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1035
1036         if (iptype == IP_TYPE_IPV4 ||
1037                 iptype == IP_TYPE_IPV4_IPV6) {
1038                 ret = __iptables_rule_remove(stc->connection, rule);
1039                 if (ret != STC_ERROR_NONE)
1040                         goto done; //LCOV_EXCL_LINE
1041         }
1042
1043         if (iptype == IP_TYPE_IPV6 ||
1044                 iptype == IP_TYPE_IPV4_IPV6)
1045                 ret = __ip6tables_rule_remove(stc->connection, rule);
1046
1047 done:
1048         return ret;
1049 }
1050
1051 stc_error_e iptables_add_list(GSList *iptables_list, iptables_ip_type_e iptype)
1052 {
1053         stc_s *stc = stc_get_manager();
1054
1055         if (!stc || !stc->connection)
1056                 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1057
1058         return __iptables_list_add(stc->connection, iptables_list, iptype);
1059 }
1060
1061 stc_error_e iptables_remove_list(GSList *iptables_list, iptables_ip_type_e iptype)
1062 {
1063         stc_s *stc = stc_get_manager();
1064
1065         if (!stc || !stc->connection)
1066                 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1067
1068         return __iptables_list_remove(stc->connection, iptables_list, iptype);
1069 }
1070
1071 API stc_error_e iptables_flush_chains(void)
1072 {
1073         stc_error_e ret = STC_ERROR_NONE;
1074         stc_s *stc = stc_get_manager();
1075
1076         if (!stc || !stc->connection)
1077                 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1078
1079         ret = _iptables_flush_in_chain(stc);
1080         if (ret != STC_ERROR_NONE)
1081                 goto done; //LCOV_EXCL_LINE
1082
1083         ret = _iptables_flush_out_chain(stc);
1084         if (ret != STC_ERROR_NONE)
1085                 goto done; //LCOV_EXCL_LINE
1086
1087         ret = __iptables_flush_chain(stc->connection, STC_FRWD_CHAIN);
1088         if (ret != STC_ERROR_NONE)
1089                 goto done; //LCOV_EXCL_LINE
1090
1091         ret = __iptables_flush_chain(stc->connection, STC_TETHER_CHAIN);
1092         if (ret != STC_ERROR_NONE)
1093                 goto done; //LCOV_EXCL_LINE
1094
1095         ret = _ip6tables_flush_in_chain(stc);
1096         if (ret != STC_ERROR_NONE)
1097                 goto done; //LCOV_EXCL_LINE
1098
1099         ret = _ip6tables_flush_out_chain(stc);
1100         if (ret != STC_ERROR_NONE)
1101                 goto done; //LCOV_EXCL_LINE
1102
1103         ret = __ip6tables_flush_chain(stc->connection, STC_FRWD_CHAIN);
1104 done:
1105         return ret;
1106 }
1107
1108 stc_error_e iptables_init(void)
1109 {
1110         __STC_LOG_FUNC_ENTER__;
1111
1112         stc_error_e ret = STC_ERROR_NONE;
1113         stc_s *stc = stc_get_manager();
1114
1115         if (!stc || !stc->connection) {
1116                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1117                 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1118         }
1119
1120         ret = _iptables_add_in_chain(stc);
1121         if (ret != STC_ERROR_NONE) {
1122                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1123                 goto done; //LCOV_EXCL_LINE
1124         }
1125
1126         ret = _iptables_add_out_chain(stc);
1127         if (ret != STC_ERROR_NONE) {
1128                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1129                 goto done; //LCOV_EXCL_LINE
1130         }
1131
1132         ret = __iptables_add_chain(stc->connection, STC_FRWD_CHAIN);
1133         if (ret != STC_ERROR_NONE) {
1134                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1135                 goto done; //LCOV_EXCL_LINE
1136         }
1137
1138         ret = __iptables_add_chain(stc->connection, STC_TETHER_CHAIN);
1139         if (ret != STC_ERROR_NONE) {
1140                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1141                 goto done; //LCOV_EXCL_LINE
1142         }
1143
1144         ret = _ip6tables_add_in_chain(stc);
1145         if (ret != STC_ERROR_NONE) {
1146                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1147                 goto done; //LCOV_EXCL_LINE
1148         }
1149
1150         ret = _ip6tables_add_out_chain(stc);
1151         if (ret != STC_ERROR_NONE) {
1152                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1153                 goto done; //LCOV_EXCL_LINE
1154         }
1155
1156         ret = __ip6tables_add_chain(stc->connection, STC_FRWD_CHAIN);
1157         if (ret != STC_ERROR_NONE) {
1158                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1159                 goto done; //LCOV_EXCL_LINE
1160         }
1161
1162         ret = __ip6tables_add_chain(stc->connection, STC_TETHER_CHAIN);
1163         if (ret != STC_ERROR_NONE) {
1164                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1165                 goto done; //LCOV_EXCL_LINE
1166         }
1167
1168         ret = _iptables_add_in_chain_jump_rule();
1169         if (ret != STC_ERROR_NONE) {
1170                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1171                 goto done; //LCOV_EXCL_LINE
1172         }
1173
1174         ret = _iptables_add_out_chain_jump_rule();
1175         if (ret != STC_ERROR_NONE) {
1176                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1177                 goto done; //LCOV_EXCL_LINE
1178         }
1179
1180         ret = __iptables_add_chain_jump_rule("FORWARD", STC_FRWD_CHAIN);
1181         if (ret != STC_ERROR_NONE) {
1182                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1183                 goto done; //LCOV_EXCL_LINE
1184         }
1185
1186         ret = __iptables_add_chain_jump_rule("FORWARD", STC_TETHER_CHAIN);
1187         if (ret != STC_ERROR_NONE) {
1188                 __STC_LOG_FUNC_EXIT__;
1189                 goto done;
1190         }
1191
1192 done:
1193         __STC_LOG_FUNC_ENTER__;
1194         return ret;
1195 }
1196
1197 stc_error_e iptables_deinit(void)
1198 {
1199         __STC_LOG_FUNC_ENTER__;
1200
1201         stc_error_e ret = STC_ERROR_NONE;
1202         stc_s *stc = stc_get_manager();
1203
1204         if (!stc || !stc->connection) {
1205                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1206                 return STC_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
1207         }
1208
1209         ret = _iptables_remove_in_chain(stc);
1210         if (ret != STC_ERROR_NONE) {
1211                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1212                 goto done; //LCOV_EXCL_LINE
1213         }
1214
1215         ret = _iptables_remove_out_chain(stc);
1216         if (ret != STC_ERROR_NONE) {
1217                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1218                 goto done; //LCOV_EXCL_LINE
1219         }
1220
1221         ret = __iptables_remove_chain(stc->connection, STC_TETHER_CHAIN);
1222         if (ret != STC_ERROR_NONE) {
1223                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1224                 goto done; //LCOV_EXCL_LINE
1225         }
1226
1227         ret = __iptables_remove_chain(stc->connection, STC_FRWD_CHAIN);
1228         if (ret != STC_ERROR_NONE) {
1229                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1230                 goto done; //LCOV_EXCL_LINE
1231         }
1232
1233         ret = _ip6tables_remove_in_chain(stc);
1234         if (ret != STC_ERROR_NONE) {
1235                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1236                 goto done; //LCOV_EXCL_LINE
1237         }
1238
1239         ret = _ip6tables_remove_out_chain(stc);
1240         if (ret != STC_ERROR_NONE) {
1241                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1242                 goto done; //LCOV_EXCL_LINE
1243         }
1244
1245         ret = __ip6tables_remove_chain(stc->connection, STC_FRWD_CHAIN);
1246         if (ret != STC_ERROR_NONE) {
1247                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
1248                 goto done; //LCOV_EXCL_LINE
1249         }
1250
1251 done:
1252         __STC_LOG_FUNC_ENTER__;
1253         return ret;
1254 }