blob: 172f8e1ab7161a359dddbd603a1e6c197fd9666c [file] [log] [blame]
Denys Vlasenko9ba75042011-11-07 15:55:39 +01001/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2011 Denys Vlasenko.
4 *
5 * Licensed under GPLv2, see file LICENSE in this source tree.
6 */
7#include "common.h"
8#include "d6_common.h"
Denys Vlasenkof3d67112020-12-15 21:55:15 +01009#include "dhcpc.h"
Denys Vlasenko9ba75042011-11-07 15:55:39 +010010#include "dhcpd.h"
11#include <netinet/in.h>
12#include <netinet/if_ether.h>
13#include <netpacket/packet.h>
14
15#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
16void FAST_FUNC d6_dump_packet(struct d6_packet *packet)
17{
18 if (dhcp_verbose < 2)
19 return;
20
James Byrne253c4e72019-04-12 17:01:51 +000021 bb_info_msg(
Denys Vlasenkof6258362017-09-29 18:02:01 +020022 " xid %x"
Denys Vlasenko9ba75042011-11-07 15:55:39 +010023 , packet->d6_xid32
24 );
25 //*bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
Denys Vlasenko76b680c2016-03-30 16:04:37 +020026 //bb_error_msg(" chaddr %s", buf);
Denys Vlasenko9ba75042011-11-07 15:55:39 +010027}
28#endif
29
30int FAST_FUNC d6_recv_kernel_packet(struct in6_addr *peer_ipv6
31 UNUSED_PARAM
32 , struct d6_packet *packet, int fd)
33{
34 int bytes;
35
36 memset(packet, 0, sizeof(*packet));
37 bytes = safe_read(fd, packet, sizeof(*packet));
38 if (bytes < 0) {
James Byrne69374872019-07-02 11:35:03 +020039 log1s("packet read error, ignoring");
Denys Vlasenko9ba75042011-11-07 15:55:39 +010040 return bytes; /* returns -1 */
41 }
42
43 if (bytes < offsetof(struct d6_packet, d6_options)) {
James Byrne69374872019-07-02 11:35:03 +020044 bb_simple_info_msg("packet with bad magic, ignoring");
Denys Vlasenko9ba75042011-11-07 15:55:39 +010045 return -2;
46 }
Denys Vlasenko8f2e99c2016-03-30 18:41:23 +020047 log1("received %s", "a packet");
Denys Vlasenko9ba75042011-11-07 15:55:39 +010048 d6_dump_packet(packet);
49
50 return bytes;
51}
52
53/* Construct a ipv6+udp header for a packet, send packet */
Denys Vlasenkof3d67112020-12-15 21:55:15 +010054int FAST_FUNC d6_send_raw_packet_from_client_data_ifindex(
Denys Vlasenko9ba75042011-11-07 15:55:39 +010055 struct d6_packet *d6_pkt, unsigned d6_pkt_size,
56 struct in6_addr *src_ipv6, int source_port,
Denys Vlasenkof3d67112020-12-15 21:55:15 +010057 struct in6_addr *dst_ipv6, int dest_port, const uint8_t *dest_arp)
Denys Vlasenko9ba75042011-11-07 15:55:39 +010058{
59 struct sockaddr_ll dest_sll;
60 struct ip6_udp_d6_packet packet;
61 int fd;
62 int result = -1;
63 const char *msg;
64
65 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
66 if (fd < 0) {
67 msg = "socket(%s)";
68 goto ret_msg;
69 }
70
71 memset(&dest_sll, 0, sizeof(dest_sll));
72 memset(&packet, 0, offsetof(struct ip6_udp_d6_packet, data));
73 packet.data = *d6_pkt; /* struct copy */
74
75 dest_sll.sll_family = AF_PACKET;
76 dest_sll.sll_protocol = htons(ETH_P_IPV6);
Denys Vlasenkof3d67112020-12-15 21:55:15 +010077 dest_sll.sll_ifindex = client_data.ifindex;
Denys Vlasenko2b9acc62017-09-29 14:09:02 +020078 /*dest_sll.sll_hatype = ARPHRD_???;*/
79 /*dest_sll.sll_pkttype = PACKET_???;*/
Denys Vlasenko9ba75042011-11-07 15:55:39 +010080 dest_sll.sll_halen = 6;
81 memcpy(dest_sll.sll_addr, dest_arp, 6);
82
83 if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
84 msg = "bind(%s)";
85 goto ret_close;
86 }
87
88 packet.ip6.ip6_vfc = (6 << 4); /* 4 bits version, top 4 bits of tclass */
89 if (src_ipv6)
90 packet.ip6.ip6_src = *src_ipv6; /* struct copy */
91 packet.ip6.ip6_dst = *dst_ipv6; /* struct copy */
92 packet.udp.source = htons(source_port);
93 packet.udp.dest = htons(dest_port);
94 /* size, excluding IP header: */
95 packet.udp.len = htons(sizeof(struct udphdr) + d6_pkt_size);
96 packet.ip6.ip6_plen = packet.udp.len;
Denys Vlasenko2b6a6b92011-11-07 18:22:06 +010097 /*
98 * Someone was smoking weed (at least) while inventing UDP checksumming:
99 * UDP checksum skips first four bytes of IPv6 header.
100 * 'next header' field should be summed as if it is one more byte
101 * to the right, therefore we write its value (IPPROTO_UDP)
102 * into ip6_hlim, and its 'real' location remains zero-filled for now.
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100103 */
104 packet.ip6.ip6_hlim = IPPROTO_UDP;
Denys Vlasenko2b6a6b92011-11-07 18:22:06 +0100105 packet.udp.check = inet_cksum(
Denys Vlasenkof3d67112020-12-15 21:55:15 +0100106 (uint8_t *)&packet + 4,
107 offsetof(struct ip6_udp_d6_packet, data) - 4 + d6_pkt_size
Denys Vlasenko2b6a6b92011-11-07 18:22:06 +0100108 );
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100109 /* fix 'hop limit' and 'next header' after UDP checksumming */
Denys Vlasenko2b6a6b92011-11-07 18:22:06 +0100110 packet.ip6.ip6_hlim = 1; /* observed Windows machines to use hlim=1 */
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100111 packet.ip6.ip6_nxt = IPPROTO_UDP;
112
113 d6_dump_packet(d6_pkt);
114 result = sendto(fd, &packet, offsetof(struct ip6_udp_d6_packet, data) + d6_pkt_size,
115 /*flags:*/ 0,
116 (struct sockaddr *) &dest_sll, sizeof(dest_sll)
117 );
118 msg = "sendto";
119 ret_close:
120 close(fd);
121 if (result < 0) {
122 ret_msg:
123 bb_perror_msg(msg, "PACKET");
124 }
125 return result;
126}
127
128/* Let the kernel do all the work for packet generation */
Denys Vlasenkof3d67112020-12-15 21:55:15 +0100129int FAST_FUNC d6_send_kernel_packet_from_client_data_ifindex(
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100130 struct d6_packet *d6_pkt, unsigned d6_pkt_size,
131 struct in6_addr *src_ipv6, int source_port,
Denys Vlasenkof3d67112020-12-15 21:55:15 +0100132 struct in6_addr *dst_ipv6, int dest_port)
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100133{
134 struct sockaddr_in6 sa;
135 int fd;
136 int result = -1;
137 const char *msg;
138
139 fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
140 if (fd < 0) {
141 msg = "socket(%s)";
142 goto ret_msg;
143 }
144 setsockopt_reuseaddr(fd);
145
146 memset(&sa, 0, sizeof(sa));
147 sa.sin6_family = AF_INET6;
148 sa.sin6_port = htons(source_port);
149 sa.sin6_addr = *src_ipv6; /* struct copy */
150 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
151 msg = "bind(%s)";
152 goto ret_close;
153 }
154
155 memset(&sa, 0, sizeof(sa));
156 sa.sin6_family = AF_INET6;
157 sa.sin6_port = htons(dest_port);
158 sa.sin6_addr = *dst_ipv6; /* struct copy */
Denys Vlasenkof3d67112020-12-15 21:55:15 +0100159 sa.sin6_scope_id = client_data.ifindex;
Denys Vlasenko9ba75042011-11-07 15:55:39 +0100160 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
161 msg = "connect";
162 goto ret_close;
163 }
164
165 d6_dump_packet(d6_pkt);
166 result = safe_write(fd, d6_pkt, d6_pkt_size);
167 msg = "write";
168 ret_close:
169 close(fd);
170 if (result < 0) {
171 ret_msg:
172 bb_perror_msg(msg, "UDP");
173 }
174 return result;
175}