xref: /openbmc/linux/net/netrom/nr_dev.c (revision f8b04488b060b155f8b6769aa70412c3630b03f0)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds #include <linux/module.h>
71da177e4SLinus Torvalds #include <linux/proc_fs.h>
81da177e4SLinus Torvalds #include <linux/kernel.h>
91da177e4SLinus Torvalds #include <linux/interrupt.h>
101da177e4SLinus Torvalds #include <linux/fs.h>
111da177e4SLinus Torvalds #include <linux/types.h>
121da177e4SLinus Torvalds #include <linux/sysctl.h>
131da177e4SLinus Torvalds #include <linux/string.h>
141da177e4SLinus Torvalds #include <linux/socket.h>
151da177e4SLinus Torvalds #include <linux/errno.h>
161da177e4SLinus Torvalds #include <linux/fcntl.h>
171da177e4SLinus Torvalds #include <linux/in.h>
181da177e4SLinus Torvalds #include <linux/if_ether.h>	/* For the statistics structure. */
195a0e3ad6STejun Heo #include <linux/slab.h>
20dc8e5416SFabian Frederick #include <linux/uaccess.h>
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include <asm/io.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <linux/inet.h>
251da177e4SLinus Torvalds #include <linux/netdevice.h>
261da177e4SLinus Torvalds #include <linux/etherdevice.h>
271da177e4SLinus Torvalds #include <linux/if_arp.h>
281da177e4SLinus Torvalds #include <linux/skbuff.h>
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include <net/ip.h>
311da177e4SLinus Torvalds #include <net/arp.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include <net/ax25.h>
341da177e4SLinus Torvalds #include <net/netrom.h>
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /*
371da177e4SLinus Torvalds  *	Only allow IP over NET/ROM frames through if the netrom device is up.
381da177e4SLinus Torvalds  */
391da177e4SLinus Torvalds 
nr_rx_ip(struct sk_buff * skb,struct net_device * dev)401da177e4SLinus Torvalds int nr_rx_ip(struct sk_buff *skb, struct net_device *dev)
411da177e4SLinus Torvalds {
42b51414b6SStephen Hemminger 	struct net_device_stats *stats = &dev->stats;
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds 	if (!netif_running(dev)) {
456ddcf626SRalf Baechle 		stats->rx_dropped++;
461da177e4SLinus Torvalds 		return 0;
471da177e4SLinus Torvalds 	}
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 	stats->rx_packets++;
501da177e4SLinus Torvalds 	stats->rx_bytes += skb->len;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	/* Spoof incoming device */
551da177e4SLinus Torvalds 	skb->dev      = dev;
56c6e6ca71SDavid S. Miller 	skb->mac_header = skb->network_header;
57c1d2bbe1SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
581da177e4SLinus Torvalds 	skb->pkt_type = PACKET_HOST;
591da177e4SLinus Torvalds 
6098a82febSRalf Baechle 	netif_rx(skb);
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	return 1;
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds 
nr_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,const void * daddr,const void * saddr,unsigned int len)653b04dddeSStephen Hemminger static int nr_header(struct sk_buff *skb, struct net_device *dev,
663b04dddeSStephen Hemminger 		     unsigned short type,
6795c96174SEric Dumazet 		     const void *daddr, const void *saddr, unsigned int len)
681da177e4SLinus Torvalds {
691da177e4SLinus Torvalds 	unsigned char *buff = skb_push(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	memcpy(buff, (saddr != NULL) ? saddr : dev->dev_addr, dev->addr_len);
721da177e4SLinus Torvalds 	buff[6] &= ~AX25_CBIT;
731da177e4SLinus Torvalds 	buff[6] &= ~AX25_EBIT;
741da177e4SLinus Torvalds 	buff[6] |= AX25_SSSID_SPARE;
751da177e4SLinus Torvalds 	buff    += AX25_ADDR_LEN;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	if (daddr != NULL)
781da177e4SLinus Torvalds 		memcpy(buff, daddr, dev->addr_len);
791da177e4SLinus Torvalds 	buff[6] &= ~AX25_CBIT;
801da177e4SLinus Torvalds 	buff[6] |= AX25_EBIT;
811da177e4SLinus Torvalds 	buff[6] |= AX25_SSSID_SPARE;
821da177e4SLinus Torvalds 	buff    += AX25_ADDR_LEN;
831da177e4SLinus Torvalds 
84*5731369aSJason Xing 	*buff++ = READ_ONCE(sysctl_netrom_network_ttl_initialiser);
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	*buff++ = NR_PROTO_IP;
871da177e4SLinus Torvalds 	*buff++ = NR_PROTO_IP;
881da177e4SLinus Torvalds 	*buff++ = 0;
891da177e4SLinus Torvalds 	*buff++ = 0;
901da177e4SLinus Torvalds 	*buff++ = NR_PROTOEXT;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	if (daddr != NULL)
931da177e4SLinus Torvalds 		return 37;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	return -37;
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
nr_set_mac_address(struct net_device * dev,void * addr)9881dcd169SRalf Baechle static int __must_check nr_set_mac_address(struct net_device *dev, void *addr)
991da177e4SLinus Torvalds {
1001da177e4SLinus Torvalds 	struct sockaddr *sa = addr;
10181dcd169SRalf Baechle 	int err;
1021da177e4SLinus Torvalds 
10381dcd169SRalf Baechle 	if (!memcmp(dev->dev_addr, sa->sa_data, dev->addr_len))
10481dcd169SRalf Baechle 		return 0;
10581dcd169SRalf Baechle 
10681dcd169SRalf Baechle 	if (dev->flags & IFF_UP) {
10781dcd169SRalf Baechle 		err = ax25_listen_register((ax25_address *)sa->sa_data, NULL);
10881dcd169SRalf Baechle 		if (err)
10981dcd169SRalf Baechle 			return err;
11081dcd169SRalf Baechle 
111c045ad2cSJakub Kicinski 		ax25_listen_release((const ax25_address *)dev->dev_addr, NULL);
11281dcd169SRalf Baechle 	}
1131da177e4SLinus Torvalds 
114ea52a0b5SJakub Kicinski 	dev_addr_set(dev, sa->sa_data);
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	return 0;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
nr_open(struct net_device * dev)1191da177e4SLinus Torvalds static int nr_open(struct net_device *dev)
1201da177e4SLinus Torvalds {
12181dcd169SRalf Baechle 	int err;
12281dcd169SRalf Baechle 
123c045ad2cSJakub Kicinski 	err = ax25_listen_register((const ax25_address *)dev->dev_addr, NULL);
12481dcd169SRalf Baechle 	if (err)
12581dcd169SRalf Baechle 		return err;
12681dcd169SRalf Baechle 
1271da177e4SLinus Torvalds 	netif_start_queue(dev);
12881dcd169SRalf Baechle 
1291da177e4SLinus Torvalds 	return 0;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
nr_close(struct net_device * dev)1321da177e4SLinus Torvalds static int nr_close(struct net_device *dev)
1331da177e4SLinus Torvalds {
134c045ad2cSJakub Kicinski 	ax25_listen_release((const ax25_address *)dev->dev_addr, NULL);
1351da177e4SLinus Torvalds 	netif_stop_queue(dev);
1361da177e4SLinus Torvalds 	return 0;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
nr_xmit(struct sk_buff * skb,struct net_device * dev)13936e4d64aSStephen Hemminger static netdev_tx_t nr_xmit(struct sk_buff *skb, struct net_device *dev)
1401da177e4SLinus Torvalds {
141b51414b6SStephen Hemminger 	struct net_device_stats *stats = &dev->stats;
142b88a762bSRalf Baechle 	unsigned int len = skb->len;
1433f2aadd0SRalf Baechle 
1443f2aadd0SRalf Baechle 	if (!nr_route_frame(skb, NULL)) {
1453f2aadd0SRalf Baechle 		kfree_skb(skb);
1461da177e4SLinus Torvalds 		stats->tx_errors++;
1476ed10654SPatrick McHardy 		return NETDEV_TX_OK;
1481da177e4SLinus Torvalds 	}
1491da177e4SLinus Torvalds 
1503f2aadd0SRalf Baechle 	stats->tx_packets++;
1513f2aadd0SRalf Baechle 	stats->tx_bytes += len;
1523f2aadd0SRalf Baechle 
1536ed10654SPatrick McHardy 	return NETDEV_TX_OK;
1543f2aadd0SRalf Baechle }
1553f2aadd0SRalf Baechle 
1563b04dddeSStephen Hemminger static const struct header_ops nr_header_ops = {
1573b04dddeSStephen Hemminger 	.create	= nr_header,
1583b04dddeSStephen Hemminger };
1593b04dddeSStephen Hemminger 
1600f6c5c8eSStephen Hemminger static const struct net_device_ops nr_netdev_ops = {
1610f6c5c8eSStephen Hemminger 	.ndo_open		= nr_open,
1620f6c5c8eSStephen Hemminger 	.ndo_stop		= nr_close,
1630f6c5c8eSStephen Hemminger 	.ndo_start_xmit		= nr_xmit,
1640f6c5c8eSStephen Hemminger 	.ndo_set_mac_address    = nr_set_mac_address,
1650f6c5c8eSStephen Hemminger };
1663b04dddeSStephen Hemminger 
nr_setup(struct net_device * dev)1671da177e4SLinus Torvalds void nr_setup(struct net_device *dev)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	dev->mtu		= NR_MAX_PACKET_SIZE;
1700f6c5c8eSStephen Hemminger 	dev->netdev_ops		= &nr_netdev_ops;
1713b04dddeSStephen Hemminger 	dev->header_ops		= &nr_header_ops;
1721da177e4SLinus Torvalds 	dev->hard_header_len	= NR_NETWORK_LEN + NR_TRANSPORT_LEN;
1731da177e4SLinus Torvalds 	dev->addr_len		= AX25_ADDR_LEN;
1741da177e4SLinus Torvalds 	dev->type		= ARPHRD_NETROM;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	/* New-style flags. */
17772377291SRalf Baechle 	dev->flags		= IFF_NOARP;
1781da177e4SLinus Torvalds }
179