xref: /linux/net/rose/rose_dev.c (revision 78f150bf94f5430fe8c34edeafe8d01706f38148)
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 (C) 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>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <asm/system.h>
241da177e4SLinus Torvalds #include <asm/io.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <linux/inet.h>
271da177e4SLinus Torvalds #include <linux/netdevice.h>
281da177e4SLinus Torvalds #include <linux/etherdevice.h>
291da177e4SLinus Torvalds #include <linux/if_arp.h>
301da177e4SLinus Torvalds #include <linux/skbuff.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <net/ip.h>
331da177e4SLinus Torvalds #include <net/arp.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include <net/ax25.h>
361da177e4SLinus Torvalds #include <net/rose.h>
371da177e4SLinus Torvalds 
383b04dddeSStephen Hemminger static int rose_header(struct sk_buff *skb, struct net_device *dev,
393b04dddeSStephen Hemminger 		       unsigned short type,
403b04dddeSStephen Hemminger 		       const void *daddr, const void *saddr, unsigned len)
411da177e4SLinus Torvalds {
421da177e4SLinus Torvalds 	unsigned char *buff = skb_push(skb, ROSE_MIN_LEN + 2);
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds 	*buff++ = ROSE_GFI | ROSE_Q_BIT;
451da177e4SLinus Torvalds 	*buff++ = 0x00;
461da177e4SLinus Torvalds 	*buff++ = ROSE_DATA;
471da177e4SLinus Torvalds 	*buff++ = 0x7F;
481da177e4SLinus Torvalds 	*buff++ = AX25_P_IP;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	if (daddr != NULL)
511da177e4SLinus Torvalds 		return 37;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	return -37;
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds static int rose_rebuild_header(struct sk_buff *skb)
571da177e4SLinus Torvalds {
58*78f150bfSPavel Emelyanov #ifdef CONFIG_INET
591da177e4SLinus Torvalds 	struct net_device *dev = skb->dev;
601da177e4SLinus Torvalds 	struct net_device_stats *stats = netdev_priv(dev);
611da177e4SLinus Torvalds 	unsigned char *bp = (unsigned char *)skb->data;
621da177e4SLinus Torvalds 	struct sk_buff *skbn;
638dc22d2bSRalf Baechle 	unsigned int len;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	if (arp_find(bp + 7, skb)) {
661da177e4SLinus Torvalds 		return 1;
671da177e4SLinus Torvalds 	}
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
701da177e4SLinus Torvalds 		kfree_skb(skb);
711da177e4SLinus Torvalds 		return 1;
721da177e4SLinus Torvalds 	}
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	if (skb->sk != NULL)
751da177e4SLinus Torvalds 		skb_set_owner_w(skbn, skb->sk);
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	kfree_skb(skb);
781da177e4SLinus Torvalds 
798dc22d2bSRalf Baechle 	len = skbn->len;
808dc22d2bSRalf Baechle 
811da177e4SLinus Torvalds 	if (!rose_route_frame(skbn, NULL)) {
821da177e4SLinus Torvalds 		kfree_skb(skbn);
831da177e4SLinus Torvalds 		stats->tx_errors++;
841da177e4SLinus Torvalds 		return 1;
851da177e4SLinus Torvalds 	}
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	stats->tx_packets++;
888dc22d2bSRalf Baechle 	stats->tx_bytes += len;
891da177e4SLinus Torvalds #endif
901da177e4SLinus Torvalds 	return 1;
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds static int rose_set_mac_address(struct net_device *dev, void *addr)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds 	struct sockaddr *sa = addr;
96a159aaa3SRalf Baechle 	int err;
97a159aaa3SRalf Baechle 
98a159aaa3SRalf Baechle 	if (!memcpy(dev->dev_addr, sa->sa_data, dev->addr_len))
99a159aaa3SRalf Baechle 		return 0;
100a159aaa3SRalf Baechle 
101a159aaa3SRalf Baechle 	if (dev->flags & IFF_UP) {
102a159aaa3SRalf Baechle 		err = rose_add_loopback_node((rose_address *)dev->dev_addr);
103a159aaa3SRalf Baechle 		if (err)
104a159aaa3SRalf Baechle 			return err;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 		rose_del_loopback_node((rose_address *)dev->dev_addr);
107a159aaa3SRalf Baechle 	}
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	return 0;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds static int rose_open(struct net_device *dev)
1151da177e4SLinus Torvalds {
116a159aaa3SRalf Baechle 	int err;
117a159aaa3SRalf Baechle 
118a159aaa3SRalf Baechle 	err = rose_add_loopback_node((rose_address *)dev->dev_addr);
119a159aaa3SRalf Baechle 	if (err)
120a159aaa3SRalf Baechle 		return err;
121a159aaa3SRalf Baechle 
1221da177e4SLinus Torvalds 	netif_start_queue(dev);
123a159aaa3SRalf Baechle 
1241da177e4SLinus Torvalds 	return 0;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static int rose_close(struct net_device *dev)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	netif_stop_queue(dev);
1301da177e4SLinus Torvalds 	rose_del_loopback_node((rose_address *)dev->dev_addr);
1311da177e4SLinus Torvalds 	return 0;
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds static int rose_xmit(struct sk_buff *skb, struct net_device *dev)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	struct net_device_stats *stats = netdev_priv(dev);
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	if (!netif_running(dev)) {
1391da177e4SLinus Torvalds 		printk(KERN_ERR "ROSE: rose_xmit - called when iface is down\n");
1401da177e4SLinus Torvalds 		return 1;
1411da177e4SLinus Torvalds 	}
1421da177e4SLinus Torvalds 	dev_kfree_skb(skb);
1431da177e4SLinus Torvalds 	stats->tx_errors++;
1441da177e4SLinus Torvalds 	return 0;
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds static struct net_device_stats *rose_get_stats(struct net_device *dev)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	return netdev_priv(dev);
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds 
1523b04dddeSStephen Hemminger static const struct header_ops rose_header_ops = {
1533b04dddeSStephen Hemminger 	.create	= rose_header,
1543b04dddeSStephen Hemminger 	.rebuild= rose_rebuild_header,
1553b04dddeSStephen Hemminger };
1563b04dddeSStephen Hemminger 
1571da177e4SLinus Torvalds void rose_setup(struct net_device *dev)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	dev->mtu		= ROSE_MAX_PACKET_SIZE - 2;
1601da177e4SLinus Torvalds 	dev->hard_start_xmit	= rose_xmit;
1611da177e4SLinus Torvalds 	dev->open		= rose_open;
1621da177e4SLinus Torvalds 	dev->stop		= rose_close;
1631da177e4SLinus Torvalds 
1643b04dddeSStephen Hemminger 	dev->header_ops		= &rose_header_ops;
1651da177e4SLinus Torvalds 	dev->hard_header_len	= AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
1661da177e4SLinus Torvalds 	dev->addr_len		= ROSE_ADDR_LEN;
1671da177e4SLinus Torvalds 	dev->type		= ARPHRD_ROSE;
1681da177e4SLinus Torvalds 	dev->set_mac_address    = rose_set_mac_address;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	/* New-style flags. */
171d2ce4bc3SRalf Baechle 	dev->flags		= IFF_NOARP;
1721da177e4SLinus Torvalds 	dev->get_stats = rose_get_stats;
1731da177e4SLinus Torvalds }
174