]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/ether_inet_pr_module.c
xnu-517.9.4.tar.gz
[apple/xnu.git] / bsd / net / ether_inet_pr_module.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1982, 1989, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
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
67 #include <net/if.h>
68 #include <net/netisr.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
74 #include <netinet/in.h>
75 #include <netinet/in_var.h>
76 #include <netinet/if_ether.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/ip.h>
79
80 #include <sys/socketvar.h>
81
82 #include <net/dlil.h>
83
84 #if LLC && CCITT
85 extern struct ifqueue pkintrq;
86 #endif
87
88
89 #if BRIDGE
90 #include <net/bridge.h>
91 #endif
92
93 /* #include "vlan.h" */
94 #if NVLAN > 0
95 #include <net/if_vlan_var.h>
96 #endif /* NVLAN > 0 */
97
98 static u_long lo_dlt = 0;
99 static ivedonethis = 0;
100 static u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
101
102 #define IFP2AC(IFP) ((struct arpcom *)IFP)
103
104
105
106 extern void * kdp_get_interface();
107
108 /*
109 * Process a received Ethernet packet;
110 * the packet is in the mbuf chain m without
111 * the ether header, which is provided separately.
112 */
113 int
114 inet_ether_input(m, frame_header, ifp, dl_tag, sync_ok)
115 struct mbuf *m;
116 char *frame_header;
117 struct ifnet *ifp;
118 u_long dl_tag;
119 int sync_ok;
120
121 {
122 register struct ether_header *eh = (struct ether_header *) frame_header;
123 register struct ifqueue *inq=0;
124 u_short ether_type;
125 int s;
126 u_int16_t ptype = -1;
127 unsigned char buf[18];
128
129 #if ISO || LLC || NETAT
130 register struct llc *l;
131 #endif
132
133 if ((ifp->if_flags & IFF_UP) == 0) {
134 m_freem(m);
135 return EJUSTRETURN;
136 }
137
138 ifp->if_lastchange = time;
139
140 if (eh->ether_dhost[0] & 1) {
141 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
142 sizeof(etherbroadcastaddr)) == 0)
143 m->m_flags |= M_BCAST;
144 else
145 m->m_flags |= M_MCAST;
146 }
147 if (m->m_flags & (M_BCAST|M_MCAST))
148 ifp->if_imcasts++;
149
150 ether_type = ntohs(eh->ether_type);
151
152 #if NVLAN > 0
153 if (ether_type == vlan_proto) {
154 if (vlan_input(eh, m) < 0)
155 ifp->if_data.ifi_noproto++;
156 return EJUSTRETURN;
157 }
158 #endif /* NVLAN > 0 */
159
160 switch (ether_type) {
161
162 case ETHERTYPE_IP:
163 if (ipflow_fastforward(m))
164 return EJUSTRETURN;
165 ptype = mtod(m, struct ip *)->ip_p;
166 if ((sync_ok == 0) ||
167 (ptype != IPPROTO_TCP && ptype != IPPROTO_UDP)) {
168 schednetisr(NETISR_IP);
169 }
170
171 inq = &ipintrq;
172 break;
173
174 case ETHERTYPE_ARP:
175 schednetisr(NETISR_ARP);
176 inq = &arpintrq;
177 break;
178
179 default: {
180 return ENOENT;
181 }
182 }
183
184 if (inq == 0)
185 return ENOENT;
186
187 s = splimp();
188 if (IF_QFULL(inq)) {
189 IF_DROP(inq);
190 m_freem(m);
191 splx(s);
192 return EJUSTRETURN;
193 } else
194 IF_ENQUEUE(inq, m);
195 splx(s);
196
197 if ((sync_ok) &&
198 (ptype == IPPROTO_TCP || ptype == IPPROTO_UDP)) {
199 extern void ipintr(void);
200
201 s = splnet();
202 ipintr();
203 splx(s);
204 }
205
206 return 0;
207 }
208
209
210
211
212 int
213 inet_ether_pre_output(ifp, m0, dst_netaddr, route, type, edst, dl_tag )
214 struct ifnet *ifp;
215 struct mbuf **m0;
216 struct sockaddr *dst_netaddr;
217 caddr_t route;
218 char *type;
219 char *edst;
220 u_long dl_tag;
221 {
222 struct rtentry *rt0 = (struct rtentry *) route;
223 int s;
224 register struct mbuf *m = *m0;
225 register struct rtentry *rt;
226 register struct ether_header *eh;
227 int off, len = m->m_pkthdr.len;
228 int hlen; /* link layer header length */
229 struct arpcom *ac = IFP2AC(ifp);
230
231
232
233 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
234 return ENETDOWN;
235
236 rt = rt0;
237 if (rt) {
238 if ((rt->rt_flags & RTF_UP) == 0) {
239 rt0 = rt = rtalloc1(dst_netaddr, 1, 0UL);
240 if (rt0)
241 rtunref(rt);
242 else
243 return EHOSTUNREACH;
244 }
245
246 if (rt->rt_flags & RTF_GATEWAY) {
247 if (rt->rt_gwroute == 0)
248 goto lookup;
249 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
250 rtfree(rt); rt = rt0;
251 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
252 0UL);
253 if ((rt = rt->rt_gwroute) == 0)
254 return (EHOSTUNREACH);
255 }
256 }
257
258
259 if (rt->rt_flags & RTF_REJECT)
260 if (rt->rt_rmx.rmx_expire == 0 ||
261 time_second < rt->rt_rmx.rmx_expire)
262 return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
263 }
264
265 hlen = ETHER_HDR_LEN;
266
267 /*
268 * Tell ether_frameout it's ok to loop packet unless negated below.
269 */
270 m->m_flags |= M_LOOP;
271
272 switch (dst_netaddr->sa_family) {
273
274 case AF_INET:
275 if (!arpresolve(ac, rt, m, dst_netaddr, edst, rt0))
276 return (EJUSTRETURN); /* if not yet resolved */
277 off = m->m_pkthdr.len - m->m_len;
278 *(u_short *)type = htons(ETHERTYPE_IP);
279 break;
280
281 case AF_UNSPEC:
282 m->m_flags &= ~M_LOOP;
283 eh = (struct ether_header *)dst_netaddr->sa_data;
284 (void)memcpy(edst, eh->ether_dhost, 6);
285 *(u_short *)type = eh->ether_type;
286 break;
287
288 default:
289 kprintf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
290 dst_netaddr->sa_family);
291
292 return EAFNOSUPPORT;
293 }
294
295 return (0);
296 }
297
298
299 int
300 ether_inet_prmod_ioctl(dl_tag, ifp, command, data)
301 u_long dl_tag;
302 struct ifnet *ifp;
303 int command;
304 caddr_t data;
305 {
306 struct ifaddr *ifa = (struct ifaddr *) data;
307 struct ifreq *ifr = (struct ifreq *) data;
308 struct rslvmulti_req *rsreq = (struct rslvmulti_req *) data;
309 int error = 0;
310 boolean_t funnel_state;
311 struct arpcom *ac = (struct arpcom *) ifp;
312 struct sockaddr_dl *sdl;
313 struct sockaddr_in *sin;
314 u_char *e_addr;
315
316
317 #if 0
318 /* No tneeded at soo_ioctlis already funnelled */
319 funnel_state = thread_funnel_set(network_flock,TRUE);
320 #endif
321
322 switch (command) {
323 case SIOCRSLVMULTI: {
324 switch(rsreq->sa->sa_family) {
325
326 case AF_INET:
327 sin = (struct sockaddr_in *)rsreq->sa;
328 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
329 return EADDRNOTAVAIL;
330 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
331 M_WAITOK);
332 sdl->sdl_len = sizeof *sdl;
333 sdl->sdl_family = AF_LINK;
334 sdl->sdl_index = ifp->if_index;
335 sdl->sdl_type = IFT_ETHER;
336 sdl->sdl_nlen = 0;
337 sdl->sdl_alen = ETHER_ADDR_LEN;
338 sdl->sdl_slen = 0;
339 e_addr = LLADDR(sdl);
340 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
341 *rsreq->llsa = (struct sockaddr *)sdl;
342 return EJUSTRETURN;
343
344 default:
345 /*
346 * Well, the text isn't quite right, but it's the name
347 * that counts...
348 */
349 return EAFNOSUPPORT;
350 }
351
352 }
353 case SIOCSIFADDR:
354 if ((ifp->if_flags & IFF_RUNNING) == 0) {
355 ifp->if_flags |= IFF_UP;
356 dlil_ioctl(0, ifp, SIOCSIFFLAGS, (caddr_t) 0);
357 }
358
359 switch (ifa->ifa_addr->sa_family) {
360
361 case AF_INET:
362
363 if (ifp->if_init)
364 ifp->if_init(ifp->if_softc); /* before arpwhohas */
365
366 arp_ifinit(IFP2AC(ifp), ifa);
367 /*
368 * Register new IP and MAC addresses with the kernel debugger
369 * if the interface is the same as was registered by IOKernelDebugger. If
370 * no interface was registered, fall back and just match against en0 interface.
371 */
372 if ((kdp_get_interface() != 0 && kdp_get_interface() == ifp->if_private)
373 || (kdp_get_interface() == 0 && ifp->if_unit == 0))
374 kdp_set_ip_and_mac_addresses(&(IA_SIN(ifa)->sin_addr), &(IFP2AC(ifp)->ac_enaddr));
375
376 break;
377
378 default:
379 break;
380 }
381
382 break;
383
384 case SIOCGIFADDR:
385 {
386 struct sockaddr *sa;
387
388 sa = (struct sockaddr *) & ifr->ifr_data;
389 bcopy(IFP2AC(ifp)->ac_enaddr,
390 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
391 }
392 break;
393
394 case SIOCSIFMTU:
395 /*
396 * IOKit IONetworkFamily will set the right MTU according to the driver
397 */
398
399 return (0);
400
401 default:
402 return EOPNOTSUPP;
403 }
404
405 //(void) thread_funnel_set(network_flock, FALSE);
406
407 return (error);
408 }
409
410
411
412
413
414 int
415 ether_attach_inet(struct ifnet *ifp, u_long *dl_tag)
416 {
417 struct dlil_proto_reg_str reg;
418 struct dlil_demux_desc desc;
419 struct dlil_demux_desc desc2;
420 u_short en_native=ETHERTYPE_IP;
421 u_short arp_native=ETHERTYPE_ARP;
422 int stat;
423 int i;
424
425
426 stat = dlil_find_dltag(ifp->if_family, ifp->if_unit, PF_INET, dl_tag);
427 if (stat == 0)
428 return (stat);
429
430 TAILQ_INIT(&reg.demux_desc_head);
431 desc.type = DLIL_DESC_RAW;
432 desc.variants.bitmask.proto_id_length = 0;
433 desc.variants.bitmask.proto_id = 0;
434 desc.variants.bitmask.proto_id_mask = 0;
435 desc.native_type = (char *) &en_native;
436 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc, next);
437 reg.interface_family = ifp->if_family;
438 reg.unit_number = ifp->if_unit;
439 reg.input = inet_ether_input;
440 reg.pre_output = inet_ether_pre_output;
441 reg.event = 0;
442 reg.offer = 0;
443 reg.ioctl = ether_inet_prmod_ioctl;
444 reg.default_proto = 1;
445 reg.protocol_family = PF_INET;
446
447 desc2 = desc;
448 desc2.native_type = (char *) &arp_native;
449 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc2, next);
450
451 stat = dlil_attach_protocol(&reg, dl_tag);
452 if (stat) {
453 printf("WARNING: ether_attach_inet can't attach ip to interface\n");
454 return stat;
455 }
456 return (0);
457 }
458
459 int ether_detach_inet(struct ifnet *ifp, u_long dl_tag)
460 {
461 int stat;
462
463 stat = dlil_find_dltag(ifp->if_family, ifp->if_unit, PF_INET, &dl_tag);
464 if (stat == 0) {
465 stat = dlil_detach_protocol(dl_tag);
466 if (stat) {
467 printf("WARNING: ether_detach_inet can't detach ip from interface\n");
468 }
469 }
470 return stat;
471 }
472