diff --git a/include/nuttx/net/ipv6ext.h b/include/nuttx/net/ipv6ext.h new file mode 100644 index 00000000000..27a54ec7c40 --- /dev/null +++ b/include/nuttx/net/ipv6ext.h @@ -0,0 +1,213 @@ +/**************************************************************************** + * include/nuttx/net/ipv6ext.h + * IPv4 Extension Header Definitions + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/* Extension headers carry optional Internet Layer information, and are + * placed between the fixed IPv6 header and the upper-layer protocol header. + * The headers form a chain, using the Next Header fields. The Next Header + * field in the fixed header indicates the type of the first extension header; + * the Next Header field of the last extension header indicates the type of + * the upper-layer protocol header in the payload of the packet. + * + * All extension headers are a multiple of 8 octets in size; some extension + * headers require internal padding to meet this requirement. + * + * Extension headers are to be examined and processed at the packet's + * destination only, except for Hop-by-Hop Options, which need to be + * processed at every intermediate node on the packet's path, including + * sending and receiving node. + * + * If a node does not recognize a specific extension header, it should discard + * the packet and send a Parameter Problem message (ICMPv6 type 4, code 1). + * When a Next Header value 0 appears in a header other than the fixed header + * a node should do the same. + */ + +#ifndef __INCLUDE_NUTTX_NET_IPV6EXT_H +#define __INCLUDE_NUTTX_NET_IPV6EXT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Commonly Used Extension Headers + * + * - Hop-by-Hop EH is used for the support of Jumbo-grams or, with the + * Router Alert option, it is an integral part in the operation of the + * IPv6 Multicast through Multicast Listener Discovery (MLD). + * - Destination EH is used in IPv6 Mobility as well as support of certain + * applications. + * - Routing EH is used in IPv6 Mobility and in Source Routing. + * - Fragmentation EH is critical in support of communication using + * fragmented packets (in IPv6, the traffic source must do fragmentation- + * routers do not perform fragmentation of the packets they forward) + * - Mobility EH is used in support of Mobile IPv6 service + * - Authentication EH is similar in format and use to the IPv4 authentication + * header defined in RFC2402. + * - Encapsulating Security Payload EH is similar in format and use to the + * IPv4 ESP header defined in RFC2406 [5]. All information following the + * Encapsulating Security Header (ESH) is encrypted and for that reason, it + * is inaccessible to intermediary network devices. The ESH can be followed + * by an additional Destination Options EH and the upper layer datagram. + */ + +/* Values of the Next Header Field. See also IP_PROTO_* definitions in + * include/nuttx/net/ip.h. + * + * These options should be applied a specific order: + * + * 1. Basic IPv6 Header + * 2. Hop-by-Hop Options (0) + * 3. Destination Options (with Routing Options) (60) + * 4. Routing Header (43) + * 5. Fragment Header (44) + * 6. Authentication Header (51) + * 7. Encapsulation Security Payload Header (50) + * 8. Destination Options (60) + * 9. Mobility Header (135) + * No next header (59) + * 10. Upper Layer TCP (6), UDP (17), ICMPv6 (58) + */ + +#define NEXT_HOPBYBOT_EH 0 /* 0 Hop-by-Hop Options Header */ + /* 6 See IP_PROTO_TCP in + * include/nuttx/net/ip.h. */ + /* 17 See IP_PROTO_TCP in + * include/nuttx/net/ip.h. */ +#define NEXT_ENCAP_EH 41 /* 41 Encapsulated IPv6 Header */ +#define NEXT_ROUTING_EH 43 /* 43 Routing Header */ +#define NEXT_FRAGMENT_EH 44 /* 44 Fragment Header */ +#define NEXT_RRSVP_EH 46 /* 46 Resource ReSerVation Protocol */ +#define NEXT_ENCAPSEC_EH 50 /* 50 Encapsulating Security Payload */ +#define NEXT_AUTH_EH 51 /* 51 Authentication Header */ + /* 58 See IP_PROTO_ICMP6 in + * include/nuttx/net/ip.h. */ +#define NEXT_NOHEADER 59 /* 59 No next header */ +#define NEXT_DESTOPT_EH 60 /* 60 Destination Options Header */ +#define NEXT_MOBILITY_EH 135 /* 135 Mobility */ +#define NEXT_HOSTID_EH 139 /* 139 Host Identity Protocol */ +#define NEXT_SHIM6_EH 140 /* 140 Shim6 Protocol */ + /* 253, 254 Reserved for experimentation + * and testing */ + +/* Values of the Two High-Order Bits in the Hop-to-hop Option Type Field */ + +#define HOP2HOP_TYPE_MASK 0xc0 +#define HOP2HOP_TYPE_SKIP 0x00 /* Skip the option */ +#define HOP2HOP_TYPE_DISCARD 0x40 /* Silently discard the packet */ +#define HOP2HOP_TYPE_DISCARD1 0x80 /* Discard and send problem message if + * ucast or mcast */ +#define HOP2HOP_TYPE_DISCARD2 0xc0 /* Discard and send problem message if + * not mcast */ + +/* Hop-to-hop Option Types */ + +#define HOP2HOP_ROUTER_ALERT 5 + +/* Hop-to-hop Router Alert Values */ + +#define OPT_RA_MLD 0 /* Datagram contains a Multicast Listener + * Discovery message (RFC 2710) */ +#define OPT_RA_RSVP 1 /* Datagram contains RSVP message */ +#define OPT_RA_ACTIVE 2 /* Datagram contains an Active Networks + * message */ + /* 3-65535 Reserved to IANA for future use */ + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/* Hop-by-hop Option Header */ + +struct ipv6_hopbyhop_extension_s +{ + uint8_t nxthdr; /* Next header */ + uint8_t len; /* Extension header length */ + uint8_t options[3]; /* Options */ +}; + +struct ipv6_routing_extension_s +{ + uint8_t nxthdr; /* Next header */ + uint8_t len; /* Extension header length */ + uint8_t type; /* Routing type */ + uint8_t segments; /* Segments left */ + uint8_t reserved[4]; /* Reserved, must be zero */ + /* IPv6 addresses follow */ +}; + +struct ipv6_fragment_extension_s +{ + uint8_t nxthdr; /* Next header */ + uint8_t reserved; /* Reserved, must be zero */ + uint8_t msoffset; /* MS offset 5:12 */ + uint8_t lsoffset; /* LS offset 0:4, M flag */ + uint8_t id[4]; /* Identification */ +}; + +struct ipv6_authenitcation_extension_s +{ + uint8_t nxthdr; /* Next header */ + uint8_t len; /* Payload length */ + uint8_t reserved[2]; /* Reserved, must be zero */ + uint8_t security[4]; /* Security parameters */ + uint8_t sequence[4]; /* Sequence number */ + uint8_t authdata[4]; /* Authentication data */ +}; + +struct ipv6_destoptions_extension_s +{ + uint8_t nxthdr; /* Next header */ + uint8_t len; /* Extension header length */ + uint8_t options[3]; /* Options */ +}; + +/* Router Alert Hop-to-Hop option */ + +struct ipv6_router_alert_s +{ + struct ipv6_hopbyhop_extension_s h2h; + + uint8_t type; /* Hop-by-hop option number (5) */ + uint8_t len; /* Length = 2 */ + uint16_t value; /* Value. See OPT_RA_* Definitions */ +}; + +#endif /* __INCLUDE_NUTTX_NET_IPV6EXT_H */ diff --git a/net/mld/mld_send.c b/net/mld/mld_send.c index 2827fae3015..446a2a9d022 100644 --- a/net/mld/mld_send.c +++ b/net/mld/mld_send.c @@ -47,7 +47,7 @@ #include #include #include -#include +#include #include #include @@ -77,9 +77,9 @@ /* Buffer layout */ #define IPv6BUF ((FAR struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)]) -#define RABUF ((FAR uint8_t *)&dev->d_buf[NET_LL_HDRLEN(dev)] + \ - IPv6_HDRLEN) -#define RASIZE (4 * sizeof(uint16_t)) +#define RABUF ((FAR struct ipv6_router_alert_s *) \ + &dev->d_buf[NET_LL_HDRLEN(dev)] + IPv6_HDRLEN) +#define RASIZE sizeof(struct ipv6_router_alert_s) #define REPORTBUF ((FAR struct mld_mcast_listen_report_v1_s *) \ &dev->d_buf[NET_LL_HDRLEN(dev)] + IPv6_HDRLEN + RASIZE) #define DONEBUF ((FAR struct mld_mcast_listen_done_v1_s *) \ @@ -114,7 +114,7 @@ void mld_send(FAR struct net_driver_s *dev, FAR struct mld_group_s *group, FAR const net_ipv6addr_t destipaddr) { FAR struct ipv6_hdr_s *ipv6; - FAR uint8_t *ra; + FAR struct ipv6_router_alert_s *ra; unsigned int mldsize; ninfo("msgid: %02x \n", group->msgid); @@ -169,7 +169,7 @@ void mld_send(FAR struct net_driver_s *dev, FAR struct mld_group_s *group, ipv6->flow = 0; /* Flow label (LS) */ ipv6->len[0] = (dev->d_sndlen >> 8); /* Length excludes the IPv6 header */ ipv6->len[1] = (dev->d_sndlen & 0xff); - ipv6->proto = IP_PROTO_ICMP6; /* ICMPv6 payload */ + ipv6->proto = NEXT_HOPBYBOT_EH; /* Hop-to-hop extension header */ ipv6->ttl = MLD_TTL; /* MLD Time-to-live */ net_ipv6addr_hdrcopy(ipv6->srcipaddr, dev->d_ipv6addr); @@ -178,14 +178,15 @@ void mld_send(FAR struct net_driver_s *dev, FAR struct mld_group_s *group, /* Add the router alert IP header option. * * The IPv6 router alert option (type 5) is defined in RFC 2711. - * REVISIT: This should go into a new header file ipv6opt.h */ ra = RABUF; - ra[0] = 5; /* Option type */ - ra[1] = 2; /* Option length */ - ra[2] = 0; /* Router alert value */ - ra[3] = 0; + memset(ra, 0, RASIZE); + + ra->h2h.nxthdr = IP_PROTO_ICMP6; /* ICMPv6 payload follows extension header */ + ra->h2h.len = 1; /* One 8-octect option follows */ + ra->type = HOP2HOP_ROUTER_ALERT; /* Router alert */ + ra->len = 2; /* Length */ /* Format the MLD ICMPv6 payload into place after the IPv6 header (with * router alert)