1 /******************************************************************************
2 * Copyright (c) 2004, 2008 IBM Corporation
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
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
13 /************************ DEFINITIONS & DECLARATIONS *********************/
16 #include <sys/socket.h>
28 /****************************** LOCAL VARIABLES **************************/
33 uint16_t net_tftp_uport;
34 uint16_t net_mtftp_uport;
36 void net_set_tftp_port(uint16_t tftp_port)
38 net_tftp_uport = tftp_port;
41 void net_set_mtftp_port(uint16_t tftp_port)
43 net_mtftp_uport = tftp_port;
48 /****************************** IMPLEMENTATION ***************************/
52 * NET: Handles UDP-packets according to Receive-handle diagram.
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)
61 int8_t handle_udp(int fd, uint8_t * udp_packet, uint32_t packetsize)
63 struct udphdr * udph = (struct udphdr *) udp_packet;
65 if (packetsize < sizeof(struct udphdr))
66 return -1; // packet is too small
68 switch (htons(udph -> uh_dport)) {
70 if (udph -> uh_sport == htons(UDPPORT_BOOTPS))
71 return handle_dhcp(fd, udp_packet + sizeof(struct udphdr),
72 packetsize - sizeof(struct udphdr));
76 if (udph -> uh_sport == htons(UDPPORT_DNSS))
77 return handle_dns(udp_packet + sizeof(struct udphdr),
78 packetsize - sizeof(struct udphdr));
82 return handle_dhcpv6(udp_packet+sizeof(struct udphdr),
83 packetsize - sizeof(struct udphdr));
86 return handle_tftp(fd, udp_packet + sizeof(struct udphdr),
87 packetsize - sizeof(struct udphdr));
89 return handle_tftp(fd, udp_packet, packetsize);
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));
105 * NET: This function handles situation when "Destination unreachable"
106 * ICMP-error occurs during sending UDP-packet.
108 * @param err_code Error Code (e.g. "Host unreachable")
109 * @param packet original UDP-packet
110 * @param packetsize length of the packet
113 void handle_udp_dun(uint8_t * udp_packet, uint32_t packetsize, uint8_t err_code)
115 struct udphdr * udph = (struct udphdr *) udp_packet;
117 if (packetsize < sizeof(struct udphdr))
118 return; // packet is too small
120 switch (htons(udph -> uh_sport)) {
122 handle_tftp_dun(err_code);
128 * NET: Creates UDP-packet. Places UDP-header in a packet and fills it
129 * with corresponding information.
131 * Use this function with similar functions for other network layers
132 * (fill_ethhdr, fill_iphdr, fill_dnshdr, fill_btphdr).
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
144 void fill_udphdr(uint8_t * packet, uint16_t packetsize,
145 uint16_t src_port, uint16_t dest_port)
147 struct udphdr * udph = (struct udphdr *) packet;
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);