darwin-xnu/bsd/net/ether_at_pr_module.c
<<
>>
Prefs
   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/route.h>
  69#include <net/if_llc.h>
  70#include <net/if_dl.h>
  71#include <net/if_types.h>
  72#include <netinet/if_ether.h>
  73
  74#include <sys/socketvar.h>
  75
  76#include <net/dlil.h>
  77#include <netat/at_pat.h>
  78#if NETAT
  79extern struct ifqueue atalkintrq;
  80#endif
  81
  82
  83#if BRIDGE
  84#include <net/bridge.h>
  85#endif
  86
  87/* #include "vlan.h" */
  88#if NVLAN > 0
  89#include <net/if_vlan_var.h>
  90#endif /* NVLAN > 0 */
  91
  92struct dl_es_at_entry 
  93{
  94        struct ifnet *ifp;
  95        int    ref_count;
  96};
  97
  98/* Local fuction declerations */
  99int at_ether_input(struct mbuf *m, char *frame_header, struct ifnet *ifp,
 100                                   u_long protocol_family, int sync_ok);
 101int ether_pre_output(struct ifnet *ifp, u_long protocol_family, struct mbuf **m0,
 102                                         const struct sockaddr *dst_netaddr, caddr_t route, char *type, char *edst);
 103int ether_prmod_ioctl(u_long protocol_family, struct ifnet *ifp, u_long command,
 104                                          caddr_t data);
 105int ether_attach_at(struct ifnet *ifp);
 106void ether_detach_at(struct ifnet *ifp);
 107
 108
 109/*
 110 * Temp static for protocol registration XXX
 111 */
 112
 113#define MAX_EN_COUNT 30
 114
 115static struct dl_es_at_entry en_at_array[MAX_EN_COUNT];
 116
 117/*
 118 * Process a received Ethernet packet;
 119 * the packet is in the mbuf chain m without
 120 * the ether header, which is provided separately.
 121 */
 122int
 123at_ether_input(
 124        struct mbuf                             *m,
 125        __unused char                   *frame_header,
 126        __unused struct ifnet   *ifp,
 127        __unused u_long                 protocol_family,
 128        __unused int                    sync_ok)
 129
 130{
 131   /*
 132        * note: for AppleTalk we need to pass the enet header of the
 133        * packet up stack. To do so, we made sure in that the FULL packet
 134        * is copied in the mbuf by the mace driver, and only the m_data and
 135        * length have been shifted to make IP and the other guys happy.
 136        */
 137
 138        m->m_data -= sizeof(struct ether_header);
 139        m->m_len += sizeof(struct ether_header);
 140        m->m_pkthdr.len += sizeof(struct ether_header);
 141        proto_input(PF_APPLETALK, m);
 142
 143        return 0;
 144}
 145
 146
 147
 148int
 149ether_pre_output(
 150        struct ifnet                    *ifp,
 151        __unused u_long                 protocol_family,
 152        struct mbuf                             **m0,
 153        const struct sockaddr   *dst_netaddr,
 154        __unused caddr_t                route,
 155        char                                    *type,
 156        char                                    *edst)
 157{
 158    register struct mbuf *m = *m0;
 159    register struct ether_header *eh;
 160    int hlen;   /* link layer header lenght */
 161
 162
 163
 164    if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 
 165        return ENETDOWN;
 166
 167    hlen = ETHER_HDR_LEN;
 168
 169    /*
 170     * Tell ether_frameout it's ok to loop packet unless negated below.
 171     */
 172    m->m_flags |= M_LOOP;
 173
 174    switch (dst_netaddr->sa_family) {
 175    case AF_UNSPEC:
 176        m->m_flags &= ~M_LOOP;
 177        eh = (struct ether_header *)dst_netaddr->sa_data;
 178        (void)memcpy(edst, eh->ether_dhost, 6);
 179        *(u_short *)type = eh->ether_type;
 180        break;
 181        
 182
 183    case AF_APPLETALK:
 184    {
 185        eh = (struct ether_header *)dst_netaddr->sa_data;
 186        bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, 6);
 187                
 188        *(u_short *)type = m->m_pkthdr.len;
 189    }
 190    break;
 191
 192
 193    default:
 194        kprintf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
 195               dst_netaddr->sa_family);
 196
 197        return EAFNOSUPPORT;
 198    }
 199
 200    return (0);
 201}
 202
 203
 204
 205
 206
 207int
 208ether_prmod_ioctl(
 209    __unused u_long     protocol_family,
 210    struct ifnet        *ifp,
 211    u_long                      command,
 212    caddr_t                     data)
 213{
 214    struct ifreq *ifr = (struct ifreq *) data;
 215    int error = 0;
 216
 217    switch (command) {
 218
 219    case SIOCSIFADDR:
 220         if ((ifp->if_flags & IFF_RUNNING) == 0) {
 221              ifnet_set_flags(ifp, IFF_UP, IFF_UP);
 222              dlil_ioctl(0, ifp, SIOCSIFFLAGS, (caddr_t) 0);
 223         }
 224
 225        break;
 226
 227    case SIOCGIFADDR:
 228        ifnet_lladdr_copy_bytes(ifp, ifr->ifr_addr.sa_data, ETHER_ADDR_LEN);
 229    break;
 230
 231    case SIOCSIFMTU:
 232        /*
 233         * Set the interface MTU.
 234         */
 235        if (ifr->ifr_mtu > ETHERMTU) {
 236            error = EINVAL;
 237        } else {
 238            ifp->if_mtu = ifr->ifr_mtu;
 239        }
 240        break;
 241
 242    default:
 243         return EOPNOTSUPP;
 244    }
 245
 246
 247    return (error);
 248}
 249
 250
 251
 252int
 253ether_attach_at(
 254        struct ifnet *ifp)
 255{
 256    struct dlil_proto_reg_str   reg;
 257    struct dlil_demux_desc      desc;
 258    struct dlil_demux_desc      desc2;
 259    int   stat;
 260    int   first_empty;
 261    int   i;
 262    u_int8_t    atalk_snap[5] = {0x08, 0x00, 0x07, 0x80, 0x9b};
 263    u_int8_t    aarp_snap[5] = {0x00, 0x00, 0x00, 0x80, 0xf3};
 264
 265    first_empty = MAX_EN_COUNT;
 266        for (i=0; i < MAX_EN_COUNT; i++) {
 267                if (en_at_array[i].ifp == 0)
 268                        first_empty = i;
 269                
 270                if (en_at_array[i].ifp == ifp) {
 271                        en_at_array[i].ref_count++;
 272                        return 0;
 273                }
 274    }
 275    
 276        if (first_empty == MAX_EN_COUNT)
 277                return ENOMEM;
 278        
 279        bzero(&reg, sizeof(reg));
 280        bzero(&desc, sizeof(desc));
 281        bzero(&desc2, sizeof(desc2));
 282        
 283        TAILQ_INIT(&reg.demux_desc_head);
 284        reg.interface_family = ifp->if_family;
 285        reg.unit_number      = ifp->if_unit;
 286        reg.input            = at_ether_input;
 287        reg.pre_output       = ether_pre_output;
 288        reg.ioctl            = ether_prmod_ioctl;
 289        reg.protocol_family  = PF_APPLETALK;
 290
 291        desc.type = DLIL_DESC_SNAP;
 292        desc.native_type = atalk_snap;
 293        desc.variants.native_type_length = sizeof(atalk_snap);
 294        TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc, next);
 295        
 296        desc2.type = DLIL_DESC_SNAP;
 297        desc2.native_type = aarp_snap;
 298        desc2.variants.native_type_length = sizeof(aarp_snap);
 299        TAILQ_INSERT_TAIL(&reg.demux_desc_head, &desc2, next);
 300        
 301    stat = dlil_attach_protocol(&reg);
 302        if (stat) {
 303                printf("WARNING: ether_attach_at can't attach at to interface\n");
 304                return stat;
 305        }
 306
 307        en_at_array[first_empty].ifp = ifp;
 308        en_at_array[first_empty].ref_count = 1;
 309        
 310        return 0;
 311} /* ether_attach_at */
 312
 313
 314void
 315ether_detach_at(struct ifnet *ifp)
 316{
 317        int i;
 318        
 319        for (i=0; i < MAX_EN_COUNT; i++) {
 320                if (en_at_array[i].ifp == ifp)
 321                        break;
 322        }
 323        
 324        if (i < MAX_EN_COUNT) {
 325                if (en_at_array[i].ref_count > 1) 
 326                        en_at_array[i].ref_count--;
 327                else {
 328                        if (en_at_array[i].ref_count == 1) {
 329                                dlil_detach_protocol(ifp, PF_APPLETALK);
 330                                en_at_array[i].ifp = 0;
 331                        }
 332                }
 333        }
 334}
 335
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.