whol.c 7.68 KB
Newer Older
1 2
/*
 * Copyright (c) 2005 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
22
 *
23
 * whol.c - Send a 'Whack-on-LAN' packet to node
24 25 26 27
 */

#include <stdio.h>
#include <fcntl.h>
28 29
#include <stdlib.h>
#include <string.h>
30 31 32 33 34

#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
35
#if defined(__FreeBSD__)
36
#include <net/if.h>
37 38 39 40 41 42
#include <net/bpf.h>
#elif defined(__linux__)
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#endif

43 44 45 46 47 48 49 50 51 52 53 54
#include <net/ethernet.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>

u_short in_cksum(u_short *addr, int len);

const int MAX_BPFNUM = 16;

const int packet_len = 666;

55 56
int whackanode(char *, char *);

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
int
main(int argc, char **argv)
{
    int failed = 0;
    char *iface;

    /*
     * Handle command line args
     */
    if (argc < 3) {
        fprintf(stderr,"Usage: whol <interface> <dst_address ...>\n");
        fprintf(stderr,"interface: Name of the interface to send packet on\n");
        fprintf(stderr,"dst_addr: MAC addresses in hex, without puncuation\n");
        exit(1);
    }
    argc--, argv++;

    iface = *argv;
    argc--, argv++;

    while (argc--)
	if (whackanode(iface, *argv++))
	    failed++;

    exit(failed);
}

int
whackanode(char *iface, char *victim)
{
87
#if defined(__FreeBSD__)
88 89
    int bpfnum;
    int bpffd;
90 91 92
#elif defined(__linux__)
    int sock;
#endif
93 94
    struct ifreq req;
    void *buf;
95
    ssize_t send_result;
96 97 98 99 100 101
    int i,j,length;
    struct ether_header *eheader;
    struct ip *ip;
    struct udphdr *udp;
    char *pbody;
    u_char dst_mac[6];
102 103 104 105
#if defined(__linux__)
    u_char src_mac[6];
    struct sockaddr_ll socket_address;
#endif
106 107 108 109 110 111

    /*
     * Convert the victim MAC address from ASCII into bytes
     */
    if (strlen(victim) != 12) {
        fprintf(stderr,"Bad format for MAC address\n");
112
	return 1;
113 114 115 116 117 118 119 120
    }
    for (i = 0; i < 6; i++) {
        char digits[3];
        unsigned int tmp;
        strncpy(digits, victim + (i*2),3); /* Copy in two digits */
        digits[2] = '\0'; /* Null-terminate */
        if (sscanf(digits,"%x",&tmp) != 1) {
            printf("Bad hex value in dst_addr: %s\n",digits);
121
	    return 1;
122 123 124 125
        }
        dst_mac[i] = tmp;
    }

126
#if defined(__FreeBSD__)
127 128 129 130 131 132 133 134 135 136 137 138 139 140
    /*
     * Find and open a BPF device to send the packet on
     */
    for (bpfnum = 0; bpfnum < MAX_BPFNUM; bpfnum++) {
        char bpfpath[1024];
        sprintf(bpfpath,"/dev/bpf%i",bpfnum);
        bpffd = open(bpfpath,O_WRONLY);
        if (bpffd >= 0) {
            break;
        }
    }

    if (bpffd < 0) {
        fprintf(stderr,"Failed to find an open-able BPF device\n");
141
	return 1;
142 143 144 145 146 147 148 149 150
    }

    /*
     * Attach it to the correct interface
     */
    strcpy(req.ifr_name,iface);

    if (ioctl(bpffd,BIOCSETIF,&req) < 0) {
        perror("BIOSETIF failed");
151
	return 1;
152
    }
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#elif defined(__linux__)
    if ((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
	perror("socket");
	return (1);
    }

    strcpy(req.ifr_name,iface);
    if (ioctl(sock,SIOCGIFHWADDR,&req) < 0) {
        perror("SIOGCIFHWADDR failed");
	return 1;
    }

    memcpy(src_mac, req.ifr_hwaddr.sa_data, sizeof(src_mac));

    strcpy(req.ifr_name,iface);
    if (ioctl(sock,SIOCGIFINDEX,&req) < 0) {
        perror("SIOGCIFINDEX failed");
	return 1;
    }

    memset(&socket_address, 0, sizeof(socket_address));
    socket_address.sll_family = PF_PACKET;
    socket_address.sll_protocol = htons(ETH_P_IP); /* XXX correct? */
    socket_address.sll_hatype = ARPHRD_ETHER; /* XXX is this needed? */
    socket_address.sll_pkttype = PACKET_OTHERHOST;
    socket_address.sll_halen = ETH_ALEN;
    socket_address.sll_ifindex = req.ifr_ifindex;
#endif
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

    /*
     * Build up a packet ourselves
     */
    buf = (void*)malloc(packet_len);
    bzero(buf,packet_len);

    eheader = buf;
    ip = buf + ETHER_HDR_LEN;
    udp = buf + ETHER_HDR_LEN + sizeof(*ip);
    pbody = buf + ETHER_HDR_LEN + sizeof(*ip) + sizeof(*udp);

    /*
     * Fill in the destination MAC, and make it look like an IP packet so that
     * the switch will deliver it.
     */
197
    memcpy(eheader->ether_dhost, dst_mac, sizeof(eheader->ether_dhost));
198 199
    eheader->ether_type = htons(ETHERTYPE_IP);

200 201 202 203 204
#if defined(__FreeBSD__)
    /* Note: The NIC will fill in the src MAC automagically on FreeBSD */
#elif defined(__linux__)
    memcpy(eheader->ether_shost, src_mac, sizeof(eheader->ether_shost));
#endif
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

    /*
     * Make an IP header
     */
    ip->ip_hl = (sizeof *ip >> 2);
    ip->ip_v = 4;
    ip->ip_tos = 0;
    ip->ip_len = htons(packet_len - ETHER_HDR_LEN);
    ip->ip_id = 0;
    ip->ip_off = 0;
    ip->ip_ttl = 1;
    ip->ip_p = 17; /* UDP, really oughta use getprotobyname */
    ip->ip_src.s_addr = 0xffffffff;
    ip->ip_dst.s_addr = 0xffffffff;
    ip->ip_sum = in_cksum((u_short *)ip,sizeof(*ip));

    /*
222
     * Make a UDP header; plain IP will work too
223
     */
224
#if defined(__FreeBSD__)
225 226 227 228
    udp->uh_sport = 0;
    udp->uh_dport = 0;
    udp->uh_ulen = htons(packet_len - ETHER_HDR_LEN - sizeof(*ip) - sizeof(*udp));
    udp->uh_sum = 0;
229 230 231 232 233 234
#else
    udp->source = 0;
    udp->dest = 0;
    udp->len = htons(packet_len - ETHER_HDR_LEN - sizeof(*ip) - sizeof(*udp));
    udp->check = 0;
#endif
235 236

    /*
237
     * Put in the magic juice that makes the victim reboot - 6 bytes of 1s,
238
     * then 16 repititions of the victim's MAC address
239 240
	 * 
	 * 11/05 NOTE: Our Intel cards ignore the 6 * 0xFF synchronization.
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
     */
    length = 0;
    for (i = 0; i < 6; i++) {
        pbody[length++] = (char)0xff;
    }

    for (i = 0; i < 16; i++) {
        for (j = 0; j < 6; j++) {
            pbody[length++] = dst_mac[j];
        }
    }

    /*
     * Whack away!
     */
256 257 258 259 260 261 262 263 264
#if defined(__FreeBSD__)
    send_result = write(bpffd,buf,packet_len);
#elif defined(__linux__)
    send_result = sendto(sock, buf, packet_len, 0,
                         (struct sockaddr *)&socket_address,
                         sizeof(socket_address));
#endif
    if (send_result < 0) {
        perror("whol: failed to send packet");
265 266
    }

267
    return 0;
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
}

/*
 * in_cksum --
 *      Checksum routine for Internet Protocol family headers (C Version)
 *      From FreeBSD's ping.c
 */

u_short
in_cksum(addr, len)
        u_short *addr;
        int len;
{
    register int nleft = len;
    register u_short *w = addr;
    register int sum = 0;
    u_short answer = 0;

    /*
     * Our algorithm is simple, using a 32 bit accumulator (sum), we add
     * sequential 16 bit words to it, and at the end, fold back all the
     * carry bits from the top 16 bits into the lower 16 bits.
     */
    while (nleft > 1)  {
        sum += *w++;
        nleft -= 2;
    }

    /* mop up an odd byte, if necessary */
    if (nleft == 1) {
        *(u_char *)(&answer) = *(u_char *)w ;
        sum += answer;
    }

    /* add back carry outs from top 16 bits to low 16 bits */
    sum = (sum >> 16) + (sum & 0xffff);     /* add hi 16 to low 16 */
    sum += (sum >> 16);                     /* add carry */
    answer = ~sum;                          /* truncate to 16 bits */
    return(answer);
}