Contiki 3.x
uip-over-mesh.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007, Swedish Institute of Computer Science.
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  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Code for tunnelling uIP packets over the Rime mesh routing module
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #include <stdio.h>
41 
42 #include "net/ipv4/uip-fw.h"
43 #include "net/ipv4/uip-over-mesh.h"
45 #include "net/rime/route.h"
46 #include "net/rime/trickle.h"
47 
48 #define ROUTE_TRICKLE_INTERVAL CLOCK_SECOND * 32
49 #define ROUTE_DISCOVERY_INTERVAL CLOCK_SECOND * 4
50 #define ROUTE_TIMEOUT CLOCK_SECOND * 4
51 
52 static struct queuebuf *queued_packet;
53 static linkaddr_t queued_receiver;
54 
55  /* Connection for route discovery: */
56 static struct route_discovery_conn route_discovery;
57 
58 /* Connection for sending data packets to the next hop node: */
59 static struct unicast_conn dataconn;
60 
61 /* Connection for sending gateway announcement message to the entire
62  network: */
63 static struct trickle_conn gateway_announce_conn;
64 
65 #define DEBUG 0
66 #if DEBUG
67 #include <stdio.h>
68 #define PRINTF(...) printf(__VA_ARGS__)
69 #else
70 #define PRINTF(...)
71 #endif
72 
73 #define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
74 
75 static struct uip_fw_netif *gw_netif;
76 static linkaddr_t gateway;
77 static uip_ipaddr_t netaddr, netmask;
78 
79 /*---------------------------------------------------------------------------*/
80 static void
81 recv_data(struct unicast_conn *c, const linkaddr_t *from)
82 {
83  struct route_entry *e;
84  linkaddr_t source;
85 
87 
88  source.u8[0] = BUF->srcipaddr.u8[2];
89  source.u8[1] = BUF->srcipaddr.u8[3];
90 
91  e = route_lookup(&source);
92  if(e == NULL) {
93  route_add(&source, from, 10, 0);
94  } else {
95  route_refresh(e);
96  }
97 
98  /* If we received data via a gateway, we refresh the gateway route.
99  * Note: we refresh OUR gateway route, although we are not sure it forwarded the data. */
100  if(!uip_ipaddr_maskcmp(&BUF->srcipaddr, &netaddr, &netmask)) {
101  e = route_lookup(&gateway);
102  if(e != NULL) {
103  route_refresh(e);
104  }
105  }
106 
107 
108  PRINTF("uip-over-mesh: %d.%d: recv_data with len %d\n",
110  tcpip_input();
111 }
112 /*---------------------------------------------------------------------------*/
113 static void
114 send_data(linkaddr_t *next)
115 {
116  PRINTF("uip-over-mesh: %d.%d: send_data with len %d\n",
118  packetbuf_totlen());
119  unicast_send(&dataconn, next);
120 }
121 /*---------------------------------------------------------------------------*/
122 static void
123 new_route(struct route_discovery_conn *c, const linkaddr_t *to)
124 {
125  struct route_entry *rt;
126 
127  if(queued_packet) {
128  PRINTF("uip-over-mesh: new route, sending queued packet\n");
129 
130  queuebuf_to_packetbuf(queued_packet);
131  queuebuf_free(queued_packet);
132  queued_packet = NULL;
133 
134  rt = route_lookup(&queued_receiver);
135  if(rt) {
136  route_decay(rt);
137  send_data(&queued_receiver);
138  }
139  }
140 }
141 /*---------------------------------------------------------------------------*/
142 static void
143 timedout(struct route_discovery_conn *c)
144 {
145  PRINTF("uip-over-mesh: packet timed out\n");
146  if(queued_packet) {
147  PRINTF("uip-over-mesh: freeing queued packet\n");
148  queuebuf_free(queued_packet);
149  queued_packet = NULL;
150  }
151 }
152 /*---------------------------------------------------------------------------*/
153 static const struct unicast_callbacks data_callbacks = { recv_data };
154 static const struct route_discovery_callbacks rdc = { new_route, timedout };
155 /*---------------------------------------------------------------------------*/
156 struct gateway_msg {
157  linkaddr_t gateway;
158 };
159 
160 static uint8_t is_gateway;
161 
162 static void
163 gateway_announce_recv(struct trickle_conn *c)
164 {
165  struct gateway_msg *msg;
166  msg = packetbuf_dataptr();
167  PRINTF("%d.%d: gateway message: %d.%d\n",
169  msg->gateway.u8[0], msg->gateway.u8[1]);
170 
171  if(!is_gateway) {
172  uip_over_mesh_set_gateway(&msg->gateway);
173  }
174 
175 }
176 /*---------------------------------------------------------------------------*/
177 void
178 uip_over_mesh_make_announced_gateway(void)
179 {
180  struct gateway_msg msg;
181  /* Make this node the gateway node, unless it already is the
182  gateway. */
183  if(!is_gateway) {
184  PRINTF("%d.%d: making myself the gateway\n",
186  uip_over_mesh_set_gateway(&linkaddr_node_addr);
187  linkaddr_copy(&(msg.gateway), &linkaddr_node_addr);
188  packetbuf_copyfrom(&msg, sizeof(struct gateway_msg));
189  trickle_send(&gateway_announce_conn);
190  is_gateway = 1;
191  }
192 }
193 const static struct trickle_callbacks trickle_call = {gateway_announce_recv};
194 /*---------------------------------------------------------------------------*/
195 void
196 uip_over_mesh_init(uint16_t channels)
197 {
198 
199  PRINTF("Our address is %d.%d (%d.%d.%d.%d)\n",
201  uip_hostaddr.u8[0], uip_hostaddr.u8[1],
202  uip_hostaddr.u8[2], uip_hostaddr.u8[3]);
203 
204  unicast_open(&dataconn, channels, &data_callbacks);
205  route_discovery_open(&route_discovery, ROUTE_DISCOVERY_INTERVAL,
206  channels + 1, &rdc);
207  trickle_open(&gateway_announce_conn, ROUTE_TRICKLE_INTERVAL, channels + 3,
208  &trickle_call);
209 
210  route_init();
211  /* Set lifetime to 30 seconds for non-refreshed routes. */
212  route_set_lifetime(30);
213 }
214 /*---------------------------------------------------------------------------*/
215 uint8_t
216 uip_over_mesh_send(void)
217 {
218  linkaddr_t receiver;
219  struct route_entry *rt;
220 
221  /* This function is called by the uip-fw module to send out an IP
222  packet. We try to send the IP packet to the next hop route, or we
223  queue the packet and send out a route request for the final
224  receiver of the packet. */
225 
226  /* Packets destined to this network is sent using mesh, whereas
227  packets destined to a network outside this network is sent towards
228  the gateway node. */
229 
230  if(uip_ipaddr_maskcmp(&BUF->destipaddr, &netaddr, &netmask)) {
231  receiver.u8[0] = BUF->destipaddr.u8[2];
232  receiver.u8[1] = BUF->destipaddr.u8[3];
233  } else {
234  if(linkaddr_cmp(&gateway, &linkaddr_node_addr)) {
235  PRINTF("uip_over_mesh_send: I am gateway, packet to %d.%d.%d.%d to local interface\n",
236  uip_ipaddr_to_quad(&BUF->destipaddr));
237  if(gw_netif != NULL) {
238  return gw_netif->output();
239  }
240  return UIP_FW_DROPPED;
241  } else if(linkaddr_cmp(&gateway, &linkaddr_null)) {
242  PRINTF("uip_over_mesh_send: No gateway setup, dropping packet\n");
243  return UIP_FW_OK;
244  } else {
245  PRINTF("uip_over_mesh_send: forwarding packet to %d.%d.%d.%d towards gateway %d.%d\n",
246  uip_ipaddr_to_quad(&BUF->destipaddr),
247  gateway.u8[0], gateway.u8[1]);
248  linkaddr_copy(&receiver, &gateway);
249  }
250  }
251 
252  PRINTF("uIP over mesh send to %d.%d with len %d\n",
253  receiver.u8[0], receiver.u8[1],
254  uip_len);
255 
256 
258 
259  /* Send TCP data with the PACKETBUF_ATTR_ERELIABLE set so that
260  an underlying power-saving MAC layer knows that it should be
261  waiting for an ACK. */
262  if(BUF->proto == UIP_PROTO_TCP) {
263  packetbuf_set_attr(PACKETBUF_ATTR_ERELIABLE, 1);
264  packetbuf_set_attr(PACKETBUF_ATTR_RELIABLE, 1);
265  /* packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE, PACKETBUF_ATTR_PACKET_TYPE_STREAM);*/
266  }
267 
268  rt = route_lookup(&receiver);
269  if(rt == NULL) {
270  PRINTF("uIP over mesh no route to %d.%d\n", receiver.u8[0], receiver.u8[1]);
271  if(queued_packet == NULL) {
272  queued_packet = queuebuf_new_from_packetbuf();
273  linkaddr_copy(&queued_receiver, &receiver);
274  route_discovery_discover(&route_discovery, &receiver, ROUTE_TIMEOUT);
275  } else if(!linkaddr_cmp(&queued_receiver, &receiver)) {
276  route_discovery_discover(&route_discovery, &receiver, ROUTE_TIMEOUT);
277  }
278  } else {
279  route_decay(rt);
280  send_data(&rt->nexthop);
281  }
282  return UIP_FW_OK;
283 }
284 /*---------------------------------------------------------------------------*/
285 void
286 uip_over_mesh_set_gateway_netif(struct uip_fw_netif *n)
287 {
288  gw_netif = n;
289 }
290 /*---------------------------------------------------------------------------*/
291 void
292 uip_over_mesh_set_gateway(linkaddr_t *gw)
293 {
294  linkaddr_copy(&gateway, gw);
295 }
296 /*---------------------------------------------------------------------------*/
297 void
298 uip_over_mesh_set_net(uip_ipaddr_t *addr, uip_ipaddr_t *mask)
299 {
300  uip_ipaddr_copy(&netaddr, addr);
301  uip_ipaddr_copy(&netmask, mask);
302 }
303 /*---------------------------------------------------------------------------*/
#define UIP_FW_DROPPED
An error message that indicates that a packet that should be forwarded or output was dropped...
Definition: uip-fw.h:171
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
uIP packet forwarding header file.
int packetbuf_copyfrom(const void *from, uint16_t len)
Copy from external data into the packetbuf.
Definition: packetbuf.c:93
#define uip_ipaddr_maskcmp(addr1, addr2, mask)
Compare two IP addresses with netmasks.
Definition: uip.h:1090
void tcpip_input(void)
Deliver an incoming packet to the TCP/IP stack.
Definition: tcpip.c:529
Representation of a uIP network interface.
Definition: uip-fw.h:54
uint8_t(* output)(void)
A pointer to the function that sends a packet.
Definition: uip-fw.h:59
const linkaddr_t linkaddr_null
The null Rime address.
#define NULL
The null pointer.
Header file for tunnelling uIP over Rime mesh
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:260
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
#define UIP_LLH_LEN
The link level header length.
Definition: uipopt.h:160
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
int packetbuf_copyto(void *to)
Copy the entire packetbuf to an external buffer.
Definition: packetbuf.c:140
#define uip_ipaddr_to_quad(a)
Convert an IP address to four bytes separated by commas.
Definition: uip.h:927
Header file for the Rime mesh routing protocol
uip_ipaddr_t netmask
The netmask of the interface.
Definition: uip-fw.h:58
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66
Header file for Trickle (reliable single source flooding) for Rime
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
#define UIP_FW_OK
A non-error message that indicates that something went OK.
Definition: uip-fw.h:132
Header file for the Rime route table