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/*---------------------------------------------------------------------------*/
43
44#include "sys/log.h"
45#define LOG_MODULE "IP64"
46#define LOG_LEVEL LOG_LEVEL_IP64
47
48/*---------------------------------------------------------------------------*/
49void
50ip64_eth_interface_input(uint8_t *packet, uint16_t len)
51{
52 struct ip64_eth_hdr *ethhdr;
53 ethhdr = (struct ip64_eth_hdr *)packet;
54
55 if(ethhdr->type == UIP_HTONS(IP64_ETH_TYPE_ARP)) {
56 len = ip64_arp_arp_input(packet, len);
57
58 if(len > 0) {
59 IP64_ETH_DRIVER.output(packet, len);
60 }
61 } else if(ethhdr->type == UIP_HTONS(IP64_ETH_TYPE_IP) &&
62 len > sizeof(struct ip64_eth_hdr)) {
63 LOG_DBG("-------------->\n");
64 uip_len = ip64_4to6(&packet[sizeof(struct ip64_eth_hdr)],
65 len - sizeof(struct ip64_eth_hdr),
66 uip_buf);
67 if(uip_len > 0) {
68 LOG_DBG("ip64_interface_process: converted %d bytes\n", uip_len);
69
70 LOG_DBG("ip64-interface: input source ");
71 LOG_DBG_6ADDR(&UIP_IP_BUF->srcipaddr);
72 LOG_DBG_(" destination ");
73 LOG_DBG_6ADDR(&UIP_IP_BUF->destipaddr);
74 LOG_DBG_("\n");
75
77 LOG_DBG("Done\n");
78 }
79 }
80}
81/*---------------------------------------------------------------------------*/
82static void
83init(void)
84{
85 LOG_INFO("ip64-eth-interface: init\n");
86}
87/*---------------------------------------------------------------------------*/
88static int
89output(void)
90{
91 int len, ret;
92
93 LOG_DBG("ip64-interface: output source ");
94 LOG_DBG_6ADDR(&UIP_IP_BUF->srcipaddr);
95 LOG_DBG_(" destination ");
96 LOG_DBG_6ADDR(&UIP_IP_BUF->destipaddr);
97 LOG_DBG_("\n");
98
99 LOG_DBG("<--------------\n");
100 len = ip64_6to4(uip_buf, uip_len,
101 &ip64_packet_buffer[sizeof(struct ip64_eth_hdr)]);
102
103 LOG_DBG("ip64-interface: output len %d\n", len);
104 if(len > 0) {
105 if(ip64_arp_check_cache(&ip64_packet_buffer[sizeof(struct ip64_eth_hdr)])) {
106 LOG_DBG("Create header\n");
107 ret = ip64_arp_create_ethhdr(ip64_packet_buffer,
108 &ip64_packet_buffer[sizeof(struct ip64_eth_hdr)]);
109 if(ret > 0) {
110 len += ret;
111 IP64_ETH_DRIVER.output(ip64_packet_buffer, len);
112 }
113 } else {
114 LOG_DBG("Create request\n");
115 len = ip64_arp_create_arp_request(ip64_packet_buffer,
116 &ip64_packet_buffer[sizeof(struct ip64_eth_hdr)]);
117 return IP64_ETH_DRIVER.output(ip64_packet_buffer, len);
118 }
119 }
120
121 return 0;
122}
123/*---------------------------------------------------------------------------*/
124const struct uip_fallback_interface ip64_eth_interface = {
125 init,
126 output
127};
128/*---------------------------------------------------------------------------*/
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:1606
void tcpip_input(void)
Deliver an incoming packet to the TCP/IP stack.
Definition: tcpip.c:445
#define UIP_IP_BUF
Direct access to IPv6 header.
Definition: uip.h:71
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition: uip.h:1157
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Definition: uip.h:465
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition: uip6.c:159
Header file for the logging system.
The Ethernet header.
Definition: ip64-eth.h:51
Header file for IPv6-related data structures.
Header file for the uIP TCP/IP stack.