]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/ether_at_pr_module.c
xnu-123.5.tar.gz
[apple/xnu.git] / bsd / net / ether_at_pr_module.c
CommitLineData
1c79356b
A
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#include <netinet/if_ether.h>
75
76#include <sys/socketvar.h>
77
78#include <net/dlil.h>
79#include <netat/at_pat.h>
80#if NETAT
81extern struct ifqueue atalkintrq;
82#endif
83
84
85#if BRIDGE
86#include <net/bridge.h>
87#endif
88
89/* #include "vlan.h" */
90#if NVLAN > 0
91#include <net/if_vlan_var.h>
92#endif /* NVLAN > 0 */
93
94static
95u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
96
97#define IFP2AC(IFP) ((struct arpcom *)IFP)
98
99
100struct dl_es_at_entry
101{
102 struct ifnet *ifp;
103 u_long dl_tag;
104 int ref_count;
105};
106
107
108/*
109 * Temp static for protocol registration XXX
110 */
111
112#define MAX_EN_COUNT 30
113
114static struct dl_es_at_entry en_at_array[MAX_EN_COUNT];
115
116/*
117 * Process a received Ethernet packet;
118 * the packet is in the mbuf chain m without
119 * the ether header, which is provided separately.
120 */
121int
122at_ether_input(m, frame_header, ifp, dl_tag, sync_ok)
123 struct mbuf *m;
124 char *frame_header;
125 struct ifnet *ifp;
126 u_long dl_tag;
127 int sync_ok;
128
129{
130 register struct ether_header *eh = (struct ether_header *) frame_header;
131 register struct ifqueue *inq=0;
132 u_short ether_type;
133 int s;
134 u_int16_t ptype = -1;
135 unsigned char buf[18];
136
137#if NETAT
138 register struct llc *l;
139#endif
140
141 if ((ifp->if_flags & IFF_UP) == 0) {
142 m_freem(m);
143 return EJUSTRETURN;
144 }
145
146 ifp->if_lastchange = time;
147
148 if (eh->ether_dhost[0] & 1) {
149 if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
150 sizeof(etherbroadcastaddr)) == 0)
151 m->m_flags |= M_BCAST;
152 else
153 m->m_flags |= M_MCAST;
154 }
155 if (m->m_flags & (M_BCAST|M_MCAST))
156 ifp->if_imcasts++;
157
158 ether_type = ntohs(eh->ether_type);
159
160#if NVLAN > 0
161 if (ether_type == vlan_proto) {
162 if (vlan_input(eh, m) < 0)
163 ifp->if_data.ifi_noproto++;
164 return EJUSTRETURN;
165 }
166#endif /* NVLAN > 0 */
167
168 if (ether_type > ETHERMTU)
169 return ENOENT;
170
171#if NETAT
172 l = mtod(m, struct llc *);
173
174 switch (l->llc_dsap) {
175 case LLC_SNAP_LSAP:
176
177 /* Temporary hack: check for AppleTalk and AARP packets */
178 /* WARNING we're checking only on the "ether_type" (the 2 bytes
179 * of the SNAP header. This shouldn't be a big deal,
180 * AppleTalk pat_input is making sure we have the right packets
181 * because it needs to discrimante AARP from EtherTalk packets.
182 */
183
184 if (l->llc_ssap == LLC_SNAP_LSAP &&
185 l->llc_un.type_snap.control == 0x03) {
186
187#ifdef APPLETALK_DEBUG
188 printf("new_ether_input: SNAP Cntrol type=0x%x Src=%s\n",
189 l->llc_un.type_snap.ether_type,
190 ether_sprintf(buf, &eh->ether_shost));
191 printf(" Dst=%s\n",
192 ether_sprintf(buf, &eh->ether_dhost));
193#endif /* APPLETALK_DEBUG */
194
195 if ((l->llc_un.type_snap.ether_type == 0x809B) ||
196 (l->llc_un.type_snap.ether_type == 0x80F3)) {
197
198
199 /*
200 * note: for AppleTalk we need to pass the enet header of the
201 * packet up stack. To do so, we made sure in that the FULL packet
202 * is copied in the mbuf by the mace driver, and only the m_data and
203 * length have been shifted to make IP and the other guys happy.
204 */
205
206 m->m_data -= sizeof(*eh);
207 m->m_len += sizeof(*eh);
208 m->m_pkthdr.len += sizeof(*eh);
209#ifdef APPLETALK_DEBUG
210 l == (struct llc *)(eh+1);
211 if (l->llc_un.type_snap.ether_type == 0x80F3) {
212 kprintf("new_ether_input: RCV AppleTalk type=0x%x Src=%s\n",
213 l->llc_un.type_snap.ether_type,
214 ether_sprintf(buf, &eh->ether_shost));
215 kprintf(" Dst=%s\n",
216 ether_sprintf(buf, &eh->ether_dhost));
217 }
218#endif /* APPLETALK_DEBUG */
219 schednetisr(NETISR_APPLETALK);
220 inq = &atalkintrq ;
221
222 break;
223 }
224 }
225
226 break;
227
228
229 default:
230 return ENOENT;
231 }
232
233
234 if (inq == 0)
235 return ENOENT;
236
237 s = splimp();
238 if (IF_QFULL(inq)) {
239 IF_DROP(inq);
240 m_freem(m);
241 splx(s);
242 return EJUSTRETURN;
243 } else
244 IF_ENQUEUE(inq, m);
245 splx(s);
246 return 0;
247#else
248 return ENOENT;
249#endif /* NETAT */
250}
251
252
253
254int
255ether_pre_output(ifp, m0, dst_netaddr, route, type, edst, dl_tag )
256 struct ifnet *ifp;
257 struct mbuf **m0;
258 struct sockaddr *dst_netaddr;
259 caddr_t route;
260 char *type;
261 char *edst;
262 u_long dl_tag;
263{
264 int s;
265 register struct mbuf *m = *m0;
266 register struct rtentry *rt;
267 register struct ether_header *eh;
268 int off, len = m->m_pkthdr.len;
269 int hlen; /* link layer header lenght */
270 struct arpcom *ac = IFP2AC(ifp);
271
272
273
274 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
275 return ENETDOWN;
276
277 hlen = ETHER_HDR_LEN;
278
279 /*
280 * Tell ether_frameout it's ok to loop packet unless negated below.
281 */
282 m->m_flags |= M_LOOP;
283
284 switch (dst_netaddr->sa_family) {
285 case AF_UNSPEC:
286 m->m_flags &= ~M_LOOP;
287 eh = (struct ether_header *)dst_netaddr->sa_data;
288 (void)memcpy(edst, eh->ether_dhost, 6);
289 *(u_short *)type = eh->ether_type;
290 break;
291
292
293 case AF_APPLETALK:
294 {
295 eh = (struct ether_header *)dst_netaddr->sa_data;
296 bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, 6);
297
298 *(u_short *)type = m->m_pkthdr.len;
299 }
300 break;
301
302
303 default:
304 kprintf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
305 dst_netaddr->sa_family);
306
307 return EAFNOSUPPORT;
308 }
309
310 return (0);
311}
312
313
314
315
316
317int
318ether_prmod_ioctl(dl_tag, ifp, command, data)
319 u_long dl_tag;
320 struct ifnet *ifp;
321 int command;
322 caddr_t data;
323{
324 struct ifaddr *ifa = (struct ifaddr *) data;
325 struct ifreq *ifr = (struct ifreq *) data;
326 int error = 0;
327 boolean_t funnel_state;
328 struct arpcom *ac = (struct arpcom *) ifp;
329 struct sockaddr_dl *sdl;
330 struct sockaddr_in *sin;
331 u_char *e_addr;
332
333
334 funnel_state = thread_funnel_set(network_flock, TRUE);
335
336 switch (command) {
337
338 case SIOCSIFADDR:
339 if ((ifp->if_flags & IFF_RUNNING) == 0) {
340 ifp->if_flags |= IFF_UP;
341 dlil_ioctl(0, ifp, SIOCSIFFLAGS, (caddr_t) 0);
342 }
343
344 break;
345
346 case SIOCGIFADDR:
347 {
348 struct sockaddr *sa;
349
350 sa = (struct sockaddr *) & ifr->ifr_data;
351 bcopy(IFP2AC(ifp)->ac_enaddr,
352 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
353 }
354 break;
355
356 case SIOCSIFMTU:
357 /*
358 * Set the interface MTU.
359 */
360 if (ifr->ifr_mtu > ETHERMTU) {
361 error = EINVAL;
362 } else {
363 ifp->if_mtu = ifr->ifr_mtu;
364 }
365 break;
366
367 default:
368 return EOPNOTSUPP;
369 }
370
371 (void) thread_funnel_set(network_flock, funnel_state);
372
373 return (error);
374}
375
376
377
378void
379ether_attach_at(struct ifnet *ifp, u_long *at_dl_tag, u_long *aarp_dl_tag)
380{
381 struct dlil_proto_reg_str reg;
382 struct dlil_demux_desc desc;
383 struct dlil_demux_desc desc2;
384 u_short native = 0; /* 802.2 frames use a length here */
385 int stat;
386 int first_empty;
387 int i;
388
389
390 first_empty = MAX_EN_COUNT;
391 for (i=0; i < MAX_EN_COUNT; i++) {
392 if (en_at_array[i].ifp == 0)
393 first_empty = i;
394
395 if (en_at_array[i].ifp == ifp) {
396 en_at_array[i].ref_count++;
397 *at_dl_tag = *aarp_dl_tag = en_at_array[i].dl_tag;
398 return;
399 }
400 }
401
402 if (first_empty == MAX_EN_COUNT)
403 return;
404
405 TAILQ_INIT(&reg.demux_desc_head);
406 desc.type = DLIL_DESC_802_2_SNAP;
407 desc.variants.desc_802_2_SNAP.dsap = LLC_SNAP_LSAP;
408 desc.variants.desc_802_2_SNAP.ssap = LLC_SNAP_LSAP;
409 desc.variants.desc_802_2_SNAP.control_code = 0x03;
410 desc.variants.desc_802_2_SNAP.org[0] = 0x08;
411 desc.variants.desc_802_2_SNAP.org[1] = 0x00;
412 desc.variants.desc_802_2_SNAP.org[2] = 0x07;
413 desc.variants.desc_802_2_SNAP.protocol_type = 0x809B;
414 desc.native_type = (char *) &native;
415 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc, next);
416 reg.interface_family = ifp->if_family;
417 reg.unit_number = ifp->if_unit;
418 reg.input = at_ether_input;
419 reg.pre_output = ether_pre_output;
420 reg.event = 0;
421 reg.offer = 0;
422 reg.ioctl = ether_prmod_ioctl;
423 reg.default_proto = 0;
424 reg.protocol_family = PF_APPLETALK;
425
426 desc2 = desc;
427 desc2.variants.desc_802_2_SNAP.protocol_type = 0x80F3;
428 desc2.variants.desc_802_2_SNAP.org[0] = 0;
429 desc2.variants.desc_802_2_SNAP.org[1] = 0;
430 desc2.variants.desc_802_2_SNAP.org[2] = 0;
431
432 TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc2, next);
433
434 stat = dlil_attach_protocol(&reg, at_dl_tag);
435 if (stat) {
436 printf("WARNING: ether_attach_at can't attach at to interface\n");
437 return;
438 }
439
440 *aarp_dl_tag = *at_dl_tag;
441
442 en_at_array[first_empty].ifp = ifp;
443 en_at_array[first_empty].dl_tag = *at_dl_tag;
444 en_at_array[first_empty].ref_count = 1;
445
446} /* ether_attach_at */
447
448
449void
450ether_detach_at(struct ifnet *ifp)
451{
452 int i;
453
454 for (i=0; i < MAX_EN_COUNT; i++) {
455 if (en_at_array[i].ifp == ifp)
456 break;
457 }
458
459 if (i < MAX_EN_COUNT) {
460 if (en_at_array[i].ref_count > 1)
461 en_at_array[i].ref_count--;
462 else {
463 if (en_at_array[i].ref_count == 1) {
464 dlil_detach_protocol(en_at_array[i].dl_tag);
465 en_at_array[i].ifp = 0;
466 }
467 }
468 }
469}