trafgen: fix NULL pointer dereference in -i option parsing
[netsniff-ng-new.git] / proto_ip_authentication_hdr.c
blobb2b7a2610569d276f25ed4dba47e0803e70c05a7
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 "dissector_eth.h"
15 #include "built_in.h"
16 #include "pkt_buff.h"
18 struct auth_hdr {
19 uint8_t h_next_header;
20 uint8_t h_payload_len;
21 uint16_t h_reserved;
22 uint32_t h_spi;
23 uint32_t h_snf;
24 } __packed;
26 static void auth_hdr(struct pkt_buff *pkt)
28 size_t i, hdr_len;
29 struct auth_hdr *auth_ops;
31 auth_ops = (struct auth_hdr *) pkt_pull(pkt, sizeof(*auth_ops));
32 if (auth_ops == NULL)
33 return;
35 hdr_len = (auth_ops->h_payload_len * 4) + 8;
37 tprintf(" [ Authentication Header ");
38 tprintf("NextHdr (%u), ", auth_ops->h_next_header);
39 if (hdr_len > pkt_len(pkt)) {
40 tprintf("HdrLen (%u, %zd Bytes %s), ",
41 auth_ops->h_payload_len, hdr_len,
42 colorize_start_full(black, red)
43 "invalid" colorize_end());
44 return;
46 tprintf("HdrLen (%u, %zd Bytes), ",auth_ops->h_payload_len, hdr_len);
47 tprintf("Reserved (0x%x), ", ntohs(auth_ops->h_reserved));
48 /* TODO
49 * Upgrade for Extended (64-bit) Sequence Number
50 * http://tools.ietf.org/html/rfc4302#section-2.5.1
52 tprintf("SPI (0x%x), ", ntohl(auth_ops->h_spi));
53 tprintf("SNF (0x%x), ", ntohl(auth_ops->h_snf));
54 tprintf("ICV 0x");
55 for (i = sizeof(struct auth_hdr); i < hdr_len; i++) {
56 uint8_t *data = pkt_pull(pkt, 1);
58 if (data == NULL) {
59 tprintf("%sinvalid%s", colorize_start_full(black, red),
60 colorize_end());
61 break;
64 tprintf("%02x", *data);
66 tprintf(" ]\n");
68 pkt_set_dissector(pkt, &eth_lay3, auth_ops->h_next_header);
71 static void auth_hdr_less(struct pkt_buff *pkt)
73 ssize_t hdr_len;
74 struct auth_hdr *auth_ops;
76 auth_ops = (struct auth_hdr *) pkt_pull(pkt, sizeof(*auth_ops));
77 if (auth_ops == NULL)
78 return;
80 hdr_len = (auth_ops->h_payload_len * 4) + 8;
81 if (hdr_len > pkt_len(pkt) || hdr_len < 0)
82 return;
84 tprintf(" AH");
86 pkt_pull(pkt, hdr_len - sizeof(*auth_ops));
87 pkt_set_dissector(pkt, &eth_lay3, auth_ops->h_next_header);
90 struct protocol ip_auth_ops = {
91 .key = 0x33,
92 .print_full = auth_hdr,
93 .print_less = auth_hdr_less,