Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 815 - (hide annotations) (download)
Fri Apr 24 18:32:46 2009 UTC (15 years ago) by niro
File MIME type: text/plain
File size: 5676 byte(s)
-updated to klibc-1.5.15
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     #ifdef IPC_DEBUG /* Only used by DEBUG(()) */
103     static char *ntoa(uint32_t addr)
104     {
105     struct in_addr in = { addr };
106     return inet_ntoa(in);
107     }
108     #endif
109    
110     /*
111     * Send a packet. The options are listed in iov[1...iov_len].
112     * 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     struct msghdr msg = {
118     .msg_name = &sll,
119     .msg_namelen = sizeof(sll),
120     .msg_iov = iov,
121     .msg_iovlen = iov_len,
122     .msg_control = NULL,
123     .msg_controllen = 0,
124     .msg_flags = 0
125     };
126     int i, len = 0;
127    
128     if (cfg_local_port != LOCAL_PORT) {
129     ipudp_hdrs.udp.source = htons(cfg_local_port);
130     ipudp_hdrs.udp.dest = htons(cfg_remote_port);
131     }
132    
133     DEBUG(("\n udp src %d dst %d", ntohs(ipudp_hdrs.udp.source),
134     ntohs(ipudp_hdrs.udp.dest)));
135    
136     DEBUG(("\n ip src %s ", ntoa(ipudp_hdrs.ip.saddr)));
137     DEBUG(("dst %s ", ntoa(ipudp_hdrs.ip.daddr)));
138    
139     /*
140     * Glue in the ip+udp header iovec
141     */
142     iov[0].iov_base = &ipudp_hdrs;
143     iov[0].iov_len = sizeof(struct header);
144    
145     for (i = 0; i < iov_len; i++)
146     len += iov[i].iov_len;
147    
148     sll.sll_family = AF_PACKET;
149     sll.sll_protocol = htons(ETH_P_IP);
150     sll.sll_ifindex = dev->ifindex;
151     sll.sll_hatype = dev->hwtype;
152     sll.sll_pkttype = PACKET_BROADCAST;
153     sll.sll_halen = dev->hwlen;
154     memcpy(sll.sll_addr, dev->hwbrd, dev->hwlen);
155    
156     ipudp_hdrs.ip.tot_len = htons(len);
157     ipudp_hdrs.ip.check = 0;
158     ipudp_hdrs.ip.check = ip_checksum((uint16_t *) & ipudp_hdrs.ip,
159     ipudp_hdrs.ip.ihl);
160    
161     ipudp_hdrs.udp.len = htons(len - sizeof(struct iphdr));
162    
163     DEBUG(("\n bytes %d\n", len));
164    
165     return sendmsg(pkt_fd, &msg, 0);
166     }
167    
168     int packet_peek(int *ifindex)
169     {
170     struct sockaddr_ll sll;
171     struct iphdr iph;
172     int ret, sllen = sizeof(struct sockaddr_ll);
173    
174     /*
175     * Peek at the IP header.
176     */
177     ret = recvfrom(pkt_fd, &iph, sizeof(struct iphdr),
178     MSG_PEEK, (struct sockaddr *)&sll, &sllen);
179     if (ret == -1)
180     return -1;
181    
182     if (sll.sll_family != AF_PACKET)
183     goto discard_pkt;
184    
185     if (iph.ihl < 5 || iph.version != IPVERSION)
186     goto discard_pkt;
187    
188     *ifindex = sll.sll_ifindex;
189    
190     return 0;
191    
192     discard_pkt:
193     packet_discard();
194     return 0;
195     }
196    
197     void packet_discard(void)
198     {
199     struct iphdr iph;
200     struct sockaddr_ll sll;
201     socklen_t sllen = sizeof(sll);
202    
203 niro 815 recvfrom(pkt_fd, &iph, sizeof(iph), 0,
204     (struct sockaddr *)&sll, &sllen);
205 niro 532 }
206    
207     /*
208     * Receive a bootp packet. The options are listed in iov[1...iov_len].
209     * iov[0] must point to the bootp packet header.
210     */
211     int packet_recv(struct iovec *iov, int iov_len)
212     {
213     struct iphdr *ip, iph;
214     struct udphdr *udp;
215     struct msghdr msg = {
216     .msg_name = NULL,
217     .msg_namelen = 0,
218     .msg_iov = iov,
219     .msg_iovlen = iov_len,
220     .msg_control = NULL,
221     .msg_controllen = 0,
222     .msg_flags = 0
223     };
224     int ret, iphl;
225    
226     ret = recvfrom(pkt_fd, &iph, sizeof(struct iphdr),
227     MSG_PEEK, NULL, NULL);
228     if (ret == -1)
229     return -1;
230    
231     if (iph.ihl < 5 || iph.version != IPVERSION)
232     goto discard_pkt;
233    
234     iphl = iph.ihl * 4;
235    
236     ip = malloc(iphl + sizeof(struct udphdr));
237     if (!ip)
238     goto discard_pkt;
239    
240     udp = (struct udphdr *)((char *)ip + iphl);
241    
242     iov[0].iov_base = ip;
243     iov[0].iov_len = iphl + sizeof(struct udphdr);
244    
245     ret = recvmsg(pkt_fd, &msg, 0);
246     if (ret == -1)
247     goto free_pkt;
248    
249     DEBUG(("<- bytes %d ", ret));
250    
251     if (ip_checksum((uint16_t *) ip, ip->ihl) != 0)
252     goto free_pkt;
253    
254     DEBUG(("\n ip src %s ", ntoa(ip->saddr)));
255     DEBUG(("dst %s ", ntoa(ip->daddr)));
256    
257     if (ntohs(ip->tot_len) > ret || ip->protocol != IPPROTO_UDP)
258     goto free_pkt;
259    
260     ret -= 4 * ip->ihl;
261    
262     DEBUG(("\n udp src %d dst %d ", ntohs(udp->source),
263     ntohs(udp->dest)));
264    
265     if (udp->source != htons(cfg_remote_port) ||
266     udp->dest != htons(cfg_local_port))
267     goto free_pkt;
268    
269     if (ntohs(udp->len) > ret)
270     goto free_pkt;
271    
272     ret -= sizeof(struct udphdr);
273    
274     free(ip);
275    
276     return ret;
277    
278     free_pkt:
279     free(ip);
280     return 0;
281    
282     discard_pkt:
283     DEBUG(("discarded\n"));
284     packet_discard();
285     return 0;
286     }