c78c238c4de9d4d0e147496b9a8e6f8dca07e850
[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 #include <netinet/ip6.h>
25
26 typedef struct private_ip_packet_t private_ip_packet_t;
27
28 /**
29 * Private additions to ip_packet_t.
30 */
31 struct private_ip_packet_t {
32
33 /**
34 * Public members
35 */
36 ip_packet_t public;
37
38 /**
39 * Source address
40 */
41 host_t *src;
42
43 /**
44 * Destination address
45 */
46 host_t *dst;
47
48 /**
49 * IP packet
50 */
51 chunk_t packet;
52
53 /**
54 * IP version
55 */
56 u_int8_t version;
57
58 /**
59 * Protocol|Next Header field
60 */
61 u_int8_t next_header;
62
63 };
64
65 METHOD(ip_packet_t, get_version, u_int8_t,
66 private_ip_packet_t *this)
67 {
68 return this->version;
69 }
70
71 METHOD(ip_packet_t, get_source, host_t*,
72 private_ip_packet_t *this)
73 {
74 return this->src;
75 }
76
77 METHOD(ip_packet_t, get_destination, host_t*,
78 private_ip_packet_t *this)
79 {
80 return this->dst;
81 }
82
83 METHOD(ip_packet_t, get_encoding, chunk_t,
84 private_ip_packet_t *this)
85 {
86 return this->packet;
87 }
88
89 METHOD(ip_packet_t, get_next_header, u_int8_t,
90 private_ip_packet_t *this)
91 {
92 return this->next_header;
93 }
94
95 METHOD(ip_packet_t, clone, ip_packet_t*,
96 private_ip_packet_t *this)
97 {
98 return ip_packet_create(this->packet);
99 }
100
101 METHOD(ip_packet_t, destroy, void,
102 private_ip_packet_t *this)
103 {
104 this->src->destroy(this->src);
105 this->dst->destroy(this->dst);
106 chunk_free(&this->packet);
107 free(this);
108 }
109
110 /**
111 * Described in header.
112 */
113 ip_packet_t *ip_packet_create(chunk_t packet)
114 {
115 private_ip_packet_t *this;
116 u_int8_t version, next_header;
117 host_t *src, *dst;
118
119 if (packet.len < 1)
120 {
121 DBG1(DBG_ESP, "IP packet too short");
122 goto failed;
123 }
124
125 version = (packet.ptr[0] & 0xf0) >> 4;
126
127 switch (version)
128 {
129 case 4:
130 {
131 struct iphdr *ip;
132
133 if (packet.len < sizeof(struct iphdr))
134 {
135 DBG1(DBG_ESP, "IPv4 packet too short");
136 goto failed;
137 }
138 ip = (struct iphdr*)packet.ptr;
139 src = host_create_from_chunk(AF_INET,
140 chunk_from_thing(ip->saddr), 0);
141 dst = host_create_from_chunk(AF_INET,
142 chunk_from_thing(ip->daddr), 0);
143 next_header = ip->protocol;
144 break;
145 }
146 case 6:
147 {
148 struct ip6_hdr *ip;
149
150 if (packet.len < sizeof(struct ip6_hdr))
151 {
152 DBG1(DBG_ESP, "IPv6 packet too short");
153 goto failed;
154 }
155 ip = (struct ip6_hdr*)packet.ptr;
156 src = host_create_from_chunk(AF_INET6,
157 chunk_from_thing(ip->ip6_src), 0);
158 dst = host_create_from_chunk(AF_INET6,
159 chunk_from_thing(ip->ip6_dst), 0);
160 next_header = ip->ip6_nxt;
161 }
162 default:
163 DBG1(DBG_ESP, "unsupported IP version");
164 goto failed;
165 }
166
167 INIT(this,
168 .public = {
169 .get_version = _get_version,
170 .get_source = _get_source,
171 .get_destination = _get_destination,
172 .get_next_header = _get_next_header,
173 .get_encoding = _get_encoding,
174 .clone = _clone,
175 .destroy = _destroy,
176 },
177 .src = src,
178 .dst = dst,
179 .packet = packet,
180 .version = version,
181 .next_header = next_header,
182 );
183 return &this->public;
184
185 failed:
186 chunk_free(&packet);
187 return NULL;
188 }