6pack.c
来自「powerpc内核mpc8241linux系统下net驱动程序」· C语言 代码 · 共 1,130 行 · 第 1/2 页
C
1,130 行
/* * 6pack.c This module implements the 6pack protocol for kernel-based * devices like TTY. It interfaces between a raw TTY and the * kernel's AX.25 protocol layers. * * Version: @(#)6pack.c 0.3.0 04/07/98 * * Authors: Andreas K鰊sgen <ajk@iehk.rwth-aachen.de> * * Quite a lot of stuff "stolen" by J鰎g Reuter from slip.c, written by * * Laurence Culhane, <loz@holmes.demon.co.uk> * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org> * */ #include <linux/config.h>#include <linux/module.h>#include <asm/system.h>#include <asm/uaccess.h>#include <asm/bitops.h>#include <linux/string.h>#include <linux/mm.h>#include <linux/interrupt.h>#include <linux/in.h>#include <linux/tty.h>#include <linux/errno.h>#include <linux/netdevice.h>#include <linux/timer.h>#include <net/ax25.h>#include <linux/etherdevice.h>#include <linux/skbuff.h>#include <linux/rtnetlink.h>#include <linux/if_arp.h>#include <linux/if_slip.h>#include <linux/init.h>#include <linux/ip.h>#include <linux/tcp.h>/* #include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <unistd.h> */#include "6pack.h"typedef unsigned char byte;typedef struct sixpack_ctrl { char if_name[8]; /* "sp0\0" .. "sp99999\0" */ struct sixpack ctrl; /* 6pack things */ struct device dev; /* the device */} sixpack_ctrl_t;static sixpack_ctrl_t **sixpack_ctrls = NULL;int sixpack_maxdev = SIXP_NRUNIT; /* Can be overridden with insmod! */static struct tty_ldisc sp_ldisc;static void sp_start_tx_timer(struct sixpack *);static void sp_xmit_on_air(unsigned long);static void resync_tnc(unsigned long);void sixpack_decode(struct sixpack *, unsigned char[], int);int encode_sixpack(unsigned char *, unsigned char *, int, unsigned char);void decode_prio_command(byte, struct sixpack *);void decode_std_command(byte, struct sixpack *);void decode_data(byte, struct sixpack *);static int tnc_init(struct sixpack *);/* Find a free 6pack channel, and link in this `tty' line. */static inline struct sixpack *sp_alloc(void){ sixpack_ctrl_t *spp = NULL; int i; if (sixpack_ctrls == NULL) return NULL; /* Master array missing ! */ for (i = 0; i < sixpack_maxdev; i++) { spp = sixpack_ctrls[i]; if (spp == NULL) break; if (!test_and_set_bit(SIXPF_INUSE, &spp->ctrl.flags)) break; } /* Too many devices... */ if (i >= sixpack_maxdev) return NULL; /* If no channels are available, allocate one */ if (!spp && (sixpack_ctrls[i] = (sixpack_ctrl_t *)kmalloc(sizeof(sixpack_ctrl_t), GFP_KERNEL)) != NULL) { spp = sixpack_ctrls[i]; memset(spp, 0, sizeof(sixpack_ctrl_t)); /* Initialize channel control data */ set_bit(SIXPF_INUSE, &spp->ctrl.flags); spp->ctrl.tty = NULL; sprintf(spp->if_name, "sp%d", i); spp->dev.name = spp->if_name; spp->dev.base_addr = i; spp->dev.priv = (void*)&(spp->ctrl); spp->dev.next = NULL; spp->dev.init = sixpack_init; } if (spp != NULL) { /* register device so that it can be ifconfig'ed */ /* sixpack_init() will be called as a side-effect */ /* SIDE-EFFECT WARNING: sixpack_init() CLEARS spp->ctrl ! */ if (register_netdev(&(spp->dev)) == 0) { set_bit(SIXPF_INUSE, &spp->ctrl.flags); spp->ctrl.dev = &(spp->dev); spp->dev.priv = (void*)&(spp->ctrl); return (&(spp->ctrl)); } else { clear_bit(SIXPF_INUSE,&(spp->ctrl.flags)); printk(KERN_WARNING "sp_alloc() - register_netdev() failure.\n"); } } return NULL;}/* Free a 6pack channel. */static inline voidsp_free(struct sixpack *sp){ /* Free all 6pack frame buffers. */ if (sp->rbuff) kfree(sp->rbuff); sp->rbuff = NULL; if (sp->xbuff) { kfree(sp->xbuff); } sp->xbuff = NULL; if (!test_and_clear_bit(SIXPF_INUSE, &sp->flags)) { printk(KERN_WARNING "%s: sp_free for already free unit.\n", sp->dev->name); }}/* Set the "sending" flag. */static inline voidsp_lock(struct sixpack *sp){ if (test_and_set_bit(0, (void *) &sp->dev->tbusy)) printk(KERN_WARNING "%s: trying to lock already locked device!\n", sp->dev->name);}/* Clear the "sending" flag. */static inline voidsp_unlock(struct sixpack *sp){ if (!test_and_clear_bit(0, (void *)&sp->dev->tbusy)) printk(KERN_WARNING "%s: trying to unlock already unlocked device!\n", sp->dev->name);}/* Send one completely decapsulated IP datagram to the IP layer. *//* This is the routine that sends the received data to the kernel AX.25. 'cmd' is the KISS command. For AX.25 data, it is zero. */static voidsp_bump(struct sixpack *sp, char cmd){ struct sk_buff *skb; int count; unsigned char *ptr; count = sp->rcount+1; sp->rx_bytes+=count; skb = dev_alloc_skb(count); if (skb == NULL) { printk(KERN_DEBUG "%s: memory squeeze, dropping packet.\n", sp->dev->name); sp->rx_dropped++; return; } skb->dev = sp->dev; ptr = skb_put(skb, count); *ptr++ = cmd; /* KISS command */ memcpy(ptr, (sp->cooked_buf)+1, count); skb->mac.raw=skb->data; skb->protocol=htons(ETH_P_AX25); netif_rx(skb); sp->rx_packets++;}/* ----------------------------------------------------------------------- *//* Encapsulate one AX.25 frame and stuff into a TTY queue. */static voidsp_encaps(struct sixpack *sp, unsigned char *icp, int len){ unsigned char *p; int actual, count; if (len > sp->mtu) /* sp->mtu = AX25_MTU = max. PACLEN = 256 */ { len = sp->mtu; printk(KERN_DEBUG "%s: truncating oversized transmit packet!\n", sp->dev->name); sp->tx_dropped++; sp_unlock(sp); return; } p = icp; if (p[0] > 5) { printk(KERN_DEBUG "%s: invalid KISS command -- dropped\n", sp->dev->name); sp_unlock(sp); return; } if ((p[0] != 0) && (len > 2)) { printk(KERN_DEBUG "%s: KISS control packet too long -- dropped\n", sp->dev->name); sp_unlock(sp); return; } if ((p[0] == 0) && (len < 15)) { printk(KERN_DEBUG "%s: bad AX.25 packet to transmit -- dropped\n", sp->dev->name); sp_unlock(sp); sp->tx_dropped++; return; } count = encode_sixpack(p, (unsigned char *) sp->xbuff, len, sp->tx_delay); sp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP); switch(p[0]) { case 1: sp->tx_delay = p[1]; return; case 2: sp->persistance = p[1]; return; case 3: sp->slottime = p[1]; return; case 4: /* ignored */ return; case 5: sp->duplex = p[1]; return; } if (p[0] == 0) { /* in case of fullduplex or DAMA operation, we don't take care about the state of the DCD or of any timers, as the determination of the correct time to send is the job of the AX.25 layer. We send immediately after data has arrived. */ if (sp->duplex == 1){ sp->led_state = 0x70; sp->tty->driver.write(sp->tty, 0, &(sp->led_state), 1); sp->tx_enable = 1; actual = sp->tty->driver.write(sp->tty, 0, sp->xbuff, count); sp->xleft = count - actual; sp->xhead = sp->xbuff + actual; sp->led_state = 0x60; sp->tty->driver.write(sp->tty, 0, &(sp->led_state), 1); } else { sp->xleft = count; sp->xhead = sp->xbuff; sp->status2 = count; if (sp->duplex == 0) sp_start_tx_timer(sp); } }}/* * Called by the TTY driver when there's room for more data. If we have * more packets to send, we send them here. */static void sixpack_write_wakeup(struct tty_struct *tty){ int actual; struct sixpack *sp = (struct sixpack *) tty->disc_data; /* First make sure we're connected. */ if (!sp || sp->magic != SIXPACK_MAGIC || !sp->dev->start) { return; } if (sp->xleft <= 0) { /* Now serial buffer is almost free & we can start * transmission of another packet */ sp->tx_packets++; tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP); sp_unlock(sp); sp->tx_enable = 0; mark_bh(NET_BH); return; } if (sp->tx_enable == 1) { actual = tty->driver.write(tty, 0, sp->xhead, sp->xleft); sp->xleft -= actual; sp->xhead += actual; }}/* ----------------------------------------------------------------------- *//* Encapsulate an IP datagram and kick it into a TTY queue. */static intsp_xmit(struct sk_buff *skb, struct device *dev){ struct sixpack *sp = (struct sixpack*)(dev->priv); if (!dev->start) { printk(KERN_WARNING "%s: xmit call when iface is down\n", dev->name); return 1; } if (dev->tbusy) return 1; /* We were not busy, so we are now... :-) */ if (skb != NULL) { sp_lock(sp); sp->tx_bytes+=skb->len; /*---2.1.x---*/ sp_encaps(sp, skb->data, skb->len); dev_kfree_skb(skb); } return 0;}/* #endif *//* perform the persistence/slottime algorithm for CSMA access. If the persistence check was successful, write the data to the serial driver. Note that in case of DAMA operation, the data is not sent here. */static void sp_xmit_on_air(unsigned long channel){ struct sixpack *sp = (struct sixpack *) channel; int actual; static unsigned char random; random = random * 17 + 41; if (((sp->status1 & SIXP_DCD_MASK) == 0) && (random < sp->persistance)) { sp->led_state = 0x70; sp->tty->driver.write(sp->tty, 0, &(sp->led_state),1); sp->tx_enable = 1; actual = sp->tty->driver.write(sp->tty, 0, sp->xbuff, sp->status2); sp->xleft -= actual; sp->xhead += actual; sp->led_state = 0x60; sp->tty->driver.write(sp->tty, 0, &(sp->led_state),1); sp->status2 = 0; } else sp_start_tx_timer(sp);} /* sp_xmit *//* #if defined(CONFIG_6PACK) || defined(CONFIG_6PACK_MODULE) *//* Return the frame type ID */static int sp_header(struct sk_buff *skb, struct device *dev, unsigned short type, void *daddr, void *saddr, unsigned len){#ifdef CONFIG_INET if (type != htons(ETH_P_AX25)) return ax25_encapsulate(skb, dev, type, daddr, saddr, len);#endif return 0;}static int sp_rebuild_header(struct sk_buff *skb){#ifdef CONFIG_INET return ax25_rebuild_header(skb);#else return 0;#endif}/* #endif */ /* CONFIG_{AX25,AX25_MODULE} *//* Open the low-level part of the 6pack channel. */static intsp_open(struct device *dev){ struct sixpack *sp = (struct sixpack*)(dev->priv); unsigned long len; if (sp->tty == NULL) return -ENODEV; /* * Allocate the 6pack frame buffers: * * rbuff Receive buffer. * xbuff Transmit buffer. * cbuff Temporary compression buffer. */ /* !!! length of the buffers. MTU is IP MTU, not PACLEN! */ len = dev->mtu * 2; sp->rbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL); if (sp->rbuff == NULL) return -ENOMEM; sp->xbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL); if (sp->xbuff == NULL) { kfree(sp->rbuff); return -ENOMEM; } sp->mtu = AX25_MTU + 73; sp->buffsize = len; sp->rcount = 0; sp->rx_count = 0; sp->rx_count_cooked = 0; sp->xleft = 0; sp->flags &= (1 << SIXPF_INUSE); /* Clear ESCAPE & ERROR flags */ sp->duplex = 0; sp->tx_delay = SIXP_TXDELAY; sp->persistance = SIXP_PERSIST; sp->slottime = SIXP_SLOTTIME; sp->led_state = 0x60; sp->status = 1; sp->status1 = 1; sp->status2 = 0; sp->tnc_ok = 0; sp->tx_enable = 0; dev->tbusy = 0; dev->start = 1; init_timer(&sp->tx_t); init_timer(&sp->resync_t); return 0;}/* Close the low-level part of the 6pack channel. */static intsp_close(struct device *dev){ struct sixpack *sp = (struct sixpack*)(dev->priv); if (sp->tty == NULL) { return -EBUSY; } sp->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP); dev->tbusy = 1; dev->start = 0; return 0;}static intsixpack_receive_room(struct tty_struct *tty){ return 65536; /* We can handle an infinite amount of data. :-) */}/* !!! receive state machine *//* * Handle the 'receiver data ready' interrupt. * This function is called by the 'tty_io' module in the kernel when * a block of 6pack data has been received, which can now be decapsulated * and sent on to some IP layer for further processing. */static voidsixpack_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count){ unsigned char buf[512]; unsigned long flags; int count1; struct sixpack *sp = (struct sixpack *) tty->disc_data; if (!sp || sp->magic != SIXPACK_MAGIC || !sp->dev->start || !count) return; save_flags(flags); cli(); memcpy(buf, cp, count<sizeof(buf)? count:sizeof(buf)); restore_flags(flags); /* Read the characters out of the buffer */ count1 = count; while(count) { count--; if (fp && *fp++) { if (!test_and_set_bit(SIXPF_ERROR, &sp->flags)) { sp->rx_errors++; } continue; } } sixpack_decode(sp, buf, count1);}/* * Open the high-level part of the 6pack channel. * This function is called by the TTY module when the * 6pack line discipline is called for. Because we are * sure the tty line exists, we only have to link it to * a free 6pcack channel... */static intsixpack_open(struct tty_struct *tty){ struct sixpack *sp = (struct sixpack *) tty->disc_data; int err; /* First make sure we're not already connected. */ if (sp && sp->magic == SIXPACK_MAGIC) return -EEXIST; /* OK. Find a free 6pack channel to use. */ if ((sp = sp_alloc()) == NULL) return -ENFILE; sp->tty = tty; tty->disc_data = sp; if (tty->driver.flush_buffer) tty->driver.flush_buffer(tty); if (tty->ldisc.flush_buffer) tty->ldisc.flush_buffer(tty); /* Restore default settings */ sp->dev->type = ARPHRD_AX25;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?