]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/ether_inet_pr_module.c
dd029a0fa822fcd680c196124c514e02d5e45079
[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 #include <net/ndrv.h>
74
75 #include <netinet/in.h>
76 #include <netinet/in_var.h>
77 #include <netinet/if_ether.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
80
81 #include <sys/socketvar.h>
82
83 #include <net/dlil.h>
84
85 #if LLC && CCITT
86 extern struct ifqueue pkintrq;
87 #endif
88
89
90 #if BRIDGE
91 #include <net/bridge.h>
92 #endif
93
94 /* #include "vlan.h" */
95 #if NVLAN > 0
96 #include <net/if_vlan_var.h>
97 #endif /* NVLAN > 0 */
98
99 static u_long lo_dlt = 0;
100 static ivedonethis = 0;
101 static u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
102
103 #define IFP2AC(IFP) ((struct arpcom *)IFP)
104
105
106
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 lenght */
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 rt->rt_refcnt--;
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 //
367 // See if another station has *our* IP address.
368 // i.e.: There is an address conflict! If a
369 // conflict exists, a message is sent to the
370 // console.
371 //
372 if (IA_SIN(ifa)->sin_addr.s_addr != 0)
373 {
374 /* don't bother for 0.0.0.0 */
375 ac->ac_ipaddr = IA_SIN(ifa)->sin_addr;
376 arpwhohas(ac, &IA_SIN(ifa)->sin_addr);
377 }
378
379 arp_ifinit(IFP2AC(ifp), ifa);
380
381 break;
382
383 default:
384 break;
385 }
386
387 break;
388
389 case SIOCGIFADDR:
390 {
391 struct sockaddr *sa;
392
393 sa = (struct sockaddr *) & ifr->ifr_data;
394 bcopy(IFP2AC(ifp)->ac_enaddr,
395 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
396 }
397 break;
398
399 case SIOCSIFMTU:
400 /*
401 * Set the interface MTU.
402 */
403 if (ifr->ifr_mtu > ETHERMTU) {
404 error = EINVAL;
405 } else {
406 ifp->if_mtu = ifr->ifr_mtu;
407 }
408 break;
409
410 default:
411 return EOPNOTSUPP;
412 }
413
414 //(void) thread_funnel_set(network_flock, FALSE);
415
416 return (error);
417 }
418
419
420
421
422
423 u_long
424 ether_attach_inet(struct ifnet *ifp)
425 {
426 struct dlil_proto_reg_str reg;
427 struct dlil_demux_desc desc;
428 struct dlil_demux_desc desc2;
429 u_long ip_dl_tag=0;
430 u_short en_native=ETHERTYPE_IP;
431 u_short arp_native=ETHERTYPE_ARP;
432 int stat;
433 int i;
434
435
436 stat = dlil_find_dltag(ifp->if_family, ifp->if_unit, PF_INET, &ip_dl_tag);
437 if (stat == 0)
438 return ip_dl_tag;
439
440 TAILQ_INIT(&reg.demux_desc_head);
441 desc.type = DLIL_DESC_RAW;
442 desc.variants.bitmask.proto_id_length = 0;
443 desc.variants.bitmask.proto_id = 0;
444 desc.variants.bitmask.proto_id_mask = 0;
445 desc.native_type = (char *) &en_native;
446 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc, next);
447 reg.interface_family = ifp->if_family;
448 reg.unit_number = ifp->if_unit;
449 reg.input = inet_ether_input;
450 reg.pre_output = inet_ether_pre_output;
451 reg.event = 0;
452 reg.offer = 0;
453 reg.ioctl = ether_inet_prmod_ioctl;
454 reg.default_proto = 1;
455 reg.protocol_family = PF_INET;
456
457 desc2 = desc;
458 desc2.native_type = (char *) &arp_native;
459 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc2, next);
460
461 stat = dlil_attach_protocol(&reg, &ip_dl_tag);
462 if (stat) {
463 printf("WARNING: ether_attach_inet can't attach ip to interface\n");
464 return stat;
465 }
466
467 return ip_dl_tag;
468 }