Subversion Repositories Kolibri OS

Rev

Rev 3545 | Rev 3601 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3545 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2010-2013. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  zeroconfig.asm - Zeroconfig service for KolibriOS              ;;
7
;;                                                                 ;;
8
;;  Written by hidnplayr@kolibrios.org                             ;;
9
;;    Some code contributed by Derpenguin                          ;;
10
;;                                                                 ;;
11
;;  DHCP code is based on that by Mike Hibbet                      ;;
12
;       (DHCP client for menuetos)                                 ;;
13
;;                                                                 ;;
14
;;          GNU GENERAL PUBLIC LICENSE                             ;;
15
;;             Version 2, June 1991                                ;;
16
;;                                                                 ;;
17
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
18
 
19
format binary as ""
20
 
21
use32
22
        org     0x0
23
 
24
        db      'MENUET01'              ; 8 byte id
25
        dd      0x01                    ; header version
26
        dd      START                   ; start of code
27
        dd      IM_END                  ; size of image
28
        dd      (I_END+0x100)           ; memory for app
29
        dd      (I_END+0x100)           ; esp
30
        dd      0, 0                    ; I_Param, I_Path
31
 
32
; CONFIGURATION
33
 
34
TIMEOUT                 = 60            ; in seconds
35
BUFFER                  = 1024          ; in bytes
36
__DEBUG__               = 1             ; enable/disable
37
__DEBUG_LEVEL__         = 1             ; 1 = all, 2 = errors
38
 
39
; CONFIGURATION FOR LINK-LOCAL
40
 
41
PROBE_WAIT              = 1             ; second  (initial random delay)
42
PROBE_MIN               = 1             ; second  (minimum delay till repeated probe)
43
PROBE_MAX               = 2             ; seconds (maximum delay till repeated probe)
44
PROBE_NUM               = 3             ;         (number of probe packets)
45
 
46
ANNOUNCE_NUM            = 2             ;         (number of announcement packets)
47
ANNOUNCE_INTERVAL       = 2             ; seconds (time between announcement packets)
48
ANNOUNCE_WAIT           = 2             ; seconds (delay before announcing)
49
 
50
MAX_CONFLICTS           = 10            ;         (max conflicts before rate limiting)
51
 
52
RATE_LIMIT_INTERVAL     = 60            ; seconds (delay between successive attempts)
53
 
54
DEFEND_INTERVAL         = 10            ; seconds (min. wait between defensive ARPs)
55
 
56
 
57
include '../proc32.inc'
58
include '../macros.inc'
59
include '../debug-fdo.inc'
60
include '../network.inc'
61
include 'dhcp.inc'
62
include '../dll.inc'
63
 
64
 
65
Ip2dword:
66
    push    edx
67
 
68
    ; This code validates if the query is an IP containing 4 numbers and 3 dots
69
 
70
    xor     al, al            ; make al (dot count) zero
71
 
72
   @@:
73
    cmp     byte[edx],'0'     ; check if this byte is a number, if not jump to no_IP
74
    jl      no_IP             ;
75
    cmp     byte[edx],'9'     ;
76
    jg      no_IP             ;
77
 
78
    inc     edx               ; the byte was a number, so lets check the next byte
79
 
80
    cmp     byte[edx],0       ; is this byte zero? (have we reached end of query?)
81
    jz      @f                ; jump to next @@ then
82
    cmp     byte[edx],':'
83
    jz      @f
84
 
85
    cmp     byte[edx],'.'     ; is this byte a dot?
86
    jne     @r                ; if not, jump to previous @@
87
 
88
    inc     al                ; the byte was a dot so increment al(dot count)
89
    inc     edx               ; next byte
90
    jmp     @r                ; lets check for numbers again (jump to previous @@)
91
 
92
   @@:                        ; we reach this when end of query reached
93
    cmp     al,3              ; check if there where 3 dots
94
    jnz     no_IP             ; if not, jump to no_IP
95
 
96
    ; The following code will convert this IP into a dword and output it in eax
97
    ; If there is also a port number specified, this will be returned in ebx, otherwise ebx is -1
98
 
99
    pop     esi               ; edx (query address) was pushed onto stack and is now popped in esi
100
 
101
    xor     edx, edx          ; result
102
    xor     eax, eax          ; current character
103
    xor     ebx, ebx          ; current byte
104
 
105
  .outer_loop:
106
    shl     edx, 8
107
    add     edx, ebx
108
    xor     ebx, ebx
109
  .inner_loop:
110
    lodsb
111
    test    eax, eax
112
    jz      .finish
113
    cmp     al, '.'
114
    jz      .outer_loop
115
    sub     eax, '0'
116
    imul    ebx, 10
117
    add     ebx, eax
118
    jmp     .inner_loop
119
  .finish:
120
    shl     edx, 8
121
    add     edx, ebx
122
 
123
    bswap   edx               ; we want little endian order
124
 
125
    ret
126
 
127
no_IP:
128
    pop     edx
129
    xor     edx, edx
130
 
131
    ret
132
 
133
 
134
 
135
 
136
 
137
 
138
START:
139
        mcall   40, EVM_STACK2
140
 
141
        DEBUGF  1,">Zero-config service loaded\n"
142
 
143
  .wait:
144
        mcall   76, API_ETH + 4 ; get MAC of ethernet interface 0
145
        cmp     eax, -1
146
        jne     .start
147
 
148
        mcall   10
149
        jmp     .wait
150
 
151
  .start:
152
        mov     word[MAC], bx
153
        mov     dword[MAC+2], eax
154
        DEBUGF  1,"->MAC: %x-%x-%x-%x-%x-%x\n", [MAC+0]:2, [MAC+1]:2, [MAC+2]:2, [MAC+3]:2, [MAC+4]:2, [MAC+5]:2
155
 
156
        mcall   40, EVM_STACK
157
 
158
        mcall   68, 11
159
 
160
        stdcall dll.Load,@IMPORT
161
        or      eax, eax
162
        jnz     try_dhcp
163
 
164
        invoke  ini.get_str, path, str_ipconfig, str_type, inibuf, 16, 0
165
 
166
        cmp     dword[inibuf], 'stat'
167
        jne     try_dhcp
168
 
169
        invoke  ini.get_str, path, str_ipconfig, str_ip, inibuf, 16, 0
170
        mov     edx, inibuf
171
        call    Ip2dword
172
        mcall   76, API_IPv4 + 3, edx
173
 
174
        invoke  ini.get_str, path, str_ipconfig, str_gateway, inibuf, 16, 0
175
        mov     edx, inibuf
176
        call    Ip2dword
177
        mcall   76, API_IPv4 + 9, edx
178
 
179
        invoke  ini.get_str, path, str_ipconfig, str_dns, inibuf, 16, 0
180
        mov     edx, inibuf
181
        call    Ip2dword
182
        mcall   76, API_IPv4 + 5, edx
183
 
184
        invoke  ini.get_str, path, str_ipconfig, str_subnet, inibuf, 16, 0
185
        mov     edx, inibuf
186
        call    Ip2dword
187
        mcall   76, API_IPv4 + 7, edx
188
 
189
 
190
        mcall   -1
191
 
192
 
193
try_dhcp:
194
 
195
        DEBUGF  1,"->Trying DHCP\n"
196
 
197
        mcall   75, 0, AF_INET4, SOCK_DGRAM, 0          ; open socket (parameters: domain, type, reserved)
198
        cmp     eax, -1
199
        je      error
200
        mov     [socketNum], eax
201
 
202
        DEBUGF  1,"->Socket %x opened\n", eax
203
 
204
        mcall   75, 2, [socketNum], sockaddr1, 18       ; bind socket to local port 68
205
        cmp     eax, -1
206
        je      error
207
 
208
        DEBUGF  1,"->Socket Bound to local port 68\n"
209
 
210
        mcall   75, 4, [socketNum], sockaddr2, 18       ; connect to 255.255.255.255 on port 67
211
        cmp     eax, -1
212
        je      error
213
 
214
        DEBUGF  1,"->Connected to 255.255.255.255 on port 67\n"
215
 
216
        mov     [dhcpMsgType], 0x01                     ; DHCP discover
217
        mov     [dhcpLease], esi                        ; esi is still -1 (-1 = forever)
218
 
219
        mcall   26, 9                                   ; Get system time
220
        imul    eax, 100
221
        mov     [currTime], eax
222
 
223
build_request:                                          ; Creates a DHCP request packet.
224
 
225
        DEBUGF  1,"->Building request\n"
226
 
227
        stdcall mem.Alloc, BUFFER
228
        mov     [dhcpMsg], eax
229
        test    eax, eax
230
        jz      dhcp_error
231
 
232
            ;;; todo: skip this bullcrap
233
 
234
        mov     edi, eax
235
        mov     ecx, BUFFER
236
        xor     eax, eax
237
        rep     stosb
238
 
239
            ;; todo: put this in a buffer instead of writing bytes and words!
240
 
241
        mov     edx, [dhcpMsg]
242
 
243
        ; Boot protocol legacy
244
        mov     [edx], byte 0x01                ; Boot request
245
        mov     [edx+1], byte 0x01              ; Ethernet
246
        mov     [edx+2], byte 0x06              ; Ethernet h/w len
247
        mov     [edx+4], dword 0x11223344       ; xid                 ;;;;;;;
248
        mov     eax, [currTime]
249
        mov     [edx+8], eax                    ; secs, our uptime
250
        mov     [edx+10], byte 0x80             ; broadcast flag set
251
        mov     eax, dword [MAC]                ; first 4 bytes of MAC
252
        mov     [edx+28],dword eax
253
        mov     ax, word [MAC+4]                ; last 2 bytes of MAC
254
        mov     [edx+32],word ax
255
 
256
        ; DHCP extension
257
        mov     [edx+236], dword 0x63538263     ; magic cookie
258
        mov     [edx+240], word 0x0135          ; option DHCP msg type
259
        mov     al, [dhcpMsgType]
260
        mov     [edx+240+2], al
261
        mov     [edx+240+3], word 0x0433        ; option Lease time = infinity
262
        mov     eax, [dhcpLease]
263
        mov     [edx+240+5], eax
264
        mov     [edx+240+9], word 0x0432        ; option requested IP address
265
        mov     eax, [dhcp.ip]
266
        mov     [edx+240+11], eax
267
        mov     [edx+240+15], word 0x0437       ; option request list
268
        mov     [edx+240+17], dword 0x0f060301
269
 
270
        cmp     [dhcpMsgType], byte 0x01        ; Check which msg we are sending
271
        jne     request_options
272
 
273
        mov     [edx+240+21], byte 0xff         ; "Discover" options
274
 
275
        mov     [dhcpMsgLen], dword 262         ; end of options marker
276
        jmp     send_dhcpmsg
277
 
278
request_options:
279
        mov     [edx+240+21], word 0x0436       ; server IP
280
        mov     eax, [dhcpServerIP]
281
        mov     [edx+240+23], eax
282
 
283
        mov     [edx+240+27], byte 0xff         ; end of options marker
284
 
285
        mov     [dhcpMsgLen], dword 268
286
 
287
send_dhcpmsg:
288
        mcall   75, 6, [socketNum], [dhcpMsg], [dhcpMsgLen]     ; write to socket ( send broadcast request )
289
 
290
        mov     eax, [dhcpMsg]                          ; Setup the DHCP buffer to receive response
291
        mov     [dhcpMsgLen], eax                       ; Used as a pointer to the data
292
 
293
        mcall   23, TIMEOUT*10                          ; wait for data
294
 
295
read_data:                                              ; we have data - this will be the response
3562 hidnplayr 296
        mcall   75, 7, [socketNum], [dhcpMsg], BUFFER, 0   ; read data from socket
3545 hidnplayr 297
 
298
        DEBUGF  1,"->%d bytes received\n", eax
299
 
300
        cmp     eax, -1
301
        je      dhcp_error
302
 
303
        mov     [dhcpMsgLen], eax
304
 
305
; depending on which msg we sent, handle the response
306
; accordingly.
307
; If the response is to a dhcp discover, then:
308
;  1) If response is DHCP OFFER then
309
;  1.1) record server IP, lease time & IP address.
310
;  1.2) send a request packet
311
; If the response is to a dhcp request, then:
312
;  1) If the response is DHCP ACK then
313
;  1.1) extract the DNS & subnet fields. Set them in the stack
314
 
315
        cmp     [dhcpMsgType], 0x01             ; did we send a discover?
316
        je      discover
317
 
318
        cmp     [dhcpMsgType], 0x03             ; did we send a request?
319
        je      request
320
 
321
        call    dhcp_end                        ; we should never reach here ;)
322
        jmp     exit
323
 
324
discover:
325
        call    parse_response
326
 
327
        cmp     [dhcpMsgType], 0x02             ; Was the response an offer?
328
        je      send_request
329
 
330
        call    dhcp_end
331
        jmp     link_local
332
 
333
send_request:
334
        mov     [dhcpMsgType], 0x03             ; make it a request
335
        jmp     build_request
336
 
337
request:
338
        call    parse_response
339
 
340
        cmp     [dhcpMsgType], 0x05             ; Was the response an ACK? It should be
341
        jne     read_data                       ; NO - read next packets
342
 
343
        call    dhcp_end
344
 
345
        mcall   76, API_IPv4 + 3, [dhcp.ip]             ; ip
346
        mcall   76, API_IPv4 + 5, [dhcp.dns]            ; dns
347
        mcall   76, API_IPv4 + 7, [dhcp.subnet]         ; subnet
348
        mcall   76, API_IPv4 + 9, [dhcp.gateway]        ; gateway
349
 
350
        jmp     exit
351
 
352
dhcp_end:
353
        mcall   close, [socketNum]
354
        stdcall mem.Free, [dhcpMsg]
355
 
356
        ret
357
 
358
;***************************************************************************
359
;   Function
360
;      parseResponse
361
;
362
;   Description
363
;      extracts the fields ( client IP address and options ) from
364
;      a DHCP response
365
;      The values go into
366
;       dhcpMsgType,dhcpLease,dhcpClientIP,dhcpServerIP,
367
;       dhcpDNSIP, dhcpSubnet
368
;      The message is stored in dhcpMsg
369
;
370
;***************************************************************************
371
parse_response:
372
 
373
        DEBUGF  1,"Data received, parsing response\n"
374
        mov     edx, [dhcpMsg]
375
 
376
        push    dword [edx+16]
377
        pop     [dhcp.ip]
378
        DEBUGF  1,"Client: %u.%u.%u.%u\n", [edx+16]:1, [edx+17]:1, [edx+18]:1, [edx+19]:1
379
 
380
; TODO: check if there really are options
381
 
382
        mov     al, 240                         ; Point to first option
383
        movzx   ecx, al
384
 
385
  .next_option:
386
        add     edx, ecx
387
 
388
        mov     al, [edx]                       ; get message identifier
389
 
390
        cmp     al, 0xff                        ; End of options?
391
        je      .done
392
 
393
        cmp     al, 0
394
        je      .pad
395
 
396
; TODO: check if we still are inside the buffer
397
 
398
        inc     edx
399
        movzx   ecx, byte [edx]                 ; get data length
400
        inc     edx                             ; point to data
401
 
402
        cmp     al, dhcp_msg_type               ; Msg type is a single byte option
403
        je      .msgtype
404
 
405
        cmp     al, dhcp_dhcp_server_id
406
        je      .server
407
 
408
        cmp     al, dhcp_address_time
409
        je      .lease
410
 
411
        cmp     al, dhcp_subnet_mask
412
        je      .subnet
413
 
414
        cmp     al, dhcp_router
415
        je      .router
416
 
417
        cmp     al, dhcp_domain_server
418
        je      .dns
419
 
420
        DEBUGF  1,"Unsupported DHCP option: %u\n", al
421
 
422
        jmp     .next_option
423
 
424
  .pad:
425
        xor     ecx, ecx
426
        inc     ecx
427
        jmp     .next_option
428
 
429
  .msgtype:
430
        mov     al, [edx]
431
        mov     [dhcpMsgType], al
432
 
433
        DEBUGF  1,"DHCP Msg type: %u\n", al
434
        jmp     .next_option                    ; Get next option
435
 
436
  .server:
437
        mov     eax, [edx]
438
        mov     [dhcpServerIP], eax
439
        DEBUGF  1,"Server: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
440
        jmp     .next_option
441
 
442
  .lease:
443
        pusha
444
        mov     eax,[edx]
445
        bswap   eax
446
        mov     [dhcpLease],eax
447
        DEBUGF  1,"lease: %d\n",eax
448
        popa
449
        jmp     .next_option
450
 
451
  .subnet:
452
        push    dword [edx]
453
        pop     [dhcp.subnet]
454
        DEBUGF  1,"Subnet: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
455
        jmp     .next_option
456
 
457
  .router:
458
        push    dword [edx]
459
        pop     [dhcp.gateway]
460
        DEBUGF  1,"Gateway: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
461
        jmp     .next_option
462
 
463
  .dns:
464
        push    dword [edx]
465
        pop     [dhcp.dns]
466
        DEBUGF  1,"DNS: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
467
        jmp     .next_option
468
 
469
  .done:
470
        ret
471
 
472
 
473
 
474
dhcp_error:
475
        call    dhcp_end
476
 
477
link_local:
478
        call    random
479
        mov     cx, ax
480
        shl     ecx, 16
481
        mov     cx, 0xfea9                              ; IP 169.254.0.0 link local net, see RFC3927
482
        mcall   76, API_IPv4 + 3, ecx                   ; mask is 255.255.0.0
483
        DEBUGF  1,"Link Local IP assinged: 169.254.%u.%u\n", [generator+0]:1, [generator+1]:1
484
        mcall   76, API_IPv4 + 7, 0xffff
485
        mcall   76, API_IPv4 + 9, 0x0
486
        mcall   76, API_IPv4 + 5, 0x0
487
 
488
        mcall   5, PROBE_WAIT*100
489
 
490
        xor     esi, esi
491
   probe_loop:
492
        call    random                                  ; create a pseudo random number in eax (seeded by MAC)
493
 
494
        cmp     al, PROBE_MIN*100                       ; check if al is bigger then PROBE_MIN
495
        jae     @f                                      ; all ok
496
        add     al, (PROBE_MAX-PROBE_MIN)*100           ; al is too small
497
   @@:
498
 
499
        cmp     al, PROBE_MAX*100
500
        jbe     @f
501
        sub     al, (PROBE_MAX-PROBE_MIN)*100
502
   @@:
503
 
504
        movzx   ebx,al
505
        DEBUGF  1,"Waiting %u0ms\n",ebx
506
        mcall   5
507
 
508
        DEBUGF  1,"Sending Probe\n"
509
        mcall   76, API_ARP + 6
510
        inc     esi
511
 
512
        cmp     esi, PROBE_NUM
513
        jb      probe_loop
514
 
515
; now we wait further ANNOUNCE_WAIT seconds and send ANNOUNCE_NUM ARP announces. If any other host has assingned
516
; IP within this time, we should create another adress, that have to be done later
517
 
518
        DEBUGF  1,"Waiting %us\n", ANNOUNCE_WAIT
519
        mcall   5, ANNOUNCE_WAIT*100
520
        xor   esi, esi
521
   announce_loop:
522
 
523
        DEBUGF  1,"Sending Announce\n"
524
        mcall   76, API_ARP + 6
525
 
526
        inc     esi
527
        cmp     esi,ANNOUNCE_NUM
528
        je      @f
529
 
530
        DEBUGF  1,"Waiting %us\n", ANNOUNCE_INTERVAL
531
        mcall   5, ANNOUNCE_INTERVAL*100
532
        jmp     announce_loop
533
   @@:
534
 
535
 
536
error:
537
        DEBUGF  1,"Socket error\n"
538
exit:   ; we should, instead of closing, detect ARP conflicts and detect if cable keeps connected ;)
539
        mcall   -1
540
 
541
 
542
random:  ; Pseudo random actually
543
 
544
        mov     eax, [generator]
545
        add     eax, -43ab45b5h
546
        ror     eax, 1
547
        bswap   eax
548
        xor     eax, dword[MAC]
549
        ror     eax, 1
550
        xor     eax, dword[MAC+2]
551
        mov     [generator], eax
552
 
553
        ret
554
 
555
; DATA AREA
556
 
557
align 16
558
@IMPORT:
559
 
560
library \
561
        libini,'libini.obj'
562
 
563
import  libini, \
564
        ini.get_str,'ini_get_str'
565
 
566
include_debug_strings
567
 
568
str_ip          db 'ip', 0
569
str_subnet      db 'subnet', 0
570
str_gateway     db 'gateway', 0
571
str_dns         db 'dns', 0
572
str_ipconfig    db 'ipconfig', 0
573
str_type        db 'type', 0
574
 
575
 
576
sockaddr1:
577
 
578
        dw AF_INET4
579
        dw 68 shl 8     ; local port
580
        dd 0            ; local IP
581
 
582
        rb 10
583
 
584
 
585
sockaddr2:
586
 
587
        dw AF_INET4
588
        dw 67 shl 8     ; destination port
589
        dd -1           ; destination IP
590
 
591
        rb 10
592
 
593
path            db  '/sys/network.ini'
594
 
595
IM_END:
596
 
597
inibuf          rb 16
598
 
599
dhcpMsgType     db  ?
600
dhcpLease       dd  ?
601
dhcpServerIP    dd  ?
602
 
603
dhcp:
604
.ip             dd  ?
605
.subnet         dd  ?
606
.dns            dd  ?
607
.gateway        dd  ?
608
 
609
 
610
dhcpMsgLen      dd  ?
611
socketNum       dd  ?
612
 
613
MAC             dp  ?
614
 
615
currTime        dd  ?
616
generator       dd  ?
617
 
618
dhcpMsg         dd  ?
619
 
620
I_END: