1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/*
* ipconfig/packet.c
*
* Packet socket handling glue.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if_packet.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <netpacket/packet.h>
#include <asm/byteorder.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include "ipconfig.h"
#include "netdev.h"
#include "packet.h"
static int pkt_fd = -1;
__u16 cfg_local_port = LOCAL_PORT;
__u16 cfg_remote_port = REMOTE_PORT;
int packet_open(void)
{
int fd, one = 1;
if (pkt_fd != -1)
return pkt_fd;
/*
* Get a PACKET socket for IP traffic.
*/
fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
if (fd == -1) {
perror("socket");
return -1;
}
/*
* We want to broadcast
*/
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one,
sizeof(one)) == -1) {
perror("SO_BROADCAST");
close(fd);
fd = -1;
}
pkt_fd = fd;
return fd;
}
void packet_close(void)
{
close(pkt_fd);
pkt_fd = -1;
}
static unsigned int ip_checksum(__u16 *hdr, int len)
{
unsigned int chksum = 0;
while (len) {
chksum += *hdr++;
chksum += *hdr++;
len--;
}
chksum = (chksum & 0xffff) + (chksum >> 16);
chksum = (chksum & 0xffff) + (chksum >> 16);
return (~chksum) & 0xffff;
}
struct header {
struct iphdr ip;
struct udphdr udp;
} __attribute__((packed));
static struct header ipudp_hdrs = {
.ip = {
.ihl = 5,
.version = IPVERSION,
.frag_off = __constant_htons(IP_DF),
.ttl = 64,
.protocol = IPPROTO_UDP,
.saddr = INADDR_ANY,
.daddr = INADDR_BROADCAST,
},
.udp = {
.source = __constant_htons(LOCAL_PORT),
.dest = __constant_htons(REMOTE_PORT),
.len = 0,
.check = 0,
},
};
#ifdef IPC_DEBUG /* Only used by DEBUG(()) */
static char *ntoa(__u32 addr)
{
struct in_addr in = { addr };
return inet_ntoa(in);
}
#endif
/*
* Send a packet. The options are listed in iov[1...iov_len].
* iov[0] is reserved for the bootp packet header.
*/
int packet_send(struct netdev *dev, struct iovec *iov, int iov_len)
{
struct sockaddr_ll sll;
struct msghdr msg = {
.msg_name = &sll,
.msg_namelen = sizeof(sll),
.msg_iov = iov,
.msg_iovlen = iov_len,
.msg_control = NULL,
.msg_controllen = 0,
.msg_flags = 0
};
int i, len = 0;
if (cfg_local_port != LOCAL_PORT) {
ipudp_hdrs.udp.source = htons(cfg_local_port);
ipudp_hdrs.udp.dest = htons(cfg_remote_port);
}
DEBUG(("\n udp src %d dst %d", ntohs(ipudp_hdrs.udp.source),
ntohs(ipudp_hdrs.udp.dest)));
DEBUG(("\n ip src %s ", ntoa(ipudp_hdrs.ip.saddr)));
DEBUG(("dst %s ", ntoa(ipudp_hdrs.ip.daddr)));
/*
* Glue in the ip+udp header iovec
*/
iov[0].iov_base = &ipudp_hdrs;
iov[0].iov_len = sizeof(struct header);
for (i = 0; i < iov_len; i++)
len += iov[i].iov_len;
sll.sll_family = AF_PACKET;
sll.sll_protocol = htons(ETH_P_IP);
sll.sll_ifindex = dev->ifindex;
sll.sll_hatype = dev->hwtype;
sll.sll_pkttype = PACKET_BROADCAST;
sll.sll_halen = dev->hwlen;
memcpy(sll.sll_addr, dev->hwbrd, dev->hwlen);
ipudp_hdrs.ip.tot_len = htons(len);
ipudp_hdrs.ip.check = 0;
ipudp_hdrs.ip.check = ip_checksum((__u16 *)&ipudp_hdrs.ip,
ipudp_hdrs.ip.ihl);
ipudp_hdrs.udp.len = htons(len - sizeof(struct iphdr));
DEBUG(("\n bytes %d\n", len));
return sendmsg(pkt_fd, &msg, 0);
}
int packet_peek(int *ifindex)
{
struct sockaddr_ll sll;
struct iphdr iph;
int ret, sllen = sizeof(struct sockaddr_ll);
/*
* Peek at the IP header.
*/
ret = recvfrom(pkt_fd, &iph, sizeof(struct iphdr),
MSG_PEEK, (struct sockaddr *)&sll, &sllen);
if (ret == -1)
return -1;
if (sll.sll_family != AF_PACKET)
goto discard_pkt;
if (iph.ihl < 5 || iph.version != IPVERSION)
goto discard_pkt;
*ifindex = sll.sll_ifindex;
return 0;
discard_pkt:
packet_discard();
return 0;
}
void packet_discard(void)
{
struct iphdr iph;
struct sockaddr_ll sll;
socklen_t sllen = sizeof(sll);
recvfrom(pkt_fd, &iph, sizeof(iph), 0,
(struct sockaddr *) &sll, &sllen);
}
/*
* Receive a bootp packet. The options are listed in iov[1...iov_len].
* iov[0] must point to the bootp packet header.
*/
int packet_recv(struct iovec *iov, int iov_len)
{
struct iphdr *ip, iph;
struct udphdr *udp;
struct msghdr msg = {
.msg_name = NULL,
.msg_namelen = 0,
.msg_iov = iov,
.msg_iovlen = iov_len,
.msg_control = NULL,
.msg_controllen = 0,
.msg_flags = 0
};
int ret, iphl;
ret = recvfrom(pkt_fd, &iph, sizeof(struct iphdr),
MSG_PEEK, NULL, NULL);
if (ret == -1)
return -1;
if (iph.ihl < 5 || iph.version != IPVERSION)
goto discard_pkt;
iphl = iph.ihl * 4;
ip = malloc(iphl + sizeof(struct udphdr));
if (!ip)
goto discard_pkt;
udp = (struct udphdr *)((char *)ip + iphl);
iov[0].iov_base = ip;
iov[0].iov_len = iphl + sizeof(struct udphdr);
ret = recvmsg(pkt_fd, &msg, 0);
if (ret == -1)
goto free_pkt;
DEBUG(("<- bytes %d ", ret));
if (ip_checksum((__u16 *)ip, ip->ihl) != 0)
goto free_pkt;
DEBUG(("\n ip src %s ", ntoa(ip->saddr)));
DEBUG(("dst %s ", ntoa(ip->daddr)));
if (ntohs(ip->tot_len) > ret || ip->protocol != IPPROTO_UDP)
goto free_pkt;
ret -= 4 * ip->ihl;
DEBUG(("\n udp src %d dst %d ", ntohs(udp->source),
ntohs(udp->dest)));
if (udp->source != htons(cfg_remote_port) ||
udp->dest != htons(cfg_local_port))
goto free_pkt;
if (ntohs(udp->len) > ret)
goto free_pkt;
ret -= sizeof(struct udphdr);
free(ip);
return ret;
free_pkt:
free(ip);
return 0;
discard_pkt:
DEBUG(("discarded\n"));
packet_discard();
return 0;
}
|