如何打印每个数据包的前34个字节(以太网和IP头)(EtherType:0x0806,协议:ARP)?

时间:2015-12-08 03:52:06

标签: kernel linux-kernel ip ethernet

  • 操作系统:Ubuntu 15.10桌面(64位)
  • 内核:4.3
  • 司机:无线
  • 路径:/net/mac80211/rx.c

以下是我当前的实现(int i = 0;仅在struct sta_info *dsta;之后,而packet_number++;就在原始rx.c文件中的skb = rx->skb;之前):

#include <linux/ip.h>
#include <linux/byteorder/generic.h>

unsigned int packet_number = 1;

(etc.)

static void ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
{
    (etc.)

    int i = 0;
    unsigned int *byte_pointer;
    unsigned long byte_contents = 0;

    if (ntohs(ehdr->h_proto) == 0x0806) {
        printk("Packet %d:", packet_number);
        for (i = 0; i < 34; i++) {
            byte_pointer = (unsigned int *) (rx->skb->data + i);
            byte_contents = (unsigned long) (&byte_pointer);
            printk("  %lx", byte_contents);
        }
    }
    printk("\n");
    packet_number++;

然而,在运行dmesg以显示printk输出后,这就是我得到的:

[  103.373847] Packet 6: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[  103.375720] Packet 7: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[  103.572086] Packet 8: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[  103.913790] Packet 12: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[  108.940615] Packet 20: ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8
[  114.221127] Packet 28: ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8
[  143.390755] Packet 1354: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[  144.394113] Packet 1401: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[  194.730646] Packet 2041: ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8
[  208.784474] Packet 2059: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[  247.409613] Packet 2130: ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8
[  272.241113] Packet 2171: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[  272.241957] Packet 2172: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[  272.648982] Packet 2174: ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8 ffff88007bd17bd8
[  273.877091] Packet 2180: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8
[  273.878104] Packet 2181: ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8 ffff88007c603cb8

这些是我想要的输出:

Packet xx: 74 46 a4 08 06 ...
Packet xx: 55 32 3e 45 98 ...
Packet xx: 00 1b 2c 70 55 ...

rx-&gt; skb-&gt;数据的数据类型是unsigned char *,为了使它与%x兼容,我必须进行这些转换,最后需要使用unsigned long和%lx代替停止发生编译警告。

你能告诉我我做错了什么,以及如何打印每个数据包的前34个字节(以太网和IP头)(EtherType:0x0806,Protocol:ARP)?

2 个答案:

答案 0 :(得分:2)

如果要将其放在多行上,则必须使用%*ph说明符或print_hex_dump()。在任何情况下,这个都不是为时间关键部分设计的。因此,在这种情况下,您必须切换到具有相应trace_printk()次呼叫的跟踪点。

对于慢速的例子如下。

pr_info("Packet %d: %34ph\n", packet_number, rx->skb->data);

pr_info("Packet %d:\n", packet_number);
print_hex_dump(KERN_INFO, "Packet:", DUMP_PREFIX_OFFSET, 16, 1, rx->skb->data, 34, false);

答案 1 :(得分:0)

byte_pointer是一个指针,
(&amp; byte_pointer)是指针的指针,你需要的只是* byte_pointer。

尝试以下代码......

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"
defaultConfig {
    applicationId "com.example.min.photozzle"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
packagingOptions {
        exclude 'LICENSE.txt'
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services-gcm:8.3.0'
compile 'com.android.support:appcompat-v7:23.0.0'

// Testing dependencies
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support:support-annotations:23.0.0'


compile group: 'com.kakao.sdk', name: 'kakaolink', version: '1.1.0'
compile group: 'com.kakao.sdk', name: 'kakaostory', version: '1.1.0'// 카카오스토리 sdk를 사용하기 위해 필요.
compile group: 'com.kakao.sdk', name: 'kakaotalk', version: '1.1.0'    // 카카오톡 sdk를 사용하기 위해 필요.
compile group: 'com.kakao.sdk', name: 'push', version: '1.1.0'    // push sdk를 사용하기 위해 필요.

compile 'com.squareup.retrofit:retrofit:1.9.0'
}

或更短

static void ieee80211_deliver_skb(  struct ieee80211_rx_data *rx  )
{
    (etc.)

    int i = 0;
    unsigned char *byte_pointer;
    unsigned char byte_contents = 0;

    if (ntohs(ehdr->h_proto) == 0x0806) {
        printk("Packet %d:", packet_number);
        for (i = 0; i < 34; i++) {
            byte_pointer = (unsigned char *) (rx->skb->data + i);
            byte_contents = *byte_pointer;
            printk("  %02x", byte_contents);
        }
    }
    printk("\n");
    packet_number++;
}



顺便说一句,Tx / Rx流程中的很多printk消息是危险的。
如果你不使用PC / NB而是使用嵌入式系统,那么频繁出现的数据包会带来太多的printk处理程序调用。系统可能不会消耗这些消息,然后由于printk缓冲区溢出而导致内核崩溃。