root/net/netrom/nr_dev.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. nr_rx_ip
  2. nr_header
  3. nr_rebuild_header
  4. nr_set_mac_address
  5. nr_open
  6. nr_close
  7. nr_xmit
  8. nr_get_stats
  9. nr_init

   1 /*
   2  *      NET/ROM release 003
   3  *
   4  *      This is ALPHA test software. This code may break your machine, randomly fail to work with new 
   5  *      releases, misbehave and/or generally screw up. It might even work. 
   6  *
   7  *      This code REQUIRES 1.3.0 or higher/ NET3.029
   8  *
   9  *      This module:
  10  *              This module is free software; you can redistribute it and/or
  11  *              modify it under the terms of the GNU General Public License
  12  *              as published by the Free Software Foundation; either version
  13  *              2 of the License, or (at your option) any later version.
  14  *
  15  *      History
  16  *      NET/ROM 001     Jonathan(G4KLX) Cloned from loopback.c
  17  *      NET/ROM 002     Steve Whitehouse(GW7RRM) fixed the set_mac_address
  18  *      NET/ROM 003     Jonathan(G4KLX) Put nr_rebuild_header into line with
  19  *                                      ax25_rebuild_header
  20  */
  21 
  22 #include <linux/config.h>
  23 #ifdef CONFIG_NETROM
  24 #include <linux/kernel.h>
  25 #include <linux/sched.h>
  26 #include <linux/interrupt.h>
  27 #include <linux/fs.h>
  28 #include <linux/types.h>
  29 #include <linux/string.h>
  30 #include <linux/socket.h>
  31 #include <linux/errno.h>
  32 #include <linux/fcntl.h>
  33 #include <linux/in.h>
  34 #include <linux/if_ether.h>     /* For the statistics structure. */
  35 
  36 #include <asm/system.h>
  37 #include <asm/segment.h>
  38 #include <asm/io.h>
  39 
  40 #include <linux/inet.h>
  41 #include <linux/netdevice.h>
  42 #include <linux/etherdevice.h>
  43 #include <linux/if_arp.h>
  44 #include <linux/skbuff.h>
  45 
  46 #include <net/ip.h>
  47 #include <net/arp.h>
  48 
  49 #include <net/ax25.h>
  50 #include <net/netrom.h>
  51 
  52 /*
  53  *      Only allow IP over NET/ROM frames through if the netrom device is up.
  54  */
  55 
  56 int nr_rx_ip(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
  57 {
  58         struct enet_statistics *stats = (struct enet_statistics *)dev->priv;
  59 
  60         if (!dev->start) {
  61                 stats->rx_errors++;
  62                 return 0;
  63         }
  64 
  65         stats->rx_packets++;
  66         skb->protocol = htons(ETH_P_IP);
  67 
  68         /* Spoof incoming device */
  69         skb->dev = dev;
  70 
  71         skb->h.raw = skb->data;
  72         ip_rcv(skb, skb->dev, NULL);
  73 
  74         return 1;
  75 }
  76 
  77 static int nr_header(struct sk_buff *skb, struct device *dev, unsigned short type,
     /* [previous][next][first][last][top][bottom][index][help] */
  78         void *daddr, void *saddr, unsigned len)
  79 {
  80         unsigned char *buff = skb_push(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
  81 
  82         memcpy(buff, (saddr != NULL) ? saddr : dev->dev_addr, dev->addr_len);
  83         buff[6] &= ~LAPB_C;
  84         buff[6] &= ~LAPB_E;
  85         buff[6] |= SSSID_SPARE;
  86         buff    += AX25_ADDR_LEN;
  87 
  88         if (daddr != NULL)
  89                 memcpy(buff, daddr, dev->addr_len);
  90         buff[6] &= ~LAPB_C;
  91         buff[6] |= LAPB_E;
  92         buff[6] |= SSSID_SPARE;
  93         buff    += AX25_ADDR_LEN;
  94 
  95         *buff++ = nr_default.ttl;
  96 
  97         *buff++ = NR_PROTO_IP;
  98         *buff++ = NR_PROTO_IP;
  99         *buff++ = 0;
 100         *buff++ = 0;
 101         *buff++ = NR_PROTOEXT;
 102 
 103         if (daddr != NULL)
 104                 return 37;
 105         
 106         return -37;     
 107 }
 108 
 109 static int nr_rebuild_header(void *buff, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
 110         unsigned long raddr, struct sk_buff *skb)
 111 {
 112         struct enet_statistics *stats = (struct enet_statistics *)dev->priv;
 113         unsigned char *bp = (unsigned char *)buff;
 114         struct sk_buff *skbn;
 115 
 116         if (!arp_query(bp + 7, raddr, dev)) {
 117                 dev_kfree_skb(skb, FREE_WRITE);
 118                 return 1;
 119         }
 120 
 121         bp[6] &= ~LAPB_C;
 122         bp[6] &= ~LAPB_E;
 123         bp[6] |= SSSID_SPARE;
 124         bp    += AX25_ADDR_LEN;
 125         
 126         bp[6] &= ~LAPB_C;
 127         bp[6] |= LAPB_E;
 128         bp[6] |= SSSID_SPARE;
 129 
 130         if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
 131                 dev_kfree_skb(skb, FREE_WRITE);
 132                 return 1;
 133         }
 134 
 135         skbn->sk = skb->sk;
 136         
 137         if (skbn->sk != NULL)
 138                 atomic_add(skbn->truesize, &skbn->sk->wmem_alloc);
 139 
 140         dev_kfree_skb(skb, FREE_WRITE);
 141 
 142         if (!nr_route_frame(skbn, NULL)) {
 143                 dev_kfree_skb(skbn, FREE_WRITE);
 144                 stats->tx_errors++;
 145         }
 146 
 147         stats->tx_packets++;
 148 
 149         return 1;
 150 }
 151 
 152 static int nr_set_mac_address(struct device *dev, void *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 153 {
 154         struct sockaddr *sa=addr;
 155         memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
 156 
 157         return 0;
 158 }
 159 
 160 static int nr_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 161 {
 162         dev->tbusy = 0;
 163         dev->start = 1;
 164 
 165         return 0;
 166 }
 167 
 168 static int nr_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 169 {
 170         dev->tbusy = 1;
 171         dev->start = 0;
 172 
 173         return 0;
 174 }
 175 
 176 static int nr_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 177 {
 178         struct enet_statistics *stats = (struct enet_statistics *)dev->priv;
 179 
 180         if (skb == NULL || dev == NULL)
 181                 return 0;
 182 
 183         if (!dev->start) {
 184                 printk("netrom: xmit call when iface is down\n");
 185                 return 1;
 186         }
 187 
 188         cli();
 189 
 190         if (dev->tbusy != 0) {
 191                 sti();
 192                 stats->tx_errors++;
 193                 return 1;
 194         }
 195 
 196         dev->tbusy = 1;
 197 
 198         sti();
 199 
 200         dev_kfree_skb(skb, FREE_WRITE);
 201 
 202         stats->tx_errors++;
 203 
 204         dev->tbusy = 0;
 205 
 206         mark_bh(NET_BH);
 207 
 208         return 0;
 209 }
 210 
 211 static struct enet_statistics *nr_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 212 {
 213         return (struct enet_statistics *)dev->priv;
 214 }
 215 
 216 int nr_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 217 {
 218         int i;
 219 
 220         dev->mtu                = 236;          /* MTU                  */
 221         dev->tbusy              = 0;
 222         dev->hard_start_xmit    = nr_xmit;
 223         dev->open               = nr_open;
 224         dev->stop               = nr_close;
 225 
 226         dev->hard_header        = nr_header;
 227         dev->hard_header_len    = AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
 228         dev->addr_len           = AX25_ADDR_LEN;
 229         dev->type               = ARPHRD_NETROM;
 230         dev->rebuild_header     = nr_rebuild_header;
 231         dev->set_mac_address    = nr_set_mac_address;
 232 
 233         /* New-style flags. */
 234         dev->flags              = 0;
 235         dev->family             = AF_INET;
 236 
 237         dev->pa_addr            = 0;
 238         dev->pa_brdaddr         = 0;
 239         dev->pa_mask            = 0;
 240         dev->pa_alen            = sizeof(unsigned long);
 241 
 242         dev->priv = kmalloc(sizeof(struct enet_statistics), GFP_KERNEL);
 243         if (dev->priv == NULL)
 244                 return -ENOMEM;
 245 
 246         memset(dev->priv, 0, sizeof(struct enet_statistics));
 247 
 248         dev->get_stats = nr_get_stats;
 249 
 250         /* Fill in the generic fields of the device structure. */
 251         for (i = 0; i < DEV_NUMBUFFS; i++)
 252                 skb_queue_head_init(&dev->buffs[i]);
 253 
 254         return 0;
 255 };
 256 
 257 #endif

/* [previous][next][first][last][top][bottom][index][help] */