1 /* 2 * src/nf-queue.c Monitor netfilter queue events 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation version 2.1 7 * of the License. 8 * 9 * Copyright (c) 2007, 2008 Patrick McHardy <kaber (at) trash.net> 10 * Copyright (c) 2010 Karl Hiramoto <karl (at) hiramoto.org> 11 */ 12 13 14 #include <netlink/cli/utils.h> 15 #include <netlink/cli/link.h> 16 #include <linux/netfilter.h> 17 #include <linux/netfilter/nfnetlink_queue.h> 18 #include <netlink/netfilter/nfnl.h> 19 #include <netlink/netfilter/queue.h> 20 #include <netlink/netfilter/queue_msg.h> 21 22 static struct nl_sock *nf_sock; 23 24 static struct nfnl_queue *alloc_queue(void) 25 { 26 struct nfnl_queue *queue; 27 28 queue = nfnl_queue_alloc(); 29 if (!queue) 30 nl_cli_fatal(ENOMEM, "Unable to allocate queue object"); 31 32 return queue; 33 } 34 35 36 static void obj_input(struct nl_object *obj, void *arg) 37 { 38 struct nfnl_queue_msg *msg = (struct nfnl_queue_msg *) obj; 39 struct nl_dump_params dp = { 40 .dp_type = NL_DUMP_STATS, 41 .dp_fd = stdout, 42 .dp_dump_msgtype = 1, 43 }; 44 uint32_t packet_id = nfnl_queue_msg_get_packetid(msg); 45 static uint32_t next_packet_id = 0; 46 struct nfnl_queue_msg *lost_msg = NULL; 47 uint8_t family; 48 uint16_t group; 49 50 if (packet_id > next_packet_id) { 51 printf("Warning: %d Out of order packets. Queue or socket overload \n", packet_id - next_packet_id); 52 group = nfnl_queue_msg_get_group(msg); 53 family = nfnl_queue_msg_get_family(msg); 54 lost_msg = nfnl_queue_msg_alloc(); 55 56 do { 57 nfnl_queue_msg_set_group(lost_msg, group); 58 nfnl_queue_msg_set_family(lost_msg, family); 59 nfnl_queue_msg_set_packetid(lost_msg, next_packet_id); 60 nfnl_queue_msg_set_verdict(lost_msg, NF_ACCEPT); 61 nfnl_queue_msg_send_verdict(nf_sock, lost_msg); 62 next_packet_id++; 63 } while (packet_id > next_packet_id); 64 65 nfnl_queue_msg_put(lost_msg); 66 } 67 68 next_packet_id = packet_id + 1; 69 nfnl_queue_msg_set_verdict(msg, NF_ACCEPT); 70 nl_object_dump(obj, &dp); 71 nfnl_queue_msg_send_verdict(nf_sock, msg); 72 } 73 74 static int event_input(struct nl_msg *msg, void *arg) 75 { 76 if (nl_msg_parse(msg, &obj_input, NULL) < 0) 77 fprintf(stderr, "<<EVENT>> Unknown message type\n"); 78 79 /* Exit nl_recvmsgs_def() and return to the main select() */ 80 return NL_STOP; 81 } 82 83 int main(int argc, char *argv[]) 84 { 85 struct nl_sock *rt_sock; 86 struct nl_cache *link_cache; 87 struct nfnl_queue *queue; 88 enum nfnl_queue_copy_mode copy_mode; 89 uint32_t copy_range; 90 int err = 1; 91 int family; 92 93 nf_sock = nfnl_queue_socket_alloc(); 94 if (nf_sock == NULL) 95 nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket"); 96 97 nl_socket_disable_seq_check(nf_sock); 98 nl_socket_modify_cb(nf_sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL); 99 100 if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) { 101 printf("Usage: nf-queue family group [ copy_mode ] " 102 "[ copy_range ]\n"); 103 printf("family: [ inet | inet6 | ... ] \n"); 104 printf("group: the --queue-num arg that you gave to iptables\n"); 105 printf("copy_mode: [ none | meta | packet ] \n"); 106 return 2; 107 } 108 109 nl_cli_connect(nf_sock, NETLINK_NETFILTER); 110 111 if ((family = nl_str2af(argv[1])) == AF_UNSPEC) 112 nl_cli_fatal(NLE_INVAL, "Unknown family \"%s\"", argv[1]); 113 114 nfnl_queue_pf_unbind(nf_sock, family); 115 if ((err = nfnl_queue_pf_bind(nf_sock, family)) < 0) 116 nl_cli_fatal(err, "Unable to bind logger: %s", 117 nl_geterror(err)); 118 119 queue = alloc_queue(); 120 nfnl_queue_set_group(queue, atoi(argv[2])); 121 122 copy_mode = NFNL_QUEUE_COPY_PACKET; 123 if (argc > 3) { 124 copy_mode = nfnl_queue_str2copy_mode(argv[3]); 125 if (copy_mode < 0) 126 nl_cli_fatal(copy_mode, 127 "Unable to parse copy mode \"%s\": %s", 128 argv[3], nl_geterror(copy_mode)); 129 } 130 nfnl_queue_set_copy_mode(queue, copy_mode); 131 132 copy_range = 0xFFFF; 133 if (argc > 4) 134 copy_range = atoi(argv[4]); 135 nfnl_queue_set_copy_range(queue, copy_range); 136 137 if ((err = nfnl_queue_create(nf_sock, queue)) < 0) 138 nl_cli_fatal(err, "Unable to bind queue: %s", nl_geterror(err)); 139 140 rt_sock = nl_cli_alloc_socket(); 141 nl_cli_connect(rt_sock, NETLINK_ROUTE); 142 link_cache = nl_cli_link_alloc_cache(rt_sock); 143 144 nl_socket_set_buffer_size(nf_sock, 1024*127, 1024*127); 145 146 while (1) { 147 fd_set rfds; 148 int nffd, rtfd, maxfd, retval; 149 150 FD_ZERO(&rfds); 151 152 maxfd = nffd = nl_socket_get_fd(nf_sock); 153 FD_SET(nffd, &rfds); 154 155 rtfd = nl_socket_get_fd(rt_sock); 156 FD_SET(rtfd, &rfds); 157 if (maxfd < rtfd) 158 maxfd = rtfd; 159 160 /* wait for an incoming message on the netlink socket */ 161 retval = select(maxfd+1, &rfds, NULL, NULL, NULL); 162 163 if (retval) { 164 if (FD_ISSET(nffd, &rfds)) 165 nl_recvmsgs_default(nf_sock); 166 if (FD_ISSET(rtfd, &rfds)) 167 nl_recvmsgs_default(rt_sock); 168 } 169 } 170 171 return 0; 172 } 173