• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020  Red Hat GmbH.  Author: Phil Sutter <phil@nwl.cc>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  */
9 
10 #include <stdlib.h>
11 #include <xtables.h>
12 
13 #include "nft-chain.h"
14 
nft_chain_alloc(struct nftnl_chain * nftnl,bool fake)15 struct nft_chain *nft_chain_alloc(struct nftnl_chain *nftnl, bool fake)
16 {
17 	struct nft_chain *c = xtables_malloc(sizeof(*c));
18 
19 	INIT_LIST_HEAD(&c->head);
20 	c->nftnl = nftnl;
21 	c->fake = fake;
22 
23 	return c;
24 }
25 
nft_chain_free(struct nft_chain * c)26 void nft_chain_free(struct nft_chain *c)
27 {
28 	if (c->nftnl)
29 		nftnl_chain_free(c->nftnl);
30 	free(c);
31 }
32 
nft_chain_list_alloc(void)33 struct nft_chain_list *nft_chain_list_alloc(void)
34 {
35 	struct nft_chain_list *list = xtables_malloc(sizeof(*list));
36 	int i;
37 
38 	INIT_LIST_HEAD(&list->list);
39 	for (i = 0; i < CHAIN_NAME_HSIZE; i++)
40 		INIT_HLIST_HEAD(&list->names[i]);
41 
42 	return list;
43 }
44 
nft_chain_list_del(struct nft_chain * c)45 void nft_chain_list_del(struct nft_chain *c)
46 {
47 	list_del(&c->head);
48 	hlist_del(&c->hnode);
49 }
50 
nft_chain_list_free(struct nft_chain_list * list)51 void nft_chain_list_free(struct nft_chain_list *list)
52 {
53 	struct nft_chain *c, *c2;
54 
55 	list_for_each_entry_safe(c, c2, &list->list, head) {
56 		nft_chain_list_del(c);
57 		nft_chain_free(c);
58 	}
59 	free(list);
60 }
61