本文主要讲解了Linux内核IP层的整体架构和对从网卡接受的报文处理流程,使用的内核的版本是2.6.32.27

为了方便理解,本文采用整体流程图加伪代码的方式对Linxu内核中IP整体实现架构和对网卡报文的处理流程进行了讲解,希望可以对大家有所帮助。阅读本文章假设大家对C语言有了一定的了解

IP层的整体实现架构

IP层接受底层数据报文的处理流程

/*
* 在NET_RX_SOFTIRQ软中后,由ETH_P_IP触发的ipv4协议入口函数
*/
int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
{
/*
* 过滤掉送往其他主机的数据包(这时网卡正在处于混杂模式)
*/
if (skb->pkt_type == PACKET_OTHERHOST)
goto drop; iph = ip_hdr(skb); /*头的长度是否至少是IP头长度(5); 是否是IPV4报文*/
if (iph->ihl < 5 || iph->version != 4)
goto inhdr_error; /*IP头长度是否正确,不是伪造的长度*/
if (!pskb_may_pull(skb, iph->ihl*4))
goto inhdr_error; iph = ip_hdr(skb);
/*检查校验和*/
if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
goto inhdr_error; len = ntohs(iph->tot_len);
if (skb->len < len) {
IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INTRUNCATEDPKTS);
goto drop;
} else if (len < (iph->ihl*4))
goto inhdr_error; /*实际尺寸不匹配套接字缓冲(skb->len)中维护的信息,则调用skb_trim调整数据包的长度*/
if (pskb_trim_rcsum(skb, len)) {
IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
goto drop;
} /*调用IP_PRE_ROUTING(NF_INET_PRE_ROUTING)上注册的钩子,
*在调用钩子处理完之后,调用钩子处理完成之后,调用ip_rcv_finish
* 后面讲防火墙的时候,我们会仔细梳理*/
return NF_HOOK(PF_INET, NF_INET_PRE_ROUTING, skb, dev, NULL, ip_rcv_finish);
} /* NF_HOOK(PF_INET, NF_INET_PRE_ROUTING, skb, dev, NULL, ip_rcv_finish)*/
#define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
{
int __ret; \
if ((__ret=nf_hook_thresh(pf, hook, (skb), indev, outdev, okfn, thresh, 1)) == 1)\
__ret = (okfn)(skb); \
__ret;
} static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
struct sk_buff *skb,
struct net_device *indev,
struct net_device *outdev,
int (*okfn)(struct sk_buff *), int thresh,
int cond)
{
/*逐个调用注册的防火墙钩子*/
return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
} /*
* 接收完数据包后的后续处理函数
*/
static int ip_rcv_finish(struct sk_buff *skb)
{
const struct iphdr *iph = ip_hdr(skb);
struct rtable *rt; /*
* 激活ip_route_input,确定报文的路由,如果ip_route_input无法从FIB中找到路由
* 则丢弃数据报文,ip_route_input将在IP路由中的专题中进行讲解
*/
if (skb_dst(skb) == NULL) {
int err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, skb->dev);
if (unlikely(err)) {
goto drop;
}
} /*检查IP报头里面是否含有选项,如果含有建立ip_options*/
if (iph->ihl > 5 && ip_rcv_options(skb))
goto drop; /*根据dst_entry的结果,使用skb_dst(skb)->input(skb)进行IP的路由选择
*传递给本地计算机的单播或多播,进入 ip_local_deliver();
*单播转发的报文进入ip_forward()
*多播转发进入ip_mr_input()
*/
return dst_input(skb);
{
skb_dst(skb)->input(skb)
} drop:
kfree_skb(skb);
return NET_RX_DROP;
} /*目的地分发策略的注册*/
static int __mkroute_input(struct sk_buff *skb,
struct fib_result *res,
struct in_device *in_dev,
__be32 daddr, __be32 saddr, u32 tos,
struct rtable **result)
{
//.......
rth->u.dst.input = ip_forward;
rth->u.dst.output = ip_output;
//......
} static int __mkroute_output(struct rtable **result,
struct fib_result *res,
const struct flowi *fl,
const struct flowi *oldflp,
struct net_device *dev_out,
unsigned flags)
{
//......
if (flags & RTCF_LOCAL) {
rth->u.dst.input = ip_local_deliver;
rth->rt_spec_dst = fl->fl4_dst;
}
if (flags & (RTCF_BROADCAST | RTCF_MULTICAST)) {
rth->rt_spec_dst = fl->fl4_src;
if (flags & RTCF_LOCAL &&
!(dev_out->flags & IFF_LOOPBACK)) {
rth->u.dst.output = ip_mc_output;
RT_CACHE_STAT_INC(out_slow_mc);
}
#ifdef CONFIG_IP_MROUTE
if (res->type == RTN_MULTICAST) {
if (IN_DEV_MFORWARD(in_dev) &&
!ipv4_is_local_multicast(oldflp->fl4_dst)) {
rth->u.dst.input = ip_mr_input;
rth->u.dst.output = ip_mc_output;
}
}
#endif }
//......
}

如果IP报文需要转发,那么分析流程如下

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*单播转发处理,负责处理转发相关的所有动作*/
int ip_forward(struct sk_buff *skb)
{
/*删除不是PACKET_HOST的数据包*/
if (skb->pkt_type != PACKET_HOST)
goto drop; /*TTL递减为1之间,丢弃该报,并返回ICMP_TIME_EXCEEDED*/
if (ip_hdr(skb)->ttl <= 1)
goto too_many_hops; /*如果skb->len大于MTU值,且Dont-Fragment被职位,则丢弃此报文,
*并返回ICMP_FRAG_NEEDED*/
if (unlikely(skb->len > dst_mtu(&rt->u.dst) && (ip_hdr(skb)->frag_off & htons(IP_DF)))) {
icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(dst_mtu(&rt->u.dst)));
goto drop;
} /*检查是否有足够的空间用于输出网络设备中的MAC报头dst.header_len()
*调用skb_cow来创建一个新的足够长的skb,并且拷贝原来的所有数据
*/
if (skb_cow(skb, LL_RESERVED_SPACE(rt->u.dst.dev)+rt->u.dst.header_len))
goto drop;
iph = ip_hdr(skb); /*TTL减少1*/
ip_decrease_ttl(iph); /*使用IP_FORWARD中注册的钩子函数,当防火墙中的钩子都与运行完成后,
*进入ip_forward_finish*/
return NF_HOOK(PF_INET, NF_INET_FORWARD, skb, skb->dev, rt->u.dst.dev, ip_forward_finish); sr_failed:
/*
* Strict routing permits no gatewaying
*/
icmp_send(skb, ICMP_DEST_UNREACH, ICMP_SR_FAILED, 0);
goto drop; too_many_hops:
/* Tell the sender its packet died... */
IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_INHDRERRORS);
icmp_send(skb, ICMP_TIME_EXCEEDED, ICMP_EXC_TTL, 0);
drop:
kfree_skb(skb);
return NET_RX_DROP;
} /*
* 该函数没有什么用途,除非启用了FASTROUTE,
* 将处理后的函数报文送入output阶段
*/
static int ip_forward_finish(struct sk_buff *skb)
{
struct ip_options * opt = &(IPCB(skb)->opt); /*使用ip_forward_options处理IP选项*/
if (unlikely(opt->optlen))
ip_forward_options(skb); /*送入到输出阶段*/
return dst_output(skb);
{
skb_dst(skb)->output(skb);
}
} /*目的地分发策略的注册*/
static int __mkroute_input(struct sk_buff *skb,
struct fib_result *res,
struct in_device *in_dev,
__be32 daddr, __be32 saddr, u32 tos,
struct rtable **result)
{
//.......
rth->u.dst.input = ip_forward;
rth->u.dst.output = ip_output;
//......
} int ip_output(struct sk_buff *skb)
{
struct net_device *dev = skb_dst(skb)->dev; /*将skb->dev指向输出设备的dev*/
skb->dev = dev;
/*设置2层包类型为ETH_P_IP*/
skb->protocol = htons(ETH_P_IP); /*使用防火墙中的NF_IP_POST_ROUTING中注册的钩子函数进行处理,
*处理完成之后进入ip_finish_output处理*/
return NF_HOOK_COND(PF_INET, NF_INET_POST_ROUTING, skb, NULL, dev,
ip_finish_output,
!(IPCB(skb)->flags & IPSKB_REROUTED));
} /*判定是否进行IP分片*/
static int ip_finish_output(struct sk_buff *skb)
{
/*如果报文尺寸大于MTU,则进行IP分片后送入ip_finish_output2
*否则直接送入ip_finish_output2
*/
if (skb->len > ip_skb_dst_mtu(skb) && !skb_is_gso(skb))
return ip_fragment(skb, ip_finish_output2);
else
return ip_finish_output2(skb);
} static const struct neigh_ops arp_generic_ops = {
.family = AF_INET,
.output = neigh_resolve_output,
.hh_output = dev_queue_xmit,
}; static const struct neigh_ops arp_hh_ops = {
.family = AF_INET,
.output = neigh_resolve_output,
.hh_output = dev_queue_xmit,
}; static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst, __be16 protocol)
{
struct hh_cache *hh; //...... if (n->nud_state & NUD_CONNECTED)
hh->hh_output = n->ops->hh_output; /*也就是dev_queue_xmit*/
else
hh->hh_output = n->ops->output; //......
} static void neigh_suspect(struct neighbour *neigh)
{
//.....
neigh->output = neigh->ops->output; /*也就是neigh_resolve_output*/ } static inline int ip_finish_output2(struct sk_buff *skb)
{
/*如果2层头数据空间不够,则重新分配足够长度的SKB,并将数据复制到新的SKB后释放原来SKB*/
if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
struct sk_buff *skb2; skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev)); if (skb->sk)
skb_set_owner_w(skb2, skb->sk);
kfree_skb(skb);
skb = skb2;
} /*如果路由出口项中已经含有2层包头缓存的引用(dst->hh),进入neigh_hh_output*/
if (dst->hh)
return neigh_hh_output(dst->hh, skb);
/*如果没有dst->hh,有dst->neighbour,则启动地址解析协议,也就是neigh_resolve_output*/
else if (dst->neighbour)
return dst->neighbour->output(skb); } static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
{
/*直接复制2层包头到套接字的包数据空间中*/
memcpy(skb->data - hh_alen, hh->hh_data, hh_alen);
skb_push(skb, hh_len); /*调用hh->hh_output(skb),也就是dev_queue_xmit进行硬件发送*/
return hh->hh_output(skb);
}

如果IP是上送本地CPU的报文,处理流程如下

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*包的本地投递*/
int ip_local_deliver(struct sk_buff *skb)
{
/*收集并组装IP分片,如果还没有收集完成,那么就等待IP分片组装完成*/
if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
if (ip_defrag(skb, IP_DEFRAG_LOCAL_DELIVER))
return 0;
} /*进入NF_IP_LOCAL_IN的过滤器处理,处理完成后进入ip_local_deliver_finish*/
return NF_HOOK(PF_INET, NF_INET_LOCAL_IN, skb, skb->dev, NULL, ip_local_deliver_finish);
} /*IP层处理完成后的协议分发函数*/
static int ip_local_deliver_finish(struct sk_buff *skb)
{
resubmit:
/*如果是RAW-IP报文,送往RAW-IP对应的处理???*/
raw = raw_local_deliver(skb, protocol); /*MAX_INET_PROTOS-1 为IP报头中协议的模,
*这里计算对应协议在ipprot中被散列的位置*/
hash = protocol & (MAX_INET_PROTOS - 1);
/*IP层上的ipprot负责管理所有的传输协议*/
ipprot = rcu_dereference(inet_protos[hash]); /*如果找到相应的协议,那么调用对应的处理例程*/
if (ipprot != NULL) {
ret = ipprot->handler(skb);
if (ret < 0) {
protocol = -ret;
goto resubmit;
}
}
/*找不到相应的处理例程*/
else {
/*又是RAW-IP报文,会在RAW-IP处理例程???
* 就丢弃,并想对端发送ICMP_DEST_UNREACH,ICMP_PROT_UNREACH*/
if (!raw)
{
icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, 0);
} kfree_skb(skb);
} return 0;
} static const struct net_protocol tcp_protocol = {
.handler = tcp_v4_rcv, /*TCP*/
}; static const struct net_protocol udp_protocol = {
.handler = udp_rcv, /*UDP*/
}; static const struct net_protocol icmp_protocol = {
.handler = icmp_rcv, /*ICMP*/
}; static const struct net_protocol igmp_protocol = {
.handler = igmp_rcv, /*IGMP*/
};

通过上面的分析讲解,我们就可以很清楚的了解到IP是如何接受一个来自于网卡的数据包,并如何进行三层报文

关于二层数据报文的处理流程和是如何送到三层进行处理,请参考我前面的博客

《Linux内核二层数据包接收流程》

希望大家批评指正

Linux内核IP层的报文处理流程(一)的更多相关文章

  1. IP 层收发报文简要剖析2--ip报文的输入ip_local_deliver

    ip报文根据路由结果:如果发往本地则调用ip_local_deliver处理报文:如果是转发出去,则调用ip_forward 处理报文. 一.ip报文转发到本地: /* * Deliver IP Pa ...

  2. IP 层收发报文简要剖析5--ip报文发送2

    udp 发送ip段报文接口ip_append_data ip_append_data 函数主要用来udp 套接字以及raw套接字发送报文的接口.在tcp中发送ack 以及rest段的ip_send_u ...

  3. IP 层收发报文简要剖析3--ip输入报文分片重组

    在ip_local_deliver中,如果检测到是分片包,则需要将报文进行重组.其所有的分片被重新组合后才能提交到上层协议,每一个被重新组合的数据包文用ipq结构实例来表示 struct ipq { ...

  4. IP 层收发报文简要剖析1-ip报文的输入

    ip层数据包处理场景如下: 网络层处理数据包文时需要和路由表以及邻居系统打交道.输入数据时,提供输入接口给链路层调用,并调用传输层的输入接口将数据输入到传输层. 在输出数据时,提供输出接口给传输层,并 ...

  5. IP 层收发报文简要剖析6--ip报文输出3 ip_push_pending_frames

    L4层的协议会把数据通过ip_append_data或ip_append_page把数据线放在缓冲区,然后再显示调用ip_push_pending_frames传送数据. 把数据放在缓冲区有两个优点, ...

  6. IP 层收发报文简要剖析4--ip 报文发送

    无论是从本地输出的数据还是转发的数据报文,经过路由后都要输出到网络设备,而输出到网络设备的接口就是dst_output(output)函数 路由的时候,dst_output函数设置为ip_output ...

  7. IP 层收发报文简要剖析6--ip_forward 报文转发

    //在函数ip_route_input_slow->ip_mkroute_input注册, /* * IP数据包的转发是由ip_forward()处理,该函数在ip_rcv_finish() * ...

  8. Linux 内核协议栈 学习资料

    终极资料 1.<Understanding Linux Network Internals> 2.<TCP/IP Architecture, Design and Implement ...

  9. Linux内核网络报文简单流程

    转:http://blog.csdn.net/adamska0104/article/details/45397177 Linux内核网络报文简单流程2014-08-12 10:05:09 分类: L ...

随机推荐

  1. Git 图解剖析(转)

    git中文件内容并没有真正存储在索引(.git/index)或者提交对象中,而是以blob的形式分别存储在数据库中(.git/objects),并用SHA-1值来校验. 索引文件用识别码列出相关的bl ...

  2. 移动开发中的Scheme跳转说明——Allowing OtherApps to Start Your Activity

    Allowing OtherApps to Start Your Activity 为了开发更多人使用的App,我们总希望我们的App能够提供一种接口被其他App调用.如我们常见的 大众点评  与  ...

  3. 【Demo 0003】Java基础-数组

    本章学习要点:       1.  了解数组的基本概念:       2.  掌握数组使用方法:  一.数组的基本概念     1.  数组定义:              同一数据类型数据的集合,在 ...

  4. 【Demo 0002】Java基础-语句

    本章学习要点:        1.  掌握Java关健语句使用方法;          2.  理解与语句相关的关键字用法; 一.Java 关键语句        Java语句以及关联关键字与C完全相 ...

  5. python 循环中的else

    众多语言中都有if else这对条件选择组合,但是在python中还有更多else使用的地方,比如说循环for,或者while都可以和else组合. 下面简单介绍一下for-else while-el ...

  6. QQ圈子降级为“应用”后应关注其隐私设置

    在之前的QQ版本中,QQ圈子的权限设置在“系统设置”对话框的“权限设置”中,如图所示. 但是在更新后的2013SP1版本中,“系统设置”对话框中的“权限设置”已经没有了“圈子权限” QQ圈子成了应用管 ...

  7. HealthKit教程 Swift版:锻炼信息

    原文:HealthKit Tutorial with Swift: Workouts 作者:Ernesto García 译者:Mr_cyz ) 欢迎回到我们的HealthKit系列教程! 在我们系列 ...

  8. STL 之 queue、priority_queue 源代码剖析

    /* * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute an ...

  9. QT插件开发方式(没看懂)

    创建一个QT的库项目,删除自动生成的.h和.cpp文件,添加一个接口定义.h文件和一个接口实现类(一个.h一个.cpp).代码如下: 1.接口文件源码 #ifndef PLUGININTERFACE_ ...

  10. 14.3.3 Locks Set by Different SQL Statements in InnoDB 不同的SQL语句在InnoDB里的锁设置

    14.3.3 Locks Set by Different SQL Statements in InnoDB 不同的SQL语句在InnoDB里的锁设置 locking read, 一个UPDATE,或 ...