blob: c235da940019a450965d6777f9b478af58dee3d2 [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.
chetan lokef6fb8f102011-08-19 10:18:16 +000043 * Chetan Loke : Implemented TPACKET_V3 block abstraction
44 * layer.
45 * Copyright (C) 2011, <lokec@ccs.neu.edu>
46 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 *
48 * This program is free software; you can redistribute it and/or
49 * modify it under the terms of the GNU General Public License
50 * as published by the Free Software Foundation; either version
51 * 2 of the License, or (at your option) any later version.
52 *
53 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/mm.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/fcntl.h>
59#include <linux/socket.h>
60#include <linux/in.h>
61#include <linux/inet.h>
62#include <linux/netdevice.h>
63#include <linux/if_packet.h>
64#include <linux/wireless.h>
Herbert Xuffbc6112007-02-04 23:33:10 -080065#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
Neil Horman0e3125c2010-11-16 10:26:47 -080068#include <linux/vmalloc.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020069#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <net/ip.h>
71#include <net/protocol.h>
72#include <linux/skbuff.h>
73#include <net/sock.h>
74#include <linux/errno.h>
75#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <asm/uaccess.h>
77#include <asm/ioctls.h>
78#include <asm/page.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040079#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <asm/io.h>
81#include <linux/proc_fs.h>
82#include <linux/seq_file.h>
83#include <linux/poll.h>
84#include <linux/module.h>
85#include <linux/init.h>
Herbert Xu905db442009-01-30 14:12:06 -080086#include <linux/mutex.h>
Eric Dumazet05423b22009-10-26 18:40:35 -070087#include <linux/if_vlan.h>
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -080088#include <linux/virtio_net.h>
Richard Cochraned85b562010-04-07 22:41:28 +000089#include <linux/errqueue.h>
Scott McMillan614f60f2010-06-02 05:53:56 -070090#include <linux/net_tstamp.h>
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +020091#include <linux/reciprocal_div.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#ifdef CONFIG_INET
93#include <net/inet_common.h>
94#endif
95
Pavel Emelyanov2787b042012-08-13 05:49:39 +000096#include "internal.h"
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 Assumptions:
100 - if device has no dev->hard_header routine, it adds and removes ll header
101 inside itself. In this case ll header is invisible outside of device,
102 but higher levels still should reserve dev->hard_header_len.
103 Some devices are enough clever to reallocate skb, when header
104 will not fit to reserved space (tunnel), another ones are silly
105 (PPP).
106 - packet socket receives packets with pulled ll header,
107 so that SOCK_RAW should push it back.
108
109On receive:
110-----------
111
112Incoming, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700113 mac_header -> ll header
114 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116Outgoing, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700117 mac_header -> ll header
118 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120Incoming, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700121 mac_header -> UNKNOWN position. It is very likely, that it points to ll
122 header. PPP makes it, that is wrong, because introduce
YOSHIFUJI Hideakidb0c58f2007-07-19 10:44:35 +0900123 assymetry between rx and tx paths.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700124 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126Outgoing, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700127 mac_header -> data. ll header is still not built!
128 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130Resume
131 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
132
133
134On transmit:
135------------
136
137dev->hard_header != NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700138 mac_header -> ll header
139 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141dev->hard_header == NULL (ll header is added by device, we cannot control it)
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700142 mac_header -> data
143 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 We should set nh.raw on output to correct posistion,
146 packet classifier depends on it.
147 */
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/* Private packet socket structures. */
150
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700151/* identical to struct packet_mreq except it has
152 * a longer address field.
153 */
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000154struct packet_mreq_max {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700155 int mr_ifindex;
156 unsigned short mr_type;
157 unsigned short mr_alen;
158 unsigned char mr_address[MAX_ADDR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159};
David S. Millera2efcfa2007-05-29 13:12:50 -0700160
Daniel Borkmann184f4892013-04-16 01:57:46 +0000161union tpacket_uhdr {
162 struct tpacket_hdr *h1;
163 struct tpacket2_hdr *h2;
164 struct tpacket3_hdr *h3;
165 void *raw;
166};
167
chetan lokef6fb8f102011-08-19 10:18:16 +0000168static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -0700169 int closing, int tx_ring);
170
chetan lokef6fb8f102011-08-19 10:18:16 +0000171#define V3_ALIGNMENT (8)
172
chetan lokebc59ba32011-08-25 10:43:30 +0000173#define BLK_HDR_LEN (ALIGN(sizeof(struct tpacket_block_desc), V3_ALIGNMENT))
chetan lokef6fb8f102011-08-19 10:18:16 +0000174
175#define BLK_PLUS_PRIV(sz_of_priv) \
176 (BLK_HDR_LEN + ALIGN((sz_of_priv), V3_ALIGNMENT))
177
chetan lokef6fb8f102011-08-19 10:18:16 +0000178#define PGV_FROM_VMALLOC 1
Johann Baudy69e3c752009-05-18 22:11:22 -0700179
chetan lokef6fb8f102011-08-19 10:18:16 +0000180#define BLOCK_STATUS(x) ((x)->hdr.bh1.block_status)
181#define BLOCK_NUM_PKTS(x) ((x)->hdr.bh1.num_pkts)
182#define BLOCK_O2FP(x) ((x)->hdr.bh1.offset_to_first_pkt)
183#define BLOCK_LEN(x) ((x)->hdr.bh1.blk_len)
184#define BLOCK_SNUM(x) ((x)->hdr.bh1.seq_num)
185#define BLOCK_O2PRIV(x) ((x)->offset_to_priv)
186#define BLOCK_PRIV(x) ((void *)((char *)(x) + BLOCK_O2PRIV(x)))
187
Johann Baudy69e3c752009-05-18 22:11:22 -0700188struct packet_sock;
189static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +0000190static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
191 struct packet_type *pt, struct net_device *orig_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
chetan lokef6fb8f102011-08-19 10:18:16 +0000193static void *packet_previous_frame(struct packet_sock *po,
194 struct packet_ring_buffer *rb,
195 int status);
196static void packet_increment_head(struct packet_ring_buffer *buff);
chetan lokebc59ba32011-08-25 10:43:30 +0000197static int prb_curr_blk_in_use(struct tpacket_kbdq_core *,
198 struct tpacket_block_desc *);
199static void *prb_dispatch_next_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f102011-08-19 10:18:16 +0000200 struct packet_sock *);
chetan lokebc59ba32011-08-25 10:43:30 +0000201static void prb_retire_current_block(struct tpacket_kbdq_core *,
chetan lokef6fb8f102011-08-19 10:18:16 +0000202 struct packet_sock *, unsigned int status);
chetan lokebc59ba32011-08-25 10:43:30 +0000203static int prb_queue_frozen(struct tpacket_kbdq_core *);
204static void prb_open_block(struct tpacket_kbdq_core *,
205 struct tpacket_block_desc *);
chetan lokef6fb8f102011-08-19 10:18:16 +0000206static void prb_retire_rx_blk_timer_expired(unsigned long);
chetan lokebc59ba32011-08-25 10:43:30 +0000207static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *);
208static void prb_init_blk_timer(struct packet_sock *,
209 struct tpacket_kbdq_core *,
210 void (*func) (unsigned long));
211static void prb_fill_rxhash(struct tpacket_kbdq_core *, struct tpacket3_hdr *);
212static void prb_clear_rxhash(struct tpacket_kbdq_core *,
213 struct tpacket3_hdr *);
214static void prb_fill_vlan_info(struct tpacket_kbdq_core *,
215 struct tpacket3_hdr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static void packet_flush_mclist(struct sock *sk);
217
Herbert Xuffbc6112007-02-04 23:33:10 -0800218struct packet_skb_cb {
219 unsigned int origlen;
220 union {
221 struct sockaddr_pkt pkt;
222 struct sockaddr_ll ll;
223 } sa;
224};
225
226#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800227
chetan lokebc59ba32011-08-25 10:43:30 +0000228#define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
chetan lokef6fb8f102011-08-19 10:18:16 +0000229#define GET_PBLOCK_DESC(x, bid) \
chetan lokebc59ba32011-08-25 10:43:30 +0000230 ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
chetan lokef6fb8f102011-08-19 10:18:16 +0000231#define GET_CURR_PBLOCK_DESC_FROM_CORE(x) \
chetan lokebc59ba32011-08-25 10:43:30 +0000232 ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
chetan lokef6fb8f102011-08-19 10:18:16 +0000233#define GET_NEXT_PRB_BLK_NUM(x) \
234 (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
235 ((x)->kactive_blk_num+1) : 0)
236
David S. Millerdc99f602011-07-05 01:45:05 -0700237static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
238static void __fanout_link(struct sock *sk, struct packet_sock *po);
239
David S. Millerce06b032011-07-04 01:44:29 -0700240/* register_prot_hook must be invoked with the po->bind_lock held,
241 * or from a context in which asynchronous accesses to the packet
242 * socket is not possible (packet_create()).
243 */
244static void register_prot_hook(struct sock *sk)
245{
246 struct packet_sock *po = pkt_sk(sk);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100247
David S. Millerce06b032011-07-04 01:44:29 -0700248 if (!po->running) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100249 if (po->fanout) {
David S. Millerdc99f602011-07-05 01:45:05 -0700250 __fanout_link(sk, po);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100251 } else {
David S. Millerdc99f602011-07-05 01:45:05 -0700252 dev_add_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100253 rcu_assign_pointer(po->cached_dev, po->prot_hook.dev);
254 }
255
David S. Millerce06b032011-07-04 01:44:29 -0700256 sock_hold(sk);
257 po->running = 1;
258 }
259}
260
261/* {,__}unregister_prot_hook() must be invoked with the po->bind_lock
262 * held. If the sync parameter is true, we will temporarily drop
263 * the po->bind_lock and do a synchronize_net to make sure no
264 * asynchronous packet processing paths still refer to the elements
265 * of po->prot_hook. If the sync parameter is false, it is the
266 * callers responsibility to take care of this.
267 */
268static void __unregister_prot_hook(struct sock *sk, bool sync)
269{
270 struct packet_sock *po = pkt_sk(sk);
271
272 po->running = 0;
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100273 if (po->fanout) {
David S. Millerdc99f602011-07-05 01:45:05 -0700274 __fanout_unlink(sk, po);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100275 } else {
David S. Millerdc99f602011-07-05 01:45:05 -0700276 __dev_remove_pack(&po->prot_hook);
Daniel Borkmanne40526c2013-11-21 16:50:58 +0100277 RCU_INIT_POINTER(po->cached_dev, NULL);
278 }
279
David S. Millerce06b032011-07-04 01:44:29 -0700280 __sock_put(sk);
281
282 if (sync) {
283 spin_unlock(&po->bind_lock);
284 synchronize_net();
285 spin_lock(&po->bind_lock);
286 }
287}
288
289static void unregister_prot_hook(struct sock *sk, bool sync)
290{
291 struct packet_sock *po = pkt_sk(sk);
292
293 if (po->running)
294 __unregister_prot_hook(sk, sync);
295}
296
Changli Gaof6dafa92010-12-07 04:26:16 +0000297static inline __pure struct page *pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000298{
299 if (is_vmalloc_addr(addr))
300 return vmalloc_to_page(addr);
301 return virt_to_page(addr);
302}
303
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700304static void __packet_set_status(struct packet_sock *po, void *frame, int status)
305{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000306 union tpacket_uhdr h;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700307
308 h.raw = frame;
309 switch (po->tp_version) {
310 case TPACKET_V1:
311 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000312 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700313 break;
314 case TPACKET_V2:
315 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000316 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700317 break;
chetan lokef6fb8f102011-08-19 10:18:16 +0000318 case TPACKET_V3:
Johann Baudy69e3c752009-05-18 22:11:22 -0700319 default:
chetan lokef6fb8f102011-08-19 10:18:16 +0000320 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700321 BUG();
322 }
323
324 smp_wmb();
325}
326
327static int __packet_get_status(struct packet_sock *po, void *frame)
328{
Daniel Borkmann184f4892013-04-16 01:57:46 +0000329 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700330
331 smp_rmb();
332
333 h.raw = frame;
334 switch (po->tp_version) {
335 case TPACKET_V1:
Changli Gao0af55bb2010-12-01 02:52:20 +0000336 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700337 return h.h1->tp_status;
338 case TPACKET_V2:
Changli Gao0af55bb2010-12-01 02:52:20 +0000339 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700340 return h.h2->tp_status;
chetan lokef6fb8f102011-08-19 10:18:16 +0000341 case TPACKET_V3:
Johann Baudy69e3c752009-05-18 22:11:22 -0700342 default:
chetan lokef6fb8f102011-08-19 10:18:16 +0000343 WARN(1, "TPACKET version not supported.\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700344 BUG();
345 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
Johann Baudy69e3c752009-05-18 22:11:22 -0700348
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000349static __u32 tpacket_get_timestamp(struct sk_buff *skb, struct timespec *ts,
350 unsigned int flags)
Daniel Borkmann7a513842013-04-23 00:39:29 +0000351{
352 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
353
354 if (shhwtstamps) {
355 if ((flags & SOF_TIMESTAMPING_SYS_HARDWARE) &&
356 ktime_to_timespec_cond(shhwtstamps->syststamp, ts))
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000357 return TP_STATUS_TS_SYS_HARDWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000358 if ((flags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
359 ktime_to_timespec_cond(shhwtstamps->hwtstamp, ts))
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000360 return TP_STATUS_TS_RAW_HARDWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000361 }
362
363 if (ktime_to_timespec_cond(skb->tstamp, ts))
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000364 return TP_STATUS_TS_SOFTWARE;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000365
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000366 return 0;
Daniel Borkmann7a513842013-04-23 00:39:29 +0000367}
368
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000369static __u32 __packet_set_timestamp(struct packet_sock *po, void *frame,
370 struct sk_buff *skb)
Willem de Bruijn2e313962013-04-23 00:39:28 +0000371{
372 union tpacket_uhdr h;
373 struct timespec ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000374 __u32 ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000375
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000376 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
377 return 0;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000378
379 h.raw = frame;
380 switch (po->tp_version) {
381 case TPACKET_V1:
382 h.h1->tp_sec = ts.tv_sec;
383 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
384 break;
385 case TPACKET_V2:
386 h.h2->tp_sec = ts.tv_sec;
387 h.h2->tp_nsec = ts.tv_nsec;
388 break;
389 case TPACKET_V3:
390 default:
391 WARN(1, "TPACKET version not supported.\n");
392 BUG();
393 }
394
395 /* one flush is safe, as both fields always lie on the same cacheline */
396 flush_dcache_page(pgv_to_page(&h.h1->tp_sec));
397 smp_wmb();
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +0000398
399 return ts_status;
Willem de Bruijn2e313962013-04-23 00:39:28 +0000400}
401
Johann Baudy69e3c752009-05-18 22:11:22 -0700402static void *packet_lookup_frame(struct packet_sock *po,
403 struct packet_ring_buffer *rb,
404 unsigned int position,
405 int status)
406{
407 unsigned int pg_vec_pos, frame_offset;
Daniel Borkmann184f4892013-04-16 01:57:46 +0000408 union tpacket_uhdr h;
Johann Baudy69e3c752009-05-18 22:11:22 -0700409
410 pg_vec_pos = position / rb->frames_per_block;
411 frame_offset = position % rb->frames_per_block;
412
Neil Horman0e3125c2010-11-16 10:26:47 -0800413 h.raw = rb->pg_vec[pg_vec_pos].buffer +
414 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700415
416 if (status != __packet_get_status(po, h.raw))
417 return NULL;
418
419 return h.raw;
420}
421
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000422static void *packet_current_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -0700423 struct packet_ring_buffer *rb,
424 int status)
425{
426 return packet_lookup_frame(po, rb, rb->head, status);
427}
428
chetan lokebc59ba32011-08-25 10:43:30 +0000429static void prb_del_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f102011-08-19 10:18:16 +0000430{
431 del_timer_sync(&pkc->retire_blk_timer);
432}
433
434static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
435 int tx_ring,
436 struct sk_buff_head *rb_queue)
437{
chetan lokebc59ba32011-08-25 10:43:30 +0000438 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f102011-08-19 10:18:16 +0000439
Duan Jiong22781a52013-12-06 13:29:36 +0800440 pkc = tx_ring ? GET_PBDQC_FROM_RB(&po->tx_ring) :
441 GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f102011-08-19 10:18:16 +0000442
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100443 spin_lock_bh(&rb_queue->lock);
chetan lokef6fb8f102011-08-19 10:18:16 +0000444 pkc->delete_blk_timer = 1;
Veaceslav Falicoec6f8092013-11-29 09:53:23 +0100445 spin_unlock_bh(&rb_queue->lock);
chetan lokef6fb8f102011-08-19 10:18:16 +0000446
447 prb_del_retire_blk_timer(pkc);
448}
449
450static void prb_init_blk_timer(struct packet_sock *po,
chetan lokebc59ba32011-08-25 10:43:30 +0000451 struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000452 void (*func) (unsigned long))
453{
454 init_timer(&pkc->retire_blk_timer);
455 pkc->retire_blk_timer.data = (long)po;
456 pkc->retire_blk_timer.function = func;
457 pkc->retire_blk_timer.expires = jiffies;
458}
459
460static void prb_setup_retire_blk_timer(struct packet_sock *po, int tx_ring)
461{
chetan lokebc59ba32011-08-25 10:43:30 +0000462 struct tpacket_kbdq_core *pkc;
chetan lokef6fb8f102011-08-19 10:18:16 +0000463
464 if (tx_ring)
465 BUG();
466
Duan Jiong22781a52013-12-06 13:29:36 +0800467 pkc = tx_ring ? GET_PBDQC_FROM_RB(&po->tx_ring) :
468 GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f102011-08-19 10:18:16 +0000469 prb_init_blk_timer(po, pkc, prb_retire_rx_blk_timer_expired);
470}
471
472static int prb_calc_retire_blk_tmo(struct packet_sock *po,
473 int blk_size_in_bytes)
474{
475 struct net_device *dev;
476 unsigned int mbits = 0, msec = 0, div = 0, tmo = 0;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000477 struct ethtool_cmd ecmd;
478 int err;
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000479 u32 speed;
chetan lokef6fb8f102011-08-19 10:18:16 +0000480
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000481 rtnl_lock();
482 dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex);
483 if (unlikely(!dev)) {
484 rtnl_unlock();
chetan lokef6fb8f102011-08-19 10:18:16 +0000485 return DEFAULT_PRB_RETIRE_TOV;
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000486 }
487 err = __ethtool_get_settings(dev, &ecmd);
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000488 speed = ethtool_cmd_speed(&ecmd);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000489 rtnl_unlock();
490 if (!err) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000491 /*
492 * If the link speed is so slow you don't really
493 * need to worry about perf anyways
494 */
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000495 if (speed < SPEED_1000 || speed == SPEED_UNKNOWN) {
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000496 return DEFAULT_PRB_RETIRE_TOV;
parav.pandit@emulex.come440cf22012-06-27 03:56:12 +0000497 } else {
498 msec = 1;
499 div = speed / 1000;
chetan lokef6fb8f102011-08-19 10:18:16 +0000500 }
501 }
502
503 mbits = (blk_size_in_bytes * 8) / (1024 * 1024);
504
505 if (div)
506 mbits /= div;
507
508 tmo = mbits * msec;
509
510 if (div)
511 return tmo+1;
512 return tmo;
513}
514
chetan lokebc59ba32011-08-25 10:43:30 +0000515static void prb_init_ft_ops(struct tpacket_kbdq_core *p1,
chetan lokef6fb8f102011-08-19 10:18:16 +0000516 union tpacket_req_u *req_u)
517{
518 p1->feature_req_word = req_u->req3.tp_feature_req_word;
519}
520
521static void init_prb_bdqc(struct packet_sock *po,
522 struct packet_ring_buffer *rb,
523 struct pgv *pg_vec,
524 union tpacket_req_u *req_u, int tx_ring)
525{
Duan Jiong22781a52013-12-06 13:29:36 +0800526 struct tpacket_kbdq_core *p1 = GET_PBDQC_FROM_RB(rb);
chetan lokebc59ba32011-08-25 10:43:30 +0000527 struct tpacket_block_desc *pbd;
chetan lokef6fb8f102011-08-19 10:18:16 +0000528
529 memset(p1, 0x0, sizeof(*p1));
530
531 p1->knxt_seq_num = 1;
532 p1->pkbdq = pg_vec;
chetan lokebc59ba32011-08-25 10:43:30 +0000533 pbd = (struct tpacket_block_desc *)pg_vec[0].buffer;
Joe Perchese3192692012-06-03 17:41:40 +0000534 p1->pkblk_start = pg_vec[0].buffer;
chetan lokef6fb8f102011-08-19 10:18:16 +0000535 p1->kblk_size = req_u->req3.tp_block_size;
536 p1->knum_blocks = req_u->req3.tp_block_nr;
537 p1->hdrlen = po->tp_hdrlen;
538 p1->version = po->tp_version;
539 p1->last_kactive_blk_num = 0;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000540 po->stats.stats3.tp_freeze_q_cnt = 0;
chetan lokef6fb8f102011-08-19 10:18:16 +0000541 if (req_u->req3.tp_retire_blk_tov)
542 p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov;
543 else
544 p1->retire_blk_tov = prb_calc_retire_blk_tmo(po,
545 req_u->req3.tp_block_size);
546 p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov);
547 p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv;
548
549 prb_init_ft_ops(p1, req_u);
550 prb_setup_retire_blk_timer(po, tx_ring);
551 prb_open_block(p1, pbd);
552}
553
554/* Do NOT update the last_blk_num first.
555 * Assumes sk_buff_head lock is held.
556 */
chetan lokebc59ba32011-08-25 10:43:30 +0000557static void _prb_refresh_rx_retire_blk_timer(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f102011-08-19 10:18:16 +0000558{
559 mod_timer(&pkc->retire_blk_timer,
560 jiffies + pkc->tov_in_jiffies);
561 pkc->last_kactive_blk_num = pkc->kactive_blk_num;
562}
563
564/*
565 * Timer logic:
566 * 1) We refresh the timer only when we open a block.
567 * By doing this we don't waste cycles refreshing the timer
568 * on packet-by-packet basis.
569 *
570 * With a 1MB block-size, on a 1Gbps line, it will take
571 * i) ~8 ms to fill a block + ii) memcpy etc.
572 * In this cut we are not accounting for the memcpy time.
573 *
574 * So, if the user sets the 'tmo' to 10ms then the timer
575 * will never fire while the block is still getting filled
576 * (which is what we want). However, the user could choose
577 * to close a block early and that's fine.
578 *
579 * But when the timer does fire, we check whether or not to refresh it.
580 * Since the tmo granularity is in msecs, it is not too expensive
581 * to refresh the timer, lets say every '8' msecs.
582 * Either the user can set the 'tmo' or we can derive it based on
583 * a) line-speed and b) block-size.
584 * prb_calc_retire_blk_tmo() calculates the tmo.
585 *
586 */
587static void prb_retire_rx_blk_timer_expired(unsigned long data)
588{
589 struct packet_sock *po = (struct packet_sock *)data;
Duan Jiong22781a52013-12-06 13:29:36 +0800590 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f102011-08-19 10:18:16 +0000591 unsigned int frozen;
chetan lokebc59ba32011-08-25 10:43:30 +0000592 struct tpacket_block_desc *pbd;
chetan lokef6fb8f102011-08-19 10:18:16 +0000593
594 spin_lock(&po->sk.sk_receive_queue.lock);
595
596 frozen = prb_queue_frozen(pkc);
597 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
598
599 if (unlikely(pkc->delete_blk_timer))
600 goto out;
601
602 /* We only need to plug the race when the block is partially filled.
603 * tpacket_rcv:
604 * lock(); increment BLOCK_NUM_PKTS; unlock()
605 * copy_bits() is in progress ...
606 * timer fires on other cpu:
607 * we can't retire the current block because copy_bits
608 * is in progress.
609 *
610 */
611 if (BLOCK_NUM_PKTS(pbd)) {
612 while (atomic_read(&pkc->blk_fill_in_prog)) {
613 /* Waiting for skb_copy_bits to finish... */
614 cpu_relax();
615 }
616 }
617
618 if (pkc->last_kactive_blk_num == pkc->kactive_blk_num) {
619 if (!frozen) {
620 prb_retire_current_block(pkc, po, TP_STATUS_BLK_TMO);
621 if (!prb_dispatch_next_block(pkc, po))
622 goto refresh_timer;
623 else
624 goto out;
625 } else {
626 /* Case 1. Queue was frozen because user-space was
627 * lagging behind.
628 */
629 if (prb_curr_blk_in_use(pkc, pbd)) {
630 /*
631 * Ok, user-space is still behind.
632 * So just refresh the timer.
633 */
634 goto refresh_timer;
635 } else {
636 /* Case 2. queue was frozen,user-space caught up,
637 * now the link went idle && the timer fired.
638 * We don't have a block to close.So we open this
639 * block and restart the timer.
640 * opening a block thaws the queue,restarts timer
641 * Thawing/timer-refresh is a side effect.
642 */
643 prb_open_block(pkc, pbd);
644 goto out;
645 }
646 }
647 }
648
649refresh_timer:
650 _prb_refresh_rx_retire_blk_timer(pkc);
651
652out:
653 spin_unlock(&po->sk.sk_receive_queue.lock);
654}
655
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000656static void prb_flush_block(struct tpacket_kbdq_core *pkc1,
chetan lokebc59ba32011-08-25 10:43:30 +0000657 struct tpacket_block_desc *pbd1, __u32 status)
chetan lokef6fb8f102011-08-19 10:18:16 +0000658{
659 /* Flush everything minus the block header */
660
661#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
662 u8 *start, *end;
663
664 start = (u8 *)pbd1;
665
666 /* Skip the block header(we know header WILL fit in 4K) */
667 start += PAGE_SIZE;
668
669 end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end);
670 for (; start < end; start += PAGE_SIZE)
671 flush_dcache_page(pgv_to_page(start));
672
673 smp_wmb();
674#endif
675
676 /* Now update the block status. */
677
678 BLOCK_STATUS(pbd1) = status;
679
680 /* Flush the block header */
681
682#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
683 start = (u8 *)pbd1;
684 flush_dcache_page(pgv_to_page(start));
685
686 smp_wmb();
687#endif
688}
689
690/*
691 * Side effect:
692 *
693 * 1) flush the block
694 * 2) Increment active_blk_num
695 *
696 * Note:We DONT refresh the timer on purpose.
697 * Because almost always the next block will be opened.
698 */
chetan lokebc59ba32011-08-25 10:43:30 +0000699static void prb_close_block(struct tpacket_kbdq_core *pkc1,
700 struct tpacket_block_desc *pbd1,
chetan lokef6fb8f102011-08-19 10:18:16 +0000701 struct packet_sock *po, unsigned int stat)
702{
703 __u32 status = TP_STATUS_USER | stat;
704
705 struct tpacket3_hdr *last_pkt;
chetan lokebc59ba32011-08-25 10:43:30 +0000706 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
chetan lokef6fb8f102011-08-19 10:18:16 +0000707
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000708 if (po->stats.stats3.tp_drops)
chetan lokef6fb8f102011-08-19 10:18:16 +0000709 status |= TP_STATUS_LOSING;
710
711 last_pkt = (struct tpacket3_hdr *)pkc1->prev;
712 last_pkt->tp_next_offset = 0;
713
714 /* Get the ts of the last pkt */
715 if (BLOCK_NUM_PKTS(pbd1)) {
716 h1->ts_last_pkt.ts_sec = last_pkt->tp_sec;
717 h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec;
718 } else {
719 /* Ok, we tmo'd - so get the current time */
720 struct timespec ts;
721 getnstimeofday(&ts);
722 h1->ts_last_pkt.ts_sec = ts.tv_sec;
723 h1->ts_last_pkt.ts_nsec = ts.tv_nsec;
724 }
725
726 smp_wmb();
727
728 /* Flush the block */
729 prb_flush_block(pkc1, pbd1, status);
730
731 pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1);
732}
733
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000734static void prb_thaw_queue(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f102011-08-19 10:18:16 +0000735{
736 pkc->reset_pending_on_curr_blk = 0;
737}
738
739/*
740 * Side effect of opening a block:
741 *
742 * 1) prb_queue is thawed.
743 * 2) retire_blk_timer is refreshed.
744 *
745 */
chetan lokebc59ba32011-08-25 10:43:30 +0000746static void prb_open_block(struct tpacket_kbdq_core *pkc1,
747 struct tpacket_block_desc *pbd1)
chetan lokef6fb8f102011-08-19 10:18:16 +0000748{
749 struct timespec ts;
chetan lokebc59ba32011-08-25 10:43:30 +0000750 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1;
chetan lokef6fb8f102011-08-19 10:18:16 +0000751
752 smp_rmb();
753
Daniel Borkmann8da30562013-05-03 02:57:00 +0000754 /* We could have just memset this but we will lose the
755 * flexibility of making the priv area sticky
756 */
chetan lokef6fb8f102011-08-19 10:18:16 +0000757
Daniel Borkmann8da30562013-05-03 02:57:00 +0000758 BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++;
759 BLOCK_NUM_PKTS(pbd1) = 0;
760 BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
chetan lokef6fb8f102011-08-19 10:18:16 +0000761
Daniel Borkmann8da30562013-05-03 02:57:00 +0000762 getnstimeofday(&ts);
chetan lokef6fb8f102011-08-19 10:18:16 +0000763
Daniel Borkmann8da30562013-05-03 02:57:00 +0000764 h1->ts_first_pkt.ts_sec = ts.tv_sec;
765 h1->ts_first_pkt.ts_nsec = ts.tv_nsec;
chetan lokef6fb8f102011-08-19 10:18:16 +0000766
Daniel Borkmann8da30562013-05-03 02:57:00 +0000767 pkc1->pkblk_start = (char *)pbd1;
768 pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
769
770 BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv);
771 BLOCK_O2PRIV(pbd1) = BLK_HDR_LEN;
772
773 pbd1->version = pkc1->version;
774 pkc1->prev = pkc1->nxt_offset;
775 pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size;
776
777 prb_thaw_queue(pkc1);
778 _prb_refresh_rx_retire_blk_timer(pkc1);
779
780 smp_wmb();
chetan lokef6fb8f102011-08-19 10:18:16 +0000781}
782
783/*
784 * Queue freeze logic:
785 * 1) Assume tp_block_nr = 8 blocks.
786 * 2) At time 't0', user opens Rx ring.
787 * 3) Some time past 't0', kernel starts filling blocks starting from 0 .. 7
788 * 4) user-space is either sleeping or processing block '0'.
789 * 5) tpacket_rcv is currently filling block '7', since there is no space left,
790 * it will close block-7,loop around and try to fill block '0'.
791 * call-flow:
792 * __packet_lookup_frame_in_block
793 * prb_retire_current_block()
794 * prb_dispatch_next_block()
795 * |->(BLOCK_STATUS == USER) evaluates to true
796 * 5.1) Since block-0 is currently in-use, we just freeze the queue.
797 * 6) Now there are two cases:
798 * 6.1) Link goes idle right after the queue is frozen.
799 * But remember, the last open_block() refreshed the timer.
800 * When this timer expires,it will refresh itself so that we can
801 * re-open block-0 in near future.
802 * 6.2) Link is busy and keeps on receiving packets. This is a simple
803 * case and __packet_lookup_frame_in_block will check if block-0
804 * is free and can now be re-used.
805 */
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000806static void prb_freeze_queue(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000807 struct packet_sock *po)
808{
809 pkc->reset_pending_on_curr_blk = 1;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +0000810 po->stats.stats3.tp_freeze_q_cnt++;
chetan lokef6fb8f102011-08-19 10:18:16 +0000811}
812
813#define TOTAL_PKT_LEN_INCL_ALIGN(length) (ALIGN((length), V3_ALIGNMENT))
814
815/*
816 * If the next block is free then we will dispatch it
817 * and return a good offset.
818 * Else, we will freeze the queue.
819 * So, caller must check the return value.
820 */
chetan lokebc59ba32011-08-25 10:43:30 +0000821static void *prb_dispatch_next_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000822 struct packet_sock *po)
823{
chetan lokebc59ba32011-08-25 10:43:30 +0000824 struct tpacket_block_desc *pbd;
chetan lokef6fb8f102011-08-19 10:18:16 +0000825
826 smp_rmb();
827
828 /* 1. Get current block num */
829 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
830
831 /* 2. If this block is currently in_use then freeze the queue */
832 if (TP_STATUS_USER & BLOCK_STATUS(pbd)) {
833 prb_freeze_queue(pkc, po);
834 return NULL;
835 }
836
837 /*
838 * 3.
839 * open this block and return the offset where the first packet
840 * needs to get stored.
841 */
842 prb_open_block(pkc, pbd);
843 return (void *)pkc->nxt_offset;
844}
845
chetan lokebc59ba32011-08-25 10:43:30 +0000846static void prb_retire_current_block(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000847 struct packet_sock *po, unsigned int status)
848{
chetan lokebc59ba32011-08-25 10:43:30 +0000849 struct tpacket_block_desc *pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
chetan lokef6fb8f102011-08-19 10:18:16 +0000850
851 /* retire/close the current block */
852 if (likely(TP_STATUS_KERNEL == BLOCK_STATUS(pbd))) {
853 /*
854 * Plug the case where copy_bits() is in progress on
855 * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't
856 * have space to copy the pkt in the current block and
857 * called prb_retire_current_block()
858 *
859 * We don't need to worry about the TMO case because
860 * the timer-handler already handled this case.
861 */
862 if (!(status & TP_STATUS_BLK_TMO)) {
863 while (atomic_read(&pkc->blk_fill_in_prog)) {
864 /* Waiting for skb_copy_bits to finish... */
865 cpu_relax();
866 }
867 }
868 prb_close_block(pkc, pbd, po, status);
869 return;
870 }
chetan lokef6fb8f102011-08-19 10:18:16 +0000871}
872
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000873static int prb_curr_blk_in_use(struct tpacket_kbdq_core *pkc,
chetan lokebc59ba32011-08-25 10:43:30 +0000874 struct tpacket_block_desc *pbd)
chetan lokef6fb8f102011-08-19 10:18:16 +0000875{
876 return TP_STATUS_USER & BLOCK_STATUS(pbd);
877}
878
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000879static int prb_queue_frozen(struct tpacket_kbdq_core *pkc)
chetan lokef6fb8f102011-08-19 10:18:16 +0000880{
881 return pkc->reset_pending_on_curr_blk;
882}
883
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000884static void prb_clear_blk_fill_status(struct packet_ring_buffer *rb)
chetan lokef6fb8f102011-08-19 10:18:16 +0000885{
chetan lokebc59ba32011-08-25 10:43:30 +0000886 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
chetan lokef6fb8f102011-08-19 10:18:16 +0000887 atomic_dec(&pkc->blk_fill_in_prog);
888}
889
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000890static void prb_fill_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000891 struct tpacket3_hdr *ppd)
892{
893 ppd->hv1.tp_rxhash = skb_get_rxhash(pkc->skb);
894}
895
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000896static void prb_clear_rxhash(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000897 struct tpacket3_hdr *ppd)
898{
899 ppd->hv1.tp_rxhash = 0;
900}
901
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000902static void prb_fill_vlan_info(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000903 struct tpacket3_hdr *ppd)
904{
905 if (vlan_tx_tag_present(pkc->skb)) {
906 ppd->hv1.tp_vlan_tci = vlan_tx_tag_get(pkc->skb);
907 ppd->tp_status = TP_STATUS_VLAN_VALID;
908 } else {
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +0000909 ppd->hv1.tp_vlan_tci = 0;
910 ppd->tp_status = TP_STATUS_AVAILABLE;
chetan lokef6fb8f102011-08-19 10:18:16 +0000911 }
912}
913
chetan lokebc59ba32011-08-25 10:43:30 +0000914static void prb_run_all_ft_ops(struct tpacket_kbdq_core *pkc,
chetan lokef6fb8f102011-08-19 10:18:16 +0000915 struct tpacket3_hdr *ppd)
916{
917 prb_fill_vlan_info(pkc, ppd);
918
919 if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH)
920 prb_fill_rxhash(pkc, ppd);
921 else
922 prb_clear_rxhash(pkc, ppd);
923}
924
Olof Johanssoneea49cc92011-11-02 11:00:49 +0000925static void prb_fill_curr_block(char *curr,
chetan lokebc59ba32011-08-25 10:43:30 +0000926 struct tpacket_kbdq_core *pkc,
927 struct tpacket_block_desc *pbd,
chetan lokef6fb8f102011-08-19 10:18:16 +0000928 unsigned int len)
929{
930 struct tpacket3_hdr *ppd;
931
932 ppd = (struct tpacket3_hdr *)curr;
933 ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len);
934 pkc->prev = curr;
935 pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len);
936 BLOCK_LEN(pbd) += TOTAL_PKT_LEN_INCL_ALIGN(len);
937 BLOCK_NUM_PKTS(pbd) += 1;
938 atomic_inc(&pkc->blk_fill_in_prog);
939 prb_run_all_ft_ops(pkc, ppd);
940}
941
942/* Assumes caller has the sk->rx_queue.lock */
943static void *__packet_lookup_frame_in_block(struct packet_sock *po,
944 struct sk_buff *skb,
945 int status,
946 unsigned int len
947 )
948{
chetan lokebc59ba32011-08-25 10:43:30 +0000949 struct tpacket_kbdq_core *pkc;
950 struct tpacket_block_desc *pbd;
chetan lokef6fb8f102011-08-19 10:18:16 +0000951 char *curr, *end;
952
Joe Perchese3192692012-06-03 17:41:40 +0000953 pkc = GET_PBDQC_FROM_RB(&po->rx_ring);
chetan lokef6fb8f102011-08-19 10:18:16 +0000954 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
955
956 /* Queue is frozen when user space is lagging behind */
957 if (prb_queue_frozen(pkc)) {
958 /*
959 * Check if that last block which caused the queue to freeze,
960 * is still in_use by user-space.
961 */
962 if (prb_curr_blk_in_use(pkc, pbd)) {
963 /* Can't record this packet */
964 return NULL;
965 } else {
966 /*
967 * Ok, the block was released by user-space.
968 * Now let's open that block.
969 * opening a block also thaws the queue.
970 * Thawing is a side effect.
971 */
972 prb_open_block(pkc, pbd);
973 }
974 }
975
976 smp_mb();
977 curr = pkc->nxt_offset;
978 pkc->skb = skb;
Joe Perchese3192692012-06-03 17:41:40 +0000979 end = (char *)pbd + pkc->kblk_size;
chetan lokef6fb8f102011-08-19 10:18:16 +0000980
981 /* first try the current block */
982 if (curr+TOTAL_PKT_LEN_INCL_ALIGN(len) < end) {
983 prb_fill_curr_block(curr, pkc, pbd, len);
984 return (void *)curr;
985 }
986
987 /* Ok, close the current block */
988 prb_retire_current_block(pkc, po, 0);
989
990 /* Now, try to dispatch the next block */
991 curr = (char *)prb_dispatch_next_block(pkc, po);
992 if (curr) {
993 pbd = GET_CURR_PBLOCK_DESC_FROM_CORE(pkc);
994 prb_fill_curr_block(curr, pkc, pbd, len);
995 return (void *)curr;
996 }
997
998 /*
999 * No free blocks are available.user_space hasn't caught up yet.
1000 * Queue was just frozen and now this packet will get dropped.
1001 */
1002 return NULL;
1003}
1004
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001005static void *packet_current_rx_frame(struct packet_sock *po,
chetan lokef6fb8f102011-08-19 10:18:16 +00001006 struct sk_buff *skb,
1007 int status, unsigned int len)
1008{
1009 char *curr = NULL;
1010 switch (po->tp_version) {
1011 case TPACKET_V1:
1012 case TPACKET_V2:
1013 curr = packet_lookup_frame(po, &po->rx_ring,
1014 po->rx_ring.head, status);
1015 return curr;
1016 case TPACKET_V3:
1017 return __packet_lookup_frame_in_block(po, skb, status, len);
1018 default:
1019 WARN(1, "TPACKET version not supported\n");
1020 BUG();
Ying Xue99aa3472012-08-06 16:27:10 +00001021 return NULL;
chetan lokef6fb8f102011-08-19 10:18:16 +00001022 }
1023}
1024
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001025static void *prb_lookup_block(struct packet_sock *po,
chetan lokef6fb8f102011-08-19 10:18:16 +00001026 struct packet_ring_buffer *rb,
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001027 unsigned int idx,
chetan lokef6fb8f102011-08-19 10:18:16 +00001028 int status)
1029{
chetan lokebc59ba32011-08-25 10:43:30 +00001030 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(rb);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001031 struct tpacket_block_desc *pbd = GET_PBLOCK_DESC(pkc, idx);
chetan lokef6fb8f102011-08-19 10:18:16 +00001032
1033 if (status != BLOCK_STATUS(pbd))
1034 return NULL;
1035 return pbd;
1036}
1037
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001038static int prb_previous_blk_num(struct packet_ring_buffer *rb)
chetan lokef6fb8f102011-08-19 10:18:16 +00001039{
1040 unsigned int prev;
1041 if (rb->prb_bdqc.kactive_blk_num)
1042 prev = rb->prb_bdqc.kactive_blk_num-1;
1043 else
1044 prev = rb->prb_bdqc.knum_blocks-1;
1045 return prev;
1046}
1047
1048/* Assumes caller has held the rx_queue.lock */
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001049static void *__prb_previous_block(struct packet_sock *po,
chetan lokef6fb8f102011-08-19 10:18:16 +00001050 struct packet_ring_buffer *rb,
1051 int status)
1052{
1053 unsigned int previous = prb_previous_blk_num(rb);
1054 return prb_lookup_block(po, rb, previous, status);
1055}
1056
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001057static void *packet_previous_rx_frame(struct packet_sock *po,
chetan lokef6fb8f102011-08-19 10:18:16 +00001058 struct packet_ring_buffer *rb,
1059 int status)
1060{
1061 if (po->tp_version <= TPACKET_V2)
1062 return packet_previous_frame(po, rb, status);
1063
1064 return __prb_previous_block(po, rb, status);
1065}
1066
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001067static void packet_increment_rx_head(struct packet_sock *po,
chetan lokef6fb8f102011-08-19 10:18:16 +00001068 struct packet_ring_buffer *rb)
1069{
1070 switch (po->tp_version) {
1071 case TPACKET_V1:
1072 case TPACKET_V2:
1073 return packet_increment_head(rb);
1074 case TPACKET_V3:
1075 default:
1076 WARN(1, "TPACKET version not supported.\n");
1077 BUG();
1078 return;
1079 }
1080}
1081
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001082static void *packet_previous_frame(struct packet_sock *po,
Johann Baudy69e3c752009-05-18 22:11:22 -07001083 struct packet_ring_buffer *rb,
1084 int status)
1085{
1086 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
1087 return packet_lookup_frame(po, rb, previous, status);
1088}
1089
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001090static void packet_increment_head(struct packet_ring_buffer *buff)
Johann Baudy69e3c752009-05-18 22:11:22 -07001091{
1092 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
1093}
1094
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001095static bool packet_rcv_has_room(struct packet_sock *po, struct sk_buff *skb)
1096{
1097 struct sock *sk = &po->sk;
1098 bool has_room;
1099
1100 if (po->prot_hook.func != tpacket_rcv)
1101 return (atomic_read(&sk->sk_rmem_alloc) + skb->truesize)
1102 <= sk->sk_rcvbuf;
1103
1104 spin_lock(&sk->sk_receive_queue.lock);
1105 if (po->tp_version == TPACKET_V3)
1106 has_room = prb_lookup_block(po, &po->rx_ring,
1107 po->rx_ring.prb_bdqc.kactive_blk_num,
1108 TP_STATUS_KERNEL);
1109 else
1110 has_room = packet_lookup_frame(po, &po->rx_ring,
1111 po->rx_ring.head,
1112 TP_STATUS_KERNEL);
1113 spin_unlock(&sk->sk_receive_queue.lock);
1114
1115 return has_room;
1116}
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118static void packet_sock_destruct(struct sock *sk)
1119{
Richard Cochraned85b562010-04-07 22:41:28 +00001120 skb_queue_purge(&sk->sk_error_queue);
1121
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001122 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
1123 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001126 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return;
1128 }
1129
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001130 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
David S. Millerdc99f602011-07-05 01:45:05 -07001133static int fanout_rr_next(struct packet_fanout *f, unsigned int num)
1134{
1135 int x = atomic_read(&f->rr_cur) + 1;
1136
1137 if (x >= num)
1138 x = 0;
1139
1140 return x;
1141}
1142
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001143static unsigned int fanout_demux_hash(struct packet_fanout *f,
1144 struct sk_buff *skb,
1145 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001146{
Daniel Borkmannf55d112e2013-08-28 22:13:10 +02001147 return reciprocal_divide(skb->rxhash, num);
David S. Millerdc99f602011-07-05 01:45:05 -07001148}
1149
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001150static unsigned int fanout_demux_lb(struct packet_fanout *f,
1151 struct sk_buff *skb,
1152 unsigned int num)
David S. Millerdc99f602011-07-05 01:45:05 -07001153{
1154 int cur, old;
1155
1156 cur = atomic_read(&f->rr_cur);
1157 while ((old = atomic_cmpxchg(&f->rr_cur, cur,
1158 fanout_rr_next(f, num))) != cur)
1159 cur = old;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001160 return cur;
David S. Millerdc99f602011-07-05 01:45:05 -07001161}
1162
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001163static unsigned int fanout_demux_cpu(struct packet_fanout *f,
1164 struct sk_buff *skb,
1165 unsigned int num)
David S. Miller95ec3eb2011-07-06 01:56:38 -07001166{
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001167 return smp_processor_id() % num;
1168}
David S. Miller95ec3eb2011-07-06 01:56:38 -07001169
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001170static unsigned int fanout_demux_rnd(struct packet_fanout *f,
1171 struct sk_buff *skb,
1172 unsigned int num)
1173{
1174 return reciprocal_divide(prandom_u32(), num);
1175}
1176
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001177static unsigned int fanout_demux_rollover(struct packet_fanout *f,
1178 struct sk_buff *skb,
1179 unsigned int idx, unsigned int skip,
1180 unsigned int num)
1181{
1182 unsigned int i, j;
1183
1184 i = j = min_t(int, f->next[idx], num - 1);
1185 do {
1186 if (i != skip && packet_rcv_has_room(pkt_sk(f->arr[i]), skb)) {
1187 if (i != j)
1188 f->next[idx] = i;
1189 return i;
1190 }
1191 if (++i == num)
1192 i = 0;
1193 } while (i != j);
1194
1195 return idx;
1196}
1197
1198static bool fanout_has_flag(struct packet_fanout *f, u16 flag)
1199{
1200 return f->flags & (flag >> 8);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001201}
1202
David S. Miller95ec3eb2011-07-06 01:56:38 -07001203static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
1204 struct packet_type *pt, struct net_device *orig_dev)
David S. Millerdc99f602011-07-05 01:45:05 -07001205{
1206 struct packet_fanout *f = pt->af_packet_priv;
1207 unsigned int num = f->num_members;
1208 struct packet_sock *po;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001209 unsigned int idx;
David S. Millerdc99f602011-07-05 01:45:05 -07001210
1211 if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
1212 !num) {
1213 kfree_skb(skb);
1214 return 0;
1215 }
1216
David S. Miller95ec3eb2011-07-06 01:56:38 -07001217 switch (f->type) {
1218 case PACKET_FANOUT_HASH:
1219 default:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001220 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_DEFRAG)) {
Eric Dumazetbc416d92011-10-06 10:28:31 +00001221 skb = ip_check_defrag(skb, IP_DEFRAG_AF_PACKET);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001222 if (!skb)
1223 return 0;
1224 }
1225 skb_get_rxhash(skb);
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001226 idx = fanout_demux_hash(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001227 break;
1228 case PACKET_FANOUT_LB:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001229 idx = fanout_demux_lb(f, skb, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001230 break;
1231 case PACKET_FANOUT_CPU:
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001232 idx = fanout_demux_cpu(f, skb, num);
1233 break;
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001234 case PACKET_FANOUT_RND:
1235 idx = fanout_demux_rnd(f, skb, num);
1236 break;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001237 case PACKET_FANOUT_ROLLOVER:
1238 idx = fanout_demux_rollover(f, skb, 0, (unsigned int) -1, num);
David S. Miller95ec3eb2011-07-06 01:56:38 -07001239 break;
David S. Miller7736d332011-07-05 01:43:20 -07001240 }
1241
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001242 po = pkt_sk(f->arr[idx]);
1243 if (fanout_has_flag(f, PACKET_FANOUT_FLAG_ROLLOVER) &&
1244 unlikely(!packet_rcv_has_room(po, skb))) {
1245 idx = fanout_demux_rollover(f, skb, idx, idx, num);
1246 po = pkt_sk(f->arr[idx]);
1247 }
David S. Millerdc99f602011-07-05 01:45:05 -07001248
1249 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
1250}
1251
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001252DEFINE_MUTEX(fanout_mutex);
1253EXPORT_SYMBOL_GPL(fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001254static LIST_HEAD(fanout_list);
1255
1256static void __fanout_link(struct sock *sk, struct packet_sock *po)
1257{
1258 struct packet_fanout *f = po->fanout;
1259
1260 spin_lock(&f->lock);
1261 f->arr[f->num_members] = sk;
1262 smp_wmb();
1263 f->num_members++;
1264 spin_unlock(&f->lock);
1265}
1266
1267static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
1268{
1269 struct packet_fanout *f = po->fanout;
1270 int i;
1271
1272 spin_lock(&f->lock);
1273 for (i = 0; i < f->num_members; i++) {
1274 if (f->arr[i] == sk)
1275 break;
1276 }
1277 BUG_ON(i >= f->num_members);
1278 f->arr[i] = f->arr[f->num_members - 1];
1279 f->num_members--;
1280 spin_unlock(&f->lock);
1281}
1282
Fengguang Wua0dfb262012-08-23 19:51:21 +08001283static bool match_fanout_group(struct packet_type *ptype, struct sock * sk)
Eric Leblondc0de08d2012-08-16 22:02:58 +00001284{
1285 if (ptype->af_packet_priv == (void*)((struct packet_sock *)sk)->fanout)
1286 return true;
1287
1288 return false;
1289}
1290
David S. Miller7736d332011-07-05 01:43:20 -07001291static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
David S. Millerdc99f602011-07-05 01:45:05 -07001292{
1293 struct packet_sock *po = pkt_sk(sk);
1294 struct packet_fanout *f, *match;
David S. Miller7736d332011-07-05 01:43:20 -07001295 u8 type = type_flags & 0xff;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001296 u8 flags = type_flags >> 8;
David S. Millerdc99f602011-07-05 01:45:05 -07001297 int err;
1298
1299 switch (type) {
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001300 case PACKET_FANOUT_ROLLOVER:
1301 if (type_flags & PACKET_FANOUT_FLAG_ROLLOVER)
1302 return -EINVAL;
David S. Millerdc99f602011-07-05 01:45:05 -07001303 case PACKET_FANOUT_HASH:
1304 case PACKET_FANOUT_LB:
David S. Miller95ec3eb2011-07-06 01:56:38 -07001305 case PACKET_FANOUT_CPU:
Daniel Borkmann5df0ddf2013-08-28 22:13:09 +02001306 case PACKET_FANOUT_RND:
David S. Millerdc99f602011-07-05 01:45:05 -07001307 break;
1308 default:
1309 return -EINVAL;
1310 }
1311
1312 if (!po->running)
1313 return -EINVAL;
1314
1315 if (po->fanout)
1316 return -EALREADY;
1317
1318 mutex_lock(&fanout_mutex);
1319 match = NULL;
1320 list_for_each_entry(f, &fanout_list, list) {
1321 if (f->id == id &&
1322 read_pnet(&f->net) == sock_net(sk)) {
1323 match = f;
1324 break;
1325 }
1326 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001327 err = -EINVAL;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001328 if (match && match->flags != flags)
Eric Dumazetafe62c62011-07-07 06:41:29 -07001329 goto out;
David S. Millerdc99f602011-07-05 01:45:05 -07001330 if (!match) {
Eric Dumazetafe62c62011-07-07 06:41:29 -07001331 err = -ENOMEM;
David S. Millerdc99f602011-07-05 01:45:05 -07001332 match = kzalloc(sizeof(*match), GFP_KERNEL);
Eric Dumazetafe62c62011-07-07 06:41:29 -07001333 if (!match)
1334 goto out;
1335 write_pnet(&match->net, sock_net(sk));
1336 match->id = id;
1337 match->type = type;
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00001338 match->flags = flags;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001339 atomic_set(&match->rr_cur, 0);
1340 INIT_LIST_HEAD(&match->list);
1341 spin_lock_init(&match->lock);
1342 atomic_set(&match->sk_ref, 0);
1343 match->prot_hook.type = po->prot_hook.type;
1344 match->prot_hook.dev = po->prot_hook.dev;
1345 match->prot_hook.func = packet_rcv_fanout;
1346 match->prot_hook.af_packet_priv = match;
Eric Leblondc0de08d2012-08-16 22:02:58 +00001347 match->prot_hook.id_match = match_fanout_group;
Eric Dumazetafe62c62011-07-07 06:41:29 -07001348 dev_add_pack(&match->prot_hook);
1349 list_add(&match->list, &fanout_list);
1350 }
1351 err = -EINVAL;
1352 if (match->type == type &&
1353 match->prot_hook.type == po->prot_hook.type &&
1354 match->prot_hook.dev == po->prot_hook.dev) {
1355 err = -ENOSPC;
1356 if (atomic_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
1357 __dev_remove_pack(&po->prot_hook);
1358 po->fanout = match;
1359 atomic_inc(&match->sk_ref);
1360 __fanout_link(sk, po);
1361 err = 0;
David S. Millerdc99f602011-07-05 01:45:05 -07001362 }
1363 }
Eric Dumazetafe62c62011-07-07 06:41:29 -07001364out:
David S. Millerdc99f602011-07-05 01:45:05 -07001365 mutex_unlock(&fanout_mutex);
1366 return err;
1367}
1368
1369static void fanout_release(struct sock *sk)
1370{
1371 struct packet_sock *po = pkt_sk(sk);
1372 struct packet_fanout *f;
1373
1374 f = po->fanout;
1375 if (!f)
1376 return;
1377
Pavel Emelyanovfff33212012-08-16 05:36:48 +00001378 mutex_lock(&fanout_mutex);
David S. Millerdc99f602011-07-05 01:45:05 -07001379 po->fanout = NULL;
1380
David S. Millerdc99f602011-07-05 01:45:05 -07001381 if (atomic_dec_and_test(&f->sk_ref)) {
1382 list_del(&f->list);
1383 dev_remove_pack(&f->prot_hook);
1384 kfree(f);
1385 }
1386 mutex_unlock(&fanout_mutex);
1387}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001389static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001391static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001393static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
1394 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct sock *sk;
1397 struct sockaddr_pkt *spkt;
1398
1399 /*
1400 * When we registered the protocol we saved the socket in the data
1401 * field for just this event.
1402 */
1403
1404 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 /*
1407 * Yank back the headers [hope the device set this
1408 * right or kerboom...]
1409 *
1410 * Incoming packets have ll header pulled,
1411 * push it back.
1412 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001413 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * so that this procedure is noop.
1415 */
1416
1417 if (skb->pkt_type == PACKET_LOOPBACK)
1418 goto out;
1419
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001420 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001421 goto out;
1422
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001423 skb = skb_share_check(skb, GFP_ATOMIC);
1424 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 goto oom;
1426
1427 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +00001428 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Phil Oester84531c22005-07-12 11:57:52 -07001430 /* drop conntrack reference */
1431 nf_reset(skb);
1432
Herbert Xuffbc6112007-02-04 23:33:10 -08001433 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001435 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 /*
1438 * The SOCK_PACKET socket receives _all_ frames.
1439 */
1440
1441 spkt->spkt_family = dev->type;
1442 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
1443 spkt->spkt_protocol = skb->protocol;
1444
1445 /*
1446 * Charge the memory to the socket. This is done specifically
1447 * to prevent sockets using all the memory up.
1448 */
1449
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001450 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return 0;
1452
1453out:
1454 kfree_skb(skb);
1455oom:
1456 return 0;
1457}
1458
1459
1460/*
1461 * Output a raw packet to a device layer. This bypasses all the other
1462 * protocol layers and you must therefore supply it with a complete frame
1463 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
1466 struct msghdr *msg, size_t len)
1467{
1468 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001469 struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001470 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 struct net_device *dev;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001472 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 int err;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001474 int extra_len = 0;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001477 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 */
1479
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001480 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001482 return -EINVAL;
1483 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
1484 proto = saddr->spkt_protocol;
1485 } else
1486 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001489 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 */
1491
danborkmann@iogearbox.netde74e922012-06-10 08:59:28 +00001492 saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001493retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001494 rcu_read_lock();
1495 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 err = -ENODEV;
1497 if (dev == NULL)
1498 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001499
David S. Millerd5e76b02007-01-25 19:30:36 -08001500 err = -ENETDOWN;
1501 if (!(dev->flags & IFF_UP))
1502 goto out_unlock;
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001505 * You may not queue a frame bigger than the mtu. This is the lowest level
1506 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001508
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001509 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
1510 if (!netif_supports_nofcs(dev)) {
1511 err = -EPROTONOSUPPORT;
1512 goto out_unlock;
1513 }
1514 extra_len = 4; /* We're doing our own CRC */
1515 }
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001518 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 goto out_unlock;
1520
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001521 if (!skb) {
1522 size_t reserved = LL_RESERVED_SPACE(dev);
Herbert Xu4ce40912011-11-18 02:20:05 +00001523 int tlen = dev->needed_tailroom;
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001524 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001526 rcu_read_unlock();
Herbert Xu4ce40912011-11-18 02:20:05 +00001527 skb = sock_wmalloc(sk, len + reserved + tlen, 0, GFP_KERNEL);
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001528 if (skb == NULL)
1529 return -ENOBUFS;
1530 /* FIXME: Save some space for broken drivers that write a hard
1531 * header at transmission time by themselves. PPP is the notable
1532 * one here. This should really be fixed at the driver level.
1533 */
1534 skb_reserve(skb, reserved);
1535 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001536
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001537 /* Try to align data part correctly */
1538 if (hhlen) {
1539 skb->data -= hhlen;
1540 skb->tail -= hhlen;
1541 if (len < hhlen)
1542 skb_reset_network_header(skb);
1543 }
1544 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
1545 if (err)
1546 goto out_free;
1547 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001550 if (len > (dev->mtu + dev->hard_header_len + extra_len)) {
Ben Greear57f89bf2011-02-11 09:35:18 +00001551 /* Earlier code assumed this would be a VLAN pkt,
1552 * double-check this now that we have the actual
1553 * packet in hand.
1554 */
1555 struct ethhdr *ehdr;
1556 skb_reset_mac_header(skb);
1557 ehdr = eth_hdr(skb);
1558 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
1559 err = -EMSGSIZE;
1560 goto out_unlock;
1561 }
1562 }
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 skb->protocol = proto;
1565 skb->dev = dev;
1566 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001567 skb->mark = sk->sk_mark;
Daniel Borkmannbf84a012013-04-14 08:08:13 +00001568
1569 sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Ben Greear3bdc0eb2012-02-11 15:39:30 +00001571 if (unlikely(extra_len == 4))
1572 skb->no_fcs = 1;
1573
Jason Wang40893fd2013-03-26 23:11:22 +00001574 skb_probe_transport_header(skb, 0);
Jason Wangc1aad272013-03-25 20:19:57 +00001575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001577 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001578 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +01001581 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +00001582out_free:
1583 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return err;
1585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Olof Johanssoneea49cc92011-11-02 11:00:49 +00001587static unsigned int run_filter(const struct sk_buff *skb,
Eric Dumazet62ab0812010-12-06 20:50:09 +00001588 const struct sock *sk,
David S. Millerdbcb5852007-01-24 15:21:02 -08001589 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 struct sk_filter *filter;
1592
Eric Dumazet80f8f102011-01-18 07:46:52 +00001593 rcu_read_lock();
1594 filter = rcu_dereference(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -08001595 if (filter != NULL)
Eric Dumazet0a148422011-04-20 09:27:32 +00001596 res = SK_RUN_FILTER(filter, skb);
Eric Dumazet80f8f102011-01-18 07:46:52 +00001597 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
David S. Millerdbcb5852007-01-24 15:21:02 -08001599 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
1602/*
Eric Dumazet62ab0812010-12-06 20:50:09 +00001603 * This function makes lazy skb cloning in hope that most of packets
1604 * are discarded by BPF.
1605 *
1606 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
1607 * and skb->cb are mangled. It works because (and until) packets
1608 * falling here are owned by current CPU. Output packets are cloned
1609 * by dev_queue_xmit_nit(), input packets are processed by net_bh
1610 * sequencially, so that if we return skb to original state on exit,
1611 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 */
1613
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001614static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
1615 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 struct sock *sk;
1618 struct sockaddr_ll *sll;
1619 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001620 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08001622 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
1624 if (skb->pkt_type == PACKET_LOOPBACK)
1625 goto drop;
1626
1627 sk = pt->af_packet_priv;
1628 po = pkt_sk(sk);
1629
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001630 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001631 goto drop;
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 skb->dev = dev;
1634
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001635 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +00001637 * exported to higher levels.
1638 *
1639 * Otherwise, the device hides details of its frame
1640 * structure, so that corresponding packet head is
1641 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 */
1643 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001644 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 else if (skb->pkt_type == PACKET_OUTGOING) {
1646 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001647 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649 }
1650
1651 snaplen = skb->len;
1652
David S. Millerdbcb5852007-01-24 15:21:02 -08001653 res = run_filter(skb, sk, snaplen);
1654 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001655 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08001656 if (snaplen > res)
1657 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00001659 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto drop_n_acct;
1661
1662 if (skb_shared(skb)) {
1663 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
1664 if (nskb == NULL)
1665 goto drop_n_acct;
1666
1667 if (skb_head != skb->data) {
1668 skb->data = skb_head;
1669 skb->len = skb_len;
1670 }
Eric Dumazetabc4e4f2012-04-19 02:24:42 +00001671 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 skb = nskb;
1673 }
1674
Herbert Xuffbc6112007-02-04 23:33:10 -08001675 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
1676 sizeof(skb->cb));
1677
1678 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 sll->sll_family = AF_PACKET;
1680 sll->sll_hatype = dev->type;
1681 sll->sll_protocol = skb->protocol;
1682 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08001683 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07001684 sll->sll_ifindex = orig_dev->ifindex;
1685 else
1686 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07001688 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Herbert Xuffbc6112007-02-04 23:33:10 -08001690 PACKET_SKB_CB(skb)->origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -08001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (pskb_trim(skb, snaplen))
1693 goto drop_n_acct;
1694
1695 skb_set_owner_r(skb, sk);
1696 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +00001697 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Phil Oester84531c22005-07-12 11:57:52 -07001699 /* drop conntrack reference */
1700 nf_reset(skb);
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 spin_lock(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00001703 po->stats.stats1.tp_packets++;
Neil Horman3b885782009-10-12 13:26:31 -07001704 skb->dropcount = atomic_read(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 __skb_queue_tail(&sk->sk_receive_queue, skb);
1706 spin_unlock(&sk->sk_receive_queue.lock);
1707 sk->sk_data_ready(sk, skb->len);
1708 return 0;
1709
1710drop_n_acct:
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00001711 spin_lock(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00001712 po->stats.stats1.tp_drops++;
Willem de Bruijn7091fbd2011-09-30 10:38:28 +00001713 atomic_inc(&sk->sk_drops);
1714 spin_unlock(&sk->sk_receive_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716drop_n_restore:
1717 if (skb_head != skb->data && skb_shared(skb)) {
1718 skb->data = skb_head;
1719 skb->len = skb_len;
1720 }
1721drop:
Neil Hormanead2ceb2009-03-11 09:49:55 +00001722 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return 0;
1724}
1725
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001726static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
1727 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 struct sock *sk;
1730 struct packet_sock *po;
1731 struct sockaddr_ll *sll;
Daniel Borkmann184f4892013-04-16 01:57:46 +00001732 union tpacket_uhdr h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001733 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -08001735 unsigned int snaplen, res;
chetan lokef6fb8f102011-08-19 10:18:16 +00001736 unsigned long status = TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001737 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 struct sk_buff *copy_skb = NULL;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001739 struct timespec ts;
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00001740 __u32 ts_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 if (skb->pkt_type == PACKET_LOOPBACK)
1743 goto drop;
1744
1745 sk = pt->af_packet_priv;
1746 po = pkt_sk(sk);
1747
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001748 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001749 goto drop;
1750
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001751 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -07001753 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 else if (skb->pkt_type == PACKET_OUTGOING) {
1755 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001756 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758 }
1759
Herbert Xu8dc41942007-02-04 23:31:32 -08001760 if (skb->ip_summed == CHECKSUM_PARTIAL)
1761 status |= TP_STATUS_CSUMNOTREADY;
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 snaplen = skb->len;
1764
David S. Millerdbcb5852007-01-24 15:21:02 -08001765 res = run_filter(skb, sk, snaplen);
1766 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001767 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -08001768 if (snaplen > res)
1769 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy8913336a2008-07-18 18:05:19 -07001772 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
1773 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 } else {
Eric Dumazet95c96172012-04-15 05:58:06 +00001775 unsigned int maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001776 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy8913336a2008-07-18 18:05:19 -07001777 (maclen < 16 ? 16 : maclen)) +
1778 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 macoff = netoff - maclen;
1780 }
chetan lokef6fb8f102011-08-19 10:18:16 +00001781 if (po->tp_version <= TPACKET_V2) {
1782 if (macoff + snaplen > po->rx_ring.frame_size) {
1783 if (po->copy_thresh &&
Eric Dumazet0fd7bac2011-12-21 07:11:44 +00001784 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) {
chetan lokef6fb8f102011-08-19 10:18:16 +00001785 if (skb_shared(skb)) {
1786 copy_skb = skb_clone(skb, GFP_ATOMIC);
1787 } else {
1788 copy_skb = skb_get(skb);
1789 skb_head = skb->data;
1790 }
1791 if (copy_skb)
1792 skb_set_owner_r(copy_skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
chetan lokef6fb8f102011-08-19 10:18:16 +00001794 snaplen = po->rx_ring.frame_size - macoff;
1795 if ((int)snaplen < 0)
1796 snaplen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 spin_lock(&sk->sk_receive_queue.lock);
chetan lokef6fb8f102011-08-19 10:18:16 +00001800 h.raw = packet_current_rx_frame(po, skb,
1801 TP_STATUS_KERNEL, (macoff+snaplen));
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001802 if (!h.raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 goto ring_is_full;
chetan lokef6fb8f102011-08-19 10:18:16 +00001804 if (po->tp_version <= TPACKET_V2) {
1805 packet_increment_rx_head(po, &po->rx_ring);
1806 /*
1807 * LOSING will be reported till you read the stats,
1808 * because it's COR - Clear On Read.
1809 * Anyways, moving it for V1/V2 only as V3 doesn't need this
1810 * at packet level.
1811 */
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00001812 if (po->stats.stats1.tp_drops)
chetan lokef6fb8f102011-08-19 10:18:16 +00001813 status |= TP_STATUS_LOSING;
1814 }
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00001815 po->stats.stats1.tp_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (copy_skb) {
1817 status |= TP_STATUS_COPY;
1818 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 spin_unlock(&sk->sk_receive_queue.lock);
1821
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001822 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00001823
1824 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp)))
Daniel Borkmann7a513842013-04-23 00:39:29 +00001825 getnstimeofday(&ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00001827 status |= ts_status;
1828
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001829 switch (po->tp_version) {
1830 case TPACKET_V1:
1831 h.h1->tp_len = skb->len;
1832 h.h1->tp_snaplen = snaplen;
1833 h.h1->tp_mac = macoff;
1834 h.h1->tp_net = netoff;
Daniel Borkmann4b457bd2013-04-16 01:29:11 +00001835 h.h1->tp_sec = ts.tv_sec;
1836 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001837 hdrlen = sizeof(*h.h1);
1838 break;
1839 case TPACKET_V2:
1840 h.h2->tp_len = skb->len;
1841 h.h2->tp_snaplen = snaplen;
1842 h.h2->tp_mac = macoff;
1843 h.h2->tp_net = netoff;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001844 h.h2->tp_sec = ts.tv_sec;
1845 h.h2->tp_nsec = ts.tv_nsec;
Ben Greeara3bcc232011-06-01 06:49:10 +00001846 if (vlan_tx_tag_present(skb)) {
1847 h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
1848 status |= TP_STATUS_VLAN_VALID;
1849 } else {
1850 h.h2->tp_vlan_tci = 0;
1851 }
Eric Dumazet13fcb7b2011-06-06 22:42:06 -07001852 h.h2->tp_padding = 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001853 hdrlen = sizeof(*h.h2);
1854 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00001855 case TPACKET_V3:
1856 /* tp_nxt_offset,vlan are already populated above.
1857 * So DONT clear those fields here
1858 */
1859 h.h3->tp_status |= status;
1860 h.h3->tp_len = skb->len;
1861 h.h3->tp_snaplen = snaplen;
1862 h.h3->tp_mac = macoff;
1863 h.h3->tp_net = netoff;
chetan lokef6fb8f102011-08-19 10:18:16 +00001864 h.h3->tp_sec = ts.tv_sec;
1865 h.h3->tp_nsec = ts.tv_nsec;
1866 hdrlen = sizeof(*h.h3);
1867 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001868 default:
1869 BUG();
1870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001872 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -07001873 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 sll->sll_family = AF_PACKET;
1875 sll->sll_hatype = dev->type;
1876 sll->sll_protocol = skb->protocol;
1877 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -08001878 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07001879 sll->sll_ifindex = orig_dev->ifindex;
1880 else
1881 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Ralf Baechlee16aa202006-12-07 00:11:33 -08001883 smp_mb();
Changli Gaof6dafa92010-12-07 04:26:16 +00001884#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 {
Changli Gao0af55bb2010-12-01 02:52:20 +00001886 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
chetan lokef6fb8f102011-08-19 10:18:16 +00001888 if (po->tp_version <= TPACKET_V2) {
1889 end = (u8 *)PAGE_ALIGN((unsigned long)h.raw
1890 + macoff + snaplen);
1891 for (start = h.raw; start < end; start += PAGE_SIZE)
1892 flush_dcache_page(pgv_to_page(start));
1893 }
Chetan Lokecc9f01b2011-07-14 08:36:33 -07001894 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
Changli Gaof6dafa92010-12-07 04:26:16 +00001896#endif
chetan lokef6fb8f102011-08-19 10:18:16 +00001897 if (po->tp_version <= TPACKET_V2)
1898 __packet_set_status(po, h.raw, status);
1899 else
1900 prb_clear_blk_fill_status(&po->rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 sk->sk_data_ready(sk, 0);
1903
1904drop_n_restore:
1905 if (skb_head != skb->data && skb_shared(skb)) {
1906 skb->data = skb_head;
1907 skb->len = skb_len;
1908 }
1909drop:
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001910 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return 0;
1912
1913ring_is_full:
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00001914 po->stats.stats1.tp_drops++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 spin_unlock(&sk->sk_receive_queue.lock);
1916
1917 sk->sk_data_ready(sk, 0);
Wei Yongjunacb5d752009-02-25 00:36:42 +00001918 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 goto drop_n_restore;
1920}
1921
Johann Baudy69e3c752009-05-18 22:11:22 -07001922static void tpacket_destruct_skb(struct sk_buff *skb)
1923{
1924 struct packet_sock *po = pkt_sk(skb->sk);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001925 void *ph;
Johann Baudy69e3c752009-05-18 22:11:22 -07001926
Johann Baudy69e3c752009-05-18 22:11:22 -07001927 if (likely(po->tx_ring.pg_vec)) {
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00001928 __u32 ts;
1929
Johann Baudy69e3c752009-05-18 22:11:22 -07001930 ph = skb_shinfo(skb)->destructor_arg;
Johann Baudy69e3c752009-05-18 22:11:22 -07001931 BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
1932 atomic_dec(&po->tx_ring.pending);
Daniel Borkmannb9c32fb2013-04-23 00:39:31 +00001933
1934 ts = __packet_set_timestamp(po, ph, skb);
1935 __packet_set_status(po, ph, TP_STATUS_AVAILABLE | ts);
Johann Baudy69e3c752009-05-18 22:11:22 -07001936 }
1937
1938 sock_wfree(skb);
1939}
1940
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001941static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
1942 void *frame, struct net_device *dev, int size_max,
Herbert Xuae641942011-11-18 02:20:04 +00001943 __be16 proto, unsigned char *addr, int hlen)
Johann Baudy69e3c752009-05-18 22:11:22 -07001944{
Daniel Borkmann184f4892013-04-16 01:57:46 +00001945 union tpacket_uhdr ph;
David S. Miller09effa62013-08-07 17:11:00 -07001946 int to_write, offset, len, tp_len, nr_frags, len_max;
Johann Baudy69e3c752009-05-18 22:11:22 -07001947 struct socket *sock = po->sk.sk_socket;
1948 struct page *page;
1949 void *data;
1950 int err;
1951
1952 ph.raw = frame;
1953
1954 skb->protocol = proto;
1955 skb->dev = dev;
1956 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001957 skb->mark = po->sk.sk_mark;
Willem de Bruijn2e313962013-04-23 00:39:28 +00001958 sock_tx_timestamp(&po->sk, &skb_shinfo(skb)->tx_flags);
Johann Baudy69e3c752009-05-18 22:11:22 -07001959 skb_shinfo(skb)->destructor_arg = ph.raw;
1960
1961 switch (po->tp_version) {
1962 case TPACKET_V2:
1963 tp_len = ph.h2->tp_len;
1964 break;
1965 default:
1966 tp_len = ph.h1->tp_len;
1967 break;
1968 }
David S. Miller09effa62013-08-07 17:11:00 -07001969 if (unlikely(tp_len > size_max)) {
1970 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
1971 return -EMSGSIZE;
1972 }
Johann Baudy69e3c752009-05-18 22:11:22 -07001973
Herbert Xuae641942011-11-18 02:20:04 +00001974 skb_reserve(skb, hlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07001975 skb_reset_network_header(skb);
Jason Wang40893fd2013-03-26 23:11:22 +00001976 skb_probe_transport_header(skb, 0);
Jason Wangc1aad272013-03-25 20:19:57 +00001977
Paul Chavent5920cd3a2012-11-06 23:10:47 +00001978 if (po->tp_tx_has_off) {
1979 int off_min, off_max, off;
1980 off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll);
1981 off_max = po->tx_ring.frame_size - tp_len;
1982 if (sock->type == SOCK_DGRAM) {
1983 switch (po->tp_version) {
1984 case TPACKET_V2:
1985 off = ph.h2->tp_net;
1986 break;
1987 default:
1988 off = ph.h1->tp_net;
1989 break;
1990 }
1991 } else {
1992 switch (po->tp_version) {
1993 case TPACKET_V2:
1994 off = ph.h2->tp_mac;
1995 break;
1996 default:
1997 off = ph.h1->tp_mac;
1998 break;
1999 }
2000 }
2001 if (unlikely((off < off_min) || (off_max < off)))
2002 return -EINVAL;
2003 data = ph.raw + off;
2004 } else {
2005 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
2006 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002007 to_write = tp_len;
2008
2009 if (sock->type == SOCK_DGRAM) {
2010 err = dev_hard_header(skb, dev, ntohs(proto), addr,
2011 NULL, tp_len);
2012 if (unlikely(err < 0))
2013 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002014 } else if (dev->hard_header_len) {
Johann Baudy69e3c752009-05-18 22:11:22 -07002015 /* net device doesn't like empty head */
2016 if (unlikely(tp_len <= dev->hard_header_len)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002017 pr_err("packet size is too short (%d < %d)\n",
2018 tp_len, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002019 return -EINVAL;
2020 }
2021
2022 skb_push(skb, dev->hard_header_len);
2023 err = skb_store_bits(skb, 0, data,
2024 dev->hard_header_len);
2025 if (unlikely(err))
2026 return err;
2027
2028 data += dev->hard_header_len;
2029 to_write -= dev->hard_header_len;
2030 }
2031
Johann Baudy69e3c752009-05-18 22:11:22 -07002032 offset = offset_in_page(data);
2033 len_max = PAGE_SIZE - offset;
2034 len = ((to_write > len_max) ? len_max : to_write);
2035
2036 skb->data_len = to_write;
2037 skb->len += to_write;
2038 skb->truesize += to_write;
2039 atomic_add(to_write, &po->sk.sk_wmem_alloc);
2040
2041 while (likely(to_write)) {
2042 nr_frags = skb_shinfo(skb)->nr_frags;
2043
2044 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002045 pr_err("Packet exceed the number of skb frags(%lu)\n",
2046 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -07002047 return -EFAULT;
2048 }
2049
Changli Gao0af55bb2010-12-01 02:52:20 +00002050 page = pgv_to_page(data);
2051 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -07002052 flush_dcache_page(page);
2053 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +00002054 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -07002055 to_write -= len;
2056 offset = 0;
2057 len_max = PAGE_SIZE;
2058 len = ((to_write > len_max) ? len_max : to_write);
2059 }
2060
2061 return tp_len;
2062}
2063
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002064static struct net_device *packet_cached_dev_get(struct packet_sock *po)
2065{
2066 struct net_device *dev;
2067
2068 rcu_read_lock();
2069 dev = rcu_dereference(po->cached_dev);
2070 if (dev)
2071 dev_hold(dev);
2072 rcu_read_unlock();
2073
2074 return dev;
2075}
2076
Johann Baudy69e3c752009-05-18 22:11:22 -07002077static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2078{
Johann Baudy69e3c752009-05-18 22:11:22 -07002079 struct sk_buff *skb;
2080 struct net_device *dev;
2081 __be16 proto;
David S. Miller09effa62013-08-07 17:11:00 -07002082 int err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002083 void *ph;
2084 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Johann Baudy69e3c752009-05-18 22:11:22 -07002085 int tp_len, size_max;
2086 unsigned char *addr;
2087 int len_sum = 0;
danborkmann@iogearbox.net9e670302012-08-20 03:34:03 +00002088 int status = TP_STATUS_AVAILABLE;
Herbert Xuae641942011-11-18 02:20:04 +00002089 int hlen, tlen;
Johann Baudy69e3c752009-05-18 22:11:22 -07002090
Johann Baudy69e3c752009-05-18 22:11:22 -07002091 mutex_lock(&po->pg_vec_lock);
2092
Johann Baudy69e3c752009-05-18 22:11:22 -07002093 if (saddr == NULL) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002094 dev = packet_cached_dev_get(po);
Johann Baudy69e3c752009-05-18 22:11:22 -07002095 proto = po->num;
2096 addr = NULL;
2097 } else {
2098 err = -EINVAL;
2099 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2100 goto out;
2101 if (msg->msg_namelen < (saddr->sll_halen
2102 + offsetof(struct sockaddr_ll,
2103 sll_addr)))
2104 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002105 proto = saddr->sll_protocol;
2106 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00002107 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
Johann Baudy69e3c752009-05-18 22:11:22 -07002108 }
2109
Johann Baudy69e3c752009-05-18 22:11:22 -07002110 err = -ENXIO;
2111 if (unlikely(dev == NULL))
2112 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002113 err = -ENETDOWN;
2114 if (unlikely(!(dev->flags & IFF_UP)))
2115 goto out_put;
2116
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002117 reserve = dev->hard_header_len;
2118
Johann Baudy69e3c752009-05-18 22:11:22 -07002119 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07002120 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07002121
David S. Miller09effa62013-08-07 17:11:00 -07002122 if (size_max > dev->mtu + reserve)
2123 size_max = dev->mtu + reserve;
2124
Johann Baudy69e3c752009-05-18 22:11:22 -07002125 do {
2126 ph = packet_current_frame(po, &po->tx_ring,
2127 TP_STATUS_SEND_REQUEST);
2128
2129 if (unlikely(ph == NULL)) {
2130 schedule();
2131 continue;
2132 }
2133
2134 status = TP_STATUS_SEND_REQUEST;
Herbert Xuae641942011-11-18 02:20:04 +00002135 hlen = LL_RESERVED_SPACE(dev);
2136 tlen = dev->needed_tailroom;
Johann Baudy69e3c752009-05-18 22:11:22 -07002137 skb = sock_alloc_send_skb(&po->sk,
Herbert Xuae641942011-11-18 02:20:04 +00002138 hlen + tlen + sizeof(struct sockaddr_ll),
Johann Baudy69e3c752009-05-18 22:11:22 -07002139 0, &err);
2140
2141 if (unlikely(skb == NULL))
2142 goto out_status;
2143
2144 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
Herbert Xuae641942011-11-18 02:20:04 +00002145 addr, hlen);
Johann Baudy69e3c752009-05-18 22:11:22 -07002146
2147 if (unlikely(tp_len < 0)) {
2148 if (po->tp_loss) {
2149 __packet_set_status(po, ph,
2150 TP_STATUS_AVAILABLE);
2151 packet_increment_head(&po->tx_ring);
2152 kfree_skb(skb);
2153 continue;
2154 } else {
2155 status = TP_STATUS_WRONG_FORMAT;
2156 err = tp_len;
2157 goto out_status;
2158 }
2159 }
2160
2161 skb->destructor = tpacket_destruct_skb;
2162 __packet_set_status(po, ph, TP_STATUS_SENDING);
2163 atomic_inc(&po->tx_ring.pending);
2164
2165 status = TP_STATUS_SEND_REQUEST;
2166 err = dev_queue_xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00002167 if (unlikely(err > 0)) {
2168 err = net_xmit_errno(err);
2169 if (err && __packet_get_status(po, ph) ==
2170 TP_STATUS_AVAILABLE) {
2171 /* skb was destructed already */
2172 skb = NULL;
2173 goto out_status;
2174 }
2175 /*
2176 * skb was dropped but not destructed yet;
2177 * let's treat it like congestion or err < 0
2178 */
2179 err = 0;
2180 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002181 packet_increment_head(&po->tx_ring);
2182 len_sum += tp_len;
Joe Perchesf64f9e72009-11-29 16:55:45 -08002183 } while (likely((ph != NULL) ||
2184 ((!(msg->msg_flags & MSG_DONTWAIT)) &&
2185 (atomic_read(&po->tx_ring.pending))))
2186 );
Johann Baudy69e3c752009-05-18 22:11:22 -07002187
2188 err = len_sum;
2189 goto out_put;
2190
Johann Baudy69e3c752009-05-18 22:11:22 -07002191out_status:
2192 __packet_set_status(po, ph, status);
2193 kfree_skb(skb);
2194out_put:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002195 dev_put(dev);
Johann Baudy69e3c752009-05-18 22:11:22 -07002196out:
2197 mutex_unlock(&po->pg_vec_lock);
2198 return err;
2199}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Olof Johanssoneea49cc92011-11-02 11:00:49 +00002201static struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
2202 size_t reserve, size_t len,
2203 size_t linear, int noblock,
2204 int *err)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002205{
2206 struct sk_buff *skb;
2207
2208 /* Under a page? Don't bother with paged skb. */
2209 if (prepad + len < PAGE_SIZE || !linear)
2210 linear = len;
2211
2212 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
Eric Dumazet28d64272013-08-08 14:38:47 -07002213 err, 0);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002214 if (!skb)
2215 return NULL;
2216
2217 skb_reserve(skb, reserve);
2218 skb_put(skb, linear);
2219 skb->data_len = len - linear;
2220 skb->len += len - linear;
2221
2222 return skb;
2223}
2224
Johann Baudy69e3c752009-05-18 22:11:22 -07002225static int packet_snd(struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 struct msghdr *msg, size_t len)
2227{
2228 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002229 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 struct sk_buff *skb;
2231 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08002232 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 unsigned char *addr;
Ben Greear827d9782011-06-01 07:18:53 +00002234 int err, reserve = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002235 struct virtio_net_hdr vnet_hdr = { 0 };
2236 int offset = 0;
2237 int vnet_hdr_len;
2238 struct packet_sock *po = pkt_sk(sk);
2239 unsigned short gso_type = 0;
Herbert Xuae641942011-11-18 02:20:04 +00002240 int hlen, tlen;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002241 int extra_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002244 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (saddr == NULL) {
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002248 dev = packet_cached_dev_get(po);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 proto = po->num;
2250 addr = NULL;
2251 } else {
2252 err = -EINVAL;
2253 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
2254 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002255 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
2256 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 proto = saddr->sll_protocol;
2258 addr = saddr->sll_addr;
Ben Greear827d9782011-06-01 07:18:53 +00002259 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 err = -ENXIO;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002263 if (unlikely(dev == NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 goto out_unlock;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002265 err = -ENETDOWN;
2266 if (unlikely(!(dev->flags & IFF_UP)))
2267 goto out_unlock;
2268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (sock->type == SOCK_RAW)
2270 reserve = dev->hard_header_len;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002271 if (po->has_vnet_hdr) {
2272 vnet_hdr_len = sizeof(vnet_hdr);
2273
2274 err = -EINVAL;
2275 if (len < vnet_hdr_len)
2276 goto out_unlock;
2277
2278 len -= vnet_hdr_len;
2279
2280 err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
2281 vnet_hdr_len);
2282 if (err < 0)
2283 goto out_unlock;
2284
2285 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
2286 (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
2287 vnet_hdr.hdr_len))
2288 vnet_hdr.hdr_len = vnet_hdr.csum_start +
2289 vnet_hdr.csum_offset + 2;
2290
2291 err = -EINVAL;
2292 if (vnet_hdr.hdr_len > len)
2293 goto out_unlock;
2294
2295 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
2296 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
2297 case VIRTIO_NET_HDR_GSO_TCPV4:
2298 gso_type = SKB_GSO_TCPV4;
2299 break;
2300 case VIRTIO_NET_HDR_GSO_TCPV6:
2301 gso_type = SKB_GSO_TCPV6;
2302 break;
2303 case VIRTIO_NET_HDR_GSO_UDP:
2304 gso_type = SKB_GSO_UDP;
2305 break;
2306 default:
2307 goto out_unlock;
2308 }
2309
2310 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
2311 gso_type |= SKB_GSO_TCP_ECN;
2312
2313 if (vnet_hdr.gso_size == 0)
2314 goto out_unlock;
2315
2316 }
2317 }
2318
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002319 if (unlikely(sock_flag(sk, SOCK_NOFCS))) {
2320 if (!netif_supports_nofcs(dev)) {
2321 err = -EPROTONOSUPPORT;
2322 goto out_unlock;
2323 }
2324 extra_len = 4; /* We're doing our own CRC */
2325 }
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 err = -EMSGSIZE;
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002328 if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN + extra_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 goto out_unlock;
2330
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002331 err = -ENOBUFS;
Herbert Xuae641942011-11-18 02:20:04 +00002332 hlen = LL_RESERVED_SPACE(dev);
2333 tlen = dev->needed_tailroom;
2334 skb = packet_alloc_skb(sk, hlen + tlen, hlen, len, vnet_hdr.hdr_len,
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002335 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002336 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 goto out_unlock;
2338
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002339 skb_set_network_header(skb, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07002341 err = -EINVAL;
2342 if (sock->type == SOCK_DGRAM &&
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002343 (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07002344 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 /* Returns -EFAULT on error */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002347 err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (err)
2349 goto out_free;
Daniel Borkmannbf84a012013-04-14 08:08:13 +00002350
2351 sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002353 if (!gso_type && (len > dev->mtu + reserve + extra_len)) {
David S. Miller09effa62013-08-07 17:11:00 -07002354 /* Earlier code assumed this would be a VLAN pkt,
2355 * double-check this now that we have the actual
2356 * packet in hand.
2357 */
2358 struct ethhdr *ehdr;
2359 skb_reset_mac_header(skb);
2360 ehdr = eth_hdr(skb);
2361 if (ehdr->h_proto != htons(ETH_P_8021Q)) {
2362 err = -EMSGSIZE;
2363 goto out_free;
2364 }
Ben Greear57f89bf2011-02-11 09:35:18 +00002365 }
2366
David S. Miller09effa62013-08-07 17:11:00 -07002367 skb->protocol = proto;
2368 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00002370 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002372 if (po->has_vnet_hdr) {
2373 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
2374 if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
2375 vnet_hdr.csum_offset)) {
2376 err = -EINVAL;
2377 goto out_free;
2378 }
2379 }
2380
2381 skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
2382 skb_shinfo(skb)->gso_type = gso_type;
2383
2384 /* Header must be checked, and gso_segs computed. */
2385 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2386 skb_shinfo(skb)->gso_segs = 0;
2387
2388 len += vnet_hdr_len;
2389 }
2390
Jason Wang40893fd2013-03-26 23:11:22 +00002391 skb_probe_transport_header(skb, reserve);
Jason Wangc1aad272013-03-25 20:19:57 +00002392
Ben Greear3bdc0eb2012-02-11 15:39:30 +00002393 if (unlikely(extra_len == 4))
2394 skb->no_fcs = 1;
2395
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 /*
2397 * Now send it
2398 */
2399
2400 err = dev_queue_xmit(skb);
2401 if (err > 0 && (err = net_xmit_errno(err)) != 0)
2402 goto out_unlock;
2403
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002404 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002406 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408out_free:
2409 kfree_skb(skb);
2410out_unlock:
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002411 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 dev_put(dev);
2413out:
2414 return err;
2415}
2416
Johann Baudy69e3c752009-05-18 22:11:22 -07002417static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
2418 struct msghdr *msg, size_t len)
2419{
Johann Baudy69e3c752009-05-18 22:11:22 -07002420 struct sock *sk = sock->sk;
2421 struct packet_sock *po = pkt_sk(sk);
2422 if (po->tx_ring.pg_vec)
2423 return tpacket_snd(po, msg);
2424 else
Johann Baudy69e3c752009-05-18 22:11:22 -07002425 return packet_snd(sock, msg, len);
2426}
2427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428/*
2429 * Close a PACKET socket. This is fairly simple. We immediately go
2430 * to 'closed' state and remove our protocol entry in the device list.
2431 */
2432
2433static int packet_release(struct socket *sock)
2434{
2435 struct sock *sk = sock->sk;
2436 struct packet_sock *po;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002437 struct net *net;
chetan lokef6fb8f102011-08-19 10:18:16 +00002438 union tpacket_req_u req_u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 if (!sk)
2441 return 0;
2442
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002443 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 po = pkt_sk(sk);
2445
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002446 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00002447 sk_del_node_init_rcu(sk);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002448 mutex_unlock(&net->packet.sklist_lock);
2449
2450 preempt_disable();
Eric Dumazet920de802008-11-24 00:09:29 -08002451 sock_prot_inuse_add(net, sk->sk_prot, -1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002452 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
stephen hemminger808f5112010-02-22 07:57:18 +00002454 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002455 unregister_prot_hook(sk, false);
Ben Greear160ff182011-06-01 07:18:52 +00002456 if (po->prot_hook.dev) {
2457 dev_put(po->prot_hook.dev);
2458 po->prot_hook.dev = NULL;
2459 }
stephen hemminger808f5112010-02-22 07:57:18 +00002460 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Phil Sutter9665d5d2013-02-01 07:21:41 +00002464 if (po->rx_ring.pg_vec) {
2465 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f102011-08-19 10:18:16 +00002466 packet_set_ring(sk, &req_u, 1, 0);
Phil Sutter9665d5d2013-02-01 07:21:41 +00002467 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002468
Phil Sutter9665d5d2013-02-01 07:21:41 +00002469 if (po->tx_ring.pg_vec) {
2470 memset(&req_u, 0, sizeof(req_u));
chetan lokef6fb8f102011-08-19 10:18:16 +00002471 packet_set_ring(sk, &req_u, 1, 1);
Phil Sutter9665d5d2013-02-01 07:21:41 +00002472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
David S. Millerdc99f602011-07-05 01:45:05 -07002474 fanout_release(sk);
2475
stephen hemminger808f5112010-02-22 07:57:18 +00002476 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 /*
2478 * Now the socket is dead. No more input will appear.
2479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 sock_orphan(sk);
2481 sock->sk = NULL;
2482
2483 /* Purge queues */
2484
2485 skb_queue_purge(&sk->sk_receive_queue);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08002486 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 sock_put(sk);
2489 return 0;
2490}
2491
2492/*
2493 * Attach a packet hook.
2494 */
2495
Al Viro0e11c912006-11-08 00:26:29 -08002496static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
2498 struct packet_sock *po = pkt_sk(sk);
David S. Millerdc99f602011-07-05 01:45:05 -07002499
Wei Yongjunaef950b2011-12-27 22:32:41 -05002500 if (po->fanout) {
2501 if (dev)
2502 dev_put(dev);
2503
David S. Millerdc99f602011-07-05 01:45:05 -07002504 return -EINVAL;
Wei Yongjunaef950b2011-12-27 22:32:41 -05002505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 lock_sock(sk);
2508
2509 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07002510 unregister_prot_hook(sk, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 po->num = protocol;
2512 po->prot_hook.type = protocol;
Ben Greear160ff182011-06-01 07:18:52 +00002513 if (po->prot_hook.dev)
2514 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 po->prot_hook.dev = dev;
2516
2517 po->ifindex = dev ? dev->ifindex : 0;
2518
2519 if (protocol == 0)
2520 goto out_unlock;
2521
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08002522 if (!dev || (dev->flags & IFF_UP)) {
David S. Millerce06b032011-07-04 01:44:29 -07002523 register_prot_hook(sk);
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08002524 } else {
2525 sk->sk_err = ENETDOWN;
2526 if (!sock_flag(sk, SOCK_DEAD))
2527 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
2529
2530out_unlock:
2531 spin_unlock(&po->bind_lock);
2532 release_sock(sk);
2533 return 0;
2534}
2535
2536/*
2537 * Bind a packet socket to a device
2538 */
2539
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002540static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
2541 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002543 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 char name[15];
2545 struct net_device *dev;
2546 int err = -ENODEV;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 /*
2549 * Check legality
2550 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002551
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002552 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002554 strlcpy(name, uaddr->sa_data, sizeof(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002556 dev = dev_get_by_name(sock_net(sk), name);
Ben Greear160ff182011-06-01 07:18:52 +00002557 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 return err;
2560}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
2563{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002564 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
2565 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 struct net_device *dev = NULL;
2567 int err;
2568
2569
2570 /*
2571 * Check legality
2572 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (addr_len < sizeof(struct sockaddr_ll))
2575 return -EINVAL;
2576 if (sll->sll_family != AF_PACKET)
2577 return -EINVAL;
2578
2579 if (sll->sll_ifindex) {
2580 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002581 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (dev == NULL)
2583 goto out;
2584 }
2585 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587out:
2588 return err;
2589}
2590
2591static struct proto packet_proto = {
2592 .name = "PACKET",
2593 .owner = THIS_MODULE,
2594 .obj_size = sizeof(struct packet_sock),
2595};
2596
2597/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002598 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 */
2600
Eric Paris3f378b62009-11-05 22:18:14 -08002601static int packet_create(struct net *net, struct socket *sock, int protocol,
2602 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 struct sock *sk;
2605 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08002606 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 int err;
2608
Eric W. Biedermandf008c92012-11-16 03:03:07 +00002609 if (!ns_capable(net->user_ns, CAP_NET_RAW))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07002611 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
2612 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return -ESOCKTNOSUPPORT;
2614
2615 sock->state = SS_UNCONNECTED;
2616
2617 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07002618 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 if (sk == NULL)
2620 goto out;
2621
2622 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 if (sock->type == SOCK_PACKET)
2624 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07002625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 sock_init_data(sock, sk);
2627
2628 po = pkt_sk(sk);
2629 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08002630 po->num = proto;
Daniel Borkmanne40526c2013-11-21 16:50:58 +01002631 RCU_INIT_POINTER(po->cached_dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08002634 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 /*
2637 * Attach a protocol block
2638 */
2639
2640 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08002641 mutex_init(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07002643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (sock->type == SOCK_PACKET)
2645 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07002646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 po->prot_hook.af_packet_priv = sk;
2648
Al Viro0e11c912006-11-08 00:26:29 -08002649 if (proto) {
2650 po->prot_hook.type = proto;
David S. Millerce06b032011-07-04 01:44:29 -07002651 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 }
2653
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002654 mutex_lock(&net->packet.sklist_lock);
stephen hemminger808f5112010-02-22 07:57:18 +00002655 sk_add_node_rcu(sk, &net->packet.sklist);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002656 mutex_unlock(&net->packet.sklist_lock);
2657
2658 preempt_disable();
Eric Dumazet36804532008-11-19 14:25:35 -08002659 sock_prot_inuse_add(net, &packet_proto, 1);
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00002660 preempt_enable();
stephen hemminger808f5112010-02-22 07:57:18 +00002661
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663out:
2664 return err;
2665}
2666
2667/*
2668 * Pull a packet from our receive queue and hand it to the user.
2669 * If necessary we block.
2670 */
2671
2672static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
2673 struct msghdr *msg, size_t len, int flags)
2674{
2675 struct sock *sk = sock->sk;
2676 struct sk_buff *skb;
2677 int copied, err;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002678 int vnet_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00002681 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 goto out;
2683
2684#if 0
2685 /* What error should we return now? EUNATTACH? */
2686 if (pkt_sk(sk)->ifindex < 0)
2687 return -ENODEV;
2688#endif
2689
Richard Cochraned85b562010-04-07 22:41:28 +00002690 if (flags & MSG_ERRQUEUE) {
Richard Cochrancb820f82013-07-19 19:40:09 +02002691 err = sock_recv_errqueue(sk, msg, len,
2692 SOL_PACKET, PACKET_TX_TIMESTAMP);
Richard Cochraned85b562010-04-07 22:41:28 +00002693 goto out;
2694 }
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 * Call the generic datagram receiver. This handles all sorts
2698 * of horrible races and re-entrancy so we can forget about it
2699 * in the protocol layers.
2700 *
2701 * Now it will return ENETDOWN, if device have just gone down,
2702 * but then it will block.
2703 */
2704
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002705 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
2707 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002708 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 * handles the blocking we don't see and worry about blocking
2710 * retries.
2711 */
2712
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002713 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 goto out;
2715
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002716 if (pkt_sk(sk)->has_vnet_hdr) {
2717 struct virtio_net_hdr vnet_hdr = { 0 };
2718
2719 err = -EINVAL;
2720 vnet_hdr_len = sizeof(vnet_hdr);
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00002721 if (len < vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002722 goto out_free;
2723
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00002724 len -= vnet_hdr_len;
2725
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002726 if (skb_is_gso(skb)) {
2727 struct skb_shared_info *sinfo = skb_shinfo(skb);
2728
2729 /* This is a hint as to how much should be linear. */
2730 vnet_hdr.hdr_len = skb_headlen(skb);
2731 vnet_hdr.gso_size = sinfo->gso_size;
2732 if (sinfo->gso_type & SKB_GSO_TCPV4)
2733 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
2734 else if (sinfo->gso_type & SKB_GSO_TCPV6)
2735 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
2736 else if (sinfo->gso_type & SKB_GSO_UDP)
2737 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
2738 else if (sinfo->gso_type & SKB_GSO_FCOE)
2739 goto out_free;
2740 else
2741 BUG();
2742 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
2743 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
2744 } else
2745 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
2746
2747 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2748 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
Michał Mirosław55508d62010-12-14 15:24:08 +00002749 vnet_hdr.csum_start = skb_checksum_start_offset(skb);
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002750 vnet_hdr.csum_offset = skb->csum_offset;
Jason Wang10a8d942011-06-10 00:56:17 +00002751 } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
2752 vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002753 } /* else everything is zero */
2754
2755 err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
2756 vnet_hdr_len);
2757 if (err < 0)
2758 goto out_free;
2759 }
2760
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002761 /* You lose any data beyond the buffer you gave. If it worries
2762 * a user program they can ask the device for its MTU
2763 * anyway.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002764 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002766 if (copied > len) {
2767 copied = len;
2768 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
2770
2771 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
2772 if (err)
2773 goto out_free;
2774
Neil Horman3b885782009-10-12 13:26:31 -07002775 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002777 if (msg->msg_name) {
2778 /* If the address length field is there to be filled
2779 * in, we fill it in now.
2780 */
2781 if (sock->type == SOCK_PACKET) {
2782 msg->msg_namelen = sizeof(struct sockaddr_pkt);
2783 } else {
2784 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
2785 msg->msg_namelen = sll->sll_halen +
2786 offsetof(struct sockaddr_ll, sll_addr);
2787 }
Herbert Xuffbc6112007-02-04 23:33:10 -08002788 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
2789 msg->msg_namelen);
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01002790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Herbert Xu8dc41942007-02-04 23:31:32 -08002792 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08002793 struct tpacket_auxdata aux;
2794
2795 aux.tp_status = TP_STATUS_USER;
2796 if (skb->ip_summed == CHECKSUM_PARTIAL)
2797 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
2798 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
2799 aux.tp_snaplen = skb->len;
2800 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03002801 aux.tp_net = skb_network_offset(skb);
Ben Greeara3bcc232011-06-01 06:49:10 +00002802 if (vlan_tx_tag_present(skb)) {
2803 aux.tp_vlan_tci = vlan_tx_tag_get(skb);
2804 aux.tp_status |= TP_STATUS_VLAN_VALID;
2805 } else {
2806 aux.tp_vlan_tci = 0;
2807 }
Eric Dumazet13fcb7b2011-06-06 22:42:06 -07002808 aux.tp_padding = 0;
Herbert Xuffbc6112007-02-04 23:33:10 -08002809 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08002810 }
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 /*
2813 * Free or return the buffer as appropriate. Again this
2814 * hides all the races and re-entrancy issues from us.
2815 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002816 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818out_free:
2819 skb_free_datagram(sk, skb);
2820out:
2821 return err;
2822}
2823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
2825 int *uaddr_len, int peer)
2826{
2827 struct net_device *dev;
2828 struct sock *sk = sock->sk;
2829
2830 if (peer)
2831 return -EOPNOTSUPP;
2832
2833 uaddr->sa_family = AF_PACKET;
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02002834 memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01002835 rcu_read_lock();
2836 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
2837 if (dev)
Daniel Borkmann2dc85bf2013-06-12 16:02:27 +02002838 strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
Eric Dumazet654d1f82009-11-02 10:43:32 +01002839 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 *uaddr_len = sizeof(*uaddr);
2841
2842 return 0;
2843}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
2845static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
2846 int *uaddr_len, int peer)
2847{
2848 struct net_device *dev;
2849 struct sock *sk = sock->sk;
2850 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00002851 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
2853 if (peer)
2854 return -EOPNOTSUPP;
2855
2856 sll->sll_family = AF_PACKET;
2857 sll->sll_ifindex = po->ifindex;
2858 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08002859 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01002860 rcu_read_lock();
2861 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (dev) {
2863 sll->sll_hatype = dev->type;
2864 sll->sll_halen = dev->addr_len;
2865 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 } else {
2867 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
2868 sll->sll_halen = 0;
2869 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01002870 rcu_read_unlock();
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002871 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 return 0;
2874}
2875
Wang Chen2aeb0b82008-07-14 20:49:46 -07002876static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
2877 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
2879 switch (i->type) {
2880 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00002881 if (i->alen != dev->addr_len)
2882 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (what > 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00002884 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 else
Jiri Pirko22bedad32010-04-01 21:22:57 +00002886 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 break;
2888 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07002889 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 break;
2891 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07002892 return dev_set_allmulti(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 break;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002894 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00002895 if (i->alen != dev->addr_len)
2896 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002897 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00002898 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002899 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00002900 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00002901 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002902 default:
2903 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07002905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
2908static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
2909{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002910 for ( ; i; i = i->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (i->ifindex == dev->ifindex)
2912 packet_dev_mc(dev, i, what);
2913 }
2914}
2915
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002916static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
2918 struct packet_sock *po = pkt_sk(sk);
2919 struct packet_mclist *ml, *i;
2920 struct net_device *dev;
2921 int err;
2922
2923 rtnl_lock();
2924
2925 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002926 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 if (!dev)
2928 goto done;
2929
2930 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00002931 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 goto done;
2933
2934 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002935 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (i == NULL)
2937 goto done;
2938
2939 err = 0;
2940 for (ml = po->mclist; ml; ml = ml->next) {
2941 if (ml->ifindex == mreq->mr_ifindex &&
2942 ml->type == mreq->mr_type &&
2943 ml->alen == mreq->mr_alen &&
2944 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
2945 ml->count++;
2946 /* Free the new element ... */
2947 kfree(i);
2948 goto done;
2949 }
2950 }
2951
2952 i->type = mreq->mr_type;
2953 i->ifindex = mreq->mr_ifindex;
2954 i->alen = mreq->mr_alen;
2955 memcpy(i->addr, mreq->mr_address, i->alen);
2956 i->count = 1;
2957 i->next = po->mclist;
2958 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07002959 err = packet_dev_mc(dev, i, 1);
2960 if (err) {
2961 po->mclist = i->next;
2962 kfree(i);
2963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
2965done:
2966 rtnl_unlock();
2967 return err;
2968}
2969
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07002970static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
2972 struct packet_mclist *ml, **mlp;
2973
2974 rtnl_lock();
2975
2976 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
2977 if (ml->ifindex == mreq->mr_ifindex &&
2978 ml->type == mreq->mr_type &&
2979 ml->alen == mreq->mr_alen &&
2980 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
2981 if (--ml->count == 0) {
2982 struct net_device *dev;
2983 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00002984 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
2985 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 kfree(ml);
2988 }
2989 rtnl_unlock();
2990 return 0;
2991 }
2992 }
2993 rtnl_unlock();
2994 return -EADDRNOTAVAIL;
2995}
2996
2997static void packet_flush_mclist(struct sock *sk)
2998{
2999 struct packet_sock *po = pkt_sk(sk);
3000 struct packet_mclist *ml;
3001
3002 if (!po->mclist)
3003 return;
3004
3005 rtnl_lock();
3006 while ((ml = po->mclist) != NULL) {
3007 struct net_device *dev;
3008
3009 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00003010 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
3011 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 kfree(ml);
3014 }
3015 rtnl_unlock();
3016}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018static int
David S. Millerb7058842009-09-30 16:12:20 -07003019packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
3021 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08003022 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 int ret;
3024
3025 if (level != SOL_PACKET)
3026 return -ENOPROTOOPT;
3027
Johann Baudy69e3c752009-05-18 22:11:22 -07003028 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003029 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 case PACKET_DROP_MEMBERSHIP:
3031 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003032 struct packet_mreq_max mreq;
3033 int len = optlen;
3034 memset(&mreq, 0, sizeof(mreq));
3035 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003037 if (len > sizeof(mreq))
3038 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003039 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07003041 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
3042 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 if (optname == PACKET_ADD_MEMBERSHIP)
3044 ret = packet_mc_add(sk, &mreq);
3045 else
3046 ret = packet_mc_drop(sk, &mreq);
3047 return ret;
3048 }
David S. Millera2efcfa2007-05-29 13:12:50 -07003049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07003051 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 {
chetan lokef6fb8f102011-08-19 10:18:16 +00003053 union tpacket_req_u req_u;
3054 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
chetan lokef6fb8f102011-08-19 10:18:16 +00003056 switch (po->tp_version) {
3057 case TPACKET_V1:
3058 case TPACKET_V2:
3059 len = sizeof(req_u.req);
3060 break;
3061 case TPACKET_V3:
3062 default:
3063 len = sizeof(req_u.req3);
3064 break;
3065 }
3066 if (optlen < len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003068 if (pkt_sk(sk)->has_vnet_hdr)
3069 return -EINVAL;
chetan lokef6fb8f102011-08-19 10:18:16 +00003070 if (copy_from_user(&req_u.req, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 return -EFAULT;
chetan lokef6fb8f102011-08-19 10:18:16 +00003072 return packet_set_ring(sk, &req_u, 0,
3073 optname == PACKET_TX_RING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
3075 case PACKET_COPY_THRESH:
3076 {
3077 int val;
3078
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003079 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003081 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 return -EFAULT;
3083
3084 pkt_sk(sk)->copy_thresh = val;
3085 return 0;
3086 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003087 case PACKET_VERSION:
3088 {
3089 int val;
3090
3091 if (optlen != sizeof(val))
3092 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07003093 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003094 return -EBUSY;
3095 if (copy_from_user(&val, optval, sizeof(val)))
3096 return -EFAULT;
3097 switch (val) {
3098 case TPACKET_V1:
3099 case TPACKET_V2:
chetan lokef6fb8f102011-08-19 10:18:16 +00003100 case TPACKET_V3:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003101 po->tp_version = val;
3102 return 0;
3103 default:
3104 return -EINVAL;
3105 }
3106 }
Patrick McHardy8913336a2008-07-18 18:05:19 -07003107 case PACKET_RESERVE:
3108 {
3109 unsigned int val;
3110
3111 if (optlen != sizeof(val))
3112 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07003113 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardy8913336a2008-07-18 18:05:19 -07003114 return -EBUSY;
3115 if (copy_from_user(&val, optval, sizeof(val)))
3116 return -EFAULT;
3117 po->tp_reserve = val;
3118 return 0;
3119 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003120 case PACKET_LOSS:
3121 {
3122 unsigned int val;
3123
3124 if (optlen != sizeof(val))
3125 return -EINVAL;
3126 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3127 return -EBUSY;
3128 if (copy_from_user(&val, optval, sizeof(val)))
3129 return -EFAULT;
3130 po->tp_loss = !!val;
3131 return 0;
3132 }
Herbert Xu8dc41942007-02-04 23:31:32 -08003133 case PACKET_AUXDATA:
3134 {
3135 int val;
3136
3137 if (optlen < sizeof(val))
3138 return -EINVAL;
3139 if (copy_from_user(&val, optval, sizeof(val)))
3140 return -EFAULT;
3141
3142 po->auxdata = !!val;
3143 return 0;
3144 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003145 case PACKET_ORIGDEV:
3146 {
3147 int val;
3148
3149 if (optlen < sizeof(val))
3150 return -EINVAL;
3151 if (copy_from_user(&val, optval, sizeof(val)))
3152 return -EFAULT;
3153
3154 po->origdev = !!val;
3155 return 0;
3156 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003157 case PACKET_VNET_HDR:
3158 {
3159 int val;
3160
3161 if (sock->type != SOCK_RAW)
3162 return -EINVAL;
3163 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3164 return -EBUSY;
3165 if (optlen < sizeof(val))
3166 return -EINVAL;
3167 if (copy_from_user(&val, optval, sizeof(val)))
3168 return -EFAULT;
3169
3170 po->has_vnet_hdr = !!val;
3171 return 0;
3172 }
Scott McMillan614f60f2010-06-02 05:53:56 -07003173 case PACKET_TIMESTAMP:
3174 {
3175 int val;
3176
3177 if (optlen != sizeof(val))
3178 return -EINVAL;
3179 if (copy_from_user(&val, optval, sizeof(val)))
3180 return -EFAULT;
3181
3182 po->tp_tstamp = val;
3183 return 0;
3184 }
David S. Millerdc99f602011-07-05 01:45:05 -07003185 case PACKET_FANOUT:
3186 {
3187 int val;
3188
3189 if (optlen != sizeof(val))
3190 return -EINVAL;
3191 if (copy_from_user(&val, optval, sizeof(val)))
3192 return -EFAULT;
3193
3194 return fanout_add(sk, val & 0xffff, val >> 16);
3195 }
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003196 case PACKET_TX_HAS_OFF:
3197 {
3198 unsigned int val;
3199
3200 if (optlen != sizeof(val))
3201 return -EINVAL;
3202 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
3203 return -EBUSY;
3204 if (copy_from_user(&val, optval, sizeof(val)))
3205 return -EFAULT;
3206 po->tp_tx_has_off = !!val;
3207 return 0;
3208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 default:
3210 return -ENOPROTOOPT;
3211 }
3212}
3213
3214static int packet_getsockopt(struct socket *sock, int level, int optname,
3215 char __user *optval, int __user *optlen)
3216{
3217 int len;
Eric Dumazetc06fff62012-04-19 21:56:11 +00003218 int val, lv = sizeof(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 struct sock *sk = sock->sk;
3220 struct packet_sock *po = pkt_sk(sk);
Eric Dumazetc06fff62012-04-19 21:56:11 +00003221 void *data = &val;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003222 union tpacket_stats_u st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224 if (level != SOL_PACKET)
3225 return -ENOPROTOOPT;
3226
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003227 if (get_user(len, optlen))
3228 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
3230 if (len < 0)
3231 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003232
Johann Baudy69e3c752009-05-18 22:11:22 -07003233 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 spin_lock_bh(&sk->sk_receive_queue.lock);
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003236 memcpy(&st, &po->stats, sizeof(st));
3237 memset(&po->stats, 0, sizeof(po->stats));
3238 spin_unlock_bh(&sk->sk_receive_queue.lock);
3239
chetan lokef6fb8f102011-08-19 10:18:16 +00003240 if (po->tp_version == TPACKET_V3) {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003241 lv = sizeof(struct tpacket_stats_v3);
Willem de Bruijn8bcdeaf2013-08-19 16:40:22 -04003242 st.stats3.tp_packets += st.stats3.tp_drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003243 data = &st.stats3;
chetan lokef6fb8f102011-08-19 10:18:16 +00003244 } else {
Eric Dumazetc06fff62012-04-19 21:56:11 +00003245 lv = sizeof(struct tpacket_stats);
Willem de Bruijn8bcdeaf2013-08-19 16:40:22 -04003246 st.stats1.tp_packets += st.stats1.tp_drops;
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003247 data = &st.stats1;
chetan lokef6fb8f102011-08-19 10:18:16 +00003248 }
Daniel Borkmannee80fbf2013-04-19 06:12:29 +00003249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08003251 case PACKET_AUXDATA:
Herbert Xu8dc41942007-02-04 23:31:32 -08003252 val = po->auxdata;
Herbert Xu8dc41942007-02-04 23:31:32 -08003253 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003254 case PACKET_ORIGDEV:
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003255 val = po->origdev;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07003256 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003257 case PACKET_VNET_HDR:
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003258 val = po->has_vnet_hdr;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08003259 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003260 case PACKET_VERSION:
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003261 val = po->tp_version;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003262 break;
3263 case PACKET_HDRLEN:
3264 if (len > sizeof(int))
3265 len = sizeof(int);
3266 if (copy_from_user(&val, optval, len))
3267 return -EFAULT;
3268 switch (val) {
3269 case TPACKET_V1:
3270 val = sizeof(struct tpacket_hdr);
3271 break;
3272 case TPACKET_V2:
3273 val = sizeof(struct tpacket2_hdr);
3274 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00003275 case TPACKET_V3:
3276 val = sizeof(struct tpacket3_hdr);
3277 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003278 default:
3279 return -EINVAL;
3280 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003281 break;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003282 case PACKET_RESERVE:
Patrick McHardy8913336a2008-07-18 18:05:19 -07003283 val = po->tp_reserve;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003284 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07003285 case PACKET_LOSS:
Johann Baudy69e3c752009-05-18 22:11:22 -07003286 val = po->tp_loss;
Johann Baudy69e3c752009-05-18 22:11:22 -07003287 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07003288 case PACKET_TIMESTAMP:
Scott McMillan614f60f2010-06-02 05:53:56 -07003289 val = po->tp_tstamp;
Scott McMillan614f60f2010-06-02 05:53:56 -07003290 break;
David S. Millerdc99f602011-07-05 01:45:05 -07003291 case PACKET_FANOUT:
David S. Millerdc99f602011-07-05 01:45:05 -07003292 val = (po->fanout ?
3293 ((u32)po->fanout->id |
Willem de Bruijn77f65eb2013-03-19 10:18:11 +00003294 ((u32)po->fanout->type << 16) |
3295 ((u32)po->fanout->flags << 24)) :
David S. Millerdc99f602011-07-05 01:45:05 -07003296 0);
David S. Millerdc99f602011-07-05 01:45:05 -07003297 break;
Paul Chavent5920cd3a2012-11-06 23:10:47 +00003298 case PACKET_TX_HAS_OFF:
3299 val = po->tp_tx_has_off;
3300 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 default:
3302 return -ENOPROTOOPT;
3303 }
3304
Eric Dumazetc06fff62012-04-19 21:56:11 +00003305 if (len > lv)
3306 len = lv;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003307 if (put_user(len, optlen))
3308 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08003309 if (copy_to_user(optval, data, len))
3310 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08003311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313
3314
Jiri Pirko351638e2013-05-28 01:30:21 +00003315static int packet_notifier(struct notifier_block *this,
3316 unsigned long msg, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
3318 struct sock *sk;
Jiri Pirko351638e2013-05-28 01:30:21 +00003319 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003320 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
stephen hemminger808f5112010-02-22 07:57:18 +00003322 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003323 sk_for_each_rcu(sk, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 struct packet_sock *po = pkt_sk(sk);
3325
3326 switch (msg) {
3327 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 if (po->mclist)
3329 packet_dev_mclist(dev, po->mclist, -1);
David S. Millera2efcfa2007-05-29 13:12:50 -07003330 /* fallthrough */
3331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 case NETDEV_DOWN:
3333 if (dev->ifindex == po->ifindex) {
3334 spin_lock(&po->bind_lock);
3335 if (po->running) {
David S. Millerce06b032011-07-04 01:44:29 -07003336 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 sk->sk_err = ENETDOWN;
3338 if (!sock_flag(sk, SOCK_DEAD))
3339 sk->sk_error_report(sk);
3340 }
3341 if (msg == NETDEV_UNREGISTER) {
3342 po->ifindex = -1;
Ben Greear160ff182011-06-01 07:18:52 +00003343 if (po->prot_hook.dev)
3344 dev_put(po->prot_hook.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 po->prot_hook.dev = NULL;
3346 }
3347 spin_unlock(&po->bind_lock);
3348 }
3349 break;
3350 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00003351 if (dev->ifindex == po->ifindex) {
3352 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07003353 if (po->num)
3354 register_prot_hook(sk);
stephen hemminger808f5112010-02-22 07:57:18 +00003355 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 break;
3358 }
3359 }
stephen hemminger808f5112010-02-22 07:57:18 +00003360 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return NOTIFY_DONE;
3362}
3363
3364
3365static int packet_ioctl(struct socket *sock, unsigned int cmd,
3366 unsigned long arg)
3367{
3368 struct sock *sk = sock->sk;
3369
Johann Baudy69e3c752009-05-18 22:11:22 -07003370 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003371 case SIOCOUTQ:
3372 {
3373 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07003374
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003375 return put_user(amount, (int __user *)arg);
3376 }
3377 case SIOCINQ:
3378 {
3379 struct sk_buff *skb;
3380 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003382 spin_lock_bh(&sk->sk_receive_queue.lock);
3383 skb = skb_peek(&sk->sk_receive_queue);
3384 if (skb)
3385 amount = skb->len;
3386 spin_unlock_bh(&sk->sk_receive_queue.lock);
3387 return put_user(amount, (int __user *)arg);
3388 }
3389 case SIOCGSTAMP:
3390 return sock_get_timestamp(sk, (struct timeval __user *)arg);
3391 case SIOCGSTAMPNS:
3392 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003395 case SIOCADDRT:
3396 case SIOCDELRT:
3397 case SIOCDARP:
3398 case SIOCGARP:
3399 case SIOCSARP:
3400 case SIOCGIFADDR:
3401 case SIOCSIFADDR:
3402 case SIOCGIFBRDADDR:
3403 case SIOCSIFBRDADDR:
3404 case SIOCGIFNETMASK:
3405 case SIOCSIFNETMASK:
3406 case SIOCGIFDSTADDR:
3407 case SIOCSIFDSTADDR:
3408 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003409 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410#endif
3411
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003412 default:
3413 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 }
3415 return 0;
3416}
3417
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003418static unsigned int packet_poll(struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 poll_table *wait)
3420{
3421 struct sock *sk = sock->sk;
3422 struct packet_sock *po = pkt_sk(sk);
3423 unsigned int mask = datagram_poll(file, sock, wait);
3424
3425 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07003426 if (po->rx_ring.pg_vec) {
chetan lokef6fb8f102011-08-19 10:18:16 +00003427 if (!packet_previous_rx_frame(po, &po->rx_ring,
3428 TP_STATUS_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 mask |= POLLIN | POLLRDNORM;
3430 }
3431 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07003432 spin_lock_bh(&sk->sk_write_queue.lock);
3433 if (po->tx_ring.pg_vec) {
3434 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
3435 mask |= POLLOUT | POLLWRNORM;
3436 }
3437 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 return mask;
3439}
3440
3441
3442/* Dirty? Well, I still did not learn better way to account
3443 * for user mmaps.
3444 */
3445
3446static void packet_mm_open(struct vm_area_struct *vma)
3447{
3448 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003449 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003451
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 if (sk)
3453 atomic_inc(&pkt_sk(sk)->mapped);
3454}
3455
3456static void packet_mm_close(struct vm_area_struct *vma)
3457{
3458 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003459 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003461
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 if (sk)
3463 atomic_dec(&pkt_sk(sk)->mapped);
3464}
3465
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003466static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003467 .open = packet_mm_open,
3468 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469};
3470
Neil Horman0e3125c2010-11-16 10:26:47 -08003471static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
3472 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
3474 int i;
3475
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003476 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08003477 if (likely(pg_vec[i].buffer)) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00003478 if (is_vmalloc_addr(pg_vec[i].buffer))
Neil Horman0e3125c2010-11-16 10:26:47 -08003479 vfree(pg_vec[i].buffer);
3480 else
3481 free_pages((unsigned long)pg_vec[i].buffer,
3482 order);
3483 pg_vec[i].buffer = NULL;
3484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 }
3486 kfree(pg_vec);
3487}
3488
Olof Johanssoneea49cc92011-11-02 11:00:49 +00003489static char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003490{
Neil Horman0e3125c2010-11-16 10:26:47 -08003491 char *buffer = NULL;
3492 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
3493 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07003494
Neil Horman0e3125c2010-11-16 10:26:47 -08003495 buffer = (char *) __get_free_pages(gfp_flags, order);
3496
3497 if (buffer)
3498 return buffer;
3499
3500 /*
3501 * __get_free_pages failed, fall back to vmalloc
3502 */
Eric Dumazetbbce5a52010-11-20 07:31:54 +00003503 buffer = vzalloc((1 << order) * PAGE_SIZE);
Neil Horman0e3125c2010-11-16 10:26:47 -08003504
3505 if (buffer)
3506 return buffer;
3507
3508 /*
3509 * vmalloc failed, lets dig into swap here
3510 */
Neil Horman0e3125c2010-11-16 10:26:47 -08003511 gfp_flags &= ~__GFP_NORETRY;
3512 buffer = (char *)__get_free_pages(gfp_flags, order);
3513 if (buffer)
3514 return buffer;
3515
3516 /*
3517 * complete and utter failure
3518 */
3519 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003520}
3521
Neil Horman0e3125c2010-11-16 10:26:47 -08003522static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003523{
3524 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08003525 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003526 int i;
3527
Neil Horman0e3125c2010-11-16 10:26:47 -08003528 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003529 if (unlikely(!pg_vec))
3530 goto out;
3531
3532 for (i = 0; i < block_nr; i++) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00003533 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08003534 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003535 goto out_free_pgvec;
3536 }
3537
3538out:
3539 return pg_vec;
3540
3541out_free_pgvec:
3542 free_pg_vec(pg_vec, order, block_nr);
3543 pg_vec = NULL;
3544 goto out;
3545}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
chetan lokef6fb8f102011-08-19 10:18:16 +00003547static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
Johann Baudy69e3c752009-05-18 22:11:22 -07003548 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549{
Neil Horman0e3125c2010-11-16 10:26:47 -08003550 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08003552 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07003553 struct packet_ring_buffer *rb;
3554 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08003555 __be16 num;
chetan lokef6fb8f102011-08-19 10:18:16 +00003556 int err = -EINVAL;
3557 /* Added to avoid minimal code churn */
3558 struct tpacket_req *req = &req_u->req;
3559
3560 /* Opening a Tx-ring is NOT supported in TPACKET_V3 */
3561 if (!closing && tx_ring && (po->tp_version > TPACKET_V2)) {
3562 WARN(1, "Tx-ring is not supported.\n");
3563 goto out;
3564 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003565
3566 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
3567 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
3568
3569 err = -EBUSY;
3570 if (!closing) {
3571 if (atomic_read(&po->mapped))
3572 goto out;
3573 if (atomic_read(&rb->pending))
3574 goto out;
3575 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003576
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 if (req->tp_block_nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07003579 err = -EBUSY;
3580 if (unlikely(rb->pg_vec))
3581 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003583 switch (po->tp_version) {
3584 case TPACKET_V1:
3585 po->tp_hdrlen = TPACKET_HDRLEN;
3586 break;
3587 case TPACKET_V2:
3588 po->tp_hdrlen = TPACKET2_HDRLEN;
3589 break;
chetan lokef6fb8f102011-08-19 10:18:16 +00003590 case TPACKET_V3:
3591 po->tp_hdrlen = TPACKET3_HDRLEN;
3592 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07003593 }
3594
Johann Baudy69e3c752009-05-18 22:11:22 -07003595 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003596 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07003597 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003598 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07003599 goto out;
Patrick McHardy8913336a2008-07-18 18:05:19 -07003600 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
Johann Baudy69e3c752009-05-18 22:11:22 -07003601 po->tp_reserve))
3602 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003603 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07003604 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
Johann Baudy69e3c752009-05-18 22:11:22 -07003606 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
3607 if (unlikely(rb->frames_per_block <= 0))
3608 goto out;
3609 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
3610 req->tp_frame_nr))
3611 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
3613 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003614 order = get_order(req->tp_block_size);
3615 pg_vec = alloc_pg_vec(req, order);
3616 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 goto out;
chetan lokef6fb8f102011-08-19 10:18:16 +00003618 switch (po->tp_version) {
3619 case TPACKET_V3:
3620 /* Transmit path is not supported. We checked
3621 * it above but just being paranoid
3622 */
3623 if (!tx_ring)
3624 init_prb_bdqc(po, rb, pg_vec, req_u, tx_ring);
3625 break;
3626 default:
3627 break;
3628 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003629 }
3630 /* Done */
3631 else {
3632 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003633 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07003634 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 }
3636
3637 lock_sock(sk);
3638
3639 /* Detach socket from network */
3640 spin_lock(&po->bind_lock);
3641 was_running = po->running;
3642 num = po->num;
3643 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 po->num = 0;
David S. Millerce06b032011-07-04 01:44:29 -07003645 __unregister_prot_hook(sk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 }
3647 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003648
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 synchronize_net();
3650
3651 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08003652 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 if (closing || atomic_read(&po->mapped) == 0) {
3654 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07003655 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08003656 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07003657 rb->frame_max = (req->tp_frame_nr - 1);
3658 rb->head = 0;
3659 rb->frame_size = req->tp_frame_size;
3660 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Changli Gaoc053fd92010-12-10 16:02:20 -08003662 swap(rb->pg_vec_order, order);
3663 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Johann Baudy69e3c752009-05-18 22:11:22 -07003665 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
3666 po->prot_hook.func = (po->rx_ring.pg_vec) ?
3667 tpacket_rcv : packet_rcv;
3668 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003670 pr_err("packet_mmap: vma is busy: %d\n",
3671 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 }
Herbert Xu905db442009-01-30 14:12:06 -08003673 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
3675 spin_lock(&po->bind_lock);
David S. Millerce06b032011-07-04 01:44:29 -07003676 if (was_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 po->num = num;
David S. Millerce06b032011-07-04 01:44:29 -07003678 register_prot_hook(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 }
3680 spin_unlock(&po->bind_lock);
chetan lokef6fb8f102011-08-19 10:18:16 +00003681 if (closing && (po->tp_version > TPACKET_V2)) {
3682 /* Because we don't support block-based V3 on tx-ring */
3683 if (!tx_ring)
3684 prb_shutdown_retire_blk_timer(po, tx_ring, rb_queue);
3685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 release_sock(sk);
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 if (pg_vec)
3689 free_pg_vec(pg_vec, order, req->tp_block_nr);
3690out:
3691 return err;
3692}
3693
Johann Baudy69e3c752009-05-18 22:11:22 -07003694static int packet_mmap(struct file *file, struct socket *sock,
3695 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
3697 struct sock *sk = sock->sk;
3698 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07003699 unsigned long size, expected_size;
3700 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 unsigned long start;
3702 int err = -EINVAL;
3703 int i;
3704
3705 if (vma->vm_pgoff)
3706 return -EINVAL;
3707
Herbert Xu905db442009-01-30 14:12:06 -08003708 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07003709
3710 expected_size = 0;
3711 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
3712 if (rb->pg_vec) {
3713 expected_size += rb->pg_vec_len
3714 * rb->pg_vec_pages
3715 * PAGE_SIZE;
3716 }
3717 }
3718
3719 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07003721
3722 size = vma->vm_end - vma->vm_start;
3723 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 goto out;
3725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07003727 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
3728 if (rb->pg_vec == NULL)
3729 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003730
Johann Baudy69e3c752009-05-18 22:11:22 -07003731 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08003732 struct page *page;
3733 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07003734 int pg_num;
3735
Changli Gaoc56b4d92010-12-01 02:52:57 +00003736 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
3737 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07003738 err = vm_insert_page(vma, start, page);
3739 if (unlikely(err))
3740 goto out;
3741 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08003742 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07003743 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 }
Johann Baudy69e3c752009-05-18 22:11:22 -07003746
David S. Miller4ebf0ae2005-12-06 16:38:35 -08003747 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 vma->vm_ops = &packet_mmap_ops;
3749 err = 0;
3750
3751out:
Herbert Xu905db442009-01-30 14:12:06 -08003752 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 return err;
3754}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003756static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 .family = PF_PACKET,
3758 .owner = THIS_MODULE,
3759 .release = packet_release,
3760 .bind = packet_bind_spkt,
3761 .connect = sock_no_connect,
3762 .socketpair = sock_no_socketpair,
3763 .accept = sock_no_accept,
3764 .getname = packet_getname_spkt,
3765 .poll = datagram_poll,
3766 .ioctl = packet_ioctl,
3767 .listen = sock_no_listen,
3768 .shutdown = sock_no_shutdown,
3769 .setsockopt = sock_no_setsockopt,
3770 .getsockopt = sock_no_getsockopt,
3771 .sendmsg = packet_sendmsg_spkt,
3772 .recvmsg = packet_recvmsg,
3773 .mmap = sock_no_mmap,
3774 .sendpage = sock_no_sendpage,
3775};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003777static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 .family = PF_PACKET,
3779 .owner = THIS_MODULE,
3780 .release = packet_release,
3781 .bind = packet_bind,
3782 .connect = sock_no_connect,
3783 .socketpair = sock_no_socketpair,
3784 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003785 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 .poll = packet_poll,
3787 .ioctl = packet_ioctl,
3788 .listen = sock_no_listen,
3789 .shutdown = sock_no_shutdown,
3790 .setsockopt = packet_setsockopt,
3791 .getsockopt = packet_getsockopt,
3792 .sendmsg = packet_sendmsg,
3793 .recvmsg = packet_recvmsg,
3794 .mmap = packet_mmap,
3795 .sendpage = sock_no_sendpage,
3796};
3797
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003798static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 .family = PF_PACKET,
3800 .create = packet_create,
3801 .owner = THIS_MODULE,
3802};
3803
3804static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003805 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806};
3807
3808#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
3810static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00003811 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
Denis V. Luneve372c412007-11-19 22:31:54 -08003813 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00003814
3815 rcu_read_lock();
3816 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817}
3818
3819static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3820{
Herbert Xu1bf40952007-12-16 14:04:02 -08003821 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00003822 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823}
3824
3825static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00003826 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
stephen hemminger808f5112010-02-22 07:57:18 +00003828 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829}
3830
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09003831static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
3833 if (v == SEQ_START_TOKEN)
3834 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
3835 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00003836 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 const struct packet_sock *po = pkt_sk(s);
3838
3839 seq_printf(seq,
Dan Rosenberg71338aa2011-05-23 12:17:35 +00003840 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 s,
3842 atomic_read(&s->sk_refcnt),
3843 s->sk_type,
3844 ntohs(po->num),
3845 po->ifindex,
3846 po->running,
3847 atomic_read(&s->sk_rmem_alloc),
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06003848 from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00003849 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 }
3851
3852 return 0;
3853}
3854
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003855static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 .start = packet_seq_start,
3857 .next = packet_seq_next,
3858 .stop = packet_seq_stop,
3859 .show = packet_seq_show,
3860};
3861
3862static int packet_seq_open(struct inode *inode, struct file *file)
3863{
Denis V. Luneve372c412007-11-19 22:31:54 -08003864 return seq_open_net(inode, file, &packet_seq_ops,
3865 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866}
3867
Arjan van de Venda7071d2007-02-12 00:55:36 -08003868static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 .owner = THIS_MODULE,
3870 .open = packet_seq_open,
3871 .read = seq_read,
3872 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08003873 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874};
3875
3876#endif
3877
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003878static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003879{
Pavel Emelyanov0fa7fa92012-08-21 01:06:47 +00003880 mutex_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08003881 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003882
Gao fengd4beaa62013-02-18 01:34:54 +00003883 if (!proc_create("packet", 0, net->proc_net, &packet_seq_fops))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003884 return -ENOMEM;
3885
3886 return 0;
3887}
3888
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003889static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003890{
Gao fengece31ff2013-02-18 01:34:56 +00003891 remove_proc_entry("packet", net->proc_net);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003892}
3893
3894static struct pernet_operations packet_net_ops = {
3895 .init = packet_net_init,
3896 .exit = packet_net_exit,
3897};
3898
3899
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900static void __exit packet_exit(void)
3901{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003903 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 sock_unregister(PF_PACKET);
3905 proto_unregister(&packet_proto);
3906}
3907
3908static int __init packet_init(void)
3909{
3910 int rc = proto_register(&packet_proto, 0);
3911
3912 if (rc != 0)
3913 goto out;
3914
3915 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08003916 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918out:
3919 return rc;
3920}
3921
3922module_init(packet_init);
3923module_exit(packet_exit);
3924MODULE_LICENSE("GPL");
3925MODULE_ALIAS_NETPROTO(PF_PACKET);