標籤:
我們知道,報文經過網卡驅動處理後,調用net_receive_skb傳遞給具體的協議處理函數,對於IPv4報文來說,其協議處理函數就是ip_rcv了,ip_rcv在進行一些健全狀態檢查等操作後,會調用ip_rcv_finish來處理報文。這也是IPv4協議對報文接收處理的開始。
我們先看下ip_rcv_finish原始碼:
ip_rcv_finish:
//ip資料報文的主要處理常式(ip_rcv僅僅只是對ip資料報做一些健康性檢查)//ip_rcv_finish 其實是進行路由表查詢,,決定報文經過IP層處理後,是繼續向上傳遞,還是進行轉寄,還是丟棄。//1.決定報文在本地傳遞或者轉寄,如果是轉寄還需要找到出口裝置和下一跳節點//2.分析和處理一些選項static int ip_rcv_finish(struct sk_buff *skb){const struct iphdr *iph = ip_hdr(skb);struct rtable *rt;/* *Initialise the virtual path cache for the packet. It describe *how the packet travels inside Linux networking. * 剛開始沒有進行路由表查詢,所以還沒有相應的路由表項:skb_dst(skb) == NULL。 * 則在路由表中尋找ip_route_input(),關於核心的路由表 */if (skb_dst(skb) == NULL) {int err = ip_route_input(skb, iph->daddr, iph->saddr, iph->tos, skb->dev); //這裡面進行了一些初始化操作,比較重要,與ip報文接下來的走向有關if (unlikely(err)) {if (err == -EHOSTUNREACH)IP_INC_STATS_BH(dev_net(skb->dev),IPSTATS_MIB_INADDRERRORS);else if (err == -ENETUNREACH)IP_INC_STATS_BH(dev_net(skb->dev),IPSTATS_MIB_INNOROUTES);goto drop;}}#ifdef CONFIG_NET_CLS_ROUTE//更新traffic cotrol(qos層)所使用的統計資料if (unlikely(skb_dst(skb)->tclassid)) {struct ip_rt_acct *st = per_cpu_ptr(ip_rt_acct, smp_processor_id());u32 idx = skb_dst(skb)->tclassid;st[idx&0xFF].o_packets++;st[idx&0xFF].o_bytes += skb->len;st[(idx>>16)&0xFF].i_packets++;st[(idx>>16)&0xFF].i_bytes += skb->len;}#endifif (iph->ihl > 5 && ip_rcv_options(skb))goto drop;rt = skb_rtable(skb); /* skb->dst包含路由資訊。根據路由類型更新SNMP統計資料 */if (rt->rt_type == RTN_MULTICAST) {IP_UPD_PO_STATS_BH(dev_net(rt->u.dst.dev), IPSTATS_MIB_INMCAST,skb->len);} else if (rt->rt_type == RTN_BROADCAST)IP_UPD_PO_STATS_BH(dev_net(rt->u.dst.dev), IPSTATS_MIB_INBCAST,skb->len);/* * dst_input實際上會調用skb->dst->input(skb).input函數會根據路由資訊設定為合適的 * 函數指標,如果是遞交到本地的則為ip_local_deliver,若是轉寄則為ip_forward. * 暫時僅先考慮ip_local_deliver。 */return dst_input(skb);drop:kfree_skb(skb);return NET_RX_DROP;}
ip_route_input會進行路由表查詢,該函數直接或間接決定了報文之後要往何處傳遞。是進行本地傳遞還是轉寄。
我們可以看到如果報文沒有被drop掉,那麼報文最終會被dst_input(skb)處理。dst_input(skb)實際上執行的是skb->dst->input(skb)。而這裡的input函數其實就是由ip_route_input決定的。
對於應該本地傳遞的報文,input指標會指向ip_local_deliver。對於該轉寄的報文,input會指向ip_forward。
本地傳遞
/* * Deliver IP Packets to the higher protocol layers. */int ip_local_deliver(struct sk_buff *skb){ /* * Reassemble IP fragments. */ if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) { if (ip_defrag(skb, IP_DEFRAG_LOCAL_DELIVER)) return 0; } return NF_HOOK(PF_INET, NF_INET_LOCAL_IN, skb, skb->dev, NULL, ip_local_deliver_finish);} 我們知道,IPv4要將報文傳送給上層協議(本地傳遞),那它需要對分段的報文進行重組,ip_defrag即完成報文重組。
然後由調用Netfilter決定是否調用ip_local_deliver_finish。ip_local_deliver_finish
static int ip_local_deliver_finish(struct sk_buff *skb){ struct net *net = dev_net(skb->dev); __skb_pull(skb, ip_hdrlen(skb)); /* 跳過IP頭部 */ /* Point into the IP datagram, just past the header. */ /* 設定傳輸層頭部位置 */ skb_reset_transport_header(skb); rcu_read_lock(); { int protocol = ip_hdr(skb)->protocol; //取出ip頭中的協議. int hash, raw; const struct net_protocol *ipprot; resubmit: // 若是raw socket發送的,需要做相應的處理,clone資料包 raw = raw_local_deliver(skb, protocol); //得到raw socket, 如果不是raw socket,則返回0 hash = protocol & (MAX_INET_PROTOS - 1); // 計算傳輸層協議處理結構在inet_protos數組hash表中的位置 ipprot = rcu_dereference(inet_protos[hash]); // 擷取傳輸層協議處理指標 if (ipprot != NULL) { int ret; //主要是ipprot是否有被當前主機註冊 if (!net_eq(net, &init_net) && !ipprot->netns_ok) { // 若擷取到了對應傳輸層的處理結構 if (net_ratelimit()) printk("%s: proto %d isn't netns-ready\n", __func__, protocol); kfree_skb(skb); goto out; } //判斷ipsec,並進行相關處理. if (!ipprot->no_policy) { if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) { kfree_skb(skb); goto out; } nf_reset(skb); } //調用handler,進入相應的4層協議的處理. ret = ipprot->handler(skb); if (ret < 0) { // 處理資料包失敗,再次嘗試 protocol = -ret; goto resubmit; } IP_INC_STATS_BH(net, IPSTATS_MIB_INDELIVERS);// 添加資料包處理統計資訊 } else {// 若沒有找到相應傳輸層的處理函數 if (!raw) { if (xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) { IP_INC_STATS_BH(net, IPSTATS_MIB_INUNKNOWNPROTOS); icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PROT_UNREACH, 0); } } else IP_INC_STATS_BH(net, IPSTATS_MIB_INDELIVERS); kfree_skb(skb); } } out: rcu_read_unlock(); return 0;}
轉寄
報文轉寄有下面幾個步驟完成:
1. 處理IP選項
2. 確定封包可以被轉寄
3.遞減封包頭部的TTL欄位,如果TTL欄位為0,則丟棄該封包
4.根據路徑相關MTU,在必要時處理分段
5.把封包傳送至外出裝置
IPv4協議中,報文的轉寄從ip_forward開始:
ip_forward
int ip_forward(struct sk_buff *skb){ struct iphdr *iph; /* Our header */ struct rtable *rt; /* Route we use */ struct ip_options * opt = &(IPCB(skb)->opt); if (skb_warn_if_lro(skb)) goto drop; if (!xfrm4_policy_check(NULL, XFRM_POLICY_FWD, skb)) goto drop; if (IPCB(skb)->opt.router_alert && ip_call_ra_chain(skb)) //處理Router_Alert選項(重要) return NET_RX_SUCCESS; //確定二層目的是發往原生,這步檢查是多餘的,因為在二層接收的時候,不是二層地址不是原生包已經被丟棄 //對於二層地址為原生資料幀,skb->pkt_type 賦值為PACKET_HOST if (skb->pkt_type != PACKET_HOST)// goto drop; skb_forward_csum(skb);//這裡只是轉寄封包,因而不關係L4層檢驗和 skb_forward_csum(skb);//這裡只是轉寄封包,因而不關係L4層檢驗和 /* * According to the RFC, we must first decrease the TTL field. If * that reaches zero, we must reply an ICMP control message telling * that the packet's lifetime expired. */ if (ip_hdr(skb)->ttl <= 1) goto too_many_hops; if (!xfrm4_route_forward(skb)) goto drop; rt = skb_rtable(skb); //如果前序含有strictroute選項,且選項中的下一跳與路由子系統的網關不同 //則表示選項失敗,封包丟棄 if (opt->is_strictroute && rt->rt_dst != rt->rt_gateway) goto sr_failed; if (unlikely(skb->len > dst_mtu(&rt->u.dst) && !skb_is_gso(skb) && (ip_hdr(skb)->frag_off & htons(IP_DF))) && !skb->local_df) { IP_INC_STATS(dev_net(rt->u.dst.dev), IPSTATS_MIB_FRAGFAILS); icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(dst_mtu(&rt->u.dst))); goto drop; } /* We are about to mangle packet. Copy it! */ if (skb_cow(skb, LL_RESERVED_SPACE(rt->u.dst.dev)+rt->u.dst.header_len)) goto drop; iph = ip_hdr(skb); /* Decrease ttl after skb cow done */ ip_decrease_ttl(iph); /* * We now generate an ICMP HOST REDIRECT giving the route * we calculated. */ if (rt->rt_flags&RTCF_DOREDIRECT && !opt->srr && !skb_sec_path(skb)) ip_rt_send_redirect(skb); skb->priority = rt_tos2priority(iph->tos); 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;} ip_forward檢查結束,如果進入ip_forward_finish說明該封包已經可以真正的傳給另一個系統了。轉寄真正工作在ip_forward_finish,中完成。
ip_forward_finish
static int ip_forward_finish(struct sk_buff *skb){ struct ip_options * opt = &(IPCB(skb)->opt); IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS); //ip_forward已經處理了兩個可能的選項,ROute_Alert 和 Strict Source Routing //這裡還需要處理其他選項(由ip_rcv_finish通過ip_options_compile進行初始化 if (unlikely(opt->optlen)) ip_forward_options(skb); return dst_output(skb); }
最後調用dst_ouput將封包傳給裝置驅動,轉寄出去,這裡其實封包與我們從傳輸層傳遞下來要傳給其他系統的封包,開始路徑一致了。
/* Output packet to network from transport. */static inline int dst_output(struct sk_buff *skb){ return skb_dst(skb)->output(skb);} output是虛擬函數,對於單播封包,會初始化為ip_output,對於多播封包,會初始化為ip_mc_output。這兩函數也會處理分段,並在最後調用ip_finish_output.
ip_output
int ip_output(struct sk_buff *skb){ struct net_device *dev = skb_dst(skb)->dev; IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len); skb->dev = dev; skb->protocol = htons(ETH_P_IP); return NF_HOOK_COND(PF_INET, NF_INET_POST_ROUTING, skb, NULL, dev, ip_finish_output, !(IPCB(skb)->flags & IPSKB_REROUTED));}ip_mc_output
int ip_mc_output(struct sk_buff *skb){ struct sock *sk = skb->sk; struct rtable *rt = skb_rtable(skb); struct net_device *dev = rt->u.dst.dev; /* * If the indicated interface is up and running, send the packet. */ IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len); skb->dev = dev; skb->protocol = htons(ETH_P_IP); /* * Multicasts are looped back for other local users */ if (rt->rt_flags&RTCF_MULTICAST) { if ((!sk || inet_sk(sk)->mc_loop)#ifdef CONFIG_IP_MROUTE /* Small optimization: do not loopback not local frames, which returned after forwarding; they will be dropped by ip_mr_input in any case. Note, that local frames are looped back to be delivered to local recipients. This check is duplicated in ip_mr_input at the moment. */ && ((rt->rt_flags&RTCF_LOCAL) || !(IPCB(skb)->flags&IPSKB_FORWARDED))#endif ) { struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); if (newskb) NF_HOOK(PF_INET, NF_INET_POST_ROUTING, newskb, NULL, newskb->dev, ip_dev_loopback_xmit); } /* Multicasts with ttl 0 must not go beyond the host */ if (ip_hdr(skb)->ttl == 0) { kfree_skb(skb); return 0; } } if (rt->rt_flags&RTCF_BROADCAST) { struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); if (newskb) NF_HOOK(PF_INET, NF_INET_POST_ROUTING, newskb, NULL, newskb->dev, ip_dev_loopback_xmit); } return NF_HOOK_COND(PF_INET, NF_INET_POST_ROUTING, skb, NULL, skb->dev, ip_finish_output, !(IPCB(skb)->flags & IPSKB_REROUTED));}
ip_finish_output
static int ip_finish_output(struct sk_buff *skb){#if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM) /* Policy lookup after SNAT yielded a new policy */ if (skb_dst(skb)->xfrm != NULL) { IPCB(skb)->flags |= IPSKB_REROUTED; return dst_output(skb); }#endif 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);}ip_finish_output會和鄰居子系統進行銜接。細節看 ipv4 傳輸的博文。
深入理解Linux網路技術內幕——IPv4 報文的接收(轉寄與本地傳遞)