当前位置: 首页 > news >正文

写的将skb copy/clone后转发到源地址的一段代码

可以作为自构建skb xmit发送的示范! 在netfilter 模块中通过!

static int skb_copy_xmit( struct sk_buff *pskb, const struct net_device *in )
{
struct sk_buff *nskb;
struct iphdr *oiph, *niph;
struct tcphdr *oth, *tcph;
struct ethhdr *oeth;
struct ethhdr *neth ;
int retval = 0;

//
oiph = ip_hdr( pskb );
oth = (struct tcphdr *)((u_int32_t *)oiph + oiph->ihl);

//dump_ethhdr( (struct ethhdr *)pskb->mac_header );
//dump_iphdr( oiph );
//dump_tcphdr( oth );

#if 1
nskb = skb_copy( pskb, GFP_ATOMIC );
#else
nskb = alloc_skb(
sizeof( struct ethhdr )
+ sizeof(struct iphdr)
+ sizeof( struct tcphdr )
+ LL_MAX_HEADER,
GFP_ATOMIC
);
#endif

if ( !nskb ) {
printk( "skb_copy occur error!\n" );
return -1;
}


//nskb->protocol = pskb->protocol ;//__constant_htons(ETH_P_802_3);
//skb_reset_mac_header(nskb);
//skb_reset_network_header(nskb);

nskb->data = skb_mac_header(nskb);
nskb->len += ETH_HLEN;

nskb->pkt_type = PACKET_OUTGOING; //OUTGOING;
nskb->dev = (struct net_device *)in;

#if 0
dev_hard_header(nskb, in, ntohs(pskb->protocol),
in->dev_addr, out->dev_addr, pskb->len );
#else
if (pskb->mac_header != NULL) {
oeth = (struct ethhdr *)pskb->mac_header;
neth = (struct ethhdr *)nskb->mac_header;
memcpy( neth->h_source, oeth->h_dest, ETH_ALEN );
memcpy( neth->h_dest, oeth->h_source, ETH_ALEN );
}
#endif
niph = ip_hdr( nskb );
tcph = (struct tcphdr *)((u_int32_t *)niph + niph->ihl);

pkt_create( oiph, niph, oth, tcph );

//dump_ethhdr( (struct ethhdr *)nskb->mac_header );
//dump_iphdr( niph );
//dump_tcphdr( tcph );


retval = dev_queue_xmit( nskb );
printk( "dev_queue_xmit return %d\n", retval );

return retval;
}

static int skb_clone_xmit( struct sk_buff *pskb, const struct net_device *in )
{
int retval = 0;
struct sk_buff *nskb;
struct iphdr *oiph;
struct tcphdr *tcph;
unsigned char eth_addr[ETH_ALEN];
struct ethhdr *oeth;
__be32 addr;
__be16 port;
u32 tcplen;


oiph = ip_hdr( pskb );
tcph = (struct tcphdr *)((u_int32_t *)oiph + oiph->ihl);

//dump_ethhdr( (struct ethhdr *)pskb->mac_header );
//dump_iphdr( oiph );
//dump_tcphdr( tcph );

nskb = skb_clone(pskb, GFP_ATOMIC);

if ( !nskb ) {
printk( "skb_clone occur error!\n" );
return -1;
}

nskb->pkt_type = PACKET_OUTGOING; //OUTGOING;
nskb->dev = (struct net_device *)in;

printk( "data=0x%p mac_header=%p\n", nskb->data, nskb->mac_header );
nskb->data = skb_mac_header(nskb);
nskb->len += ETH_HLEN;

if (pskb->mac_header != NULL) {

oeth = (struct ethhdr *)nskb->mac_header;
memcpy( eth_addr, oeth->h_source, ETH_ALEN );
memcpy( oeth->h_source, oeth->h_dest, ETH_ALEN );
memcpy( oeth->h_dest, eth_addr, ETH_ALEN );
}
//
oiph = ip_hdr( pskb );
tcph = (struct tcphdr *)((u_int32_t *)oiph + oiph->ihl);

addr = oiph->daddr;
oiph->daddr = oiph->saddr;
oiph->saddr = addr;

// tcp
port = tcph->source;
tcph->source = tcph->dest;
tcph->dest = port;

tcph->doff = sizeof(struct tcphdr) / 4;
tcph->ack_seq = htonl( ntohl(tcph->seq) + 1 );
tcph->seq = 0;

tcph->ack = 1;
tcph->syn = 1;
//tcph->psh = 1;
//tcph->rst = 1;
tcplen = ntohs( oiph->tot_len ) - oiph->ihl * 4;
tcph->check = 0;
tcph->check = tcp_v4_check( tcplen,
oiph->saddr, oiph->daddr,
csum_partial(tcph,
tcplen, 0));

ip_send_check( oiph );

//dump_ethhdr( (struct ethhdr *)nskb->mac_header );
//dump_iphdr( oiph );
/dump_tcphdr( tcph );


retval = dev_queue_xmit(nskb);
printk( "dev_queue_xmit return %d\n", retval );
return retval;
}

相关文章:

  • “简单低级”的微软技术与研究生的贬值
  • 中缀表达式转后缀表达式
  • wxGlade wxFormBuilder
  • 价值驱动:移动支付的推手
  • 赖床随想记录
  • 世博会助中移动从3G迈向4G
  • 《中关村两个男人的斗争》外传——西直门
  • JavaScript 面向对象的使用
  • Symbian下自动切换SDK的批处理代码
  • [Web开发] 在线 Javascript 代码格式美化工具
  • 如果浏览器是女人……
  • C#几种常用的排序算法
  • DFS测试_引用状态持续缓存时间
  • “139邮箱”练好内功比发电影票更重要
  • 7 个效果震憾的 HTML5 应用组件
  • Codepen 每日精选(2018-3-25)
  • Golang-长连接-状态推送
  • js如何打印object对象
  • leetcode-27. Remove Element
  • MD5加密原理解析及OC版原理实现
  • MySQL数据库运维之数据恢复
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • PAT A1120
  • 仿天猫超市收藏抛物线动画工具库
  • 力扣(LeetCode)965
  • 前端_面试
  • 前端设计模式
  • 推荐一款sublime text 3 支持JSX和es201x 代码格式化的插件
  • 我感觉这是史上最牛的防sql注入方法类
  • 鱼骨图 - 如何绘制?
  • 源码之下无秘密 ── 做最好的 Netty 源码分析教程
  • AI算硅基生命吗,为什么?
  • 完善智慧办公建设,小熊U租获京东数千万元A+轮融资 ...
  • ​插件化DPI在商用WIFI中的价值
  • #define用法
  • #NOIP 2014# day.2 T2 寻找道路
  • (12)目标检测_SSD基于pytorch搭建代码
  • (二)JAVA使用POI操作excel
  • (一)WLAN定义和基本架构转
  • (转)关于如何学好游戏3D引擎编程的一些经验
  • (轉貼) UML中文FAQ (OO) (UML)
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .NET Standard / dotnet-core / net472 —— .NET 究竟应该如何大小写?
  • .net 简单实现MD5
  • .Net 知识杂记
  • .NET/C# 使用 SpanT 为字符串处理提升性能
  • .NET多线程执行函数
  • .net反编译工具
  • .NET委托:一个关于C#的睡前故事
  • .Net中的设计模式——Factory Method模式
  • @RestControllerAdvice异常统一处理类失效原因
  • [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序...
  • [2016.7.Test1] T1 三进制异或
  • [2016.7.test1] T2 偷天换日 [codevs 1163 访问艺术馆(类似)]
  • [AIGC] MySQL存储引擎详解