Rev 6908 | Rev 7679 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 6908 | Rev 7678 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
2 | ;; ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2016. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2019. All rights reserved. ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
5 | ;; ;; |
6 | ;; IPv4.INC ;; |
6 | ;; IPv4.INC ;; |
7 | ;; ;; |
7 | ;; ;; |
8 | ;; Part of the TCP/IP network stack for KolibriOS ;; |
8 | ;; Part of the TCP/IP network stack for KolibriOS ;; |
Line 14... | Line 14... | ||
14 | ;; GNU GENERAL PUBLIC LICENSE ;; |
14 | ;; GNU GENERAL PUBLIC LICENSE ;; |
15 | ;; Version 2, June 1991 ;; |
15 | ;; Version 2, June 1991 ;; |
16 | ;; ;; |
16 | ;; ;; |
17 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
17 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
Line 18... | Line 18... | ||
18 | 18 | ||
Line 19... | Line 19... | ||
19 | $Revision: 6908 $ |
19 | $Revision: 7678 $ |
20 | 20 | ||
Line 21... | Line 21... | ||
21 | IPv4_MAX_FRAGMENTS = 64 |
21 | IPv4_MAX_FRAGMENTS = 64 |
Line 59... | Line 59... | ||
59 | Owner dd ? ; Pointer to structure of driver |
59 | Owner dd ? ; Pointer to structure of driver |
60 | rb 2 ; to match ethernet header size ;;; FIXME |
60 | rb 2 ; to match ethernet header size ;;; FIXME |
61 | ; Ip header begins here (we will need the IP header to re-construct the complete packet) |
61 | ; Ip header begins here (we will need the IP header to re-construct the complete packet) |
62 | ends |
62 | ends |
Line 63... | Line 63... | ||
63 | 63 | ||
64 | struct IPv4_ROUTE |
64 | ;struct IPv4_ROUTE |
65 | 65 | ; |
|
66 | Destination dd ? |
66 | ; Destination dd ? |
67 | Gateway dd ? |
67 | ; Gateway dd ? |
68 | Flags dd ? |
68 | ; Flags dd ? |
69 | Use dd ? |
69 | ; Use dd ? |
70 | Interface dd ? |
70 | ; Interface dd ? |
71 | 71 | ; |
|
72 | ends |
- | |
Line 73... | Line 72... | ||
73 | 72 | ;ends |
|
74 | 73 | ||
Line 75... | Line 74... | ||
75 | uglobal |
74 | uglobal |
76 | align 4 |
75 | align 4 |
77 | 76 | ||
78 | IP_LIST rd NET_DEVICES_MAX |
77 | IPv4_address rd NET_DEVICES_MAX |
79 | SUBNET_LIST rd NET_DEVICES_MAX |
78 | IPv4_subnet rd NET_DEVICES_MAX |
Line 80... | Line 79... | ||
80 | DNS_LIST rd NET_DEVICES_MAX |
79 | IPv4_nameserver rd NET_DEVICES_MAX |
81 | GATEWAY_LIST rd NET_DEVICES_MAX |
80 | IPv4_gateway rd NET_DEVICES_MAX |
82 | BROADCAST_LIST rd NET_DEVICES_MAX |
81 | IPv4_broadcast rd NET_DEVICES_MAX |
Line 83... | Line 82... | ||
83 | 82 | ||
Line 84... | Line 83... | ||
84 | IPv4_packets_tx rd NET_DEVICES_MAX |
83 | IPv4_packets_tx rd NET_DEVICES_MAX |
Line 85... | Line 84... | ||
85 | IPv4_packets_rx rd NET_DEVICES_MAX |
84 | IPv4_packets_rx rd NET_DEVICES_MAX |
Line 86... | Line 85... | ||
86 | IPv4_packets_dumped rd NET_DEVICES_MAX |
85 | IPv4_packets_dumped rd NET_DEVICES_MAX |
Line 98... | Line 97... | ||
98 | ; ; |
97 | ; ; |
99 | ;-----------------------------------------------------------------; |
98 | ;-----------------------------------------------------------------; |
100 | macro ipv4_init { |
99 | macro ipv4_init { |
Line 101... | Line 100... | ||
101 | 100 | ||
102 | xor eax, eax |
101 | xor eax, eax |
103 | mov edi, IP_LIST |
102 | mov edi, IPv4_address |
104 | mov ecx, 7*NET_DEVICES_MAX + (sizeof.IPv4_FRAGMENT_slot*IPv4_MAX_FRAGMENTS)/4 |
103 | mov ecx, 7*NET_DEVICES_MAX + (sizeof.IPv4_FRAGMENT_slot*IPv4_MAX_FRAGMENTS)/4 |
Line 105... | Line 104... | ||
105 | rep stosd |
104 | rep stosd |
Line 114... | Line 113... | ||
114 | ;-----------------------------------------------------------------; |
113 | ;-----------------------------------------------------------------; |
115 | macro ipv4_decrease_fragment_ttls { |
114 | macro ipv4_decrease_fragment_ttls { |
Line 116... | Line 115... | ||
116 | 115 | ||
Line 117... | Line 116... | ||
117 | local .loop, .next |
116 | local .loop, .next |
118 | 117 | ||
119 | mov esi, IPv4_FRAGMENT_LIST |
118 | mov esi, IPv4_fragments |
120 | mov ecx, IPv4_MAX_FRAGMENTS |
119 | mov ecx, IPv4_MAX_FRAGMENTS |
121 | .loop: |
120 | .loop: |
122 | cmp [esi + IPv4_FRAGMENT_slot.ttl], 0 |
121 | cmp [esi + IPv4_FRAGMENT_slot.ttl], 0 |
Line 242... | Line 241... | ||
242 | ;-------------------------------- |
241 | ;-------------------------------- |
243 | ; Check if destination IP matches |
242 | ; Check if destination IP matches |
Line 244... | Line 243... | ||
244 | 243 | ||
245 | ; local ip (Using RFC1122 strong end system model) |
244 | ; local ip (Using RFC1122 strong end system model) |
246 | mov eax, [edx + IPv4_header.DestinationAddress] |
245 | mov eax, [edx + IPv4_header.DestinationAddress] |
247 | cmp eax, [IP_LIST + edi] |
246 | cmp eax, [IPv4_address + edi] |
Line 248... | Line 247... | ||
248 | je .ip_ok |
247 | je .ip_ok |
249 | 248 | ||
250 | ; network layer broadcast |
249 | ; network layer broadcast |
Line 251... | Line 250... | ||
251 | cmp eax, [BROADCAST_LIST + edi] |
250 | cmp eax, [IPv4_broadcast + edi] |
252 | je .ip_ok |
251 | je .ip_ok |
253 | 252 | ||
Line 259... | Line 258... | ||
259 | and eax, 0x0fffffff |
258 | and eax, 0x0fffffff |
260 | cmp eax, 224 |
259 | cmp eax, 224 |
261 | je .ip_ok |
260 | je .ip_ok |
Line 262... | Line 261... | ||
262 | 261 | ||
263 | ; maybe we just dont have an IP yet and should accept everything on the IP level |
262 | ; maybe we just dont have an IP yet and should accept everything on the IP level |
264 | cmp [IP_LIST + edi], 0 |
263 | cmp [IPv4_address + edi], 0 |
Line 265... | Line 264... | ||
265 | je .ip_ok |
264 | je .ip_ok |
266 | 265 | ||
267 | ; or it's just not meant for us.. :( |
266 | ; or it's just not meant for us.. :( |
Line 417... | Line 416... | ||
417 | 416 | ||
418 | .is_first_fragment: |
417 | .is_first_fragment: |
419 | DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: First fragment packet received!\n" |
418 | DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_input: First fragment packet received!\n" |
420 | ; try to locate a free slot.. |
419 | ; try to locate a free slot.. |
421 | mov ecx, IPv4_MAX_FRAGMENTS |
420 | mov ecx, IPv4_MAX_FRAGMENTS |
422 | mov esi, IPv4_FRAGMENT_LIST |
421 | mov esi, IPv4_fragments |
423 | .find_free_slot: |
422 | .find_free_slot: |
424 | cmp word [esi + IPv4_FRAGMENT_slot.ttl], 0 |
423 | cmp word [esi + IPv4_FRAGMENT_slot.ttl], 0 |
425 | je .found_free_slot |
424 | je .found_free_slot |
426 | add esi, sizeof.IPv4_FRAGMENT_slot |
425 | add esi, sizeof.IPv4_FRAGMENT_slot |
Line 582... | Line 581... | ||
582 | ;;; TODO: the RFC says we should check protocol number too |
581 | ;;; TODO: the RFC says we should check protocol number too |
Line 583... | Line 582... | ||
583 | 582 | ||
584 | push eax ebx ecx edx |
583 | push eax ebx ecx edx |
585 | mov ax, [edx + IPv4_header.Identification] |
584 | mov ax, [edx + IPv4_header.Identification] |
586 | mov ecx, IPv4_MAX_FRAGMENTS |
585 | mov ecx, IPv4_MAX_FRAGMENTS |
587 | mov esi, IPv4_FRAGMENT_LIST |
586 | mov esi, IPv4_fragments |
588 | mov ebx, [edx + IPv4_header.SourceAddress] |
587 | mov ebx, [edx + IPv4_header.SourceAddress] |
589 | mov edx, [edx + IPv4_header.DestinationAddress] |
588 | mov edx, [edx + IPv4_header.DestinationAddress] |
590 | .find_slot: |
589 | .find_slot: |
591 | cmp [esi + IPv4_FRAGMENT_slot.id], ax |
590 | cmp [esi + IPv4_FRAGMENT_slot.id], ax |
Line 647... | Line 646... | ||
647 | push ax |
646 | push ax |
Line 648... | Line 647... | ||
648 | 647 | ||
Line 649... | Line 648... | ||
649 | inc [IPv4_packets_tx + edi] ; update stats |
648 | inc [IPv4_packets_tx + edi] ; update stats |
650 | 649 | ||
651 | mov ax, ETHER_PROTO_IPv4 |
650 | mov ax, ETHER_PROTO_IPv4 |
652 | mov ebx, [NET_DRV_LIST + edi] |
651 | mov ebx, [net_drv_list + edi] |
653 | mov ecx, [esp + 6 + 8 + 2] |
652 | mov ecx, [esp + 6 + 8 + 2] |
654 | add ecx, sizeof.IPv4_header |
653 | add ecx, sizeof.IPv4_header |
655 | mov edx, esp |
654 | mov edx, esp |
Line 704... | Line 703... | ||
704 | DEBUGF DEBUG_NETWORK_ERROR, "IPv4_output: Packet too large!\n" |
703 | DEBUGF DEBUG_NETWORK_ERROR, "IPv4_output: Packet too large!\n" |
705 | xor eax, eax |
704 | xor eax, eax |
706 | ret |
705 | ret |
Line 707... | Line 706... | ||
707 | 706 | ||
- | 707 | .loopback: |
|
- | 708 | inc [IPv4_packets_tx + edi] ; update stats |
|
708 | .loopback: |
709 | |
709 | mov dword [esp], eax ; set source IP to dest IP |
710 | mov dword [esp], eax ; set source IP to dest IP |
710 | mov ecx, [esp + 10] |
711 | mov ecx, [esp + 10] |
711 | add ecx, sizeof.IPv4_header |
712 | add ecx, sizeof.IPv4_header |
712 | mov edi, AF_INET4 |
713 | mov edi, AF_INET4 |
Line 744... | Line 745... | ||
744 | push ebx ; push the mac |
745 | push ebx ; push the mac |
745 | push ax |
746 | push ax |
Line 746... | Line 747... | ||
746 | 747 | ||
747 | inc [IPv4_packets_tx + 4*edi] |
748 | inc [IPv4_packets_tx + 4*edi] |
748 | mov ax, ETHER_PROTO_IPv4 |
749 | mov ax, ETHER_PROTO_IPv4 |
749 | mov ebx, [NET_DRV_LIST + 4*edi] |
750 | mov ebx, [net_drv_list + 4*edi] |
750 | mov ecx, [esp + 6 + 4] |
751 | mov ecx, [esp + 6 + 4] |
751 | add ecx, sizeof.IPv4_header |
752 | add ecx, sizeof.IPv4_header |
752 | mov edx, esp |
753 | mov edx, esp |
753 | call eth_output |
754 | call eth_output |
Line 943... | Line 944... | ||
943 | cmp eax, 0xffffffff |
944 | cmp eax, 0xffffffff |
944 | je .broadcast |
945 | je .broadcast |
Line 945... | Line 946... | ||
945 | 946 | ||
946 | xor edi, edi |
947 | xor edi, edi |
947 | .loop: |
948 | .loop: |
948 | mov ebx, [IP_LIST + edi] |
949 | mov ebx, [IPv4_address + edi] |
949 | and ebx, [SUBNET_LIST + edi] |
950 | and ebx, [IPv4_subnet + edi] |
950 | jz .next |
951 | jz .next |
951 | mov ecx, eax |
952 | mov ecx, eax |
952 | and ecx, [SUBNET_LIST + edi] |
953 | and ecx, [IPv4_subnet + edi] |
953 | cmp ebx, ecx |
954 | cmp ebx, ecx |
954 | je .got_it |
955 | je .got_it |
955 | .next: |
956 | .next: |
956 | add edi, 4 |
957 | add edi, 4 |
957 | cmp edi, 4*NET_DEVICES_MAX |
958 | cmp edi, 4*NET_DEVICES_MAX |
Line 958... | Line 959... | ||
958 | jb .loop |
959 | jb .loop |
959 | 960 | ||
960 | mov eax, [GATEWAY_LIST + 4] ; TODO: let user (or a user space daemon) configure default route |
961 | mov eax, [IPv4_gateway + 4] ; TODO: let user (or a user space daemon) configure default route |
961 | .broadcast: |
962 | .broadcast: |
962 | mov edi, 4 ; TODO: same as above |
963 | mov edi, 4 ; TODO: same as above |
963 | .got_it: |
964 | .got_it: |
964 | DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_route: %u\n", edi |
965 | DEBUGF DEBUG_NETWORK_VERBOSE, "IPv4_route: %u\n", edi |
965 | test edx, edx |
966 | test edx, edx |
966 | jnz @f |
967 | jnz @f |
Line 967... | Line 968... | ||
967 | mov edx, [IP_LIST + edi] |
968 | mov edx, [IPv4_address + edi] |
Line 968... | Line 969... | ||
968 | @@: |
969 | @@: |
969 | 970 | ||
970 | ret |
971 | ret |
971 | 972 | ||
972 | .got_device: |
973 | .got_device: |
Line 973... | Line 974... | ||
973 | ; Validate device ptr and convert to device number |
974 | ; Validate device ptr and convert to device number |
Line 974... | Line 975... | ||
974 | call net_ptr_to_num4 |
975 | call net_ptr_to_num4 |
975 | cmp edi, -1 |
976 | cmp edi, -1 |
976 | je .fail |
977 | je .fail |
Line 977... | Line 978... | ||
977 | 978 | ||
978 | mov edx, [IP_LIST + edi] ; Source IP |
979 | mov edx, [IPv4_address + edi] ; Source IP |
979 | 980 | ||
980 | ; Broadcast does not need gateway |
981 | ; Broadcast does not need gateway |
981 | cmp eax, 0xffffffff |
982 | cmp eax, 0xffffffff |
982 | je @f |
983 | je @f |
983 | 984 | ||
984 | ; Check if we should route to gateway or not |
985 | ; Check if we should route to gateway or not |
985 | mov ebx, [IP_LIST + edi] |
986 | mov ebx, [IPv4_address + edi] |
986 | and ebx, [SUBNET_LIST + edi] |
987 | and ebx, [IPv4_subnet + edi] |
987 | mov ecx, eax |
988 | mov ecx, eax |
Line 988... | Line 989... | ||
988 | and ecx, [SUBNET_LIST + edi] |
989 | and ecx, [IPv4_subnet + edi] |
Line 1036... | Line 1037... | ||
1036 | pop edx eax |
1037 | pop edx eax |
Line 1037... | Line 1038... | ||
1037 | 1038 | ||
1038 | ; Fill in local IP |
1039 | ; Fill in local IP |
1039 | cmp [eax + IP_SOCKET.LocalIP], 0 |
1040 | cmp [eax + IP_SOCKET.LocalIP], 0 |
1040 | jne @f |
1041 | jne @f |
1041 | push [IP_LIST + 4] ; FIXME: use correct local IP |
1042 | push [IPv4_address + 4] ; FIXME: use correct local IP |
Line 1042... | Line 1043... | ||
1042 | pop [eax + IP_SOCKET.LocalIP] |
1043 | pop [eax + IP_SOCKET.LocalIP] |
1043 | 1044 | ||
1044 | ; Fill in remote IP |
1045 | ; Fill in remote IP |
Line 1098... | Line 1099... | ||
1098 | .packets_rx: |
1099 | .packets_rx: |
1099 | mov eax, [IPv4_packets_rx + eax] |
1100 | mov eax, [IPv4_packets_rx + eax] |
1100 | ret |
1101 | ret |
Line 1101... | Line 1102... | ||
1101 | 1102 | ||
1102 | .read_ip: |
1103 | .read_ip: |
1103 | mov eax, [IP_LIST + eax] |
1104 | mov eax, [IPv4_address + eax] |
Line 1104... | Line 1105... | ||
1104 | ret |
1105 | ret |
1105 | 1106 | ||
1106 | .write_ip: |
1107 | .write_ip: |
Line 1107... | Line 1108... | ||
1107 | mov [IP_LIST + eax], ecx |
1108 | mov [IPv4_address + eax], ecx |
1108 | mov edi, eax ; device number, we'll need it for ARP |
1109 | mov edi, eax ; device number, we'll need it for ARP |
1109 | 1110 | ||
1110 | ; pre-calculate the local broadcast address |
1111 | ; pre-calculate the local broadcast address |
1111 | mov ebx, [SUBNET_LIST + eax] |
1112 | mov ebx, [IPv4_subnet + eax] |
Line 1112... | Line 1113... | ||
1112 | not ebx |
1113 | not ebx |
1113 | or ebx, ecx |
1114 | or ebx, ecx |
1114 | mov [BROADCAST_LIST + eax], ebx |
1115 | mov [IPv4_broadcast + eax], ebx |
Line 1115... | Line 1116... | ||
1115 | 1116 | ||
1116 | mov ebx, [NET_DRV_LIST + eax] |
1117 | mov ebx, [net_drv_list + eax] |
1117 | mov eax, [IP_LIST + eax] |
1118 | mov eax, [IPv4_address + eax] |
Line 1118... | Line 1119... | ||
1118 | call arp_output_request ; now send a gratuitous ARP |
1119 | call arp_output_request ; now send a gratuitous ARP |
1119 | 1120 | ||
1120 | call net_send_event |
1121 | call net_send_event |
Line 1121... | Line 1122... | ||
1121 | xor eax, eax |
1122 | xor eax, eax |
1122 | ret |
1123 | ret |
1123 | 1124 | ||
1124 | .read_dns: |
1125 | .read_dns: |
1125 | mov eax, [DNS_LIST + eax] |
1126 | mov eax, [IPv4_nameserver + eax] |
Line 1126... | Line 1127... | ||
1126 | ret |
1127 | ret |
1127 | 1128 | ||
1128 | .write_dns: |
1129 | .write_dns: |
Line 1129... | Line 1130... | ||
1129 | mov [DNS_LIST + eax], ecx |
1130 | mov [IPv4_nameserver + eax], ecx |
1130 | call net_send_event |
1131 | call net_send_event |
Line 1131... | Line 1132... | ||
1131 | xor eax, eax |
1132 | xor eax, eax |
1132 | ret |
1133 | ret |
1133 | 1134 | ||
1134 | .read_subnet: |
1135 | .read_subnet: |
1135 | mov eax, [SUBNET_LIST + eax] |
1136 | mov eax, [IPv4_subnet + eax] |
Line 1136... | Line 1137... | ||
1136 | ret |
1137 | ret |
1137 | 1138 | ||
1138 | .write_subnet: |
1139 | .write_subnet: |
Line 1139... | Line 1140... | ||
1139 | mov [SUBNET_LIST + eax], ecx |
1140 | mov [IPv4_subnet + eax], ecx |
1140 | 1141 | ||
1141 | ; pre-calculate the local broadcast address |
1142 | ; pre-calculate the local broadcast address |
Line 1142... | Line 1143... | ||
1142 | mov ebx, [IP_LIST + eax] |
1143 | mov ebx, [IPv4_address + eax] |
1143 | not ecx |
1144 | not ecx |
Line 1144... | Line 1145... | ||
1144 | or ecx, ebx |
1145 | or ecx, ebx |
1145 | mov [BROADCAST_LIST + eax], ecx |
1146 | mov [IPv4_broadcast + eax], ecx |
1146 | 1147 | ||
1147 | call net_send_event |
1148 | call net_send_event |