📄 sdla_chdlc.c
字号:
/* Initialize device driver entry points */ dev->open = &if_open; dev->stop = &if_close; dev->hard_header = &if_header; dev->rebuild_header = &if_rebuild_hdr; dev->hard_start_xmit = &if_send; dev->get_stats = &if_stats; dev->tx_timeout = &if_tx_timeout; dev->watchdog_timeo = TX_TIMEOUT; /* Initialize media-specific parameters */ dev->flags |= IFF_POINTOPOINT; dev->flags |= IFF_NOARP; /* Enable Mulitcasting if user selected */ if (chdlc_priv_area->mc == WANOPT_YES){ dev->flags |= IFF_MULTICAST; } if (chdlc_priv_area->true_if_encoding){ dev->type = ARPHRD_HDLC; /* This breaks the tcpdump */ }else{ dev->type = ARPHRD_PPP; } dev->mtu = card->wandev.mtu; /* for API usage, add the API header size to the requested MTU size */ if(card->u.c.usedby == API) { dev->mtu += sizeof(api_tx_hdr_t); } dev->hard_header_len = CHDLC_HDR_LEN; /* Initialize hardware parameters */ dev->irq = wandev->irq; dev->dma = wandev->dma; dev->base_addr = wandev->ioport; dev->mem_start = wandev->maddr; dev->mem_end = wandev->maddr + wandev->msize - 1; /* Set transmit buffer queue length * If too low packets will not be retransmitted * by stack. */ dev->tx_queue_len = 100; SET_MODULE_OWNER(dev); return 0;}/*============================================================================ * Open network interface. * o enable communications and interrupts. * o prevent module from unloading by incrementing use count * * Return 0 if O.k. or errno. */static int if_open(struct net_device* dev){ chdlc_private_area_t* chdlc_priv_area = dev->priv; sdla_t* card = chdlc_priv_area->card; struct timeval tv; int err = 0; /* Only one open per interface is allowed */ if (netif_running(dev)) return -EBUSY; /* Initialize the work queue entry */ chdlc_priv_area->tq_working=0; INIT_WORK(&chdlc_priv_area->common.wanpipe_work, (void *)(void *)chdlc_work, dev); /* Allocate and initialize BH circular buffer */ /* Add 1 to MAX_BH_BUFF so we don't have test with (MAX_BH_BUFF-1) */ chdlc_priv_area->bh_head = kmalloc((sizeof(bh_data_t)*(MAX_BH_BUFF+1)),GFP_ATOMIC); memset(chdlc_priv_area->bh_head,0,(sizeof(bh_data_t)*(MAX_BH_BUFF+1))); atomic_set(&chdlc_priv_area->bh_buff_used, 0); do_gettimeofday(&tv); chdlc_priv_area->router_start_time = tv.tv_sec; netif_start_queue(dev); wanpipe_open(card); /* TTY is configured during wanpipe_set_termios * call, not here */ if (card->tty_opt) return err; set_bit(0,&chdlc_priv_area->config_chdlc); chdlc_priv_area->config_chdlc_timeout=jiffies; /* Start the CHDLC configuration after 1sec delay. * This will give the interface initilization time * to finish its configuration */ mod_timer(&chdlc_priv_area->poll_delay_timer, jiffies + HZ); return err;}/*============================================================================ * Close network interface. * o if this is the last close, then disable communications and interrupts. * o reset flags. */static int if_close(struct net_device* dev){ chdlc_private_area_t* chdlc_priv_area = dev->priv; sdla_t* card = chdlc_priv_area->card; if (chdlc_priv_area->bh_head){ int i; struct sk_buff *skb; for (i=0; i<(MAX_BH_BUFF+1); i++){ skb = ((bh_data_t *)&chdlc_priv_area->bh_head[i])->skb; if (skb != NULL){ dev_kfree_skb_any(skb); } } kfree(chdlc_priv_area->bh_head); chdlc_priv_area->bh_head=NULL; } netif_stop_queue(dev); wanpipe_close(card); del_timer(&chdlc_priv_area->poll_delay_timer); return 0;}static void disable_comm (sdla_t *card){ SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags; if (card->u.c.comm_enabled){ chdlc_disable_comm_shutdown (card); }else{ flags->interrupt_info_struct.interrupt_permission = 0; } if (!tty_init_cnt) return; if (card->tty_opt){ struct serial_state * state; if (!(--tty_init_cnt)){ int e1; serial_driver.refcount=0; if ((e1 = tty_unregister_driver(&serial_driver))) printk("SERIAL: failed to unregister serial driver (%d)\n", e1); printk(KERN_INFO "%s: Unregistering TTY Driver, Major %i\n", card->devname,WAN_TTY_MAJOR); } card->tty=NULL; tty_card_map[card->tty_minor]=NULL; state = &rs_table[card->tty_minor]; memset(state, 0, sizeof(*state)); } return;}/*============================================================================ * Build media header. * * The trick here is to put packet type (Ethertype) into 'protocol' field of * the socket buffer, so that we don't forget it. If packet type is not * supported, set skb->protocol to 0 and discard packet later. * * Return: media header length. */static int if_header(struct sk_buff* skb, struct net_device* dev, unsigned short type, void* daddr, void* saddr, unsigned len){ skb->protocol = htons(type); return CHDLC_HDR_LEN;}/*============================================================================ * Handle transmit timeout event from netif watchdog */static void if_tx_timeout(struct net_device *dev){ chdlc_private_area_t* chan = dev->priv; sdla_t *card = chan->card; /* If our device stays busy for at least 5 seconds then we will * kick start the device by making dev->tbusy = 0. We expect * that our device never stays busy more than 5 seconds. So this * is only used as a last resort. */ ++card->wandev.stats.collisions; printk (KERN_INFO "%s: Transmit timed out on %s\n", card->devname,dev->name); netif_wake_queue (dev);}/*============================================================================ * Re-build media header. * * Return: 1 physical address resolved. * 0 physical address not resolved */static int if_rebuild_hdr (struct sk_buff *skb){ return 1;}/*============================================================================ * Send a packet on a network interface. * o set tbusy flag (marks start of the transmission) to block a timer-based * transmit from overlapping. * o check link state. If link is not up, then drop the packet. * o execute adapter send command. * o free socket buffer * * Return: 0 complete (socket buffer must be freed) * non-0 packet may be re-transmitted (tbusy must be set) * * Notes: * 1. This routine is called either by the protocol stack or by the "net * bottom half" (with interrupts enabled). * 2. Setting tbusy flag will inhibit further transmit requests from the * protocol stack and can be used for flow control with protocol layer. */static int if_send(struct sk_buff* skb, struct net_device* dev){ chdlc_private_area_t *chdlc_priv_area = dev->priv; sdla_t *card = chdlc_priv_area->card; SHARED_MEMORY_INFO_STRUCT *flags = card->u.c.flags; INTERRUPT_INFORMATION_STRUCT *chdlc_int = &flags->interrupt_info_struct; int udp_type = 0; unsigned long smp_flags; int err=0; netif_stop_queue(dev); if (skb == NULL){ /* If we get here, some higher layer thinks we've missed an * tx-done interrupt. */ printk(KERN_INFO "%s: interface %s got kicked!\n", card->devname, dev->name); netif_wake_queue(dev); return 0; } if (ntohs(skb->protocol) != htons(PVC_PROT)){ /* check the udp packet type */ udp_type = udp_pkt_type(skb, card); if (udp_type == UDP_CPIPE_TYPE){ if(store_udp_mgmt_pkt(UDP_PKT_FRM_STACK, card, skb, dev, chdlc_priv_area)){ chdlc_int->interrupt_permission |= APP_INT_ON_TIMER; } netif_start_queue(dev); return 0; } /* check to see if the source IP address is a broadcast or */ /* multicast IP address */ if(chk_bcast_mcast_addr(card, dev, skb)){ ++card->wandev.stats.tx_dropped; dev_kfree_skb_any(skb); netif_start_queue(dev); return 0; } } /* Lock the 508 Card: SMP is supported */ if(card->hw.type != SDLA_S514){ s508_lock(card,&smp_flags); } if(test_and_set_bit(SEND_CRIT, (void*)&card->wandev.critical)) { printk(KERN_INFO "%s: Critical in if_send: %lx\n", card->wandev.name,card->wandev.critical); ++card->wandev.stats.tx_dropped; netif_start_queue(dev); goto if_send_exit_crit; } if(card->u.c.state != WAN_CONNECTED){ ++card->wandev.stats.tx_dropped; netif_start_queue(dev); }else if(!skb->protocol){ ++card->wandev.stats.tx_errors; netif_start_queue(dev); }else { void* data = skb->data; unsigned len = skb->len; unsigned char attr; /* If it's an API packet pull off the API * header. Also check that the packet size * is larger than the API header */ if (card->u.c.usedby == API){ api_tx_hdr_t* api_tx_hdr; /* discard the frame if we are configured for */ /* 'receive only' mode or if there is no data */ if (card->u.c.receive_only || (len <= sizeof(api_tx_hdr_t))) { ++card->wandev.stats.tx_dropped; netif_start_queue(dev); goto if_send_exit_crit; } api_tx_hdr = (api_tx_hdr_t *)data; attr = api_tx_hdr->attr; data += sizeof(api_tx_hdr_t); len -= sizeof(api_tx_hdr_t); } if(chdlc_send(card, data, len)) { netif_stop_queue(dev); }else{ ++card->wandev.stats.tx_packets; card->wandev.stats.tx_bytes += len; netif_start_queue(dev); dev->trans_start = jiffies; } }if_send_exit_crit: if (!(err=netif_queue_stopped(dev))) { dev_kfree_skb_any(skb); }else{ chdlc_priv_area->tick_counter = jiffies; chdlc_int->interrupt_permission |= APP_INT_ON_TX_FRAME; } clear_bit(SEND_CRIT, (void*)&card->wandev.critical); if(card->hw.type != SDLA_S514){ s508_unlock(card,&smp_flags); } return err;}/*============================================================================ * Check to see if the packet to be transmitted contains a broadcast or * multicast source IP address. */static int chk_bcast_mcast_addr(sdla_t *card, struct net_device* dev, struct sk_buff *skb){ u32 src_ip_addr; u32 broadcast_ip_addr = 0; struct in_device *in_dev; /* read the IP source address from the outgoing packet */ src_ip_addr = *(u32 *)(skb->data + 12); /* read the IP broadcast address for the device */ in_dev = dev->ip_ptr; if(in_dev != NULL) { struct in_ifaddr *ifa= in_dev->ifa_list; if(ifa != NULL) broadcast_ip_addr = ifa->ifa_broadcast; else return 0; } /* check if the IP Source Address is a Broadcast address */ if((dev->flags & IFF_BROADCAST) && (src_ip_addr == broadcast_ip_addr)) { printk(KERN_INFO "%s: Broadcast Source Address silently discarded\n", card->devname); return 1; } /* check if the IP Source Address is a Multicast address */ if((ntohl(src_ip_addr) >= 0xE0000001) && (ntohl(src_ip_addr) <= 0xFFFFFFFE)) { printk(KERN_INFO "%s: Multicast Source Address silently discarded\n", card->devname); return 1; } return 0;}/*============================================================================ * Reply to UDP Management system. * Return length of reply. */static int reply_udp( unsigned char *data, unsigned int mbox_len ){ unsigned short len, udp_length, temp, ip_length; unsigned long ip_temp; int even_bound = 0; chdlc_udp_pkt_t *c_udp_pkt = (chdlc_udp_pkt_t *)data; /* Set length of packet */ len = sizeof(ip_pkt_t)+ sizeof(udp_pkt_t)+ sizeof(wp_mgmt_t)+ sizeof(cblock_t)+ sizeof(trace_info_t)+ mbox_len; /* fill in UDP reply */ c_udp_pkt->wp_mgmt.request_reply = UDPMGMT_REPLY; /* fill in UDP length */ udp_length = sizeof(udp_pkt_t)+ sizeof(wp_mgmt_t)+ sizeof(cblock_t)+ sizeof(trace_info_t)+ mbox_len; /* put it on an even boundary */ if ( udp_length & 0x0001 ) { udp_length += 1; len += 1; even_bound = 1; } temp = (udp_length<<8)|(udp_length>>8); c_udp_pkt->udp_pkt.udp_length = temp; /* swap UDP ports */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -