blob: 5efef5b5879ebac8870f3babd03c66cb1817fc66 [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 Dumazet94b05952009-10-16 04:02:20 +0000212 struct packet_type prot_hook ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
214
Herbert Xuffbc6112007-02-04 23:33:10 -0800215struct packet_skb_cb {
216 unsigned int origlen;
217 union {
218 struct sockaddr_pkt pkt;
219 struct sockaddr_ll ll;
220 } sa;
221};
222
223#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800224
Changli Gaof6dafa92010-12-07 04:26:16 +0000225static inline __pure struct page *pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000226{
227 if (is_vmalloc_addr(addr))
228 return vmalloc_to_page(addr);
229 return virt_to_page(addr);
230}
231
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700232static void __packet_set_status(struct packet_sock *po, void *frame, int status)
233{
234 union {
235 struct tpacket_hdr *h1;
236 struct tpacket2_hdr *h2;
237 void *raw;
238 } h;
239
240 h.raw = frame;
241 switch (po->tp_version) {
242 case TPACKET_V1:
243 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000244 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700245 break;
246 case TPACKET_V2:
247 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000248 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700249 break;
Johann Baudy69e3c752009-05-18 22:11:22 -0700250 default:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000251 pr_err("TPACKET version not supported\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700252 BUG();
253 }
254
255 smp_wmb();
256}
257
258static int __packet_get_status(struct packet_sock *po, void *frame)
259{
260 union {
261 struct tpacket_hdr *h1;
262 struct tpacket2_hdr *h2;
263 void *raw;
264 } h;
265
266 smp_rmb();
267
268 h.raw = frame;
269 switch (po->tp_version) {
270 case TPACKET_V1:
Changli Gao0af55bb2010-12-01 02:52:20 +0000271 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700272 return h.h1->tp_status;
273 case TPACKET_V2:
Changli Gao0af55bb2010-12-01 02:52:20 +0000274 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700275 return h.h2->tp_status;
276 default:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000277 pr_err("TPACKET version not supported\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700278 BUG();
279 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
Johann Baudy69e3c752009-05-18 22:11:22 -0700282
283static void *packet_lookup_frame(struct packet_sock *po,
284 struct packet_ring_buffer *rb,
285 unsigned int position,
286 int status)
287{
288 unsigned int pg_vec_pos, frame_offset;
289 union {
290 struct tpacket_hdr *h1;
291 struct tpacket2_hdr *h2;
292 void *raw;
293 } h;
294
295 pg_vec_pos = position / rb->frames_per_block;
296 frame_offset = position % rb->frames_per_block;
297
Neil Horman0e3125c2010-11-16 10:26:47 -0800298 h.raw = rb->pg_vec[pg_vec_pos].buffer +
299 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700300
301 if (status != __packet_get_status(po, h.raw))
302 return NULL;
303
304 return h.raw;
305}
306
307static inline void *packet_current_frame(struct packet_sock *po,
308 struct packet_ring_buffer *rb,
309 int status)
310{
311 return packet_lookup_frame(po, rb, rb->head, status);
312}
313
314static inline void *packet_previous_frame(struct packet_sock *po,
315 struct packet_ring_buffer *rb,
316 int status)
317{
318 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
319 return packet_lookup_frame(po, rb, previous, status);
320}
321
322static inline void packet_increment_head(struct packet_ring_buffer *buff)
323{
324 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static inline struct packet_sock *pkt_sk(struct sock *sk)
328{
329 return (struct packet_sock *)sk;
330}
331
332static void packet_sock_destruct(struct sock *sk)
333{
Richard Cochraned85b562010-04-07 22:41:28 +0000334 skb_queue_purge(&sk->sk_error_queue);
335
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700336 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
337 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000340 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return;
342 }
343
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -0800344 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800348static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800350static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000352static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
353 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct sock *sk;
356 struct sockaddr_pkt *spkt;
357
358 /*
359 * When we registered the protocol we saved the socket in the data
360 * field for just this event.
361 */
362
363 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /*
366 * Yank back the headers [hope the device set this
367 * right or kerboom...]
368 *
369 * Incoming packets have ll header pulled,
370 * push it back.
371 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700372 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * so that this procedure is noop.
374 */
375
376 if (skb->pkt_type == PACKET_LOOPBACK)
377 goto out;
378
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800379 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800380 goto out;
381
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000382 skb = skb_share_check(skb, GFP_ATOMIC);
383 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 goto oom;
385
386 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +0000387 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Phil Oester84531c22005-07-12 11:57:52 -0700389 /* drop conntrack reference */
390 nf_reset(skb);
391
Herbert Xuffbc6112007-02-04 23:33:10 -0800392 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700394 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /*
397 * The SOCK_PACKET socket receives _all_ frames.
398 */
399
400 spkt->spkt_family = dev->type;
401 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
402 spkt->spkt_protocol = skb->protocol;
403
404 /*
405 * Charge the memory to the socket. This is done specifically
406 * to prevent sockets using all the memory up.
407 */
408
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000409 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411
412out:
413 kfree_skb(skb);
414oom:
415 return 0;
416}
417
418
419/*
420 * Output a raw packet to a device layer. This bypasses all the other
421 * protocol layers and you must therefore supply it with a complete frame
422 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
425 struct msghdr *msg, size_t len)
426{
427 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000428 struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000429 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct net_device *dev;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000431 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 int err;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900435 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
437
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000438 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000440 return -EINVAL;
441 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
442 proto = saddr->spkt_protocol;
443 } else
444 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900447 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
449
450 saddr->spkt_device[13] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000451retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100452 rcu_read_lock();
453 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 err = -ENODEV;
455 if (dev == NULL)
456 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900457
David S. Millerd5e76b02007-01-25 19:30:36 -0800458 err = -ENETDOWN;
459 if (!(dev->flags & IFF_UP))
460 goto out_unlock;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000463 * You may not queue a frame bigger than the mtu. This is the lowest level
464 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 err = -EMSGSIZE;
Ben Greear57f89bf2011-02-11 09:35:18 +0000468 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto out_unlock;
470
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000471 if (!skb) {
472 size_t reserved = LL_RESERVED_SPACE(dev);
473 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000475 rcu_read_unlock();
476 skb = sock_wmalloc(sk, len + reserved, 0, GFP_KERNEL);
477 if (skb == NULL)
478 return -ENOBUFS;
479 /* FIXME: Save some space for broken drivers that write a hard
480 * header at transmission time by themselves. PPP is the notable
481 * one here. This should really be fixed at the driver level.
482 */
483 skb_reserve(skb, reserved);
484 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900485
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000486 /* Try to align data part correctly */
487 if (hhlen) {
488 skb->data -= hhlen;
489 skb->tail -= hhlen;
490 if (len < hhlen)
491 skb_reset_network_header(skb);
492 }
493 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
494 if (err)
495 goto out_free;
496 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
Ben Greear57f89bf2011-02-11 09:35:18 +0000499 if (len > (dev->mtu + dev->hard_header_len)) {
500 /* Earlier code assumed this would be a VLAN pkt,
501 * double-check this now that we have the actual
502 * packet in hand.
503 */
504 struct ethhdr *ehdr;
505 skb_reset_mac_header(skb);
506 ehdr = eth_hdr(skb);
507 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
508 err = -EMSGSIZE;
509 goto out_unlock;
510 }
511 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 skb->protocol = proto;
514 skb->dev = dev;
515 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +0000516 skb->mark = sk->sk_mark;
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000517 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +0000518 if (err < 0)
519 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +0100522 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000523 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100526 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000527out_free:
528 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return err;
530}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Eric Dumazet62ab0812010-12-06 20:50:09 +0000532static inline unsigned int run_filter(const struct sk_buff *skb,
533 const struct sock *sk,
David S. Millerdbcb5852007-01-24 15:21:02 -0800534 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct sk_filter *filter;
537
Eric Dumazet80f8f102011-01-18 07:46:52 +0000538 rcu_read_lock();
539 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -0800540 if (filter != NULL)
Eric Dumazet93aaae22010-11-19 09:49:59 -0800541 res = sk_run_filter(skb, filter->insns);
Eric Dumazet80f8f102011-01-18 07:46:52 +0000542 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
David S. Millerdbcb5852007-01-24 15:21:02 -0800544 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547/*
Eric Dumazet62ab0812010-12-06 20:50:09 +0000548 * This function makes lazy skb cloning in hope that most of packets
549 * are discarded by BPF.
550 *
551 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
552 * and skb->cb are mangled. It works because (and until) packets
553 * falling here are owned by current CPU. Output packets are cloned
554 * by dev_queue_xmit_nit(), input packets are processed by net_bh
555 * sequencially, so that if we return skb to original state on exit,
556 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
558
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000559static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
560 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct sock *sk;
563 struct sockaddr_ll *sll;
564 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000565 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800567 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 if (skb->pkt_type == PACKET_LOOPBACK)
570 goto drop;
571
572 sk = pt->af_packet_priv;
573 po = pkt_sk(sk);
574
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800575 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800576 goto drop;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 skb->dev = dev;
579
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700580 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +0000582 * exported to higher levels.
583 *
584 * Otherwise, the device hides details of its frame
585 * structure, so that corresponding packet head is
586 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
588 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700589 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 else if (skb->pkt_type == PACKET_OUTGOING) {
591 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300592 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594 }
595
596 snaplen = skb->len;
597
David S. Millerdbcb5852007-01-24 15:21:02 -0800598 res = run_filter(skb, sk, snaplen);
599 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700600 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800601 if (snaplen > res)
602 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
605 (unsigned)sk->sk_rcvbuf)
606 goto drop_n_acct;
607
608 if (skb_shared(skb)) {
609 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
610 if (nskb == NULL)
611 goto drop_n_acct;
612
613 if (skb_head != skb->data) {
614 skb->data = skb_head;
615 skb->len = skb_len;
616 }
617 kfree_skb(skb);
618 skb = nskb;
619 }
620
Herbert Xuffbc6112007-02-04 23:33:10 -0800621 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
622 sizeof(skb->cb));
623
624 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 sll->sll_family = AF_PACKET;
626 sll->sll_hatype = dev->type;
627 sll->sll_protocol = skb->protocol;
628 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800629 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700630 sll->sll_ifindex = orig_dev->ifindex;
631 else
632 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700634 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Herbert Xuffbc6112007-02-04 23:33:10 -0800636 PACKET_SKB_CB(skb)->origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -0800637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (pskb_trim(skb, snaplen))
639 goto drop_n_acct;
640
641 skb_set_owner_r(skb, sk);
642 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +0000643 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Phil Oester84531c22005-07-12 11:57:52 -0700645 /* drop conntrack reference */
646 nf_reset(skb);
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 spin_lock(&sk->sk_receive_queue.lock);
649 po->stats.tp_packets++;
Neil Horman3b885782009-10-12 13:26:31 -0700650 skb->dropcount = atomic_read(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 __skb_queue_tail(&sk->sk_receive_queue, skb);
652 spin_unlock(&sk->sk_receive_queue.lock);
653 sk->sk_data_ready(sk, skb->len);
654 return 0;
655
656drop_n_acct:
Neil Horman3b885782009-10-12 13:26:31 -0700657 po->stats.tp_drops = atomic_inc_return(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659drop_n_restore:
660 if (skb_head != skb->data && skb_shared(skb)) {
661 skb->data = skb_head;
662 skb->len = skb_len;
663 }
664drop:
Neil Hormanead2ceb2009-03-11 09:49:55 +0000665 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667}
668
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000669static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
670 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 struct sock *sk;
673 struct packet_sock *po;
674 struct sockaddr_ll *sll;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700675 union {
676 struct tpacket_hdr *h1;
677 struct tpacket2_hdr *h2;
678 void *raw;
679 } h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000680 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800682 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700684 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct sk_buff *copy_skb = NULL;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700686 struct timeval tv;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700687 struct timespec ts;
Scott McMillan614f60f2010-06-02 05:53:56 -0700688 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (skb->pkt_type == PACKET_LOOPBACK)
691 goto drop;
692
693 sk = pt->af_packet_priv;
694 po = pkt_sk(sk);
695
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800696 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800697 goto drop;
698
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700699 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700701 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 else if (skb->pkt_type == PACKET_OUTGOING) {
703 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300704 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 }
707
Herbert Xu8dc41942007-02-04 23:31:32 -0800708 if (skb->ip_summed == CHECKSUM_PARTIAL)
709 status |= TP_STATUS_CSUMNOTREADY;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 snaplen = skb->len;
712
David S. Millerdbcb5852007-01-24 15:21:02 -0800713 res = run_filter(skb, sk, snaplen);
714 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700715 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800716 if (snaplen > res)
717 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -0700720 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
721 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 } else {
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300723 unsigned maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700724 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -0700725 (maclen < 16 ? 16 : maclen)) +
726 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 macoff = netoff - maclen;
728 }
729
Johann Baudy69e3c752009-05-18 22:11:22 -0700730 if (macoff + snaplen > po->rx_ring.frame_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (po->copy_thresh &&
732 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
733 (unsigned)sk->sk_rcvbuf) {
734 if (skb_shared(skb)) {
735 copy_skb = skb_clone(skb, GFP_ATOMIC);
736 } else {
737 copy_skb = skb_get(skb);
738 skb_head = skb->data;
739 }
740 if (copy_skb)
741 skb_set_owner_r(copy_skb, sk);
742 }
Johann Baudy69e3c752009-05-18 22:11:22 -0700743 snaplen = po->rx_ring.frame_size - macoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if ((int)snaplen < 0)
745 snaplen = 0;
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 spin_lock(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -0700749 h.raw = packet_current_frame(po, &po->rx_ring, TP_STATUS_KERNEL);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700750 if (!h.raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 goto ring_is_full;
Johann Baudy69e3c752009-05-18 22:11:22 -0700752 packet_increment_head(&po->rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 po->stats.tp_packets++;
754 if (copy_skb) {
755 status |= TP_STATUS_COPY;
756 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
757 }
758 if (!po->stats.tp_drops)
759 status &= ~TP_STATUS_LOSING;
760 spin_unlock(&sk->sk_receive_queue.lock);
761
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700762 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700764 switch (po->tp_version) {
765 case TPACKET_V1:
766 h.h1->tp_len = skb->len;
767 h.h1->tp_snaplen = snaplen;
768 h.h1->tp_mac = macoff;
769 h.h1->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -0700770 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
771 && shhwtstamps->syststamp.tv64)
772 tv = ktime_to_timeval(shhwtstamps->syststamp);
773 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
774 && shhwtstamps->hwtstamp.tv64)
775 tv = ktime_to_timeval(shhwtstamps->hwtstamp);
776 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700777 tv = ktime_to_timeval(skb->tstamp);
778 else
779 do_gettimeofday(&tv);
780 h.h1->tp_sec = tv.tv_sec;
781 h.h1->tp_usec = tv.tv_usec;
782 hdrlen = sizeof(*h.h1);
783 break;
784 case TPACKET_V2:
785 h.h2->tp_len = skb->len;
786 h.h2->tp_snaplen = snaplen;
787 h.h2->tp_mac = macoff;
788 h.h2->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -0700789 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
790 && shhwtstamps->syststamp.tv64)
791 ts = ktime_to_timespec(shhwtstamps->syststamp);
792 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
793 && shhwtstamps->hwtstamp.tv64)
794 ts = ktime_to_timespec(shhwtstamps->hwtstamp);
795 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700796 ts = ktime_to_timespec(skb->tstamp);
797 else
798 getnstimeofday(&ts);
799 h.h2->tp_sec = ts.tv_sec;
800 h.h2->tp_nsec = ts.tv_nsec;
Eric Dumazet05423b22009-10-26 18:40:35 -0700801 h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700802 hdrlen = sizeof(*h.h2);
803 break;
804 default:
805 BUG();
806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700808 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700809 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 sll->sll_family = AF_PACKET;
811 sll->sll_hatype = dev->type;
812 sll->sll_protocol = skb->protocol;
813 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800814 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700815 sll->sll_ifindex = orig_dev->ifindex;
816 else
817 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700819 __packet_set_status(po, h.raw, status);
Ralf Baechlee16aa202006-12-07 00:11:33 -0800820 smp_mb();
Changli Gaof6dafa92010-12-07 04:26:16 +0000821#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 {
Changli Gao0af55bb2010-12-01 02:52:20 +0000823 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Changli Gao0af55bb2010-12-01 02:52:20 +0000825 end = (u8 *)PAGE_ALIGN((unsigned long)h.raw + macoff + snaplen);
826 for (start = h.raw; start < end; start += PAGE_SIZE)
827 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
Changli Gaof6dafa92010-12-07 04:26:16 +0000829#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 sk->sk_data_ready(sk, 0);
832
833drop_n_restore:
834 if (skb_head != skb->data && skb_shared(skb)) {
835 skb->data = skb_head;
836 skb->len = skb_len;
837 }
838drop:
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900839 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 0;
841
842ring_is_full:
843 po->stats.tp_drops++;
844 spin_unlock(&sk->sk_receive_queue.lock);
845
846 sk->sk_data_ready(sk, 0);
Wei Yongjunacb5d752009-02-25 00:36:42 +0000847 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 goto drop_n_restore;
849}
850
Johann Baudy69e3c752009-05-18 22:11:22 -0700851static void tpacket_destruct_skb(struct sk_buff *skb)
852{
853 struct packet_sock *po = pkt_sk(skb->sk);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000854 void *ph;
Johann Baudy69e3c752009-05-18 22:11:22 -0700855
856 BUG_ON(skb == NULL);
857
858 if (likely(po->tx_ring.pg_vec)) {
859 ph = skb_shinfo(skb)->destructor_arg;
860 BUG_ON(__packet_get_status(po, ph) != TP_STATUS_SENDING);
861 BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
862 atomic_dec(&po->tx_ring.pending);
863 __packet_set_status(po, ph, TP_STATUS_AVAILABLE);
864 }
865
866 sock_wfree(skb);
867}
868
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000869static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
870 void *frame, struct net_device *dev, int size_max,
871 __be16 proto, unsigned char *addr)
Johann Baudy69e3c752009-05-18 22:11:22 -0700872{
873 union {
874 struct tpacket_hdr *h1;
875 struct tpacket2_hdr *h2;
876 void *raw;
877 } ph;
878 int to_write, offset, len, tp_len, nr_frags, len_max;
879 struct socket *sock = po->sk.sk_socket;
880 struct page *page;
881 void *data;
882 int err;
883
884 ph.raw = frame;
885
886 skb->protocol = proto;
887 skb->dev = dev;
888 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +0000889 skb->mark = po->sk.sk_mark;
Johann Baudy69e3c752009-05-18 22:11:22 -0700890 skb_shinfo(skb)->destructor_arg = ph.raw;
891
892 switch (po->tp_version) {
893 case TPACKET_V2:
894 tp_len = ph.h2->tp_len;
895 break;
896 default:
897 tp_len = ph.h1->tp_len;
898 break;
899 }
900 if (unlikely(tp_len > size_max)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000901 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
Johann Baudy69e3c752009-05-18 22:11:22 -0700902 return -EMSGSIZE;
903 }
904
905 skb_reserve(skb, LL_RESERVED_SPACE(dev));
906 skb_reset_network_header(skb);
907
908 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
909 to_write = tp_len;
910
911 if (sock->type == SOCK_DGRAM) {
912 err = dev_hard_header(skb, dev, ntohs(proto), addr,
913 NULL, tp_len);
914 if (unlikely(err < 0))
915 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000916 } else if (dev->hard_header_len) {
Johann Baudy69e3c752009-05-18 22:11:22 -0700917 /* net device doesn't like empty head */
918 if (unlikely(tp_len <= dev->hard_header_len)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000919 pr_err("packet size is too short (%d < %d)\n",
920 tp_len, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -0700921 return -EINVAL;
922 }
923
924 skb_push(skb, dev->hard_header_len);
925 err = skb_store_bits(skb, 0, data,
926 dev->hard_header_len);
927 if (unlikely(err))
928 return err;
929
930 data += dev->hard_header_len;
931 to_write -= dev->hard_header_len;
932 }
933
934 err = -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -0700935 offset = offset_in_page(data);
936 len_max = PAGE_SIZE - offset;
937 len = ((to_write > len_max) ? len_max : to_write);
938
939 skb->data_len = to_write;
940 skb->len += to_write;
941 skb->truesize += to_write;
942 atomic_add(to_write, &po->sk.sk_wmem_alloc);
943
944 while (likely(to_write)) {
945 nr_frags = skb_shinfo(skb)->nr_frags;
946
947 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000948 pr_err("Packet exceed the number of skb frags(%lu)\n",
949 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -0700950 return -EFAULT;
951 }
952
Changli Gao0af55bb2010-12-01 02:52:20 +0000953 page = pgv_to_page(data);
954 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -0700955 flush_dcache_page(page);
956 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +0000957 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -0700958 to_write -= len;
959 offset = 0;
960 len_max = PAGE_SIZE;
961 len = ((to_write > len_max) ? len_max : to_write);
962 }
963
964 return tp_len;
965}
966
967static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
968{
969 struct socket *sock;
970 struct sk_buff *skb;
971 struct net_device *dev;
972 __be16 proto;
973 int ifindex, err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000974 void *ph;
975 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Johann Baudy69e3c752009-05-18 22:11:22 -0700976 int tp_len, size_max;
977 unsigned char *addr;
978 int len_sum = 0;
979 int status = 0;
980
981 sock = po->sk.sk_socket;
982
983 mutex_lock(&po->pg_vec_lock);
984
985 err = -EBUSY;
986 if (saddr == NULL) {
987 ifindex = po->ifindex;
988 proto = po->num;
989 addr = NULL;
990 } else {
991 err = -EINVAL;
992 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
993 goto out;
994 if (msg->msg_namelen < (saddr->sll_halen
995 + offsetof(struct sockaddr_ll,
996 sll_addr)))
997 goto out;
998 ifindex = saddr->sll_ifindex;
999 proto = saddr->sll_protocol;
1000 addr = saddr->sll_addr;
1001 }
1002
1003 dev = dev_get_by_index(sock_net(&po->sk), ifindex);
1004 err = -ENXIO;
1005 if (unlikely(dev == NULL))
1006 goto out;
1007
1008 reserve = dev->hard_header_len;
1009
1010 err = -ENETDOWN;
1011 if (unlikely(!(dev->flags & IFF_UP)))
1012 goto out_put;
1013
1014 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07001015 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07001016
1017 if (size_max > dev->mtu + reserve)
1018 size_max = dev->mtu + reserve;
1019
1020 do {
1021 ph = packet_current_frame(po, &po->tx_ring,
1022 TP_STATUS_SEND_REQUEST);
1023
1024 if (unlikely(ph == NULL)) {
1025 schedule();
1026 continue;
1027 }
1028
1029 status = TP_STATUS_SEND_REQUEST;
1030 skb = sock_alloc_send_skb(&po->sk,
1031 LL_ALLOCATED_SPACE(dev)
1032 + sizeof(struct sockaddr_ll),
1033 0, &err);
1034
1035 if (unlikely(skb == NULL))
1036 goto out_status;
1037
1038 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
1039 addr);
1040
1041 if (unlikely(tp_len < 0)) {
1042 if (po->tp_loss) {
1043 __packet_set_status(po, ph,
1044 TP_STATUS_AVAILABLE);
1045 packet_increment_head(&po->tx_ring);
1046 kfree_skb(skb);
1047 continue;
1048 } else {
1049 status = TP_STATUS_WRONG_FORMAT;
1050 err = tp_len;
1051 goto out_status;
1052 }
1053 }
1054
1055 skb->destructor = tpacket_destruct_skb;
1056 __packet_set_status(po, ph, TP_STATUS_SENDING);
1057 atomic_inc(&po->tx_ring.pending);
1058
1059 status = TP_STATUS_SEND_REQUEST;
1060 err = dev_queue_xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00001061 if (unlikely(err > 0)) {
1062 err = net_xmit_errno(err);
1063 if (err && __packet_get_status(po, ph) ==
1064 TP_STATUS_AVAILABLE) {
1065 /* skb was destructed already */
1066 skb = NULL;
1067 goto out_status;
1068 }
1069 /*
1070 * skb was dropped but not destructed yet;
1071 * let's treat it like congestion or err < 0
1072 */
1073 err = 0;
1074 }
Johann Baudy69e3c752009-05-18 22:11:22 -07001075 packet_increment_head(&po->tx_ring);
1076 len_sum += tp_len;
Joe Perchesf64f9e72009-11-29 16:55:45 -08001077 } while (likely((ph != NULL) ||
1078 ((!(msg->msg_flags & MSG_DONTWAIT)) &&
1079 (atomic_read(&po->tx_ring.pending))))
1080 );
Johann Baudy69e3c752009-05-18 22:11:22 -07001081
1082 err = len_sum;
1083 goto out_put;
1084
Johann Baudy69e3c752009-05-18 22:11:22 -07001085out_status:
1086 __packet_set_status(po, ph, status);
1087 kfree_skb(skb);
1088out_put:
1089 dev_put(dev);
1090out:
1091 mutex_unlock(&po->pg_vec_lock);
1092 return err;
1093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001095static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
1096 size_t reserve, size_t len,
1097 size_t linear, int noblock,
1098 int *err)
1099{
1100 struct sk_buff *skb;
1101
1102 /* Under a page? Don't bother with paged skb. */
1103 if (prepad + len < PAGE_SIZE || !linear)
1104 linear = len;
1105
1106 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
1107 err);
1108 if (!skb)
1109 return NULL;
1110
1111 skb_reserve(skb, reserve);
1112 skb_put(skb, linear);
1113 skb->data_len = len - linear;
1114 skb->len += len - linear;
1115
1116 return skb;
1117}
1118
Johann Baudy69e3c752009-05-18 22:11:22 -07001119static int packet_snd(struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct msghdr *msg, size_t len)
1121{
1122 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001123 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 struct sk_buff *skb;
1125 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08001126 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 unsigned char *addr;
1128 int ifindex, err, reserve = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001129 struct virtio_net_hdr vnet_hdr = { 0 };
1130 int offset = 0;
1131 int vnet_hdr_len;
1132 struct packet_sock *po = pkt_sk(sk);
1133 unsigned short gso_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001136 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (saddr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 ifindex = po->ifindex;
1141 proto = po->num;
1142 addr = NULL;
1143 } else {
1144 err = -EINVAL;
1145 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
1146 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001147 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
1148 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 ifindex = saddr->sll_ifindex;
1150 proto = saddr->sll_protocol;
1151 addr = saddr->sll_addr;
1152 }
1153
1154
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001155 dev = dev_get_by_index(sock_net(sk), ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 err = -ENXIO;
1157 if (dev == NULL)
1158 goto out_unlock;
1159 if (sock->type == SOCK_RAW)
1160 reserve = dev->hard_header_len;
1161
David S. Millerd5e76b02007-01-25 19:30:36 -08001162 err = -ENETDOWN;
1163 if (!(dev->flags & IFF_UP))
1164 goto out_unlock;
1165
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001166 if (po->has_vnet_hdr) {
1167 vnet_hdr_len = sizeof(vnet_hdr);
1168
1169 err = -EINVAL;
1170 if (len < vnet_hdr_len)
1171 goto out_unlock;
1172
1173 len -= vnet_hdr_len;
1174
1175 err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
1176 vnet_hdr_len);
1177 if (err < 0)
1178 goto out_unlock;
1179
1180 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1181 (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
1182 vnet_hdr.hdr_len))
1183 vnet_hdr.hdr_len = vnet_hdr.csum_start +
1184 vnet_hdr.csum_offset + 2;
1185
1186 err = -EINVAL;
1187 if (vnet_hdr.hdr_len > len)
1188 goto out_unlock;
1189
1190 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1191 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1192 case VIRTIO_NET_HDR_GSO_TCPV4:
1193 gso_type = SKB_GSO_TCPV4;
1194 break;
1195 case VIRTIO_NET_HDR_GSO_TCPV6:
1196 gso_type = SKB_GSO_TCPV6;
1197 break;
1198 case VIRTIO_NET_HDR_GSO_UDP:
1199 gso_type = SKB_GSO_UDP;
1200 break;
1201 default:
1202 goto out_unlock;
1203 }
1204
1205 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1206 gso_type |= SKB_GSO_TCP_ECN;
1207
1208 if (vnet_hdr.gso_size == 0)
1209 goto out_unlock;
1210
1211 }
1212 }
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 err = -EMSGSIZE;
Ben Greear57f89bf2011-02-11 09:35:18 +00001215 if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 goto out_unlock;
1217
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001218 err = -ENOBUFS;
1219 skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev),
1220 LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len,
1221 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001222 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 goto out_unlock;
1224
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001225 skb_set_network_header(skb, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001227 err = -EINVAL;
1228 if (sock->type == SOCK_DGRAM &&
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001229 (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001230 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /* Returns -EFAULT on error */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001233 err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (err)
1235 goto out_free;
Oliver Hartkopp2244d072010-08-17 08:59:14 +00001236 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +00001237 if (err < 0)
1238 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Ben Greear57f89bf2011-02-11 09:35:18 +00001240 if (!gso_type && (len > dev->mtu + reserve)) {
1241 /* Earlier code assumed this would be a VLAN pkt,
1242 * double-check this now that we have the actual
1243 * packet in hand.
1244 */
1245 struct ethhdr *ehdr;
1246 skb_reset_mac_header(skb);
1247 ehdr = eth_hdr(skb);
1248 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
1249 err = -EMSGSIZE;
1250 goto out_free;
1251 }
1252 }
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 skb->protocol = proto;
1255 skb->dev = dev;
1256 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001257 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001259 if (po->has_vnet_hdr) {
1260 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1261 if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
1262 vnet_hdr.csum_offset)) {
1263 err = -EINVAL;
1264 goto out_free;
1265 }
1266 }
1267
1268 skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
1269 skb_shinfo(skb)->gso_type = gso_type;
1270
1271 /* Header must be checked, and gso_segs computed. */
1272 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1273 skb_shinfo(skb)->gso_segs = 0;
1274
1275 len += vnet_hdr_len;
1276 }
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /*
1279 * Now send it
1280 */
1281
1282 err = dev_queue_xmit(skb);
1283 if (err > 0 && (err = net_xmit_errno(err)) != 0)
1284 goto out_unlock;
1285
1286 dev_put(dev);
1287
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001288 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290out_free:
1291 kfree_skb(skb);
1292out_unlock:
1293 if (dev)
1294 dev_put(dev);
1295out:
1296 return err;
1297}
1298
Johann Baudy69e3c752009-05-18 22:11:22 -07001299static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
1300 struct msghdr *msg, size_t len)
1301{
Johann Baudy69e3c752009-05-18 22:11:22 -07001302 struct sock *sk = sock->sk;
1303 struct packet_sock *po = pkt_sk(sk);
1304 if (po->tx_ring.pg_vec)
1305 return tpacket_snd(po, msg);
1306 else
Johann Baudy69e3c752009-05-18 22:11:22 -07001307 return packet_snd(sock, msg, len);
1308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/*
1311 * Close a PACKET socket. This is fairly simple. We immediately go
1312 * to 'closed' state and remove our protocol entry in the device list.
1313 */
1314
1315static int packet_release(struct socket *sock)
1316{
1317 struct sock *sk = sock->sk;
1318 struct packet_sock *po;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001319 struct net *net;
Johann Baudy69e3c752009-05-18 22:11:22 -07001320 struct tpacket_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 if (!sk)
1323 return 0;
1324
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001325 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 po = pkt_sk(sk);
1327
stephen hemminger808f5112010-02-22 07:57:18 +00001328 spin_lock_bh(&net->packet.sklist_lock);
1329 sk_del_node_init_rcu(sk);
Eric Dumazet920de802008-11-24 00:09:29 -08001330 sock_prot_inuse_add(net, sk->sk_prot, -1);
stephen hemminger808f5112010-02-22 07:57:18 +00001331 spin_unlock_bh(&net->packet.sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
stephen hemminger808f5112010-02-22 07:57:18 +00001333 spin_lock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (po->running) {
1335 /*
stephen hemminger808f5112010-02-22 07:57:18 +00001336 * Remove from protocol table
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 po->running = 0;
1339 po->num = 0;
stephen hemminger808f5112010-02-22 07:57:18 +00001340 __dev_remove_pack(&po->prot_hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 __sock_put(sk);
1342 }
stephen hemminger808f5112010-02-22 07:57:18 +00001343 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Johann Baudy69e3c752009-05-18 22:11:22 -07001347 memset(&req, 0, sizeof(req));
1348
1349 if (po->rx_ring.pg_vec)
1350 packet_set_ring(sk, &req, 1, 0);
1351
1352 if (po->tx_ring.pg_vec)
1353 packet_set_ring(sk, &req, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
stephen hemminger808f5112010-02-22 07:57:18 +00001355 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /*
1357 * Now the socket is dead. No more input will appear.
1358 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 sock_orphan(sk);
1360 sock->sk = NULL;
1361
1362 /* Purge queues */
1363
1364 skb_queue_purge(&sk->sk_receive_queue);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001365 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 sock_put(sk);
1368 return 0;
1369}
1370
1371/*
1372 * Attach a packet hook.
1373 */
1374
Al Viro0e11c912006-11-08 00:26:29 -08001375static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct packet_sock *po = pkt_sk(sk);
1378 /*
1379 * Detach an existing hook if present.
1380 */
1381
1382 lock_sock(sk);
1383
1384 spin_lock(&po->bind_lock);
1385 if (po->running) {
1386 __sock_put(sk);
1387 po->running = 0;
1388 po->num = 0;
1389 spin_unlock(&po->bind_lock);
1390 dev_remove_pack(&po->prot_hook);
1391 spin_lock(&po->bind_lock);
1392 }
1393
1394 po->num = protocol;
1395 po->prot_hook.type = protocol;
1396 po->prot_hook.dev = dev;
1397
1398 po->ifindex = dev ? dev->ifindex : 0;
1399
1400 if (protocol == 0)
1401 goto out_unlock;
1402
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001403 if (!dev || (dev->flags & IFF_UP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 dev_add_pack(&po->prot_hook);
1405 sock_hold(sk);
1406 po->running = 1;
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001407 } else {
1408 sk->sk_err = ENETDOWN;
1409 if (!sock_flag(sk, SOCK_DEAD))
1410 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
1413out_unlock:
1414 spin_unlock(&po->bind_lock);
1415 release_sock(sk);
1416 return 0;
1417}
1418
1419/*
1420 * Bind a packet socket to a device
1421 */
1422
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001423static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
1424 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001426 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 char name[15];
1428 struct net_device *dev;
1429 int err = -ENODEV;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 /*
1432 * Check legality
1433 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001434
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001435 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001437 strlcpy(name, uaddr->sa_data, sizeof(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001439 dev = dev_get_by_name(sock_net(sk), name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (dev) {
1441 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
1442 dev_put(dev);
1443 }
1444 return err;
1445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1448{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001449 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
1450 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 struct net_device *dev = NULL;
1452 int err;
1453
1454
1455 /*
1456 * Check legality
1457 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (addr_len < sizeof(struct sockaddr_ll))
1460 return -EINVAL;
1461 if (sll->sll_family != AF_PACKET)
1462 return -EINVAL;
1463
1464 if (sll->sll_ifindex) {
1465 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001466 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (dev == NULL)
1468 goto out;
1469 }
1470 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
1471 if (dev)
1472 dev_put(dev);
1473
1474out:
1475 return err;
1476}
1477
1478static struct proto packet_proto = {
1479 .name = "PACKET",
1480 .owner = THIS_MODULE,
1481 .obj_size = sizeof(struct packet_sock),
1482};
1483
1484/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001485 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 */
1487
Eric Paris3f378b62009-11-05 22:18:14 -08001488static int packet_create(struct net *net, struct socket *sock, int protocol,
1489 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 struct sock *sk;
1492 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08001493 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 int err;
1495
1496 if (!capable(CAP_NET_RAW))
1497 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07001498 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
1499 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return -ESOCKTNOSUPPORT;
1501
1502 sock->state = SS_UNCONNECTED;
1503
1504 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001505 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (sk == NULL)
1507 goto out;
1508
1509 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (sock->type == SOCK_PACKET)
1511 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 sock_init_data(sock, sk);
1514
1515 po = pkt_sk(sk);
1516 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08001517 po->num = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001520 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 /*
1523 * Attach a protocol block
1524 */
1525
1526 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08001527 mutex_init(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (sock->type == SOCK_PACKET)
1531 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 po->prot_hook.af_packet_priv = sk;
1534
Al Viro0e11c912006-11-08 00:26:29 -08001535 if (proto) {
1536 po->prot_hook.type = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 dev_add_pack(&po->prot_hook);
1538 sock_hold(sk);
1539 po->running = 1;
1540 }
1541
stephen hemminger808f5112010-02-22 07:57:18 +00001542 spin_lock_bh(&net->packet.sklist_lock);
1543 sk_add_node_rcu(sk, &net->packet.sklist);
Eric Dumazet36804532008-11-19 14:25:35 -08001544 sock_prot_inuse_add(net, &packet_proto, 1);
stephen hemminger808f5112010-02-22 07:57:18 +00001545 spin_unlock_bh(&net->packet.sklist_lock);
1546
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548out:
1549 return err;
1550}
1551
Richard Cochraned85b562010-04-07 22:41:28 +00001552static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
1553{
1554 struct sock_exterr_skb *serr;
1555 struct sk_buff *skb, *skb2;
1556 int copied, err;
1557
1558 err = -EAGAIN;
1559 skb = skb_dequeue(&sk->sk_error_queue);
1560 if (skb == NULL)
1561 goto out;
1562
1563 copied = skb->len;
1564 if (copied > len) {
1565 msg->msg_flags |= MSG_TRUNC;
1566 copied = len;
1567 }
1568 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1569 if (err)
1570 goto out_free_skb;
1571
1572 sock_recv_timestamp(msg, sk, skb);
1573
1574 serr = SKB_EXT_ERR(skb);
1575 put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
1576 sizeof(serr->ee), &serr->ee);
1577
1578 msg->msg_flags |= MSG_ERRQUEUE;
1579 err = copied;
1580
1581 /* Reset and regenerate socket error */
1582 spin_lock_bh(&sk->sk_error_queue.lock);
1583 sk->sk_err = 0;
1584 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
1585 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
1586 spin_unlock_bh(&sk->sk_error_queue.lock);
1587 sk->sk_error_report(sk);
1588 } else
1589 spin_unlock_bh(&sk->sk_error_queue.lock);
1590
1591out_free_skb:
1592 kfree_skb(skb);
1593out:
1594 return err;
1595}
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597/*
1598 * Pull a packet from our receive queue and hand it to the user.
1599 * If necessary we block.
1600 */
1601
1602static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1603 struct msghdr *msg, size_t len, int flags)
1604{
1605 struct sock *sk = sock->sk;
1606 struct sk_buff *skb;
1607 int copied, err;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001608 struct sockaddr_ll *sll;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001609 int vnet_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00001612 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto out;
1614
1615#if 0
1616 /* What error should we return now? EUNATTACH? */
1617 if (pkt_sk(sk)->ifindex < 0)
1618 return -ENODEV;
1619#endif
1620
Richard Cochraned85b562010-04-07 22:41:28 +00001621 if (flags & MSG_ERRQUEUE) {
1622 err = packet_recv_error(sk, msg, len);
1623 goto out;
1624 }
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 * Call the generic datagram receiver. This handles all sorts
1628 * of horrible races and re-entrancy so we can forget about it
1629 * in the protocol layers.
1630 *
1631 * Now it will return ENETDOWN, if device have just gone down,
1632 * but then it will block.
1633 */
1634
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001635 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001638 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 * handles the blocking we don't see and worry about blocking
1640 * retries.
1641 */
1642
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001643 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 goto out;
1645
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001646 if (pkt_sk(sk)->has_vnet_hdr) {
1647 struct virtio_net_hdr vnet_hdr = { 0 };
1648
1649 err = -EINVAL;
1650 vnet_hdr_len = sizeof(vnet_hdr);
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001651 if (len < vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001652 goto out_free;
1653
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001654 len -= vnet_hdr_len;
1655
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001656 if (skb_is_gso(skb)) {
1657 struct skb_shared_info *sinfo = skb_shinfo(skb);
1658
1659 /* This is a hint as to how much should be linear. */
1660 vnet_hdr.hdr_len = skb_headlen(skb);
1661 vnet_hdr.gso_size = sinfo->gso_size;
1662 if (sinfo->gso_type & SKB_GSO_TCPV4)
1663 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1664 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1665 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1666 else if (sinfo->gso_type & SKB_GSO_UDP)
1667 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1668 else if (sinfo->gso_type & SKB_GSO_FCOE)
1669 goto out_free;
1670 else
1671 BUG();
1672 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1673 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1674 } else
1675 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1676
1677 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1678 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00001679 vnet_hdr.csum_start = skb_checksum_start_offset(skb);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001680 vnet_hdr.csum_offset = skb->csum_offset;
1681 } /* else everything is zero */
1682
1683 err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
1684 vnet_hdr_len);
1685 if (err < 0)
1686 goto out_free;
1687 }
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /*
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001690 * If the address length field is there to be filled in, we fill
1691 * it in now.
1692 */
1693
Herbert Xuffbc6112007-02-04 23:33:10 -08001694 sll = &PACKET_SKB_CB(skb)->sa.ll;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001695 if (sock->type == SOCK_PACKET)
1696 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1697 else
1698 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1699
1700 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 * You lose any data beyond the buffer you gave. If it worries a
1702 * user program they can ask the device for its MTU anyway.
1703 */
1704
1705 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001706 if (copied > len) {
1707 copied = len;
1708 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
1711 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1712 if (err)
1713 goto out_free;
1714
Neil Horman3b885782009-10-12 13:26:31 -07001715 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 if (msg->msg_name)
Herbert Xuffbc6112007-02-04 23:33:10 -08001718 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
1719 msg->msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Herbert Xu8dc41942007-02-04 23:31:32 -08001721 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08001722 struct tpacket_auxdata aux;
1723
1724 aux.tp_status = TP_STATUS_USER;
1725 if (skb->ip_summed == CHECKSUM_PARTIAL)
1726 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
1727 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
1728 aux.tp_snaplen = skb->len;
1729 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001730 aux.tp_net = skb_network_offset(skb);
Eric Dumazet05423b22009-10-26 18:40:35 -07001731 aux.tp_vlan_tci = vlan_tx_tag_get(skb);
Herbert Xuffbc6112007-02-04 23:33:10 -08001732
1733 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08001734 }
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /*
1737 * Free or return the buffer as appropriate. Again this
1738 * hides all the races and re-entrancy issues from us.
1739 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001740 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742out_free:
1743 skb_free_datagram(sk, skb);
1744out:
1745 return err;
1746}
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1749 int *uaddr_len, int peer)
1750{
1751 struct net_device *dev;
1752 struct sock *sk = sock->sk;
1753
1754 if (peer)
1755 return -EOPNOTSUPP;
1756
1757 uaddr->sa_family = AF_PACKET;
Eric Dumazet654d1f82009-11-02 10:43:32 +01001758 rcu_read_lock();
1759 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
1760 if (dev)
Vasiliy Kulikov67286642010-11-10 12:09:10 -08001761 strncpy(uaddr->sa_data, dev->name, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001762 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 memset(uaddr->sa_data, 0, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001764 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 *uaddr_len = sizeof(*uaddr);
1766
1767 return 0;
1768}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1771 int *uaddr_len, int peer)
1772{
1773 struct net_device *dev;
1774 struct sock *sk = sock->sk;
1775 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001776 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 if (peer)
1779 return -EOPNOTSUPP;
1780
1781 sll->sll_family = AF_PACKET;
1782 sll->sll_ifindex = po->ifindex;
1783 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08001784 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01001785 rcu_read_lock();
1786 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (dev) {
1788 sll->sll_hatype = dev->type;
1789 sll->sll_halen = dev->addr_len;
1790 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 } else {
1792 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1793 sll->sll_halen = 0;
1794 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01001795 rcu_read_unlock();
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001796 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 return 0;
1799}
1800
Wang Chen2aeb0b82008-07-14 20:49:46 -07001801static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
1802 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
1804 switch (i->type) {
1805 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00001806 if (i->alen != dev->addr_len)
1807 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001809 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00001811 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 break;
1813 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07001814 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 break;
1816 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07001817 return dev_set_allmulti(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 break;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001819 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00001820 if (i->alen != dev->addr_len)
1821 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001822 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001823 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001824 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001825 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001826 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001827 default:
1828 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07001830 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
1833static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1834{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001835 for ( ; i; i = i->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (i->ifindex == dev->ifindex)
1837 packet_dev_mc(dev, i, what);
1838 }
1839}
1840
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001841static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 struct packet_sock *po = pkt_sk(sk);
1844 struct packet_mclist *ml, *i;
1845 struct net_device *dev;
1846 int err;
1847
1848 rtnl_lock();
1849
1850 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001851 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 if (!dev)
1853 goto done;
1854
1855 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00001856 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 goto done;
1858
1859 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001860 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (i == NULL)
1862 goto done;
1863
1864 err = 0;
1865 for (ml = po->mclist; ml; ml = ml->next) {
1866 if (ml->ifindex == mreq->mr_ifindex &&
1867 ml->type == mreq->mr_type &&
1868 ml->alen == mreq->mr_alen &&
1869 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1870 ml->count++;
1871 /* Free the new element ... */
1872 kfree(i);
1873 goto done;
1874 }
1875 }
1876
1877 i->type = mreq->mr_type;
1878 i->ifindex = mreq->mr_ifindex;
1879 i->alen = mreq->mr_alen;
1880 memcpy(i->addr, mreq->mr_address, i->alen);
1881 i->count = 1;
1882 i->next = po->mclist;
1883 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07001884 err = packet_dev_mc(dev, i, 1);
1885 if (err) {
1886 po->mclist = i->next;
1887 kfree(i);
1888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890done:
1891 rtnl_unlock();
1892 return err;
1893}
1894
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001895static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
1897 struct packet_mclist *ml, **mlp;
1898
1899 rtnl_lock();
1900
1901 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
1902 if (ml->ifindex == mreq->mr_ifindex &&
1903 ml->type == mreq->mr_type &&
1904 ml->alen == mreq->mr_alen &&
1905 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1906 if (--ml->count == 0) {
1907 struct net_device *dev;
1908 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00001909 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
1910 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 kfree(ml);
1913 }
1914 rtnl_unlock();
1915 return 0;
1916 }
1917 }
1918 rtnl_unlock();
1919 return -EADDRNOTAVAIL;
1920}
1921
1922static void packet_flush_mclist(struct sock *sk)
1923{
1924 struct packet_sock *po = pkt_sk(sk);
1925 struct packet_mclist *ml;
1926
1927 if (!po->mclist)
1928 return;
1929
1930 rtnl_lock();
1931 while ((ml = po->mclist) != NULL) {
1932 struct net_device *dev;
1933
1934 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00001935 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
1936 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 kfree(ml);
1939 }
1940 rtnl_unlock();
1941}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943static int
David S. Millerb7058842009-09-30 16:12:20 -07001944packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08001947 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 int ret;
1949
1950 if (level != SOL_PACKET)
1951 return -ENOPROTOOPT;
1952
Johann Baudy69e3c752009-05-18 22:11:22 -07001953 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001954 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 case PACKET_DROP_MEMBERSHIP:
1956 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001957 struct packet_mreq_max mreq;
1958 int len = optlen;
1959 memset(&mreq, 0, sizeof(mreq));
1960 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001962 if (len > sizeof(mreq))
1963 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001964 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001966 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
1967 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (optname == PACKET_ADD_MEMBERSHIP)
1969 ret = packet_mc_add(sk, &mreq);
1970 else
1971 ret = packet_mc_drop(sk, &mreq);
1972 return ret;
1973 }
David S. Millera2efcfa2007-05-29 13:12:50 -07001974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07001976 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 {
1978 struct tpacket_req req;
1979
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001980 if (optlen < sizeof(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001982 if (pkt_sk(sk)->has_vnet_hdr)
1983 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001984 if (copy_from_user(&req, optval, sizeof(req)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -07001986 return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988 case PACKET_COPY_THRESH:
1989 {
1990 int val;
1991
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001992 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001994 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return -EFAULT;
1996
1997 pkt_sk(sk)->copy_thresh = val;
1998 return 0;
1999 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002000 case PACKET_VERSION:
2001 {
2002 int val;
2003
2004 if (optlen != sizeof(val))
2005 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002006 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002007 return -EBUSY;
2008 if (copy_from_user(&val, optval, sizeof(val)))
2009 return -EFAULT;
2010 switch (val) {
2011 case TPACKET_V1:
2012 case TPACKET_V2:
2013 po->tp_version = val;
2014 return 0;
2015 default:
2016 return -EINVAL;
2017 }
2018 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07002019 case PACKET_RESERVE:
2020 {
2021 unsigned int val;
2022
2023 if (optlen != sizeof(val))
2024 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002025 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardy8913336a2008-07-18 18:05:19 -07002026 return -EBUSY;
2027 if (copy_from_user(&val, optval, sizeof(val)))
2028 return -EFAULT;
2029 po->tp_reserve = val;
2030 return 0;
2031 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002032 case PACKET_LOSS:
2033 {
2034 unsigned int val;
2035
2036 if (optlen != sizeof(val))
2037 return -EINVAL;
2038 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2039 return -EBUSY;
2040 if (copy_from_user(&val, optval, sizeof(val)))
2041 return -EFAULT;
2042 po->tp_loss = !!val;
2043 return 0;
2044 }
Herbert Xu8dc41942007-02-04 23:31:32 -08002045 case PACKET_AUXDATA:
2046 {
2047 int val;
2048
2049 if (optlen < sizeof(val))
2050 return -EINVAL;
2051 if (copy_from_user(&val, optval, sizeof(val)))
2052 return -EFAULT;
2053
2054 po->auxdata = !!val;
2055 return 0;
2056 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002057 case PACKET_ORIGDEV:
2058 {
2059 int val;
2060
2061 if (optlen < sizeof(val))
2062 return -EINVAL;
2063 if (copy_from_user(&val, optval, sizeof(val)))
2064 return -EFAULT;
2065
2066 po->origdev = !!val;
2067 return 0;
2068 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002069 case PACKET_VNET_HDR:
2070 {
2071 int val;
2072
2073 if (sock->type != SOCK_RAW)
2074 return -EINVAL;
2075 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2076 return -EBUSY;
2077 if (optlen < sizeof(val))
2078 return -EINVAL;
2079 if (copy_from_user(&val, optval, sizeof(val)))
2080 return -EFAULT;
2081
2082 po->has_vnet_hdr = !!val;
2083 return 0;
2084 }
Scott McMillan614f60f2010-06-02 05:53:56 -07002085 case PACKET_TIMESTAMP:
2086 {
2087 int val;
2088
2089 if (optlen != sizeof(val))
2090 return -EINVAL;
2091 if (copy_from_user(&val, optval, sizeof(val)))
2092 return -EFAULT;
2093
2094 po->tp_tstamp = val;
2095 return 0;
2096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 default:
2098 return -ENOPROTOOPT;
2099 }
2100}
2101
2102static int packet_getsockopt(struct socket *sock, int level, int optname,
2103 char __user *optval, int __user *optlen)
2104{
2105 int len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002106 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 struct sock *sk = sock->sk;
2108 struct packet_sock *po = pkt_sk(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08002109 void *data;
2110 struct tpacket_stats st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 if (level != SOL_PACKET)
2113 return -ENOPROTOOPT;
2114
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002115 if (get_user(len, optlen))
2116 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 if (len < 0)
2119 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002120
Johann Baudy69e3c752009-05-18 22:11:22 -07002121 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (len > sizeof(struct tpacket_stats))
2124 len = sizeof(struct tpacket_stats);
2125 spin_lock_bh(&sk->sk_receive_queue.lock);
2126 st = po->stats;
2127 memset(&po->stats, 0, sizeof(st));
2128 spin_unlock_bh(&sk->sk_receive_queue.lock);
2129 st.tp_packets += st.tp_drops;
2130
Herbert Xu8dc41942007-02-04 23:31:32 -08002131 data = &st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08002133 case PACKET_AUXDATA:
2134 if (len > sizeof(int))
2135 len = sizeof(int);
2136 val = po->auxdata;
2137
2138 data = &val;
2139 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002140 case PACKET_ORIGDEV:
2141 if (len > sizeof(int))
2142 len = sizeof(int);
2143 val = po->origdev;
2144
2145 data = &val;
2146 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002147 case PACKET_VNET_HDR:
2148 if (len > sizeof(int))
2149 len = sizeof(int);
2150 val = po->has_vnet_hdr;
2151
2152 data = &val;
2153 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002154 case PACKET_VERSION:
2155 if (len > sizeof(int))
2156 len = sizeof(int);
2157 val = po->tp_version;
2158 data = &val;
2159 break;
2160 case PACKET_HDRLEN:
2161 if (len > sizeof(int))
2162 len = sizeof(int);
2163 if (copy_from_user(&val, optval, len))
2164 return -EFAULT;
2165 switch (val) {
2166 case TPACKET_V1:
2167 val = sizeof(struct tpacket_hdr);
2168 break;
2169 case TPACKET_V2:
2170 val = sizeof(struct tpacket2_hdr);
2171 break;
2172 default:
2173 return -EINVAL;
2174 }
2175 data = &val;
2176 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07002177 case PACKET_RESERVE:
2178 if (len > sizeof(unsigned int))
2179 len = sizeof(unsigned int);
2180 val = po->tp_reserve;
2181 data = &val;
2182 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07002183 case PACKET_LOSS:
2184 if (len > sizeof(unsigned int))
2185 len = sizeof(unsigned int);
2186 val = po->tp_loss;
2187 data = &val;
2188 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07002189 case PACKET_TIMESTAMP:
2190 if (len > sizeof(int))
2191 len = sizeof(int);
2192 val = po->tp_tstamp;
2193 data = &val;
2194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 default:
2196 return -ENOPROTOOPT;
2197 }
2198
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002199 if (put_user(len, optlen))
2200 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08002201 if (copy_to_user(optval, data, len))
2202 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
2206
2207static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
2208{
2209 struct sock *sk;
2210 struct hlist_node *node;
Jason Lunzad930652007-02-20 23:19:54 -08002211 struct net_device *dev = data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002212 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
stephen hemminger808f5112010-02-22 07:57:18 +00002214 rcu_read_lock();
2215 sk_for_each_rcu(sk, node, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 struct packet_sock *po = pkt_sk(sk);
2217
2218 switch (msg) {
2219 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (po->mclist)
2221 packet_dev_mclist(dev, po->mclist, -1);
David S. Millera2efcfa2007-05-29 13:12:50 -07002222 /* fallthrough */
2223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 case NETDEV_DOWN:
2225 if (dev->ifindex == po->ifindex) {
2226 spin_lock(&po->bind_lock);
2227 if (po->running) {
2228 __dev_remove_pack(&po->prot_hook);
2229 __sock_put(sk);
2230 po->running = 0;
2231 sk->sk_err = ENETDOWN;
2232 if (!sock_flag(sk, SOCK_DEAD))
2233 sk->sk_error_report(sk);
2234 }
2235 if (msg == NETDEV_UNREGISTER) {
2236 po->ifindex = -1;
2237 po->prot_hook.dev = NULL;
2238 }
2239 spin_unlock(&po->bind_lock);
2240 }
2241 break;
2242 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00002243 if (dev->ifindex == po->ifindex) {
2244 spin_lock(&po->bind_lock);
2245 if (po->num && !po->running) {
2246 dev_add_pack(&po->prot_hook);
2247 sock_hold(sk);
2248 po->running = 1;
2249 }
2250 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 break;
2253 }
2254 }
stephen hemminger808f5112010-02-22 07:57:18 +00002255 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return NOTIFY_DONE;
2257}
2258
2259
2260static int packet_ioctl(struct socket *sock, unsigned int cmd,
2261 unsigned long arg)
2262{
2263 struct sock *sk = sock->sk;
2264
Johann Baudy69e3c752009-05-18 22:11:22 -07002265 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002266 case SIOCOUTQ:
2267 {
2268 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07002269
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002270 return put_user(amount, (int __user *)arg);
2271 }
2272 case SIOCINQ:
2273 {
2274 struct sk_buff *skb;
2275 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002277 spin_lock_bh(&sk->sk_receive_queue.lock);
2278 skb = skb_peek(&sk->sk_receive_queue);
2279 if (skb)
2280 amount = skb->len;
2281 spin_unlock_bh(&sk->sk_receive_queue.lock);
2282 return put_user(amount, (int __user *)arg);
2283 }
2284 case SIOCGSTAMP:
2285 return sock_get_timestamp(sk, (struct timeval __user *)arg);
2286 case SIOCGSTAMPNS:
2287 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002290 case SIOCADDRT:
2291 case SIOCDELRT:
2292 case SIOCDARP:
2293 case SIOCGARP:
2294 case SIOCSARP:
2295 case SIOCGIFADDR:
2296 case SIOCSIFADDR:
2297 case SIOCGIFBRDADDR:
2298 case SIOCSIFBRDADDR:
2299 case SIOCGIFNETMASK:
2300 case SIOCSIFNETMASK:
2301 case SIOCGIFDSTADDR:
2302 case SIOCSIFDSTADDR:
2303 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002304 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305#endif
2306
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002307 default:
2308 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
2310 return 0;
2311}
2312
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002313static unsigned int packet_poll(struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 poll_table *wait)
2315{
2316 struct sock *sk = sock->sk;
2317 struct packet_sock *po = pkt_sk(sk);
2318 unsigned int mask = datagram_poll(file, sock, wait);
2319
2320 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002321 if (po->rx_ring.pg_vec) {
2322 if (!packet_previous_frame(po, &po->rx_ring, TP_STATUS_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 mask |= POLLIN | POLLRDNORM;
2324 }
2325 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002326 spin_lock_bh(&sk->sk_write_queue.lock);
2327 if (po->tx_ring.pg_vec) {
2328 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
2329 mask |= POLLOUT | POLLWRNORM;
2330 }
2331 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return mask;
2333}
2334
2335
2336/* Dirty? Well, I still did not learn better way to account
2337 * for user mmaps.
2338 */
2339
2340static void packet_mm_open(struct vm_area_struct *vma)
2341{
2342 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002343 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (sk)
2347 atomic_inc(&pkt_sk(sk)->mapped);
2348}
2349
2350static void packet_mm_close(struct vm_area_struct *vma)
2351{
2352 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002353 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (sk)
2357 atomic_dec(&pkt_sk(sk)->mapped);
2358}
2359
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002360static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002361 .open = packet_mm_open,
2362 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363};
2364
Neil Horman0e3125c2010-11-16 10:26:47 -08002365static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
2366 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
2368 int i;
2369
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002370 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002371 if (likely(pg_vec[i].buffer)) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002372 if (is_vmalloc_addr(pg_vec[i].buffer))
Neil Horman0e3125c2010-11-16 10:26:47 -08002373 vfree(pg_vec[i].buffer);
2374 else
2375 free_pages((unsigned long)pg_vec[i].buffer,
2376 order);
2377 pg_vec[i].buffer = NULL;
2378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380 kfree(pg_vec);
2381}
2382
Changli Gaoc56b4d92010-12-01 02:52:57 +00002383static inline char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002384{
Neil Horman0e3125c2010-11-16 10:26:47 -08002385 char *buffer = NULL;
2386 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
2387 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07002388
Neil Horman0e3125c2010-11-16 10:26:47 -08002389 buffer = (char *) __get_free_pages(gfp_flags, order);
2390
2391 if (buffer)
2392 return buffer;
2393
2394 /*
2395 * __get_free_pages failed, fall back to vmalloc
2396 */
Eric Dumazetbbce5a52010-11-20 07:31:54 +00002397 buffer = vzalloc((1 << order) * PAGE_SIZE);
Neil Horman0e3125c2010-11-16 10:26:47 -08002398
2399 if (buffer)
2400 return buffer;
2401
2402 /*
2403 * vmalloc failed, lets dig into swap here
2404 */
Neil Horman0e3125c2010-11-16 10:26:47 -08002405 gfp_flags &= ~__GFP_NORETRY;
2406 buffer = (char *)__get_free_pages(gfp_flags, order);
2407 if (buffer)
2408 return buffer;
2409
2410 /*
2411 * complete and utter failure
2412 */
2413 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002414}
2415
Neil Horman0e3125c2010-11-16 10:26:47 -08002416static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002417{
2418 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08002419 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002420 int i;
2421
Neil Horman0e3125c2010-11-16 10:26:47 -08002422 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002423 if (unlikely(!pg_vec))
2424 goto out;
2425
2426 for (i = 0; i < block_nr; i++) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002427 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08002428 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002429 goto out_free_pgvec;
2430 }
2431
2432out:
2433 return pg_vec;
2434
2435out_free_pgvec:
2436 free_pg_vec(pg_vec, order, block_nr);
2437 pg_vec = NULL;
2438 goto out;
2439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Johann Baudy69e3c752009-05-18 22:11:22 -07002441static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
2442 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Neil Horman0e3125c2010-11-16 10:26:47 -08002444 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08002446 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002447 struct packet_ring_buffer *rb;
2448 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08002449 __be16 num;
Johann Baudy69e3c752009-05-18 22:11:22 -07002450 int err;
2451
2452 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
2453 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
2454
2455 err = -EBUSY;
2456 if (!closing) {
2457 if (atomic_read(&po->mapped))
2458 goto out;
2459 if (atomic_read(&rb->pending))
2460 goto out;
2461 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if (req->tp_block_nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07002465 err = -EBUSY;
2466 if (unlikely(rb->pg_vec))
2467 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002469 switch (po->tp_version) {
2470 case TPACKET_V1:
2471 po->tp_hdrlen = TPACKET_HDRLEN;
2472 break;
2473 case TPACKET_V2:
2474 po->tp_hdrlen = TPACKET2_HDRLEN;
2475 break;
2476 }
2477
Johann Baudy69e3c752009-05-18 22:11:22 -07002478 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002479 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07002480 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002481 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002482 goto out;
Patrick McHardy8913336a2008-07-18 18:05:19 -07002483 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
Johann Baudy69e3c752009-05-18 22:11:22 -07002484 po->tp_reserve))
2485 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002486 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002487 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Johann Baudy69e3c752009-05-18 22:11:22 -07002489 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
2490 if (unlikely(rb->frames_per_block <= 0))
2491 goto out;
2492 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
2493 req->tp_frame_nr))
2494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002497 order = get_order(req->tp_block_size);
2498 pg_vec = alloc_pg_vec(req, order);
2499 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002501 }
2502 /* Done */
2503 else {
2504 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002505 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07002506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
2508
2509 lock_sock(sk);
2510
2511 /* Detach socket from network */
2512 spin_lock(&po->bind_lock);
2513 was_running = po->running;
2514 num = po->num;
2515 if (was_running) {
2516 __dev_remove_pack(&po->prot_hook);
2517 po->num = 0;
2518 po->running = 0;
2519 __sock_put(sk);
2520 }
2521 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 synchronize_net();
2524
2525 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08002526 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if (closing || atomic_read(&po->mapped) == 0) {
2528 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002529 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08002530 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07002531 rb->frame_max = (req->tp_frame_nr - 1);
2532 rb->head = 0;
2533 rb->frame_size = req->tp_frame_size;
2534 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Changli Gaoc053fd92010-12-10 16:02:20 -08002536 swap(rb->pg_vec_order, order);
2537 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Johann Baudy69e3c752009-05-18 22:11:22 -07002539 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
2540 po->prot_hook.func = (po->rx_ring.pg_vec) ?
2541 tpacket_rcv : packet_rcv;
2542 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002544 pr_err("packet_mmap: vma is busy: %d\n",
2545 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
Herbert Xu905db442009-01-30 14:12:06 -08002547 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 spin_lock(&po->bind_lock);
2550 if (was_running && !po->running) {
2551 sock_hold(sk);
2552 po->running = 1;
2553 po->num = num;
2554 dev_add_pack(&po->prot_hook);
2555 }
2556 spin_unlock(&po->bind_lock);
2557
2558 release_sock(sk);
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 if (pg_vec)
2561 free_pg_vec(pg_vec, order, req->tp_block_nr);
2562out:
2563 return err;
2564}
2565
Johann Baudy69e3c752009-05-18 22:11:22 -07002566static int packet_mmap(struct file *file, struct socket *sock,
2567 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
2569 struct sock *sk = sock->sk;
2570 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002571 unsigned long size, expected_size;
2572 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 unsigned long start;
2574 int err = -EINVAL;
2575 int i;
2576
2577 if (vma->vm_pgoff)
2578 return -EINVAL;
2579
Herbert Xu905db442009-01-30 14:12:06 -08002580 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002581
2582 expected_size = 0;
2583 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2584 if (rb->pg_vec) {
2585 expected_size += rb->pg_vec_len
2586 * rb->pg_vec_pages
2587 * PAGE_SIZE;
2588 }
2589 }
2590
2591 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002593
2594 size = vma->vm_end - vma->vm_start;
2595 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 goto out;
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07002599 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2600 if (rb->pg_vec == NULL)
2601 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002602
Johann Baudy69e3c752009-05-18 22:11:22 -07002603 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002604 struct page *page;
2605 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07002606 int pg_num;
2607
Changli Gaoc56b4d92010-12-01 02:52:57 +00002608 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
2609 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07002610 err = vm_insert_page(vma, start, page);
2611 if (unlikely(err))
2612 goto out;
2613 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08002614 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002615 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002618
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002619 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 vma->vm_ops = &packet_mmap_ops;
2621 err = 0;
2622
2623out:
Herbert Xu905db442009-01-30 14:12:06 -08002624 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 return err;
2626}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002628static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 .family = PF_PACKET,
2630 .owner = THIS_MODULE,
2631 .release = packet_release,
2632 .bind = packet_bind_spkt,
2633 .connect = sock_no_connect,
2634 .socketpair = sock_no_socketpair,
2635 .accept = sock_no_accept,
2636 .getname = packet_getname_spkt,
2637 .poll = datagram_poll,
2638 .ioctl = packet_ioctl,
2639 .listen = sock_no_listen,
2640 .shutdown = sock_no_shutdown,
2641 .setsockopt = sock_no_setsockopt,
2642 .getsockopt = sock_no_getsockopt,
2643 .sendmsg = packet_sendmsg_spkt,
2644 .recvmsg = packet_recvmsg,
2645 .mmap = sock_no_mmap,
2646 .sendpage = sock_no_sendpage,
2647};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002649static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 .family = PF_PACKET,
2651 .owner = THIS_MODULE,
2652 .release = packet_release,
2653 .bind = packet_bind,
2654 .connect = sock_no_connect,
2655 .socketpair = sock_no_socketpair,
2656 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002657 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 .poll = packet_poll,
2659 .ioctl = packet_ioctl,
2660 .listen = sock_no_listen,
2661 .shutdown = sock_no_shutdown,
2662 .setsockopt = packet_setsockopt,
2663 .getsockopt = packet_getsockopt,
2664 .sendmsg = packet_sendmsg,
2665 .recvmsg = packet_recvmsg,
2666 .mmap = packet_mmap,
2667 .sendpage = sock_no_sendpage,
2668};
2669
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002670static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 .family = PF_PACKET,
2672 .create = packet_create,
2673 .owner = THIS_MODULE,
2674};
2675
2676static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002677 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678};
2679
2680#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00002683 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
Denis V. Luneve372c412007-11-19 22:31:54 -08002685 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00002686
2687 rcu_read_lock();
2688 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689}
2690
2691static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2692{
Herbert Xu1bf40952007-12-16 14:04:02 -08002693 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00002694 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
2697static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00002698 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
stephen hemminger808f5112010-02-22 07:57:18 +00002700 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002703static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
2705 if (v == SEQ_START_TOKEN)
2706 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
2707 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00002708 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 const struct packet_sock *po = pkt_sk(s);
2710
2711 seq_printf(seq,
2712 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
2713 s,
2714 atomic_read(&s->sk_refcnt),
2715 s->sk_type,
2716 ntohs(po->num),
2717 po->ifindex,
2718 po->running,
2719 atomic_read(&s->sk_rmem_alloc),
2720 sock_i_uid(s),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002721 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
2723
2724 return 0;
2725}
2726
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002727static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 .start = packet_seq_start,
2729 .next = packet_seq_next,
2730 .stop = packet_seq_stop,
2731 .show = packet_seq_show,
2732};
2733
2734static int packet_seq_open(struct inode *inode, struct file *file)
2735{
Denis V. Luneve372c412007-11-19 22:31:54 -08002736 return seq_open_net(inode, file, &packet_seq_ops,
2737 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
Arjan van de Venda7071d2007-02-12 00:55:36 -08002740static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 .owner = THIS_MODULE,
2742 .open = packet_seq_open,
2743 .read = seq_read,
2744 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002745 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746};
2747
2748#endif
2749
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002750static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002751{
stephen hemminger808f5112010-02-22 07:57:18 +00002752 spin_lock_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08002753 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002754
2755 if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
2756 return -ENOMEM;
2757
2758 return 0;
2759}
2760
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002761static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002762{
2763 proc_net_remove(net, "packet");
2764}
2765
2766static struct pernet_operations packet_net_ops = {
2767 .init = packet_net_init,
2768 .exit = packet_net_exit,
2769};
2770
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772static void __exit packet_exit(void)
2773{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002775 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 sock_unregister(PF_PACKET);
2777 proto_unregister(&packet_proto);
2778}
2779
2780static int __init packet_init(void)
2781{
2782 int rc = proto_register(&packet_proto, 0);
2783
2784 if (rc != 0)
2785 goto out;
2786
2787 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002788 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790out:
2791 return rc;
2792}
2793
2794module_init(packet_init);
2795module_exit(packet_exit);
2796MODULE_LICENSE("GPL");
2797MODULE_ALIAS_NETPROTO(PF_PACKET);