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