uip-icmp6.c

Go to the documentation of this file.
00001 /**
00002  * \addtogroup uip6
00003  * @{
00004  */
00005 
00006 /**
00007  * \file
00008  *         ICMPv6 echo request and error messages (RFC 4443)
00009  * \author Julien Abeille <jabeille@cisco.com> 
00010  * \author Mathilde Durvy <mdurvy@cisco.com>
00011  */
00012 
00013 /*
00014  * Copyright (c) 2001-2003, Adam Dunkels.
00015  * All rights reserved.
00016  *
00017  * Redistribution and use in source and binary forms, with or without
00018  * modification, are permitted provided that the following conditions
00019  * are met:
00020  * 1. Redistributions of source code must retain the above copyright
00021  *    notice, this list of conditions and the following disclaimer.
00022  * 2. Redistributions in binary form must reproduce the above copyright
00023  *    notice, this list of conditions and the following disclaimer in the
00024  *    documentation and/or other materials provided with the distribution.
00025  * 3. The name of the author may not be used to endorse or promote
00026  *    products derived from this software without specific prior
00027  *    written permission.
00028  *
00029  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00030  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00031  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00032  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00033  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00034  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00035  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00036  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00037  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00038  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00039  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00040  *
00041  * This file is part of the uIP TCP/IP stack.
00042  *
00043  */
00044 
00045 #include <string.h>
00046 #include "net/uip-ds6.h"
00047 #include "net/uip-icmp6.h"
00048 
00049 #define DEBUG 0
00050 #if DEBUG
00051 #include <stdio.h>
00052 #define PRINTF(...) printf(__VA_ARGS__)
00053 #define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
00054 #define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",lladdr->addr[0], lladdr->addr[1], lladdr->addr[2], lladdr->addr[3],lladdr->addr[4], lladdr->addr[5])
00055 #else
00056 #define PRINTF(...)
00057 #define PRINT6ADDR(addr)
00058 #endif
00059 
00060 #define UIP_IP_BUF                ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
00061 #define UIP_ICMP_BUF            ((struct uip_icmp_hdr *)&uip_buf[uip_l2_l3_hdr_len])
00062 #define UIP_ICMP6_ERROR_BUF  ((struct uip_icmp6_error *)&uip_buf[uip_l2_l3_icmp_hdr_len])
00063 
00064 /** \brief temporary IP address */
00065 static uip_ipaddr_t tmp_ipaddr;
00066 
00067 /*---------------------------------------------------------------------------*/
00068 void
00069 uip_icmp6_echo_request_input(void)
00070 {
00071   /*
00072    * we send an echo reply. It is trivial if there was no extension
00073    * headers in the request otherwise we need to remove the extension
00074    * headers and change a few fields
00075    */
00076   PRINTF("Received Echo Request from");
00077   PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
00078   PRINTF("to");
00079   PRINT6ADDR(&UIP_IP_BUF->destipaddr);
00080   PRINTF("\n");
00081   
00082   /* IP header */
00083   UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit;
00084 
00085   if(uip_is_addr_mcast(&UIP_IP_BUF->destipaddr)){
00086     uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
00087     uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
00088   } else {
00089     uip_ipaddr_copy(&tmp_ipaddr, &UIP_IP_BUF->srcipaddr);
00090     uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
00091     uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &tmp_ipaddr);
00092   }
00093 
00094   if(uip_ext_len > 0) {
00095     /* If there were extension headers*/
00096     UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
00097     uip_len -= uip_ext_len;
00098     UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
00099     UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
00100     /* move the echo request payload (starting after the icmp header)
00101      * to the new location in the reply.
00102      * The shift is equal to the length of the extension headers present
00103      * Note: UIP_ICMP_BUF still points to the echo request at this stage
00104      */
00105     memmove((uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN - uip_ext_len,
00106             (uint8_t *)UIP_ICMP_BUF + UIP_ICMPH_LEN, 
00107             (uip_len - UIP_IPH_LEN - UIP_ICMPH_LEN));
00108   }
00109   /* Below is important for the correctness of UIP_ICMP_BUF and the
00110    * checksum
00111    */
00112   uip_ext_len = 0;
00113   /* Note: now UIP_ICMP_BUF points to the beginning of the echo reply */
00114   UIP_ICMP_BUF->type = ICMP6_ECHO_REPLY;
00115   UIP_ICMP_BUF->icode = 0;
00116   UIP_ICMP_BUF->icmpchksum = 0;
00117   UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
00118  
00119   PRINTF("Sending Echo Reply to");
00120   PRINT6ADDR(&UIP_IP_BUF->destipaddr);
00121   PRINTF("from");
00122   PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
00123   PRINTF("\n");
00124   UIP_STAT(++uip_stat.icmp.sent);
00125   return;
00126 }
00127 /*---------------------------------------------------------------------------*/
00128 void
00129 uip_icmp6_error_output(u8_t type, u8_t code, u32_t param) {
00130   uip_ext_len = 0;
00131 
00132  /* check if originating packet is not an ICMP error*/
00133   if(UIP_IP_BUF->proto == UIP_PROTO_ICMP6 && UIP_ICMP_BUF->type < 128){
00134     uip_len = 0;
00135     return;
00136   }
00137 
00138   /* remember data of original packet before shifting */
00139   uip_ipaddr_copy(&tmp_ipaddr, &UIP_IP_BUF->destipaddr);   
00140     
00141   uip_len += UIP_IPICMPH_LEN + UIP_ICMP6_ERROR_LEN;
00142   
00143   if(uip_len > UIP_LINK_MTU)
00144     uip_len = UIP_LINK_MTU; 
00145 
00146   memmove((uint8_t *)UIP_ICMP6_ERROR_BUF + UIP_ICMP6_ERROR_LEN,
00147           (void *)UIP_IP_BUF, uip_len - UIP_IPICMPH_LEN - UIP_ICMP6_ERROR_LEN);
00148 
00149   UIP_IP_BUF->vtc = 0x60;
00150   UIP_IP_BUF->tcflow = 0;
00151   UIP_IP_BUF->flow = 0;
00152   UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
00153   UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit;
00154 
00155   /* the source should not be unspecified nor multicast, the check for
00156      multicast is done in uip_process */
00157   if(uip_is_addr_unspecified(&UIP_IP_BUF->srcipaddr)){
00158     uip_len = 0;
00159     return;
00160   }
00161   
00162   uip_ipaddr_copy(&UIP_IP_BUF->destipaddr, &UIP_IP_BUF->srcipaddr);
00163 
00164   if(uip_is_addr_mcast(&tmp_ipaddr)){
00165     if(type == ICMP6_PARAM_PROB && code == ICMP6_PARAMPROB_OPTION){
00166       uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &tmp_ipaddr);
00167     } else {
00168       uip_len = 0;
00169       return;
00170     }
00171   } else {
00172 #if UIP_CONF_ROUTER
00173     /* need to pick a source that corresponds to this node */
00174     uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &tmp_ipaddr);
00175 #else
00176     uip_ipaddr_copy(&UIP_IP_BUF->srcipaddr, &tmp_ipaddr);
00177 #endif
00178   }
00179   
00180   UIP_ICMP_BUF->type = type;
00181   UIP_ICMP_BUF->icode = code;
00182   UIP_ICMP6_ERROR_BUF->param = uip_htonl(param);
00183   UIP_IP_BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
00184   UIP_IP_BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
00185   UIP_ICMP_BUF->icmpchksum = 0;
00186   UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
00187 
00188   UIP_STAT(++uip_stat.icmp.sent);
00189 
00190   PRINTF("Sending ICMPv6 ERROR message to");
00191   PRINT6ADDR(&UIP_IP_BUF->destipaddr);
00192   PRINTF("from");
00193   PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
00194   PRINTF("\n");
00195   return;
00196 }
00197 
00198 /*---------------------------------------------------------------------------*/
00199 void
00200 uip_icmp6_send(uip_ipaddr_t *dest, int type, int code, int payload_len)
00201 {
00202 
00203   UIP_IP_BUF->vtc = 0x60;
00204   UIP_IP_BUF->tcflow = 0;
00205   UIP_IP_BUF->flow = 0;
00206   UIP_IP_BUF->proto = UIP_PROTO_ICMP6;
00207   UIP_IP_BUF->ttl = uip_ds6_if.cur_hop_limit;
00208   UIP_IP_BUF->len[0] = (UIP_ICMPH_LEN + payload_len) >> 8;
00209   UIP_IP_BUF->len[1] = (UIP_ICMPH_LEN + payload_len) & 0xff;
00210 
00211   memcpy(&UIP_IP_BUF->destipaddr, dest, sizeof(*dest));
00212   uip_ds6_select_src(&UIP_IP_BUF->srcipaddr, &UIP_IP_BUF->destipaddr);
00213 
00214   UIP_ICMP_BUF->type = type;
00215   UIP_ICMP_BUF->icode = code;
00216 
00217   UIP_ICMP_BUF->icmpchksum = 0;
00218   UIP_ICMP_BUF->icmpchksum = ~uip_icmp6chksum();
00219 
00220   uip_len = UIP_IPH_LEN + UIP_ICMPH_LEN + payload_len;
00221   tcpip_ipv6_output();
00222 }
00223 /*---------------------------------------------------------------------------*/
00224 
00225 /** @} */

Generated on Mon Apr 11 14:23:32 2011 for Contiki 2.5 by  doxygen 1.6.1