aboutsummaryrefslogtreecommitdiff
path: root/docs/j1demo/firmware/ip.fs
blob: 7c661371927b91cbb0217a7ec4ad025a5fa63de8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
( IP networking: headers and wrapup         JCB 13:21 08/24/10)
module[ ip"

: ip-datalength ( -- u ) \ length of current IP packet in words
    ETH.IP.LENGTH packet@
    d# 20 - 2/
;

: ip-isproto ( u -- f ) \ true if packet PROTO is u
    ETH.IP.TTLPROTO packet@ h# ff and =
;

: ip-identification
    ip-id-counter d# 1 over +! @
;

: @ethaddr ( eth-addr -- mac01 mac23 mac45 )
    ?dup
    if
        dup @ swap 2+ 2@
    else
        ethaddr-broadcast
    then
;

: ip-header ( dst-ip src-ip eth-addr protocol -- )
    >r
    mac-pkt-begin

    @ethaddr mac-pkt-3,
    net-my-mac mac-pkt-3,
    h# 800 mac-pkt-,

    h# 4500
    h# 0000                  \  length
    ip-identification
    mac-pkt-3,
    h# 4000                  \  do not fragment
    h# 4000 r> or            \  TTL, protocol
    d# 0                        \  checksum
    mac-pkt-3,
    mac-pkt-2,              \  src ip
    mac-pkt-2,              \  dst ip
;

: ip-wrapup ( bytelen -- )
    \  write IP length
    ETH.IP -
    ETH.IP.LENGTH packetout-off mac!

    \  write IP checksum
    ETH.IP packetout-off d# 10 mac-checksum
    ETH.IP.CHKSUM packetout-off mac!
;

: ip-packet-srcip
    d# 2 ETH.IP.SRCIP mac-inoffset mac@n
;

( ICMP return and originate                  JCB 13:22 08/24/10)

\  Someone pings us, generate a return packet

: icmp-handler
    IP_PROTO_ICMP ip-isproto
    ETH.IP.ICMP.TYPECODE packet@ h# 800 =
    and if
        ip-packet-srcip
        2dup arp-lookup
        ?dup if
            \  transmit ICMP reply
                                    \  dstip *ethaddr
            net-my-ip rot           \  dstip srcip *ethaddr
            d# 1 ip-header

            \  Now the ICMP header
            d# 0 mac-pkt-,

            s" =====> ICMP seq " type
            ETH.IP.ICMP.SEQUENCE mac-inoffset mac@ u. cr

            ETH.IP.ICMP.IDENTIFIER mac-inoffset
            ip-datalength 2-        ( offset n )
            tuck
            mac-checksum mac-pkt-,
            ETH.IP.ICMP.IDENTIFIER mac-pkt-src

            mac-pkt-complete
            ip-wrapup
            mac-send
        else
            2drop
        then
    then
;
    
: ping ( ip. -- ) \ originate
    2dup arp-lookup
    ?dup if
        \  transmit ICMP request
                                \  dstip *ethaddr
        net-my-ip rot           \  dstip srcip *ethaddr
        d# 1 ip-header

        \  Now the ICMP header
        h# 800 mac-pkt-,

        \  id is h# 550b, seq is lo word of time
        h# 550b time@ drop
        2dup +1c h# 800 +1c
        d# 28 begin swap d# 0 +1c swap 1- dup 0= until drop
        invert mac-pkt-,     \  checksum
        mac-pkt-2,
        d# 28 mac-pkt-,0

        mac-pkt-complete
        ip-wrapup
        mac-send
    else
        2drop
    then
;

]module