1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. 24*0Sstevel@tonic-gate * All rights reserved. Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * This code is conformant to revision 7 of 2292bis. Some of these functions 31*0Sstevel@tonic-gate * were provided (named inet6_rthdr_) in a very similar form in RFC 2292. 32*0Sstevel@tonic-gate * The RFC 2292 variants are not supported. 33*0Sstevel@tonic-gate */ 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate #include <stdio.h> 36*0Sstevel@tonic-gate #include <ctype.h> 37*0Sstevel@tonic-gate #include <string.h> 38*0Sstevel@tonic-gate #include <stdlib.h> 39*0Sstevel@tonic-gate #include <sys/types.h> 40*0Sstevel@tonic-gate #include <sys/socket.h> 41*0Sstevel@tonic-gate #include <netinet/in.h> 42*0Sstevel@tonic-gate #include <netinet/ip6.h> 43*0Sstevel@tonic-gate #include <unistd.h> 44*0Sstevel@tonic-gate #include <errno.h> 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate #define MAX_RTHDR0_SEGMENTS 127 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate /* 49*0Sstevel@tonic-gate * Return amount of space needed to hold N segments for the specified 50*0Sstevel@tonic-gate * routing type. Does NOT include space for cmsghdr. 51*0Sstevel@tonic-gate */ 52*0Sstevel@tonic-gate socklen_t 53*0Sstevel@tonic-gate inet6_rth_space(int type, int segments) 54*0Sstevel@tonic-gate { 55*0Sstevel@tonic-gate if (type != IPV6_RTHDR_TYPE_0 || segments < 0 || 56*0Sstevel@tonic-gate segments > MAX_RTHDR0_SEGMENTS) 57*0Sstevel@tonic-gate return (0); 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate return (sizeof (struct ip6_rthdr0) + 60*0Sstevel@tonic-gate segments * sizeof (struct in6_addr)); 61*0Sstevel@tonic-gate } 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate /* 64*0Sstevel@tonic-gate * Initializes rthdr structure. Verifies the segments against the length of 65*0Sstevel@tonic-gate * the buffer. 66*0Sstevel@tonic-gate * Note that a routing header can only hold 127 segments since the length field 67*0Sstevel@tonic-gate * in the header is just a byte. 68*0Sstevel@tonic-gate */ 69*0Sstevel@tonic-gate void * 70*0Sstevel@tonic-gate inet6_rth_init(void *bp, socklen_t bp_len, int type, int segments) 71*0Sstevel@tonic-gate { 72*0Sstevel@tonic-gate struct ip6_rthdr0 *rthdr; 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate if (type != IPV6_RTHDR_TYPE_0 || segments < 0 || 75*0Sstevel@tonic-gate segments > MAX_RTHDR0_SEGMENTS) 76*0Sstevel@tonic-gate return (NULL); 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate if (bp_len < sizeof (struct ip6_rthdr0) + 79*0Sstevel@tonic-gate segments * sizeof (struct in6_addr)) 80*0Sstevel@tonic-gate return (NULL); 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate rthdr = (struct ip6_rthdr0 *)bp; 83*0Sstevel@tonic-gate rthdr->ip6r0_nxt = 0; 84*0Sstevel@tonic-gate rthdr->ip6r0_len = (segments * 2); 85*0Sstevel@tonic-gate rthdr->ip6r0_type = type; 86*0Sstevel@tonic-gate rthdr->ip6r0_segleft = 0; /* Incremented by rthdr_add */ 87*0Sstevel@tonic-gate *(uint32_t *)&rthdr->ip6r0_reserved = 0; 88*0Sstevel@tonic-gate return (bp); 89*0Sstevel@tonic-gate } 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate /* 92*0Sstevel@tonic-gate * Add one more address to the routing header. Fails when there is no more 93*0Sstevel@tonic-gate * room. 94*0Sstevel@tonic-gate */ 95*0Sstevel@tonic-gate int 96*0Sstevel@tonic-gate inet6_rth_add(void *bp, const struct in6_addr *addr) 97*0Sstevel@tonic-gate { 98*0Sstevel@tonic-gate struct ip6_rthdr0 *rthdr; 99*0Sstevel@tonic-gate struct in6_addr *addrs; 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate rthdr = (struct ip6_rthdr0 *)bp; 102*0Sstevel@tonic-gate if ((rthdr->ip6r0_segleft + 1) * 2 > rthdr->ip6r0_len) { 103*0Sstevel@tonic-gate /* Not room for one more */ 104*0Sstevel@tonic-gate return (-1); 105*0Sstevel@tonic-gate } 106*0Sstevel@tonic-gate addrs = (struct in6_addr *)((char *)rthdr + sizeof (*rthdr)); 107*0Sstevel@tonic-gate addrs[rthdr->ip6r0_segleft++] = *addr; 108*0Sstevel@tonic-gate return (0); 109*0Sstevel@tonic-gate } 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate /* 112*0Sstevel@tonic-gate * Reverse a source route. Both arguments can point to the same buffer. 113*0Sstevel@tonic-gate */ 114*0Sstevel@tonic-gate int 115*0Sstevel@tonic-gate inet6_rth_reverse(const void *in, void *out) 116*0Sstevel@tonic-gate { 117*0Sstevel@tonic-gate struct ip6_rthdr0 *rtin, *rtout; 118*0Sstevel@tonic-gate int i, segments; 119*0Sstevel@tonic-gate struct in6_addr tmp; 120*0Sstevel@tonic-gate struct in6_addr *rtout_addrs; 121*0Sstevel@tonic-gate struct in6_addr *rtin_addrs; 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate rtin = (struct ip6_rthdr0 *)in; 124*0Sstevel@tonic-gate rtout = (struct ip6_rthdr0 *)out; 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate if (rtout->ip6r0_type != 0 || rtin->ip6r0_type != 0 || 127*0Sstevel@tonic-gate rtout->ip6r0_len > MAX_RTHDR0_SEGMENTS * 2 || 128*0Sstevel@tonic-gate rtin->ip6r0_len > MAX_RTHDR0_SEGMENTS * 2 || 129*0Sstevel@tonic-gate rtout->ip6r0_len != rtin->ip6r0_len) 130*0Sstevel@tonic-gate return (-1); 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate segments = rtin->ip6r0_len / 2; 133*0Sstevel@tonic-gate rtout_addrs = (struct in6_addr *)((char *)rtout + sizeof (*rtout)); 134*0Sstevel@tonic-gate rtin_addrs = (struct in6_addr *)((char *)rtin + sizeof (*rtin)); 135*0Sstevel@tonic-gate for (i = 0; i < (segments + 1)/2; i++) { 136*0Sstevel@tonic-gate tmp = rtin_addrs[i]; 137*0Sstevel@tonic-gate rtout_addrs[i] = rtin_addrs[segments - 1 - i]; 138*0Sstevel@tonic-gate rtout_addrs[segments - 1 - i] = tmp; 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate rtout->ip6r0_segleft = segments; 141*0Sstevel@tonic-gate return (0); 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate /* 145*0Sstevel@tonic-gate * Return the number of segments in the routing header. 146*0Sstevel@tonic-gate */ 147*0Sstevel@tonic-gate int 148*0Sstevel@tonic-gate inet6_rth_segments(const void *bp) 149*0Sstevel@tonic-gate { 150*0Sstevel@tonic-gate struct ip6_rthdr0 *rthdr; 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate rthdr = (struct ip6_rthdr0 *)bp; 153*0Sstevel@tonic-gate if (rthdr->ip6r0_type == 0) { 154*0Sstevel@tonic-gate if (rthdr->ip6r0_len > MAX_RTHDR0_SEGMENTS * 2) { 155*0Sstevel@tonic-gate return (-1); 156*0Sstevel@tonic-gate } else { 157*0Sstevel@tonic-gate return (rthdr->ip6r0_len / 2); 158*0Sstevel@tonic-gate } 159*0Sstevel@tonic-gate } else { 160*0Sstevel@tonic-gate return (-1); 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate } 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate /* 165*0Sstevel@tonic-gate * Return a pointer to an element in the source route. 166*0Sstevel@tonic-gate * This uses the C convention for index [0, size-1]. 167*0Sstevel@tonic-gate */ 168*0Sstevel@tonic-gate struct in6_addr * 169*0Sstevel@tonic-gate inet6_rth_getaddr(const void *bp, int index) 170*0Sstevel@tonic-gate { 171*0Sstevel@tonic-gate struct ip6_rthdr0 *rthdr; 172*0Sstevel@tonic-gate struct in6_addr *rv; 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate rthdr = (struct ip6_rthdr0 *)bp; 175*0Sstevel@tonic-gate if (index >= rthdr->ip6r0_len/2 || index < 0) 176*0Sstevel@tonic-gate return (NULL); 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate rv = (struct in6_addr *)((char *)rthdr + sizeof (*rthdr)); 179*0Sstevel@tonic-gate return (&rv[index]); 180*0Sstevel@tonic-gate } 181