pcap_mm: Fix compiler warnings
[netsniff-ng.git] / proto_ip_authentication_hdr.c
blob2e0116395fa9ad353e812792ff46cdf5dd80ebd0
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2012 Markus Amend <markus@netsniff-ng.org>, Deutsche Flugsicherung GmbH
4 * Subject to the GPL, version 2.
6 * IP Authentication Header described in RFC4302
7 */
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <netinet/in.h> /* for ntohs() */
13 #include "proto.h"
14 #include "protos.h"
15 #include "dissector_eth.h"
16 #include "built_in.h"
17 #include "pkt_buff.h"
19 struct auth_hdr {
20 uint8_t h_next_header;
21 uint8_t h_payload_len;
22 uint16_t h_reserved;
23 uint32_t h_spi;
24 uint32_t h_snf;
25 } __packed;
27 static void auth_hdr(struct pkt_buff *pkt)
29 size_t i, hdr_len;
30 struct auth_hdr *auth_ops;
32 auth_ops = (struct auth_hdr *) pkt_pull(pkt, sizeof(*auth_ops));
33 if (auth_ops == NULL)
34 return;
36 hdr_len = (auth_ops->h_payload_len * 4) + 8;
38 tprintf(" [ Authentication Header ");
39 tprintf("NextHdr (%u), ", auth_ops->h_next_header);
40 if (hdr_len > pkt_len(pkt)) {
41 tprintf("HdrLen (%u, %zd Bytes %s), ",
42 auth_ops->h_payload_len, hdr_len,
43 colorize_start_full(black, red)
44 "invalid" colorize_end());
45 return;
47 tprintf("HdrLen (%u, %zd Bytes), ",auth_ops->h_payload_len, hdr_len);
48 tprintf("Reserved (0x%x), ", ntohs(auth_ops->h_reserved));
49 /* TODO
50 * Upgrade for Extended (64-bit) Sequence Number
51 * http://tools.ietf.org/html/rfc4302#section-2.5.1
53 tprintf("SPI (0x%x), ", ntohl(auth_ops->h_spi));
54 tprintf("SNF (0x%x), ", ntohl(auth_ops->h_snf));
55 tprintf("ICV 0x");
56 for (i = sizeof(struct auth_hdr); i < hdr_len; i++) {
57 uint8_t *data = pkt_pull(pkt, 1);
59 if (data == NULL) {
60 tprintf("%sinvalid%s", colorize_start_full(black, red),
61 colorize_end());
62 break;
65 tprintf("%02x", *data);
67 tprintf(" ]\n");
69 pkt_set_proto(pkt, &eth_lay3, auth_ops->h_next_header);
72 static void auth_hdr_less(struct pkt_buff *pkt)
74 ssize_t hdr_len;
75 struct auth_hdr *auth_ops;
77 auth_ops = (struct auth_hdr *) pkt_pull(pkt, sizeof(*auth_ops));
78 if (auth_ops == NULL)
79 return;
81 hdr_len = (auth_ops->h_payload_len * 4) + 8;
82 if (hdr_len > pkt_len(pkt) || hdr_len < 0)
83 return;
85 tprintf(" AH");
87 pkt_pull(pkt, hdr_len - sizeof(*auth_ops));
88 pkt_set_proto(pkt, &eth_lay3, auth_ops->h_next_header);
91 struct protocol ip_auth_ops = {
92 .key = 0x33,
93 .print_full = auth_hdr,
94 .print_less = auth_hdr_less,