Magellan Linux

Annotation of /trunk/mkinitrd-magellan/klibc/usr/kinit/ipconfig/packet.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1297 - (hide annotations) (download)
Fri May 27 15:12:11 2011 UTC (12 years, 11 months ago) by niro
File MIME type: text/plain
File size: 5579 byte(s)
-updated to klibc-1.5.22 with mntproc definitions patch included
1 niro 532 /*
2     * Packet socket handling glue.
3     */
4     #include <sys/types.h>
5     #include <sys/socket.h>
6     #include <stdio.h>
7     #include <stdlib.h>
8     #include <string.h>
9     #include <unistd.h>
10     #include <net/if_packet.h>
11     #include <netinet/if_ether.h>
12     #include <netinet/in.h>
13     #include <netpacket/packet.h>
14     #include <asm/byteorder.h>
15     #include <arpa/inet.h>
16     #include <netinet/ip.h>
17     #include <netinet/udp.h>
18    
19     #include "ipconfig.h"
20     #include "netdev.h"
21     #include "packet.h"
22    
23     static int pkt_fd = -1;
24    
25     uint16_t cfg_local_port = LOCAL_PORT;
26     uint16_t cfg_remote_port = REMOTE_PORT;
27    
28     int packet_open(void)
29     {
30     int fd, one = 1;
31    
32     if (pkt_fd != -1)
33     return pkt_fd;
34    
35     /*
36     * Get a PACKET socket for IP traffic.
37     */
38     fd = socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
39     if (fd == -1) {
40     perror("socket");
41     return -1;
42     }
43    
44     /*
45     * We want to broadcast
46     */
47 niro 815 if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one,
48     sizeof(one)) == -1) {
49 niro 532 perror("SO_BROADCAST");
50     close(fd);
51     fd = -1;
52     }
53    
54     pkt_fd = fd;
55    
56     return fd;
57     }
58    
59     void packet_close(void)
60     {
61     close(pkt_fd);
62     pkt_fd = -1;
63     }
64    
65     static unsigned int ip_checksum(uint16_t *hdr, int len)
66     {
67     unsigned int chksum = 0;
68    
69     while (len) {
70     chksum += *hdr++;
71     chksum += *hdr++;
72     len--;
73     }
74     chksum = (chksum & 0xffff) + (chksum >> 16);
75     chksum = (chksum & 0xffff) + (chksum >> 16);
76     return (~chksum) & 0xffff;
77     }
78    
79     struct header {
80     struct iphdr ip;
81     struct udphdr udp;
82     } __attribute__ ((packed));
83    
84     static struct header ipudp_hdrs = {
85     .ip = {
86     .ihl = 5,
87     .version = IPVERSION,
88     .frag_off = __constant_htons(IP_DF),
89     .ttl = 64,
90     .protocol = IPPROTO_UDP,
91     .saddr = INADDR_ANY,
92     .daddr = INADDR_BROADCAST,
93     },
94     .udp = {
95     .source = __constant_htons(LOCAL_PORT),
96     .dest = __constant_htons(REMOTE_PORT),
97     .len = 0,
98     .check = 0,
99     },
100     };
101    
102 niro 1122 #ifdef DEBUG /* Only used with dprintf() */
103 niro 532 static char *ntoa(uint32_t addr)
104     {
105     struct in_addr in = { addr };
106     return inet_ntoa(in);
107     }
108 niro 1122 #endif /* DEBUG */
109 niro 532
110     /*
111 niro 1122 * Send a packet. The options are listed in iov[1...iov_len-1].
112 niro 532 * iov[0] is reserved for the bootp packet header.
113     */
114     int packet_send(struct netdev *dev, struct iovec *iov, int iov_len)
115     {
116     struct sockaddr_ll sll;
117 niro 1122 struct msghdr msg;
118 niro 532 int i, len = 0;
119    
120 niro 1122 memset(&sll, 0, sizeof(sll));
121     msg.msg_name = &sll;
122     msg.msg_namelen = sizeof(sll);
123     msg.msg_iov = iov;
124     msg.msg_iovlen = iov_len;
125     msg.msg_control = NULL;
126     msg.msg_controllen = 0;
127     msg.msg_flags = 0;
128    
129 niro 532 if (cfg_local_port != LOCAL_PORT) {
130     ipudp_hdrs.udp.source = htons(cfg_local_port);
131     ipudp_hdrs.udp.dest = htons(cfg_remote_port);
132     }
133    
134 niro 1122 dprintf("\n udp src %d dst %d", ntohs(ipudp_hdrs.udp.source),
135     ntohs(ipudp_hdrs.udp.dest));
136 niro 532
137 niro 1122 dprintf("\n ip src %s ", ntoa(ipudp_hdrs.ip.saddr));
138     dprintf("dst %s ", ntoa(ipudp_hdrs.ip.daddr));
139 niro 532
140     /*
141     * Glue in the ip+udp header iovec
142     */
143     iov[0].iov_base = &ipudp_hdrs;
144     iov[0].iov_len = sizeof(struct header);
145    
146     for (i = 0; i < iov_len; i++)
147     len += iov[i].iov_len;
148    
149     sll.sll_family = AF_PACKET;
150     sll.sll_protocol = htons(ETH_P_IP);
151     sll.sll_ifindex = dev->ifindex;
152     sll.sll_hatype = dev->hwtype;
153     sll.sll_pkttype = PACKET_BROADCAST;
154     sll.sll_halen = dev->hwlen;
155     memcpy(sll.sll_addr, dev->hwbrd, dev->hwlen);
156    
157     ipudp_hdrs.ip.tot_len = htons(len);
158     ipudp_hdrs.ip.check = 0;
159     ipudp_hdrs.ip.check = ip_checksum((uint16_t *) & ipudp_hdrs.ip,
160     ipudp_hdrs.ip.ihl);
161    
162     ipudp_hdrs.udp.len = htons(len - sizeof(struct iphdr));
163    
164 niro 1122 dprintf("\n bytes %d\n", len);
165 niro 532
166     return sendmsg(pkt_fd, &msg, 0);
167     }
168    
169 niro 1297 void packet_discard(struct netdev *dev)
170 niro 532 {
171     struct iphdr iph;
172     struct sockaddr_ll sll;
173     socklen_t sllen = sizeof(sll);
174    
175 niro 1297 sll.sll_ifindex = dev->ifindex;
176    
177 niro 815 recvfrom(pkt_fd, &iph, sizeof(iph), 0,
178     (struct sockaddr *)&sll, &sllen);
179 niro 532 }
180    
181     /*
182     * Receive a bootp packet. The options are listed in iov[1...iov_len].
183     * iov[0] must point to the bootp packet header.
184 niro 1122 * Returns:
185 niro 1297 * -1 = Error, try again later
186     * 0 = Discarded packet (non-DHCP/BOOTP traffic)
187 niro 1122 * >0 = Size of packet
188 niro 532 */
189 niro 1297 int packet_recv(struct netdev* dev, struct iovec *iov, int iov_len)
190 niro 532 {
191     struct iphdr *ip, iph;
192     struct udphdr *udp;
193     struct msghdr msg = {
194     .msg_name = NULL,
195     .msg_namelen = 0,
196     .msg_iov = iov,
197     .msg_iovlen = iov_len,
198     .msg_control = NULL,
199     .msg_controllen = 0,
200     .msg_flags = 0
201     };
202     int ret, iphl;
203 niro 1297 struct sockaddr_ll sll;
204     socklen_t sllen = sizeof(sll);
205 niro 532
206 niro 1297 sll.sll_ifindex = dev->ifindex;
207     msg.msg_name = &sll;
208     msg.msg_namelen = sllen;
209    
210 niro 532 ret = recvfrom(pkt_fd, &iph, sizeof(struct iphdr),
211 niro 1297 MSG_PEEK, (struct sockaddr *)&sll, &sllen);
212 niro 532 if (ret == -1)
213 niro 1297 return -1;
214 niro 532
215     if (iph.ihl < 5 || iph.version != IPVERSION)
216     goto discard_pkt;
217    
218     iphl = iph.ihl * 4;
219    
220     ip = malloc(iphl + sizeof(struct udphdr));
221     if (!ip)
222     goto discard_pkt;
223    
224     udp = (struct udphdr *)((char *)ip + iphl);
225    
226     iov[0].iov_base = ip;
227     iov[0].iov_len = iphl + sizeof(struct udphdr);
228    
229     ret = recvmsg(pkt_fd, &msg, 0);
230     if (ret == -1)
231     goto free_pkt;
232    
233 niro 1122 dprintf("<- bytes %d ", ret);
234 niro 532
235     if (ip_checksum((uint16_t *) ip, ip->ihl) != 0)
236     goto free_pkt;
237    
238 niro 1122 dprintf("\n ip src %s ", ntoa(ip->saddr));
239     dprintf("dst %s ", ntoa(ip->daddr));
240 niro 532
241     if (ntohs(ip->tot_len) > ret || ip->protocol != IPPROTO_UDP)
242     goto free_pkt;
243    
244     ret -= 4 * ip->ihl;
245    
246 niro 1122 dprintf("\n udp src %d dst %d ", ntohs(udp->source),
247     ntohs(udp->dest));
248 niro 532
249     if (udp->source != htons(cfg_remote_port) ||
250     udp->dest != htons(cfg_local_port))
251     goto free_pkt;
252    
253     if (ntohs(udp->len) > ret)
254     goto free_pkt;
255    
256     ret -= sizeof(struct udphdr);
257    
258     free(ip);
259    
260     return ret;
261    
262     free_pkt:
263 niro 1122 dprintf("freed\n");
264 niro 532 free(ip);
265     return 0;
266    
267     discard_pkt:
268 niro 1122 dprintf("discarded\n");
269 niro 1297 packet_discard(dev);
270 niro 532 return 0;
271     }