| 1 | /* |
|---|
| 2 | * This file is part of libtrace |
|---|
| 3 | * |
|---|
| 4 | * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand. |
|---|
| 5 | * Authors: Daniel Lawson |
|---|
| 6 | * Perry Lorier |
|---|
| 7 | * |
|---|
| 8 | * All rights reserved. |
|---|
| 9 | * |
|---|
| 10 | * This code has been developed by the University of Waikato WAND |
|---|
| 11 | * research group. For further information please see http://www.wand.net.nz/ |
|---|
| 12 | * |
|---|
| 13 | * libtrace is free software; you can redistribute it and/or modify |
|---|
| 14 | * it under the terms of the GNU General Public License as published by |
|---|
| 15 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 16 | * (at your option) any later version. |
|---|
| 17 | * |
|---|
| 18 | * libtrace is distributed in the hope that it will be useful, |
|---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 21 | * GNU General Public License for more details. |
|---|
| 22 | * |
|---|
| 23 | * You should have received a copy of the GNU General Public License |
|---|
| 24 | * along with libtrace; if not, write to the Free Software |
|---|
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 26 | * |
|---|
| 27 | * $Id: format_template.c,v 1.13 2005/11/22 23:38:56 dlawson Exp $ |
|---|
| 28 | * |
|---|
| 29 | */ |
|---|
| 30 | |
|---|
| 31 | #include "libtrace.h" |
|---|
| 32 | #include "libtrace_int.h" |
|---|
| 33 | #include "format_helper.h" |
|---|
| 34 | #include "config.h" |
|---|
| 35 | #include "stdlib.h" |
|---|
| 36 | |
|---|
| 37 | #ifdef HAVE_INTTYPES_H |
|---|
| 38 | # include <inttypes.h> |
|---|
| 39 | #else |
|---|
| 40 | # error "Can't find inttypes.h" |
|---|
| 41 | #endif |
|---|
| 42 | |
|---|
| 43 | #include <sys/socket.h> |
|---|
| 44 | #include <netpacket/packet.h> |
|---|
| 45 | #include <net/ethernet.h> |
|---|
| 46 | #include <net/if_arp.h> |
|---|
| 47 | |
|---|
| 48 | #include <string.h> |
|---|
| 49 | #include <net/if.h> |
|---|
| 50 | #include <sys/ioctl.h> |
|---|
| 51 | |
|---|
| 52 | static struct libtrace_format_t linuxnative; |
|---|
| 53 | |
|---|
| 54 | struct libtrace_format_data_t { |
|---|
| 55 | int fd; |
|---|
| 56 | int snaplen; |
|---|
| 57 | int promisc; |
|---|
| 58 | }; |
|---|
| 59 | |
|---|
| 60 | struct libtrace_linuxnative_header { |
|---|
| 61 | struct timeval ts; |
|---|
| 62 | int wirelen; |
|---|
| 63 | int caplen; |
|---|
| 64 | struct sockaddr_ll hdr; |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | struct libtrace_linuxnative_format_data_t { |
|---|
| 68 | int fd; |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | #define FORMAT(x) ((struct libtrace_format_data_t*)(x)) |
|---|
| 72 | #define DATAOUT(x) ((struct libtrace_linuxnative_format_data_t*)((x)->format_data)) |
|---|
| 73 | |
|---|
| 74 | static int linuxnative_init_input(libtrace_t *libtrace) |
|---|
| 75 | { |
|---|
| 76 | libtrace->format_data = (struct libtrace_format_data_t *) |
|---|
| 77 | malloc(sizeof(struct libtrace_format_data_t)); |
|---|
| 78 | FORMAT(libtrace->format_data)->fd = -1; |
|---|
| 79 | FORMAT(libtrace->format_data)->promisc = 0; |
|---|
| 80 | FORMAT(libtrace->format_data)->snaplen = 65536; |
|---|
| 81 | |
|---|
| 82 | return 0; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | static int linuxnative_init_output(libtrace_out_t *libtrace) |
|---|
| 86 | { |
|---|
| 87 | libtrace->format_data = (struct libtrace_linuxnative_format_data_t*) |
|---|
| 88 | malloc(sizeof(struct libtrace_linuxnative_format_data_t)); |
|---|
| 89 | DATAOUT(libtrace)->fd = -1; |
|---|
| 90 | |
|---|
| 91 | return 0; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | static int linuxnative_start_input(libtrace_t *libtrace) |
|---|
| 95 | { |
|---|
| 96 | struct sockaddr_ll addr; |
|---|
| 97 | FORMAT(libtrace->format_data)->fd = |
|---|
| 98 | socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); |
|---|
| 99 | if (FORMAT(libtrace->format_data)->fd==-1) { |
|---|
| 100 | free(libtrace->format_data); |
|---|
| 101 | return -1; |
|---|
| 102 | } |
|---|
| 103 | addr.sll_family = AF_PACKET; |
|---|
| 104 | addr.sll_protocol = htons(ETH_P_ALL); |
|---|
| 105 | if (strlen(libtrace->uridata)) { |
|---|
| 106 | addr.sll_ifindex = if_nametoindex(libtrace->uridata); |
|---|
| 107 | if (addr.sll_ifindex == 0) { |
|---|
| 108 | close(FORMAT(libtrace->format_data)->fd); |
|---|
| 109 | free(libtrace->format_data); |
|---|
| 110 | return -1; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | else { |
|---|
| 114 | addr.sll_ifindex = 0; |
|---|
| 115 | } |
|---|
| 116 | if (bind(FORMAT(libtrace->format_data)->fd, |
|---|
| 117 | (struct sockaddr*)&addr, |
|---|
| 118 | sizeof(addr))==-1) { |
|---|
| 119 | free(libtrace->format_data); |
|---|
| 120 | return -1; |
|---|
| 121 | } |
|---|
| 122 | /* enable promisc mode when listening on an interface */ |
|---|
| 123 | if (addr.sll_ifindex!=0) { |
|---|
| 124 | struct packet_mreq mreq; |
|---|
| 125 | socklen_t socklen = sizeof(mreq); |
|---|
| 126 | mreq.mr_ifindex = addr.sll_ifindex; |
|---|
| 127 | mreq.mr_type = PACKET_MR_PROMISC; |
|---|
| 128 | setsockopt(FORMAT(libtrace->format_data)->fd, |
|---|
| 129 | SOL_PACKET, |
|---|
| 130 | PACKET_ADD_MEMBERSHIP, |
|---|
| 131 | &mreq, |
|---|
| 132 | socklen); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | return 0; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | static int linuxnative_start_output(libtrace_out_t *libtrace) |
|---|
| 139 | { |
|---|
| 140 | FORMAT(libtrace->format_data)->fd = |
|---|
| 141 | socket(PF_PACKET, SOCK_RAW, 0); |
|---|
| 142 | if (FORMAT(libtrace->format_data)->fd==-1) { |
|---|
| 143 | free(libtrace->format_data); |
|---|
| 144 | return -1; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | return 0; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | static int linuxnative_pause_input(libtrace_t *libtrace) |
|---|
| 151 | { |
|---|
| 152 | close(FORMAT(libtrace->format_data)->fd); |
|---|
| 153 | FORMAT(libtrace->format_data)->fd=-1; |
|---|
| 154 | |
|---|
| 155 | return 0; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | static int linuxnative_fin_input(libtrace_t *libtrace) |
|---|
| 159 | { |
|---|
| 160 | free(libtrace->format_data); |
|---|
| 161 | return 0; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | static int linuxnative_fin_output(libtrace_out_t *libtrace) |
|---|
| 165 | { |
|---|
| 166 | close(DATAOUT(libtrace)->fd); |
|---|
| 167 | DATAOUT(libtrace)->fd=-1; |
|---|
| 168 | free(libtrace->format_data); |
|---|
| 169 | return 0; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | static int linuxnative_config_input(libtrace_t *libtrace, |
|---|
| 173 | trace_option_t option, |
|---|
| 174 | void *data) |
|---|
| 175 | { |
|---|
| 176 | switch(option) { |
|---|
| 177 | case TRACE_OPTION_SNAPLEN: |
|---|
| 178 | FORMAT(libtrace->format_data)->snaplen=*(int*)data; |
|---|
| 179 | return 0; |
|---|
| 180 | case TRACE_OPTION_PROMISC: |
|---|
| 181 | FORMAT(libtrace->format_data)->promisc=*(int*)data; |
|---|
| 182 | return 0; |
|---|
| 183 | case TRACE_OPTION_FILTER: |
|---|
| 184 | /* We don't support bpf filters in any special way |
|---|
| 185 | * so return an error and let libtrace deal with |
|---|
| 186 | * emulating it |
|---|
| 187 | */ |
|---|
| 188 | break; |
|---|
| 189 | /* Avoid default: so that future options will cause a warning |
|---|
| 190 | * here to remind us to implement it, or flag it as |
|---|
| 191 | * unimplementable |
|---|
| 192 | */ |
|---|
| 193 | } |
|---|
| 194 | trace_set_err(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
|---|
| 195 | "Unknown option %i", option); |
|---|
| 196 | return -1; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | #define LIBTRACE_MIN(a,b) ((a)<(b) ? (a) : (b)) |
|---|
| 200 | |
|---|
| 201 | static int linuxnative_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) |
|---|
| 202 | { |
|---|
| 203 | struct libtrace_linuxnative_header *hdr; |
|---|
| 204 | socklen_t socklen; |
|---|
| 205 | int snaplen; |
|---|
| 206 | if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { |
|---|
| 207 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
|---|
| 208 | packet->buf_control = TRACE_CTRL_PACKET; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | packet->header = packet->buffer; |
|---|
| 212 | packet->type = RT_DATA_LINUX_NATIVE; |
|---|
| 213 | packet->payload = (char*)packet->buffer+sizeof(*hdr); |
|---|
| 214 | |
|---|
| 215 | hdr=(void*)packet->buffer; |
|---|
| 216 | socklen=sizeof(hdr->hdr); |
|---|
| 217 | snaplen=LIBTRACE_MIN( |
|---|
| 218 | (int)LIBTRACE_PACKET_BUFSIZE-(int)sizeof(*hdr), |
|---|
| 219 | (int)FORMAT(libtrace->format_data)->snaplen); |
|---|
| 220 | hdr->wirelen = recvfrom(FORMAT(libtrace->format_data)->fd, |
|---|
| 221 | (void*)packet->payload, |
|---|
| 222 | snaplen, |
|---|
| 223 | MSG_TRUNC, |
|---|
| 224 | (void *)&hdr->hdr, |
|---|
| 225 | &socklen); |
|---|
| 226 | |
|---|
| 227 | if (hdr->wirelen==-1) |
|---|
| 228 | return -1; |
|---|
| 229 | |
|---|
| 230 | hdr->caplen=LIBTRACE_MIN(snaplen,hdr->wirelen); |
|---|
| 231 | |
|---|
| 232 | if (ioctl(FORMAT(libtrace->format_data)->fd,SIOCGSTAMP,&hdr->ts)==-1) |
|---|
| 233 | perror("ioctl(SIOCGSTAMP)"); |
|---|
| 234 | |
|---|
| 235 | return hdr->wirelen+sizeof(*hdr); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | static int linuxnative_write_packet(libtrace_out_t *trace, |
|---|
| 239 | const libtrace_packet_t *packet) |
|---|
| 240 | { |
|---|
| 241 | struct sockaddr_ll hdr; |
|---|
| 242 | |
|---|
| 243 | hdr.sll_family = AF_PACKET; |
|---|
| 244 | hdr.sll_protocol = 0; |
|---|
| 245 | hdr.sll_ifindex = if_nametoindex(packet->trace->uridata); |
|---|
| 246 | hdr.sll_hatype = 0; |
|---|
| 247 | hdr.sll_pkttype = 0; |
|---|
| 248 | hdr.sll_halen = 6; /* FIXME */ |
|---|
| 249 | memcpy(hdr.sll_addr,packet->payload,hdr.sll_halen); |
|---|
| 250 | |
|---|
| 251 | return sendto(DATAOUT(trace)->fd, |
|---|
| 252 | packet->payload, |
|---|
| 253 | trace_get_capture_length(packet), |
|---|
| 254 | 0, |
|---|
| 255 | (struct sockaddr*)&hdr, sizeof(hdr)); |
|---|
| 256 | |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | static libtrace_linktype_t linuxnative_get_link_type(const struct libtrace_packet_t *packet) { |
|---|
| 260 | switch (htons((((struct libtrace_linuxnative_header*)(packet->buffer))->hdr.sll_protocol))) { |
|---|
| 261 | case ETH_P_IP: |
|---|
| 262 | case ETH_P_IPV6: |
|---|
| 263 | case ETH_P_ARP: |
|---|
| 264 | return TRACE_TYPE_ETH; |
|---|
| 265 | default: /* shrug, beyond me! */ |
|---|
| 266 | printf("unknown type %x\n",(((struct libtrace_linuxnative_header*)(packet->buffer))->hdr.sll_protocol)); |
|---|
| 267 | return -1; |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | static int8_t linuxnative_get_direction(const struct libtrace_packet_t *packet) { |
|---|
| 272 | switch (((struct libtrace_linuxnative_header*)(packet->buffer))->hdr.sll_pkttype) { |
|---|
| 273 | case PACKET_OUTGOING: |
|---|
| 274 | return 0; |
|---|
| 275 | default: |
|---|
| 276 | return 1; |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | static struct timeval linuxnative_get_timeval(const libtrace_packet_t *packet) |
|---|
| 281 | { |
|---|
| 282 | return ((struct libtrace_linuxnative_header*)(packet->buffer))->ts; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | static int linuxnative_get_capture_length(const libtrace_packet_t *packet) |
|---|
| 286 | { |
|---|
| 287 | return ((struct libtrace_linuxnative_header*)(packet->buffer))->caplen; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | static int linuxnative_get_wire_length(const libtrace_packet_t *packet) |
|---|
| 291 | { |
|---|
| 292 | return ((struct libtrace_linuxnative_header*)(packet->buffer))->wirelen; |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | static int linuxnative_get_framing_length(const libtrace_packet_t *packet) |
|---|
| 296 | { |
|---|
| 297 | return sizeof(struct libtrace_linuxnative_header); |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | static int linuxnative_get_fd(const libtrace_t *trace) { |
|---|
| 301 | return FORMAT(trace->format_data)->fd; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | static void linuxnative_help() { |
|---|
| 305 | printf("linuxnative format module: $Revision$\n"); |
|---|
| 306 | printf("Supported input URIs:\n"); |
|---|
| 307 | printf("\tint:\n"); |
|---|
| 308 | printf("\n"); |
|---|
| 309 | printf("Supported output URIs:\n"); |
|---|
| 310 | printf("\tnone\n"); |
|---|
| 311 | printf("\n"); |
|---|
| 312 | return; |
|---|
| 313 | } |
|---|
| 314 | static struct libtrace_format_t linuxnative = { |
|---|
| 315 | "int", |
|---|
| 316 | "$Id: format_linuxnative.c,v 1.13 2005/11/22 23:38:56 dlawson Exp $", |
|---|
| 317 | TRACE_FORMAT_LINUX_NATIVE, |
|---|
| 318 | linuxnative_init_input, /* init_input */ |
|---|
| 319 | linuxnative_config_input, /* config_input */ |
|---|
| 320 | linuxnative_start_input, /* start_input */ |
|---|
| 321 | linuxnative_pause_input, /* pause_input */ |
|---|
| 322 | linuxnative_init_output, /* init_output */ |
|---|
| 323 | NULL, /* config_output */ |
|---|
| 324 | linuxnative_start_output, /* start_ouput */ |
|---|
| 325 | linuxnative_fin_input, /* fin_input */ |
|---|
| 326 | linuxnative_fin_output, /* fin_output */ |
|---|
| 327 | linuxnative_read_packet, /* read_packet */ |
|---|
| 328 | NULL, /* fin_packet */ |
|---|
| 329 | linuxnative_write_packet, /* write_packet */ |
|---|
| 330 | linuxnative_get_link_type, /* get_link_type */ |
|---|
| 331 | linuxnative_get_direction, /* get_direction */ |
|---|
| 332 | NULL, /* set_direction */ |
|---|
| 333 | NULL, /* get_erf_timestamp */ |
|---|
| 334 | linuxnative_get_timeval, /* get_timeval */ |
|---|
| 335 | NULL, /* get_seconds */ |
|---|
| 336 | NULL, /* seek_erf */ |
|---|
| 337 | NULL, /* seek_timeval */ |
|---|
| 338 | NULL, /* seek_seconds */ |
|---|
| 339 | linuxnative_get_capture_length, /* get_capture_length */ |
|---|
| 340 | linuxnative_get_wire_length, /* get_wire_length */ |
|---|
| 341 | linuxnative_get_framing_length, /* get_framing_length */ |
|---|
| 342 | NULL, /* set_capture_length */ |
|---|
| 343 | linuxnative_get_fd, /* get_fd */ |
|---|
| 344 | trace_event_device, /* trace_event */ |
|---|
| 345 | linuxnative_help, /* help */ |
|---|
| 346 | NULL |
|---|
| 347 | }; |
|---|
| 348 | |
|---|
| 349 | void CONSTRUCTOR linuxnative_constructor() { |
|---|
| 350 | register_format(&linuxnative); |
|---|
| 351 | } |
|---|