link_proto.c revision 1.2.4.2 1 1.2.4.2 ad /* $NetBSD: link_proto.c,v 1.2.4.2 2007/08/20 22:07:08 ad Exp $ */
2 1.2.4.2 ad
3 1.2.4.2 ad /*-
4 1.2.4.2 ad * Copyright (c) 1982, 1986, 1993
5 1.2.4.2 ad * The Regents of the University of California. All rights reserved.
6 1.2.4.2 ad *
7 1.2.4.2 ad * Redistribution and use in source and binary forms, with or without
8 1.2.4.2 ad * modification, are permitted provided that the following conditions
9 1.2.4.2 ad * are met:
10 1.2.4.2 ad * 1. Redistributions of source code must retain the above copyright
11 1.2.4.2 ad * notice, this list of conditions and the following disclaimer.
12 1.2.4.2 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.4.2 ad * notice, this list of conditions and the following disclaimer in the
14 1.2.4.2 ad * documentation and/or other materials provided with the distribution.
15 1.2.4.2 ad * 3. Neither the name of the University nor the names of its contributors
16 1.2.4.2 ad * may be used to endorse or promote products derived from this software
17 1.2.4.2 ad * without specific prior written permission.
18 1.2.4.2 ad *
19 1.2.4.2 ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.2.4.2 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.2.4.2 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.2.4.2 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.2.4.2 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.2.4.2 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.2.4.2 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.2.4.2 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.2.4.2 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.2.4.2 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.2.4.2 ad * SUCH DAMAGE.
30 1.2.4.2 ad *
31 1.2.4.2 ad * @(#)uipc_proto.c 8.2 (Berkeley) 2/14/95
32 1.2.4.2 ad */
33 1.2.4.2 ad
34 1.2.4.2 ad #include <sys/cdefs.h>
35 1.2.4.2 ad __KERNEL_RCSID(0, "$NetBSD: link_proto.c,v 1.2.4.2 2007/08/20 22:07:08 ad Exp $");
36 1.2.4.2 ad
37 1.2.4.2 ad #include <sys/param.h>
38 1.2.4.2 ad #include <sys/socket.h>
39 1.2.4.2 ad #include <sys/protosw.h>
40 1.2.4.2 ad #include <sys/domain.h>
41 1.2.4.2 ad #include <sys/mbuf.h>
42 1.2.4.2 ad #include <sys/un.h>
43 1.2.4.2 ad #include <sys/socketvar.h>
44 1.2.4.2 ad
45 1.2.4.2 ad #include <net/if.h>
46 1.2.4.2 ad #include <net/if_dl.h>
47 1.2.4.2 ad #include <net/raw_cb.h>
48 1.2.4.2 ad
49 1.2.4.2 ad static int sockaddr_dl_cmp(const struct sockaddr *, const struct sockaddr *);
50 1.2.4.2 ad
51 1.2.4.2 ad /*
52 1.2.4.2 ad * Definitions of protocols supported in the link-layer domain.
53 1.2.4.2 ad */
54 1.2.4.2 ad
55 1.2.4.2 ad DOMAIN_DEFINE(linkdomain); /* forward define and add to link set */
56 1.2.4.2 ad
57 1.2.4.2 ad POOL_INIT(sockaddr_dl_pool, sizeof(struct sockaddr_dl), 0, 0, 0,
58 1.2.4.2 ad "sockaddr_dl_pool", NULL, IPL_NET);
59 1.2.4.2 ad
60 1.2.4.2 ad struct domain linkdomain = {
61 1.2.4.2 ad .dom_family = AF_LINK,
62 1.2.4.2 ad .dom_name = "link",
63 1.2.4.2 ad .dom_externalize = NULL,
64 1.2.4.2 ad .dom_dispose = NULL,
65 1.2.4.2 ad .dom_protosw = NULL,
66 1.2.4.2 ad .dom_protoswNPROTOSW = NULL,
67 1.2.4.2 ad .dom_sa_pool = &sockaddr_dl_pool,
68 1.2.4.2 ad .dom_sa_len = sizeof(struct sockaddr_dl),
69 1.2.4.2 ad .dom_sockaddr_cmp = sockaddr_dl_cmp
70 1.2.4.2 ad };
71 1.2.4.2 ad
72 1.2.4.2 ad /* Compare the field at byte offsets [fieldstart, fieldend) in
73 1.2.4.2 ad * two memory regions, [l, l + llen) and [r, r + llen).
74 1.2.4.2 ad */
75 1.2.4.2 ad static inline int
76 1.2.4.2 ad submemcmp(const void *l, const void *r,
77 1.2.4.2 ad const uint_fast8_t llen, const uint_fast8_t rlen,
78 1.2.4.2 ad const uint_fast8_t fieldstart, const uint_fast8_t fieldend)
79 1.2.4.2 ad {
80 1.2.4.2 ad uint_fast8_t cmpend, minlen;
81 1.2.4.2 ad const uint8_t *lb = l, *rb = r;
82 1.2.4.2 ad int rc;
83 1.2.4.2 ad
84 1.2.4.2 ad minlen = MIN(llen, rlen);
85 1.2.4.2 ad
86 1.2.4.2 ad /* The field is missing from one region. The shorter region is the
87 1.2.4.2 ad * lesser region.
88 1.2.4.2 ad */
89 1.2.4.2 ad if (fieldstart >= minlen)
90 1.2.4.2 ad return llen - rlen;
91 1.2.4.2 ad
92 1.2.4.2 ad /* Two empty, present fields are always equal. */
93 1.2.4.2 ad if (fieldstart > fieldend)
94 1.2.4.2 ad return 0;
95 1.2.4.2 ad
96 1.2.4.2 ad cmpend = MIN(fieldend, minlen);
97 1.2.4.2 ad
98 1.2.4.2 ad rc = memcmp(&lb[fieldstart], &rb[fieldstart], cmpend - fieldstart);
99 1.2.4.2 ad
100 1.2.4.2 ad if (rc != 0)
101 1.2.4.2 ad return rc;
102 1.2.4.2 ad /* If one or both fields are truncated, then the shorter is the lesser
103 1.2.4.2 ad * field.
104 1.2.4.2 ad */
105 1.2.4.2 ad if (minlen < fieldend)
106 1.2.4.2 ad return llen - rlen;
107 1.2.4.2 ad /* Fields are full-length and equal. The fields are equal. */
108 1.2.4.2 ad return 0;
109 1.2.4.2 ad }
110 1.2.4.2 ad
111 1.2.4.2 ad uint8_t
112 1.2.4.2 ad sockaddr_dl_measure(uint8_t namelen, uint8_t addrlen)
113 1.2.4.2 ad {
114 1.2.4.2 ad return offsetof(struct sockaddr_dl, sdl_data[namelen + addrlen]);
115 1.2.4.2 ad }
116 1.2.4.2 ad
117 1.2.4.2 ad void
118 1.2.4.2 ad sockaddr_dl_init(struct sockaddr_dl *sdl, uint16_t ifindex, uint8_t type,
119 1.2.4.2 ad const void *name, uint8_t namelen, const void *addr, uint8_t addrlen)
120 1.2.4.2 ad {
121 1.2.4.2 ad sdl->sdl_family = AF_LINK;
122 1.2.4.2 ad sdl->sdl_slen = 0;
123 1.2.4.2 ad sdl->sdl_len = sockaddr_dl_measure(namelen, addrlen);
124 1.2.4.2 ad KASSERT(sdl->sdl_len <= sizeof(*sdl));
125 1.2.4.2 ad sdl->sdl_index = ifindex;
126 1.2.4.2 ad sdl->sdl_type = type;
127 1.2.4.2 ad memset(&sdl->sdl_data[0], 0, sizeof(sdl->sdl_data));
128 1.2.4.2 ad if (name != NULL)
129 1.2.4.2 ad memcpy(&sdl->sdl_data[0], name, namelen);
130 1.2.4.2 ad sdl->sdl_nlen = namelen;
131 1.2.4.2 ad if (addr != NULL)
132 1.2.4.2 ad memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
133 1.2.4.2 ad sdl->sdl_alen = addrlen;
134 1.2.4.2 ad }
135 1.2.4.2 ad
136 1.2.4.2 ad static int
137 1.2.4.2 ad sockaddr_dl_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
138 1.2.4.2 ad {
139 1.2.4.2 ad int rc;
140 1.2.4.2 ad const uint_fast8_t indexofs = offsetof(struct sockaddr_dl, sdl_index);
141 1.2.4.2 ad const uint_fast8_t nlenofs = offsetof(struct sockaddr_dl, sdl_nlen);
142 1.2.4.2 ad uint_fast8_t dataofs = offsetof(struct sockaddr_dl, sdl_data[0]);
143 1.2.4.2 ad const struct sockaddr_dl *sdl1, *sdl2;
144 1.2.4.2 ad
145 1.2.4.2 ad sdl1 = satocsdl(sa1);
146 1.2.4.2 ad sdl2 = satocsdl(sa2);
147 1.2.4.2 ad
148 1.2.4.2 ad rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
149 1.2.4.2 ad indexofs, nlenofs);
150 1.2.4.2 ad
151 1.2.4.2 ad if (rc != 0)
152 1.2.4.2 ad return rc;
153 1.2.4.2 ad
154 1.2.4.2 ad rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
155 1.2.4.2 ad dataofs, dataofs + MIN(sdl1->sdl_nlen, sdl2->sdl_nlen));
156 1.2.4.2 ad
157 1.2.4.2 ad if (rc != 0)
158 1.2.4.2 ad return rc;
159 1.2.4.2 ad
160 1.2.4.2 ad if (sdl1->sdl_nlen != sdl2->sdl_nlen)
161 1.2.4.2 ad return sdl1->sdl_nlen - sdl2->sdl_nlen;
162 1.2.4.2 ad
163 1.2.4.2 ad dataofs += sdl1->sdl_nlen;
164 1.2.4.2 ad
165 1.2.4.2 ad rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
166 1.2.4.2 ad dataofs, dataofs + MIN(sdl1->sdl_alen, sdl2->sdl_alen));
167 1.2.4.2 ad
168 1.2.4.2 ad if (rc != 0)
169 1.2.4.2 ad return rc;
170 1.2.4.2 ad
171 1.2.4.2 ad if (sdl1->sdl_alen != sdl2->sdl_alen)
172 1.2.4.2 ad return sdl1->sdl_alen - sdl2->sdl_alen;
173 1.2.4.2 ad
174 1.2.4.2 ad dataofs += sdl1->sdl_alen;
175 1.2.4.2 ad
176 1.2.4.2 ad rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
177 1.2.4.2 ad dataofs, dataofs + MIN(sdl1->sdl_slen, sdl2->sdl_slen));
178 1.2.4.2 ad
179 1.2.4.2 ad if (sdl1->sdl_slen != sdl2->sdl_slen)
180 1.2.4.2 ad return sdl1->sdl_slen - sdl2->sdl_slen;
181 1.2.4.2 ad
182 1.2.4.2 ad return sdl1->sdl_len - sdl2->sdl_len;
183 1.2.4.2 ad }
184 1.2.4.2 ad
185 1.2.4.2 ad struct sockaddr_dl *
186 1.2.4.2 ad sockaddr_dl_setaddr(struct sockaddr_dl *sdl, const void *addr, uint8_t addrlen)
187 1.2.4.2 ad {
188 1.2.4.2 ad uint_fast8_t endofs;
189 1.2.4.2 ad
190 1.2.4.2 ad endofs =
191 1.2.4.2 ad offsetof(struct sockaddr_dl, sdl_data[sdl->sdl_nlen + addrlen]);
192 1.2.4.2 ad
193 1.2.4.2 ad if (endofs > sizeof(struct sockaddr_dl)) {
194 1.2.4.2 ad printf("%s: too long: %" PRIu8 " bytes\n", __func__, addrlen);
195 1.2.4.2 ad sdl->sdl_alen = 0;
196 1.2.4.2 ad return NULL;
197 1.2.4.2 ad }
198 1.2.4.2 ad memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
199 1.2.4.2 ad sdl->sdl_alen = addrlen;
200 1.2.4.2 ad return sdl;
201 1.2.4.2 ad }
202