summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/mac/plugins/mac_ipv4.c
blob: 85ddf8dbfa281db31cc0b4c755f1566ee18c4841 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * DL_IPV4 MAC Type plugin for the Nemo mac module
 */

#include <sys/types.h>
#include <sys/modctl.h>
#include <sys/dlpi.h>
#include <sys/mac.h>
#include <sys/mac_ipv4.h>
#include <sys/byteorder.h>
#include <sys/strsun.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <inet/common.h>
#include <inet/ip.h>
#include <inet/iptun.h>

static struct modlmisc mac_ipv4_modlmisc = {
	&mod_miscops,
	"IPv4 tunneling MAC plugin"
};

static struct modlinkage mac_ipv4_modlinkage = {
	MODREV_1,
	&mac_ipv4_modlmisc,
	NULL
};

static mactype_ops_t mac_ipv4_type_ops;

int
_init(void)
{
	mactype_register_t *mtrp;
	int	err;

	if ((mtrp = mactype_alloc(MACTYPE_VERSION)) == NULL)
		return (ENOTSUP);
	mtrp->mtr_ident = MAC_PLUGIN_IDENT_IPV4;
	mtrp->mtr_ops = &mac_ipv4_type_ops;
	mtrp->mtr_mactype = DL_IPV4;
	mtrp->mtr_nativetype = DL_IPV4;
	mtrp->mtr_addrlen = sizeof (ipaddr_t);
	if ((err = mactype_register(mtrp)) == 0) {
		if ((err = mod_install(&mac_ipv4_modlinkage)) != 0)
			(void) mactype_unregister(MAC_PLUGIN_IDENT_IPV4);
	}
	mactype_free(mtrp);
	return (err);
}

int
_fini(void)
{
	int	err;
	if ((err = mactype_unregister(MAC_PLUGIN_IDENT_IPV4)) != 0)
		return (err);
	return (mod_remove(&mac_ipv4_modlinkage));
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&mac_ipv4_modlinkage, modinfop));
}

/*
 * MAC Type plugin operations
 */

/* ARGSUSED */
int
mac_ipv4_unicst_verify(const void *addr, void *pdata)
{
	const ipaddr_t *ipaddr = addr;
	return ((CLASSD(*ipaddr) || (*ipaddr == INADDR_BROADCAST)) ?
	    EINVAL : 0);
}

/* ARGSUSED */
int
mac_ipv4_multicst_verify(const void *addr, void *pdata)
{
	/*
	 * IPv4 configured tunnels do not have the concept of link-layer
	 * multicast.
	 */
	return (ENOTSUP);
}

/*
 * Check the legality of an IPv4 tunnel SAP value.  The only two acceptable
 * values are IPPROTO_ENCAP (IPv4 in IPv4) and IPPROTO_IPV6 (IPv6 in IPv4).
 */
/* ARGSUSED */
boolean_t
mac_ipv4_sap_verify(uint32_t sap, uint32_t *bind_sap, void *pdata)
{
	if (sap == IPPROTO_ENCAP || sap == IPPROTO_IPV6 || sap == 0) {
		if (bind_sap != NULL)
			*bind_sap = sap;
		return (B_TRUE);
	}
	return (B_FALSE);
}

/*
 * Build an IPv4 link-layer header for tunneling.  If provided, the
 * template header provided by the driver supplies the header length, type
 * of service, don't fragment flag, ttl, and potential options (depending
 * on the header length).
 */
/* ARGSUSED */
mblk_t *
mac_ipv4_header(const void *saddr, const void *daddr, uint32_t sap, void *pdata,
    mblk_t *payload, size_t extra_len)
{
	struct ip	*iphp;
	struct ip	*tmpl_iphp = pdata;
	mblk_t		*mp;
	size_t		hdr_len = sizeof (struct ip);

	if (!mac_ipv4_sap_verify(sap, NULL, NULL))
		return (NULL);

	if (tmpl_iphp != NULL)
		hdr_len = tmpl_iphp->ip_hl * sizeof (uint32_t);

	if ((mp = allocb(hdr_len + extra_len, BPRI_HI)) == NULL)
		return (NULL);

	iphp = (struct ip *)mp->b_rptr;

	bzero(iphp, hdr_len + extra_len);
	if (tmpl_iphp != NULL) {
		bcopy(tmpl_iphp, iphp, hdr_len);
	} else {
		iphp->ip_hl = IP_SIMPLE_HDR_LENGTH_IN_WORDS;
		iphp->ip_off = htons(IP_DF);
		iphp->ip_ttl = IPTUN_DEFAULT_HOPLIMIT;
	}

	iphp->ip_v = IPVERSION;
	iphp->ip_len = 0;
	iphp->ip_p = (uint8_t)sap;
	bcopy(saddr, &(iphp->ip_src), sizeof (struct in_addr));
	bcopy(daddr, &(iphp->ip_dst), sizeof (struct in_addr));

	mp->b_wptr += hdr_len;
	return (mp);
}

/* ARGSUSED */
int
mac_ipv4_header_info(mblk_t *mp, void *pdata, mac_header_info_t *hdr_info)
{
	struct ip	*iphp;

	if (MBLKL(mp) < sizeof (struct ip))
		return (EINVAL);

	iphp = (struct ip *)mp->b_rptr;

	/*
	 * IPv4 tunnels don't have a concept of link-layer multicast since
	 * they have fixed unicast endpoints.
	 */
	if (mac_ipv4_unicst_verify(&iphp->ip_dst, NULL) != 0)
		return (EINVAL);

	hdr_info->mhi_hdrsize = iphp->ip_hl * sizeof (uint32_t);
	hdr_info->mhi_pktsize = 0;
	hdr_info->mhi_daddr = (const uint8_t *)&(iphp->ip_dst);
	hdr_info->mhi_saddr = (const uint8_t *)&(iphp->ip_src);
	hdr_info->mhi_origsap = hdr_info->mhi_bindsap = iphp->ip_p;
	hdr_info->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
	return (0);
}

/*
 * Plugin data is either NULL or a pointer to an IPv4 header.
 */
boolean_t
mac_ipv4_pdata_verify(void *pdata, size_t pdata_size)
{
	const struct ip	*iphp = pdata;

	if (pdata == NULL)
		return (pdata_size == 0);
	if (pdata_size < sizeof (struct ip))
		return (B_FALSE);
	/* Make sure that the header length field matches pdata_size */
	return (pdata_size == iphp->ip_hl * sizeof (uint32_t));
}

static mactype_ops_t	mac_ipv4_type_ops = {
	MTOPS_PDATA_VERIFY,
	mac_ipv4_unicst_verify,
	mac_ipv4_multicst_verify,
	mac_ipv4_sap_verify,
	mac_ipv4_header,
	mac_ipv4_header_info,
	mac_ipv4_pdata_verify,
	NULL,
	NULL,
	NULL
};