2012-03-26 13:53:58 +03:00
|
|
|
/*
|
|
|
|
* WrapSix
|
|
|
|
* Copyright (C) 2008-2012 Michal Zima <xhire@mujmalysvet.cz>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2012-07-04 20:04:42 +03:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2012-03-26 13:53:58 +03:00
|
|
|
*
|
|
|
|
* This program 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
|
2012-07-04 20:04:42 +03:00
|
|
|
* GNU General Public License for more details.
|
2012-03-26 13:53:58 +03:00
|
|
|
*
|
2012-07-04 20:04:42 +03:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2012-03-26 13:53:58 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-03-26 13:58:18 +03:00
|
|
|
#include <net/if.h> /* struct ifreq */
|
2012-03-26 13:53:58 +03:00
|
|
|
#include <netinet/if_ether.h> /* {P,A}F_PACKET, ETH_P_*, socket, SOCK_RAW,
|
2012-07-03 12:15:10 +03:00
|
|
|
* setsockopt, SOL_SOCKET, SO_BINDTODEVICE,
|
|
|
|
* sendto */
|
2012-03-26 13:53:58 +03:00
|
|
|
#include <netinet/in.h> /* htons */
|
|
|
|
#include <netpacket/packet.h> /* sockaddr_ll, PACKET_OTHERHOST */
|
2012-07-04 12:37:42 +03:00
|
|
|
#include <stdio.h> /* perror */
|
2012-03-29 23:03:57 +03:00
|
|
|
#include <string.h> /* memcpy */
|
2012-03-26 13:53:58 +03:00
|
|
|
#include <unistd.h> /* close */
|
|
|
|
|
|
|
|
#include "ipv4.h"
|
2012-07-04 12:37:42 +03:00
|
|
|
#include "log.h"
|
2012-03-26 13:53:58 +03:00
|
|
|
#include "transmitter.h"
|
|
|
|
#include "wrapper.h"
|
|
|
|
|
|
|
|
struct sockaddr_ll socket_address;
|
2012-03-29 23:03:57 +03:00
|
|
|
struct sockaddr_in socket_address_ipv4;
|
|
|
|
int sock, sock_ipv4;
|
2012-03-26 13:53:58 +03:00
|
|
|
|
|
|
|
/**
|
2012-07-03 12:15:10 +03:00
|
|
|
* Initialize sockets and all needed properties. Should be called only once on
|
|
|
|
* program startup.
|
2012-03-26 13:53:58 +03:00
|
|
|
*
|
2012-07-04 12:37:42 +03:00
|
|
|
* @return 0 for success
|
|
|
|
* @return 1 for failure
|
2012-03-26 13:53:58 +03:00
|
|
|
*/
|
|
|
|
int transmission_init(void)
|
|
|
|
{
|
2012-03-29 23:03:57 +03:00
|
|
|
unsigned char on = 1;
|
|
|
|
|
|
|
|
/** RAW socket **/
|
2012-03-26 13:53:58 +03:00
|
|
|
/* prepare settings for RAW socket */
|
2012-07-03 12:15:10 +03:00
|
|
|
socket_address.sll_family = PF_PACKET; /* raw communication */
|
|
|
|
socket_address.sll_protocol = htons(ETH_P_IP); /* L3 proto */
|
|
|
|
socket_address.sll_ifindex = interface.ifr_ifindex;
|
|
|
|
socket_address.sll_pkttype = PACKET_OTHERHOST;
|
2012-03-26 13:53:58 +03:00
|
|
|
|
|
|
|
/* initialize RAW socket */
|
|
|
|
if ((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) {
|
2012-07-04 12:37:42 +03:00
|
|
|
log_error("Couldn't open RAW socket.");
|
2012-03-26 13:53:58 +03:00
|
|
|
perror("socket()");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bind the socket to the interface */
|
2012-07-03 12:15:10 +03:00
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &interface,
|
|
|
|
sizeof(struct ifreq)) == -1) {
|
2012-07-04 12:37:42 +03:00
|
|
|
log_error("Couldn't bind the socket to the interface.");
|
2012-03-26 13:53:58 +03:00
|
|
|
perror("setsockopt()");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-29 23:03:57 +03:00
|
|
|
|
|
|
|
/** IPv4 socket **/
|
|
|
|
/* prepare settings for RAW IPv4 socket */
|
|
|
|
socket_address_ipv4.sin_family = AF_INET;
|
|
|
|
socket_address_ipv4.sin_port = 0x0;
|
|
|
|
|
|
|
|
/* initialize RAW IPv4 socket */
|
|
|
|
if ((sock_ipv4 = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == -1) {
|
2012-07-04 12:37:42 +03:00
|
|
|
log_error("Couldn't open RAW IPv4 socket.");
|
2012-03-29 23:03:57 +03:00
|
|
|
perror("socket()");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we will provide our own IPv4 header */
|
2012-07-03 12:15:10 +03:00
|
|
|
if (setsockopt(sock_ipv4, IPPROTO_IP, IP_HDRINCL, &on,
|
|
|
|
sizeof(on)) == -1) {
|
2012-07-04 12:37:42 +03:00
|
|
|
log_error("Couldn't apply the socket settings.");
|
2012-03-29 23:03:57 +03:00
|
|
|
perror("setsockopt()");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-26 13:53:58 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-03 12:15:10 +03:00
|
|
|
* Close sockets. Should be called only once on program shutdown.
|
2012-03-26 13:53:58 +03:00
|
|
|
*
|
2012-07-04 12:37:42 +03:00
|
|
|
* @return 0 for success
|
|
|
|
* @return 1 for failure
|
2012-03-26 13:53:58 +03:00
|
|
|
*/
|
|
|
|
int transmission_quit(void)
|
|
|
|
{
|
|
|
|
/* close the socket */
|
2012-03-29 23:03:57 +03:00
|
|
|
if (close(sock) || close(sock_ipv4)) {
|
2012-07-04 12:37:42 +03:00
|
|
|
log_warn("Couldn't close the transmission sockets.");
|
2012-03-26 13:53:58 +03:00
|
|
|
perror("close()");
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send raw packet -- not doing any modifications to it.
|
|
|
|
*
|
|
|
|
* @param data Raw packet data, including ethernet header
|
|
|
|
* @param length Length of the whole packet in bytes
|
|
|
|
*
|
2012-07-04 12:37:42 +03:00
|
|
|
* @return 0 for success
|
|
|
|
* @return 1 for failure
|
2012-03-26 13:53:58 +03:00
|
|
|
*/
|
2012-03-29 23:03:57 +03:00
|
|
|
int transmit_raw(unsigned char *data, unsigned int length)
|
2012-03-26 13:53:58 +03:00
|
|
|
{
|
2012-07-03 12:15:10 +03:00
|
|
|
if (sendto(sock, data, length, 0, (struct sockaddr *) &socket_address,
|
|
|
|
sizeof(struct sockaddr_ll)) != (int) length) {
|
2012-07-04 12:37:42 +03:00
|
|
|
log_error("Couldn't send a RAW packet.");
|
2012-03-26 13:53:58 +03:00
|
|
|
perror("sendto()");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-29 23:03:57 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send IPv4 packet with IPv4 header supplied. Ethernet header is added by OS.
|
|
|
|
*
|
|
|
|
* @param ip Destination IPv4 address
|
2012-07-03 12:15:10 +03:00
|
|
|
* @param data Raw packet data, excluding ethernet header, but
|
|
|
|
* including IPv4 header
|
2012-03-29 23:03:57 +03:00
|
|
|
* @param length Length of the whole packet in bytes
|
|
|
|
*
|
2012-07-04 12:37:42 +03:00
|
|
|
* @return 0 for success
|
|
|
|
* @return 1 for failure
|
2012-03-29 23:03:57 +03:00
|
|
|
*/
|
2012-07-03 12:15:10 +03:00
|
|
|
int transmit_ipv4(struct s_ipv4_addr *ip, unsigned char *data,
|
|
|
|
unsigned int length)
|
2012-03-29 23:03:57 +03:00
|
|
|
{
|
|
|
|
/* set the destination IPv4 address */
|
2012-07-03 12:15:10 +03:00
|
|
|
memcpy(&socket_address_ipv4.sin_addr.s_addr, ip,
|
|
|
|
sizeof(struct s_ipv4_addr));
|
2012-03-29 23:03:57 +03:00
|
|
|
|
2012-07-03 12:15:10 +03:00
|
|
|
if (sendto(sock_ipv4, data, length, 0,
|
|
|
|
(struct sockaddr *) &socket_address_ipv4,
|
|
|
|
sizeof(struct sockaddr)) != (int) length) {
|
2012-07-04 12:37:42 +03:00
|
|
|
log_error("Couldn't send an IPv4 packet.");
|
2012-03-29 23:03:57 +03:00
|
|
|
perror("sendto()");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|