Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
LWIP
nat
LWIP 的IP转发功能
发布于 2023-05-19 15:48:41 浏览:678
订阅该版
在阅读LWIP的源码的时候,发现ip4_forward中的逻辑无法理解,希望大家能一起看看这里的逻辑,话不多说,先上代码: ```c /** * Forwards an IP packet. It finds an appropriate route for the * packet, decrements the TTL value of the packet, adjusts the * checksum and outputs the packet on the appropriate interface. * * @param p the packet to forward (p->payload points to IP header) * @param iphdr the IP header of the input packet * @param inp the netif on which this packet was received */ static void ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) { struct netif *netif; PERF_START; LWIP_UNUSED_ARG(inp); if (!ip4_canforward(p)) { goto return_noroute; } /* RFC3927 2.7: do not forward link-local addresses */ if (ip4_addr_islinklocal(ip4_current_dest_addr())) { LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); goto return_noroute; } /* Find network interface where to forward this IP packet to. */ netif = ip4_route_src(ip4_current_src_addr(), ip4_current_dest_addr()); if (netif == NULL) { LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n", ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); /* @todo: send ICMP_DUR_NET? */ goto return_noroute; } #if !IP_FORWARD_ALLOW_TX_ON_RX_NETIF /* Do not forward packets onto the same network interface on which * they arrived. */ if (netif == inp) { LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: not bouncing packets back on incoming interface.\n")); goto return_noroute; } #endif /* IP_FORWARD_ALLOW_TX_ON_RX_NETIF */ /* decrement TTL */ IPH_TTL_SET(iphdr, IPH_TTL(iphdr) - 1); /* send ICMP if TTL == 0 */ if (IPH_TTL(iphdr) == 0) { MIB2_STATS_INC(mib2.ipinhdrerrors); #if LWIP_ICMP /* Don't send ICMP messages in response to ICMP messages */ if (IPH_PROTO(iphdr) != IP_PROTO_ICMP) { icmp_time_exceeded(p, ICMP_TE_TTL); } #endif /* LWIP_ICMP */ return; } /* Incrementally update the IP checksum. */ if (IPH_CHKSUM(iphdr) >= PP_HTONS(0xffffU - 0x100)) { IPH_CHKSUM_SET(iphdr, (u16_t)(IPH_CHKSUM(iphdr) + PP_HTONS(0x100) + 1)); } else { IPH_CHKSUM_SET(iphdr, (u16_t)(IPH_CHKSUM(iphdr) + PP_HTONS(0x100))); } LWIP_DEBUGF(IP_DEBUG, ("ip4_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); IP_STATS_INC(ip.fw); MIB2_STATS_INC(mib2.ipforwdatagrams); IP_STATS_INC(ip.xmit); PERF_STOP("ip4_forward"); /* don't fragment if interface has mtu set to 0 [loopif] */ if (netif->mtu && (p->tot_len > netif->mtu)) { if ((IPH_OFFSET(iphdr) & PP_NTOHS(IP_DF)) == 0) { #if IP_FRAG ip4_frag(p, netif, ip4_current_dest_addr()); #else /* IP_FRAG */ /* @todo: send ICMP Destination Unreachable code 13 "Communication administratively prohibited"? */ #endif /* IP_FRAG */ } else { #if LWIP_ICMP /* send ICMP Destination Unreachable code 4: "Fragmentation Needed and DF Set" */ icmp_dest_unreach(p, ICMP_DUR_FRAG); #endif /* LWIP_ICMP */ } return; } /* transmit pbuf on chosen interface */ netif->output(netif, p, ip4_current_dest_addr()); // 这里为什么会调用 netif->output ???? return; return_noroute: MIB2_STATS_INC(mib2.ipoutnoroutes); } #endif /* IP_FORWARD */ ``` 我理解的IP转发功能是指,当netif上收到一个IP包的时候,发现目的IP并不是自己,然后调用ip_forward 尝试进行转发,但是代码中在找到对应的netif后,为什么会调用netif_arpout ?? 发出去后怎么能收到呢?
查看更多
0
个回答
默认排序
按发布时间排序
暂无答案,快来添加答案吧
撰写答案
登录
注册新账号
关注者
0
被浏览
678
关于作者
Exxfire
这家伙很懒,什么也没写!
提问
1
回答
4
被采纳
0
关注TA
发私信
相关问题
1
RT-THREAD在STM32H747平台上移植lwip
2
{lwip}使能RT_LWIP_DHCP时可以获取到ip
3
stm32f103 LWIP 2.0.2 TCP收发问题
4
lwip2.1不重启修改IP
5
关于网络协议栈的测试
6
可否将LWIP升级到2.1.2 和 2.0.3?
7
socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
8
tcpclient 插拔网线问题?
9
两个tcpclient同时通讯可以吗?
10
SO_BINDTODEVICE 未定义该如何解决
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
RT-Thread CI编译产物artifacts自动上传功能介绍
2
STM32G030移植RT-Thread
3
CubeMX & RT-Thread Studio 联合开发说明
4
RT-Thread项目助手v0.3 | Ubuntu与MacOS平台的RT-Thread Env
5
【FRA156测评DM-MCX】- 环境配置篇
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
I2C_IIC
ESP8266
UART
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
GD32
定时器
ADC
flashDB
编译报错
socket
中断
rt_mq_消息队列_msg_queue
keil_MDK
Debug
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
lchnu
3
个答案
2
次被采纳
张世争
1
个答案
2
次被采纳
a1012112796
10
个答案
1
次被采纳
三世执戟
8
个答案
1
次被采纳
聚散无由
5
个答案
1
次被采纳
本月文章贡献
jinchanchan
12
篇文章
14
次点赞
ssdd45555
3
篇文章
2
次点赞
聚散无由
1
篇文章
4
次点赞
RTT_逍遥
1
篇文章
2
次点赞
hywing
1
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部