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#include <sys/param.h>
59#include <sys/systm.h>
60#include <sys/kernel.h>
61#include <sys/malloc.h>
62#include <sys/mbuf.h>
63#include <sys/socket.h>
64#include <sys/sockio.h>
65#include <sys/sysctl.h>
66#include <kern/lock.h>
67
68#include <net/if.h>
69#include <net/route.h>
70#include <net/if_llc.h>
71#include <net/if_dl.h>
72#include <net/if_types.h>
73#include <net/ndrv.h>
74#include <net/kpi_protocol.h>
75
76#include <netinet/in.h>
77#include <netinet/in_var.h>
78#include <netinet/if_ether.h>
79#include <netinet/in_systm.h>
80#include <netinet/ip.h>
81
82#if INET6
83#include <netinet6/nd6.h>
84#include <netinet6/in6_ifattach.h>
85#endif
86
87
88
89#include <sys/socketvar.h>
90
91#include <net/dlil.h>
92
93
94#if LLC && CCITT
95extern struct ifqueue pkintrq;
96#endif
97
98
99#if BRIDGE
100#include <net/bridge.h>
101#endif
102
103
104#if NVLAN > 0
105#include <net/if_vlan_var.h>
106#endif
107
108
109int ether_attach_inet6(struct ifnet *ifp, u_long protocol_family);
110int ether_detach_inet6(struct ifnet *ifp, u_long protocol_family);
111
112
113
114
115
116
117static errno_t
118inet6_ether_input(
119 __unused ifnet_t ifp,
120 protocol_family_t protocol,
121 mbuf_t packet,
122 __unused char *header)
123{
124 proto_input(protocol, packet);
125 return 0;
126}
127
128static errno_t
129inet6_ether_pre_output(
130 ifnet_t ifp,
131 __unused protocol_family_t protocol_family,
132 mbuf_t *m0,
133 const struct sockaddr *dst_netaddr,
134 void *route,
135 char *type,
136 char *edst)
137{
138 errno_t result;
139 struct sockaddr_dl sdl;
140 register struct mbuf *m = *m0;
141
142
143
144
145 m->m_flags |= M_LOOP;
146
147 result = nd6_lookup_ipv6(ifp, (const struct sockaddr_in6*)dst_netaddr,
148 &sdl, sizeof(sdl), route, *m0);
149
150 if (result == 0) {
151 *(u_int16_t*)type = htons(ETHERTYPE_IPV6);
152 bcopy(LLADDR(&sdl), edst, sdl.sdl_alen);
153 }
154
155
156
157 return result;
158}
159
160static int
161ether_inet6_resolve_multi(
162 ifnet_t ifp,
163 const struct sockaddr *proto_addr,
164 struct sockaddr_dl *out_ll,
165 size_t ll_len)
166{
167 static const size_t minsize = offsetof(struct sockaddr_dl, sdl_data[0]) + ETHER_ADDR_LEN;
168 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6*)proto_addr;
169
170 if (proto_addr->sa_family != AF_INET6)
171 return EAFNOSUPPORT;
172
173 if (proto_addr->sa_len < sizeof(struct sockaddr_in6))
174 return EINVAL;
175
176 if (ll_len < minsize)
177 return EMSGSIZE;
178
179 bzero(out_ll, minsize);
180 out_ll->sdl_len = minsize;
181 out_ll->sdl_family = AF_LINK;
182 out_ll->sdl_index = ifp->if_index;
183 out_ll->sdl_type = IFT_ETHER;
184 out_ll->sdl_nlen = 0;
185 out_ll->sdl_alen = ETHER_ADDR_LEN;
186 out_ll->sdl_slen = 0;
187 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, LLADDR(out_ll));
188
189 return 0;
190}
191
192
193static errno_t
194ether_inet6_prmod_ioctl(
195 ifnet_t ifp,
196 __unused protocol_family_t protocol_family,
197 u_int32_t command,
198 void* data)
199{
200 struct ifreq *ifr = (struct ifreq *) data;
201 int error = 0;
202
203 switch (command) {
204 case SIOCSIFADDR:
205 if ((ifp->if_flags & IFF_RUNNING) == 0) {
206 ifnet_set_flags(ifp, IFF_UP, IFF_UP);
207 dlil_ioctl(0, ifp, SIOCSIFFLAGS, (caddr_t) 0);
208 }
209
210 break;
211
212 case SIOCGIFADDR:
213 ifnet_lladdr_copy_bytes(ifp, ifr->ifr_addr.sa_data, ETHER_ADDR_LEN);
214 break;
215
216 case SIOCSIFMTU:
217
218
219
220
221 return (0);
222
223 default:
224 return EOPNOTSUPP;
225 }
226
227 return (error);
228}
229
230int
231ether_attach_inet6(
232 struct ifnet *ifp,
233 __unused u_long protocol_family)
234{
235 struct ifnet_attach_proto_param proto;
236 struct ifnet_demux_desc demux[1];
237 u_short en_6native=htons(ETHERTYPE_IPV6);
238 errno_t error;
239
240 bzero(&proto, sizeof(proto));
241 demux[0].type = DLIL_DESC_ETYPE2;
242 demux[0].data = &en_6native;
243 demux[0].datalen = sizeof(en_6native);
244 proto.demux_list = demux;
245 proto.demux_count = 1;
246 proto.input = inet6_ether_input;
247 proto.pre_output = inet6_ether_pre_output;
248 proto.ioctl = ether_inet6_prmod_ioctl;
249 proto.resolve = ether_inet6_resolve_multi;
250 error = ifnet_attach_protocol(ifp, protocol_family, &proto);
251 if (error && error != EEXIST) {
252 printf("WARNING: ether_attach_inet6 can't attach ipv6 to %s%d\n",
253 ifp->if_name, ifp->if_unit);
254 }
255
256 return error;
257}
258
259int
260ether_detach_inet6(
261 struct ifnet *ifp,
262 u_long protocol_family)
263{
264 errno_t error;
265
266 error = ifnet_detach_protocol(ifp, protocol_family);
267 if (error && error != ENOENT) {
268 printf("WARNING: ether_detach_inet6 can't detach ipv6 from %s%d\n",
269 ifp->if_name, ifp->if_unit);
270 }
271
272 return error;
273}
274
275