00001 00002 /* 00003 * OLSR Basic Multicast Forwarding (BMF) plugin. 00004 * Copyright (c) 2005 - 2007, Thales Communications, Huizen, The Netherlands. 00005 * Written by Erik Tromp. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in 00016 * the documentation and/or other materials provided with the 00017 * distribution. 00018 * * Neither the name of Thales, BMF nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00023 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00024 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00025 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 00026 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00028 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00029 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00030 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 00031 * OF THE POSSIBILITY OF SUCH DAMAGE. 00032 */ 00033 00034 /* ------------------------------------------------------------------------- 00035 * File : Packet.c 00036 * Description: IP packet and Ethernet frame processing functions 00037 * Created : 29 Jun 2006 00038 * 00039 * ------------------------------------------------------------------------- */ 00040 00041 #include "Packet.h" 00042 00043 /* System includes */ 00044 #include <stddef.h> /* NULL */ 00045 #include <assert.h> /* assert() */ 00046 #include <string.h> /* memcpy() */ 00047 #include <sys/types.h> /* u_int8_t, u_int16_t, u_int32_t */ 00048 #include <netinet/in.h> /* ntohs(), htons() */ 00049 #include <netinet/ip.h> /* struct iphdr */ 00050 00051 #include "defs.h" /* ARM_NOWARN_ALIGN */ 00052 00053 /* ------------------------------------------------------------------------- 00054 * Function : IsIpFragment 00055 * Description: Check if an IP packet is an IP fragment 00056 * Input : ipPacket - the IP packet 00057 * Output : none 00058 * Return : true (1) or false (0) 00059 * Data Used : none 00060 * ------------------------------------------------------------------------- */ 00061 int 00062 IsIpFragment(unsigned char *ipPacket) 00063 { 00064 struct ip *iph; 00065 00066 assert(ipPacket != NULL); 00067 00068 iph = (struct ip *)(ARM_NOWARN_ALIGN(ipPacket)); 00069 if ((ntohs(iph->ip_off) & IP_OFFMASK) != 0) { 00070 return 1; 00071 } 00072 return 0; 00073 } /* IsIpFragment */ 00074 00075 /* ------------------------------------------------------------------------- 00076 * Function : GetIpTotalLength 00077 * Description: Retrieve the total length of the IP packet (in bytes) of 00078 * an IP packet 00079 * Input : ipPacket - the IP packet 00080 * Output : none 00081 * Return : IP packet length 00082 * Data Used : none 00083 * ------------------------------------------------------------------------- */ 00084 u_int16_t 00085 GetIpTotalLength(unsigned char *ipPacket) 00086 { 00087 struct iphdr *iph; 00088 00089 assert(ipPacket != NULL); 00090 00091 iph = (struct iphdr *)(ARM_NOWARN_ALIGN(ipPacket)); 00092 return ntohs(iph->tot_len); 00093 } /* GetIpTotalLength */ 00094 00095 /* ------------------------------------------------------------------------- 00096 * Function : GetIpHeaderLength 00097 * Description: Retrieve the IP header length (in bytes) of an IP packet 00098 * Input : ipPacket - the IP packet 00099 * Output : none 00100 * Return : IP header length 00101 * Data Used : none 00102 * ------------------------------------------------------------------------- */ 00103 unsigned int 00104 GetIpHeaderLength(unsigned char *ipPacket) 00105 { 00106 struct iphdr *iph; 00107 00108 assert(ipPacket != NULL); 00109 00110 iph = (struct iphdr *)(ARM_NOWARN_ALIGN(ipPacket)); 00111 return iph->ihl << 2; 00112 } /* GetIpHeaderLength */ 00113 00114 /* ------------------------------------------------------------------------- 00115 * Function : GetTtl 00116 * Description: Retrieve the TTL (Time To Live) value from the IP header of 00117 * an IP packet 00118 * Input : ipPacket - the IP packet 00119 * Output : none 00120 * Return : TTL value 00121 * Data Used : none 00122 * ------------------------------------------------------------------------- */ 00123 u_int8_t 00124 GetTtl(unsigned char *ipPacket) 00125 { 00126 struct iphdr *iph; 00127 00128 assert(ipPacket != NULL); 00129 00130 iph = (struct iphdr *)(ARM_NOWARN_ALIGN(ipPacket)); 00131 return iph->ttl; 00132 } /* GetTtl */ 00133 00134 /* ------------------------------------------------------------------------- 00135 * Function : SaveTtlAndChecksum 00136 * Description: Save the TTL (Time To Live) value and IP checksum as found in 00137 * the IP header of an IP packet 00138 * Input : ipPacket - the IP packet 00139 * Output : sttl - the TTL and checksum values 00140 * Return : none 00141 * Data Used : none 00142 * ------------------------------------------------------------------------- */ 00143 void 00144 SaveTtlAndChecksum(unsigned char *ipPacket, struct TSaveTtl *sttl) 00145 { 00146 struct iphdr *iph; 00147 00148 assert(ipPacket != NULL && sttl != NULL); 00149 00150 iph = (struct iphdr *)(ARM_NOWARN_ALIGN(ipPacket)); 00151 sttl->ttl = iph->ttl; 00152 sttl->check = ntohs(iph->check); 00153 } /* SaveTtlAndChecksum */ 00154 00155 /* ------------------------------------------------------------------------- 00156 * Function : RestoreTtlAndChecksum 00157 * Description: Restore the TTL (Time To Live) value and IP checksum in 00158 * the IP header of an IP packet 00159 * Input : ipPacket - the IP packet 00160 * sttl - the TTL and checksum values 00161 * Output : none 00162 * Return : none 00163 * Data Used : none 00164 * ------------------------------------------------------------------------- */ 00165 void 00166 RestoreTtlAndChecksum(unsigned char *ipPacket, struct TSaveTtl *sttl) 00167 { 00168 struct iphdr *iph; 00169 00170 assert(ipPacket != NULL && sttl != NULL); 00171 00172 iph = (struct iphdr *)(ARM_NOWARN_ALIGN(ipPacket)); 00173 iph->ttl = sttl->ttl; 00174 iph->check = htons(sttl->check); 00175 } /* RestoreTtlAndChecksum */ 00176 00177 /* ------------------------------------------------------------------------- 00178 * Function : DecreaseTtlAndUpdateHeaderChecksum 00179 * Description: For an IP packet, decrement the TTL value and update the IP header 00180 * checksum accordingly. 00181 * Input : ipPacket - the IP packet 00182 * Output : none 00183 * Return : none 00184 * Data Used : none 00185 * Notes : See also RFC1141 00186 * ------------------------------------------------------------------------- */ 00187 void 00188 DecreaseTtlAndUpdateHeaderChecksum(unsigned char *ipPacket) 00189 { 00190 struct iphdr *iph; 00191 u_int32_t sum; 00192 00193 assert(ipPacket != NULL); 00194 00195 iph = (struct iphdr *)(ARM_NOWARN_ALIGN(ipPacket)); 00196 00197 iph->ttl--; /* decrement ttl */ 00198 sum = ntohs(iph->check) + 0x100; /* increment checksum high byte */ 00199 iph->check = htons(sum + (sum >> 16)); /* add carry */ 00200 } /* DecreaseTtlAndUpdateHeaderChecksum */ 00201 00202 /* ------------------------------------------------------------------------- 00203 * Function : GetIpHeader 00204 * Description: Retrieve the IP header from BMF encapsulation UDP data 00205 * Input : encapsulationUdpData - the encapsulation UDP data 00206 * Output : none 00207 * Return : IP header 00208 * Data Used : none 00209 * ------------------------------------------------------------------------- */ 00210 struct ip * 00211 GetIpHeader(unsigned char *encapsulationUdpData) 00212 { 00213 return (struct ip *)(ARM_NOWARN_ALIGN(encapsulationUdpData + ENCAP_HDR_LEN)); 00214 } /* GetIpHeader */ 00215 00216 /* ------------------------------------------------------------------------- 00217 * Function : GetIpPacket 00218 * Description: Retrieve the IP packet from BMF encapsulation UDP data 00219 * Input : encapsulationUdpData - the encapsulation UDP data 00220 * Output : none 00221 * Return : The IP packet 00222 * Data Used : none 00223 * ------------------------------------------------------------------------- */ 00224 unsigned char * 00225 GetIpPacket(unsigned char *encapsulationUdpData) 00226 { 00227 return encapsulationUdpData + ENCAP_HDR_LEN; 00228 } /* GetIpPacket */ 00229 00230 /* ------------------------------------------------------------------------- 00231 * Function : GetEncapsulationUdpDataLength 00232 * Description: Return the length of BMF encapsulation UDP data 00233 * Input : encapsulationUdpData - the encapsulation UDP data 00234 * Output : none 00235 * Return : The encapsulation data length 00236 * Data Used : none 00237 * ------------------------------------------------------------------------- */ 00238 u_int16_t 00239 GetEncapsulationUdpDataLength(unsigned char *encapsulationUdpData) 00240 { 00241 return GetIpTotalLength(GetIpPacket(encapsulationUdpData)) + ENCAP_HDR_LEN; 00242 } /* GetEncapsulationUdpDataLength */ 00243 00244 00245 /* 00246 * Local Variables: 00247 * c-basic-offset: 2 00248 * indent-tabs-mode: nil 00249 * End: 00250 */
1.6.3