blob: e79efaf063895422275675279c4a9230d0bffd76 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * PACKET - implements raw packet sockets.
7 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Alan Cox, <gw4pts@gw4pts.ampr.org>
11 *
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090012 * Fixes:
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Alan Cox : verify_area() now used correctly
14 * Alan Cox : new skbuff lists, look ma no backlogs!
15 * Alan Cox : tidied skbuff lists.
16 * Alan Cox : Now uses generic datagram routines I
17 * added. Also fixed the peek/read crash
18 * from all old Linux datagram code.
19 * Alan Cox : Uses the improved datagram code.
20 * Alan Cox : Added NULL's for socket options.
21 * Alan Cox : Re-commented the code.
22 * Alan Cox : Use new kernel side addressing
23 * Rob Janssen : Correct MTU usage.
24 * Dave Platt : Counter leaks caused by incorrect
25 * interrupt locking and some slightly
26 * dubious gcc output. Can you read
27 * compiler: it said _VOLATILE_
28 * Richard Kooijman : Timestamp fixes.
29 * Alan Cox : New buffers. Use sk->mac.raw.
30 * Alan Cox : sendmsg/recvmsg support.
31 * Alan Cox : Protocol setting support
32 * Alexey Kuznetsov : Untied from IPv4 stack.
33 * Cyrus Durgin : Fixed kerneld for kmod.
34 * Michal Ostrowski : Module initialization cleanup.
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090035 * Ulises Alonso : Frame number limit removal and
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * packet_set_ring memory leak.
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070037 * Eric Biederman : Allow for > 8 byte hardware addresses.
38 * The convention is that longer addresses
39 * will simply extend the hardware address
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090040 * byte arrays at the end of sockaddr_ll
Eric W. Biederman0fb375f2005-09-21 00:11:37 -070041 * and packet_mreq.
Johann Baudy69e3c752009-05-18 22:11:22 -070042 * Johann Baudy : Added TX RING.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 *
44 * This program is free software; you can redistribute it and/or
45 * modify it under the terms of the GNU General Public License
46 * as published by the Free Software Foundation; either version
47 * 2 of the License, or (at your option) any later version.
48 *
49 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +090050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/mm.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080053#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/fcntl.h>
55#include <linux/socket.h>
56#include <linux/in.h>
57#include <linux/inet.h>
58#include <linux/netdevice.h>
59#include <linux/if_packet.h>
60#include <linux/wireless.h>
Herbert Xuffbc6112007-02-04 23:33:10 -080061#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/kmod.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Neil Horman0e3125c2010-11-16 10:26:47 -080064#include <linux/vmalloc.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020065#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <net/ip.h>
67#include <net/protocol.h>
68#include <linux/skbuff.h>
69#include <net/sock.h>
70#include <linux/errno.h>
71#include <linux/timer.h>
72#include <asm/system.h>
73#include <asm/uaccess.h>
74#include <asm/ioctls.h>
75#include <asm/page.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040076#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <asm/io.h>
78#include <linux/proc_fs.h>
79#include <linux/seq_file.h>
80#include <linux/poll.h>
81#include <linux/module.h>
82#include <linux/init.h>
Herbert Xu905db442009-01-30 14:12:06 -080083#include <linux/mutex.h>
Eric Dumazet05423b22009-10-26 18:40:35 -070084#include <linux/if_vlan.h>
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -080085#include <linux/virtio_net.h>
Richard Cochraned85b562010-04-07 22:41:28 +000086#include <linux/errqueue.h>
Scott McMillan614f60f2010-06-02 05:53:56 -070087#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#ifdef CONFIG_INET
90#include <net/inet_common.h>
91#endif
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 Assumptions:
95 - if device has no dev->hard_header routine, it adds and removes ll header
96 inside itself. In this case ll header is invisible outside of device,
97 but higher levels still should reserve dev->hard_header_len.
98 Some devices are enough clever to reallocate skb, when header
99 will not fit to reserved space (tunnel), another ones are silly
100 (PPP).
101 - packet socket receives packets with pulled ll header,
102 so that SOCK_RAW should push it back.
103
104On receive:
105-----------
106
107Incoming, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700108 mac_header -> ll header
109 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111Outgoing, dev->hard_header!=NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700112 mac_header -> ll header
113 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115Incoming, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700116 mac_header -> UNKNOWN position. It is very likely, that it points to ll
117 header. PPP makes it, that is wrong, because introduce
YOSHIFUJI Hideakidb0c58f2007-07-19 10:44:35 +0900118 assymetry between rx and tx paths.
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700119 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121Outgoing, dev->hard_header==NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700122 mac_header -> data. ll header is still not built!
123 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125Resume
126 If dev->hard_header==NULL we are unlikely to restore sensible ll header.
127
128
129On transmit:
130------------
131
132dev->hard_header != NULL
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700133 mac_header -> ll header
134 data -> ll header
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136dev->hard_header == NULL (ll header is added by device, we cannot control it)
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700137 mac_header -> data
138 data -> data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 We should set nh.raw on output to correct posistion,
141 packet classifier depends on it.
142 */
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Private packet socket structures. */
145
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000146struct packet_mclist {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct packet_mclist *next;
148 int ifindex;
149 int count;
150 unsigned short type;
151 unsigned short alen;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700152 unsigned char addr[MAX_ADDR_LEN];
153};
154/* identical to struct packet_mreq except it has
155 * a longer address field.
156 */
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000157struct packet_mreq_max {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -0700158 int mr_ifindex;
159 unsigned short mr_type;
160 unsigned short mr_alen;
161 unsigned char mr_address[MAX_ADDR_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162};
David S. Millera2efcfa2007-05-29 13:12:50 -0700163
Johann Baudy69e3c752009-05-18 22:11:22 -0700164static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
165 int closing, int tx_ring);
166
Neil Horman0e3125c2010-11-16 10:26:47 -0800167#define PGV_FROM_VMALLOC 1
168struct pgv {
169 char *buffer;
Neil Horman0e3125c2010-11-16 10:26:47 -0800170};
171
Johann Baudy69e3c752009-05-18 22:11:22 -0700172struct packet_ring_buffer {
Neil Horman0e3125c2010-11-16 10:26:47 -0800173 struct pgv *pg_vec;
Johann Baudy69e3c752009-05-18 22:11:22 -0700174 unsigned int head;
175 unsigned int frames_per_block;
176 unsigned int frame_size;
177 unsigned int frame_max;
178
179 unsigned int pg_vec_order;
180 unsigned int pg_vec_pages;
181 unsigned int pg_vec_len;
182
183 atomic_t pending;
184};
185
186struct packet_sock;
187static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189static void packet_flush_mclist(struct sock *sk);
190
191struct packet_sock {
192 /* struct sock has to be the first member of packet_sock */
193 struct sock sk;
194 struct tpacket_stats stats;
Johann Baudy69e3c752009-05-18 22:11:22 -0700195 struct packet_ring_buffer rx_ring;
196 struct packet_ring_buffer tx_ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 int copy_thresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 spinlock_t bind_lock;
Herbert Xu905db442009-01-30 14:12:06 -0800199 struct mutex pg_vec_lock;
Herbert Xu8dc41942007-02-04 23:31:32 -0800200 unsigned int running:1, /* prot_hook is attached*/
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700201 auxdata:1,
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -0800202 origdev:1,
203 has_vnet_hdr:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 int ifindex; /* bound device */
Al Viro0e11c912006-11-08 00:26:29 -0800205 __be16 num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 struct packet_mclist *mclist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 atomic_t mapped;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700208 enum tpacket_versions tp_version;
209 unsigned int tp_hdrlen;
Patrick McHardy89133362008-07-18 18:05:19 -0700210 unsigned int tp_reserve;
Johann Baudy69e3c752009-05-18 22:11:22 -0700211 unsigned int tp_loss:1;
Scott McMillan614f60f2010-06-02 05:53:56 -0700212 unsigned int tp_tstamp;
Eric Dumazet94b05952009-10-16 04:02:20 +0000213 struct packet_type prot_hook ____cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
Herbert Xuffbc6112007-02-04 23:33:10 -0800216struct packet_skb_cb {
217 unsigned int origlen;
218 union {
219 struct sockaddr_pkt pkt;
220 struct sockaddr_ll ll;
221 } sa;
222};
223
224#define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
Herbert Xu8dc41942007-02-04 23:31:32 -0800225
Changli Gaof6dafa92010-12-07 04:26:16 +0000226static inline __pure struct page *pgv_to_page(void *addr)
Changli Gao0af55bb2010-12-01 02:52:20 +0000227{
228 if (is_vmalloc_addr(addr))
229 return vmalloc_to_page(addr);
230 return virt_to_page(addr);
231}
232
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700233static void __packet_set_status(struct packet_sock *po, void *frame, int status)
234{
235 union {
236 struct tpacket_hdr *h1;
237 struct tpacket2_hdr *h2;
238 void *raw;
239 } h;
240
241 h.raw = frame;
242 switch (po->tp_version) {
243 case TPACKET_V1:
244 h.h1->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000245 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700246 break;
247 case TPACKET_V2:
248 h.h2->tp_status = status;
Changli Gao0af55bb2010-12-01 02:52:20 +0000249 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700250 break;
Johann Baudy69e3c752009-05-18 22:11:22 -0700251 default:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000252 pr_err("TPACKET version not supported\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700253 BUG();
254 }
255
256 smp_wmb();
257}
258
259static int __packet_get_status(struct packet_sock *po, void *frame)
260{
261 union {
262 struct tpacket_hdr *h1;
263 struct tpacket2_hdr *h2;
264 void *raw;
265 } h;
266
267 smp_rmb();
268
269 h.raw = frame;
270 switch (po->tp_version) {
271 case TPACKET_V1:
Changli Gao0af55bb2010-12-01 02:52:20 +0000272 flush_dcache_page(pgv_to_page(&h.h1->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700273 return h.h1->tp_status;
274 case TPACKET_V2:
Changli Gao0af55bb2010-12-01 02:52:20 +0000275 flush_dcache_page(pgv_to_page(&h.h2->tp_status));
Johann Baudy69e3c752009-05-18 22:11:22 -0700276 return h.h2->tp_status;
277 default:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000278 pr_err("TPACKET version not supported\n");
Johann Baudy69e3c752009-05-18 22:11:22 -0700279 BUG();
280 return 0;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
Johann Baudy69e3c752009-05-18 22:11:22 -0700283
284static void *packet_lookup_frame(struct packet_sock *po,
285 struct packet_ring_buffer *rb,
286 unsigned int position,
287 int status)
288{
289 unsigned int pg_vec_pos, frame_offset;
290 union {
291 struct tpacket_hdr *h1;
292 struct tpacket2_hdr *h2;
293 void *raw;
294 } h;
295
296 pg_vec_pos = position / rb->frames_per_block;
297 frame_offset = position % rb->frames_per_block;
298
Neil Horman0e3125c2010-11-16 10:26:47 -0800299 h.raw = rb->pg_vec[pg_vec_pos].buffer +
300 (frame_offset * rb->frame_size);
Johann Baudy69e3c752009-05-18 22:11:22 -0700301
302 if (status != __packet_get_status(po, h.raw))
303 return NULL;
304
305 return h.raw;
306}
307
308static inline void *packet_current_frame(struct packet_sock *po,
309 struct packet_ring_buffer *rb,
310 int status)
311{
312 return packet_lookup_frame(po, rb, rb->head, status);
313}
314
315static inline void *packet_previous_frame(struct packet_sock *po,
316 struct packet_ring_buffer *rb,
317 int status)
318{
319 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
320 return packet_lookup_frame(po, rb, previous, status);
321}
322
323static inline void packet_increment_head(struct packet_ring_buffer *buff)
324{
325 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
326}
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328static inline struct packet_sock *pkt_sk(struct sock *sk)
329{
330 return (struct packet_sock *)sk;
331}
332
333static void packet_sock_destruct(struct sock *sk)
334{
Richard Cochraned85b562010-04-07 22:41:28 +0000335 skb_queue_purge(&sk->sk_error_queue);
336
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700337 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
338 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 if (!sock_flag(sk, SOCK_DEAD)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000341 pr_err("Attempt to release alive packet socket: %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return;
343 }
344
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -0800345 sk_refcnt_debug_dec(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800349static const struct proto_ops packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800351static const struct proto_ops packet_ops_spkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000353static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
354 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct sock *sk;
357 struct sockaddr_pkt *spkt;
358
359 /*
360 * When we registered the protocol we saved the socket in the data
361 * field for just this event.
362 */
363
364 sk = pt->af_packet_priv;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /*
367 * Yank back the headers [hope the device set this
368 * right or kerboom...]
369 *
370 * Incoming packets have ll header pulled,
371 * push it back.
372 *
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700373 * For outgoing ones skb->data == skb_mac_header(skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * so that this procedure is noop.
375 */
376
377 if (skb->pkt_type == PACKET_LOOPBACK)
378 goto out;
379
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800380 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800381 goto out;
382
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000383 skb = skb_share_check(skb, GFP_ATOMIC);
384 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto oom;
386
387 /* drop any routing info */
Eric Dumazetadf30902009-06-02 05:19:30 +0000388 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Phil Oester84531c22005-07-12 11:57:52 -0700390 /* drop conntrack reference */
391 nf_reset(skb);
392
Herbert Xuffbc6112007-02-04 23:33:10 -0800393 spkt = &PACKET_SKB_CB(skb)->sa.pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700395 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /*
398 * The SOCK_PACKET socket receives _all_ frames.
399 */
400
401 spkt->spkt_family = dev->type;
402 strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
403 spkt->spkt_protocol = skb->protocol;
404
405 /*
406 * Charge the memory to the socket. This is done specifically
407 * to prevent sockets using all the memory up.
408 */
409
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000410 if (sock_queue_rcv_skb(sk, skb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return 0;
412
413out:
414 kfree_skb(skb);
415oom:
416 return 0;
417}
418
419
420/*
421 * Output a raw packet to a device layer. This bypasses all the other
422 * protocol layers and you must therefore supply it with a complete frame
423 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
426 struct msghdr *msg, size_t len)
427{
428 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000429 struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000430 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct net_device *dev;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000432 __be16 proto = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 int err;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900436 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 */
438
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000439 if (saddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (msg->msg_namelen < sizeof(struct sockaddr))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000441 return -EINVAL;
442 if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
443 proto = saddr->spkt_protocol;
444 } else
445 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900448 * Find the device first to size check it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
450
451 saddr->spkt_device[13] = 0;
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000452retry:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100453 rcu_read_lock();
454 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 err = -ENODEV;
456 if (dev == NULL)
457 goto out_unlock;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900458
David S. Millerd5e76b02007-01-25 19:30:36 -0800459 err = -ENETDOWN;
460 if (!(dev->flags & IFF_UP))
461 goto out_unlock;
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /*
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000464 * You may not queue a frame bigger than the mtu. This is the lowest level
465 * raw protocol and you must do your own fragmentation at this level.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 err = -EMSGSIZE;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -0800469 if (len > dev->mtu + dev->hard_header_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 goto out_unlock;
471
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000472 if (!skb) {
473 size_t reserved = LL_RESERVED_SPACE(dev);
474 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000476 rcu_read_unlock();
477 skb = sock_wmalloc(sk, len + reserved, 0, GFP_KERNEL);
478 if (skb == NULL)
479 return -ENOBUFS;
480 /* FIXME: Save some space for broken drivers that write a hard
481 * header at transmission time by themselves. PPP is the notable
482 * one here. This should really be fixed at the driver level.
483 */
484 skb_reserve(skb, reserved);
485 skb_reset_network_header(skb);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900486
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000487 /* Try to align data part correctly */
488 if (hhlen) {
489 skb->data -= hhlen;
490 skb->tail -= hhlen;
491 if (len < hhlen)
492 skb_reset_network_header(skb);
493 }
494 err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
495 if (err)
496 goto out_free;
497 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 skb->protocol = proto;
502 skb->dev = dev;
503 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +0000504 skb->mark = sk->sk_mark;
Oliver Hartkopp2244d072010-08-17 08:59:14 +0000505 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +0000506 if (err < 0)
507 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 dev_queue_xmit(skb);
Eric Dumazet654d1f82009-11-02 10:43:32 +0100510 rcu_read_unlock();
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000511 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513out_unlock:
Eric Dumazet654d1f82009-11-02 10:43:32 +0100514 rcu_read_unlock();
Eric Dumazet1a35ca82009-12-15 05:47:03 +0000515out_free:
516 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return err;
518}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Eric Dumazet62ab0812010-12-06 20:50:09 +0000520static inline unsigned int run_filter(const struct sk_buff *skb,
521 const struct sock *sk,
David S. Millerdbcb5852007-01-24 15:21:02 -0800522 unsigned int res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 struct sk_filter *filter;
525
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700526 rcu_read_lock_bh();
Paul E. McKenneya898def2010-02-22 17:04:49 -0800527 filter = rcu_dereference_bh(sk->sk_filter);
David S. Millerdbcb5852007-01-24 15:21:02 -0800528 if (filter != NULL)
Eric Dumazet93aaae22010-11-19 09:49:59 -0800529 res = sk_run_filter(skb, filter->insns);
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700530 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
David S. Millerdbcb5852007-01-24 15:21:02 -0800532 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/*
Eric Dumazet62ab0812010-12-06 20:50:09 +0000536 * This function makes lazy skb cloning in hope that most of packets
537 * are discarded by BPF.
538 *
539 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
540 * and skb->cb are mangled. It works because (and until) packets
541 * falling here are owned by current CPU. Output packets are cloned
542 * by dev_queue_xmit_nit(), input packets are processed by net_bh
543 * sequencially, so that if we return skb to original state on exit,
544 * we will not harm anyone.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
546
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000547static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
548 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 struct sock *sk;
551 struct sockaddr_ll *sll;
552 struct packet_sock *po;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000553 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800555 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 if (skb->pkt_type == PACKET_LOOPBACK)
558 goto drop;
559
560 sk = pt->af_packet_priv;
561 po = pkt_sk(sk);
562
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800563 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800564 goto drop;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 skb->dev = dev;
567
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700568 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* The device has an explicit notion of ll header,
Eric Dumazet62ab0812010-12-06 20:50:09 +0000570 * exported to higher levels.
571 *
572 * Otherwise, the device hides details of its frame
573 * structure, so that corresponding packet head is
574 * never delivered to user.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 */
576 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700577 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 else if (skb->pkt_type == PACKET_OUTGOING) {
579 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300580 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582 }
583
584 snaplen = skb->len;
585
David S. Millerdbcb5852007-01-24 15:21:02 -0800586 res = run_filter(skb, sk, snaplen);
587 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700588 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800589 if (snaplen > res)
590 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
593 (unsigned)sk->sk_rcvbuf)
594 goto drop_n_acct;
595
596 if (skb_shared(skb)) {
597 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
598 if (nskb == NULL)
599 goto drop_n_acct;
600
601 if (skb_head != skb->data) {
602 skb->data = skb_head;
603 skb->len = skb_len;
604 }
605 kfree_skb(skb);
606 skb = nskb;
607 }
608
Herbert Xuffbc6112007-02-04 23:33:10 -0800609 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
610 sizeof(skb->cb));
611
612 sll = &PACKET_SKB_CB(skb)->sa.ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 sll->sll_family = AF_PACKET;
614 sll->sll_hatype = dev->type;
615 sll->sll_protocol = skb->protocol;
616 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800617 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700618 sll->sll_ifindex = orig_dev->ifindex;
619 else
620 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700622 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Herbert Xuffbc6112007-02-04 23:33:10 -0800624 PACKET_SKB_CB(skb)->origlen = skb->len;
Herbert Xu8dc41942007-02-04 23:31:32 -0800625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (pskb_trim(skb, snaplen))
627 goto drop_n_acct;
628
629 skb_set_owner_r(skb, sk);
630 skb->dev = NULL;
Eric Dumazetadf30902009-06-02 05:19:30 +0000631 skb_dst_drop(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Phil Oester84531c22005-07-12 11:57:52 -0700633 /* drop conntrack reference */
634 nf_reset(skb);
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 spin_lock(&sk->sk_receive_queue.lock);
637 po->stats.tp_packets++;
Neil Horman3b885782009-10-12 13:26:31 -0700638 skb->dropcount = atomic_read(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 __skb_queue_tail(&sk->sk_receive_queue, skb);
640 spin_unlock(&sk->sk_receive_queue.lock);
641 sk->sk_data_ready(sk, skb->len);
642 return 0;
643
644drop_n_acct:
Neil Horman3b885782009-10-12 13:26:31 -0700645 po->stats.tp_drops = atomic_inc_return(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647drop_n_restore:
648 if (skb_head != skb->data && skb_shared(skb)) {
649 skb->data = skb_head;
650 skb->len = skb_len;
651 }
652drop:
Neil Hormanead2ceb2009-03-11 09:49:55 +0000653 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return 0;
655}
656
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000657static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
658 struct packet_type *pt, struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct sock *sk;
661 struct packet_sock *po;
662 struct sockaddr_ll *sll;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700663 union {
664 struct tpacket_hdr *h1;
665 struct tpacket2_hdr *h2;
666 void *raw;
667 } h;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000668 u8 *skb_head = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 int skb_len = skb->len;
David S. Millerdbcb5852007-01-24 15:21:02 -0800670 unsigned int snaplen, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700672 unsigned short macoff, netoff, hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct sk_buff *copy_skb = NULL;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700674 struct timeval tv;
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700675 struct timespec ts;
Scott McMillan614f60f2010-06-02 05:53:56 -0700676 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 if (skb->pkt_type == PACKET_LOOPBACK)
679 goto drop;
680
681 sk = pt->af_packet_priv;
682 po = pkt_sk(sk);
683
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800684 if (!net_eq(dev_net(dev), sock_net(sk)))
Denis V. Lunevd12d01d2007-11-19 22:28:35 -0800685 goto drop;
686
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700687 if (dev->header_ops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (sk->sk_type != SOCK_DGRAM)
Arnaldo Carvalho de Melo98e399f2007-03-19 15:33:04 -0700689 skb_push(skb, skb->data - skb_mac_header(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 else if (skb->pkt_type == PACKET_OUTGOING) {
691 /* Special case: outgoing packets have ll header at head */
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300692 skb_pull(skb, skb_network_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 }
695
Herbert Xu8dc41942007-02-04 23:31:32 -0800696 if (skb->ip_summed == CHECKSUM_PARTIAL)
697 status |= TP_STATUS_CSUMNOTREADY;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 snaplen = skb->len;
700
David S. Millerdbcb5852007-01-24 15:21:02 -0800701 res = run_filter(skb, sk, snaplen);
702 if (!res)
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700703 goto drop_n_restore;
David S. Millerdbcb5852007-01-24 15:21:02 -0800704 if (snaplen > res)
705 snaplen = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 if (sk->sk_type == SOCK_DGRAM) {
Patrick McHardy89133362008-07-18 18:05:19 -0700708 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
709 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 } else {
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -0300711 unsigned maclen = skb_network_offset(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700712 netoff = TPACKET_ALIGN(po->tp_hdrlen +
Patrick McHardy89133362008-07-18 18:05:19 -0700713 (maclen < 16 ? 16 : maclen)) +
714 po->tp_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 macoff = netoff - maclen;
716 }
717
Johann Baudy69e3c752009-05-18 22:11:22 -0700718 if (macoff + snaplen > po->rx_ring.frame_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (po->copy_thresh &&
720 atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
721 (unsigned)sk->sk_rcvbuf) {
722 if (skb_shared(skb)) {
723 copy_skb = skb_clone(skb, GFP_ATOMIC);
724 } else {
725 copy_skb = skb_get(skb);
726 skb_head = skb->data;
727 }
728 if (copy_skb)
729 skb_set_owner_r(copy_skb, sk);
730 }
Johann Baudy69e3c752009-05-18 22:11:22 -0700731 snaplen = po->rx_ring.frame_size - macoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if ((int)snaplen < 0)
733 snaplen = 0;
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 spin_lock(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -0700737 h.raw = packet_current_frame(po, &po->rx_ring, TP_STATUS_KERNEL);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700738 if (!h.raw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto ring_is_full;
Johann Baudy69e3c752009-05-18 22:11:22 -0700740 packet_increment_head(&po->rx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 po->stats.tp_packets++;
742 if (copy_skb) {
743 status |= TP_STATUS_COPY;
744 __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
745 }
746 if (!po->stats.tp_drops)
747 status &= ~TP_STATUS_LOSING;
748 spin_unlock(&sk->sk_receive_queue.lock);
749
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700750 skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700752 switch (po->tp_version) {
753 case TPACKET_V1:
754 h.h1->tp_len = skb->len;
755 h.h1->tp_snaplen = snaplen;
756 h.h1->tp_mac = macoff;
757 h.h1->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -0700758 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
759 && shhwtstamps->syststamp.tv64)
760 tv = ktime_to_timeval(shhwtstamps->syststamp);
761 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
762 && shhwtstamps->hwtstamp.tv64)
763 tv = ktime_to_timeval(shhwtstamps->hwtstamp);
764 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700765 tv = ktime_to_timeval(skb->tstamp);
766 else
767 do_gettimeofday(&tv);
768 h.h1->tp_sec = tv.tv_sec;
769 h.h1->tp_usec = tv.tv_usec;
770 hdrlen = sizeof(*h.h1);
771 break;
772 case TPACKET_V2:
773 h.h2->tp_len = skb->len;
774 h.h2->tp_snaplen = snaplen;
775 h.h2->tp_mac = macoff;
776 h.h2->tp_net = netoff;
Scott McMillan614f60f2010-06-02 05:53:56 -0700777 if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
778 && shhwtstamps->syststamp.tv64)
779 ts = ktime_to_timespec(shhwtstamps->syststamp);
780 else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
781 && shhwtstamps->hwtstamp.tv64)
782 ts = ktime_to_timespec(shhwtstamps->hwtstamp);
783 else if (skb->tstamp.tv64)
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700784 ts = ktime_to_timespec(skb->tstamp);
785 else
786 getnstimeofday(&ts);
787 h.h2->tp_sec = ts.tv_sec;
788 h.h2->tp_nsec = ts.tv_nsec;
Eric Dumazet05423b22009-10-26 18:40:35 -0700789 h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700790 hdrlen = sizeof(*h.h2);
791 break;
792 default:
793 BUG();
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700796 sll = h.raw + TPACKET_ALIGN(hdrlen);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700797 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 sll->sll_family = AF_PACKET;
799 sll->sll_hatype = dev->type;
800 sll->sll_protocol = skb->protocol;
801 sll->sll_pkttype = skb->pkt_type;
Peter P Waskiewicz Jr8032b462007-11-10 22:03:25 -0800802 if (unlikely(po->origdev))
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -0700803 sll->sll_ifindex = orig_dev->ifindex;
804 else
805 sll->sll_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Patrick McHardybbd6ef82008-07-14 22:50:15 -0700807 __packet_set_status(po, h.raw, status);
Ralf Baechlee16aa202006-12-07 00:11:33 -0800808 smp_mb();
Changli Gaof6dafa92010-12-07 04:26:16 +0000809#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 {
Changli Gao0af55bb2010-12-01 02:52:20 +0000811 u8 *start, *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Changli Gao0af55bb2010-12-01 02:52:20 +0000813 end = (u8 *)PAGE_ALIGN((unsigned long)h.raw + macoff + snaplen);
814 for (start = h.raw; start < end; start += PAGE_SIZE)
815 flush_dcache_page(pgv_to_page(start));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
Changli Gaof6dafa92010-12-07 04:26:16 +0000817#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 sk->sk_data_ready(sk, 0);
820
821drop_n_restore:
822 if (skb_head != skb->data && skb_shared(skb)) {
823 skb->data = skb_head;
824 skb->len = skb_len;
825 }
826drop:
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +0900827 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return 0;
829
830ring_is_full:
831 po->stats.tp_drops++;
832 spin_unlock(&sk->sk_receive_queue.lock);
833
834 sk->sk_data_ready(sk, 0);
Wei Yongjunacb5d752009-02-25 00:36:42 +0000835 kfree_skb(copy_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto drop_n_restore;
837}
838
Johann Baudy69e3c752009-05-18 22:11:22 -0700839static void tpacket_destruct_skb(struct sk_buff *skb)
840{
841 struct packet_sock *po = pkt_sk(skb->sk);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000842 void *ph;
Johann Baudy69e3c752009-05-18 22:11:22 -0700843
844 BUG_ON(skb == NULL);
845
846 if (likely(po->tx_ring.pg_vec)) {
847 ph = skb_shinfo(skb)->destructor_arg;
848 BUG_ON(__packet_get_status(po, ph) != TP_STATUS_SENDING);
849 BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
850 atomic_dec(&po->tx_ring.pending);
851 __packet_set_status(po, ph, TP_STATUS_AVAILABLE);
852 }
853
854 sock_wfree(skb);
855}
856
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000857static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
858 void *frame, struct net_device *dev, int size_max,
859 __be16 proto, unsigned char *addr)
Johann Baudy69e3c752009-05-18 22:11:22 -0700860{
861 union {
862 struct tpacket_hdr *h1;
863 struct tpacket2_hdr *h2;
864 void *raw;
865 } ph;
866 int to_write, offset, len, tp_len, nr_frags, len_max;
867 struct socket *sock = po->sk.sk_socket;
868 struct page *page;
869 void *data;
870 int err;
871
872 ph.raw = frame;
873
874 skb->protocol = proto;
875 skb->dev = dev;
876 skb->priority = po->sk.sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +0000877 skb->mark = po->sk.sk_mark;
Johann Baudy69e3c752009-05-18 22:11:22 -0700878 skb_shinfo(skb)->destructor_arg = ph.raw;
879
880 switch (po->tp_version) {
881 case TPACKET_V2:
882 tp_len = ph.h2->tp_len;
883 break;
884 default:
885 tp_len = ph.h1->tp_len;
886 break;
887 }
888 if (unlikely(tp_len > size_max)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000889 pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
Johann Baudy69e3c752009-05-18 22:11:22 -0700890 return -EMSGSIZE;
891 }
892
893 skb_reserve(skb, LL_RESERVED_SPACE(dev));
894 skb_reset_network_header(skb);
895
896 data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
897 to_write = tp_len;
898
899 if (sock->type == SOCK_DGRAM) {
900 err = dev_hard_header(skb, dev, ntohs(proto), addr,
901 NULL, tp_len);
902 if (unlikely(err < 0))
903 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000904 } else if (dev->hard_header_len) {
Johann Baudy69e3c752009-05-18 22:11:22 -0700905 /* net device doesn't like empty head */
906 if (unlikely(tp_len <= dev->hard_header_len)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000907 pr_err("packet size is too short (%d < %d)\n",
908 tp_len, dev->hard_header_len);
Johann Baudy69e3c752009-05-18 22:11:22 -0700909 return -EINVAL;
910 }
911
912 skb_push(skb, dev->hard_header_len);
913 err = skb_store_bits(skb, 0, data,
914 dev->hard_header_len);
915 if (unlikely(err))
916 return err;
917
918 data += dev->hard_header_len;
919 to_write -= dev->hard_header_len;
920 }
921
922 err = -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -0700923 offset = offset_in_page(data);
924 len_max = PAGE_SIZE - offset;
925 len = ((to_write > len_max) ? len_max : to_write);
926
927 skb->data_len = to_write;
928 skb->len += to_write;
929 skb->truesize += to_write;
930 atomic_add(to_write, &po->sk.sk_wmem_alloc);
931
932 while (likely(to_write)) {
933 nr_frags = skb_shinfo(skb)->nr_frags;
934
935 if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000936 pr_err("Packet exceed the number of skb frags(%lu)\n",
937 MAX_SKB_FRAGS);
Johann Baudy69e3c752009-05-18 22:11:22 -0700938 return -EFAULT;
939 }
940
Changli Gao0af55bb2010-12-01 02:52:20 +0000941 page = pgv_to_page(data);
942 data += len;
Johann Baudy69e3c752009-05-18 22:11:22 -0700943 flush_dcache_page(page);
944 get_page(page);
Changli Gao0af55bb2010-12-01 02:52:20 +0000945 skb_fill_page_desc(skb, nr_frags, page, offset, len);
Johann Baudy69e3c752009-05-18 22:11:22 -0700946 to_write -= len;
947 offset = 0;
948 len_max = PAGE_SIZE;
949 len = ((to_write > len_max) ? len_max : to_write);
950 }
951
952 return tp_len;
953}
954
955static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
956{
957 struct socket *sock;
958 struct sk_buff *skb;
959 struct net_device *dev;
960 __be16 proto;
961 int ifindex, err, reserve = 0;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +0000962 void *ph;
963 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Johann Baudy69e3c752009-05-18 22:11:22 -0700964 int tp_len, size_max;
965 unsigned char *addr;
966 int len_sum = 0;
967 int status = 0;
968
969 sock = po->sk.sk_socket;
970
971 mutex_lock(&po->pg_vec_lock);
972
973 err = -EBUSY;
974 if (saddr == NULL) {
975 ifindex = po->ifindex;
976 proto = po->num;
977 addr = NULL;
978 } else {
979 err = -EINVAL;
980 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
981 goto out;
982 if (msg->msg_namelen < (saddr->sll_halen
983 + offsetof(struct sockaddr_ll,
984 sll_addr)))
985 goto out;
986 ifindex = saddr->sll_ifindex;
987 proto = saddr->sll_protocol;
988 addr = saddr->sll_addr;
989 }
990
991 dev = dev_get_by_index(sock_net(&po->sk), ifindex);
992 err = -ENXIO;
993 if (unlikely(dev == NULL))
994 goto out;
995
996 reserve = dev->hard_header_len;
997
998 err = -ENETDOWN;
999 if (unlikely(!(dev->flags & IFF_UP)))
1000 goto out_put;
1001
1002 size_max = po->tx_ring.frame_size
Gabor Gombasb5dd8842009-10-29 03:19:11 -07001003 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
Johann Baudy69e3c752009-05-18 22:11:22 -07001004
1005 if (size_max > dev->mtu + reserve)
1006 size_max = dev->mtu + reserve;
1007
1008 do {
1009 ph = packet_current_frame(po, &po->tx_ring,
1010 TP_STATUS_SEND_REQUEST);
1011
1012 if (unlikely(ph == NULL)) {
1013 schedule();
1014 continue;
1015 }
1016
1017 status = TP_STATUS_SEND_REQUEST;
1018 skb = sock_alloc_send_skb(&po->sk,
1019 LL_ALLOCATED_SPACE(dev)
1020 + sizeof(struct sockaddr_ll),
1021 0, &err);
1022
1023 if (unlikely(skb == NULL))
1024 goto out_status;
1025
1026 tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
1027 addr);
1028
1029 if (unlikely(tp_len < 0)) {
1030 if (po->tp_loss) {
1031 __packet_set_status(po, ph,
1032 TP_STATUS_AVAILABLE);
1033 packet_increment_head(&po->tx_ring);
1034 kfree_skb(skb);
1035 continue;
1036 } else {
1037 status = TP_STATUS_WRONG_FORMAT;
1038 err = tp_len;
1039 goto out_status;
1040 }
1041 }
1042
1043 skb->destructor = tpacket_destruct_skb;
1044 __packet_set_status(po, ph, TP_STATUS_SENDING);
1045 atomic_inc(&po->tx_ring.pending);
1046
1047 status = TP_STATUS_SEND_REQUEST;
1048 err = dev_queue_xmit(skb);
Jarek Poplawskieb70df12010-01-10 22:04:19 +00001049 if (unlikely(err > 0)) {
1050 err = net_xmit_errno(err);
1051 if (err && __packet_get_status(po, ph) ==
1052 TP_STATUS_AVAILABLE) {
1053 /* skb was destructed already */
1054 skb = NULL;
1055 goto out_status;
1056 }
1057 /*
1058 * skb was dropped but not destructed yet;
1059 * let's treat it like congestion or err < 0
1060 */
1061 err = 0;
1062 }
Johann Baudy69e3c752009-05-18 22:11:22 -07001063 packet_increment_head(&po->tx_ring);
1064 len_sum += tp_len;
Joe Perchesf64f9e72009-11-29 16:55:45 -08001065 } while (likely((ph != NULL) ||
1066 ((!(msg->msg_flags & MSG_DONTWAIT)) &&
1067 (atomic_read(&po->tx_ring.pending))))
1068 );
Johann Baudy69e3c752009-05-18 22:11:22 -07001069
1070 err = len_sum;
1071 goto out_put;
1072
Johann Baudy69e3c752009-05-18 22:11:22 -07001073out_status:
1074 __packet_set_status(po, ph, status);
1075 kfree_skb(skb);
1076out_put:
1077 dev_put(dev);
1078out:
1079 mutex_unlock(&po->pg_vec_lock);
1080 return err;
1081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001083static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
1084 size_t reserve, size_t len,
1085 size_t linear, int noblock,
1086 int *err)
1087{
1088 struct sk_buff *skb;
1089
1090 /* Under a page? Don't bother with paged skb. */
1091 if (prepad + len < PAGE_SIZE || !linear)
1092 linear = len;
1093
1094 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
1095 err);
1096 if (!skb)
1097 return NULL;
1098
1099 skb_reserve(skb, reserve);
1100 skb_put(skb, linear);
1101 skb->data_len = len - linear;
1102 skb->len += len - linear;
1103
1104 return skb;
1105}
1106
Johann Baudy69e3c752009-05-18 22:11:22 -07001107static int packet_snd(struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 struct msghdr *msg, size_t len)
1109{
1110 struct sock *sk = sock->sk;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001111 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 struct sk_buff *skb;
1113 struct net_device *dev;
Al Viro0e11c912006-11-08 00:26:29 -08001114 __be16 proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 unsigned char *addr;
1116 int ifindex, err, reserve = 0;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001117 struct virtio_net_hdr vnet_hdr = { 0 };
1118 int offset = 0;
1119 int vnet_hdr_len;
1120 struct packet_sock *po = pkt_sk(sk);
1121 unsigned short gso_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001124 * Get and verify the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (saddr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 ifindex = po->ifindex;
1129 proto = po->num;
1130 addr = NULL;
1131 } else {
1132 err = -EINVAL;
1133 if (msg->msg_namelen < sizeof(struct sockaddr_ll))
1134 goto out;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001135 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
1136 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 ifindex = saddr->sll_ifindex;
1138 proto = saddr->sll_protocol;
1139 addr = saddr->sll_addr;
1140 }
1141
1142
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001143 dev = dev_get_by_index(sock_net(sk), ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 err = -ENXIO;
1145 if (dev == NULL)
1146 goto out_unlock;
1147 if (sock->type == SOCK_RAW)
1148 reserve = dev->hard_header_len;
1149
David S. Millerd5e76b02007-01-25 19:30:36 -08001150 err = -ENETDOWN;
1151 if (!(dev->flags & IFF_UP))
1152 goto out_unlock;
1153
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001154 if (po->has_vnet_hdr) {
1155 vnet_hdr_len = sizeof(vnet_hdr);
1156
1157 err = -EINVAL;
1158 if (len < vnet_hdr_len)
1159 goto out_unlock;
1160
1161 len -= vnet_hdr_len;
1162
1163 err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
1164 vnet_hdr_len);
1165 if (err < 0)
1166 goto out_unlock;
1167
1168 if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
1169 (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
1170 vnet_hdr.hdr_len))
1171 vnet_hdr.hdr_len = vnet_hdr.csum_start +
1172 vnet_hdr.csum_offset + 2;
1173
1174 err = -EINVAL;
1175 if (vnet_hdr.hdr_len > len)
1176 goto out_unlock;
1177
1178 if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
1179 switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
1180 case VIRTIO_NET_HDR_GSO_TCPV4:
1181 gso_type = SKB_GSO_TCPV4;
1182 break;
1183 case VIRTIO_NET_HDR_GSO_TCPV6:
1184 gso_type = SKB_GSO_TCPV6;
1185 break;
1186 case VIRTIO_NET_HDR_GSO_UDP:
1187 gso_type = SKB_GSO_UDP;
1188 break;
1189 default:
1190 goto out_unlock;
1191 }
1192
1193 if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
1194 gso_type |= SKB_GSO_TCP_ECN;
1195
1196 if (vnet_hdr.gso_size == 0)
1197 goto out_unlock;
1198
1199 }
1200 }
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 err = -EMSGSIZE;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001203 if (!gso_type && (len > dev->mtu+reserve))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 goto out_unlock;
1205
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001206 err = -ENOBUFS;
1207 skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev),
1208 LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len,
1209 msg->msg_flags & MSG_DONTWAIT, &err);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001210 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 goto out_unlock;
1212
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001213 skb_set_network_header(skb, reserve);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001215 err = -EINVAL;
1216 if (sock->type == SOCK_DGRAM &&
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001217 (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
Stephen Hemminger0c4e8582007-10-09 01:36:32 -07001218 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /* Returns -EFAULT on error */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001221 err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (err)
1223 goto out_free;
Oliver Hartkopp2244d072010-08-17 08:59:14 +00001224 err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
Richard Cochraned85b562010-04-07 22:41:28 +00001225 if (err < 0)
1226 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 skb->protocol = proto;
1229 skb->dev = dev;
1230 skb->priority = sk->sk_priority;
Eric Dumazet2d37a182009-10-01 19:14:46 +00001231 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001233 if (po->has_vnet_hdr) {
1234 if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
1235 if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
1236 vnet_hdr.csum_offset)) {
1237 err = -EINVAL;
1238 goto out_free;
1239 }
1240 }
1241
1242 skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
1243 skb_shinfo(skb)->gso_type = gso_type;
1244
1245 /* Header must be checked, and gso_segs computed. */
1246 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1247 skb_shinfo(skb)->gso_segs = 0;
1248
1249 len += vnet_hdr_len;
1250 }
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /*
1253 * Now send it
1254 */
1255
1256 err = dev_queue_xmit(skb);
1257 if (err > 0 && (err = net_xmit_errno(err)) != 0)
1258 goto out_unlock;
1259
1260 dev_put(dev);
1261
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001262 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264out_free:
1265 kfree_skb(skb);
1266out_unlock:
1267 if (dev)
1268 dev_put(dev);
1269out:
1270 return err;
1271}
1272
Johann Baudy69e3c752009-05-18 22:11:22 -07001273static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
1274 struct msghdr *msg, size_t len)
1275{
Johann Baudy69e3c752009-05-18 22:11:22 -07001276 struct sock *sk = sock->sk;
1277 struct packet_sock *po = pkt_sk(sk);
1278 if (po->tx_ring.pg_vec)
1279 return tpacket_snd(po, msg);
1280 else
Johann Baudy69e3c752009-05-18 22:11:22 -07001281 return packet_snd(sock, msg, len);
1282}
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284/*
1285 * Close a PACKET socket. This is fairly simple. We immediately go
1286 * to 'closed' state and remove our protocol entry in the device list.
1287 */
1288
1289static int packet_release(struct socket *sock)
1290{
1291 struct sock *sk = sock->sk;
1292 struct packet_sock *po;
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08001293 struct net *net;
Johann Baudy69e3c752009-05-18 22:11:22 -07001294 struct tpacket_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 if (!sk)
1297 return 0;
1298
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001299 net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 po = pkt_sk(sk);
1301
stephen hemminger808f5112010-02-22 07:57:18 +00001302 spin_lock_bh(&net->packet.sklist_lock);
1303 sk_del_node_init_rcu(sk);
Eric Dumazet920de802008-11-24 00:09:29 -08001304 sock_prot_inuse_add(net, sk->sk_prot, -1);
stephen hemminger808f5112010-02-22 07:57:18 +00001305 spin_unlock_bh(&net->packet.sklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
stephen hemminger808f5112010-02-22 07:57:18 +00001307 spin_lock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (po->running) {
1309 /*
stephen hemminger808f5112010-02-22 07:57:18 +00001310 * Remove from protocol table
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 po->running = 0;
1313 po->num = 0;
stephen hemminger808f5112010-02-22 07:57:18 +00001314 __dev_remove_pack(&po->prot_hook);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 __sock_put(sk);
1316 }
stephen hemminger808f5112010-02-22 07:57:18 +00001317 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 packet_flush_mclist(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Johann Baudy69e3c752009-05-18 22:11:22 -07001321 memset(&req, 0, sizeof(req));
1322
1323 if (po->rx_ring.pg_vec)
1324 packet_set_ring(sk, &req, 1, 0);
1325
1326 if (po->tx_ring.pg_vec)
1327 packet_set_ring(sk, &req, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
stephen hemminger808f5112010-02-22 07:57:18 +00001329 synchronize_net();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /*
1331 * Now the socket is dead. No more input will appear.
1332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 sock_orphan(sk);
1334 sock->sk = NULL;
1335
1336 /* Purge queues */
1337
1338 skb_queue_purge(&sk->sk_receive_queue);
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001339 sk_refcnt_debug_release(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 sock_put(sk);
1342 return 0;
1343}
1344
1345/*
1346 * Attach a packet hook.
1347 */
1348
Al Viro0e11c912006-11-08 00:26:29 -08001349static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 struct packet_sock *po = pkt_sk(sk);
1352 /*
1353 * Detach an existing hook if present.
1354 */
1355
1356 lock_sock(sk);
1357
1358 spin_lock(&po->bind_lock);
1359 if (po->running) {
1360 __sock_put(sk);
1361 po->running = 0;
1362 po->num = 0;
1363 spin_unlock(&po->bind_lock);
1364 dev_remove_pack(&po->prot_hook);
1365 spin_lock(&po->bind_lock);
1366 }
1367
1368 po->num = protocol;
1369 po->prot_hook.type = protocol;
1370 po->prot_hook.dev = dev;
1371
1372 po->ifindex = dev ? dev->ifindex : 0;
1373
1374 if (protocol == 0)
1375 goto out_unlock;
1376
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001377 if (!dev || (dev->flags & IFF_UP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 dev_add_pack(&po->prot_hook);
1379 sock_hold(sk);
1380 po->running = 1;
Urs Thuermannbe85d4a2007-11-12 21:05:20 -08001381 } else {
1382 sk->sk_err = ENETDOWN;
1383 if (!sock_flag(sk, SOCK_DEAD))
1384 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386
1387out_unlock:
1388 spin_unlock(&po->bind_lock);
1389 release_sock(sk);
1390 return 0;
1391}
1392
1393/*
1394 * Bind a packet socket to a device
1395 */
1396
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001397static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
1398 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001400 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 char name[15];
1402 struct net_device *dev;
1403 int err = -ENODEV;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /*
1406 * Check legality
1407 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001408
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001409 if (addr_len != sizeof(struct sockaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001411 strlcpy(name, uaddr->sa_data, sizeof(name));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001413 dev = dev_get_by_name(sock_net(sk), name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (dev) {
1415 err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
1416 dev_put(dev);
1417 }
1418 return err;
1419}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
1422{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001423 struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
1424 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 struct net_device *dev = NULL;
1426 int err;
1427
1428
1429 /*
1430 * Check legality
1431 */
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (addr_len < sizeof(struct sockaddr_ll))
1434 return -EINVAL;
1435 if (sll->sll_family != AF_PACKET)
1436 return -EINVAL;
1437
1438 if (sll->sll_ifindex) {
1439 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001440 dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (dev == NULL)
1442 goto out;
1443 }
1444 err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
1445 if (dev)
1446 dev_put(dev);
1447
1448out:
1449 return err;
1450}
1451
1452static struct proto packet_proto = {
1453 .name = "PACKET",
1454 .owner = THIS_MODULE,
1455 .obj_size = sizeof(struct packet_sock),
1456};
1457
1458/*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001459 * Create a packet of type SOCK_PACKET.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 */
1461
Eric Paris3f378b62009-11-05 22:18:14 -08001462static int packet_create(struct net *net, struct socket *sock, int protocol,
1463 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 struct sock *sk;
1466 struct packet_sock *po;
Al Viro0e11c912006-11-08 00:26:29 -08001467 __be16 proto = (__force __be16)protocol; /* weird, but documented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 int err;
1469
1470 if (!capable(CAP_NET_RAW))
1471 return -EPERM;
David S. Millerbe020972007-05-29 13:16:31 -07001472 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
1473 sock->type != SOCK_PACKET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return -ESOCKTNOSUPPORT;
1475
1476 sock->state = SS_UNCONNECTED;
1477
1478 err = -ENOBUFS;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001479 sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (sk == NULL)
1481 goto out;
1482
1483 sock->ops = &packet_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (sock->type == SOCK_PACKET)
1485 sock->ops = &packet_ops_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 sock_init_data(sock, sk);
1488
1489 po = pkt_sk(sk);
1490 sk->sk_family = PF_PACKET;
Al Viro0e11c912006-11-08 00:26:29 -08001491 po->num = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 sk->sk_destruct = packet_sock_destruct;
Pavel Emelyanov17ab56a2007-11-10 21:38:48 -08001494 sk_refcnt_debug_inc(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 /*
1497 * Attach a protocol block
1498 */
1499
1500 spin_lock_init(&po->bind_lock);
Herbert Xu905db442009-01-30 14:12:06 -08001501 mutex_init(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 po->prot_hook.func = packet_rcv;
David S. Millerbe020972007-05-29 13:16:31 -07001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (sock->type == SOCK_PACKET)
1505 po->prot_hook.func = packet_rcv_spkt;
David S. Millerbe020972007-05-29 13:16:31 -07001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 po->prot_hook.af_packet_priv = sk;
1508
Al Viro0e11c912006-11-08 00:26:29 -08001509 if (proto) {
1510 po->prot_hook.type = proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 dev_add_pack(&po->prot_hook);
1512 sock_hold(sk);
1513 po->running = 1;
1514 }
1515
stephen hemminger808f5112010-02-22 07:57:18 +00001516 spin_lock_bh(&net->packet.sklist_lock);
1517 sk_add_node_rcu(sk, &net->packet.sklist);
Eric Dumazet36804532008-11-19 14:25:35 -08001518 sock_prot_inuse_add(net, &packet_proto, 1);
stephen hemminger808f5112010-02-22 07:57:18 +00001519 spin_unlock_bh(&net->packet.sklist_lock);
1520
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522out:
1523 return err;
1524}
1525
Richard Cochraned85b562010-04-07 22:41:28 +00001526static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
1527{
1528 struct sock_exterr_skb *serr;
1529 struct sk_buff *skb, *skb2;
1530 int copied, err;
1531
1532 err = -EAGAIN;
1533 skb = skb_dequeue(&sk->sk_error_queue);
1534 if (skb == NULL)
1535 goto out;
1536
1537 copied = skb->len;
1538 if (copied > len) {
1539 msg->msg_flags |= MSG_TRUNC;
1540 copied = len;
1541 }
1542 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1543 if (err)
1544 goto out_free_skb;
1545
1546 sock_recv_timestamp(msg, sk, skb);
1547
1548 serr = SKB_EXT_ERR(skb);
1549 put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
1550 sizeof(serr->ee), &serr->ee);
1551
1552 msg->msg_flags |= MSG_ERRQUEUE;
1553 err = copied;
1554
1555 /* Reset and regenerate socket error */
1556 spin_lock_bh(&sk->sk_error_queue.lock);
1557 sk->sk_err = 0;
1558 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
1559 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
1560 spin_unlock_bh(&sk->sk_error_queue.lock);
1561 sk->sk_error_report(sk);
1562 } else
1563 spin_unlock_bh(&sk->sk_error_queue.lock);
1564
1565out_free_skb:
1566 kfree_skb(skb);
1567out:
1568 return err;
1569}
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571/*
1572 * Pull a packet from our receive queue and hand it to the user.
1573 * If necessary we block.
1574 */
1575
1576static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
1577 struct msghdr *msg, size_t len, int flags)
1578{
1579 struct sock *sk = sock->sk;
1580 struct sk_buff *skb;
1581 int copied, err;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001582 struct sockaddr_ll *sll;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001583 int vnet_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 err = -EINVAL;
Richard Cochraned85b562010-04-07 22:41:28 +00001586 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 goto out;
1588
1589#if 0
1590 /* What error should we return now? EUNATTACH? */
1591 if (pkt_sk(sk)->ifindex < 0)
1592 return -ENODEV;
1593#endif
1594
Richard Cochraned85b562010-04-07 22:41:28 +00001595 if (flags & MSG_ERRQUEUE) {
1596 err = packet_recv_error(sk, msg, len);
1597 goto out;
1598 }
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 * Call the generic datagram receiver. This handles all sorts
1602 * of horrible races and re-entrancy so we can forget about it
1603 * in the protocol layers.
1604 *
1605 * Now it will return ENETDOWN, if device have just gone down,
1606 * but then it will block.
1607 */
1608
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001609 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 /*
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001612 * An error occurred so return it. Because skb_recv_datagram()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 * handles the blocking we don't see and worry about blocking
1614 * retries.
1615 */
1616
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08001617 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 goto out;
1619
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001620 if (pkt_sk(sk)->has_vnet_hdr) {
1621 struct virtio_net_hdr vnet_hdr = { 0 };
1622
1623 err = -EINVAL;
1624 vnet_hdr_len = sizeof(vnet_hdr);
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001625 if (len < vnet_hdr_len)
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001626 goto out_free;
1627
Mariusz Kozlowski1f18b712010-11-08 11:58:45 +00001628 len -= vnet_hdr_len;
1629
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001630 if (skb_is_gso(skb)) {
1631 struct skb_shared_info *sinfo = skb_shinfo(skb);
1632
1633 /* This is a hint as to how much should be linear. */
1634 vnet_hdr.hdr_len = skb_headlen(skb);
1635 vnet_hdr.gso_size = sinfo->gso_size;
1636 if (sinfo->gso_type & SKB_GSO_TCPV4)
1637 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
1638 else if (sinfo->gso_type & SKB_GSO_TCPV6)
1639 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
1640 else if (sinfo->gso_type & SKB_GSO_UDP)
1641 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
1642 else if (sinfo->gso_type & SKB_GSO_FCOE)
1643 goto out_free;
1644 else
1645 BUG();
1646 if (sinfo->gso_type & SKB_GSO_TCP_ECN)
1647 vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
1648 } else
1649 vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
1650
1651 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1652 vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
1653 vnet_hdr.csum_start = skb->csum_start -
1654 skb_headroom(skb);
1655 vnet_hdr.csum_offset = skb->csum_offset;
1656 } /* else everything is zero */
1657
1658 err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
1659 vnet_hdr_len);
1660 if (err < 0)
1661 goto out_free;
1662 }
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /*
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001665 * If the address length field is there to be filled in, we fill
1666 * it in now.
1667 */
1668
Herbert Xuffbc6112007-02-04 23:33:10 -08001669 sll = &PACKET_SKB_CB(skb)->sa.ll;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001670 if (sock->type == SOCK_PACKET)
1671 msg->msg_namelen = sizeof(struct sockaddr_pkt);
1672 else
1673 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
1674
1675 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 * You lose any data beyond the buffer you gave. If it worries a
1677 * user program they can ask the device for its MTU anyway.
1678 */
1679
1680 copied = skb->len;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001681 if (copied > len) {
1682 copied = len;
1683 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
1685
1686 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1687 if (err)
1688 goto out_free;
1689
Neil Horman3b885782009-10-12 13:26:31 -07001690 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 if (msg->msg_name)
Herbert Xuffbc6112007-02-04 23:33:10 -08001693 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
1694 msg->msg_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Herbert Xu8dc41942007-02-04 23:31:32 -08001696 if (pkt_sk(sk)->auxdata) {
Herbert Xuffbc6112007-02-04 23:33:10 -08001697 struct tpacket_auxdata aux;
1698
1699 aux.tp_status = TP_STATUS_USER;
1700 if (skb->ip_summed == CHECKSUM_PARTIAL)
1701 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
1702 aux.tp_len = PACKET_SKB_CB(skb)->origlen;
1703 aux.tp_snaplen = skb->len;
1704 aux.tp_mac = 0;
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001705 aux.tp_net = skb_network_offset(skb);
Eric Dumazet05423b22009-10-26 18:40:35 -07001706 aux.tp_vlan_tci = vlan_tx_tag_get(skb);
Herbert Xuffbc6112007-02-04 23:33:10 -08001707
1708 put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
Herbert Xu8dc41942007-02-04 23:31:32 -08001709 }
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /*
1712 * Free or return the buffer as appropriate. Again this
1713 * hides all the races and re-entrancy issues from us.
1714 */
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001715 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717out_free:
1718 skb_free_datagram(sk, skb);
1719out:
1720 return err;
1721}
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1724 int *uaddr_len, int peer)
1725{
1726 struct net_device *dev;
1727 struct sock *sk = sock->sk;
1728
1729 if (peer)
1730 return -EOPNOTSUPP;
1731
1732 uaddr->sa_family = AF_PACKET;
Eric Dumazet654d1f82009-11-02 10:43:32 +01001733 rcu_read_lock();
1734 dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
1735 if (dev)
Vasiliy Kulikov67286642010-11-10 12:09:10 -08001736 strncpy(uaddr->sa_data, dev->name, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001737 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 memset(uaddr->sa_data, 0, 14);
Eric Dumazet654d1f82009-11-02 10:43:32 +01001739 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 *uaddr_len = sizeof(*uaddr);
1741
1742 return 0;
1743}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1746 int *uaddr_len, int peer)
1747{
1748 struct net_device *dev;
1749 struct sock *sk = sock->sk;
1750 struct packet_sock *po = pkt_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001751 DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 if (peer)
1754 return -EOPNOTSUPP;
1755
1756 sll->sll_family = AF_PACKET;
1757 sll->sll_ifindex = po->ifindex;
1758 sll->sll_protocol = po->num;
Vasiliy Kulikov67286642010-11-10 12:09:10 -08001759 sll->sll_pkttype = 0;
Eric Dumazet654d1f82009-11-02 10:43:32 +01001760 rcu_read_lock();
1761 dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 if (dev) {
1763 sll->sll_hatype = dev->type;
1764 sll->sll_halen = dev->addr_len;
1765 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 } else {
1767 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
1768 sll->sll_halen = 0;
1769 }
Eric Dumazet654d1f82009-11-02 10:43:32 +01001770 rcu_read_unlock();
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001771 *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 return 0;
1774}
1775
Wang Chen2aeb0b82008-07-14 20:49:46 -07001776static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
1777 int what)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
1779 switch (i->type) {
1780 case PACKET_MR_MULTICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00001781 if (i->alen != dev->addr_len)
1782 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (what > 0)
Jiri Pirko22bedad2010-04-01 21:22:57 +00001784 return dev_mc_add(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 else
Jiri Pirko22bedad2010-04-01 21:22:57 +00001786 return dev_mc_del(dev, i->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 break;
1788 case PACKET_MR_PROMISC:
Wang Chen2aeb0b82008-07-14 20:49:46 -07001789 return dev_set_promiscuity(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 break;
1791 case PACKET_MR_ALLMULTI:
Wang Chen2aeb0b82008-07-14 20:49:46 -07001792 return dev_set_allmulti(dev, what);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 break;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001794 case PACKET_MR_UNICAST:
Jiri Pirko11625632010-03-02 20:40:01 +00001795 if (i->alen != dev->addr_len)
1796 return -EINVAL;
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001797 if (what > 0)
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001798 return dev_uc_add(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001799 else
Jiri Pirkoa748ee22010-04-01 21:22:09 +00001800 return dev_uc_del(dev, i->addr);
Eric W. Biedermand95ed922009-05-19 18:27:17 +00001801 break;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001802 default:
1803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
Wang Chen2aeb0b82008-07-14 20:49:46 -07001805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
1808static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1809{
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001810 for ( ; i; i = i->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (i->ifindex == dev->ifindex)
1812 packet_dev_mc(dev, i, what);
1813 }
1814}
1815
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001816static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
1818 struct packet_sock *po = pkt_sk(sk);
1819 struct packet_mclist *ml, *i;
1820 struct net_device *dev;
1821 int err;
1822
1823 rtnl_lock();
1824
1825 err = -ENODEV;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001826 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (!dev)
1828 goto done;
1829
1830 err = -EINVAL;
Jiri Pirko11625632010-03-02 20:40:01 +00001831 if (mreq->mr_alen > dev->addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 goto done;
1833
1834 err = -ENOBUFS;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001835 i = kmalloc(sizeof(*i), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (i == NULL)
1837 goto done;
1838
1839 err = 0;
1840 for (ml = po->mclist; ml; ml = ml->next) {
1841 if (ml->ifindex == mreq->mr_ifindex &&
1842 ml->type == mreq->mr_type &&
1843 ml->alen == mreq->mr_alen &&
1844 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1845 ml->count++;
1846 /* Free the new element ... */
1847 kfree(i);
1848 goto done;
1849 }
1850 }
1851
1852 i->type = mreq->mr_type;
1853 i->ifindex = mreq->mr_ifindex;
1854 i->alen = mreq->mr_alen;
1855 memcpy(i->addr, mreq->mr_address, i->alen);
1856 i->count = 1;
1857 i->next = po->mclist;
1858 po->mclist = i;
Wang Chen2aeb0b82008-07-14 20:49:46 -07001859 err = packet_dev_mc(dev, i, 1);
1860 if (err) {
1861 po->mclist = i->next;
1862 kfree(i);
1863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865done:
1866 rtnl_unlock();
1867 return err;
1868}
1869
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001870static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 struct packet_mclist *ml, **mlp;
1873
1874 rtnl_lock();
1875
1876 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
1877 if (ml->ifindex == mreq->mr_ifindex &&
1878 ml->type == mreq->mr_type &&
1879 ml->alen == mreq->mr_alen &&
1880 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1881 if (--ml->count == 0) {
1882 struct net_device *dev;
1883 *mlp = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00001884 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
1885 if (dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 kfree(ml);
1888 }
1889 rtnl_unlock();
1890 return 0;
1891 }
1892 }
1893 rtnl_unlock();
1894 return -EADDRNOTAVAIL;
1895}
1896
1897static void packet_flush_mclist(struct sock *sk)
1898{
1899 struct packet_sock *po = pkt_sk(sk);
1900 struct packet_mclist *ml;
1901
1902 if (!po->mclist)
1903 return;
1904
1905 rtnl_lock();
1906 while ((ml = po->mclist) != NULL) {
1907 struct net_device *dev;
1908
1909 po->mclist = ml->next;
Eric Dumazetad959e72009-10-16 06:38:46 +00001910 dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
1911 if (dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 packet_dev_mc(dev, ml, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 kfree(ml);
1914 }
1915 rtnl_unlock();
1916}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918static int
David S. Millerb7058842009-09-30 16:12:20 -07001919packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 struct sock *sk = sock->sk;
Herbert Xu8dc41942007-02-04 23:31:32 -08001922 struct packet_sock *po = pkt_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 int ret;
1924
1925 if (level != SOL_PACKET)
1926 return -ENOPROTOOPT;
1927
Johann Baudy69e3c752009-05-18 22:11:22 -07001928 switch (optname) {
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09001929 case PACKET_ADD_MEMBERSHIP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 case PACKET_DROP_MEMBERSHIP:
1931 {
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001932 struct packet_mreq_max mreq;
1933 int len = optlen;
1934 memset(&mreq, 0, sizeof(mreq));
1935 if (len < sizeof(struct packet_mreq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return -EINVAL;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001937 if (len > sizeof(mreq))
1938 len = sizeof(mreq);
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001939 if (copy_from_user(&mreq, optval, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return -EFAULT;
Eric W. Biederman0fb375f2005-09-21 00:11:37 -07001941 if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
1942 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (optname == PACKET_ADD_MEMBERSHIP)
1944 ret = packet_mc_add(sk, &mreq);
1945 else
1946 ret = packet_mc_drop(sk, &mreq);
1947 return ret;
1948 }
David S. Millera2efcfa2007-05-29 13:12:50 -07001949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 case PACKET_RX_RING:
Johann Baudy69e3c752009-05-18 22:11:22 -07001951 case PACKET_TX_RING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 {
1953 struct tpacket_req req;
1954
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001955 if (optlen < sizeof(req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return -EINVAL;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08001957 if (pkt_sk(sk)->has_vnet_hdr)
1958 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001959 if (copy_from_user(&req, optval, sizeof(req)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return -EFAULT;
Johann Baudy69e3c752009-05-18 22:11:22 -07001961 return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
1963 case PACKET_COPY_THRESH:
1964 {
1965 int val;
1966
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001967 if (optlen != sizeof(val))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return -EINVAL;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00001969 if (copy_from_user(&val, optval, sizeof(val)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 return -EFAULT;
1971
1972 pkt_sk(sk)->copy_thresh = val;
1973 return 0;
1974 }
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001975 case PACKET_VERSION:
1976 {
1977 int val;
1978
1979 if (optlen != sizeof(val))
1980 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07001981 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardybbd6ef82008-07-14 22:50:15 -07001982 return -EBUSY;
1983 if (copy_from_user(&val, optval, sizeof(val)))
1984 return -EFAULT;
1985 switch (val) {
1986 case TPACKET_V1:
1987 case TPACKET_V2:
1988 po->tp_version = val;
1989 return 0;
1990 default:
1991 return -EINVAL;
1992 }
1993 }
Patrick McHardy89133362008-07-18 18:05:19 -07001994 case PACKET_RESERVE:
1995 {
1996 unsigned int val;
1997
1998 if (optlen != sizeof(val))
1999 return -EINVAL;
Johann Baudy69e3c752009-05-18 22:11:22 -07002000 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
Patrick McHardy89133362008-07-18 18:05:19 -07002001 return -EBUSY;
2002 if (copy_from_user(&val, optval, sizeof(val)))
2003 return -EFAULT;
2004 po->tp_reserve = val;
2005 return 0;
2006 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002007 case PACKET_LOSS:
2008 {
2009 unsigned int val;
2010
2011 if (optlen != sizeof(val))
2012 return -EINVAL;
2013 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2014 return -EBUSY;
2015 if (copy_from_user(&val, optval, sizeof(val)))
2016 return -EFAULT;
2017 po->tp_loss = !!val;
2018 return 0;
2019 }
Herbert Xu8dc41942007-02-04 23:31:32 -08002020 case PACKET_AUXDATA:
2021 {
2022 int val;
2023
2024 if (optlen < sizeof(val))
2025 return -EINVAL;
2026 if (copy_from_user(&val, optval, sizeof(val)))
2027 return -EFAULT;
2028
2029 po->auxdata = !!val;
2030 return 0;
2031 }
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002032 case PACKET_ORIGDEV:
2033 {
2034 int val;
2035
2036 if (optlen < sizeof(val))
2037 return -EINVAL;
2038 if (copy_from_user(&val, optval, sizeof(val)))
2039 return -EFAULT;
2040
2041 po->origdev = !!val;
2042 return 0;
2043 }
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002044 case PACKET_VNET_HDR:
2045 {
2046 int val;
2047
2048 if (sock->type != SOCK_RAW)
2049 return -EINVAL;
2050 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
2051 return -EBUSY;
2052 if (optlen < sizeof(val))
2053 return -EINVAL;
2054 if (copy_from_user(&val, optval, sizeof(val)))
2055 return -EFAULT;
2056
2057 po->has_vnet_hdr = !!val;
2058 return 0;
2059 }
Scott McMillan614f60f2010-06-02 05:53:56 -07002060 case PACKET_TIMESTAMP:
2061 {
2062 int val;
2063
2064 if (optlen != sizeof(val))
2065 return -EINVAL;
2066 if (copy_from_user(&val, optval, sizeof(val)))
2067 return -EFAULT;
2068
2069 po->tp_tstamp = val;
2070 return 0;
2071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 default:
2073 return -ENOPROTOOPT;
2074 }
2075}
2076
2077static int packet_getsockopt(struct socket *sock, int level, int optname,
2078 char __user *optval, int __user *optlen)
2079{
2080 int len;
Herbert Xu8dc41942007-02-04 23:31:32 -08002081 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 struct sock *sk = sock->sk;
2083 struct packet_sock *po = pkt_sk(sk);
Herbert Xu8dc41942007-02-04 23:31:32 -08002084 void *data;
2085 struct tpacket_stats st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 if (level != SOL_PACKET)
2088 return -ENOPROTOOPT;
2089
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002090 if (get_user(len, optlen))
2091 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 if (len < 0)
2094 return -EINVAL;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002095
Johann Baudy69e3c752009-05-18 22:11:22 -07002096 switch (optname) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 case PACKET_STATISTICS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (len > sizeof(struct tpacket_stats))
2099 len = sizeof(struct tpacket_stats);
2100 spin_lock_bh(&sk->sk_receive_queue.lock);
2101 st = po->stats;
2102 memset(&po->stats, 0, sizeof(st));
2103 spin_unlock_bh(&sk->sk_receive_queue.lock);
2104 st.tp_packets += st.tp_drops;
2105
Herbert Xu8dc41942007-02-04 23:31:32 -08002106 data = &st;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 break;
Herbert Xu8dc41942007-02-04 23:31:32 -08002108 case PACKET_AUXDATA:
2109 if (len > sizeof(int))
2110 len = sizeof(int);
2111 val = po->auxdata;
2112
2113 data = &val;
2114 break;
Peter P. Waskiewicz Jr80feaac2007-04-20 16:05:39 -07002115 case PACKET_ORIGDEV:
2116 if (len > sizeof(int))
2117 len = sizeof(int);
2118 val = po->origdev;
2119
2120 data = &val;
2121 break;
Sridhar Samudralabfd5f4a2010-02-04 20:24:10 -08002122 case PACKET_VNET_HDR:
2123 if (len > sizeof(int))
2124 len = sizeof(int);
2125 val = po->has_vnet_hdr;
2126
2127 data = &val;
2128 break;
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002129 case PACKET_VERSION:
2130 if (len > sizeof(int))
2131 len = sizeof(int);
2132 val = po->tp_version;
2133 data = &val;
2134 break;
2135 case PACKET_HDRLEN:
2136 if (len > sizeof(int))
2137 len = sizeof(int);
2138 if (copy_from_user(&val, optval, len))
2139 return -EFAULT;
2140 switch (val) {
2141 case TPACKET_V1:
2142 val = sizeof(struct tpacket_hdr);
2143 break;
2144 case TPACKET_V2:
2145 val = sizeof(struct tpacket2_hdr);
2146 break;
2147 default:
2148 return -EINVAL;
2149 }
2150 data = &val;
2151 break;
Patrick McHardy89133362008-07-18 18:05:19 -07002152 case PACKET_RESERVE:
2153 if (len > sizeof(unsigned int))
2154 len = sizeof(unsigned int);
2155 val = po->tp_reserve;
2156 data = &val;
2157 break;
Johann Baudy69e3c752009-05-18 22:11:22 -07002158 case PACKET_LOSS:
2159 if (len > sizeof(unsigned int))
2160 len = sizeof(unsigned int);
2161 val = po->tp_loss;
2162 data = &val;
2163 break;
Scott McMillan614f60f2010-06-02 05:53:56 -07002164 case PACKET_TIMESTAMP:
2165 if (len > sizeof(int))
2166 len = sizeof(int);
2167 val = po->tp_tstamp;
2168 data = &val;
2169 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 default:
2171 return -ENOPROTOOPT;
2172 }
2173
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002174 if (put_user(len, optlen))
2175 return -EFAULT;
Herbert Xu8dc41942007-02-04 23:31:32 -08002176 if (copy_to_user(optval, data, len))
2177 return -EFAULT;
Kris Katterjohn8ae55f02006-01-23 16:28:02 -08002178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181
2182static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
2183{
2184 struct sock *sk;
2185 struct hlist_node *node;
Jason Lunzad930652007-02-20 23:19:54 -08002186 struct net_device *dev = data;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002187 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
stephen hemminger808f5112010-02-22 07:57:18 +00002189 rcu_read_lock();
2190 sk_for_each_rcu(sk, node, &net->packet.sklist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 struct packet_sock *po = pkt_sk(sk);
2192
2193 switch (msg) {
2194 case NETDEV_UNREGISTER:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (po->mclist)
2196 packet_dev_mclist(dev, po->mclist, -1);
David S. Millera2efcfa2007-05-29 13:12:50 -07002197 /* fallthrough */
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 case NETDEV_DOWN:
2200 if (dev->ifindex == po->ifindex) {
2201 spin_lock(&po->bind_lock);
2202 if (po->running) {
2203 __dev_remove_pack(&po->prot_hook);
2204 __sock_put(sk);
2205 po->running = 0;
2206 sk->sk_err = ENETDOWN;
2207 if (!sock_flag(sk, SOCK_DEAD))
2208 sk->sk_error_report(sk);
2209 }
2210 if (msg == NETDEV_UNREGISTER) {
2211 po->ifindex = -1;
2212 po->prot_hook.dev = NULL;
2213 }
2214 spin_unlock(&po->bind_lock);
2215 }
2216 break;
2217 case NETDEV_UP:
stephen hemminger808f5112010-02-22 07:57:18 +00002218 if (dev->ifindex == po->ifindex) {
2219 spin_lock(&po->bind_lock);
2220 if (po->num && !po->running) {
2221 dev_add_pack(&po->prot_hook);
2222 sock_hold(sk);
2223 po->running = 1;
2224 }
2225 spin_unlock(&po->bind_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 break;
2228 }
2229 }
stephen hemminger808f5112010-02-22 07:57:18 +00002230 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return NOTIFY_DONE;
2232}
2233
2234
2235static int packet_ioctl(struct socket *sock, unsigned int cmd,
2236 unsigned long arg)
2237{
2238 struct sock *sk = sock->sk;
2239
Johann Baudy69e3c752009-05-18 22:11:22 -07002240 switch (cmd) {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002241 case SIOCOUTQ:
2242 {
2243 int amount = sk_wmem_alloc_get(sk);
Eric Dumazet31e6d362009-06-17 19:05:41 -07002244
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002245 return put_user(amount, (int __user *)arg);
2246 }
2247 case SIOCINQ:
2248 {
2249 struct sk_buff *skb;
2250 int amount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002252 spin_lock_bh(&sk->sk_receive_queue.lock);
2253 skb = skb_peek(&sk->sk_receive_queue);
2254 if (skb)
2255 amount = skb->len;
2256 spin_unlock_bh(&sk->sk_receive_queue.lock);
2257 return put_user(amount, (int __user *)arg);
2258 }
2259 case SIOCGSTAMP:
2260 return sock_get_timestamp(sk, (struct timeval __user *)arg);
2261 case SIOCGSTAMPNS:
2262 return sock_get_timestampns(sk, (struct timespec __user *)arg);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264#ifdef CONFIG_INET
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002265 case SIOCADDRT:
2266 case SIOCDELRT:
2267 case SIOCDARP:
2268 case SIOCGARP:
2269 case SIOCSARP:
2270 case SIOCGIFADDR:
2271 case SIOCSIFADDR:
2272 case SIOCGIFBRDADDR:
2273 case SIOCSIFBRDADDR:
2274 case SIOCGIFNETMASK:
2275 case SIOCSIFNETMASK:
2276 case SIOCGIFDSTADDR:
2277 case SIOCSIFDSTADDR:
2278 case SIOCSIFFLAGS:
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002279 return inet_dgram_ops.ioctl(sock, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280#endif
2281
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002282 default:
2283 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285 return 0;
2286}
2287
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002288static unsigned int packet_poll(struct file *file, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 poll_table *wait)
2290{
2291 struct sock *sk = sock->sk;
2292 struct packet_sock *po = pkt_sk(sk);
2293 unsigned int mask = datagram_poll(file, sock, wait);
2294
2295 spin_lock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002296 if (po->rx_ring.pg_vec) {
2297 if (!packet_previous_frame(po, &po->rx_ring, TP_STATUS_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 mask |= POLLIN | POLLRDNORM;
2299 }
2300 spin_unlock_bh(&sk->sk_receive_queue.lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002301 spin_lock_bh(&sk->sk_write_queue.lock);
2302 if (po->tx_ring.pg_vec) {
2303 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
2304 mask |= POLLOUT | POLLWRNORM;
2305 }
2306 spin_unlock_bh(&sk->sk_write_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return mask;
2308}
2309
2310
2311/* Dirty? Well, I still did not learn better way to account
2312 * for user mmaps.
2313 */
2314
2315static void packet_mm_open(struct vm_area_struct *vma)
2316{
2317 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002318 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (sk)
2322 atomic_inc(&pkt_sk(sk)->mapped);
2323}
2324
2325static void packet_mm_close(struct vm_area_struct *vma)
2326{
2327 struct file *file = vma->vm_file;
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002328 struct socket *sock = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (sk)
2332 atomic_dec(&pkt_sk(sk)->mapped);
2333}
2334
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002335static const struct vm_operations_struct packet_mmap_ops = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002336 .open = packet_mm_open,
2337 .close = packet_mm_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338};
2339
Neil Horman0e3125c2010-11-16 10:26:47 -08002340static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
2341 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
2343 int i;
2344
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002345 for (i = 0; i < len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002346 if (likely(pg_vec[i].buffer)) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002347 if (is_vmalloc_addr(pg_vec[i].buffer))
Neil Horman0e3125c2010-11-16 10:26:47 -08002348 vfree(pg_vec[i].buffer);
2349 else
2350 free_pages((unsigned long)pg_vec[i].buffer,
2351 order);
2352 pg_vec[i].buffer = NULL;
2353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355 kfree(pg_vec);
2356}
2357
Changli Gaoc56b4d92010-12-01 02:52:57 +00002358static inline char *alloc_one_pg_vec_page(unsigned long order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002359{
Neil Horman0e3125c2010-11-16 10:26:47 -08002360 char *buffer = NULL;
2361 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
2362 __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
Eric Dumazet719bfea2009-04-15 03:39:52 -07002363
Neil Horman0e3125c2010-11-16 10:26:47 -08002364 buffer = (char *) __get_free_pages(gfp_flags, order);
2365
2366 if (buffer)
2367 return buffer;
2368
2369 /*
2370 * __get_free_pages failed, fall back to vmalloc
2371 */
Eric Dumazetbbce5a52010-11-20 07:31:54 +00002372 buffer = vzalloc((1 << order) * PAGE_SIZE);
Neil Horman0e3125c2010-11-16 10:26:47 -08002373
2374 if (buffer)
2375 return buffer;
2376
2377 /*
2378 * vmalloc failed, lets dig into swap here
2379 */
Neil Horman0e3125c2010-11-16 10:26:47 -08002380 gfp_flags &= ~__GFP_NORETRY;
2381 buffer = (char *)__get_free_pages(gfp_flags, order);
2382 if (buffer)
2383 return buffer;
2384
2385 /*
2386 * complete and utter failure
2387 */
2388 return NULL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002389}
2390
Neil Horman0e3125c2010-11-16 10:26:47 -08002391static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002392{
2393 unsigned int block_nr = req->tp_block_nr;
Neil Horman0e3125c2010-11-16 10:26:47 -08002394 struct pgv *pg_vec;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002395 int i;
2396
Neil Horman0e3125c2010-11-16 10:26:47 -08002397 pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002398 if (unlikely(!pg_vec))
2399 goto out;
2400
2401 for (i = 0; i < block_nr; i++) {
Changli Gaoc56b4d92010-12-01 02:52:57 +00002402 pg_vec[i].buffer = alloc_one_pg_vec_page(order);
Neil Horman0e3125c2010-11-16 10:26:47 -08002403 if (unlikely(!pg_vec[i].buffer))
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002404 goto out_free_pgvec;
2405 }
2406
2407out:
2408 return pg_vec;
2409
2410out_free_pgvec:
2411 free_pg_vec(pg_vec, order, block_nr);
2412 pg_vec = NULL;
2413 goto out;
2414}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Johann Baudy69e3c752009-05-18 22:11:22 -07002416static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
2417 int closing, int tx_ring)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Neil Horman0e3125c2010-11-16 10:26:47 -08002419 struct pgv *pg_vec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 struct packet_sock *po = pkt_sk(sk);
Al Viro0e11c912006-11-08 00:26:29 -08002421 int was_running, order = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002422 struct packet_ring_buffer *rb;
2423 struct sk_buff_head *rb_queue;
Al Viro0e11c912006-11-08 00:26:29 -08002424 __be16 num;
Johann Baudy69e3c752009-05-18 22:11:22 -07002425 int err;
2426
2427 rb = tx_ring ? &po->tx_ring : &po->rx_ring;
2428 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
2429
2430 err = -EBUSY;
2431 if (!closing) {
2432 if (atomic_read(&po->mapped))
2433 goto out;
2434 if (atomic_read(&rb->pending))
2435 goto out;
2436 }
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (req->tp_block_nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 /* Sanity tests and some calculations */
Johann Baudy69e3c752009-05-18 22:11:22 -07002440 err = -EBUSY;
2441 if (unlikely(rb->pg_vec))
2442 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Patrick McHardybbd6ef82008-07-14 22:50:15 -07002444 switch (po->tp_version) {
2445 case TPACKET_V1:
2446 po->tp_hdrlen = TPACKET_HDRLEN;
2447 break;
2448 case TPACKET_V2:
2449 po->tp_hdrlen = TPACKET2_HDRLEN;
2450 break;
2451 }
2452
Johann Baudy69e3c752009-05-18 22:11:22 -07002453 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002454 if (unlikely((int)req->tp_block_size <= 0))
Johann Baudy69e3c752009-05-18 22:11:22 -07002455 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002456 if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002457 goto out;
Patrick McHardy89133362008-07-18 18:05:19 -07002458 if (unlikely(req->tp_frame_size < po->tp_hdrlen +
Johann Baudy69e3c752009-05-18 22:11:22 -07002459 po->tp_reserve))
2460 goto out;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002461 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
Johann Baudy69e3c752009-05-18 22:11:22 -07002462 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Johann Baudy69e3c752009-05-18 22:11:22 -07002464 rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
2465 if (unlikely(rb->frames_per_block <= 0))
2466 goto out;
2467 if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
2468 req->tp_frame_nr))
2469 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 err = -ENOMEM;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002472 order = get_order(req->tp_block_size);
2473 pg_vec = alloc_pg_vec(req, order);
2474 if (unlikely(!pg_vec))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002476 }
2477 /* Done */
2478 else {
2479 err = -EINVAL;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002480 if (unlikely(req->tp_frame_nr))
Johann Baudy69e3c752009-05-18 22:11:22 -07002481 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
2483
2484 lock_sock(sk);
2485
2486 /* Detach socket from network */
2487 spin_lock(&po->bind_lock);
2488 was_running = po->running;
2489 num = po->num;
2490 if (was_running) {
2491 __dev_remove_pack(&po->prot_hook);
2492 po->num = 0;
2493 po->running = 0;
2494 __sock_put(sk);
2495 }
2496 spin_unlock(&po->bind_lock);
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 synchronize_net();
2499
2500 err = -EBUSY;
Herbert Xu905db442009-01-30 14:12:06 -08002501 mutex_lock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (closing || atomic_read(&po->mapped) == 0) {
2503 err = 0;
Johann Baudy69e3c752009-05-18 22:11:22 -07002504 spin_lock_bh(&rb_queue->lock);
Changli Gaoc053fd92010-12-10 16:02:20 -08002505 swap(rb->pg_vec, pg_vec);
Johann Baudy69e3c752009-05-18 22:11:22 -07002506 rb->frame_max = (req->tp_frame_nr - 1);
2507 rb->head = 0;
2508 rb->frame_size = req->tp_frame_size;
2509 spin_unlock_bh(&rb_queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Changli Gaoc053fd92010-12-10 16:02:20 -08002511 swap(rb->pg_vec_order, order);
2512 swap(rb->pg_vec_len, req->tp_block_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Johann Baudy69e3c752009-05-18 22:11:22 -07002514 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
2515 po->prot_hook.func = (po->rx_ring.pg_vec) ?
2516 tpacket_rcv : packet_rcv;
2517 skb_queue_purge(rb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (atomic_read(&po->mapped))
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002519 pr_err("packet_mmap: vma is busy: %d\n",
2520 atomic_read(&po->mapped));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 }
Herbert Xu905db442009-01-30 14:12:06 -08002522 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 spin_lock(&po->bind_lock);
2525 if (was_running && !po->running) {
2526 sock_hold(sk);
2527 po->running = 1;
2528 po->num = num;
2529 dev_add_pack(&po->prot_hook);
2530 }
2531 spin_unlock(&po->bind_lock);
2532
2533 release_sock(sk);
2534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (pg_vec)
2536 free_pg_vec(pg_vec, order, req->tp_block_nr);
2537out:
2538 return err;
2539}
2540
Johann Baudy69e3c752009-05-18 22:11:22 -07002541static int packet_mmap(struct file *file, struct socket *sock,
2542 struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
2544 struct sock *sk = sock->sk;
2545 struct packet_sock *po = pkt_sk(sk);
Johann Baudy69e3c752009-05-18 22:11:22 -07002546 unsigned long size, expected_size;
2547 struct packet_ring_buffer *rb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 unsigned long start;
2549 int err = -EINVAL;
2550 int i;
2551
2552 if (vma->vm_pgoff)
2553 return -EINVAL;
2554
Herbert Xu905db442009-01-30 14:12:06 -08002555 mutex_lock(&po->pg_vec_lock);
Johann Baudy69e3c752009-05-18 22:11:22 -07002556
2557 expected_size = 0;
2558 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2559 if (rb->pg_vec) {
2560 expected_size += rb->pg_vec_len
2561 * rb->pg_vec_pages
2562 * PAGE_SIZE;
2563 }
2564 }
2565
2566 if (expected_size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 goto out;
Johann Baudy69e3c752009-05-18 22:11:22 -07002568
2569 size = vma->vm_end - vma->vm_start;
2570 if (size != expected_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 goto out;
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 start = vma->vm_start;
Johann Baudy69e3c752009-05-18 22:11:22 -07002574 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
2575 if (rb->pg_vec == NULL)
2576 continue;
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002577
Johann Baudy69e3c752009-05-18 22:11:22 -07002578 for (i = 0; i < rb->pg_vec_len; i++) {
Neil Horman0e3125c2010-11-16 10:26:47 -08002579 struct page *page;
2580 void *kaddr = rb->pg_vec[i].buffer;
Johann Baudy69e3c752009-05-18 22:11:22 -07002581 int pg_num;
2582
Changli Gaoc56b4d92010-12-01 02:52:57 +00002583 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
2584 page = pgv_to_page(kaddr);
Johann Baudy69e3c752009-05-18 22:11:22 -07002585 err = vm_insert_page(vma, start, page);
2586 if (unlikely(err))
2587 goto out;
2588 start += PAGE_SIZE;
Neil Horman0e3125c2010-11-16 10:26:47 -08002589 kaddr += PAGE_SIZE;
Johann Baudy69e3c752009-05-18 22:11:22 -07002590 }
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
Johann Baudy69e3c752009-05-18 22:11:22 -07002593
David S. Miller4ebf0ae2005-12-06 16:38:35 -08002594 atomic_inc(&po->mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 vma->vm_ops = &packet_mmap_ops;
2596 err = 0;
2597
2598out:
Herbert Xu905db442009-01-30 14:12:06 -08002599 mutex_unlock(&po->pg_vec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return err;
2601}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002603static const struct proto_ops packet_ops_spkt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 .family = PF_PACKET,
2605 .owner = THIS_MODULE,
2606 .release = packet_release,
2607 .bind = packet_bind_spkt,
2608 .connect = sock_no_connect,
2609 .socketpair = sock_no_socketpair,
2610 .accept = sock_no_accept,
2611 .getname = packet_getname_spkt,
2612 .poll = datagram_poll,
2613 .ioctl = packet_ioctl,
2614 .listen = sock_no_listen,
2615 .shutdown = sock_no_shutdown,
2616 .setsockopt = sock_no_setsockopt,
2617 .getsockopt = sock_no_getsockopt,
2618 .sendmsg = packet_sendmsg_spkt,
2619 .recvmsg = packet_recvmsg,
2620 .mmap = sock_no_mmap,
2621 .sendpage = sock_no_sendpage,
2622};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002624static const struct proto_ops packet_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 .family = PF_PACKET,
2626 .owner = THIS_MODULE,
2627 .release = packet_release,
2628 .bind = packet_bind,
2629 .connect = sock_no_connect,
2630 .socketpair = sock_no_socketpair,
2631 .accept = sock_no_accept,
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002632 .getname = packet_getname,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 .poll = packet_poll,
2634 .ioctl = packet_ioctl,
2635 .listen = sock_no_listen,
2636 .shutdown = sock_no_shutdown,
2637 .setsockopt = packet_setsockopt,
2638 .getsockopt = packet_getsockopt,
2639 .sendmsg = packet_sendmsg,
2640 .recvmsg = packet_recvmsg,
2641 .mmap = packet_mmap,
2642 .sendpage = sock_no_sendpage,
2643};
2644
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002645static const struct net_proto_family packet_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 .family = PF_PACKET,
2647 .create = packet_create,
2648 .owner = THIS_MODULE,
2649};
2650
2651static struct notifier_block packet_netdev_notifier = {
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002652 .notifier_call = packet_notifier,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653};
2654
2655#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger808f5112010-02-22 07:57:18 +00002658 __acquires(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
Denis V. Luneve372c4142007-11-19 22:31:54 -08002660 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00002661
2662 rcu_read_lock();
2663 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664}
2665
2666static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2667{
Herbert Xu1bf40952007-12-16 14:04:02 -08002668 struct net *net = seq_file_net(seq);
stephen hemminger808f5112010-02-22 07:57:18 +00002669 return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670}
2671
2672static void packet_seq_stop(struct seq_file *seq, void *v)
stephen hemminger808f5112010-02-22 07:57:18 +00002673 __releases(RCU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
stephen hemminger808f5112010-02-22 07:57:18 +00002675 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
YOSHIFUJI Hideaki1ce4f282007-02-09 23:25:10 +09002678static int packet_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
2680 if (v == SEQ_START_TOKEN)
2681 seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
2682 else {
Li Zefanb7ceabd2010-02-08 23:19:29 +00002683 struct sock *s = sk_entry(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 const struct packet_sock *po = pkt_sk(s);
2685
2686 seq_printf(seq,
2687 "%p %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
2688 s,
2689 atomic_read(&s->sk_refcnt),
2690 s->sk_type,
2691 ntohs(po->num),
2692 po->ifindex,
2693 po->running,
2694 atomic_read(&s->sk_rmem_alloc),
2695 sock_i_uid(s),
Eric Dumazet40d4e3d2009-07-21 21:57:59 +00002696 sock_i_ino(s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
2698
2699 return 0;
2700}
2701
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002702static const struct seq_operations packet_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 .start = packet_seq_start,
2704 .next = packet_seq_next,
2705 .stop = packet_seq_stop,
2706 .show = packet_seq_show,
2707};
2708
2709static int packet_seq_open(struct inode *inode, struct file *file)
2710{
Denis V. Luneve372c4142007-11-19 22:31:54 -08002711 return seq_open_net(inode, file, &packet_seq_ops,
2712 sizeof(struct seq_net_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
2714
Arjan van de Venda7071d2007-02-12 00:55:36 -08002715static const struct file_operations packet_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 .owner = THIS_MODULE,
2717 .open = packet_seq_open,
2718 .read = seq_read,
2719 .llseek = seq_lseek,
Denis V. Luneve372c4142007-11-19 22:31:54 -08002720 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721};
2722
2723#endif
2724
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002725static int __net_init packet_net_init(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002726{
stephen hemminger808f5112010-02-22 07:57:18 +00002727 spin_lock_init(&net->packet.sklist_lock);
Denis V. Lunev2aaef4e2007-12-11 04:19:54 -08002728 INIT_HLIST_HEAD(&net->packet.sklist);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002729
2730 if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
2731 return -ENOMEM;
2732
2733 return 0;
2734}
2735
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002736static void __net_exit packet_net_exit(struct net *net)
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002737{
2738 proc_net_remove(net, "packet");
2739}
2740
2741static struct pernet_operations packet_net_ops = {
2742 .init = packet_net_init,
2743 .exit = packet_net_exit,
2744};
2745
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747static void __exit packet_exit(void)
2748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 unregister_netdevice_notifier(&packet_netdev_notifier);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002750 unregister_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 sock_unregister(PF_PACKET);
2752 proto_unregister(&packet_proto);
2753}
2754
2755static int __init packet_init(void)
2756{
2757 int rc = proto_register(&packet_proto, 0);
2758
2759 if (rc != 0)
2760 goto out;
2761
2762 sock_register(&packet_family_ops);
Denis V. Lunevd12d01d2007-11-19 22:28:35 -08002763 register_pernet_subsys(&packet_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 register_netdevice_notifier(&packet_netdev_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765out:
2766 return rc;
2767}
2768
2769module_init(packet_init);
2770module_exit(packet_exit);
2771MODULE_LICENSE("GPL");
2772MODULE_ALIAS_NETPROTO(PF_PACKET);