| 1 | /* Protocol decodes for layer 2 */ |
|---|
| 2 | #include "libtrace.h" |
|---|
| 3 | #include "protocols.h" |
|---|
| 4 | #include "libtrace_int.h" |
|---|
| 5 | #include <assert.h> |
|---|
| 6 | |
|---|
| 7 | /* Returns the payload from 802.3 ethernet. Type optionally returned in |
|---|
| 8 | * "type" in host byte order. This will return a vlan header. |
|---|
| 9 | */ |
|---|
| 10 | void *trace_get_payload_from_ethernet(void *ethernet, |
|---|
| 11 | uint16_t *type, |
|---|
| 12 | uint32_t *remaining) |
|---|
| 13 | { |
|---|
| 14 | libtrace_ether_t *eth = (libtrace_ether_t*)ethernet; |
|---|
| 15 | |
|---|
| 16 | if (remaining) { |
|---|
| 17 | if (*remaining < sizeof(*eth)) |
|---|
| 18 | return NULL; |
|---|
| 19 | *remaining-=sizeof(*eth); |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | if (type) |
|---|
| 23 | *type = ntohs(eth->ether_type); |
|---|
| 24 | |
|---|
| 25 | return (void*)((char *)eth + sizeof(*eth)); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | /* skip any 802.1q headers if necessary |
|---|
| 29 | * type is input/output |
|---|
| 30 | */ |
|---|
| 31 | void *trace_get_vlan_payload_from_ethernet_payload(void *ethernet, uint16_t *type, |
|---|
| 32 | uint32_t *remaining) |
|---|
| 33 | { |
|---|
| 34 | assert(type && "You must pass a type in!"); |
|---|
| 35 | |
|---|
| 36 | if (*type == 0x8100) { |
|---|
| 37 | libtrace_8021q_t *vlanhdr = (libtrace_8021q_t *)ethernet; |
|---|
| 38 | |
|---|
| 39 | if (remaining) { |
|---|
| 40 | if (*remaining < sizeof(libtrace_8021q_t)) |
|---|
| 41 | return NULL; |
|---|
| 42 | |
|---|
| 43 | *remaining=*remaining-sizeof(libtrace_8021q_t); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | *type = ntohs(vlanhdr->vlan_ether_type); |
|---|
| 47 | |
|---|
| 48 | return (void*)((char *)ethernet + sizeof(*vlanhdr)); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | return ethernet; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /* skip any MPLS headers if necessary, guessing what the next type is |
|---|
| 55 | * type is input/output. If the next type is "ethernet" this will |
|---|
| 56 | * return a type of 0x0000. |
|---|
| 57 | */ |
|---|
| 58 | void *trace_get_mpls_payload_from_ethernet_payload(void *ethernet, |
|---|
| 59 | uint16_t *type, uint32_t *remaining) |
|---|
| 60 | { |
|---|
| 61 | assert(type && "You must pass a type in!"); |
|---|
| 62 | |
|---|
| 63 | if (*type == 0x8847) { |
|---|
| 64 | if ((((char*)ethernet)[2]&0x01)==0) { |
|---|
| 65 | *type = 0x8847; |
|---|
| 66 | } |
|---|
| 67 | else { |
|---|
| 68 | if (!remaining || *remaining>=5) { |
|---|
| 69 | switch (((char*)ethernet)[4]&0xF0) { |
|---|
| 70 | case 0x40: |
|---|
| 71 | *type = 0x0800; |
|---|
| 72 | break; |
|---|
| 73 | case 0x60: |
|---|
| 74 | *type = 0x86DD; |
|---|
| 75 | break; |
|---|
| 76 | default: |
|---|
| 77 | /* Ethernet */ |
|---|
| 78 | *type = 0; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | ethernet=(char*)ethernet+4; |
|---|
| 83 | if (remaining) { |
|---|
| 84 | if (*remaining<4) |
|---|
| 85 | return NULL; |
|---|
| 86 | else |
|---|
| 87 | *remaining-=4; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | return ethernet; |
|---|
| 92 | } |
|---|
| 93 | else |
|---|
| 94 | return NULL; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | static void *trace_get_payload_from_llcsnap(void *link, |
|---|
| 98 | uint16_t *type, uint32_t *remaining) |
|---|
| 99 | { |
|---|
| 100 | /* 64 byte capture. */ |
|---|
| 101 | libtrace_llcsnap_t *llc = (libtrace_llcsnap_t*)link; |
|---|
| 102 | |
|---|
| 103 | if (remaining) { |
|---|
| 104 | if (*remaining < sizeof(libtrace_llcsnap_t)) |
|---|
| 105 | return NULL; |
|---|
| 106 | *remaining-=(sizeof(libtrace_llcsnap_t)); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | llc = (libtrace_llcsnap_t*)((char *)llc); |
|---|
| 110 | |
|---|
| 111 | if (type) *type = ntohs(llc->type); |
|---|
| 112 | |
|---|
| 113 | return (void*)((char*)llc+sizeof(*llc)); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | static void *trace_get_payload_from_80211(void *link, uint16_t *type, uint32_t *remaining) |
|---|
| 118 | { |
|---|
| 119 | libtrace_80211_t *wifi; |
|---|
| 120 | uint16_t *eth; /* ethertype */ |
|---|
| 121 | int8_t extra = 0; /* how many QoS bytes to skip */ |
|---|
| 122 | |
|---|
| 123 | if (remaining && *remaining < sizeof(libtrace_80211_t)) |
|---|
| 124 | return NULL; |
|---|
| 125 | |
|---|
| 126 | wifi=(libtrace_80211_t*)link; |
|---|
| 127 | |
|---|
| 128 | /* Data packet? */ |
|---|
| 129 | if (wifi->type != 2) { |
|---|
| 130 | return NULL; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | /* If FromDS and ToDS are both set then we have a four-address |
|---|
| 134 | * frame. Otherwise we have a three-address frame */ |
|---|
| 135 | if (!(wifi->to_ds && wifi->from_ds)) |
|---|
| 136 | extra -= 6; |
|---|
| 137 | |
|---|
| 138 | /* Indicates QoS field present, see IEEE802.11e-2005 pg 21 */ |
|---|
| 139 | if (wifi->subtype & 0x8) |
|---|
| 140 | extra += 2; |
|---|
| 141 | |
|---|
| 142 | if (remaining && *remaining < sizeof(*eth)) |
|---|
| 143 | return NULL; |
|---|
| 144 | |
|---|
| 145 | eth=(uint16_t *)((char*)wifi+sizeof(*wifi)+extra); |
|---|
| 146 | |
|---|
| 147 | if (*eth == 0xaaaa) |
|---|
| 148 | /* Payload contains an 802.2 LLC/SNAP frame */ |
|---|
| 149 | return trace_get_payload_from_llcsnap((void *)eth, type, remaining); |
|---|
| 150 | |
|---|
| 151 | /* Otherwise we assume an Ethernet II frame */ |
|---|
| 152 | if (type) *type=ntohs(*eth); |
|---|
| 153 | if (remaining) *remaining = *remaining - sizeof(libtrace_80211_t) - extra - sizeof(*eth); |
|---|
| 154 | |
|---|
| 155 | return (void*)((char*)eth+sizeof(*eth)); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | static void *trace_get_payload_from_ppp(void *link, |
|---|
| 159 | uint16_t *type, uint32_t *remaining) |
|---|
| 160 | { |
|---|
| 161 | /* 64 byte capture. */ |
|---|
| 162 | libtrace_ppp_t *ppp = (libtrace_ppp_t*)link; |
|---|
| 163 | |
|---|
| 164 | if (remaining) { |
|---|
| 165 | if (*remaining < sizeof(libtrace_ppp_t)) |
|---|
| 166 | return NULL; |
|---|
| 167 | *remaining-=sizeof(libtrace_ppp_t); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | if (type) { |
|---|
| 171 | switch(ntohs(ppp->protocol)) { |
|---|
| 172 | case 0x0021: *type = 0x0800; break; |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | return (void*)((char *)ppp+sizeof(*ppp)); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | typedef struct libtrace_chdlc_t { |
|---|
| 181 | uint8_t address; /** 0xF0 for unicast, 0xF8 for multicast */ |
|---|
| 182 | uint8_t control; |
|---|
| 183 | uint16_t ethertype; |
|---|
| 184 | } libtrace_chdlc_t; |
|---|
| 185 | |
|---|
| 186 | static void *trace_get_payload_from_chdlc(void *link, |
|---|
| 187 | uint16_t *type, uint32_t *remaining) |
|---|
| 188 | { |
|---|
| 189 | libtrace_chdlc_t *chdlc = (libtrace_chdlc_t*)link; |
|---|
| 190 | |
|---|
| 191 | if (remaining) { |
|---|
| 192 | if (*remaining < sizeof(libtrace_chdlc_t)) |
|---|
| 193 | return NULL; |
|---|
| 194 | *remaining-=sizeof(libtrace_chdlc_t); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | if (type) { |
|---|
| 198 | *type=ntohs(chdlc->ethertype); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | return (void*)((char *)chdlc+sizeof(*chdlc)); |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | void *trace_get_payload_from_link(void *link, libtrace_linktype_t linktype, |
|---|
| 206 | uint16_t *ethertype, uint32_t *remaining) |
|---|
| 207 | { |
|---|
| 208 | void *l = NULL; |
|---|
| 209 | |
|---|
| 210 | do { |
|---|
| 211 | l = trace_get_payload_from_meta(link, &linktype, remaining); |
|---|
| 212 | if (l != NULL) { |
|---|
| 213 | link=l; |
|---|
| 214 | continue; |
|---|
| 215 | } |
|---|
| 216 | } while (0); |
|---|
| 217 | |
|---|
| 218 | return trace_get_payload_from_layer2(link,linktype,ethertype,remaining); |
|---|
| 219 | |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | DLLEXPORT void *trace_get_layer2(const libtrace_packet_t *packet, |
|---|
| 223 | libtrace_linktype_t *linktype, |
|---|
| 224 | uint32_t *remaining) |
|---|
| 225 | { |
|---|
| 226 | uint32_t dummyrem; |
|---|
| 227 | |
|---|
| 228 | assert(packet != NULL); |
|---|
| 229 | assert(linktype != NULL); |
|---|
| 230 | |
|---|
| 231 | if (remaining == NULL) |
|---|
| 232 | remaining = &dummyrem; |
|---|
| 233 | |
|---|
| 234 | void *meta = trace_get_packet_meta(packet, linktype, remaining); |
|---|
| 235 | |
|---|
| 236 | /* If there are no meta-data headers, we just return the start of the |
|---|
| 237 | * packet buffer, along with the linktype, etc. |
|---|
| 238 | */ |
|---|
| 239 | if (meta == NULL) |
|---|
| 240 | return trace_get_packet_buffer(packet, linktype, remaining); |
|---|
| 241 | |
|---|
| 242 | /* If there are meta-data headers, we need to skip over them until we |
|---|
| 243 | * find a non-meta data header and return that. |
|---|
| 244 | */ |
|---|
| 245 | for(;;) { |
|---|
| 246 | void *nexthdr = trace_get_payload_from_meta(meta, |
|---|
| 247 | linktype, remaining); |
|---|
| 248 | if (nexthdr == NULL) |
|---|
| 249 | return meta; |
|---|
| 250 | meta = nexthdr; |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | DLLEXPORT |
|---|
| 255 | void *trace_get_payload_from_atm(void *link, |
|---|
| 256 | uint8_t *type, uint32_t *remaining) |
|---|
| 257 | { |
|---|
| 258 | libtrace_atm_capture_cell_t *cell; |
|---|
| 259 | if (remaining && *remaining<sizeof(libtrace_atm_capture_cell_t)) |
|---|
| 260 | return NULL; |
|---|
| 261 | cell=(libtrace_atm_capture_cell_t*)link; |
|---|
| 262 | |
|---|
| 263 | if (type) |
|---|
| 264 | *type=cell->pt; |
|---|
| 265 | |
|---|
| 266 | if (remaining) |
|---|
| 267 | *remaining-=sizeof(libtrace_atm_capture_cell_t); |
|---|
| 268 | |
|---|
| 269 | return ((char*)link)+sizeof(libtrace_atm_capture_cell_t); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | DLLEXPORT void *trace_get_payload_from_layer2(void *link, |
|---|
| 275 | libtrace_linktype_t linktype, |
|---|
| 276 | uint16_t *ethertype, |
|---|
| 277 | uint32_t *remaining) |
|---|
| 278 | { |
|---|
| 279 | void *l; |
|---|
| 280 | switch(linktype) { |
|---|
| 281 | /* Packet Metadata headers, not layer2 headers */ |
|---|
| 282 | case TRACE_TYPE_80211_PRISM: |
|---|
| 283 | case TRACE_TYPE_80211_RADIO: |
|---|
| 284 | case TRACE_TYPE_PFLOG: |
|---|
| 285 | case TRACE_TYPE_LINUX_SLL: |
|---|
| 286 | return NULL; |
|---|
| 287 | |
|---|
| 288 | /* duck packets have no payload! */ |
|---|
| 289 | case TRACE_TYPE_DUCK: |
|---|
| 290 | return NULL; |
|---|
| 291 | |
|---|
| 292 | /* The payload is in these packets does |
|---|
| 293 | not correspond to a genuine link-layer |
|---|
| 294 | */ |
|---|
| 295 | case TRACE_TYPE_METADATA: |
|---|
| 296 | return NULL; |
|---|
| 297 | |
|---|
| 298 | case TRACE_TYPE_80211: |
|---|
| 299 | return trace_get_payload_from_80211(link,ethertype,remaining); |
|---|
| 300 | case TRACE_TYPE_ETH: |
|---|
| 301 | return trace_get_payload_from_ethernet(link,ethertype,remaining); |
|---|
| 302 | case TRACE_TYPE_NONE: |
|---|
| 303 | if ((*(char*)link&0xF0) == 0x40) |
|---|
| 304 | *ethertype=0x0800; |
|---|
| 305 | else if ((*(char*)link&0xF0) == 0x60) |
|---|
| 306 | *ethertype=0x86DD; |
|---|
| 307 | return link; /* I love the simplicity */ |
|---|
| 308 | case TRACE_TYPE_PPP: |
|---|
| 309 | return trace_get_payload_from_ppp(link,ethertype,remaining); |
|---|
| 310 | case TRACE_TYPE_ATM: |
|---|
| 311 | l=trace_get_payload_from_atm(link,NULL,remaining); |
|---|
| 312 | /* FIXME: We shouldn't skip llcsnap here, we should return |
|---|
| 313 | * an ethertype for it (somehow) |
|---|
| 314 | */ |
|---|
| 315 | return (l ? trace_get_payload_from_llcsnap(l, |
|---|
| 316 | ethertype, remaining):NULL); |
|---|
| 317 | case TRACE_TYPE_LLCSNAP: |
|---|
| 318 | return trace_get_payload_from_llcsnap(link,ethertype,remaining); |
|---|
| 319 | |
|---|
| 320 | case TRACE_TYPE_HDLC_POS: |
|---|
| 321 | return trace_get_payload_from_chdlc(link,ethertype, |
|---|
| 322 | remaining); |
|---|
| 323 | /* TODO: Unsupported */ |
|---|
| 324 | case TRACE_TYPE_POS: |
|---|
| 325 | case TRACE_TYPE_AAL5: |
|---|
| 326 | return NULL; |
|---|
| 327 | } |
|---|
| 328 | return NULL; |
|---|
| 329 | |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | |
|---|