NPTL: Remove gratuitous Linuxisms from gai_misc.h.
[platform/upstream/glibc.git] / inet / inet6_rth.c
1 /* Copyright (C) 2006-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2006.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <string.h>
20 #include <netinet/in.h>
21 #include <netinet/ip6.h>
22
23
24 /* RFC 3542, 7.1
25
26    This function returns the number of bytes required to hold a
27    Routing header of the specified type containing the specified
28    number of segments (addresses).  For an IPv6 Type 0 Routing header,
29    the number of segments must be between 0 and 127, inclusive.  */
30 socklen_t
31 inet6_rth_space (int type, int segments)
32 {
33   switch (type)
34     {
35     case IPV6_RTHDR_TYPE_0:
36       if (segments < 0 || segments > 127)
37         return 0;
38
39       return sizeof (struct ip6_rthdr0) + segments * sizeof (struct in6_addr);
40     }
41
42   return 0;
43 }
44
45
46 /* RFC 3542, 7.2
47
48    This function initializes the buffer pointed to by BP to contain a
49    Routing header of the specified type and sets ip6r_len based on the
50    segments parameter.  */
51 void *
52 inet6_rth_init (void *bp, socklen_t bp_len, int type, int segments)
53 {
54   struct ip6_rthdr *rthdr = (struct ip6_rthdr *) bp;
55
56   switch (type)
57     {
58     case IPV6_RTHDR_TYPE_0:
59       /* Make sure the parameters are valid and the buffer is large enough.  */
60       if (segments < 0 || segments > 127)
61         break;
62
63       socklen_t len = (sizeof (struct ip6_rthdr0)
64                        + segments * sizeof (struct in6_addr));
65       if (len > bp_len)
66         break;
67
68       /* Some implementations seem to initialize the whole memory area.  */
69       memset (bp, '\0', len);
70
71       /* Length in units of 8 octets.  */
72       rthdr->ip6r_len = segments * sizeof (struct in6_addr) / 8;
73       rthdr->ip6r_type = IPV6_RTHDR_TYPE_0;
74       return bp;
75     }
76
77   return NULL;
78 }
79
80
81 /* RFC 3542, 7.3
82
83    This function adds the IPv6 address pointed to by addr to the end of
84    the Routing header being constructed.  */
85 int
86 inet6_rth_add (void *bp, const struct in6_addr *addr)
87 {
88   struct ip6_rthdr *rthdr = (struct ip6_rthdr *) bp;
89
90   switch (rthdr->ip6r_type)
91     {
92       struct ip6_rthdr0 *rthdr0;
93     case IPV6_RTHDR_TYPE_0:
94       rthdr0 = (struct ip6_rthdr0 *) rthdr;
95       if (rthdr0->ip6r0_len * 8 / sizeof (struct in6_addr)
96           - rthdr0->ip6r0_segleft < 1)
97         return -1;
98
99       memcpy (&rthdr0->ip6r0_addr[rthdr0->ip6r0_segleft++],
100               addr, sizeof (struct in6_addr));
101
102       return 0;
103     }
104
105   return -1;
106 }
107
108
109 /* RFC 3542, 7.4
110
111    This function takes a Routing header extension header (pointed to by
112    the first argument) and writes a new Routing header that sends
113    datagrams along the reverse of that route.  The function reverses the
114    order of the addresses and sets the segleft member in the new Routing
115    header to the number of segments.  */
116 int
117 inet6_rth_reverse (const void *in, void *out)
118 {
119   struct ip6_rthdr *in_rthdr = (struct ip6_rthdr *) in;
120
121   switch (in_rthdr->ip6r_type)
122     {
123       struct ip6_rthdr0 *in_rthdr0;
124       struct ip6_rthdr0 *out_rthdr0;
125     case IPV6_RTHDR_TYPE_0:
126       in_rthdr0 = (struct ip6_rthdr0 *) in;
127       out_rthdr0 = (struct ip6_rthdr0 *) out;
128
129       /* Copy header, not the addresses.  The memory regions can overlap.  */
130       memmove (out_rthdr0, in_rthdr0, sizeof (struct ip6_rthdr0));
131
132       int total = in_rthdr0->ip6r0_len * 8 / sizeof (struct in6_addr);
133       for (int i = 0; i < total / 2; ++i)
134         {
135           /* Remember, IN_RTHDR0 and OUT_RTHDR0 might overlap.  */
136           struct in6_addr temp = in_rthdr0->ip6r0_addr[i];
137           out_rthdr0->ip6r0_addr[i] = in_rthdr0->ip6r0_addr[total - 1 - i];
138           out_rthdr0->ip6r0_addr[total - 1 - i] = temp;
139         }
140       if (total % 2 != 0 && in != out)
141         out_rthdr0->ip6r0_addr[total / 2] = in_rthdr0->ip6r0_addr[total / 2];
142
143       out_rthdr0->ip6r0_segleft = total;
144
145       return 0;
146     }
147
148   return -1;
149 }
150
151
152 /* RFC 3542, 7.5
153
154    This function returns the number of segments (addresses) contained in
155    the Routing header described by BP.  */
156 int
157 inet6_rth_segments (const void *bp)
158 {
159   struct ip6_rthdr *rthdr = (struct ip6_rthdr *) bp;
160
161   switch (rthdr->ip6r_type)
162     {
163     case IPV6_RTHDR_TYPE_0:
164
165       return rthdr->ip6r_len * 8 / sizeof (struct in6_addr);
166     }
167
168   return -1;
169 }
170
171
172 /* RFC 3542, 7.6
173
174    This function returns a pointer to the IPv6 address specified by
175    index (which must have a value between 0 and one less than the
176    value returned by 'inet6_rth_segments') in the Routing header
177    described by BP.  */
178 struct in6_addr *
179 inet6_rth_getaddr (const void *bp, int index)
180 {
181   struct ip6_rthdr *rthdr = (struct ip6_rthdr *) bp;
182
183   switch (rthdr->ip6r_type)
184     {
185        struct ip6_rthdr0 *rthdr0;
186     case IPV6_RTHDR_TYPE_0:
187       rthdr0 = (struct ip6_rthdr0 *) rthdr;
188
189       if (index >= rthdr0->ip6r0_len * 8 / sizeof (struct in6_addr))
190         break;
191
192       return &rthdr0->ip6r0_addr[index];
193     }
194
195   return NULL;
196 }