| 1 | \ ========== Copyright Header Begin ========================================== |
| 2 | \ |
| 3 | \ Hypervisor Software File: udp.fth |
| 4 | \ |
| 5 | \ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. |
| 6 | \ |
| 7 | \ - Do no alter or remove copyright notices |
| 8 | \ |
| 9 | \ - Redistribution and use of this software in source and binary forms, with |
| 10 | \ or without modification, are permitted provided that the following |
| 11 | \ conditions are met: |
| 12 | \ |
| 13 | \ - Redistribution of source code must retain the above copyright notice, |
| 14 | \ this list of conditions and the following disclaimer. |
| 15 | \ |
| 16 | \ - Redistribution in binary form must reproduce the above copyright notice, |
| 17 | \ this list of conditions and the following disclaimer in the |
| 18 | \ documentation and/or other materials provided with the distribution. |
| 19 | \ |
| 20 | \ Neither the name of Sun Microsystems, Inc. or the names of contributors |
| 21 | \ may be used to endorse or promote products derived from this software |
| 22 | \ without specific prior written permission. |
| 23 | \ |
| 24 | \ This software is provided "AS IS," without a warranty of any kind. |
| 25 | \ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, |
| 26 | \ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A |
| 27 | \ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN |
| 28 | \ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR |
| 29 | \ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR |
| 30 | \ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN |
| 31 | \ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR |
| 32 | \ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE |
| 33 | \ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, |
| 34 | \ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF |
| 35 | \ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
| 36 | \ |
| 37 | \ You acknowledge that this software is not designed, licensed or |
| 38 | \ intended for use in the design, construction, operation or maintenance of |
| 39 | \ any nuclear facility. |
| 40 | \ |
| 41 | \ ========== Copyright Header End ============================================ |
| 42 | id: @(#)udp.fth 1.1 04/09/07 |
| 43 | purpose: UDP support |
| 44 | copyright: Copyright 2004 Sun Microsystems, Inc. All Rights Reserved |
| 45 | copyright: Use is subject to license terms. |
| 46 | |
| 47 | \ RFC 768: User Datagram Protocol |
| 48 | |
| 49 | fload ${BP}/pkg/netinet/udp-h.fth |
| 50 | |
| 51 | headerless |
| 52 | |
| 53 | /queue-head instance buffer: udp-inpcb-list \ Head of UDP's INPCB list |
| 54 | |
| 55 | : udp-init ( -- ) udp-inpcb-list queue-init ; |
| 56 | : udp-close ( -- ) ; |
| 57 | |
| 58 | \ Allocate UDP control block. |
| 59 | : ucb-alloc ( -- ucb ) |
| 60 | /udp-control-block dup alloc-mem tuck swap erase ( ucb ) |
| 61 | dup >ucb-dgramq queue-init ( ucb ) |
| 62 | ; |
| 63 | |
| 64 | \ Free resources held by UDP control block. |
| 65 | : ucb-free ( ucb -- ) |
| 66 | dup >ucb-dgramq ( ucb qhead ) |
| 67 | begin dup pkt-dequeue ?dup while ( ucb qhead pkt ) |
| 68 | pkt-free ( ucb qhead ) |
| 69 | repeat drop ( ucb ) |
| 70 | /udp-control-block free-mem ( ) |
| 71 | ; |
| 72 | |
| 73 | \ Compute UDP packet checksum. |
| 74 | : udp-checksum ( ip-pkt -- checksum ) |
| 75 | IPPROTO_UDP over >ip-src /ip-addr (in-cksum) |
| 76 | over >ip-dest /ip-addr (in-cksum) |
| 77 | swap ippkt>payload rot over + -rot in-cksum |
| 78 | ; |
| 79 | |
| 80 | \ Verify UDP packet checksum. The checksum field will be zero if the |
| 81 | \ sender did not compute a checksum. |
| 82 | : udp-checksum-ok? ( udpip-pkt -- flag ) |
| 83 | dup >udp-cksum ntohw@ 0= swap udp-checksum 0= or |
| 84 | ; |
| 85 | |
| 86 | \ Check if datagram is for this INPCB |
| 87 | : ucb-match? ( pkt inpcb -- pkt match? ) |
| 88 | over >udp-dport ntohw@ over in-lport@ = if ( pkt inpcb ) |
| 89 | over >ip-dest over >in-laddr ip= if ( pkt inpcb ) |
| 90 | drop true ( pkt true ) |
| 91 | else ( pkt inpcb ) |
| 92 | >in-laddr inaddr-any? ( pkt match? ) |
| 93 | then ( pkt match? ) |
| 94 | else ( pkt inpcb ) |
| 95 | drop false ( pkt false ) |
| 96 | then ( pkt match? ) |
| 97 | ; |
| 98 | |
| 99 | \ UDP demultiplexing. |
| 100 | : ucb-locate ( pkt -- ucb | 0 ) |
| 101 | udp-inpcb-list ['] ucb-match? find-queue-entry nip dup if |
| 102 | inpcb>ucb |
| 103 | then |
| 104 | ; |
| 105 | |
| 106 | \ Handle incoming UDP datagram. |
| 107 | : udp-input ( pkt -- ) |
| 108 | dup udp-checksum-ok? if ( pkt ) |
| 109 | dup ucb-locate ?dup if ( pkt ucb ) |
| 110 | >ucb-dgramq swap pkt-enqueue ( ) |
| 111 | else ( pkt ) |
| 112 | pkt-free ( ) |
| 113 | then ( ) |
| 114 | else ( pkt ) |
| 115 | pkt-free ( ) |
| 116 | then ( ) |
| 117 | ; |
| 118 | ['] udp-input to (udp-input) |
| 119 | |
| 120 | \ Format and send the UDP datagram. |
| 121 | : udp-output ( inpcb data len -- #sent | error# ) |
| 122 | pkt-alloc ?dup 0= if 3drop 0 exit then ( inpcb data len pkt ) |
| 123 | over >r ( inpcb data len pkt ) |
| 124 | dup /udpip-header + 2swap rot swap move ( inpcb pkt ) ( r: len ) |
| 125 | |
| 126 | IPPROTO_UDP over >ip-protocol c! |
| 127 | my-ip-addr over >ip-src copy-ip-addr |
| 128 | over >in-faddr over >ip-dest copy-ip-addr |
| 129 | IP_DEFAULT_TTL over >ip-ttl c! |
| 130 | 0 over >ip-service c! |
| 131 | r@ /udpip-header + over >ip-len htonw! ( inpcb pkt ) |
| 132 | |
| 133 | over in-lport@ over >udp-sport htonw! ( inpcb pkt ) |
| 134 | swap in-fport@ over >udp-dport htonw! ( pkt ) |
| 135 | r@ /udp-header + over >udp-len htonw! ( pkt ) |
| 136 | 0 over >udp-cksum htonw! ( pkt ) |
| 137 | dup udp-checksum over >udp-cksum htonw! ( pkt ) |
| 138 | |
| 139 | ip-output ( #sent | error# ) |
| 140 | r> drop ( #sent | error# ) |
| 141 | ; |
| 142 | |
| 143 | : udp-poll ( -- ) ip-poll ; |
| 144 | |
| 145 | headers |