Contiki-NG
ip64-eth-interface.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 "net/ipv6/uip.h"
33 #include "net/ipv6/uip-ds6.h"
34 #include "dev/slip.h"
35 
36 #include "ip64/ip64.h"
37 #include "ip64/ip64-arp.h"
38 #include "ip64/ip64-eth-interface.h"
39 
40 #include <string.h>
41 
42 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
43 
44 #define DEBUG DEBUG_NONE
45 #include "net/ipv6/uip-debug.h"
46 #define printf(...)
47 /*---------------------------------------------------------------------------*/
48 void
49 ip64_eth_interface_input(uint8_t *packet, uint16_t len)
50 {
51  struct ip64_eth_hdr *ethhdr;
52  ethhdr = (struct ip64_eth_hdr *)packet;
53 
54  if(ethhdr->type == UIP_HTONS(IP64_ETH_TYPE_ARP)) {
55  len = ip64_arp_arp_input(packet, len);
56 
57  if(len > 0) {
58  IP64_ETH_DRIVER.output(packet, len);
59  }
60  } else if(ethhdr->type == UIP_HTONS(IP64_ETH_TYPE_IP) &&
61  len > sizeof(struct ip64_eth_hdr)) {
62  printf("-------------->\n");
63  uip_len = ip64_4to6(&packet[sizeof(struct ip64_eth_hdr)],
64  len - sizeof(struct ip64_eth_hdr),
66  if(uip_len > 0) {
67  printf("ip64_interface_process: converted %d bytes\n", uip_len);
68 
69  printf("ip64-interface: input source ");
70  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
71  PRINTF(" destination ");
72  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
73  PRINTF("\n");
74 
75  tcpip_input();
76  printf("Done\n");
77  }
78  }
79 }
80 /*---------------------------------------------------------------------------*/
81 static void
82 init(void)
83 {
84  printf("ip64-eth-interface: init\n");
85 }
86 /*---------------------------------------------------------------------------*/
87 static int
88 output(void)
89 {
90  int len, ret;
91 
92  printf("ip64-interface: output source ");
93  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
94  PRINTF(" destination ");
95  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
96  PRINTF("\n");
97 
98  printf("<--------------\n");
99  len = ip64_6to4(&uip_buf[UIP_LLH_LEN], uip_len,
100  &ip64_packet_buffer[sizeof(struct ip64_eth_hdr)]);
101 
102  printf("ip64-interface: output len %d\n", len);
103  if(len > 0) {
104  if(ip64_arp_check_cache(&ip64_packet_buffer[sizeof(struct ip64_eth_hdr)])) {
105  printf("Create header\n");
106  ret = ip64_arp_create_ethhdr(ip64_packet_buffer,
107  &ip64_packet_buffer[sizeof(struct ip64_eth_hdr)]);
108  if(ret > 0) {
109  len += ret;
110  IP64_ETH_DRIVER.output(ip64_packet_buffer, len);
111  }
112  } else {
113  printf("Create request\n");
114  len = ip64_arp_create_arp_request(ip64_packet_buffer,
115  &ip64_packet_buffer[sizeof(struct ip64_eth_hdr)]);
116  return IP64_ETH_DRIVER.output(ip64_packet_buffer, len);
117  }
118  }
119 
120  return 0;
121 }
122 /*---------------------------------------------------------------------------*/
123 const struct uip_fallback_interface ip64_eth_interface = {
124  init,
125  output
126 };
127 /*---------------------------------------------------------------------------*/
#define UIP_IP_BUF
Pointer to IP header.
Definition: uip-nd6.c:97
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition: uip6.c:179
A set of debugging macros for the IP stack
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_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_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition: uip.h:1230
Header file for the uIP TCP/IP stack.
The Ethernet header.
Definition: ip64-eth.h:51
void tcpip_input(void)
Deliver an incoming packet to the TCP/IP stack.
Definition: tcpip.c:449