Contiki-NG
slip-bridge.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, 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  */
30 
31 /**
32  * \file
33  * Slip fallback interface
34  * \author
35  * Niclas Finne <nfi@sics.se>
36  * Joakim Eriksson <joakime@sics.se>
37  * Joel Hoglund <joel@sics.se>
38  * Nicolas Tsiftes <nvt@sics.se>
39  */
40 /*---------------------------------------------------------------------------*/
41 #include "net/ipv6/uip.h"
42 #include "net/ipv6/uip-ds6.h"
43 #include "dev/slip.h"
44 #include <string.h>
45 /*---------------------------------------------------------------------------*/
46 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
47 /*---------------------------------------------------------------------------*/
48 /* Log configuration */
49 #include "sys/log.h"
50 #define LOG_MODULE "SLIP"
51 #define LOG_LEVEL LOG_LEVEL_NONE
52 /*---------------------------------------------------------------------------*/
53 void set_prefix_64(uip_ipaddr_t *);
54 
55 static uip_ipaddr_t last_sender;
56 
57 /*---------------------------------------------------------------------------*/
58 void
59 request_prefix(void)
60 {
61  /* mess up uip_buf with a dirty request... */
62  uip_buf[0] = '?';
63  uip_buf[1] = 'P';
64  uip_len = 2;
66  uip_clear_buf();
67 }
68 /*---------------------------------------------------------------------------*/
69 static void
70 slip_input_callback(void)
71 {
72  LOG_DBG("SIN: %u\n", uip_len);
73  if(uip_buf[UIP_LLH_LEN] == '!') {
74  LOG_INFO("Got configuration message of type %c\n",
75  uip_buf[UIP_LLH_LEN + 1]);
76  if(uip_buf[UIP_LLH_LEN + 1] == 'P') {
77  uip_ipaddr_t prefix;
78  /* Here we set a prefix !!! */
79  memset(&prefix, 0, 16);
80  memcpy(&prefix, &uip_buf[UIP_LLH_LEN + 2], 8);
81 
82  uip_clear_buf();
83 
84  LOG_INFO("Setting prefix ");
85  LOG_INFO_6ADDR(&prefix);
86  LOG_INFO_("\n");
87  set_prefix_64(&prefix);
88  }
89  uip_clear_buf();
90 
91  } else if(uip_buf[UIP_LLH_LEN] == '?') {
92  LOG_INFO("Got request message of type %c\n", uip_buf[UIP_LLH_LEN + 1]);
93  if(uip_buf[UIP_LLH_LEN + 1] == 'M') {
94  char *hexchar = "0123456789abcdef";
95  int j;
96  /* this is just a test so far... just to see if it works */
97  uip_buf[0] = '!';
98  for(j = 0; j < UIP_LLADDR_LEN; j++) {
99  uip_buf[2 + j * 2] = hexchar[uip_lladdr.addr[j] >> 4];
100  uip_buf[3 + j * 2] = hexchar[uip_lladdr.addr[j] & 15];
101  }
102  uip_len = 18;
104  }
105  uip_clear_buf();
106  } else {
107  /* Save the last sender received over SLIP to avoid bouncing the
108  packet back if no route is found */
109  uip_ipaddr_copy(&last_sender, &UIP_IP_BUF->srcipaddr);
110  }
111 }
112 /*---------------------------------------------------------------------------*/
113 static void
114 init(void)
115 {
116  slip_arch_init();
117  process_start(&slip_process, NULL);
118  slip_set_input_callback(slip_input_callback);
119 }
120 /*---------------------------------------------------------------------------*/
121 static int
122 output(void)
123 {
124  if(uip_ipaddr_cmp(&last_sender, &UIP_IP_BUF->srcipaddr)) {
125  /* Do not bounce packets back over SLIP if the packet was received
126  over SLIP */
127  LOG_ERR("slip-bridge: Destination off-link but no route src=");
128  LOG_ERR_6ADDR(&UIP_IP_BUF->srcipaddr);
129  LOG_ERR_(" dst=");
130  LOG_ERR_6ADDR(&UIP_IP_BUF->destipaddr);
131  LOG_ERR_("\n");
132  } else {
133  LOG_DBG("SUT: %u\n", uip_len);
134  slip_send();
135  }
136  return 0;
137 }
138 /*---------------------------------------------------------------------------*/
139 const struct uip_fallback_interface rpl_interface = {
140  init, output
141 };
142 /*---------------------------------------------------------------------------*/
#define UIP_IP_BUF
Pointer to IP header.
Definition: uip-nd6.c:97
void slip_set_input_callback(void(*c)(void))
Set a function to be called when there is activity on the SLIP interface; used for detecting if a nod...
Definition: slip.c:142
uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:107
void slip_write(const void *_ptr, int len)
Send using SLIP len bytes starting from the location pointed to by ptr.
Definition: slip.c:171
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition: uip6.c:179
void slip_send(void)
Send an IP packet from the uIP buffer with SLIP.
Definition: slip.c:190
void slip_arch_init(void)
Initialize the SLIP driver.
Definition: slip-arch.c:62
static uint8_t output(const linkaddr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
Definition: sicslowpan.c:1549
Header file for IPv6-related data structures.
#define UIP_LLADDR_LEN
802.15.4 address
Definition: uip.h:148
#define UIP_LLH_LEN
The link level header length.
Definition: uipopt.h:141
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Definition: uip.h:513
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1018
Header file for the uIP TCP/IP stack.
Header file for the logging system
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99