"Linux Network Kernel" common function summary __oracle

Source: Internet
Author: User
Tags get ip goto

The following functions are all based on the 3.10.90 version of the kernel, and may be different for older or newer kernels. macros for log output Log macros are used in exactly the same way as PRINTK can be customized tag, used in the DMESG output log to filter out the information belongs to this module can be controlled by the debug macro output log, when the log is closed, all log code will not be compiled

#define DEBUG 1
#define MOD_TAG "Custom TAG"
#if debug
#define LOG (FMT, args ...) \
do{\
    printk ("[%s ]: ", mod_tag); \
    PRINTK (FMT, # #args); \
    printk (" \ n "); \
}while (0)
#else
#define LOG (msg)
# endif
get the Saddr and daddr in Sk_buff

In the acquisition of IP, it is important to determine at this point in the Sk_buff IP.
Because this function may be called in many functions, in order to distinguish the caller, add the caller parameter, you can pass in a string or use __function__ directly.

static void Show_skb_ip (struct sk_buff *skb, const char *caller)
{
    LOG ("Called by%s", caller);
    struct IPHDR *iph = NULL;
    unsigned char *ip = NULL;
    IPH = IP_HDR (SKB);
    ip = (unsigned char*) &iph->daddr;
    LOG ("daddr:%d.%d.%d.%d", Ip[0], ip[1], ip[2], ip[3]);
    ip = (unsigned char*) &iph->saddr;
    LOG ("saddr:%d.%d.%d.%d", Ip[0], ip[1], ip[2], ip[3]);
Get IP address of network card device
Static __be32 get_dev_addr (char *dev_name)
{
    struct net_device *net_dev;
    struct IN_IFADDR *ifaddr;
    if (!dev_name)
        goto err_name_null;
    Net_dev = Dev_get_by_name (&init_net, dev_name);
    IFADDR = net_dev->ip_ptr->ifa_list;
    if (!ifaddr)
        goto err_ifaddr_null;
    LOG ("%s%x", __function__, ifaddr->ifa_local);
    return ifaddr->ifa_local;
Err_name_null:
    LOG ("device name is null");
    return 0;
Err_ifaddr_null:
    LOG ("ifaddr is null, maybe it has no IP address");
    return 0;
}
get MAC address for other devices on LAN

Using the neighbor subsystem's arp_table to query other device's MAC address, need to know each other's IP address, as well as the corresponding network card interface of the device.

static char *get_neigh_ha (struct net_device *dev, __be32 IP)
{struct neighbour
    ;
    if (!dev)
        goto Err_no_such_dev;
    Neigh = Neigh_lookup (&arp_tbl, &ip, dev);
    if (!neigh)
        goto Err_no_such_neigh;
    return neigh->ha;
Err_no_such_dev:
    LOG ("%s:no such Device", __function__);
    return NULL;
Err_no_such_neigh:
    LOG ("%s:no such neigh", __function__);
    return NULL;
}
get the current IP address of the NIC
Static __be32 get_dev_addr (char *dev_name)
{
    struct net_device *net_dev;
    struct IN_IFADDR *ifaddr;
    if (!dev_name)
        goto err_name_null;
    Net_dev = Dev_get_by_name (&init_net, dev_name);
    IFADDR = net_dev->ip_ptr->ifa_list;
    if (!ifaddr)
        goto err_ifaddr_null;
    LOG ("%s%x", __function__, ifaddr->ifa_local);
    return ifaddr->ifa_local;
Err_name_null:
    LOG ("device name is null");
    return 0;
Err_ifaddr_null:
    LOG ("ifaddr is null, maybe it has no IP address");
    return 0;
}
forces a packet to be sent to a device in the LAN, ignoring the destination IP
static int dev_xmit (struct sk_buff *skb, char *dev_name, __be32 target_ip) {int err;
    unsigned char *neigh_ha, *local_ha;
    struct ETHHDR *mac_h;

    struct Net_device *dev;
    dev = dev_get_by_name (&init_net, dev_name);

    if (!dev) goto Err_get_dev;
    Neigh_ha = Get_neigh_ha (dev, target_ip);
    if (!neigh_ha) goto Err_get_neigh;

    Show_ha ("neighbor", Neigh_ha);
    Local_ha = dev->dev_addr;
    if (!local_ha) goto err_get_local;

    Show_ha ("local", dev->dev_addr);
    Skb->dev = Dev;
    Skb->pkt_type = Packet_otherhost;
    Skb->protocol = __constant_htons (ETH_P_IP);
    skb->ip_summed = checksum_unnecessary;

    skb->priority = 0;

    Show_skb_ip (SKB, __function__);
    Skb_push (SKB, sizeof (struct ETHHDR));
    Skb_reset_mac_header (SKB);

    Mac_h = ETH_HDR (SKB);
    memset (mac_h, 0, sizeof (struct ETHHDR));
    Mac_h->h_proto = __constant_htons (ETH_P_IP); memcpy (Mac_h->h_source, Local_ha, Eth_alen);

    memcpy (Mac_h->h_dest, Neigh_ha, Eth_alen);
    Err = Dev_queue_xmit (SKB);
    if (Err < 0) goto Err_xmit;
return 0;
    Err_get_dev:log ("Fail to get dev%s", dev_name);
return-1;
    Err_get_neigh:log ("Fail to get neighbor%x's MAC address over%s", Target_ip, Dev_name);
return-1;
    Err_get_local:log ("fail to get local MAC address");
return-1;
    Err_xmit:log ("fail to XMit SKB");
return-1; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.