Subversion Repositories Kolibri OS

Rev

Rev 3518 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2013. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  STACK.INC                                                      ;;
  7. ;;                                                                 ;;
  8. ;;  TCP/IP stack for KolibriOS                                     ;;
  9. ;;                                                                 ;;
  10. ;;    Written by hidnplayr@kolibrios.org                           ;;
  11. ;;                                                                 ;;
  12. ;;     Some parts of code are based on the work of:                ;;
  13. ;;      Mike Hibbett (menuetos network stack)                      ;;
  14. ;;      Eugen Brasoveanu (solar os network stack and drivers)      ;;
  15. ;;      mike.dld (kolibrios socket code)                           ;;
  16. ;;                                                                 ;;
  17. ;;     TCP part is based on 4.4BSD                                 ;;
  18. ;;                                                                 ;;
  19. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  20. ;;             Version 2, June 1991                                ;;
  21. ;;                                                                 ;;
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23.  
  24. $Revision: 3523 $
  25.  
  26. uglobal
  27.         net_10ms        dd ?
  28.         net_tmr_count   dw ?
  29. endg
  30.  
  31. MAX_NET_DEVICES         = 16
  32. ARP_BLOCK               = 1             ; true or false
  33.  
  34. MIN_EPHEMERAL_PORT      = 49152
  35. MIN_EPHEMERAL_PORT_N    = 0x00C0        ; same in Network byte order (FIXME)
  36. MAX_EPHEMERAL_PORT      = 61000
  37. MAX_EPHEMERAL_PORT_N    = 0x48EE        ; same in Network byte order (FIXME)
  38.  
  39. ; Ethernet protocol numbers
  40. ETHER_ARP               = 0x0608
  41. ETHER_IPv4              = 0x0008
  42. ETHER_IPv6              = 0xDD86
  43. ETHER_PPP_DISCOVERY     = 0x6388
  44. ETHER_PPP_SESSION       = 0x6488
  45.  
  46. ; PPP protocol numbers
  47. PPP_IPv4                = 0x2100
  48. PPP_IPV6                = 0x5780
  49.  
  50. ;Protocol family
  51. AF_UNSPEC               = 0
  52. AF_LOCAL                = 1
  53. AF_INET4                = 2
  54. AF_INET6                = 10
  55. AF_PPP                  = 777
  56.  
  57. ; Internet protocol numbers
  58. IP_PROTO_IP             = 0
  59. IP_PROTO_ICMP           = 1
  60. IP_PROTO_TCP            = 6
  61. IP_PROTO_UDP            = 17
  62.  
  63. ; PPP protocol number
  64. PPP_PROTO_ETHERNET      = 666
  65.  
  66. ; Socket types
  67. SOCK_STREAM             = 1
  68. SOCK_DGRAM              = 2
  69. SOCK_RAW                = 3
  70.  
  71. ; Socket options
  72. SO_ACCEPTCON            = 1 shl 0
  73. SO_BROADCAST            = 1 shl 1
  74. SO_DEBUG                = 1 shl 2
  75. SO_DONTROUTE            = 1 shl 3
  76. SO_KEEPALIVE            = 1 shl 4
  77. SO_OOBINLINE            = 1 shl 5
  78. SO_REUSEADDR            = 1 shl 6
  79. SO_REUSEPORT            = 1 shl 7
  80. SO_USELOOPBACK          = 1 shl 8
  81. SO_BINDTODEVICE         = 1 shl 9
  82.  
  83. SO_BLOCK                = 1 shl 10    ; TO BE REMOVED
  84. SO_NONBLOCK             = 1 shl 31
  85.  
  86. ; Socket flags for user calls
  87. MSG_PEEK                = 0x02
  88. MSG_DONTWAIT            = 0x40
  89.  
  90. ; Socket level
  91. SOL_SOCKET              = 0
  92.  
  93.  
  94. ; Socket States
  95. SS_NOFDREF              = 0x0001      ; no file table ref any more
  96. SS_ISCONNECTED          = 0x0002      ; socket connected to a peer
  97. SS_ISCONNECTING         = 0x0004      ; in process of connecting to peer
  98. SS_ISDISCONNECTING      = 0x0008      ; in process of disconnecting
  99. SS_CANTSENDMORE         = 0x0010      ; can't send more data to peer
  100. SS_CANTRCVMORE          = 0x0020      ; can't receive more data from peer
  101. SS_RCVATMARK            = 0x0040      ; at mark on input
  102. SS_ISABORTING           = 0x0080      ; aborting fd references - close()
  103. SS_RESTARTSYS           = 0x0100      ; restart blocked system calls
  104. SS_ISDISCONNECTED       = 0x0800      ; socket disconnected from peer
  105.  
  106. SS_ASYNC                = 0x0100      ; async i/o notify
  107. SS_ISCONFIRMING         = 0x0200      ; deciding to accept connection req
  108. SS_MORETOCOME           = 0x0400
  109.  
  110. SS_BLOCKED              = 0x8000
  111.  
  112.  
  113. SOCKET_MAXDATA          = 4096*32     ; must be 4096*(power of 2) where 'power of 2' is at least 8
  114.  
  115. ; Network driver types
  116. NET_TYPE_LOOPBACK       = 0
  117. NET_TYPE_ETH            = 1
  118. NET_TYPE_SLIP           = 2
  119.  
  120. MAX_backlog             = 20          ; maximum backlog for stream sockets
  121.  
  122. ; Error Codes
  123. ENOBUFS                 = 55
  124. ECONNREFUSED            = 61
  125. ECONNRESET              = 52
  126. ETIMEDOUT               = 60
  127. ECONNABORTED            = 53
  128.  
  129. ; Api protocol numbers
  130. API_ETH                 = 0
  131. API_IPv4                = 1
  132. API_ICMP                = 2
  133. API_UDP                 = 3
  134. API_TCP                 = 4
  135. API_ARP                 = 5
  136. API_PPPOE               = 6
  137. API_IPv6                = 7
  138.  
  139. HWACC_TCP_IPv4          = 1 shl 0
  140.  
  141. struct  NET_DEVICE
  142.  
  143.         type            dd ?    ; Type field
  144.         mtu             dd ?    ; Maximal Transmission Unit
  145.         name            dd ?    ; Ptr to 0 terminated string
  146.  
  147.         unload          dd ?    ; Ptrs to driver functions
  148.         reset           dd ?    ;
  149.         transmit        dd ?    ;
  150.  
  151.         bytes_tx        dq ?    ; Statistics, updated by the driver
  152.         bytes_rx        dq ?    ;
  153.         packets_tx      dd ?    ;
  154.         packets_rx      dd ?    ;
  155.  
  156.         state           dd ?    ; link state (0 = no link)
  157.         hwacc           dd ?    ; bitmask stating enabled HW accelerations (offload engines)
  158.  
  159. ends
  160.  
  161.  
  162. ; Exactly as it says..
  163. macro pseudo_random reg {
  164.         add     reg, [esp]
  165.         rol     reg, 5
  166.         xor     reg, [timer_ticks]
  167.         add     reg, [CPU_FREQ]
  168.         imul    reg, 214013
  169.         xor     reg, 0xdeadbeef
  170.         rol     reg, 9
  171. }
  172.  
  173. ; Network to Hardware byte order (dword)
  174. macro ntohd reg {
  175.  
  176.         rol     word reg, 8
  177.         rol     dword reg, 16
  178.         rol     word reg , 8
  179.  
  180. }
  181.  
  182. ; Network to Hardware byte order (word)
  183. macro ntohw reg {
  184.  
  185.         rol     word reg, 8
  186.  
  187. }
  188.  
  189.  
  190. include "queue.inc"
  191.  
  192. include "loopback.inc"
  193. include "ethernet.inc"
  194.  
  195. include "PPPoE.inc"
  196.  
  197. include "ARP.inc"
  198. include "IPv4.inc"
  199. include "IPv6.inc"
  200.  
  201. include "icmp.inc"
  202. include "udp.inc"
  203. include "tcp.inc"
  204.  
  205. include "socket.inc"
  206.  
  207.  
  208.  
  209. align 4
  210. uglobal
  211.  
  212.         NET_RUNNING     dd  ?
  213.         NET_DEFAULT     dd  ?
  214.         NET_DRV_LIST    rd  MAX_NET_DEVICES
  215.  
  216. endg
  217.  
  218.  
  219. ;-----------------------------------------------------------------
  220. ;
  221. ; stack_init
  222. ;
  223. ;  This function calls all network init procedures
  224. ;
  225. ;  IN:  /
  226. ;  OUT: /
  227. ;
  228. ;-----------------------------------------------------------------
  229. align 4
  230. stack_init:
  231.  
  232. ; Init the network drivers list
  233.         xor     eax, eax
  234.         mov     edi, NET_RUNNING
  235.         mov     ecx, (MAX_NET_DEVICES + 2)
  236.         rep     stosd
  237.  
  238.         PPPoE_init
  239.  
  240.         IPv4_init
  241. ;        IPv6_init
  242.         ICMP_init
  243.  
  244.         ARP_init
  245.         UDP_init
  246.         TCP_init
  247.  
  248.         SOCKET_init
  249.  
  250.         mov     [net_tmr_count], 0
  251.  
  252.         ret
  253.  
  254.  
  255. ;-----------------------------------------------------------------
  256. ;
  257. ; stack_handler
  258. ;
  259. ;  This function is called in kernel loop
  260. ;
  261. ;  IN:  /
  262. ;  OUT: /
  263. ;
  264. ;-----------------------------------------------------------------
  265. align 4
  266. stack_handler:
  267.  
  268.         cmp     [NET_RUNNING], 0
  269.         je      .exit
  270.  
  271.  
  272.         ; Test for 10ms tick
  273.         mov     eax, [timer_ticks]
  274.         cmp     eax, [net_10ms]
  275.         je      .exit
  276.         mov     [net_10ms], eax
  277.  
  278.         test    [net_10ms], 0x0f        ; 160ms
  279.         jnz     .exit
  280.  
  281.         TCP_timer_160ms
  282.  
  283.         test    [net_10ms], 0x3f        ; 640ms
  284.         jnz     .exit
  285.  
  286.         TCP_timer_640ms
  287.         ARP_decrease_entry_ttls
  288.         IPv4_decrease_fragment_ttls
  289.  
  290.   .exit:
  291.         ret
  292.  
  293.  
  294.  
  295. align 4
  296. NET_link_changed:
  297.  
  298.         DEBUGF  1,"NET_link_changed device=0x%x status=0x%x\n", ebx, [ebx + NET_DEVICE.state]
  299.  
  300. align 4
  301. NET_send_event:
  302.  
  303.         DEBUGF  1,"NET_send_event\n"
  304.  
  305. ; Send event to all applications
  306.         push    edi ecx
  307.         mov     edi, SLOT_BASE
  308.         mov     ecx, [TASK_COUNT]
  309.   .loop:
  310.         add     edi, 256
  311.         or      [edi + APPDATA.event_mask], EVENT_NETWORK2
  312.         loop    .loop
  313.         pop     ecx edi
  314.  
  315.         call    change_task
  316.  
  317.         ret
  318.  
  319.  
  320.  
  321. ;-----------------------------------------------------------------
  322. ;
  323. ; NET_add_device:
  324. ;
  325. ;  This function is called by the network drivers,
  326. ;  to register each running NIC to the kernel
  327. ;
  328. ;  IN:  Pointer to device structure in ebx
  329. ;  OUT: Device num in eax, -1 on error
  330. ;
  331. ;-----------------------------------------------------------------
  332. align 4
  333. NET_add_device:
  334.  
  335.         DEBUGF  1,"NET_Add_Device: %x\n", ebx   ;;; TODO: use mutex to lock net device list
  336.  
  337.         cmp     [NET_RUNNING], MAX_NET_DEVICES
  338.         jae     .error
  339.  
  340. ;----------------------------------
  341. ; Check if device is already listed
  342.         mov     eax, ebx
  343.         mov     ecx, MAX_NET_DEVICES    ; We need to check whole list because a device may be removed without re-organizing list
  344.         mov     edi, NET_DRV_LIST
  345.  
  346.         repne   scasd                   ; See if device is already in the list
  347.         jz      .error
  348.  
  349. ;----------------------------
  350. ; Find empty slot in the list
  351.         xor     eax, eax
  352.         mov     ecx, MAX_NET_DEVICES
  353.         mov     edi, NET_DRV_LIST
  354.  
  355.         repne   scasd
  356.         jnz     .error
  357.  
  358.         sub     edi, 4
  359.  
  360. ;-----------------------------
  361. ; Add device to the found slot
  362.         mov     [edi], ebx              ; add device to list
  363.  
  364.         mov     eax, edi                ; Calculate device number in eax
  365.         sub     eax, NET_DRV_LIST
  366.         shr     eax, 2
  367.  
  368.         inc     [NET_RUNNING]           ; Indicate that one more network device is up and running
  369.  
  370.         cmp     eax, 1                  ; If it's the first network device, try to set it as default
  371.         jne     @f
  372.         push    eax
  373.         call    NET_set_default
  374.         pop     eax
  375.        @@:
  376.  
  377.         call    NET_send_event
  378.  
  379.         DEBUGF  1,"Device number: %u\n", eax
  380.         ret
  381.  
  382.   .error:
  383.         or      eax, -1
  384.         DEBUGF  2,"Adding network device failed\n"
  385.         ret
  386.  
  387.  
  388.  
  389. ;-----------------------------------------------------------------
  390. ;
  391. ; NET_set_default
  392. ;
  393. ;  API to set the default interface
  394. ;
  395. ;  IN:  Device num in eax
  396. ;  OUT: Device num in eax, -1 on error
  397. ;
  398. ;-----------------------------------------------------------------
  399. align 4
  400. NET_set_default:
  401.  
  402.         DEBUGF  1,"NET_set_default: device=%x\n", eax
  403.  
  404.         cmp     eax, MAX_NET_DEVICES
  405.         jae     .error
  406.  
  407.         cmp     [NET_DRV_LIST+eax*4], 0
  408.         je      .error
  409.  
  410.         mov     [NET_DEFAULT], eax
  411.  
  412.         DEBUGF  1,"NET_set_default: succes\n"
  413.         ret
  414.  
  415.   .error:
  416.         or      eax, -1
  417.         DEBUGF  1,"NET_set_default: failed\n"
  418.         ret
  419.  
  420.  
  421. ;-----------------------------------------------------------------
  422. ;
  423. ; NET_Remove_Device:
  424. ;
  425. ;  This function is called by etwork drivers,
  426. ;  to unregister network devices from the kernel
  427. ;
  428. ;  IN:  Pointer to device structure in ebx
  429. ;  OUT: eax: -1 on error
  430. ;
  431. ;-----------------------------------------------------------------
  432. align 4
  433. NET_remove_device:
  434.  
  435.         cmp     [NET_RUNNING], 0
  436.         je      .error
  437.  
  438.         cmp     [NET_DRV_LIST], ebx
  439.         jne     @f
  440.         mov     [NET_DRV_LIST], 0
  441.         cmp     [NET_RUNNING], 1
  442.         je      @f
  443.         ; there are still active devices, find one and make it default
  444.         xor     eax, eax
  445.         mov     ecx, MAX_NET_DEVICES
  446.         mov     edi, NET_DRV_LIST
  447.         repe    scasd
  448.         je      @f
  449.         shr     edi, 2
  450.         dec     edi
  451.         mov     [NET_DEFAULT], edi
  452.        @@:
  453.  
  454. ;----------------------------
  455. ; Find the driver in the list
  456.  
  457.         mov     eax, ebx
  458.         mov     ecx, MAX_NET_DEVICES
  459.         mov     edi, NET_DRV_LIST+4
  460.  
  461.         repne   scasd
  462.         jnz     .error
  463.  
  464. ;------------------------
  465. ; Remove it from the list
  466.  
  467.         xor     eax, eax
  468.         mov     dword [edi-4], eax
  469.  
  470.         call    NET_send_event
  471.  
  472.         dec     [NET_RUNNING]
  473.         ret
  474.  
  475.   .error:
  476.         or      eax, -1
  477.         ret
  478.  
  479.  
  480.  
  481. ;-----------------------------------------------------------------
  482. ;
  483. ; NET_ptr_to_num
  484. ;
  485. ; IN:  ebx = ptr to device struct
  486. ; OUT: edi = -1 on error, device number otherwise
  487. ;
  488. ;-----------------------------------------------------------------
  489. align 4
  490. NET_ptr_to_num:
  491.         push    ecx
  492.  
  493.         mov     ecx, MAX_NET_DEVICES
  494.         mov     edi, NET_DRV_LIST
  495.  
  496.   .loop:
  497.         cmp     ebx, [edi]
  498.         jz      .found
  499.         add     edi, 4
  500.         dec     ecx
  501.         jnz     .loop
  502.  
  503.         ; repnz  scasd could work too if eax is used instead of ebx!
  504.  
  505.         or      edi, -1
  506.  
  507.         pop     ecx
  508.         ret
  509.  
  510.   .found:
  511.         sub     edi, NET_DRV_LIST
  512.         shr     edi, 2
  513.  
  514.         pop     ecx
  515.         ret
  516.  
  517. ;-----------------------------------------------------------------
  518. ;
  519. ; checksum_1
  520. ;
  521. ;  This is the first of two functions needed to calculate a checksum.
  522. ;
  523. ;  IN:  edx = start offset for semi-checksum
  524. ;       esi = pointer to data
  525. ;       ecx = data size
  526. ;  OUT: edx = semi-checksum
  527. ;
  528. ;
  529. ; Code was optimized by diamond
  530. ;
  531. ;-----------------------------------------------------------------
  532. align 4
  533. checksum_1:
  534.  
  535.         shr     ecx, 1
  536.         pushf
  537.         jz      .no_2
  538.  
  539.         shr     ecx, 1
  540.         pushf
  541.         jz      .no_4
  542.  
  543.         shr     ecx, 1
  544.         pushf
  545.         jz      .no_8
  546.  
  547.   .loop:
  548.         add     dl, [esi+1]
  549.         adc     dh, [esi+0]
  550.  
  551.         adc     dl, [esi+3]
  552.         adc     dh, [esi+2]
  553.  
  554.         adc     dl, [esi+5]
  555.         adc     dh, [esi+4]
  556.  
  557.         adc     dl, [esi+7]
  558.         adc     dh, [esi+6]
  559.  
  560.         adc     edx, 0
  561.         add     esi, 8
  562.  
  563.         dec     ecx
  564.         jnz     .loop
  565.  
  566.         adc     edx, 0
  567.  
  568.   .no_8:
  569.         popf
  570.         jnc     .no_4
  571.  
  572.         add     dl, [esi+1]
  573.         adc     dh, [esi+0]
  574.  
  575.         adc     dl, [esi+3]
  576.         adc     dh, [esi+2]
  577.  
  578.         adc     edx, 0
  579.         add     esi, 4
  580.  
  581.   .no_4:
  582.         popf
  583.         jnc     .no_2
  584.  
  585.         add     dl, [esi+1]
  586.         adc     dh, [esi+0]
  587.  
  588.         adc     edx, 0
  589.         inc     esi
  590.         inc     esi
  591.  
  592.   .no_2:
  593.         popf
  594.         jnc     .end
  595.  
  596.         add     dh, [esi+0]
  597.         adc     edx, 0
  598.   .end:
  599.         ret
  600.  
  601. ;-----------------------------------------------------------------
  602. ;
  603. ; checksum_2
  604. ;
  605. ;  This function calculates the final ip/tcp/udp checksum for you
  606. ;
  607. ;  IN:  edx = semi-checksum
  608. ;  OUT: dx = checksum (in INET byte order)
  609. ;
  610. ;-----------------------------------------------------------------
  611. align 4
  612. checksum_2:
  613.  
  614.         mov     ecx, edx
  615.         shr     ecx, 16
  616.         and     edx, 0xffff
  617.         add     edx, ecx
  618.  
  619.         mov     ecx, edx
  620.         shr     ecx, 16
  621.         add     dx, cx
  622.         test    dx, dx          ; it seems that ZF is not set when CF is set :(
  623.         not     dx
  624.         jnz     .not_zero
  625.         dec     dx
  626.   .not_zero:
  627.         xchg    dl, dh
  628.  
  629.         DEBUGF  1,"Checksum: %x\n", dx
  630.  
  631.         ret
  632.  
  633.  
  634.  
  635. ;----------------------------------------------------------------
  636. ;
  637. ;  System function to work with network devices (75)
  638. ;
  639. ;----------------------------------------------------------------
  640. align 4
  641. sys_network:                    ; FIXME: make default device easily accessible
  642.  
  643.         cmp     ebx, -1
  644.         jne     @f
  645.  
  646.         mov     eax, [NET_RUNNING]
  647.         jmp     .return
  648.  
  649.    @@:
  650.         cmp     bh, MAX_NET_DEVICES             ; Check if device number exists
  651.         jae     .doesnt_exist
  652.  
  653.         mov     esi, ebx
  654.         and     esi, 0x0000ff00
  655.         shr     esi, 6
  656.  
  657.         cmp     dword [esi + NET_DRV_LIST], 0   ; check if driver is running
  658.         je      .doesnt_exist
  659.  
  660.         mov     eax, [esi + NET_DRV_LIST]
  661.  
  662.         and     ebx, 0x000000ff
  663.         cmp     ebx, .number
  664.         ja      .doesnt_exist
  665.         jmp     dword [.table + 4*ebx]
  666.  
  667.   .table:
  668.         dd      .get_type               ; 0
  669.         dd      .get_dev_name           ; 1
  670.         dd      .reset                  ; 2
  671.         dd      .stop                   ; 3
  672.         dd      .get_ptr                ; 4
  673.         dd      .get_drv_name           ; 5
  674.         dd      .set_default            ; 6
  675.   .number = ($ - .table) / 4 - 1
  676.  
  677.   .get_type:                            ; 0 = Get device type (ethernet/token ring/...)
  678.  
  679.         mov     eax, [eax + NET_DEVICE.type]
  680.         jmp     .return
  681.  
  682.  
  683.   .get_dev_name:                        ; 1 = Get device name
  684.  
  685.         mov     esi, [eax + NET_DEVICE.name]
  686.         mov     edi, ecx
  687.  
  688.         mov     ecx, 64/4 ; max length
  689.         rep     movsd
  690.  
  691.         xor     eax, eax
  692.         jmp     .return
  693.  
  694.   .reset:                               ; 2 = Reset the device
  695.  
  696.         call    [eax + NET_DEVICE.reset]
  697.         jmp     .return
  698.  
  699.   .stop:                                ; 3 = Stop driver for this device
  700.  
  701.         call    [eax + NET_DEVICE.unload]
  702.         jmp     .return
  703.  
  704.  
  705.   .get_ptr:                             ; 4 = Get driver pointer
  706.  
  707.         jmp     .return
  708.  
  709.  
  710.   .get_drv_name:                        ; 5 = Get driver name
  711.  
  712.         xor     eax, eax
  713.         jmp     .return
  714.  
  715.  
  716.   .set_default:                         ; 6 = Set default device
  717.  
  718.         call    NET_set_default
  719.         jmp     .return
  720.  
  721.   .doesnt_exist:
  722.         mov     eax, -1
  723.  
  724.   .return:
  725.         mov     [esp+32], eax
  726.         ret
  727.  
  728.  
  729. ;----------------------------------------------------------------
  730. ;
  731. ;  System function to work with protocols  (76)
  732. ;
  733. ;----------------------------------------------------------------
  734. align 4
  735. sys_protocols:
  736.         cmp     bh, MAX_NET_DEVICES             ; Check if device number exists
  737.         jae     .doesnt_exist
  738.  
  739.         mov     esi, ebx
  740.         and     esi, 0x0000ff00
  741.         shr     esi, 6                          ; now we have the device num * 4 in esi
  742.         cmp     [esi + NET_DRV_LIST], 0         ; check if driver is running
  743.         je      .doesnt_exist
  744.  
  745.         push    .return                         ; return address (we will be using jumps instead of calls)
  746.  
  747.         mov     eax, ebx                        ; set ax to protocol number
  748.         shr     eax, 16                         ;
  749.  
  750.         cmp     ax, API_ETH
  751.         je      ETH_api
  752.  
  753.         cmp     ax, API_IPv4
  754.         je      IPv4_api
  755.  
  756.         cmp     ax, API_ICMP
  757.         je      ICMP_api
  758.  
  759.         cmp     ax, API_UDP
  760.         je      UDP_api
  761.  
  762.         cmp     ax, API_TCP
  763.         je      TCP_api
  764.  
  765.         cmp     ax, API_ARP
  766.         je      ARP_api
  767.  
  768.         cmp     ax, API_PPPOE
  769.         je      PPPoE_api
  770.  
  771.         cmp     ax, API_IPv6
  772.         je      IPv6_api
  773.  
  774.         add     esp, 4                           ; if we reached here, no function was called, so we need to balance stack
  775.  
  776.   .doesnt_exist:
  777.         mov     eax, -1
  778.  
  779.   .return:
  780.         mov     [esp+28+4], eax                 ; return eax value to the program
  781.         ret
  782.