struct iphdr is Linux specific use struct ip instead
[strongswan.git] / src / libipsec / ip_packet.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16
17 #include "ip_packet.h"
18
19 #include <library.h>
20 #include <debug.h>
21
22 #include <netinet/in.h>
23 #include <netinet/ip.h>
24 #ifdef HAVE_NETINET_IP6_H
25 #include <netinet/ip6.h>
26 #endif
27
28 typedef struct private_ip_packet_t private_ip_packet_t;
29
30 /**
31 * Private additions to ip_packet_t.
32 */
33 struct private_ip_packet_t {
34
35 /**
36 * Public members
37 */
38 ip_packet_t public;
39
40 /**
41 * Source address
42 */
43 host_t *src;
44
45 /**
46 * Destination address
47 */
48 host_t *dst;
49
50 /**
51 * IP packet
52 */
53 chunk_t packet;
54
55 /**
56 * IP version
57 */
58 u_int8_t version;
59
60 /**
61 * Protocol|Next Header field
62 */
63 u_int8_t next_header;
64
65 };
66
67 METHOD(ip_packet_t, get_version, u_int8_t,
68 private_ip_packet_t *this)
69 {
70 return this->version;
71 }
72
73 METHOD(ip_packet_t, get_source, host_t*,
74 private_ip_packet_t *this)
75 {
76 return this->src;
77 }
78
79 METHOD(ip_packet_t, get_destination, host_t*,
80 private_ip_packet_t *this)
81 {
82 return this->dst;
83 }
84
85 METHOD(ip_packet_t, get_encoding, chunk_t,
86 private_ip_packet_t *this)
87 {
88 return this->packet;
89 }
90
91 METHOD(ip_packet_t, get_next_header, u_int8_t,
92 private_ip_packet_t *this)
93 {
94 return this->next_header;
95 }
96
97 METHOD(ip_packet_t, clone, ip_packet_t*,
98 private_ip_packet_t *this)
99 {
100 return ip_packet_create(this->packet);
101 }
102
103 METHOD(ip_packet_t, destroy, void,
104 private_ip_packet_t *this)
105 {
106 this->src->destroy(this->src);
107 this->dst->destroy(this->dst);
108 chunk_free(&this->packet);
109 free(this);
110 }
111
112 /**
113 * Described in header.
114 */
115 ip_packet_t *ip_packet_create(chunk_t packet)
116 {
117 private_ip_packet_t *this;
118 u_int8_t version, next_header;
119 host_t *src, *dst;
120
121 if (packet.len < 1)
122 {
123 DBG1(DBG_ESP, "IP packet too short");
124 goto failed;
125 }
126
127 version = (packet.ptr[0] & 0xf0) >> 4;
128
129 switch (version)
130 {
131 case 4:
132 {
133 struct ip *ip;
134
135 if (packet.len < sizeof(struct ip))
136 {
137 DBG1(DBG_ESP, "IPv4 packet too short");
138 goto failed;
139 }
140 ip = (struct ip*)packet.ptr;
141 src = host_create_from_chunk(AF_INET,
142 chunk_from_thing(ip->ip_src), 0);
143 dst = host_create_from_chunk(AF_INET,
144 chunk_from_thing(ip->ip_dst), 0);
145 next_header = ip->ip_p;
146 break;
147 }
148 #ifdef HAVE_NETINET_IP6_H
149 case 6:
150 {
151 struct ip6_hdr *ip;
152
153 if (packet.len < sizeof(struct ip6_hdr))
154 {
155 DBG1(DBG_ESP, "IPv6 packet too short");
156 goto failed;
157 }
158 ip = (struct ip6_hdr*)packet.ptr;
159 src = host_create_from_chunk(AF_INET6,
160 chunk_from_thing(ip->ip6_src), 0);
161 dst = host_create_from_chunk(AF_INET6,
162 chunk_from_thing(ip->ip6_dst), 0);
163 next_header = ip->ip6_nxt;
164 }
165 #endif /* HAVE_NETINET_IP6_H */
166 default:
167 DBG1(DBG_ESP, "unsupported IP version");
168 goto failed;
169 }
170
171 INIT(this,
172 .public = {
173 .get_version = _get_version,
174 .get_source = _get_source,
175 .get_destination = _get_destination,
176 .get_next_header = _get_next_header,
177 .get_encoding = _get_encoding,
178 .clone = _clone,
179 .destroy = _destroy,
180 },
181 .src = src,
182 .dst = dst,
183 .packet = packet,
184 .version = version,
185 .next_header = next_header,
186 );
187 return &this->public;
188
189 failed:
190 chunk_free(&packet);
191 return NULL;
192 }