11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 31da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 41da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 51da177e4SLinus Torvalds * (at your option) any later version. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk) 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds #include <linux/module.h> 101da177e4SLinus Torvalds #include <linux/proc_fs.h> 111da177e4SLinus Torvalds #include <linux/kernel.h> 121da177e4SLinus Torvalds #include <linux/interrupt.h> 131da177e4SLinus Torvalds #include <linux/fs.h> 141da177e4SLinus Torvalds #include <linux/types.h> 151da177e4SLinus Torvalds #include <linux/sysctl.h> 161da177e4SLinus Torvalds #include <linux/string.h> 171da177e4SLinus Torvalds #include <linux/socket.h> 181da177e4SLinus Torvalds #include <linux/errno.h> 191da177e4SLinus Torvalds #include <linux/fcntl.h> 201da177e4SLinus Torvalds #include <linux/in.h> 211da177e4SLinus Torvalds #include <linux/if_ether.h> /* For the statistics structure. */ 22*5a0e3ad6STejun Heo #include <linux/slab.h> 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds #include <asm/system.h> 251da177e4SLinus Torvalds #include <asm/uaccess.h> 261da177e4SLinus Torvalds #include <asm/io.h> 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #include <linux/inet.h> 291da177e4SLinus Torvalds #include <linux/netdevice.h> 301da177e4SLinus Torvalds #include <linux/etherdevice.h> 311da177e4SLinus Torvalds #include <linux/if_arp.h> 321da177e4SLinus Torvalds #include <linux/skbuff.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #include <net/ip.h> 351da177e4SLinus Torvalds #include <net/arp.h> 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds #include <net/ax25.h> 381da177e4SLinus Torvalds #include <net/netrom.h> 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds /* 411da177e4SLinus Torvalds * Only allow IP over NET/ROM frames through if the netrom device is up. 421da177e4SLinus Torvalds */ 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds int nr_rx_ip(struct sk_buff *skb, struct net_device *dev) 451da177e4SLinus Torvalds { 46b51414b6SStephen Hemminger struct net_device_stats *stats = &dev->stats; 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds if (!netif_running(dev)) { 496ddcf626SRalf Baechle stats->rx_dropped++; 501da177e4SLinus Torvalds return 0; 511da177e4SLinus Torvalds } 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds stats->rx_packets++; 541da177e4SLinus Torvalds stats->rx_bytes += skb->len; 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds skb->protocol = htons(ETH_P_IP); 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds /* Spoof incoming device */ 591da177e4SLinus Torvalds skb->dev = dev; 60c6e6ca71SDavid S. Miller skb->mac_header = skb->network_header; 61c1d2bbe1SArnaldo Carvalho de Melo skb_reset_network_header(skb); 621da177e4SLinus Torvalds skb->pkt_type = PACKET_HOST; 631da177e4SLinus Torvalds 6498a82febSRalf Baechle netif_rx(skb); 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds return 1; 671da177e4SLinus Torvalds } 681da177e4SLinus Torvalds 6998a82febSRalf Baechle #ifdef CONFIG_INET 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds static int nr_rebuild_header(struct sk_buff *skb) 721da177e4SLinus Torvalds { 731da177e4SLinus Torvalds unsigned char *bp = skb->data; 741da177e4SLinus Torvalds 753f2aadd0SRalf Baechle if (arp_find(bp + 7, skb)) 761da177e4SLinus Torvalds return 1; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds bp[6] &= ~AX25_CBIT; 791da177e4SLinus Torvalds bp[6] &= ~AX25_EBIT; 801da177e4SLinus Torvalds bp[6] |= AX25_SSSID_SPARE; 811da177e4SLinus Torvalds bp += AX25_ADDR_LEN; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds bp[6] &= ~AX25_CBIT; 841da177e4SLinus Torvalds bp[6] |= AX25_EBIT; 851da177e4SLinus Torvalds bp[6] |= AX25_SSSID_SPARE; 861da177e4SLinus Torvalds 873f2aadd0SRalf Baechle return 0; 881da177e4SLinus Torvalds } 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds #else 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds static int nr_rebuild_header(struct sk_buff *skb) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds return 1; 951da177e4SLinus Torvalds } 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds #endif 981da177e4SLinus Torvalds 993b04dddeSStephen Hemminger static int nr_header(struct sk_buff *skb, struct net_device *dev, 1003b04dddeSStephen Hemminger unsigned short type, 1013b04dddeSStephen Hemminger const void *daddr, const void *saddr, unsigned len) 1021da177e4SLinus Torvalds { 1031da177e4SLinus Torvalds unsigned char *buff = skb_push(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN); 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds memcpy(buff, (saddr != NULL) ? saddr : dev->dev_addr, dev->addr_len); 1061da177e4SLinus Torvalds buff[6] &= ~AX25_CBIT; 1071da177e4SLinus Torvalds buff[6] &= ~AX25_EBIT; 1081da177e4SLinus Torvalds buff[6] |= AX25_SSSID_SPARE; 1091da177e4SLinus Torvalds buff += AX25_ADDR_LEN; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds if (daddr != NULL) 1121da177e4SLinus Torvalds memcpy(buff, daddr, dev->addr_len); 1131da177e4SLinus Torvalds buff[6] &= ~AX25_CBIT; 1141da177e4SLinus Torvalds buff[6] |= AX25_EBIT; 1151da177e4SLinus Torvalds buff[6] |= AX25_SSSID_SPARE; 1161da177e4SLinus Torvalds buff += AX25_ADDR_LEN; 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds *buff++ = sysctl_netrom_network_ttl_initialiser; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds *buff++ = NR_PROTO_IP; 1211da177e4SLinus Torvalds *buff++ = NR_PROTO_IP; 1221da177e4SLinus Torvalds *buff++ = 0; 1231da177e4SLinus Torvalds *buff++ = 0; 1241da177e4SLinus Torvalds *buff++ = NR_PROTOEXT; 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds if (daddr != NULL) 1271da177e4SLinus Torvalds return 37; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds return -37; 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 13281dcd169SRalf Baechle static int __must_check nr_set_mac_address(struct net_device *dev, void *addr) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds struct sockaddr *sa = addr; 13581dcd169SRalf Baechle int err; 1361da177e4SLinus Torvalds 13781dcd169SRalf Baechle if (!memcmp(dev->dev_addr, sa->sa_data, dev->addr_len)) 13881dcd169SRalf Baechle return 0; 13981dcd169SRalf Baechle 14081dcd169SRalf Baechle if (dev->flags & IFF_UP) { 14181dcd169SRalf Baechle err = ax25_listen_register((ax25_address *)sa->sa_data, NULL); 14281dcd169SRalf Baechle if (err) 14381dcd169SRalf Baechle return err; 14481dcd169SRalf Baechle 1451da177e4SLinus Torvalds ax25_listen_release((ax25_address *)dev->dev_addr, NULL); 14681dcd169SRalf Baechle } 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds memcpy(dev->dev_addr, sa->sa_data, dev->addr_len); 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds return 0; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds static int nr_open(struct net_device *dev) 1541da177e4SLinus Torvalds { 15581dcd169SRalf Baechle int err; 15681dcd169SRalf Baechle 15781dcd169SRalf Baechle err = ax25_listen_register((ax25_address *)dev->dev_addr, NULL); 15881dcd169SRalf Baechle if (err) 15981dcd169SRalf Baechle return err; 16081dcd169SRalf Baechle 1611da177e4SLinus Torvalds netif_start_queue(dev); 16281dcd169SRalf Baechle 1631da177e4SLinus Torvalds return 0; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds static int nr_close(struct net_device *dev) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds ax25_listen_release((ax25_address *)dev->dev_addr, NULL); 1691da177e4SLinus Torvalds netif_stop_queue(dev); 1701da177e4SLinus Torvalds return 0; 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 17336e4d64aSStephen Hemminger static netdev_tx_t nr_xmit(struct sk_buff *skb, struct net_device *dev) 1741da177e4SLinus Torvalds { 175b51414b6SStephen Hemminger struct net_device_stats *stats = &dev->stats; 176b88a762bSRalf Baechle unsigned int len = skb->len; 1773f2aadd0SRalf Baechle 1783f2aadd0SRalf Baechle if (!nr_route_frame(skb, NULL)) { 1793f2aadd0SRalf Baechle kfree_skb(skb); 1801da177e4SLinus Torvalds stats->tx_errors++; 1816ed10654SPatrick McHardy return NETDEV_TX_OK; 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1843f2aadd0SRalf Baechle stats->tx_packets++; 1853f2aadd0SRalf Baechle stats->tx_bytes += len; 1863f2aadd0SRalf Baechle 1876ed10654SPatrick McHardy return NETDEV_TX_OK; 1883f2aadd0SRalf Baechle } 1893f2aadd0SRalf Baechle 1903b04dddeSStephen Hemminger static const struct header_ops nr_header_ops = { 1913b04dddeSStephen Hemminger .create = nr_header, 1923b04dddeSStephen Hemminger .rebuild= nr_rebuild_header, 1933b04dddeSStephen Hemminger }; 1943b04dddeSStephen Hemminger 1950f6c5c8eSStephen Hemminger static const struct net_device_ops nr_netdev_ops = { 1960f6c5c8eSStephen Hemminger .ndo_open = nr_open, 1970f6c5c8eSStephen Hemminger .ndo_stop = nr_close, 1980f6c5c8eSStephen Hemminger .ndo_start_xmit = nr_xmit, 1990f6c5c8eSStephen Hemminger .ndo_set_mac_address = nr_set_mac_address, 2000f6c5c8eSStephen Hemminger }; 2013b04dddeSStephen Hemminger 2021da177e4SLinus Torvalds void nr_setup(struct net_device *dev) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds dev->mtu = NR_MAX_PACKET_SIZE; 2050f6c5c8eSStephen Hemminger dev->netdev_ops = &nr_netdev_ops; 2063b04dddeSStephen Hemminger dev->header_ops = &nr_header_ops; 2071da177e4SLinus Torvalds dev->hard_header_len = NR_NETWORK_LEN + NR_TRANSPORT_LEN; 2081da177e4SLinus Torvalds dev->addr_len = AX25_ADDR_LEN; 2091da177e4SLinus Torvalds dev->type = ARPHRD_NETROM; 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds /* New-style flags. */ 21272377291SRalf Baechle dev->flags = IFF_NOARP; 2131da177e4SLinus Torvalds } 214