summaryrefslogtreecommitdiff
path: root/usr/src/lib/libsocket/inet/inet6_rthdr.c
blob: 4134f8a196e1f0b6d81a592c879dc8a0a9c5aab7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2003 Sun Microsystems, Inc.
 * All rights reserved.  Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * This code is conformant to revision 7 of 2292bis.  Some of these functions
 * were provided (named inet6_rthdr_) in a very similar form in RFC 2292.
 * The RFC 2292 variants are not supported.
 */

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <unistd.h>
#include <errno.h>

#define	MAX_RTHDR0_SEGMENTS 127

/*
 * Return amount of space needed to hold N segments for the specified
 * routing type. Does NOT include space for cmsghdr.
 */
socklen_t
inet6_rth_space(int type, int segments)
{
	if (type != IPV6_RTHDR_TYPE_0 || segments < 0 ||
	    segments > MAX_RTHDR0_SEGMENTS)
		return (0);

	return (sizeof (struct ip6_rthdr0) +
	    segments * sizeof (struct in6_addr));
}

/*
 * Initializes rthdr structure. Verifies the segments against the length of
 * the buffer.
 * Note that a routing header can only hold 127 segments since the length field
 * in the header is just a byte.
 */
void *
inet6_rth_init(void *bp, socklen_t bp_len, int type, int segments)
{
	struct ip6_rthdr0 *rthdr;

	if (type != IPV6_RTHDR_TYPE_0 || segments < 0 ||
	    segments > MAX_RTHDR0_SEGMENTS)
		return (NULL);

	if (bp_len < sizeof (struct ip6_rthdr0) +
	    segments * sizeof (struct in6_addr))
		return (NULL);

	rthdr = (struct ip6_rthdr0 *)bp;
	rthdr->ip6r0_nxt = 0;
	rthdr->ip6r0_len = (segments * 2);
	rthdr->ip6r0_type = type;
	rthdr->ip6r0_segleft = 0;	/* Incremented by rthdr_add */
	*(uint32_t *)&rthdr->ip6r0_reserved = 0;
	return (bp);
}

/*
 * Add one more address to the routing header. Fails when there is no more
 * room.
 */
int
inet6_rth_add(void *bp, const struct in6_addr *addr)
{
	struct ip6_rthdr0 *rthdr;
	struct in6_addr *addrs;

	rthdr = (struct ip6_rthdr0 *)bp;
	if ((rthdr->ip6r0_segleft + 1) * 2 > rthdr->ip6r0_len) {
		/* Not room for one more */
		return (-1);
	}
	addrs = (struct in6_addr *)((char *)rthdr + sizeof (*rthdr));
	addrs[rthdr->ip6r0_segleft++] = *addr;
	return (0);
}

/*
 * Reverse a source route. Both arguments can point to the same buffer.
 */
int
inet6_rth_reverse(const void *in, void *out)
{
	struct ip6_rthdr0 *rtin, *rtout;
	int i, segments;
	struct in6_addr tmp;
	struct in6_addr *rtout_addrs;
	struct in6_addr *rtin_addrs;

	rtin = (struct ip6_rthdr0 *)in;
	rtout = (struct ip6_rthdr0 *)out;

	if (rtout->ip6r0_type != 0 || rtin->ip6r0_type != 0 ||
	    rtout->ip6r0_len > MAX_RTHDR0_SEGMENTS * 2 ||
	    rtin->ip6r0_len > MAX_RTHDR0_SEGMENTS * 2 ||
	    rtout->ip6r0_len != rtin->ip6r0_len)
		return (-1);

	segments = rtin->ip6r0_len / 2;
	rtout_addrs = (struct in6_addr *)((char *)rtout + sizeof (*rtout));
	rtin_addrs = (struct in6_addr *)((char *)rtin + sizeof (*rtin));
	for (i = 0; i < (segments + 1)/2; i++) {
		tmp = rtin_addrs[i];
		rtout_addrs[i] = rtin_addrs[segments - 1 - i];
		rtout_addrs[segments - 1 - i] = tmp;
	}
	rtout->ip6r0_segleft = segments;
	return (0);
}

/*
 * Return the number of segments in the routing header.
 */
int
inet6_rth_segments(const void *bp)
{
	struct ip6_rthdr0 *rthdr;

	rthdr = (struct ip6_rthdr0 *)bp;
	if (rthdr->ip6r0_type == 0) {
		if (rthdr->ip6r0_len > MAX_RTHDR0_SEGMENTS * 2) {
			return (-1);
		} else {
			return (rthdr->ip6r0_len / 2);
		}
	} else {
		return (-1);
	}
}

/*
 * Return a pointer to an element in the source route.
 * This uses the C convention for index [0, size-1].
 */
struct in6_addr *
inet6_rth_getaddr(const void *bp, int index)
{
	struct ip6_rthdr0 *rthdr;
	struct in6_addr *rv;

	rthdr = (struct ip6_rthdr0 *)bp;
	if (index >= rthdr->ip6r0_len/2 || index < 0)
		return (NULL);

	rv = (struct in6_addr *)((char *)rthdr + sizeof (*rthdr));
	return (&rv[index]);
}