From patchwork Thu May 4 15:12:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Numan Siddique X-Patchwork-Id: 758608 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wJdnG12snz9s2Q for ; Fri, 5 May 2017 01:12:50 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id B32E3B47; Thu, 4 May 2017 15:12:46 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 59990B1E for ; Thu, 4 May 2017 15:12:45 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id AE99818A for ; Thu, 4 May 2017 15:12:44 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 27DADC059752; Thu, 4 May 2017 15:12:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 27DADC059752 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=nusiddiq@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 27DADC059752 Received: from nusiddiq.blr.redhat.com (unknown [10.70.1.222]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2844A19E87; Thu, 4 May 2017 15:12:42 +0000 (UTC) From: nusiddiq@redhat.com To: dev@openvswitch.org Date: Thu, 4 May 2017 20:42:30 +0530 Message-Id: <20170504151230.11799-1-nusiddiq@redhat.com> In-Reply-To: <20170504151004.11283-1-nusiddiq@redhat.com> References: <20170504151004.11283-1-nusiddiq@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 04 May 2017 15:12:44 +0000 (UTC) X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [ovs-dev] [PATCH v5 1/6] packets: add compose_nd_ra X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org From: Zong Kai LI This patch introduces methods to compose a Router Advertisement (RA) packet, introduces flags for RA. RA packet composed structures against specification in RFC4861. Caller can use compse_nd_ra_with_sll_mtu_opts to compose a RA packet with Source Link-layer Address Option and MTU Option. Caller can use packet_put_ra_prefix_opt to append a Prefix Information Option to a RA packet. Signed-off-by: Zongkai LI --- lib/packets.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/packets.h | 59 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) diff --git a/lib/packets.c b/lib/packets.c index 94e7d87..dbe5105 100644 --- a/lib/packets.c +++ b/lib/packets.c @@ -35,6 +35,7 @@ const struct in6_addr in6addr_exact = IN6ADDR_EXACT_INIT; const struct in6_addr in6addr_all_hosts = IN6ADDR_ALL_HOSTS_INIT; +const struct in6_addr in6addr_all_routers = IN6ADDR_ALL_ROUTERS_INIT; struct in6_addr flow_tnl_dst(const struct flow_tnl *tnl) @@ -1428,6 +1429,93 @@ compose_nd_na(struct dp_packet *b, ND_MSG_LEN + ND_OPT_LEN)); } +/* Compose an IPv6 Neighbor Discovery Router Advertisement message with + * Source Link-layer Address Option and MTU Option. + * Caller can call packet_put_ra_prefix_opt to append Prefix Information + * Options to composed messags in 'b'. */ +void +compose_nd_ra_with_sll_mtu_opts(struct dp_packet *b, + const struct eth_addr eth_src, + const struct eth_addr eth_dst, + const struct in6_addr *ipv6_src, + const struct in6_addr *ipv6_dst, + uint8_t cur_hop_limit, uint8_t mo_flags, + ovs_be16 router_lt, ovs_be32 reachable_time, + ovs_be32 retrans_timer, ovs_be32 mtu) +{ + struct ovs_ra_msg *ra; + struct ovs_nd_mtu_opt *mtu_opt; + struct ovs_nd_opt *lla_opt; + uint32_t icmp_csum; + + /* Don't compose Router Advertisement packet with MTU Option if mtu + * value is 0. */ + bool with_mtu = mtu != 0; + size_t mtu_opt_len = with_mtu ? ND_MTU_OPT_LEN : 0; + + eth_compose(b, eth_dst, eth_src, ETH_TYPE_IPV6, IPV6_HEADER_LEN); + ra = compose_ipv6(b, IPPROTO_ICMPV6, ipv6_src, ipv6_dst, 0, 0, 255, + RA_MSG_LEN + ND_OPT_LEN + mtu_opt_len); + + ra->icmph.icmp6_type = ND_ROUTER_ADVERT; + ra->icmph.icmp6_code = 0; + ra->cur_hop_limit = cur_hop_limit; + ra->mo_flags = mo_flags; + ra->router_lifetime = router_lt; + ra->reachable_time = reachable_time; + ra->retrans_timer = retrans_timer; + + lla_opt = &ra->options[0]; + lla_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; + lla_opt->nd_opt_len = 1; + lla_opt->nd_opt_mac = eth_src; + + if (with_mtu) { + /* ovs_nd_mtu_opt has the same size with ovs_nd_opt. */ + mtu_opt = (struct ovs_nd_mtu_opt *)(lla_opt + 1); + mtu_opt->type = ND_OPT_MTU; + mtu_opt->len = 1; + mtu_opt->reserved = 0; + mtu_opt->mtu = mtu; + } + + ra->icmph.icmp6_cksum = 0; + icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b)); + ra->icmph.icmp6_cksum = csum_finish(csum_continue( + icmp_csum, ra, RA_MSG_LEN + ND_OPT_LEN + mtu_opt_len)); +} + +/* Append an IPv6 Neighbor Discovery Prefix Information option to a + * Router Advertisement message. */ +void +packet_put_ra_prefix_opt(struct dp_packet *b, + uint8_t plen, uint8_t la_flags, ovs_be32 valid_lifetime, + ovs_be32 preferred_lifetime, const ovs_be128 prefix) +{ + size_t prev_l4_size = dp_packet_l4_size(b); + struct ip6_hdr *nh = dp_packet_l3(b); + nh->ip6_plen = htons(prev_l4_size + ND_PREFIX_OPT_LEN); + + struct ovs_ra_msg *ra = dp_packet_l4(b); + struct ovs_nd_prefix_opt *prefix_opt; + uint32_t icmp_csum; + + prefix_opt = dp_packet_put_uninit(b, sizeof(struct ovs_nd_prefix_opt)); + prefix_opt->type = ND_OPT_PREFIX_INFORMATION; + prefix_opt->len = 4; + prefix_opt->prefix_len = plen; + prefix_opt->la_flags = la_flags; + prefix_opt->valid_lifetime = valid_lifetime; + prefix_opt->preferred_lifetime = preferred_lifetime; + prefix_opt->reserved = 0; + memcpy(prefix_opt->prefix.be32, prefix.be32, sizeof(ovs_be32[4])); + + ra->icmph.icmp6_cksum = 0; + icmp_csum = packet_csum_pseudoheader6(dp_packet_l3(b)); + ra->icmph.icmp6_cksum = csum_finish(csum_continue( + icmp_csum, ra, prev_l4_size + ND_PREFIX_OPT_LEN)); +} + uint32_t packet_csum_pseudoheader(const struct ip_header *ip) { diff --git a/lib/packets.h b/lib/packets.h index 639f5e4..17be082 100644 --- a/lib/packets.h +++ b/lib/packets.h @@ -903,6 +903,33 @@ struct ovs_nd_opt { }; BUILD_ASSERT_DECL(ND_OPT_LEN == sizeof(struct ovs_nd_opt)); +/* Neighbor Discovery option: Prefix Information. */ +#define ND_PREFIX_OPT_LEN 32 +struct ovs_nd_prefix_opt { + uint8_t type; /* ND_OPT_PREFIX_INFORMATION */ + uint8_t len; /* The length of Prefix Information Option shoulud be 4. */ + uint8_t prefix_len; + uint8_t la_flags; /* ND_PREFIX_ON_LINK and ND_PREFIX_AUTONOMOUS_ADDRESS flags. */ + ovs_be32 valid_lifetime; + ovs_be32 preferred_lifetime; + ovs_be32 reserved; /* Always should be 0. */ + union ovs_16aligned_in6_addr prefix; +}; +BUILD_ASSERT_DECL(ND_PREFIX_OPT_LEN == sizeof(struct ovs_nd_prefix_opt)); + +#define ND_PREFIX_ON_LINK 0x80 +#define ND_PREFIX_AUTONOMOUS_ADDRESS 0x40 + +/* Neighbor Discovery option: MTU. */ +#define ND_MTU_OPT_LEN 8 +struct ovs_nd_mtu_opt { + uint8_t type; /* ND_OPT_MTU */ + uint8_t len; /* The length of MTU Option shoulud be 1. */ + ovs_be16 reserved; /* Always should be 0. */ + ovs_be32 mtu; +}; +BUILD_ASSERT_DECL(ND_MTU_OPT_LEN == sizeof(struct ovs_nd_mtu_opt)); + /* Like struct nd_msg (from ndisc.h), but whereas that struct requires 32-bit * alignment, this one only requires 16-bit alignment. */ #define ND_MSG_LEN 24 @@ -914,10 +941,26 @@ struct ovs_nd_msg { }; BUILD_ASSERT_DECL(ND_MSG_LEN == sizeof(struct ovs_nd_msg)); +/* Neighbor Discovery packet flags. */ #define ND_RSO_ROUTER 0x80000000 #define ND_RSO_SOLICITED 0x40000000 #define ND_RSO_OVERRIDE 0x20000000 +#define RA_MSG_LEN 16 +struct ovs_ra_msg { + struct icmp6_header icmph; + uint8_t cur_hop_limit; + uint8_t mo_flags; /* ND_RA_MANAGED_ADDRESS and ND_RA_OTHER_CONFIG flags. */ + ovs_be16 router_lifetime; + ovs_be32 reachable_time; + ovs_be32 retrans_timer; + struct ovs_nd_opt options[0]; +}; +BUILD_ASSERT_DECL(RA_MSG_LEN == sizeof(struct ovs_ra_msg)); + +#define ND_RA_MANAGED_ADDRESS 0x80 +#define ND_RA_OTHER_CONFIG 0x40 + /* * Use the same struct for MLD and MLD2, naming members as the defined fields in * in the corresponding version of the protocol, though they are reserved in the @@ -972,6 +1015,10 @@ extern const struct in6_addr in6addr_all_hosts; #define IN6ADDR_ALL_HOSTS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01 } } } +extern const struct in6_addr in6addr_all_routers; +#define IN6ADDR_ALL_ROUTERS_INIT { { { 0xff,0x02,0x00,0x00,0x00,0x00,0x00,0x00, \ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02 } } } + static inline bool ipv6_addr_equals(const struct in6_addr *a, const struct in6_addr *b) { @@ -1196,6 +1243,18 @@ void compose_nd_na(struct dp_packet *, const struct eth_addr eth_src, const struct in6_addr *ipv6_src, const struct in6_addr *ipv6_dst, ovs_be32 rso_flags); +void compose_nd_ra_with_sll_mtu_opts(struct dp_packet *, + const struct eth_addr eth_src, + const struct eth_addr eth_dst, + const struct in6_addr *ipv6_src, + const struct in6_addr *ipv6_dst, + uint8_t cur_hop_limit, uint8_t mo_flags, + ovs_be16 router_lt, ovs_be32 reachable_time, + ovs_be32 retrans_timer, ovs_be32 mtu); +void packet_put_ra_prefix_opt(struct dp_packet *, + uint8_t plen, uint8_t la_flags, + ovs_be32 valid_lifetime, ovs_be32 preferred_lifetime, + const ovs_be128 router_prefix); uint32_t packet_csum_pseudoheader(const struct ip_header *); void IP_ECN_set_ce(struct dp_packet *pkt, bool is_ipv6);