4
* Packet socket handling glue.
7
#include <sys/socket.h>
12
#include <net/if_packet.h>
13
#include <netinet/if_ether.h>
14
#include <netinet/in.h>
15
#include <netpacket/packet.h>
16
#include <asm/byteorder.h>
17
#include <arpa/inet.h>
18
#include <netinet/ip.h>
19
#include <netinet/udp.h>
25
static int pkt_fd = -1;
27
__u16 cfg_local_port = LOCAL_PORT;
28
__u16 cfg_remote_port = REMOTE_PORT;
38
* Get a PACKET socket for IP traffic.
40
fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
47
* We want to broadcast
49
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one,
51
perror("SO_BROADCAST");
61
void packet_close(void)
67
static unsigned int ip_checksum(__u16 *hdr, int len)
69
unsigned int chksum = 0;
76
chksum = (chksum & 0xffff) + (chksum >> 16);
77
chksum = (chksum & 0xffff) + (chksum >> 16);
78
return (~chksum) & 0xffff;
84
} __attribute__((packed));
86
static struct header ipudp_hdrs = {
90
.frag_off = __constant_htons(IP_DF),
92
.protocol = IPPROTO_UDP,
94
.daddr = INADDR_BROADCAST,
97
.source = __constant_htons(LOCAL_PORT),
98
.dest = __constant_htons(REMOTE_PORT),
104
#ifdef IPC_DEBUG /* Only used by DEBUG(()) */
105
static char *ntoa(__u32 addr)
107
struct in_addr in = { addr };
108
return inet_ntoa(in);
113
* Send a packet. The options are listed in iov[1...iov_len].
114
* iov[0] is reserved for the bootp packet header.
116
int packet_send(struct netdev *dev, struct iovec *iov, int iov_len)
118
struct sockaddr_ll sll;
119
struct msghdr msg = {
121
.msg_namelen = sizeof(sll),
123
.msg_iovlen = iov_len,
130
if (cfg_local_port != LOCAL_PORT) {
131
ipudp_hdrs.udp.source = htons(cfg_local_port);
132
ipudp_hdrs.udp.dest = htons(cfg_remote_port);
135
DEBUG(("\n udp src %d dst %d", ntohs(ipudp_hdrs.udp.source),
136
ntohs(ipudp_hdrs.udp.dest)));
138
DEBUG(("\n ip src %s ", ntoa(ipudp_hdrs.ip.saddr)));
139
DEBUG(("dst %s ", ntoa(ipudp_hdrs.ip.daddr)));
142
* Glue in the ip+udp header iovec
144
iov[0].iov_base = &ipudp_hdrs;
145
iov[0].iov_len = sizeof(struct header);
147
for (i = 0; i < iov_len; i++)
148
len += iov[i].iov_len;
150
sll.sll_family = AF_PACKET;
151
sll.sll_protocol = htons(ETH_P_IP);
152
sll.sll_ifindex = dev->ifindex;
153
sll.sll_hatype = dev->hwtype;
154
sll.sll_pkttype = PACKET_BROADCAST;
155
sll.sll_halen = dev->hwlen;
156
memcpy(sll.sll_addr, dev->hwbrd, dev->hwlen);
158
ipudp_hdrs.ip.tot_len = htons(len);
159
ipudp_hdrs.ip.check = 0;
160
ipudp_hdrs.ip.check = ip_checksum((__u16 *)&ipudp_hdrs.ip,
163
ipudp_hdrs.udp.len = htons(len - sizeof(struct iphdr));
165
DEBUG(("\n bytes %d\n", len));
167
return sendmsg(pkt_fd, &msg, 0);
170
int packet_peek(int *ifindex)
172
struct sockaddr_ll sll;
174
int ret, sllen = sizeof(struct sockaddr_ll);
177
* Peek at the IP header.
179
ret = recvfrom(pkt_fd, &iph, sizeof(struct iphdr),
180
MSG_PEEK, (struct sockaddr *)&sll, &sllen);
184
if (sll.sll_family != AF_PACKET)
187
if (iph.ihl < 5 || iph.version != IPVERSION)
190
*ifindex = sll.sll_ifindex;
199
void packet_discard(void)
202
struct sockaddr_ll sll;
203
socklen_t sllen = sizeof(sll);
205
recvfrom(pkt_fd, &iph, sizeof(iph), 0,
206
(struct sockaddr *) &sll, &sllen);
210
* Receive a bootp packet. The options are listed in iov[1...iov_len].
211
* iov[0] must point to the bootp packet header.
213
int packet_recv(struct iovec *iov, int iov_len)
215
struct iphdr *ip, iph;
217
struct msghdr msg = {
221
.msg_iovlen = iov_len,
228
ret = recvfrom(pkt_fd, &iph, sizeof(struct iphdr),
229
MSG_PEEK, NULL, NULL);
233
if (iph.ihl < 5 || iph.version != IPVERSION)
238
ip = malloc(iphl + sizeof(struct udphdr));
242
udp = (struct udphdr *)((char *)ip + iphl);
244
iov[0].iov_base = ip;
245
iov[0].iov_len = iphl + sizeof(struct udphdr);
247
ret = recvmsg(pkt_fd, &msg, 0);
251
DEBUG(("<- bytes %d ", ret));
253
if (ip_checksum((__u16 *)ip, ip->ihl) != 0)
256
DEBUG(("\n ip src %s ", ntoa(ip->saddr)));
257
DEBUG(("dst %s ", ntoa(ip->daddr)));
259
if (ntohs(ip->tot_len) > ret || ip->protocol != IPPROTO_UDP)
264
DEBUG(("\n udp src %d dst %d ", ntohs(udp->source),
267
if (udp->source != htons(cfg_remote_port) ||
268
udp->dest != htons(cfg_local_port))
271
if (ntohs(udp->len) > ret)
274
ret -= sizeof(struct udphdr);
285
DEBUG(("discarded\n"));