busybox/networking/udhcp/d6_packet.c
<<
>>
Prefs
   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 "dhcpc.h"
  10#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
  21        bb_info_msg(
  22                " xid %x"
  23                , packet->d6_xid32
  24        );
  25        //*bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0';
  26        //bb_error_msg(" chaddr %s", buf);
  27}
  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) {
  39                log1s("packet read error, ignoring");
  40                return bytes; /* returns -1 */
  41        }
  42
  43        if (bytes < offsetof(struct d6_packet, d6_options)) {
  44                bb_simple_info_msg("packet with bad magic, ignoring");
  45                return -2;
  46        }
  47        log2("received %s", "a packet");
  48        /* log2 because more informative msg for valid packets is printed later at log1 level */
  49        d6_dump_packet(packet);
  50
  51        return bytes;
  52}
  53
  54/* Construct a ipv6+udp header for a packet, send packet */
  55int FAST_FUNC d6_send_raw_packet_from_client_data_ifindex(
  56                struct d6_packet *d6_pkt, unsigned d6_pkt_size,
  57                struct in6_addr *src_ipv6, int source_port,
  58                struct in6_addr *dst_ipv6, int dest_port, const uint8_t *dest_arp)
  59{
  60        struct sockaddr_ll dest_sll;
  61        struct ip6_udp_d6_packet packet;
  62        int fd;
  63        int result = -1;
  64        const char *msg;
  65
  66        fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
  67        if (fd < 0) {
  68                msg = "socket(%s)";
  69                goto ret_msg;
  70        }
  71
  72        memset(&dest_sll, 0, sizeof(dest_sll));
  73        memset(&packet, 0, offsetof(struct ip6_udp_d6_packet, data));
  74        packet.data = *d6_pkt; /* struct copy */
  75
  76        dest_sll.sll_family = AF_PACKET;
  77        dest_sll.sll_protocol = htons(ETH_P_IPV6);
  78        dest_sll.sll_ifindex = client_data.ifindex;
  79        /*dest_sll.sll_hatype = ARPHRD_???;*/
  80        /*dest_sll.sll_pkttype = PACKET_???;*/
  81        dest_sll.sll_halen = 6;
  82        memcpy(dest_sll.sll_addr, dest_arp, 6);
  83
  84        if (bind(fd, (struct sockaddr *)&dest_sll, sizeof(dest_sll)) < 0) {
  85                msg = "bind(%s)";
  86                goto ret_close;
  87        }
  88
  89        packet.ip6.ip6_vfc = (6 << 4); /* 4 bits version, top 4 bits of tclass */
  90        if (src_ipv6)
  91                packet.ip6.ip6_src = *src_ipv6; /* struct copy */
  92        packet.ip6.ip6_dst = *dst_ipv6; /* struct copy */
  93        packet.udp.source = htons(source_port);
  94        packet.udp.dest = htons(dest_port);
  95        /* size, excluding IP header: */
  96        packet.udp.len = htons(sizeof(struct udphdr) + d6_pkt_size);
  97        packet.ip6.ip6_plen = packet.udp.len;
  98        /*
  99         * Someone was smoking weed (at least) while inventing UDP checksumming:
 100         * UDP checksum skips first four bytes of IPv6 header.
 101         * 'next header' field should be summed as if it is one more byte
 102         * to the right, therefore we write its value (IPPROTO_UDP)
 103         * into ip6_hlim, and its 'real' location remains zero-filled for now.
 104         */
 105        packet.ip6.ip6_hlim = IPPROTO_UDP;
 106        packet.udp.check = inet_cksum(
 107                        (uint8_t *)&packet + 4,
 108                        offsetof(struct ip6_udp_d6_packet, data) - 4 + d6_pkt_size
 109        );
 110        /* fix 'hop limit' and 'next header' after UDP checksumming */
 111        packet.ip6.ip6_hlim = 1; /* observed Windows machines to use hlim=1 */
 112        packet.ip6.ip6_nxt = IPPROTO_UDP;
 113
 114        d6_dump_packet(d6_pkt);
 115        result = sendto(fd, &packet, offsetof(struct ip6_udp_d6_packet, data) + d6_pkt_size,
 116                        /*flags:*/ 0,
 117                        (struct sockaddr *) &dest_sll, sizeof(dest_sll)
 118        );
 119        msg = "sendto";
 120 ret_close:
 121        close(fd);
 122        if (result < 0) {
 123 ret_msg:
 124                bb_perror_msg(msg, "PACKET");
 125        }
 126        return result;
 127}
 128
 129/* Let the kernel do all the work for packet generation */
 130int FAST_FUNC d6_send_kernel_packet_from_client_data_ifindex(
 131                struct d6_packet *d6_pkt, unsigned d6_pkt_size,
 132                struct in6_addr *src_ipv6, int source_port,
 133                struct in6_addr *dst_ipv6, int dest_port)
 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; /* struct copy */
 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; /* struct copy */
 160        sa.sin6_scope_id = client_data.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