📄 cycx_x25.c
字号:
/** cycx_x25.c Cyclom 2X WAN Link Driver. X.25 module.** Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>** Copyright: (c) 1998-2003 Arnaldo Carvalho de Melo** Based on sdla_x25.c by Gene Kozin <genek@compuserve.com>** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU General Public License* as published by the Free Software Foundation; either version* 2 of the License, or (at your option) any later version.* ============================================================================* 2001/01/12 acme use dev_kfree_skb_irq on interrupt context* 2000/04/02 acme dprintk, cycx_debug* fixed the bug introduced in get_dev_by_lcn and* get_dev_by_dte_addr by the anonymous hacker* that converted this driver to softnet* 2000/01/08 acme cleanup* 1999/10/27 acme use ARPHRD_HWX25 so that the X.25 stack know* that we have a X.25 stack implemented in* firmware onboard* 1999/10/18 acme support for X.25 sockets in if_send,* beware: socket(AF_X25...) IS WORK IN PROGRESS,* TCP/IP over X.25 via wanrouter not affected,* working.* 1999/10/09 acme chan_disc renamed to chan_disconnect,* began adding support for X.25 sockets:* conf->protocol in new_if* 1999/10/05 acme fixed return E... to return -E...* 1999/08/10 acme serialized access to the card thru a spinlock* in x25_exec* 1999/08/09 acme removed per channel spinlocks* removed references to enable_tx_int* 1999/05/28 acme fixed nibble_to_byte, ackvc now properly treated* if_send simplified* 1999/05/25 acme fixed t1, t2, t21 & t23 configuration* use spinlocks instead of cli/sti in some points* 1999/05/24 acme finished the x25_get_stat function* 1999/05/23 acme dev->type = ARPHRD_X25 (tcpdump only works,* AFAIT, with ARPHRD_ETHER). This seems to be* needed to use socket(AF_X25)...* Now the config file must specify a peer media* address for svc channels over a crossover cable.* Removed hold_timeout from x25_channel_t,* not used.* A little enhancement in the DEBUG processing* 1999/05/22 acme go to DISCONNECTED in disconnect_confirm_intr,* instead of chan_disc.* 1999/05/16 marcelo fixed timer initialization in SVCs* 1999/01/05 acme x25_configure now get (most of) all* parameters...* 1999/01/05 acme pktlen now (correctly) uses log2 (value* configured)* 1999/01/03 acme judicious use of data types (u8, u16, u32, etc)* 1999/01/03 acme cyx_isr: reset dpmbase to acknowledge* indication (interrupt from cyclom 2x)* 1999/01/02 acme cyx_isr: first hackings...* 1999/01/0203 acme when initializing an array don't give less* elements than declared...* example: char send_cmd[6] = "?\xFF\x10";* you'll gonna lose a couple hours, 'cause your* brain won't admit that there's an error in the* above declaration... the side effect is that* memset is put into the unresolved symbols* instead of using the inline memset functions...* 1999/01/02 acme began chan_connect, chan_send, x25_send* 1998/12/31 acme x25_configure* this code can be compiled as non module* 1998/12/27 acme code cleanup* IPX code wiped out! let's decrease code* complexity for now, remember: I'm learning! :)* bps_to_speed_code OK* 1998/12/26 acme Minimal debug code cleanup* 1998/08/08 acme Initial version.*/#define CYCLOMX_X25_DEBUG 1#include <linux/ctype.h> /* isdigit() */#include <linux/errno.h> /* return codes */#include <linux/if_arp.h> /* ARPHRD_HWX25 */#include <linux/kernel.h> /* printk(), and other useful stuff */#include <linux/module.h>#include <linux/string.h> /* inline memset(), etc. */#include <linux/slab.h> /* kmalloc(), kfree() */#include <linux/stddef.h> /* offsetof(), etc. */#include <linux/wanrouter.h> /* WAN router definitions */#include <asm/byteorder.h> /* htons(), etc. */#include <linux/cyclomx.h> /* Cyclom 2X common user API definitions */#include <linux/cycx_x25.h> /* X.25 firmware API definitions */#include <net/x25device.h>/* Defines & Macros */#define CYCX_X25_MAX_CMD_RETRY 5#define CYCX_X25_CHAN_MTU 2048 /* unfragmented logical channel MTU *//* Data Structures *//* This is an extension of the 'struct net_device' we create for each network interface to keep the rest of X.25 channel-specific data. */struct cycx_x25_channel { /* This member must be first. */ struct net_device *slave; /* WAN slave */ char name[WAN_IFNAME_SZ+1]; /* interface name, ASCIIZ */ char addr[WAN_ADDRESS_SZ+1]; /* media address, ASCIIZ */ char *local_addr; /* local media address, ASCIIZ - svc thru crossover cable */ s16 lcn; /* logical channel number/conn.req.key*/ u8 link; struct timer_list timer; /* timer used for svc channel disc. */ u16 protocol; /* ethertype, 0 - multiplexed */ u8 svc; /* 0 - permanent, 1 - switched */ u8 state; /* channel state */ u8 drop_sequence; /* mark sequence for dropping */ u32 idle_tmout; /* sec, before disconnecting */ struct sk_buff *rx_skb; /* receive socket buffer */ struct cycx_device *card; /* -> owner */ struct net_device_stats ifstats;/* interface statistics */};/* Function Prototypes *//* WAN link driver entry points. These are called by the WAN router module. */static int cycx_wan_update(struct wan_device *wandev), cycx_wan_new_if(struct wan_device *wandev, struct net_device *dev, wanif_conf_t *conf), cycx_wan_del_if(struct wan_device *wandev, struct net_device *dev);/* Network device interface */static int cycx_netdevice_init(struct net_device *dev), cycx_netdevice_open(struct net_device *dev), cycx_netdevice_stop(struct net_device *dev), cycx_netdevice_hard_header(struct sk_buff *skb, struct net_device *dev, u16 type, void *daddr, void *saddr, unsigned len), cycx_netdevice_rebuild_header(struct sk_buff *skb), cycx_netdevice_hard_start_xmit(struct sk_buff *skb, struct net_device *dev);static struct net_device_stats * cycx_netdevice_get_stats(struct net_device *dev);/* Interrupt handlers */static void cycx_x25_irq_handler(struct cycx_device *card), cycx_x25_irq_tx(struct cycx_device *card, struct cycx_x25_cmd *cmd), cycx_x25_irq_rx(struct cycx_device *card, struct cycx_x25_cmd *cmd), cycx_x25_irq_log(struct cycx_device *card, struct cycx_x25_cmd *cmd), cycx_x25_irq_stat(struct cycx_device *card, struct cycx_x25_cmd *cmd), cycx_x25_irq_connect_confirm(struct cycx_device *card, struct cycx_x25_cmd *cmd), cycx_x25_irq_disconnect_confirm(struct cycx_device *card, struct cycx_x25_cmd *cmd), cycx_x25_irq_connect(struct cycx_device *card, struct cycx_x25_cmd *cmd), cycx_x25_irq_disconnect(struct cycx_device *card, struct cycx_x25_cmd *cmd), cycx_x25_irq_spurious(struct cycx_device *card, struct cycx_x25_cmd *cmd);/* X.25 firmware interface functions */static int cycx_x25_configure(struct cycx_device *card, struct cycx_x25_config *conf), cycx_x25_get_stats(struct cycx_device *card), cycx_x25_send(struct cycx_device *card, u8 link, u8 lcn, u8 bitm, int len, void *buf), cycx_x25_connect_response(struct cycx_device *card, struct cycx_x25_channel *chan), cycx_x25_disconnect_response(struct cycx_device *card, u8 link, u8 lcn);/* channel functions */static int cycx_x25_chan_connect(struct net_device *dev), cycx_x25_chan_send(struct net_device *dev, struct sk_buff *skb);static void cycx_x25_chan_disconnect(struct net_device *dev), cycx_x25_chan_send_event(struct net_device *dev, u8 event);/* Miscellaneous functions */static void cycx_x25_set_chan_state(struct net_device *dev, u8 state), cycx_x25_chan_timer(unsigned long d);static void nibble_to_byte(u8 *s, u8 *d, u8 len, u8 nibble), reset_timer(struct net_device *dev);static u8 bps_to_speed_code(u32 bps);static u8 cycx_log2(u32 n);static unsigned dec_to_uint(u8 *str, int len);static struct net_device *cycx_x25_get_dev_by_lcn(struct wan_device *wandev, s16 lcn);static struct net_device * cycx_x25_get_dev_by_dte_addr(struct wan_device *wandev, char *dte);#ifdef CYCLOMX_X25_DEBUGstatic void hex_dump(char *msg, unsigned char *p, int len);static void cycx_x25_dump_config(struct cycx_x25_config *conf);static void cycx_x25_dump_stats(struct cycx_x25_stats *stats);static void cycx_x25_dump_devs(struct wan_device *wandev);#else#define hex_dump(msg, p, len)#define cycx_x25_dump_config(conf)#define cycx_x25_dump_stats(stats)#define cycx_x25_dump_devs(wandev)#endif/* Public Functions *//* X.25 Protocol Initialization routine. * * This routine is called by the main Cyclom 2X module during setup. At this * point adapter is completely initialized and X.25 firmware is running. * o configure adapter * o initialize protocol-specific fields of the adapter data space. * * Return: 0 o.k. * < 0 failure. */int cycx_x25_wan_init(struct cycx_device *card, wandev_conf_t *conf){ struct cycx_x25_config cfg; /* Verify configuration ID */ if (conf->config_id != WANCONFIG_X25) { printk(KERN_INFO "%s: invalid configuration ID %u!\n", card->devname, conf->config_id); return -EINVAL; } /* Initialize protocol-specific fields */ card->mbox = card->hw.dpmbase + X25_MBOX_OFFS; card->u.x.connection_keys = 0; spin_lock_init(&card->u.x.lock); /* Configure adapter. Here we set reasonable defaults, then parse * device configuration structure and set configuration options. * Most configuration options are verified and corrected (if * necessary) since we can't rely on the adapter to do so and don't * want it to fail either. */ memset(&cfg, 0, sizeof(cfg)); cfg.link = 0; cfg.clock = conf->clocking == WANOPT_EXTERNAL ? 8 : 55; cfg.speed = bps_to_speed_code(conf->bps); cfg.n3win = 7; cfg.n2win = 2; cfg.n2 = 5; cfg.nvc = 1; cfg.npvc = 1; cfg.flags = 0x02; /* default = V35 */ cfg.t1 = 10; /* line carrier timeout */ cfg.t2 = 29; /* tx timeout */ cfg.t21 = 180; /* CALL timeout */ cfg.t23 = 180; /* CLEAR timeout */ /* adjust MTU */ if (!conf->mtu || conf->mtu >= 512) card->wandev.mtu = 512; else if (conf->mtu >= 256) card->wandev.mtu = 256; else if (conf->mtu >= 128) card->wandev.mtu = 128; else card->wandev.mtu = 64; cfg.pktlen = cycx_log2(card->wandev.mtu); if (conf->station == WANOPT_DTE) { cfg.locaddr = 3; /* DTE */ cfg.remaddr = 1; /* DCE */ } else { cfg.locaddr = 1; /* DCE */ cfg.remaddr = 3; /* DTE */ } if (conf->interface == WANOPT_RS232) cfg.flags = 0; /* FIXME just reset the 2nd bit */ if (conf->u.x25.hi_pvc) { card->u.x.hi_pvc = min_t(unsigned int, conf->u.x25.hi_pvc, 4095); card->u.x.lo_pvc = min_t(unsigned int, conf->u.x25.lo_pvc, card->u.x.hi_pvc); } if (conf->u.x25.hi_svc) { card->u.x.hi_svc = min_t(unsigned int, conf->u.x25.hi_svc, 4095); card->u.x.lo_svc = min_t(unsigned int, conf->u.x25.lo_svc, card->u.x.hi_svc); } if (card->u.x.lo_pvc == 255) cfg.npvc = 0; else cfg.npvc = card->u.x.hi_pvc - card->u.x.lo_pvc + 1; cfg.nvc = card->u.x.hi_svc - card->u.x.lo_svc + 1 + cfg.npvc; if (conf->u.x25.hdlc_window) cfg.n2win = min_t(unsigned int, conf->u.x25.hdlc_window, 7); if (conf->u.x25.pkt_window) cfg.n3win = min_t(unsigned int, conf->u.x25.pkt_window, 7); if (conf->u.x25.t1) cfg.t1 = min_t(unsigned int, conf->u.x25.t1, 30); if (conf->u.x25.t2) cfg.t2 = min_t(unsigned int, conf->u.x25.t2, 30); if (conf->u.x25.t11_t21) cfg.t21 = min_t(unsigned int, conf->u.x25.t11_t21, 30); if (conf->u.x25.t13_t23) cfg.t23 = min_t(unsigned int, conf->u.x25.t13_t23, 30); if (conf->u.x25.n2) cfg.n2 = min_t(unsigned int, conf->u.x25.n2, 30); /* initialize adapter */ if (cycx_x25_configure(card, &cfg)) return -EIO; /* Initialize protocol-specific fields of adapter data space */ card->wandev.bps = conf->bps; card->wandev.interface = conf->interface; card->wandev.clocking = conf->clocking; card->wandev.station = conf->station; card->isr = cycx_x25_irq_handler; card->exec = NULL; card->wandev.update = cycx_wan_update; card->wandev.new_if = cycx_wan_new_if; card->wandev.del_if = cycx_wan_del_if; card->wandev.state = WAN_DISCONNECTED; return 0;}/* WAN Device Driver Entry Points *//* Update device status & statistics. */static int cycx_wan_update(struct wan_device *wandev){ /* sanity checks */ if (!wandev || !wandev->private) return -EFAULT; if (wandev->state == WAN_UNCONFIGURED) return -ENODEV; cycx_x25_get_stats(wandev->private); return 0;}/* Create new logical channel. * This routine is called by the router when ROUTER_IFNEW IOCTL is being * handled. * o parse media- and hardware-specific configuration * o make sure that a new channel can be created * o allocate resources, if necessary * o prepare network device structure for registration. * * Return: 0 o.k. * < 0 failure (channel will not be created) */static int cycx_wan_new_if(struct wan_device *wandev, struct net_device *dev, wanif_conf_t *conf){ struct cycx_device *card = wandev->private; struct cycx_x25_channel *chan; int err = 0; if (!conf->name[0] || strlen(conf->name) > WAN_IFNAME_SZ) { printk(KERN_INFO "%s: invalid interface name!\n", card->devname); return -EINVAL; } /* allocate and initialize private data */ chan = kmalloc(sizeof(struct cycx_x25_channel), GFP_KERNEL); if (!chan) return -ENOMEM; memset(chan, 0, sizeof(*chan)); strcpy(chan->name, conf->name); chan->card = card; chan->link = conf->port; chan->protocol = conf->protocol ? ETH_P_X25 : ETH_P_IP; chan->rx_skb = NULL; /* only used in svc connected thru crossover cable */ chan->local_addr = NULL; if (conf->addr[0] == '@') { /* SVC */ int len = strlen(conf->local_addr); if (len) { if (len > WAN_ADDRESS_SZ) { printk(KERN_ERR "%s: %s local addr too long!\n", wandev->name, chan->name); kfree(chan); return -EINVAL; } else { chan->local_addr = kmalloc(len + 1, GFP_KERNEL); if (!chan->local_addr) { kfree(chan); return -ENOMEM; } } strncpy(chan->local_addr, conf->local_addr, WAN_ADDRESS_SZ); } chan->svc = 1; strncpy(chan->addr, &conf->addr[1], WAN_ADDRESS_SZ); init_timer(&chan->timer); chan->timer.function = cycx_x25_chan_timer; chan->timer.data = (unsigned long)dev; /* Set channel timeouts (default if not specified) */ chan->idle_tmout = conf->idle_timeout ? conf->idle_timeout : 90; } else if (isdigit(conf->addr[0])) { /* PVC */ s16 lcn = dec_to_uint(conf->addr, 0); if (lcn >= card->u.x.lo_pvc && lcn <= card->u.x.hi_pvc) chan->lcn = lcn; else { printk(KERN_ERR "%s: PVC %u is out of range on interface %s!\n", wandev->name, lcn, chan->name); err = -EINVAL; } } else { printk(KERN_ERR "%s: invalid media address on interface %s!\n", wandev->name, chan->name); err = -EINVAL; } if (err) { kfree(chan->local_addr); kfree(chan); return err; } /* prepare network device data space for registration */ strcpy(dev->name, chan->name); dev->init = cycx_netdevice_init; dev->priv = chan; return 0;}/* Delete logical channel. */static int cycx_wan_del_if(struct wan_device *wandev, struct net_device *dev){ if (dev->priv) { struct cycx_x25_channel *chan = dev->priv; if (chan->svc) { kfree(chan->local_addr); if (chan->state == WAN_CONNECTED) del_timer(&chan->timer); } kfree(chan); dev->priv = NULL; } return 0;}/* Network Device Interface *//* Initialize Linux network interface. * * This routine is called only once for each interface, during Linux network * interface registration. Returning anything but zero will fail interface * registration. */static int cycx_netdevice_init(struct net_device *dev){ struct cycx_x25_channel *chan = dev->priv; struct cycx_device *card = chan->card; struct wan_device *wandev = &card->wandev; /* Initialize device driver entry points */ dev->open = cycx_netdevice_open; dev->stop = cycx_netdevice_stop; dev->hard_header = cycx_netdevice_hard_header; dev->rebuild_header = cycx_netdevice_rebuild_header; dev->hard_start_xmit = cycx_netdevice_hard_start_xmit; dev->get_stats = cycx_netdevice_get_stats; /* Initialize media-specific parameters */ dev->mtu = CYCX_X25_CHAN_MTU; dev->type = ARPHRD_HWX25; /* ARP h/w type */ dev->hard_header_len = 0; /* media header length */ dev->addr_len = 0; /* hardware address length */ if (!chan->svc) *(u16*)dev->dev_addr = htons(chan->lcn); /* Initialize hardware parameters (just for reference) */ dev->irq = wandev->irq; dev->dma = wandev->dma; dev->base_addr = wandev->ioport; dev->mem_start = (unsigned long)wandev->maddr; dev->mem_end = (unsigned long)(wandev->maddr + wandev->msize - 1); dev->flags |= IFF_NOARP; /* Set transmit buffer queue length */ dev->tx_queue_len = 10; SET_MODULE_OWNER(dev); /* Initialize socket buffers */ cycx_x25_set_chan_state(dev, WAN_DISCONNECTED); return 0;}/* Open network interface. * o prevent module from unloading by incrementing use count * o if link is disconnected then initiate connection * * Return 0 if O.k. or errno. */static int cycx_netdevice_open(struct net_device *dev){ if (netif_running(dev)) return -EBUSY; /* only one open is allowed */ netif_start_queue(dev); return 0;}/* Close network interface. * o reset flags. * o if there's no more open channels then disconnect physical link. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -