?????Linux??TCP/IP???????????,??Linux ???????????? TCP/IP ??????? BSD ??????????????????????????????? - PowerPoint PPT Presentation

1 / 110
About This Presentation
Title:

?????Linux??TCP/IP???????????,??Linux ???????????? TCP/IP ??????? BSD ???????????????????????????????

Description:

8 Linux TCP/IP Linux TCP/IP BSD ... – PowerPoint PPT presentation

Number of Views:176
Avg rating:3.0/5.0
Slides: 111
Provided by: hm2
Category:
Tags: bsd | tcp | linux | protocol

less

Transcript and Presenter's Notes

Title: ?????Linux??TCP/IP???????????,??Linux ???????????? TCP/IP ??????? BSD ???????????????????????????????


1
?8? ??
  • ?????Linux??TCP/IP???????????,??Linux
    ???????????? TCP/IP ??????? BSD
    ???????????????????????????????

2
  • 8.1 ????
  • 8.1.1 TCP/IP ????
  • 8.1.2 TCP/IP ??????????
  • 8.2 ???
  • 8.2.1 ??????
  • 8.2.2 ?????????????
  • 8.2.3 ???????
  • 8.2.4 ????????
  • 8.3 ??????
  • 8.3.1 ????????????
  • 8.3.2 ???????????
  • 8.3.3 ???????????
  • 8.3.4 ?????????????
  • 8.4 ????????
  • 8.4.1 Linux????????
  • 8.4.2 ??????
  • 8.4.3 ??????
  • 8.4.4 ??????
  • 8.4.5 Socket???????
  • 8.5 IP ?
  • 8.5.1 ?? IP ???
  • 8.5.2 ?? IP ???
  • 8.5.3 ?????????
  • 8.5.4 ??????
  • 8.5.5 IP ??
  • ?????

3
  • 8.1 ????
  • 8.1.1 TCP/IP ????
  • TCP/IP ??? Internet ?????,?????????????TCP/
    IP ????????????,????????????IP(Internet
    Protocol)???????TCP(Transmission Control
    Protocol)????????UDP (User Datagram
    Protocol)?Internet??????ICMP(Internet Control
    Message Protocol)?
  • ??????ARP(Address Resolution
    Protocol)???Linux?,?????socket???????????

4
  • ? 8.1 TCP/IP ??????

TCP/IP ?????????????,?8.1?TCP/IP
?????????????,TCP/IP???????????,??????????????????
?????
5
  • ?????(Network Interface Layer)????????????????????
    ?????,??????????,?????????????????????????????????
    ?????
  • ???(Internet Layer)??????????(IP)?????????????????
    ??????IP????????????????????????????????????????
    ??????????????????????????
  • IP?????????,?????????????????????????????
    ?????????????,????????????????????????Internet???
    ???ICMP,Internet?????IGMP???????ARP??

6
  • ???(Transport Layer)??????????????????????????,???
    ?????????????????????????????????????????????????(
    Transmission Control Protocol, TCP)????????,??????
    ????????????(User Datagram Protocol,
    UDP)?????????,????????????
  • ???(Application Layer)???????????????????,???????,
    ??????????SNMP(Simple Network Management
    Protocol)???????FTP(File Transfer
    Protocol)?????????SMTP(Simple Mail Transfer
    Protocol)??

7
  • 8.1.2 TCP/IP ??????????
  • ? 8.2 ??? TCP ?????????????????TCP ?? IP
    ????????????,??,IP ???????? TCP ??????UDP ??? IP
    ??????????,??????,???? IP ??????????? IP
    ???????????????????,?? IP ???????????,

8
  • 8.1.2 TCP/IP ??????????
  • ?????????????? IP ?????????????????????????????
    ????????,??????????????????????????,??,???????????
    ???????????????????,?????? TCP/IP ???????,???????
    IP ??,????

9
  • 8.1.2 TCP/IP ??????????
  • ?????????????????????,??????????????????,??
    Web ????????? 80???????,IP ??????????????

10
  • 8.1.2 TCP/IP ??????????
  • ?????????????????????,??????????????????,??
    Web ????????? 80???????,IP ??????????????

11
  • IP ????????????????? IP ?????? 8.2 ?,IP
    ????????????????????????,IP ??????????????????????
    ??????????????????????,???????????????????????,???
    6 ?????,? 00-A0-0C-13-CC-78??????????????,???????
    ????????????????????? IP ?????,???????????,??,????
    ?????????????
  • ??????????????????????? TCP/IP
    ????????????????????,???????????,??????????????,??
    ???????????Linux ???????,???? IP
    ??????????????????,Linux ??????? IP
    ?????????????(???)??????????????,?????????????????
    ???????????

12
? 8.3Linux ???????
? TCP/IP ???????,Linux ??????????TCP/IP
????????,Linux???????5??? 8.3 ??? Linux ????????
13
  • (1) BSD socket???????BSD socket????,??socket?????
    struct socket?????????????????/net/socket.c
    /net/protocols.c ??
  • (2) INET socket?BSD socket???????????????,????tcp
    /ip,????AF_INET???socket?,???????????,?????struct
    sock???
  • ???????/net/ipv4/protocol.c?
    /net/ipv4/af_inet.c?/net/core/sock.c ??
  • (3)TCP/UDP?????????,????struct
    inet_protocol?struct proto??????????????????/net/
    ipv4/udp.c?/net/ipv4/datagram.c?/net/ipv4/tcp.c?/n
    et/ipv4/tcp_input.c?/net/ipv4//tcp_output.c?
    /net/ipv4/tcp_minisocks.c?/net/ipv4/tcp_output.c?/
    net/ipv4/tcp_timer.c ??
  • (4)IP?????????,????struct packet_type????????????
    /net/ipv4/ip_forward.c?ip_fragment.c?ip_input.c?i
    p_output.c ??
  • (5)?????????????????struct net_device??,??????dev
    .c?,??????/driver/net????
  • ????????????,????????,??????,???????

14
  • 8.2 ???
  • ?????(socket)????????????????I/O?????????
    ? ????,??socket??????socket??????socket????????
  • 8.2.1 ??????
  • ??????TCP/IP???????????????????????????????
    ?????????,?????????????????????
  • ?????????????,??????????????????????????
    ?????????????????????,?????????????????
  • Linux?BSD ???????????????
  • 1. ??(stream)
  • ??????????????????????????????????????IN
    ET????TCP????????? ?PF_INET?????,SOCK_STREAM??????
    ??????inet_stream_ops, ?????????tcp_prot?

15
  • 2. ???(Datagram)
  • ????????????????????????????,?????????????????
    ??????????????,????????????????????????????UDP????
    ??????????????(SOCK_DGRAM)??????????inet_dgram_ops
    ,?????????udp_prot?
  • 3. ??(Raw)
  • ?????????????????????????????(SOCK_RAW)???????
    ???inet_dgram_ops,?????????raw_prot?
  • 4. ??????(Reliable Delivered Messages)
  • ??????????????,??????????
  • 5. ?????(Sequenced Packets)
  • ?????????????,?????????????
  • 6. ???(Packet)
  • ??????BSD ?????,??Linux ??????????????????????
    ??

16
  • 8.2.2?????????????
  • ?????Unix????? (file descriptor)
    ?????????????
  • Unix??????????I/O???,????????????????????
    ??????????????????????????????????????????????????
    ?????
  • ??????????????,??????????????????open()??
    ?????????????????????????,????????????????????????
    ???????????????????
  • ??,???????????????????,???????,??????????
    ?????,?????????,??????????????????????????????????
    ?????????????,???????????????????

17
1. BSD???
?8.1 BSD????????
?? ??
release ?????
bind ??????
connect ??????(TCP)?????????(UDP)
socketpair ?????????????
accept ????????
getname ??????
ioctl ??ioctl()??
listen ????????????
shutdown ??????????????
setsockopt ?????????
getsockopt ?????????
sendmsg ?????
recvmsg ?????
mmap ????????
sendpage ??????(????sendfile())
sk ??????sock,??????????
18
  • ?Linux?,BSD?????????????(sockfs)?????????
    ?????????????????????????????????????????????,????
    ?,??????,?????????????????????????????????????sock
    et(family,type,protocol)??????????family,???type,?
    ??protocol?????,Linux?????????????????????????????
    ????????????,???????BSD???,??????????sockfs?????in
    ode?BSD?????????socket??????,??u.socket_i??????,u.
    socket_I?sockfs?????inode??????????
  • ???BSD??????????????
  • inode ??sockfs?inode????
  • File ??sockfs????????
  • State ??socket?????SS_FREE(???),SS_UNCNNECTE
    D(???),SS_CONNECTING(????),SS_CONNECTED(???),SS_DI
    SCONNECTING(??????)?
  • Ops ????proto_ops????,????socket?????????????
    ???socket??????????????????????????????,?????????
    ??????????????????
  • ?8.1 ???BSD?????????

19
?? ??
close ?????
connect ????????????
disconnect ????
accept ??????
ioctl Ioctl
init ??INET?????
destroy ????
shutdown ????????????
setsockopt ??socket
getsockopt ??socket??
sendmsg ??
recvmsg ??
bind ??????
backlog_rcv ??????????????
hash ?INET??????hash?
unhash ????
get_port ?INET????????
2. INET???
?8.2 INET????? ???
20
  • INET????struct sock??????????TCP/IP???BSD?
    ??????INET???????sk???BSD??????????????,??INET???
    ??TCP/IP???????????????????INET???,????????????IP?
    ?,?????????,???????????????????,?????????????
  • INET?????????????????????proto????????,?8
    .2???????
  • INET?????????BSD??????????????,?????,??BS
    D??????????INET???????
  • sock??????80??,????????????????????????

21
  • 3. ????????
  • ??????????,??????????
  • ?? socket()??????????(sockfs)????????????????
    ?????????????,??????????????????????,?????????????
    ????????????,???????????
  • (1) ??????????socket_file_ops,
    ??????????(proto_ops)???,?????????????????(proto)?
    ???
  • (2) ??????socket????,??????sock?????????????i
    node?u.socket_i?????????????????
  • (3) ???????????????????, ??????????????????,?
    ??????????????????

22
  • 8.2.3 ???????
  • 1. sock_init
  • ?2.2?2.4???????,init/main.c?start_kernel????kernel
    _thread???init??,?init?????????do_basic_setup???,?
    ?????net/socket.c???sock_init??,??????TCP/IP???,??
    ?ipx?????
  • ???????,sock_init???????????
  • for (i 0 i lt NPROTO i)net_familiesi
    NULLsk_init()proto_init()
  • ??net_families?include/linux/net.h????
  • struct net_proto_familyint familyint
    (create)(struct socket sock, int protocol)
    // These are counters for the number of different
    methods of each we supportshort
    authenticationshort encryptionshort
    encrypt_net
  • ??????????,?create?callback???????,??AF_INET??????
    ???TCP/ICMP??????

23
  • sk_init???net/core/sock.c??
  • struct sock sk_alloc(int family, int priority,
    int zero_it)struct sock sk
    kmem_cache_alloc(sk_cachep, priority)if(sk)
    if (zero_it)memset(sk, 0, sizeof(struct
    sock))sk-gtfamily familyreturn sk
  • proto_init?????????
  • void _init proto_init(void)extern struct
    net_proto protocolsstruct net_proto propro
    protocolswhile (pro-gtname !
    NULL)(pro-gtinit_func)(pro)pro
  • struct net_proto?include/linux/net.h?
  • struct net_protoconst char name // Protocol
    namevoid (init_func)(struct net_proto ) //
    Bootstrap
  • ?protocols???net/protocols.c???,??????????????,???
    ????AF_INET?AF_PACKET?????????inet_proto_init?pack
    et_proto_init ?

24
  • 2. IPv4???PACKET??????
  • ????IPv4???PACKET????????????PACKET??????????,????
    ??????,??PACKET??,?net/packet/af_packet.c????packe
    t_proto_init????
  • void _init packet_proto_init(struct net_proto
    pro)sock_register(packet_family_ops)registe
    r_netdevice_notifier(packet_netdev_notifier)
  • ???sock_register?????,?????net/socket.c????net_fam
    ilies???????
  • int sock_register(struct net_proto_family
    ops)if (ops-gtfamily gt NPROTO)
    printk(KERN_CRIT "protocol d gt
    NPROTO(d)\n",ops-gtfamily, NPROTO)return
    -ENOBUFSnet_familiesops-gtfamilyopsreturn
    0

25
  • ??packet_netdev_notifier??structnotifier_block,???
    ??include/linux/notifier.h?????
  • struct notifier_blockint (notifier_call)(struc
    t notifier_block self, unsigned long, void
    )struct notifier_block nextint priority
  • register_netdevice_notifier????net/core/dev.c?,??
  • int register_netdevice_notifier(struct
    notifier_block nb)return notifier_chain_regist
    er(netdev_chain, nb)
  • notifier_chain_register???include/linux/notifier.h
    ??
  • extern _inline_ int notifier_chain_register(struc
    t notifier_block list, struct notifier_block
    n)while(list)if(n-gtpriority gt
    (list)-gtpriority)breaklist
    ((list)-gtnext)n-gtnext listlistnretu
    rn 0

26
  • ????block????,??block?????block??????notifier
    _chain_register???,?????????netdev_chain??????????
    ?interface???????????????????ioctl???,????????????
    ?????????????register_netdevice_notifier?????netde
    v_notifier????,??????????notifier_call?????interfa
    ce??????
  • inet_proto_init????net/ipv4/af_inet.c?,?????
    ifdef??????????????????CONFIG_NET_IPIP?CONFIG_NET
    _IPGRE?CONFIG_IP_FIREWALL?CONFIG_IP_MASQUERADE?CON
    FIG_IP_MROUTE ,???CONFIG_INET_RARP?CONFIG_PROC_FS?
    ??????,?????????

27
  • (void) sock_register(inet_family_ops)for(p
    inet_protocol_base p ! NULL) struct
    inet_protocol tmp(struct inet_protocol
    )p-gtnextinet_add_protocol(p)printk("ss",p-gt
    name,tmp?", ""\n")p tmp
  • arp_init()ip_init()tcp_v4_init(inet_family_op
    s)tcp_init()icmp_init(inet_family_ops)
  • rarp_ioctl_hook rarp_ioctlproc_net_register(p
    roc_net_rarp)proc_net_register(proc_net_raw)p
    roc_net_register(proc_net_snmp)proc_net_registe
    r(proc_net_netstat)proc_net_register(proc_net_
    sockstat)proc_net_register(proc_net_tcp)proc_
    net_register(proc_net_udp)

28
  • 3. struct inet_protocol?inet_add_protocol??
  • struct inet_protocol??????include/net/protocol.h??
    ?
  • struct inet_protocolint (handler)(struct
    sk_buff skb, unsigned short len)void
    (err_handler)(struct sk_buff skb, unsigned char
    dp, int len)struct inet_protocol
    nextunsigned char protocolunsigned char
    copy1void dataconst char name
  • ??????????callback?????,??????????,???copy???????,
    data????????????

29
  • inet_add_protocol???net/ipv4/protocol.c?
  • void inet_add_protocol(struct inet_protocol
    prot)unsigned char hashstruct inet_protocol
    p2
  • hash prot-gtprotocol (MAX_INET_PROTOS -
    1)prot -gtnext inet_protoshashinet_protosh
    ash protprot-gtcopy 0
  • p2 (struct inet_protocol ) prot-gtnextwhile(p2
    ! NULL)if (p2-gtprotocol
    prot-gtprotocol)prot-gtcopy 1breakp2
    (struct inet_protocol ) p2-gtnext
  • ?????????????hash?,??hash?????????,????hash???????
    ??????????

30
  • ??????CONFIG_SYSCTL ,?arp_init????net/ipv4/arp.c?
  • neigh_table_init(arp_tbl)dev_add_pack(arp_pack
    et_type)proc_net_register(proc_net_arp)
  • ??????dev_add_pack???
  • neigh_table_init???net/core/neighbour.c??
  • void neigh_table_init(struct neigh_table
    tbl)unsigned long now jiffies
  • tbl-gtparms.reachable_time neigh_rand_reach_time(
    tbl-gtparms.base_reachable_time)
  • init_timer(tbl-gtgc_timer)tbl-gtgc_timer.data
    (unsigned long)tbltbl-gtgc_timer.function
    neigh_periodic_timertbl-gtgc_timer.expires now
    tbl-gtgc_interval tbl-gtparms.reachable_timead
    d_timer(tbl-gtgc_timer)
  • init_timer(tbl-gtproxy_timer)tbl-gtproxy_timer.da
    ta (unsigned long)tbltbl-gtproxy_timer.function
    neigh_proxy_processskb_queue_head_init(tbl-gt
    proxy_queue)
  • tbl-gtlast_flush nowtbl-gtlast_rand now
    tbl-gtparms.reachable_time20tbl-gtnext
    neigh_tablesneigh_tables tbl
  • jiffies????????,?i386?????jiffies??50ms,????????ti
    mer,??????????timerlist???gc_timer???????????(garb
    age collect timer)???????arp?cache????,??????expir
    es??,????????????arp???proxy_timer??????????timer?
    function??????????,data????????????????

31
  • ???dev_add_pack??,??net/core/dev.c??
  • void dev_add_pack(struct packet_type pt)int
    hashifdef CONFIG_NET_FASTROUTE// Hack to
    detect packet socketif (pt-gtdata)
    netdev_fastroute_obstaclesdev_clear_fastrout
    e(pt-gtdev)endifif(pt-gttypehtons(ETH_P_ALL)
    )netdev_nitpt-gtnextptype_allptype_allpt
    elsehashntohs(pt-gttype)15pt-gtnext
    ptype_basehashptype_basehash pt
  • ????????,???ptype_all,?????????????,?????hash????
    ???,???????????

32
  • struct packet_type????include/linux/netdevice.h?,?
    ??????,??????????
  • unsigned short type // This is really
    htons(ether_type)?struct device dev //
    NULL is wildcarded hereint (func) (struct
    sk_buff ,struct device , struct packet_type
    )void data // Private to the packet
    typestruct packet_type next
  • ???func??????arp_packet_type??
  • static struct packet_type arp_packet_type
    _constant_htons(ETH_P_ARP),NULL, // All
    devicesarp_rcv,NULL,NULL

33
  • arp_init????????proc_net_register??,?????include/l
    inux/proc_fs.h?
  • static inline int proc_net_register(struct
    proc_dir_entry x)return proc_register(proc_ne
    t, x)
  • proc_register?fs/proc/root.c???,???????proc_net???
    ??????????????,??TCP??/proc???????????,????????/pr
    oc/net?????????????????????

34
  • 4. ip_init??
  • ip_init????net/ipv4/ip_output.c?
    (?????????CONFIG_PROC_FS,CONFIG_IP_MULTICAST?CONFI
    G_NET_CLS_ROUTE)
  • _initfunc(void ip_init(void))dev_add_pack(ip_p
    acket_type)ip_rt_init()proc_net_register(proc
    _net_igmp)

35
  • ip_rt_init????net/ipv4/route.c?????,????????
  • _initfunc(void ip_rt_init(void))struct
    proc_dir_entry entdevinet_init()ip_fib_init()
    rt_periodic_timer.function rt_check_expire//
    All the timers, started at system startup
    tendto synchronize?Perturb it a
    bit.rt_periodic_timer.expires jiffies
    net_random()ip_rt_gc_interval
    ip_rt_gc_intervaladd_timer(rt_periodic_timer)
  • proc_net_register((struct proc_dir_entry)
    PROC_NET_RTCACHE, 8, "rt_cache",S_IFREG
    S_IRUGO, 1, 0, 0,0, proc_net_inode_operations,r
    t_cache_get_info)ent create_proc_entry("net/
    rt_acct", 0, 0)ent-gtread_proc
    ip_rt_acct_read
  • ????????????notifier???????????,???/proc??????????
    ????proc_net_register????????,?create_proc_entry??
    ?/proc/net????????rt_acct,????????(account)???ip_r
    t_acct_read????,????????????ip_rt_acct????????????
    ?

36
  • devinet_init???net/ipv4/devinet.c?????,?????
  • register_gifconf(PF_INET, inet_gifconf)register_
    netdevice_notifier(ip_netdev_notifier)
  • register_netdevice_notifier????PACKET??????????,re
    gister_gifconf?????????SIOCGIFCONF????????????????
    ???PF_INET??,??????inet_gifconf???
  • ????net/ipv4/devinet.c??inet_gifconf??,????????int
    erface???????,?????name?address????

37
  • register_gifconf????net/core/dev.c?,??
  • static gifconf_func_t gifconf_list NPROTO
  • int register_gifconf(unsigned int family,
    gifconf_func_t gifconf)if (familygtNPROTO)re
    turn -EINVALgifconf_listfamily
    gifconfreturn 0
  • dev_ioctl?????????interface?I/O???gifconf_list????
    ?dev_ifconf??????,?dev_ifconf???dev_ioctl???????in
    terface?ioctl??????????????????gifconf????gifconf?
    ?generous interface configure,???????????

38
  • ip_fib_init????net/ipv4/fib_frontend.c?,??
  • _initfunc(void ip_fib_init(void))proc_net_regis
    ter((struct proc_dir_entry) PROC_NET_ROUTE, 5,
    "route",S_IFREG S_IRUGO, 1, 0, 0,0,
    proc_net_inode_operations,fib_get_procinfo)
  • fib_rules_init()register_netdevice_notifier(fib
    _netdev_notifier)register_inetaddr_notifier(fib
    _inetaddr_notifier)
  • register_inetaddr_notifier??????register_netdevice
    _notifier??,????notifier_chain_register??????????,
    ???????interface?????????????net/ipv4/fib_rules.c?
    ?fib_rules_init????????register_netdevice_notifier
    ????fib_rules_notifier??????
  • fib??IPv4 Forwarding Information
    Base,?IPv4??????

39
  • 5. tcp_v4_init???
  • ????net/ipv4/tcp_ipv4.c??tcp_v4_init??
  • _initfunc(void tcp_v4_init(struct
    net_proto_family ops))int err
  • tcp_inode.i_mode S_IFSOCKtcp_inode.i_sock
    1tcp_inode.i_uid 0tcp_inode.i_gid 0
  • tcp_socket-gtinode tcp_inodetcp_socket-gtstate
    SS_UNCONNECTEDtcp_socket-gttypeSOCK_RAW
  • if ((errops-gtcreate(tcp_socket,
    IPPROTO_TCP))lt0)panic("Failed to create the TCP
    control socket.\n")tcp_socket-gtsk-gtallocationGF
    P_ATOMICtcp_socket-gtsk-gtnum
    256tcp_socket-gtsk-gtip_ttl MAXTTL
  • tcp_inode????inode??,?tcp_socket??tcp_inode.u.sock
    et_I,??????????????????

40
  • tcp_socket??????,?????TCP???socket?????socke
    t,?TCP???????socket?,?socket????socket????????????
    ????SYN???RST,???????????SYN(???accept????socket)
    ?
  • ??AF_INET??,ops-gtcreate?????net/ipv4/af_inet
    .c?inet_create??,??????????socket????????,????????
    ?
  • ???socket?????,??????????????net/ipv4/tcp.c?
    ??tcp_init????????????hash??bucket????????????????
  • tcp_openreq_cacheptcp_bucket_cacheptcp_timewait_
    cacheptcp_ehashtcp_bhash
  • ??ehash??established hash, bhash??bind
    hash,????????????TCP_ESTABLISHED lt sk-gtstate lt
    TCP_CLOSE???SOCK??????????????????

41
  • ??net/ipv4/icmp.c??icmp_init????ICMP?????????
    ??,????ICMP,???tcp_v4_init???IPPROTO_TCP???IPPROTO
    _ICMP?proc_net_register?????????
  • ??,?Linux IP?????????????????,??????????
  • (1)dev_add_pack
  • ??????????????,????????????????????????,????
    ??????copy?????ETH_P_ALL???????????????????ptype_a
    ll???ptype_base hash????
  • (2)inet_add_protocol
  • ???????IP??????,??TCP?UDP??
  • (3)proc_net_register(?????proc_register)
  • ?/proc/net???????????????????????????????

42
  • 8.2.4 ????????
  • ????????????????,????????????A,????????B,??A
    ??????????B??????,?"Hello",?B??????
  • ??TCP????????,??????,?????UDP?????

43
  • 1. ?????
  • ???????,????????(socket),???????????????
  • ...
  • int sockfd
  • sockfdsocket(AF_INET,SOCK_DGRAM,0)
  • ...
  • ???????,?????0x80????????,???????????Socket?
    ????????,???????????????,?sys_socketcall??????,???
    ?????????sys_socket?socket_bind????

44
  • sys_socket??sock_create????struct
    socket??(?include/linux/net.h),???????????????????
    ?,????????????????(???inode,????????type????),????
    ??????????,?
  • ...
  • net_familiesfamily-gtcreate(sock, protocol)
  • ...
  • ?????????AF_INET,??????????inet_create()net_famil
    ies????,????????(net families)???,???????sock_regi
    ster???

45
  • ?struct socket??????????????struct
    sock???,????????????????inet_create?????????,?????
    ???(??socket????????),???????
  • ...
  • if (sk-gtprot-gtinit)
  • sk-gtprot-gtinit(sk)
  • ...
  • ?????SOCK_STREAM,???tcp_v4_init_sock???SOCK_D
    GRAM???socket?????????,??socket?????
  • ?inet_create()????,?????sock_map_fd?????????
    ???????????????file???????????????????????
  • ????????????????,????????????????????,?????
    ???????????

46
  • 2. ????
  • ???A??????,?????????(??sendto???????????)
  • ...
  • write(sockfd,"Hello",strlen("Hello"))
  • ...
  • write???????????sys_write,??????????????struc
    t file??????????(file????)???(file-gtf_mode
    FMODE_WRITE?true),????????????
  • ...
  • if (file-gtf_op (write file-gtf_op-gtwrite) !
    NULL)
  • ret write(file, buf, count, file-gtf_pos)
  • ...
  • ??f_op??struct file_operations????,?sock_map
    _fd?????socket_file_ops,?????(/net/socket.c)

47
  • static struct file_operations socket_file_ops
  • llseek sock_lseek,
  • read sock_read,
  • write sock_write,
  • poll sock_poll,
  • ioctl sock_ioctl,
  • mmap sock_mmap,
  • open sock_no_open, // special open code to
    disallow
  • open via /proc
  • release sock_close,
  • fasync sock_fasync,
  • readv sock_readv,
  • writev sock_writev
  • ??wirte?????????sock_write,??????????????str
    uct msghdr,?????sock_sendmsg.?
  • ...
  • sock-gtops-gtsendmsg(sock, msg, size, scm)
  • ...
  • ????sock-gtops?inet_create()???????????UDP???
    ?,sock-gtops???inet_dgram_ops(?sock-gtops
    inet_dgram_ops),????net/ipv4/Af_inet.c?

48
  • struct proto_ops inet_dgram_ops
  • family PF_INET,
  • release inet_release,
  • bind inet_bind,
  • connect inet_dgram_connect,
  • socketpair sock_no_socketpair,
  • accept sock_no_accept,
  • getname inet_getname,
  • poll datagram_poll,
  • ioctl inet_ioctl,
  • listen sock_no_listen,
  • shutdown inet_shutdown,
  • setsockopt inet_setsockopt,
  • getsockopt inet_getsockopt,
  • sendmsg inet_sendmsg,
  • recvmsg inet_recvmsg,
  • mmap sock_no_mmap,
  • ?????inet_sendmsg()??,??????????????????

49
  • ...
  • sk-gtprot-gtsendmsg(sk, msg, size)
  • ...
  • ?????????????inet_create?
  • ...
  • protudp_prot
  • ...
  • ??udp_sendmsg???,????????????UDP?(???,?????)
    ,?????ip_route_output,??????????,??
  • ...
  • ip_build_xmit(sk,
  • (sk-gtno_check UDP_CSUM_NOXMIT ?
  • udp_getfrag_nosum
  • udp_getfrag),
  • ufh, ulen, ipc, rt, msg-gtmsg_flags)
  • ...
  • ip_build_xmit????????????sk_buff,???????IP???

50
  • NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL,
  • rt-gtu.dst.dev,output_maybe_reroute)
  • ...
  • ??????????????,????????????output_maybe_rero
    ute,?output_maybe_reroute?????
  • return skb-gtdst-gtoutput(skb)
  • ?????ip_route_output????,ip_route_output?????????,
    ???????skb-gtdst?(???ip_route_output_slow?rth-gtu.
    dst.outputip_output)?
  • ??ip_output??,???????ip_finish_output????????
    ????????net_device??,?ip_finish_output?????????ip_
    route_output???????????????netfilter?NF_IP_POST_RO
    UTING??????,?????ip_finish_output2,???????
  • ...
  • hh-gthh_output(skb)
  • ...
  • ?????dev_queue_xmit,?????TCP/IP????,????????
    ???
  • ?????????,??????

51
  • ...
  • dev-gthard_start_xmit(skb, dev)
  • ...
  • ?????????????????,?????????????,?????3c509??(
    ??????3c509.c),?????????(el3_probe),?
  • ...
  • dev-gthard_start_xmit el3_start_xmit
  • ...
  • ?????IO??,?????????????,?????????
  • ???????????,???????,????????????????????????
    ???????????????(???TCP??)?

52
  • 3. ????
  • ???????????,?????????,??????????????????3c509?????
    ???el3_interrupt(???IRQ???????,????????request_ir
    q????)???????????????????IO??,?????(?IO?inw??),???
    ??????,??el3_rx(dev)??????
  • ?el3_rx?,???????????struct sk_buff,???????,???????
    ??netif_rx(dev.c)????CPU???,???????????????????net
    if_rx?????????????sk_buff???????,????????,????????
    ,?????????????
  • ...
  • _skb_queue_tail(queue-gtinput_pkt_queue,skb)
  • _cpu_raise_softirq(this_cpu, NET_RX_SOFTIRQ)
  • ...
  • ???????,???????????CPU??(??????????)????????schedu
    le()?,??????????,????????????

53
  • ...
  • if (softirq_active(this_cpu) softirq_mask(this_c
    pu))
  • goto handle_softirq
  • handle_softirq_back
  • ...
  • ...
  • handle_softirq
  • do_softirq()
  • goto handle_softirq_back
  • ...
  • ???????,?????net_dev_init?,????????????net_rx
    _action
  • ...
  • open_softirq(NET_TX_SOFTIRQ, net_tx_action,
    NULL)
  • ...
  • ??????????????,?????????NET_TX_SOFTIRQ???,???
    ??net_rx_action?
  • net_tx_action???2.2????net_bh???????,????????
    ???????,?????ptype_all,???????ptype_base16??????
    ?????????OSI7???????????????????????struct
    packet_type??,????????dev_add_pack????????ptype_al
    l?ptype_base??

54
  • ??packet_type??type??ETH_P_ALL?,?????ptype_a
    ll???,???ip_packet_type,????ptype_base16????????
    ????????????ETH_P_ALL????,?????????????,?????????
    ???????
  • skb-gtprotocol??el3_rx????,??????????????????
    ??,??????,????ETH_P_IP,???net_tx_action?,???IP????
    ??????ip_packet_type ????,??????ip_recv()?
  • pt_prev-gtfunc(????ip_recv)?????atomic_inc(s
    kb-gtusers)??(?2.2??????????skb_clone,????),???????
    sk_buff??????????????????????????????sk_buff?(????
    )???kfree_skb,?kfree_skb?????????????????,????????
    ????(_kfree_skb),????????1?

55
  • ??net/ipv4/ip_input.c????ip_recv???????????
    ????????(???????????????),???????????????2.4???,??
    ?????????,??????ip_recv??????,???????ip_recv??????
    ???ip_rcv_finish????ip_rcv_finish?,??????IP??(????
    ?)?IP?,??????ip_route_input????,???????skb-gtdst???
    ???????????????????????????,?????????????????????
    ???????????,???ip_local_deliver(/net/ipv4/ip_inpu
    t.c),????ip_forward(/net/ipv4/ip_forward.c)?skb-gtd
    st-gtinput???????????????????
  • ??????????,????ip_local_deliver?????????????
    ??,???????????????,??ip_local_deliver??????????,??
    ????(???sk_buff???),??????
  • ???????netfilter????????????,???/net/ipv4/ip
    _input.c?ip_local_deliver_finish??

56
  • ???(?TCP?UDP?RAW)????????inet_protos?(??inet
    _add_protocol)?ip_local_deliver_finish???IP???????
    ????(?iph-gtprotocol),??????????????,???udp,???ippr
    ot-gthandler????udp_rcv??
  • ???????????socket???????struct
    socket/struct sock???udp_rcv???udp_v4_lookup??????
    ???sock,?????????udp_queue_rcv_skb(/net/ipv4/udp.c
    )??????sock_queue_rcv_skb??,????sk_buff??????,????
    ??????

57
  • ...
  • kb_set_owner_r(skb, sk)
  • skb_queue_tail(sk-gtreceive_queue, skb)
  • if (!sk-gtdead)
  • sk-gtdata_ready(sk,skb-gtlen)
  • return 0
  • ...
  • sk-gtdata_ready????sock????????(sock_init_data)
  • ...
  • sk-gtdata_readysock_def_readable
  • ...
  • ????????
  • ??B??????,??????
  • ...
  • read(sockfd,buff,sizeof(buff))
  • ...
  • ?????????????sys_read(fs/read_write.c)??????
    ?write???,?????udp_recvmsg?????skb_recv_datagram,?
    ????????,?socket???????,?????(signal_pending(curre
    nt)),??data_ready???????????????(wake_up_interrupt
    ible(sk-gtsleep))?

58
  • 8.3 ??????
  • ??????,???????????(sk_buff)?????????,?????????????
    ?sk_buff???,??????????????????
  • 8.3.1 ????????????

Linux?????????????,?????????????????????????
?,Linux?????????????????????????????,????????????
?????????? ???????????????????,??????????????
????????,????????????????,??,??????????????,??????
??????????????,???????????????????????????????????
?????,????????????????????????,????????,??????????
???,Linux ????????(sk_buff)????????????????Linux??
???????????????????????????????sk_buff,sk_buff
????????????,?????????????????????????????????????
buffer??sk_buffs? ???????,???????????????
sk_buffs??????????????????
? 8.4 Linux ???????(sk_buff)
59
8.3.2 ???????????
? 8.5 sk_buff ???????
???? ??
Head ??????????????sk_buff ???????????,??????????
data ???????????????????????? sk_buff ???????????
tail ??????????????? data ????,??????????? sk_buff ???????????
end ????????????? head ????,sk_buff ?????,???????????
?? 8.4 ??,?? sk_buff ?????????4?????????????????
???,???????????????????,? 8.5 ????????????
60
sk_buff ???????,len ? truesize,?????????????
?????????????sk_buff ?????????????????????????????
??????,???????????? sk_buff ???? data ? tail
??,?????? 8.6 ??? ? sk_buff ?????,?????????
sk_buff ?????????????????????????????????????
sk_buff ???????? ? 8.6 sk_buff ???????????
???? ??
push ???? data ????????????? len ??????????????,?????????????????????
pull ???? push ???????,?? data ??????????,??? len ?????????????????????????????
put ???? tail ????????????? len ??????????????,?????????????????????
trim ???? put ???????,?? tail ??????????,??? len ?????????????????????????????
61
  • 8.3.3 ???????????
  • ??sk_buff???????????? ????????
    ????????sk_buffs???????,??????????????
    ????????????????????,????????????????
  • ???????
  • void append_frame(char buf,int len)
  • struct sk_buff skb alloc_skb(len,GFP_ATOMIC)
  • if (skb NULL)
  • my_dropped
  • else
  • skb_put(skb,len)
  • memcpy(skb-gtdata,data,len)
  • skb_append(my_list,skb)
  • void process_queue(void)
  • struct sk_buff skb
  • while ((skb skb_dequeue(my_list)) ! NULL)
  • process_data(skb)

62
? net/core/dev.c ?? netif_rx() ? net_bh()
????????,??????????????????,????????
skb_put() ??????????memcpy?????
???????????????,?????skb_push????????,??????????8.
5??
? 8.5
skb_put?skb_push ??skb_reserve()????????,?????????
????????? skb alloc_skb(lenheadspace,GFP_KERNE
L) skb_reserve(skb,headspace) skb_put(skb,len)
memcpy_fromfs(skb-gtdata,data,len) pass_to_m_proto
col(skb)
63
  • ???????
  • (1) skb_dequeue()
  • struct sk_buff _skb_dequeue(struct sk_buff_head
    list)
  • ????????????,??????????? ??? skb_queue_head ?
    skb_queue_tail ??????????
  • (2) skb_queue_head()
  • void skb_queue_head(struct sk_buff_head
    list,struct sk_buff newsk)
  • ????????????????
  • (3) skb_queue_tail()
  • void skb_queue_tail(struct sk_buff_head
    list,struct sk_buff newsk)
  • ????????????????
  • (4) skb_unlink()
  • void skb_unlink(struct sk_buff skb)
  • ?????????????????????,???????????

64
  • (5) skb_insert skb_append
  • void skb_insert(struct sk_buff old,struct
    sk_buff newsk)
  • void skb_append(struct sk_buff old,struct
    sk_buff newsk)
  • ????????????????????,??????????????????????
  • (6) alloc_skb()
  • define alloc_skb(size,priority)
  • (struct sk_buff ) kmalloc(size,priority)
  • ????????sk_buff? ???????? skb-gtfree1 ???????
  • (7) kfree_skb()
  • void kfree_skb(struct sk_buff skb)
  • ??????sk_buff?
  • (8) skb_clone()
  • struct sk_buff skb_clone(struct sk_buff
    skb,int gfp_mask)
  • ???sk_buff???,?????????
  • (9) skb_copy()
  • int skb_copy_datagram(struct sk_buff skb,int
    offset,
  • char to,int size)
  • ????sk_buff????

65
  • 8.3.4 ?????????????
  • sock_queue_rcv_skb() ????????????????????
  • sk my_find_socket(whatever)
  • if (sock_queue_rcv_skb(sk,skb) -1)
  • myproto_stats?dropped
  • kfree_skb(skb,FREE_READ)
  • return
  • ?????????????socket? ?????????,???????
  • ????,sock_alloc_send_skb() ????????,??????
  • skbsock_alloc_send_skb(sk,????)
  • if(skb NULL)
  • return -err
  • skb-gtsk sk
  • skb_reserve(skb,headroom)
  • skb_put(skb,len)
  • memcpy(skb-gtdata,data,len)
  • protocol_do_something(skb)
  • ???????? skb-gtsk sk,sock_alloc_send_skb()
    ?????????socket,????????kfree_skb()?,??sk?????????
    ???????????,????????????

66
  • 8.4 ????????
  • ?Linux?,????????????????????(packet)???????
  • 8.4.1 Linux????????
  • ????????????????????,???????????????,???????
    ????,??????????????????????????????????????????lin
    ux/net/core???,??linux/net/core/dev.c????????
  • Linux??TCP/IP,IPX,X.25,AppleTalk????,???????
    ?????linux/net/???????????????TCP/IP
    (IPv4)??,??????linux/net/ipv4?,??linux/net/ipv4/af
    _inet.c?????????

67
???????????linux/net/socket.c?????Socket????????
???????????????linux/net/socket.c? 1. ????
?
8.6 ????????? ???Linux?????????????(???????
)??drivers/net/skeleton.c ????????????????????????
?????????????????????,????????????,?netif_rx()??,?
????????????????????????????????????????????,??8.6
???
68
2. ???? ???Linux??????????,?????????????????
???????????(??????)?
ethn ????
trn ???
sln SLIP??
pppn PPP??
plipn PLIP??
tunln IP IP????
nrn NetROM ????
isdnn ISDN??
dummyn ???
lo ??????
69
  • 3. ????
  • ??register_netdev(struct device )??????
    struct device ??????????? ?????????struct
    device??????????????? unregister_netdev(struct
    device ) ???????????,??????????????????,?????????
    ,???? struct device dev_get(const char
    name)?????????????,??????,????????????????
  • ???????????
  • int register_my_device(void)
  • int i 0
  • for(i 0i lt 100i)
  • sprintf(mydevice?name,"mydevd",i)
  • if(dev_get(mydevice?name) NULL)
  • if(register_netdev(mydevice) ! 0)
  • return -EIO
  • return 0
  • printk("100 mydevs loaded? Unable to load more?
  • ")
  • return -ENFILE

70
  • 4. ????????
  • ????????????? struct device
    ?,????????????????? struct device???????
  • ?????(name)?????????,????????,???????????eth
    n???,????????????
  • ??????(Bus Interface Parameters)????????????
    ??????
  • ?????(Protocol Layer Variables)???????????
    mtu??

71
  • 8.4.2 ??????
  • ??????????????????,??include/linux/if_arp.h?
    28?????ARP??????????????????????????????????,?????
    ????????,???????????????????????????????3c501?????
    ???(???linux/drivers/net/3c501.c)??,??????????
  • ?????????????????????,?????????????

72
  • ??????????,?linux/drivers/net/3c501.c?el_sta
    rt_xmit() ???????3Com3c501???????????,????????????
    ????????,?????????
  • ??????????????????,??????????,?????????linux
    /drivers/net/3c501.c?el_interrupt()????,??????????
    ???????,??????????????,????????????el_interrupt()?
    ???????????????,??????????el_receive()?el_receive(
    )????????????,???????????????????(dev_alloc_skb(
    )),?????????????????,??????net/core/dev.c??????net
    if_rx(),???????
  • ????????????????,????????????????,??????????
    ??????????

73
  • ???????,???????????net/core/dev.c???netif_rx
    ()?????,????????????????????,?????????????????????
    ?,??????????????????????
  • ???????????????????????????????????,????????
    ???????,??????????????????????dev_base??,??linux/i
    nclude/linux/netdevice.h ????structnet_device
    dev_base?????????????????????????,?????????(?incl
    ude/linux/netdevice.h????)?net_device??,??????????
    ?????????????

74
  • struct net_device
  • open()
  • stop()
  • hard_start_xmit()
  • hard_header()
  • rebuild_header()
  • set_mac_address()
  • do_ioctl()
  • set_config()
  • hard_header_cache()
  • header_cache_update()
  • change_mtu()
  • tx_timeout()
  • hard_header_parse()
  • neigh_setup()
  • accept_fastpath()

75
  • ????????????????????,????dev_base???????,????
    ???????????net_device?????????????dev_base????????
    ????linux/drivers/net/net_init.c????register_netde
    v(dev_3c50)????????????net_device????????????????
    ????/drivers/net/3c501.c??el1_probe1()????
  • el1_probe1()
  • dev-gtopen el_open
  • dev-gthard_start_xmit el_start_xmit
  • dev-gttx_timeout el_timeout
  • dev-gtwatchdog_timeo HZ
  • dev-gtstop el1_close
  • dev-gtget_stats el1_get_stats
  • dev-gtset_multicast_list set_multicast_list
  • ether_setup(dev)
  • ?????????drivers/net/net_init.c??ether_setup
    (dev)????
  • ??dev-gthard_start_xmit el_start_xmit,??????
    ?????????????dev-gthard_start_xmit?????,el_start_x
    mit????????????????????

76
  • 8.4.3 ??????
  • ??????????????????,?????????????????????????
    ???????????????
  • ???????????net/core/dev.c?dev_queue_xmit()??
    ????????????????IP,??ARP??,???????????????????,???
    ?????????????dev_queue_xmit()?????????dev-gthard_st
    art_xmit(),dev-gthard_start_xmit()?????????????????
    ?????,??dev-gthard_start_xmit()???????el_start_xmit
    ()?

77
  • ???????,?????????net/core/dev.c
    ????netif_rx()???????????,????????????????????????
    ?????,???????????????????????????????????????,??,?
    ?dev_base?????????????????????????????????????
    net/core/dev.c ?static struct packet_ptype_base16
    ?????????????????????????,?????????????
  • ????,IP?????????ip_rcv()???,?ARP?????arp_rcv
    ()?,???????????????????????????

78
  • ??????????????????????,?????net/core/dev.c??
    dev_add_pack()???dev_remove_pack()???Ip?????net/ip
    v4/ip_output.c ???????void _init ip_init(void)???
  • dev_add_pack(ip_packet_type)

79
  • ?????????net/core/dev.c ????netif_rx()??????
    ??????,?????????,??????????,????????cpu_raise_soft
    irq (this_cpu?NET_RX_SOFTIRQ)???????,
    ?open_softirq(NET_RX_SOFTIRQ, net_rx_action,
    NULL)????,NET_RX_SOFTIRQ???????????net/core/dev.c?
    ?net_rx_action()?net_rx_action()?????????????ptype
    _base16????????,?????????????,????????????,?????
    ?????,???????????net_rx_action()??pt_prev-gtfunc()?
    ?????????IP????,ptype_baseETH_P_IP-gtfunc()(ip_rc
    v()),??????????IP???

80
  • 8.4.4 ??????
  • ??????????????????linux/include/linux/socket
    .h?,Linux?BSD Socket?????32???????,??PF_INET??????
    TCP/IP???(IPv4, ????????IPv4)???????????,?????????
    ????TCP/IP?????????inux/net/ipv4/????,linux/net/ip
    v4/af_inet.c?????????
  • ?Linux2.4.x??,???TCP/IP???????IGMP?TCP?UDP?I
    CMP?ARP?IP?????????,IP?ARP????????????????????,???
    ?????????(core)???????????????(TCP?UDP?IGMP?ICMP)?
    ??????IP???,???IP??????,????IP??????,???????Socket
    ???????????????IP?????????,??????????,????????????
    ???????

81
  • ?IP????,??????(core) ?????IP????,??ptype_bas
    eETH_P_IP???IP???????IP???ip_packet_type-gtip_rcv
    ()?????????IP?,????IP???????ip_rcv()(linux/net/ipv
    4/ip_input.c)??????ip_rcv()??????IP???????checksum
    ?????,????????????????????ip_rcv_finish()(???????
    NF_HOOK????)???,ip_rcv_finish()????????IP????????I
    P??????????,??????????????????????/linux/net/ipv4/
    route.c???ip_route_input()??????????,??????????
  • (1)???????(???????TCP,UDP,IGMP???????)
  • (2)?????????(????NAT??????)
  • (3)?????????(??????)

82
  • ?????????,ip_route_input()????net/ipv4/route
    .c?ip_route_input_slow(),?ip_route_input_slow()???
    rth-gtu.dst.inputip_local_deliver,?????IP????????,
    ?????????????????????????,???ip_rcv_finish()?ip_rc
    v_finish()?????skb-gtdst-gtinput(skb),????????ip_loc
    al_deliver()??,ip_local_deliver()??????ip_local_de
    liver_finish()?????????????
  • ???????(core)??????ptype_base16???????????
    ?????????,?/linux/net/ipv4/protocol.c?,??????????
    ?????struct net_protocolinet_protosMAX_INET_PROT
    OS,?????????IP????????????(IGMP,TCP,UDP,ICMP)????
    ??????????TCP???????

83
  • linux/net/ipv4/protocol.c line67
  • static struct inet_protocol tcp_protocol
  • handlertcp_v4_rcv,// ???????
  • err_handlertcp_v4_err,// ???????
  • nextIPPROTO_PREVIOUS,
  • protocolIPPROTO_TCP,
  • name"TCP"
  • ???????linux/net/ipv4/protocol.c,IP?????????
    ?????????TCP???linux/net/ipv4/protocol.c???,??????
    ?????????igmp_rcv()?udp_rcv()?icmp_rcv()??????????
    ?,???inet_protosMAX_INET_PROTOS???????????inet_a
    dd_protocol()???,???????inet_del_protocol()????ine
    t_protosMAX_INET_PROTOS???????linux/net/ipv4/af_
    inet.c inet_init()????????
  • inet_init()
  • printk(KERN_INFO "IP Protocols")
  • for (p inet_protocol_base p ! NULL)
  • struct inet_protocol tmp (struct inet_protocol
    ) p-gtnext
  • inet_add_protocol(p)// ????
  • printk("ss",p-gtname,tmp?", ""\n")
  • p tmp

84
  • ???Linux??????????????, ??linux?????dmesg???
    ????????????
  • IP Protocols ICMP,UDP,TCP,IGMP
  • ??,??inet_protos ????ICMP?UDP?TCP?IGMP????
    ?inet_protocol????,???????????????????
  • Linux 2.4.x?linux/include/linux/socket.h????
    32????BSDsocket??,????TCP/IP?IPX/SPX?X.25?,???????
    ????????TCP/IP????TCP????????,???UDP??????????????
    ????,??????????????,???????socket?????
  • ?BSD socket????????,????????????????????????
    ?????TCP?????????

85
  • sock_descriptor socket(AF_INET,SOCK_STREAM,0)
  • connect(sock_descriptor, ??,)
  • send(sock_descriptor,hello world)
  • recv(sock_descriptor,buffer,1024,0)
  • ??????????Inet??,?TCP/IP??,????????????,????
    ??TCP??,?????????socket?????????
  • ????,??socket?????????????(???AF_INET),???32
    ??????????????????,????????,????????????????????,?
    ????????????????,Inet???????TCP???

86
  • ???????????????,??????????????,?????????????
    ?????????????,????????????,??????????linux/net/soc
    ket.c???????
  • staticstructnet_proto_family net_familiesNPROTO
  • ??????????,net_families2?TCP/IP??,net_fami
    lies3?X.25??,???????????,?include/linux/socket.h
    ??????????????net_proto_family?ops???,????????????
    ????????????????ops???????sock_register()(linux/ne
    t/socket.c)???????,??TCP/IP???????????
  • int _init inet_init(void) (net/ipv4/af_inet.c)
  • (void) sock_register(inet_family_ops)
  • ????AF_INET(??????2),?????net_failies2
    ?????????

87
  • ????????,??????????????????????????????????,
    ??????????????????TCP/IP????,?????????
    net/ipv4/af_inet.c??struct list_head
    inetswSOCK_MAX???????????????????,????net/ipv4/a
    f_inet.c???????inetsw_array
  • static struct inet_protosw inetsw_array
  • typeSOCK_STREAM,
  • protocolIPPROTO_TCP,
  • prottcp_prot,
  • opsinet_stream_ops,
  • capability-1,
  • no_check0,
  • flagsINET_PROTOSW_PERMANENT,
  • ,
  • typeSOCK_DGRAM,
  • protocolIPPROTO_UDP,
  • protudp_prot,
  • opsinet_dgram_ops,
  • capability-1,
  • no_checkUDP_CSUM_DEFAULT,

88
  • ????,SOCK_STREAM????TCP??,SOCK_DGRAM????UDP?
    ?,SOCK_RAW????IP???????????inet_register_protosw()
    ?inetsw_array?????????inetswSOCK_MAX??????inet_i
    nit() (net/ipv4/af_inet.c) ?????????
  • ???????????socket,???accept?send()?connect()
    ?release()?bind()????????????ops?prot?????????????
    TCP??
  • typeSOCK_STREAM,
  • protocolIPPROTO_TCP,
  • prottcp_prot,
  • opsinet_stream_ops,
  • capability-1,
  • no_check0,
  • flagsINET_PROTOSW_PERMANENT,
  • ,
  • ???? tcp_prot???

89
  • struct proto tcp_prot
  • name"TCP",
  • closetcp_close,
  • connecttcp_v4_connect,
  • disconnecttcp_disconnect,
  • accepttcp_accept,
  • ioctltcp_ioctl,
  • inittcp_v4_init_sock,
  • destroytcp_v4_destroy_sock,
  • shutdowntcp_shutdown,
  • setsockopttcp_setsockopt,
  • getsockopttcp_getsockopt,
  • sendmsgtcp_sendmsg,
  • recvmsgtcp_recvmsg,
  • backlog_rcvtcp_v4_do_rcv,
  • hashtcp_v4_hash,
  • unhashtcp_unhash,
  • get_porttcp_v4_get_port,

90
  • 8.4.5 Socket???????
  • ????socket()?bind()?connect()?accept?send()?
    release()???linux/net/socket.c?????,??????????????
    ???????sys_????
  • ?????socket()?????,Socket(AF_INET,SOCK_STREA
    M,0)?????sys_socket(),sys_socket()????socket_creat
    ()socket_creat()???????????????net_families????
    ?????,???????????????????????,??????????create()??
    ??????????AF_INET,inet_creat()?????,?inet_creat()?
    ??????inetswSOCK_MAX???????,??????????socket,??S
    OCK_STREAM,TCP?????,
  • inet_creat()
  • answerinetsw ????????
  • sock-gtops answer-gtops
  • sk-gtprot answer-gtprot

91
  • 8.5 IP ?
  • 8.5.1 ?? IP ???
  • ???? device ??????? dev_base
    ??,???????????,?????????????? dev_base ???????
    device ???????????? device ???????????,?????????,?
    ??????????????????????????????,???????????????????
    ????????????????????????,????????????? sk_buff
    ??????????????????,???? sk_buff ??????? backlog
    ??????? backlog ???????,???? sk_buff ???????????
    sk_buff ??? backlog ???,?????????????????,????????
    ?????????????

92
  • ??????????????????,??,?????????????????????,
    ?????,??????????? sk_buff ??? backlog
    ??????????????????????????????
  • ? Linux ??????????,?????? ptype_all ???
    ptype_base ?????? packet_type ??????????packet_typ
    e ????????????????????????????????????????????????
    ???packet_type ??????????? ptype_all??????????????
    ???,?????? ptype_all ???ptype_base
    ??????,??????????????,????????????????????????????
    ????????,??????????????? sk_buff ????????
    packet_type ?????packet_type ????????(??????????),
    ??,????????????? sk_buff,sk_buff??????????????????
    ???

93
  • 8.5.2 ?? IP ???
  • ????????????,?????????(??????)???????????,????????
    ?? sk_buff ????????,?????????????,???????????????
  • sk_buff ??????????????,??,IP ??????????????,??????
    ???????????????????????? PPP ??????????,??????????
    ???????????????,? PPP ??????????????????????????,?
    ??????????,??????????????????????

94
  • ??????? IP ???,IP ????????? IP
    ?????????????????????? IP ??,??????? rtable
    ????????????????????? IP ???????? device
    ?????????????????????????????????,????????????????
    ????????????,???????? 8.2,????????????????????????
    ???????,????????????????????????? IP
    ???,????????????????????????????????? ARP
    ????,?????,?????????? ARP ????????????????????????
    ?????????,??????????? IP ??????? ARP ???????

95
  • 8.5.3 ?????????
  • ????????????????,???????????????????????????
    ????????,IP ????????????????????????????????????,I
    P ???????????,??????????????
  • ? IP ????????,IP ? IP ????????? IP
    ????????,??????? device ?????????? mtu
    ??,????????????(?????)?????? mtu ??????? IP
    ??????,????? IP ??????????(mtu ?????)????????
    sk_buff ??,??? IP ?????????,?????? IP
    ????????????????????? IP ?????????? IP ????
    sk_buff,??????

96
  • IP ?????????????????,?? IP
    ?????????????,??????,???????????????? IP ????,IP
    ???????????????????????????,IP ?????? ipq
    ????,???????????? IP ????? ipqueue ???????? IP
    ?????,IP ????? ipq ????,?????? ipfrag
    ???????????? ipq ?????????????IP ??????????????
    IP ?????,???????????? IP ???????????????,?????????
    sk_buff ??????????????????????????????,ipq ?????
    ipfrag ???,?????????????,??,?????????????????????

97
  • 8.5.4??????
  • IP?????????????????????????,IP??????????,??
    ???????????????????????,??? IP ????????????,??????
    ??? IP ???? sk_buff ?????????????,IP ?????????IP
    ?????????????,????,?????????????????????????,Linux
    ????????????????????????????,????????????????????
    ???????????????????,????????? ARP ????? IP
    ??????????
  • ARP ??????????ARP ???? ARP ???ARP
    ?????????? IP ??,? ARP ????????? IP ??????????ARP
    ????????,??,???????,????????????????? ARP
    ?????,??? IP ?????????? ARP ??,??????????? ARP
    ????

98
? 8.7 arp_table ????????
?? ??
?????? ? ARP ???????????
?????? ? ARP ???????????
?? ??????????,??????????
IP ?? ??????? IP ???
???? ?????????
??? ???????????
??? ?? timer_list ?????,???? ARP ????????
???? ? ARP ?????????
sk_buff ?? ????? IP ??? sk_buff ????
Linux ?? ARP ???????? arp_table
?????,??????????? IP ???????????????????? IP
?????,???????????? arp_table ???????????? 8.7 ???
99
  • ARP ???????(? arp_tables ??)??,???????
    arp_table ????????????????,???????????????? IP
    ??????????????????????,?????????????? arp_table
    ??????,Linux ??????????????,???? arp_table ???
    hh_cache ??????
  • ??????? IP ?????,???????? arp_table ??,ARP
    ???? ARP ????????????????? arp_talbe
    ??,??????????? sk_buff ? arp_table ??? sk_buff
    ???????????????? IP ??? sk_buff ??,??? sk_buff
    ??????????,????????? ARP ??????UDP ???????????,?
    TCP ???????? TCP ??????????? IP
    ???????????????,arp_table ??????????,???? sk_buff
    ??????????????????????? sk_buff ????

100
  • ARP ??????????? IP ??? ARP ???ARP ?????????
    packet_type ??????????????,??,???????? ARP
    ????????? ARP ????????? ARP ???,???????????
    device ???????????? ARP ?????
  • ?????????????ARP?????????????????,??????????
    ????????????????????????(neighbor
    cache)?,?????????arp cache??????????arp?????neighb
    or cache?????????/proc/net/arp?
  • ??neighbor cache????????neighbour?????????h
    a,?????????????????????????hh_cache?????,hh_cache?
    ??????????????????????????????????,??????????????
    ????????????????

101
  • ??????????????,? IP ??????????????????,?
    DHCP ???,????? IP ????????????? ARP ?????????,
    ARP ??????????????? arp_table ????????????????????
    ??,????????????????????????? arp_table
    ???????,????????????????? ARP ??????,????
    arp_table ?????????????????????,? ARP
    ???????????,?????????,??????????? ARP ?????

102
  • 8.5.5 IP ??
  • IP???????????????????????????????????????
    ,?????????????????????? IP ????,??????????????????
    ????????,?????????????????????????????,???????????
    ???????IP ?????????????????????????????????,??????
    ??????,??????????????? IP ????????????????????????
    ?????,?????,????????? IP ????????????,????????????
    ???,??????????????????????

103
  • ?????? BSD ?????? IOCTL ????????????????????
    ??????INET ?????????????????????? IP
    ???????????????,????????????????????????????,?????
    ?????????????????,??????????????? IP
    ???????????????????????????????(gated)?????,???
    BSD ?????? IOCTL ????????
  • ???OSPF??BGP???????,????????????????????????
    ??????????,????????????????????????????,??????????
    ,??????????
  • Linux?????????????????????(Routing
    Infomation Base,RIB),??????????(Forwarding
    Infomation Base,FIB)?

104
  • 1. ????
  • ????????????,????????FIB?????????????,??????
    ?????????,????????????????????????????,??????????
    ??????????,???????????????????????/proc/net/rt_cac
    he??????
  • ????? IP ???,???????????????????????,???????
    ???????????????????????????,IP ??????????,????????
    ???????????????????,??????????,??????????????????

105
  • ?????????? (ip_rt_hash_table),??????????
    rtable ??????????????????? IP ????????????????????
    ????????? IP ???????????,???????????????? rtable
    ???????,??? IP ???????? IP ???????? device
    ????,?????????????????????????????????????????????
    (? jiffies ??)?????????,?????????????????????????
    ?????????,?????????????????????? rtable
    ??????,??????????????????????????????,??? rtable
    ????,????????????,?????????? rtable
    ?????????????????,??????????????????????????????,?
    ?,???????,???????

106
  • ?????????????????rt_hash_table??????????IP
    ??????????,?????????????????,Linux???????????????,
    ?????????????????????????????????
  • 2. ???????
  • ?????(Forwarding Infomation
    Base,FIB)????????,???????????????????????FIB??????
    ???,?????????????????FIB???????????,??????????????
    ???
  • ? 8.7??????????????????????????????,????????
    ????????,?????????????????????? IP
    ???,????????????,?????????????????????????????????
    ?????
  • ?? IP ????? fib_zone ?????????? fib_zones
    ???????????? fib_zone,?????????? IP
    ????????????????????? fib_node ? fib_info
    ??????,?????????? fib_zone ??????fz_list
    ???????????????,?????????????? fib_node ?????

107
(No Transcript)
108
  • ????? IP ??,????????,?????????????????????
    IP ????????????????????????,????,?????????????,???
    ????????????????????????? metric,????????????????,
    ??? metric ????????,??????? IP ????,??? metric
    ???,??????

109
  • ?????
  • 1. ??????????????????????
  • 2. ?????????????,????????????????
  • 3. ??????????,???????????????????NOS???,??,NOS????
    ???????
  • 4.???????????????????????????????????????
  • 5.???????????????????????
  • 6.???????,NOS???????????????,NOS?????????
  • 7.??????????NOS???????????
  • 8.??????send??????(??)????????????????

110
  • 9.??????,????????????
  • 10.RPC???????????????????????????????
  • 11.?????????????(??????,????????,????????????????
    ?lt??,????,?????,????,?????gt)
  • 13.socket?????????????
  • 14.????socket????????????????
  • 15.????????????????????????
  • 16.??????,???????????????(??)????????????
Write a Comment
User Comments (0)
About PowerShow.com