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