Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

ip6.h

Go to the documentation of this file.
00001 /*
00002  * ip6.h
00003  *
00004  * Internet Protocol, Version 6 (RFC 2460).
00005  *
00006  * Copyright (c) 2002 Dug Song <dugsong@monkey.org>
00007  *
00008  * $Id: ip6.h,v 1.6 2004/02/23 10:01:15 dugsong Exp $
00009  */
00010 
00011 #ifndef DNET_IP6_H
00012 #define DNET_IP6_H
00013 
00014 #define IP6_ADDR_LEN    16
00015 #define IP6_ADDR_BITS   128
00016 
00017 #define IP6_HDR_LEN     40              /* IPv6 header length */
00018 #define IP6_LEN_MIN     IP6_HDR_LEN
00019 #define IP6_LEN_MAX     65535           /* non-jumbo payload */
00020 
00021 #define IP6_MTU_MIN     1280            /* minimum MTU (1024 + 256) */
00022 
00023 typedef struct ip6_addr {
00024         uint8_t         data[IP6_ADDR_LEN];
00025 } ip6_addr_t;
00026 
00027 #ifndef __GNUC__
00028 # define __attribute__(x)
00029 # pragma pack(1)
00030 #endif
00031 
00032 /*
00033  * IPv6 header
00034  */
00035 struct ip6_hdr {
00036         union {
00037                 struct ip6_hdr_ctl {
00038                         uint32_t        ip6_un1_flow; /* 20 bits of flow ID */
00039                         uint16_t        ip6_un1_plen; /* payload length */
00040                         uint8_t         ip6_un1_nxt;  /* next header */
00041                         uint8_t         ip6_un1_hlim; /* hop limit */
00042                 } ip6_un1;
00043                 uint8_t ip6_un2_vfc;    /* 4 bits version, top 4 bits class */
00044         } ip6_ctlun;
00045         ip6_addr_t      ip6_src;
00046         ip6_addr_t      ip6_dst;
00047 } __attribute__((__packed__));
00048 
00049 #define ip6_vfc         ip6_ctlun.ip6_un2_vfc
00050 #define ip6_flow        ip6_ctlun.ip6_un1.ip6_un1_flow
00051 #define ip6_plen        ip6_ctlun.ip6_un1.ip6_un1_plen
00052 #define ip6_nxt         ip6_ctlun.ip6_un1.ip6_un1_nxt   /* IP_PROTO_* */
00053 #define ip6_hlim        ip6_ctlun.ip6_un1.ip6_un1_hlim
00054 
00055 #define IP6_VERSION             0x60
00056 #define IP6_VERSION_MASK        0xf0            /* ip6_vfc version */
00057 
00058 #if DNET_BYTESEX == DNET_BIG_ENDIAN
00059 #define IP6_FLOWINFO_MASK       0x0fffffff      /* ip6_flow info (28 bits) */
00060 #define IP6_FLOWLABEL_MASK      0x000fffff      /* ip6_flow label (20 bits) */
00061 #elif DNET_BYTESEX == DNET_LIL_ENDIAN
00062 #define IP6_FLOWINFO_MASK       0xffffff0f      /* ip6_flow info (28 bits) */
00063 #define IP6_FLOWLABEL_MASK      0xffff0f00      /* ip6_flow label (20 bits) */
00064 #endif
00065 
00066 /*
00067  * Hop limit (ip6_hlim)
00068  */
00069 #define IP6_HLIM_DEFAULT        64
00070 #define IP6_HLIM_MAX            255
00071 
00072 /*
00073  * Preferred extension header order from RFC 2460, 4.1:
00074  *
00075  * IP_PROTO_IPV6, IP_PROTO_HOPOPTS, IP_PROTO_DSTOPTS, IP_PROTO_ROUTING,
00076  * IP_PROTO_FRAGMENT, IP_PROTO_AH, IP_PROTO_ESP, IP_PROTO_DSTOPTS, IP_PROTO_*
00077  */
00078 
00079 /*
00080  * Routing header data (IP_PROTO_ROUTING)
00081  */
00082 struct ip6_ext_data_routing {
00083         uint8_t  type;                  /* routing type */
00084         uint8_t  segleft;               /* segments left */
00085         /* followed by routing type specific data */
00086 } __attribute__((__packed__));
00087 
00088 struct ip6_ext_data_routing0 {
00089         uint8_t  type;                  /* always zero */
00090         uint8_t  segleft;               /* segments left */
00091         uint8_t  reserved;              /* reserved field */
00092         uint8_t  slmap[3];              /* strict/loose bit map */
00093         ip6_addr_t  addr[1];            /* up to 23 addresses */
00094 } __attribute__((__packed__));
00095 
00096 /*
00097  * Fragment header data (IP_PROTO_FRAGMENT)
00098  */
00099 struct ip6_ext_data_fragment {
00100         uint16_t  offlg;                /* offset, reserved, and flag */
00101         uint32_t  ident;                /* identification */
00102 } __attribute__((__packed__));
00103 
00104 /*
00105  * Fragmentation offset, reserved, and flags (offlg)
00106  */
00107 #if DNET_BYTESEX == DNET_BIG_ENDIAN
00108 #define IP6_OFF_MASK            0xfff8  /* mask out offset from offlg */
00109 #define IP6_RESERVED_MASK       0x0006  /* reserved bits in offlg */
00110 #define IP6_MORE_FRAG           0x0001  /* more-fragments flag */
00111 #elif DNET_BYTESEX == DNET_LIL_ENDIAN
00112 #define IP6_OFF_MASK            0xf8ff  /* mask out offset from offlg */
00113 #define IP6_RESERVED_MASK       0x0600  /* reserved bits in offlg */
00114 #define IP6_MORE_FRAG           0x0100  /* more-fragments flag */
00115 #endif
00116 
00117 /*
00118  * Option types, for IP_PROTO_HOPOPTS, IP_PROTO_DSTOPTS headers
00119  */
00120 #define IP6_OPT_PAD1            0x00    /* 00 0 00000 */
00121 #define IP6_OPT_PADN            0x01    /* 00 0 00001 */
00122 #define IP6_OPT_JUMBO           0xC2    /* 11 0 00010 = 194 */
00123 #define IP6_OPT_JUMBO_LEN       6
00124 #define IP6_OPT_RTALERT         0x05    /* 00 0 00101 */
00125 #define IP6_OPT_RTALERT_LEN     4
00126 #define IP6_OPT_RTALERT_MLD     0       /* Datagram contains an MLD message */
00127 #define IP6_OPT_RTALERT_RSVP    1       /* Datagram contains an RSVP message */
00128 #define IP6_OPT_RTALERT_ACTNET  2       /* contains an Active Networks msg */
00129 #define IP6_OPT_LEN_MIN         2
00130 
00131 #define IP6_OPT_TYPE(o)         ((o) & 0xC0)    /* high 2 bits of opt_type */
00132 #define IP6_OPT_TYPE_SKIP       0x00    /* continue processing on failure */
00133 #define IP6_OPT_TYPE_DISCARD    0x40    /* discard packet on failure */
00134 #define IP6_OPT_TYPE_FORCEICMP  0x80    /* discard and send ICMP on failure */
00135 #define IP6_OPT_TYPE_ICMP       0xC0    /* ...only if non-multicast dst */
00136 
00137 #define IP6_OPT_MUTABLE         0x20    /* option data may change en route */
00138 
00139 /*
00140  * Extension header (chained via {ip6,ext}_nxt, following IPv6 header)
00141  */
00142 struct ip6_ext_hdr {
00143         uint8_t  ext_nxt;       /* next header */
00144         uint8_t  ext_len;       /* following length in units of 8 octets */
00145         union {
00146                 struct ip6_ext_data_routing     routing;
00147                 struct ip6_ext_data_fragment    fragment;
00148         } ext_data;
00149 } __attribute__((__packed__));
00150 
00151 #ifndef __GNUC__
00152 # pragma pack()
00153 #endif
00154 
00155 /*
00156  * Reserved addresses
00157  */
00158 #define IP6_ADDR_UNSPEC \
00159         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
00160 #define IP6_ADDR_LOOPBACK \
00161         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"
00162 
00163 #define ip6_pack_hdr(hdr, fc, fl, plen, nxt, hlim, src, dst) do {       \
00164         struct ip6_hdr *ip6 = (struct ip6_hdr *)(hdr);                  \
00165         ip6->ip6_flow = htonl(((uint32_t)(fc) << 28) &                  \
00166             (IP6_FLOWLABEL_MASK | (fl)));                               \
00167         ip6->ip6_vfc = (IP6_VERSION | ((fc) >> 4));                     \
00168         ip6->ip6_plen = htons((plen));                                  \
00169         ip6->ip6_nxt = (nxt); ip6->ip6_hlim = (hlim);                   \
00170         memmove(&ip6->ip6_src, &(src), IP6_ADDR_LEN);                   \
00171         memmove(&ip6->ip6_dst, &(dst), IP6_ADDR_LEN);                   \
00172 } while (0);
00173 
00174 __BEGIN_DECLS
00175 char    *ip6_ntop(const ip6_addr_t *ip6, char *dst, size_t size);
00176 int      ip6_pton(const char *src, ip6_addr_t *dst);
00177 char    *ip6_ntoa(const ip6_addr_t *ip6);
00178 #define  ip6_aton ip6_pton
00179 
00180 void     ip6_checksum(void *buf, size_t len);
00181 __END_DECLS
00182 
00183 #endif /* DNET_IP6_H */

Generated on Sun May 14 14:51:10 2006 by  doxygen 1.4.2