These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / SLOF / clients / net-snk / app / netlib / udp.c
1 /******************************************************************************
2  * Copyright (c) 2004, 2008 IBM Corporation
3  * All rights reserved.
4  * This program and the accompanying materials
5  * are made available under the terms of the BSD License
6  * which accompanies this distribution, and is available at
7  * http://www.opensource.org/licenses/bsd-license.php
8  *
9  * Contributors:
10  *     IBM Corporation - initial implementation
11  *****************************************************************************/
12
13 /************************ DEFINITIONS & DECLARATIONS *********************/
14
15 #include <udp.h>
16 #include <sys/socket.h>
17 #include <dhcp.h>
18 #include <dhcpv6.h>
19 #include <dns.h>
20 #ifdef USE_MTFTP
21 #include <mtftp.h>
22 #else
23 #include <tftp.h>
24 #endif
25
26
27
28 /****************************** LOCAL VARIABLES **************************/
29
30
31 #ifdef USE_MTFTP
32
33 uint16_t net_tftp_uport;
34 uint16_t net_mtftp_uport;
35
36 void net_set_tftp_port(uint16_t tftp_port)
37 {
38         net_tftp_uport = tftp_port;
39 }
40
41 void net_set_mtftp_port(uint16_t tftp_port)
42 {
43         net_mtftp_uport = tftp_port;
44 }
45
46 #endif
47
48 /****************************** IMPLEMENTATION ***************************/
49
50
51 /**
52  * NET: Handles UDP-packets according to Receive-handle diagram.
53  *
54  * @param  udp_packet UDP-packet to be handled
55  * @param  packetsize Length of the packet
56  * @return            ZERO - packet handled successfully;
57  *                    NON ZERO - packet was not handled (e.g. bad format)
58  * @see               receive_ether
59  * @see               udphdr
60  */
61 int8_t handle_udp(int fd, uint8_t * udp_packet, uint32_t packetsize)
62 {
63         struct udphdr * udph = (struct udphdr *) udp_packet;
64
65         if (packetsize < sizeof(struct udphdr))
66                 return -1; // packet is too small
67
68         switch (htons(udph -> uh_dport)) {
69         case UDPPORT_BOOTPC:
70                 if (udph -> uh_sport == htons(UDPPORT_BOOTPS))
71                         return handle_dhcp(fd, udp_packet + sizeof(struct udphdr),
72                                             packetsize - sizeof(struct udphdr));
73                 else
74                         return -1;
75         case UDPPORT_DNSC:
76                 if (udph -> uh_sport == htons(UDPPORT_DNSS))
77                         return handle_dns(udp_packet + sizeof(struct udphdr),
78                                           packetsize - sizeof(struct udphdr));
79                 else
80                         return -1;
81         case UDPPORT_DHCPV6C:
82                 return handle_dhcpv6(udp_packet+sizeof(struct udphdr),
83                                      packetsize - sizeof(struct udphdr));
84         case UDPPORT_TFTPC:
85 #ifdef USE_MTFTP
86                 return handle_tftp(fd, udp_packet + sizeof(struct udphdr),
87                                        packetsize - sizeof(struct udphdr));
88 #else
89                 return handle_tftp(fd, udp_packet, packetsize);
90 #endif
91         default:
92 #ifdef USE_MTFTP
93                 if (htons(udph -> uh_dport) == net_tftp_uport)
94                         return handle_tftp(fd, udp_packet + sizeof(struct udphdr),
95                        packetsize - sizeof(struct udphdr));
96                 else if (htons(udph -> uh_dport) == net_mtftp_uport)
97                         return handle_tftp(fd, udp_packet + sizeof(struct udphdr),
98                        packetsize - sizeof(struct udphdr));
99 #endif
100                 return -1;
101         }
102 }
103
104 /**
105  * NET: This function handles situation when "Destination unreachable"
106  *      ICMP-error occurs during sending UDP-packet.
107  *
108  * @param  err_code   Error Code (e.g. "Host unreachable")
109  * @param  packet     original UDP-packet
110  * @param  packetsize length of the packet
111  * @see               handle_icmp
112  */
113 void handle_udp_dun(uint8_t * udp_packet, uint32_t packetsize, uint8_t err_code)
114 {
115         struct udphdr * udph = (struct udphdr *) udp_packet;
116
117         if (packetsize < sizeof(struct udphdr))
118                 return; // packet is too small
119
120         switch (htons(udph -> uh_sport)) {
121         case UDPPORT_TFTPC:
122                 handle_tftp_dun(err_code);
123                 break;
124         }
125 }
126
127 /**
128  * NET: Creates UDP-packet. Places UDP-header in a packet and fills it
129  *      with corresponding information.
130  *      <p>
131  *      Use this function with similar functions for other network layers
132  *      (fill_ethhdr, fill_iphdr, fill_dnshdr, fill_btphdr).
133  *
134  * @param  packet      Points to the place where UDP-header must be placed.
135  * @param  packetsize  Size of the packet in bytes incl. this hdr and data.
136  * @param  src_port    UDP source port
137  * @param  dest_port   UDP destination port
138  * @see                udphdr
139  * @see                fill_ethhdr
140  * @see                fill_iphdr
141  * @see                fill_dnshdr
142  * @see                fill_btphdr
143  */
144 void fill_udphdr(uint8_t * packet, uint16_t packetsize,
145                  uint16_t src_port, uint16_t dest_port)
146 {
147         struct udphdr * udph = (struct udphdr *) packet;
148
149         udph -> uh_sport = htons(src_port);
150         udph -> uh_dport = htons(dest_port);
151         udph -> uh_ulen = htons(packetsize);
152         udph -> uh_sum = htons(0);
153 }