pcap.c (5930B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2013-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #define _GNU_SOURCE 28 #include <stdio.h> 29 #include <fcntl.h> 30 #include <unistd.h> 31 #include <stdlib.h> 32 #include <sys/stat.h> 33 #include <sys/uio.h> 34 #include <sys/types.h> 35 #include <ell/ell.h> 36 37 #include "monitor/pcap.h" 38 39 struct pcap_hdr { 40 uint32_t magic_number; /* magic number */ 41 uint16_t version_major; /* major version number */ 42 uint16_t version_minor; /* minor version number */ 43 int32_t thiszone; /* GMT to local correction */ 44 uint32_t sigfigs; /* accuracy of timestamps */ 45 uint32_t snaplen; /* max length of captured packets, in octets */ 46 uint32_t network; /* data link type */ 47 } __attribute__ ((packed)); 48 #define PCAP_HDR_SIZE (sizeof(struct pcap_hdr)) 49 50 struct pcap_pkt { 51 uint32_t ts_sec; /* timestamp seconds */ 52 uint32_t ts_usec; /* timestamp microseconds */ 53 uint32_t incl_len; /* number of octets of packet saved in file */ 54 uint32_t orig_len; /* actual length of packet */ 55 } __attribute__ ((packed)); 56 #define PCAP_PKT_SIZE (sizeof(struct pcap_pkt)) 57 58 struct pcap { 59 int fd; 60 bool closed; 61 uint32_t type; 62 uint32_t snaplen; 63 size_t size; 64 }; 65 66 struct pcap *pcap_open(const char *pathname) 67 { 68 struct pcap *pcap; 69 struct pcap_hdr hdr; 70 ssize_t len; 71 72 pcap = l_new(struct pcap, 1); 73 74 pcap->fd = open(pathname, O_RDONLY | O_CLOEXEC); 75 if (pcap->fd < 0) { 76 perror("Failed to open PCAP file"); 77 l_free(pcap); 78 return NULL; 79 } 80 81 len = read(pcap->fd, &hdr, PCAP_HDR_SIZE); 82 if (len < 0) { 83 perror("Failed to read PCAP header"); 84 goto failed; 85 } 86 87 if (len != PCAP_HDR_SIZE) { 88 fprintf(stderr, "Wrong PCAP header size\n"); 89 goto failed; 90 } 91 92 if (hdr.magic_number != 0xa1b2c3d4) { 93 fprintf(stderr, "Wrong PCAP header magic\n"); 94 goto failed; 95 } 96 97 if (hdr.version_major != 2 || hdr.version_minor != 4) { 98 fprintf(stderr, "Wrong PCAP version number\n"); 99 goto failed; 100 } 101 102 pcap->closed = false; 103 pcap->snaplen = hdr.snaplen; 104 pcap->type = hdr.network; 105 106 return pcap; 107 108 failed: 109 close(pcap->fd); 110 l_free(pcap); 111 112 return NULL; 113 } 114 115 116 struct pcap *pcap_create(const char *pathname) 117 { 118 struct pcap *pcap; 119 struct pcap_hdr hdr; 120 ssize_t len; 121 122 pcap = l_new(struct pcap, 1); 123 124 pcap->fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 125 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 126 if (pcap->fd < 0) { 127 perror("Failed to create PCAP file"); 128 l_free(pcap); 129 return NULL; 130 } 131 132 pcap->closed = false; 133 pcap->snaplen = 0x0000ffff; 134 pcap->type = 0x00000071; 135 136 memset(&hdr, 0, sizeof(hdr)); 137 hdr.magic_number = 0xa1b2c3d4; 138 hdr.version_major = 0x0002; 139 hdr.version_minor = 0x0004; 140 hdr.thiszone = 0; 141 hdr.sigfigs = 0; 142 hdr.snaplen = pcap->snaplen; 143 hdr.network = pcap->type; 144 145 len = write(pcap->fd, &hdr, PCAP_HDR_SIZE); 146 if (len < 0) { 147 perror("Failed to write PCAP header"); 148 goto failed; 149 } 150 151 if (len != PCAP_HDR_SIZE) { 152 fprintf(stderr, "Written PCAP header size mismatch\n"); 153 goto failed; 154 } 155 156 pcap->size += len; 157 158 return pcap; 159 160 failed: 161 close(pcap->fd); 162 l_free(pcap); 163 164 return NULL; 165 } 166 167 void pcap_close(struct pcap *pcap) 168 { 169 if (!pcap) 170 return; 171 172 if (pcap->fd >= 0) 173 close(pcap->fd); 174 175 l_free(pcap); 176 } 177 178 uint32_t pcap_get_type(struct pcap *pcap) 179 { 180 if (!pcap) 181 return PCAP_TYPE_INVALID; 182 183 return pcap->type; 184 } 185 186 uint32_t pcap_get_snaplen(struct pcap *pcap) 187 { 188 if (!pcap) 189 return 0; 190 191 return pcap->snaplen; 192 } 193 194 size_t pcap_get_size(struct pcap *pcap) 195 { 196 return pcap->size; 197 } 198 199 bool pcap_read(struct pcap *pcap, struct timeval *tv, 200 void *data, uint32_t size, uint32_t *len, uint32_t *real_len) 201 { 202 struct pcap_pkt pkt; 203 uint32_t toread; 204 ssize_t bytes_read; 205 206 if (!pcap) 207 return false; 208 209 if (pcap->closed) 210 return false; 211 212 bytes_read = read(pcap->fd, &pkt, PCAP_PKT_SIZE); 213 if (bytes_read != PCAP_PKT_SIZE) { 214 pcap->closed = true; 215 return false; 216 } 217 218 if (pkt.incl_len > size) 219 toread = size; 220 else 221 toread = pkt.incl_len; 222 223 bytes_read = read(pcap->fd, data, toread); 224 if (bytes_read < 0) { 225 pcap->closed = true; 226 return false; 227 } 228 229 if ((uint32_t) bytes_read < pkt.incl_len) { 230 if (lseek(pcap->fd, pkt.incl_len - bytes_read, SEEK_CUR) < 0) { 231 pcap->closed = true; 232 return false; 233 } 234 } 235 236 if (tv) { 237 tv->tv_sec = pkt.ts_sec; 238 tv->tv_usec = pkt.ts_usec; 239 } 240 241 if (len) 242 *len = toread; 243 244 if (real_len) 245 *real_len = pkt.incl_len; 246 247 return true; 248 } 249 250 bool pcap_write(struct pcap *pcap, const struct timeval *tv, 251 const void *phdr, uint32_t plen, 252 const void *data, uint32_t size) 253 { 254 struct iovec iov[3]; 255 struct pcap_pkt pkt; 256 ssize_t written; 257 258 if (!pcap) 259 return false; 260 261 if (pcap->closed) 262 return false; 263 264 memset(&pkt, 0, sizeof(pkt)); 265 if (tv) { 266 pkt.ts_sec = tv->tv_sec; 267 pkt.ts_usec = tv->tv_usec; 268 } 269 pkt.incl_len = plen + size; 270 pkt.orig_len = plen + size; 271 272 iov[0].iov_base = &pkt; 273 iov[0].iov_len = PCAP_PKT_SIZE; 274 iov[1].iov_base = (void *) phdr; 275 iov[1].iov_len = plen; 276 iov[2].iov_base = (void *) data; 277 iov[2].iov_len = size; 278 279 written = writev(pcap->fd, iov, 3); 280 if (written < 0) { 281 pcap->closed = true; 282 return false; 283 } 284 285 if (written < (ssize_t) (PCAP_PKT_SIZE + plen + size)) { 286 pcap->closed = true; 287 return false; 288 } 289 290 pcap->size += written; 291 292 return true; 293 }