Separate out the length checks from IP dispatch and also do them along
[dragonfly.git] / sys / net / if_fddisubr.c
1 /*
2  * Copyright (c) 1995, 1996
3  *      Matt Thomas <matt@3am-software.com>.  All rights reserved.
4  * Copyright (c) 1982, 1989, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *      from: if_ethersubr.c,v 1.5 1994/12/13 22:31:45 wollman Exp
36  * $FreeBSD: src/sys/net/if_fddisubr.c,v 1.41.2.8 2002/02/20 23:34:09 fjoe Exp $
37  * $DragonFly: src/sys/net/Attic/if_fddisubr.c,v 1.11 2004/07/23 07:16:30 joerg Exp $
38  */
39
40 #include "opt_atalk.h"
41 #include "opt_inet.h"
42 #include "opt_inet6.h"
43 #include "opt_ipx.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49 #include <sys/malloc.h>
50
51 #include <net/if.h>
52 #include <net/bpf.h>
53 #include <net/netisr.h>
54 #include <net/route.h>
55 #include <net/if_llc.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58
59 #if defined(INET) || defined(INET6)
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <netinet/if_ether.h>
63 #include <net/ethernet.h>
64 #endif
65 #ifdef INET6
66 #include <netinet6/nd6.h>
67 #endif
68 #if defined(__DragonFly__) || defined(__FreeBSD__)
69 #include <netinet/if_fddi.h>
70 #else
71 #include <net/if_fddi.h>
72 #endif
73
74 #ifdef IPX
75 #include <netproto/ipx/ipx.h> 
76 #include <netproto/ipx/ipx_if.h>
77 #endif
78
79 #ifdef NS
80 #include <netns/ns.h>
81 #include <netns/ns_if.h>
82 #endif
83
84 #ifdef DECNET
85 #include <netdnet/dn.h>
86 #endif
87
88 #ifdef NETATALK
89 #include <netproto/atalk/at.h>
90 #include <netproto/atalk/at_var.h>
91 #include <netproto/atalk/at_extern.h>
92
93 #define llc_snap_org_code llc_un.type_snap.org_code
94 #define llc_snap_ether_type llc_un.type_snap.ether_type
95
96 extern u_char   at_org_code[ 3 ];
97 extern u_char   aarp_org_code[ 3 ];
98 #endif /* NETATALK */
99
100 static  int fddi_resolvemulti (struct ifnet *, struct sockaddr **,
101                                    struct sockaddr *);
102 static void     fddi_input(struct ifnet *, struct mbuf *);
103 static int      fddi_output(struct ifnet *, struct mbuf *, struct sockaddr *,
104                             struct rtentry *);
105
106 #define senderr(e) { error = (e); goto bad;}
107
108 /*
109  * This really should be defined in if_llc.h but in case it isn't.
110  */
111 #ifndef llc_snap
112 #define llc_snap        llc_un.type_snap
113 #endif
114
115 #if defined(__bsdi__) || defined(__NetBSD__)
116 #define RTALLOC1(a, b)                  rtalloc1(a, b)
117 #define ARPRESOLVE(a, b, c, d, e, f)    arpresolve(a, b, c, d, e)
118 #elif defined(__DragonFly__) || defined(__FreeBSD__)
119 #define RTALLOC1(a, b)                  rtalloc1(a, b, 0UL)
120 #define ARPRESOLVE(a, b, c, d, e, f)    arpresolve(a, b, c, d, e, f)
121 #endif
122 /*
123  * FDDI output routine.
124  * Encapsulate a packet of type family for the local net.
125  * Use trailer local net encapsulation if enough data in first
126  * packet leaves a multiple of 512 bytes of data in remainder.
127  * Assumes that ifp is actually pointer to arpcom structure.
128  */
129 static int
130 fddi_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
131             struct rtentry *rt0)
132 {
133         u_int16_t type;
134         int s, loop_copy = 0, error = 0, hdrcmplt = 0;
135         u_char esrc[6], edst[6];
136         struct rtentry *rt;
137         struct fddi_header *fh;
138         struct arpcom *ac = (struct arpcom *)ifp;
139
140         if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
141                 senderr(ENETDOWN);
142         getmicrotime(&ifp->if_lastchange);
143 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
144         if ((rt = rt0) != NULL) {
145                 if ((rt->rt_flags & RTF_UP) == 0) {
146                         if ((rt0 = rt = RTALLOC1(dst, 1)) != NULL)
147                                 rt->rt_refcnt--;
148                         else 
149                                 senderr(EHOSTUNREACH);
150                 }
151                 if (rt->rt_flags & RTF_GATEWAY) {
152                         if (rt->rt_gwroute == 0)
153                                 goto lookup;
154                         if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
155                                 rtfree(rt); rt = rt0;
156                         lookup: rt->rt_gwroute = RTALLOC1(rt->rt_gateway, 1);
157                                 if ((rt = rt->rt_gwroute) == 0)
158                                         senderr(EHOSTUNREACH);
159                         }
160                 }
161                 if (rt->rt_flags & RTF_REJECT)
162                         if (rt->rt_rmx.rmx_expire == 0 ||
163                             time_second < rt->rt_rmx.rmx_expire)
164                                 senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
165         }
166 #endif
167         switch (dst->sa_family) {
168
169 #ifdef INET
170         case AF_INET: {
171 #if !defined(__bsdi__) || _BSDI_VERSION >= 199401
172                 if (!ARPRESOLVE(ifp, rt, m, dst, edst, rt0))
173                         return (0);     /* if not yet resolved */
174 #else
175                 int usetrailers;
176                 if (!arpresolve(ac, m, &((struct sockaddr_in *)dst)->sin_addr, edst, &usetrailers))
177                         return (0);     /* if not yet resolved */
178 #endif
179                 type = htons(ETHERTYPE_IP);
180                 break;
181         }
182 #endif
183 #ifdef INET6
184         case AF_INET6:
185                 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
186                         /* Something bad happened */
187                         return(0);
188                 }
189                 type = htons(ETHERTYPE_IPV6);
190                 break;
191 #endif
192 #ifdef IPX
193         case AF_IPX:
194                 type = htons(ETHERTYPE_IPX);
195                 bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
196                     (caddr_t)edst, sizeof (edst));
197                 break;
198 #endif
199 #ifdef NETATALK
200         case AF_APPLETALK: {
201             struct at_ifaddr *aa;
202             if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
203                 return (0);
204             /*
205              * ifaddr is the first thing in at_ifaddr
206              */
207             if ((aa = at_ifawithnet( (struct sockaddr_at *)dst)) == 0)
208                 goto bad;
209             
210             /*
211              * In the phase 2 case, we need to prepend an mbuf for the llc header.
212              * Since we must preserve the value of m, which is passed to us by
213              * value, we m_copy() the first mbuf, and use it for our llc header.
214              */
215             if (aa->aa_flags & AFA_PHASE2) {
216                 struct llc llc;
217
218                 M_PREPEND(m, sizeof(struct llc), MB_WAIT);
219                 if (m == 0)
220                         senderr(ENOBUFS);
221                 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
222                 llc.llc_control = LLC_UI;
223                 bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
224                 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
225                 bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
226                 type = 0;
227             } else {
228                 type = htons(ETHERTYPE_AT);
229             }
230             break;
231         }
232 #endif /* NETATALK */
233 #ifdef NS
234         case AF_NS:
235                 type = htons(ETHERTYPE_NS);
236                 bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
237                     (caddr_t)edst, sizeof (edst));
238                 break;
239 #endif
240
241         case pseudo_AF_HDRCMPLT:
242         {
243                 struct ether_header *eh;
244                 hdrcmplt = 1;
245                 eh = (struct ether_header *)dst->sa_data;
246                 (void)memcpy((caddr_t)esrc, (caddr_t)eh->ether_shost, sizeof (esrc));
247                 /* FALLTHROUGH */
248         }
249
250         case AF_UNSPEC:
251         {
252                 struct ether_header *eh;
253                 loop_copy = -1;
254                 eh = (struct ether_header *)dst->sa_data;
255                 (void)memcpy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
256                 if (*edst & 1)
257                         m->m_flags |= (M_BCAST|M_MCAST);
258                 type = eh->ether_type;
259                 break;
260         }
261
262         case AF_IMPLINK:
263         {
264                 fh = mtod(m, struct fddi_header *);
265                 error = EPROTONOSUPPORT;
266                 switch (fh->fddi_fc & (FDDIFC_C|FDDIFC_L|FDDIFC_F)) {
267                         case FDDIFC_LLC_ASYNC: {
268                                 /* legal priorities are 0 through 7 */
269                                 if ((fh->fddi_fc & FDDIFC_Z) > 7)
270                                         goto bad;
271                                 break;
272                         }
273                         case FDDIFC_LLC_SYNC: {
274                                 /* FDDIFC_Z bits reserved, must be zero */
275                                 if (fh->fddi_fc & FDDIFC_Z)
276                                         goto bad;
277                                 break;
278                         }
279                         case FDDIFC_SMT: {
280                                 /* FDDIFC_Z bits must be non zero */
281                                 if ((fh->fddi_fc & FDDIFC_Z) == 0)
282                                         goto bad;
283                                 break;
284                         }
285                         default: {
286                                 /* anything else is too dangerous */
287                                 goto bad;
288                         }
289                 }
290                 error = 0;
291                 if (fh->fddi_dhost[0] & 1)
292                         m->m_flags |= (M_BCAST|M_MCAST);
293                 goto queue_it;
294         }
295         default:
296                 printf("%s: can't handle af%d\n", ifp->if_xname,
297                         dst->sa_family);
298                 senderr(EAFNOSUPPORT);
299         }
300
301         if (type != 0) {
302                 struct llc *l;
303                 M_PREPEND(m, sizeof (struct llc), MB_DONTWAIT);
304                 if (m == 0)
305                         senderr(ENOBUFS);
306                 l = mtod(m, struct llc *);
307                 l->llc_control = LLC_UI;
308                 l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
309                 l->llc_snap.org_code[0] = l->llc_snap.org_code[1] = l->llc_snap.org_code[2] = 0;
310                 (void)memcpy((caddr_t) &l->llc_snap.ether_type, (caddr_t) &type,
311                         sizeof(u_int16_t));
312         }
313
314         /*
315          * Add local net header.  If no space in first mbuf,
316          * allocate another.
317          */
318         M_PREPEND(m, sizeof (struct fddi_header), MB_DONTWAIT);
319         if (m == 0)
320                 senderr(ENOBUFS);
321         fh = mtod(m, struct fddi_header *);
322         fh->fddi_fc = FDDIFC_LLC_ASYNC|FDDIFC_LLC_PRIO4;
323         (void)memcpy((caddr_t)fh->fddi_dhost, (caddr_t)edst, sizeof (edst));
324   queue_it:
325         if (hdrcmplt)
326                 (void)memcpy((caddr_t)fh->fddi_shost, (caddr_t)esrc,
327                         sizeof(fh->fddi_shost));
328         else
329                 (void)memcpy((caddr_t)fh->fddi_shost, (caddr_t)ac->ac_enaddr,
330                         sizeof(fh->fddi_shost));
331         /*
332          * If a simplex interface, and the packet is being sent to our
333          * Ethernet address or a broadcast address, loopback a copy.
334          * XXX To make a simplex device behave exactly like a duplex
335          * device, we should copy in the case of sending to our own
336          * ethernet address (thus letting the original actually appear
337          * on the wire). However, we don't do that here for security
338          * reasons and compatibility with the original behavior.
339          */
340         if ((ifp->if_flags & IFF_SIMPLEX) &&
341            (loop_copy != -1)) {
342                 if ((m->m_flags & M_BCAST) || loop_copy) {
343                         struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
344
345                         (void) if_simloop(ifp,
346                                 n, dst->sa_family, sizeof(struct fddi_header));
347                 } else if (bcmp(fh->fddi_dhost,
348                     fh->fddi_shost, sizeof(fh->fddi_shost)) == 0) {
349                         (void) if_simloop(ifp,
350                                 m, dst->sa_family, sizeof(struct fddi_header));
351                         return(0);      /* XXX */
352                 }
353         }
354
355         s = splimp();
356         /*
357          * Queue message on interface, and start output if interface
358          * not yet active.
359          */
360         if (IF_QFULL(&ifp->if_snd)) {
361                 IF_DROP(&ifp->if_snd);
362                 splx(s);
363                 senderr(ENOBUFS);
364         }
365         ifp->if_obytes += m->m_pkthdr.len;
366         if (m->m_flags & M_MCAST)
367                 ifp->if_omcasts++;
368         IF_ENQUEUE(&ifp->if_snd, m);
369         if ((ifp->if_flags & IFF_OACTIVE) == 0)
370                 (*ifp->if_start)(ifp);
371         splx(s);
372
373         return (error);
374
375 bad:
376         if (m)
377                 m_freem(m);
378         return (error);
379 }
380
381 /*
382  * Process a received FDDI packet;
383  * the packet is in the mbuf chain m without
384  * the fddi header, which is provided separately.
385  */
386 static void
387 fddi_input(struct ifnet *ifp, struct mbuf *m)
388 {
389         int isr;
390         struct llc *l;
391         struct fddi_header *fh = mtod(m, struct fddi_header *);
392
393         if (m->m_len < sizeof(struct fddi_header)) {
394                 /* XXX error in the caller. */
395                 m_freem(m);
396                 return;
397         }
398         m_adj(m, sizeof(struct fddi_header));
399         m->m_pkthdr.rcvif = ifp;
400
401         if ((ifp->if_flags & IFF_UP) == 0) {
402                 m_freem(m);
403                 return;
404         }
405         getmicrotime(&ifp->if_lastchange);
406         ifp->if_ibytes += m->m_pkthdr.len + sizeof (*fh);
407         if (fh->fddi_dhost[0] & 1) {
408                 if (bcmp(ifp->if_broadcastaddr, fh->fddi_dhost,
409                          ifp->if_addrlen) == 0)
410                         m->m_flags |= M_BCAST;
411                 else
412                         m->m_flags |= M_MCAST;
413                 ifp->if_imcasts++;
414         } else if ((ifp->if_flags & IFF_PROMISC) &&
415             bcmp(((struct arpcom *)ifp)->ac_enaddr, (caddr_t)fh->fddi_dhost,
416                     sizeof(fh->fddi_dhost)) != 0) {
417                 m_freem(m);
418                 return;
419         }
420
421 #ifdef M_LINK0
422         /*
423          * If this has a LLC priority of 0, then mark it so upper
424          * layers have a hint that it really came via a FDDI/Ethernet
425          * bridge.
426          */
427         if ((fh->fddi_fc & FDDIFC_LLC_PRIO7) == FDDIFC_LLC_PRIO0)
428                 m->m_flags |= M_LINK0;
429 #endif
430
431         l = mtod(m, struct llc *);
432         switch (l->llc_dsap) {
433 #if defined(INET) || defined(INET6) || defined(NS) || defined(DECNET) || defined(IPX) || defined(NETATALK)
434         case LLC_SNAP_LSAP:
435         {
436                 u_int16_t type;
437                 if (l->llc_control != LLC_UI || l->llc_ssap != LLC_SNAP_LSAP)
438                         goto dropanyway;
439 #ifdef NETATALK
440                 if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
441                          sizeof(at_org_code)) == 0 &&
442                     sizeof(at_org_code) == 0 &&
443                     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
444                         m_adj(m, sizeof(struct llc));
445                         isr = NETISR_ATALK2;
446                         break;
447                 }
448
449                 if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
450                          sizeof(aarp_org_code)) == 0 &&
451                         ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
452                     m_adj( m, sizeof( struct llc ));
453                     isr = NETISR_AARP;
454                     break;
455                 }
456 #endif /* NETATALK */
457                 if (l->llc_snap.org_code[0] != 0 || l->llc_snap.org_code[1] != 0|| l->llc_snap.org_code[2] != 0)
458                         goto dropanyway;
459                 type = ntohs(l->llc_snap.ether_type);
460                 m_adj(m, 8);
461                 switch (type) {
462 #ifdef INET
463                 case ETHERTYPE_IP:
464                         if (ipflow_fastforward(m))
465                                 return;
466                         isr = NETISR_IP;
467                         break;
468
469                 case ETHERTYPE_ARP:
470                         if (ifp->if_flags & IFF_NOARP)
471                                 goto dropanyway;
472                         isr = NETISR_ARP;
473                         break;
474 #endif
475 #ifdef INET6
476                 case ETHERTYPE_IPV6:
477                         isr = NETISR_IPV6;
478                         break;
479 #endif
480 #ifdef IPX      
481                 case ETHERTYPE_IPX: 
482                         isr = NETISR_IPX;
483                         break;  
484 #endif   
485 #ifdef NS
486                 case ETHERTYPE_NS:
487                         isr = NETISR_NS;
488                         break;
489 #endif
490 #ifdef DECNET
491                 case ETHERTYPE_DECNET:
492                         isr = NETISR_DECNET;
493                         break;
494 #endif
495 #ifdef NETATALK 
496                 case ETHERTYPE_AT:
497                         isr = NETISR_ATALK1;
498                         break;
499                 case ETHERTYPE_AARP:
500                         isr = NETISR_AARP;
501                         break;
502 #endif /* NETATALK */
503                 default:
504                         /* printf("fddi_input: unknown protocol 0x%x\n", type); */
505                         ifp->if_noproto++;
506                         goto dropanyway;
507                 }
508                 break;
509         }
510 #endif /* INET || NS */
511                 
512         default:
513                 /* printf("fddi_input: unknown dsap 0x%x\n", l->llc_dsap); */
514                 ifp->if_noproto++;
515         dropanyway:
516                 m_freem(m);
517                 return;
518         }
519
520         netisr_dispatch(isr, m);
521 }
522
523 /*
524  * Perform common duties while attaching to interface list
525  */
526 #ifdef __NetBSD__
527 #define ifa_next        ifa_list.tqe_next
528 #endif
529
530 void
531 fddi_ifattach(ifp)
532         struct ifnet *ifp;
533 {
534         struct ifaddr *ifa;
535         struct sockaddr_dl *sdl;
536
537         ifp->if_input = fddi_input;
538         ifp->if_output = fddi_output;
539         ifp->if_type = IFT_FDDI;
540         ifp->if_addrlen = 6;
541         ifp->if_broadcastaddr = fddibroadcastaddr;
542         ifp->if_hdrlen = 21;
543         ifp->if_mtu = FDDIMTU;
544         ifp->if_resolvemulti = fddi_resolvemulti;
545         ifp->if_baudrate = 100000000;
546 #ifdef IFF_NOTRAILERS
547         ifp->if_flags |= IFF_NOTRAILERS;
548 #endif
549         if_attach(ifp);
550 #if defined(__DragonFly__) || defined(__FreeBSD__)
551         ifa = ifnet_addrs[ifp->if_index - 1];
552         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
553         sdl->sdl_type = IFT_FDDI;
554         sdl->sdl_alen = ifp->if_addrlen;
555         bcopy(((struct arpcom *)ifp)->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
556 #elif defined(__NetBSD__)
557         LIST_INIT(&((struct arpcom *)ifp)->ac_multiaddrs);
558         for (ifa = ifp->if_addrlist.tqh_first; ifa != NULL; ifa = ifa->ifa_list.tqe_next)
559 #else
560         for (ifa = ifp->if_addrlist; ifa != NULL; ifa = ifa->ifa_next)
561 #endif
562 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
563                 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
564                     sdl->sdl_family == AF_LINK) {
565                         sdl->sdl_type = IFT_FDDI;
566                         sdl->sdl_alen = ifp->if_addrlen;
567                         bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr,
568                               LLADDR(sdl), ifp->if_addrlen);
569                         break;
570                 }
571 #endif
572         bpfattach(ifp, DLT_FDDI, sizeof(struct fddi_header));
573 }
574
575 static int
576 fddi_resolvemulti(ifp, llsa, sa)
577         struct ifnet *ifp;
578         struct sockaddr **llsa;
579         struct sockaddr *sa;
580 {
581         struct sockaddr_dl *sdl;
582         struct sockaddr_in *sin;
583 #ifdef INET6
584         struct sockaddr_in6 *sin6;
585 #endif
586         u_char *e_addr;
587
588         switch(sa->sa_family) {
589         case AF_LINK:
590                 /*
591                  * No mapping needed. Just check that it's a valid MC address.
592                  */
593                 sdl = (struct sockaddr_dl *)sa;
594                 e_addr = LLADDR(sdl);
595                 if ((e_addr[0] & 1) != 1)
596                         return EADDRNOTAVAIL;
597                 *llsa = 0;
598                 return 0;
599
600 #ifdef INET
601         case AF_INET:
602                 sin = (struct sockaddr_in *)sa;
603                 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
604                         return EADDRNOTAVAIL;
605                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
606                        M_WAITOK);
607                 sdl->sdl_len = sizeof *sdl;
608                 sdl->sdl_family = AF_LINK;
609                 sdl->sdl_index = ifp->if_index;
610                 sdl->sdl_type = IFT_FDDI;
611                 sdl->sdl_nlen = 0;
612                 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
613                 sdl->sdl_slen = 0;
614                 e_addr = LLADDR(sdl);
615                 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
616                 *llsa = (struct sockaddr *)sdl;
617                 return 0;
618 #endif
619 #ifdef INET6
620         case AF_INET6:
621                 sin6 = (struct sockaddr_in6 *)sa;
622                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
623                         /*
624                          * An IP6 address of 0 means listen to all
625                          * of the Ethernet multicast address used for IP6.
626                          * (This is used for multicast routers.)
627                          */
628                         ifp->if_flags |= IFF_ALLMULTI;
629                         *llsa = 0;
630                         return 0;
631                 }
632                 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
633                         return EADDRNOTAVAIL;
634                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
635                        M_WAITOK);
636                 sdl->sdl_len = sizeof *sdl;
637                 sdl->sdl_family = AF_LINK;
638                 sdl->sdl_index = ifp->if_index;
639                 sdl->sdl_type = IFT_FDDI;
640                 sdl->sdl_nlen = 0;
641                 sdl->sdl_alen = ETHER_ADDR_LEN; /* XXX */
642                 sdl->sdl_slen = 0;
643                 e_addr = LLADDR(sdl);
644                 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
645                 *llsa = (struct sockaddr *)sdl;
646                 return 0;
647 #endif
648
649         default:
650                 /*
651                  * Well, the text isn't quite right, but it's the name
652                  * that counts...
653                  */
654                 return EAFNOSUPPORT;
655         }
656 }