blob: ace29f1c4c5be3ca297a8968e1f61b8072ce2245 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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: $Id: af_packet.c,v 1.61 2002/02/08 03:57:19 davem Exp $
9 *
Jesper Juhl02c30a82005-05-05 16:16:16 -070010 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Alan Cox, <gw4pts@gw4pts.ampr.org>
13 *
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090014 * Fixes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * 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 * Alan Cox : Protocol setting support
34 * Alexey Kuznetsov : Untied from IPv4 stack.
35 * Cyrus Durgin : Fixed kerneld for kmod.
36 * Michal Ostrowski : Module initialization cleanup.
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090037 * Ulises Alonso : Frame number limit removal and
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * packet_set_ring memory leak.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070039 * Eric Biederman : Allow for > 8 byte hardware addresses.
40 * The convention is that longer addresses
41 * will simply extend the hardware address
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090042 * byte arrays at the end of sockaddr_ll
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070043 * and packet_mreq.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
45 * This program is free software; you can redistribute it and/or
46 * modify it under the terms of the GNU General Public License
47 * as published by the Free Software Foundation; either version
48 * 2 of the License, or (at your option) any later version.
49 *
50 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/mm.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080054#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/fcntl.h>
56#include <linux/socket.h>
57#include <linux/in.h>
58#include <linux/inet.h>
59#include <linux/netdevice.h>
60#include <linux/if_packet.h>
61#include <linux/wireless.h>
Herbert Xuffbc6112007-02-04 23:33:10 -080062#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/kmod.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020064#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/ip.h>
66#include <net/protocol.h>
67#include <linux/skbuff.h>
68#include <net/sock.h>
69#include <linux/errno.h>
70#include <linux/timer.h>
71#include <asm/system.h>
72#include <asm/uaccess.h>
73#include <asm/ioctls.h>
74#include <asm/page.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040075#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <asm/io.h>
77#include <linux/proc_fs.h>
78#include <linux/seq_file.h>
79#include <linux/poll.h>
80#include <linux/module.h>
81#include <linux/init.h>
82
83#ifdef CONFIG_INET
84#include <net/inet_common.h>
85#endif
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 Assumptions:
89 - if device has no dev->hard_header routine, it adds and removes ll header
90 inside itself. In this case ll header is invisible outside of device,
91 but higher levels still should reserve dev->hard_header_len.
92 Some devices are enough clever to reallocate skb, when header
93 will not fit to reserved space (tunnel), another ones are silly
94 (PPP).
95 - packet socket receives packets with pulled ll header,
96 so that SOCK_RAW should push it back.
97
98On receive:
99-----------
100
101Incoming, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700102 mac_header -> ll header
103 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105Outgoing, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700106 mac_header -> ll header
107 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109Incoming, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700110 mac_header -> UNKNOWN position. It is very likely, that it points to ll
111 header. PPP makes it, that is wrong, because introduce
YOSHIFUJI Hideakidb0c58f2007-07-19 10:44:35 +0900112 assymetry between rx and tx paths.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700113 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115Outgoing, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700116 mac_header -> data. ll header is still not built!
117 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119Resume
120 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
121
122
123On transmit:
124------------
125
126dev->hard_header != NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700127 mac_header -> ll header
128 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130dev->hard_header == NULL (ll header is added by device, we cannot control it)
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700131 mac_header -> data
132 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 We should set nh.raw on output to correct posistion,
135 packet classifier depends on it.
136 */
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Private packet socket structures. */
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140struct packet_mclist
141{
142 struct packet_mclist *next;
143 int ifindex;
144 int count;
145 unsigned short type;
146 unsigned short alen;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700147 unsigned char addr[MAX_ADDR_LEN];
148};
149/* identical to struct packet_mreq except it has
150 * a longer address field.
151 */
152struct packet_mreq_max
153{
154 int mr_ifindex;
155 unsigned short mr_type;
156 unsigned short mr_alen;
157 unsigned char mr_address[MAX_ADDR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
David S. Millera2efcfa2007-05-29 13:12:50 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#ifdef CONFIG_PACKET_MMAP
161static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing);
162#endif
163
164static void packet_flush_mclist(struct sock *sk);
165
166struct packet_sock {
167 /* struct sock has to be the first member of packet_sock */
168 struct sock sk;
169 struct tpacket_stats stats;
170#ifdef CONFIG_PACKET_MMAP
171 char * *pg_vec;
172 unsigned int head;
173 unsigned int frames_per_block;
174 unsigned int frame_size;
175 unsigned int frame_max;
176 int copy_thresh;
177#endif
178 struct packet_type prot_hook;
179 spinlock_t bind_lock;
Herbert Xu8dc41942007-02-04 23:31:32 -0800180 unsigned int running:1, /* prot_hook is attached*/
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700181 auxdata:1,
182 origdev:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int ifindex; /* bound device */
Al Viro0e11c912006-11-08 00:26:29 -0800184 __be16 num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct packet_mclist *mclist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#ifdef CONFIG_PACKET_MMAP
187 atomic_t mapped;
188 unsigned int pg_vec_order;
189 unsigned int pg_vec_pages;
190 unsigned int pg_vec_len;
191#endif
192};
193
Herbert Xuffbc6112007-02-04 23:33:10 -0800194struct packet_skb_cb {
195 unsigned int origlen;
196 union {
197 struct sockaddr_pkt pkt;
198 struct sockaddr_ll ll;
199 } sa;
200};
201
202#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204#ifdef CONFIG_PACKET_MMAP
205
Jason Lunzad930652007-02-20 23:19:54 -0800206static inline struct tpacket_hdr *packet_lookup_frame(struct packet_sock *po, unsigned int position)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 unsigned int pg_vec_pos, frame_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 pg_vec_pos = position / po->frames_per_block;
211 frame_offset = position % po->frames_per_block;
212
Jason Lunzad930652007-02-20 23:19:54 -0800213 return (struct tpacket_hdr *)(po->pg_vec[pg_vec_pos] + (frame_offset * po->frame_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215#endif
216
217static inline struct packet_sock *pkt_sk(struct sock *sk)
218{
219 return (struct packet_sock *)sk;
220}
221
222static void packet_sock_destruct(struct sock *sk)
223{
224 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
225 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
226
227 if (!sock_flag(sk, SOCK_DEAD)) {
228 printk("Attempt to release alive packet socket: %p\n", sk);
229 return;
230 }
231
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -0800232 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800236static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800238static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700240static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct sock *sk;
243 struct sockaddr_pkt *spkt;
244
245 /*
246 * When we registered the protocol we saved the socket in the data
247 * field for just this event.
248 */
249
250 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /*
253 * Yank back the headers [hope the device set this
254 * right or kerboom...]
255 *
256 * Incoming packets have ll header pulled,
257 * push it back.
258 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700259 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 * so that this procedure is noop.
261 */
262
263 if (skb->pkt_type == PACKET_LOOPBACK)
264 goto out;
265
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800266 if (dev->nd_net != sk->sk_net)
267 goto out;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
270 goto oom;
271
272 /* drop any routing info */
273 dst_release(skb->dst);
274 skb->dst = NULL;
275
Phil Oester84531c22005-07-12 11:57:52 -0700276 /* drop conntrack reference */
277 nf_reset(skb);
278
Herbert Xuffbc6112007-02-04 23:33:10 -0800279 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700281 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /*
284 * The SOCK_PACKET socket receives _all_ frames.
285 */
286
287 spkt->spkt_family = dev->type;
288 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
289 spkt->spkt_protocol = skb->protocol;
290
291 /*
292 * Charge the memory to the socket. This is done specifically
293 * to prevent sockets using all the memory up.
294 */
295
296 if (sock_queue_rcv_skb(sk,skb) == 0)
297 return 0;
298
299out:
300 kfree_skb(skb);
301oom:
302 return 0;
303}
304
305
306/*
307 * Output a raw packet to a device layer. This bypasses all the other
308 * protocol layers and you must therefore supply it with a complete frame
309 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
312 struct msghdr *msg, size_t len)
313{
314 struct sock *sk = sock->sk;
315 struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name;
316 struct sk_buff *skb;
317 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -0800318 __be16 proto=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int err;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900322 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
324
325 if (saddr)
326 {
327 if (msg->msg_namelen < sizeof(struct sockaddr))
328 return(-EINVAL);
329 if (msg->msg_namelen==sizeof(struct sockaddr_pkt))
330 proto=saddr->spkt_protocol;
331 }
332 else
333 return(-ENOTCONN); /* SOCK_PACKET must be sent giving an address */
334
335 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900336 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
338
339 saddr->spkt_device[13] = 0;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800340 dev = dev_get_by_name(sk->sk_net, saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 err = -ENODEV;
342 if (dev == NULL)
343 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900344
David S. Millerd5e76b02007-01-25 19:30:36 -0800345 err = -ENETDOWN;
346 if (!(dev->flags & IFF_UP))
347 goto out_unlock;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /*
350 * You may not queue a frame bigger than the mtu. This is the lowest level
351 * raw protocol and you must do your own fragmentation at this level.
352 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 err = -EMSGSIZE;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -0800355 if (len > dev->mtu + dev->hard_header_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 goto out_unlock;
357
358 err = -ENOBUFS;
359 skb = sock_wmalloc(sk, len + LL_RESERVED_SPACE(dev), 0, GFP_KERNEL);
360
361 /*
362 * If the write buffer is full, then tough. At this level the user gets to
363 * deal with the problem - do your own algorithmic backoffs. That's far
364 * more flexible.
365 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900366
367 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 goto out_unlock;
369
370 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900371 * Fill it in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* FIXME: Save some space for broken drivers that write a
375 * hard header at transmission time by themselves. PPP is the
376 * notable one here. This should really be fixed at the driver level.
377 */
378 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700379 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /* Try to align data part correctly */
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700382 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 skb->data -= dev->hard_header_len;
384 skb->tail -= dev->hard_header_len;
385 if (len < dev->hard_header_len)
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700386 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 /* Returns -EFAULT on error */
390 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
391 skb->protocol = proto;
392 skb->dev = dev;
393 skb->priority = sk->sk_priority;
394 if (err)
395 goto out_free;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
398 * Now send it
399 */
400
401 dev_queue_xmit(skb);
402 dev_put(dev);
403 return(len);
404
405out_free:
406 kfree_skb(skb);
407out_unlock:
408 if (dev)
409 dev_put(dev);
410 return err;
411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
David S. Millerdbcb5852007-01-24 15:21:02 -0800413static inline unsigned int run_filter(struct sk_buff *skb, struct sock *sk,
414 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 struct sk_filter *filter;
417
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700418 rcu_read_lock_bh();
419 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -0800420 if (filter != NULL)
421 res = sk_run_filter(skb, filter->insns, filter->len);
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700422 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
David S. Millerdbcb5852007-01-24 15:21:02 -0800424 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427/*
428 This function makes lazy skb cloning in hope that most of packets
429 are discarded by BPF.
430
431 Note tricky part: we DO mangle shared skb! skb->data, skb->len
432 and skb->cb are mangled. It works because (and until) packets
433 falling here are owned by current CPU. Output packets are cloned
434 by dev_queue_xmit_nit(), input packets are processed by net_bh
435 sequencially, so that if we return skb to original state on exit,
436 we will not harm anyone.
437 */
438
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700439static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct sock *sk;
442 struct sockaddr_ll *sll;
443 struct packet_sock *po;
444 u8 * skb_head = skb->data;
445 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800446 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (skb->pkt_type == PACKET_LOOPBACK)
449 goto drop;
450
451 sk = pt->af_packet_priv;
452 po = pkt_sk(sk);
453
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800454 if (dev->nd_net != sk->sk_net)
455 goto drop;
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 skb->dev = dev;
458
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700459 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* The device has an explicit notion of ll header,
461 exported to higher levels.
462
463 Otherwise, the device hides datails of it frame
464 structure, so that corresponding packet head
465 never delivered to user.
466 */
467 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700468 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 else if (skb->pkt_type == PACKET_OUTGOING) {
470 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300471 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 }
474
475 snaplen = skb->len;
476
David S. Millerdbcb5852007-01-24 15:21:02 -0800477 res = run_filter(skb, sk, snaplen);
478 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700479 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800480 if (snaplen > res)
481 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
484 (unsigned)sk->sk_rcvbuf)
485 goto drop_n_acct;
486
487 if (skb_shared(skb)) {
488 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
489 if (nskb == NULL)
490 goto drop_n_acct;
491
492 if (skb_head != skb->data) {
493 skb->data = skb_head;
494 skb->len = skb_len;
495 }
496 kfree_skb(skb);
497 skb = nskb;
498 }
499
Herbert Xuffbc6112007-02-04 23:33:10 -0800500 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
501 sizeof(skb->cb));
502
503 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 sll->sll_family = AF_PACKET;
505 sll->sll_hatype = dev->type;
506 sll->sll_protocol = skb->protocol;
507 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800508 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700509 sll->sll_ifindex = orig_dev->ifindex;
510 else
511 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700513 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Herbert Xuffbc6112007-02-04 23:33:10 -0800515 PACKET_SKB_CB(skb)->origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -0800516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 if (pskb_trim(skb, snaplen))
518 goto drop_n_acct;
519
520 skb_set_owner_r(skb, sk);
521 skb->dev = NULL;
522 dst_release(skb->dst);
523 skb->dst = NULL;
524
Phil Oester84531c22005-07-12 11:57:52 -0700525 /* drop conntrack reference */
526 nf_reset(skb);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 spin_lock(&sk->sk_receive_queue.lock);
529 po->stats.tp_packets++;
530 __skb_queue_tail(&sk->sk_receive_queue, skb);
531 spin_unlock(&sk->sk_receive_queue.lock);
532 sk->sk_data_ready(sk, skb->len);
533 return 0;
534
535drop_n_acct:
536 spin_lock(&sk->sk_receive_queue.lock);
537 po->stats.tp_drops++;
538 spin_unlock(&sk->sk_receive_queue.lock);
539
540drop_n_restore:
541 if (skb_head != skb->data && skb_shared(skb)) {
542 skb->data = skb_head;
543 skb->len = skb_len;
544 }
545drop:
546 kfree_skb(skb);
547 return 0;
548}
549
550#ifdef CONFIG_PACKET_MMAP
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700551static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 struct sock *sk;
554 struct packet_sock *po;
555 struct sockaddr_ll *sll;
556 struct tpacket_hdr *h;
557 u8 * skb_head = skb->data;
558 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800559 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
561 unsigned short macoff, netoff;
562 struct sk_buff *copy_skb = NULL;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700563 struct timeval tv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (skb->pkt_type == PACKET_LOOPBACK)
566 goto drop;
567
568 sk = pt->af_packet_priv;
569 po = pkt_sk(sk);
570
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800571 if (dev->nd_net != sk->sk_net)
572 goto drop;
573
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700574 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700576 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 else if (skb->pkt_type == PACKET_OUTGOING) {
578 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300579 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581 }
582
Herbert Xu8dc41942007-02-04 23:31:32 -0800583 if (skb->ip_summed == CHECKSUM_PARTIAL)
584 status |= TP_STATUS_CSUMNOTREADY;
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 snaplen = skb->len;
587
David S. Millerdbcb5852007-01-24 15:21:02 -0800588 res = run_filter(skb, sk, snaplen);
589 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700590 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800591 if (snaplen > res)
592 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 if (sk->sk_type == SOCK_DGRAM) {
595 macoff = netoff = TPACKET_ALIGN(TPACKET_HDRLEN) + 16;
596 } else {
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300597 unsigned maclen = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 netoff = TPACKET_ALIGN(TPACKET_HDRLEN + (maclen < 16 ? 16 : maclen));
599 macoff = netoff - maclen;
600 }
601
602 if (macoff + snaplen > po->frame_size) {
603 if (po->copy_thresh &&
604 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
605 (unsigned)sk->sk_rcvbuf) {
606 if (skb_shared(skb)) {
607 copy_skb = skb_clone(skb, GFP_ATOMIC);
608 } else {
609 copy_skb = skb_get(skb);
610 skb_head = skb->data;
611 }
612 if (copy_skb)
613 skb_set_owner_r(copy_skb, sk);
614 }
615 snaplen = po->frame_size - macoff;
616 if ((int)snaplen < 0)
617 snaplen = 0;
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 spin_lock(&sk->sk_receive_queue.lock);
Jason Lunzad930652007-02-20 23:19:54 -0800621 h = packet_lookup_frame(po, po->head);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (h->tp_status)
624 goto ring_is_full;
625 po->head = po->head != po->frame_max ? po->head+1 : 0;
626 po->stats.tp_packets++;
627 if (copy_skb) {
628 status |= TP_STATUS_COPY;
629 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
630 }
631 if (!po->stats.tp_drops)
632 status &= ~TP_STATUS_LOSING;
633 spin_unlock(&sk->sk_receive_queue.lock);
634
Patrick McHardycbe21d82006-09-17 23:59:57 -0700635 skb_copy_bits(skb, 0, (u8*)h + macoff, snaplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 h->tp_len = skb->len;
638 h->tp_snaplen = snaplen;
639 h->tp_mac = macoff;
640 h->tp_net = netoff;
Stephen Hemminger50f17782007-09-06 13:55:02 +0100641 if (skb->tstamp.tv64)
642 tv = ktime_to_timeval(skb->tstamp);
643 else
644 do_gettimeofday(&tv);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700645 h->tp_sec = tv.tv_sec;
646 h->tp_usec = tv.tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 sll = (struct sockaddr_ll*)((u8*)h + TPACKET_ALIGN(sizeof(*h)));
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700649 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 sll->sll_family = AF_PACKET;
651 sll->sll_hatype = dev->type;
652 sll->sll_protocol = skb->protocol;
653 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800654 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700655 sll->sll_ifindex = orig_dev->ifindex;
656 else
657 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 h->tp_status = status;
Ralf Baechlee16aa202006-12-07 00:11:33 -0800660 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 {
663 struct page *p_start, *p_end;
664 u8 *h_end = (u8 *)h + macoff + snaplen - 1;
665
666 p_start = virt_to_page(h);
667 p_end = virt_to_page(h_end);
668 while (p_start <= p_end) {
669 flush_dcache_page(p_start);
670 p_start++;
671 }
672 }
673
674 sk->sk_data_ready(sk, 0);
675
676drop_n_restore:
677 if (skb_head != skb->data && skb_shared(skb)) {
678 skb->data = skb_head;
679 skb->len = skb_len;
680 }
681drop:
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900682 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684
685ring_is_full:
686 po->stats.tp_drops++;
687 spin_unlock(&sk->sk_receive_queue.lock);
688
689 sk->sk_data_ready(sk, 0);
690 if (copy_skb)
691 kfree_skb(copy_skb);
692 goto drop_n_restore;
693}
694
695#endif
696
697
698static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
699 struct msghdr *msg, size_t len)
700{
701 struct sock *sk = sock->sk;
702 struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name;
703 struct sk_buff *skb;
704 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -0800705 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 unsigned char *addr;
707 int ifindex, err, reserve = 0;
708
709 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900710 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (saddr == NULL) {
714 struct packet_sock *po = pkt_sk(sk);
715
716 ifindex = po->ifindex;
717 proto = po->num;
718 addr = NULL;
719 } else {
720 err = -EINVAL;
721 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
722 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700723 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
724 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 ifindex = saddr->sll_ifindex;
726 proto = saddr->sll_protocol;
727 addr = saddr->sll_addr;
728 }
729
730
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800731 dev = dev_get_by_index(sk->sk_net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 err = -ENXIO;
733 if (dev == NULL)
734 goto out_unlock;
735 if (sock->type == SOCK_RAW)
736 reserve = dev->hard_header_len;
737
David S. Millerd5e76b02007-01-25 19:30:36 -0800738 err = -ENETDOWN;
739 if (!(dev->flags & IFF_UP))
740 goto out_unlock;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 err = -EMSGSIZE;
743 if (len > dev->mtu+reserve)
744 goto out_unlock;
745
746 skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev),
747 msg->msg_flags & MSG_DONTWAIT, &err);
748 if (skb==NULL)
749 goto out_unlock;
750
751 skb_reserve(skb, LL_RESERVED_SPACE(dev));
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700752 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Stephen Hemminger0c4e8582007-10-09 01:36:32 -0700754 err = -EINVAL;
755 if (sock->type == SOCK_DGRAM &&
756 dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len) < 0)
757 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /* Returns -EFAULT on error */
760 err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
761 if (err)
762 goto out_free;
763
764 skb->protocol = proto;
765 skb->dev = dev;
766 skb->priority = sk->sk_priority;
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /*
769 * Now send it
770 */
771
772 err = dev_queue_xmit(skb);
773 if (err > 0 && (err = net_xmit_errno(err)) != 0)
774 goto out_unlock;
775
776 dev_put(dev);
777
778 return(len);
779
780out_free:
781 kfree_skb(skb);
782out_unlock:
783 if (dev)
784 dev_put(dev);
785out:
786 return err;
787}
788
789/*
790 * Close a PACKET socket. This is fairly simple. We immediately go
791 * to 'closed' state and remove our protocol entry in the device list.
792 */
793
794static int packet_release(struct socket *sock)
795{
796 struct sock *sk = sock->sk;
797 struct packet_sock *po;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800798 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 if (!sk)
801 return 0;
802
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800803 net = sk->sk_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 po = pkt_sk(sk);
805
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800806 write_lock_bh(&net->packet_sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 sk_del_node_init(sk);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800808 write_unlock_bh(&net->packet_sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /*
811 * Unhook packet receive handler.
812 */
813
814 if (po->running) {
815 /*
816 * Remove the protocol hook
817 */
818 dev_remove_pack(&po->prot_hook);
819 po->running = 0;
820 po->num = 0;
821 __sock_put(sk);
822 }
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826#ifdef CONFIG_PACKET_MMAP
827 if (po->pg_vec) {
828 struct tpacket_req req;
829 memset(&req, 0, sizeof(req));
830 packet_set_ring(sk, &req, 1);
831 }
832#endif
833
834 /*
835 * Now the socket is dead. No more input will appear.
836 */
837
838 sock_orphan(sk);
839 sock->sk = NULL;
840
841 /* Purge queues */
842
843 skb_queue_purge(&sk->sk_receive_queue);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -0800844 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 sock_put(sk);
847 return 0;
848}
849
850/*
851 * Attach a packet hook.
852 */
853
Al Viro0e11c912006-11-08 00:26:29 -0800854static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 struct packet_sock *po = pkt_sk(sk);
857 /*
858 * Detach an existing hook if present.
859 */
860
861 lock_sock(sk);
862
863 spin_lock(&po->bind_lock);
864 if (po->running) {
865 __sock_put(sk);
866 po->running = 0;
867 po->num = 0;
868 spin_unlock(&po->bind_lock);
869 dev_remove_pack(&po->prot_hook);
870 spin_lock(&po->bind_lock);
871 }
872
873 po->num = protocol;
874 po->prot_hook.type = protocol;
875 po->prot_hook.dev = dev;
876
877 po->ifindex = dev ? dev->ifindex : 0;
878
879 if (protocol == 0)
880 goto out_unlock;
881
Urs Thuermannbe85d4a2007-11-12 21:05:20 -0800882 if (!dev || (dev->flags & IFF_UP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 dev_add_pack(&po->prot_hook);
884 sock_hold(sk);
885 po->running = 1;
Urs Thuermannbe85d4a2007-11-12 21:05:20 -0800886 } else {
887 sk->sk_err = ENETDOWN;
888 if (!sock_flag(sk, SOCK_DEAD))
889 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
892out_unlock:
893 spin_unlock(&po->bind_lock);
894 release_sock(sk);
895 return 0;
896}
897
898/*
899 * Bind a packet socket to a device
900 */
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int addr_len)
903{
904 struct sock *sk=sock->sk;
905 char name[15];
906 struct net_device *dev;
907 int err = -ENODEV;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /*
910 * Check legality
911 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900912
Kris Katterjohn8ae55f02006-01-23 16:28:02 -0800913 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -EINVAL;
915 strlcpy(name,uaddr->sa_data,sizeof(name));
916
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800917 dev = dev_get_by_name(sk->sk_net, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (dev) {
919 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
920 dev_put(dev);
921 }
922 return err;
923}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
926{
927 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
928 struct sock *sk=sock->sk;
929 struct net_device *dev = NULL;
930 int err;
931
932
933 /*
934 * Check legality
935 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (addr_len < sizeof(struct sockaddr_ll))
938 return -EINVAL;
939 if (sll->sll_family != AF_PACKET)
940 return -EINVAL;
941
942 if (sll->sll_ifindex) {
943 err = -ENODEV;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800944 dev = dev_get_by_index(sk->sk_net, sll->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (dev == NULL)
946 goto out;
947 }
948 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
949 if (dev)
950 dev_put(dev);
951
952out:
953 return err;
954}
955
956static struct proto packet_proto = {
957 .name = "PACKET",
958 .owner = THIS_MODULE,
959 .obj_size = sizeof(struct packet_sock),
960};
961
962/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900963 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 */
965
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700966static int packet_create(struct net *net, struct socket *sock, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 struct sock *sk;
969 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -0800970 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 int err;
972
973 if (!capable(CAP_NET_RAW))
974 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -0700975 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
976 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return -ESOCKTNOSUPPORT;
978
979 sock->state = SS_UNCONNECTED;
980
981 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700982 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (sk == NULL)
984 goto out;
985
986 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (sock->type == SOCK_PACKET)
988 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -0700989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 sock_init_data(sock, sk);
991
992 po = pkt_sk(sk);
993 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -0800994 po->num = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -0800997 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /*
1000 * Attach a protocol block
1001 */
1002
1003 spin_lock_init(&po->bind_lock);
1004 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (sock->type == SOCK_PACKET)
1007 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 po->prot_hook.af_packet_priv = sk;
1010
Al Viro0e11c912006-11-08 00:26:29 -08001011 if (proto) {
1012 po->prot_hook.type = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 dev_add_pack(&po->prot_hook);
1014 sock_hold(sk);
1015 po->running = 1;
1016 }
1017
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001018 write_lock_bh(&net->packet_sklist_lock);
1019 sk_add_node(sk, &net->packet_sklist);
1020 write_unlock_bh(&net->packet_sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return(0);
1022out:
1023 return err;
1024}
1025
1026/*
1027 * Pull a packet from our receive queue and hand it to the user.
1028 * If necessary we block.
1029 */
1030
1031static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1032 struct msghdr *msg, size_t len, int flags)
1033{
1034 struct sock *sk = sock->sk;
1035 struct sk_buff *skb;
1036 int copied, err;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001037 struct sockaddr_ll *sll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 err = -EINVAL;
1040 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
1041 goto out;
1042
1043#if 0
1044 /* What error should we return now? EUNATTACH? */
1045 if (pkt_sk(sk)->ifindex < 0)
1046 return -ENODEV;
1047#endif
1048
1049 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 * Call the generic datagram receiver. This handles all sorts
1051 * of horrible races and re-entrancy so we can forget about it
1052 * in the protocol layers.
1053 *
1054 * Now it will return ENETDOWN, if device have just gone down,
1055 * but then it will block.
1056 */
1057
1058 skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
1059
1060 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001061 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 * handles the blocking we don't see and worry about blocking
1063 * retries.
1064 */
1065
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001066 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 goto out;
1068
1069 /*
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001070 * If the address length field is there to be filled in, we fill
1071 * it in now.
1072 */
1073
Herbert Xuffbc6112007-02-04 23:33:10 -08001074 sll = &PACKET_SKB_CB(skb)->sa.ll;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001075 if (sock->type == SOCK_PACKET)
1076 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1077 else
1078 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1079
1080 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 * You lose any data beyond the buffer you gave. If it worries a
1082 * user program they can ask the device for its MTU anyway.
1083 */
1084
1085 copied = skb->len;
1086 if (copied > len)
1087 {
1088 copied=len;
1089 msg->msg_flags|=MSG_TRUNC;
1090 }
1091
1092 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1093 if (err)
1094 goto out_free;
1095
1096 sock_recv_timestamp(msg, sk, skb);
1097
1098 if (msg->msg_name)
Herbert Xuffbc6112007-02-04 23:33:10 -08001099 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
1100 msg->msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Herbert Xu8dc41942007-02-04 23:31:32 -08001102 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08001103 struct tpacket_auxdata aux;
1104
1105 aux.tp_status = TP_STATUS_USER;
1106 if (skb->ip_summed == CHECKSUM_PARTIAL)
1107 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
1108 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
1109 aux.tp_snaplen = skb->len;
1110 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001111 aux.tp_net = skb_network_offset(skb);
Herbert Xuffbc6112007-02-04 23:33:10 -08001112
1113 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08001114 }
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /*
1117 * Free or return the buffer as appropriate. Again this
1118 * hides all the races and re-entrancy issues from us.
1119 */
1120 err = (flags&MSG_TRUNC) ? skb->len : copied;
1121
1122out_free:
1123 skb_free_datagram(sk, skb);
1124out:
1125 return err;
1126}
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1129 int *uaddr_len, int peer)
1130{
1131 struct net_device *dev;
1132 struct sock *sk = sock->sk;
1133
1134 if (peer)
1135 return -EOPNOTSUPP;
1136
1137 uaddr->sa_family = AF_PACKET;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001138 dev = dev_get_by_index(sk->sk_net, pkt_sk(sk)->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (dev) {
1140 strlcpy(uaddr->sa_data, dev->name, 15);
1141 dev_put(dev);
1142 } else
1143 memset(uaddr->sa_data, 0, 14);
1144 *uaddr_len = sizeof(*uaddr);
1145
1146 return 0;
1147}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1150 int *uaddr_len, int peer)
1151{
1152 struct net_device *dev;
1153 struct sock *sk = sock->sk;
1154 struct packet_sock *po = pkt_sk(sk);
1155 struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
1156
1157 if (peer)
1158 return -EOPNOTSUPP;
1159
1160 sll->sll_family = AF_PACKET;
1161 sll->sll_ifindex = po->ifindex;
1162 sll->sll_protocol = po->num;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001163 dev = dev_get_by_index(sk->sk_net, po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (dev) {
1165 sll->sll_hatype = dev->type;
1166 sll->sll_halen = dev->addr_len;
1167 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
1168 dev_put(dev);
1169 } else {
1170 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1171 sll->sll_halen = 0;
1172 }
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001173 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 return 0;
1176}
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178static void packet_dev_mc(struct net_device *dev, struct packet_mclist *i, int what)
1179{
1180 switch (i->type) {
1181 case PACKET_MR_MULTICAST:
1182 if (what > 0)
1183 dev_mc_add(dev, i->addr, i->alen, 0);
1184 else
1185 dev_mc_delete(dev, i->addr, i->alen, 0);
1186 break;
1187 case PACKET_MR_PROMISC:
1188 dev_set_promiscuity(dev, what);
1189 break;
1190 case PACKET_MR_ALLMULTI:
1191 dev_set_allmulti(dev, what);
1192 break;
1193 default:;
1194 }
1195}
1196
1197static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1198{
1199 for ( ; i; i=i->next) {
1200 if (i->ifindex == dev->ifindex)
1201 packet_dev_mc(dev, i, what);
1202 }
1203}
1204
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001205static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 struct packet_sock *po = pkt_sk(sk);
1208 struct packet_mclist *ml, *i;
1209 struct net_device *dev;
1210 int err;
1211
1212 rtnl_lock();
1213
1214 err = -ENODEV;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001215 dev = __dev_get_by_index(sk->sk_net, mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (!dev)
1217 goto done;
1218
1219 err = -EINVAL;
1220 if (mreq->mr_alen > dev->addr_len)
1221 goto done;
1222
1223 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001224 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (i == NULL)
1226 goto done;
1227
1228 err = 0;
1229 for (ml = po->mclist; ml; ml = ml->next) {
1230 if (ml->ifindex == mreq->mr_ifindex &&
1231 ml->type == mreq->mr_type &&
1232 ml->alen == mreq->mr_alen &&
1233 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1234 ml->count++;
1235 /* Free the new element ... */
1236 kfree(i);
1237 goto done;
1238 }
1239 }
1240
1241 i->type = mreq->mr_type;
1242 i->ifindex = mreq->mr_ifindex;
1243 i->alen = mreq->mr_alen;
1244 memcpy(i->addr, mreq->mr_address, i->alen);
1245 i->count = 1;
1246 i->next = po->mclist;
1247 po->mclist = i;
1248 packet_dev_mc(dev, i, +1);
1249
1250done:
1251 rtnl_unlock();
1252 return err;
1253}
1254
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001255static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
1257 struct packet_mclist *ml, **mlp;
1258
1259 rtnl_lock();
1260
1261 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
1262 if (ml->ifindex == mreq->mr_ifindex &&
1263 ml->type == mreq->mr_type &&
1264 ml->alen == mreq->mr_alen &&
1265 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1266 if (--ml->count == 0) {
1267 struct net_device *dev;
1268 *mlp = ml->next;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001269 dev = dev_get_by_index(sk->sk_net, ml->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (dev) {
1271 packet_dev_mc(dev, ml, -1);
1272 dev_put(dev);
1273 }
1274 kfree(ml);
1275 }
1276 rtnl_unlock();
1277 return 0;
1278 }
1279 }
1280 rtnl_unlock();
1281 return -EADDRNOTAVAIL;
1282}
1283
1284static void packet_flush_mclist(struct sock *sk)
1285{
1286 struct packet_sock *po = pkt_sk(sk);
1287 struct packet_mclist *ml;
1288
1289 if (!po->mclist)
1290 return;
1291
1292 rtnl_lock();
1293 while ((ml = po->mclist) != NULL) {
1294 struct net_device *dev;
1295
1296 po->mclist = ml->next;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001297 if ((dev = dev_get_by_index(sk->sk_net, ml->ifindex)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 packet_dev_mc(dev, ml, -1);
1299 dev_put(dev);
1300 }
1301 kfree(ml);
1302 }
1303 rtnl_unlock();
1304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306static int
1307packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
1308{
1309 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08001310 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 int ret;
1312
1313 if (level != SOL_PACKET)
1314 return -ENOPROTOOPT;
1315
1316 switch(optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001317 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 case PACKET_DROP_MEMBERSHIP:
1319 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001320 struct packet_mreq_max mreq;
1321 int len = optlen;
1322 memset(&mreq, 0, sizeof(mreq));
1323 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001325 if (len > sizeof(mreq))
1326 len = sizeof(mreq);
1327 if (copy_from_user(&mreq,optval,len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001329 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
1330 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (optname == PACKET_ADD_MEMBERSHIP)
1332 ret = packet_mc_add(sk, &mreq);
1333 else
1334 ret = packet_mc_drop(sk, &mreq);
1335 return ret;
1336 }
David S. Millera2efcfa2007-05-29 13:12:50 -07001337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338#ifdef CONFIG_PACKET_MMAP
1339 case PACKET_RX_RING:
1340 {
1341 struct tpacket_req req;
1342
1343 if (optlen<sizeof(req))
1344 return -EINVAL;
1345 if (copy_from_user(&req,optval,sizeof(req)))
1346 return -EFAULT;
1347 return packet_set_ring(sk, &req, 0);
1348 }
1349 case PACKET_COPY_THRESH:
1350 {
1351 int val;
1352
1353 if (optlen!=sizeof(val))
1354 return -EINVAL;
1355 if (copy_from_user(&val,optval,sizeof(val)))
1356 return -EFAULT;
1357
1358 pkt_sk(sk)->copy_thresh = val;
1359 return 0;
1360 }
1361#endif
Herbert Xu8dc41942007-02-04 23:31:32 -08001362 case PACKET_AUXDATA:
1363 {
1364 int val;
1365
1366 if (optlen < sizeof(val))
1367 return -EINVAL;
1368 if (copy_from_user(&val, optval, sizeof(val)))
1369 return -EFAULT;
1370
1371 po->auxdata = !!val;
1372 return 0;
1373 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07001374 case PACKET_ORIGDEV:
1375 {
1376 int val;
1377
1378 if (optlen < sizeof(val))
1379 return -EINVAL;
1380 if (copy_from_user(&val, optval, sizeof(val)))
1381 return -EFAULT;
1382
1383 po->origdev = !!val;
1384 return 0;
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 default:
1387 return -ENOPROTOOPT;
1388 }
1389}
1390
1391static int packet_getsockopt(struct socket *sock, int level, int optname,
1392 char __user *optval, int __user *optlen)
1393{
1394 int len;
Herbert Xu8dc41942007-02-04 23:31:32 -08001395 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 struct sock *sk = sock->sk;
1397 struct packet_sock *po = pkt_sk(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08001398 void *data;
1399 struct tpacket_stats st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 if (level != SOL_PACKET)
1402 return -ENOPROTOOPT;
1403
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001404 if (get_user(len, optlen))
1405 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 if (len < 0)
1408 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 switch(optname) {
1411 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (len > sizeof(struct tpacket_stats))
1413 len = sizeof(struct tpacket_stats);
1414 spin_lock_bh(&sk->sk_receive_queue.lock);
1415 st = po->stats;
1416 memset(&po->stats, 0, sizeof(st));
1417 spin_unlock_bh(&sk->sk_receive_queue.lock);
1418 st.tp_packets += st.tp_drops;
1419
Herbert Xu8dc41942007-02-04 23:31:32 -08001420 data = &st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08001422 case PACKET_AUXDATA:
1423 if (len > sizeof(int))
1424 len = sizeof(int);
1425 val = po->auxdata;
1426
1427 data = &val;
1428 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07001429 case PACKET_ORIGDEV:
1430 if (len > sizeof(int))
1431 len = sizeof(int);
1432 val = po->origdev;
1433
1434 data = &val;
1435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 default:
1437 return -ENOPROTOOPT;
1438 }
1439
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001440 if (put_user(len, optlen))
1441 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08001442 if (copy_to_user(optval, data, len))
1443 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
1447
1448static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
1449{
1450 struct sock *sk;
1451 struct hlist_node *node;
Jason Lunzad930652007-02-20 23:19:54 -08001452 struct net_device *dev = data;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001453 struct net *net = dev->nd_net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001455 read_lock(&net->packet_sklist_lock);
1456 sk_for_each(sk, node, &net->packet_sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 struct packet_sock *po = pkt_sk(sk);
1458
1459 switch (msg) {
1460 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (po->mclist)
1462 packet_dev_mclist(dev, po->mclist, -1);
David S. Millera2efcfa2007-05-29 13:12:50 -07001463 /* fallthrough */
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 case NETDEV_DOWN:
1466 if (dev->ifindex == po->ifindex) {
1467 spin_lock(&po->bind_lock);
1468 if (po->running) {
1469 __dev_remove_pack(&po->prot_hook);
1470 __sock_put(sk);
1471 po->running = 0;
1472 sk->sk_err = ENETDOWN;
1473 if (!sock_flag(sk, SOCK_DEAD))
1474 sk->sk_error_report(sk);
1475 }
1476 if (msg == NETDEV_UNREGISTER) {
1477 po->ifindex = -1;
1478 po->prot_hook.dev = NULL;
1479 }
1480 spin_unlock(&po->bind_lock);
1481 }
1482 break;
1483 case NETDEV_UP:
1484 spin_lock(&po->bind_lock);
1485 if (dev->ifindex == po->ifindex && po->num &&
1486 !po->running) {
1487 dev_add_pack(&po->prot_hook);
1488 sock_hold(sk);
1489 po->running = 1;
1490 }
1491 spin_unlock(&po->bind_lock);
1492 break;
1493 }
1494 }
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001495 read_unlock(&net->packet_sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return NOTIFY_DONE;
1497}
1498
1499
1500static int packet_ioctl(struct socket *sock, unsigned int cmd,
1501 unsigned long arg)
1502{
1503 struct sock *sk = sock->sk;
1504
1505 switch(cmd) {
1506 case SIOCOUTQ:
1507 {
1508 int amount = atomic_read(&sk->sk_wmem_alloc);
1509 return put_user(amount, (int __user *)arg);
1510 }
1511 case SIOCINQ:
1512 {
1513 struct sk_buff *skb;
1514 int amount = 0;
1515
1516 spin_lock_bh(&sk->sk_receive_queue.lock);
1517 skb = skb_peek(&sk->sk_receive_queue);
1518 if (skb)
1519 amount = skb->len;
1520 spin_unlock_bh(&sk->sk_receive_queue.lock);
1521 return put_user(amount, (int __user *)arg);
1522 }
1523 case SIOCGSTAMP:
1524 return sock_get_timestamp(sk, (struct timeval __user *)arg);
Eric Dumazetae40eb12007-03-18 17:33:16 -07001525 case SIOCGSTAMPNS:
1526 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528#ifdef CONFIG_INET
1529 case SIOCADDRT:
1530 case SIOCDELRT:
1531 case SIOCDARP:
1532 case SIOCGARP:
1533 case SIOCSARP:
1534 case SIOCGIFADDR:
1535 case SIOCSIFADDR:
1536 case SIOCGIFBRDADDR:
1537 case SIOCSIFBRDADDR:
1538 case SIOCGIFNETMASK:
1539 case SIOCSIFNETMASK:
1540 case SIOCGIFDSTADDR:
1541 case SIOCSIFDSTADDR:
1542 case SIOCSIFFLAGS:
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001543 if (sk->sk_net != &init_net)
1544 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return inet_dgram_ops.ioctl(sock, cmd, arg);
1546#endif
1547
1548 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001549 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551 return 0;
1552}
1553
1554#ifndef CONFIG_PACKET_MMAP
1555#define packet_mmap sock_no_mmap
1556#define packet_poll datagram_poll
1557#else
1558
1559static unsigned int packet_poll(struct file * file, struct socket *sock,
1560 poll_table *wait)
1561{
1562 struct sock *sk = sock->sk;
1563 struct packet_sock *po = pkt_sk(sk);
1564 unsigned int mask = datagram_poll(file, sock, wait);
1565
1566 spin_lock_bh(&sk->sk_receive_queue.lock);
1567 if (po->pg_vec) {
1568 unsigned last = po->head ? po->head-1 : po->frame_max;
1569 struct tpacket_hdr *h;
1570
Jason Lunzad930652007-02-20 23:19:54 -08001571 h = packet_lookup_frame(po, last);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 if (h->tp_status)
1574 mask |= POLLIN | POLLRDNORM;
1575 }
1576 spin_unlock_bh(&sk->sk_receive_queue.lock);
1577 return mask;
1578}
1579
1580
1581/* Dirty? Well, I still did not learn better way to account
1582 * for user mmaps.
1583 */
1584
1585static void packet_mm_open(struct vm_area_struct *vma)
1586{
1587 struct file *file = vma->vm_file;
Eric Dumazetb69aee02005-09-06 14:42:45 -07001588 struct socket * sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (sk)
1592 atomic_inc(&pkt_sk(sk)->mapped);
1593}
1594
1595static void packet_mm_close(struct vm_area_struct *vma)
1596{
1597 struct file *file = vma->vm_file;
Eric Dumazetb69aee02005-09-06 14:42:45 -07001598 struct socket * sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (sk)
1602 atomic_dec(&pkt_sk(sk)->mapped);
1603}
1604
1605static struct vm_operations_struct packet_mmap_ops = {
1606 .open = packet_mm_open,
1607 .close =packet_mm_close,
1608};
1609
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001610static void free_pg_vec(char **pg_vec, unsigned int order, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 int i;
1613
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001614 for (i = 0; i < len; i++) {
1615 if (likely(pg_vec[i]))
1616 free_pages((unsigned long) pg_vec[i], order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618 kfree(pg_vec);
1619}
1620
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001621static inline char *alloc_one_pg_vec_page(unsigned long order)
1622{
1623 return (char *) __get_free_pages(GFP_KERNEL | __GFP_COMP | __GFP_ZERO,
1624 order);
1625}
1626
1627static char **alloc_pg_vec(struct tpacket_req *req, int order)
1628{
1629 unsigned int block_nr = req->tp_block_nr;
1630 char **pg_vec;
1631 int i;
1632
1633 pg_vec = kzalloc(block_nr * sizeof(char *), GFP_KERNEL);
1634 if (unlikely(!pg_vec))
1635 goto out;
1636
1637 for (i = 0; i < block_nr; i++) {
1638 pg_vec[i] = alloc_one_pg_vec_page(order);
1639 if (unlikely(!pg_vec[i]))
1640 goto out_free_pgvec;
1641 }
1642
1643out:
1644 return pg_vec;
1645
1646out_free_pgvec:
1647 free_pg_vec(pg_vec, order, block_nr);
1648 pg_vec = NULL;
1649 goto out;
1650}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing)
1653{
1654 char **pg_vec = NULL;
1655 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08001656 int was_running, order = 0;
1657 __be16 num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 int err = 0;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (req->tp_block_nr) {
1661 int i, l;
1662
1663 /* Sanity tests and some calculations */
1664
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001665 if (unlikely(po->pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return -EBUSY;
1667
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001668 if (unlikely((int)req->tp_block_size <= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001670 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001672 if (unlikely(req->tp_frame_size < TPACKET_HDRLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001674 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return -EINVAL;
1676
1677 po->frames_per_block = req->tp_block_size/req->tp_frame_size;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001678 if (unlikely(po->frames_per_block <= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001680 if (unlikely((po->frames_per_block * req->tp_block_nr) !=
1681 req->tp_frame_nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001685 order = get_order(req->tp_block_size);
1686 pg_vec = alloc_pg_vec(req, order);
1687 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 l = 0;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001691 for (i = 0; i < req->tp_block_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 char *ptr = pg_vec[i];
1693 struct tpacket_hdr *header;
1694 int k;
1695
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001696 for (k = 0; k < po->frames_per_block; k++) {
1697 header = (struct tpacket_hdr *) ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 header->tp_status = TP_STATUS_KERNEL;
1699 ptr += req->tp_frame_size;
1700 }
1701 }
1702 /* Done */
1703 } else {
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001704 if (unlikely(req->tp_frame_nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return -EINVAL;
1706 }
1707
1708 lock_sock(sk);
1709
1710 /* Detach socket from network */
1711 spin_lock(&po->bind_lock);
1712 was_running = po->running;
1713 num = po->num;
1714 if (was_running) {
1715 __dev_remove_pack(&po->prot_hook);
1716 po->num = 0;
1717 po->running = 0;
1718 __sock_put(sk);
1719 }
1720 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 synchronize_net();
1723
1724 err = -EBUSY;
1725 if (closing || atomic_read(&po->mapped) == 0) {
1726 err = 0;
1727#define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
1728
1729 spin_lock_bh(&sk->sk_receive_queue.lock);
1730 pg_vec = XC(po->pg_vec, pg_vec);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001731 po->frame_max = (req->tp_frame_nr - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 po->head = 0;
1733 po->frame_size = req->tp_frame_size;
1734 spin_unlock_bh(&sk->sk_receive_queue.lock);
1735
1736 order = XC(po->pg_vec_order, order);
1737 req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr);
1738
1739 po->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
1740 po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv;
1741 skb_queue_purge(&sk->sk_receive_queue);
1742#undef XC
1743 if (atomic_read(&po->mapped))
1744 printk(KERN_DEBUG "packet_mmap: vma is busy: %d\n", atomic_read(&po->mapped));
1745 }
1746
1747 spin_lock(&po->bind_lock);
1748 if (was_running && !po->running) {
1749 sock_hold(sk);
1750 po->running = 1;
1751 po->num = num;
1752 dev_add_pack(&po->prot_hook);
1753 }
1754 spin_unlock(&po->bind_lock);
1755
1756 release_sock(sk);
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (pg_vec)
1759 free_pg_vec(pg_vec, order, req->tp_block_nr);
1760out:
1761 return err;
1762}
1763
1764static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
1765{
1766 struct sock *sk = sock->sk;
1767 struct packet_sock *po = pkt_sk(sk);
1768 unsigned long size;
1769 unsigned long start;
1770 int err = -EINVAL;
1771 int i;
1772
1773 if (vma->vm_pgoff)
1774 return -EINVAL;
1775
1776 size = vma->vm_end - vma->vm_start;
1777
1778 lock_sock(sk);
1779 if (po->pg_vec == NULL)
1780 goto out;
1781 if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE)
1782 goto out;
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 start = vma->vm_start;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001785 for (i = 0; i < po->pg_vec_len; i++) {
1786 struct page *page = virt_to_page(po->pg_vec[i]);
1787 int pg_num;
1788
1789 for (pg_num = 0; pg_num < po->pg_vec_pages; pg_num++, page++) {
1790 err = vm_insert_page(vma, start, page);
1791 if (unlikely(err))
1792 goto out;
1793 start += PAGE_SIZE;
1794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08001796 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 vma->vm_ops = &packet_mmap_ops;
1798 err = 0;
1799
1800out:
1801 release_sock(sk);
1802 return err;
1803}
1804#endif
1805
1806
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001807static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 .family = PF_PACKET,
1809 .owner = THIS_MODULE,
1810 .release = packet_release,
1811 .bind = packet_bind_spkt,
1812 .connect = sock_no_connect,
1813 .socketpair = sock_no_socketpair,
1814 .accept = sock_no_accept,
1815 .getname = packet_getname_spkt,
1816 .poll = datagram_poll,
1817 .ioctl = packet_ioctl,
1818 .listen = sock_no_listen,
1819 .shutdown = sock_no_shutdown,
1820 .setsockopt = sock_no_setsockopt,
1821 .getsockopt = sock_no_getsockopt,
1822 .sendmsg = packet_sendmsg_spkt,
1823 .recvmsg = packet_recvmsg,
1824 .mmap = sock_no_mmap,
1825 .sendpage = sock_no_sendpage,
1826};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001828static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 .family = PF_PACKET,
1830 .owner = THIS_MODULE,
1831 .release = packet_release,
1832 .bind = packet_bind,
1833 .connect = sock_no_connect,
1834 .socketpair = sock_no_socketpair,
1835 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001836 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 .poll = packet_poll,
1838 .ioctl = packet_ioctl,
1839 .listen = sock_no_listen,
1840 .shutdown = sock_no_shutdown,
1841 .setsockopt = packet_setsockopt,
1842 .getsockopt = packet_getsockopt,
1843 .sendmsg = packet_sendmsg,
1844 .recvmsg = packet_recvmsg,
1845 .mmap = packet_mmap,
1846 .sendpage = sock_no_sendpage,
1847};
1848
1849static struct net_proto_family packet_family_ops = {
1850 .family = PF_PACKET,
1851 .create = packet_create,
1852 .owner = THIS_MODULE,
1853};
1854
1855static struct notifier_block packet_netdev_notifier = {
1856 .notifier_call =packet_notifier,
1857};
1858
1859#ifdef CONFIG_PROC_FS
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001860static inline struct sock *packet_seq_idx(struct net *net, loff_t off)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 struct sock *s;
1863 struct hlist_node *node;
1864
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001865 sk_for_each(s, node, &net->packet_sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (!off--)
1867 return s;
1868 }
1869 return NULL;
1870}
1871
1872static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
1873{
Denis V. Luneve372c412007-11-19 22:31:54 -08001874 struct net *net = seq_file_net(seq);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001875 read_lock(&net->packet_sklist_lock);
1876 return *pos ? packet_seq_idx(net, *pos - 1) : SEQ_START_TOKEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
1879static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1880{
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001881 struct net *net = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 ++*pos;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001883 return (v == SEQ_START_TOKEN)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001884 ? sk_head(&net->packet_sklist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 : sk_next((struct sock*)v) ;
1886}
1887
1888static void packet_seq_stop(struct seq_file *seq, void *v)
1889{
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001890 struct net *net = seq->private;
1891 read_unlock(&net->packet_sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001894static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
1896 if (v == SEQ_START_TOKEN)
1897 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
1898 else {
1899 struct sock *s = v;
1900 const struct packet_sock *po = pkt_sk(s);
1901
1902 seq_printf(seq,
1903 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
1904 s,
1905 atomic_read(&s->sk_refcnt),
1906 s->sk_type,
1907 ntohs(po->num),
1908 po->ifindex,
1909 po->running,
1910 atomic_read(&s->sk_rmem_alloc),
1911 sock_i_uid(s),
1912 sock_i_ino(s) );
1913 }
1914
1915 return 0;
1916}
1917
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001918static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 .start = packet_seq_start,
1920 .next = packet_seq_next,
1921 .stop = packet_seq_stop,
1922 .show = packet_seq_show,
1923};
1924
1925static int packet_seq_open(struct inode *inode, struct file *file)
1926{
Denis V. Luneve372c412007-11-19 22:31:54 -08001927 return seq_open_net(inode, file, &packet_seq_ops,
1928 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
Arjan van de Venda7071d2007-02-12 00:55:36 -08001931static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 .owner = THIS_MODULE,
1933 .open = packet_seq_open,
1934 .read = seq_read,
1935 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08001936 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937};
1938
1939#endif
1940
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001941static int packet_net_init(struct net *net)
1942{
1943 rwlock_init(&net->packet_sklist_lock);
1944 INIT_HLIST_HEAD(&net->packet_sklist);
1945
1946 if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
1947 return -ENOMEM;
1948
1949 return 0;
1950}
1951
1952static void packet_net_exit(struct net *net)
1953{
1954 proc_net_remove(net, "packet");
1955}
1956
1957static struct pernet_operations packet_net_ops = {
1958 .init = packet_net_init,
1959 .exit = packet_net_exit,
1960};
1961
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963static void __exit packet_exit(void)
1964{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001966 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 sock_unregister(PF_PACKET);
1968 proto_unregister(&packet_proto);
1969}
1970
1971static int __init packet_init(void)
1972{
1973 int rc = proto_register(&packet_proto, 0);
1974
1975 if (rc != 0)
1976 goto out;
1977
1978 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001979 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981out:
1982 return rc;
1983}
1984
1985module_init(packet_init);
1986module_exit(packet_exit);
1987MODULE_LICENSE("GPL");
1988MODULE_ALIAS_NETPROTO(PF_PACKET);