Contiki-NG
ip64-addrmap.c
1/*
2 * Copyright (c) 2012, Thingsquare, http://www.thingsquare.com/.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32#include "ip64/ip64-addrmap.h"
33#include "ip64/ip64.h"
34#include "lib/memb.h"
35#include "lib/list.h"
36#include "ip64-conf.h"
37#include "lib/random.h"
38
39#include <string.h>
40
41/*---------------------------------------------------------------------------*/
42
43#include "sys/log.h"
44#define LOG_MODULE "IP64"
45#define LOG_LEVEL LOG_LEVEL_IP64
46
47/*---------------------------------------------------------------------------*/
48
49#ifdef IP64_ADDRMAP_CONF_ENTRIES
50#define NUM_ENTRIES IP64_ADDRMAP_CONF_ENTRIES
51#else /* IP64_ADDRMAP_CONF_ENTRIES */
52#define NUM_ENTRIES 32
53#endif /* IP64_ADDRMAP_CONF_ENTRIES */
54
55MEMB(entrymemb, struct ip64_addrmap_entry, NUM_ENTRIES);
56LIST(entrylist);
57
58#define FIRST_MAPPED_PORT 10000
59#define LAST_MAPPED_PORT 20000
60static uint16_t mapped_port = FIRST_MAPPED_PORT;
61
62/*---------------------------------------------------------------------------*/
63struct ip64_addrmap_entry *
64ip64_addrmap_list(void)
65{
66 return list_head(entrylist);
67}
68/*---------------------------------------------------------------------------*/
69void
70ip64_addrmap_init(void)
71{
72 memb_init(&entrymemb);
73 list_init(entrylist);
74 mapped_port = FIRST_MAPPED_PORT;
75}
76/*---------------------------------------------------------------------------*/
77static void
78check_age(void)
79{
80 struct ip64_addrmap_entry *m;
81
82 /* Walk through the list of address mappings, throw away the ones
83 that are too old. */
84 m = list_head(entrylist);
85 while(m != NULL) {
86 if(timer_expired(&m->timer)) {
87 list_remove(entrylist, m);
88 memb_free(&entrymemb, m);
89 m = list_head(entrylist);
90 } else {
91 m = list_item_next(m);
92 }
93 }
94}
95/*---------------------------------------------------------------------------*/
96static int
97recycle(void)
98{
99 /* Find the oldest recyclable mapping and remove it. */
100 struct ip64_addrmap_entry *m, *oldest;
101
102 /* Walk through the list of address mappings, throw away the ones
103 that are too old. */
104
105 oldest = NULL;
106 for(m = list_head(entrylist);
107 m != NULL;
108 m = list_item_next(m)) {
109 if(m->flags & FLAGS_RECYCLABLE) {
110 if(oldest == NULL) {
111 oldest = m;
112 } else {
113 if(timer_remaining(&m->timer) <
114 timer_remaining(&oldest->timer)) {
115 oldest = m;
116 }
117 }
118 }
119 }
120
121 /* If we found an oldest recyclable entry, remove it and return
122 non-zero. */
123 if(oldest != NULL) {
124 list_remove(entrylist, oldest);
125 memb_free(&entrymemb, oldest);
126 return 1;
127 }
128
129 return 0;
130}
131/*---------------------------------------------------------------------------*/
132struct ip64_addrmap_entry *
133ip64_addrmap_lookup(const uip_ip6addr_t *ip6addr,
134 uint16_t ip6port,
135 const uip_ip4addr_t *ip4addr,
136 uint16_t ip4port,
137 uint8_t protocol)
138{
139 struct ip64_addrmap_entry *m;
140
141 LOG_DBG("lookup ip4port %d ip6port %d\n", uip_htons(ip4port),
142 uip_htons(ip6port));
143 check_age();
144 for(m = list_head(entrylist); m != NULL; m = list_item_next(m)) {
145 LOG_DBG("protocol %d %d, ip4port %d %d, ip6port %d %d, ip4 %d ip6 %d\n",
146 m->protocol, protocol,
147 m->ip4port, ip4port,
148 m->ip6port, ip6port,
149 uip_ip4addr_cmp(&m->ip4addr, ip4addr),
150 uip_ip6addr_cmp(&m->ip6addr, ip6addr));
151 if(m->protocol == protocol &&
152 m->ip4port == ip4port &&
153 m->ip6port == ip6port &&
154 uip_ip4addr_cmp(&m->ip4addr, ip4addr) &&
155 uip_ip6addr_cmp(&m->ip6addr, ip6addr)) {
156 m->ip6to4++;
157 return m;
158 }
159 }
160 return NULL;
161}
162/*---------------------------------------------------------------------------*/
163struct ip64_addrmap_entry *
164ip64_addrmap_lookup_port(uint16_t mapped_port, uint8_t protocol)
165{
166 struct ip64_addrmap_entry *m;
167
168 check_age();
169 for(m = list_head(entrylist); m != NULL; m = list_item_next(m)) {
170 LOG_DBG("mapped port %d %d, protocol %d %d\n",
171 m->mapped_port, mapped_port,
172 m->protocol, protocol);
173 if(m->mapped_port == mapped_port &&
174 m->protocol == protocol) {
175 m->ip4to6++;
176 return m;
177 }
178 }
179 return NULL;
180}
181/*---------------------------------------------------------------------------*/
182static void
183increase_mapped_port(void)
184{
185 mapped_port = (random_rand() % (LAST_MAPPED_PORT - FIRST_MAPPED_PORT)) +
186 FIRST_MAPPED_PORT;
187}
188/*---------------------------------------------------------------------------*/
189struct ip64_addrmap_entry *
190ip64_addrmap_create(const uip_ip6addr_t *ip6addr,
191 uint16_t ip6port,
192 const uip_ip4addr_t *ip4addr,
193 uint16_t ip4port,
194 uint8_t protocol)
195{
196 struct ip64_addrmap_entry *m;
197
198 check_age();
199 m = memb_alloc(&entrymemb);
200 if(m == NULL) {
201 /* We could not allocate an entry, try to recycle one and try to
202 allocate again. */
203 if(recycle()) {
204 m = memb_alloc(&entrymemb);
205 }
206 }
207 if(m != NULL) {
208 uip_ip4addr_copy(&m->ip4addr, ip4addr);
209 m->ip4port = ip4port;
210 uip_ip6addr_copy(&m->ip6addr, ip6addr);
211 m->ip6port = ip6port;
212 m->protocol = protocol;
213 m->flags = FLAGS_NONE;
214 m->ip6to4 = 1;
215 m->ip4to6 = 0;
216 timer_set(&m->timer, 0);
217
218 /* Pick a new, unused local port. First make sure that the
219 mapped_port number does not belong to any active connection. If
220 so, we keep increasing the mapped_port until we're free. */
221 {
222 struct ip64_addrmap_entry *n;
223 n = list_head(entrylist);
224 while(n != NULL) {
225 if(n->mapped_port == mapped_port) {
226 increase_mapped_port();
227 n = list_head(entrylist);
228 } else {
229 n = list_item_next(m);
230 }
231 }
232 }
233 m->mapped_port = mapped_port;
234 increase_mapped_port();
235
236 list_add(entrylist, m);
237 return m;
238 }
239 return NULL;
240}
241/*---------------------------------------------------------------------------*/
242void
243ip64_addrmap_set_lifetime(struct ip64_addrmap_entry *e,
244 clock_time_t time)
245{
246 if(e != NULL) {
247 timer_set(&e->timer, time);
248 }
249}
250/*---------------------------------------------------------------------------*/
251void
252ip64_addrmap_set_recycleble(struct ip64_addrmap_entry *e)
253{
254 if(e != NULL) {
255 e->flags |= FLAGS_RECYCLABLE;
256 }
257}
258/*---------------------------------------------------------------------------*/
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:58
void list_init(list_t list)
Initialize a list.
Definition: list.c:57
#define LIST(name)
Declare a linked list.
Definition: list.h:89
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:89
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
Definition: list.c:152
void * list_item_next(const void *item)
Get the next item following this item.
Definition: list.c:203
void * list_head(const_list_t list)
Get a pointer to the first element of a list.
Definition: list.c:63
int memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:78
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
Definition: memb.c:52
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:91
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:123
clock_time_t timer_remaining(struct timer *t)
The time until the timer expires.
Definition: timer.c:143
#define uip_ip4addr_cmp(addr1, addr2)
Compare two IP addresses.
Definition: uip.h:998
uint16_t uip_htons(uint16_t val)
Convert a 16-bit quantity from host byte order to network byte order.
Definition: uip6.c:2339
Linked list manipulation routines.
Header file for the logging system.
Memory block allocation routines.
Representation of an IP address.
Definition: uip.h:95