Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
0005  */
0006 #include <linux/module.h>
0007 #include <linux/proc_fs.h>
0008 #include <linux/kernel.h>
0009 #include <linux/interrupt.h>
0010 #include <linux/fs.h>
0011 #include <linux/types.h>
0012 #include <linux/sysctl.h>
0013 #include <linux/string.h>
0014 #include <linux/socket.h>
0015 #include <linux/errno.h>
0016 #include <linux/fcntl.h>
0017 #include <linux/in.h>
0018 #include <linux/if_ether.h> /* For the statistics structure. */
0019 #include <linux/slab.h>
0020 #include <linux/uaccess.h>
0021 
0022 #include <asm/io.h>
0023 
0024 #include <linux/inet.h>
0025 #include <linux/netdevice.h>
0026 #include <linux/etherdevice.h>
0027 #include <linux/if_arp.h>
0028 #include <linux/skbuff.h>
0029 
0030 #include <net/ip.h>
0031 #include <net/arp.h>
0032 
0033 #include <net/ax25.h>
0034 #include <net/netrom.h>
0035 
0036 /*
0037  *  Only allow IP over NET/ROM frames through if the netrom device is up.
0038  */
0039 
0040 int nr_rx_ip(struct sk_buff *skb, struct net_device *dev)
0041 {
0042     struct net_device_stats *stats = &dev->stats;
0043 
0044     if (!netif_running(dev)) {
0045         stats->rx_dropped++;
0046         return 0;
0047     }
0048 
0049     stats->rx_packets++;
0050     stats->rx_bytes += skb->len;
0051 
0052     skb->protocol = htons(ETH_P_IP);
0053 
0054     /* Spoof incoming device */
0055     skb->dev      = dev;
0056     skb->mac_header = skb->network_header;
0057     skb_reset_network_header(skb);
0058     skb->pkt_type = PACKET_HOST;
0059 
0060     netif_rx(skb);
0061 
0062     return 1;
0063 }
0064 
0065 static int nr_header(struct sk_buff *skb, struct net_device *dev,
0066              unsigned short type,
0067              const void *daddr, const void *saddr, unsigned int len)
0068 {
0069     unsigned char *buff = skb_push(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
0070 
0071     memcpy(buff, (saddr != NULL) ? saddr : dev->dev_addr, dev->addr_len);
0072     buff[6] &= ~AX25_CBIT;
0073     buff[6] &= ~AX25_EBIT;
0074     buff[6] |= AX25_SSSID_SPARE;
0075     buff    += AX25_ADDR_LEN;
0076 
0077     if (daddr != NULL)
0078         memcpy(buff, daddr, dev->addr_len);
0079     buff[6] &= ~AX25_CBIT;
0080     buff[6] |= AX25_EBIT;
0081     buff[6] |= AX25_SSSID_SPARE;
0082     buff    += AX25_ADDR_LEN;
0083 
0084     *buff++ = sysctl_netrom_network_ttl_initialiser;
0085 
0086     *buff++ = NR_PROTO_IP;
0087     *buff++ = NR_PROTO_IP;
0088     *buff++ = 0;
0089     *buff++ = 0;
0090     *buff++ = NR_PROTOEXT;
0091 
0092     if (daddr != NULL)
0093         return 37;
0094 
0095     return -37;
0096 }
0097 
0098 static int __must_check nr_set_mac_address(struct net_device *dev, void *addr)
0099 {
0100     struct sockaddr *sa = addr;
0101     int err;
0102 
0103     if (!memcmp(dev->dev_addr, sa->sa_data, dev->addr_len))
0104         return 0;
0105 
0106     if (dev->flags & IFF_UP) {
0107         err = ax25_listen_register((ax25_address *)sa->sa_data, NULL);
0108         if (err)
0109             return err;
0110 
0111         ax25_listen_release((const ax25_address *)dev->dev_addr, NULL);
0112     }
0113 
0114     dev_addr_set(dev, sa->sa_data);
0115 
0116     return 0;
0117 }
0118 
0119 static int nr_open(struct net_device *dev)
0120 {
0121     int err;
0122 
0123     err = ax25_listen_register((const ax25_address *)dev->dev_addr, NULL);
0124     if (err)
0125         return err;
0126 
0127     netif_start_queue(dev);
0128 
0129     return 0;
0130 }
0131 
0132 static int nr_close(struct net_device *dev)
0133 {
0134     ax25_listen_release((const ax25_address *)dev->dev_addr, NULL);
0135     netif_stop_queue(dev);
0136     return 0;
0137 }
0138 
0139 static netdev_tx_t nr_xmit(struct sk_buff *skb, struct net_device *dev)
0140 {
0141     struct net_device_stats *stats = &dev->stats;
0142     unsigned int len = skb->len;
0143 
0144     if (!nr_route_frame(skb, NULL)) {
0145         kfree_skb(skb);
0146         stats->tx_errors++;
0147         return NETDEV_TX_OK;
0148     }
0149 
0150     stats->tx_packets++;
0151     stats->tx_bytes += len;
0152 
0153     return NETDEV_TX_OK;
0154 }
0155 
0156 static const struct header_ops nr_header_ops = {
0157     .create = nr_header,
0158 };
0159 
0160 static const struct net_device_ops nr_netdev_ops = {
0161     .ndo_open       = nr_open,
0162     .ndo_stop       = nr_close,
0163     .ndo_start_xmit     = nr_xmit,
0164     .ndo_set_mac_address    = nr_set_mac_address,
0165 };
0166 
0167 void nr_setup(struct net_device *dev)
0168 {
0169     dev->mtu        = NR_MAX_PACKET_SIZE;
0170     dev->netdev_ops     = &nr_netdev_ops;
0171     dev->header_ops     = &nr_header_ops;
0172     dev->hard_header_len    = NR_NETWORK_LEN + NR_TRANSPORT_LEN;
0173     dev->addr_len       = AX25_ADDR_LEN;
0174     dev->type       = ARPHRD_NETROM;
0175 
0176     /* New-style flags. */
0177     dev->flags      = IFF_NOARP;
0178 }