1
2
3
4
5
6
7#include "common.h"
8#include "d6_common.h"
9#include "dhcpd.h"
10#include <netinet/in.h>
11#include <netinet/if_ether.h>
12#include <netpacket/packet.h>
13
14#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
15void FAST_FUNC d6_dump_packet(struct d6_packet *packet)
16{
17 if (dhcp_verbose < 2)
18 return;
19
20 bb_error_msg(
21 " xid %x"
22 , packet->d6_xid32
23 );
24
25
26}
27#endif
28
29int FAST_FUNC d6_recv_kernel_packet(struct in6_addr *peer_ipv6
30 UNUSED_PARAM
31 , struct d6_packet *packet, int fd)
32{
33 int bytes;
34
35 memset(packet, 0, sizeof(*packet));
36 bytes = safe_read(fd, packet, sizeof(*packet));
37 if (bytes < 0) {
38 log1("packet read error, ignoring");
39 return bytes;
40 }
41
42 if (bytes < offsetof(struct d6_packet, d6_options)) {
43 bb_error_msg("packet with bad magic, ignoring");
44 return -2;
45 }
46 log1("received %s", "a packet");
47 d6_dump_packet(packet);
48
49 return bytes;
50}
51
52
53int FAST_FUNC d6_send_raw_packet(
54 struct d6_packet *d6_pkt, unsigned d6_pkt_size,
55 struct in6_addr *src_ipv6, int source_port,
56 struct in6_addr *dst_ipv6, int dest_port, const uint8_t *dest_arp,
57 int ifindex)
58{
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;
74
75 dest_sll.sll_family = AF_PACKET;
76 dest_sll.sll_protocol = htons(ETH_P_IPV6);
77 dest_sll.sll_ifindex = ifindex;
78
79
80 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);
89 if (src_ipv6)
90 packet.ip6.ip6_src = *src_ipv6;
91 packet.ip6.ip6_dst = *dst_ipv6;
92 packet.udp.source = htons(source_port);
93 packet.udp.dest = htons(dest_port);
94
95 packet.udp.len = htons(sizeof(struct udphdr) + d6_pkt_size);
96 packet.ip6.ip6_plen = packet.udp.len;
97
98
99
100
101
102
103
104 packet.ip6.ip6_hlim = IPPROTO_UDP;
105 packet.udp.check = inet_cksum(
106 (uint16_t *)&packet + 2,
107 offsetof(struct ip6_udp_d6_packet, data) - 4 + d6_pkt_size
108 );
109
110 packet.ip6.ip6_hlim = 1;
111 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 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
129int FAST_FUNC d6_send_kernel_packet(
130 struct d6_packet *d6_pkt, unsigned d6_pkt_size,
131 struct in6_addr *src_ipv6, int source_port,
132 struct in6_addr *dst_ipv6, int dest_port,
133 int ifindex)
134{
135 struct sockaddr_in6 sa;
136 int fd;
137 int result = -1;
138 const char *msg;
139
140 fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
141 if (fd < 0) {
142 msg = "socket(%s)";
143 goto ret_msg;
144 }
145 setsockopt_reuseaddr(fd);
146
147 memset(&sa, 0, sizeof(sa));
148 sa.sin6_family = AF_INET6;
149 sa.sin6_port = htons(source_port);
150 sa.sin6_addr = *src_ipv6;
151 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
152 msg = "bind(%s)";
153 goto ret_close;
154 }
155
156 memset(&sa, 0, sizeof(sa));
157 sa.sin6_family = AF_INET6;
158 sa.sin6_port = htons(dest_port);
159 sa.sin6_addr = *dst_ipv6;
160 sa.sin6_scope_id = ifindex;
161 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
162 msg = "connect";
163 goto ret_close;
164 }
165
166 d6_dump_packet(d6_pkt);
167 result = safe_write(fd, d6_pkt, d6_pkt_size);
168 msg = "write";
169 ret_close:
170 close(fd);
171 if (result < 0) {
172 ret_msg:
173 bb_perror_msg(msg, "UDP");
174 }
175 return result;
176}
177