1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4#include <time.h> 5 6#include <libmnl/libmnl.h> 7#include <libnetfilter_conntrack/libnetfilter_conntrack.h> 8 9struct callback_args { 10 struct mnl_socket *nl; 11 unsigned int seq; 12 int bit; 13}; 14 15static void set_label(struct nf_conntrack *ct, struct callback_args *cbargs) 16{ 17 struct nfct_bitmask *b = (void *) nfct_get_attr(ct, ATTR_CONNLABELS); 18 int bit = cbargs->bit; 19 char buf[MNL_SOCKET_BUFFER_SIZE]; 20 struct nlmsghdr *nlh; 21 struct nfgenmsg *nfh; 22 23 if (b) { 24 if (bit < 0) 25 b = nfct_bitmask_new(0); 26 else if (nfct_bitmask_test_bit(b, bit)) 27 return; 28 } else { 29 b = nfct_bitmask_new(0); 30 } 31 32 if (!b) 33 return; 34 if (bit >= 0) 35 nfct_bitmask_set_bit(b, bit); 36 nfct_set_attr(ct, ATTR_CONNLABELS, b); 37 38 if (bit >= 0) { 39 b = nfct_bitmask_new(bit); 40 if (b) { 41 nfct_bitmask_set_bit(b, bit); 42 nfct_set_attr(ct, ATTR_CONNLABELS_MASK, b); 43 } 44 } 45 46 cbargs->seq++; 47 48 nlh = mnl_nlmsg_put_header(buf); 49 nlh->nlmsg_type = (NFNL_SUBSYS_CTNETLINK << 8) | IPCTNL_MSG_CT_NEW; 50 nlh->nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE; 51 nlh->nlmsg_seq = cbargs->seq; 52 53 nfh = mnl_nlmsg_put_extra_header(nlh, sizeof(struct nfgenmsg)); 54 nfh->nfgen_family = nfct_get_attr_u8(ct, ATTR_L3PROTO); 55 nfh->version = NFNETLINK_V0; 56 nfh->res_id = 0; 57 58 nfct_nlmsg_build(nlh, ct); 59 60 if (mnl_socket_sendto(cbargs->nl, nlh, nlh->nlmsg_len) < 0) 61 perror("mnl_socket_sendto"); 62} 63 64static int data_cb(const struct nlmsghdr *nlh, void *data) 65{ 66 struct nf_conntrack *ct; 67 char buf[4096]; 68 69 ct = nfct_new(); 70 if (ct == NULL) 71 return MNL_CB_OK; 72 73 nfct_nlmsg_parse(nlh, ct); 74 75 nfct_snprintf(buf, sizeof(buf), ct, NFCT_T_UNKNOWN, NFCT_O_DEFAULT, 0); 76 printf("%s\n", buf); 77 78 set_label(ct, data); 79 80 nfct_destroy(ct); 81 82 return MNL_CB_OK; 83} 84 85static void show_labels(struct nfct_labelmap *l) 86{ 87 unsigned int i = 0; 88 const char *name; 89 90 if (l) { 91 fputs("usage: program label, configured labels are:\n", stderr); 92 while ((name = nfct_labelmap_get_name(l, i))) { 93 if (*name) 94 fprintf(stderr, "%s -> bit %d\n", name, i); 95 i++; 96 } 97 } else { 98 fputs("no labels configured, usage: program bit\n", stderr); 99 } 100 exit(1); 101} 102 103static struct mnl_socket *sock_nl_create(void) 104{ 105 struct mnl_socket *nl; 106 107 nl = mnl_socket_open(NETLINK_NETFILTER); 108 if (nl == NULL) { 109 perror("mnl_socket_open"); 110 exit(EXIT_FAILURE); 111 } 112 113 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { 114 perror("mnl_socket_bind"); 115 exit(EXIT_FAILURE); 116 } 117 118 return nl; 119} 120 121int main(int argc, char *argv[]) 122{ 123 struct mnl_socket *nl; 124 struct nlmsghdr *nlh; 125 struct nfgenmsg *nfh; 126 char buf[MNL_SOCKET_BUFFER_SIZE]; 127 unsigned int seq, portid; 128 struct callback_args cbargs; 129 int ret; 130 struct nfct_labelmap *l = nfct_labelmap_new(NULL); 131 132 if (argc < 2) 133 show_labels(l); 134 135 cbargs.bit = l ? nfct_labelmap_get_bit(l, argv[1]) : -1; 136 137 if (cbargs.bit < 0) { 138 cbargs.bit = atoi(argv[1]); 139 if (cbargs.bit == 0 && argv[1][0] != '0') 140 show_labels(l); 141 } 142 143 if (cbargs.bit < 0) 144 puts("will clear all labels"); 145 else 146 printf("will set label bit %d\n", cbargs.bit); 147 148 nl = sock_nl_create(); 149 portid = mnl_socket_get_portid(nl); 150 151 nlh = mnl_nlmsg_put_header(buf); 152 nlh->nlmsg_type = (NFNL_SUBSYS_CTNETLINK << 8) | IPCTNL_MSG_CT_GET; 153 nlh->nlmsg_flags = NLM_F_REQUEST|NLM_F_DUMP; 154 nlh->nlmsg_seq = seq = time(NULL); 155 156 nfh = mnl_nlmsg_put_extra_header(nlh, sizeof(struct nfgenmsg)); 157 nfh->nfgen_family = AF_UNSPEC; 158 nfh->version = NFNETLINK_V0; 159 nfh->res_id = 0; 160 161 162 ret = mnl_socket_sendto(nl, nlh, nlh->nlmsg_len); 163 if (ret == -1) { 164 perror("mnl_socket_sendto"); 165 exit(EXIT_FAILURE); 166 } 167 168 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); 169 170 171 cbargs.nl = sock_nl_create(); 172 cbargs.seq = seq; 173 174 while (ret > 0) { 175 ret = mnl_cb_run(buf, ret, seq, portid, data_cb, &cbargs); 176 if (ret <= MNL_CB_STOP) 177 break; 178 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); 179 } 180 if (ret == -1) { 181 perror("mnl_socket_recvfrom"); 182 exit(EXIT_FAILURE); 183 } 184 185 if (l) 186 nfct_labelmap_destroy(l); 187 mnl_socket_close(nl); 188 189 return 0; 190} 191