1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * PACKET - implements raw packet sockets.
7 *
8 * Version: @(#)packet.c 1.0.6 05/25/93
9 *
10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Alan Cox, <gw4pts@gw4pts.ampr.org>
13 *
14 * Fixes:
15 * Alan Cox : verify_area() now used correctly
16 * Alan Cox : new skbuff lists, look ma no backlogs!
17 * Alan Cox : tidied skbuff lists.
18 * Alan Cox : Now uses generic datagram routines I
19 * added. Also fixed the peek/read crash
20 * from all old Linux datagram code.
21 * Alan Cox : Uses the improved datagram code.
22 * Alan Cox : Added NULL's for socket options.
23 * Alan Cox : Re-commented the code.
24 * Alan Cox : Use new kernel side addressing
25 * Rob Janssen : Correct MTU usage.
26 * Dave Platt : Counter leaks caused by incorrect
27 * interrupt locking and some slightly
28 * dubious gcc output. Can you read
29 * compiler: it said _VOLATILE_
30 * Richard Kooijman : Timestamp fixes.
31 *
32 * This program is free software; you can redistribute it and/or
33 * modify it under the terms of the GNU General Public License
34 * as published by the Free Software Foundation; either version
35 * 2 of the License, or (at your option) any later version.
36 *
37 */
38
39 #include <linux/types.h>
40 #include <linux/sched.h>
41 #include <linux/mm.h>
42 #include <linux/fcntl.h>
43 #include <linux/socket.h>
44 #include <linux/in.h>
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <net/ip.h>
48 #include <net/protocol.h>
49 #include <linux/skbuff.h>
50 #include <net/sock.h>
51 #include <linux/errno.h>
52 #include <linux/timer.h>
53 #include <asm/system.h>
54 #include <asm/segment.h>
55
56 /*
57 * We really ought to have a single public _inline_ min function!
58 */
59
60 static unsigned long min(unsigned long a, unsigned long b)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
61 {
62 if (a < b)
63 return(a);
64 return(b);
65 }
66
67
68 /*
69 * This should be the easiest of all, all we do is copy it into a buffer.
70 */
71
72 int packet_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
73 {
74 struct sock *sk;
75 unsigned long flags;
76
77 /*
78 * When we registered the protocol we saved the socket in the data
79 * field for just this event.
80 */
81
82 sk = (struct sock *) pt->data;
83
84 /*
85 * The SOCK_PACKET socket receives _all_ frames, and as such
86 * therefore needs to put the header back onto the buffer.
87 * (it was removed by inet_bh()).
88 */
89
90 skb->dev = dev;
91 skb->len += dev->hard_header_len;
92
93 /*
94 * Charge the memory to the socket. This is done specifically
95 * to prevent sockets using all the memory up.
96 */
97
98 if (sk->rmem_alloc & 0xFF000000) {
99 printk("packet_rcv: sk->rmem_alloc = %ld\n", sk->rmem_alloc);
100 sk->rmem_alloc = 0;
101 }
102
103 if (sk->rmem_alloc + skb->mem_len >= sk->rcvbuf)
104 {
105 /* printk("packet_rcv: drop, %d+%d>%d\n", sk->rmem_alloc, skb->mem_len, sk->rcvbuf); */
106 skb->sk = NULL;
107 kfree_skb(skb, FREE_READ);
108 return(0);
109 }
110
111 save_flags(flags);
112 cli();
113
114 skb->sk = sk;
115 sk->rmem_alloc += skb->mem_len;
116
117 /*
118 * Queue the packet up, and wake anyone waiting for it.
119 */
120
121 skb_queue_tail(&sk->receive_queue,skb);
122 if(!sk->dead)
123 sk->data_ready(sk,skb->len);
124
125 restore_flags(flags);
126
127 /*
128 * Processing complete.
129 */
130
131 return(0);
132 }
133
134
135 /*
136 * Output a raw packet to a device layer. This bypasses all the other
137 * protocol layers and you must therefore supply it with a complete frame
138 */
139
140 static int packet_sendto(struct sock *sk, unsigned char *from, int len,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
141 int noblock, unsigned flags, struct sockaddr_in *usin,
142 int addr_len)
143 {
144 struct sk_buff *skb;
145 struct device *dev;
146 struct sockaddr *saddr=(struct sockaddr *)usin;
147
148 /*
149 * Check the flags.
150 */
151
152 if (flags)
153 return(-EINVAL);
154
155 /*
156 * Get and verify the address.
157 */
158
159 if (usin)
160 {
161 if (addr_len < sizeof(*saddr))
162 return(-EINVAL);
163 }
164 else
165 return(-EINVAL); /* SOCK_PACKET must be sent giving an address */
166
167 /*
168 * Find the device first to size check it
169 */
170
171 saddr->sa_data[13] = 0;
172 dev = dev_get(saddr->sa_data);
173 if (dev == NULL)
174 {
175 return(-ENXIO);
176 }
177
178 /*
179 * You may not queue a frame bigger than the mtu. This is the lowest level
180 * raw protocol and you must do your own fragmentation at this level.
181 */
182
183 if(len>dev->mtu+dev->hard_header_len)
184 return -EMSGSIZE;
185
186 skb = sk->prot->wmalloc(sk, len, 0, GFP_KERNEL);
187
188 /*
189 * If the write buffer is full, then tough. At this level the user gets to
190 * deal with the problem - do your own algorithmic backoffs.
191 */
192
193 if (skb == NULL)
194 {
195 return(-ENOBUFS);
196 }
197
198 /*
199 * Fill it in
200 */
201
202 skb->sk = sk;
203 skb->free = 1;
204 memcpy_fromfs(skb->data, from, len);
205 skb->len = len;
206 skb->arp = 1; /* No ARP needs doing on this (complete) frame */
207
208 /*
209 * Now send it
210 */
211
212 if (dev->flags & IFF_UP)
213 dev_queue_xmit(skb, dev, sk->priority);
214 else
215 kfree_skb(skb, FREE_WRITE);
216 return(len);
217 }
218
219 /*
220 * A write to a SOCK_PACKET can't actually do anything useful and will
221 * always fail but we include it for completeness and future expansion.
222 */
223
224 static int packet_write(struct sock *sk, unsigned char *buff,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
225 int len, int noblock, unsigned flags)
226 {
227 return(packet_sendto(sk, buff, len, noblock, flags, NULL, 0));
228 }
229
230 /*
231 * Close a SOCK_PACKET socket. This is fairly simple. We immediately go
232 * to 'closed' state and remove our protocol entry in the device list.
233 * The release_sock() will destroy the socket if a user has closed the
234 * file side of the object.
235 */
236
237 static void packet_close(struct sock *sk, int timeout)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
238 {
239 sk->inuse = 1;
240 sk->state = TCP_CLOSE;
241 dev_remove_pack((struct packet_type *)sk->pair);
242 kfree_s((void *)sk->pair, sizeof(struct packet_type));
243 sk->pair = NULL;
244 release_sock(sk);
245 }
246
247 /*
248 * Create a packet of type SOCK_PACKET. We do one slightly irregular
249 * thing here that wants tidying up. We borrow the 'pair' pointer in
250 * the socket object so we can find the packet_type entry in the
251 * device list. The reverse is easy as we use the data field of the
252 * packet type to point to our socket.
253 */
254
255 static int packet_init(struct sock *sk)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
256 {
257 struct packet_type *p;
258
259 p = (struct packet_type *) kmalloc(sizeof(*p), GFP_KERNEL);
260 if (p == NULL)
261 return(-ENOMEM);
262
263 p->func = packet_rcv;
264 p->type = sk->num;
265 p->data = (void *)sk;
266 p->dev = NULL;
267 dev_add_pack(p);
268
269 /*
270 * We need to remember this somewhere.
271 */
272
273 sk->pair = (struct sock *)p;
274
275 return(0);
276 }
277
278
279 /*
280 * Pull a packet from our receive queue and hand it to the user.
281 * If necessary we block.
282 */
283
284 int packet_recvfrom(struct sock *sk, unsigned char *to, int len,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
285 int noblock, unsigned flags, struct sockaddr_in *sin,
286 int *addr_len)
287 {
288 int copied=0;
289 struct sk_buff *skb;
290 struct sockaddr *saddr;
291 int err;
292 int truesize;
293
294 saddr = (struct sockaddr *)sin;
295
296 if (sk->shutdown & RCV_SHUTDOWN)
297 return(0);
298
299 /*
300 * If the address length field is there to be filled in, we fill
301 * it in now.
302 */
303
304 if (addr_len)
305 *addr_len=sizeof(*saddr);
306
307 /*
308 * Call the generic datagram receiver. This handles all sorts
309 * of horrible races and re-entrancy so we can forget about it
310 * in the protocol layers.
311 */
312
313 skb=skb_recv_datagram(sk,flags,noblock,&err);
314
315 /*
316 * An error occurred so return it. Because skb_recv_datagram()
317 * handles the blocking we don't see and worry about blocking
318 * retries.
319 */
320
321 if(skb==NULL)
322 return err;
323
324 /*
325 * You lose any data beyond the buffer you gave. If it worries a
326 * user program they can ask the device for its MTU anyway.
327 */
328
329 truesize = skb->len;
330 copied = min(len, truesize);
331
332 memcpy_tofs(to, skb->data, copied); /* We can't use skb_copy_datagram here */
333 sk->stamp=skb->stamp;
334
335 /*
336 * Copy the address.
337 */
338
339 if (saddr)
340 {
341 saddr->sa_family = skb->dev->type;
342 memcpy(saddr->sa_data,skb->dev->name, 14);
343 }
344
345 /*
346 * Free or return the buffer as appropriate. Again this hides all the
347 * races and re-entrancy issues from us.
348 */
349
350 skb_free_datagram(skb);
351
352 /*
353 * We are done.
354 */
355
356 release_sock(sk);
357 return(truesize);
358 }
359
360
361 /*
362 * A packet read can succeed and is just the same as a recvfrom but without the
363 * addresses being recorded.
364 */
365
366 int packet_read(struct sock *sk, unsigned char *buff,
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
367 int len, int noblock, unsigned flags)
368 {
369 return(packet_recvfrom(sk, buff, len, noblock, flags, NULL, NULL));
370 }
371
372
373 /*
374 * This structure declares to the lower layer socket subsystem currently
375 * incorrectly embedded in the IP code how to behave. This interface needs
376 * a lot of work and will change.
377 */
378
379 struct proto packet_prot =
380 {
381 sock_wmalloc,
382 sock_rmalloc,
383 sock_wfree,
384 sock_rfree,
385 sock_rspace,
386 sock_wspace,
387 packet_close,
388 packet_read,
389 packet_write,
390 packet_sendto,
391 packet_recvfrom,
392 ip_build_header, /* Not actually used */
393 NULL,
394 NULL,
395 ip_queue_xmit, /* These two are not actually used */
396 NULL,
397 NULL,
398 NULL,
399 NULL,
400 datagram_select,
401 NULL,
402 packet_init,
403 NULL,
404 NULL, /* No set/get socket options */
405 NULL,
406 128,
407 0,
408 "PACKET",
409 0, 0
410 };