blob: bb281bf330aaf30342fde4829fe2d710e1aad507 [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 Viroa1f8e7f72006-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 McHardy8913336a2008-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 Dumazet94b059522009-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
David S. Millerce06b032011-07-04 01:44:29 -0700225static inline struct packet_sock *pkt_sk(struct sock *sk)
226{
227 return (struct packet_sock *)sk;
228}
229
230/* register_prot_hook must be invoked with the po->bind_lock held,
231 * or from a context in which asynchronous accesses to the packet
232 * socket is not possible (packet_create()).
233 */
234static void register_prot_hook(struct sock *sk)
235{
236 struct packet_sock *po = pkt_sk(sk);
237 if (!po->running) {
238 dev_add_pack(&po->prot_hook);
239 sock_hold(sk);
240 po->running = 1;
241 }
242}
243
244/* {,__}unregister_prot_hook() must be invoked with the po->bind_lock
245 * held. If the sync parameter is true, we will temporarily drop
246 * the po->bind_lock and do a synchronize_net to make sure no
247 * asynchronous packet processing paths still refer to the elements
248 * of po->prot_hook. If the sync parameter is false, it is the
249 * callers responsibility to take care of this.
250 */
251static void __unregister_prot_hook(struct sock *sk, bool sync)
252{
253 struct packet_sock *po = pkt_sk(sk);
254
255 po->running = 0;
256 __dev_remove_pack(&po->prot_hook);
257 __sock_put(sk);
258
259 if (sync) {
260 spin_unlock(&po->bind_lock);
261 synchronize_net();
262 spin_lock(&po->bind_lock);
263 }
264}
265
266static void unregister_prot_hook(struct sock *sk, bool sync)
267{
268 struct packet_sock *po = pkt_sk(sk);
269
270 if (po->running)
271 __unregister_prot_hook(sk, sync);
272}
273
Changli Gaof6dafa92010-12-07 04:26:16 +0000274static inline __pure struct page *pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000275{
276 if (is_vmalloc_addr(addr))
277 return vmalloc_to_page(addr);
278 return virt_to_page(addr);
279}
280
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700281static void __packet_set_status(struct packet_sock *po, void *frame, int status)
282{
283 union {
284 struct tpacket_hdr *h1;
285 struct tpacket2_hdr *h2;
286 void *raw;
287 } h;
288
289 h.raw = frame;
290 switch (po->tp_version) {
291 case TPACKET_V1:
292 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000293 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700294 break;
295 case TPACKET_V2:
296 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000297 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700298 break;
Johann Baudy69e3c752009-05-18 22:11:22 -0700299 default:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000300 pr_err("TPACKET version not supported\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700301 BUG();
302 }
303
304 smp_wmb();
305}
306
307static int __packet_get_status(struct packet_sock *po, void *frame)
308{
309 union {
310 struct tpacket_hdr *h1;
311 struct tpacket2_hdr *h2;
312 void *raw;
313 } h;
314
315 smp_rmb();
316
317 h.raw = frame;
318 switch (po->tp_version) {
319 case TPACKET_V1:
Changli Gao0af55bb2010-12-01 02:52:20 +0000320 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700321 return h.h1->tp_status;
322 case TPACKET_V2:
Changli Gao0af55bb2010-12-01 02:52:20 +0000323 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700324 return h.h2->tp_status;
325 default:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000326 pr_err("TPACKET version not supported\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700327 BUG();
328 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
Johann Baudy69e3c752009-05-18 22:11:22 -0700331
332static void *packet_lookup_frame(struct packet_sock *po,
333 struct packet_ring_buffer *rb,
334 unsigned int position,
335 int status)
336{
337 unsigned int pg_vec_pos, frame_offset;
338 union {
339 struct tpacket_hdr *h1;
340 struct tpacket2_hdr *h2;
341 void *raw;
342 } h;
343
344 pg_vec_pos = position / rb->frames_per_block;
345 frame_offset = position % rb->frames_per_block;
346
Neil Horman0e3125c2010-11-16 10:26:47 -0800347 h.raw = rb->pg_vec[pg_vec_pos].buffer +
348 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700349
350 if (status != __packet_get_status(po, h.raw))
351 return NULL;
352
353 return h.raw;
354}
355
356static inline void *packet_current_frame(struct packet_sock *po,
357 struct packet_ring_buffer *rb,
358 int status)
359{
360 return packet_lookup_frame(po, rb, rb->head, status);
361}
362
363static inline void *packet_previous_frame(struct packet_sock *po,
364 struct packet_ring_buffer *rb,
365 int status)
366{
367 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
368 return packet_lookup_frame(po, rb, previous, status);
369}
370
371static inline void packet_increment_head(struct packet_ring_buffer *buff)
372{
373 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static void packet_sock_destruct(struct sock *sk)
377{
Richard Cochraned85b562010-04-07 22:41:28 +0000378 skb_queue_purge(&sk->sk_error_queue);
379
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700380 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
381 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000384 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return;
386 }
387
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -0800388 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800392static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800394static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000396static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
397 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct sock *sk;
400 struct sockaddr_pkt *spkt;
401
402 /*
403 * When we registered the protocol we saved the socket in the data
404 * field for just this event.
405 */
406
407 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /*
410 * Yank back the headers [hope the device set this
411 * right or kerboom...]
412 *
413 * Incoming packets have ll header pulled,
414 * push it back.
415 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700416 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 * so that this procedure is noop.
418 */
419
420 if (skb->pkt_type == PACKET_LOOPBACK)
421 goto out;
422
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800423 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800424 goto out;
425
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000426 skb = skb_share_check(skb, GFP_ATOMIC);
427 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 goto oom;
429
430 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +0000431 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Phil Oester84531c22005-07-12 11:57:52 -0700433 /* drop conntrack reference */
434 nf_reset(skb);
435
Herbert Xuffbc6112007-02-04 23:33:10 -0800436 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700438 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /*
441 * The SOCK_PACKET socket receives _all_ frames.
442 */
443
444 spkt->spkt_family = dev->type;
445 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
446 spkt->spkt_protocol = skb->protocol;
447
448 /*
449 * Charge the memory to the socket. This is done specifically
450 * to prevent sockets using all the memory up.
451 */
452
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000453 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455
456out:
457 kfree_skb(skb);
458oom:
459 return 0;
460}
461
462
463/*
464 * Output a raw packet to a device layer. This bypasses all the other
465 * protocol layers and you must therefore supply it with a complete frame
466 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
469 struct msghdr *msg, size_t len)
470{
471 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000472 struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000473 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct net_device *dev;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000475 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 int err;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900479 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
481
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000482 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000484 return -EINVAL;
485 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
486 proto = saddr->spkt_protocol;
487 } else
488 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900491 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 */
493
494 saddr->spkt_device[13] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000495retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100496 rcu_read_lock();
497 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 err = -ENODEV;
499 if (dev == NULL)
500 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900501
David S. Millerd5e76b02007-01-25 19:30:36 -0800502 err = -ENETDOWN;
503 if (!(dev->flags & IFF_UP))
504 goto out_unlock;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000507 * You may not queue a frame bigger than the mtu. This is the lowest level
508 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 err = -EMSGSIZE;
Ben Greear57f89bf2011-02-11 09:35:18 +0000512 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto out_unlock;
514
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000515 if (!skb) {
516 size_t reserved = LL_RESERVED_SPACE(dev);
517 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000519 rcu_read_unlock();
520 skb = sock_wmalloc(sk, len + reserved, 0, GFP_KERNEL);
521 if (skb == NULL)
522 return -ENOBUFS;
523 /* FIXME: Save some space for broken drivers that write a hard
524 * header at transmission time by themselves. PPP is the notable
525 * one here. This should really be fixed at the driver level.
526 */
527 skb_reserve(skb, reserved);
528 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900529
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000530 /* Try to align data part correctly */
531 if (hhlen) {
532 skb->data -= hhlen;
533 skb->tail -= hhlen;
534 if (len < hhlen)
535 skb_reset_network_header(skb);
536 }
537 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
538 if (err)
539 goto out_free;
540 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542
Ben Greear57f89bf2011-02-11 09:35:18 +0000543 if (len > (dev->mtu + dev->hard_header_len)) {
544 /* Earlier code assumed this would be a VLAN pkt,
545 * double-check this now that we have the actual
546 * packet in hand.
547 */
548 struct ethhdr *ehdr;
549 skb_reset_mac_header(skb);
550 ehdr = eth_hdr(skb);
551 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
552 err = -EMSGSIZE;
553 goto out_unlock;
554 }
555 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 skb->protocol = proto;
558 skb->dev = dev;
559 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +0000560 skb->mark = sk->sk_mark;
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000561 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +0000562 if (err < 0)
563 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +0100566 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000567 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100570 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000571out_free:
572 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return err;
574}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Eric Dumazet62ab0812010-12-06 20:50:09 +0000576static inline unsigned int run_filter(const struct sk_buff *skb,
577 const struct sock *sk,
David S. Millerdbcb5852007-01-24 15:21:02 -0800578 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct sk_filter *filter;
581
Eric Dumazet80f8f102011-01-18 07:46:52 +0000582 rcu_read_lock();
583 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -0800584 if (filter != NULL)
Eric Dumazet0a148422011-04-20 09:27:32 +0000585 res = SK_RUN_FILTER(filter, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +0000586 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
David S. Millerdbcb5852007-01-24 15:21:02 -0800588 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591/*
Eric Dumazet62ab0812010-12-06 20:50:09 +0000592 * This function makes lazy skb cloning in hope that most of packets
593 * are discarded by BPF.
594 *
595 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
596 * and skb->cb are mangled. It works because (and until) packets
597 * falling here are owned by current CPU. Output packets are cloned
598 * by dev_queue_xmit_nit(), input packets are processed by net_bh
599 * sequencially, so that if we return skb to original state on exit,
600 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
602
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000603static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
604 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct sock *sk;
607 struct sockaddr_ll *sll;
608 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000609 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800611 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 if (skb->pkt_type == PACKET_LOOPBACK)
614 goto drop;
615
616 sk = pt->af_packet_priv;
617 po = pkt_sk(sk);
618
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800619 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800620 goto drop;
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 skb->dev = dev;
623
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700624 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +0000626 * exported to higher levels.
627 *
628 * Otherwise, the device hides details of its frame
629 * structure, so that corresponding packet head is
630 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
632 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700633 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 else if (skb->pkt_type == PACKET_OUTGOING) {
635 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300636 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638 }
639
640 snaplen = skb->len;
641
David S. Millerdbcb5852007-01-24 15:21:02 -0800642 res = run_filter(skb, sk, snaplen);
643 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700644 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800645 if (snaplen > res)
646 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
649 (unsigned)sk->sk_rcvbuf)
650 goto drop_n_acct;
651
652 if (skb_shared(skb)) {
653 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
654 if (nskb == NULL)
655 goto drop_n_acct;
656
657 if (skb_head != skb->data) {
658 skb->data = skb_head;
659 skb->len = skb_len;
660 }
661 kfree_skb(skb);
662 skb = nskb;
663 }
664
Herbert Xuffbc6112007-02-04 23:33:10 -0800665 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
666 sizeof(skb->cb));
667
668 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 sll->sll_family = AF_PACKET;
670 sll->sll_hatype = dev->type;
671 sll->sll_protocol = skb->protocol;
672 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800673 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700674 sll->sll_ifindex = orig_dev->ifindex;
675 else
676 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700678 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Herbert Xuffbc6112007-02-04 23:33:10 -0800680 PACKET_SKB_CB(skb)->origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -0800681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (pskb_trim(skb, snaplen))
683 goto drop_n_acct;
684
685 skb_set_owner_r(skb, sk);
686 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +0000687 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Phil Oester84531c22005-07-12 11:57:52 -0700689 /* drop conntrack reference */
690 nf_reset(skb);
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 spin_lock(&sk->sk_receive_queue.lock);
693 po->stats.tp_packets++;
Neil Horman3b885782009-10-12 13:26:31 -0700694 skb->dropcount = atomic_read(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 __skb_queue_tail(&sk->sk_receive_queue, skb);
696 spin_unlock(&sk->sk_receive_queue.lock);
697 sk->sk_data_ready(sk, skb->len);
698 return 0;
699
700drop_n_acct:
Neil Horman3b885782009-10-12 13:26:31 -0700701 po->stats.tp_drops = atomic_inc_return(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703drop_n_restore:
704 if (skb_head != skb->data && skb_shared(skb)) {
705 skb->data = skb_head;
706 skb->len = skb_len;
707 }
708drop:
Neil Hormanead2ceb2009-03-11 09:49:55 +0000709 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return 0;
711}
712
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000713static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
714 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 struct sock *sk;
717 struct packet_sock *po;
718 struct sockaddr_ll *sll;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700719 union {
720 struct tpacket_hdr *h1;
721 struct tpacket2_hdr *h2;
722 void *raw;
723 } h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000724 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800726 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700728 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 struct sk_buff *copy_skb = NULL;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700730 struct timeval tv;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700731 struct timespec ts;
Scott McMillan614f60f2010-06-02 05:53:56 -0700732 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 if (skb->pkt_type == PACKET_LOOPBACK)
735 goto drop;
736
737 sk = pt->af_packet_priv;
738 po = pkt_sk(sk);
739
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800740 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800741 goto drop;
742
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700743 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700745 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 else if (skb->pkt_type == PACKET_OUTGOING) {
747 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300748 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750 }
751
Herbert Xu8dc41942007-02-04 23:31:32 -0800752 if (skb->ip_summed == CHECKSUM_PARTIAL)
753 status |= TP_STATUS_CSUMNOTREADY;
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 snaplen = skb->len;
756
David S. Millerdbcb5852007-01-24 15:21:02 -0800757 res = run_filter(skb, sk, snaplen);
758 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700759 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800760 if (snaplen > res)
761 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -0700764 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
765 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 } else {
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300767 unsigned maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700768 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -0700769 (maclen < 16 ? 16 : maclen)) +
770 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 macoff = netoff - maclen;
772 }
773
Johann Baudy69e3c752009-05-18 22:11:22 -0700774 if (macoff + snaplen > po->rx_ring.frame_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (po->copy_thresh &&
776 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
777 (unsigned)sk->sk_rcvbuf) {
778 if (skb_shared(skb)) {
779 copy_skb = skb_clone(skb, GFP_ATOMIC);
780 } else {
781 copy_skb = skb_get(skb);
782 skb_head = skb->data;
783 }
784 if (copy_skb)
785 skb_set_owner_r(copy_skb, sk);
786 }
Johann Baudy69e3c752009-05-18 22:11:22 -0700787 snaplen = po->rx_ring.frame_size - macoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if ((int)snaplen < 0)
789 snaplen = 0;
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 spin_lock(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -0700793 h.raw = packet_current_frame(po, &po->rx_ring, TP_STATUS_KERNEL);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700794 if (!h.raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 goto ring_is_full;
Johann Baudy69e3c752009-05-18 22:11:22 -0700796 packet_increment_head(&po->rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 po->stats.tp_packets++;
798 if (copy_skb) {
799 status |= TP_STATUS_COPY;
800 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
801 }
802 if (!po->stats.tp_drops)
803 status &= ~TP_STATUS_LOSING;
804 spin_unlock(&sk->sk_receive_queue.lock);
805
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700806 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700808 switch (po->tp_version) {
809 case TPACKET_V1:
810 h.h1->tp_len = skb->len;
811 h.h1->tp_snaplen = snaplen;
812 h.h1->tp_mac = macoff;
813 h.h1->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -0700814 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
815 && shhwtstamps->syststamp.tv64)
816 tv = ktime_to_timeval(shhwtstamps->syststamp);
817 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
818 && shhwtstamps->hwtstamp.tv64)
819 tv = ktime_to_timeval(shhwtstamps->hwtstamp);
820 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700821 tv = ktime_to_timeval(skb->tstamp);
822 else
823 do_gettimeofday(&tv);
824 h.h1->tp_sec = tv.tv_sec;
825 h.h1->tp_usec = tv.tv_usec;
826 hdrlen = sizeof(*h.h1);
827 break;
828 case TPACKET_V2:
829 h.h2->tp_len = skb->len;
830 h.h2->tp_snaplen = snaplen;
831 h.h2->tp_mac = macoff;
832 h.h2->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -0700833 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
834 && shhwtstamps->syststamp.tv64)
835 ts = ktime_to_timespec(shhwtstamps->syststamp);
836 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
837 && shhwtstamps->hwtstamp.tv64)
838 ts = ktime_to_timespec(shhwtstamps->hwtstamp);
839 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700840 ts = ktime_to_timespec(skb->tstamp);
841 else
842 getnstimeofday(&ts);
843 h.h2->tp_sec = ts.tv_sec;
844 h.h2->tp_nsec = ts.tv_nsec;
Ben Greeara3bcc232011-06-01 06:49:10 +0000845 if (vlan_tx_tag_present(skb)) {
846 h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
847 status |= TP_STATUS_VLAN_VALID;
848 } else {
849 h.h2->tp_vlan_tci = 0;
850 }
Eric Dumazet13fcb7b2011-06-06 22:42:06 -0700851 h.h2->tp_padding = 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700852 hdrlen = sizeof(*h.h2);
853 break;
854 default:
855 BUG();
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700858 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700859 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 sll->sll_family = AF_PACKET;
861 sll->sll_hatype = dev->type;
862 sll->sll_protocol = skb->protocol;
863 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800864 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700865 sll->sll_ifindex = orig_dev->ifindex;
866 else
867 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700869 __packet_set_status(po, h.raw, status);
Ralf Baechlee16aa202006-12-07 00:11:33 -0800870 smp_mb();
Changli Gaof6dafa92010-12-07 04:26:16 +0000871#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 {
Changli Gao0af55bb2010-12-01 02:52:20 +0000873 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Changli Gao0af55bb2010-12-01 02:52:20 +0000875 end = (u8 *)PAGE_ALIGN((unsigned long)h.raw + macoff + snaplen);
876 for (start = h.raw; start < end; start += PAGE_SIZE)
877 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
Changli Gaof6dafa92010-12-07 04:26:16 +0000879#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 sk->sk_data_ready(sk, 0);
882
883drop_n_restore:
884 if (skb_head != skb->data && skb_shared(skb)) {
885 skb->data = skb_head;
886 skb->len = skb_len;
887 }
888drop:
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900889 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return 0;
891
892ring_is_full:
893 po->stats.tp_drops++;
894 spin_unlock(&sk->sk_receive_queue.lock);
895
896 sk->sk_data_ready(sk, 0);
Wei Yongjunacb5d752009-02-25 00:36:42 +0000897 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 goto drop_n_restore;
899}
900
Johann Baudy69e3c752009-05-18 22:11:22 -0700901static void tpacket_destruct_skb(struct sk_buff *skb)
902{
903 struct packet_sock *po = pkt_sk(skb->sk);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000904 void *ph;
Johann Baudy69e3c752009-05-18 22:11:22 -0700905
906 BUG_ON(skb == NULL);
907
908 if (likely(po->tx_ring.pg_vec)) {
909 ph = skb_shinfo(skb)->destructor_arg;
910 BUG_ON(__packet_get_status(po, ph) != TP_STATUS_SENDING);
911 BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
912 atomic_dec(&po->tx_ring.pending);
913 __packet_set_status(po, ph, TP_STATUS_AVAILABLE);
914 }
915
916 sock_wfree(skb);
917}
918
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000919static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
920 void *frame, struct net_device *dev, int size_max,
921 __be16 proto, unsigned char *addr)
Johann Baudy69e3c752009-05-18 22:11:22 -0700922{
923 union {
924 struct tpacket_hdr *h1;
925 struct tpacket2_hdr *h2;
926 void *raw;
927 } ph;
928 int to_write, offset, len, tp_len, nr_frags, len_max;
929 struct socket *sock = po->sk.sk_socket;
930 struct page *page;
931 void *data;
932 int err;
933
934 ph.raw = frame;
935
936 skb->protocol = proto;
937 skb->dev = dev;
938 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +0000939 skb->mark = po->sk.sk_mark;
Johann Baudy69e3c752009-05-18 22:11:22 -0700940 skb_shinfo(skb)->destructor_arg = ph.raw;
941
942 switch (po->tp_version) {
943 case TPACKET_V2:
944 tp_len = ph.h2->tp_len;
945 break;
946 default:
947 tp_len = ph.h1->tp_len;
948 break;
949 }
950 if (unlikely(tp_len > size_max)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000951 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
Johann Baudy69e3c752009-05-18 22:11:22 -0700952 return -EMSGSIZE;
953 }
954
955 skb_reserve(skb, LL_RESERVED_SPACE(dev));
956 skb_reset_network_header(skb);
957
958 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
959 to_write = tp_len;
960
961 if (sock->type == SOCK_DGRAM) {
962 err = dev_hard_header(skb, dev, ntohs(proto), addr,
963 NULL, tp_len);
964 if (unlikely(err < 0))
965 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000966 } else if (dev->hard_header_len) {
Johann Baudy69e3c752009-05-18 22:11:22 -0700967 /* net device doesn't like empty head */
968 if (unlikely(tp_len <= dev->hard_header_len)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000969 pr_err("packet size is too short (%d < %d)\n",
970 tp_len, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -0700971 return -EINVAL;
972 }
973
974 skb_push(skb, dev->hard_header_len);
975 err = skb_store_bits(skb, 0, data,
976 dev->hard_header_len);
977 if (unlikely(err))
978 return err;
979
980 data += dev->hard_header_len;
981 to_write -= dev->hard_header_len;
982 }
983
984 err = -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -0700985 offset = offset_in_page(data);
986 len_max = PAGE_SIZE - offset;
987 len = ((to_write > len_max) ? len_max : to_write);
988
989 skb->data_len = to_write;
990 skb->len += to_write;
991 skb->truesize += to_write;
992 atomic_add(to_write, &po->sk.sk_wmem_alloc);
993
994 while (likely(to_write)) {
995 nr_frags = skb_shinfo(skb)->nr_frags;
996
997 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000998 pr_err("Packet exceed the number of skb frags(%lu)\n",
999 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07001000 return -EFAULT;
1001 }
1002
Changli Gao0af55bb2010-12-01 02:52:20 +00001003 page = pgv_to_page(data);
1004 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07001005 flush_dcache_page(page);
1006 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00001007 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07001008 to_write -= len;
1009 offset = 0;
1010 len_max = PAGE_SIZE;
1011 len = ((to_write > len_max) ? len_max : to_write);
1012 }
1013
1014 return tp_len;
1015}
1016
1017static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
1018{
Johann Baudy69e3c752009-05-18 22:11:22 -07001019 struct sk_buff *skb;
1020 struct net_device *dev;
1021 __be16 proto;
Ben Greear827d9782011-06-01 07:18:53 +00001022 bool need_rls_dev = false;
1023 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001024 void *ph;
1025 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Johann Baudy69e3c752009-05-18 22:11:22 -07001026 int tp_len, size_max;
1027 unsigned char *addr;
1028 int len_sum = 0;
1029 int status = 0;
1030
Johann Baudy69e3c752009-05-18 22:11:22 -07001031 mutex_lock(&po->pg_vec_lock);
1032
1033 err = -EBUSY;
1034 if (saddr == NULL) {
Ben Greear827d9782011-06-01 07:18:53 +00001035 dev = po->prot_hook.dev;
Johann Baudy69e3c752009-05-18 22:11:22 -07001036 proto = po->num;
1037 addr = NULL;
1038 } else {
1039 err = -EINVAL;
1040 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
1041 goto out;
1042 if (msg->msg_namelen < (saddr->sll_halen
1043 + offsetof(struct sockaddr_ll,
1044 sll_addr)))
1045 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07001046 proto = saddr->sll_protocol;
1047 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00001048 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
1049 need_rls_dev = true;
Johann Baudy69e3c752009-05-18 22:11:22 -07001050 }
1051
Johann Baudy69e3c752009-05-18 22:11:22 -07001052 err = -ENXIO;
1053 if (unlikely(dev == NULL))
1054 goto out;
1055
1056 reserve = dev->hard_header_len;
1057
1058 err = -ENETDOWN;
1059 if (unlikely(!(dev->flags & IFF_UP)))
1060 goto out_put;
1061
1062 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07001063 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07001064
1065 if (size_max > dev->mtu + reserve)
1066 size_max = dev->mtu + reserve;
1067
1068 do {
1069 ph = packet_current_frame(po, &po->tx_ring,
1070 TP_STATUS_SEND_REQUEST);
1071
1072 if (unlikely(ph == NULL)) {
1073 schedule();
1074 continue;
1075 }
1076
1077 status = TP_STATUS_SEND_REQUEST;
1078 skb = sock_alloc_send_skb(&po->sk,
1079 LL_ALLOCATED_SPACE(dev)
1080 + sizeof(struct sockaddr_ll),
1081 0, &err);
1082
1083 if (unlikely(skb == NULL))
1084 goto out_status;
1085
1086 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
1087 addr);
1088
1089 if (unlikely(tp_len < 0)) {
1090 if (po->tp_loss) {
1091 __packet_set_status(po, ph,
1092 TP_STATUS_AVAILABLE);
1093 packet_increment_head(&po->tx_ring);
1094 kfree_skb(skb);
1095 continue;
1096 } else {
1097 status = TP_STATUS_WRONG_FORMAT;
1098 err = tp_len;
1099 goto out_status;
1100 }
1101 }
1102
1103 skb->destructor = tpacket_destruct_skb;
1104 __packet_set_status(po, ph, TP_STATUS_SENDING);
1105 atomic_inc(&po->tx_ring.pending);
1106
1107 status = TP_STATUS_SEND_REQUEST;
1108 err = dev_queue_xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00001109 if (unlikely(err > 0)) {
1110 err = net_xmit_errno(err);
1111 if (err && __packet_get_status(po, ph) ==
1112 TP_STATUS_AVAILABLE) {
1113 /* skb was destructed already */
1114 skb = NULL;
1115 goto out_status;
1116 }
1117 /*
1118 * skb was dropped but not destructed yet;
1119 * let's treat it like congestion or err < 0
1120 */
1121 err = 0;
1122 }
Johann Baudy69e3c752009-05-18 22:11:22 -07001123 packet_increment_head(&po->tx_ring);
1124 len_sum += tp_len;
Joe Perchesf64f9e72009-11-29 16:55:45 -08001125 } while (likely((ph != NULL) ||
1126 ((!(msg->msg_flags & MSG_DONTWAIT)) &&
1127 (atomic_read(&po->tx_ring.pending))))
1128 );
Johann Baudy69e3c752009-05-18 22:11:22 -07001129
1130 err = len_sum;
1131 goto out_put;
1132
Johann Baudy69e3c752009-05-18 22:11:22 -07001133out_status:
1134 __packet_set_status(po, ph, status);
1135 kfree_skb(skb);
1136out_put:
Ben Greear827d9782011-06-01 07:18:53 +00001137 if (need_rls_dev)
1138 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07001139out:
1140 mutex_unlock(&po->pg_vec_lock);
1141 return err;
1142}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001144static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
1145 size_t reserve, size_t len,
1146 size_t linear, int noblock,
1147 int *err)
1148{
1149 struct sk_buff *skb;
1150
1151 /* Under a page? Don't bother with paged skb. */
1152 if (prepad + len < PAGE_SIZE || !linear)
1153 linear = len;
1154
1155 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
1156 err);
1157 if (!skb)
1158 return NULL;
1159
1160 skb_reserve(skb, reserve);
1161 skb_put(skb, linear);
1162 skb->data_len = len - linear;
1163 skb->len += len - linear;
1164
1165 return skb;
1166}
1167
Johann Baudy69e3c752009-05-18 22:11:22 -07001168static int packet_snd(struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct msghdr *msg, size_t len)
1170{
1171 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001172 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 struct sk_buff *skb;
1174 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08001175 __be16 proto;
Ben Greear827d9782011-06-01 07:18:53 +00001176 bool need_rls_dev = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 unsigned char *addr;
Ben Greear827d9782011-06-01 07:18:53 +00001178 int err, reserve = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001179 struct virtio_net_hdr vnet_hdr = { 0 };
1180 int offset = 0;
1181 int vnet_hdr_len;
1182 struct packet_sock *po = pkt_sk(sk);
1183 unsigned short gso_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001186 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (saddr == NULL) {
Ben Greear827d9782011-06-01 07:18:53 +00001190 dev = po->prot_hook.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 proto = po->num;
1192 addr = NULL;
1193 } else {
1194 err = -EINVAL;
1195 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
1196 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001197 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
1198 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 proto = saddr->sll_protocol;
1200 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00001201 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
1202 need_rls_dev = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 err = -ENXIO;
1206 if (dev == NULL)
1207 goto out_unlock;
1208 if (sock->type == SOCK_RAW)
1209 reserve = dev->hard_header_len;
1210
David S. Millerd5e76b02007-01-25 19:30:36 -08001211 err = -ENETDOWN;
1212 if (!(dev->flags & IFF_UP))
1213 goto out_unlock;
1214
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001215 if (po->has_vnet_hdr) {
1216 vnet_hdr_len = sizeof(vnet_hdr);
1217
1218 err = -EINVAL;
1219 if (len < vnet_hdr_len)
1220 goto out_unlock;
1221
1222 len -= vnet_hdr_len;
1223
1224 err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
1225 vnet_hdr_len);
1226 if (err < 0)
1227 goto out_unlock;
1228
1229 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1230 (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
1231 vnet_hdr.hdr_len))
1232 vnet_hdr.hdr_len = vnet_hdr.csum_start +
1233 vnet_hdr.csum_offset + 2;
1234
1235 err = -EINVAL;
1236 if (vnet_hdr.hdr_len > len)
1237 goto out_unlock;
1238
1239 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1240 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1241 case VIRTIO_NET_HDR_GSO_TCPV4:
1242 gso_type = SKB_GSO_TCPV4;
1243 break;
1244 case VIRTIO_NET_HDR_GSO_TCPV6:
1245 gso_type = SKB_GSO_TCPV6;
1246 break;
1247 case VIRTIO_NET_HDR_GSO_UDP:
1248 gso_type = SKB_GSO_UDP;
1249 break;
1250 default:
1251 goto out_unlock;
1252 }
1253
1254 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1255 gso_type |= SKB_GSO_TCP_ECN;
1256
1257 if (vnet_hdr.gso_size == 0)
1258 goto out_unlock;
1259
1260 }
1261 }
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 err = -EMSGSIZE;
Ben Greear57f89bf2011-02-11 09:35:18 +00001264 if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 goto out_unlock;
1266
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001267 err = -ENOBUFS;
1268 skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev),
1269 LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len,
1270 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001271 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 goto out_unlock;
1273
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001274 skb_set_network_header(skb, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001276 err = -EINVAL;
1277 if (sock->type == SOCK_DGRAM &&
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001278 (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001279 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 /* Returns -EFAULT on error */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001282 err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (err)
1284 goto out_free;
Oliver Hartkopp2244d072010-08-17 08:59:14 +00001285 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +00001286 if (err < 0)
1287 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Ben Greear57f89bf2011-02-11 09:35:18 +00001289 if (!gso_type && (len > dev->mtu + reserve)) {
1290 /* Earlier code assumed this would be a VLAN pkt,
1291 * double-check this now that we have the actual
1292 * packet in hand.
1293 */
1294 struct ethhdr *ehdr;
1295 skb_reset_mac_header(skb);
1296 ehdr = eth_hdr(skb);
1297 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
1298 err = -EMSGSIZE;
1299 goto out_free;
1300 }
1301 }
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 skb->protocol = proto;
1304 skb->dev = dev;
1305 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001306 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001308 if (po->has_vnet_hdr) {
1309 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1310 if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
1311 vnet_hdr.csum_offset)) {
1312 err = -EINVAL;
1313 goto out_free;
1314 }
1315 }
1316
1317 skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
1318 skb_shinfo(skb)->gso_type = gso_type;
1319
1320 /* Header must be checked, and gso_segs computed. */
1321 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1322 skb_shinfo(skb)->gso_segs = 0;
1323
1324 len += vnet_hdr_len;
1325 }
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /*
1328 * Now send it
1329 */
1330
1331 err = dev_queue_xmit(skb);
1332 if (err > 0 && (err = net_xmit_errno(err)) != 0)
1333 goto out_unlock;
1334
Ben Greear827d9782011-06-01 07:18:53 +00001335 if (need_rls_dev)
1336 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001338 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340out_free:
1341 kfree_skb(skb);
1342out_unlock:
Ben Greear827d9782011-06-01 07:18:53 +00001343 if (dev && need_rls_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 dev_put(dev);
1345out:
1346 return err;
1347}
1348
Johann Baudy69e3c752009-05-18 22:11:22 -07001349static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
1350 struct msghdr *msg, size_t len)
1351{
Johann Baudy69e3c752009-05-18 22:11:22 -07001352 struct sock *sk = sock->sk;
1353 struct packet_sock *po = pkt_sk(sk);
1354 if (po->tx_ring.pg_vec)
1355 return tpacket_snd(po, msg);
1356 else
Johann Baudy69e3c752009-05-18 22:11:22 -07001357 return packet_snd(sock, msg, len);
1358}
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360/*
1361 * Close a PACKET socket. This is fairly simple. We immediately go
1362 * to 'closed' state and remove our protocol entry in the device list.
1363 */
1364
1365static int packet_release(struct socket *sock)
1366{
1367 struct sock *sk = sock->sk;
1368 struct packet_sock *po;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001369 struct net *net;
Johann Baudy69e3c752009-05-18 22:11:22 -07001370 struct tpacket_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 if (!sk)
1373 return 0;
1374
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001375 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 po = pkt_sk(sk);
1377
stephen hemminger808f5112010-02-22 07:57:18 +00001378 spin_lock_bh(&net->packet.sklist_lock);
1379 sk_del_node_init_rcu(sk);
Eric Dumazet920de802008-11-24 00:09:29 -08001380 sock_prot_inuse_add(net, sk->sk_prot, -1);
stephen hemminger808f5112010-02-22 07:57:18 +00001381 spin_unlock_bh(&net->packet.sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
stephen hemminger808f5112010-02-22 07:57:18 +00001383 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07001384 unregister_prot_hook(sk, false);
Ben Greear160ff182011-06-01 07:18:52 +00001385 if (po->prot_hook.dev) {
1386 dev_put(po->prot_hook.dev);
1387 po->prot_hook.dev = NULL;
1388 }
stephen hemminger808f5112010-02-22 07:57:18 +00001389 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Johann Baudy69e3c752009-05-18 22:11:22 -07001393 memset(&req, 0, sizeof(req));
1394
1395 if (po->rx_ring.pg_vec)
1396 packet_set_ring(sk, &req, 1, 0);
1397
1398 if (po->tx_ring.pg_vec)
1399 packet_set_ring(sk, &req, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
stephen hemminger808f5112010-02-22 07:57:18 +00001401 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
1403 * Now the socket is dead. No more input will appear.
1404 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 sock_orphan(sk);
1406 sock->sk = NULL;
1407
1408 /* Purge queues */
1409
1410 skb_queue_purge(&sk->sk_receive_queue);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001411 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 sock_put(sk);
1414 return 0;
1415}
1416
1417/*
1418 * Attach a packet hook.
1419 */
1420
Al Viro0e11c912006-11-08 00:26:29 -08001421static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 struct packet_sock *po = pkt_sk(sk);
1424 /*
1425 * Detach an existing hook if present.
1426 */
1427
1428 lock_sock(sk);
1429
1430 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07001431 unregister_prot_hook(sk, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 po->num = protocol;
1433 po->prot_hook.type = protocol;
Ben Greear160ff182011-06-01 07:18:52 +00001434 if (po->prot_hook.dev)
1435 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 po->prot_hook.dev = dev;
1437
1438 po->ifindex = dev ? dev->ifindex : 0;
1439
1440 if (protocol == 0)
1441 goto out_unlock;
1442
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001443 if (!dev || (dev->flags & IFF_UP)) {
David S. Millerce06b032011-07-04 01:44:29 -07001444 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001445 } else {
1446 sk->sk_err = ENETDOWN;
1447 if (!sock_flag(sk, SOCK_DEAD))
1448 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450
1451out_unlock:
1452 spin_unlock(&po->bind_lock);
1453 release_sock(sk);
1454 return 0;
1455}
1456
1457/*
1458 * Bind a packet socket to a device
1459 */
1460
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001461static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
1462 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001464 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 char name[15];
1466 struct net_device *dev;
1467 int err = -ENODEV;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /*
1470 * Check legality
1471 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001472
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001473 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001475 strlcpy(name, uaddr->sa_data, sizeof(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001477 dev = dev_get_by_name(sock_net(sk), name);
Ben Greear160ff182011-06-01 07:18:52 +00001478 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return err;
1481}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1484{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001485 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
1486 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 struct net_device *dev = NULL;
1488 int err;
1489
1490
1491 /*
1492 * Check legality
1493 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (addr_len < sizeof(struct sockaddr_ll))
1496 return -EINVAL;
1497 if (sll->sll_family != AF_PACKET)
1498 return -EINVAL;
1499
1500 if (sll->sll_ifindex) {
1501 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001502 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (dev == NULL)
1504 goto out;
1505 }
1506 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508out:
1509 return err;
1510}
1511
1512static struct proto packet_proto = {
1513 .name = "PACKET",
1514 .owner = THIS_MODULE,
1515 .obj_size = sizeof(struct packet_sock),
1516};
1517
1518/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001519 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 */
1521
Eric Paris3f378b62009-11-05 22:18:14 -08001522static int packet_create(struct net *net, struct socket *sock, int protocol,
1523 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 struct sock *sk;
1526 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08001527 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 int err;
1529
1530 if (!capable(CAP_NET_RAW))
1531 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07001532 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
1533 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return -ESOCKTNOSUPPORT;
1535
1536 sock->state = SS_UNCONNECTED;
1537
1538 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001539 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (sk == NULL)
1541 goto out;
1542
1543 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (sock->type == SOCK_PACKET)
1545 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 sock_init_data(sock, sk);
1548
1549 po = pkt_sk(sk);
1550 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08001551 po->num = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001554 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
1556 /*
1557 * Attach a protocol block
1558 */
1559
1560 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08001561 mutex_init(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (sock->type == SOCK_PACKET)
1565 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 po->prot_hook.af_packet_priv = sk;
1568
Al Viro0e11c912006-11-08 00:26:29 -08001569 if (proto) {
1570 po->prot_hook.type = proto;
David S. Millerce06b032011-07-04 01:44:29 -07001571 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
1573
stephen hemminger808f5112010-02-22 07:57:18 +00001574 spin_lock_bh(&net->packet.sklist_lock);
1575 sk_add_node_rcu(sk, &net->packet.sklist);
Eric Dumazet36804532008-11-19 14:25:35 -08001576 sock_prot_inuse_add(net, &packet_proto, 1);
stephen hemminger808f5112010-02-22 07:57:18 +00001577 spin_unlock_bh(&net->packet.sklist_lock);
1578
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580out:
1581 return err;
1582}
1583
Richard Cochraned85b562010-04-07 22:41:28 +00001584static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
1585{
1586 struct sock_exterr_skb *serr;
1587 struct sk_buff *skb, *skb2;
1588 int copied, err;
1589
1590 err = -EAGAIN;
1591 skb = skb_dequeue(&sk->sk_error_queue);
1592 if (skb == NULL)
1593 goto out;
1594
1595 copied = skb->len;
1596 if (copied > len) {
1597 msg->msg_flags |= MSG_TRUNC;
1598 copied = len;
1599 }
1600 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1601 if (err)
1602 goto out_free_skb;
1603
1604 sock_recv_timestamp(msg, sk, skb);
1605
1606 serr = SKB_EXT_ERR(skb);
1607 put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
1608 sizeof(serr->ee), &serr->ee);
1609
1610 msg->msg_flags |= MSG_ERRQUEUE;
1611 err = copied;
1612
1613 /* Reset and regenerate socket error */
1614 spin_lock_bh(&sk->sk_error_queue.lock);
1615 sk->sk_err = 0;
1616 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
1617 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
1618 spin_unlock_bh(&sk->sk_error_queue.lock);
1619 sk->sk_error_report(sk);
1620 } else
1621 spin_unlock_bh(&sk->sk_error_queue.lock);
1622
1623out_free_skb:
1624 kfree_skb(skb);
1625out:
1626 return err;
1627}
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/*
1630 * Pull a packet from our receive queue and hand it to the user.
1631 * If necessary we block.
1632 */
1633
1634static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1635 struct msghdr *msg, size_t len, int flags)
1636{
1637 struct sock *sk = sock->sk;
1638 struct sk_buff *skb;
1639 int copied, err;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001640 struct sockaddr_ll *sll;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001641 int vnet_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00001644 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 goto out;
1646
1647#if 0
1648 /* What error should we return now? EUNATTACH? */
1649 if (pkt_sk(sk)->ifindex < 0)
1650 return -ENODEV;
1651#endif
1652
Richard Cochraned85b562010-04-07 22:41:28 +00001653 if (flags & MSG_ERRQUEUE) {
1654 err = packet_recv_error(sk, msg, len);
1655 goto out;
1656 }
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 * Call the generic datagram receiver. This handles all sorts
1660 * of horrible races and re-entrancy so we can forget about it
1661 * in the protocol layers.
1662 *
1663 * Now it will return ENETDOWN, if device have just gone down,
1664 * but then it will block.
1665 */
1666
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001667 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001670 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 * handles the blocking we don't see and worry about blocking
1672 * retries.
1673 */
1674
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001675 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 goto out;
1677
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001678 if (pkt_sk(sk)->has_vnet_hdr) {
1679 struct virtio_net_hdr vnet_hdr = { 0 };
1680
1681 err = -EINVAL;
1682 vnet_hdr_len = sizeof(vnet_hdr);
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001683 if (len < vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001684 goto out_free;
1685
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001686 len -= vnet_hdr_len;
1687
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001688 if (skb_is_gso(skb)) {
1689 struct skb_shared_info *sinfo = skb_shinfo(skb);
1690
1691 /* This is a hint as to how much should be linear. */
1692 vnet_hdr.hdr_len = skb_headlen(skb);
1693 vnet_hdr.gso_size = sinfo->gso_size;
1694 if (sinfo->gso_type & SKB_GSO_TCPV4)
1695 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1696 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1697 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1698 else if (sinfo->gso_type & SKB_GSO_UDP)
1699 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1700 else if (sinfo->gso_type & SKB_GSO_FCOE)
1701 goto out_free;
1702 else
1703 BUG();
1704 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1705 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1706 } else
1707 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1708
1709 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1710 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00001711 vnet_hdr.csum_start = skb_checksum_start_offset(skb);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001712 vnet_hdr.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00001713 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
1714 vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001715 } /* else everything is zero */
1716
1717 err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
1718 vnet_hdr_len);
1719 if (err < 0)
1720 goto out_free;
1721 }
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /*
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001724 * If the address length field is there to be filled in, we fill
1725 * it in now.
1726 */
1727
Herbert Xuffbc6112007-02-04 23:33:10 -08001728 sll = &PACKET_SKB_CB(skb)->sa.ll;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001729 if (sock->type == SOCK_PACKET)
1730 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1731 else
1732 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1733
1734 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 * You lose any data beyond the buffer you gave. If it worries a
1736 * user program they can ask the device for its MTU anyway.
1737 */
1738
1739 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001740 if (copied > len) {
1741 copied = len;
1742 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744
1745 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1746 if (err)
1747 goto out_free;
1748
Neil Horman3b885782009-10-12 13:26:31 -07001749 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 if (msg->msg_name)
Herbert Xuffbc6112007-02-04 23:33:10 -08001752 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
1753 msg->msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Herbert Xu8dc41942007-02-04 23:31:32 -08001755 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08001756 struct tpacket_auxdata aux;
1757
1758 aux.tp_status = TP_STATUS_USER;
1759 if (skb->ip_summed == CHECKSUM_PARTIAL)
1760 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
1761 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
1762 aux.tp_snaplen = skb->len;
1763 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001764 aux.tp_net = skb_network_offset(skb);
Ben Greeara3bcc232011-06-01 06:49:10 +00001765 if (vlan_tx_tag_present(skb)) {
1766 aux.tp_vlan_tci = vlan_tx_tag_get(skb);
1767 aux.tp_status |= TP_STATUS_VLAN_VALID;
1768 } else {
1769 aux.tp_vlan_tci = 0;
1770 }
Eric Dumazet13fcb7b2011-06-06 22:42:06 -07001771 aux.tp_padding = 0;
Herbert Xuffbc6112007-02-04 23:33:10 -08001772 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08001773 }
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 /*
1776 * Free or return the buffer as appropriate. Again this
1777 * hides all the races and re-entrancy issues from us.
1778 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001779 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781out_free:
1782 skb_free_datagram(sk, skb);
1783out:
1784 return err;
1785}
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1788 int *uaddr_len, int peer)
1789{
1790 struct net_device *dev;
1791 struct sock *sk = sock->sk;
1792
1793 if (peer)
1794 return -EOPNOTSUPP;
1795
1796 uaddr->sa_family = AF_PACKET;
Eric Dumazet654d1f82009-11-02 10:43:32 +01001797 rcu_read_lock();
1798 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
1799 if (dev)
Vasiliy Kulikov67286642010-11-10 12:09:10 -08001800 strncpy(uaddr->sa_data, dev->name, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001801 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 memset(uaddr->sa_data, 0, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001803 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 *uaddr_len = sizeof(*uaddr);
1805
1806 return 0;
1807}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1810 int *uaddr_len, int peer)
1811{
1812 struct net_device *dev;
1813 struct sock *sk = sock->sk;
1814 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001815 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 if (peer)
1818 return -EOPNOTSUPP;
1819
1820 sll->sll_family = AF_PACKET;
1821 sll->sll_ifindex = po->ifindex;
1822 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08001823 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01001824 rcu_read_lock();
1825 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (dev) {
1827 sll->sll_hatype = dev->type;
1828 sll->sll_halen = dev->addr_len;
1829 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 } else {
1831 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1832 sll->sll_halen = 0;
1833 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01001834 rcu_read_unlock();
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001835 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 return 0;
1838}
1839
Wang Chen2aeb0b82008-07-14 20:49:46 -07001840static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
1841 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 switch (i->type) {
1844 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00001845 if (i->alen != dev->addr_len)
1846 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001848 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00001850 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 break;
1852 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07001853 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 break;
1855 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07001856 return dev_set_allmulti(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 break;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001858 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00001859 if (i->alen != dev->addr_len)
1860 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001861 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001862 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001863 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001864 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001865 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001866 default:
1867 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07001869 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
1872static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1873{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001874 for ( ; i; i = i->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (i->ifindex == dev->ifindex)
1876 packet_dev_mc(dev, i, what);
1877 }
1878}
1879
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001880static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 struct packet_sock *po = pkt_sk(sk);
1883 struct packet_mclist *ml, *i;
1884 struct net_device *dev;
1885 int err;
1886
1887 rtnl_lock();
1888
1889 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001890 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (!dev)
1892 goto done;
1893
1894 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00001895 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 goto done;
1897
1898 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001899 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (i == NULL)
1901 goto done;
1902
1903 err = 0;
1904 for (ml = po->mclist; ml; ml = ml->next) {
1905 if (ml->ifindex == mreq->mr_ifindex &&
1906 ml->type == mreq->mr_type &&
1907 ml->alen == mreq->mr_alen &&
1908 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1909 ml->count++;
1910 /* Free the new element ... */
1911 kfree(i);
1912 goto done;
1913 }
1914 }
1915
1916 i->type = mreq->mr_type;
1917 i->ifindex = mreq->mr_ifindex;
1918 i->alen = mreq->mr_alen;
1919 memcpy(i->addr, mreq->mr_address, i->alen);
1920 i->count = 1;
1921 i->next = po->mclist;
1922 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07001923 err = packet_dev_mc(dev, i, 1);
1924 if (err) {
1925 po->mclist = i->next;
1926 kfree(i);
1927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929done:
1930 rtnl_unlock();
1931 return err;
1932}
1933
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001934static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 struct packet_mclist *ml, **mlp;
1937
1938 rtnl_lock();
1939
1940 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
1941 if (ml->ifindex == mreq->mr_ifindex &&
1942 ml->type == mreq->mr_type &&
1943 ml->alen == mreq->mr_alen &&
1944 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1945 if (--ml->count == 0) {
1946 struct net_device *dev;
1947 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00001948 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
1949 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 kfree(ml);
1952 }
1953 rtnl_unlock();
1954 return 0;
1955 }
1956 }
1957 rtnl_unlock();
1958 return -EADDRNOTAVAIL;
1959}
1960
1961static void packet_flush_mclist(struct sock *sk)
1962{
1963 struct packet_sock *po = pkt_sk(sk);
1964 struct packet_mclist *ml;
1965
1966 if (!po->mclist)
1967 return;
1968
1969 rtnl_lock();
1970 while ((ml = po->mclist) != NULL) {
1971 struct net_device *dev;
1972
1973 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00001974 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
1975 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 kfree(ml);
1978 }
1979 rtnl_unlock();
1980}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982static int
David S. Millerb7058842009-09-30 16:12:20 -07001983packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08001986 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 int ret;
1988
1989 if (level != SOL_PACKET)
1990 return -ENOPROTOOPT;
1991
Johann Baudy69e3c752009-05-18 22:11:22 -07001992 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001993 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 case PACKET_DROP_MEMBERSHIP:
1995 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001996 struct packet_mreq_max mreq;
1997 int len = optlen;
1998 memset(&mreq, 0, sizeof(mreq));
1999 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002001 if (len > sizeof(mreq))
2002 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002003 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002005 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
2006 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (optname == PACKET_ADD_MEMBERSHIP)
2008 ret = packet_mc_add(sk, &mreq);
2009 else
2010 ret = packet_mc_drop(sk, &mreq);
2011 return ret;
2012 }
David S. Millera2efcfa2007-05-29 13:12:50 -07002013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07002015 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 {
2017 struct tpacket_req req;
2018
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002019 if (optlen < sizeof(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002021 if (pkt_sk(sk)->has_vnet_hdr)
2022 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002023 if (copy_from_user(&req, optval, sizeof(req)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -07002025 return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027 case PACKET_COPY_THRESH:
2028 {
2029 int val;
2030
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002031 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002033 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return -EFAULT;
2035
2036 pkt_sk(sk)->copy_thresh = val;
2037 return 0;
2038 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002039 case PACKET_VERSION:
2040 {
2041 int val;
2042
2043 if (optlen != sizeof(val))
2044 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002045 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002046 return -EBUSY;
2047 if (copy_from_user(&val, optval, sizeof(val)))
2048 return -EFAULT;
2049 switch (val) {
2050 case TPACKET_V1:
2051 case TPACKET_V2:
2052 po->tp_version = val;
2053 return 0;
2054 default:
2055 return -EINVAL;
2056 }
2057 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07002058 case PACKET_RESERVE:
2059 {
2060 unsigned int val;
2061
2062 if (optlen != sizeof(val))
2063 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002064 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardy8913336a2008-07-18 18:05:19 -07002065 return -EBUSY;
2066 if (copy_from_user(&val, optval, sizeof(val)))
2067 return -EFAULT;
2068 po->tp_reserve = val;
2069 return 0;
2070 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002071 case PACKET_LOSS:
2072 {
2073 unsigned int val;
2074
2075 if (optlen != sizeof(val))
2076 return -EINVAL;
2077 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2078 return -EBUSY;
2079 if (copy_from_user(&val, optval, sizeof(val)))
2080 return -EFAULT;
2081 po->tp_loss = !!val;
2082 return 0;
2083 }
Herbert Xu8dc41942007-02-04 23:31:32 -08002084 case PACKET_AUXDATA:
2085 {
2086 int val;
2087
2088 if (optlen < sizeof(val))
2089 return -EINVAL;
2090 if (copy_from_user(&val, optval, sizeof(val)))
2091 return -EFAULT;
2092
2093 po->auxdata = !!val;
2094 return 0;
2095 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002096 case PACKET_ORIGDEV:
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->origdev = !!val;
2106 return 0;
2107 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002108 case PACKET_VNET_HDR:
2109 {
2110 int val;
2111
2112 if (sock->type != SOCK_RAW)
2113 return -EINVAL;
2114 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2115 return -EBUSY;
2116 if (optlen < sizeof(val))
2117 return -EINVAL;
2118 if (copy_from_user(&val, optval, sizeof(val)))
2119 return -EFAULT;
2120
2121 po->has_vnet_hdr = !!val;
2122 return 0;
2123 }
Scott McMillan614f60f2010-06-02 05:53:56 -07002124 case PACKET_TIMESTAMP:
2125 {
2126 int val;
2127
2128 if (optlen != sizeof(val))
2129 return -EINVAL;
2130 if (copy_from_user(&val, optval, sizeof(val)))
2131 return -EFAULT;
2132
2133 po->tp_tstamp = val;
2134 return 0;
2135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 default:
2137 return -ENOPROTOOPT;
2138 }
2139}
2140
2141static int packet_getsockopt(struct socket *sock, int level, int optname,
2142 char __user *optval, int __user *optlen)
2143{
2144 int len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002145 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 struct sock *sk = sock->sk;
2147 struct packet_sock *po = pkt_sk(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08002148 void *data;
2149 struct tpacket_stats st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 if (level != SOL_PACKET)
2152 return -ENOPROTOOPT;
2153
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002154 if (get_user(len, optlen))
2155 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 if (len < 0)
2158 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002159
Johann Baudy69e3c752009-05-18 22:11:22 -07002160 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (len > sizeof(struct tpacket_stats))
2163 len = sizeof(struct tpacket_stats);
2164 spin_lock_bh(&sk->sk_receive_queue.lock);
2165 st = po->stats;
2166 memset(&po->stats, 0, sizeof(st));
2167 spin_unlock_bh(&sk->sk_receive_queue.lock);
2168 st.tp_packets += st.tp_drops;
2169
Herbert Xu8dc41942007-02-04 23:31:32 -08002170 data = &st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08002172 case PACKET_AUXDATA:
2173 if (len > sizeof(int))
2174 len = sizeof(int);
2175 val = po->auxdata;
2176
2177 data = &val;
2178 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002179 case PACKET_ORIGDEV:
2180 if (len > sizeof(int))
2181 len = sizeof(int);
2182 val = po->origdev;
2183
2184 data = &val;
2185 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002186 case PACKET_VNET_HDR:
2187 if (len > sizeof(int))
2188 len = sizeof(int);
2189 val = po->has_vnet_hdr;
2190
2191 data = &val;
2192 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002193 case PACKET_VERSION:
2194 if (len > sizeof(int))
2195 len = sizeof(int);
2196 val = po->tp_version;
2197 data = &val;
2198 break;
2199 case PACKET_HDRLEN:
2200 if (len > sizeof(int))
2201 len = sizeof(int);
2202 if (copy_from_user(&val, optval, len))
2203 return -EFAULT;
2204 switch (val) {
2205 case TPACKET_V1:
2206 val = sizeof(struct tpacket_hdr);
2207 break;
2208 case TPACKET_V2:
2209 val = sizeof(struct tpacket2_hdr);
2210 break;
2211 default:
2212 return -EINVAL;
2213 }
2214 data = &val;
2215 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07002216 case PACKET_RESERVE:
2217 if (len > sizeof(unsigned int))
2218 len = sizeof(unsigned int);
2219 val = po->tp_reserve;
2220 data = &val;
2221 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07002222 case PACKET_LOSS:
2223 if (len > sizeof(unsigned int))
2224 len = sizeof(unsigned int);
2225 val = po->tp_loss;
2226 data = &val;
2227 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07002228 case PACKET_TIMESTAMP:
2229 if (len > sizeof(int))
2230 len = sizeof(int);
2231 val = po->tp_tstamp;
2232 data = &val;
2233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 default:
2235 return -ENOPROTOOPT;
2236 }
2237
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002238 if (put_user(len, optlen))
2239 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08002240 if (copy_to_user(optval, data, len))
2241 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
2244
2245
2246static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
2247{
2248 struct sock *sk;
2249 struct hlist_node *node;
Jason Lunzad930652007-02-20 23:19:54 -08002250 struct net_device *dev = data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002251 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
stephen hemminger808f5112010-02-22 07:57:18 +00002253 rcu_read_lock();
2254 sk_for_each_rcu(sk, node, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 struct packet_sock *po = pkt_sk(sk);
2256
2257 switch (msg) {
2258 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (po->mclist)
2260 packet_dev_mclist(dev, po->mclist, -1);
David S. Millera2efcfa2007-05-29 13:12:50 -07002261 /* fallthrough */
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 case NETDEV_DOWN:
2264 if (dev->ifindex == po->ifindex) {
2265 spin_lock(&po->bind_lock);
2266 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07002267 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 sk->sk_err = ENETDOWN;
2269 if (!sock_flag(sk, SOCK_DEAD))
2270 sk->sk_error_report(sk);
2271 }
2272 if (msg == NETDEV_UNREGISTER) {
2273 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00002274 if (po->prot_hook.dev)
2275 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 po->prot_hook.dev = NULL;
2277 }
2278 spin_unlock(&po->bind_lock);
2279 }
2280 break;
2281 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00002282 if (dev->ifindex == po->ifindex) {
2283 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002284 if (po->num)
2285 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00002286 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 break;
2289 }
2290 }
stephen hemminger808f5112010-02-22 07:57:18 +00002291 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return NOTIFY_DONE;
2293}
2294
2295
2296static int packet_ioctl(struct socket *sock, unsigned int cmd,
2297 unsigned long arg)
2298{
2299 struct sock *sk = sock->sk;
2300
Johann Baudy69e3c752009-05-18 22:11:22 -07002301 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002302 case SIOCOUTQ:
2303 {
2304 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07002305
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002306 return put_user(amount, (int __user *)arg);
2307 }
2308 case SIOCINQ:
2309 {
2310 struct sk_buff *skb;
2311 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002313 spin_lock_bh(&sk->sk_receive_queue.lock);
2314 skb = skb_peek(&sk->sk_receive_queue);
2315 if (skb)
2316 amount = skb->len;
2317 spin_unlock_bh(&sk->sk_receive_queue.lock);
2318 return put_user(amount, (int __user *)arg);
2319 }
2320 case SIOCGSTAMP:
2321 return sock_get_timestamp(sk, (struct timeval __user *)arg);
2322 case SIOCGSTAMPNS:
2323 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002326 case SIOCADDRT:
2327 case SIOCDELRT:
2328 case SIOCDARP:
2329 case SIOCGARP:
2330 case SIOCSARP:
2331 case SIOCGIFADDR:
2332 case SIOCSIFADDR:
2333 case SIOCGIFBRDADDR:
2334 case SIOCSIFBRDADDR:
2335 case SIOCGIFNETMASK:
2336 case SIOCSIFNETMASK:
2337 case SIOCGIFDSTADDR:
2338 case SIOCSIFDSTADDR:
2339 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002340 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341#endif
2342
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002343 default:
2344 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
2346 return 0;
2347}
2348
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002349static unsigned int packet_poll(struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 poll_table *wait)
2351{
2352 struct sock *sk = sock->sk;
2353 struct packet_sock *po = pkt_sk(sk);
2354 unsigned int mask = datagram_poll(file, sock, wait);
2355
2356 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002357 if (po->rx_ring.pg_vec) {
2358 if (!packet_previous_frame(po, &po->rx_ring, TP_STATUS_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 mask |= POLLIN | POLLRDNORM;
2360 }
2361 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002362 spin_lock_bh(&sk->sk_write_queue.lock);
2363 if (po->tx_ring.pg_vec) {
2364 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
2365 mask |= POLLOUT | POLLWRNORM;
2366 }
2367 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return mask;
2369}
2370
2371
2372/* Dirty? Well, I still did not learn better way to account
2373 * for user mmaps.
2374 */
2375
2376static void packet_mm_open(struct vm_area_struct *vma)
2377{
2378 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002379 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (sk)
2383 atomic_inc(&pkt_sk(sk)->mapped);
2384}
2385
2386static void packet_mm_close(struct vm_area_struct *vma)
2387{
2388 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002389 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (sk)
2393 atomic_dec(&pkt_sk(sk)->mapped);
2394}
2395
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002396static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002397 .open = packet_mm_open,
2398 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399};
2400
Neil Horman0e3125c2010-11-16 10:26:47 -08002401static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
2402 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
2404 int i;
2405
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002406 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002407 if (likely(pg_vec[i].buffer)) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002408 if (is_vmalloc_addr(pg_vec[i].buffer))
Neil Horman0e3125c2010-11-16 10:26:47 -08002409 vfree(pg_vec[i].buffer);
2410 else
2411 free_pages((unsigned long)pg_vec[i].buffer,
2412 order);
2413 pg_vec[i].buffer = NULL;
2414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
2416 kfree(pg_vec);
2417}
2418
Changli Gaoc56b4d92010-12-01 02:52:57 +00002419static inline char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002420{
Neil Horman0e3125c2010-11-16 10:26:47 -08002421 char *buffer = NULL;
2422 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
2423 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07002424
Neil Horman0e3125c2010-11-16 10:26:47 -08002425 buffer = (char *) __get_free_pages(gfp_flags, order);
2426
2427 if (buffer)
2428 return buffer;
2429
2430 /*
2431 * __get_free_pages failed, fall back to vmalloc
2432 */
Eric Dumazetbbce5a52010-11-20 07:31:54 +00002433 buffer = vzalloc((1 << order) * PAGE_SIZE);
Neil Horman0e3125c2010-11-16 10:26:47 -08002434
2435 if (buffer)
2436 return buffer;
2437
2438 /*
2439 * vmalloc failed, lets dig into swap here
2440 */
Neil Horman0e3125c2010-11-16 10:26:47 -08002441 gfp_flags &= ~__GFP_NORETRY;
2442 buffer = (char *)__get_free_pages(gfp_flags, order);
2443 if (buffer)
2444 return buffer;
2445
2446 /*
2447 * complete and utter failure
2448 */
2449 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002450}
2451
Neil Horman0e3125c2010-11-16 10:26:47 -08002452static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002453{
2454 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08002455 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002456 int i;
2457
Neil Horman0e3125c2010-11-16 10:26:47 -08002458 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002459 if (unlikely(!pg_vec))
2460 goto out;
2461
2462 for (i = 0; i < block_nr; i++) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002463 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08002464 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002465 goto out_free_pgvec;
2466 }
2467
2468out:
2469 return pg_vec;
2470
2471out_free_pgvec:
2472 free_pg_vec(pg_vec, order, block_nr);
2473 pg_vec = NULL;
2474 goto out;
2475}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Johann Baudy69e3c752009-05-18 22:11:22 -07002477static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
2478 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
Neil Horman0e3125c2010-11-16 10:26:47 -08002480 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08002482 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002483 struct packet_ring_buffer *rb;
2484 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08002485 __be16 num;
Johann Baudy69e3c752009-05-18 22:11:22 -07002486 int err;
2487
2488 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
2489 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
2490
2491 err = -EBUSY;
2492 if (!closing) {
2493 if (atomic_read(&po->mapped))
2494 goto out;
2495 if (atomic_read(&rb->pending))
2496 goto out;
2497 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 if (req->tp_block_nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07002501 err = -EBUSY;
2502 if (unlikely(rb->pg_vec))
2503 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002505 switch (po->tp_version) {
2506 case TPACKET_V1:
2507 po->tp_hdrlen = TPACKET_HDRLEN;
2508 break;
2509 case TPACKET_V2:
2510 po->tp_hdrlen = TPACKET2_HDRLEN;
2511 break;
2512 }
2513
Johann Baudy69e3c752009-05-18 22:11:22 -07002514 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002515 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07002516 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002517 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002518 goto out;
Patrick McHardy8913336a2008-07-18 18:05:19 -07002519 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
Johann Baudy69e3c752009-05-18 22:11:22 -07002520 po->tp_reserve))
2521 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002522 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002523 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Johann Baudy69e3c752009-05-18 22:11:22 -07002525 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
2526 if (unlikely(rb->frames_per_block <= 0))
2527 goto out;
2528 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
2529 req->tp_frame_nr))
2530 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002533 order = get_order(req->tp_block_size);
2534 pg_vec = alloc_pg_vec(req, order);
2535 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002537 }
2538 /* Done */
2539 else {
2540 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002541 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07002542 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544
2545 lock_sock(sk);
2546
2547 /* Detach socket from network */
2548 spin_lock(&po->bind_lock);
2549 was_running = po->running;
2550 num = po->num;
2551 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 po->num = 0;
David S. Millerce06b032011-07-04 01:44:29 -07002553 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
2555 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 synchronize_net();
2558
2559 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08002560 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (closing || atomic_read(&po->mapped) == 0) {
2562 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002563 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08002564 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07002565 rb->frame_max = (req->tp_frame_nr - 1);
2566 rb->head = 0;
2567 rb->frame_size = req->tp_frame_size;
2568 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Changli Gaoc053fd92010-12-10 16:02:20 -08002570 swap(rb->pg_vec_order, order);
2571 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Johann Baudy69e3c752009-05-18 22:11:22 -07002573 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
2574 po->prot_hook.func = (po->rx_ring.pg_vec) ?
2575 tpacket_rcv : packet_rcv;
2576 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002578 pr_err("packet_mmap: vma is busy: %d\n",
2579 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
Herbert Xu905db442009-01-30 14:12:06 -08002581 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002584 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 po->num = num;
David S. Millerce06b032011-07-04 01:44:29 -07002586 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
2588 spin_unlock(&po->bind_lock);
2589
2590 release_sock(sk);
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 if (pg_vec)
2593 free_pg_vec(pg_vec, order, req->tp_block_nr);
2594out:
2595 return err;
2596}
2597
Johann Baudy69e3c752009-05-18 22:11:22 -07002598static int packet_mmap(struct file *file, struct socket *sock,
2599 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
2601 struct sock *sk = sock->sk;
2602 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002603 unsigned long size, expected_size;
2604 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 unsigned long start;
2606 int err = -EINVAL;
2607 int i;
2608
2609 if (vma->vm_pgoff)
2610 return -EINVAL;
2611
Herbert Xu905db442009-01-30 14:12:06 -08002612 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002613
2614 expected_size = 0;
2615 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2616 if (rb->pg_vec) {
2617 expected_size += rb->pg_vec_len
2618 * rb->pg_vec_pages
2619 * PAGE_SIZE;
2620 }
2621 }
2622
2623 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002625
2626 size = vma->vm_end - vma->vm_start;
2627 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 goto out;
2629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07002631 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2632 if (rb->pg_vec == NULL)
2633 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002634
Johann Baudy69e3c752009-05-18 22:11:22 -07002635 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002636 struct page *page;
2637 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07002638 int pg_num;
2639
Changli Gaoc56b4d92010-12-01 02:52:57 +00002640 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
2641 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07002642 err = vm_insert_page(vma, start, page);
2643 if (unlikely(err))
2644 goto out;
2645 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08002646 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002647 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002650
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002651 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 vma->vm_ops = &packet_mmap_ops;
2653 err = 0;
2654
2655out:
Herbert Xu905db442009-01-30 14:12:06 -08002656 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 return err;
2658}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002660static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 .family = PF_PACKET,
2662 .owner = THIS_MODULE,
2663 .release = packet_release,
2664 .bind = packet_bind_spkt,
2665 .connect = sock_no_connect,
2666 .socketpair = sock_no_socketpair,
2667 .accept = sock_no_accept,
2668 .getname = packet_getname_spkt,
2669 .poll = datagram_poll,
2670 .ioctl = packet_ioctl,
2671 .listen = sock_no_listen,
2672 .shutdown = sock_no_shutdown,
2673 .setsockopt = sock_no_setsockopt,
2674 .getsockopt = sock_no_getsockopt,
2675 .sendmsg = packet_sendmsg_spkt,
2676 .recvmsg = packet_recvmsg,
2677 .mmap = sock_no_mmap,
2678 .sendpage = sock_no_sendpage,
2679};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002681static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 .family = PF_PACKET,
2683 .owner = THIS_MODULE,
2684 .release = packet_release,
2685 .bind = packet_bind,
2686 .connect = sock_no_connect,
2687 .socketpair = sock_no_socketpair,
2688 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002689 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 .poll = packet_poll,
2691 .ioctl = packet_ioctl,
2692 .listen = sock_no_listen,
2693 .shutdown = sock_no_shutdown,
2694 .setsockopt = packet_setsockopt,
2695 .getsockopt = packet_getsockopt,
2696 .sendmsg = packet_sendmsg,
2697 .recvmsg = packet_recvmsg,
2698 .mmap = packet_mmap,
2699 .sendpage = sock_no_sendpage,
2700};
2701
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002702static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 .family = PF_PACKET,
2704 .create = packet_create,
2705 .owner = THIS_MODULE,
2706};
2707
2708static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002709 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710};
2711
2712#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00002715 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
Denis V. Luneve372c412007-11-19 22:31:54 -08002717 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00002718
2719 rcu_read_lock();
2720 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
2723static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2724{
Herbert Xu1bf40952007-12-16 14:04:02 -08002725 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00002726 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
2729static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00002730 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
stephen hemminger808f5112010-02-22 07:57:18 +00002732 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733}
2734
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002735static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
2737 if (v == SEQ_START_TOKEN)
2738 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
2739 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00002740 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 const struct packet_sock *po = pkt_sk(s);
2742
2743 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00002744 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 s,
2746 atomic_read(&s->sk_refcnt),
2747 s->sk_type,
2748 ntohs(po->num),
2749 po->ifindex,
2750 po->running,
2751 atomic_read(&s->sk_rmem_alloc),
2752 sock_i_uid(s),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002753 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 }
2755
2756 return 0;
2757}
2758
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002759static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 .start = packet_seq_start,
2761 .next = packet_seq_next,
2762 .stop = packet_seq_stop,
2763 .show = packet_seq_show,
2764};
2765
2766static int packet_seq_open(struct inode *inode, struct file *file)
2767{
Denis V. Luneve372c412007-11-19 22:31:54 -08002768 return seq_open_net(inode, file, &packet_seq_ops,
2769 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770}
2771
Arjan van de Venda7071d2007-02-12 00:55:36 -08002772static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 .owner = THIS_MODULE,
2774 .open = packet_seq_open,
2775 .read = seq_read,
2776 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002777 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778};
2779
2780#endif
2781
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002782static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002783{
stephen hemminger808f5112010-02-22 07:57:18 +00002784 spin_lock_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08002785 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002786
2787 if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
2788 return -ENOMEM;
2789
2790 return 0;
2791}
2792
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002793static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002794{
2795 proc_net_remove(net, "packet");
2796}
2797
2798static struct pernet_operations packet_net_ops = {
2799 .init = packet_net_init,
2800 .exit = packet_net_exit,
2801};
2802
2803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804static void __exit packet_exit(void)
2805{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002807 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 sock_unregister(PF_PACKET);
2809 proto_unregister(&packet_proto);
2810}
2811
2812static int __init packet_init(void)
2813{
2814 int rc = proto_register(&packet_proto, 0);
2815
2816 if (rc != 0)
2817 goto out;
2818
2819 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002820 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822out:
2823 return rc;
2824}
2825
2826module_init(packet_init);
2827module_exit(packet_exit);
2828MODULE_LICENSE("GPL");
2829MODULE_ALIAS_NETPROTO(PF_PACKET);