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