RFC: Mechanism for adding MIPv6 extension headers
Subsystems (2)
Hello all,
Attached is a patch which includes the relevant functionality for adding mipv6 extension headers to data packets. It probably does not compile, as I included only the files which are directly involved in the adding mechanism to minimize the size of the patch. If you want to try out the mechanism I can prepare a full patch.
The patch is against bk changeset 1.1325 and is meant as a basis for discussion about the extension header addition mechanism. The code is still work in progress and lacks a proper interface. The mechanism has been tested with tcp and raw sockets and with tcp+ipsec.
An overview of the system:
User inserts the mipv6 information into the kernel. Based on this information ip6_add_miproute adds a new cached route. This cached route contains mip6_output as output function for adding the extension headers, a decreased pmtu and mipv6 binding information. The route also contains a pointer (u.dst->child) to a new route which contains correct forwarding information for mipv6 intermediate hops and the raw pmtu.
Adding of extension headers in mip6_output is done as in esp6_output.
The mechanism is fairly close to xfrm, except for storing the mipv6 information only in a cached route. Thus the state for a mipv6 binding is soft. This is a tradeoff between keeping the overhead of mipv6 small and having persistent state. If routes change, the mipv6 state can be easily reinserted into the kernel, since the userspace daemon needs to keep track of it for signaling purposes anyhow.
I will not go more into details here, but I am happy to answer any questions about the design. Your comments are much appreciated.
Regards,
Henrik
mipv6-kernel/net/ipv6/mip6.c renamed from ipv6/mip6.c +341 -0
@@ -0,0 +1,341 @@ +#include <linux/types.h> +#include <linux/in6.h> +#include <linux/skbuff.h> +#include <linux/init.h> +#include <net/ipv6.h> +#include <net/checksum.h> +#include <net/protocol.h> +#include <net/ip6_fib.h> +#include <net/ip6_route.h> +#include <net/mipv6.h> + +#define IPPROTO_MOBILITY 62 + + +struct mipv6_be +{ +u8 payload;/* Payload Protocol */ +u8 length;/* MH Length */ +u8 type;/* MH Type */ +u8 reserved;/* Reserved */ +u16 checksum;/* Checksum */ +u8 status;/* Error code */ +u8 reserved_2; +struct in6_addr home_addr; +} __attribute__ ((packed)); + +struct socket *mipv6_mh_socket =NULL ; + +static int dstopts_getfrag (const void *data,struct in6_addr *addr, +char *buff,unsigned int offset,unsigned int len) +{ +memcpy (buff, data + offset, len); +return 0 ; +} +static __inline__void mip6_xmit_lock (void ) +{ + + +local_bh_disable (); +if (unlikely (!spin_trylock (&mipv6_mh_socket->sk->lock.slock))) +BUG (); +} + +static __inline__void mip6_xmit_unlock (void ) +{ +spin_unlock_bh (&mipv6_mh_socket->sk->lock.slock); +} + + +void mip6_send_be (struct in6_addr *daddr, +struct in6_addr *saddr, +struct in6_addr *hao_addr) +{ +struct flowi fl; +struct mipv6_be be; +struct sock *sk = mipv6_mh_socket->sk; + +memset (&fl,0 ,sizeof (fl)); + fl.proto =IPPROTO_MOBILITY ; +ipv6_addr_copy (&fl.fl6_dst, daddr); +ipv6_addr_copy (&fl.fl6_src, saddr); + fl.fl6_flowlabel =0 ; + fl.oif = sk->bound_dev_if; + +memset (&be,0 ,sizeof (be)); + be.payload =NEXTHDR_NONE ; + be.length =2 ; + be.type =7 ; +ipv6_addr_copy (&be.home_addr, hao_addr); + be.status =1 ;/* Home address option without binding */ + +mip6_xmit_lock (); +ip6_build_xmit (sk, dstopts_getfrag, &be, &fl,sizeof (be),NULL ,255 , +MSG_DONTWAIT ); +mip6_xmit_unlock (); +} +/* TODO: Move the home address option / BCE check to tcp/udp/raw +* processing so cached route in socket can be used +* to avoid route lookup +*/ +int mip6_hao_check (struct sk_buff *skb,u8 nexthdr) +{ +struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb; +struct in6_addr *coaddr; +struct rt6_info *rt; +/* Home address option in mobility header messages is checked +by userspace mipv6 daemon */ + +if (!opt || !opt->dst_nofrag || nexthdr ==IPPROTO_MOBILITY ) +return 0 ; +if (opt && opt->dst_nofrag) { + rt =rt6_lookup (&skb->nh.ipv6h->saddr, &skb->nh.ipv6h->daddr,0 ,0 ); +if (rt) { +if (rt->binding.flags &MIPV6_F_BCE ) { +dst_release (&rt->u.dst); +return 0 ; + } +else +dst_release (&rt->u.dst); + } + coaddr = (struct in6_addr *)((u8 *)skb->nh.raw + opt->dst_nofrag); +mip6_send_be (coaddr, &skb->nh.ipv6h->daddr, &skb->nh.ipv6h->saddr); +return -1 ; + } +} + +/** +* mipv6_append_rt2hdr - Add Type 2 Routing Header +* @rt: buffer for new routing header +* @addr: intermediate hop address +* +* Adds a Routing Header Type 2 in a packet. Stores newly created +* routing header in buffer @rt. Type 2 RT only carries one address, +* so there is no need to process old routing header. @rt must have +* allocated space for 24 bytes. +**/ +void mipv6_append_rt2hdr (struct rt2_hdr *rt,struct in6_addr *addr) +{ +struct rt2_hdr *rt2 = (struct rt2_hdr *)rt; + +memset (rt2,0 ,sizeof (*rt2)); + rt2->rt_hdr.type =2 ; + rt2->rt_hdr.hdrlen =2 ; + rt2->rt_hdr.segments_left =1 ; +ipv6_addr_copy (&rt2->addr, addr); +} + +struct mipv6_padn +{ +__u8 type; +__u8 length; +__u8 data[0 ]; +} __attribute__ ((packed)); + +/* +* Add Pad1 or PadN option to data +*/ +int mipv6_add_pad (u8 *data,int n) +{ +struct mipv6_padn *padn; + +if (n <=0 )return 0 ; +if (n ==1 ) { + *data =MIPV6_OPT_PAD1 ; +return 1 ; + } + padn = (struct mipv6_padn *)data; + padn->type =MIPV6_OPT_PADN ; + padn->length = n -2 ; +memset (padn->data,0 , n -2 ); +return n; +} + +/** +* mipv6_append_home_addr - Add Home Address Option +* @opt: buffer for Home Address Option +* @offset: offset from beginning of @opt +* @addr: address for HAO +* +* Adds a Home Address Option to a packet. Option is stored in +* @offset from beginning of @opt. The option is created but the +* original source address in IPv6 header is left intact. The source +* address will be changed from home address to CoA after the checksum +* has been calculated in getfrag. Padding is done automatically, and +* @opt must have allocated space for both actual option and pad. +* Returns offset from @opt to end of options. +**/ +int mipv6_append_home_addr (u8 *opt,struct in6_addr *addr) +{ +int pad; +struct ipv6_dstopt_homeaddr *ho; +int offset =sizeof (struct ipv6_opt_hdr ); + + pad = (6 - offset) &7 ; +mipv6_add_pad (opt + offset, pad); + + ho = (struct ipv6_dstopt_homeaddr *)(opt + offset + pad); + ho->type =IPV6_TLV_HOMEADDR ; + ho->length =sizeof (*ho) -2 ; +ipv6_addr_copy (&ho->addr, addr); + +return offset + pad +sizeof (*ho); +} + + +static int get_offset (u8 *packet,u32 packet_len,u8 *nexthdr,int *offset_prevhdr) +{ +u16 offset =sizeof (struct ipv6hdr ); +struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr *)(packet + offset); +u8 nextnexthdr; + + *nexthdr = ((struct ipv6hdr *)packet)->nexthdr; + +while (offset +1 < packet_len) { + +switch (*nexthdr) { + +case NEXTHDR_HOP : +case NEXTHDR_ROUTING : + *offset_prevhdr = offset; + offset +=ipv6_optlen (exthdr); + *nexthdr = exthdr->nexthdr; + exthdr = (struct ipv6_opt_hdr *)(packet + offset); +break ; + +case NEXTHDR_DEST : + nextnexthdr = + ((struct ipv6_opt_hdr *)(packet + offset +ipv6_optlen (exthdr)))->nexthdr; +/* XXX We know the option is inner dest opt +with next next header check. */ +if (nextnexthdr !=NEXTHDR_HOP && + nextnexthdr !=NEXTHDR_ROUTING && + nextnexthdr !=NEXTHDR_DEST ) { +return offset; + } + *offset_prevhdr = offset; + offset +=ipv6_optlen (exthdr); + *nexthdr = exthdr->nexthdr; + exthdr = (struct ipv6_opt_hdr *)(packet + offset); +break ; + +default : +return offset; + } + } + +return offset; +} + + +int mip6_output (struct sk_buff *skb) + +{ +struct ipv6hdr *iph =NULL , *top_iph; +struct dst_entry *dst = skb->dst; +struct ipv6_opt_hdr *prevhdr =NULL ; +struct rt6_info *rt = (struct rt6_info *)skb->dst; +u8 nexthdr; +int offset_prevhdr =0 ; +int hdr_len =get_offset (skb->nh.raw, skb->len, &nexthdr, &offset_prevhdr); +int len, err =0 ; + +if (nexthdr ==IPPROTO_MOBILITY )/* No exthdrs for MH */ + goto out; + +/* First, if the skb is not checksummed, complete checksum. */ +if (skb->ip_summed ==CHECKSUM_HW &&skb_checksum_help (skb) ==NULL ) { + err = -EINVAL ; + goto error; + } + iph =kmalloc (hdr_len,GFP_ATOMIC ); + +if (!iph) { + err = -ENOMEM ; + goto error; + } + +memcpy (iph, skb->nh.raw, hdr_len); +__skb_pull (skb, hdr_len); + +/* TODO: Is this correct ? */ +if ((err =skb_cow (skb,mip6_hdrlen (rt->binding.flags)) !=0 )) + goto error; +if (rt->binding.flags &MIPV6_F_BULE ) { +struct ipv6_opt_hdr *dstopt; + dstopt = (struct ipv6_opt_hdr *)skb_push (skb,sizeof (struct ipv6_dstopt_homeaddr ) +6 ); + dstopt->nexthdr = nexthdr; + len =mipv6_append_home_addr ((u8 *)dstopt, &iph->saddr); + dstopt->hdrlen = (len >>3 ) -1 ; +ipv6_addr_copy (&iph->saddr, &rt->binding.lcoa); + skb->h.raw = (unsigned char *)dstopt; + nexthdr =IPPROTO_DSTOPTS ; + + } +if (rt->binding.flags &MIPV6_F_BCE ) { +struct rt2_hdr *rt2; + rt2 = (struct rt2_hdr *)skb_push (skb,sizeof (struct rt2_hdr )); + skb->h.raw = (unsigned char *)rt2; +mipv6_append_rt2hdr (rt2, &iph->daddr); +ipv6_addr_copy (&iph->daddr, &rt->binding.rcoa); + rt2->rt_hdr.nexthdr = nexthdr; + nexthdr =IPPROTO_ROUTING ; + } + + top_iph = (struct ipv6hdr *)skb_push (skb, hdr_len); +memcpy (top_iph, iph, hdr_len); + skb->nh.raw = skb->data; +kfree (iph); + top_iph->payload_len =htons (skb->len -sizeof (struct ipv6hdr )); +if (offset_prevhdr) { + prevhdr = (struct ipv6_opt_hdr *)((int *)top_iph + offset_prevhdr); + prevhdr->nexthdr = nexthdr; + + }else { + top_iph->nexthdr = nexthdr; + } + + out: +if ((skb->dst =dst_pop (dst)) ==NULL ) { + err = -EHOSTUNREACH ; + goto error; + } + +return NET_XMIT_BYPASS ; + error: +kfree_skb (skb); +return err; +} + +int mip6_init (void ) +{ + mipv6_mh_socket =sock_alloc (); + mipv6_mh_socket->type =SOCK_RAW ; +struct sock *sk; +int err; + +if ((err =sock_create (PF_INET6 ,SOCK_RAW ,IPPROTO_MOBILITY , + &mipv6_mh_socket)) <0 ) { +printk (KERN_ERR +"Failed to initialize the MIP6 MH control socket (err %d).\n" , + err); +sock_release (mipv6_mh_socket); + mipv6_mh_socket =NULL ;/* for safety */ +return err; + } + + sk = mipv6_mh_socket->sk; + sk->allocation =GFP_ATOMIC ; + sk->sndbuf =SK_WMEM_MAX ; + sk->prot->unhash (sk); +return 0 ; +} + +void mip6_cleanup (void ) +{ +if (mipv6_mh_socket)sock_release (mipv6_mh_socket); + mipv6_mh_socket =NULL ;/* For safety. */ +} + + +MODULE_LICENSE ("GPL" );
mipv6-kernel/net/ipv6/route.c renamed from ipv6/route.c +119 -12
186 lines not on this page · raw message
mipv6-kernel/net/ipv6/af_inet6.c renamed from ipv6/af_inet6.c +21 -3
@@ -57,6 +57,7 @@#include <net/transp_v6.h> #include <net/ip6_route.h> #include <net/addrconf.h> +#include <net/mipv6.h> #include <asm/uaccess.h> #include <asm/system.h>
@@ -310,7 +311,7 @@ }else {if (addr_type !=IPV6_ADDR_ANY ) {/* ipv4 addr of the socket is invalid. Only the -* unspecified and mapped address have a v4 equivalent. +* unpecified and mapped address have a v4 equivalent. */ v4addr =LOOPBACK4_IPV6 ;if (!(addr_type &IPV6_ADDR_MULTICAST )) {
@@ -475,7 +476,7 @@case SIOCADDRT :case SIOCDELRT : - +case SIOCADDMIPINFO :return (ipv6_route_ioctl (cmd,(void *)arg));case SIOCSIFADDR :
@@ -780,6 +781,14 @@ err =ndisc_init (&inet6_family_ops);if (err) goto ndisc_fail; +#ifdef CONFIG_IPV6_TUNNEL + err =ip6_tunnel_init (); +if (err) + goto ip6_tunnel_fail; +#endif + err =mip6_init (); +if (err) + goto mip6_fail; err =igmp6_init (&inet6_family_ops);if (err) goto igmp_fail;
@@ -816,7 +825,6 @@/* Init v6 transport protocols. */ udpv6_init ();tcpv6_init (); -return 0 ;#ifdef CONFIG_PROC_FS
@@ -834,6 +842,12 @@igmp6_cleanup ();#endif igmp_fail: +mip6_cleanup (); +mip6_fail: +#ifdef CONFIG_IPV6_TUNNEL +ip6_tunnel_cleanup (); +ip6_tunnel_fail: +#endifndisc_cleanup (); ndisc_fail:icmpv6_cleanup ();
@@ -869,6 +883,10 @@ip6_route_cleanup ();ipv6_packet_cleanup ();igmp6_cleanup (); +mip6_cleanup (); +#ifdef CONFIG_IPV6_TUNNEL +ip6_tunnel_cleanup (); +#endif ndisc_cleanup ();icmpv6_cleanup ();#ifdef CONFIG_SYSCTL
mipv6-kernel/include/net/mipv6.h renamed from net/mipv6.h +53 -0
53 lines not on this page · raw message
mipv6-kernel/include/net/ip6_fib.h renamed from net/ip6_fib.h +10 -3
@@ -50,6 +50,13 @@int plen; }; +struct mip6_info +{ +struct in6_addr lcoa; +struct in6_addr rcoa; +u32 flags; +}; + struct rt6_info { union {
@@ -71,8 +78,9 @@struct rt6key rt6i_dst;struct rt6key rt6i_src; - +u8 rt6i_protocol; +struct mip6_info binding; };struct fib6_walker_t
@@ -111,10 +119,9 @@struct rt6_statistics {__u32 fib_nodes;__u32 fib_route_nodes; -__u32 fib_rt_alloc;/* permanent routes */ +__u32 fib_rt_alloc;/* permanet routes */ __u32 fib_rt_entries;/* rt entries in table */ __u32 fib_rt_cache;/* cache routes */ -__u32 fib_discarded_routes; };#define RTN_TL_ROOT 0x0001