udhcpc6: new applet. Not yet functional.
[oweals/busybox.git] / networking / udhcp / d6_packet.c
1 /* 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"
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
15 void FAST_FUNC d6_dump_packet(struct d6_packet *packet)
16 {
17         if (dhcp_verbose < 2)
18                 return;
19
20         bb_info_msg(
21                 " xid %x"
22                 , packet->d6_xid32
23         );
24         //*bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
25         //bb_info_msg(" chaddr %s", buf);
26 }
27 #endif
28
29 int 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; /* returns -1 */
40         }
41
42         if (bytes < offsetof(struct d6_packet, d6_options)) {
43                 bb_info_msg("Packet with bad magic, ignoring");
44                 return -2;
45         }
46         log1("Received a packet");
47         d6_dump_packet(packet);
48
49         return bytes;
50 }
51
52 /* Construct a ipv6+udp header for a packet, send packet */
53 int 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; /* struct copy */
74
75         dest_sll.sll_family = AF_PACKET;
76         dest_sll.sll_protocol = htons(ETH_P_IPV6);
77         dest_sll.sll_ifindex = ifindex;
78         dest_sll.sll_halen = 6;
79         memcpy(dest_sll.sll_addr, dest_arp, 6);
80
81         if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
82                 msg = "bind(%s)";
83                 goto ret_close;
84         }
85
86         packet.ip6.ip6_vfc = (6 << 4); /* 4 bits version, top 4 bits of tclass */
87         if (src_ipv6)
88                 packet.ip6.ip6_src = *src_ipv6; /* struct copy */
89         packet.ip6.ip6_dst = *dst_ipv6; /* struct copy */
90         packet.udp.source = htons(source_port);
91         packet.udp.dest = htons(dest_port);
92         /* size, excluding IP header: */
93         packet.udp.len = htons(sizeof(struct udphdr) + d6_pkt_size);
94         packet.ip6.ip6_plen = packet.udp.len;
95         /* UDP checksum skips first four bytes of IP header.
96          * IPv6 'hop limit' field should be 0.
97          * 'next header' field should be summed as if it is in a different
98          * position, therefore we write its value into ip6_hlim:
99          */
100         packet.ip6.ip6_hlim = IPPROTO_UDP;
101         packet.udp.check = inet_cksum((uint16_t *)&packet + 2,
102                                 offsetof(struct ip6_udp_d6_packet, data) - 4 + d6_pkt_size);
103         /* fix 'hop limit' and 'next header' after UDP checksumming */
104         packet.ip6.ip6_hlim = 8;
105         packet.ip6.ip6_nxt = IPPROTO_UDP;
106
107         d6_dump_packet(d6_pkt);
108         result = sendto(fd, &packet, offsetof(struct ip6_udp_d6_packet, data) + d6_pkt_size,
109                         /*flags:*/ 0,
110                         (struct sockaddr *) &dest_sll, sizeof(dest_sll)
111         );
112         msg = "sendto";
113  ret_close:
114         close(fd);
115         if (result < 0) {
116  ret_msg:
117                 bb_perror_msg(msg, "PACKET");
118         }
119         return result;
120 }
121
122 /* Let the kernel do all the work for packet generation */
123 int FAST_FUNC d6_send_kernel_packet(
124                 struct d6_packet *d6_pkt, unsigned d6_pkt_size,
125                 struct in6_addr *src_ipv6, int source_port,
126                 struct in6_addr *dst_ipv6, int dest_port)
127 {
128         struct sockaddr_in6 sa;
129         int fd;
130         int result = -1;
131         const char *msg;
132
133         fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
134         if (fd < 0) {
135                 msg = "socket(%s)";
136                 goto ret_msg;
137         }
138         setsockopt_reuseaddr(fd);
139
140         memset(&sa, 0, sizeof(sa));
141         sa.sin6_family = AF_INET6;
142         sa.sin6_port = htons(source_port);
143         sa.sin6_addr = *src_ipv6; /* struct copy */
144         if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
145                 msg = "bind(%s)";
146                 goto ret_close;
147         }
148
149         memset(&sa, 0, sizeof(sa));
150         sa.sin6_family = AF_INET6;
151         sa.sin6_port = htons(dest_port);
152         sa.sin6_addr = *dst_ipv6; /* struct copy */
153         if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == -1) {
154                 msg = "connect";
155                 goto ret_close;
156         }
157
158         d6_dump_packet(d6_pkt);
159         result = safe_write(fd, d6_pkt, d6_pkt_size);
160         msg = "write";
161  ret_close:
162         close(fd);
163         if (result < 0) {
164  ret_msg:
165                 bb_perror_msg(msg, "UDP");
166         }
167         return result;
168 }