developer | 0f54b52 | 2023-03-08 10:04:11 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include "netfilter_flowtable.h" |
| 5 | |
| 6 | static void attr_dump(struct nfattr *attr) |
| 7 | { |
| 8 | char *data = nla_data(attr); |
| 9 | int i = 0; |
| 10 | |
| 11 | while (i < nal_len(attr)) { |
| 12 | printf("%x ", *(data + i)); |
| 13 | i++; |
| 14 | if (i % 16 == 0) |
| 15 | printf("\n"); |
| 16 | } |
| 17 | printf("\n"); |
| 18 | } |
| 19 | |
| 20 | struct ftnl_handle *ftnl_open(void) |
| 21 | { |
| 22 | struct ftnl_handle *h = NULL; |
| 23 | |
| 24 | h = malloc(sizeof(struct ftnl_handle)); |
| 25 | if (!h) |
| 26 | return NULL; |
| 27 | |
| 28 | h->nfnlh = nfnl_open(); |
| 29 | if (!h->nfnlh) { |
| 30 | printf("nfnl open fail\n"); |
| 31 | free(h); |
| 32 | return NULL; |
| 33 | } |
| 34 | |
| 35 | h->ftnlssh = nfnl_subsys_open(h->nfnlh, NFNL_SUBSYS_FLOWTABLE, 1, 0); |
| 36 | if (!h->ftnlssh) { |
| 37 | nfnl_close(h->nfnlh); |
| 38 | printf("subsys open fail\n"); |
| 39 | free(h); |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | return h; |
| 44 | } |
| 45 | |
| 46 | void ftnl_close(struct ftnl_handle *h) |
| 47 | { |
| 48 | nfnl_subsys_close(h->ftnlssh); |
| 49 | nfnl_close(h->nfnlh); |
| 50 | free(h); |
| 51 | } |
| 52 | |
| 53 | static void build_tuple(struct nlmsghdr *nlh, size_t size, |
| 54 | struct flow_tuple *tuple) |
| 55 | { |
| 56 | struct nfattr *nest_tuple, *nest_ip, *nest_proto; |
| 57 | |
| 58 | nest_tuple = nfnl_nest(nlh, size, FTA_TUPLE); |
| 59 | |
| 60 | nest_ip = nfnl_nest(nlh, size, FTA_TUPLE_IP); |
| 61 | nfnl_addattr_l(nlh, size, FTA_IP_V4_SRC, |
| 62 | &tuple->sip4, sizeof(uint32_t)); |
| 63 | nfnl_addattr_l(nlh, size, FTA_IP_V4_DST, |
| 64 | &tuple->dip4, sizeof(uint32_t)); |
| 65 | nfnl_nest_end(nlh, nest_ip); |
| 66 | |
| 67 | nest_proto = nfnl_nest(nlh, size, FTA_TUPLE_PROTO); |
| 68 | nfnl_addattr_l(nlh, size, FTA_PROTO_NUM, |
| 69 | &tuple->proto, sizeof(uint8_t)); |
| 70 | nfnl_addattr_l(nlh, size, FTA_PROTO_SPORT, |
| 71 | &tuple->sport, sizeof(uint16_t)); |
| 72 | nfnl_addattr_l(nlh, size, FTA_PROTO_DPORT, |
| 73 | &tuple->dport, sizeof(uint16_t)); |
| 74 | nfnl_nest_end(nlh, nest_proto); |
| 75 | |
| 76 | nfnl_nest_end(nlh, nest_tuple); |
| 77 | // attr_dump(nest_tuple); |
| 78 | } |
| 79 | |
| 80 | int ftnl_flush_table(struct ftnl_handle *h) |
| 81 | { |
| 82 | struct nlmsghdr nlh; |
| 83 | int ret; |
| 84 | |
| 85 | /* construct msg */ |
| 86 | nfnl_fill_hdr(h->ftnlssh, &nlh, 0, AF_INET, 0, |
| 87 | FT_MSG_FLUSH, NLM_F_REQUEST | NLM_F_ACK); |
| 88 | |
| 89 | /* send msg */ |
| 90 | ret = nfnl_send(h->nfnlh, &nlh); |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | int ftnl_del_flow(struct ftnl_handle *h, struct flow_tuple *tuple) |
| 95 | { |
| 96 | const int size = 256; |
| 97 | union { |
| 98 | char buffer[size]; |
| 99 | struct nlmsghdr nlh; |
| 100 | } u; |
| 101 | int ret; |
| 102 | |
| 103 | /* construct msg */ |
| 104 | nfnl_fill_hdr(h->ftnlssh, &u.nlh, 0, AF_INET, 0, |
| 105 | FT_MSG_DEL, NLM_F_REQUEST|NLM_F_ACK); |
| 106 | build_tuple(&u.nlh, size, tuple); |
| 107 | |
| 108 | /* send msg */ |
| 109 | ret = nfnl_send(h->nfnlh, &u.nlh); |
| 110 | |
| 111 | return ret; |
| 112 | } |