| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */cxx . . $ `<file>/src/common.c`/ |
| 2 | /* |
| 3 | * (C) 2012-2013 by Pablo Neira Ayuso <[email protected]> |
| 4 | */ |
| 5 | |
| 6 | #include <stdlib.h> |
| 7 | #include <sys/socket.h> |
| 8 | #include <time.h> |
| 9 | #include <arpa/inet.h> |
| 10 | #include <linux/netlink.h> |
| 11 | #include <linux/netfilter/nfnetlink.h>cxx . . $ `<file>/include/linux/netfilter/nfnetlink.h`/ |
| 12 | #include <linux/netfilter/nf_tables.h>cxx . . $ `<file>/include/linux/netfilter/nf_tables.h`/ |
| 13 | |
| 14 | #include <libmnl/libmnl.h> |
| 15 | #include <libnftnl/common.h>cxx . . $ `<file>/include/libnftnl/common.h`/ |
| 16 | #include <libnftnl/set.h>cxx . . $ `<file>/include/libnftnl/set.h`/ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include "internal.h"cxx . . $ `<file>/include/internal.h`/ |
| 20 | |
| 21 | static struct nlmsghdrcxx . . $ nlmsghdr# *__nftnl_nlmsg_build_hdrcxx . . $ __nftnl_nlmsg_build_hdr(710dd394b7093b60).(char *buf, uint16_tcxx . . $ uint16_t# type, |
| 22 | uint16_tcxx . . $ uint16_t# family, |
| 23 | uint16_tcxx . . $ uint16_t# flags, uint32_tcxx . . $ uint32_t# seq, |
| 24 | uint16_tcxx . . $ uint16_t# res_id) |
| 25 | { |
| 26 | struct nlmsghdrcxx . . $ nlmsghdr# *nlh; |
| 27 | struct nfgenmsgcxx . . $ nfgenmsg# *nfh; |
| 28 | |
| 29 | nlh = mnl_nlmsg_put_headercxx . . $ mnl_nlmsg_put_header(a335e2471b463cb6).(buf); |
| 30 | nlh->nlmsg_typecxx . . $ nlmsghdr#nlmsg_type. = type; |
| 31 | nlh->nlmsg_flagscxx . . $ nlmsghdr#nlmsg_flags. = NLM_F_REQUESTcxx . . $ `<external>/10309334508915867907/netlink.h:62:9`! | flags; |
| 32 | nlh->nlmsg_seqcxx . . $ nlmsghdr#nlmsg_seq. = seq; |
| 33 | |
| 34 | nfh = mnl_nlmsg_put_extra_headercxx . . $ mnl_nlmsg_put_extra_header(3776ee98844235ce).(nlh, sizeof(struct nfgenmsgcxx . . $ nfgenmsg#)); |
| 35 | nfh->nfgen_familycxx . . $ nfgenmsg#nfgen_family. = family; |
| 36 | nfh->versioncxx . . $ nfgenmsg#version. = NFNETLINK_V0cxx . . $ `include/linux/netfilter/nfnetlink.h:35:9`!; |
| 37 | nfh->res_idcxx . . $ nfgenmsg#res_id. = htonscxx . . $ `<external>/9581057695539925927/in.h:431:12`!(res_id); |
| 38 | |
| 39 | return nlh; |
| 40 | } |
| 41 | |
| 42 | EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_nlmsg_build_hdr); |
| 43 | struct nlmsghdrcxx . . $ nlmsghdr# *nftnl_nlmsg_build_hdrcxx . . $ nftnl_nlmsg_build_hdr(43058a18160afe25).(char *buf, uint16_tcxx . . $ uint16_t# type, uint16_tcxx . . $ uint16_t# family, |
| 44 | uint16_tcxx . . $ uint16_t# flags, uint32_tcxx . . $ uint32_t# seq) |
| 45 | { |
| 46 | return __nftnl_nlmsg_build_hdrcxx . . $ __nftnl_nlmsg_build_hdr(710dd394b7093b60).(buf, (NFNL_SUBSYS_NFTABLEScxx . . $ `include/linux/netfilter/nfnetlink.h:56:9`! << 8) | type, |
| 47 | family, flags, seq, 0); |
| 48 | } |
| 49 | |
| 50 | EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_parse_err_alloc); |
| 51 | struct nftnl_parse_errcxx . . $ nftnl_parse_err# *nftnl_parse_err_alloccxx . . $ nftnl_parse_err_alloc(22836a0f940ce2fb).(void) |
| 52 | { |
| 53 | struct nftnl_parse_errcxx . . $ nftnl_parse_err# *err; |
| 54 | |
| 55 | err = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(struct nftnl_parse_errcxx . . $ nftnl_parse_err#)); |
| 56 | if (err == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) |
| 57 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 58 | |
| 59 | err->errorcxx . . $ nftnl_parse_err#error. = NFTNL_PARSE_EOPNOTSUPPcxx . . $ NFTNL_PARSE_EOPNOTSUPP.; |
| 60 | |
| 61 | return err; |
| 62 | } |
| 63 | |
| 64 | EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_parse_err_free); |
| 65 | void nftnl_parse_err_freecxx . . $ nftnl_parse_err_free(fa4fb86ec91b2a45).(struct nftnl_parse_errcxx . . $ nftnl_parse_err# *err) |
| 66 | { |
| 67 | xfreecxx . . $ `include/utils.h:22:9`!(err); |
| 68 | } |
| 69 | |
| 70 | EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_parse_perror); |
| 71 | int nftnl_parse_perrorcxx . . $ nftnl_parse_perror(2a8939efe9e994ca).(const char *msg, struct nftnl_parse_errcxx . . $ nftnl_parse_err# *err) |
| 72 | { |
| 73 | switch (err->errorcxx . . $ nftnl_parse_err#error.) { |
| 74 | case NFTNL_PARSE_EBADINPUTcxx . . $ NFTNL_PARSE_EBADINPUT.: |
| 75 | return fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%s: Bad input format in line %d column %d\n", |
| 76 | msg, err->linecxx . . $ nftnl_parse_err#line., err->columncxx . . $ nftnl_parse_err#column.); |
| 77 | case NFTNL_PARSE_EMISSINGNODEcxx . . $ NFTNL_PARSE_EMISSINGNODE.: |
| 78 | return fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%s: Node \"%s\" not found\n", |
| 79 | msg, err->node_namecxx . . $ nftnl_parse_err#node_name.); |
| 80 | case NFTNL_PARSE_EBADTYPEcxx . . $ NFTNL_PARSE_EBADTYPE.: |
| 81 | return fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%s: Invalid type in node \"%s\"\n", |
| 82 | msg, err->node_namecxx . . $ nftnl_parse_err#node_name.); |
| 83 | case NFTNL_PARSE_EOPNOTSUPPcxx . . $ NFTNL_PARSE_EOPNOTSUPP.: |
| 84 | return fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%s: Operation not supported\n", msg); |
| 85 | default: |
| 86 | return fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%s: Undefined error\n", msg); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_batch_begin); |
| 91 | struct nlmsghdrcxx . . $ nlmsghdr# *nftnl_batch_begincxx . . $ nftnl_batch_begin(3b71e58f089924bf).(char *buf, uint32_tcxx . . $ uint32_t# seq) |
| 92 | { |
| 93 | return __nftnl_nlmsg_build_hdrcxx . . $ __nftnl_nlmsg_build_hdr(710dd394b7093b60).(buf, NFNL_MSG_BATCH_BEGINcxx . . $ `include/linux/netfilter/nfnetlink.h:102:9`!, AF_UNSPECcxx . . $ `<external>/8962547315054503424/socket.h:93:9`!, |
| 94 | 0, seq, NFNL_SUBSYS_NFTABLEScxx . . $ `include/linux/netfilter/nfnetlink.h:56:9`!); |
| 95 | } |
| 96 | |
| 97 | EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_batch_end); |
| 98 | struct nlmsghdrcxx . . $ nlmsghdr# *nftnl_batch_endcxx . . $ nftnl_batch_end(3b71e58f089924bf).(char *buf, uint32_tcxx . . $ uint32_t# seq) |
| 99 | { |
| 100 | return __nftnl_nlmsg_build_hdrcxx . . $ __nftnl_nlmsg_build_hdr(710dd394b7093b60).(buf, NFNL_MSG_BATCH_ENDcxx . . $ `include/linux/netfilter/nfnetlink.h:103:9`!, AF_UNSPECcxx . . $ `<external>/8962547315054503424/socket.h:93:9`!, |
| 101 | 0, seq, NFNL_SUBSYS_NFTABLEScxx . . $ `include/linux/netfilter/nfnetlink.h:56:9`!); |
| 102 | } |
| 103 | |
| 104 | EXPORT_SYMBOLcxx . . $ `include/utils.h:14:10`!(nftnl_batch_is_supported); |
| 105 | int nftnl_batch_is_supportedcxx . . $ nftnl_batch_is_supported(b126dc7c1de90089).(void) |
| 106 | { |
| 107 | struct mnl_socketcxx . . $ mnl_socket# *nl; |
| 108 | struct mnl_nlmsg_batchcxx . . $ mnl_nlmsg_batch# *b; |
| 109 | char buf[MNL_SOCKET_BUFFER_SIZEcxx . . $ `<external>/12386019527593959648/libmnl.h:20:9`!]; |
| 110 | uint32_tcxx . . $ uint32_t# seq = timecxx . . $ time(7f38134db6521d23).(NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!), req_seq; |
| 111 | int ret; |
| 112 | |
| 113 | nl = mnl_socket_opencxx . . $ mnl_socket_open(11c66b52d058e643).(NETLINK_NETFILTERcxx . . $ `<external>/10309334508915867907/netlink.h:21:9`!); |
| 114 | if (nl == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!) |
| 115 | return -1; |
| 116 | |
| 117 | if (mnl_socket_bindcxx . . $ mnl_socket_bind(b1e0dc383ab1e375).(nl, 0, MNL_SOCKET_AUTOPIDcxx . . $ `<external>/12386019527593959648/libmnl.h:19:9`!) < 0) |
| 118 | return -1; |
| 119 | |
| 120 | b = mnl_nlmsg_batch_startcxx . . $ mnl_nlmsg_batch_start(92148d5219220a56).(buf, sizeof(buf)); |
| 121 | |
| 122 | nftnl_batch_begincxx . . $ nftnl_batch_begin(3b71e58f089924bf).(mnl_nlmsg_batch_currentcxx . . $ mnl_nlmsg_batch_current(e6dcd6b3a8cfcc6e).(b), seq++); |
| 123 | mnl_nlmsg_batch_nextcxx . . $ mnl_nlmsg_batch_next(5cafdd85047999f6).(b); |
| 124 | |
| 125 | req_seq = seq; |
| 126 | nftnl_nlmsg_build_hdrcxx . . $ nftnl_nlmsg_build_hdr(43058a18160afe25).(mnl_nlmsg_batch_currentcxx . . $ mnl_nlmsg_batch_current(e6dcd6b3a8cfcc6e).(b), NFT_MSG_NEWSETcxx . . $ NFT_MSG_NEWSET., |
| 127 | AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!, NLM_F_ACKcxx . . $ `<external>/10309334508915867907/netlink.h:64:9`!, seq++); |
| 128 | mnl_nlmsg_batch_nextcxx . . $ mnl_nlmsg_batch_next(5cafdd85047999f6).(b); |
| 129 | |
| 130 | nftnl_batch_endcxx . . $ nftnl_batch_end(3b71e58f089924bf).(mnl_nlmsg_batch_currentcxx . . $ mnl_nlmsg_batch_current(e6dcd6b3a8cfcc6e).(b), seq++); |
| 131 | mnl_nlmsg_batch_nextcxx . . $ mnl_nlmsg_batch_next(5cafdd85047999f6).(b); |
| 132 | |
| 133 | ret = mnl_socket_sendtocxx . . $ mnl_socket_sendto(e711b469f0ec0dac).(nl, mnl_nlmsg_batch_headcxx . . $ mnl_nlmsg_batch_head(e6dcd6b3a8cfcc6e).(b), |
| 134 | mnl_nlmsg_batch_sizecxx . . $ mnl_nlmsg_batch_size(70b831e9aabf33b5).(b)); |
| 135 | if (ret < 0) |
| 136 | goto err; |
| 137 | |
| 138 | mnl_nlmsg_batch_stopcxx . . $ mnl_nlmsg_batch_stop(9ec449ff7bd2d2ec).(b); |
| 139 | |
| 140 | ret = mnl_socket_recvfromcxx . . $ mnl_socket_recvfrom(d560911c441f5ef5).(nl, buf, sizeof(buf)); |
| 141 | while (ret > 0) { |
| 142 | ret = mnl_cb_runcxx . . $ mnl_cb_run(ccf3087af063d440).(buf, ret, req_seq, mnl_socket_get_portidcxx . . $ mnl_socket_get_portid(c1ab11c2a3cbee49).(nl), |
| 143 | NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!); |
| 144 | if (ret <= 0) |
| 145 | break; |
| 146 | |
| 147 | ret = mnl_socket_recvfromcxx . . $ mnl_socket_recvfrom(d560911c441f5ef5).(nl, buf, sizeof(buf)); |
| 148 | } |
| 149 | mnl_socket_closecxx . . $ mnl_socket_close(9c71261816e44eac).(nl); |
| 150 | |
| 151 | /* We're sending an incomplete message to see if the kernel supports |
| 152 | * set messages in batches. EINVAL means that we sent an incomplete |
| 153 | * message with missing attributes. The kernel just ignores messages |
| 154 | * that we cannot include in the batch. |
| 155 | */ |
| 156 | return (ret == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!) ? 1 : 0; |
| 157 | err: |
| 158 | mnl_nlmsg_batch_stopcxx . . $ mnl_nlmsg_batch_stop(9ec449ff7bd2d2ec).(b); |
| 159 | return -1; |
| 160 | } |