summaryrefslogtreecommitdiffstats
path: root/usr.sbin/ypserv/yp_access_inet6.c
blob: a45de20052fccf407cfccdd1c25650c337d53dd1 (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
/*
 * Copyright (c) 2010-2011 Hiroki Sato.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>

#include "yp_extern.h"

int
yp_mask2prefixlen_in6(const struct sockaddr *sap, int *prefixlen)
{
        int x, y = 0;
        const u_char *p;
	const struct in6_addr *addr;
	const struct sockaddr_in6 *sain6p;

	sain6p = (const struct sockaddr_in6 *)sap;
	addr = &(sain6p->sin6_addr);
        for (x = 0; x < (int)sizeof(addr->s6_addr); x++)
		if (addr->s6_addr[x] != 0xff)
			break;
        if (x < (int)sizeof(addr->s6_addr))
                for (y = 0; y < 8; y++)
                        if ((addr->s6_addr[x] & (0x80 >> y)) == 0)
                                break;
        if (x < (int)sizeof(addr->s6_addr)) {
                if (y != 0 && (addr->s6_addr[x] & (0x00ff >> y)) != 0)
                        return (-1);
		p = (const u_char *)&addr->s6_addr[x + 1];
                for (; p < addr->s6_addr + sizeof(addr->s6_addr); p++)
                        if (*p != 0)
                                return (-1);
        }
	*prefixlen = x * 8 + y;
        return (0);
}

int
yp_prefixlen2mask_in6(struct sockaddr *sap, const int *prefixlen)
{
	int i;
	int len;
	int bytelen, bitlen;
	u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
	struct in6_addr *addr;
	struct sockaddr_in6 *sain6p;

	len = *prefixlen;
	if (0 > len || len > 128)
		return (-1);

	sain6p = (struct sockaddr_in6 *)sap;
	memset(&sain6p->sin6_addr, 0, sizeof(sain6p->sin6_addr));
	addr = &(sain6p->sin6_addr);
	bytelen = len / 8;
	bitlen = len % 8;
	for (i = 0; i < bytelen; i++)
		addr->s6_addr[i] = 0xff;
	if (bitlen)
		addr->s6_addr[bytelen] = maskarray[bitlen - 1];
	return (0);
}

struct sockaddr *
yp_mask_in6(const struct sockaddr *addr, const struct sockaddr *mask)
{
	int i;
	const u_char *p, *q;
	u_char *r;
	const struct sockaddr_in6 *in6_addr;
	const struct sockaddr_in6 *in6_mask;
	struct sockaddr_in6 *in6_res;

	in6_addr = (const struct sockaddr_in6 *)addr;
	in6_mask = (const struct sockaddr_in6 *)mask;

	if ((in6_res = malloc(sizeof(*in6_res))) == NULL)
		return NULL;
	memcpy(in6_res, in6_addr, sizeof(*in6_res));
	p = (const u_char *)&(in6_addr->sin6_addr.s6_addr);
	q = (const u_char *)&(in6_mask->sin6_addr.s6_addr);
	r = (u_char *)&(in6_res->sin6_addr.s6_addr);
	for (i = 0; i < (int)sizeof(in6_addr->sin6_addr.s6_addr); i++)
		r[i] = p[i] & q[i];
        
	return ((struct sockaddr *)in6_res);
}

int
yp_compare_subnet_in6(const struct sockaddr *a1, const struct sockaddr *a2)
{
	const struct sockaddr_in6 *in6_a1 = (const struct sockaddr_in6 *)a1;
	const struct sockaddr_in6 *in6_a2 = (const struct sockaddr_in6 *)a2;

	if (debug) {
		yp_error("yp_subnet_cmp_in6(): a1");
		yp_debug_sa(a1);
		yp_error("yp_subnet_cmp_in6(): a2");
		yp_debug_sa(a2);
		yp_error("yp_subnet_cmp_in6(): scope: %d - %d",
		    in6_a1->sin6_scope_id, in6_a2->sin6_scope_id);
        }

	if (in6_a1->sin6_scope_id != in6_a2->sin6_scope_id)
		return (-1);

	return (memcmp(in6_a1->sin6_addr.s6_addr,
		    in6_a2->sin6_addr.s6_addr,
		    sizeof(in6_a1->sin6_addr.s6_addr)));
}
OpenPOWER on IntegriCloud