blob: 67f6749a0a456b11edb93f39a4d3b439e06e9896 [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 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Alan Cox, <gw4pts@gw4pts.ampr.org>
11 *
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090012 * Fixes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Alan Cox : verify_area() now used correctly
14 * Alan Cox : new skbuff lists, look ma no backlogs!
15 * Alan Cox : tidied skbuff lists.
16 * Alan Cox : Now uses generic datagram routines I
17 * added. Also fixed the peek/read crash
18 * from all old Linux datagram code.
19 * Alan Cox : Uses the improved datagram code.
20 * Alan Cox : Added NULL's for socket options.
21 * Alan Cox : Re-commented the code.
22 * Alan Cox : Use new kernel side addressing
23 * Rob Janssen : Correct MTU usage.
24 * Dave Platt : Counter leaks caused by incorrect
25 * interrupt locking and some slightly
26 * dubious gcc output. Can you read
27 * compiler: it said _VOLATILE_
28 * Richard Kooijman : Timestamp fixes.
29 * Alan Cox : New buffers. Use sk->mac.raw.
30 * Alan Cox : sendmsg/recvmsg support.
31 * Alan Cox : Protocol setting support
32 * Alexey Kuznetsov : Untied from IPv4 stack.
33 * Cyrus Durgin : Fixed kerneld for kmod.
34 * Michal Ostrowski : Module initialization cleanup.
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090035 * Ulises Alonso : Frame number limit removal and
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * packet_set_ring memory leak.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070037 * Eric Biederman : Allow for > 8 byte hardware addresses.
38 * The convention is that longer addresses
39 * will simply extend the hardware address
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090040 * byte arrays at the end of sockaddr_ll
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070041 * and packet_mreq.
Johann Baudy69e3c752009-05-18 22:11:22 -070042 * Johann Baudy : Added TX RING.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 *
44 * This program is free software; you can redistribute it and/or
45 * modify it under the terms of the GNU General Public License
46 * as published by the Free Software Foundation; either version
47 * 2 of the License, or (at your option) any later version.
48 *
49 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/mm.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080053#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/fcntl.h>
55#include <linux/socket.h>
56#include <linux/in.h>
57#include <linux/inet.h>
58#include <linux/netdevice.h>
59#include <linux/if_packet.h>
60#include <linux/wireless.h>
Herbert Xuffbc6112007-02-04 23:33:10 -080061#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Neil Horman0e3125c2010-11-16 10:26:47 -080064#include <linux/vmalloc.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020065#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <net/ip.h>
67#include <net/protocol.h>
68#include <linux/skbuff.h>
69#include <net/sock.h>
70#include <linux/errno.h>
71#include <linux/timer.h>
72#include <asm/system.h>
73#include <asm/uaccess.h>
74#include <asm/ioctls.h>
75#include <asm/page.h>
Al Viroa1f8e7f2006-10-19 16:08:53 -040076#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <asm/io.h>
78#include <linux/proc_fs.h>
79#include <linux/seq_file.h>
80#include <linux/poll.h>
81#include <linux/module.h>
82#include <linux/init.h>
Herbert Xu905db442009-01-30 14:12:06 -080083#include <linux/mutex.h>
Eric Dumazet05423b22009-10-26 18:40:35 -070084#include <linux/if_vlan.h>
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -080085#include <linux/virtio_net.h>
Richard Cochraned85b562010-04-07 22:41:28 +000086#include <linux/errqueue.h>
Scott McMillan614f60f2010-06-02 05:53:56 -070087#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#ifdef CONFIG_INET
90#include <net/inet_common.h>
91#endif
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 Assumptions:
95 - if device has no dev->hard_header routine, it adds and removes ll header
96 inside itself. In this case ll header is invisible outside of device,
97 but higher levels still should reserve dev->hard_header_len.
98 Some devices are enough clever to reallocate skb, when header
99 will not fit to reserved space (tunnel), another ones are silly
100 (PPP).
101 - packet socket receives packets with pulled ll header,
102 so that SOCK_RAW should push it back.
103
104On receive:
105-----------
106
107Incoming, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700108 mac_header -> ll header
109 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111Outgoing, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700112 mac_header -> ll header
113 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115Incoming, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700116 mac_header -> UNKNOWN position. It is very likely, that it points to ll
117 header. PPP makes it, that is wrong, because introduce
YOSHIFUJI Hideakidb0c58f2007-07-19 10:44:35 +0900118 assymetry between rx and tx paths.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700119 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121Outgoing, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700122 mac_header -> data. ll header is still not built!
123 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125Resume
126 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
127
128
129On transmit:
130------------
131
132dev->hard_header != NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700133 mac_header -> ll header
134 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136dev->hard_header == NULL (ll header is added by device, we cannot control it)
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700137 mac_header -> data
138 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 We should set nh.raw on output to correct posistion,
141 packet classifier depends on it.
142 */
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Private packet socket structures. */
145
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000146struct packet_mclist {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct packet_mclist *next;
148 int ifindex;
149 int count;
150 unsigned short type;
151 unsigned short alen;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700152 unsigned char addr[MAX_ADDR_LEN];
153};
154/* identical to struct packet_mreq except it has
155 * a longer address field.
156 */
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000157struct packet_mreq_max {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700158 int mr_ifindex;
159 unsigned short mr_type;
160 unsigned short mr_alen;
161 unsigned char mr_address[MAX_ADDR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
David S. Millera2efcfa2007-05-29 13:12:50 -0700163
Johann Baudy69e3c752009-05-18 22:11:22 -0700164static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
165 int closing, int tx_ring);
166
Neil Horman0e3125c2010-11-16 10:26:47 -0800167struct pgv {
168 char *buffer;
Neil Horman0e3125c2010-11-16 10:26:47 -0800169};
170
Johann Baudy69e3c752009-05-18 22:11:22 -0700171struct packet_ring_buffer {
Neil Horman0e3125c2010-11-16 10:26:47 -0800172 struct pgv *pg_vec;
Johann Baudy69e3c752009-05-18 22:11:22 -0700173 unsigned int head;
174 unsigned int frames_per_block;
175 unsigned int frame_size;
176 unsigned int frame_max;
177
178 unsigned int pg_vec_order;
179 unsigned int pg_vec_pages;
180 unsigned int pg_vec_len;
181
182 atomic_t pending;
183};
184
185struct packet_sock;
186static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188static void packet_flush_mclist(struct sock *sk);
189
190struct packet_sock {
191 /* struct sock has to be the first member of packet_sock */
192 struct sock sk;
193 struct tpacket_stats stats;
Johann Baudy69e3c752009-05-18 22:11:22 -0700194 struct packet_ring_buffer rx_ring;
195 struct packet_ring_buffer tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 int copy_thresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 spinlock_t bind_lock;
Herbert Xu905db442009-01-30 14:12:06 -0800198 struct mutex pg_vec_lock;
Herbert Xu8dc41942007-02-04 23:31:32 -0800199 unsigned int running:1, /* prot_hook is attached*/
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700200 auxdata:1,
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -0800201 origdev:1,
202 has_vnet_hdr:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int ifindex; /* bound device */
Al Viro0e11c912006-11-08 00:26:29 -0800204 __be16 num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct packet_mclist *mclist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 atomic_t mapped;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700207 enum tpacket_versions tp_version;
208 unsigned int tp_hdrlen;
Patrick McHardy89133362008-07-18 18:05:19 -0700209 unsigned int tp_reserve;
Johann Baudy69e3c752009-05-18 22:11:22 -0700210 unsigned int tp_loss:1;
Scott McMillan614f60f2010-06-02 05:53:56 -0700211 unsigned int tp_tstamp;
Eric Dumazet94b05952009-10-16 04:02:20 +0000212 struct packet_type prot_hook ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
Herbert Xuffbc6112007-02-04 23:33:10 -0800215struct packet_skb_cb {
216 unsigned int origlen;
217 union {
218 struct sockaddr_pkt pkt;
219 struct sockaddr_ll ll;
220 } sa;
221};
222
223#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800224
Changli Gaof6dafa92010-12-07 04:26:16 +0000225static inline __pure struct page *pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000226{
227 if (is_vmalloc_addr(addr))
228 return vmalloc_to_page(addr);
229 return virt_to_page(addr);
230}
231
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700232static void __packet_set_status(struct packet_sock *po, void *frame, int status)
233{
234 union {
235 struct tpacket_hdr *h1;
236 struct tpacket2_hdr *h2;
237 void *raw;
238 } h;
239
240 h.raw = frame;
241 switch (po->tp_version) {
242 case TPACKET_V1:
243 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000244 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700245 break;
246 case TPACKET_V2:
247 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000248 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700249 break;
Johann Baudy69e3c752009-05-18 22:11:22 -0700250 default:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000251 pr_err("TPACKET version not supported\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700252 BUG();
253 }
254
255 smp_wmb();
256}
257
258static int __packet_get_status(struct packet_sock *po, void *frame)
259{
260 union {
261 struct tpacket_hdr *h1;
262 struct tpacket2_hdr *h2;
263 void *raw;
264 } h;
265
266 smp_rmb();
267
268 h.raw = frame;
269 switch (po->tp_version) {
270 case TPACKET_V1:
Changli Gao0af55bb2010-12-01 02:52:20 +0000271 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700272 return h.h1->tp_status;
273 case TPACKET_V2:
Changli Gao0af55bb2010-12-01 02:52:20 +0000274 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700275 return h.h2->tp_status;
276 default:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000277 pr_err("TPACKET version not supported\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700278 BUG();
279 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
Johann Baudy69e3c752009-05-18 22:11:22 -0700282
283static void *packet_lookup_frame(struct packet_sock *po,
284 struct packet_ring_buffer *rb,
285 unsigned int position,
286 int status)
287{
288 unsigned int pg_vec_pos, frame_offset;
289 union {
290 struct tpacket_hdr *h1;
291 struct tpacket2_hdr *h2;
292 void *raw;
293 } h;
294
295 pg_vec_pos = position / rb->frames_per_block;
296 frame_offset = position % rb->frames_per_block;
297
Neil Horman0e3125c2010-11-16 10:26:47 -0800298 h.raw = rb->pg_vec[pg_vec_pos].buffer +
299 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700300
301 if (status != __packet_get_status(po, h.raw))
302 return NULL;
303
304 return h.raw;
305}
306
307static inline void *packet_current_frame(struct packet_sock *po,
308 struct packet_ring_buffer *rb,
309 int status)
310{
311 return packet_lookup_frame(po, rb, rb->head, status);
312}
313
314static inline void *packet_previous_frame(struct packet_sock *po,
315 struct packet_ring_buffer *rb,
316 int status)
317{
318 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
319 return packet_lookup_frame(po, rb, previous, status);
320}
321
322static inline void packet_increment_head(struct packet_ring_buffer *buff)
323{
324 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static inline struct packet_sock *pkt_sk(struct sock *sk)
328{
329 return (struct packet_sock *)sk;
330}
331
332static void packet_sock_destruct(struct sock *sk)
333{
Richard Cochraned85b562010-04-07 22:41:28 +0000334 skb_queue_purge(&sk->sk_error_queue);
335
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700336 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
337 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000340 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return;
342 }
343
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -0800344 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800348static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800350static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000352static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
353 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct sock *sk;
356 struct sockaddr_pkt *spkt;
357
358 /*
359 * When we registered the protocol we saved the socket in the data
360 * field for just this event.
361 */
362
363 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /*
366 * Yank back the headers [hope the device set this
367 * right or kerboom...]
368 *
369 * Incoming packets have ll header pulled,
370 * push it back.
371 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700372 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * so that this procedure is noop.
374 */
375
376 if (skb->pkt_type == PACKET_LOOPBACK)
377 goto out;
378
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800379 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800380 goto out;
381
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000382 skb = skb_share_check(skb, GFP_ATOMIC);
383 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 goto oom;
385
386 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +0000387 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Phil Oester84531c22005-07-12 11:57:52 -0700389 /* drop conntrack reference */
390 nf_reset(skb);
391
Herbert Xuffbc6112007-02-04 23:33:10 -0800392 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700394 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /*
397 * The SOCK_PACKET socket receives _all_ frames.
398 */
399
400 spkt->spkt_family = dev->type;
401 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
402 spkt->spkt_protocol = skb->protocol;
403
404 /*
405 * Charge the memory to the socket. This is done specifically
406 * to prevent sockets using all the memory up.
407 */
408
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000409 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411
412out:
413 kfree_skb(skb);
414oom:
415 return 0;
416}
417
418
419/*
420 * Output a raw packet to a device layer. This bypasses all the other
421 * protocol layers and you must therefore supply it with a complete frame
422 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
425 struct msghdr *msg, size_t len)
426{
427 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000428 struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000429 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct net_device *dev;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000431 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int err;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900435 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
437
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000438 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000440 return -EINVAL;
441 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
442 proto = saddr->spkt_protocol;
443 } else
444 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900447 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
449
450 saddr->spkt_device[13] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000451retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100452 rcu_read_lock();
453 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 err = -ENODEV;
455 if (dev == NULL)
456 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900457
David S. Millerd5e76b02007-01-25 19:30:36 -0800458 err = -ENETDOWN;
459 if (!(dev->flags & IFF_UP))
460 goto out_unlock;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000463 * You may not queue a frame bigger than the mtu. This is the lowest level
464 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 err = -EMSGSIZE;
Ben Greear57f89bf2011-02-11 09:35:18 +0000468 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto out_unlock;
470
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000471 if (!skb) {
472 size_t reserved = LL_RESERVED_SPACE(dev);
473 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000475 rcu_read_unlock();
476 skb = sock_wmalloc(sk, len + reserved, 0, GFP_KERNEL);
477 if (skb == NULL)
478 return -ENOBUFS;
479 /* FIXME: Save some space for broken drivers that write a hard
480 * header at transmission time by themselves. PPP is the notable
481 * one here. This should really be fixed at the driver level.
482 */
483 skb_reserve(skb, reserved);
484 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900485
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000486 /* Try to align data part correctly */
487 if (hhlen) {
488 skb->data -= hhlen;
489 skb->tail -= hhlen;
490 if (len < hhlen)
491 skb_reset_network_header(skb);
492 }
493 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
494 if (err)
495 goto out_free;
496 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
Ben Greear57f89bf2011-02-11 09:35:18 +0000499 if (len > (dev->mtu + dev->hard_header_len)) {
500 /* Earlier code assumed this would be a VLAN pkt,
501 * double-check this now that we have the actual
502 * packet in hand.
503 */
504 struct ethhdr *ehdr;
505 skb_reset_mac_header(skb);
506 ehdr = eth_hdr(skb);
507 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
508 err = -EMSGSIZE;
509 goto out_unlock;
510 }
511 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 skb->protocol = proto;
514 skb->dev = dev;
515 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +0000516 skb->mark = sk->sk_mark;
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000517 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +0000518 if (err < 0)
519 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +0100522 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000523 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100526 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000527out_free:
528 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return err;
530}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Eric Dumazet62ab0812010-12-06 20:50:09 +0000532static inline unsigned int run_filter(const struct sk_buff *skb,
533 const struct sock *sk,
David S. Millerdbcb5852007-01-24 15:21:02 -0800534 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct sk_filter *filter;
537
Eric Dumazet80f8f102011-01-18 07:46:52 +0000538 rcu_read_lock();
539 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -0800540 if (filter != NULL)
Eric Dumazet0a148422011-04-20 09:27:32 +0000541 res = SK_RUN_FILTER(filter, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +0000542 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
David S. Millerdbcb5852007-01-24 15:21:02 -0800544 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547/*
Eric Dumazet62ab0812010-12-06 20:50:09 +0000548 * This function makes lazy skb cloning in hope that most of packets
549 * are discarded by BPF.
550 *
551 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
552 * and skb->cb are mangled. It works because (and until) packets
553 * falling here are owned by current CPU. Output packets are cloned
554 * by dev_queue_xmit_nit(), input packets are processed by net_bh
555 * sequencially, so that if we return skb to original state on exit,
556 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
558
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000559static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
560 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct sock *sk;
563 struct sockaddr_ll *sll;
564 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000565 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800567 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 if (skb->pkt_type == PACKET_LOOPBACK)
570 goto drop;
571
572 sk = pt->af_packet_priv;
573 po = pkt_sk(sk);
574
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800575 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800576 goto drop;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 skb->dev = dev;
579
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700580 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +0000582 * exported to higher levels.
583 *
584 * Otherwise, the device hides details of its frame
585 * structure, so that corresponding packet head is
586 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
588 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700589 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 else if (skb->pkt_type == PACKET_OUTGOING) {
591 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300592 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 }
595
596 snaplen = skb->len;
597
David S. Millerdbcb5852007-01-24 15:21:02 -0800598 res = run_filter(skb, sk, snaplen);
599 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700600 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800601 if (snaplen > res)
602 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
605 (unsigned)sk->sk_rcvbuf)
606 goto drop_n_acct;
607
608 if (skb_shared(skb)) {
609 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
610 if (nskb == NULL)
611 goto drop_n_acct;
612
613 if (skb_head != skb->data) {
614 skb->data = skb_head;
615 skb->len = skb_len;
616 }
617 kfree_skb(skb);
618 skb = nskb;
619 }
620
Herbert Xuffbc6112007-02-04 23:33:10 -0800621 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
622 sizeof(skb->cb));
623
624 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 sll->sll_family = AF_PACKET;
626 sll->sll_hatype = dev->type;
627 sll->sll_protocol = skb->protocol;
628 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800629 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700630 sll->sll_ifindex = orig_dev->ifindex;
631 else
632 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700634 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Herbert Xuffbc6112007-02-04 23:33:10 -0800636 PACKET_SKB_CB(skb)->origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -0800637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (pskb_trim(skb, snaplen))
639 goto drop_n_acct;
640
641 skb_set_owner_r(skb, sk);
642 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +0000643 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Phil Oester84531c22005-07-12 11:57:52 -0700645 /* drop conntrack reference */
646 nf_reset(skb);
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 spin_lock(&sk->sk_receive_queue.lock);
649 po->stats.tp_packets++;
Neil Horman3b885782009-10-12 13:26:31 -0700650 skb->dropcount = atomic_read(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 __skb_queue_tail(&sk->sk_receive_queue, skb);
652 spin_unlock(&sk->sk_receive_queue.lock);
653 sk->sk_data_ready(sk, skb->len);
654 return 0;
655
656drop_n_acct:
Neil Horman3b885782009-10-12 13:26:31 -0700657 po->stats.tp_drops = atomic_inc_return(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659drop_n_restore:
660 if (skb_head != skb->data && skb_shared(skb)) {
661 skb->data = skb_head;
662 skb->len = skb_len;
663 }
664drop:
Neil Hormanead2ceb2009-03-11 09:49:55 +0000665 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667}
668
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000669static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
670 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 struct sock *sk;
673 struct packet_sock *po;
674 struct sockaddr_ll *sll;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700675 union {
676 struct tpacket_hdr *h1;
677 struct tpacket2_hdr *h2;
678 void *raw;
679 } h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000680 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800682 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700684 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct sk_buff *copy_skb = NULL;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700686 struct timeval tv;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700687 struct timespec ts;
Scott McMillan614f60f2010-06-02 05:53:56 -0700688 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (skb->pkt_type == PACKET_LOOPBACK)
691 goto drop;
692
693 sk = pt->af_packet_priv;
694 po = pkt_sk(sk);
695
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800696 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800697 goto drop;
698
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700699 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700701 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 else if (skb->pkt_type == PACKET_OUTGOING) {
703 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300704 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 }
707
Herbert Xu8dc41942007-02-04 23:31:32 -0800708 if (skb->ip_summed == CHECKSUM_PARTIAL)
709 status |= TP_STATUS_CSUMNOTREADY;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 snaplen = skb->len;
712
David S. Millerdbcb5852007-01-24 15:21:02 -0800713 res = run_filter(skb, sk, snaplen);
714 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700715 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800716 if (snaplen > res)
717 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy89133362008-07-18 18:05:19 -0700720 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
721 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 } else {
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300723 unsigned maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700724 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy89133362008-07-18 18:05:19 -0700725 (maclen < 16 ? 16 : maclen)) +
726 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 macoff = netoff - maclen;
728 }
729
Johann Baudy69e3c752009-05-18 22:11:22 -0700730 if (macoff + snaplen > po->rx_ring.frame_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (po->copy_thresh &&
732 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
733 (unsigned)sk->sk_rcvbuf) {
734 if (skb_shared(skb)) {
735 copy_skb = skb_clone(skb, GFP_ATOMIC);
736 } else {
737 copy_skb = skb_get(skb);
738 skb_head = skb->data;
739 }
740 if (copy_skb)
741 skb_set_owner_r(copy_skb, sk);
742 }
Johann Baudy69e3c752009-05-18 22:11:22 -0700743 snaplen = po->rx_ring.frame_size - macoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if ((int)snaplen < 0)
745 snaplen = 0;
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 spin_lock(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -0700749 h.raw = packet_current_frame(po, &po->rx_ring, TP_STATUS_KERNEL);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700750 if (!h.raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 goto ring_is_full;
Johann Baudy69e3c752009-05-18 22:11:22 -0700752 packet_increment_head(&po->rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 po->stats.tp_packets++;
754 if (copy_skb) {
755 status |= TP_STATUS_COPY;
756 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
757 }
758 if (!po->stats.tp_drops)
759 status &= ~TP_STATUS_LOSING;
760 spin_unlock(&sk->sk_receive_queue.lock);
761
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700762 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700764 switch (po->tp_version) {
765 case TPACKET_V1:
766 h.h1->tp_len = skb->len;
767 h.h1->tp_snaplen = snaplen;
768 h.h1->tp_mac = macoff;
769 h.h1->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -0700770 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
771 && shhwtstamps->syststamp.tv64)
772 tv = ktime_to_timeval(shhwtstamps->syststamp);
773 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
774 && shhwtstamps->hwtstamp.tv64)
775 tv = ktime_to_timeval(shhwtstamps->hwtstamp);
776 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700777 tv = ktime_to_timeval(skb->tstamp);
778 else
779 do_gettimeofday(&tv);
780 h.h1->tp_sec = tv.tv_sec;
781 h.h1->tp_usec = tv.tv_usec;
782 hdrlen = sizeof(*h.h1);
783 break;
784 case TPACKET_V2:
785 h.h2->tp_len = skb->len;
786 h.h2->tp_snaplen = snaplen;
787 h.h2->tp_mac = macoff;
788 h.h2->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -0700789 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
790 && shhwtstamps->syststamp.tv64)
791 ts = ktime_to_timespec(shhwtstamps->syststamp);
792 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
793 && shhwtstamps->hwtstamp.tv64)
794 ts = ktime_to_timespec(shhwtstamps->hwtstamp);
795 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700796 ts = ktime_to_timespec(skb->tstamp);
797 else
798 getnstimeofday(&ts);
799 h.h2->tp_sec = ts.tv_sec;
800 h.h2->tp_nsec = ts.tv_nsec;
Ben Greeara3bcc232011-06-01 06:49:10 +0000801 if (vlan_tx_tag_present(skb)) {
802 h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
803 status |= TP_STATUS_VLAN_VALID;
804 } else {
805 h.h2->tp_vlan_tci = 0;
806 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700807 hdrlen = sizeof(*h.h2);
808 break;
809 default:
810 BUG();
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700813 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700814 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 sll->sll_family = AF_PACKET;
816 sll->sll_hatype = dev->type;
817 sll->sll_protocol = skb->protocol;
818 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800819 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700820 sll->sll_ifindex = orig_dev->ifindex;
821 else
822 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700824 __packet_set_status(po, h.raw, status);
Ralf Baechlee16aa202006-12-07 00:11:33 -0800825 smp_mb();
Changli Gaof6dafa92010-12-07 04:26:16 +0000826#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 {
Changli Gao0af55bb2010-12-01 02:52:20 +0000828 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Changli Gao0af55bb2010-12-01 02:52:20 +0000830 end = (u8 *)PAGE_ALIGN((unsigned long)h.raw + macoff + snaplen);
831 for (start = h.raw; start < end; start += PAGE_SIZE)
832 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
Changli Gaof6dafa92010-12-07 04:26:16 +0000834#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 sk->sk_data_ready(sk, 0);
837
838drop_n_restore:
839 if (skb_head != skb->data && skb_shared(skb)) {
840 skb->data = skb_head;
841 skb->len = skb_len;
842 }
843drop:
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900844 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return 0;
846
847ring_is_full:
848 po->stats.tp_drops++;
849 spin_unlock(&sk->sk_receive_queue.lock);
850
851 sk->sk_data_ready(sk, 0);
Wei Yongjunacb5d752009-02-25 00:36:42 +0000852 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 goto drop_n_restore;
854}
855
Johann Baudy69e3c752009-05-18 22:11:22 -0700856static void tpacket_destruct_skb(struct sk_buff *skb)
857{
858 struct packet_sock *po = pkt_sk(skb->sk);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000859 void *ph;
Johann Baudy69e3c752009-05-18 22:11:22 -0700860
861 BUG_ON(skb == NULL);
862
863 if (likely(po->tx_ring.pg_vec)) {
864 ph = skb_shinfo(skb)->destructor_arg;
865 BUG_ON(__packet_get_status(po, ph) != TP_STATUS_SENDING);
866 BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
867 atomic_dec(&po->tx_ring.pending);
868 __packet_set_status(po, ph, TP_STATUS_AVAILABLE);
869 }
870
871 sock_wfree(skb);
872}
873
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000874static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
875 void *frame, struct net_device *dev, int size_max,
876 __be16 proto, unsigned char *addr)
Johann Baudy69e3c752009-05-18 22:11:22 -0700877{
878 union {
879 struct tpacket_hdr *h1;
880 struct tpacket2_hdr *h2;
881 void *raw;
882 } ph;
883 int to_write, offset, len, tp_len, nr_frags, len_max;
884 struct socket *sock = po->sk.sk_socket;
885 struct page *page;
886 void *data;
887 int err;
888
889 ph.raw = frame;
890
891 skb->protocol = proto;
892 skb->dev = dev;
893 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +0000894 skb->mark = po->sk.sk_mark;
Johann Baudy69e3c752009-05-18 22:11:22 -0700895 skb_shinfo(skb)->destructor_arg = ph.raw;
896
897 switch (po->tp_version) {
898 case TPACKET_V2:
899 tp_len = ph.h2->tp_len;
900 break;
901 default:
902 tp_len = ph.h1->tp_len;
903 break;
904 }
905 if (unlikely(tp_len > size_max)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000906 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
Johann Baudy69e3c752009-05-18 22:11:22 -0700907 return -EMSGSIZE;
908 }
909
910 skb_reserve(skb, LL_RESERVED_SPACE(dev));
911 skb_reset_network_header(skb);
912
913 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
914 to_write = tp_len;
915
916 if (sock->type == SOCK_DGRAM) {
917 err = dev_hard_header(skb, dev, ntohs(proto), addr,
918 NULL, tp_len);
919 if (unlikely(err < 0))
920 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000921 } else if (dev->hard_header_len) {
Johann Baudy69e3c752009-05-18 22:11:22 -0700922 /* net device doesn't like empty head */
923 if (unlikely(tp_len <= dev->hard_header_len)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000924 pr_err("packet size is too short (%d < %d)\n",
925 tp_len, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -0700926 return -EINVAL;
927 }
928
929 skb_push(skb, dev->hard_header_len);
930 err = skb_store_bits(skb, 0, data,
931 dev->hard_header_len);
932 if (unlikely(err))
933 return err;
934
935 data += dev->hard_header_len;
936 to_write -= dev->hard_header_len;
937 }
938
939 err = -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -0700940 offset = offset_in_page(data);
941 len_max = PAGE_SIZE - offset;
942 len = ((to_write > len_max) ? len_max : to_write);
943
944 skb->data_len = to_write;
945 skb->len += to_write;
946 skb->truesize += to_write;
947 atomic_add(to_write, &po->sk.sk_wmem_alloc);
948
949 while (likely(to_write)) {
950 nr_frags = skb_shinfo(skb)->nr_frags;
951
952 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000953 pr_err("Packet exceed the number of skb frags(%lu)\n",
954 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -0700955 return -EFAULT;
956 }
957
Changli Gao0af55bb2010-12-01 02:52:20 +0000958 page = pgv_to_page(data);
959 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -0700960 flush_dcache_page(page);
961 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +0000962 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -0700963 to_write -= len;
964 offset = 0;
965 len_max = PAGE_SIZE;
966 len = ((to_write > len_max) ? len_max : to_write);
967 }
968
969 return tp_len;
970}
971
972static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
973{
Johann Baudy69e3c752009-05-18 22:11:22 -0700974 struct sk_buff *skb;
975 struct net_device *dev;
976 __be16 proto;
Ben Greear827d9782011-06-01 07:18:53 +0000977 bool need_rls_dev = false;
978 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000979 void *ph;
980 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Johann Baudy69e3c752009-05-18 22:11:22 -0700981 int tp_len, size_max;
982 unsigned char *addr;
983 int len_sum = 0;
984 int status = 0;
985
Johann Baudy69e3c752009-05-18 22:11:22 -0700986 mutex_lock(&po->pg_vec_lock);
987
988 err = -EBUSY;
989 if (saddr == NULL) {
Ben Greear827d9782011-06-01 07:18:53 +0000990 dev = po->prot_hook.dev;
Johann Baudy69e3c752009-05-18 22:11:22 -0700991 proto = po->num;
992 addr = NULL;
993 } else {
994 err = -EINVAL;
995 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
996 goto out;
997 if (msg->msg_namelen < (saddr->sll_halen
998 + offsetof(struct sockaddr_ll,
999 sll_addr)))
1000 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07001001 proto = saddr->sll_protocol;
1002 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00001003 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
1004 need_rls_dev = true;
Johann Baudy69e3c752009-05-18 22:11:22 -07001005 }
1006
Johann Baudy69e3c752009-05-18 22:11:22 -07001007 err = -ENXIO;
1008 if (unlikely(dev == NULL))
1009 goto out;
1010
1011 reserve = dev->hard_header_len;
1012
1013 err = -ENETDOWN;
1014 if (unlikely(!(dev->flags & IFF_UP)))
1015 goto out_put;
1016
1017 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07001018 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07001019
1020 if (size_max > dev->mtu + reserve)
1021 size_max = dev->mtu + reserve;
1022
1023 do {
1024 ph = packet_current_frame(po, &po->tx_ring,
1025 TP_STATUS_SEND_REQUEST);
1026
1027 if (unlikely(ph == NULL)) {
1028 schedule();
1029 continue;
1030 }
1031
1032 status = TP_STATUS_SEND_REQUEST;
1033 skb = sock_alloc_send_skb(&po->sk,
1034 LL_ALLOCATED_SPACE(dev)
1035 + sizeof(struct sockaddr_ll),
1036 0, &err);
1037
1038 if (unlikely(skb == NULL))
1039 goto out_status;
1040
1041 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
1042 addr);
1043
1044 if (unlikely(tp_len < 0)) {
1045 if (po->tp_loss) {
1046 __packet_set_status(po, ph,
1047 TP_STATUS_AVAILABLE);
1048 packet_increment_head(&po->tx_ring);
1049 kfree_skb(skb);
1050 continue;
1051 } else {
1052 status = TP_STATUS_WRONG_FORMAT;
1053 err = tp_len;
1054 goto out_status;
1055 }
1056 }
1057
1058 skb->destructor = tpacket_destruct_skb;
1059 __packet_set_status(po, ph, TP_STATUS_SENDING);
1060 atomic_inc(&po->tx_ring.pending);
1061
1062 status = TP_STATUS_SEND_REQUEST;
1063 err = dev_queue_xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00001064 if (unlikely(err > 0)) {
1065 err = net_xmit_errno(err);
1066 if (err && __packet_get_status(po, ph) ==
1067 TP_STATUS_AVAILABLE) {
1068 /* skb was destructed already */
1069 skb = NULL;
1070 goto out_status;
1071 }
1072 /*
1073 * skb was dropped but not destructed yet;
1074 * let's treat it like congestion or err < 0
1075 */
1076 err = 0;
1077 }
Johann Baudy69e3c752009-05-18 22:11:22 -07001078 packet_increment_head(&po->tx_ring);
1079 len_sum += tp_len;
Joe Perchesf64f9e72009-11-29 16:55:45 -08001080 } while (likely((ph != NULL) ||
1081 ((!(msg->msg_flags & MSG_DONTWAIT)) &&
1082 (atomic_read(&po->tx_ring.pending))))
1083 );
Johann Baudy69e3c752009-05-18 22:11:22 -07001084
1085 err = len_sum;
1086 goto out_put;
1087
Johann Baudy69e3c752009-05-18 22:11:22 -07001088out_status:
1089 __packet_set_status(po, ph, status);
1090 kfree_skb(skb);
1091out_put:
Ben Greear827d9782011-06-01 07:18:53 +00001092 if (need_rls_dev)
1093 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07001094out:
1095 mutex_unlock(&po->pg_vec_lock);
1096 return err;
1097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001099static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
1100 size_t reserve, size_t len,
1101 size_t linear, int noblock,
1102 int *err)
1103{
1104 struct sk_buff *skb;
1105
1106 /* Under a page? Don't bother with paged skb. */
1107 if (prepad + len < PAGE_SIZE || !linear)
1108 linear = len;
1109
1110 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
1111 err);
1112 if (!skb)
1113 return NULL;
1114
1115 skb_reserve(skb, reserve);
1116 skb_put(skb, linear);
1117 skb->data_len = len - linear;
1118 skb->len += len - linear;
1119
1120 return skb;
1121}
1122
Johann Baudy69e3c752009-05-18 22:11:22 -07001123static int packet_snd(struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 struct msghdr *msg, size_t len)
1125{
1126 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001127 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct sk_buff *skb;
1129 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08001130 __be16 proto;
Ben Greear827d9782011-06-01 07:18:53 +00001131 bool need_rls_dev = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 unsigned char *addr;
Ben Greear827d9782011-06-01 07:18:53 +00001133 int err, reserve = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001134 struct virtio_net_hdr vnet_hdr = { 0 };
1135 int offset = 0;
1136 int vnet_hdr_len;
1137 struct packet_sock *po = pkt_sk(sk);
1138 unsigned short gso_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001141 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (saddr == NULL) {
Ben Greear827d9782011-06-01 07:18:53 +00001145 dev = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 proto = po->num;
1147 addr = NULL;
1148 } else {
1149 err = -EINVAL;
1150 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
1151 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001152 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
1153 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 proto = saddr->sll_protocol;
1155 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00001156 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
1157 need_rls_dev = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 err = -ENXIO;
1161 if (dev == NULL)
1162 goto out_unlock;
1163 if (sock->type == SOCK_RAW)
1164 reserve = dev->hard_header_len;
1165
David S. Millerd5e76b02007-01-25 19:30:36 -08001166 err = -ENETDOWN;
1167 if (!(dev->flags & IFF_UP))
1168 goto out_unlock;
1169
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001170 if (po->has_vnet_hdr) {
1171 vnet_hdr_len = sizeof(vnet_hdr);
1172
1173 err = -EINVAL;
1174 if (len < vnet_hdr_len)
1175 goto out_unlock;
1176
1177 len -= vnet_hdr_len;
1178
1179 err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
1180 vnet_hdr_len);
1181 if (err < 0)
1182 goto out_unlock;
1183
1184 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1185 (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
1186 vnet_hdr.hdr_len))
1187 vnet_hdr.hdr_len = vnet_hdr.csum_start +
1188 vnet_hdr.csum_offset + 2;
1189
1190 err = -EINVAL;
1191 if (vnet_hdr.hdr_len > len)
1192 goto out_unlock;
1193
1194 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1195 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1196 case VIRTIO_NET_HDR_GSO_TCPV4:
1197 gso_type = SKB_GSO_TCPV4;
1198 break;
1199 case VIRTIO_NET_HDR_GSO_TCPV6:
1200 gso_type = SKB_GSO_TCPV6;
1201 break;
1202 case VIRTIO_NET_HDR_GSO_UDP:
1203 gso_type = SKB_GSO_UDP;
1204 break;
1205 default:
1206 goto out_unlock;
1207 }
1208
1209 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1210 gso_type |= SKB_GSO_TCP_ECN;
1211
1212 if (vnet_hdr.gso_size == 0)
1213 goto out_unlock;
1214
1215 }
1216 }
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 err = -EMSGSIZE;
Ben Greear57f89bf2011-02-11 09:35:18 +00001219 if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 goto out_unlock;
1221
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001222 err = -ENOBUFS;
1223 skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev),
1224 LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len,
1225 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001226 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 goto out_unlock;
1228
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001229 skb_set_network_header(skb, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001231 err = -EINVAL;
1232 if (sock->type == SOCK_DGRAM &&
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001233 (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001234 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /* Returns -EFAULT on error */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001237 err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (err)
1239 goto out_free;
Oliver Hartkopp2244d072010-08-17 08:59:14 +00001240 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +00001241 if (err < 0)
1242 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Ben Greear57f89bf2011-02-11 09:35:18 +00001244 if (!gso_type && (len > dev->mtu + reserve)) {
1245 /* Earlier code assumed this would be a VLAN pkt,
1246 * double-check this now that we have the actual
1247 * packet in hand.
1248 */
1249 struct ethhdr *ehdr;
1250 skb_reset_mac_header(skb);
1251 ehdr = eth_hdr(skb);
1252 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
1253 err = -EMSGSIZE;
1254 goto out_free;
1255 }
1256 }
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 skb->protocol = proto;
1259 skb->dev = dev;
1260 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001261 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001263 if (po->has_vnet_hdr) {
1264 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1265 if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
1266 vnet_hdr.csum_offset)) {
1267 err = -EINVAL;
1268 goto out_free;
1269 }
1270 }
1271
1272 skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
1273 skb_shinfo(skb)->gso_type = gso_type;
1274
1275 /* Header must be checked, and gso_segs computed. */
1276 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1277 skb_shinfo(skb)->gso_segs = 0;
1278
1279 len += vnet_hdr_len;
1280 }
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /*
1283 * Now send it
1284 */
1285
1286 err = dev_queue_xmit(skb);
1287 if (err > 0 && (err = net_xmit_errno(err)) != 0)
1288 goto out_unlock;
1289
Ben Greear827d9782011-06-01 07:18:53 +00001290 if (need_rls_dev)
1291 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001293 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295out_free:
1296 kfree_skb(skb);
1297out_unlock:
Ben Greear827d9782011-06-01 07:18:53 +00001298 if (dev && need_rls_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 dev_put(dev);
1300out:
1301 return err;
1302}
1303
Johann Baudy69e3c752009-05-18 22:11:22 -07001304static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
1305 struct msghdr *msg, size_t len)
1306{
Johann Baudy69e3c752009-05-18 22:11:22 -07001307 struct sock *sk = sock->sk;
1308 struct packet_sock *po = pkt_sk(sk);
1309 if (po->tx_ring.pg_vec)
1310 return tpacket_snd(po, msg);
1311 else
Johann Baudy69e3c752009-05-18 22:11:22 -07001312 return packet_snd(sock, msg, len);
1313}
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315/*
1316 * Close a PACKET socket. This is fairly simple. We immediately go
1317 * to 'closed' state and remove our protocol entry in the device list.
1318 */
1319
1320static int packet_release(struct socket *sock)
1321{
1322 struct sock *sk = sock->sk;
1323 struct packet_sock *po;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001324 struct net *net;
Johann Baudy69e3c752009-05-18 22:11:22 -07001325 struct tpacket_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 if (!sk)
1328 return 0;
1329
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001330 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 po = pkt_sk(sk);
1332
stephen hemminger808f5112010-02-22 07:57:18 +00001333 spin_lock_bh(&net->packet.sklist_lock);
1334 sk_del_node_init_rcu(sk);
Eric Dumazet920de802008-11-24 00:09:29 -08001335 sock_prot_inuse_add(net, sk->sk_prot, -1);
stephen hemminger808f5112010-02-22 07:57:18 +00001336 spin_unlock_bh(&net->packet.sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
stephen hemminger808f5112010-02-22 07:57:18 +00001338 spin_lock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (po->running) {
1340 /*
stephen hemminger808f5112010-02-22 07:57:18 +00001341 * Remove from protocol table
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 po->running = 0;
1344 po->num = 0;
stephen hemminger808f5112010-02-22 07:57:18 +00001345 __dev_remove_pack(&po->prot_hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 __sock_put(sk);
1347 }
Ben Greear160ff182011-06-01 07:18:52 +00001348 if (po->prot_hook.dev) {
1349 dev_put(po->prot_hook.dev);
1350 po->prot_hook.dev = NULL;
1351 }
stephen hemminger808f5112010-02-22 07:57:18 +00001352 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Johann Baudy69e3c752009-05-18 22:11:22 -07001356 memset(&req, 0, sizeof(req));
1357
1358 if (po->rx_ring.pg_vec)
1359 packet_set_ring(sk, &req, 1, 0);
1360
1361 if (po->tx_ring.pg_vec)
1362 packet_set_ring(sk, &req, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
stephen hemminger808f5112010-02-22 07:57:18 +00001364 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 /*
1366 * Now the socket is dead. No more input will appear.
1367 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 sock_orphan(sk);
1369 sock->sk = NULL;
1370
1371 /* Purge queues */
1372
1373 skb_queue_purge(&sk->sk_receive_queue);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001374 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 sock_put(sk);
1377 return 0;
1378}
1379
1380/*
1381 * Attach a packet hook.
1382 */
1383
Al Viro0e11c912006-11-08 00:26:29 -08001384static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 struct packet_sock *po = pkt_sk(sk);
1387 /*
1388 * Detach an existing hook if present.
1389 */
1390
1391 lock_sock(sk);
1392
1393 spin_lock(&po->bind_lock);
1394 if (po->running) {
1395 __sock_put(sk);
1396 po->running = 0;
1397 po->num = 0;
1398 spin_unlock(&po->bind_lock);
1399 dev_remove_pack(&po->prot_hook);
1400 spin_lock(&po->bind_lock);
1401 }
1402
1403 po->num = protocol;
1404 po->prot_hook.type = protocol;
Ben Greear160ff182011-06-01 07:18:52 +00001405 if (po->prot_hook.dev)
1406 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 po->prot_hook.dev = dev;
1408
1409 po->ifindex = dev ? dev->ifindex : 0;
1410
1411 if (protocol == 0)
1412 goto out_unlock;
1413
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001414 if (!dev || (dev->flags & IFF_UP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 dev_add_pack(&po->prot_hook);
1416 sock_hold(sk);
1417 po->running = 1;
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001418 } else {
1419 sk->sk_err = ENETDOWN;
1420 if (!sock_flag(sk, SOCK_DEAD))
1421 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423
1424out_unlock:
1425 spin_unlock(&po->bind_lock);
1426 release_sock(sk);
1427 return 0;
1428}
1429
1430/*
1431 * Bind a packet socket to a device
1432 */
1433
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001434static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
1435 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001437 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 char name[15];
1439 struct net_device *dev;
1440 int err = -ENODEV;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 /*
1443 * Check legality
1444 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001445
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001446 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001448 strlcpy(name, uaddr->sa_data, sizeof(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001450 dev = dev_get_by_name(sock_net(sk), name);
Ben Greear160ff182011-06-01 07:18:52 +00001451 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return err;
1454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1457{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001458 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
1459 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 struct net_device *dev = NULL;
1461 int err;
1462
1463
1464 /*
1465 * Check legality
1466 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (addr_len < sizeof(struct sockaddr_ll))
1469 return -EINVAL;
1470 if (sll->sll_family != AF_PACKET)
1471 return -EINVAL;
1472
1473 if (sll->sll_ifindex) {
1474 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001475 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (dev == NULL)
1477 goto out;
1478 }
1479 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481out:
1482 return err;
1483}
1484
1485static struct proto packet_proto = {
1486 .name = "PACKET",
1487 .owner = THIS_MODULE,
1488 .obj_size = sizeof(struct packet_sock),
1489};
1490
1491/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001492 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 */
1494
Eric Paris3f378b62009-11-05 22:18:14 -08001495static int packet_create(struct net *net, struct socket *sock, int protocol,
1496 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 struct sock *sk;
1499 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08001500 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 int err;
1502
1503 if (!capable(CAP_NET_RAW))
1504 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07001505 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
1506 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return -ESOCKTNOSUPPORT;
1508
1509 sock->state = SS_UNCONNECTED;
1510
1511 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001512 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (sk == NULL)
1514 goto out;
1515
1516 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (sock->type == SOCK_PACKET)
1518 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 sock_init_data(sock, sk);
1521
1522 po = pkt_sk(sk);
1523 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08001524 po->num = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001527 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 /*
1530 * Attach a protocol block
1531 */
1532
1533 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08001534 mutex_init(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (sock->type == SOCK_PACKET)
1538 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 po->prot_hook.af_packet_priv = sk;
1541
Al Viro0e11c912006-11-08 00:26:29 -08001542 if (proto) {
1543 po->prot_hook.type = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 dev_add_pack(&po->prot_hook);
1545 sock_hold(sk);
1546 po->running = 1;
1547 }
1548
stephen hemminger808f5112010-02-22 07:57:18 +00001549 spin_lock_bh(&net->packet.sklist_lock);
1550 sk_add_node_rcu(sk, &net->packet.sklist);
Eric Dumazet36804532008-11-19 14:25:35 -08001551 sock_prot_inuse_add(net, &packet_proto, 1);
stephen hemminger808f5112010-02-22 07:57:18 +00001552 spin_unlock_bh(&net->packet.sklist_lock);
1553
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555out:
1556 return err;
1557}
1558
Richard Cochraned85b562010-04-07 22:41:28 +00001559static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
1560{
1561 struct sock_exterr_skb *serr;
1562 struct sk_buff *skb, *skb2;
1563 int copied, err;
1564
1565 err = -EAGAIN;
1566 skb = skb_dequeue(&sk->sk_error_queue);
1567 if (skb == NULL)
1568 goto out;
1569
1570 copied = skb->len;
1571 if (copied > len) {
1572 msg->msg_flags |= MSG_TRUNC;
1573 copied = len;
1574 }
1575 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1576 if (err)
1577 goto out_free_skb;
1578
1579 sock_recv_timestamp(msg, sk, skb);
1580
1581 serr = SKB_EXT_ERR(skb);
1582 put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
1583 sizeof(serr->ee), &serr->ee);
1584
1585 msg->msg_flags |= MSG_ERRQUEUE;
1586 err = copied;
1587
1588 /* Reset and regenerate socket error */
1589 spin_lock_bh(&sk->sk_error_queue.lock);
1590 sk->sk_err = 0;
1591 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
1592 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
1593 spin_unlock_bh(&sk->sk_error_queue.lock);
1594 sk->sk_error_report(sk);
1595 } else
1596 spin_unlock_bh(&sk->sk_error_queue.lock);
1597
1598out_free_skb:
1599 kfree_skb(skb);
1600out:
1601 return err;
1602}
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604/*
1605 * Pull a packet from our receive queue and hand it to the user.
1606 * If necessary we block.
1607 */
1608
1609static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1610 struct msghdr *msg, size_t len, int flags)
1611{
1612 struct sock *sk = sock->sk;
1613 struct sk_buff *skb;
1614 int copied, err;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001615 struct sockaddr_ll *sll;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001616 int vnet_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00001619 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto out;
1621
1622#if 0
1623 /* What error should we return now? EUNATTACH? */
1624 if (pkt_sk(sk)->ifindex < 0)
1625 return -ENODEV;
1626#endif
1627
Richard Cochraned85b562010-04-07 22:41:28 +00001628 if (flags & MSG_ERRQUEUE) {
1629 err = packet_recv_error(sk, msg, len);
1630 goto out;
1631 }
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 * Call the generic datagram receiver. This handles all sorts
1635 * of horrible races and re-entrancy so we can forget about it
1636 * in the protocol layers.
1637 *
1638 * Now it will return ENETDOWN, if device have just gone down,
1639 * but then it will block.
1640 */
1641
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001642 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001645 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 * handles the blocking we don't see and worry about blocking
1647 * retries.
1648 */
1649
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001650 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 goto out;
1652
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001653 if (pkt_sk(sk)->has_vnet_hdr) {
1654 struct virtio_net_hdr vnet_hdr = { 0 };
1655
1656 err = -EINVAL;
1657 vnet_hdr_len = sizeof(vnet_hdr);
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001658 if (len < vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001659 goto out_free;
1660
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001661 len -= vnet_hdr_len;
1662
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001663 if (skb_is_gso(skb)) {
1664 struct skb_shared_info *sinfo = skb_shinfo(skb);
1665
1666 /* This is a hint as to how much should be linear. */
1667 vnet_hdr.hdr_len = skb_headlen(skb);
1668 vnet_hdr.gso_size = sinfo->gso_size;
1669 if (sinfo->gso_type & SKB_GSO_TCPV4)
1670 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1671 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1672 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1673 else if (sinfo->gso_type & SKB_GSO_UDP)
1674 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1675 else if (sinfo->gso_type & SKB_GSO_FCOE)
1676 goto out_free;
1677 else
1678 BUG();
1679 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1680 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1681 } else
1682 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1683
1684 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1685 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00001686 vnet_hdr.csum_start = skb_checksum_start_offset(skb);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001687 vnet_hdr.csum_offset = skb->csum_offset;
1688 } /* else everything is zero */
1689
1690 err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
1691 vnet_hdr_len);
1692 if (err < 0)
1693 goto out_free;
1694 }
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 /*
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001697 * If the address length field is there to be filled in, we fill
1698 * it in now.
1699 */
1700
Herbert Xuffbc6112007-02-04 23:33:10 -08001701 sll = &PACKET_SKB_CB(skb)->sa.ll;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001702 if (sock->type == SOCK_PACKET)
1703 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1704 else
1705 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1706
1707 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 * You lose any data beyond the buffer you gave. If it worries a
1709 * user program they can ask the device for its MTU anyway.
1710 */
1711
1712 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001713 if (copied > len) {
1714 copied = len;
1715 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717
1718 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1719 if (err)
1720 goto out_free;
1721
Neil Horman3b885782009-10-12 13:26:31 -07001722 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 if (msg->msg_name)
Herbert Xuffbc6112007-02-04 23:33:10 -08001725 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
1726 msg->msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Herbert Xu8dc41942007-02-04 23:31:32 -08001728 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08001729 struct tpacket_auxdata aux;
1730
1731 aux.tp_status = TP_STATUS_USER;
1732 if (skb->ip_summed == CHECKSUM_PARTIAL)
1733 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
1734 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
1735 aux.tp_snaplen = skb->len;
1736 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001737 aux.tp_net = skb_network_offset(skb);
Ben Greeara3bcc232011-06-01 06:49:10 +00001738 if (vlan_tx_tag_present(skb)) {
1739 aux.tp_vlan_tci = vlan_tx_tag_get(skb);
1740 aux.tp_status |= TP_STATUS_VLAN_VALID;
1741 } else {
1742 aux.tp_vlan_tci = 0;
1743 }
Herbert Xuffbc6112007-02-04 23:33:10 -08001744 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08001745 }
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /*
1748 * Free or return the buffer as appropriate. Again this
1749 * hides all the races and re-entrancy issues from us.
1750 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001751 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753out_free:
1754 skb_free_datagram(sk, skb);
1755out:
1756 return err;
1757}
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1760 int *uaddr_len, int peer)
1761{
1762 struct net_device *dev;
1763 struct sock *sk = sock->sk;
1764
1765 if (peer)
1766 return -EOPNOTSUPP;
1767
1768 uaddr->sa_family = AF_PACKET;
Eric Dumazet654d1f82009-11-02 10:43:32 +01001769 rcu_read_lock();
1770 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
1771 if (dev)
Vasiliy Kulikov67286642010-11-10 12:09:10 -08001772 strncpy(uaddr->sa_data, dev->name, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001773 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 memset(uaddr->sa_data, 0, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001775 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 *uaddr_len = sizeof(*uaddr);
1777
1778 return 0;
1779}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1782 int *uaddr_len, int peer)
1783{
1784 struct net_device *dev;
1785 struct sock *sk = sock->sk;
1786 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001787 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 if (peer)
1790 return -EOPNOTSUPP;
1791
1792 sll->sll_family = AF_PACKET;
1793 sll->sll_ifindex = po->ifindex;
1794 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08001795 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01001796 rcu_read_lock();
1797 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (dev) {
1799 sll->sll_hatype = dev->type;
1800 sll->sll_halen = dev->addr_len;
1801 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 } else {
1803 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1804 sll->sll_halen = 0;
1805 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01001806 rcu_read_unlock();
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001807 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 return 0;
1810}
1811
Wang Chen2aeb0b82008-07-14 20:49:46 -07001812static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
1813 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 switch (i->type) {
1816 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00001817 if (i->alen != dev->addr_len)
1818 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (what > 0)
Jiri Pirko22bedad2010-04-01 21:22:57 +00001820 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 else
Jiri Pirko22bedad2010-04-01 21:22:57 +00001822 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 break;
1824 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07001825 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 break;
1827 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07001828 return dev_set_allmulti(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 break;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001830 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00001831 if (i->alen != dev->addr_len)
1832 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001833 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001834 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001835 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001836 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001837 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001838 default:
1839 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07001841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1845{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001846 for ( ; i; i = i->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (i->ifindex == dev->ifindex)
1848 packet_dev_mc(dev, i, what);
1849 }
1850}
1851
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001852static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 struct packet_sock *po = pkt_sk(sk);
1855 struct packet_mclist *ml, *i;
1856 struct net_device *dev;
1857 int err;
1858
1859 rtnl_lock();
1860
1861 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001862 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (!dev)
1864 goto done;
1865
1866 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00001867 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 goto done;
1869
1870 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001871 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (i == NULL)
1873 goto done;
1874
1875 err = 0;
1876 for (ml = po->mclist; ml; ml = ml->next) {
1877 if (ml->ifindex == mreq->mr_ifindex &&
1878 ml->type == mreq->mr_type &&
1879 ml->alen == mreq->mr_alen &&
1880 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1881 ml->count++;
1882 /* Free the new element ... */
1883 kfree(i);
1884 goto done;
1885 }
1886 }
1887
1888 i->type = mreq->mr_type;
1889 i->ifindex = mreq->mr_ifindex;
1890 i->alen = mreq->mr_alen;
1891 memcpy(i->addr, mreq->mr_address, i->alen);
1892 i->count = 1;
1893 i->next = po->mclist;
1894 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07001895 err = packet_dev_mc(dev, i, 1);
1896 if (err) {
1897 po->mclist = i->next;
1898 kfree(i);
1899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901done:
1902 rtnl_unlock();
1903 return err;
1904}
1905
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001906static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
1908 struct packet_mclist *ml, **mlp;
1909
1910 rtnl_lock();
1911
1912 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
1913 if (ml->ifindex == mreq->mr_ifindex &&
1914 ml->type == mreq->mr_type &&
1915 ml->alen == mreq->mr_alen &&
1916 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1917 if (--ml->count == 0) {
1918 struct net_device *dev;
1919 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00001920 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
1921 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 kfree(ml);
1924 }
1925 rtnl_unlock();
1926 return 0;
1927 }
1928 }
1929 rtnl_unlock();
1930 return -EADDRNOTAVAIL;
1931}
1932
1933static void packet_flush_mclist(struct sock *sk)
1934{
1935 struct packet_sock *po = pkt_sk(sk);
1936 struct packet_mclist *ml;
1937
1938 if (!po->mclist)
1939 return;
1940
1941 rtnl_lock();
1942 while ((ml = po->mclist) != NULL) {
1943 struct net_device *dev;
1944
1945 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00001946 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
1947 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 kfree(ml);
1950 }
1951 rtnl_unlock();
1952}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954static int
David S. Millerb7058842009-09-30 16:12:20 -07001955packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
1957 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08001958 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 int ret;
1960
1961 if (level != SOL_PACKET)
1962 return -ENOPROTOOPT;
1963
Johann Baudy69e3c752009-05-18 22:11:22 -07001964 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001965 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 case PACKET_DROP_MEMBERSHIP:
1967 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001968 struct packet_mreq_max mreq;
1969 int len = optlen;
1970 memset(&mreq, 0, sizeof(mreq));
1971 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001973 if (len > sizeof(mreq))
1974 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001975 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001977 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
1978 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (optname == PACKET_ADD_MEMBERSHIP)
1980 ret = packet_mc_add(sk, &mreq);
1981 else
1982 ret = packet_mc_drop(sk, &mreq);
1983 return ret;
1984 }
David S. Millera2efcfa2007-05-29 13:12:50 -07001985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07001987 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 {
1989 struct tpacket_req req;
1990
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001991 if (optlen < sizeof(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001993 if (pkt_sk(sk)->has_vnet_hdr)
1994 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001995 if (copy_from_user(&req, optval, sizeof(req)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -07001997 return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999 case PACKET_COPY_THRESH:
2000 {
2001 int val;
2002
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002003 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002005 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return -EFAULT;
2007
2008 pkt_sk(sk)->copy_thresh = val;
2009 return 0;
2010 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002011 case PACKET_VERSION:
2012 {
2013 int val;
2014
2015 if (optlen != sizeof(val))
2016 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002017 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002018 return -EBUSY;
2019 if (copy_from_user(&val, optval, sizeof(val)))
2020 return -EFAULT;
2021 switch (val) {
2022 case TPACKET_V1:
2023 case TPACKET_V2:
2024 po->tp_version = val;
2025 return 0;
2026 default:
2027 return -EINVAL;
2028 }
2029 }
Patrick McHardy89133362008-07-18 18:05:19 -07002030 case PACKET_RESERVE:
2031 {
2032 unsigned int val;
2033
2034 if (optlen != sizeof(val))
2035 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002036 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardy89133362008-07-18 18:05:19 -07002037 return -EBUSY;
2038 if (copy_from_user(&val, optval, sizeof(val)))
2039 return -EFAULT;
2040 po->tp_reserve = val;
2041 return 0;
2042 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002043 case PACKET_LOSS:
2044 {
2045 unsigned int val;
2046
2047 if (optlen != sizeof(val))
2048 return -EINVAL;
2049 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2050 return -EBUSY;
2051 if (copy_from_user(&val, optval, sizeof(val)))
2052 return -EFAULT;
2053 po->tp_loss = !!val;
2054 return 0;
2055 }
Herbert Xu8dc41942007-02-04 23:31:32 -08002056 case PACKET_AUXDATA:
2057 {
2058 int val;
2059
2060 if (optlen < sizeof(val))
2061 return -EINVAL;
2062 if (copy_from_user(&val, optval, sizeof(val)))
2063 return -EFAULT;
2064
2065 po->auxdata = !!val;
2066 return 0;
2067 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002068 case PACKET_ORIGDEV:
2069 {
2070 int val;
2071
2072 if (optlen < sizeof(val))
2073 return -EINVAL;
2074 if (copy_from_user(&val, optval, sizeof(val)))
2075 return -EFAULT;
2076
2077 po->origdev = !!val;
2078 return 0;
2079 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002080 case PACKET_VNET_HDR:
2081 {
2082 int val;
2083
2084 if (sock->type != SOCK_RAW)
2085 return -EINVAL;
2086 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2087 return -EBUSY;
2088 if (optlen < sizeof(val))
2089 return -EINVAL;
2090 if (copy_from_user(&val, optval, sizeof(val)))
2091 return -EFAULT;
2092
2093 po->has_vnet_hdr = !!val;
2094 return 0;
2095 }
Scott McMillan614f60f2010-06-02 05:53:56 -07002096 case PACKET_TIMESTAMP:
2097 {
2098 int val;
2099
2100 if (optlen != sizeof(val))
2101 return -EINVAL;
2102 if (copy_from_user(&val, optval, sizeof(val)))
2103 return -EFAULT;
2104
2105 po->tp_tstamp = val;
2106 return 0;
2107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 default:
2109 return -ENOPROTOOPT;
2110 }
2111}
2112
2113static int packet_getsockopt(struct socket *sock, int level, int optname,
2114 char __user *optval, int __user *optlen)
2115{
2116 int len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002117 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 struct sock *sk = sock->sk;
2119 struct packet_sock *po = pkt_sk(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08002120 void *data;
2121 struct tpacket_stats st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
2123 if (level != SOL_PACKET)
2124 return -ENOPROTOOPT;
2125
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002126 if (get_user(len, optlen))
2127 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 if (len < 0)
2130 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002131
Johann Baudy69e3c752009-05-18 22:11:22 -07002132 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (len > sizeof(struct tpacket_stats))
2135 len = sizeof(struct tpacket_stats);
2136 spin_lock_bh(&sk->sk_receive_queue.lock);
2137 st = po->stats;
2138 memset(&po->stats, 0, sizeof(st));
2139 spin_unlock_bh(&sk->sk_receive_queue.lock);
2140 st.tp_packets += st.tp_drops;
2141
Herbert Xu8dc41942007-02-04 23:31:32 -08002142 data = &st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08002144 case PACKET_AUXDATA:
2145 if (len > sizeof(int))
2146 len = sizeof(int);
2147 val = po->auxdata;
2148
2149 data = &val;
2150 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002151 case PACKET_ORIGDEV:
2152 if (len > sizeof(int))
2153 len = sizeof(int);
2154 val = po->origdev;
2155
2156 data = &val;
2157 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002158 case PACKET_VNET_HDR:
2159 if (len > sizeof(int))
2160 len = sizeof(int);
2161 val = po->has_vnet_hdr;
2162
2163 data = &val;
2164 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002165 case PACKET_VERSION:
2166 if (len > sizeof(int))
2167 len = sizeof(int);
2168 val = po->tp_version;
2169 data = &val;
2170 break;
2171 case PACKET_HDRLEN:
2172 if (len > sizeof(int))
2173 len = sizeof(int);
2174 if (copy_from_user(&val, optval, len))
2175 return -EFAULT;
2176 switch (val) {
2177 case TPACKET_V1:
2178 val = sizeof(struct tpacket_hdr);
2179 break;
2180 case TPACKET_V2:
2181 val = sizeof(struct tpacket2_hdr);
2182 break;
2183 default:
2184 return -EINVAL;
2185 }
2186 data = &val;
2187 break;
Patrick McHardy89133362008-07-18 18:05:19 -07002188 case PACKET_RESERVE:
2189 if (len > sizeof(unsigned int))
2190 len = sizeof(unsigned int);
2191 val = po->tp_reserve;
2192 data = &val;
2193 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07002194 case PACKET_LOSS:
2195 if (len > sizeof(unsigned int))
2196 len = sizeof(unsigned int);
2197 val = po->tp_loss;
2198 data = &val;
2199 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07002200 case PACKET_TIMESTAMP:
2201 if (len > sizeof(int))
2202 len = sizeof(int);
2203 val = po->tp_tstamp;
2204 data = &val;
2205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 default:
2207 return -ENOPROTOOPT;
2208 }
2209
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002210 if (put_user(len, optlen))
2211 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08002212 if (copy_to_user(optval, data, len))
2213 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
2217
2218static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
2219{
2220 struct sock *sk;
2221 struct hlist_node *node;
Jason Lunzad930652007-02-20 23:19:54 -08002222 struct net_device *dev = data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002223 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
stephen hemminger808f5112010-02-22 07:57:18 +00002225 rcu_read_lock();
2226 sk_for_each_rcu(sk, node, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 struct packet_sock *po = pkt_sk(sk);
2228
2229 switch (msg) {
2230 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (po->mclist)
2232 packet_dev_mclist(dev, po->mclist, -1);
David S. Millera2efcfa2007-05-29 13:12:50 -07002233 /* fallthrough */
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 case NETDEV_DOWN:
2236 if (dev->ifindex == po->ifindex) {
2237 spin_lock(&po->bind_lock);
2238 if (po->running) {
2239 __dev_remove_pack(&po->prot_hook);
2240 __sock_put(sk);
2241 po->running = 0;
2242 sk->sk_err = ENETDOWN;
2243 if (!sock_flag(sk, SOCK_DEAD))
2244 sk->sk_error_report(sk);
2245 }
2246 if (msg == NETDEV_UNREGISTER) {
2247 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00002248 if (po->prot_hook.dev)
2249 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 po->prot_hook.dev = NULL;
2251 }
2252 spin_unlock(&po->bind_lock);
2253 }
2254 break;
2255 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00002256 if (dev->ifindex == po->ifindex) {
2257 spin_lock(&po->bind_lock);
2258 if (po->num && !po->running) {
2259 dev_add_pack(&po->prot_hook);
2260 sock_hold(sk);
2261 po->running = 1;
2262 }
2263 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 break;
2266 }
2267 }
stephen hemminger808f5112010-02-22 07:57:18 +00002268 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return NOTIFY_DONE;
2270}
2271
2272
2273static int packet_ioctl(struct socket *sock, unsigned int cmd,
2274 unsigned long arg)
2275{
2276 struct sock *sk = sock->sk;
2277
Johann Baudy69e3c752009-05-18 22:11:22 -07002278 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002279 case SIOCOUTQ:
2280 {
2281 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07002282
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002283 return put_user(amount, (int __user *)arg);
2284 }
2285 case SIOCINQ:
2286 {
2287 struct sk_buff *skb;
2288 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002290 spin_lock_bh(&sk->sk_receive_queue.lock);
2291 skb = skb_peek(&sk->sk_receive_queue);
2292 if (skb)
2293 amount = skb->len;
2294 spin_unlock_bh(&sk->sk_receive_queue.lock);
2295 return put_user(amount, (int __user *)arg);
2296 }
2297 case SIOCGSTAMP:
2298 return sock_get_timestamp(sk, (struct timeval __user *)arg);
2299 case SIOCGSTAMPNS:
2300 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002303 case SIOCADDRT:
2304 case SIOCDELRT:
2305 case SIOCDARP:
2306 case SIOCGARP:
2307 case SIOCSARP:
2308 case SIOCGIFADDR:
2309 case SIOCSIFADDR:
2310 case SIOCGIFBRDADDR:
2311 case SIOCSIFBRDADDR:
2312 case SIOCGIFNETMASK:
2313 case SIOCSIFNETMASK:
2314 case SIOCGIFDSTADDR:
2315 case SIOCSIFDSTADDR:
2316 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002317 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318#endif
2319
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002320 default:
2321 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323 return 0;
2324}
2325
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002326static unsigned int packet_poll(struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 poll_table *wait)
2328{
2329 struct sock *sk = sock->sk;
2330 struct packet_sock *po = pkt_sk(sk);
2331 unsigned int mask = datagram_poll(file, sock, wait);
2332
2333 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002334 if (po->rx_ring.pg_vec) {
2335 if (!packet_previous_frame(po, &po->rx_ring, TP_STATUS_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 mask |= POLLIN | POLLRDNORM;
2337 }
2338 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002339 spin_lock_bh(&sk->sk_write_queue.lock);
2340 if (po->tx_ring.pg_vec) {
2341 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
2342 mask |= POLLOUT | POLLWRNORM;
2343 }
2344 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return mask;
2346}
2347
2348
2349/* Dirty? Well, I still did not learn better way to account
2350 * for user mmaps.
2351 */
2352
2353static void packet_mm_open(struct vm_area_struct *vma)
2354{
2355 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002356 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (sk)
2360 atomic_inc(&pkt_sk(sk)->mapped);
2361}
2362
2363static void packet_mm_close(struct vm_area_struct *vma)
2364{
2365 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002366 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (sk)
2370 atomic_dec(&pkt_sk(sk)->mapped);
2371}
2372
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002373static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002374 .open = packet_mm_open,
2375 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376};
2377
Neil Horman0e3125c2010-11-16 10:26:47 -08002378static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
2379 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
2381 int i;
2382
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002383 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002384 if (likely(pg_vec[i].buffer)) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002385 if (is_vmalloc_addr(pg_vec[i].buffer))
Neil Horman0e3125c2010-11-16 10:26:47 -08002386 vfree(pg_vec[i].buffer);
2387 else
2388 free_pages((unsigned long)pg_vec[i].buffer,
2389 order);
2390 pg_vec[i].buffer = NULL;
2391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393 kfree(pg_vec);
2394}
2395
Changli Gaoc56b4d92010-12-01 02:52:57 +00002396static inline char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002397{
Neil Horman0e3125c2010-11-16 10:26:47 -08002398 char *buffer = NULL;
2399 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
2400 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07002401
Neil Horman0e3125c2010-11-16 10:26:47 -08002402 buffer = (char *) __get_free_pages(gfp_flags, order);
2403
2404 if (buffer)
2405 return buffer;
2406
2407 /*
2408 * __get_free_pages failed, fall back to vmalloc
2409 */
Eric Dumazetbbce5a52010-11-20 07:31:54 +00002410 buffer = vzalloc((1 << order) * PAGE_SIZE);
Neil Horman0e3125c2010-11-16 10:26:47 -08002411
2412 if (buffer)
2413 return buffer;
2414
2415 /*
2416 * vmalloc failed, lets dig into swap here
2417 */
Neil Horman0e3125c2010-11-16 10:26:47 -08002418 gfp_flags &= ~__GFP_NORETRY;
2419 buffer = (char *)__get_free_pages(gfp_flags, order);
2420 if (buffer)
2421 return buffer;
2422
2423 /*
2424 * complete and utter failure
2425 */
2426 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002427}
2428
Neil Horman0e3125c2010-11-16 10:26:47 -08002429static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002430{
2431 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08002432 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002433 int i;
2434
Neil Horman0e3125c2010-11-16 10:26:47 -08002435 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002436 if (unlikely(!pg_vec))
2437 goto out;
2438
2439 for (i = 0; i < block_nr; i++) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002440 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08002441 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002442 goto out_free_pgvec;
2443 }
2444
2445out:
2446 return pg_vec;
2447
2448out_free_pgvec:
2449 free_pg_vec(pg_vec, order, block_nr);
2450 pg_vec = NULL;
2451 goto out;
2452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Johann Baudy69e3c752009-05-18 22:11:22 -07002454static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
2455 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
Neil Horman0e3125c2010-11-16 10:26:47 -08002457 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08002459 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002460 struct packet_ring_buffer *rb;
2461 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08002462 __be16 num;
Johann Baudy69e3c752009-05-18 22:11:22 -07002463 int err;
2464
2465 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
2466 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
2467
2468 err = -EBUSY;
2469 if (!closing) {
2470 if (atomic_read(&po->mapped))
2471 goto out;
2472 if (atomic_read(&rb->pending))
2473 goto out;
2474 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if (req->tp_block_nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07002478 err = -EBUSY;
2479 if (unlikely(rb->pg_vec))
2480 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002482 switch (po->tp_version) {
2483 case TPACKET_V1:
2484 po->tp_hdrlen = TPACKET_HDRLEN;
2485 break;
2486 case TPACKET_V2:
2487 po->tp_hdrlen = TPACKET2_HDRLEN;
2488 break;
2489 }
2490
Johann Baudy69e3c752009-05-18 22:11:22 -07002491 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002492 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07002493 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002494 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002495 goto out;
Patrick McHardy89133362008-07-18 18:05:19 -07002496 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
Johann Baudy69e3c752009-05-18 22:11:22 -07002497 po->tp_reserve))
2498 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002499 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002500 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Johann Baudy69e3c752009-05-18 22:11:22 -07002502 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
2503 if (unlikely(rb->frames_per_block <= 0))
2504 goto out;
2505 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
2506 req->tp_frame_nr))
2507 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002510 order = get_order(req->tp_block_size);
2511 pg_vec = alloc_pg_vec(req, order);
2512 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002514 }
2515 /* Done */
2516 else {
2517 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002518 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07002519 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
2521
2522 lock_sock(sk);
2523
2524 /* Detach socket from network */
2525 spin_lock(&po->bind_lock);
2526 was_running = po->running;
2527 num = po->num;
2528 if (was_running) {
2529 __dev_remove_pack(&po->prot_hook);
2530 po->num = 0;
2531 po->running = 0;
2532 __sock_put(sk);
2533 }
2534 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 synchronize_net();
2537
2538 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08002539 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 if (closing || atomic_read(&po->mapped) == 0) {
2541 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002542 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08002543 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07002544 rb->frame_max = (req->tp_frame_nr - 1);
2545 rb->head = 0;
2546 rb->frame_size = req->tp_frame_size;
2547 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Changli Gaoc053fd92010-12-10 16:02:20 -08002549 swap(rb->pg_vec_order, order);
2550 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Johann Baudy69e3c752009-05-18 22:11:22 -07002552 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
2553 po->prot_hook.func = (po->rx_ring.pg_vec) ?
2554 tpacket_rcv : packet_rcv;
2555 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002557 pr_err("packet_mmap: vma is busy: %d\n",
2558 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
Herbert Xu905db442009-01-30 14:12:06 -08002560 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562 spin_lock(&po->bind_lock);
2563 if (was_running && !po->running) {
2564 sock_hold(sk);
2565 po->running = 1;
2566 po->num = num;
2567 dev_add_pack(&po->prot_hook);
2568 }
2569 spin_unlock(&po->bind_lock);
2570
2571 release_sock(sk);
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (pg_vec)
2574 free_pg_vec(pg_vec, order, req->tp_block_nr);
2575out:
2576 return err;
2577}
2578
Johann Baudy69e3c752009-05-18 22:11:22 -07002579static int packet_mmap(struct file *file, struct socket *sock,
2580 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
2582 struct sock *sk = sock->sk;
2583 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002584 unsigned long size, expected_size;
2585 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 unsigned long start;
2587 int err = -EINVAL;
2588 int i;
2589
2590 if (vma->vm_pgoff)
2591 return -EINVAL;
2592
Herbert Xu905db442009-01-30 14:12:06 -08002593 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002594
2595 expected_size = 0;
2596 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2597 if (rb->pg_vec) {
2598 expected_size += rb->pg_vec_len
2599 * rb->pg_vec_pages
2600 * PAGE_SIZE;
2601 }
2602 }
2603
2604 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002606
2607 size = vma->vm_end - vma->vm_start;
2608 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 goto out;
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07002612 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2613 if (rb->pg_vec == NULL)
2614 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002615
Johann Baudy69e3c752009-05-18 22:11:22 -07002616 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002617 struct page *page;
2618 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07002619 int pg_num;
2620
Changli Gaoc56b4d92010-12-01 02:52:57 +00002621 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
2622 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07002623 err = vm_insert_page(vma, start, page);
2624 if (unlikely(err))
2625 goto out;
2626 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08002627 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002628 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002631
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002632 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 vma->vm_ops = &packet_mmap_ops;
2634 err = 0;
2635
2636out:
Herbert Xu905db442009-01-30 14:12:06 -08002637 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 return err;
2639}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002641static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 .family = PF_PACKET,
2643 .owner = THIS_MODULE,
2644 .release = packet_release,
2645 .bind = packet_bind_spkt,
2646 .connect = sock_no_connect,
2647 .socketpair = sock_no_socketpair,
2648 .accept = sock_no_accept,
2649 .getname = packet_getname_spkt,
2650 .poll = datagram_poll,
2651 .ioctl = packet_ioctl,
2652 .listen = sock_no_listen,
2653 .shutdown = sock_no_shutdown,
2654 .setsockopt = sock_no_setsockopt,
2655 .getsockopt = sock_no_getsockopt,
2656 .sendmsg = packet_sendmsg_spkt,
2657 .recvmsg = packet_recvmsg,
2658 .mmap = sock_no_mmap,
2659 .sendpage = sock_no_sendpage,
2660};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002662static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 .family = PF_PACKET,
2664 .owner = THIS_MODULE,
2665 .release = packet_release,
2666 .bind = packet_bind,
2667 .connect = sock_no_connect,
2668 .socketpair = sock_no_socketpair,
2669 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002670 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 .poll = packet_poll,
2672 .ioctl = packet_ioctl,
2673 .listen = sock_no_listen,
2674 .shutdown = sock_no_shutdown,
2675 .setsockopt = packet_setsockopt,
2676 .getsockopt = packet_getsockopt,
2677 .sendmsg = packet_sendmsg,
2678 .recvmsg = packet_recvmsg,
2679 .mmap = packet_mmap,
2680 .sendpage = sock_no_sendpage,
2681};
2682
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002683static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 .family = PF_PACKET,
2685 .create = packet_create,
2686 .owner = THIS_MODULE,
2687};
2688
2689static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002690 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691};
2692
2693#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00002696 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
Denis V. Luneve372c412007-11-19 22:31:54 -08002698 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00002699
2700 rcu_read_lock();
2701 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
2704static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2705{
Herbert Xu1bf40952007-12-16 14:04:02 -08002706 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00002707 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
2710static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00002711 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
stephen hemminger808f5112010-02-22 07:57:18 +00002713 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002716static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 if (v == SEQ_START_TOKEN)
2719 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
2720 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00002721 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 const struct packet_sock *po = pkt_sk(s);
2723
2724 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00002725 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 s,
2727 atomic_read(&s->sk_refcnt),
2728 s->sk_type,
2729 ntohs(po->num),
2730 po->ifindex,
2731 po->running,
2732 atomic_read(&s->sk_rmem_alloc),
2733 sock_i_uid(s),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002734 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 }
2736
2737 return 0;
2738}
2739
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002740static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 .start = packet_seq_start,
2742 .next = packet_seq_next,
2743 .stop = packet_seq_stop,
2744 .show = packet_seq_show,
2745};
2746
2747static int packet_seq_open(struct inode *inode, struct file *file)
2748{
Denis V. Luneve372c412007-11-19 22:31:54 -08002749 return seq_open_net(inode, file, &packet_seq_ops,
2750 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
Arjan van de Venda7071d2007-02-12 00:55:36 -08002753static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 .owner = THIS_MODULE,
2755 .open = packet_seq_open,
2756 .read = seq_read,
2757 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002758 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759};
2760
2761#endif
2762
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002763static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002764{
stephen hemminger808f5112010-02-22 07:57:18 +00002765 spin_lock_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08002766 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002767
2768 if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
2769 return -ENOMEM;
2770
2771 return 0;
2772}
2773
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002774static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002775{
2776 proc_net_remove(net, "packet");
2777}
2778
2779static struct pernet_operations packet_net_ops = {
2780 .init = packet_net_init,
2781 .exit = packet_net_exit,
2782};
2783
2784
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785static void __exit packet_exit(void)
2786{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002788 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 sock_unregister(PF_PACKET);
2790 proto_unregister(&packet_proto);
2791}
2792
2793static int __init packet_init(void)
2794{
2795 int rc = proto_register(&packet_proto, 0);
2796
2797 if (rc != 0)
2798 goto out;
2799
2800 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002801 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803out:
2804 return rc;
2805}
2806
2807module_init(packet_init);
2808module_exit(packet_exit);
2809MODULE_LICENSE("GPL");
2810MODULE_ALIAS_NETPROTO(PF_PACKET);